Skip to content

enhancement of compare feature #25322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions modules/git/repo_branch_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package git

import (
"context"
"fmt"
"sort"
"strings"

Expand Down Expand Up @@ -95,6 +96,41 @@ func (repo *Repository) GetBranchNames(skip, limit int) ([]string, int, error) {
return branchNames, len(branchData), nil
}

// GetRemotetBranchNames returns branches from the repository remote, skipping "skip" initial branches and
// returning at most "limit" branches, or all branches if "limit" is 0.
func (repo *Repository) GetRemotetBranchNames(remote string, skip, limit int) ([]string, int, error) {
var branchNames []string

refs, err := repo.gogitRepo.References()
if err != nil {
return nil, 0, nil
}

i := 0
count := 0
refPrefix := fmt.Sprintf("refs/remotes/%s/", remote)

_ = refs.ForEach(func(ref *plumbing.Reference) error {
refName := ref.Name().String()
if !strings.HasPrefix(refName, refPrefix) {
return nil
}

count++
if i < skip {
i++
return nil
} else if limit != 0 && count > skip+limit {
return nil
}

branchNames = append(branchNames, strings.TrimPrefix(refName, refPrefix))
return nil
})

return branchNames, count, nil
}

// WalkReferences walks all the references from the repository
// refType should be empty, ObjectTag or ObjectBranch. All other values are equivalent to empty.
func WalkReferences(ctx context.Context, repoPath string, walkfn func(sha1, refname string) error) (int, error) {
Expand Down
9 changes: 9 additions & 0 deletions modules/git/repo_branch_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"strings"

Expand Down Expand Up @@ -65,6 +66,14 @@ func (repo *Repository) GetBranchNames(skip, limit int) ([]string, int, error) {
return callShowRef(repo.Ctx, repo.Path, BranchPrefix, TrustedCmdArgs{BranchPrefix, "--sort=-committerdate"}, skip, limit)
}

// GetRemotetBranchNames returns branches from the repository remote, skipping "skip" initial branches and
// returning at most "limit" branches, or all branches if "limit" is 0.
func (repo *Repository) GetRemotetBranchNames(remote string, skip, limit int) ([]string, int, error) {
refPrefix := fmt.Sprintf("refs/remotes/%s/", remote)

return callShowRef(repo.Ctx, repo.Path, refPrefix, ToTrustedCmdArgs([]string{refPrefix, "--sort=-committerdate"}), skip, limit)
}

// WalkReferences walks all the references from the repository
func WalkReferences(ctx context.Context, repoPath string, walkfn func(sha1, refname string) error) (int, error) {
return walkShowRef(ctx, repoPath, nil, 0, 0, walkfn)
Expand Down
8 changes: 8 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,7 @@ branch = Branch
tree = Tree
clear_ref = `Clear current reference`
filter_branch_and_tag = Filter branch or tag
filter_repo = Search repository
find_tag = Find tag
branches = Branches
tags = Tags
Expand Down Expand Up @@ -1679,6 +1680,13 @@ issues.reference_link = Reference: %s

compare.compare_base = base
compare.compare_head = compare
compare.mode.in_same_repo = Compare in same repository
compare.mode.across_repos = Compare across repositorys
compare.mode.across_service = Compare with external repository
compare.titile = Comparing %s
compare.button_title = Compare
compare.refs_not_exist = Head or base ref is not exist.
compare.head_info_not_exist = Head user or repository is not exist.

pulls.desc = Enable pull requests and code reviews.
pulls.new = New Pull Request
Expand Down
Loading