1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package gitutil
- import (
- "github.com/pkg/errors"
- )
- type TagsPage struct {
-
- Tags []string
-
- HasLatest bool
-
- PreviousAfter string
-
- HasNext bool
- }
- func (module) ListTagsAfter(repoPath, after string, limit int) (*TagsPage, error) {
- all, err := Module.RepoTags(repoPath)
- if err != nil {
- return nil, errors.Wrap(err, "get tags")
- }
- total := len(all)
- if limit < 0 {
- limit = 0
- }
-
- if after == "" && limit == 0 {
- return &TagsPage{
- Tags: all,
- HasLatest: true,
- }, nil
- }
-
- if after == "" && limit > 0 {
- endIdx := limit
- if limit > total {
- endIdx = total
- }
- return &TagsPage{
- Tags: all[:endIdx],
- HasLatest: true,
- HasNext: limit < total,
- }, nil
- }
-
- previousAfter := ""
- found := false
- tags := make([]string, 0, len(all))
- for i := range all {
- if all[i] != after {
- continue
- }
- found = true
- if limit > 0 && i-limit >= 0 {
- previousAfter = all[i-limit]
- }
-
- if i+1 < total {
- tags = all[i+1:]
- }
- break
- }
- if !found {
- tags = all
- }
-
- if limit == 0 || len(tags) <= limit {
- return &TagsPage{
- Tags: tags,
- HasLatest: !found,
- PreviousAfter: previousAfter,
- }, nil
- }
- return &TagsPage{
- Tags: tags[:limit],
- HasLatest: !found,
- PreviousAfter: previousAfter,
- HasNext: true,
- }, nil
- }
|