1
0

tag.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package gitutil
  2. import (
  3. "github.com/cockroachdb/errors"
  4. )
  5. // TagsPage contains a list of tags and pagination information.
  6. type TagsPage struct {
  7. // List of tags in the current page.
  8. Tags []string
  9. // Whether the results include the latest tag.
  10. HasLatest bool
  11. // When results do not include the latest tag, an indicator of 'after' to go back.
  12. PreviousAfter string
  13. // Whether there are more tags in the next page.
  14. HasNext bool
  15. }
  16. func (module) ListTagsAfter(repoPath, after string, limit int) (*TagsPage, error) {
  17. all, err := Module.RepoTags(repoPath)
  18. if err != nil {
  19. return nil, errors.Wrap(err, "get tags")
  20. }
  21. total := len(all)
  22. if limit < 0 {
  23. limit = 0
  24. }
  25. // Returns everything when no filter and no limit
  26. if after == "" && limit == 0 {
  27. return &TagsPage{
  28. Tags: all,
  29. HasLatest: true,
  30. }, nil
  31. }
  32. // No filter but has a limit, returns first X tags
  33. if after == "" && limit > 0 {
  34. endIdx := min(limit, total)
  35. return &TagsPage{
  36. Tags: all[:endIdx],
  37. HasLatest: true,
  38. HasNext: limit < total,
  39. }, nil
  40. }
  41. // Loop over all tags see if we can find the filter
  42. previousAfter := ""
  43. found := false
  44. tags := make([]string, 0, len(all))
  45. for i := range all {
  46. if all[i] != after {
  47. continue
  48. }
  49. found = true
  50. if limit > 0 && i-limit >= 0 {
  51. previousAfter = all[i-limit]
  52. }
  53. // In case filter is the oldest one
  54. if i+1 < total {
  55. tags = all[i+1:]
  56. }
  57. break
  58. }
  59. if !found {
  60. tags = all
  61. }
  62. // If all tags after match is equal to the limit, it reaches the oldest tag as well.
  63. if limit == 0 || len(tags) <= limit {
  64. return &TagsPage{
  65. Tags: tags,
  66. HasLatest: !found,
  67. PreviousAfter: previousAfter,
  68. }, nil
  69. }
  70. return &TagsPage{
  71. Tags: tags[:limit],
  72. HasLatest: !found,
  73. PreviousAfter: previousAfter,
  74. HasNext: true,
  75. }, nil
  76. }