Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit a35ce6e

Browse files
authored
Merge pull request #1160 from novas0x2a/fix-refspec
fix wildcard handling in RefSpec matching
2 parents 26b54e8 + af3226f commit a35ce6e

File tree

2 files changed

+62
-11
lines changed

2 files changed

+62
-11
lines changed

config/refspec.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ func (s RefSpec) matchGlob(n plumbing.ReferenceName) bool {
9999

100100
var prefix, suffix string
101101
prefix = src[0:wildcard]
102-
if len(src) < wildcard {
103-
suffix = src[wildcard+1 : len(suffix)]
102+
if len(src) > wildcard+1 {
103+
suffix = src[wildcard+1:]
104104
}
105105

106-
return len(name) > len(prefix)+len(suffix) &&
106+
return len(name) >= len(prefix)+len(suffix) &&
107107
strings.HasPrefix(name, prefix) &&
108108
strings.HasSuffix(name, suffix)
109109
}

config/refspec_test.go

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,38 @@ func (s *RefSpecSuite) TestRefSpecMatch(c *C) {
9696
}
9797

9898
func (s *RefSpecSuite) TestRefSpecMatchGlob(c *C) {
99-
spec := RefSpec("refs/heads/*:refs/remotes/origin/*")
100-
c.Assert(spec.Match(plumbing.ReferenceName("refs/tag/foo")), Equals, false)
101-
c.Assert(spec.Match(plumbing.ReferenceName("refs/heads/foo")), Equals, true)
99+
tests := map[string]map[string]bool{
100+
"refs/heads/*:refs/remotes/origin/*": {
101+
"refs/tag/foo": false,
102+
"refs/heads/foo": true,
103+
},
104+
"refs/heads/*bc:refs/remotes/origin/*bc": {
105+
"refs/heads/abc": true,
106+
"refs/heads/bc": true,
107+
"refs/heads/abx": false,
108+
},
109+
"refs/heads/a*c:refs/remotes/origin/a*c": {
110+
"refs/heads/abc": true,
111+
"refs/heads/ac": true,
112+
"refs/heads/abx": false,
113+
},
114+
"refs/heads/ab*:refs/remotes/origin/ab*": {
115+
"refs/heads/abc": true,
116+
"refs/heads/ab": true,
117+
"refs/heads/xbc": false,
118+
},
119+
}
120+
121+
for specStr, data := range tests {
122+
spec := RefSpec(specStr)
123+
for ref, matches := range data {
124+
c.Assert(spec.Match(plumbing.ReferenceName(ref)),
125+
Equals,
126+
matches,
127+
Commentf("while matching spec %q against ref %q", specStr, ref),
128+
)
129+
}
130+
}
102131
}
103132

104133
func (s *RefSpecSuite) TestRefSpecDst(c *C) {
@@ -110,11 +139,33 @@ func (s *RefSpecSuite) TestRefSpecDst(c *C) {
110139
}
111140

112141
func (s *RefSpecSuite) TestRefSpecDstBlob(c *C) {
113-
spec := RefSpec("refs/heads/*:refs/remotes/origin/*")
114-
c.Assert(
115-
spec.Dst(plumbing.ReferenceName("refs/heads/foo")).String(), Equals,
116-
"refs/remotes/origin/foo",
117-
)
142+
ref := "refs/heads/abc"
143+
tests := map[string]string{
144+
"refs/heads/*:refs/remotes/origin/*": "refs/remotes/origin/abc",
145+
"refs/heads/*bc:refs/remotes/origin/*": "refs/remotes/origin/a",
146+
"refs/heads/*bc:refs/remotes/origin/*bc": "refs/remotes/origin/abc",
147+
"refs/heads/a*c:refs/remotes/origin/*": "refs/remotes/origin/b",
148+
"refs/heads/a*c:refs/remotes/origin/a*c": "refs/remotes/origin/abc",
149+
"refs/heads/ab*:refs/remotes/origin/*": "refs/remotes/origin/c",
150+
"refs/heads/ab*:refs/remotes/origin/ab*": "refs/remotes/origin/abc",
151+
"refs/heads/*abc:refs/remotes/origin/*abc": "refs/remotes/origin/abc",
152+
"refs/heads/abc*:refs/remotes/origin/abc*": "refs/remotes/origin/abc",
153+
// for these two cases, git specifically logs:
154+
// error: * Ignoring funny ref 'refs/remotes/origin/' locally
155+
// and ignores the ref; go-git does not currently do this validation,
156+
// but probably should.
157+
// "refs/heads/*abc:refs/remotes/origin/*": "",
158+
// "refs/heads/abc*:refs/remotes/origin/*": "",
159+
}
160+
161+
for specStr, dst := range tests {
162+
spec := RefSpec(specStr)
163+
c.Assert(spec.Dst(plumbing.ReferenceName(ref)).String(),
164+
Equals,
165+
dst,
166+
Commentf("while getting dst from spec %q with ref %q", specStr, ref),
167+
)
168+
}
118169
}
119170
func (s *RefSpecSuite) TestMatchAny(c *C) {
120171
specs := []RefSpec{

0 commit comments

Comments
 (0)