From 7bfb873d66c2ef02736d0542af98a98ff297d6eb Mon Sep 17 00:00:00 2001 From: Serge Medvid Date: Fri, 9 Sep 2022 20:51:30 +0300 Subject: [PATCH 1/4] Sort branches and tags by date descending (#5709, #17316) --- modules/git/repo_branch_nogogit.go | 10 +++++----- modules/git/repo_tag_nogogit.go | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/git/repo_branch_nogogit.go b/modules/git/repo_branch_nogogit.go index 2983a35ca56f6..ba6e66ab89e1e 100644 --- a/modules/git/repo_branch_nogogit.go +++ b/modules/git/repo_branch_nogogit.go @@ -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) } // 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 switch refType { case ObjectTag: - arg = "--tags" + arg = "refs/tags --sort=-taggerdate" case ObjectBranch: - arg = "--heads" + arg = "refs/heads --sort=-committerdate" default: arg = "" } @@ -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)...) } err := NewCommand(ctx, args...).Run(&RunOpts{ Dir: repoPath, diff --git a/modules/git/repo_tag_nogogit.go b/modules/git/repo_tag_nogogit.go index 9a574666f82c5..33f0f8062fe51 100644 --- a/modules/git/repo_tag_nogogit.go +++ b/modules/git/repo_tag_nogogit.go @@ -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 } From 9e56f19b6cb51179aecd2b2bac0f885035903007 Mon Sep 17 00:00:00 2001 From: Serge Medvid Date: Sat, 10 Sep 2022 15:53:29 +0300 Subject: [PATCH 2/4] Fix unit tests; branches are now showing by commit date, descending --- modules/git/repo_branch_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/git/repo_branch_test.go b/modules/git/repo_branch_test.go index 56f7387097df7..58a738e28bc63 100644 --- a/modules/git/repo_branch_test.go +++ b/modules/git/repo_branch_test.go @@ -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) From d021bff1dbfde95ea96fb81b1a7ec357f69bde1f Mon Sep 17 00:00:00 2001 From: Serge Medvid Date: Tue, 13 Sep 2022 19:11:20 +0300 Subject: [PATCH 3/4] Replace hardcoded strings with predefined variables --- modules/git/repo_branch_nogogit.go | 6 +++--- modules/git/repo_tag_nogogit.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/git/repo_branch_nogogit.go b/modules/git/repo_branch_nogogit.go index ba6e66ab89e1e..f6b9ebe97fba7 100644 --- a/modules/git/repo_branch_nogogit.go +++ b/modules/git/repo_branch_nogogit.go @@ -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, "refs/heads --sort=-committerdate", skip, limit) + return callShowRef(repo.Ctx, repo.Path, BranchPrefix, BranchPrefix + " --sort=-committerdate", skip, limit) } // 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 switch refType { case ObjectTag: - arg = "refs/tags --sort=-taggerdate" + arg = TagPrefix + " --sort=-taggerdate" case ObjectBranch: - arg = "refs/heads --sort=-committerdate" + arg = BranchPrefix + " --sort=-committerdate" default: arg = "" } diff --git a/modules/git/repo_tag_nogogit.go b/modules/git/repo_tag_nogogit.go index 33f0f8062fe51..5e0f751362232 100644 --- a/modules/git/repo_tag_nogogit.go +++ b/modules/git/repo_tag_nogogit.go @@ -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, "refs/tags --sort=-taggerdate", skip, limit) + tags, _, err = callShowRef(repo.Ctx, repo.Path, TagPrefix, TagPrefix + " --sort=-taggerdate", skip, limit) return tags, err } From 30fdb7f9cc96cd80a98d21b342ca02ced15994e6 Mon Sep 17 00:00:00 2001 From: Serge Medvid Date: Tue, 13 Sep 2022 19:52:23 +0300 Subject: [PATCH 4/4] Fix formatting with the previous commit --- modules/git/repo_branch_nogogit.go | 2 +- modules/git/repo_tag_nogogit.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/git/repo_branch_nogogit.go b/modules/git/repo_branch_nogogit.go index f6b9ebe97fba7..f5fb244cb4990 100644 --- a/modules/git/repo_branch_nogogit.go +++ b/modules/git/repo_branch_nogogit.go @@ -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, BranchPrefix + " --sort=-committerdate", skip, limit) + return callShowRef(repo.Ctx, repo.Path, BranchPrefix, BranchPrefix+" --sort=-committerdate", skip, limit) } // WalkReferences walks all the references from the repository diff --git a/modules/git/repo_tag_nogogit.go b/modules/git/repo_tag_nogogit.go index 5e0f751362232..c2df8b14fdaeb 100644 --- a/modules/git/repo_tag_nogogit.go +++ b/modules/git/repo_tag_nogogit.go @@ -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, TagPrefix + " --sort=-taggerdate", skip, limit) + tags, _, err = callShowRef(repo.Ctx, repo.Path, TagPrefix, TagPrefix+" --sort=-taggerdate", skip, limit) return tags, err }