]> source.dussan.org Git - gitea.git/commitdiff
Add tag name in the commits list (#31082)
authorLunny Xiao <xiaolunwen@gmail.com>
Mon, 19 Aug 2024 17:04:06 +0000 (01:04 +0800)
committerGitHub <noreply@github.com>
Mon, 19 Aug 2024 17:04:06 +0000 (17:04 +0000)
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">

models/migrations/migrations.go
models/migrations/v1_23/v304.go [new file with mode: 0644]
models/repo/release.go
models/repo/release_test.go
options/locale/locale_en-US.ini
routers/web/repo/commit.go
templates/repo/commits_list.tmpl
templates/repo/graph/commits.tmpl
templates/repo/tag/name.tmpl [new file with mode: 0644]

index a3264160e54183d2dd9652289bb408251e697ab9..13551423ce4703ee64aa8f0119801e079a77fff2 100644 (file)
@@ -599,6 +599,8 @@ var migrations = []Migration{
        NewMigration("Add index to action_task stopped log_expired", v1_23.AddIndexToActionTaskStoppedLogExpired),
        // v303 -> v304
        NewMigration("Add metadata column for comment table", v1_23.AddCommentMetaDataColumn),
+       // v304 -> v305
+       NewMigration("Add index for release sha1", v1_23.AddIndexForReleaseSha1),
 }
 
 // GetCurrentDBVersion returns the current db version
diff --git a/models/migrations/v1_23/v304.go b/models/migrations/v1_23/v304.go
new file mode 100644 (file)
index 0000000..65cffed
--- /dev/null
@@ -0,0 +1,13 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package v1_23 //nolint
+
+import "xorm.io/xorm"
+
+func AddIndexForReleaseSha1(x *xorm.Engine) error {
+       type Release struct {
+               Sha1 string `xorm:"INDEX VARCHAR(64)"`
+       }
+       return x.Sync(new(Release))
+}
index 3123edd9780812f94d82074fb952d1ca3533c509..1643258301144e85d51a005064942843523a9eac 100644 (file)
@@ -77,7 +77,7 @@ type Release struct {
        Target           string
        TargetBehind     string `xorm:"-"` // to handle non-existing or empty target
        Title            string
-       Sha1             string `xorm:"VARCHAR(64)"`
+       Sha1             string `xorm:"INDEX VARCHAR(64)"`
        NumCommits       int64
        NumCommitsBehind int64              `xorm:"-"`
        Note             string             `xorm:"TEXT"`
@@ -537,3 +537,17 @@ func InsertReleases(ctx context.Context, rels ...*Release) error {
 
        return committer.Commit()
 }
+
+func FindTagsByCommitIDs(ctx context.Context, repoID int64, commitIDs ...string) (map[string][]*Release, error) {
+       releases := make([]*Release, 0, len(commitIDs))
+       if err := db.GetEngine(ctx).Where("repo_id=?", repoID).
+               In("sha1", commitIDs).
+               Find(&releases); err != nil {
+               return nil, err
+       }
+       res := make(map[string][]*Release, len(releases))
+       for _, r := range releases {
+               res[r.Sha1] = append(res[r.Sha1], r)
+       }
+       return res, nil
+}
index 3643bff7f16ef70d55e97478aaf1fee51707809b..41ea083229d72a154c589978ef2476291dac0d82 100644 (file)
@@ -25,3 +25,16 @@ func TestMigrate_InsertReleases(t *testing.T) {
        err := InsertReleases(db.DefaultContext, r)
        assert.NoError(t, err)
 }
+
+func Test_FindTagsByCommitIDs(t *testing.T) {
+       assert.NoError(t, unittest.PrepareTestDatabase())
+
+       sha1Rels, err := FindTagsByCommitIDs(db.DefaultContext, 1, "65f1bf27bc3bf70f64657658635e66094edbcb4d")
+       assert.NoError(t, err)
+       assert.Len(t, sha1Rels, 1)
+       rels := sha1Rels["65f1bf27bc3bf70f64657658635e66094edbcb4d"]
+       assert.Len(t, rels, 3)
+       assert.Equal(t, "v1.1", rels[0].TagName)
+       assert.Equal(t, "delete-tag", rels[1].TagName)
+       assert.Equal(t, "v1.0", rels[2].TagName)
+}
index ef7628967c56f40b7978883fe68f7f00d8347c94..042fd549a0b3010e37c1fc35eb80863b185f0481 100644 (file)
@@ -1274,6 +1274,7 @@ commit_graph.color = Color
 commit.contained_in = This commit is contained in:
 commit.contained_in_default_branch = This commit is part of the default branch
 commit.load_referencing_branches_and_tags = Load branches and tags referencing this commit
+commit.load_tags_failed = Load tags failed because of internal error
 blame = Blame
 download_file = Download file
 normal_view = Normal View
index e5d84db61793375e85055c4267ec4178eba6ebc6..c91d5fad0565e10ff53548ff57670068431afa97 100644 (file)
@@ -83,7 +83,17 @@ func Commits(ctx *context.Context) {
                return
        }
        ctx.Data["Commits"] = processGitCommits(ctx, commits)
-
+       commitIDs := make([]string, 0, len(commits))
+       for _, c := range commits {
+               commitIDs = append(commitIDs, c.ID.String())
+       }
+       commitsTagsMap, err := repo_model.FindTagsByCommitIDs(ctx, ctx.Repo.Repository.ID, commitIDs...)
+       if err != nil {
+               log.Error("FindTagsByCommitIDs: %v", err)
+               ctx.Flash.Error(ctx.Tr("repo.commit.load_tags_failed"))
+       } else {
+               ctx.Data["CommitsTagsMap"] = commitsTagsMap
+       }
        ctx.Data["Username"] = ctx.Repo.Owner.Name
        ctx.Data["Reponame"] = ctx.Repo.Repository.Name
        ctx.Data["CommitCount"] = commitsCount
index bb5d2a03947785ccdbbd11deab1380da71c0cdb1..917a445fde9170a17bdf065a48d4f710f25a1cc2 100644 (file)
                                                {{if IsMultilineCommitMessage .Message}}
                                                <pre class="commit-body tw-hidden">{{RenderCommitBody $.Context .Message ($.Repository.ComposeMetas ctx)}}</pre>
                                                {{end}}
+                                               {{if $.CommitsTagsMap}}
+                                                       {{range (index $.CommitsTagsMap .ID.String)}}
+                                                               {{- template "repo/tag/name" dict "RepoLink" $.Repository.Link "TagName" .TagName "IsRelease" (not .IsTag) -}}
+                                                       {{end}}
+                                               {{end}}
                                        </td>
                                        {{if .Committer}}
                                                <td class="text right aligned">{{TimeSince .Committer.When ctx.Locale}}</td>
index 39b86d9a1642d479885afe0e4918a0c2d8cdd272..9b179552df86c03f6879b015ccfc53e304cee988 100644 (file)
@@ -42,9 +42,7 @@
                                                                        </a>
                                                                {{end}}
                                                        {{else if eq $refGroup "tags"}}
-                                                               <a class="ui labelled basic tiny button" href="{{$.RepoLink}}/src/tag/{{.ShortName|PathEscape}}">
-                                                                       {{svg "octicon-tag"}} {{.ShortName}}
-                                                               </a>
+                                                               {{- template "repo/tag/name" dict "RepoLink" $.Repository.Link "TagName" .ShortName -}}
                                                        {{else if eq $refGroup "remotes"}}
                                                                <a class="ui labelled basic tiny button" href="{{$.RepoLink}}/src/commit/{{$commit.Rev|PathEscape}}">
                                                                        {{svg "octicon-cross-reference"}} {{.ShortName}}
diff --git a/templates/repo/tag/name.tmpl b/templates/repo/tag/name.tmpl
new file mode 100644 (file)
index 0000000..c304201
--- /dev/null
@@ -0,0 +1,3 @@
+<a class="ui label basic tiny button{{if .IsRelease}} primary{{end}}" href="{{.RepoLink}}/src/tag/{{.TagName|PathEscape}}">
+{{svg "octicon-tag"}} {{.TagName}}
+</a>