Skip to content

Commit 8d9f8e1

Browse files
authored
Fix tags sort by creation time (descending) on branch/tag dropdowns (#23491)
This PR fixes the tags sort issue mentioned in #23432 The tags on dropdown shoud be sorted in descending order of time but are not. Because when getting tags, it execeutes `git tag sort --sort=-taggerdate`. Git supports two types of tags: lightweight and annotated, and `git tag sort --sort=-taggerdate` dosen't work with lightweight tags, which will not give correct result. This PR add `GetTagNamesByRepoID ` to get tags from the database so the tags are sorted. Also adapt this change to the droplist when comparing branches. Dropdown places: <img width="369" alt="截屏2023-03-15 14 25 39" src="https://user-images.githubusercontent.com/17645053/225224506-65a72e50-4c11-41d7-8187-a7e9c7dab2cb.png"> <img width="675" alt="截屏2023-03-15 14 25 27" src="https://user-images.githubusercontent.com/17645053/225224526-65ce8008-340c-43f6-aa65-b6bd9e1a1bf1.png">
1 parent 661e78b commit 8d9f8e1

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

models/repo/release.go

+22
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,28 @@ func GetReleasesByRepoID(ctx context.Context, repoID int64, opts FindReleasesOpt
253253
return rels, sess.Find(&rels)
254254
}
255255

256+
// GetTagNamesByRepoID returns a list of release tag names of repository.
257+
func GetTagNamesByRepoID(ctx context.Context, repoID int64) ([]string, error) {
258+
listOptions := db.ListOptions{
259+
ListAll: true,
260+
}
261+
opts := FindReleasesOptions{
262+
ListOptions: listOptions,
263+
IncludeDrafts: true,
264+
IncludeTags: true,
265+
HasSha1: util.OptionalBoolTrue,
266+
}
267+
268+
tags := make([]string, 0)
269+
sess := db.GetEngine(ctx).
270+
Table("release").
271+
Desc("created_unix", "id").
272+
Where(opts.toConds(repoID)).
273+
Cols("tag_name")
274+
275+
return tags, sess.Find(&tags)
276+
}
277+
256278
// CountReleasesByRepoID returns a number of releases matching FindReleaseOptions and RepoID.
257279
func CountReleasesByRepoID(repoID int64, opts FindReleasesOptions) (int64, error) {
258280
return db.GetEngine(db.DefaultContext).Where(opts.toConds(repoID)).Count(new(Release))

modules/context/repo.go

+2-13
Original file line numberDiff line numberDiff line change
@@ -660,20 +660,9 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) {
660660
return
661661
}
662662

663-
tags, err := ctx.Repo.GitRepo.GetTags(0, 0)
663+
tags, err := repo_model.GetTagNamesByRepoID(ctx, ctx.Repo.Repository.ID)
664664
if err != nil {
665-
if strings.Contains(err.Error(), "fatal: not a git repository ") {
666-
log.Error("Repository %-v has a broken repository on the file system: %s Error: %v", ctx.Repo.Repository, ctx.Repo.Repository.RepoPath(), err)
667-
ctx.Repo.Repository.Status = repo_model.RepositoryBroken
668-
ctx.Repo.Repository.IsEmpty = true
669-
ctx.Data["BranchName"] = ctx.Repo.Repository.DefaultBranch
670-
// Only allow access to base of repo or settings
671-
if !isHomeOrSettings {
672-
ctx.Redirect(ctx.Repo.RepoLink)
673-
}
674-
return
675-
}
676-
ctx.ServerError("GetTags", err)
665+
ctx.ServerError("GetTagNamesByRepoID", err)
677666
return
678667
}
679668
ctx.Data["Tags"] = tags

routers/web/repo/compare.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -717,10 +717,9 @@ func CompareDiff(ctx *context.Context) {
717717
return
718718
}
719719

720-
baseGitRepo := ctx.Repo.GitRepo
721-
baseTags, err := baseGitRepo.GetTags(0, 0)
720+
baseTags, err := repo_model.GetTagNamesByRepoID(ctx, ctx.Repo.Repository.ID)
722721
if err != nil {
723-
ctx.ServerError("GetTags", err)
722+
ctx.ServerError("GetTagNamesByRepoID", err)
724723
return
725724
}
726725
ctx.Data["Tags"] = baseTags
@@ -738,9 +737,9 @@ func CompareDiff(ctx *context.Context) {
738737
}
739738
ctx.Data["HeadBranches"] = headBranches
740739

741-
headTags, err := ci.HeadGitRepo.GetTags(0, 0)
740+
headTags, err := repo_model.GetTagNamesByRepoID(ctx, ci.HeadRepo.ID)
742741
if err != nil {
743-
ctx.ServerError("GetTags", err)
742+
ctx.ServerError("GetTagNamesByRepoID", err)
744743
return
745744
}
746745
ctx.Data["HeadTags"] = headTags

0 commit comments

Comments
 (0)