Skip to content

Commit 0d14447

Browse files
6543zeripatha1012112796
authored
[API] Add pagination to ListBranches (#14524)
* make PaginateUserSlice generic -> PaginateSlice * Add pagination to ListBranches * add skip, limit to Repository.GetBranches() * Move routers/api/v1/utils/utils PaginateSlice -> modules/util/paginate.go * repo_module.GetBranches paginate * fix & rename & more logging * better description Co-authored-by: zeripath <[email protected]> Co-authored-by: a1012112796 <[email protected]>
1 parent c295a27 commit 0d14447

File tree

20 files changed

+239
-87
lines changed

20 files changed

+239
-87
lines changed

integrations/branches_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ func branchAction(t *testing.T, button string) (*HTMLDoc, string) {
5757

5858
htmlDoc := NewHTMLParser(t, resp.Body)
5959
link, exists := htmlDoc.doc.Find(button).Attr("data-url")
60-
assert.True(t, exists, "The template has changed")
60+
if !assert.True(t, exists, "The template has changed") {
61+
t.Skip()
62+
}
6163

6264
req = NewRequestWithValues(t, "POST", link, map[string]string{
6365
"_csrf": getCsrf(t, htmlDoc.doc),
@@ -69,7 +71,7 @@ func branchAction(t *testing.T, button string) (*HTMLDoc, string) {
6971
req = NewRequest(t, "GET", "/user2/repo1/branches")
7072
resp = session.MakeRequest(t, req, http.StatusOK)
7173

72-
return NewHTMLParser(t, resp.Body), url.Query()["name"][0]
74+
return NewHTMLParser(t, resp.Body), url.Query().Get("name")
7375
}
7476

7577
func getCsrf(t *testing.T, doc *goquery.Document) string {

modules/context/repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ func RepoAssignment() func(http.Handler) http.Handler {
554554
}
555555
ctx.Data["Tags"] = tags
556556

557-
brs, err := ctx.Repo.GitRepo.GetBranches()
557+
brs, _, err := ctx.Repo.GitRepo.GetBranches(0, 0)
558558
if err != nil {
559559
ctx.ServerError("GetBranches", err)
560560
return
@@ -747,7 +747,7 @@ func RepoRefByType(refType RepoRefType) func(http.Handler) http.Handler {
747747
refName = ctx.Repo.Repository.DefaultBranch
748748
ctx.Repo.BranchName = refName
749749
if !ctx.Repo.GitRepo.IsBranchExist(refName) {
750-
brs, err := ctx.Repo.GitRepo.GetBranches()
750+
brs, _, err := ctx.Repo.GitRepo.GetBranches(0, 0)
751751
if err != nil {
752752
ctx.ServerError("GetBranches", err)
753753
return

modules/git/repo_branch.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,17 @@ func (repo *Repository) GetBranch(branch string) (*Branch, error) {
7878
}
7979

8080
// GetBranchesByPath returns a branch by it's path
81-
func GetBranchesByPath(path string) ([]*Branch, error) {
81+
// if limit = 0 it will not limit
82+
func GetBranchesByPath(path string, skip, limit int) ([]*Branch, int, error) {
8283
gitRepo, err := OpenRepository(path)
8384
if err != nil {
84-
return nil, err
85+
return nil, 0, err
8586
}
8687
defer gitRepo.Close()
8788

88-
brs, err := gitRepo.GetBranches()
89+
brs, countAll, err := gitRepo.GetBranches(skip, limit)
8990
if err != nil {
90-
return nil, err
91+
return nil, 0, err
9192
}
9293

9394
branches := make([]*Branch, len(brs))
@@ -99,7 +100,7 @@ func GetBranchesByPath(path string) ([]*Branch, error) {
99100
}
100101
}
101102

102-
return branches, nil
103+
return branches, countAll, nil
103104
}
104105

105106
// DeleteBranchOptions Option(s) for delete branch

modules/git/repo_branch_gogit.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,32 @@ func (repo *Repository) IsBranchExist(name string) bool {
2525
return reference.Type() != plumbing.InvalidReference
2626
}
2727

28-
// GetBranches returns all branches of the repository.
29-
func (repo *Repository) GetBranches() ([]string, error) {
28+
// GetBranches returns branches from the repository, skipping skip initial branches and
29+
// returning at most limit branches, or all branches if limit is 0.
30+
func (repo *Repository) GetBranches(skip, limit int) ([]string, int, error) {
3031
var branchNames []string
3132

3233
branches, err := repo.gogitRepo.Branches()
3334
if err != nil {
34-
return nil, err
35+
return nil, 0, err
3536
}
3637

38+
i := 0
39+
count := 0
3740
_ = branches.ForEach(func(branch *plumbing.Reference) error {
41+
count++
42+
if i < skip {
43+
i++
44+
return nil
45+
} else if limit != 0 && count > skip+limit {
46+
return nil
47+
}
48+
3849
branchNames = append(branchNames, strings.TrimPrefix(branch.Name().String(), BranchPrefix))
3950
return nil
4051
})
4152

4253
// TODO: Sort?
4354

44-
return branchNames, nil
55+
return branchNames, count, nil
4556
}

modules/git/repo_branch_nogogit.go

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ func (repo *Repository) IsBranchExist(name string) bool {
2121
return IsReferenceExist(repo.Path, BranchPrefix+name)
2222
}
2323

24-
// GetBranches returns all branches of the repository.
25-
func (repo *Repository) GetBranches() ([]string, error) {
26-
return callShowRef(repo.Path, BranchPrefix, "--heads")
24+
// GetBranches returns branches from the repository, skipping skip initial branches and
25+
// returning at most limit branches, or all branches if limit is 0.
26+
func (repo *Repository) GetBranches(skip, limit int) ([]string, int, error) {
27+
return callShowRef(repo.Path, BranchPrefix, "--heads", skip, limit)
2728
}
2829

29-
func callShowRef(repoPath, prefix, arg string) ([]string, error) {
30-
var branchNames []string
31-
30+
// callShowRef return refs, if limit = 0 it will not limit
31+
func callShowRef(repoPath, prefix, arg string, skip, limit int) (branchNames []string, countAll int, err error) {
3232
stdoutReader, stdoutWriter := io.Pipe()
3333
defer func() {
3434
_ = stdoutReader.Close()
@@ -49,8 +49,21 @@ func callShowRef(repoPath, prefix, arg string) ([]string, error) {
4949
}
5050
}()
5151

52+
i := 0
5253
bufReader := bufio.NewReader(stdoutReader)
53-
for {
54+
for i < skip {
55+
_, isPrefix, err := bufReader.ReadLine()
56+
if err == io.EOF {
57+
return branchNames, i, nil
58+
}
59+
if err != nil {
60+
return nil, 0, err
61+
}
62+
if !isPrefix {
63+
i++
64+
}
65+
}
66+
for limit == 0 || i < skip+limit {
5467
// The output of show-ref is simply a list:
5568
// <sha> SP <ref> LF
5669
_, err := bufReader.ReadSlice(' ')
@@ -59,24 +72,39 @@ func callShowRef(repoPath, prefix, arg string) ([]string, error) {
5972
_, err = bufReader.ReadSlice(' ')
6073
}
6174
if err == io.EOF {
62-
return branchNames, nil
75+
return branchNames, i, nil
6376
}
6477
if err != nil {
65-
return nil, err
78+
return nil, 0, err
6679
}
6780

6881
branchName, err := bufReader.ReadString('\n')
6982
if err == io.EOF {
7083
// This shouldn't happen... but we'll tolerate it for the sake of peace
71-
return branchNames, nil
84+
return branchNames, i, nil
7285
}
7386
if err != nil {
74-
return nil, err
87+
return nil, i, err
7588
}
7689
branchName = strings.TrimPrefix(branchName, prefix)
7790
if len(branchName) > 0 {
7891
branchName = branchName[:len(branchName)-1]
7992
}
8093
branchNames = append(branchNames, branchName)
94+
i++
95+
}
96+
// count all refs
97+
for limit != 0 {
98+
_, isPrefix, err := bufReader.ReadLine()
99+
if err == io.EOF {
100+
return branchNames, i, nil
101+
}
102+
if err != nil {
103+
return nil, 0, err
104+
}
105+
if !isPrefix {
106+
i++
107+
}
81108
}
109+
return branchNames, i, nil
82110
}

modules/git/repo_branch_test.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,26 @@ func TestRepository_GetBranches(t *testing.T) {
1717
assert.NoError(t, err)
1818
defer bareRepo1.Close()
1919

20-
branches, err := bareRepo1.GetBranches()
20+
branches, countAll, err := bareRepo1.GetBranches(0, 2)
21+
22+
assert.NoError(t, err)
23+
assert.Len(t, branches, 2)
24+
assert.EqualValues(t, 3, countAll)
25+
assert.ElementsMatch(t, []string{"branch1", "branch2"}, branches)
26+
27+
branches, countAll, err = bareRepo1.GetBranches(0, 0)
2128

2229
assert.NoError(t, err)
2330
assert.Len(t, branches, 3)
31+
assert.EqualValues(t, 3, countAll)
2432
assert.ElementsMatch(t, []string{"branch1", "branch2", "master"}, branches)
33+
34+
branches, countAll, err = bareRepo1.GetBranches(5, 1)
35+
36+
assert.NoError(t, err)
37+
assert.Len(t, branches, 0)
38+
assert.EqualValues(t, 3, countAll)
39+
assert.ElementsMatch(t, []string{}, branches)
2540
}
2641

2742
func BenchmarkRepository_GetBranches(b *testing.B) {
@@ -33,7 +48,7 @@ func BenchmarkRepository_GetBranches(b *testing.B) {
3348
defer bareRepo1.Close()
3449

3550
for i := 0; i < b.N; i++ {
36-
_, err := bareRepo1.GetBranches()
51+
_, _, err := bareRepo1.GetBranches(0, 0)
3752
if err != nil {
3853
b.Fatal(err)
3954
}

modules/git/repo_tag_nogogit.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func (repo *Repository) IsTagExist(name string) bool {
1313
}
1414

1515
// GetTags returns all tags of the repository.
16-
func (repo *Repository) GetTags() ([]string, error) {
17-
return callShowRef(repo.Path, TagPrefix, "--tags")
16+
func (repo *Repository) GetTags() (tags []string, err error) {
17+
tags, _, err = callShowRef(repo.Path, TagPrefix, "--tags", 0, 0)
18+
return
1819
}

modules/repository/branch.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import (
1313

1414
// GetBranch returns a branch by its name
1515
func GetBranch(repo *models.Repository, branch string) (*git.Branch, error) {
16+
if len(branch) == 0 {
17+
return nil, fmt.Errorf("GetBranch: empty string for branch")
18+
}
1619
gitRepo, err := git.OpenRepository(repo.RepoPath())
1720
if err != nil {
1821
return nil, err
@@ -22,9 +25,10 @@ func GetBranch(repo *models.Repository, branch string) (*git.Branch, error) {
2225
return gitRepo.GetBranch(branch)
2326
}
2427

25-
// GetBranches returns all the branches of a repository
26-
func GetBranches(repo *models.Repository) ([]*git.Branch, error) {
27-
return git.GetBranchesByPath(repo.RepoPath())
28+
// GetBranches returns branches from the repository, skipping skip initial branches and
29+
// returning at most limit branches, or all branches if limit is 0.
30+
func GetBranches(repo *models.Repository, skip, limit int) ([]*git.Branch, int, error) {
31+
return git.GetBranchesByPath(repo.RepoPath(), skip, limit)
2832
}
2933

3034
// checkBranchName validates branch name with existing repository branches
@@ -35,7 +39,7 @@ func checkBranchName(repo *models.Repository, name string) error {
3539
}
3640
defer gitRepo.Close()
3741

38-
branches, err := GetBranches(repo)
42+
branches, _, err := GetBranches(repo, 0, 0)
3943
if err != nil {
4044
return err
4145
}

modules/repository/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func adoptRepository(ctx models.DBContext, repoPath string, u *models.User, repo
239239

240240
repo.DefaultBranch = strings.TrimPrefix(repo.DefaultBranch, git.BranchPrefix)
241241
}
242-
branches, _ := gitRepo.GetBranches()
242+
branches, _, _ := gitRepo.GetBranches(0, 0)
243243
found := false
244244
hasDefault := false
245245
hasMaster := false

modules/util/paginate.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package util
6+
7+
import "reflect"
8+
9+
// PaginateSlice cut a slice as per pagination options
10+
// if page = 0 it do not paginate
11+
func PaginateSlice(list interface{}, page, pageSize int) interface{} {
12+
if page <= 0 || pageSize <= 0 {
13+
return list
14+
}
15+
if reflect.TypeOf(list).Kind() != reflect.Slice {
16+
return list
17+
}
18+
19+
listValue := reflect.ValueOf(list)
20+
21+
page--
22+
23+
if page*pageSize >= listValue.Len() {
24+
return listValue.Slice(listValue.Len(), listValue.Len()).Interface()
25+
}
26+
27+
listValue = listValue.Slice(page*pageSize, listValue.Len())
28+
29+
if listValue.Len() > pageSize {
30+
return listValue.Slice(0, pageSize).Interface()
31+
}
32+
33+
return listValue.Interface()
34+
}

modules/util/paginate_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package util
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestPaginateSlice(t *testing.T) {
14+
stringSlice := []string{"a", "b", "c", "d", "e"}
15+
result, ok := PaginateSlice(stringSlice, 1, 2).([]string)
16+
assert.True(t, ok)
17+
assert.EqualValues(t, []string{"a", "b"}, result)
18+
19+
result, ok = PaginateSlice(stringSlice, 100, 2).([]string)
20+
assert.True(t, ok)
21+
assert.EqualValues(t, []string{}, result)
22+
23+
result, ok = PaginateSlice(stringSlice, 3, 2).([]string)
24+
assert.True(t, ok)
25+
assert.EqualValues(t, []string{"e"}, result)
26+
27+
result, ok = PaginateSlice(stringSlice, 1, 0).([]string)
28+
assert.True(t, ok)
29+
assert.EqualValues(t, []string{"a", "b", "c", "d", "e"}, result)
30+
31+
result, ok = PaginateSlice(stringSlice, 1, -1).([]string)
32+
assert.True(t, ok)
33+
assert.EqualValues(t, []string{"a", "b", "c", "d", "e"}, result)
34+
35+
type Test struct {
36+
Val int
37+
}
38+
39+
var testVar = []*Test{{Val: 2}, {Val: 3}, {Val: 4}}
40+
testVar, ok = PaginateSlice(testVar, 1, 50).([]*Test)
41+
assert.True(t, ok)
42+
assert.EqualValues(t, []*Test{{Val: 2}, {Val: 3}, {Val: 4}}, testVar)
43+
44+
testVar, ok = PaginateSlice(testVar, 2, 2).([]*Test)
45+
assert.True(t, ok)
46+
assert.EqualValues(t, []*Test{{Val: 4}}, testVar)
47+
}

routers/api/v1/org/org.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"code.gitea.io/gitea/modules/context"
1414
"code.gitea.io/gitea/modules/convert"
1515
api "code.gitea.io/gitea/modules/structs"
16+
"code.gitea.io/gitea/modules/util"
1617
"code.gitea.io/gitea/modules/web"
1718
"code.gitea.io/gitea/routers/api/v1/user"
1819
"code.gitea.io/gitea/routers/api/v1/utils"
@@ -28,9 +29,9 @@ func listUserOrgs(ctx *context.APIContext, u *models.User) {
2829
ctx.Error(http.StatusInternalServerError, "GetOrgsByUserID", err)
2930
return
3031
}
31-
maxResults := len(orgs)
3232

33-
orgs = utils.PaginateUserSlice(orgs, listOptions.Page, listOptions.PageSize)
33+
maxResults := len(orgs)
34+
orgs, _ = util.PaginateSlice(orgs, listOptions.Page, listOptions.PageSize).([]*models.User)
3435

3536
apiOrgs := make([]*api.Organization, len(orgs))
3637
for i := range orgs {

0 commit comments

Comments
 (0)