@@ -11,7 +11,10 @@ import (
11
11
"os"
12
12
"path"
13
13
"path/filepath"
14
+ "strings"
14
15
"time"
16
+
17
+ "github.com/Unknwon/com"
15
18
)
16
19
17
20
// Repository represents a Git repository.
@@ -198,3 +201,63 @@ func MoveFile(repoPath, oldTreeName, newTreeName string) error {
198
201
_ , err := NewCommand ("mv" ).AddArguments (oldTreeName , newTreeName ).RunInDir (repoPath )
199
202
return err
200
203
}
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