Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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