From ee1d85657dbfb4cabbaf47ba94d57bd7864bf5a4 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 31 Aug 2021 04:20:19 +0200 Subject: [PATCH] Add skip and limit to git.GetTags --- modules/context/repo.go | 2 +- modules/git/repo_tag_gogit.go | 13 ++++++++++++- modules/git/repo_tag_nogogit.go | 5 +++-- modules/repository/repo.go | 2 +- routers/web/repo/compare.go | 6 +++--- routers/web/repo/release.go | 2 +- 6 files changed, 21 insertions(+), 9 deletions(-) diff --git a/modules/context/repo.go b/modules/context/repo.go index df71638350254..89ff890cc37ee 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -547,7 +547,7 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) { return } - tags, err := ctx.Repo.GitRepo.GetTags() + tags, err := ctx.Repo.GitRepo.GetTags(0, 0) if err != nil { ctx.ServerError("GetTags", err) return diff --git a/modules/git/repo_tag_gogit.go b/modules/git/repo_tag_gogit.go index 3022fe96f7772..ff8a6d53eee6e 100644 --- a/modules/git/repo_tag_gogit.go +++ b/modules/git/repo_tag_gogit.go @@ -21,7 +21,8 @@ func (repo *Repository) IsTagExist(name string) bool { } // GetTags returns all tags of the repository. -func (repo *Repository) GetTags() ([]string, error) { +// returning at most limit tags, or all if limit is 0. +func (repo *Repository) GetTags(skip, limit int) ([]string, error) { var tagNames []string tags, err := repo.gogitRepo.Tags() @@ -40,5 +41,15 @@ func (repo *Repository) GetTags() ([]string, error) { tagNames[i], tagNames[j] = tagNames[j], tagNames[i] } + // since we have to reverse order we can paginate only afterwards + if len(tagNames) < skip { + tagNames = []string{} + } else { + tagNames = tagNames[skip:] + } + if limit != 0 && len(tagNames) > limit { + tagNames = tagNames[:limit] + } + return tagNames, nil } diff --git a/modules/git/repo_tag_nogogit.go b/modules/git/repo_tag_nogogit.go index 0170f0cc76e7d..172b6fd66cbb0 100644 --- a/modules/git/repo_tag_nogogit.go +++ b/modules/git/repo_tag_nogogit.go @@ -18,7 +18,8 @@ func (repo *Repository) IsTagExist(name string) bool { } // GetTags returns all tags of the repository. -func (repo *Repository) GetTags() (tags []string, err error) { - tags, _, err = callShowRef(repo.Path, TagPrefix, "--tags", 0, 0) +// returning at most limit tags, or all if limit is 0. +func (repo *Repository) GetTags(skip, limit int) (tags []string, err error) { + tags, _, err = callShowRef(repo.Path, TagPrefix, "--tags", skip, limit) return } diff --git a/modules/repository/repo.go b/modules/repository/repo.go index 6b870397754ee..00413549ee093 100644 --- a/modules/repository/repo.go +++ b/modules/repository/repo.go @@ -250,7 +250,7 @@ func SyncReleasesWithTags(repo *models.Repository, gitRepo *git.Repository) erro } } } - tags, err := gitRepo.GetTags() + tags, err := gitRepo.GetTags(0, 0) if err != nil { return fmt.Errorf("GetTags: %v", err) } diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index eb6c37a1a6363..0a0b38f078323 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -606,7 +606,7 @@ func getBranchesAndTagsForRepo(user *models.User, repo *models.Repository) (bool if err != nil { return false, nil, nil, err } - tags, err := gitRepo.GetTags() + tags, err := gitRepo.GetTags(0, 0) if err != nil { return false, nil, nil, err } @@ -632,7 +632,7 @@ func CompareDiff(ctx *context.Context) { } baseGitRepo := ctx.Repo.GitRepo - baseTags, err := baseGitRepo.GetTags() + baseTags, err := baseGitRepo.GetTags(0, 0) if err != nil { ctx.ServerError("GetTags", err) return @@ -646,7 +646,7 @@ func CompareDiff(ctx *context.Context) { } ctx.Data["HeadBranches"] = headBranches - headTags, err := headGitRepo.GetTags() + headTags, err := headGitRepo.GetTags(0, 0) if err != nil { ctx.ServerError("GetTags", err) return diff --git a/routers/web/repo/release.go b/routers/web/repo/release.go index ef98790f52d31..f7363886882d2 100644 --- a/routers/web/repo/release.go +++ b/routers/web/repo/release.go @@ -83,7 +83,7 @@ func releasesOrTags(ctx *context.Context, isTagList bool) { ctx.Data["PageIsTagList"] = false } - tags, err := ctx.Repo.GitRepo.GetTags() + tags, err := ctx.Repo.GitRepo.GetTags(0, 0) if err != nil { ctx.ServerError("GetTags", err) return