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.

RefSpecTest.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * Copyright (C) 2013, Robin Stocker <robin@nibor.org>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.transport;
  46. import static org.junit.Assert.assertEquals;
  47. import static org.junit.Assert.assertFalse;
  48. import static org.junit.Assert.assertNotNull;
  49. import static org.junit.Assert.assertNotSame;
  50. import static org.junit.Assert.assertNull;
  51. import static org.junit.Assert.assertSame;
  52. import static org.junit.Assert.assertTrue;
  53. import org.eclipse.jgit.lib.ObjectIdRef;
  54. import org.eclipse.jgit.lib.Ref;
  55. import org.eclipse.jgit.transport.RefSpec.WildcardMode;
  56. import org.junit.Test;
  57. public class RefSpecTest {
  58. @Test
  59. public void testMasterMaster() {
  60. final String sn = "refs/heads/master";
  61. final RefSpec rs = new RefSpec(sn + ":" + sn);
  62. assertFalse(rs.isForceUpdate());
  63. assertFalse(rs.isWildcard());
  64. assertEquals(sn, rs.getSource());
  65. assertEquals(sn, rs.getDestination());
  66. assertEquals(sn + ":" + sn, rs.toString());
  67. assertEquals(rs, new RefSpec(rs.toString()));
  68. Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn, null);
  69. assertTrue(rs.matchSource(r));
  70. assertTrue(rs.matchDestination(r));
  71. assertSame(rs, rs.expandFromSource(r));
  72. r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn + "-and-more", null);
  73. assertFalse(rs.matchSource(r));
  74. assertFalse(rs.matchDestination(r));
  75. }
  76. @Test
  77. public void testSplitLastColon() {
  78. final String lhs = ":m:a:i:n:t";
  79. final String rhs = "refs/heads/maint";
  80. final RefSpec rs = new RefSpec(lhs + ":" + rhs);
  81. assertFalse(rs.isForceUpdate());
  82. assertFalse(rs.isWildcard());
  83. assertEquals(lhs, rs.getSource());
  84. assertEquals(rhs, rs.getDestination());
  85. assertEquals(lhs + ":" + rhs, rs.toString());
  86. assertEquals(rs, new RefSpec(rs.toString()));
  87. }
  88. @Test
  89. public void testForceMasterMaster() {
  90. final String sn = "refs/heads/master";
  91. final RefSpec rs = new RefSpec("+" + sn + ":" + sn);
  92. assertTrue(rs.isForceUpdate());
  93. assertFalse(rs.isWildcard());
  94. assertEquals(sn, rs.getSource());
  95. assertEquals(sn, rs.getDestination());
  96. assertEquals("+" + sn + ":" + sn, rs.toString());
  97. assertEquals(rs, new RefSpec(rs.toString()));
  98. Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn, null);
  99. assertTrue(rs.matchSource(r));
  100. assertTrue(rs.matchDestination(r));
  101. assertSame(rs, rs.expandFromSource(r));
  102. r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn + "-and-more", null);
  103. assertFalse(rs.matchSource(r));
  104. assertFalse(rs.matchDestination(r));
  105. }
  106. @Test
  107. public void testMaster() {
  108. final String sn = "refs/heads/master";
  109. final RefSpec rs = new RefSpec(sn);
  110. assertFalse(rs.isForceUpdate());
  111. assertFalse(rs.isWildcard());
  112. assertEquals(sn, rs.getSource());
  113. assertNull(rs.getDestination());
  114. assertEquals(sn, rs.toString());
  115. assertEquals(rs, new RefSpec(rs.toString()));
  116. Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn, null);
  117. assertTrue(rs.matchSource(r));
  118. assertFalse(rs.matchDestination(r));
  119. assertSame(rs, rs.expandFromSource(r));
  120. r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn + "-and-more", null);
  121. assertFalse(rs.matchSource(r));
  122. assertFalse(rs.matchDestination(r));
  123. }
  124. @Test
  125. public void testForceMaster() {
  126. final String sn = "refs/heads/master";
  127. final RefSpec rs = new RefSpec("+" + sn);
  128. assertTrue(rs.isForceUpdate());
  129. assertFalse(rs.isWildcard());
  130. assertEquals(sn, rs.getSource());
  131. assertNull(rs.getDestination());
  132. assertEquals("+" + sn, rs.toString());
  133. assertEquals(rs, new RefSpec(rs.toString()));
  134. Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn, null);
  135. assertTrue(rs.matchSource(r));
  136. assertFalse(rs.matchDestination(r));
  137. assertSame(rs, rs.expandFromSource(r));
  138. r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn + "-and-more", null);
  139. assertFalse(rs.matchSource(r));
  140. assertFalse(rs.matchDestination(r));
  141. }
  142. @Test
  143. public void testDeleteMaster() {
  144. final String sn = "refs/heads/master";
  145. final RefSpec rs = new RefSpec(":" + sn);
  146. assertFalse(rs.isForceUpdate());
  147. assertFalse(rs.isWildcard());
  148. assertNull(rs.getSource());
  149. assertEquals(sn, rs.getDestination());
  150. assertEquals(":" + sn, rs.toString());
  151. assertEquals(rs, new RefSpec(rs.toString()));
  152. Ref r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn, null);
  153. assertFalse(rs.matchSource(r));
  154. assertTrue(rs.matchDestination(r));
  155. assertSame(rs, rs.expandFromSource(r));
  156. r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, sn + "-and-more", null);
  157. assertFalse(rs.matchSource(r));
  158. assertFalse(rs.matchDestination(r));
  159. }
  160. @Test
  161. public void testForceRemotesOrigin() {
  162. final String srcn = "refs/heads/*";
  163. final String dstn = "refs/remotes/origin/*";
  164. final RefSpec rs = new RefSpec("+" + srcn + ":" + dstn);
  165. assertTrue(rs.isForceUpdate());
  166. assertTrue(rs.isWildcard());
  167. assertEquals(srcn, rs.getSource());
  168. assertEquals(dstn, rs.getDestination());
  169. assertEquals("+" + srcn + ":" + dstn, rs.toString());
  170. assertEquals(rs, new RefSpec(rs.toString()));
  171. Ref r;
  172. RefSpec expanded;
  173. r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/heads/master", null);
  174. assertTrue(rs.matchSource(r));
  175. assertFalse(rs.matchDestination(r));
  176. expanded = rs.expandFromSource(r);
  177. assertNotSame(rs, expanded);
  178. assertTrue(expanded.isForceUpdate());
  179. assertFalse(expanded.isWildcard());
  180. assertEquals(r.getName(), expanded.getSource());
  181. assertEquals("refs/remotes/origin/master", expanded.getDestination());
  182. r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/remotes/origin/next", null);
  183. assertFalse(rs.matchSource(r));
  184. assertTrue(rs.matchDestination(r));
  185. r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, "refs/tags/v1.0", null);
  186. assertFalse(rs.matchSource(r));
  187. assertFalse(rs.matchDestination(r));
  188. }
  189. @Test
  190. public void testCreateEmpty() {
  191. final RefSpec rs = new RefSpec();
  192. assertFalse(rs.isForceUpdate());
  193. assertFalse(rs.isWildcard());
  194. assertEquals("HEAD", rs.getSource());
  195. assertNull(rs.getDestination());
  196. assertEquals("HEAD", rs.toString());
  197. }
  198. @Test
  199. public void testSetForceUpdate() {
  200. final String s = "refs/heads/*:refs/remotes/origin/*";
  201. final RefSpec a = new RefSpec(s);
  202. assertFalse(a.isForceUpdate());
  203. RefSpec b = a.setForceUpdate(true);
  204. assertNotSame(a, b);
  205. assertFalse(a.isForceUpdate());
  206. assertTrue(b.isForceUpdate());
  207. assertEquals(s, a.toString());
  208. assertEquals("+" + s, b.toString());
  209. }
  210. @Test
  211. public void testSetSource() {
  212. final RefSpec a = new RefSpec();
  213. final RefSpec b = a.setSource("refs/heads/master");
  214. assertNotSame(a, b);
  215. assertEquals("HEAD", a.toString());
  216. assertEquals("refs/heads/master", b.toString());
  217. }
  218. @Test
  219. public void testSetDestination() {
  220. final RefSpec a = new RefSpec();
  221. final RefSpec b = a.setDestination("refs/heads/master");
  222. assertNotSame(a, b);
  223. assertEquals("HEAD", a.toString());
  224. assertEquals("HEAD:refs/heads/master", b.toString());
  225. }
  226. @Test
  227. public void testSetDestination_SourceNull() {
  228. final RefSpec a = new RefSpec();
  229. RefSpec b;
  230. b = a.setDestination("refs/heads/master");
  231. b = b.setSource(null);
  232. assertNotSame(a, b);
  233. assertEquals("HEAD", a.toString());
  234. assertEquals(":refs/heads/master", b.toString());
  235. }
  236. @Test
  237. public void testSetSourceDestination() {
  238. final RefSpec a = new RefSpec();
  239. final RefSpec b;
  240. b = a.setSourceDestination("refs/heads/*", "refs/remotes/origin/*");
  241. assertNotSame(a, b);
  242. assertEquals("HEAD", a.toString());
  243. assertEquals("refs/heads/*:refs/remotes/origin/*", b.toString());
  244. }
  245. @Test
  246. public void testExpandFromDestination_NonWildcard() {
  247. final String src = "refs/heads/master";
  248. final String dst = "refs/remotes/origin/master";
  249. final RefSpec a = new RefSpec(src + ":" + dst);
  250. final RefSpec r = a.expandFromDestination(dst);
  251. assertSame(a, r);
  252. assertFalse(r.isWildcard());
  253. assertEquals(src, r.getSource());
  254. assertEquals(dst, r.getDestination());
  255. }
  256. @Test
  257. public void testExpandFromDestination_Wildcard() {
  258. final String src = "refs/heads/master";
  259. final String dst = "refs/remotes/origin/master";
  260. final RefSpec a = new RefSpec("refs/heads/*:refs/remotes/origin/*");
  261. final RefSpec r = a.expandFromDestination(dst);
  262. assertNotSame(a, r);
  263. assertFalse(r.isWildcard());
  264. assertEquals(src, r.getSource());
  265. assertEquals(dst, r.getDestination());
  266. }
  267. @Test
  268. public void isWildcardShouldWorkForWildcardSuffixAndComponent() {
  269. assertTrue(RefSpec.isWildcard("refs/heads/*"));
  270. assertTrue(RefSpec.isWildcard("refs/pull/*/head"));
  271. assertFalse(RefSpec.isWildcard("refs/heads/a"));
  272. }
  273. @Test
  274. public void testWildcardInMiddleOfSource() {
  275. RefSpec a = new RefSpec("+refs/pull/*/head:refs/remotes/origin/pr/*");
  276. assertTrue(a.isWildcard());
  277. assertTrue(a.matchSource("refs/pull/a/head"));
  278. assertTrue(a.matchSource("refs/pull/foo/head"));
  279. assertTrue(a.matchSource("refs/pull/foo/bar/head"));
  280. assertFalse(a.matchSource("refs/pull/foo"));
  281. assertFalse(a.matchSource("refs/pull/head"));
  282. assertFalse(a.matchSource("refs/pull/foo/head/more"));
  283. assertFalse(a.matchSource("refs/pullx/head"));
  284. RefSpec b = a.expandFromSource("refs/pull/foo/head");
  285. assertEquals("refs/remotes/origin/pr/foo", b.getDestination());
  286. RefSpec c = a.expandFromDestination("refs/remotes/origin/pr/foo");
  287. assertEquals("refs/pull/foo/head", c.getSource());
  288. }
  289. @Test
  290. public void testWildcardInMiddleOfDestionation() {
  291. RefSpec a = new RefSpec("+refs/heads/*:refs/remotes/origin/*/head");
  292. assertTrue(a.isWildcard());
  293. assertTrue(a.matchDestination("refs/remotes/origin/a/head"));
  294. assertTrue(a.matchDestination("refs/remotes/origin/foo/head"));
  295. assertTrue(a.matchDestination("refs/remotes/origin/foo/bar/head"));
  296. assertFalse(a.matchDestination("refs/remotes/origin/foo"));
  297. assertFalse(a.matchDestination("refs/remotes/origin/head"));
  298. assertFalse(a.matchDestination("refs/remotes/origin/foo/head/more"));
  299. assertFalse(a.matchDestination("refs/remotes/originx/head"));
  300. RefSpec b = a.expandFromSource("refs/heads/foo");
  301. assertEquals("refs/remotes/origin/foo/head", b.getDestination());
  302. RefSpec c = a.expandFromDestination("refs/remotes/origin/foo/head");
  303. assertEquals("refs/heads/foo", c.getSource());
  304. }
  305. @Test
  306. public void testWildcardAfterText1() {
  307. RefSpec a = new RefSpec("refs/heads/*/for-linus:refs/remotes/mine/*-blah");
  308. assertTrue(a.isWildcard());
  309. assertTrue(a.matchDestination("refs/remotes/mine/x-blah"));
  310. assertTrue(a.matchDestination("refs/remotes/mine/foo-blah"));
  311. assertTrue(a.matchDestination("refs/remotes/mine/foo/x-blah"));
  312. assertFalse(a.matchDestination("refs/remotes/origin/foo/x-blah"));
  313. RefSpec b = a.expandFromSource("refs/heads/foo/for-linus");
  314. assertEquals("refs/remotes/mine/foo-blah", b.getDestination());
  315. RefSpec c = a.expandFromDestination("refs/remotes/mine/foo-blah");
  316. assertEquals("refs/heads/foo/for-linus", c.getSource());
  317. }
  318. @Test
  319. public void testWildcardAfterText2() {
  320. RefSpec a = new RefSpec("refs/heads*/for-linus:refs/remotes/mine/*");
  321. assertTrue(a.isWildcard());
  322. assertTrue(a.matchSource("refs/headsx/for-linus"));
  323. assertTrue(a.matchSource("refs/headsfoo/for-linus"));
  324. assertTrue(a.matchSource("refs/headsx/foo/for-linus"));
  325. assertFalse(a.matchSource("refs/headx/for-linus"));
  326. RefSpec b = a.expandFromSource("refs/headsx/for-linus");
  327. assertEquals("refs/remotes/mine/x", b.getDestination());
  328. RefSpec c = a.expandFromDestination("refs/remotes/mine/x");
  329. assertEquals("refs/headsx/for-linus", c.getSource());
  330. RefSpec d = a.expandFromSource("refs/headsx/foo/for-linus");
  331. assertEquals("refs/remotes/mine/x/foo", d.getDestination());
  332. RefSpec e = a.expandFromDestination("refs/remotes/mine/x/foo");
  333. assertEquals("refs/headsx/foo/for-linus", e.getSource());
  334. }
  335. @Test
  336. public void testWildcardMirror() {
  337. RefSpec a = new RefSpec("*:*");
  338. assertTrue(a.isWildcard());
  339. assertTrue(a.matchSource("a"));
  340. assertTrue(a.matchSource("foo"));
  341. assertTrue(a.matchSource("foo/bar"));
  342. assertTrue(a.matchDestination("a"));
  343. assertTrue(a.matchDestination("foo"));
  344. assertTrue(a.matchDestination("foo/bar"));
  345. RefSpec b = a.expandFromSource("refs/heads/foo");
  346. assertEquals("refs/heads/foo", b.getDestination());
  347. RefSpec c = a.expandFromDestination("refs/heads/foo");
  348. assertEquals("refs/heads/foo", c.getSource());
  349. }
  350. @Test
  351. public void testWildcardAtStart() {
  352. RefSpec a = new RefSpec("*/head:refs/heads/*");
  353. assertTrue(a.isWildcard());
  354. assertTrue(a.matchSource("a/head"));
  355. assertTrue(a.matchSource("foo/head"));
  356. assertTrue(a.matchSource("foo/bar/head"));
  357. assertFalse(a.matchSource("/head"));
  358. assertFalse(a.matchSource("a/head/extra"));
  359. RefSpec b = a.expandFromSource("foo/head");
  360. assertEquals("refs/heads/foo", b.getDestination());
  361. RefSpec c = a.expandFromDestination("refs/heads/foo");
  362. assertEquals("foo/head", c.getSource());
  363. }
  364. @Test(expected = IllegalArgumentException.class)
  365. public void invalidWhenSourceEndsWithSlash() {
  366. assertNotNull(new RefSpec("refs/heads/"));
  367. }
  368. @Test(expected = IllegalArgumentException.class)
  369. public void invalidWhenDestinationEndsWithSlash() {
  370. assertNotNull(new RefSpec("refs/heads/master:refs/heads/"));
  371. }
  372. @Test(expected = IllegalArgumentException.class)
  373. public void invalidWhenSourceOnlyAndWildcard() {
  374. assertNotNull(new RefSpec("refs/heads/*"));
  375. }
  376. @Test(expected = IllegalArgumentException.class)
  377. public void invalidWhenDestinationOnlyAndWildcard() {
  378. assertNotNull(new RefSpec(":refs/heads/*"));
  379. }
  380. @Test(expected = IllegalArgumentException.class)
  381. public void invalidWhenOnlySourceWildcard() {
  382. assertNotNull(new RefSpec("refs/heads/*:refs/heads/foo"));
  383. }
  384. @Test(expected = IllegalArgumentException.class)
  385. public void invalidWhenOnlyDestinationWildcard() {
  386. assertNotNull(new RefSpec("refs/heads/foo:refs/heads/*"));
  387. }
  388. @Test(expected = IllegalArgumentException.class)
  389. public void invalidWhenMoreThanOneWildcardInSource() {
  390. assertNotNull(new RefSpec("refs/heads/*/*:refs/heads/*"));
  391. }
  392. @Test(expected = IllegalArgumentException.class)
  393. public void invalidWhenMoreThanOneWildcardInDestination() {
  394. assertNotNull(new RefSpec("refs/heads/*:refs/heads/*/*"));
  395. }
  396. @Test(expected = IllegalArgumentException.class)
  397. public void invalidSourceDoubleSlashes() {
  398. assertNotNull(new RefSpec("refs/heads//wrong"));
  399. }
  400. @Test(expected = IllegalArgumentException.class)
  401. public void invalidSlashAtStart() {
  402. assertNotNull(new RefSpec("/foo:/foo"));
  403. }
  404. @Test(expected = IllegalArgumentException.class)
  405. public void invalidDestinationDoubleSlashes() {
  406. assertNotNull(new RefSpec(":refs/heads//wrong"));
  407. }
  408. @Test(expected = IllegalArgumentException.class)
  409. public void invalidSetSource() {
  410. RefSpec a = new RefSpec("refs/heads/*:refs/remotes/origin/*");
  411. a.setSource("refs/heads/*/*");
  412. }
  413. @Test(expected = IllegalArgumentException.class)
  414. public void invalidSetDestination() {
  415. RefSpec a = new RefSpec("refs/heads/*:refs/remotes/origin/*");
  416. a.setDestination("refs/remotes/origin/*/*");
  417. }
  418. @Test
  419. public void sourceOnlywithWildcard() {
  420. RefSpec a = new RefSpec("refs/heads/*",
  421. WildcardMode.ALLOW_MISMATCH);
  422. assertTrue(a.matchSource("refs/heads/master"));
  423. assertNull(a.getDestination());
  424. }
  425. @Test
  426. public void destinationWithWildcard() {
  427. RefSpec a = new RefSpec("refs/heads/master:refs/heads/*",
  428. WildcardMode.ALLOW_MISMATCH);
  429. assertTrue(a.matchSource("refs/heads/master"));
  430. assertTrue(a.matchDestination("refs/heads/master"));
  431. assertTrue(a.matchDestination("refs/heads/foo"));
  432. }
  433. @Test
  434. public void onlyWildCard() {
  435. RefSpec a = new RefSpec("*", WildcardMode.ALLOW_MISMATCH);
  436. assertTrue(a.matchSource("refs/heads/master"));
  437. assertNull(a.getDestination());
  438. }
  439. }