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_nogogit.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. "errors"
  9. "io"
  10. "code.gitea.io/gitea/modules/log"
  11. )
  12. // IsTagExist returns true if given tag exists in the repository.
  13. func (repo *Repository) IsTagExist(name string) bool {
  14. if name == "" {
  15. return false
  16. }
  17. return repo.IsReferenceExist(TagPrefix + name)
  18. }
  19. // GetTags returns all tags of the repository.
  20. // returning at most limit tags, or all if limit is 0.
  21. func (repo *Repository) GetTags(skip, limit int) (tags []string, err error) {
  22. tags, _, err = callShowRef(repo.Ctx, repo.Path, TagPrefix, "--tags", skip, limit)
  23. return
  24. }
  25. // GetTagType gets the type of the tag, either commit (simple) or tag (annotated)
  26. func (repo *Repository) GetTagType(id SHA1) (string, error) {
  27. wr, rd, cancel := repo.CatFileBatchCheck(repo.Ctx)
  28. defer cancel()
  29. _, err := wr.Write([]byte(id.String() + "\n"))
  30. if err != nil {
  31. return "", err
  32. }
  33. _, typ, _, err := ReadBatchLine(rd)
  34. if IsErrNotExist(err) {
  35. return "", ErrNotExist{ID: id.String()}
  36. }
  37. return typ, nil
  38. }
  39. func (repo *Repository) getTag(tagID SHA1, name string) (*Tag, error) {
  40. t, ok := repo.tagCache.Get(tagID.String())
  41. if ok {
  42. log.Debug("Hit cache: %s", tagID)
  43. tagClone := *t.(*Tag)
  44. tagClone.Name = name // This is necessary because lightweight tags may have same id
  45. return &tagClone, nil
  46. }
  47. tp, err := repo.GetTagType(tagID)
  48. if err != nil {
  49. return nil, err
  50. }
  51. // Get the commit ID and tag ID (may be different for annotated tag) for the returned tag object
  52. commitIDStr, err := repo.GetTagCommitID(name)
  53. if err != nil {
  54. // every tag should have a commit ID so return all errors
  55. return nil, err
  56. }
  57. commitID, err := NewIDFromString(commitIDStr)
  58. if err != nil {
  59. return nil, err
  60. }
  61. // If type is "commit, the tag is a lightweight tag
  62. if ObjectType(tp) == ObjectCommit {
  63. commit, err := repo.GetCommit(commitIDStr)
  64. if err != nil {
  65. return nil, err
  66. }
  67. tag := &Tag{
  68. Name: name,
  69. ID: tagID,
  70. Object: commitID,
  71. Type: tp,
  72. Tagger: commit.Committer,
  73. Message: commit.Message(),
  74. }
  75. repo.tagCache.Set(tagID.String(), tag)
  76. return tag, nil
  77. }
  78. // The tag is an annotated tag with a message.
  79. wr, rd, cancel := repo.CatFileBatch(repo.Ctx)
  80. defer cancel()
  81. if _, err := wr.Write([]byte(tagID.String() + "\n")); err != nil {
  82. return nil, err
  83. }
  84. _, typ, size, err := ReadBatchLine(rd)
  85. if err != nil {
  86. if errors.Is(err, io.EOF) || IsErrNotExist(err) {
  87. return nil, ErrNotExist{ID: tagID.String()}
  88. }
  89. return nil, err
  90. }
  91. if typ != "tag" {
  92. return nil, ErrNotExist{ID: tagID.String()}
  93. }
  94. // then we need to parse the tag
  95. // and load the commit
  96. data, err := io.ReadAll(io.LimitReader(rd, size))
  97. if err != nil {
  98. return nil, err
  99. }
  100. _, err = rd.Discard(1)
  101. if err != nil {
  102. return nil, err
  103. }
  104. tag, err := parseTagData(data)
  105. if err != nil {
  106. return nil, err
  107. }
  108. tag.Name = name
  109. tag.ID = tagID
  110. tag.Type = tp
  111. repo.tagCache.Set(tagID.String(), tag)
  112. return tag, nil
  113. }