diff options
author | 6543 <6543@obermui.de> | 2020-02-25 20:07:07 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-25 14:07:07 -0500 |
commit | 4427a936b4c7bd07908ccbe96104928dd29cf59d (patch) | |
tree | d5eb9406613c6262a44b21985cebf31cde2b5e5d /modules | |
parent | b098cc24c5d93ca96ff72faf3093f45c30c974f0 (diff) | |
download | gitea-4427a936b4c7bd07908ccbe96104928dd29cf59d.tar.gz gitea-4427a936b4c7bd07908ccbe96104928dd29cf59d.zip |
[API] enable paggination for ListRepoTags (#10454)
* enable paggination for repoTags
* precalculate first, cut slice second
* Apply suggestions from code review
Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com>
Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com>
Diffstat (limited to 'modules')
-rw-r--r-- | modules/git/repo_tag.go | 14 | ||||
-rw-r--r-- | modules/git/repo_tag_test.go | 2 |
2 files changed, 14 insertions, 2 deletions
diff --git a/modules/git/repo_tag.go b/modules/git/repo_tag.go index 9e08d00f0d..f27c28ad02 100644 --- a/modules/git/repo_tag.go +++ b/modules/git/repo_tag.go @@ -187,7 +187,7 @@ func (repo *Repository) GetTag(name string) (*Tag, error) { } // GetTagInfos returns all tag infos of the repository. -func (repo *Repository) GetTagInfos() ([]*Tag, error) { +func (repo *Repository) GetTagInfos(page, pageSize int) ([]*Tag, error) { // TODO this a slow implementation, makes one git command per tag stdout, err := NewCommand("tag").RunInDir(repo.Path) if err != nil { @@ -195,6 +195,18 @@ func (repo *Repository) GetTagInfos() ([]*Tag, error) { } tagNames := strings.Split(strings.TrimRight(stdout, "\n"), "\n") + + if page != 0 { + skip := (page - 1) * pageSize + if skip >= len(tagNames) { + return nil, nil + } + if (len(tagNames) - skip) < pageSize { + pageSize = len(tagNames) - skip + } + tagNames = tagNames[skip : skip+pageSize] + } + var tags = make([]*Tag, 0, len(tagNames)) for _, tagName := range tagNames { tagName = strings.TrimSpace(tagName) diff --git a/modules/git/repo_tag_test.go b/modules/git/repo_tag_test.go index 90f2b37358..7a644e39db 100644 --- a/modules/git/repo_tag_test.go +++ b/modules/git/repo_tag_test.go @@ -18,7 +18,7 @@ func TestRepository_GetTags(t *testing.T) { assert.NoError(t, err) defer bareRepo1.Close() - tags, err := bareRepo1.GetTagInfos() + tags, err := bareRepo1.GetTagInfos(0, 0) assert.NoError(t, err) assert.Len(t, tags, 1) assert.EqualValues(t, "test", tags[0].Name) |