Skip to content

Commit b682a2c

Browse files
Show exact tag for commit on diff view (#11846)
* Show exact tag for commit on diff view * Fix comment Co-authored-by: techknowlogick <[email protected]>
1 parent e282fbe commit b682a2c

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

modules/git/commit.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ func (c *Commit) GetSubModule(entryname string) (*SubModule, error) {
466466
return nil, nil
467467
}
468468

469-
// GetBranchName gets the closes branch name (as returned by 'git name-rev --name-only')
469+
// GetBranchName gets the closest branch name (as returned by 'git name-rev --name-only')
470470
func (c *Commit) GetBranchName() (string, error) {
471471
data, err := NewCommand("name-rev", "--exclude", "refs/tags/*", "--name-only", "--no-undefined", c.ID.String()).RunInDir(c.repo.Path)
472472
if err != nil {
@@ -482,6 +482,21 @@ func (c *Commit) GetBranchName() (string, error) {
482482
return strings.SplitN(strings.TrimSpace(data), "~", 2)[0], nil
483483
}
484484

485+
// GetTagName gets the current tag name for given commit
486+
func (c *Commit) GetTagName() (string, error) {
487+
data, err := NewCommand("describe", "--exact-match", "--tags", c.ID.String()).RunInDir(c.repo.Path)
488+
if err != nil {
489+
// handle special case where there is no tag for this commit
490+
if strings.Contains(err.Error(), "no tag exactly matches") {
491+
return "", nil
492+
}
493+
494+
return "", err
495+
}
496+
497+
return strings.TrimSpace(data), nil
498+
}
499+
485500
// CommitFileStatus represents status of files in a commit.
486501
type CommitFileStatus struct {
487502
Added []string

routers/repo/commit.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,12 @@ func Diff(ctx *context.Context) {
311311
ctx.ServerError("commit.GetBranchName", err)
312312
return
313313
}
314+
315+
ctx.Data["TagName"], err = commit.GetTagName()
316+
if err != nil {
317+
ctx.ServerError("commit.GetTagName", err)
318+
return
319+
}
314320
ctx.HTML(200, tplCommitPage)
315321
}
316322

templates/repo/commit_page.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
{{if .BranchName}}
3131
<span class="text grey">{{svg "octicon-git-branch" 16}}{{.BranchName}}</span>
3232
{{end}}
33+
{{if .TagName}}
34+
<span class="text grey">{{svg "octicon-tag" 16}}{{.TagName}}</span>
35+
{{end}}
3336
</div>
3437
<div class="ui attached info segment {{$class}}">
3538
<div class="ui stackable grid">

0 commit comments

Comments
 (0)