aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCirno the Strongest <1447794+CirnoT@users.noreply.github.com>2020-05-23 21:49:48 +0200
committerGitHub <noreply@github.com>2020-05-23 20:49:48 +0100
commit31df012968bd0acb3ed6748c7bf0f99d8ecc4031 (patch)
treed8a7a2798249129205734e086cd8f01e773de582
parent723b1992711dc55548f0014552cc3b0a98bb86e8 (diff)
downloadgitea-31df012968bd0acb3ed6748c7bf0f99d8ecc4031.tar.gz
gitea-31df012968bd0acb3ed6748c7bf0f99d8ecc4031.zip
Properly handle and return empty string for dangling commits in GetBranchName (#11587)
-rw-r--r--models/issue_comment.go2
-rw-r--r--modules/git/commit.go7
-rw-r--r--routers/repo/commit.go1
-rw-r--r--templates/repo/commit_page.tmpl4
4 files changed, 11 insertions, 3 deletions
diff --git a/models/issue_comment.go b/models/issue_comment.go
index e23fae6715..3f2ee8b7d9 100644
--- a/models/issue_comment.go
+++ b/models/issue_comment.go
@@ -1129,7 +1129,7 @@ func getCommitIDsFromRepo(repo *Repository, oldCommitID, newCommitID, baseBranch
return nil, false, err
}
- if oldCommitBranch == "undefined" {
+ if oldCommitBranch == "" {
commitIDs = make([]string, 2)
commitIDs[0] = oldCommitID
commitIDs[1] = newCommitID
diff --git a/modules/git/commit.go b/modules/git/commit.go
index 8c7732a26b..61428d23a4 100644
--- a/modules/git/commit.go
+++ b/modules/git/commit.go
@@ -468,8 +468,13 @@ func (c *Commit) GetSubModule(entryname string) (*SubModule, error) {
// GetBranchName gets the closes branch name (as returned by 'git name-rev --name-only')
func (c *Commit) GetBranchName() (string, error) {
- data, err := NewCommand("name-rev", "--name-only", c.ID.String()).RunInDir(c.repo.Path)
+ data, err := NewCommand("name-rev", "--name-only", "--no-undefined", c.ID.String()).RunInDir(c.repo.Path)
if err != nil {
+ // handle special case where git can not describe commit
+ if strings.Contains(err.Error(), "cannot describe") {
+ return "", nil
+ }
+
return "", err
}
diff --git a/routers/repo/commit.go b/routers/repo/commit.go
index a7a8d30d09..d87d220f24 100644
--- a/routers/repo/commit.go
+++ b/routers/repo/commit.go
@@ -309,6 +309,7 @@ func Diff(ctx *context.Context) {
ctx.Data["BranchName"], err = commit.GetBranchName()
if err != nil {
ctx.ServerError("commit.GetBranchName", err)
+ return
}
ctx.HTML(200, tplCommitPage)
}
diff --git a/templates/repo/commit_page.tmpl b/templates/repo/commit_page.tmpl
index f884bee7be..7a91663ec8 100644
--- a/templates/repo/commit_page.tmpl
+++ b/templates/repo/commit_page.tmpl
@@ -27,7 +27,9 @@
{{if IsMultilineCommitMessage .Commit.Message}}
<pre class="commit-body">{{RenderCommitBody .Commit.Message $.RepoLink $.Repository.ComposeMetas}}</pre>
{{end}}
- <span class="text grey">{{svg "octicon-git-branch" 16}}{{.BranchName}}</span>
+ {{if .BranchName}}
+ <span class="text grey">{{svg "octicon-git-branch" 16}}{{.BranchName}}</span>
+ {{end}}
</div>
<div class="ui attached info segment {{$class}}">
<div class="ui stackable grid">