Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

RepoCommandTest.java 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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.eclipse.jgit.storage.file.FileBasedConfig;
  57. import org.eclipse.jgit.util.FS;
  58. import org.junit.Test;
  59. public class RepoCommandTest extends RepositoryTestCase {
  60. private static final String BRANCH = "branch";
  61. private static final String TAG = "release";
  62. private Repository defaultDb;
  63. private Repository notDefaultDb;
  64. private Repository groupADb;
  65. private Repository groupBDb;
  66. private String rootUri;
  67. private String defaultUri;
  68. private String notDefaultUri;
  69. private String groupAUri;
  70. private String groupBUri;
  71. private ObjectId oldCommitId;
  72. public void setUp() throws Exception {
  73. super.setUp();
  74. defaultDb = createWorkRepository();
  75. try (Git git = new Git(defaultDb)) {
  76. JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "branch world");
  77. git.add().addFilepattern("hello.txt").call();
  78. oldCommitId = git.commit().setMessage("Initial commit").call().getId();
  79. git.checkout().setName(BRANCH).setCreateBranch(true).call();
  80. git.checkout().setName("master").call();
  81. git.tag().setName(TAG).call();
  82. JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "master world");
  83. git.add().addFilepattern("hello.txt").call();
  84. git.commit().setMessage("Second commit").call();
  85. addRepoToClose(defaultDb);
  86. }
  87. notDefaultDb = createWorkRepository();
  88. try (Git git = new Git(notDefaultDb)) {
  89. JGitTestUtil.writeTrashFile(notDefaultDb, "world.txt", "hello");
  90. git.add().addFilepattern("world.txt").call();
  91. git.commit().setMessage("Initial commit").call();
  92. addRepoToClose(notDefaultDb);
  93. }
  94. groupADb = createWorkRepository();
  95. try (Git git = new Git(groupADb)) {
  96. JGitTestUtil.writeTrashFile(groupADb, "a.txt", "world");
  97. git.add().addFilepattern("a.txt").call();
  98. git.commit().setMessage("Initial commit").call();
  99. addRepoToClose(groupADb);
  100. }
  101. groupBDb = createWorkRepository();
  102. try (Git git = new Git(groupBDb)) {
  103. JGitTestUtil.writeTrashFile(groupBDb, "b.txt", "world");
  104. git.add().addFilepattern("b.txt").call();
  105. git.commit().setMessage("Initial commit").call();
  106. addRepoToClose(groupBDb);
  107. }
  108. resolveRelativeUris();
  109. }
  110. @Test
  111. public void testAddRepoManifest() throws Exception {
  112. StringBuilder xmlContent = new StringBuilder();
  113. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  114. .append("<manifest>")
  115. .append("<remote name=\"remote1\" fetch=\".\" />")
  116. .append("<default revision=\"master\" remote=\"remote1\" />")
  117. .append("<project path=\"foo\" name=\"")
  118. .append(defaultUri)
  119. .append("\" />")
  120. .append("</manifest>");
  121. writeTrashFile("manifest.xml", xmlContent.toString());
  122. RepoCommand command = new RepoCommand(db);
  123. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  124. .setURI(rootUri)
  125. .call();
  126. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  127. assertTrue("submodule should be checked out", hello.exists());
  128. BufferedReader reader = new BufferedReader(new FileReader(hello));
  129. String content = reader.readLine();
  130. reader.close();
  131. assertEquals("submodule content should be as expected",
  132. "master world", content);
  133. }
  134. @Test
  135. public void testRepoManifestGroups() throws Exception {
  136. StringBuilder xmlContent = new StringBuilder();
  137. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  138. .append("<manifest>")
  139. .append("<remote name=\"remote1\" fetch=\".\" />")
  140. .append("<default revision=\"master\" remote=\"remote1\" />")
  141. .append("<project path=\"foo\" name=\"")
  142. .append(defaultUri)
  143. .append("\" groups=\"a,test\" />")
  144. .append("<project path=\"bar\" name=\"")
  145. .append(notDefaultUri)
  146. .append("\" groups=\"notdefault\" />")
  147. .append("<project path=\"a\" name=\"")
  148. .append(groupAUri)
  149. .append("\" groups=\"a\" />")
  150. .append("<project path=\"b\" name=\"")
  151. .append(groupBUri)
  152. .append("\" groups=\"b\" />")
  153. .append("</manifest>");
  154. // default should have foo, a & b
  155. Repository localDb = createWorkRepository();
  156. JGitTestUtil.writeTrashFile(
  157. localDb, "manifest.xml", xmlContent.toString());
  158. RepoCommand command = new RepoCommand(localDb);
  159. command
  160. .setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  161. .setURI(rootUri)
  162. .call();
  163. File file = new File(localDb.getWorkTree(), "foo/hello.txt");
  164. assertTrue("default should have foo", file.exists());
  165. file = new File(localDb.getWorkTree(), "bar/world.txt");
  166. assertFalse("default shouldn't have bar", file.exists());
  167. file = new File(localDb.getWorkTree(), "a/a.txt");
  168. assertTrue("default should have a", file.exists());
  169. file = new File(localDb.getWorkTree(), "b/b.txt");
  170. assertTrue("default should have b", file.exists());
  171. // all,-a should have bar & b
  172. localDb = createWorkRepository();
  173. JGitTestUtil.writeTrashFile(
  174. localDb, "manifest.xml", xmlContent.toString());
  175. command = new RepoCommand(localDb);
  176. command
  177. .setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  178. .setURI(rootUri)
  179. .setGroups("all,-a")
  180. .call();
  181. file = new File(localDb.getWorkTree(), "foo/hello.txt");
  182. assertFalse("\"all,-a\" shouldn't have foo", file.exists());
  183. file = new File(localDb.getWorkTree(), "bar/world.txt");
  184. assertTrue("\"all,-a\" should have bar", file.exists());
  185. file = new File(localDb.getWorkTree(), "a/a.txt");
  186. assertFalse("\"all,-a\" shuoldn't have a", file.exists());
  187. file = new File(localDb.getWorkTree(), "b/b.txt");
  188. assertTrue("\"all,-a\" should have b", file.exists());
  189. }
  190. @Test
  191. public void testRepoManifestCopyFile() throws Exception {
  192. Repository localDb = createWorkRepository();
  193. StringBuilder xmlContent = new StringBuilder();
  194. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  195. .append("<manifest>")
  196. .append("<remote name=\"remote1\" fetch=\".\" />")
  197. .append("<default revision=\"master\" remote=\"remote1\" />")
  198. .append("<project path=\"foo\" name=\"")
  199. .append(defaultUri)
  200. .append("\">")
  201. .append("<copyfile src=\"hello.txt\" dest=\"Hello\" />")
  202. .append("</project>")
  203. .append("</manifest>");
  204. JGitTestUtil.writeTrashFile(
  205. localDb, "manifest.xml", xmlContent.toString());
  206. RepoCommand command = new RepoCommand(localDb);
  207. command
  208. .setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  209. .setURI(rootUri)
  210. .call();
  211. // The original file should exist
  212. File hello = new File(localDb.getWorkTree(), "foo/hello.txt");
  213. assertTrue("The original file should exist", hello.exists());
  214. BufferedReader reader = new BufferedReader(new FileReader(hello));
  215. String content = reader.readLine();
  216. reader.close();
  217. assertEquals("The original file should have expected content",
  218. "master world", content);
  219. // The dest file should also exist
  220. hello = new File(localDb.getWorkTree(), "Hello");
  221. assertTrue("The destination file should exist", hello.exists());
  222. reader = new BufferedReader(new FileReader(hello));
  223. content = reader.readLine();
  224. reader.close();
  225. assertEquals("The destination file should have expected content",
  226. "master world", content);
  227. }
  228. @Test
  229. public void testBareRepo() throws Exception {
  230. try (
  231. Repository remoteDb = createBareRepository();
  232. Repository tempDb = createWorkRepository()) {
  233. StringBuilder xmlContent = new StringBuilder();
  234. xmlContent
  235. .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  236. .append("<manifest>")
  237. .append("<remote name=\"remote1\" fetch=\".\" />")
  238. .append("<default revision=\"master\" remote=\"remote1\" />")
  239. .append("<project path=\"foo\" name=\"").append(defaultUri)
  240. .append("\" />").append("</manifest>");
  241. JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
  242. xmlContent.toString());
  243. RepoCommand command = new RepoCommand(remoteDb);
  244. command.setPath(
  245. tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  246. .setURI(rootUri).call();
  247. // Clone it
  248. File directory = createTempDirectory("testBareRepo");
  249. Repository localDb = Git.cloneRepository().setDirectory(directory)
  250. .setURI(remoteDb.getDirectory().toURI().toString()).call()
  251. .getRepository();
  252. // The .gitmodules file should exist
  253. File gitmodules = new File(localDb.getWorkTree(), ".gitmodules");
  254. assertTrue("The .gitmodules file should exist", gitmodules.exists());
  255. // The first line of .gitmodules file should be expected
  256. BufferedReader reader = new BufferedReader(new FileReader(
  257. gitmodules));
  258. String content = reader.readLine();
  259. reader.close();
  260. assertEquals(
  261. "The first line of .gitmodules file should be as expected",
  262. "[submodule \"foo\"]", content);
  263. // The gitlink should be the same as remote head sha1
  264. String gitlink = localDb.resolve(Constants.HEAD + ":foo").name();
  265. localDb.close();
  266. String remote = defaultDb.resolve(Constants.HEAD).name();
  267. assertEquals("The gitlink should be the same as remote head",
  268. remote, gitlink);
  269. }
  270. }
  271. @Test
  272. public void testRevision() throws Exception {
  273. StringBuilder xmlContent = new StringBuilder();
  274. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  275. .append("<manifest>")
  276. .append("<remote name=\"remote1\" fetch=\".\" />")
  277. .append("<default revision=\"master\" remote=\"remote1\" />")
  278. .append("<project path=\"foo\" name=\"")
  279. .append(defaultUri)
  280. .append("\" revision=\"")
  281. .append(oldCommitId.name())
  282. .append("\" />")
  283. .append("</manifest>");
  284. writeTrashFile("manifest.xml", xmlContent.toString());
  285. RepoCommand command = new RepoCommand(db);
  286. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  287. .setURI(rootUri)
  288. .call();
  289. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  290. BufferedReader reader = new BufferedReader(new FileReader(hello));
  291. String content = reader.readLine();
  292. reader.close();
  293. assertEquals("submodule content should be as expected",
  294. "branch world", content);
  295. }
  296. @Test
  297. public void testRevisionBranch() throws Exception {
  298. StringBuilder xmlContent = new StringBuilder();
  299. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  300. .append("<manifest>")
  301. .append("<remote name=\"remote1\" fetch=\".\" />")
  302. .append("<default revision=\"")
  303. .append(BRANCH)
  304. .append("\" remote=\"remote1\" />")
  305. .append("<project path=\"foo\" name=\"")
  306. .append(defaultUri)
  307. .append("\" />")
  308. .append("</manifest>");
  309. writeTrashFile("manifest.xml", xmlContent.toString());
  310. RepoCommand command = new RepoCommand(db);
  311. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  312. .setURI(rootUri)
  313. .call();
  314. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  315. BufferedReader reader = new BufferedReader(new FileReader(hello));
  316. String content = reader.readLine();
  317. reader.close();
  318. assertEquals("submodule content should be as expected",
  319. "branch world", content);
  320. }
  321. @Test
  322. public void testRevisionTag() throws Exception {
  323. StringBuilder xmlContent = new StringBuilder();
  324. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  325. .append("<manifest>")
  326. .append("<remote name=\"remote1\" fetch=\".\" />")
  327. .append("<default revision=\"master\" remote=\"remote1\" />")
  328. .append("<project path=\"foo\" name=\"")
  329. .append(defaultUri)
  330. .append("\" revision=\"")
  331. .append(TAG)
  332. .append("\" />")
  333. .append("</manifest>");
  334. writeTrashFile("manifest.xml", xmlContent.toString());
  335. RepoCommand command = new RepoCommand(db);
  336. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  337. .setURI(rootUri)
  338. .call();
  339. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  340. BufferedReader reader = new BufferedReader(new FileReader(hello));
  341. String content = reader.readLine();
  342. reader.close();
  343. assertEquals("submodule content should be as expected",
  344. "branch world", content);
  345. }
  346. @Test
  347. public void testRevisionBare() throws Exception {
  348. try (
  349. Repository remoteDb = createBareRepository();
  350. Repository tempDb = createWorkRepository()) {
  351. StringBuilder xmlContent = new StringBuilder();
  352. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  353. .append("<manifest>")
  354. .append("<remote name=\"remote1\" fetch=\".\" />")
  355. .append("<default revision=\"").append(BRANCH)
  356. .append("\" remote=\"remote1\" />")
  357. .append("<project path=\"foo\" name=\"").append(defaultUri)
  358. .append("\" />").append("</manifest>");
  359. JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
  360. xmlContent.toString());
  361. RepoCommand command = new RepoCommand(remoteDb);
  362. command.setPath(
  363. tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  364. .setURI(rootUri).call();
  365. // Clone it
  366. File directory = createTempDirectory("testRevisionBare");
  367. Repository localDb = Git.cloneRepository().setDirectory(directory)
  368. .setURI(remoteDb.getDirectory().toURI().toString()).call()
  369. .getRepository();
  370. // The gitlink should be the same as oldCommitId
  371. String gitlink = localDb.resolve(Constants.HEAD + ":foo").name();
  372. localDb.close();
  373. assertEquals("The gitlink is same as remote head",
  374. oldCommitId.name(), gitlink);
  375. }
  376. }
  377. @Test
  378. public void testCopyFileBare() throws Exception {
  379. try (
  380. Repository remoteDb = createBareRepository();
  381. Repository tempDb = createWorkRepository()) {
  382. StringBuilder xmlContent = new StringBuilder();
  383. xmlContent
  384. .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  385. .append("<manifest>")
  386. .append("<remote name=\"remote1\" fetch=\".\" />")
  387. .append("<default revision=\"master\" remote=\"remote1\" />")
  388. .append("<project path=\"foo\" name=\"").append(defaultUri)
  389. .append("\" revision=\"").append(BRANCH).append("\" >")
  390. .append("<copyfile src=\"hello.txt\" dest=\"Hello\" />")
  391. .append("<copyfile src=\"hello.txt\" dest=\"foo/Hello\" />")
  392. .append("</project>").append("</manifest>");
  393. JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
  394. xmlContent.toString());
  395. RepoCommand command = new RepoCommand(remoteDb);
  396. command.setPath(
  397. tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  398. .setURI(rootUri).call();
  399. // Clone it
  400. File directory = createTempDirectory("testCopyFileBare");
  401. Repository localDb = Git.cloneRepository().setDirectory(directory)
  402. .setURI(remoteDb.getDirectory().toURI().toString()).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 foo/Hello file should be skipped.
  408. File foohello = new File(localDb.getWorkTree(), "foo/Hello");
  409. assertFalse(
  410. "The foo/Hello file should be skipped", foohello.exists());
  411. localDb.close();
  412. // The content of Hello file should be expected
  413. BufferedReader reader = new BufferedReader(new FileReader(hello));
  414. String content = reader.readLine();
  415. reader.close();
  416. assertEquals("The Hello file should have expected content",
  417. "branch world", content);
  418. }
  419. }
  420. @Test
  421. public void testReplaceManifestBare() throws Exception {
  422. try (
  423. Repository remoteDb = createBareRepository();
  424. Repository tempDb = createWorkRepository()) {
  425. StringBuilder xmlContent = new StringBuilder();
  426. xmlContent
  427. .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  428. .append("<manifest>")
  429. .append("<remote name=\"remote1\" fetch=\".\" />")
  430. .append("<default revision=\"master\" remote=\"remote1\" />")
  431. .append("<project path=\"foo\" name=\"").append(defaultUri)
  432. .append("\" revision=\"").append(BRANCH).append("\" >")
  433. .append("<copyfile src=\"hello.txt\" dest=\"Hello\" />")
  434. .append("</project>").append("</manifest>");
  435. JGitTestUtil.writeTrashFile(tempDb, "old.xml",
  436. xmlContent.toString());
  437. RepoCommand command = new RepoCommand(remoteDb);
  438. command.setPath(tempDb.getWorkTree().getAbsolutePath() + "/old.xml")
  439. .setURI(rootUri).call();
  440. xmlContent = new StringBuilder();
  441. xmlContent
  442. .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  443. .append("<manifest>")
  444. .append("<remote name=\"remote1\" fetch=\".\" />")
  445. .append("<default revision=\"master\" remote=\"remote1\" />")
  446. .append("<project path=\"bar\" name=\"")
  447. .append(defaultUri)
  448. .append("\" revision=\"")
  449. .append(BRANCH)
  450. .append("\" >")
  451. .append("<copyfile src=\"hello.txt\" dest=\"Hello.txt\" />")
  452. .append("</project>").append("</manifest>");
  453. JGitTestUtil.writeTrashFile(tempDb, "new.xml",
  454. xmlContent.toString());
  455. command = new RepoCommand(remoteDb);
  456. command.setPath(tempDb.getWorkTree().getAbsolutePath() + "/new.xml")
  457. .setURI(rootUri).call();
  458. // Clone it
  459. File directory = createTempDirectory("testReplaceManifestBare");
  460. Repository localDb = Git.cloneRepository().setDirectory(directory)
  461. .setURI(remoteDb.getDirectory().toURI().toString()).call()
  462. .getRepository();
  463. // The Hello file should not exist
  464. File hello = new File(localDb.getWorkTree(), "Hello");
  465. assertFalse("The Hello file shouldn't exist", hello.exists());
  466. // The Hello.txt file should exist
  467. File hellotxt = new File(localDb.getWorkTree(), "Hello.txt");
  468. assertTrue("The Hello.txt file should exist", hellotxt.exists());
  469. // The .gitmodules file should have 'submodule "bar"' and shouldn't
  470. // have
  471. // 'submodule "foo"' lines.
  472. File dotmodules = new File(localDb.getWorkTree(),
  473. Constants.DOT_GIT_MODULES);
  474. localDb.close();
  475. BufferedReader reader = new BufferedReader(new FileReader(
  476. dotmodules));
  477. boolean foo = false;
  478. boolean bar = false;
  479. while (true) {
  480. String line = reader.readLine();
  481. if (line == null)
  482. break;
  483. if (line.contains("submodule \"foo\""))
  484. foo = true;
  485. if (line.contains("submodule \"bar\""))
  486. bar = true;
  487. }
  488. reader.close();
  489. assertTrue("The bar submodule should exist", bar);
  490. assertFalse("The foo submodule shouldn't exist", foo);
  491. }
  492. }
  493. @Test
  494. public void testRemoveOverlappingBare() throws Exception {
  495. try (
  496. Repository remoteDb = createBareRepository();
  497. Repository tempDb = createWorkRepository()) {
  498. StringBuilder xmlContent = new StringBuilder();
  499. xmlContent
  500. .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  501. .append("<manifest>")
  502. .append("<remote name=\"remote1\" fetch=\".\" />")
  503. .append("<default revision=\"master\" remote=\"remote1\" />")
  504. .append("<project path=\"foo/bar\" name=\"")
  505. .append(groupBUri).append("\" />")
  506. .append("<project path=\"a\" name=\"").append(groupAUri)
  507. .append("\" />").append("<project path=\"foo\" name=\"")
  508. .append(defaultUri).append("\" />").append("</manifest>");
  509. JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
  510. xmlContent.toString());
  511. RepoCommand command = new RepoCommand(remoteDb);
  512. command.setPath(
  513. tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  514. .setURI(rootUri).call();
  515. // Clone it
  516. File directory = createTempDirectory("testRemoveOverlappingBare");
  517. Repository localDb = Git.cloneRepository().setDirectory(directory)
  518. .setURI(remoteDb.getDirectory().toURI().toString()).call()
  519. .getRepository();
  520. // The .gitmodules file should have 'submodule "foo"' and shouldn't
  521. // have
  522. // 'submodule "foo/bar"' lines.
  523. File dotmodules = new File(localDb.getWorkTree(),
  524. Constants.DOT_GIT_MODULES);
  525. localDb.close();
  526. BufferedReader reader = new BufferedReader(new FileReader(
  527. dotmodules));
  528. boolean foo = false;
  529. boolean foobar = false;
  530. boolean a = false;
  531. while (true) {
  532. String line = reader.readLine();
  533. if (line == null)
  534. break;
  535. if (line.contains("submodule \"foo\""))
  536. foo = true;
  537. if (line.contains("submodule \"foo/bar\""))
  538. foobar = true;
  539. if (line.contains("submodule \"a\""))
  540. a = true;
  541. }
  542. reader.close();
  543. assertTrue("The foo submodule should exist", foo);
  544. assertFalse("The foo/bar submodule shouldn't exist", foobar);
  545. assertTrue("The a submodule should exist", a);
  546. }
  547. }
  548. @Test
  549. public void testIncludeTag() throws Exception {
  550. Repository localDb = createWorkRepository();
  551. Repository tempDb = createWorkRepository();
  552. StringBuilder xmlContent = new StringBuilder();
  553. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  554. .append("<manifest>")
  555. .append("<include name=\"_include.xml\" />")
  556. .append("<default revision=\"master\" remote=\"remote1\" />")
  557. .append("</manifest>");
  558. JGitTestUtil.writeTrashFile(
  559. tempDb, "manifest.xml", xmlContent.toString());
  560. xmlContent = new StringBuilder();
  561. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  562. .append("<manifest>")
  563. .append("<remote name=\"remote1\" fetch=\".\" />")
  564. .append("<default revision=\"master\" remote=\"remote1\" />")
  565. .append("<project path=\"foo\" name=\"")
  566. .append(defaultUri)
  567. .append("\" />")
  568. .append("</manifest>");
  569. JGitTestUtil.writeTrashFile(
  570. tempDb, "_include.xml", xmlContent.toString());
  571. RepoCommand command = new RepoCommand(localDb);
  572. command
  573. .setPath(tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  574. .setURI(rootUri)
  575. .call();
  576. File hello = new File(localDb.getWorkTree(), "foo/hello.txt");
  577. assertTrue("submodule should be checked out", hello.exists());
  578. BufferedReader reader = new BufferedReader(new FileReader(hello));
  579. String content = reader.readLine();
  580. reader.close();
  581. assertEquals("submodule content should be as expected",
  582. "master world", content);
  583. }
  584. @Test
  585. public void testNonDefaultRemotes() throws Exception {
  586. StringBuilder xmlContent = new StringBuilder();
  587. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  588. .append("<manifest>")
  589. .append("<remote name=\"remote1\" fetch=\".\" />")
  590. .append("<remote name=\"remote2\" fetch=\"")
  591. .append(notDefaultUri)
  592. .append("\" />")
  593. .append("<default revision=\"master\" remote=\"remote1\" />")
  594. .append("<project path=\"foo\" name=\"")
  595. .append(defaultUri)
  596. .append("\" />")
  597. .append("<project path=\"bar\" name=\".\" remote=\"remote2\" />")
  598. .append("</manifest>");
  599. Repository localDb = createWorkRepository();
  600. JGitTestUtil.writeTrashFile(
  601. localDb, "manifest.xml", xmlContent.toString());
  602. RepoCommand command = new RepoCommand(localDb);
  603. command
  604. .setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  605. .setURI(rootUri)
  606. .call();
  607. File file = new File(localDb.getWorkTree(), "foo/hello.txt");
  608. assertTrue("We should have foo", file.exists());
  609. file = new File(localDb.getWorkTree(), "bar/world.txt");
  610. assertTrue("We should have bar", file.exists());
  611. }
  612. @Test
  613. public void testRemoteAlias() throws Exception {
  614. StringBuilder xmlContent = new StringBuilder();
  615. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  616. .append("<manifest>")
  617. .append("<remote name=\"remote1\" fetch=\".\" alias=\"remote2\" />")
  618. .append("<default revision=\"master\" remote=\"remote2\" />")
  619. .append("<project path=\"foo\" name=\"")
  620. .append(defaultUri)
  621. .append("\" />")
  622. .append("</manifest>");
  623. Repository localDb = createWorkRepository();
  624. JGitTestUtil.writeTrashFile(
  625. localDb, "manifest.xml", xmlContent.toString());
  626. RepoCommand command = new RepoCommand(localDb);
  627. command
  628. .setPath(localDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  629. .setURI(rootUri)
  630. .call();
  631. File file = new File(localDb.getWorkTree(), "foo/hello.txt");
  632. assertTrue("We should have foo", file.exists());
  633. }
  634. @Test
  635. public void testTargetBranch() throws Exception {
  636. try (
  637. Repository remoteDb1 = createBareRepository();
  638. Repository remoteDb2 = createBareRepository();
  639. Repository tempDb = createWorkRepository()) {
  640. StringBuilder xmlContent = new StringBuilder();
  641. xmlContent
  642. .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  643. .append("<manifest>")
  644. .append("<remote name=\"remote1\" fetch=\".\" />")
  645. .append("<default revision=\"master\" remote=\"remote1\" />")
  646. .append("<project path=\"foo\" name=\"").append(defaultUri)
  647. .append("\" />").append("</manifest>");
  648. JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
  649. xmlContent.toString());
  650. RepoCommand command = new RepoCommand(remoteDb1);
  651. command
  652. .setPath(tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  653. .setURI(rootUri)
  654. .setTargetBranch("test")
  655. .call();
  656. ObjectId branchId = remoteDb1.resolve(
  657. Constants.R_HEADS + "test^{tree}");
  658. command = new RepoCommand(remoteDb2);
  659. command
  660. .setPath(tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  661. .setURI(rootUri)
  662. .call();
  663. ObjectId defaultId = remoteDb2.resolve(Constants.HEAD + "^{tree}");
  664. assertEquals(
  665. "The tree id of branch db and default db should be the same",
  666. branchId, defaultId);
  667. }
  668. }
  669. @Test
  670. public void testRecordRemoteBranch() throws Exception {
  671. try (
  672. Repository remoteDb = createBareRepository();
  673. Repository tempDb = createWorkRepository()) {
  674. StringBuilder xmlContent = new StringBuilder();
  675. xmlContent
  676. .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  677. .append("<manifest>")
  678. .append("<remote name=\"remote1\" fetch=\".\" />")
  679. .append("<default revision=\"master\" remote=\"remote1\" />")
  680. .append("<project path=\"with-branch\" ")
  681. .append("revision=\"master\" ")
  682. .append("name=\"").append(notDefaultUri).append("\" />")
  683. .append("<project path=\"with-long-branch\" ")
  684. .append("revision=\"refs/heads/master\" ")
  685. .append("name=\"").append(defaultUri).append("\" />")
  686. .append("</manifest>");
  687. JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
  688. xmlContent.toString());
  689. RepoCommand command = new RepoCommand(remoteDb);
  690. command.setPath(tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  691. .setURI(rootUri)
  692. .setRecordRemoteBranch(true)
  693. .call();
  694. // Clone it
  695. File directory = createTempDirectory("testBareRepo");
  696. try (Repository localDb = Git.cloneRepository()
  697. .setDirectory(directory)
  698. .setURI(remoteDb.getDirectory().toURI().toString()).call()
  699. .getRepository();) {
  700. // The .gitmodules file should exist
  701. File gitmodules = new File(localDb.getWorkTree(),
  702. ".gitmodules");
  703. assertTrue("The .gitmodules file should exist",
  704. gitmodules.exists());
  705. FileBasedConfig c = new FileBasedConfig(gitmodules,
  706. FS.DETECTED);
  707. c.load();
  708. assertEquals("standard branches work", "master",
  709. c.getString("submodule", "with-branch", "branch"));
  710. assertEquals("long branches work", "refs/heads/master",
  711. c.getString("submodule", "with-long-branch", "branch"));
  712. }
  713. }
  714. }
  715. @Test
  716. public void testRemoteRevision() throws Exception {
  717. StringBuilder xmlContent = new StringBuilder();
  718. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  719. .append("<manifest>")
  720. .append("<remote name=\"remote1\" fetch=\".\" />")
  721. .append("<remote name=\"remote2\" fetch=\".\" revision=\"")
  722. .append(BRANCH)
  723. .append("\" />")
  724. .append("<default remote=\"remote1\" revision=\"master\" />")
  725. .append("<project path=\"foo\" remote=\"remote2\" name=\"")
  726. .append(defaultUri)
  727. .append("\" />")
  728. .append("</manifest>");
  729. writeTrashFile("manifest.xml", xmlContent.toString());
  730. RepoCommand command = new RepoCommand(db);
  731. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  732. .setURI(rootUri)
  733. .call();
  734. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  735. BufferedReader reader = new BufferedReader(new FileReader(hello));
  736. String content = reader.readLine();
  737. reader.close();
  738. assertEquals("submodule content should be as expected",
  739. "branch world", content);
  740. }
  741. @Test
  742. public void testDefaultRemoteRevision() throws Exception {
  743. StringBuilder xmlContent = new StringBuilder();
  744. xmlContent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  745. .append("<manifest>")
  746. .append("<remote name=\"remote1\" fetch=\".\" revision=\"")
  747. .append(BRANCH)
  748. .append("\" />")
  749. .append("<default remote=\"remote1\" />")
  750. .append("<project path=\"foo\" name=\"")
  751. .append(defaultUri)
  752. .append("\" />")
  753. .append("</manifest>");
  754. writeTrashFile("manifest.xml", xmlContent.toString());
  755. RepoCommand command = new RepoCommand(db);
  756. command.setPath(db.getWorkTree().getAbsolutePath() + "/manifest.xml")
  757. .setURI(rootUri)
  758. .call();
  759. File hello = new File(db.getWorkTree(), "foo/hello.txt");
  760. BufferedReader reader = new BufferedReader(new FileReader(hello));
  761. String content = reader.readLine();
  762. reader.close();
  763. assertEquals("submodule content should be as expected",
  764. "branch world", content);
  765. }
  766. private void resolveRelativeUris() {
  767. // Find the longest common prefix ends with "/" as rootUri.
  768. defaultUri = defaultDb.getDirectory().toURI().toString();
  769. notDefaultUri = notDefaultDb.getDirectory().toURI().toString();
  770. groupAUri = groupADb.getDirectory().toURI().toString();
  771. groupBUri = groupBDb.getDirectory().toURI().toString();
  772. int start = 0;
  773. while (start <= defaultUri.length()) {
  774. int newStart = defaultUri.indexOf('/', start + 1);
  775. String prefix = defaultUri.substring(0, newStart);
  776. if (!notDefaultUri.startsWith(prefix) ||
  777. !groupAUri.startsWith(prefix) ||
  778. !groupBUri.startsWith(prefix)) {
  779. start++;
  780. rootUri = defaultUri.substring(0, start) + "manifest";
  781. defaultUri = defaultUri.substring(start);
  782. notDefaultUri = notDefaultUri.substring(start);
  783. groupAUri = groupAUri.substring(start);
  784. groupBUri = groupBUri.substring(start);
  785. return;
  786. }
  787. start = newStart;
  788. }
  789. }
  790. }