Skip to content
This repository was archived by the owner on Apr 12, 2019. It is now read-only.

Commit 1b3739c

Browse files
cez81lunny
authored andcommitted
Add GetRepoSize implemented using count-objects (#40)
1 parent 5b41327 commit 1b3739c

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

repo.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import (
1111
"os"
1212
"path"
1313
"path/filepath"
14+
"strings"
1415
"time"
16+
17+
"github.com/Unknwon/com"
1518
)
1619

1720
// Repository represents a Git repository.
@@ -198,3 +201,63 @@ func MoveFile(repoPath, oldTreeName, newTreeName string) error {
198201
_, err := NewCommand("mv").AddArguments(oldTreeName, newTreeName).RunInDir(repoPath)
199202
return err
200203
}
204+
205+
// CountObject represents repository count objects report
206+
type CountObject struct {
207+
Count int64
208+
Size int64
209+
InPack int64
210+
Packs int64
211+
SizePack int64
212+
PrunePack int64
213+
Garbage int64
214+
SizeGarbage int64
215+
}
216+
217+
const (
218+
statCount = "count: "
219+
statSize = "size: "
220+
statInpack = "in-pack: "
221+
statPacks = "packs: "
222+
statSizePack = "size-pack: "
223+
statPrunePackage = "prune-package: "
224+
statGarbage = "garbage: "
225+
statSizeGarbage = "size-garbage: "
226+
)
227+
228+
// GetRepoSize returns disk consumption for repo in path
229+
func GetRepoSize(repoPath string) (*CountObject, error) {
230+
cmd := NewCommand("count-objects", "-v")
231+
stdout, err := cmd.RunInDir(repoPath)
232+
if err != nil {
233+
return nil, err
234+
}
235+
236+
return parseSize(stdout), nil
237+
}
238+
239+
// parseSize parses the output from count-objects and return a CountObject
240+
func parseSize(objects string) *CountObject {
241+
repoSize := new(CountObject)
242+
for _, line := range strings.Split(objects, "\n") {
243+
switch {
244+
case strings.HasPrefix(line, statCount):
245+
repoSize.Count = com.StrTo(line[7:]).MustInt64()
246+
case strings.HasPrefix(line, statSize):
247+
repoSize.Size = com.StrTo(line[6:]).MustInt64() * 1024
248+
case strings.HasPrefix(line, statInpack):
249+
repoSize.InPack = com.StrTo(line[9:]).MustInt64()
250+
case strings.HasPrefix(line, statPacks):
251+
repoSize.Packs = com.StrTo(line[7:]).MustInt64()
252+
case strings.HasPrefix(line, statSizePack):
253+
repoSize.SizePack = com.StrTo(line[11:]).MustInt64() * 1024
254+
case strings.HasPrefix(line, statPrunePackage):
255+
repoSize.PrunePack = com.StrTo(line[16:]).MustInt64()
256+
case strings.HasPrefix(line, statGarbage):
257+
repoSize.Garbage = com.StrTo(line[9:]).MustInt64()
258+
case strings.HasPrefix(line, statSizeGarbage):
259+
repoSize.SizeGarbage = com.StrTo(line[14:]).MustInt64() * 1024
260+
}
261+
}
262+
return repoSize
263+
}

0 commit comments

Comments
 (0)