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 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  4. *
  5. * This program and the accompanying materials are made available under the
  6. * terms of the Eclipse Distribution License v. 1.0 which is available at
  7. * https://www.eclipse.org/org/documents/edl-v10.php.
  8. *
  9. * SPDX-License-Identifier: BSD-3-Clause
  10. */
  11. package org.eclipse.jgit.transport;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertFalse;
  14. import static org.junit.Assert.assertNotNull;
  15. import static org.junit.Assert.assertSame;
  16. import static org.junit.Assert.assertTrue;
  17. import java.util.Arrays;
  18. import java.util.Collections;
  19. import java.util.List;
  20. import org.eclipse.jgit.errors.ConfigInvalidException;
  21. import org.eclipse.jgit.lib.Config;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. public class RemoteConfigTest {
  25. private Config config;
  26. @Before
  27. public void setUp() throws Exception {
  28. config = new Config();
  29. }
  30. private void readConfig(String dat) throws ConfigInvalidException {
  31. config = new Config();
  32. config.fromText(dat);
  33. }
  34. private void checkConfig(String exp) {
  35. assertEquals(exp, config.toText());
  36. }
  37. @Test
  38. public void testSimple() throws Exception {
  39. readConfig("[remote \"spearce\"]\n"
  40. + "url = http://www.spearce.org/egit.git\n"
  41. + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
  42. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  43. final List<URIish> allURIs = rc.getURIs();
  44. RefSpec spec;
  45. assertEquals("spearce", rc.getName());
  46. assertNotNull(allURIs);
  47. assertNotNull(rc.getFetchRefSpecs());
  48. assertNotNull(rc.getPushRefSpecs());
  49. assertNotNull(rc.getTagOpt());
  50. assertEquals(0, rc.getTimeout());
  51. assertSame(TagOpt.AUTO_FOLLOW, rc.getTagOpt());
  52. assertEquals(1, allURIs.size());
  53. assertEquals("http://www.spearce.org/egit.git", allURIs.get(0)
  54. .toString());
  55. assertEquals(1, rc.getFetchRefSpecs().size());
  56. spec = rc.getFetchRefSpecs().get(0);
  57. assertTrue(spec.isForceUpdate());
  58. assertTrue(spec.isWildcard());
  59. assertEquals("refs/heads/*", spec.getSource());
  60. assertEquals("refs/remotes/spearce/*", spec.getDestination());
  61. assertEquals(0, rc.getPushRefSpecs().size());
  62. }
  63. @Test
  64. public void testSimpleNoTags() throws Exception {
  65. readConfig("[remote \"spearce\"]\n"
  66. + "url = http://www.spearce.org/egit.git\n"
  67. + "fetch = +refs/heads/*:refs/remotes/spearce/*\n"
  68. + "tagopt = --no-tags\n");
  69. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  70. assertSame(TagOpt.NO_TAGS, rc.getTagOpt());
  71. }
  72. @Test
  73. public void testSimpleAlwaysTags() throws Exception {
  74. readConfig("[remote \"spearce\"]\n"
  75. + "url = http://www.spearce.org/egit.git\n"
  76. + "fetch = +refs/heads/*:refs/remotes/spearce/*\n"
  77. + "tagopt = --tags\n");
  78. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  79. assertSame(TagOpt.FETCH_TAGS, rc.getTagOpt());
  80. }
  81. @Test
  82. public void testMirror() throws Exception {
  83. readConfig("[remote \"spearce\"]\n"
  84. + "url = http://www.spearce.org/egit.git\n"
  85. + "fetch = +refs/heads/*:refs/heads/*\n"
  86. + "fetch = refs/tags/*:refs/tags/*\n");
  87. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  88. final List<URIish> allURIs = rc.getURIs();
  89. RefSpec spec;
  90. assertEquals("spearce", rc.getName());
  91. assertNotNull(allURIs);
  92. assertNotNull(rc.getFetchRefSpecs());
  93. assertNotNull(rc.getPushRefSpecs());
  94. assertEquals(1, allURIs.size());
  95. assertEquals("http://www.spearce.org/egit.git", allURIs.get(0)
  96. .toString());
  97. assertEquals(2, rc.getFetchRefSpecs().size());
  98. spec = rc.getFetchRefSpecs().get(0);
  99. assertTrue(spec.isForceUpdate());
  100. assertTrue(spec.isWildcard());
  101. assertEquals("refs/heads/*", spec.getSource());
  102. assertEquals("refs/heads/*", spec.getDestination());
  103. spec = rc.getFetchRefSpecs().get(1);
  104. assertFalse(spec.isForceUpdate());
  105. assertTrue(spec.isWildcard());
  106. assertEquals("refs/tags/*", spec.getSource());
  107. assertEquals("refs/tags/*", spec.getDestination());
  108. assertEquals(0, rc.getPushRefSpecs().size());
  109. }
  110. @Test
  111. public void testBackup() throws Exception {
  112. readConfig("[remote \"backup\"]\n"
  113. + "url = http://www.spearce.org/egit.git\n"
  114. + "url = user@repo.or.cz:/srv/git/egit.git\n"
  115. + "push = +refs/heads/*:refs/heads/*\n"
  116. + "push = refs/tags/*:refs/tags/*\n");
  117. final RemoteConfig rc = new RemoteConfig(config, "backup");
  118. final List<URIish> allURIs = rc.getURIs();
  119. RefSpec spec;
  120. assertEquals("backup", rc.getName());
  121. assertNotNull(allURIs);
  122. assertNotNull(rc.getFetchRefSpecs());
  123. assertNotNull(rc.getPushRefSpecs());
  124. assertEquals(2, allURIs.size());
  125. assertEquals("http://www.spearce.org/egit.git", allURIs.get(0)
  126. .toString());
  127. assertEquals("user@repo.or.cz:/srv/git/egit.git", allURIs.get(1)
  128. .toString());
  129. assertEquals(0, rc.getFetchRefSpecs().size());
  130. assertEquals(2, rc.getPushRefSpecs().size());
  131. spec = rc.getPushRefSpecs().get(0);
  132. assertTrue(spec.isForceUpdate());
  133. assertTrue(spec.isWildcard());
  134. assertEquals("refs/heads/*", spec.getSource());
  135. assertEquals("refs/heads/*", spec.getDestination());
  136. spec = rc.getPushRefSpecs().get(1);
  137. assertFalse(spec.isForceUpdate());
  138. assertTrue(spec.isWildcard());
  139. assertEquals("refs/tags/*", spec.getSource());
  140. assertEquals("refs/tags/*", spec.getDestination());
  141. }
  142. @Test
  143. public void testUploadPack() throws Exception {
  144. readConfig("[remote \"example\"]\n"
  145. + "url = user@example.com:egit.git\n"
  146. + "fetch = +refs/heads/*:refs/remotes/example/*\n"
  147. + "uploadpack = /path/to/git/git-upload-pack\n"
  148. + "receivepack = /path/to/git/git-receive-pack\n");
  149. final RemoteConfig rc = new RemoteConfig(config, "example");
  150. final List<URIish> allURIs = rc.getURIs();
  151. RefSpec spec;
  152. assertEquals("example", rc.getName());
  153. assertNotNull(allURIs);
  154. assertNotNull(rc.getFetchRefSpecs());
  155. assertNotNull(rc.getPushRefSpecs());
  156. assertEquals(1, allURIs.size());
  157. assertEquals("user@example.com:egit.git", allURIs.get(0).toString());
  158. assertEquals(1, rc.getFetchRefSpecs().size());
  159. spec = rc.getFetchRefSpecs().get(0);
  160. assertTrue(spec.isForceUpdate());
  161. assertTrue(spec.isWildcard());
  162. assertEquals("refs/heads/*", spec.getSource());
  163. assertEquals("refs/remotes/example/*", spec.getDestination());
  164. assertEquals(0, rc.getPushRefSpecs().size());
  165. assertEquals("/path/to/git/git-upload-pack", rc.getUploadPack());
  166. assertEquals("/path/to/git/git-receive-pack", rc.getReceivePack());
  167. }
  168. @Test
  169. public void testUnknown() throws Exception {
  170. readConfig("");
  171. final RemoteConfig rc = new RemoteConfig(config, "backup");
  172. assertEquals(0, rc.getURIs().size());
  173. assertEquals(0, rc.getFetchRefSpecs().size());
  174. assertEquals(0, rc.getPushRefSpecs().size());
  175. assertEquals("git-upload-pack", rc.getUploadPack());
  176. assertEquals("git-receive-pack", rc.getReceivePack());
  177. }
  178. @Test
  179. public void testAddURI() throws Exception {
  180. readConfig("");
  181. final URIish uri = new URIish("/some/dir");
  182. final RemoteConfig rc = new RemoteConfig(config, "backup");
  183. assertEquals(0, rc.getURIs().size());
  184. assertTrue(rc.addURI(uri));
  185. assertEquals(1, rc.getURIs().size());
  186. assertSame(uri, rc.getURIs().get(0));
  187. assertFalse(rc.addURI(new URIish(uri.toString())));
  188. assertEquals(1, rc.getURIs().size());
  189. }
  190. @Test
  191. public void testRemoveFirstURI() throws Exception {
  192. readConfig("");
  193. final URIish a = new URIish("/some/dir");
  194. final URIish b = new URIish("/another/dir");
  195. final URIish c = new URIish("/more/dirs");
  196. final RemoteConfig rc = new RemoteConfig(config, "backup");
  197. assertTrue(rc.addURI(a));
  198. assertTrue(rc.addURI(b));
  199. assertTrue(rc.addURI(c));
  200. assertEquals(3, rc.getURIs().size());
  201. assertSame(a, rc.getURIs().get(0));
  202. assertSame(b, rc.getURIs().get(1));
  203. assertSame(c, rc.getURIs().get(2));
  204. assertTrue(rc.removeURI(a));
  205. assertEquals(2, rc.getURIs().size());
  206. assertSame(b, rc.getURIs().get(0));
  207. assertSame(c, rc.getURIs().get(1));
  208. }
  209. @Test
  210. public void testRemoveMiddleURI() throws Exception {
  211. readConfig("");
  212. final URIish a = new URIish("/some/dir");
  213. final URIish b = new URIish("/another/dir");
  214. final URIish c = new URIish("/more/dirs");
  215. final RemoteConfig rc = new RemoteConfig(config, "backup");
  216. assertTrue(rc.addURI(a));
  217. assertTrue(rc.addURI(b));
  218. assertTrue(rc.addURI(c));
  219. assertEquals(3, rc.getURIs().size());
  220. assertSame(a, rc.getURIs().get(0));
  221. assertSame(b, rc.getURIs().get(1));
  222. assertSame(c, rc.getURIs().get(2));
  223. assertTrue(rc.removeURI(b));
  224. assertEquals(2, rc.getURIs().size());
  225. assertSame(a, rc.getURIs().get(0));
  226. assertSame(c, rc.getURIs().get(1));
  227. }
  228. @Test
  229. public void testRemoveLastURI() throws Exception {
  230. readConfig("");
  231. final URIish a = new URIish("/some/dir");
  232. final URIish b = new URIish("/another/dir");
  233. final URIish c = new URIish("/more/dirs");
  234. final RemoteConfig rc = new RemoteConfig(config, "backup");
  235. assertTrue(rc.addURI(a));
  236. assertTrue(rc.addURI(b));
  237. assertTrue(rc.addURI(c));
  238. assertEquals(3, rc.getURIs().size());
  239. assertSame(a, rc.getURIs().get(0));
  240. assertSame(b, rc.getURIs().get(1));
  241. assertSame(c, rc.getURIs().get(2));
  242. assertTrue(rc.removeURI(c));
  243. assertEquals(2, rc.getURIs().size());
  244. assertSame(a, rc.getURIs().get(0));
  245. assertSame(b, rc.getURIs().get(1));
  246. }
  247. @Test
  248. public void testRemoveOnlyURI() throws Exception {
  249. readConfig("");
  250. final URIish a = new URIish("/some/dir");
  251. final RemoteConfig rc = new RemoteConfig(config, "backup");
  252. assertTrue(rc.addURI(a));
  253. assertEquals(1, rc.getURIs().size());
  254. assertSame(a, rc.getURIs().get(0));
  255. assertTrue(rc.removeURI(a));
  256. assertEquals(0, rc.getURIs().size());
  257. }
  258. @Test
  259. public void testCreateOrigin() throws Exception {
  260. final RemoteConfig rc = new RemoteConfig(config, "origin");
  261. rc.addURI(new URIish("/some/dir"));
  262. rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
  263. + rc.getName() + "/*"));
  264. rc.update(config);
  265. checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
  266. + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n");
  267. }
  268. @Test
  269. public void testSaveAddURI() throws Exception {
  270. readConfig("[remote \"spearce\"]\n"
  271. + "url = http://www.spearce.org/egit.git\n"
  272. + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
  273. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  274. rc.addURI(new URIish("/some/dir"));
  275. assertEquals(2, rc.getURIs().size());
  276. rc.update(config);
  277. checkConfig("[remote \"spearce\"]\n"
  278. + "\turl = http://www.spearce.org/egit.git\n"
  279. + "\turl = /some/dir\n"
  280. + "\tfetch = +refs/heads/*:refs/remotes/spearce/*\n");
  281. }
  282. @Test
  283. public void testSaveRemoveLastURI() throws Exception {
  284. readConfig("[remote \"spearce\"]\n"
  285. + "url = http://www.spearce.org/egit.git\n"
  286. + "url = /some/dir\n"
  287. + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
  288. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  289. assertEquals(2, rc.getURIs().size());
  290. rc.removeURI(new URIish("/some/dir"));
  291. assertEquals(1, rc.getURIs().size());
  292. rc.update(config);
  293. checkConfig("[remote \"spearce\"]\n"
  294. + "\turl = http://www.spearce.org/egit.git\n"
  295. + "\tfetch = +refs/heads/*:refs/remotes/spearce/*\n");
  296. }
  297. @Test
  298. public void testSaveRemoveFirstURI() throws Exception {
  299. readConfig("[remote \"spearce\"]\n"
  300. + "url = http://www.spearce.org/egit.git\n"
  301. + "url = /some/dir\n"
  302. + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");
  303. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  304. assertEquals(2, rc.getURIs().size());
  305. rc.removeURI(new URIish("http://www.spearce.org/egit.git"));
  306. assertEquals(1, rc.getURIs().size());
  307. rc.update(config);
  308. checkConfig("[remote \"spearce\"]\n" + "\turl = /some/dir\n"
  309. + "\tfetch = +refs/heads/*:refs/remotes/spearce/*\n");
  310. }
  311. @Test
  312. public void testSaveNoTags() throws Exception {
  313. final RemoteConfig rc = new RemoteConfig(config, "origin");
  314. rc.addURI(new URIish("/some/dir"));
  315. rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
  316. + rc.getName() + "/*"));
  317. rc.setTagOpt(TagOpt.NO_TAGS);
  318. rc.update(config);
  319. checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
  320. + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
  321. + "\ttagopt = --no-tags\n");
  322. }
  323. @Test
  324. public void testSaveAllTags() throws Exception {
  325. final RemoteConfig rc = new RemoteConfig(config, "origin");
  326. rc.addURI(new URIish("/some/dir"));
  327. rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
  328. + rc.getName() + "/*"));
  329. rc.setTagOpt(TagOpt.FETCH_TAGS);
  330. rc.update(config);
  331. checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
  332. + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
  333. + "\ttagopt = --tags\n");
  334. }
  335. @Test
  336. public void testSimpleTimeout() throws Exception {
  337. readConfig("[remote \"spearce\"]\n"
  338. + "url = http://www.spearce.org/egit.git\n"
  339. + "fetch = +refs/heads/*:refs/remotes/spearce/*\n"
  340. + "timeout = 12\n");
  341. final RemoteConfig rc = new RemoteConfig(config, "spearce");
  342. assertEquals(12, rc.getTimeout());
  343. }
  344. @Test
  345. public void testSaveTimeout() throws Exception {
  346. final RemoteConfig rc = new RemoteConfig(config, "origin");
  347. rc.addURI(new URIish("/some/dir"));
  348. rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
  349. + rc.getName() + "/*"));
  350. rc.setTimeout(60);
  351. rc.update(config);
  352. checkConfig("[remote \"origin\"]\n" + "\turl = /some/dir\n"
  353. + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
  354. + "\ttimeout = 60\n");
  355. }
  356. @Test
  357. public void noInsteadOf() throws Exception {
  358. config.setString("remote", "origin", "url", "short:project.git");
  359. config.setString("url", "https://server/repos/", "name", "short:");
  360. RemoteConfig rc = new RemoteConfig(config, "origin");
  361. assertFalse(rc.getURIs().isEmpty());
  362. assertEquals("short:project.git", rc.getURIs().get(0).toASCIIString());
  363. }
  364. @Test
  365. public void singleInsteadOf() throws Exception {
  366. config.setString("remote", "origin", "url", "short:project.git");
  367. config.setString("url", "https://server/repos/", "insteadOf", "short:");
  368. RemoteConfig rc = new RemoteConfig(config, "origin");
  369. assertFalse(rc.getURIs().isEmpty());
  370. assertEquals("https://server/repos/project.git", rc.getURIs().get(0)
  371. .toASCIIString());
  372. }
  373. @Test
  374. public void multipleInsteadOf() throws Exception {
  375. config.setString("remote", "origin", "url", "prefixproject.git");
  376. config.setStringList("url", "https://server/repos/", "insteadOf",
  377. Arrays.asList("pre", "prefix", "pref", "perf"));
  378. RemoteConfig rc = new RemoteConfig(config, "origin");
  379. assertFalse(rc.getURIs().isEmpty());
  380. assertEquals("https://server/repos/project.git", rc.getURIs().get(0)
  381. .toASCIIString());
  382. }
  383. @Test
  384. public void noPushInsteadOf() throws Exception {
  385. config.setString("remote", "origin", "pushurl", "short:project.git");
  386. config.setString("url", "https://server/repos/", "name", "short:");
  387. RemoteConfig rc = new RemoteConfig(config, "origin");
  388. assertFalse(rc.getPushURIs().isEmpty());
  389. assertEquals("short:project.git", rc.getPushURIs().get(0)
  390. .toASCIIString());
  391. }
  392. @Test
  393. public void pushInsteadOfNotAppliedToPushUri() throws Exception {
  394. config.setString("remote", "origin", "pushurl", "short:project.git");
  395. config.setString("url", "https://server/repos/", "pushInsteadOf",
  396. "short:");
  397. RemoteConfig rc = new RemoteConfig(config, "origin");
  398. assertFalse(rc.getPushURIs().isEmpty());
  399. assertEquals("short:project.git",
  400. rc.getPushURIs().get(0).toASCIIString());
  401. }
  402. @Test
  403. public void pushInsteadOfAppliedToUri() throws Exception {
  404. config.setString("remote", "origin", "url", "short:project.git");
  405. config.setString("url", "https://server/repos/", "pushInsteadOf",
  406. "short:");
  407. RemoteConfig rc = new RemoteConfig(config, "origin");
  408. assertFalse(rc.getPushURIs().isEmpty());
  409. assertEquals("https://server/repos/project.git",
  410. rc.getPushURIs().get(0).toASCIIString());
  411. }
  412. @Test
  413. public void multiplePushInsteadOf() throws Exception {
  414. config.setString("remote", "origin", "url", "prefixproject.git");
  415. config.setStringList("url", "https://server/repos/", "pushInsteadOf",
  416. Arrays.asList("pre", "prefix", "pref", "perf"));
  417. RemoteConfig rc = new RemoteConfig(config, "origin");
  418. assertFalse(rc.getPushURIs().isEmpty());
  419. assertEquals("https://server/repos/project.git", rc.getPushURIs()
  420. .get(0).toASCIIString());
  421. }
  422. @Test
  423. public void pushInsteadOfNoPushUrl() throws Exception {
  424. config.setString("remote", "origin", "url",
  425. "http://git.eclipse.org/gitroot/jgit/jgit");
  426. config.setStringList("url", "ssh://someone@git.eclipse.org:29418/",
  427. "pushInsteadOf",
  428. Collections.singletonList("http://git.eclipse.org/gitroot/"));
  429. RemoteConfig rc = new RemoteConfig(config, "origin");
  430. assertFalse(rc.getPushURIs().isEmpty());
  431. assertEquals("ssh://someone@git.eclipse.org:29418/jgit/jgit",
  432. rc.getPushURIs().get(0).toASCIIString());
  433. }
  434. }