Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show exact tag for commit on diff view #11846

Merged
merged 4 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion modules/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions routers/repo/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
3 changes: 3 additions & 0 deletions templates/repo/commit_page.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
{{if .BranchName}}
<span class="text grey">{{svg "octicon-git-branch" 16}}{{.BranchName}}</span>
{{end}}
{{if .TagName}}
<span class="text grey">{{svg "octicon-tag" 16}}{{.TagName}}</span>
{{end}}
</div>
<div class="ui attached info segment {{$class}}">
<div class="ui stackable grid">
Expand Down