mirror of
				https://github.com/go-gitea/gitea
				synced 2025-11-04 05:18:25 +00:00 
			
		
		
		
	Refactor Git command functions to use WithXXX methods instead of exposing RunOpts. This change simplifies reuse across gitrepo and improves consistency, encapsulation, and maintainability of command options. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
		
			
				
	
	
		
			38 lines
		
	
	
		
			980 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			38 lines
		
	
	
		
			980 B
		
	
	
	
		
			Go
		
	
	
	
	
	
// Copyright 2025 The Gitea Authors. All rights reserved.
 | 
						|
// SPDX-License-Identifier: MIT
 | 
						|
 | 
						|
package gitrepo
 | 
						|
 | 
						|
import (
 | 
						|
	"os"
 | 
						|
	"path/filepath"
 | 
						|
)
 | 
						|
 | 
						|
const notRegularFileMode = os.ModeSymlink | os.ModeNamedPipe | os.ModeSocket | os.ModeDevice | os.ModeCharDevice | os.ModeIrregular
 | 
						|
 | 
						|
// CalcRepositorySize returns the disk consumption for a given path
 | 
						|
func CalcRepositorySize(repo Repository) (int64, error) {
 | 
						|
	var size int64
 | 
						|
	err := filepath.WalkDir(repoPath(repo), func(_ string, entry os.DirEntry, err error) error {
 | 
						|
		if os.IsNotExist(err) { // ignore the error because some files (like temp/lock file) may be deleted during traversing.
 | 
						|
			return nil
 | 
						|
		} else if err != nil {
 | 
						|
			return err
 | 
						|
		}
 | 
						|
		if entry.IsDir() {
 | 
						|
			return nil
 | 
						|
		}
 | 
						|
		info, err := entry.Info()
 | 
						|
		if os.IsNotExist(err) { // ignore the error as above
 | 
						|
			return nil
 | 
						|
		} else if err != nil {
 | 
						|
			return err
 | 
						|
		}
 | 
						|
		if (info.Mode() & notRegularFileMode) == 0 {
 | 
						|
			size += info.Size()
 | 
						|
		}
 | 
						|
		return nil
 | 
						|
	})
 | 
						|
	return size, err
 | 
						|
}
 |