Skip to content

Commit e18e31d

Browse files
GiteaBotwxiaoguang
andauthored
Fix commit range paging (#32944) (#32962)
Backport #32944 by wxiaoguang Co-authored-by: wxiaoguang <[email protected]>
1 parent e1026fe commit e18e31d

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

Diff for: modules/git/repo_commit.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,6 @@ type CommitsByFileAndRangeOptions struct {
216216

217217
// CommitsByFileAndRange return the commits according revision file and the page
218218
func (repo *Repository) CommitsByFileAndRange(opts CommitsByFileAndRangeOptions) ([]*Commit, error) {
219-
skip := (opts.Page - 1) * setting.Git.CommitsRangeSize
220-
221219
stdoutReader, stdoutWriter := io.Pipe()
222220
defer func() {
223221
_ = stdoutReader.Close()
@@ -226,8 +224,8 @@ func (repo *Repository) CommitsByFileAndRange(opts CommitsByFileAndRangeOptions)
226224
go func() {
227225
stderr := strings.Builder{}
228226
gitCmd := NewCommand(repo.Ctx, "rev-list").
229-
AddOptionFormat("--max-count=%d", setting.Git.CommitsRangeSize*opts.Page).
230-
AddOptionFormat("--skip=%d", skip)
227+
AddOptionFormat("--max-count=%d", setting.Git.CommitsRangeSize).
228+
AddOptionFormat("--skip=%d", (opts.Page-1)*setting.Git.CommitsRangeSize)
231229
gitCmd.AddDynamicArguments(opts.Revision)
232230

233231
if opts.Not != "" {

Diff for: modules/git/repo_commit_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import (
88
"path/filepath"
99
"testing"
1010

11+
"code.gitea.io/gitea/modules/setting"
12+
"code.gitea.io/gitea/modules/test"
13+
1114
"github.com/stretchr/testify/assert"
15+
"github.com/stretchr/testify/require"
1216
)
1317

1418
func TestRepository_GetCommitBranches(t *testing.T) {
@@ -126,3 +130,21 @@ func TestGetRefCommitID(t *testing.T) {
126130
}
127131
}
128132
}
133+
134+
func TestCommitsByFileAndRange(t *testing.T) {
135+
defer test.MockVariableValue(&setting.Git.CommitsRangeSize, 2)()
136+
137+
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
138+
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
139+
require.NoError(t, err)
140+
defer bareRepo1.Close()
141+
142+
// "foo" has 3 commits in "master" branch
143+
commits, err := bareRepo1.CommitsByFileAndRange(CommitsByFileAndRangeOptions{Revision: "master", File: "foo", Page: 1})
144+
require.NoError(t, err)
145+
assert.Len(t, commits, 2)
146+
147+
commits, err = bareRepo1.CommitsByFileAndRange(CommitsByFileAndRangeOptions{Revision: "master", File: "foo", Page: 2})
148+
require.NoError(t, err)
149+
assert.Len(t, commits, 1)
150+
}

0 commit comments

Comments
 (0)