Skip to content

Commit 77f604a

Browse files
authored
Add skip and limit to git.GetTags (#16897)
* Make GetTags() api similar to GetBranches() * Use it for Tag/Release page
1 parent 9ca0e79 commit 77f604a

File tree

6 files changed

+33
-20
lines changed

6 files changed

+33
-20
lines changed

modules/context/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) {
547547
return
548548
}
549549

550-
tags, err := ctx.Repo.GitRepo.GetTags()
550+
tags, err := ctx.Repo.GitRepo.GetTags(0, 0)
551551
if err != nil {
552552
ctx.ServerError("GetTags", err)
553553
return

modules/git/repo_tag_gogit.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ func (repo *Repository) IsTagExist(name string) bool {
2121
}
2222

2323
// GetTags returns all tags of the repository.
24-
func (repo *Repository) GetTags() ([]string, error) {
24+
// returning at most limit tags, or all if limit is 0.
25+
func (repo *Repository) GetTags(skip, limit int) ([]string, error) {
2526
var tagNames []string
2627

2728
tags, err := repo.gogitRepo.Tags()
@@ -40,5 +41,15 @@ func (repo *Repository) GetTags() ([]string, error) {
4041
tagNames[i], tagNames[j] = tagNames[j], tagNames[i]
4142
}
4243

44+
// since we have to reverse order we can paginate only afterwards
45+
if len(tagNames) < skip {
46+
tagNames = []string{}
47+
} else {
48+
tagNames = tagNames[skip:]
49+
}
50+
if limit != 0 && len(tagNames) > limit {
51+
tagNames = tagNames[:limit]
52+
}
53+
4354
return tagNames, nil
4455
}

modules/git/repo_tag_nogogit.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ func (repo *Repository) IsTagExist(name string) bool {
1818
}
1919

2020
// GetTags returns all tags of the repository.
21-
func (repo *Repository) GetTags() (tags []string, err error) {
22-
tags, _, err = callShowRef(repo.Path, TagPrefix, "--tags", 0, 0)
21+
// returning at most limit tags, or all if limit is 0.
22+
func (repo *Repository) GetTags(skip, limit int) (tags []string, err error) {
23+
tags, _, err = callShowRef(repo.Path, TagPrefix, "--tags", skip, limit)
2324
return
2425
}

modules/repository/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func SyncReleasesWithTags(repo *models.Repository, gitRepo *git.Repository) erro
250250
}
251251
}
252252
}
253-
tags, err := gitRepo.GetTags()
253+
tags, err := gitRepo.GetTags(0, 0)
254254
if err != nil {
255255
return fmt.Errorf("GetTags: %v", err)
256256
}

routers/web/repo/compare.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ func getBranchesAndTagsForRepo(user *models.User, repo *models.Repository) (bool
606606
if err != nil {
607607
return false, nil, nil, err
608608
}
609-
tags, err := gitRepo.GetTags()
609+
tags, err := gitRepo.GetTags(0, 0)
610610
if err != nil {
611611
return false, nil, nil, err
612612
}
@@ -632,7 +632,7 @@ func CompareDiff(ctx *context.Context) {
632632
}
633633

634634
baseGitRepo := ctx.Repo.GitRepo
635-
baseTags, err := baseGitRepo.GetTags()
635+
baseTags, err := baseGitRepo.GetTags(0, 0)
636636
if err != nil {
637637
ctx.ServerError("GetTags", err)
638638
return
@@ -646,7 +646,7 @@ func CompareDiff(ctx *context.Context) {
646646
}
647647
ctx.Data["HeadBranches"] = headBranches
648648

649-
headTags, err := headGitRepo.GetTags()
649+
headTags, err := headGitRepo.GetTags(0, 0)
650650
if err != nil {
651651
ctx.ServerError("GetTags", err)
652652
return

routers/web/repo/release.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,18 @@ func releasesOrTags(ctx *context.Context, isTagList bool) {
8383
ctx.Data["PageIsTagList"] = false
8484
}
8585

86-
tags, err := ctx.Repo.GitRepo.GetTags()
86+
listOptions := models.ListOptions{
87+
Page: ctx.FormInt("page"),
88+
PageSize: ctx.FormInt("limit"),
89+
}
90+
if listOptions.PageSize == 0 {
91+
listOptions.PageSize = setting.Repository.Release.DefaultPagingNum
92+
}
93+
if listOptions.PageSize > setting.API.MaxResponseItems {
94+
listOptions.PageSize = setting.API.MaxResponseItems
95+
}
96+
97+
tags, err := ctx.Repo.GitRepo.GetTags(listOptions.GetStartEnd())
8798
if err != nil {
8899
ctx.ServerError("GetTags", err)
89100
return
@@ -92,19 +103,9 @@ func releasesOrTags(ctx *context.Context, isTagList bool) {
92103

93104
writeAccess := ctx.Repo.CanWrite(models.UnitTypeReleases)
94105
ctx.Data["CanCreateRelease"] = writeAccess && !ctx.Repo.Repository.IsArchived
95-
limit := ctx.FormInt("limit")
96-
if limit == 0 {
97-
limit = setting.Repository.Release.DefaultPagingNum
98-
}
99-
if limit > setting.API.MaxResponseItems {
100-
limit = setting.API.MaxResponseItems
101-
}
102106

103107
opts := models.FindReleasesOptions{
104-
ListOptions: models.ListOptions{
105-
Page: ctx.FormInt("page"),
106-
PageSize: limit,
107-
},
108+
ListOptions: listOptions,
108109
IncludeDrafts: writeAccess && !isTagList,
109110
IncludeTags: isTagList,
110111
}

0 commit comments

Comments
 (0)