Skip to content

Commit 13d0130

Browse files
committed
Separate out a git package
1 parent 00a409b commit 13d0130

File tree

6 files changed

+85
-75
lines changed

6 files changed

+85
-75
lines changed

git-sizer.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"runtime/pprof"
1111
"strconv"
1212

13+
"github.com/github/git-sizer/git"
1314
"github.com/github/git-sizer/isatty"
1415
"github.com/github/git-sizer/sizes"
1516
)
@@ -111,29 +112,29 @@ func mainImplementation() error {
111112
return errors.New("excess arguments")
112113
}
113114

114-
repo, err := sizes.NewRepository(".")
115+
repo, err := git.NewRepository(".")
115116
if err != nil {
116117
return fmt.Errorf("couldn't open Git repository: %s", err)
117118
}
118119
defer repo.Close()
119120

120121
var historySize sizes.HistorySize
121122

122-
var filter sizes.ReferenceFilter
123+
var filter git.ReferenceFilter
123124
if processBranches || processTags || processRemotes {
124-
var filters []sizes.ReferenceFilter
125+
var filters []git.ReferenceFilter
125126
if processBranches {
126-
filters = append(filters, sizes.BranchesFilter)
127+
filters = append(filters, git.BranchesFilter)
127128
}
128129
if processTags {
129-
filters = append(filters, sizes.TagsFilter)
130+
filters = append(filters, git.TagsFilter)
130131
}
131132
if processRemotes {
132-
filters = append(filters, sizes.RemotesFilter)
133+
filters = append(filters, git.RemotesFilter)
133134
}
134-
filter = sizes.OrFilter(filters...)
135+
filter = git.OrFilter(filters...)
135136
} else {
136-
filter = sizes.AllReferencesFilter
137+
filter = git.AllReferencesFilter
137138
}
138139

139140
historySize, err = sizes.ScanRepositoryUsingGraph(repo, filter, nameStyle, progress)

sizes/git.go renamed to git/git.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package sizes
1+
package git
22

