repo_tag.go 2.3 KB

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