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.

RemoteConfigTest.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.transport;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertNotNull;
  48. import static org.junit.Assert.assertSame;
  49. import static org.junit.Assert.assertTrue;
  50. import java.util.Arrays;
  51. import java.util.List;
  52. import org.eclipse.jgit.errors.ConfigInvalidException;
  53. import org.eclipse.jgit.lib.Config;
  54. import org.junit.Before;
  55. import org.junit.Test;
  56. public class RemoteConfigTest {
  57. private Config config;
  58. @Before
  59. public void setUp() throws Exception {
  60. config = new Config();
  61. }
  62. private void readConfig(final String dat) throws ConfigInvalidException {
  63. config = new Config();
  64. config.fromText(dat);
  65. }
  66. private void checkConfig(final String exp) {
  67. assertEquals(exp, config.toText());
  68. }
  69. @Test
  70. public void testSimple() throws Exception {
  71. readConfig("[remote \"spearce\"]\n"
  72. + "url = http://www.spearce.org/egit.git\n"
  73. + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
  74. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  75. final List<URIish> allURIs = rc.getURIs();
  76. RefSpec spec;
  77. assertEquals("spearce", rc.getName());
  78. assertNotNull(allURIs);
  79. assertNotNull(rc.getFetchRefSpecs());
  80. assertNotNull(rc.getPushRefSpecs());
  81. assertNotNull(rc.getTagOpt());
  82. assertEquals(0, rc.getTimeout());
  83. assertSame(TagOpt.AUTO_FOLLOW, rc.getTagOpt());
  84. assertEquals(1, allURIs.size());
  85. assertEquals("http://www.spearce.org/egit.git", allURIs.get(0)
  86. .toString());
  87. assertEquals(1, rc.getFetchRefSpecs().size());
  88. spec = rc.getFetchRefSpecs().get(0);
  89. assertTrue(spec.isForceUpdate());
  90. assertTrue(spec.isWildcard());
  91. assertEquals("refs/heads/*", spec.getSource());
  92. assertEquals("refs/remotes/spearce/*", spec.getDestination());
  93. assertEquals(0, rc.getPushRefSpecs().size());
  94. }
  95. @Test
  96. public void testSimpleNoTags() throws Exception {
  97. readConfig("[remote \"spearce\"]\n"
  98. + "url = http://www.spearce.org/egit.git\n"
  99. + "fetch = +refs/heads/*:refs/remotes/spearce/*\n"
  100. + "tagopt = --no-tags\n");
  101. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  102. assertSame(TagOpt.NO_TAGS, rc.getTagOpt());
  103. }
  104. @Test
  105. public void testSimpleAlwaysTags() throws Exception {
  106. readConfig("[remote \"spearce\"]\n"
  107. + "url = http://www.spearce.org/egit.git\n"
  108. + "fetch = +refs/heads/*:refs/remotes/spearce/*\n"
  109. + "tagopt = --tags\n");
  110. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  111. assertSame(TagOpt.FETCH_TAGS, rc.getTagOpt());
  112. }
  113. @Test
  114. public void testMirror() throws Exception {
  115. readConfig("[remote \"spearce\"]\n"
  116. + "url = http://www.spearce.org/egit.git\n"
  117. + "fetch = +refs/heads/*:refs/heads/*\n"
  118. + "fetch = refs/tags/*:refs/tags/*\n");
  119. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  120. final List<URIish> allURIs = rc.getURIs();
  121. RefSpec spec;
  122. assertEquals("spearce", rc.getName());
  123. assertNotNull(allURIs);
  124. assertNotNull(rc.getFetchRefSpecs());
  125. assertNotNull(rc.getPushRefSpecs());
  126. assertEquals(1, allURIs.size());
  127. assertEquals("http://www.spearce.org/egit.git", allURIs.get(0)
  128. .toString());
  129. assertEquals(2, rc.getFetchRefSpecs().size());
  130. spec = rc.getFetchRefSpecs().get(0);
  131. assertTrue(spec.isForceUpdate());
  132. assertTrue(spec.isWildcard());
  133. assertEquals("refs/heads/*", spec.getSource());
  134. assertEquals("refs/heads/*", spec.getDestination());
  135. spec = rc.getFetchRefSpecs().get(1);
  136. assertFalse(spec.isForceUpdate());
  137. assertTrue(spec.isWildcard());
  138. assertEquals("refs/tags/*", spec.getSource());
  139. assertEquals("refs/tags/*", spec.getDestination());
  140. assertEquals(0, rc.getPushRefSpecs().size());
  141. }
  142. @Test
  143. public void testBackup() throws Exception {
  144. readConfig("[remote \"backup\"]\n"
  145. + "url = http://www.spearce.org/egit.git\n"
  146. + "url = user@repo.or.cz:/srv/git/egit.git\n"
  147. + "push = +refs/heads/*:refs/heads/*\n"
  148. + "push = refs/tags/*:refs/tags/*\n");
  149. final RemoteConfig rc = new RemoteConfig(config, "backup");
  150. final List<URIish> allURIs = rc.getURIs();
  151. RefSpec spec;
  152. assertEquals("backup", rc.getName());
  153. assertNotNull(allURIs);
  154. assertNotNull(rc.getFetchRefSpecs());
  155. assertNotNull(rc.getPushRefSpecs());
  156. assertEquals(2, allURIs.size());
  157. assertEquals("http://www.spearce.org/egit.git", allURIs.get(0)
  158. .toString());
  159. assertEquals("user@repo.or.cz:/srv/git/egit.git", allURIs.get(1)
  160. .toString());
  161. assertEquals(0, rc.getFetchRefSpecs().size());
  162. assertEquals(2, rc.getPushRefSpecs().size());
  163. spec = rc.getPushRefSpecs().get(0);
  164. assertTrue(spec.isForceUpdate());
  165. assertTrue(spec.isWildcard());
  166. assertEquals("refs/heads/*", spec.getSource());
  167. assertEquals("refs/heads/*", spec.getDestination());
  168. spec = rc.getPushRefSpecs().get(1);
  169. assertFalse(spec.isForceUpdate());
  170. assertTrue(spec.isWildcard());
  171. assertEquals("refs/tags/*", spec.getSource());
  172. assertEquals("refs/tags/*", spec.getDestination());
  173. }
  174. @Test
  175. public void testUploadPack() throws Exception {
  176. readConfig("[remote \"example\"]\n"
  177. + "url = user@example.com:egit.git\n"
  178. + "fetch = +refs/heads/*:refs/remotes/example/*\n"
  179. + "uploadpack = /path/to/git/git-upload-pack\n"
  180. + "receivepack = /path/to/git/git-receive-pack\n");
  181. final RemoteConfig rc = new RemoteConfig(config, "example");
  182. final List<URIish> allURIs = rc.getURIs();
  183. RefSpec spec;
  184. assertEquals("example", rc.getName());
  185. assertNotNull(allURIs);
  186. assertNotNull(rc.getFetchRefSpecs());
  187. assertNotNull(rc.getPushRefSpecs());
  188. assertEquals(1, allURIs.size());
  189. assertEquals("user@example.com:egit.git", allURIs.get(0).toString());
  190. assertEquals(1, rc.getFetchRefSpecs().size());
  191. spec = rc.getFetchRefSpecs().get(0);
  192. assertTrue(spec.isForceUpdate());
  193. assertTrue(spec.isWildcard());
  194. assertEquals("refs/heads/*", spec.getSource());
  195. assertEquals("refs/remotes/example/*", spec.getDestination());
  196. assertEquals(0, rc.getPushRefSpecs().size());
  197. assertEquals("/path/to/git/git-upload-pack", rc.getUploadPack());
  198. assertEquals("/path/to/git/git-receive-pack", rc.getReceivePack());
  199. }
  200. @Test
  201. public void testUnknown() throws Exception {
  202. readConfig("");
  203. final RemoteConfig rc = new RemoteConfig(config, "backup");
  204. assertEquals(0, rc.getURIs().size());
  205. assertEquals(0, rc.getFetchRefSpecs().size());
  206. assertEquals(0, rc.getPushRefSpecs().size());
  207. assertEquals("git-upload-pack", rc.getUploadPack());
  208. assertEquals("git-receive-pack", rc.getReceivePack());
  209. }
  210. @Test
  211. public void testAddURI() throws Exception {
  212. readConfig("");
  213. final URIish uri = new URIish("/some/dir");
  214. final RemoteConfig rc = new RemoteConfig(config, "backup");
  215. assertEquals(0, rc.getURIs().size());
  216. assertTrue(rc.addURI(uri));
  217. assertEquals(1, rc.getURIs().size());
  218. assertSame(uri, rc.getURIs().get(0));
  219. assertFalse(rc.addURI(new URIish(uri.toString())));
  220. assertEquals(1, rc.getURIs().size());
  221. }
  222. @Test
  223. public void testRemoveFirstURI() throws Exception {
  224. readConfig("");
  225. final URIish a = new URIish("/some/dir");
  226. final URIish b = new URIish("/another/dir");
  227. final URIish c = new URIish("/more/dirs");
  228. final RemoteConfig rc = new RemoteConfig(config, "backup");
  229. assertTrue(rc.addURI(a));
  230. assertTrue(rc.addURI(b));
  231. assertTrue(rc.addURI(c));
  232. assertEquals(3, rc.getURIs().size());
  233. assertSame(a, rc.getURIs().get(0));
  234. assertSame(b, rc.getURIs().get(1));
  235. assertSame(c, rc.getURIs().get(2));
  236. assertTrue(rc.removeURI(a));
  237. assertEquals(2, rc.getURIs().size());
  238. assertSame(b, rc.getURIs().get(0));
  239. assertSame(c, rc.getURIs().get(1));
  240. }
  241. @Test
  242. public void testRemoveMiddleURI() throws Exception {
  243. readConfig("");
  244. final URIish a = new URIish("/some/dir");
  245. final URIish b = new URIish("/another/dir");
  246. final URIish c = new URIish("/more/dirs");
  247. final RemoteConfig rc = new RemoteConfig(config, "backup");
  248. assertTrue(rc.addURI(a));
  249. assertTrue(rc.addURI(b));
  250. assertTrue(rc.addURI(c));
  251. assertEquals(3, rc.getURIs().size());
  252. assertSame(a, rc.getURIs().get(0));
  253. assertSame(b, rc.getURIs().get(1));
  254. assertSame(c, rc.getURIs().get(2));
  255. assertTrue(rc.removeURI(b));
  256. assertEquals(2, rc.getURIs().size());
  257. assertSame(a, rc.getURIs().get(0));
  258. assertSame(c, rc.getURIs().get(1));
  259. }
  260. @Test
  261. public void testRemoveLastURI() throws Exception {
  262. readConfig("");
  263. final URIish a = new URIish("/some/dir");
  264. final URIish b = new URIish("/another/dir");
  265. final URIish c = new URIish("/more/dirs");
  266. final RemoteConfig rc = new RemoteConfig(config, "backup");
  267. assertTrue(rc.addURI(a));
  268. assertTrue(rc.addURI(b));
  269. assertTrue(rc.addURI(c));
  270. assertEquals(3, rc.getURIs().size());
  271. assertSame(a, rc.getURIs().get(0));
  272. assertSame(b, rc.getURIs().get(1));
  273. assertSame(c, rc.getURIs().get(2));
  274. assertTrue(rc.removeURI(c));
  275. assertEquals(2, rc.getURIs().size());
  276. assertSame(a, rc.getURIs().get(0));
  277. assertSame(b, rc.getURIs().get(1));
  278. }
  279. @Test
  280. public void testRemoveOnlyURI() throws Exception {
  281. readConfig("");
  282. final URIish a = new URIish("/some/dir");
  283. final RemoteConfig rc = new RemoteConfig(config, "backup");
  284. assertTrue(rc.addURI(a));
  285. assertEquals(1, rc.getURIs().size());
  286. assertSame(a, rc.getURIs().get(0));
  287. assertTrue(rc.removeURI(a));
  288. assertEquals(0, rc.getURIs().size());
  289. }
  290. @Test
  291. public void testCreateOrigin() throws Exception {
  292. final RemoteConfig rc = new RemoteConfig(config, "origin");
  293. rc.addURI(new URIish("/some/dir"));
  294. rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
  295. + rc.getName() + "/*"));
  296. rc.update(config);
  297. checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
  298. + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n");
  299. }
  300. @Test
  301. public void testSaveAddURI() throws Exception {
  302. readConfig("[remote \"spearce\"]\n"
  303. + "url = http://www.spearce.org/egit.git\n"
  304. + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
  305. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  306. rc.addURI(new URIish("/some/dir"));
  307. assertEquals(2, rc.getURIs().size());
  308. rc.update(config);
  309. checkConfig("[remote \"spearce\"]\n"
  310. + "\turl = http://www.spearce.org/egit.git\n"
  311. + "\turl = /some/dir\n"
  312. + "\tfetch = +refs/heads/*:refs/remotes/spearce/*\n");
  313. }
  314. @Test
  315. public void testSaveRemoveLastURI() throws Exception {
  316. readConfig("[remote \"spearce\"]\n"
  317. + "url = http://www.spearce.org/egit.git\n"
  318. + "url = /some/dir\n"
  319. + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
  320. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  321. assertEquals(2, rc.getURIs().size());
  322. rc.removeURI(new URIish("/some/dir"));
  323. assertEquals(1, rc.getURIs().size());
  324. rc.update(config);
  325. checkConfig("[remote \"spearce\"]\n"
  326. + "\turl = http://www.spearce.org/egit.git\n"
  327. + "\tfetch = +refs/heads/*:refs/remotes/spearce/*\n");
  328. }
  329. @Test
  330. public void testSaveRemoveFirstURI() throws Exception {
  331. readConfig("[remote \"spearce\"]\n"
  332. + "url = http://www.spearce.org/egit.git\n"
  333. + "url = /some/dir\n"
  334. + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
  335. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  336. assertEquals(2, rc.getURIs().size());
  337. rc.removeURI(new URIish("http://www.spearce.org/egit.git"));
  338. assertEquals(1, rc.getURIs().size());
  339. rc.update(config);
  340. checkConfig("[remote \"spearce\"]\n" + "\turl = /some/dir\n"
  341. + "\tfetch = +refs/heads/*:refs/remotes/spearce/*\n");
  342. }
  343. @Test
  344. public void testSaveNoTags() throws Exception {
  345. final RemoteConfig rc = new RemoteConfig(config, "origin");
  346. rc.addURI(new URIish("/some/dir"));
  347. rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
  348. + rc.getName() + "/*"));
  349. rc.setTagOpt(TagOpt.NO_TAGS);
  350. rc.update(config);
  351. checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
  352. + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
  353. + "\ttagopt = --no-tags\n");
  354. }
  355. @Test
  356. public void testSaveAllTags() throws Exception {
  357. final RemoteConfig rc = new RemoteConfig(config, "origin");
  358. rc.addURI(new URIish("/some/dir"));
  359. rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
  360. + rc.getName() + "/*"));
  361. rc.setTagOpt(TagOpt.FETCH_TAGS);
  362. rc.update(config);
  363. checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
  364. + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
  365. + "\ttagopt = --tags\n");
  366. }
  367. @Test
  368. public void testSimpleTimeout() throws Exception {
  369. readConfig("[remote \"spearce\"]\n"
  370. + "url = http://www.spearce.org/egit.git\n"
  371. + "fetch = +refs/heads/*:refs/remotes/spearce/*\n"
  372. + "timeout = 12\n");
  373. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  374. assertEquals(12, rc.getTimeout());
  375. }
  376. @Test
  377. public void testSaveTimeout() throws Exception {
  378. final RemoteConfig rc = new RemoteConfig(config, "origin");
  379. rc.addURI(new URIish("/some/dir"));
  380. rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
  381. + rc.getName() + "/*"));
  382. rc.setTimeout(60);
  383. rc.update(config);
  384. checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
  385. + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
  386. + "\ttimeout = 60\n");
  387. }
  388. @Test
  389. public void noInsteadOf() throws Exception {
  390. config.setString("remote", "origin", "url", "short:project.git");
  391. config.setString("url", "https://server/repos/", "name", "short:");
  392. RemoteConfig rc = new RemoteConfig(config, "origin");
  393. assertFalse(rc.getURIs().isEmpty());
  394. assertEquals("short:project.git", rc.getURIs().get(0).toASCIIString());
  395. }
  396. @Test
  397. public void singleInsteadOf() throws Exception {
  398. config.setString("remote", "origin", "url", "short:project.git");
  399. config.setString("url", "https://server/repos/", "insteadOf", "short:");
  400. RemoteConfig rc = new RemoteConfig(config, "origin");
  401. assertFalse(rc.getURIs().isEmpty());
  402. assertEquals("https://server/repos/project.git", rc.getURIs().get(0)
  403. .toASCIIString());
  404. }
  405. @Test
  406. public void multipleInsteadOf() throws Exception {
  407. config.setString("remote", "origin", "url", "prefixproject.git");
  408. config.setStringList("url", "https://server/repos/", "insteadOf",
  409. Arrays.asList("pre", "prefix", "pref", "perf"));
  410. RemoteConfig rc = new RemoteConfig(config, "origin");
  411. assertFalse(rc.getURIs().isEmpty());
  412. assertEquals("https://server/repos/project.git", rc.getURIs().get(0)
  413. .toASCIIString());
  414. }
  415. @Test
  416. public void noPushInsteadOf() throws Exception {
  417. config.setString("remote", "origin", "pushurl", "short:project.git");
  418. config.setString("url", "https://server/repos/", "name", "short:");
  419. RemoteConfig rc = new RemoteConfig(config, "origin");
  420. assertFalse(rc.getPushURIs().isEmpty());
  421. assertEquals("short:project.git", rc.getPushURIs().get(0)
  422. .toASCIIString());
  423. }
  424. @Test
  425. public void singlePushInsteadOf() throws Exception {
  426. config.setString("remote", "origin", "pushurl", "short:project.git");
  427. config.setString("url", "https://server/repos/", "pushInsteadOf",
  428. "short:");
  429. RemoteConfig rc = new RemoteConfig(config, "origin");
  430. assertFalse(rc.getPushURIs().isEmpty());
  431. assertEquals("https://server/repos/project.git", rc.getPushURIs()
  432. .get(0).toASCIIString());
  433. }
  434. @Test
  435. public void multiplePushInsteadOf() throws Exception {
  436. config.setString("remote", "origin", "pushurl", "prefixproject.git");
  437. config.setStringList("url", "https://server/repos/", "pushInsteadOf",
  438. Arrays.asList("pre", "prefix", "pref", "perf"));
  439. RemoteConfig rc = new RemoteConfig(config, "origin");
  440. assertFalse(rc.getPushURIs().isEmpty());
  441. assertEquals("https://server/repos/project.git", rc.getPushURIs()
  442. .get(0).toASCIIString());
  443. }
  444. }