Skip to content
This repository was archived by the owner on Apr 12, 2019. It is now read-only.

limit commit count to improve performance on large repositories #128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,12 @@ func CommitChanges(repoPath string, opts CommitChangesOptions) error {
return err
}

func commitsCount(repoPath, revision, relpath string) (int64, error) {
func commitsCount(repoPath, revision, relpath string, max int) (int64, error) {
var cmd *Command
cmd = NewCommand("rev-list", "--count")
if max > 0 {
cmd.AddArguments("--max-count", strconv.Itoa(max))
}
cmd.AddArguments(revision)
if len(relpath) > 0 {
cmd.AddArguments("--", relpath)
Expand All @@ -178,13 +181,13 @@ func commitsCount(repoPath, revision, relpath string) (int64, error) {
}

// CommitsCount returns number of total commits of until given revision.
func CommitsCount(repoPath, revision string) (int64, error) {
return commitsCount(repoPath, revision, "")
func CommitsCount(repoPath, revision string, max int) (int64, error) {
return commitsCount(repoPath, revision, "", max)
}

// CommitsCount returns number of total commits of until current revision.
func (c *Commit) CommitsCount() (int64, error) {
return CommitsCount(c.repo.Path, c.ID.String())
func (c *Commit) CommitsCount(max int) (int64, error) {
return CommitsCount(c.repo.Path, c.ID.String(), max)
}

// CommitsByRange returns the specific page commits before current revision, every page's number default by CommitsRangeSize
Expand Down
2 changes: 1 addition & 1 deletion commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestCommitsCount(t *testing.T) {
commitsCount, _ := CommitsCount(".", "d86a90f801dbe279db095437a8c7ea42c60e8d98")
commitsCount, _ := CommitsCount(".", "d86a90f801dbe279db095437a8c7ea42c60e8d98", 0)
assert.Equal(t, int64(3), commitsCount)
}

Expand Down
8 changes: 4 additions & 4 deletions repo_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ func (repo *Repository) getFilesChanged(id1 string, id2 string) ([]string, error
}

// FileCommitsCount return the number of files at a revison
func (repo *Repository) FileCommitsCount(revision, file string) (int64, error) {
return commitsCount(repo.Path, revision, file)
func (repo *Repository) FileCommitsCount(revision, file string, max int) (int64, error) {
return commitsCount(repo.Path, revision, file, max)
}

// CommitsByFileAndRange return the commits accroding revison file and the page
Expand Down Expand Up @@ -281,8 +281,8 @@ func (repo *Repository) CommitsBetweenIDs(last, before string) (*list.List, erro
}

// CommitsCountBetween return numbers of commits between two commits
func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) {
return commitsCount(repo.Path, start+"..."+end, "")
func (repo *Repository) CommitsCountBetween(start, end string, max int) (int64, error) {
return commitsCount(repo.Path, start+"..."+end, "", max)
}

// commitsBefore the limit is depth, not total number of returned commits.
Expand Down