-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathgraph.go
764 lines (641 loc) · 18.6 KB
/
graph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
package sizes
import (
"context"
"errors"
"fmt"
"sync"
"github.com/github/git-sizer/counts"
"github.com/github/git-sizer/git"
"github.com/github/git-sizer/meter"
)
type Root interface {
Name() string
OID() git.OID
Walk() bool
}
type ReferenceRoot interface {
Root
Reference() git.Reference
Groups() []RefGroupSymbol
}
// ScanRepositoryUsingGraph scans `repo`, using `rg` to decide which
// references to scan and how to group them. `nameStyle` specifies
// whether the output should include full names, hashes only, or
// nothing in the footnotes. `progress` tells whether a progress meter
// should be displayed while it works.
//
// It returns the size data for the repository.
func ScanRepositoryUsingGraph(
ctx context.Context,
repo *git.Repository,
roots []Root,
nameStyle NameStyle,
progressMeter meter.Progress,
) (HistorySize, error) {
graph := NewGraph(nameStyle)
objIter, err := repo.NewObjectIter(ctx)
if err != nil {
return HistorySize{}, err
}
errChan := make(chan error, 1)
// Feed the references that we want to walk into the stdin of the
// object iterator:
go func() {
defer objIter.Close()
errChan <- func() error {
for _, root := range roots {
if !root.Walk() {
continue
}
if err := objIter.AddRoot(root.OID()); err != nil {
return err
}
}
return nil
}()
}()
type ObjectHeader struct {
oid git.OID
objectSize counts.Count32
}
type CommitHeader struct {
ObjectHeader
tree git.OID
}
// We process the blobs right away, but record these other types
// of objects for later processing. The order of processing
// strongly affects performance, which prefers object locality and
// prefers having as few "dangling pointers" as possible. It also
// affects which of multiple equally-sized objects are chosen and
// which references the `PathResolver` chooses to refer to
// commits. Note that we process different types of objects in
// different orders:
//
// * Blobs are processed in roughly reverse-chronological order
// This is relatively inconsequential because blobs can't point
// at any other objects.
//
// * Trees are processed in roughly reverse-chronological order
// (the order that they come out of `git rev-parse --date-order
// --objects`). This is more efficient than the reverse because
// the Git command outputs the whole tree corresponding to a
// commit before moving onto the next commit. So when we process
// them in this order, we have at most one "treeful" of trees
// pending at any given moment (and usually much less); there
// are no "dangling pointers" carried over from one commit to
// the next. Plus, this allows us to use
// `AdjustMaxIfNecessary()`, which leads to less churn in the
// `PathResolver`.
//
// * Commits are processed in roughly chronological order when
// computing sizes and looking for the "biggest" commits. This
// is preferable because the opposite order would leave most
// commits pending until we worked all the way to the start of
// history. But by using `AdjustMaxIfPossible()`, we still
// preferentially choose the newest commits.
//
// But when feeding commits to the `PathResolver`, we process
// the commits in reverse chronological order. This helps prefer
// new commits when naming blobs and trees.
//
// * References are processed in alphabetical order. (It might be
// a tiny improvement to pick the order more intentionally, to
// favor certain references when naming commits that are pointed
// to by multiple references, but it doesn't seem worth the
// effort.)
var trees, tags []ObjectHeader
var commits []CommitHeader
progressMeter.Start("Processing blobs: %d")
for {
obj, ok, err := objIter.Next()
if err != nil {
return HistorySize{}, err
}
if !ok {
break
}
switch obj.ObjectType {
case "blob":
progressMeter.Inc()
graph.RegisterBlob(obj.OID, obj.ObjectSize)
case "tree":
trees = append(trees, ObjectHeader{obj.OID, obj.ObjectSize})
case "commit":
commits = append(commits, CommitHeader{ObjectHeader{obj.OID, obj.ObjectSize}, repo.NullOID()})
case "tag":
tags = append(tags, ObjectHeader{obj.OID, obj.ObjectSize})
default:
return HistorySize{}, fmt.Errorf("unexpected object type: %s", obj.ObjectType)
}
}
progressMeter.Done()
err = <-errChan
if err != nil {
return HistorySize{}, err
}
objectIter, err := repo.NewBatchObjectIter(ctx)
if err != nil {
return HistorySize{}, err
}
go func() {
defer objectIter.Close()
errChan <- func() error {
for _, obj := range trees {
if err := objectIter.RequestObject(obj.oid); err != nil {
return fmt.Errorf("requesting tree '%s': %w", obj.oid, err)
}
}
for i := len(commits); i > 0; i-- {
obj := commits[i-1]
if err := objectIter.RequestObject(obj.oid); err != nil {
return fmt.Errorf("requesting commit '%s': %w", obj.oid, err)
}
}
for _, obj := range tags {
if err := objectIter.RequestObject(obj.oid); err != nil {
return fmt.Errorf("requesting tag '%s': %w", obj.oid, err)
}
}
return nil
}()
}()
progressMeter.Start("Processing trees: %d")
for range trees {
obj, ok, err := objectIter.Next()
if err != nil {
return HistorySize{}, err
}
if !ok {
return HistorySize{}, errors.New("fewer trees read than expected")
}
if obj.ObjectType != "tree" {
return HistorySize{}, fmt.Errorf("expected tree; read %#v", obj.ObjectType)
}
progressMeter.Inc()
tree, err := git.ParseTree(obj.OID, obj.Data)
if err != nil {
return HistorySize{}, err
}
err = graph.RegisterTree(obj.OID, tree)
if err != nil {
return HistorySize{}, err
}
}
progressMeter.Done()
// Process the commits in (roughly) chronological order, to
// minimize the number of commits that are pending at any one
// time:
progressMeter.Start("Processing commits: %d")
for i := len(commits); i > 0; i-- {
obj, ok, err := objectIter.Next()
if err != nil {
return HistorySize{}, err
}
if !ok {
return HistorySize{}, errors.New("fewer commits read than expected")
}
if obj.ObjectType != "commit" {
return HistorySize{}, fmt.Errorf("expected commit; read %#v", obj.ObjectType)
}
commit, err := git.ParseCommit(obj.OID, obj.Data)
if err != nil {
return HistorySize{}, err
}
if obj.OID != commits[i-1].oid {
panic("commits not read in same order as requested")
}
commits[i-1].tree = commit.Tree
progressMeter.Inc()
graph.RegisterCommit(obj.OID, commit)
}
progressMeter.Done()
// Tell PathResolver about the commits in (roughly) reverse
// chronological order, to favor new ones in the paths of trees:
if nameStyle != NameStyleNone {
progressMeter.Start("Matching commits to trees: %d")
for _, commit := range commits {
progressMeter.Inc()
graph.pathResolver.RecordCommit(commit.oid, commit.tree)
}
progressMeter.Done()
}
progressMeter.Start("Processing annotated tags: %d")
for range tags {
obj, ok, err := objectIter.Next()
if err != nil {
return HistorySize{}, err
}
if !ok {
return HistorySize{}, errors.New("fewer tags read than expected")
}
if obj.ObjectType != "tag" {
return HistorySize{}, fmt.Errorf("expected tag; read %#v", obj.ObjectType)
}
tag, err := git.ParseTag(obj.OID, obj.Data)
if err != nil {
return HistorySize{}, err
}
progressMeter.Inc()
graph.RegisterTag(obj.OID, tag)
}
progressMeter.Done()
err = <-errChan
if err != nil {
return HistorySize{}, err
}
progressMeter.Start("Processing references: %d")
for _, root := range roots {
progressMeter.Inc()
if refRoot, ok := root.(ReferenceRoot); ok {
graph.RegisterReference(refRoot.Reference(), refRoot.Groups())
}
if root.Walk() {
graph.pathResolver.RecordName(root.Name(), root.OID())
}
}
progressMeter.Done()
return graph.HistorySize(), nil
}
// Graph is an object graph that is being built up.
type Graph struct {
blobLock sync.Mutex
blobSizes map[git.OID]BlobSize
treeLock sync.Mutex
treeRecords map[git.OID]*treeRecord
treeSizes map[git.OID]TreeSize
commitLock sync.Mutex
commitSizes map[git.OID]CommitSize
tagLock sync.Mutex
tagRecords map[git.OID]*tagRecord
tagSizes map[git.OID]TagSize
// Statistics about the overall history size:
historyLock sync.Mutex
historySize HistorySize
pathResolver PathResolver
}
// NewGraph creates and returns a new `*Graph` instance.
func NewGraph(nameStyle NameStyle) *Graph {
return &Graph{
blobSizes: make(map[git.OID]BlobSize),
treeRecords: make(map[git.OID]*treeRecord),
treeSizes: make(map[git.OID]TreeSize),
commitSizes: make(map[git.OID]CommitSize),
tagRecords: make(map[git.OID]*tagRecord),
tagSizes: make(map[git.OID]TagSize),
historySize: HistorySize{
ReferenceGroups: make(map[RefGroupSymbol]*counts.Count32),
},
pathResolver: NewPathResolver(nameStyle),
}
}
// RegisterReference records the specified reference in `g`.
func (g *Graph) RegisterReference(ref git.Reference, groups []RefGroupSymbol) {
g.historyLock.Lock()
g.historySize.recordReference(g, ref)
for _, group := range groups {
g.historySize.recordReferenceGroup(g, group)
}
g.historyLock.Unlock()
}
// Register a name that can be used for the specified OID.
func (g *Graph) RegisterName(name string, oid git.OID) {
g.pathResolver.RecordName(name, oid)
}
// HistorySize returns the size data that have been collected.
func (g *Graph) HistorySize() HistorySize {
g.treeLock.Lock()
defer g.treeLock.Unlock()
g.tagLock.Lock()
defer g.tagLock.Unlock()
g.historyLock.Lock()
defer g.historyLock.Unlock()
if len(g.treeRecords) != 0 {
panic(fmt.Sprintf("%d tree records remain!", len(g.treeRecords)))
}
if len(g.tagRecords) != 0 {
panic(fmt.Sprintf("%d tag records remain!", len(g.tagRecords)))
}
return g.historySize
}
// RegisterBlob records that the specified `oid` is a blob with the
// specified size.
func (g *Graph) RegisterBlob(oid git.OID, objectSize counts.Count32) {
size := BlobSize{Size: objectSize}
// There are no listeners. Since this is a blob, we know all that
// we need to know about it. So skip the record and just fill in
// the size.
g.blobLock.Lock()
g.blobSizes[oid] = size
g.blobLock.Unlock()
g.historyLock.Lock()
g.historySize.recordBlob(g, oid, size)
g.historyLock.Unlock()
}
// The `Require*Size` functions behave as follows:
//
// * If the size of the object with name `oid` is already known. In
// this case, return true as the second value.
//
// * If the size of the object is not yet known, then register the
// listener to be informed some time in the future when the size is
// known. In this case, return false as the second value.
func (g *Graph) GetBlobSize(oid git.OID) BlobSize {
// See if we already know the size:
size, ok := g.blobSizes[oid]
if !ok {
panic("blob size not known")
}
return size
}
func (g *Graph) RequireTreeSize(oid git.OID, listener func(TreeSize)) (TreeSize, bool) {
g.treeLock.Lock()
size, ok := g.treeSizes[oid]
if ok {
g.treeLock.Unlock()
return size, true
}
record, ok := g.treeRecords[oid]
if !ok {
record = newTreeRecord(oid)
g.treeRecords[oid] = record
}
record.addListener(listener)
g.treeLock.Unlock()
return TreeSize{}, false
}
func (g *Graph) GetTreeSize(oid git.OID) TreeSize {
g.treeLock.Lock()
size, ok := g.treeSizes[oid]
if !ok {
panic("tree size not available!")
}
g.treeLock.Unlock()
return size
}
// Record that the specified `oid` is the specified `tree`.
func (g *Graph) RegisterTree(oid git.OID, tree *git.Tree) error {
g.treeLock.Lock()
if _, ok := g.treeSizes[oid]; ok {
panic(fmt.Sprintf("tree %s registered twice!", oid))
}
// See if we already have a record for this tree:
record, ok := g.treeRecords[oid]
if !ok {
record = newTreeRecord(oid)
g.treeRecords[oid] = record
}
g.treeLock.Unlock()
// Let the record take care of the rest:
return record.initialize(g, oid, tree)
}
func (g *Graph) finalizeTreeSize(
oid git.OID, size TreeSize, objectSize counts.Count32, treeEntries counts.Count32,
) {
g.treeLock.Lock()
g.treeSizes[oid] = size
delete(g.treeRecords, oid)
g.treeLock.Unlock()
g.historyLock.Lock()
g.historySize.recordTree(g, oid, size, objectSize, treeEntries)
g.historyLock.Unlock()
}
type treeRecord struct {
oid git.OID
// Limit to only one mutator at a time.
lock sync.Mutex
// The size of this object, in bytes. Initialized iff pending !=
// -1.
objectSize counts.Count32
// The number of entries directly in this tree. Initialized iff
// pending != -1.
entryCount counts.Count32
// The size of the items we know so far:
size TreeSize
// The number of dependencies whose callbacks haven't yet been
// invoked. Before the tree itself has been read, this is set to
// -1. After it has been read, it counts down the number of
// dependencies that are still unknown. When this number goes to
// zero, then `size` is the final answer.
pending int
// The listeners waiting to learn our size.
listeners []func(TreeSize)
}
func newTreeRecord(oid git.OID) *treeRecord {
return &treeRecord{
oid: oid,
size: TreeSize{ExpandedTreeCount: 1},
pending: -1,
}
}
// Initialize `r` (which is empty) based on `tree`.
func (r *treeRecord) initialize(g *Graph, oid git.OID, tree *git.Tree) error {
r.lock.Lock()
defer r.lock.Unlock()
r.objectSize = tree.Size()
r.pending = 0
iter := tree.Iter()
for {
entry, ok, err := iter.NextEntry()
if err != nil {
return err
}
if !ok {
break
}
name := entry.Name
switch {
case entry.Filemode&0o170000 == 0o40000:
// Tree
listener := func(size TreeSize) {
// This listener is called when the tree pointed to by
// `entry` has been fully processed.
r.lock.Lock()
defer r.lock.Unlock()
g.pathResolver.RecordTreeEntry(oid, name, entry.OID)
r.size.addDescendent(name, size)
r.pending--
// This might inform *our* listeners that we are now
// fully processed:
r.maybeFinalize(g)
}
treeSize, ok := g.RequireTreeSize(entry.OID, listener)
if ok {
r.size.addDescendent(name, treeSize)
} else {
r.pending++
}
r.entryCount.Increment(1)
case entry.Filemode&0o170000 == 0o160000:
// Commit (i.e., submodule)
r.size.addSubmodule(name)
r.entryCount.Increment(1)
case entry.Filemode&0o170000 == 0o120000:
// Symlink
g.pathResolver.RecordTreeEntry(oid, name, entry.OID)
r.size.addLink(name)
r.entryCount.Increment(1)
default:
// Blob
g.pathResolver.RecordTreeEntry(oid, name, entry.OID)
blobSize := g.GetBlobSize(entry.OID)
r.size.addBlob(name, blobSize)
r.entryCount.Increment(1)
}
}
r.maybeFinalize(g)
return nil
}
func (r *treeRecord) maybeFinalize(g *Graph) {
if r.pending == 0 {
g.finalizeTreeSize(r.oid, r.size, r.objectSize, r.entryCount)
for _, listener := range r.listeners {
listener(r.size)
}
}
}
// Must be called either before `r` is published or while it is
// locked.
func (r *treeRecord) addListener(listener func(TreeSize)) {
r.listeners = append(r.listeners, listener)
}
func (g *Graph) GetCommitSize(oid git.OID) CommitSize {
g.commitLock.Lock()
size, ok := g.commitSizes[oid]
if !ok {
panic("commit is not available")
}
g.commitLock.Unlock()
return size
}
// Record that the specified `oid` is the specified `commit`.
func (g *Graph) RegisterCommit(oid git.OID, commit *git.Commit) {
g.commitLock.Lock()
if _, ok := g.commitSizes[oid]; ok {
panic(fmt.Sprintf("commit %s registered twice!", oid))
}
g.commitLock.Unlock()
// The number of direct parents of this commit.
parentCount := counts.NewCount32(uint64(len(commit.Parents)))
// The size of the items we know so far:
size := CommitSize{}
// The tree:
treeSize := g.GetTreeSize(commit.Tree)
size.addTree(treeSize)
for _, parent := range commit.Parents {
parentSize := g.GetCommitSize(parent)
size.addParent(parentSize)
}
// Add 1 for this commit itself:
size.MaxAncestorDepth.Increment(1)
g.commitLock.Lock()
g.commitSizes[oid] = size
g.commitLock.Unlock()
g.historyLock.Lock()
g.historySize.recordCommit(g, oid, size, commit.Size, parentCount)
g.historyLock.Unlock()
}
func (g *Graph) RequireTagSize(oid git.OID, listener func(TagSize)) (TagSize, bool) {
g.tagLock.Lock()
size, ok := g.tagSizes[oid]
if ok {
g.tagLock.Unlock()
return size, true
}
record, ok := g.tagRecords[oid]
if !ok {
record = newTagRecord(oid)
g.tagRecords[oid] = record
}
record.addListener(listener)
g.tagLock.Unlock()
return TagSize{}, false
}
// Record that the specified `oid` is the specified `tag`.
func (g *Graph) RegisterTag(oid git.OID, tag *git.Tag) {
g.tagLock.Lock()
if _, ok := g.tagSizes[oid]; ok {
panic(fmt.Sprintf("tag %s registered twice!", oid))
}
// See if we already have a record for this tag:
record, ok := g.tagRecords[oid]
if !ok {
record = newTagRecord(oid)
g.tagRecords[oid] = record
}
g.tagLock.Unlock()
// Let the record take care of the rest:
record.initialize(g, oid, tag)
}
func (g *Graph) finalizeTagSize(oid git.OID, size TagSize, objectSize counts.Count32) {
g.tagLock.Lock()
g.tagSizes[oid] = size
delete(g.tagRecords, oid)
g.tagLock.Unlock()
g.historyLock.Lock()
g.historySize.recordTag(g, oid, size, objectSize)
g.historyLock.Unlock()
}
type tagRecord struct {
oid git.OID
// Limit to only one mutator at a time.
lock sync.Mutex
// The size of this commit object in bytes.
objectSize counts.Count32
// The size of the items we know so far:
size TagSize
// See treeRecord.pending. This value can be at most 1 for a Tag.
pending int8
// See treeRecord.listeners.
listeners []func(TagSize)
}
func newTagRecord(oid git.OID) *tagRecord {
return &tagRecord{
oid: oid,
pending: -1,
}
}
// Initialize `r` (which is empty) based on `tag`.
func (r *tagRecord) initialize(g *Graph, oid git.OID, tag *git.Tag) {
r.lock.Lock()
defer r.lock.Unlock()
r.objectSize = tag.Size
r.pending = 0
r.size.TagDepth = 1
// The only thing that a tag cares about its ancestors is how many
// tags have to be traversed to get to a real object. So we only
// need to listen to the referent if it is another tag.
switch tag.ReferentType {
case "tag":
listener := func(size TagSize) {
r.lock.Lock()
defer r.lock.Unlock()
r.size.TagDepth.Increment(size.TagDepth)
r.pending--
r.maybeFinalize(g)
}
tagSize, ok := g.RequireTagSize(tag.Referent, listener)
if ok {
r.size.TagDepth.Increment(tagSize.TagDepth)
} else {
r.pending++
}
case "commit":
case "tree":
case "blob":
default:
}
r.maybeFinalize(g)
}
func (r *tagRecord) maybeFinalize(g *Graph) {
if r.pending == 0 {
g.finalizeTagSize(r.oid, r.size, r.objectSize)
for _, listener := range r.listeners {
listener(r.size)
}
}
}
// Must be called either before `r` is published or while it is
// locked.
func (r *tagRecord) addListener(listener func(TagSize)) {
r.listeners = append(r.listeners, listener)
}