Debian Codegraph @

plumbing/object/commitgraph/commitnode_walker_topo_order.go

raw
1 package commitgraph
2
3 import (
4 "io"
5
6 "github.com/go-git/go-git/v5/plumbing"
7 "github.com/go-git/go-git/v5/plumbing/storer"
8
9 "github.com/emirpasic/gods/trees/binaryheap"
10 )
11
12 type commitNodeIteratorTopological struct {
13 exploreStack commitNodeStackable
14 visitStack commitNodeStackable
15 inCounts map[plumbing.Hash]int
16
17 ignore map[plumbing.Hash]struct{}
18 }
19
20 // NewCommitNodeIterTopoOrder returns a CommitNodeIter that walks the commit history,
21 // starting at the given commit and visiting its parents in a topological order but
22 // with the constraint that no parent is emitted before its children are emitted.
23 //
24 // This matches `git log --topo-order`
25 func NewCommitNodeIterTopoOrder(c CommitNode,
26 seenExternal map[plumbing.Hash]bool,
27 ignore []plumbing.Hash,
28 ) CommitNodeIter {
29 seen := composeIgnores(ignore, seenExternal)
30 inCounts := make(map[plumbing.Hash]int)
31
32 heap := &commitNodeHeap{binaryheap.NewWith(generationAndDateOrderComparator)}
33 heap.Push(c)
34
35 lifo := &commitNodeLifo{make([]CommitNode, 0, 8)}
36 lifo.Push(c)
37
38 return &commitNodeIteratorTopological{
39 exploreStack: heap,
40 visitStack: lifo,
41 inCounts: inCounts,
42 ignore: seen,
43 }
44 }
45
46 func (iter *commitNodeIteratorTopological) Next() (CommitNode, error) {
47 var next CommitNode
48 for {
49 var ok bool
50 next, ok = iter.visitStack.Pop()
51 if !ok {
52 return nil, io.EOF
53 }
54
55 if iter.inCounts[next.ID()] == 0 {
56 break
57 }
58 }
59
60 minimumLevel, generationV2 := next.GenerationV2(), true
61 if minimumLevel == 0 {
62 minimumLevel, generationV2 = next.Generation(), false
63 }
64
65 parents := make([]CommitNode, 0, len(next.ParentHashes()))
66 for i := range next.ParentHashes() {
67 pc, err := next.ParentNode(i)
68 if err != nil {
69 return nil, err
70 }
71
72 parents = append(parents, pc)
73
74 if generationV2 {
75 if pc.GenerationV2() < minimumLevel {
76 minimumLevel = pc.GenerationV2()
77 }
78 continue
79 }
80
81 if pc.Generation() < minimumLevel {
82 minimumLevel = pc.Generation()
83 }
84 }
85
86 // EXPLORE
87 for {
88 toExplore, ok := iter.exploreStack.Peek()
89 if !ok {
90 break
91 }
92
93 if toExplore.ID() != next.ID() && iter.exploreStack.Size() == 1 {
94 break
95 }
96 if generationV2 {
97 if toExplore.GenerationV2() < minimumLevel {
98 break
99 }
100 } else {
101 if toExplore.Generation() < minimumLevel {
102 break
103 }
104 }
105
106 iter.exploreStack.Pop()
107 for i, h := range toExplore.ParentHashes() {
108 if _, has := iter.ignore[h]; has {
109 continue
110 }
111 iter.inCounts[h]++
112
113 if iter.inCounts[h] == 1 {
114 pc, err := toExplore.ParentNode(i)
115 if err != nil {
116 return nil, err
117 }
118 iter.exploreStack.Push(pc)
119 }
120 }
121 }
122
123 // VISIT
124 for i, h := range next.ParentHashes() {
125 if _, has := iter.ignore[h]; has {
126 continue
127 }
128 iter.inCounts[h]--
129
130 if iter.inCounts[h] == 0 {
131 iter.visitStack.Push(parents[i])
132 }
133 }
134 delete(iter.inCounts, next.ID())
135
136 return next, nil
137 }
138
139 func (iter *commitNodeIteratorTopological) ForEach(cb func(CommitNode) error) error {
140 for {
141 obj, err := iter.Next()
142 if err != nil {
143 if err == io.EOF {
144 return nil
145 }
146
147 return err
148 }
149
150 if err := cb(obj); err != nil {
151 if err == storer.ErrStop {
152 return nil
153 }
154
155 return err
156 }
157 }
158 }
159
160 func (iter *commitNodeIteratorTopological) Close() {
161 }