mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
[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 <art27@cantab.net> Co-authored-by: a1012112796 <1012112796@qq.com>
This commit is contained in:
@@ -78,16 +78,17 @@ func (repo *Repository) GetBranch(branch string) (*Branch, error) {
|
||||
}
|
||||
|
||||
// GetBranchesByPath returns a branch by it's path
|
||||
func GetBranchesByPath(path string) ([]*Branch, error) {
|
||||
// if limit = 0 it will not limit
|
||||
func GetBranchesByPath(path string, skip, limit int) ([]*Branch, int, error) {
|
||||
gitRepo, err := OpenRepository(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
defer gitRepo.Close()
|
||||
|
||||
brs, err := gitRepo.GetBranches()
|
||||
brs, countAll, err := gitRepo.GetBranches(skip, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
branches := make([]*Branch, len(brs))
|
||||
@@ -99,7 +100,7 @@ func GetBranchesByPath(path string) ([]*Branch, error) {
|
||||
}
|
||||
}
|
||||
|
||||
return branches, nil
|
||||
return branches, countAll, nil
|
||||
}
|
||||
|
||||
// DeleteBranchOptions Option(s) for delete branch
|
||||
|
@@ -25,21 +25,32 @@ func (repo *Repository) IsBranchExist(name string) bool {
|
||||
return reference.Type() != plumbing.InvalidReference
|
||||
}
|
||||
|
||||
// GetBranches returns all branches of the repository.
|
||||
func (repo *Repository) GetBranches() ([]string, error) {
|
||||
// GetBranches returns branches from the repository, skipping skip initial branches and
|
||||
// returning at most limit branches, or all branches if limit is 0.
|
||||
func (repo *Repository) GetBranches(skip, limit int) ([]string, int, error) {
|
||||
var branchNames []string
|
||||
|
||||
branches, err := repo.gogitRepo.Branches()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
i := 0
|
||||
count := 0
|
||||
_ = branches.ForEach(func(branch *plumbing.Reference) error {
|
||||
count++
|
||||
if i < skip {
|
||||
i++
|
||||
return nil
|
||||
} else if limit != 0 && count > skip+limit {
|
||||
return nil
|
||||
}
|
||||
|
||||
branchNames = append(branchNames, strings.TrimPrefix(branch.Name().String(), BranchPrefix))
|
||||
return nil
|
||||
})
|
||||
|
||||
// TODO: Sort?
|
||||
|
||||
return branchNames, nil
|
||||
return branchNames, count, nil
|
||||
}
|
||||
|
@@ -21,14 +21,14 @@ func (repo *Repository) IsBranchExist(name string) bool {
|
||||
return IsReferenceExist(repo.Path, BranchPrefix+name)
|
||||
}
|
||||
|
||||
// GetBranches returns all branches of the repository.
|
||||
func (repo *Repository) GetBranches() ([]string, error) {
|
||||
return callShowRef(repo.Path, BranchPrefix, "--heads")
|
||||
// GetBranches returns branches from the repository, skipping skip initial branches and
|
||||
// returning at most limit branches, or all branches if limit is 0.
|
||||
func (repo *Repository) GetBranches(skip, limit int) ([]string, int, error) {
|
||||
return callShowRef(repo.Path, BranchPrefix, "--heads", skip, limit)
|
||||
}
|
||||
|
||||
func callShowRef(repoPath, prefix, arg string) ([]string, error) {
|
||||
var branchNames []string
|
||||
|
||||
// callShowRef return refs, if limit = 0 it will not limit
|
||||
func callShowRef(repoPath, prefix, arg string, skip, limit int) (branchNames []string, countAll int, err error) {
|
||||
stdoutReader, stdoutWriter := io.Pipe()
|
||||
defer func() {
|
||||
_ = stdoutReader.Close()
|
||||
@@ -49,8 +49,21 @@ func callShowRef(repoPath, prefix, arg string) ([]string, error) {
|
||||
}
|
||||
}()
|
||||
|
||||
i := 0
|
||||
bufReader := bufio.NewReader(stdoutReader)
|
||||
for {
|
||||
for i < skip {
|
||||
_, isPrefix, err := bufReader.ReadLine()
|
||||
if err == io.EOF {
|
||||
return branchNames, i, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if !isPrefix {
|
||||
i++
|
||||
}
|
||||
}
|
||||
for limit == 0 || i < skip+limit {
|
||||
// The output of show-ref is simply a list:
|
||||
// <sha> SP <ref> LF
|
||||
_, err := bufReader.ReadSlice(' ')
|
||||
@@ -59,24 +72,39 @@ func callShowRef(repoPath, prefix, arg string) ([]string, error) {
|
||||
_, err = bufReader.ReadSlice(' ')
|
||||
}
|
||||
if err == io.EOF {
|
||||
return branchNames, nil
|
||||
return branchNames, i, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
branchName, err := bufReader.ReadString('\n')
|
||||
if err == io.EOF {
|
||||
// This shouldn't happen... but we'll tolerate it for the sake of peace
|
||||
return branchNames, nil
|
||||
return branchNames, i, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, i, err
|
||||
}
|
||||
branchName = strings.TrimPrefix(branchName, prefix)
|
||||
if len(branchName) > 0 {
|
||||
branchName = branchName[:len(branchName)-1]
|
||||
}
|
||||
branchNames = append(branchNames, branchName)
|
||||
i++
|
||||
}
|
||||
// count all refs
|
||||
for limit != 0 {
|
||||
_, isPrefix, err := bufReader.ReadLine()
|
||||
if err == io.EOF {
|
||||
return branchNames, i, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if !isPrefix {
|
||||
i++
|
||||
}
|
||||
}
|
||||
return branchNames, i, nil
|
||||
}
|
||||
|
@@ -17,11 +17,26 @@ func TestRepository_GetBranches(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
defer bareRepo1.Close()
|
||||
|
||||
branches, err := bareRepo1.GetBranches()
|
||||
branches, countAll, err := bareRepo1.GetBranches(0, 2)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, branches, 2)
|
||||
assert.EqualValues(t, 3, countAll)
|
||||
assert.ElementsMatch(t, []string{"branch1", "branch2"}, branches)
|
||||
|
||||
branches, countAll, err = bareRepo1.GetBranches(0, 0)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, branches, 3)
|
||||
assert.EqualValues(t, 3, countAll)
|
||||
assert.ElementsMatch(t, []string{"branch1", "branch2", "master"}, branches)
|
||||
|
||||
branches, countAll, err = bareRepo1.GetBranches(5, 1)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, branches, 0)
|
||||
assert.EqualValues(t, 3, countAll)
|
||||
assert.ElementsMatch(t, []string{}, branches)
|
||||
}
|
||||
|
||||
func BenchmarkRepository_GetBranches(b *testing.B) {
|
||||
@@ -33,7 +48,7 @@ func BenchmarkRepository_GetBranches(b *testing.B) {
|
||||
defer bareRepo1.Close()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := bareRepo1.GetBranches()
|
||||
_, _, err := bareRepo1.GetBranches(0, 0)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
@@ -13,6 +13,7 @@ func (repo *Repository) IsTagExist(name string) bool {
|
||||
}
|
||||
|
||||
// GetTags returns all tags of the repository.
|
||||
func (repo *Repository) GetTags() ([]string, error) {
|
||||
return callShowRef(repo.Path, TagPrefix, "--tags")
|
||||
func (repo *Repository) GetTags() (tags []string, err error) {
|
||||
tags, _, err = callShowRef(repo.Path, TagPrefix, "--tags", 0, 0)
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user