From b682a2c1b2d5efde24378858453903b5b89d6df8 Mon Sep 17 00:00:00 2001 From: Cirno the Strongest <1447794+CirnoT@users.noreply.github.com> Date: Thu, 11 Jun 2020 21:42:55 +0200 Subject: [PATCH] Show exact tag for commit on diff view (#11846) * Show exact tag for commit on diff view * Fix comment Co-authored-by: techknowlogick --- modules/git/commit.go | 17 ++++++++++++++++- routers/repo/commit.go | 6 ++++++ templates/repo/commit_page.tmpl | 3 +++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/modules/git/commit.go b/modules/git/commit.go index a114c902d..d6448bf26 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -466,7 +466,7 @@ func (c *Commit) GetSubModule(entryname string) (*SubModule, error) { return nil, nil } -// GetBranchName gets the closes branch name (as returned by 'git name-rev --name-only') +// GetBranchName gets the closest branch name (as returned by 'git name-rev --name-only') func (c *Commit) GetBranchName() (string, error) { data, err := NewCommand("name-rev", "--exclude", "refs/tags/*", "--name-only", "--no-undefined", c.ID.String()).RunInDir(c.repo.Path) if err != nil { @@ -482,6 +482,21 @@ func (c *Commit) GetBranchName() (string, error) { return strings.SplitN(strings.TrimSpace(data), "~", 2)[0], nil } +// GetTagName gets the current tag name for given commit +func (c *Commit) GetTagName() (string, error) { + data, err := NewCommand("describe", "--exact-match", "--tags", c.ID.String()).RunInDir(c.repo.Path) + if err != nil { + // handle special case where there is no tag for this commit + if strings.Contains(err.Error(), "no tag exactly matches") { + return "", nil + } + + return "", err + } + + return strings.TrimSpace(data), nil +} + // CommitFileStatus represents status of files in a commit. type CommitFileStatus struct { Added []string diff --git a/routers/repo/commit.go b/routers/repo/commit.go index 2b6c9cece..004d4915d 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -311,6 +311,12 @@ func Diff(ctx *context.Context) { ctx.ServerError("commit.GetBranchName", err) return } + + ctx.Data["TagName"], err = commit.GetTagName() + if err != nil { + ctx.ServerError("commit.GetTagName", err) + return + } ctx.HTML(200, tplCommitPage) } diff --git a/templates/repo/commit_page.tmpl b/templates/repo/commit_page.tmpl index 7a967ee55..51f56574c 100644 --- a/templates/repo/commit_page.tmpl +++ b/templates/repo/commit_page.tmpl @@ -30,6 +30,9 @@ {{if .BranchName}} {{svg "octicon-git-branch" 16}}{{.BranchName}} {{end}} + {{if .TagName}} + {{svg "octicon-tag" 16}}{{.TagName}} + {{end}}