From 1a7102800a8b1d3331a104bfec70c1bbf5483870 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 26 May 2024 12:26:37 +0800 Subject: [PATCH 1/8] Add tag name in the commits list --- models/repo/release.go | 13 +++++++++++++ routers/web/repo/commit.go | 12 +++++++++++- templates/repo/commits_list.tmpl | 3 +++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/models/repo/release.go b/models/repo/release.go index a9f65f6c3e886..91f3f1687c3fc 100644 --- a/models/repo/release.go +++ b/models/repo/release.go @@ -563,3 +563,16 @@ 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 +} diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index a2c6ac33e8f8e..40795c50855af 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -81,8 +81,18 @@ func Commits(ctx *context.Context) { ctx.ServerError("CommitsByRange", err) return } - ctx.Data["Commits"] = git_model.ConvertFromGitCommit(ctx, commits, ctx.Repo.Repository) + ctx.Data["Commits"] = git_model.ConvertFromGitCommit(ctx, commits, ctx.Repo.Repository) + 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 { + ctx.ServerError("CommitsByRange", err) + return + } + ctx.Data["CommitsTagsMap"] = commitsTagsMap ctx.Data["Username"] = ctx.Repo.Owner.Name ctx.Data["Reponame"] = ctx.Repo.Repository.Name ctx.Data["CommitCount"] = commitsCount diff --git a/templates/repo/commits_list.tmpl b/templates/repo/commits_list.tmpl index bb5d2a0394778..4f562b5da0761 100644 --- a/templates/repo/commits_list.tmpl +++ b/templates/repo/commits_list.tmpl @@ -72,6 +72,9 @@ {{if IsMultilineCommitMessage .Message}}
{{RenderCommitBody $.Context .Message ($.Repository.ComposeMetas ctx)}}
{{end}} + {{range (index $.CommitsTagsMap .ID.String)}} + {{.TagName}} + {{end}} {{if .Committer}} {{TimeSince .Committer.When ctx.Locale}} From 303d00cfa23721320bcbfaf6f571a33af4ff080f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 26 May 2024 12:38:13 +0800 Subject: [PATCH 2/8] Add test for FindTagsByCommitIDs --- models/repo/release.go | 3 ++- models/repo/release_test.go | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/models/repo/release.go b/models/repo/release.go index 91f3f1687c3fc..f9c82ea69f81e 100644 --- a/models/repo/release.go +++ b/models/repo/release.go @@ -567,7 +567,8 @@ func InsertReleases(ctx context.Context, rels ...*Release) error { 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 { + In("sha1", commitIDs). + Find(&releases); err != nil { return nil, err } res := make(map[string][]*Release, len(releases)) diff --git a/models/repo/release_test.go b/models/repo/release_test.go index 3643bff7f16ef..41ea083229d72 100644 --- a/models/repo/release_test.go +++ b/models/repo/release_test.go @@ -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) +} From 7b824a551327aebd9081aca8750076899b094fe3 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 26 May 2024 12:56:03 +0800 Subject: [PATCH 3/8] Add index for release sha1 --- models/migrations/migrations.go | 4 ++++ models/migrations/v1_23/v300.go | 13 +++++++++++++ models/repo/release.go | 2 +- 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 models/migrations/v1_23/v300.go diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 4501585250597..77d38c07295e7 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -21,6 +21,7 @@ import ( "code.gitea.io/gitea/models/migrations/v1_20" "code.gitea.io/gitea/models/migrations/v1_21" "code.gitea.io/gitea/models/migrations/v1_22" + "code.gitea.io/gitea/models/migrations/v1_23" "code.gitea.io/gitea/models/migrations/v1_6" "code.gitea.io/gitea/models/migrations/v1_7" "code.gitea.io/gitea/models/migrations/v1_8" @@ -587,6 +588,9 @@ var migrations = []Migration{ NewMigration("Drop wrongly created table o_auth2_application", v1_22.DropWronglyCreatedTable), // Gitea 1.22.0-rc1 ends at 299 + + // v298 -> v299 + NewMigration("Add index for release sha1", v1_23.AddIndexForReleaseSha1), } // GetCurrentDBVersion returns the current db version diff --git a/models/migrations/v1_23/v300.go b/models/migrations/v1_23/v300.go new file mode 100644 index 0000000000000..65cffedbd99be --- /dev/null +++ b/models/migrations/v1_23/v300.go @@ -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)) +} diff --git a/models/repo/release.go b/models/repo/release.go index f9c82ea69f81e..e708ad215a742 100644 --- a/models/repo/release.go +++ b/models/repo/release.go @@ -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"` From 14475e9892563cedf8e8cae34d4407b058685715 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 27 May 2024 23:22:11 +0800 Subject: [PATCH 4/8] Follow delvh's suggestion --- templates/repo/commits_list.tmpl | 2 +- templates/repo/tag/name.tmpl | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 templates/repo/tag/name.tmpl diff --git a/templates/repo/commits_list.tmpl b/templates/repo/commits_list.tmpl index 4f562b5da0761..3e4dc919d711c 100644 --- a/templates/repo/commits_list.tmpl +++ b/templates/repo/commits_list.tmpl @@ -73,7 +73,7 @@
{{RenderCommitBody $.Context .Message ($.Repository.ComposeMetas ctx)}}
{{end}} {{range (index $.CommitsTagsMap .ID.String)}} - {{.TagName}} + {{- template "repo/tag/name" dict "RepoFullName" $.Repository.FullName "TagName" .TagName "IsRelease" (not .IsTag) -}} {{end}} {{if .Committer}} diff --git a/templates/repo/tag/name.tmpl b/templates/repo/tag/name.tmpl new file mode 100644 index 0000000000000..07abee13e3c99 --- /dev/null +++ b/templates/repo/tag/name.tmpl @@ -0,0 +1 @@ +{{.TagName}} From aebfe5c604d147be02cac680e174981fb32d7794 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 28 May 2024 10:57:01 +0800 Subject: [PATCH 5/8] Use shared tag name template --- templates/repo/commits_list.tmpl | 2 +- templates/repo/graph/commits.tmpl | 4 +--- templates/repo/tag/name.tmpl | 4 +++- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/templates/repo/commits_list.tmpl b/templates/repo/commits_list.tmpl index 3e4dc919d711c..d8eb0a6e9ac78 100644 --- a/templates/repo/commits_list.tmpl +++ b/templates/repo/commits_list.tmpl @@ -73,7 +73,7 @@
{{RenderCommitBody $.Context .Message ($.Repository.ComposeMetas ctx)}}
{{end}} {{range (index $.CommitsTagsMap .ID.String)}} - {{- template "repo/tag/name" dict "RepoFullName" $.Repository.FullName "TagName" .TagName "IsRelease" (not .IsTag) -}} + {{- template "repo/tag/name" dict "RepoLink" $.Repository.Link "TagName" .TagName "IsRelease" (not .IsTag) -}} {{end}} {{if .Committer}} diff --git a/templates/repo/graph/commits.tmpl b/templates/repo/graph/commits.tmpl index a90c5309b09bb..beadfe831a839 100644 --- a/templates/repo/graph/commits.tmpl +++ b/templates/repo/graph/commits.tmpl @@ -42,9 +42,7 @@ {{end}} {{else if eq $refGroup "tags"}} - - {{svg "octicon-tag"}} {{.ShortName}} - + {{- template "repo/tag/name" dict "RepoLink" $.Repository.Link "TagName" .ShortName -}} {{else if eq $refGroup "remotes"}} {{svg "octicon-cross-reference"}} {{.ShortName}} diff --git a/templates/repo/tag/name.tmpl b/templates/repo/tag/name.tmpl index 07abee13e3c99..c3042014d3f4f 100644 --- a/templates/repo/tag/name.tmpl +++ b/templates/repo/tag/name.tmpl @@ -1 +1,3 @@ -{{.TagName}} + +{{svg "octicon-tag"}} {{.TagName}} + From e4146f96116bd7d48860c424c45ba648f1fd05f9 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 18 Aug 2024 21:20:51 -0700 Subject: [PATCH 6/8] Revert unnecessary change --- models/migrations/migrations.go | 1 - 1 file changed, 1 deletion(-) diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index ed8596b2bf693..13551423ce470 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -588,7 +588,6 @@ var migrations = []Migration{ NewMigration("Drop wrongly created table o_auth2_application", v1_22.DropWronglyCreatedTable), // Gitea 1.22.0-rc1 ends at 299 - // Gitea 1.22.0 ends at 299 // v299 -> v300 NewMigration("Add content version to issue and comment table", v1_23.AddContentVersionToIssueAndComment), From fac2eb8038284da4ac5aa8cc34f2841ff2aa781c Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 18 Aug 2024 23:03:54 -0700 Subject: [PATCH 7/8] Some improvements --- routers/web/repo/commit.go | 7 ++++--- templates/repo/commits_list.tmpl | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index d1c9dc4a20f68..28d113dfbc27d 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -89,10 +89,11 @@ func Commits(ctx *context.Context) { } commitsTagsMap, err := repo_model.FindTagsByCommitIDs(ctx, ctx.Repo.Repository.ID, commitIDs...) if err != nil { - ctx.ServerError("CommitsByRange", err) - return + log.Error("FindTagsByCommitIDs: %v", err) + ctx.Flash.Error("Load tags failed because of internal error") + } else { + ctx.Data["CommitsTagsMap"] = commitsTagsMap } - ctx.Data["CommitsTagsMap"] = commitsTagsMap ctx.Data["Username"] = ctx.Repo.Owner.Name ctx.Data["Reponame"] = ctx.Repo.Repository.Name ctx.Data["CommitCount"] = commitsCount diff --git a/templates/repo/commits_list.tmpl b/templates/repo/commits_list.tmpl index d8eb0a6e9ac78..917a445fde917 100644 --- a/templates/repo/commits_list.tmpl +++ b/templates/repo/commits_list.tmpl @@ -72,8 +72,10 @@ {{if IsMultilineCommitMessage .Message}}
{{RenderCommitBody $.Context .Message ($.Repository.ComposeMetas ctx)}}
{{end}} - {{range (index $.CommitsTagsMap .ID.String)}} - {{- template "repo/tag/name" dict "RepoLink" $.Repository.Link "TagName" .TagName "IsRelease" (not .IsTag) -}} + {{if $.CommitsTagsMap}} + {{range (index $.CommitsTagsMap .ID.String)}} + {{- template "repo/tag/name" dict "RepoLink" $.Repository.Link "TagName" .TagName "IsRelease" (not .IsTag) -}} + {{end}} {{end}} {{if .Committer}} From 5d81b683f55343f23214efc4b7653ff714ef0d31 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 19 Aug 2024 09:00:55 -0700 Subject: [PATCH 8/8] Use locale for tags load failure --- options/locale/locale_en-US.ini | 1 + routers/web/repo/commit.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index ef7628967c56f..042fd549a0b30 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -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 diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index 28d113dfbc27d..c91d5fad0565e 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -90,7 +90,7 @@ func Commits(ctx *context.Context) { commitsTagsMap, err := repo_model.FindTagsByCommitIDs(ctx, ctx.Repo.Repository.ID, commitIDs...) if err != nil { log.Error("FindTagsByCommitIDs: %v", err) - ctx.Flash.Error("Load tags failed because of internal error") + ctx.Flash.Error(ctx.Tr("repo.commit.load_tags_failed")) } else { ctx.Data["CommitsTagsMap"] = commitsTagsMap }