Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-29 13:16:10 +01:00

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
This commit is contained in:
sergemedvid 2022-09-14 15:11:24 +03:00 committed by GitHub
parent 5933f04094
commit e07d089be0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 8 deletions

View file

@ -63,7 +63,7 @@ func (repo *Repository) IsBranchExist(name string) bool {
// GetBranchNames returns branches from the repository, skipping skip initial branches and // GetBranchNames returns branches from the repository, skipping skip initial branches and
// returning at most limit branches, or all branches if limit is 0. // returning at most limit branches, or all branches if limit is 0.
func (repo *Repository) GetBranchNames(skip, limit int) ([]string, int, error) { 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, BranchPrefix+" --sort=-committerdate", skip, limit)
} }
// WalkReferences walks all the references from the repository // WalkReferences walks all the references from the repository
@ -77,9 +77,9 @@ func (repo *Repository) WalkReferences(refType ObjectType, skip, limit int, walk
var arg string var arg string
switch refType { switch refType {
case ObjectTag: case ObjectTag:
arg = "--tags" arg = TagPrefix + " --sort=-taggerdate"
case ObjectBranch: case ObjectBranch:
arg = "--heads" arg = BranchPrefix + " --sort=-committerdate"
default: default:
arg = "" arg = ""
} }
@ -107,9 +107,9 @@ func walkShowRef(ctx context.Context, repoPath, arg string, skip, limit int, wal
go func() { go func() {
stderrBuilder := &strings.Builder{} stderrBuilder := &strings.Builder{}
args := []string{"show-ref"} args := []string{"for-each-ref", "--format=%(objectname) %(refname)"}
if arg != "" { if arg != "" {
args = append(args, arg) args = append(args, strings.Fields(arg)...)
} }
err := NewCommand(ctx, args...).Run(&RunOpts{ err := NewCommand(ctx, args...).Run(&RunOpts{
Dir: repoPath, Dir: repoPath,

View file

@ -22,14 +22,14 @@ func TestRepository_GetBranches(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, branches, 2) assert.Len(t, branches, 2)
assert.EqualValues(t, 3, countAll) 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) branches, countAll, err = bareRepo1.GetBranchNames(0, 0)
assert.NoError(t, err) assert.NoError(t, err)
assert.Len(t, branches, 3) assert.Len(t, branches, 3)
assert.EqualValues(t, 3, countAll) 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) branches, countAll, err = bareRepo1.GetBranchNames(5, 1)

View file

@ -26,7 +26,7 @@ func (repo *Repository) IsTagExist(name string) bool {
// GetTags returns all tags of the repository. // GetTags returns all tags of the repository.
// returning at most limit tags, or all if limit is 0. // returning at most limit tags, or all if limit is 0.
func (repo *Repository) GetTags(skip, limit int) (tags []string, err error) { 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, TagPrefix+" --sort=-taggerdate", skip, limit)
return tags, err return tags, err
} }