fix: repoGetAllCommits should allow for the use of limit with path (#11752)

Pass down the `limit` value to use in the `rev-list` command.

Issue: https://codeberg.org/forgejo/forgejo/issues/11405

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11752
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
This commit is contained in:
minhn 2026-04-30 18:39:01 +02:00 committed by Gusted
commit 68be312467
4 changed files with 47 additions and 3 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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"
},