repo_tag.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. // Tag is a commit.
  47. if ObjectType(tp) == COMMIT {
  48. tag := &Tag{
  49. Id: id,
  50. Object: id,
  51. Type: string(COMMIT),
  52. repo: repo,
  53. }
  54. repo.tagCache[id] = tag
  55. return tag, nil
  56. }
  57. // Tag with message.
  58. data, bytErr, err := com.ExecCmdDirBytes(repo.Path, "git", "cat-file", "-p", id.String())
  59. if err != nil {
  60. return nil, errors.New(string(bytErr))
  61. }
  62. tag, err := parseTagData(data)
  63. if err != nil {
  64. return nil, err
  65. }
  66. tag.Id = id
  67. tag.repo = repo
  68. repo.tagCache[id] = tag
  69. return tag, nil
  70. }
  71. // GetTag returns a Git tag by given name.
  72. func (repo *Repository) GetTag(tagName string) (*Tag, error) {
  73. stdout, stderr, err := com.ExecCmdDir(repo.Path, "git", "show-ref", "--tags", tagName)
  74. if err != nil {
  75. return nil, errors.New(stderr)
  76. }
  77. id, err := NewIdFromString(strings.Split(stdout, " ")[0])
  78. if err != nil {
  79. return nil, err
  80. }
  81. tag, err := repo.getTag(id)
  82. if err != nil {
  83. return nil, err
  84. }
  85. tag.Name = tagName
  86. return tag, nil
  87. }