You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo_tag.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. "strings"
  7. "github.com/mcuadros/go-version"
  8. )
  9. // TagPrefix tags prefix path on the repository
  10. const TagPrefix = "refs/tags/"
  11. // IsTagExist returns true if given tag exists in the repository.
  12. func IsTagExist(repoPath, name string) bool {
  13. return IsReferenceExist(repoPath, TagPrefix+name)
  14. }
  15. // IsTagExist returns true if given tag exists in the repository.
  16. func (repo *Repository) IsTagExist(name string) bool {
  17. return IsTagExist(repo.Path, name)
  18. }
  19. // CreateTag create one tag in the repository
  20. func (repo *Repository) CreateTag(name, revision string) error {
  21. _, err := NewCommand("tag", name, revision).RunInDir(repo.Path)
  22. return err
  23. }
  24. func (repo *Repository) getTag(id SHA1) (*Tag, error) {
  25. t, ok := repo.tagCache.Get(id.String())
  26. if ok {
  27. log("Hit cache: %s", id)
  28. return t.(*Tag), nil
  29. }
  30. // Get tag type
  31. tp, err := NewCommand("cat-file", "-t", id.String()).RunInDir(repo.Path)
  32. if err != nil {
  33. return nil, err
  34. }
  35. tp = strings.TrimSpace(tp)
  36. // Tag is a commit.
  37. if ObjectType(tp) == ObjectCommit {
  38. tag := &Tag{
  39. ID: id,
  40. Object: id,
  41. Type: string(ObjectCommit),
  42. repo: repo,
  43. }
  44. repo.tagCache.Set(id.String(), tag)
  45. return tag, nil
  46. }
  47. // Tag with message.
  48. data, err := NewCommand("cat-file", "-p", id.String()).RunInDirBytes(repo.Path)
  49. if err != nil {
  50. return nil, err
  51. }
  52. tag, err := parseTagData(data)
  53. if err != nil {
  54. return nil, err
  55. }
  56. tag.ID = id
  57. tag.repo = repo
  58. repo.tagCache.Set(id.String(), tag)
  59. return tag, nil
  60. }
  61. // GetTag returns a Git tag by given name.
  62. func (repo *Repository) GetTag(name string) (*Tag, error) {
  63. stdout, err := NewCommand("show-ref", "--tags", name).RunInDir(repo.Path)
  64. if err != nil {
  65. return nil, err
  66. }
  67. id, err := NewIDFromString(strings.Split(stdout, " ")[0])
  68. if err != nil {
  69. return nil, err
  70. }
  71. tag, err := repo.getTag(id)
  72. if err != nil {
  73. return nil, err
  74. }
  75. tag.Name = name
  76. return tag, nil
  77. }
  78. // GetTagInfos returns all tag infos of the repository.
  79. func (repo *Repository) GetTagInfos() ([]*Tag, error) {
  80. // TODO this a slow implementation, makes one git command per tag
  81. stdout, err := NewCommand("tag").RunInDir(repo.Path)
  82. if err != nil {
  83. return nil, err
  84. }
  85. tagNames := strings.Split(stdout, "\n")
  86. var tags = make([]*Tag, 0, len(tagNames))
  87. for _, tagName := range tagNames {
  88. tagName = strings.TrimSpace(tagName)
  89. if len(tagName) == 0 {
  90. continue
  91. }
  92. tag, err := repo.GetTag(tagName)
  93. if err != nil {
  94. return nil, err
  95. }
  96. tags = append(tags, tag)
  97. }
  98. sortTagsByTime(tags)
  99. return tags, nil
  100. }
  101. // GetTags returns all tags of the repository.
  102. func (repo *Repository) GetTags() ([]string, error) {
  103. cmd := NewCommand("tag", "-l")
  104. if version.Compare(gitVersion, "2.0.0", ">=") {
  105. cmd.AddArguments("--sort=-v:refname")
  106. }
  107. stdout, err := cmd.RunInDir(repo.Path)
  108. if err != nil {
  109. return nil, err
  110. }
  111. tags := strings.Split(stdout, "\n")
  112. tags = tags[:len(tags)-1]
  113. if version.Compare(gitVersion, "2.0.0", "<") {
  114. version.Sort(tags)
  115. // Reverse order
  116. for i := 0; i < len(tags)/2; i++ {
  117. j := len(tags) - i - 1
  118. tags[i], tags[j] = tags[j], tags[i]
  119. }
  120. }
  121. return tags, nil
  122. }