From 084c9c62f1ede777f2606947c7a4b443d60f85ad Mon Sep 17 00:00:00 2001 From: Chongyi Zheng Date: Wed, 13 Jul 2022 14:34:39 -0400 Subject: [PATCH 1/8] Handle branch name with prefix already --- modules/git/repo_branch_nogogit.go | 5 ++++- modules/git/repo_commit.go | 10 ++++++++-- routers/web/repo/release.go | 3 ++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/modules/git/repo_branch_nogogit.go b/modules/git/repo_branch_nogogit.go index 2983a35ca56f6..aaae26fa27351 100644 --- a/modules/git/repo_branch_nogogit.go +++ b/modules/git/repo_branch_nogogit.go @@ -57,7 +57,10 @@ func (repo *Repository) IsBranchExist(name string) bool { return false } - return repo.IsReferenceExist(BranchPrefix + name) + if !strings.HasPrefix(name, BranchPrefix) { + name = BranchPrefix + name + } + return repo.IsReferenceExist(name) } // GetBranchNames returns branches from the repository, skipping skip initial branches and diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index e6fec4d1a32e2..182989eed91b4 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -16,12 +16,18 @@ import ( // GetBranchCommitID returns last commit ID string of given branch. func (repo *Repository) GetBranchCommitID(name string) (string, error) { - return repo.GetRefCommitID(BranchPrefix + name) + if !strings.HasPrefix(name, BranchPrefix) { + name = BranchPrefix + name + } + return repo.GetRefCommitID(name) } // GetTagCommitID returns last commit ID string of given tag. func (repo *Repository) GetTagCommitID(name string) (string, error) { - return repo.GetRefCommitID(TagPrefix + name) + if !strings.HasPrefix(name, TagPrefix) { + name = TagPrefix + name + } + return repo.GetRefCommitID(name) } // GetCommit returns commit object of by ID string. diff --git a/routers/web/repo/release.go b/routers/web/repo/release.go index ab87c3e2385af..820f89e21366a 100644 --- a/routers/web/repo/release.go +++ b/routers/web/repo/release.go @@ -16,6 +16,7 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" @@ -35,7 +36,7 @@ const ( // calReleaseNumCommitsBehind calculates given release has how many commits behind release target. func calReleaseNumCommitsBehind(repoCtx *context.Repository, release *models.Release, countCache map[string]int64) error { // Fast return if release target is same as default branch. - if repoCtx.BranchName == release.Target { + if repoCtx.BranchName == strings.TrimPrefix(release.Target, git.BranchPrefix) { release.NumCommitsBehind = repoCtx.CommitsCount - release.NumCommits return nil } From 05134fbf2c02bfaf8503d1060559a7c83db703a5 Mon Sep 17 00:00:00 2001 From: Chongyi Zheng Date: Thu, 14 Jul 2022 16:09:47 -0400 Subject: [PATCH 2/8] Allow refname resolution in Releases page only --- modules/git/repo_branch_nogogit.go | 19 ++++++++++++++++--- modules/git/repo_commit.go | 20 ++++++++++++-------- routers/web/repo/release.go | 17 +++++++++++------ 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/modules/git/repo_branch_nogogit.go b/modules/git/repo_branch_nogogit.go index aaae26fa27351..6b791e4b0b6aa 100644 --- a/modules/git/repo_branch_nogogit.go +++ b/modules/git/repo_branch_nogogit.go @@ -57,10 +57,23 @@ func (repo *Repository) IsBranchExist(name string) bool { return false } - if !strings.HasPrefix(name, BranchPrefix) { - name = BranchPrefix + name + return repo.IsReferenceExist(BranchPrefix + name) +} + +// ResolveBranch resolves an ambiguous branch name to its explicit name, and if the branch exists +func (repo *Repository) ResolveBranch(name string) (string, bool) { + if name == "" { + return "", false } - return repo.IsReferenceExist(name) + + if repo.IsReferenceExist(name) { + return name, true + } + if repo.IsReferenceExist(BranchPrefix + name) { + return BranchPrefix + name, true + } + + return "", false } // GetBranchNames returns branches from the repository, skipping skip initial branches and diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index 182989eed91b4..b5cf13da35983 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -16,18 +16,12 @@ import ( // GetBranchCommitID returns last commit ID string of given branch. func (repo *Repository) GetBranchCommitID(name string) (string, error) { - if !strings.HasPrefix(name, BranchPrefix) { - name = BranchPrefix + name - } - return repo.GetRefCommitID(name) + return repo.GetRefCommitID(BranchPrefix + name) } // GetTagCommitID returns last commit ID string of given tag. func (repo *Repository) GetTagCommitID(name string) (string, error) { - if !strings.HasPrefix(name, TagPrefix) { - name = TagPrefix + name - } - return repo.GetRefCommitID(name) + return repo.GetRefCommitID(TagPrefix + name) } // GetCommit returns commit object of by ID string. @@ -58,6 +52,16 @@ func (repo *Repository) GetTagCommit(name string) (*Commit, error) { return repo.GetCommit(commitID) } +// GetRefCommit get the commit of then given reference. +// The refname has to be explicit, e.g. "refs/heads/main" +func (repo *Repository) GetRefCommit(name string) (*Commit, error) { + commitID, err := repo.GetRefCommitID(name) + if err != nil { + return nil, err + } + return repo.GetCommit(commitID) +} + func (repo *Repository) getCommitByPathWithID(id SHA1, relpath string) (*Commit, error) { // File name starts with ':' must be escaped. if relpath[0] == ':' { diff --git a/routers/web/repo/release.go b/routers/web/repo/release.go index 820f89e21366a..841dfa4b91b94 100644 --- a/routers/web/repo/release.go +++ b/routers/web/repo/release.go @@ -16,7 +16,6 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" - "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" @@ -36,19 +35,25 @@ const ( // calReleaseNumCommitsBehind calculates given release has how many commits behind release target. func calReleaseNumCommitsBehind(repoCtx *context.Repository, release *models.Release, countCache map[string]int64) error { // Fast return if release target is same as default branch. - if repoCtx.BranchName == strings.TrimPrefix(release.Target, git.BranchPrefix) { + if repoCtx.BranchName == release.Target { release.NumCommitsBehind = repoCtx.CommitsCount - release.NumCommits return nil } // Get count if not exists if _, ok := countCache[release.Target]; !ok { - if repoCtx.GitRepo.IsBranchExist(release.Target) { - commit, err := repoCtx.GitRepo.GetBranchCommit(release.Target) + if branchName, exists := repoCtx.GitRepo.ResolveBranch(release.Target); exists { + if repoCtx.BranchName == branchName { + countCache[branchName] = repoCtx.CommitsCount + release.NumCommitsBehind = repoCtx.CommitsCount - release.NumCommits + return nil + } + + commit, err := repoCtx.GitRepo.GetRefCommit(branchName) if err != nil { - return fmt.Errorf("GetBranchCommit: %v", err) + return fmt.Errorf("GetRefCommit: %v", err) } - countCache[release.Target], err = commit.CommitsCount() + countCache[branchName], err = commit.CommitsCount() if err != nil { return fmt.Errorf("CommitsCount: %v", err) } From 98c466b874142b37e7bc5e5a9676fcad6f1e5689 Mon Sep 17 00:00:00 2001 From: Chongyi Zheng Date: Thu, 14 Jul 2022 16:17:46 -0400 Subject: [PATCH 3/8] Update comment --- modules/git/repo_branch_nogogit.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/git/repo_branch_nogogit.go b/modules/git/repo_branch_nogogit.go index 6b791e4b0b6aa..349a07033ce47 100644 --- a/modules/git/repo_branch_nogogit.go +++ b/modules/git/repo_branch_nogogit.go @@ -60,7 +60,8 @@ func (repo *Repository) IsBranchExist(name string) bool { return repo.IsReferenceExist(BranchPrefix + name) } -// ResolveBranch resolves an ambiguous branch name to its explicit name, and if the branch exists +// ResolveBranch resolves an ambiguous branch name to its explicit name, and if the branch exists. +// i.e. "main" -> "refs/heads/main" func (repo *Repository) ResolveBranch(name string) (string, bool) { if name == "" { return "", false From 6e1ee49c77c236bc376dced5fb2c71ead6a8f7a6 Mon Sep 17 00:00:00 2001 From: Chongyi Zheng Date: Thu, 14 Jul 2022 16:21:49 -0400 Subject: [PATCH 4/8] Implement the same method for gogit --- modules/git/repo_branch_gogit.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/modules/git/repo_branch_gogit.go b/modules/git/repo_branch_gogit.go index dc295765629bc..8f5de0ee26911 100644 --- a/modules/git/repo_branch_gogit.go +++ b/modules/git/repo_branch_gogit.go @@ -51,6 +51,23 @@ func (repo *Repository) IsBranchExist(name string) bool { return reference.Type() != plumbing.InvalidReference } +// ResolveBranch resolves an ambiguous branch name to its explicit name, and if the branch exists. +// i.e. "main" -> "refs/heads/main" +func (repo *Repository) ResolveBranch(name string) (string, bool) { + if name == "" { + return "", false + } + + if repo.IsReferenceExist(name) { + return name, true + } + if repo.IsReferenceExist(BranchPrefix + name) { + return BranchPrefix + name, true + } + + return "", false +} + // GetBranches 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) { From c3351dc8a7e770ddbbad2a9dda9b96aac207cacb Mon Sep 17 00:00:00 2001 From: harryzcy Date: Fri, 22 Jul 2022 15:26:17 -0400 Subject: [PATCH 5/8] Ensure reference has BranchPrefix --- modules/git/repo_branch_gogit.go | 2 +- modules/git/repo_branch_nogogit.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/git/repo_branch_gogit.go b/modules/git/repo_branch_gogit.go index 8f5de0ee26911..808ce1f4b3ff1 100644 --- a/modules/git/repo_branch_gogit.go +++ b/modules/git/repo_branch_gogit.go @@ -58,7 +58,7 @@ func (repo *Repository) ResolveBranch(name string) (string, bool) { return "", false } - if repo.IsReferenceExist(name) { + if strings.HasPrefix(name, BranchPrefix) && repo.IsReferenceExist(name) { return name, true } if repo.IsReferenceExist(BranchPrefix + name) { diff --git a/modules/git/repo_branch_nogogit.go b/modules/git/repo_branch_nogogit.go index 349a07033ce47..76a7f76af298d 100644 --- a/modules/git/repo_branch_nogogit.go +++ b/modules/git/repo_branch_nogogit.go @@ -67,7 +67,7 @@ func (repo *Repository) ResolveBranch(name string) (string, bool) { return "", false } - if repo.IsReferenceExist(name) { + if strings.HasPrefix(name, BranchPrefix) && repo.IsReferenceExist(name) { return name, true } if repo.IsReferenceExist(BranchPrefix + name) { From 4fd2ff53b8324633a7cfb04d94406602d2dd0cbc Mon Sep 17 00:00:00 2001 From: harryzcy Date: Fri, 22 Jul 2022 21:14:05 -0400 Subject: [PATCH 6/8] Revert all changes --- modules/git/repo_branch_gogit.go | 17 ----------------- modules/git/repo_branch_nogogit.go | 17 ----------------- modules/git/repo_commit.go | 10 ---------- routers/web/repo/release.go | 14 ++++---------- 4 files changed, 4 insertions(+), 54 deletions(-) diff --git a/modules/git/repo_branch_gogit.go b/modules/git/repo_branch_gogit.go index 808ce1f4b3ff1..dc295765629bc 100644 --- a/modules/git/repo_branch_gogit.go +++ b/modules/git/repo_branch_gogit.go @@ -51,23 +51,6 @@ func (repo *Repository) IsBranchExist(name string) bool { return reference.Type() != plumbing.InvalidReference } -// ResolveBranch resolves an ambiguous branch name to its explicit name, and if the branch exists. -// i.e. "main" -> "refs/heads/main" -func (repo *Repository) ResolveBranch(name string) (string, bool) { - if name == "" { - return "", false - } - - if strings.HasPrefix(name, BranchPrefix) && repo.IsReferenceExist(name) { - return name, true - } - if repo.IsReferenceExist(BranchPrefix + name) { - return BranchPrefix + name, true - } - - return "", false -} - // GetBranches 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) { diff --git a/modules/git/repo_branch_nogogit.go b/modules/git/repo_branch_nogogit.go index 76a7f76af298d..2983a35ca56f6 100644 --- a/modules/git/repo_branch_nogogit.go +++ b/modules/git/repo_branch_nogogit.go @@ -60,23 +60,6 @@ func (repo *Repository) IsBranchExist(name string) bool { return repo.IsReferenceExist(BranchPrefix + name) } -// ResolveBranch resolves an ambiguous branch name to its explicit name, and if the branch exists. -// i.e. "main" -> "refs/heads/main" -func (repo *Repository) ResolveBranch(name string) (string, bool) { - if name == "" { - return "", false - } - - if strings.HasPrefix(name, BranchPrefix) && repo.IsReferenceExist(name) { - return name, true - } - if repo.IsReferenceExist(BranchPrefix + name) { - return BranchPrefix + name, true - } - - return "", false -} - // 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) { diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index b5cf13da35983..e6fec4d1a32e2 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -52,16 +52,6 @@ func (repo *Repository) GetTagCommit(name string) (*Commit, error) { return repo.GetCommit(commitID) } -// GetRefCommit get the commit of then given reference. -// The refname has to be explicit, e.g. "refs/heads/main" -func (repo *Repository) GetRefCommit(name string) (*Commit, error) { - commitID, err := repo.GetRefCommitID(name) - if err != nil { - return nil, err - } - return repo.GetCommit(commitID) -} - func (repo *Repository) getCommitByPathWithID(id SHA1, relpath string) (*Commit, error) { // File name starts with ':' must be escaped. if relpath[0] == ':' { diff --git a/routers/web/repo/release.go b/routers/web/repo/release.go index 841dfa4b91b94..c8f34a3c837f7 100644 --- a/routers/web/repo/release.go +++ b/routers/web/repo/release.go @@ -42,20 +42,14 @@ func calReleaseNumCommitsBehind(repoCtx *context.Repository, release *models.Rel // Get count if not exists if _, ok := countCache[release.Target]; !ok { - if branchName, exists := repoCtx.GitRepo.ResolveBranch(release.Target); exists { - if repoCtx.BranchName == branchName { - countCache[branchName] = repoCtx.CommitsCount - release.NumCommitsBehind = repoCtx.CommitsCount - release.NumCommits - return nil - } - - commit, err := repoCtx.GitRepo.GetRefCommit(branchName) + if repoCtx.GitRepo.IsBranchExist(release.Target) { + commit, err := repoCtx.GitRepo.GetBranchCommit(release.Target) if err != nil { return fmt.Errorf("GetRefCommit: %v", err) } - countCache[branchName], err = commit.CommitsCount() + countCache[release.Target], err = commit.CommitsCount() if err != nil { - return fmt.Errorf("CommitsCount: %v", err) + return fmt.Errorf("GetBranchCommit: %v", err) } } else { // Use NumCommits of the newest release on that target From e4aedf218e670b01920b92baed731129a0fc1bc1 Mon Sep 17 00:00:00 2001 From: harryzcy Date: Fri, 22 Jul 2022 21:14:38 -0400 Subject: [PATCH 7/8] Revert errors --- routers/web/repo/release.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/web/repo/release.go b/routers/web/repo/release.go index c8f34a3c837f7..ab87c3e2385af 100644 --- a/routers/web/repo/release.go +++ b/routers/web/repo/release.go @@ -45,11 +45,11 @@ func calReleaseNumCommitsBehind(repoCtx *context.Repository, release *models.Rel if repoCtx.GitRepo.IsBranchExist(release.Target) { commit, err := repoCtx.GitRepo.GetBranchCommit(release.Target) if err != nil { - return fmt.Errorf("GetRefCommit: %v", err) + return fmt.Errorf("GetBranchCommit: %v", err) } countCache[release.Target], err = commit.CommitsCount() if err != nil { - return fmt.Errorf("GetBranchCommit: %v", err) + return fmt.Errorf("CommitsCount: %v", err) } } else { // Use NumCommits of the newest release on that target From ba30dbd0883db7bdb360426e6af7e2746329cde7 Mon Sep 17 00:00:00 2001 From: harryzcy Date: Fri, 22 Jul 2022 21:22:40 -0400 Subject: [PATCH 8/8] Convert github release commitish during migration --- services/migrations/github.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/services/migrations/github.go b/services/migrations/github.go index 5f5b430fa9e07..049fd1dfb3c41 100644 --- a/services/migrations/github.go +++ b/services/migrations/github.go @@ -15,6 +15,7 @@ import ( "strings" "time" + "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" base "code.gitea.io/gitea/modules/migration" "code.gitea.io/gitea/modules/proxy" @@ -291,10 +292,14 @@ func (g *GithubDownloaderV3) GetLabels() ([]*base.Label, error) { } func (g *GithubDownloaderV3) convertGithubRelease(rel *github.RepositoryRelease) *base.Release { + // GitHub allows commitish to be a reference. + // In this case, we need to remove the prefix, i.e. convert "refs/heads/main" to "main". + targetCommitish := strings.TrimPrefix(rel.GetTargetCommitish(), git.BranchPrefix) + r := &base.Release{ Name: rel.GetName(), TagName: rel.GetTagName(), - TargetCommitish: rel.GetTargetCommitish(), + TargetCommitish: targetCommitish, Draft: rel.GetDraft(), Prerelease: rel.GetPrerelease(), Created: rel.GetCreatedAt().Time,