Skip to content
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

Sort branches and tags by date descending #21136

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 5 additions & 5 deletions modules/git/repo_branch_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (repo *Repository) IsBranchExist(name string) bool {
// GetBranchNames 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) GetBranchNames(skip, limit int) ([]string, int, error) {
return callShowRef(repo.Ctx, repo.Path, BranchPrefix, "--heads", skip, limit)
return callShowRef(repo.Ctx, repo.Path, BranchPrefix, "refs/heads --sort=-committerdate", skip, limit)
sergemedvid marked this conversation as resolved.
Show resolved Hide resolved
}

// WalkReferences walks all the references from the repository
Expand All @@ -77,9 +77,9 @@ func (repo *Repository) WalkReferences(refType ObjectType, skip, limit int, walk
var arg string
switch refType {
case ObjectTag:
arg = "--tags"
arg = "refs/tags --sort=-taggerdate"
case ObjectBranch:
arg = "--heads"
arg = "refs/heads --sort=-committerdate"
default:
arg = ""
}
Expand Down Expand Up @@ -107,9 +107,9 @@ func walkShowRef(ctx context.Context, repoPath, arg string, skip, limit int, wal

go func() {
stderrBuilder := &strings.Builder{}
args := []string{"show-ref"}
args := []string{"for-each-ref", "--format=%(objectname) %(refname)"}
if arg != "" {
args = append(args, arg)
args = append(args, strings.Fields(arg)...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Splitting arguments by space is dangerous.

}
err := NewCommand(ctx, args...).Run(&RunOpts{
Dir: repoPath,
Expand Down
4 changes: 2 additions & 2 deletions modules/git/repo_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ func TestRepository_GetBranches(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, branches, 2)
assert.EqualValues(t, 3, countAll)
assert.ElementsMatch(t, []string{"branch1", "branch2"}, branches)
assert.ElementsMatch(t, []string{"master", "branch2"}, branches)

branches, countAll, err = bareRepo1.GetBranchNames(0, 0)

assert.NoError(t, err)
assert.Len(t, branches, 3)
assert.EqualValues(t, 3, countAll)
assert.ElementsMatch(t, []string{"branch1", "branch2", "master"}, branches)
assert.ElementsMatch(t, []string{"master", "branch2", "branch1"}, branches)

branches, countAll, err = bareRepo1.GetBranchNames(5, 1)

Expand Down
2 changes: 1 addition & 1 deletion modules/git/repo_tag_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (repo *Repository) IsTagExist(name string) bool {
// GetTags returns all tags of the repository.
// returning at most limit tags, or all if limit is 0.
func (repo *Repository) GetTags(skip, limit int) (tags []string, err error) {
tags, _, err = callShowRef(repo.Ctx, repo.Path, TagPrefix, "--tags", skip, limit)
tags, _, err = callShowRef(repo.Ctx, repo.Path, TagPrefix, "refs/tags --sort=-taggerdate", skip, limit)
return tags, err
}

Expand Down