1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.eclipse.jgit.transport;
47
48 import static org.junit.Assert.assertEquals;
49 import static org.junit.Assert.assertFalse;
50 import static org.junit.Assert.assertNotNull;
51 import static org.junit.Assert.assertNotSame;
52 import static org.junit.Assert.assertNull;
53 import static org.junit.Assert.assertSame;
54 import static org.junit.Assert.assertTrue;
55
56 import org.eclipse.jgit.lib.ObjectIdRef;
57 import org.eclipse.jgit.lib.Ref;
58 import org.eclipse.jgit.transport.RefSpec.WildcardMode;
59 import org.junit.Test;
60
61 public class RefSpecTest {
62 @Test
63 public void testMasterMaster() {
64 final String sn = "refs/heads/master";
65 final RefSpec rs = new RefSpec(sn + ":" + sn);
66 assertFalse(rs.isForceUpdate());
67 assertFalse(rs.isWildcard());
68 assertEquals(sn, rs.getSource());
69 assertEquals(sn, rs.getDestination());
70 assertEquals(sn + ":" + sn, rs.toString());
71 assertEquals(rs, new RefSpec(rs.toString()));
72
73 Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn, null);
74 assertTrue(rs.matchSource(r));
75 assertTrue(rs.matchDestination(r));
76 assertSame(rs, rs.expandFromSource(r));
77
78 r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn + "-and-more", null);
79 assertFalse(rs.matchSource(r));
80 assertFalse(rs.matchDestination(r));
81 }
82
83 @Test
84 public void testSplitLastColon() {
85 final String lhs = ":m:a:i:n:t";
86 final String rhs = "refs/heads/maint";
87 final RefSpec rs = new RefSpec(lhs + ":" + rhs);
88 assertFalse(rs.isForceUpdate());
89 assertFalse(rs.isWildcard());
90 assertEquals(lhs, rs.getSource());
91 assertEquals(rhs, rs.getDestination());
92 assertEquals(lhs + ":" + rhs, rs.toString());
93 assertEquals(rs, new RefSpec(rs.toString()));
94 }
95
96 @Test
97 public void testForceMasterMaster() {
98 final String sn = "refs/heads/master";
99 final RefSpec rs = new RefSpec("+" + sn + ":" + sn);
100 assertTrue(rs.isForceUpdate());
101 assertFalse(rs.isWildcard());
102 assertEquals(sn, rs.getSource());
103 assertEquals(sn, rs.getDestination());
104 assertEquals("+" + sn + ":" + sn, rs.toString());
105 assertEquals(rs, new RefSpec(rs.toString()));
106
107 Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn, null);
108 assertTrue(rs.matchSource(r));
109 assertTrue(rs.matchDestination(r));
110 assertSame(rs, rs.expandFromSource(r));
111
112 r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn + "-and-more", null);
113 assertFalse(rs.matchSource(r));
114 assertFalse(rs.matchDestination(r));
115 }
116
117 @Test
118 public void testMaster() {
119 final String sn = "refs/heads/master";
120 final RefSpec rs = new RefSpec(sn);
121 assertFalse(rs.isForceUpdate());
122 assertFalse(rs.isWildcard());
123 assertEquals(sn, rs.getSource());
124 assertNull(rs.getDestination());
125 assertEquals(sn, rs.toString());
126 assertEquals(rs, new RefSpec(rs.toString()));
127
128 Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn, null);
129 assertTrue(rs.matchSource(r));
130 assertFalse(rs.matchDestination(r));
131 assertSame(rs, rs.expandFromSource(r));
132
133 r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn + "-and-more", null);
134 assertFalse(rs.matchSource(r));
135 assertFalse(rs.matchDestination(r));
136 }
137
138 @Test
139 public void testForceMaster() {
140 final String sn = "refs/heads/master";
141 final RefSpec rs = new RefSpec("+" + sn);
142 assertTrue(rs.isForceUpdate());
143 assertFalse(rs.isWildcard());
144 assertEquals(sn, rs.getSource());
145 assertNull(rs.getDestination());
146 assertEquals("+" + sn, rs.toString());
147 assertEquals(rs, new RefSpec(rs.toString()));
148
149 Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn, null);
150 assertTrue(rs.matchSource(r));
151 assertFalse(rs.matchDestination(r));
152 assertSame(rs, rs.expandFromSource(r));
153
154 r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn + "-and-more", null);
155 assertFalse(rs.matchSource(r));
156 assertFalse(rs.matchDestination(r));
157 }
158
159 @Test
160 public void testDeleteMaster() {
161 final String sn = "refs/heads/master";
162 final RefSpec rs = new RefSpec(":" + sn);
163 assertFalse(rs.isForceUpdate());
164 assertFalse(rs.isWildcard());
165 assertNull(rs.getSource());
166 assertEquals(sn, rs.getDestination());
167 assertEquals(":" + sn, rs.toString());
168 assertEquals(rs, new RefSpec(rs.toString()));
169
170 Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn, null);
171 assertFalse(rs.matchSource(r));
172 assertTrue(rs.matchDestination(r));
173 assertSame(rs, rs.expandFromSource(r));
174
175 r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn + "-and-more", null);
176 assertFalse(rs.matchSource(r));
177 assertFalse(rs.matchDestination(r));
178 }
179
180 @Test
181 public void testForceRemotesOrigin() {
182 final String srcn = "refs/heads/*";
183 final String dstn = "refs/remotes/origin/*";
184 final RefSpec rs = new RefSpec("+" + srcn + ":" + dstn);
185 assertTrue(rs.isForceUpdate());
186 assertTrue(rs.isWildcard());
187 assertEquals(srcn, rs.getSource());
188 assertEquals(dstn, rs.getDestination());
189 assertEquals("+" + srcn + ":" + dstn, rs.toString());
190 assertEquals(rs, new RefSpec(rs.toString()));
191
192 Ref r;
193 RefSpec expanded;
194
195 r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master", null);
196 assertTrue(rs.matchSource(r));
197 assertFalse(rs.matchDestination(r));
198 expanded = rs.expandFromSource(r);
199 assertNotSame(rs, expanded);
200 assertTrue(expanded.isForceUpdate());
201 assertFalse(expanded.isWildcard());
202 assertEquals(r.getName(), expanded.getSource());
203 assertEquals("refs/remotes/origin/master", expanded.getDestination());
204
205 r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/remotes/origin/next", null);
206 assertFalse(rs.matchSource(r));
207 assertTrue(rs.matchDestination(r));
208
209 r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/tags/v1.0", null);
210 assertFalse(rs.matchSource(r));
211 assertFalse(rs.matchDestination(r));
212 }
213
214 @Test
215 public void testCreateEmpty() {
216 final RefSpec rs = new RefSpec();
217 assertFalse(rs.isForceUpdate());
218 assertFalse(rs.isWildcard());
219 assertEquals("HEAD", rs.getSource());
220 assertNull(rs.getDestination());
221 assertEquals("HEAD", rs.toString());
222 }
223
224 @Test
225 public void testSetForceUpdate() {
226 final String s = "refs/heads/*:refs/remotes/origin/*";
227 final RefSpec a = new RefSpec(s);
228 assertFalse(a.isForceUpdate());
229 RefSpec b = a.setForceUpdate(true);
230 assertNotSame(a, b);
231 assertFalse(a.isForceUpdate());
232 assertTrue(b.isForceUpdate());
233 assertEquals(s, a.toString());
234 assertEquals("+" + s, b.toString());
235 }
236
237 @Test
238 public void testSetSource() {
239 final RefSpec a = new RefSpec();
240 final RefSpec b = a.setSource("refs/heads/master");
241 assertNotSame(a, b);
242 assertEquals("HEAD", a.toString());
243 assertEquals("refs/heads/master", b.toString());
244 }
245
246 @Test
247 public void testSetDestination() {
248 final RefSpec a = new RefSpec();
249 final RefSpec b = a.setDestination("refs/heads/master");
250 assertNotSame(a, b);
251 assertEquals("HEAD", a.toString());
252 assertEquals("HEAD:refs/heads/master", b.toString());
253 }
254
255 @Test
256 public void testSetDestination_SourceNull() {
257 final RefSpec a = new RefSpec();
258 RefSpec b;
259
260 b = a.setDestination("refs/heads/master");
261 b = b.setSource(null);
262 assertNotSame(a, b);
263 assertEquals("HEAD", a.toString());
264 assertEquals(":refs/heads/master", b.toString());
265 }
266
267 @Test
268 public void testSetSourceDestination() {
269 final RefSpec a = new RefSpec();
270 final RefSpec b;
271 b = a.setSourceDestination("refs/heads/*", "refs/remotes/origin/*");
272 assertNotSame(a, b);
273 assertEquals("HEAD", a.toString());
274 assertEquals("refs/heads/*:refs/remotes/origin/*", b.toString());
275 }
276
277 @Test
278 public void testExpandFromDestination_NonWildcard() {
279 final String src = "refs/heads/master";
280 final String dst = "refs/remotes/origin/master";
281 final RefSpec a = new RefSpec(src + ":" + dst);
282 final RefSpec r = a.expandFromDestination(dst);
283 assertSame(a, r);
284 assertFalse(r.isWildcard());
285 assertEquals(src, r.getSource());
286 assertEquals(dst, r.getDestination());
287 }
288
289 @Test
290 public void testExpandFromDestination_Wildcard() {
291 final String src = "refs/heads/master";
292 final String dst = "refs/remotes/origin/master";
293 final RefSpec a = new RefSpec("refs/heads/*:refs/remotes/origin/*");
294 final RefSpec r = a.expandFromDestination(dst);
295 assertNotSame(a, r);
296 assertFalse(r.isWildcard());
297 assertEquals(src, r.getSource());
298 assertEquals(dst, r.getDestination());
299 }
300
301 @Test
302 public void isWildcardShouldWorkForWildcardSuffixAndComponent() {
303 assertTrue(RefSpec.isWildcard("refs/heads/*"));
304 assertTrue(RefSpec.isWildcard("refs/pull/*/head"));
305 assertFalse(RefSpec.isWildcard("refs/heads/a"));
306 }
307
308 @Test
309 public void testWildcardInMiddleOfSource() {
310 RefSpec a = new RefSpec("+refs/pull/*/head:refs/remotes/origin/pr/*");
311 assertTrue(a.isWildcard());
312 assertTrue(a.matchSource("refs/pull/a/head"));
313 assertTrue(a.matchSource("refs/pull/foo/head"));
314 assertTrue(a.matchSource("refs/pull/foo/bar/head"));
315 assertFalse(a.matchSource("refs/pull/foo"));
316 assertFalse(a.matchSource("refs/pull/head"));
317 assertFalse(a.matchSource("refs/pull/foo/head/more"));
318 assertFalse(a.matchSource("refs/pullx/head"));
319
320 RefSpec b = a.expandFromSource("refs/pull/foo/head");
321 assertEquals("refs/remotes/origin/pr/foo", b.getDestination());
322 RefSpec c = a.expandFromDestination("refs/remotes/origin/pr/foo");
323 assertEquals("refs/pull/foo/head", c.getSource());
324 }
325
326 @Test
327 public void testWildcardInMiddleOfDestionation() {
328 RefSpec a = new RefSpec("+refs/heads/*:refs/remotes/origin/*/head");
329 assertTrue(a.isWildcard());
330 assertTrue(a.matchDestination("refs/remotes/origin/a/head"));
331 assertTrue(a.matchDestination("refs/remotes/origin/foo/head"));
332 assertTrue(a.matchDestination("refs/remotes/origin/foo/bar/head"));
333 assertFalse(a.matchDestination("refs/remotes/origin/foo"));
334 assertFalse(a.matchDestination("refs/remotes/origin/head"));
335 assertFalse(a.matchDestination("refs/remotes/origin/foo/head/more"));
336 assertFalse(a.matchDestination("refs/remotes/originx/head"));
337
338 RefSpec b = a.expandFromSource("refs/heads/foo");
339 assertEquals("refs/remotes/origin/foo/head", b.getDestination());
340 RefSpec c = a.expandFromDestination("refs/remotes/origin/foo/head");
341 assertEquals("refs/heads/foo", c.getSource());
342 }
343
344 @Test
345 public void testWildcardAfterText1() {
346 RefSpec a = new RefSpec("refs/heads/*/for-linus:refs/remotes/mine/*-blah");
347 assertTrue(a.isWildcard());
348 assertTrue(a.matchDestination("refs/remotes/mine/x-blah"));
349 assertTrue(a.matchDestination("refs/remotes/mine/foo-blah"));
350 assertTrue(a.matchDestination("refs/remotes/mine/foo/x-blah"));
351 assertFalse(a.matchDestination("refs/remotes/origin/foo/x-blah"));
352
353 RefSpec b = a.expandFromSource("refs/heads/foo/for-linus");
354 assertEquals("refs/remotes/mine/foo-blah", b.getDestination());
355 RefSpec c = a.expandFromDestination("refs/remotes/mine/foo-blah");
356 assertEquals("refs/heads/foo/for-linus", c.getSource());
357 }
358
359 @Test
360 public void testWildcardAfterText2() {
361 RefSpec a = new RefSpec("refs/heads*/for-linus:refs/remotes/mine/*");
362 assertTrue(a.isWildcard());
363 assertTrue(a.matchSource("refs/headsx/for-linus"));
364 assertTrue(a.matchSource("refs/headsfoo/for-linus"));
365 assertTrue(a.matchSource("refs/headsx/foo/for-linus"));
366 assertFalse(a.matchSource("refs/headx/for-linus"));
367
368 RefSpec b = a.expandFromSource("refs/headsx/for-linus");
369 assertEquals("refs/remotes/mine/x", b.getDestination());
370 RefSpec c = a.expandFromDestination("refs/remotes/mine/x");
371 assertEquals("refs/headsx/for-linus", c.getSource());
372
373 RefSpec d = a.expandFromSource("refs/headsx/foo/for-linus");
374 assertEquals("refs/remotes/mine/x/foo", d.getDestination());
375 RefSpec e = a.expandFromDestination("refs/remotes/mine/x/foo");
376 assertEquals("refs/headsx/foo/for-linus", e.getSource());
377 }
378
379 @Test
380 public void testWildcardMirror() {
381 RefSpec a = new RefSpec("*:*");
382 assertTrue(a.isWildcard());
383 assertTrue(a.matchSource("a"));
384 assertTrue(a.matchSource("foo"));
385 assertTrue(a.matchSource("foo/bar"));
386 assertTrue(a.matchDestination("a"));
387 assertTrue(a.matchDestination("foo"));
388 assertTrue(a.matchDestination("foo/bar"));
389
390 RefSpec b = a.expandFromSource("refs/heads/foo");
391 assertEquals("refs/heads/foo", b.getDestination());
392 RefSpec c = a.expandFromDestination("refs/heads/foo");
393 assertEquals("refs/heads/foo", c.getSource());
394 }
395
396 @Test
397 public void testWildcardAtStart() {
398 RefSpec a = new RefSpec("*/head:refs/heads/*");
399 assertTrue(a.isWildcard());
400 assertTrue(a.matchSource("a/head"));
401 assertTrue(a.matchSource("foo/head"));
402 assertTrue(a.matchSource("foo/bar/head"));
403 assertFalse(a.matchSource("/head"));
404 assertFalse(a.matchSource("a/head/extra"));
405
406 RefSpec b = a.expandFromSource("foo/head");
407 assertEquals("refs/heads/foo", b.getDestination());
408 RefSpec c = a.expandFromDestination("refs/heads/foo");
409 assertEquals("foo/head", c.getSource());
410 }
411
412 @Test(expected = IllegalArgumentException.class)
413 public void invalidWhenSourceEndsWithSlash() {
414 assertNotNull(new RefSpec("refs/heads/"));
415 }
416
417 @Test(expected = IllegalArgumentException.class)
418 public void invalidWhenDestinationEndsWithSlash() {
419 assertNotNull(new RefSpec("refs/heads/master:refs/heads/"));
420 }
421
422 @Test(expected = IllegalArgumentException.class)
423 public void invalidWhenSourceOnlyAndWildcard() {
424 assertNotNull(new RefSpec("refs/heads/*"));
425 }
426
427 @Test(expected = IllegalArgumentException.class)
428 public void invalidWhenDestinationOnlyAndWildcard() {
429 assertNotNull(new RefSpec(":refs/heads/*"));
430 }
431
432 @Test(expected = IllegalArgumentException.class)
433 public void invalidWhenOnlySourceWildcard() {
434 assertNotNull(new RefSpec("refs/heads/*:refs/heads/foo"));
435 }
436
437 @Test(expected = IllegalArgumentException.class)
438 public void invalidWhenOnlyDestinationWildcard() {
439 assertNotNull(new RefSpec("refs/heads/foo:refs/heads/*"));
440 }
441
442 @Test(expected = IllegalArgumentException.class)
443 public void invalidWhenMoreThanOneWildcardInSource() {
444 assertNotNull(new RefSpec("refs/heads/*/*:refs/heads/*"));
445 }
446
447 @Test(expected = IllegalArgumentException.class)
448 public void invalidWhenMoreThanOneWildcardInDestination() {
449 assertNotNull(new RefSpec("refs/heads/*:refs/heads/*/*"));
450 }
451
452 @Test(expected = IllegalArgumentException.class)
453 public void invalidSourceDoubleSlashes() {
454 assertNotNull(new RefSpec("refs/heads//wrong"));
455 }
456
457 @Test(expected = IllegalArgumentException.class)
458 public void invalidSlashAtStart() {
459 assertNotNull(new RefSpec("/foo:/foo"));
460 }
461
462 @Test(expected = IllegalArgumentException.class)
463 public void invalidDestinationDoubleSlashes() {
464 assertNotNull(new RefSpec(":refs/heads//wrong"));
465 }
466
467 @Test(expected = IllegalArgumentException.class)
468 public void invalidSetSource() {
469 RefSpec a = new RefSpec("refs/heads/*:refs/remotes/origin/*");
470 a.setSource("refs/heads/*/*");
471 }
472
473 @Test(expected = IllegalArgumentException.class)
474 public void invalidSetDestination() {
475 RefSpec a = new RefSpec("refs/heads/*:refs/remotes/origin/*");
476 a.setDestination("refs/remotes/origin/*/*");
477 }
478
479 @Test
480 public void sourceOnlywithWildcard() {
481 RefSpec a = new RefSpec("refs/heads/*",
482 WildcardMode.ALLOW_MISMATCH);
483 assertTrue(a.matchSource("refs/heads/master"));
484 assertNull(a.getDestination());
485 }
486
487 @Test
488 public void destinationWithWildcard() {
489 RefSpec a = new RefSpec("refs/heads/master:refs/heads/*",
490 WildcardMode.ALLOW_MISMATCH);
491 assertTrue(a.matchSource("refs/heads/master"));
492 assertTrue(a.matchDestination("refs/heads/master"));
493 assertTrue(a.matchDestination("refs/heads/foo"));
494 }
495
496 @Test
497 public void onlyWildCard() {
498 RefSpec a = new RefSpec("*", WildcardMode.ALLOW_MISMATCH);
499 assertTrue(a.matchSource("refs/heads/master"));
500 assertNull(a.getDestination());
501 }
502 }