@@ -11,11 +11,13 @@ const (
11
11
refSpecWildcard = "*"
12
12
refSpecForce = "+"
13
13
refSpecSeparator = ":"
14
+ refSpecNegative = "^"
14
15
)
15
16
16
17
var (
17
18
ErrRefSpecMalformedSeparator = errors .New ("malformed refspec, separators are wrong" )
18
19
ErrRefSpecMalformedWildcard = errors .New ("malformed refspec, mismatched number of wildcards" )
20
+ ErrRefSpecMalformedNegative = errors .New ("malformed negative refspec, one ^ and no separators allowed" )
19
21
)
20
22
21
23
// RefSpec is a mapping from local branches to remote references.
@@ -31,6 +33,24 @@ type RefSpec string
31
33
// Validate validates the RefSpec
32
34
func (s RefSpec ) Validate () error {
33
35
spec := string (s )
36
+
37
+ if strings .Index (spec , refSpecNegative ) == 0 {
38
+ // This is a negative refspec
39
+ if strings .Count (spec , refSpecNegative ) != 1 {
40
+ return ErrRefSpecMalformedNegative
41
+ }
42
+
43
+ if strings .Count (spec , refSpecSeparator ) != 0 {
44
+ return ErrRefSpecMalformedNegative
45
+ }
46
+
47
+ if strings .Count (spec , refSpecWildcard ) > 1 {
48
+ return ErrRefSpecMalformedWildcard
49
+ }
50
+
51
+ return nil
52
+ }
53
+
34
54
if strings .Count (spec , refSpecSeparator ) != 1 {
35
55
return ErrRefSpecMalformedSeparator
36
56
}
@@ -64,12 +84,17 @@ func (s RefSpec) IsExactSHA1() bool {
64
84
return plumbing .IsHash (s .Src ())
65
85
}
66
86
87
+ // IsNegative returns if the refspec is a negative one
88
+ func (s RefSpec ) IsNegative () bool {
89
+ return s [0 ] == refSpecNegative [0 ]
90
+ }
91
+
67
92
// Src return the src side.
68
93
func (s RefSpec ) Src () string {
69
94
spec := string (s )
70
95
71
96
var start int
72
- if s .IsForceUpdate () {
97
+ if s .IsForceUpdate () || s . IsNegative () {
73
98
start = 1
74
99
} else {
75
100
start = 0
0 commit comments