Skip to content

Commit 1ffc4bc

Browse files
committed
readboferelimit
1 parent 4cc1cbb commit 1ffc4bc

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

commit.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ func (c *Commit) CommitsBefore() (*list.List, error) {
164164
return c.repo.getCommitsBefore(c.ID)
165165
}
166166

167+
func (c *Commit) CommitsBeforeLimit(num int) (*list.List, error) {
168+
return c.repo.getCommitsBeforeLimit(c.ID, num)
169+
}
170+
167171
func (c *Commit) CommitsBeforeUntil(commitID string) (*list.List, error) {
168172
endCommit, err := c.repo.GetCommit(commitID)
169173
if err != nil {

repo_commit.go

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,13 @@ func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) {
246246
return commitsCount(repo.Path, start+"..."+end, "")
247247
}
248248

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+
250256
commit, err := repo.getCommit(id)
251257
if err != nil {
252258
return fmt.Errorf("getCommit: %v", err)
@@ -262,26 +268,26 @@ func (repo *Repository) commitsBefore(l *list.List, parent *list.Element, id sha
262268
break
263269
} else if in.Value.(*Commit).ID.Equal(commit.ID) {
264270
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+
}
272274

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
277277
}
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+
278284
in = in.Next()
279285
}
280286

281287
e = l.InsertAfter(commit, in)
282288
}
283289

284-
var pr = parent
290+
pr := parent
285291
if commit.ParentCount() > 1 {
286292
pr = e
287293
}
@@ -291,7 +297,7 @@ func (repo *Repository) commitsBefore(l *list.List, parent *list.Element, id sha
291297
if err != nil {
292298
return err
293299
}
294-
err = repo.commitsBefore(l, pr, id, 0)
300+
err = repo.commitsBefore(l, pr, id, current+1, limit)
295301
if err != nil {
296302
return err
297303
}
@@ -302,5 +308,10 @@ func (repo *Repository) commitsBefore(l *list.List, parent *list.Element, id sha
302308

303309
func (repo *Repository) getCommitsBefore(id sha1) (*list.List, error) {
304310
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)
306317
}

0 commit comments

Comments
 (0)