diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index 0ebdb0f8a0..99ffac7afb 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -217,10 +217,15 @@ type CommitsByFileAndRangeOptions struct { File string Not string Page int + PageSize int } // CommitsByFileAndRange return the commits according revision file and the page func (repo *Repository) CommitsByFileAndRange(opts CommitsByFileAndRangeOptions) ([]*Commit, error) { + if opts.PageSize <= 0 { + opts.PageSize = setting.Git.CommitsRangeSize + } + skip := (opts.Page - 1) * setting.Git.CommitsRangeSize stdoutReader, stdoutWriter := io.Pipe() @@ -231,7 +236,7 @@ func (repo *Repository) CommitsByFileAndRange(opts CommitsByFileAndRangeOptions) go func() { stderr := strings.Builder{} gitCmd := NewCommand(repo.Ctx, "rev-list"). - AddOptionFormat("--max-count=%d", setting.Git.CommitsRangeSize). + AddOptionFormat("--max-count=%d", opts.PageSize). AddOptionFormat("--skip=%d", skip) gitCmd.AddDynamicArguments(opts.Revision) diff --git a/modules/git/repo_commit_test.go b/modules/git/repo_commit_test.go index 99cde92300..401848a975 100644 --- a/modules/git/repo_commit_test.go +++ b/modules/git/repo_commit_test.go @@ -200,6 +200,44 @@ func TestCommitsByFileAndRange(t *testing.T) { } } +func TestCommitsByFileAndRangeWithPageSize(t *testing.T) { + bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") + bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path) + require.NoError(t, err) + defer bareRepo1.Close() + defer test.MockVariableValue(&setting.Git.CommitsRangeSize, 2)() + + testCases := []struct { + File string + Page int + PageSize int + ExpectedCommitCount int + }{ + {"file1.txt", 1, 1, 1}, + {"file2.txt", 1, 1, 1}, + {"file*.txt", 1, 2, 2}, + {"file*.txt", 1, 1, 1}, + {"foo", 1, 2, 2}, + {"foo", 1, 1, 1}, + {"foo", 2, 1, 1}, + {"foo", 3, 0, 0}, + {"foo", 3, 2, 0}, + {"f*", 1, 2, 2}, + {"f*", 2, 2, 2}, + {"f*", 3, 1, 1}, + } + for _, testCase := range testCases { + commits, err := bareRepo1.CommitsByFileAndRange(CommitsByFileAndRangeOptions{ + Revision: "master", + File: testCase.File, + Page: testCase.Page, + PageSize: testCase.PageSize, + }) + require.NoError(t, err) + assert.Len(t, commits, testCase.ExpectedCommitCount, "file: '%s', page: %d", testCase.File, testCase.Page) + } +} + func TestGetCommitsFromIDs(t *testing.T) { bareRepo1, err := openRepositoryWithDefaultContext(filepath.Join(testReposDir, "repo1_bare")) require.NoError(t, err) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 4221e59f17..4d89eb182d 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -135,7 +135,7 @@ func GetAllCommits(ctx *context.APIContext) { // type: integer // - name: limit // in: query - // description: page size of results (ignored if used with 'path') + // description: page size of results // type: integer // - name: not // in: query @@ -244,6 +244,7 @@ func GetAllCommits(ctx *context.APIContext) { File: path, Not: not, Page: listOptions.Page, + PageSize: listOptions.PageSize, }) if err != nil { ctx.Error(http.StatusInternalServerError, "CommitsByFileAndRange", err) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index e74f0039a6..45159b7c71 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -7734,7 +7734,7 @@ }, { "type": "integer", - "description": "page size of results (ignored if used with 'path')", + "description": "page size of results", "name": "limit", "in": "query" },