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

Commit ec90a8c

Browse files
committed
Add GetRepoSize implemented using count-objects
1 parent 5b41327 commit ec90a8c

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

repo.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import (
1111
"os"
1212
"path"
1313
"path/filepath"
14+
"strings"
1415
"time"
16+
17+
"github.com/Unknwon/com"
18+
"github.com/mcuadros/go-version"
1519
)
1620

1721
// Repository represents a Git repository.
@@ -198,3 +202,67 @@ func MoveFile(repoPath, oldTreeName, newTreeName string) error {
198202
_, err := NewCommand("mv").AddArguments(oldTreeName, newTreeName).RunInDir(repoPath)
199203
return err
200204
}
205+
206+
// CountObject represents repository count objects report
207+
type CountObject struct {
208+
Count int64
209+
Size int64
210+
InPack int64
211+
Packs int64
212+
SizePack int64
213+
PrunePack int64
214+
Garbage int64
215+
SizeGarbage int64
216+
}
217+
218+
const (
219+
_STAT_COUNT = "count: "
220+
_STAT_SIZE = "size: "
221+
_STAT_INPACK = "in-pack: "
222+
_STAT_PACKS = "packs: "
223+
_STAT_SIZEPACK = "size-pack: "
224+
_STAT_PRUNEPACKAGE = "prune-package: "
225+
_STAT_GARBAGE = "garbage: "
226+
_STAT_SIZEGARBAGE = "size-garbage: "
227+
)
228+
229+
// GetRepoSize returns disk consumption for repo in path
230+
func GetRepoSize(repoPath string) (*CountObject, error) {
231+
if version.Compare(gitVersion, "1.8.3", "<") {
232+
return nil, ErrUnsupportedVersion{"1.8.3"}
233+
}
234+
235+
cmd := NewCommand("count-objects", "-v")
236+
stdout, err := cmd.RunInDir(repoPath)
237+
if err != nil {
238+
return nil, err
239+
}
240+
241+
return parseSize(stdout), nil
242+
}
243+
244+
// parseSize parses the output from count-objects and return a CountObject
245+
func parseSize(objects string) *CountObject {
246+
repoSize := new(CountObject)
247+
for _, line := range strings.Split(objects, "\n") {
248+
switch {
249+
case strings.HasPrefix(line, _STAT_COUNT):
250+
repoSize.Count = com.StrTo(line[7:]).MustInt64()
251+
case strings.HasPrefix(line, _STAT_SIZE):
252+
repoSize.Size = com.StrTo(line[6:]).MustInt64() * 1024
253+
case strings.HasPrefix(line, _STAT_INPACK):
254+
repoSize.InPack = com.StrTo(line[9:]).MustInt64()
255+
case strings.HasPrefix(line, _STAT_PACKS):
256+
repoSize.Packs = com.StrTo(line[7:]).MustInt64()
257+
case strings.HasPrefix(line, _STAT_SIZEPACK):
258+
repoSize.SizePack = com.StrTo(line[11:]).MustInt64() * 1024
259+
case strings.HasPrefix(line, _STAT_PRUNEPACKAGE):
260+
repoSize.PrunePack = com.StrTo(line[16:]).MustInt64()
261+
case strings.HasPrefix(line, _STAT_GARBAGE):
262+
repoSize.Garbage = com.StrTo(line[9:]).MustInt64()
263+
case strings.HasPrefix(line, _STAT_SIZEGARBAGE):
264+
repoSize.SizeGarbage = com.StrTo(line[14:]).MustInt64() * 1024
265+
}
266+
}
267+
return repoSize
268+
}

0 commit comments

Comments
 (0)