Skip to content

Commit e07d089

Browse files
authored
Sort branches and tags by date descending (#21136)
This fixes #5709 and #17316 by changing the order of listed branches and tags to show the ones with latest commits atop. It's achieved with changing underlying "show-ref" git command with "for-each-ref" as suggested in https://stackoverflow.com/a/5188364 Also, it's passing format string so the output matches "show-ref" command output. close #5709 close #17316
1 parent 5933f04 commit e07d089

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

modules/git/repo_branch_nogogit.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (repo *Repository) IsBranchExist(name string) bool {
6363
// GetBranchNames returns branches from the repository, skipping skip initial branches and
6464
// returning at most limit branches, or all branches if limit is 0.
6565
func (repo *Repository) GetBranchNames(skip, limit int) ([]string, int, error) {
66-
return callShowRef(repo.Ctx, repo.Path, BranchPrefix, "--heads", skip, limit)
66+
return callShowRef(repo.Ctx, repo.Path, BranchPrefix, BranchPrefix+" --sort=-committerdate", skip, limit)
6767
}
6868

6969
// WalkReferences walks all the references from the repository
@@ -77,9 +77,9 @@ func (repo *Repository) WalkReferences(refType ObjectType, skip, limit int, walk
7777
var arg string
7878
switch refType {
7979
case ObjectTag:
80-
arg = "--tags"
80+
arg = TagPrefix + " --sort=-taggerdate"
8181
case ObjectBranch:
82-
arg = "--heads"
82+
arg = BranchPrefix + " --sort=-committerdate"
8383
default:
8484
arg = ""
8585
}
@@ -107,9 +107,9 @@ func walkShowRef(ctx context.Context, repoPath, arg string, skip, limit int, wal
107107

108108
go func() {
109109
stderrBuilder := &strings.Builder{}
110-
args := []string{"show-ref"}
110+
args := []string{"for-each-ref", "--format=%(objectname) %(refname)"}
111111
if arg != "" {
112-
args = append(args, arg)
112+
args = append(args, strings.Fields(arg)...)
113113
}
114114
err := NewCommand(ctx, args...).Run(&RunOpts{
115115
Dir: repoPath,

modules/git/repo_branch_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ func TestRepository_GetBranches(t *testing.T) {
2222
assert.NoError(t, err)
2323
assert.Len(t, branches, 2)
2424
assert.EqualValues(t, 3, countAll)
25-
assert.ElementsMatch(t, []string{"branch1", "branch2"}, branches)
25+
assert.ElementsMatch(t, []string{"master", "branch2"}, branches)
2626

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

2929
assert.NoError(t, err)
3030
assert.Len(t, branches, 3)
3131
assert.EqualValues(t, 3, countAll)
32-
assert.ElementsMatch(t, []string{"branch1", "branch2", "master"}, branches)
32+
assert.ElementsMatch(t, []string{"master", "branch2", "branch1"}, branches)
3333

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

modules/git/repo_tag_nogogit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (repo *Repository) IsTagExist(name string) bool {
2626
// GetTags returns all tags of the repository.
2727
// returning at most limit tags, or all if limit is 0.
2828
func (repo *Repository) GetTags(skip, limit int) (tags []string, err error) {
29-
tags, _, err = callShowRef(repo.Ctx, repo.Path, TagPrefix, "--tags", skip, limit)
29+
tags, _, err = callShowRef(repo.Ctx, repo.Path, TagPrefix, TagPrefix+" --sort=-taggerdate", skip, limit)
3030
return tags, err
3131
}
3232

0 commit comments

Comments
 (0)