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.go 2.1KB

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