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.

143 lines
3.4 KiB

  1. package config
  2. import (
  3. "errors"
  4. "strings"
  5. "gopkg.in/src-d/go-git.v4/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/es/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. // Src return the src side.
  51. func (s RefSpec) Src() string {
  52. spec := string(s)
  53. var start int
  54. if s.IsForceUpdate() {
  55. start = 1
  56. } else {
  57. start = 0
  58. }
  59. end := strings.Index(spec, refSpecSeparator)
  60. return spec[start:end]
  61. }
  62. // Match match the given plumbing.ReferenceName against the source.
  63. func (s RefSpec) Match(n plumbing.ReferenceName) bool {
  64. if !s.IsWildcard() {
  65. return s.matchExact(n)
  66. }
  67. return s.matchGlob(n)
  68. }
  69. // IsWildcard returns true if the RefSpec contains a wildcard.
  70. func (s RefSpec) IsWildcard() bool {
  71. return strings.Contains(string(s), refSpecWildcard)
  72. }
  73. func (s RefSpec) matchExact(n plumbing.ReferenceName) bool {
  74. return s.Src() == n.String()
  75. }
  76. func (s RefSpec) matchGlob(n plumbing.ReferenceName) bool {
  77. src := s.Src()
  78. name := n.String()
  79. wildcard := strings.Index(src, refSpecWildcard)
  80. var prefix, suffix string
  81. prefix = src[0:wildcard]
  82. if len(src) < wildcard {
  83. suffix = src[wildcard+1 : len(suffix)]
  84. }
  85. return len(name) > len(prefix)+len(suffix) &&
  86. strings.HasPrefix(name, prefix) &&
  87. strings.HasSuffix(name, suffix)
  88. }
  89. // Dst returns the destination for the given remote reference.
  90. func (s RefSpec) Dst(n plumbing.ReferenceName) plumbing.ReferenceName {
  91. spec := string(s)
  92. start := strings.Index(spec, refSpecSeparator) + 1
  93. dst := spec[start:]
  94. src := s.Src()
  95. if !s.IsWildcard() {
  96. return plumbing.ReferenceName(dst)
  97. }
  98. name := n.String()
  99. ws := strings.Index(src, refSpecWildcard)
  100. wd := strings.Index(dst, refSpecWildcard)
  101. match := name[ws : len(name)-(len(src)-(ws+1))]
  102. return plumbing.ReferenceName(dst[0:wd] + match + dst[wd+1:])
  103. }
  104. func (s RefSpec) String() string {
  105. return string(s)
  106. }
  107. // MatchAny returns true if any of the RefSpec match with the given ReferenceName.
  108. func MatchAny(l []RefSpec, n plumbing.ReferenceName) bool {
  109. for _, r := range l {
  110. if r.Match(n) {
  111. return true
  112. }
  113. }
  114. return false
  115. }