diff options
author | zeripath <art27@cantab.net> | 2021-12-08 19:08:16 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-08 19:08:16 +0000 |
commit | 9e6e1dc950f06bbd000d5b6438f39113e8902082 (patch) | |
tree | 204f359885c2bda09603de7de90b93f61c8e6922 /modules/git/repo_branch_gogit.go | |
parent | b59875aa123f2cc3a5026d30ac557e99c05603a6 (diff) | |
download | gitea-9e6e1dc950f06bbd000d5b6438f39113e8902082.tar.gz gitea-9e6e1dc950f06bbd000d5b6438f39113e8902082.zip |
Improve checkBranchName (#17901)
The current implementation of checkBranchName is highly inefficient
involving opening the repository, the listing all of the branch names
checking them individually before then using using opened repo to get
the tags.
This PR avoids this by simply walking the references from show-ref
instead of opening the repository (in the nogogit case).
Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'modules/git/repo_branch_gogit.go')
-rw-r--r-- | modules/git/repo_branch_gogit.go | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/modules/git/repo_branch_gogit.go b/modules/git/repo_branch_gogit.go index 6bf14b3999..d159aafd6f 100644 --- a/modules/git/repo_branch_gogit.go +++ b/modules/git/repo_branch_gogit.go @@ -9,6 +9,7 @@ package git import ( + "context" "strings" "github.com/go-git/go-git/v5/plumbing" @@ -52,7 +53,7 @@ func (repo *Repository) IsBranchExist(name string) bool { // 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) { +func (repo *Repository) GetBranchNames(skip, limit int) ([]string, int, error) { var branchNames []string branches, err := repo.gogitRepo.Branches() @@ -79,3 +80,26 @@ func (repo *Repository) GetBranches(skip, limit int) ([]string, int, error) { return branchNames, count, nil } + +// WalkReferences walks all the references from the repository +func WalkReferences(ctx context.Context, repoPath string, walkfn func(string) error) (int, error) { + repo, err := OpenRepositoryCtx(ctx, repoPath) + if err != nil { + return 0, err + } + defer repo.Close() + + i := 0 + iter, err := repo.gogitRepo.References() + if err != nil { + return i, err + } + defer iter.Close() + + err = iter.ForEach(func(ref *plumbing.Reference) error { + err := walkfn(string(ref.Name())) + i++ + return err + }) + return i, err +} |