[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:
6543 2021-02-03 20:06:13 +01:00 committed by GitHub
parent c295a27d4a
commit 0d1444751f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 239 additions and 87 deletions

View file

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