Skip to content

Commit 31df012

Browse files
authored
Properly handle and return empty string for dangling commits in GetBranchName (#11587)
1 parent 723b199 commit 31df012

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

models/issue_comment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ func getCommitIDsFromRepo(repo *Repository, oldCommitID, newCommitID, baseBranch
11291129
return nil, false, err
11301130
}
11311131

1132-
if oldCommitBranch == "undefined" {
1132+
if oldCommitBranch == "" {
11331133
commitIDs = make([]string, 2)
11341134
commitIDs[0] = oldCommitID
11351135
commitIDs[1] = newCommitID

modules/git/commit.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,13 @@ func (c *Commit) GetSubModule(entryname string) (*SubModule, error) {
468468

469469
// GetBranchName gets the closes branch name (as returned by 'git name-rev --name-only')
470470
func (c *Commit) GetBranchName() (string, error) {
471-
data, err := NewCommand("name-rev", "--name-only", c.ID.String()).RunInDir(c.repo.Path)
471+
data, err := NewCommand("name-rev", "--name-only", "--no-undefined", c.ID.String()).RunInDir(c.repo.Path)
472472
if err != nil {
473+
// handle special case where git can not describe commit
474+
if strings.Contains(err.Error(), "cannot describe") {
475+
return "", nil
476+
}
477+
473478
return "", err
474479
}
475480

routers/repo/commit.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ func Diff(ctx *context.Context) {
309309
ctx.Data["BranchName"], err = commit.GetBranchName()
310310
if err != nil {
311311
ctx.ServerError("commit.GetBranchName", err)
312+
return
312313
}
313314
ctx.HTML(200, tplCommitPage)
314315
}

templates/repo/commit_page.tmpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
{{if IsMultilineCommitMessage .Commit.Message}}
2828
<pre class="commit-body">{{RenderCommitBody .Commit.Message $.RepoLink $.Repository.ComposeMetas}}</pre>
2929
{{end}}
30-
<span class="text grey">{{svg "octicon-git-branch" 16}}{{.BranchName}}</span>
30+
{{if .BranchName}}
31+
<span class="text grey">{{svg "octicon-git-branch" 16}}{{.BranchName}}</span>
32+
{{end}}
3133
</div>
3234
<div class="ui attached info segment {{$class}}">
3335
<div class="ui stackable grid">

0 commit comments

Comments
 (0)