Skip to content

Commit ee1d64d

Browse files
authored
Stop using git count-objects and use raw directory size for repository (#8848)
* Migrate from git count-objects to a raw directory size * As per @guillep2k ignore unusual files
1 parent 44ec9b9 commit ee1d64d

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

models/migrations/v28.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ func addRepoSize(x *xorm.Engine) (err error) {
6060
}
6161

6262
repoPath := filepath.Join(setting.RepoRootPath, strings.ToLower(user.Name), strings.ToLower(repo.Name)) + ".git"
63-
countObject, err := git.GetRepoSize(repoPath)
63+
countObject, err := git.CountObjects(repoPath)
6464
if err != nil {
65-
log.Warn("GetRepoSize: %v", err)
65+
log.Warn("CountObjects: %v", err)
6666
continue
6767
}
6868

models/repo.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
api "code.gitea.io/gitea/modules/structs"
3737
"code.gitea.io/gitea/modules/sync"
3838
"code.gitea.io/gitea/modules/timeutil"
39+
"code.gitea.io/gitea/modules/util"
3940

4041
"github.com/mcuadros/go-version"
4142
"github.com/unknwon/com"
@@ -708,17 +709,17 @@ func (repo *Repository) IsOwnedBy(userID int64) bool {
708709
}
709710

710711
func (repo *Repository) updateSize(e Engine) error {
711-
repoInfoSize, err := git.GetRepoSize(repo.repoPath(e))
712+
size, err := util.GetDirectorySize(repo.repoPath(e))
712713
if err != nil {
713714
return fmt.Errorf("UpdateSize: %v", err)
714715
}
715716

716-
repo.Size = repoInfoSize.Size + repoInfoSize.SizePack
717+
repo.Size = size
717718
_, err = e.ID(repo.ID).Cols("size").Update(repo)
718719
return err
719720
}
720721

721-
// UpdateSize updates the repository size, calculating it using git.GetRepoSize
722+
// UpdateSize updates the repository size, calculating it using util.GetDirectorySize
722723
func (repo *Repository) UpdateSize() error {
723724
return repo.updateSize(x)
724725
}

modules/git/repo.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ const (
304304
statSizeGarbage = "size-garbage: "
305305
)
306306

307-
// GetRepoSize returns disk consumption for repo in path
308-
func GetRepoSize(repoPath string) (*CountObject, error) {
307+
// CountObjects returns the results of git count-objects on the repoPath
308+
func CountObjects(repoPath string) (*CountObject, error) {
309309
cmd := NewCommand("count-objects", "-v")
310310
stdout, err := cmd.RunInDir(repoPath)
311311
if err != nil {

modules/util/path.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
package util
66

7-
import "path/filepath"
7+
import (
8+
"os"
9+
"path/filepath"
10+
)
811

912
// EnsureAbsolutePath ensure that a path is absolute, making it
1013
// relative to absoluteBase if necessary
@@ -14,3 +17,17 @@ func EnsureAbsolutePath(path string, absoluteBase string) string {
1417
}
1518
return filepath.Join(absoluteBase, path)
1619
}
20+
21+
const notRegularFileMode os.FileMode = os.ModeDir | os.ModeSymlink | os.ModeNamedPipe | os.ModeSocket | os.ModeDevice | os.ModeCharDevice | os.ModeIrregular
22+
23+
// GetDirectorySize returns the dumb disk consumption for a given path
24+
func GetDirectorySize(path string) (int64, error) {
25+
var size int64
26+
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
27+
if info != nil && (info.Mode()&notRegularFileMode) == 0 {
28+
size += info.Size()
29+
}
30+
return err
31+
})
32+
return size, err
33+
}

0 commit comments

Comments
 (0)