@@ -125,6 +125,7 @@ func (f *HistoryIdx) repoHistoryIdx(repo *git.Repository, start, target plumbing
125
125
// history. Because the frame keeps track of which was its index, we can
126
126
// return accurate indexes even if there are multiple branches.
127
127
stack := []* stackFrame {{0 , 0 , []plumbing.Hash {start }}}
128
+ visitedHashes := make (map [plumbing.Hash ]struct {})
128
129
129
130
for {
130
131
if len (stack ) == 0 {
@@ -133,7 +134,13 @@ func (f *HistoryIdx) repoHistoryIdx(repo *git.Repository, start, target plumbing
133
134
134
135
frame := stack [len (stack )- 1 ]
135
136
136
- c , err := repo .CommitObject (frame .hashes [frame .pos ])
137
+ h := frame .hashes [frame .pos ]
138
+ _ , ok := visitedHashes [h ]
139
+ if ! ok {
140
+ visitedHashes [h ] = struct {}{}
141
+ }
142
+
143
+ c , err := repo .CommitObject (h )
137
144
if err == plumbing .ErrObjectNotFound {
138
145
return - 1 , nil
139
146
}
@@ -153,7 +160,17 @@ func (f *HistoryIdx) repoHistoryIdx(repo *git.Repository, start, target plumbing
153
160
}
154
161
155
162
if c .NumParents () > 0 {
156
- stack = append (stack , & stackFrame {frame .idx + 1 , 0 , c .ParentHashes })
163
+ newParents := make ([]plumbing.Hash , 0 , c .NumParents ())
164
+ for _ , h = range c .ParentHashes {
165
+ _ , ok = visitedHashes [h ]
166
+ if ! ok {
167
+ newParents = append (newParents , h )
168
+ }
169
+ }
170
+
171
+ if len (newParents ) > 0 {
172
+ stack = append (stack , & stackFrame {frame .idx + 1 , 0 , newParents })
173
+ }
157
174
}
158
175
}
159
176
}
0 commit comments