repo_tag.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2014 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. "errors"
  7. "strings"
  8. "github.com/Unknwon/com"
  9. )
  10. func IsTagExist(repoPath, tagName string) bool {
  11. _, _, err := com.ExecCmdDir(repoPath, "git", "show-ref", "--verify", "refs/tags/"+tagName)
  12. return err == nil
  13. }
  14. func (repo *Repository) IsTagExist(tagName string) bool {
  15. return IsTagExist(repo.Path, tagName)
  16. }
  17. func (repo *Repository) getTagsReversed() ([]string, error) {
  18. stdout, stderr, err := com.ExecCmdDir(repo.Path, "git", "tag", "-l", "--sort=-v:refname")
  19. if err != nil {
  20. return nil, concatenateError(err, stderr)
  21. }
  22. tags := strings.Split(stdout, "\n")
  23. return tags[:len(tags)-1], nil
  24. }
  25. // GetTags returns all tags of given repository.
  26. func (repo *Repository) GetTags() ([]string, error) {
  27. if gitVer.AtLeast(MustParseVersion("2.0.0")) {
  28. return repo.getTagsReversed()
  29. }
  30. stdout, stderr, err := com.ExecCmdDir(repo.Path, "git", "tag", "-l")
  31. if err != nil {
  32. return nil, concatenateError(err, stderr)
  33. }
  34. tags := strings.Split(stdout, "\n")
  35. return tags[:len(tags)-1], nil
  36. }
  37. func (repo *Repository) CreateTag(tagName, idStr string) error {
  38. _, stderr, err := com.ExecCmdDir(repo.Path, "git", "tag", tagName, idStr)
  39. if err != nil {
  40. return errors.New(stderr)
  41. }
  42. return nil
  43. }
  44. func (repo *Repository) getTag(id sha1) (*Tag, error) {
  45. if repo.tagCache != nil {
  46. if t, ok := repo.tagCache[id]; ok {
  47. return t, nil
  48. }
  49. } else {
  50. repo.tagCache = make(map[sha1]*Tag, 10)
  51. }
  52. // Get tag type.
  53. tp, stderr, err := com.ExecCmdDir(repo.Path, "git", "cat-file", "-t", id.String())
  54. if err != nil {
  55. return nil, errors.New(stderr)
  56. }
  57. tp = strings.TrimSpace(tp)
  58. // Tag is a commit.
  59. if ObjectType(tp) == COMMIT {
  60. tag := &Tag{
  61. Id: id,
  62. Object: id,
  63. Type: string(COMMIT),
  64. repo: repo,
  65. }
  66. repo.tagCache[id] = tag
  67. return tag, nil
  68. }
  69. // Tag with message.
  70. data, bytErr, err := com.ExecCmdDirBytes(repo.Path, "git", "cat-file", "-p", id.String())
  71. if err != nil {
  72. return nil, errors.New(string(bytErr))
  73. }
  74. tag, err := parseTagData(data)
  75. if err != nil {
  76. return nil, err
  77. }
  78. tag.Id = id
  79. tag.repo = repo
  80. repo.tagCache[id] = tag
  81. return tag, nil
  82. }
  83. // GetTag returns a Git tag by given name.
  84. func (repo *Repository) GetTag(tagName string) (*Tag, error) {
  85. stdout, stderr, err := com.ExecCmdDir(repo.Path, "git", "show-ref", "--tags", tagName)
  86. if err != nil {
  87. return nil, errors.New(stderr)
  88. }
  89. id, err := NewIdFromString(strings.Split(stdout, " ")[0])
  90. if err != nil {
  91. return nil, err
  92. }
  93. tag, err := repo.getTag(id)
  94. if err != nil {
  95. return nil, err
  96. }
  97. tag.Name = tagName
  98. return tag, nil
  99. }