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_gogit.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. //go:build gogit
  6. package git
  7. import (
  8. "strings"
  9. "code.gitea.io/gitea/modules/log"
  10. "github.com/go-git/go-git/v5/plumbing"
  11. )
  12. // IsTagExist returns true if given tag exists in the repository.
  13. func (repo *Repository) IsTagExist(name string) bool {
  14. _, err := repo.gogitRepo.Reference(plumbing.ReferenceName(TagPrefix+name), true)
  15. return err == nil
  16. }
  17. // GetTags returns all tags of the repository.
  18. // returning at most limit tags, or all if limit is 0.
  19. func (repo *Repository) GetTags(skip, limit int) ([]string, error) {
  20. var tagNames []string
  21. tags, err := repo.gogitRepo.Tags()
  22. if err != nil {
  23. return nil, err
  24. }
  25. _ = tags.ForEach(func(tag *plumbing.Reference) error {
  26. tagNames = append(tagNames, strings.TrimPrefix(tag.Name().String(), TagPrefix))
  27. return nil
  28. })
  29. // Reverse order
  30. for i := 0; i < len(tagNames)/2; i++ {
  31. j := len(tagNames) - i - 1
  32. tagNames[i], tagNames[j] = tagNames[j], tagNames[i]
  33. }
  34. // since we have to reverse order we can paginate only afterwards
  35. if len(tagNames) < skip {
  36. tagNames = []string{}
  37. } else {
  38. tagNames = tagNames[skip:]
  39. }
  40. if limit != 0 && len(tagNames) > limit {
  41. tagNames = tagNames[:limit]
  42. }
  43. return tagNames, nil
  44. }
  45. // GetTagType gets the type of the tag, either commit (simple) or tag (annotated)
  46. func (repo *Repository) GetTagType(id SHA1) (string, error) {
  47. // Get tag type
  48. obj, err := repo.gogitRepo.Object(plumbing.AnyObject, id)
  49. if err != nil {
  50. if err == plumbing.ErrReferenceNotFound {
  51. return "", &ErrNotExist{ID: id.String()}
  52. }
  53. return "", err
  54. }
  55. return obj.Type().String(), nil
  56. }
  57. func (repo *Repository) getTag(tagID SHA1, name string) (*Tag, error) {
  58. t, ok := repo.tagCache.Get(tagID.String())
  59. if ok {
  60. log.Debug("Hit cache: %s", tagID)
  61. tagClone := *t.(*Tag)
  62. tagClone.Name = name // This is necessary because lightweight tags may have same id
  63. return &tagClone, nil
  64. }
  65. tp, err := repo.GetTagType(tagID)
  66. if err != nil {
  67. return nil, err
  68. }
  69. // Get the commit ID and tag ID (may be different for annotated tag) for the returned tag object
  70. commitIDStr, err := repo.GetTagCommitID(name)
  71. if err != nil {
  72. // every tag should have a commit ID so return all errors
  73. return nil, err
  74. }
  75. commitID, err := NewIDFromString(commitIDStr)
  76. if err != nil {
  77. return nil, err
  78. }
  79. // If type is "commit, the tag is a lightweight tag
  80. if ObjectType(tp) == ObjectCommit {
  81. commit, err := repo.GetCommit(commitIDStr)
  82. if err != nil {
  83. return nil, err
  84. }
  85. tag := &Tag{
  86. Name: name,
  87. ID: tagID,
  88. Object: commitID,
  89. Type: tp,
  90. Tagger: commit.Committer,
  91. Message: commit.Message(),
  92. }
  93. repo.tagCache.Set(tagID.String(), tag)
  94. return tag, nil
  95. }
  96. gogitTag, err := repo.gogitRepo.TagObject(tagID)
  97. if err != nil {
  98. if err == plumbing.ErrReferenceNotFound {
  99. return nil, &ErrNotExist{ID: tagID.String()}
  100. }
  101. return nil, err
  102. }
  103. tag := &Tag{
  104. Name: name,
  105. ID: tagID,
  106. Object: gogitTag.Target,
  107. Type: tp,
  108. Tagger: &gogitTag.Tagger,
  109. Message: gogitTag.Message,
  110. }
  111. repo.tagCache.Set(tagID.String(), tag)
  112. return tag, nil
  113. }