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.

refspec.go 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package config
  2. import (
  3. "errors"
  4. "strings"
  5. "github.com/go-git/go-git/v5/plumbing"
  6. )
  7. const (
  8. refSpecWildcard = "*"
  9. refSpecForce = "+"
  10. refSpecSeparator = ":"
  11. )
  12. var (
  13. ErrRefSpecMalformedSeparator = errors.New("malformed refspec, separators are wrong")
  14. ErrRefSpecMalformedWildcard = errors.New("malformed refspec, mismatched number of wildcards")
  15. )
  16. // RefSpec is a mapping from local branches to remote references.
  17. // The format of the refspec is an optional +, followed by <src>:<dst>, where
  18. // <src> is the pattern for references on the remote side and <dst> is where
  19. // those references will be written locally. The + tells Git to update the
  20. // reference even if it isn’t a fast-forward.
  21. // eg.: "+refs/heads/*:refs/remotes/origin/*"
  22. //
  23. // https://git-scm.com/book/en/v2/Git-Internals-The-Refspec
  24. type RefSpec string
  25. // Validate validates the RefSpec
  26. func (s RefSpec) Validate() error {
  27. spec := string(s)
  28. if strings.Count(spec, refSpecSeparator) != 1 {
  29. return ErrRefSpecMalformedSeparator
  30. }
  31. sep := strings.Index(spec, refSpecSeparator)
  32. if sep == len(spec)-1 {
  33. return ErrRefSpecMalformedSeparator
  34. }
  35. ws := strings.Count(spec[0:sep], refSpecWildcard)
  36. wd := strings.Count(spec[sep+1:], refSpecWildcard)
  37. if ws == wd && ws < 2 && wd < 2 {
  38. return nil
  39. }
  40. return ErrRefSpecMalformedWildcard
  41. }
  42. // IsForceUpdate returns if update is allowed in non fast-forward merges.
  43. func (s RefSpec) IsForceUpdate() bool {
  44. return s[0] == refSpecForce[0]
  45. }
  46. // IsDelete returns true if the refspec indicates a delete (empty src).
  47. func (s RefSpec) IsDelete() bool {
  48. return s[0] == refSpecSeparator[0]
  49. }
  50. // IsExactSHA1 returns true if the source is a SHA1 hash.
  51. func (s RefSpec) IsExactSHA1() bool {
  52. return plumbing.IsHash(s.Src())
  53. }
  54. // Src return the src side.
  55. func (s RefSpec) Src() string {
  56. spec := string(s)
  57. var start int
  58. if s.IsForceUpdate() {
  59. start = 1
  60. } else {
  61. start = 0
  62. }
  63. end := strings.Index(spec, refSpecSeparator)
  64. return spec[start:end]
  65. }
  66. // Match match the given plumbing.ReferenceName against the source.
  67. func (s RefSpec) Match(n plumbing.ReferenceName) bool {
  68. if !s.IsWildcard() {
  69. return s.matchExact(n)
  70. }
  71. return s.matchGlob(n)
  72. }
  73. // IsWildcard returns true if the RefSpec contains a wildcard.
  74. func (s RefSpec) IsWildcard() bool {
  75. return strings.Contains(string(s), refSpecWildcard)
  76. }
  77. func (s RefSpec) matchExact(n plumbing.ReferenceName) bool {
  78. return s.Src() == n.String()
  79. }
  80. func (s RefSpec) matchGlob(n plumbing.ReferenceName) bool {
  81. src := s.Src()
  82. name := n.String()
  83. wildcard := strings.Index(src, refSpecWildcard)
  84. var prefix, suffix string
  85. prefix = src[0:wildcard]
  86. if len(src) > wildcard+1 {
  87. suffix = src[wildcard+1:]
  88. }
  89. return len(name) >= len(prefix)+len(suffix) &&
  90. strings.HasPrefix(name, prefix) &&
  91. strings.HasSuffix(name, suffix)
  92. }
  93. // Dst returns the destination for the given remote reference.
  94. func (s RefSpec) Dst(n plumbing.ReferenceName) plumbing.ReferenceName {
  95. spec := string(s)
  96. start := strings.Index(spec, refSpecSeparator) + 1
  97. dst := spec[start:]
  98. src := s.Src()
  99. if !s.IsWildcard() {
  100. return plumbing.ReferenceName(dst)
  101. }
  102. name := n.String()
  103. ws := strings.Index(src, refSpecWildcard)
  104. wd := strings.Index(dst, refSpecWildcard)
  105. match := name[ws : len(name)-(len(src)-(ws+1))]
  106. return plumbing.ReferenceName(dst[0:wd] + match + dst[wd+1:])
  107. }
  108. func (s RefSpec) Reverse() RefSpec {
  109. spec := string(s)
  110. separator := strings.Index(spec, refSpecSeparator)
  111. return RefSpec(spec[separator+1:] + refSpecSeparator + spec[:separator])
  112. }
  113. func (s RefSpec) String() string {
  114. return string(s)
  115. }
  116. // MatchAny returns true if any of the RefSpec match with the given ReferenceName.
  117. func MatchAny(l []RefSpec, n plumbing.ReferenceName) bool {
  118. for _, r := range l {
  119. if r.Match(n) {
  120. return true
  121. }
  122. }
  123. return false
  124. }