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.

RepoCommandTest.java 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /*
  2. * Copyright (C) 2014, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.gitrepo;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertTrue;
  47. import java.io.BufferedReader;
  48. import java.io.File;
  49. import java.io.FileReader;
  50. import org.eclipse.jgit.api.Git;
  51. import org.eclipse.jgit.junit.JGitTestUtil;
  52. import org.eclipse.jgit.junit.RepositoryTestCase;
  53. import org.eclipse.jgit.lib.Constants;
  54. import org.eclipse.jgit.lib.ObjectId;
  55. import org.eclipse.jgit.lib.Repository;
  56. import org.junit.Test;
  57. public class RepoCommandTest extends RepositoryTestCase {
  58. private static final String BRANCH = "branch";
  59. private static final String TAG = "release";
  60. private Repository defaultDb;
  61. private Repository notDefaultDb;
  62. private Repository groupADb;
  63. private Repository groupBDb;
  64. private String rootUri;
  65. private String defaultUri;
  66. private String notDefaultUri;
  67. private String groupAUri;
  68. private String groupBUri;
  69. private ObjectId oldCommitId;
  70. public void setUp() throws Exception {
  71. super.setUp();
  72. defaultDb = createWorkRepository();
  73. Git git = new Git(defaultDb);
  74. JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "branch world");
  75. git.add().addFilepattern("hello.txt").call();
  76. oldCommitId = git.commit().setMessage("Initial commit").call().getId();
  77. git.checkout().setName(BRANCH).setCreateBranch(true).call();
  78. git.checkout().setName("master").call();
  79. git.tag().setName(TAG).call();
  80. JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "master world");
  81. git.add().addFilepattern("hello.txt").call();
  82. git.commit().setMessage("Second commit").call();
  83. notDefaultDb = createWorkRepository();
  84. git = new Git(notDefaultDb);
  85. JGitTestUtil.writeTrashFile(notDefaultDb, "world.txt", "hello");
  86. git.add().addFilepattern("world.txt").call();
  87. git.commit().setMessage("Initial commit").call();
  88. groupADb = createWorkRepository();
  89. git = new Git(groupADb);
  90. JGitTestUtil.writeTrashFile(groupADb, "a.txt", "world");
  91. git.add().addFilepattern("a.txt").call();
  92. git.commit().setMessage("Initial commit").call();
  93. groupBDb = createWorkRepository();
  94. git = new Git(groupBDb);
  95. JGitTestUtil.writeTrashFile(groupBDb, "b.txt", "world");
  96. git.add().addFilepattern("b.txt").call();
  97. git.commit().setMessage("Initial commit").call();
  98. resolveRelativeUris();
  99. }
  100. @Test
  101. public void testAddRepoManifest() throws Exception {
  102. StringBuilder xmlContent = new StringBuilder();
  103. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  104. .append("<manifest>")
  105. .append("<remote name=\"remote1\" fetch=\".\" />")
  106. .append("<default revision=\"master\" remote=\"remote1\" />")
  107. .append("<project path=\"foo\" name=\"")
  108. .append(defaultUri)
  109. .append("\" />")
  110. .append("</manifest>");
  111. writeTrashFile("manifest.xml", xmlContent.toString());
  112. RepoCommand command = new RepoCommand(db);
  113. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  114. .setURI(rootUri)
  115. .call();
  116. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  117. assertTrue("submodule should be checked out", hello.exists());
  118. BufferedReader reader = new BufferedReader(new FileReader(hello));
  119. String content = reader.readLine();
  120. reader.close();
  121. assertEquals("submodule content should be as expected",
  122. "master world", content);
  123. }
  124. @Test
  125. public void testRepoManifestGroups() throws Exception {
  126. StringBuilder xmlContent = new StringBuilder();
  127. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  128. .append("<manifest>")
  129. .append("<remote name=\"remote1\" fetch=\".\" />")
  130. .append("<default revision=\"master\" remote=\"remote1\" />")
  131. .append("<project path=\"foo\" name=\"")
  132. .append(defaultUri)
  133. .append("\" groups=\"a,test\" />")
  134. .append("<project path=\"bar\" name=\"")
  135. .append(notDefaultUri)
  136. .append("\" groups=\"notdefault\" />")
  137. .append("<project path=\"a\" name=\"")
  138. .append(groupAUri)
  139. .append("\" groups=\"a\" />")
  140. .append("<project path=\"b\" name=\"")
  141. .append(groupBUri)
  142. .append("\" groups=\"b\" />")
  143. .append("</manifest>");
  144. // default should have foo, a & b
  145. Repository localDb = createWorkRepository();
  146. JGitTestUtil.writeTrashFile(
  147. localDb, "manifest.xml", xmlContent.toString());
  148. RepoCommand command = new RepoCommand(localDb);
  149. command
  150. .setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  151. .setURI(rootUri)
  152. .call();
  153. File file = new File(localDb.getWorkTree(), "foo/hello.txt");
  154. assertTrue("default should have foo", file.exists());
  155. file = new File(localDb.getWorkTree(), "bar/world.txt");
  156. assertFalse("default shouldn't have bar", file.exists());
  157. file = new File(localDb.getWorkTree(), "a/a.txt");
  158. assertTrue("default should have a", file.exists());
  159. file = new File(localDb.getWorkTree(), "b/b.txt");
  160. assertTrue("default should have b", file.exists());
  161. // all,-a should have bar & b
  162. localDb = createWorkRepository();
  163. JGitTestUtil.writeTrashFile(
  164. localDb, "manifest.xml", xmlContent.toString());
  165. command = new RepoCommand(localDb);
  166. command
  167. .setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  168. .setURI(rootUri)
  169. .setGroups("all,-a")
  170. .call();
  171. file = new File(localDb.getWorkTree(), "foo/hello.txt");
  172. assertFalse("\"all,-a\" shouldn't have foo", file.exists());
  173. file = new File(localDb.getWorkTree(), "bar/world.txt");
  174. assertTrue("\"all,-a\" should have bar", file.exists());
  175. file = new File(localDb.getWorkTree(), "a/a.txt");
  176. assertFalse("\"all,-a\" shuoldn't have a", file.exists());
  177. file = new File(localDb.getWorkTree(), "b/b.txt");
  178. assertTrue("\"all,-a\" should have b", file.exists());
  179. }
  180. @Test
  181. public void testRepoManifestCopyFile() throws Exception {
  182. Repository localDb = createWorkRepository();
  183. StringBuilder xmlContent = new StringBuilder();
  184. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  185. .append("<manifest>")
  186. .append("<remote name=\"remote1\" fetch=\".\" />")
  187. .append("<default revision=\"master\" remote=\"remote1\" />")
  188. .append("<project path=\"foo\" name=\"")
  189. .append(defaultUri)
  190. .append("\">")
  191. .append("<copyfile src=\"hello.txt\" dest=\"Hello\" />")
  192. .append("</project>")
  193. .append("</manifest>");
  194. JGitTestUtil.writeTrashFile(
  195. localDb, "manifest.xml", xmlContent.toString());
  196. RepoCommand command = new RepoCommand(localDb);
  197. command
  198. .setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  199. .setURI(rootUri)
  200. .call();
  201. // The original file should exist
  202. File hello = new File(localDb.getWorkTree(), "foo/hello.txt");
  203. assertTrue("The original file should exist", hello.exists());
  204. BufferedReader reader = new BufferedReader(new FileReader(hello));
  205. String content = reader.readLine();
  206. reader.close();
  207. assertEquals("The original file should have expected content",
  208. "master world", content);
  209. // The dest file should also exist
  210. hello = new File(localDb.getWorkTree(), "Hello");
  211. assertTrue("The destination file should exist", hello.exists());
  212. reader = new BufferedReader(new FileReader(hello));
  213. content = reader.readLine();
  214. reader.close();
  215. assertEquals("The destination file should have expected content",
  216. "master world", content);
  217. }
  218. @Test
  219. public void testBareRepo() throws Exception {
  220. Repository remoteDb = createBareRepository();
  221. Repository tempDb = createWorkRepository();
  222. StringBuilder xmlContent = new StringBuilder();
  223. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  224. .append("<manifest>")
  225. .append("<remote name=\"remote1\" fetch=\".\" />")
  226. .append("<default revision=\"master\" remote=\"remote1\" />")
  227. .append("<project path=\"foo\" name=\"")
  228. .append(defaultUri)
  229. .append("\" />")
  230. .append("</manifest>");
  231. JGitTestUtil.writeTrashFile(
  232. tempDb, "manifest.xml", xmlContent.toString());
  233. RepoCommand command = new RepoCommand(remoteDb);
  234. command
  235. .setPath(tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  236. .setURI(rootUri)
  237. .call();
  238. // Clone it
  239. File directory = createTempDirectory("testBareRepo");
  240. Repository localDb = Git
  241. .cloneRepository()
  242. .setDirectory(directory)
  243. .setURI(remoteDb.getDirectory().toURI().toString())
  244. .call()
  245. .getRepository();
  246. // The .gitmodules file should exist
  247. File gitmodules = new File(localDb.getWorkTree(), ".gitmodules");
  248. assertTrue("The .gitmodules file should exist", gitmodules.exists());
  249. // The first line of .gitmodules file should be expected
  250. BufferedReader reader = new BufferedReader(new FileReader(gitmodules));
  251. String content = reader.readLine();
  252. reader.close();
  253. assertEquals(
  254. "The first line of .gitmodules file should be as expected",
  255. "[submodule \"foo\"]", content);
  256. // The gitlink should be the same as remote head sha1
  257. String gitlink = localDb.resolve(Constants.HEAD + ":foo").name();
  258. String remote = defaultDb.resolve(Constants.HEAD).name();
  259. assertEquals("The gitlink should be the same as remote head",
  260. remote, gitlink);
  261. }
  262. @Test
  263. public void testRevision() throws Exception {
  264. StringBuilder xmlContent = new StringBuilder();
  265. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  266. .append("<manifest>")
  267. .append("<remote name=\"remote1\" fetch=\".\" />")
  268. .append("<default revision=\"master\" remote=\"remote1\" />")
  269. .append("<project path=\"foo\" name=\"")
  270. .append(defaultUri)
  271. .append("\" revision=\"")
  272. .append(oldCommitId.name())
  273. .append("\" />")
  274. .append("</manifest>");
  275. writeTrashFile("manifest.xml", xmlContent.toString());
  276. RepoCommand command = new RepoCommand(db);
  277. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  278. .setURI(rootUri)
  279. .call();
  280. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  281. BufferedReader reader = new BufferedReader(new FileReader(hello));
  282. String content = reader.readLine();
  283. reader.close();
  284. assertEquals("submodule content should be as expected",
  285. "branch world", content);
  286. }
  287. @Test
  288. public void testRevisionBranch() throws Exception {
  289. StringBuilder xmlContent = new StringBuilder();
  290. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  291. .append("<manifest>")
  292. .append("<remote name=\"remote1\" fetch=\".\" />")
  293. .append("<default revision=\"")
  294. .append(BRANCH)
  295. .append("\" remote=\"remote1\" />")
  296. .append("<project path=\"foo\" name=\"")
  297. .append(defaultUri)
  298. .append("\" />")
  299. .append("</manifest>");
  300. writeTrashFile("manifest.xml", xmlContent.toString());
  301. RepoCommand command = new RepoCommand(db);
  302. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  303. .setURI(rootUri)
  304. .call();
  305. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  306. BufferedReader reader = new BufferedReader(new FileReader(hello));
  307. String content = reader.readLine();
  308. reader.close();
  309. assertEquals("submodule content should be as expected",
  310. "branch world", content);
  311. }
  312. @Test
  313. public void testRevisionTag() throws Exception {
  314. StringBuilder xmlContent = new StringBuilder();
  315. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  316. .append("<manifest>")
  317. .append("<remote name=\"remote1\" fetch=\".\" />")
  318. .append("<default revision=\"master\" remote=\"remote1\" />")
  319. .append("<project path=\"foo\" name=\"")
  320. .append(defaultUri)
  321. .append("\" revision=\"")
  322. .append(TAG)
  323. .append("\" />")
  324. .append("</manifest>");
  325. writeTrashFile("manifest.xml", xmlContent.toString());
  326. RepoCommand command = new RepoCommand(db);
  327. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  328. .setURI(rootUri)
  329. .call();
  330. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  331. BufferedReader reader = new BufferedReader(new FileReader(hello));
  332. String content = reader.readLine();
  333. reader.close();
  334. assertEquals("submodule content should be as expected",
  335. "branch world", content);
  336. }
  337. @Test
  338. public void testRevisionBare() throws Exception {
  339. Repository remoteDb = createBareRepository();
  340. Repository tempDb = createWorkRepository();
  341. StringBuilder xmlContent = new StringBuilder();
  342. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  343. .append("<manifest>")
  344. .append("<remote name=\"remote1\" fetch=\".\" />")
  345. .append("<default revision=\"")
  346. .append(BRANCH)
  347. .append("\" remote=\"remote1\" />")
  348. .append("<project path=\"foo\" name=\"")
  349. .append(defaultUri)
  350. .append("\" />")
  351. .append("</manifest>");
  352. JGitTestUtil.writeTrashFile(
  353. tempDb, "manifest.xml", xmlContent.toString());
  354. RepoCommand command = new RepoCommand(remoteDb);
  355. command
  356. .setPath(tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  357. .setURI(rootUri)
  358. .call();
  359. // Clone it
  360. File directory = createTempDirectory("testRevisionBare");
  361. Repository localDb = Git
  362. .cloneRepository()
  363. .setDirectory(directory)
  364. .setURI(remoteDb.getDirectory().toURI().toString())
  365. .call()
  366. .getRepository();
  367. // The gitlink should be the same as oldCommitId
  368. String gitlink = localDb.resolve(Constants.HEAD + ":foo").name();
  369. assertEquals("The gitlink is same as remote head",
  370. oldCommitId.name(), gitlink);
  371. }
  372. @Test
  373. public void testCopyFileBare() throws Exception {
  374. Repository remoteDb = createBareRepository();
  375. Repository tempDb = createWorkRepository();
  376. StringBuilder xmlContent = new StringBuilder();
  377. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  378. .append("<manifest>")
  379. .append("<remote name=\"remote1\" fetch=\".\" />")
  380. .append("<default revision=\"master\" remote=\"remote1\" />")
  381. .append("<project path=\"foo\" name=\"")
  382. .append(defaultUri)
  383. .append("\" revision=\"")
  384. .append(BRANCH)
  385. .append("\" >")
  386. .append("<copyfile src=\"hello.txt\" dest=\"Hello\" />")
  387. .append("</project>")
  388. .append("</manifest>");
  389. JGitTestUtil.writeTrashFile(
  390. tempDb, "manifest.xml", xmlContent.toString());
  391. RepoCommand command = new RepoCommand(remoteDb);
  392. command
  393. .setPath(tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  394. .setURI(rootUri)
  395. .call();
  396. // Clone it
  397. File directory = createTempDirectory("testCopyFileBare");
  398. Repository localDb = Git
  399. .cloneRepository()
  400. .setDirectory(directory)
  401. .setURI(remoteDb.getDirectory().toURI().toString())
  402. .call()
  403. .getRepository();
  404. // The Hello file should exist
  405. File hello = new File(localDb.getWorkTree(), "Hello");
  406. assertTrue("The Hello file should exist", hello.exists());
  407. // The content of Hello file should be expected
  408. BufferedReader reader = new BufferedReader(new FileReader(hello));
  409. String content = reader.readLine();
  410. reader.close();
  411. assertEquals("The Hello file should have expected content",
  412. "branch world", content);
  413. }
  414. @Test
  415. public void testReplaceManifestBare() throws Exception {
  416. Repository remoteDb = createBareRepository();
  417. Repository tempDb = createWorkRepository();
  418. StringBuilder xmlContent = new StringBuilder();
  419. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  420. .append("<manifest>")
  421. .append("<remote name=\"remote1\" fetch=\".\" />")
  422. .append("<default revision=\"master\" remote=\"remote1\" />")
  423. .append("<project path=\"foo\" name=\"")
  424. .append(defaultUri)
  425. .append("\" revision=\"")
  426. .append(BRANCH)
  427. .append("\" >")
  428. .append("<copyfile src=\"hello.txt\" dest=\"Hello\" />")
  429. .append("</project>")
  430. .append("</manifest>");
  431. JGitTestUtil.writeTrashFile(tempDb, "old.xml", xmlContent.toString());
  432. RepoCommand command = new RepoCommand(remoteDb);
  433. command.setPath(tempDb.getWorkTree().getAbsolutePath() + "/old.xml")
  434. .setURI(rootUri)
  435. .call();
  436. xmlContent = new StringBuilder();
  437. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  438. .append("<manifest>")
  439. .append("<remote name=\"remote1\" fetch=\".\" />")
  440. .append("<default revision=\"master\" remote=\"remote1\" />")
  441. .append("<project path=\"bar\" name=\"")
  442. .append(defaultUri)
  443. .append("\" revision=\"")
  444. .append(BRANCH)
  445. .append("\" >")
  446. .append("<copyfile src=\"hello.txt\" dest=\"Hello.txt\" />")
  447. .append("</project>")
  448. .append("</manifest>");
  449. JGitTestUtil.writeTrashFile(tempDb, "new.xml", xmlContent.toString());
  450. command = new RepoCommand(remoteDb);
  451. command.setPath(tempDb.getWorkTree().getAbsolutePath() + "/new.xml")
  452. .setURI(rootUri)
  453. .call();
  454. // Clone it
  455. File directory = createTempDirectory("testReplaceManifestBare");
  456. Repository localDb = Git
  457. .cloneRepository()
  458. .setDirectory(directory)
  459. .setURI(remoteDb.getDirectory().toURI().toString())
  460. .call()
  461. .getRepository();
  462. // The Hello file should not exist
  463. File hello = new File(localDb.getWorkTree(), "Hello");
  464. assertFalse("The Hello file shouldn't exist", hello.exists());
  465. // The Hello.txt file should exist
  466. File hellotxt = new File(localDb.getWorkTree(), "Hello.txt");
  467. assertTrue("The Hello.txt file should exist", hellotxt.exists());
  468. // The .gitmodules file should have 'submodule "bar"' and shouldn't have
  469. // 'submodule "foo"' lines.
  470. File dotmodules = new File(localDb.getWorkTree(),
  471. Constants.DOT_GIT_MODULES);
  472. BufferedReader reader = new BufferedReader(new FileReader(dotmodules));
  473. boolean foo = false;
  474. boolean bar = false;
  475. while (true) {
  476. String line = reader.readLine();
  477. if (line == null)
  478. break;
  479. if (line.contains("submodule \"foo\""))
  480. foo = true;
  481. if (line.contains("submodule \"bar\""))
  482. bar = true;
  483. }
  484. reader.close();
  485. assertTrue("The bar submodule should exist", bar);
  486. assertFalse("The foo submodule shouldn't exist", foo);
  487. }
  488. @Test
  489. public void testRemoveOverlappingBare() throws Exception {
  490. Repository remoteDb = createBareRepository();
  491. Repository tempDb = createWorkRepository();
  492. StringBuilder xmlContent = new StringBuilder();
  493. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  494. .append("<manifest>")
  495. .append("<remote name=\"remote1\" fetch=\".\" />")
  496. .append("<default revision=\"master\" remote=\"remote1\" />")
  497. .append("<project path=\"foo/bar\" name=\"")
  498. .append(groupBUri)
  499. .append("\" />")
  500. .append("<project path=\"a\" name=\"")
  501. .append(groupAUri)
  502. .append("\" />")
  503. .append("<project path=\"foo\" name=\"")
  504. .append(defaultUri)
  505. .append("\" />")
  506. .append("</manifest>");
  507. JGitTestUtil.writeTrashFile(
  508. tempDb, "manifest.xml", xmlContent.toString());
  509. RepoCommand command = new RepoCommand(remoteDb);
  510. command
  511. .setPath(tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  512. .setURI(rootUri)
  513. .call();
  514. // Clone it
  515. File directory = createTempDirectory("testRemoveOverlappingBare");
  516. Repository localDb = Git
  517. .cloneRepository()
  518. .setDirectory(directory)
  519. .setURI(remoteDb.getDirectory().toURI().toString())
  520. .call()
  521. .getRepository();
  522. // The .gitmodules file should have 'submodule "foo"' and shouldn't have
  523. // 'submodule "foo/bar"' lines.
  524. File dotmodules = new File(localDb.getWorkTree(),
  525. Constants.DOT_GIT_MODULES);
  526. BufferedReader reader = new BufferedReader(new FileReader(dotmodules));
  527. boolean foo = false;
  528. boolean foobar = false;
  529. boolean a = false;
  530. while (true) {
  531. String line = reader.readLine();
  532. if (line == null)
  533. break;
  534. if (line.contains("submodule \"foo\""))
  535. foo = true;
  536. if (line.contains("submodule \"foo/bar\""))
  537. foobar = true;
  538. if (line.contains("submodule \"a\""))
  539. a = true;
  540. }
  541. reader.close();
  542. assertTrue("The foo submodule should exist", foo);
  543. assertFalse("The foo/bar submodule shouldn't exist", foobar);
  544. assertTrue("The a submodule should exist", a);
  545. }
  546. @Test
  547. public void testIncludeTag() throws Exception {
  548. Repository localDb = createWorkRepository();
  549. Repository tempDb = createWorkRepository();
  550. StringBuilder xmlContent = new StringBuilder();
  551. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  552. .append("<manifest>")
  553. .append("<include name=\"_include.xml\" />")
  554. .append("<default revision=\"master\" remote=\"remote1\" />")
  555. .append("</manifest>");
  556. JGitTestUtil.writeTrashFile(
  557. tempDb, "manifest.xml", xmlContent.toString());
  558. xmlContent = new StringBuilder();
  559. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  560. .append("<manifest>")
  561. .append("<remote name=\"remote1\" fetch=\".\" />")
  562. .append("<default revision=\"master\" remote=\"remote1\" />")
  563. .append("<project path=\"foo\" name=\"")
  564. .append(defaultUri)
  565. .append("\" />")
  566. .append("</manifest>");
  567. JGitTestUtil.writeTrashFile(
  568. tempDb, "_include.xml", xmlContent.toString());
  569. RepoCommand command = new RepoCommand(localDb);
  570. command
  571. .setPath(tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  572. .setURI(rootUri)
  573. .call();
  574. File hello = new File(localDb.getWorkTree(), "foo/hello.txt");
  575. assertTrue("submodule should be checked out", hello.exists());
  576. BufferedReader reader = new BufferedReader(new FileReader(hello));
  577. String content = reader.readLine();
  578. reader.close();
  579. assertEquals("submodule content should be as expected",
  580. "master world", content);
  581. }
  582. private void resolveRelativeUris() {
  583. // Find the longest common prefix ends with "/" as rootUri.
  584. defaultUri = defaultDb.getDirectory().toURI().toString();
  585. notDefaultUri = notDefaultDb.getDirectory().toURI().toString();
  586. groupAUri = groupADb.getDirectory().toURI().toString();
  587. groupBUri = groupBDb.getDirectory().toURI().toString();
  588. int start = 0;
  589. while (start <= defaultUri.length()) {
  590. int newStart = defaultUri.indexOf('/', start + 1);
  591. String prefix = defaultUri.substring(0, newStart);
  592. if (!notDefaultUri.startsWith(prefix) ||
  593. !groupAUri.startsWith(prefix) ||
  594. !groupBUri.startsWith(prefix)) {
  595. start++;
  596. rootUri = defaultUri.substring(0, start) + "manifest";
  597. defaultUri = defaultUri.substring(start);
  598. notDefaultUri = notDefaultUri.substring(start);
  599. groupAUri = groupAUri.substring(start);
  600. groupBUri = groupBUri.substring(start);
  601. return;
  602. }
  603. start = newStart;
  604. }
  605. }
  606. }