33
import (
44
"bufio"
@@ -564,6 +564,10 @@ func ParseTree(oid Oid, data []byte) (*Tree, error) {
564564
return &Tree{string(data)}, nil
565565
}
566566

567+
func (tree Tree) Size() counts.Count32 {
568+
return counts.NewCount32(uint64(len(tree.data)))
569+
}
570+
567571
// Note that Name shares memory with the tree data that were
568572
// originally read; i.e., retaining a pointer to Name keeps the tree
569573
// data reachable.

sizes/graph.go

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import (
99
"time"
1010

1111
"github.com/github/git-sizer/counts"
12+
"github.com/github/git-sizer/git"
1213
"github.com/github/git-sizer/meter"
1314
)
1415

1516
func ScanRepositoryUsingGraph(
16-
repo *Repository, filter ReferenceFilter, nameStyle NameStyle, progress bool,
17+
repo *git.Repository, filter git.ReferenceFilter, nameStyle NameStyle, progress bool,
1718
) (HistorySize, error) {
1819
graph := NewGraph(nameStyle)
1920
var progressMeter meter.Progress
@@ -44,7 +45,7 @@ func ScanRepositoryUsingGraph(
4445
}()
4546

4647
errChan := make(chan error, 1)
47-
var refs []Reference
48+
var refs []git.Reference
4849
// Feed the references that we want into the stdin of the object
4950
// iterator:
5051
go func() {
@@ -82,13 +83,13 @@ func ScanRepositoryUsingGraph(
8283
}()
8384

8485
type ObjectHeader struct {
85-
oid Oid
86+
oid git.Oid
8687
objectSize counts.Count32
8788
}
8889

8990
type CommitHeader struct {
9091
ObjectHeader
91-
tree Oid
92+
tree git.Oid
9293
}
9394

9495
// We process the blobs right away, but record these other types
@@ -151,7 +152,7 @@ func ScanRepositoryUsingGraph(
151152
case "tree":
152153
trees = append(trees, ObjectHeader{oid, objectSize})
153154
case "commit":
154-
commits = append(commits, CommitHeader{ObjectHeader{oid, objectSize}, NullOid})
155+
commits = append(commits, CommitHeader{ObjectHeader{oid, objectSize}, git.NullOid})
155156
case "tag":
156157
tags = append(tags, ObjectHeader{oid, objectSize})
157158
default:
@@ -242,7 +243,7 @@ func ScanRepositoryUsingGraph(
242243
return HistorySize{}, fmt.Errorf("expected tree; read %#v", objectType)
243244
}
244245
progressMeter.Inc()
245-
tree, err := ParseTree(oid, data)
246+
tree, err := git.ParseTree(oid, data)
246247
if err != nil {
247248
return HistorySize{}, err
248249
}
@@ -268,7 +269,7 @@ func ScanRepositoryUsingGraph(
268269
if objectType != "commit" {
269270
return HistorySize{}, fmt.Errorf("expected commit; read %#v", objectType)
270271
}
271-
commit, err := ParseCommit(oid, data)
272+
commit, err := git.ParseCommit(oid, data)
272273
if err != nil {
273274
return HistorySize{}, err
274275
}
@@ -304,7 +305,7 @@ func ScanRepositoryUsingGraph(
304305
if objectType != "tag" {
305306
return HistorySize{}, fmt.Errorf("expected tag; read %#v", objectType)
306307
}
307-
tag, err := ParseTag(oid, data)
308+
tag, err := git.ParseTag(oid, data)
308309
if err != nil {
309310
return HistorySize{}, err
310311
}
@@ -336,21 +337,21 @@ func ScanRepositoryUsingGraph(
336337

337338
// An object graph that is being built up.
338339
type Graph struct {
339-
repo *Repository
340+
repo *git.Repository
340341

341342
blobLock sync.Mutex
342-
blobSizes map[Oid]BlobSize
343+
blobSizes map[git.Oid]BlobSize
343344

344345
treeLock sync.Mutex
345-
treeRecords map[Oid]*treeRecord
346-
treeSizes map[Oid]TreeSize
346+
treeRecords map[git.Oid]*treeRecord
347+
treeSizes map[git.Oid]TreeSize
347348

348349
commitLock sync.Mutex
349-
commitSizes map[Oid]CommitSize
350+
commitSizes map[git.Oid]CommitSize
350351

351352
tagLock sync.Mutex
352-
tagRecords map[Oid]*tagRecord
353-
tagSizes map[Oid]TagSize
353+
tagRecords map[git.Oid]*tagRecord
354+
tagSizes map[git.Oid]TagSize
354355

355356
// Statistics about the overall history size:
356357
historyLock sync.Mutex
@@ -361,21 +362,21 @@ type Graph struct {
361362

362363
func NewGraph(nameStyle NameStyle) *Graph {
363364
return &Graph{
364-
blobSizes: make(map[Oid]BlobSize),
365+
blobSizes: make(map[git.Oid]BlobSize),
365366

366-
treeRecords: make(map[Oid]*treeRecord),
367-
treeSizes: make(map[Oid]TreeSize),
367+
treeRecords: make(map[git.Oid]*treeRecord),
368+
treeSizes: make(map[git.Oid]TreeSize),
368369

369-
commitSizes: make(map[Oid]CommitSize),
370+
commitSizes: make(map[git.Oid]CommitSize),
370371

371-
tagRecords: make(map[Oid]*tagRecord),
372-
tagSizes: make(map[Oid]TagSize),
372+
tagRecords: make(map[git.Oid]*tagRecord),
373+
tagSizes: make(map[git.Oid]TagSize),
373374

374375
pathResolver: NewPathResolver(nameStyle),
375376
}
376377
}
377378

378-
func (g *Graph) RegisterReference(ref Reference) {
379+
func (g *Graph) RegisterReference(ref git.Reference) {
379380
g.historyLock.Lock()
380381
g.historySize.recordReference(g, ref)
381382
g.historyLock.Unlock()
@@ -400,7 +401,7 @@ func (g *Graph) HistorySize() HistorySize {
400401
}
401402

402403
// Record that the specified `oid` is a blob with the specified size.
403-
func (g *Graph) RegisterBlob(oid Oid, objectSize counts.Count32) {
404+
func (g *Graph) RegisterBlob(oid git.Oid, objectSize counts.Count32) {
404405
size := BlobSize{Size: objectSize}
405406
// There are no listeners. Since this is a blob, we know all that
406407
// we need to know about it. So skip the record and just fill in
@@ -423,7 +424,7 @@ func (g *Graph) RegisterBlob(oid Oid, objectSize counts.Count32) {
423424
// listener to be informed some time in the future when the size is
424425
// known. In this case, return false as the second value.
425426

426-
func (g *Graph) GetBlobSize(oid Oid) BlobSize {
427+
func (g *Graph) GetBlobSize(oid git.Oid) BlobSize {
427428
// See if we already know the size:
428429
size, ok := g.blobSizes[oid]
429430
if !ok {
@@ -432,7 +433,7 @@ func (g *Graph) GetBlobSize(oid Oid) BlobSize {
432433
return size
433434
}
434435

435-
func (g *Graph) RequireTreeSize(oid Oid, listener func(TreeSize)) (TreeSize, bool) {
436+
func (g *Graph) RequireTreeSize(oid git.Oid, listener func(TreeSize)) (TreeSize, bool) {
436437
g.treeLock.Lock()
437438

438439
size, ok := g.treeSizes[oid]
@@ -454,7 +455,7 @@ func (g *Graph) RequireTreeSize(oid Oid, listener func(TreeSize)) (TreeSize, boo
454455
return TreeSize{}, false
455456
}
456457

457-
func (g *Graph) GetTreeSize(oid Oid) TreeSize {
458+
func (g *Graph) GetTreeSize(oid git.Oid) TreeSize {
458459
g.treeLock.Lock()
459460

460461
size, ok := g.treeSizes[oid]
@@ -466,7 +467,7 @@ func (g *Graph) GetTreeSize(oid Oid) TreeSize {
466467
}
467468

468469
// Record that the specified `oid` is the specified `tree`.
469-
func (g *Graph) RegisterTree(oid Oid, tree *Tree) error {
470+
func (g *Graph) RegisterTree(oid git.Oid, tree *git.Tree) error {
470471
g.treeLock.Lock()
471472

472473
if _, ok := g.treeSizes[oid]; ok {
@@ -487,7 +488,7 @@ func (g *Graph) RegisterTree(oid Oid, tree *Tree) error {
487488
}
488489

489490
func (g *Graph) finalizeTreeSize(
490-
oid Oid, size TreeSize, objectSize counts.Count32, treeEntries counts.Count32,
491+
oid git.Oid, size TreeSize, objectSize counts.Count32, treeEntries counts.Count32,
491492
) {
492493
g.treeLock.Lock()
493494
g.treeSizes[oid] = size
@@ -500,7 +501,7 @@ func (g *Graph) finalizeTreeSize(
500501
}
501502

502503
type treeRecord struct {
503-
oid Oid
504+
oid git.Oid
504505

505506
// Limit to only one mutator at a time.
506507
lock sync.Mutex
@@ -527,7 +528,7 @@ type treeRecord struct {
527528
listeners []func(TreeSize)
528529
}
529530

530-
func newTreeRecord(oid Oid) *treeRecord {
531+
func newTreeRecord(oid git.Oid) *treeRecord {
531532
return &treeRecord{
532533
oid: oid,
533534
size: TreeSize{ExpandedTreeCount: 1},
@@ -536,11 +537,11 @@ func newTreeRecord(oid Oid) *treeRecord {
536537
}
537538

538539
// Initialize `r` (which is empty) based on `tree`.
539-
func (r *treeRecord) initialize(g *Graph, oid Oid, tree *Tree) error {
540+
func (r *treeRecord) initialize(g *Graph, oid git.Oid, tree *git.Tree) error {
540541
r.lock.Lock()
541542
defer r.lock.Unlock()
542543

543-
r.objectSize = counts.NewCount32(uint64(len(tree.data)))
544+
r.objectSize = tree.Size()
544545
r.pending = 0
545546

546547
iter := tree.Iter()
@@ -624,7 +625,7 @@ func (r *treeRecord) addListener(listener func(TreeSize)) {
624625
r.listeners = append(r.listeners, listener)
625626
}
626627

627-
func (g *Graph) GetCommitSize(oid Oid) CommitSize {
628+
func (g *Graph) GetCommitSize(oid git.Oid) CommitSize {
628629
g.commitLock.Lock()
629630

630631
size, ok := g.commitSizes[oid]
@@ -637,7 +638,7 @@ func (g *Graph) GetCommitSize(oid Oid) CommitSize {
637638
}
638639

639640
// Record that the specified `oid` is the specified `commit`.
640-
func (g *Graph) RegisterCommit(oid Oid, commit *Commit) {
641+
func (g *Graph) RegisterCommit(oid git.Oid, commit *git.Commit) {
641642
g.commitLock.Lock()
642643
if _, ok := g.commitSizes[oid]; ok {
643644
panic(fmt.Sprintf("commit %s registered twice!", oid))
@@ -671,7 +672,7 @@ func (g *Graph) RegisterCommit(oid Oid, commit *Commit) {
671672
g.historyLock.Unlock()
672673
}
673674

674-
func (g *Graph) RequireTagSize(oid Oid, listener func(TagSize)) (TagSize, bool) {
675+
func (g *Graph) RequireTagSize(oid git.Oid, listener func(TagSize)) (TagSize, bool) {
675676
g.tagLock.Lock()
676677

677678
size, ok := g.tagSizes[oid]
@@ -694,7 +695,7 @@ func (g *Graph) RequireTagSize(oid Oid, listener func(TagSize)) (TagSize, bool)
694695
}
695696

696697
// Record that the specified `oid` is the specified `tag`.
697-
func (g *Graph) RegisterTag(oid Oid, tag *Tag) {
698+
func (g *Graph) RegisterTag(oid git.Oid, tag *git.Tag) {
698699
g.tagLock.Lock()
699700

700701
if _, ok := g.tagSizes[oid]; ok {
@@ -714,7 +715,7 @@ func (g *Graph) RegisterTag(oid Oid, tag *Tag) {
714715
record.initialize(g, oid, tag)
715716
}
716717

717-
func (g *Graph) finalizeTagSize(oid Oid, size TagSize, objectSize counts.Count32) {
718+
func (g *Graph) finalizeTagSize(oid git.Oid, size TagSize, objectSize counts.Count32) {
718719
g.tagLock.Lock()
719720
g.tagSizes[oid] = size
720721
delete(g.tagRecords, oid)
@@ -726,7 +727,7 @@ func (g *Graph) finalizeTagSize(oid Oid, size TagSize, objectSize counts.Count32
726727
}
727728

728729
type tagRecord struct {
729-
oid Oid
730+
oid git.Oid
730731

731732
// Limit to only one mutator at a time.
732733
lock sync.Mutex
@@ -744,15 +745,15 @@ type tagRecord struct {
744745
listeners []func(TagSize)
745746
}
746747

747-
func newTagRecord(oid Oid) *tagRecord {
748+
func newTagRecord(oid git.Oid) *tagRecord {
748749
return &tagRecord{
749750
oid: oid,
750751
pending: -1,
751752
}
752753
}
753754

754755
// Initialize `r` (which is empty) based on `tag`.
755-
func (r *tagRecord) initialize(g *Graph, oid Oid, tag *Tag) {
756+
func (r *tagRecord) initialize(g *Graph, oid git.Oid, tag *git.Tag) {
756757
r.lock.Lock()
757758
defer r.lock.Unlock()
758759

sizes/output.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strconv"
99

1010
"github.com/github/git-sizer/counts"
11+
"github.com/github/git-sizer/git"
1112
)
1213

1314
func (s BlobSize) String() string {
@@ -167,7 +168,7 @@ func (l *item) Emit(t *table, buf io.Writer, indent int) {
167168
}
168169

169170
func (l *item) Footnote(nameStyle NameStyle) string {
170-
if l.path == nil || l.path.Oid == NullOid {
171+
if l.path == nil || l.path.Oid == git.NullOid {
171172
return ""
172173
}
173174
switch nameStyle {

0 commit comments

Comments
 (0)