Skip to content

Commit

Permalink
Properly handle and return empty string for dangling commits in GetBr…
Browse files Browse the repository at this point in the history
…anchName (go-gitea#11587)
  • Loading branch information
CirnoT authored and Yohann Delafollye committed Jul 31, 2020
1 parent d1147d6 commit 69ba7e0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion modules/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions routers/repo/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 3 additions & 1 deletion templates/repo/commit_page.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down

0 comments on commit 69ba7e0

Please sign in to comment.