Skip to content

Commit ccf7366

Browse files
authored
Add tag name in the commits list (#31082)
Fix #10036 This PR adds some labels for tags of this commit after the commit message on the commits table. The tag template is share as commit graph's. Desktop: <img width="1302" alt="image" src="https://github.com/go-gitea/gitea/assets/81045/ba94e1e6-2a3d-44f3-85a3-575fb5667c97"> Mobile: <img width="370" alt="image" src="https://github.com/go-gitea/gitea/assets/81045/e3eb1f44-3686-4012-aa9d-52cd88b22c0e">
1 parent 4f5c966 commit ccf7366

File tree

9 files changed

+64
-5
lines changed

9 files changed

+64
-5
lines changed

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,8 @@ var migrations = []Migration{
599599
NewMigration("Add index to action_task stopped log_expired", v1_23.AddIndexToActionTaskStoppedLogExpired),
600600
// v303 -> v304
601601
NewMigration("Add metadata column for comment table", v1_23.AddCommentMetaDataColumn),
602+
// v304 -> v305
603+
NewMigration("Add index for release sha1", v1_23.AddIndexForReleaseSha1),
602604
}
603605

604606
// GetCurrentDBVersion returns the current db version

models/migrations/v1_23/v304.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_23 //nolint
5+
6+
import "xorm.io/xorm"
7+
8+
func AddIndexForReleaseSha1(x *xorm.Engine) error {
9+
type Release struct {
10+
Sha1 string `xorm:"INDEX VARCHAR(64)"`
11+
}
12+
return x.Sync(new(Release))
13+
}

models/repo/release.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ type Release struct {
7777
Target string
7878
TargetBehind string `xorm:"-"` // to handle non-existing or empty target
7979
Title string
80-
Sha1 string `xorm:"VARCHAR(64)"`
80+
Sha1 string `xorm:"INDEX VARCHAR(64)"`
8181
NumCommits int64
8282
NumCommitsBehind int64 `xorm:"-"`
8383
Note string `xorm:"TEXT"`
@@ -537,3 +537,17 @@ func InsertReleases(ctx context.Context, rels ...*Release) error {
537537

538538
return committer.Commit()
539539
}
540+
541+
func FindTagsByCommitIDs(ctx context.Context, repoID int64, commitIDs ...string) (map[string][]*Release, error) {
542+
releases := make([]*Release, 0, len(commitIDs))
543+
if err := db.GetEngine(ctx).Where("repo_id=?", repoID).
544+
In("sha1", commitIDs).
545+
Find(&releases); err != nil {
546+
return nil, err
547+
}
548+
res := make(map[string][]*Release, len(releases))
549+
for _, r := range releases {
550+
res[r.Sha1] = append(res[r.Sha1], r)
551+
}
552+
return res, nil
553+
}

models/repo/release_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,16 @@ func TestMigrate_InsertReleases(t *testing.T) {
2525
err := InsertReleases(db.DefaultContext, r)
2626
assert.NoError(t, err)
2727
}
28+
29+
func Test_FindTagsByCommitIDs(t *testing.T) {
30+
assert.NoError(t, unittest.PrepareTestDatabase())
31+
32+
sha1Rels, err := FindTagsByCommitIDs(db.DefaultContext, 1, "65f1bf27bc3bf70f64657658635e66094edbcb4d")
33+
assert.NoError(t, err)
34+
assert.Len(t, sha1Rels, 1)
35+
rels := sha1Rels["65f1bf27bc3bf70f64657658635e66094edbcb4d"]
36+
assert.Len(t, rels, 3)
37+
assert.Equal(t, "v1.1", rels[0].TagName)
38+
assert.Equal(t, "delete-tag", rels[1].TagName)
39+
assert.Equal(t, "v1.0", rels[2].TagName)
40+
}

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,7 @@ commit_graph.color = Color
12741274
commit.contained_in = This commit is contained in:
12751275
commit.contained_in_default_branch = This commit is part of the default branch
12761276
commit.load_referencing_branches_and_tags = Load branches and tags referencing this commit
1277+
commit.load_tags_failed = Load tags failed because of internal error
12771278
blame = Blame
12781279
download_file = Download file
12791280
normal_view = Normal View

routers/web/repo/commit.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,17 @@ func Commits(ctx *context.Context) {
8383
return
8484
}
8585
ctx.Data["Commits"] = processGitCommits(ctx, commits)
86-
86+
commitIDs := make([]string, 0, len(commits))
87+
for _, c := range commits {
88+
commitIDs = append(commitIDs, c.ID.String())
89+
}
90+
commitsTagsMap, err := repo_model.FindTagsByCommitIDs(ctx, ctx.Repo.Repository.ID, commitIDs...)
91+
if err != nil {
92+
log.Error("FindTagsByCommitIDs: %v", err)
93+
ctx.Flash.Error(ctx.Tr("repo.commit.load_tags_failed"))
94+
} else {
95+
ctx.Data["CommitsTagsMap"] = commitsTagsMap
96+
}
8797
ctx.Data["Username"] = ctx.Repo.Owner.Name
8898
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
8999
ctx.Data["CommitCount"] = commitsCount

templates/repo/commits_list.tmpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@
7272
{{if IsMultilineCommitMessage .Message}}
7373
<pre class="commit-body tw-hidden">{{RenderCommitBody $.Context .Message ($.Repository.ComposeMetas ctx)}}</pre>
7474
{{end}}
75+
{{if $.CommitsTagsMap}}
76+
{{range (index $.CommitsTagsMap .ID.String)}}
77+
{{- template "repo/tag/name" dict "RepoLink" $.Repository.Link "TagName" .TagName "IsRelease" (not .IsTag) -}}
78+
{{end}}
79+
{{end}}
7580
</td>
7681
{{if .Committer}}
7782
<td class="text right aligned">{{TimeSince .Committer.When ctx.Locale}}</td>

templates/repo/graph/commits.tmpl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@
4242
</a>
4343
{{end}}
4444
{{else if eq $refGroup "tags"}}
45-
<a class="ui labelled basic tiny button" href="{{$.RepoLink}}/src/tag/{{.ShortName|PathEscape}}">
46-
{{svg "octicon-tag"}} {{.ShortName}}
47-
</a>
45+
{{- template "repo/tag/name" dict "RepoLink" $.Repository.Link "TagName" .ShortName -}}
4846
{{else if eq $refGroup "remotes"}}
4947
<a class="ui labelled basic tiny button" href="{{$.RepoLink}}/src/commit/{{$commit.Rev|PathEscape}}">
5048
{{svg "octicon-cross-reference"}} {{.ShortName}}

templates/repo/tag/name.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<a class="ui label basic tiny button{{if .IsRelease}} primary{{end}}" href="{{.RepoLink}}/src/tag/{{.TagName|PathEscape}}">
2+
{{svg "octicon-tag"}} {{.TagName}}
3+
</a>

0 commit comments

Comments
 (0)