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