@@ -246,7 +246,13 @@ func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) {
246
246
return commitsCount (repo .Path , start + "..." + end , "" )
247
247
}
248
248
249
- func (repo * Repository ) commitsBefore (l * list.List , parent * list.Element , id sha1 , limit int ) error {
249
+ // The limit is depth, not total number of returned commits.
250
+ func (repo * Repository ) commitsBefore (l * list.List , parent * list.Element , id sha1 , current , limit int ) error {
251
+ // Reach the limit
252
+ if limit > 0 && current > limit {
253
+ return nil
254
+ }
255
+
250
256
commit , err := repo .getCommit (id )
251
257
if err != nil {
252
258
return fmt .Errorf ("getCommit: %v" , err )
@@ -262,26 +268,26 @@ func (repo *Repository) commitsBefore(l *list.List, parent *list.Element, id sha
262
268
break
263
269
} else if in .Value .(* Commit ).ID .Equal (commit .ID ) {
264
270
return nil
265
- } else {
266
- if in .Next () == nil {
267
- break
268
- }
269
- if in .Value .(* Commit ).Committer .When .Equal (commit .Committer .When ) {
270
- break
271
- }
271
+ } else if in .Next () == nil {
272
+ break
273
+ }
272
274
273
- if in .Value .(* Commit ).Committer .When .After (commit .Committer .When ) &&
274
- in .Next ().Value .(* Commit ).Committer .When .Before (commit .Committer .When ) {
275
- break
276
- }
275
+ if in .Value .(* Commit ).Committer .When .Equal (commit .Committer .When ) {
276
+ break
277
277
}
278
+
279
+ if in .Value .(* Commit ).Committer .When .After (commit .Committer .When ) &&
280
+ in .Next ().Value .(* Commit ).Committer .When .Before (commit .Committer .When ) {
281
+ break
282
+ }
283
+
278
284
in = in .Next ()
279
285
}
280
286
281
287
e = l .InsertAfter (commit , in )
282
288
}
283
289
284
- var pr = parent
290
+ pr : = parent
285
291
if commit .ParentCount () > 1 {
286
292
pr = e
287
293
}
@@ -291,7 +297,7 @@ func (repo *Repository) commitsBefore(l *list.List, parent *list.Element, id sha
291
297
if err != nil {
292
298
return err
293
299
}
294
- err = repo .commitsBefore (l , pr , id , 0 )
300
+ err = repo .commitsBefore (l , pr , id , current + 1 , limit )
295
301
if err != nil {
296
302
return err
297
303
}
@@ -302,5 +308,10 @@ func (repo *Repository) commitsBefore(l *list.List, parent *list.Element, id sha
302
308
303
309
func (repo * Repository ) getCommitsBefore (id sha1 ) (* list.List , error ) {
304
310
l := list .New ()
305
- return l , repo .commitsBefore (l , nil , id , 0 )
311
+ return l , repo .commitsBefore (l , nil , id , 1 , 0 )
312
+ }
313
+
314
+ func (repo * Repository ) getCommitsBeforeLimit (id sha1 , num int ) (* list.List , error ) {
315
+ l := list .New ()
316
+ return l , repo .commitsBefore (l , nil , id , 1 , num )
306
317
}
0 commit comments