repo_tag.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import (
  6. "fmt"
  7. "strings"
  8. "github.com/mcuadros/go-version"
  9. )
  10. const TAG_PREFIX = "refs/tags/"
  11. // IsTagExist returns true if given tag exists in the repository.
  12. func IsTagExist(repoPath, name string) bool {
  13. return IsReferenceExist(repoPath, TAG_PREFIX+name)
  14. }
  15. func (repo *Repository) IsTagExist(name string) bool {
  16. return IsTagExist(repo.Path, name)
  17. }
  18. func (repo *Repository) CreateTag(name, revision string) error {
  19. _, err := NewCommand("tag", name, revision).RunInDir(repo.Path)
  20. return err
  21. }
  22. func (repo *Repository) getTag(id sha1) (*Tag, error) {
  23. t, ok := repo.tagCache.Get(id.String())
  24. if ok {
  25. log("Hit cache: %s", id)
  26. return t.(*Tag), nil
  27. }
  28. // Get tag type
  29. tp, err := NewCommand("cat-file", "-t", id.String()).RunInDir(repo.Path)
  30. if err != nil {
  31. return nil, err
  32. }
  33. tp = strings.TrimSpace(tp)
  34. // Tag is a commit.
  35. if ObjectType(tp) == OBJECT_COMMIT {
  36. tag := &Tag{
  37. ID: id,
  38. Object: id,
  39. Type: string(OBJECT_COMMIT),
  40. repo: repo,
  41. }
  42. repo.tagCache.Set(id.String(), tag)
  43. return tag, nil
  44. }
  45. // Tag with message.
  46. data, err := NewCommand("cat-file", "-p", id.String()).RunInDirBytes(repo.Path)
  47. if err != nil {
  48. return nil, err
  49. }
  50. tag, err := parseTagData(data)
  51. if err != nil {
  52. return nil, err
  53. }
  54. tag.ID = id
  55. tag.repo = repo
  56. repo.tagCache.Set(id.String(), tag)
  57. return tag, nil
  58. }
  59. // GetTag returns a Git tag by given name.
  60. func (repo *Repository) GetTag(name string) (*Tag, error) {
  61. stdout, err := NewCommand("show-ref", "--tags", name).RunInDir(repo.Path)
  62. if err != nil {
  63. return nil, err
  64. }
  65. id, err := NewIDFromString(strings.Split(stdout, " ")[0])
  66. if err != nil {
  67. return nil, err
  68. }
  69. tag, err := repo.getTag(id)
  70. if err != nil {
  71. return nil, err
  72. }
  73. tag.Name = name
  74. return tag, nil
  75. }
  76. // GetTags returns all tags of the repository.
  77. func (repo *Repository) GetTags() ([]string, error) {
  78. cmd := NewCommand("tag", "-l")
  79. if version.Compare(gitVersion, "2.0.0", ">=") {
  80. cmd.AddArguments("--sort=-v:refname")
  81. }
  82. stdout, err := cmd.RunInDir(repo.Path)
  83. if err != nil {
  84. return nil, err
  85. }
  86. tags := strings.Split(stdout, "\n")
  87. tags = tags[:len(tags)-1]
  88. if version.Compare(gitVersion, "2.0.0", "<") {
  89. version.Sort(tags)
  90. // Reverse order
  91. for i := 0; i < len(tags)/2; i++ {
  92. j := len(tags) - i - 1
  93. tags[i], tags[j] = tags[j], tags[i]
  94. }
  95. }
  96. return tags, nil
  97. }
  98. type TagsResult struct {
  99. // Indicates whether results include the latest tag.
  100. HasLatest bool
  101. // If results do not include the latest tag, a indicator 'after' to go back.
  102. PreviousAfter string
  103. // Indicates whether results include the oldest tag.
  104. ReachEnd bool
  105. // List of returned tags.
  106. Tags []string
  107. }
  108. // GetTagsAfter returns list of tags 'after' (exlusive) given tag.
  109. func (repo *Repository) GetTagsAfter(after string, limit int) (*TagsResult, error) {
  110. allTags, err := repo.GetTags()
  111. if err != nil {
  112. return nil, fmt.Errorf("GetTags: %v", err)
  113. }
  114. if limit < 0 {
  115. limit = 0
  116. }
  117. numAllTags := len(allTags)
  118. if len(after) == 0 && limit == 0 {
  119. return &TagsResult{
  120. HasLatest: true,
  121. ReachEnd: true,
  122. Tags: allTags,
  123. }, nil
  124. } else if len(after) == 0 && limit > 0 {
  125. endIdx := limit
  126. if limit >= numAllTags {
  127. endIdx = numAllTags
  128. }
  129. return &TagsResult{
  130. HasLatest: true,
  131. ReachEnd: limit >= numAllTags,
  132. Tags: allTags[:endIdx],
  133. }, nil
  134. }
  135. previousAfter := ""
  136. hasMatch := false
  137. tags := make([]string, 0, len(allTags))
  138. for i := range allTags {
  139. if hasMatch {
  140. tags = allTags[i:]
  141. break
  142. }
  143. if allTags[i] == after {
  144. hasMatch = true
  145. if limit > 0 && i-limit >= 0 {
  146. previousAfter = allTags[i-limit]
  147. }
  148. continue
  149. }
  150. }
  151. if !hasMatch {
  152. tags = allTags
  153. }
  154. // If all tags after match is equal to the limit, it reaches the oldest tag as well.
  155. if limit == 0 || len(tags) <= limit {
  156. return &TagsResult{
  157. HasLatest: !hasMatch,
  158. PreviousAfter: previousAfter,
  159. ReachEnd: true,
  160. Tags: tags,
  161. }, nil
  162. }
  163. return &TagsResult{
  164. HasLatest: !hasMatch,
  165. PreviousAfter: previousAfter,
  166. Tags: tags[:limit],
  167. }, nil
  168. }
  169. // DeleteTag deletes a tag from the repository
  170. func (repo *Repository) DeleteTag(name string) error {
  171. cmd := NewCommand("tag", "-d")
  172. cmd.AddArguments(name)
  173. _, err := cmd.RunInDir(repo.Path)
  174. return err
  175. }