From 31df012968bd0acb3ed6748c7bf0f99d8ecc4031 Mon Sep 17 00:00:00 2001 From: Cirno the Strongest <1447794+CirnoT@users.noreply.github.com> Date: Sat, 23 May 2020 21:49:48 +0200 Subject: [PATCH] Properly handle and return empty string for dangling commits in GetBranchName (#11587) --- models/issue_comment.go | 2 +- modules/git/commit.go | 7 ++++++- routers/repo/commit.go | 1 + templates/repo/commit_page.tmpl | 4 +++- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/models/issue_comment.go b/models/issue_comment.go index e23fae671..3f2ee8b7d 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 8c7732a26..61428d23a 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 a7a8d30d0..d87d220f2 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 f884bee7b..7a91663ec 100644 --- a/templates/repo/commit_page.tmpl +++ b/templates/repo/commit_page.tmpl @@ -27,7 +27,9 @@ {{if IsMultilineCommitMessage .Commit.Message}}
{{RenderCommitBody .Commit.Message $.RepoLink $.Repository.ComposeMetas}}
{{end}} - {{svg "octicon-git-branch" 16}}{{.BranchName}} + {{if .BranchName}} + {{svg "octicon-git-branch" 16}}{{.BranchName}} + {{end}}