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.

CloneCommandTest.java 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /*
  2. * Copyright (C) 2011, 2013 Chris Aniszczyk <caniszczyk@gmail.com>
  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.api;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertNull;
  48. import static org.junit.Assert.assertTrue;
  49. import static org.junit.Assert.fail;
  50. import java.io.File;
  51. import java.io.IOException;
  52. import java.net.URISyntaxException;
  53. import java.util.Collections;
  54. import java.util.List;
  55. import java.util.Map;
  56. import org.eclipse.jgit.api.ListBranchCommand.ListMode;
  57. import org.eclipse.jgit.api.errors.GitAPIException;
  58. import org.eclipse.jgit.api.errors.JGitInternalException;
  59. import org.eclipse.jgit.errors.NoWorkTreeException;
  60. import org.eclipse.jgit.junit.RepositoryTestCase;
  61. import org.eclipse.jgit.junit.TestRepository;
  62. import org.eclipse.jgit.lib.ConfigConstants;
  63. import org.eclipse.jgit.lib.Constants;
  64. import org.eclipse.jgit.lib.ObjectId;
  65. import org.eclipse.jgit.lib.Ref;
  66. import org.eclipse.jgit.lib.Repository;
  67. import org.eclipse.jgit.revwalk.RevBlob;
  68. import org.eclipse.jgit.revwalk.RevCommit;
  69. import org.eclipse.jgit.storage.file.FileBasedConfig;
  70. import org.eclipse.jgit.submodule.SubmoduleStatus;
  71. import org.eclipse.jgit.submodule.SubmoduleStatusType;
  72. import org.eclipse.jgit.submodule.SubmoduleWalk;
  73. import org.eclipse.jgit.transport.RefSpec;
  74. import org.eclipse.jgit.transport.RemoteConfig;
  75. import org.eclipse.jgit.util.SystemReader;
  76. import org.junit.Test;
  77. public class CloneCommandTest extends RepositoryTestCase {
  78. private Git git;
  79. private TestRepository<Repository> tr;
  80. public void setUp() throws Exception {
  81. super.setUp();
  82. tr = new TestRepository<Repository>(db);
  83. git = new Git(db);
  84. // commit something
  85. writeTrashFile("Test.txt", "Hello world");
  86. git.add().addFilepattern("Test.txt").call();
  87. git.commit().setMessage("Initial commit").call();
  88. git.tag().setName("tag-initial").setMessage("Tag initial").call();
  89. // create a test branch and switch to it
  90. git.checkout().setCreateBranch(true).setName("test").call();
  91. // commit something on the test branch
  92. writeTrashFile("Test.txt", "Some change");
  93. git.add().addFilepattern("Test.txt").call();
  94. git.commit().setMessage("Second commit").call();
  95. RevBlob blob = tr.blob("blob-not-in-master-branch");
  96. git.tag().setName("tag-for-blob").setObjectId(blob).call();
  97. }
  98. @Test
  99. public void testCloneRepository() throws IOException,
  100. JGitInternalException, GitAPIException, URISyntaxException {
  101. File directory = createTempDirectory("testCloneRepository");
  102. CloneCommand command = Git.cloneRepository();
  103. command.setDirectory(directory);
  104. command.setURI(fileUri());
  105. Git git2 = command.call();
  106. addRepoToClose(git2.getRepository());
  107. assertNotNull(git2);
  108. ObjectId id = git2.getRepository().resolve("tag-for-blob");
  109. assertNotNull(id);
  110. assertEquals(git2.getRepository().getFullBranch(), "refs/heads/test");
  111. assertEquals(
  112. "origin",
  113. git2.getRepository()
  114. .getConfig()
  115. .getString(ConfigConstants.CONFIG_BRANCH_SECTION,
  116. "test", ConfigConstants.CONFIG_KEY_REMOTE));
  117. assertEquals(
  118. "refs/heads/test",
  119. git2.getRepository()
  120. .getConfig()
  121. .getString(ConfigConstants.CONFIG_BRANCH_SECTION,
  122. "test", ConfigConstants.CONFIG_KEY_MERGE));
  123. assertEquals(2, git2.branchList().setListMode(ListMode.REMOTE).call()
  124. .size());
  125. assertEquals(new RefSpec("+refs/heads/*:refs/remotes/origin/*"),
  126. fetchRefSpec(git2.getRepository()));
  127. }
  128. @Test
  129. public void testCloneRepositoryExplicitGitDir() throws IOException,
  130. JGitInternalException, GitAPIException {
  131. File directory = createTempDirectory("testCloneRepository");
  132. CloneCommand command = Git.cloneRepository();
  133. command.setDirectory(directory);
  134. command.setGitDir(new File(directory, ".git"));
  135. command.setURI(fileUri());
  136. Git git2 = command.call();
  137. addRepoToClose(git2.getRepository());
  138. assertEquals(directory, git2.getRepository().getWorkTree());
  139. assertEquals(new File(directory, ".git"), git2.getRepository()
  140. .getDirectory());
  141. }
  142. @Test
  143. public void testCloneRepositoryExplicitGitDirNonStd() throws IOException,
  144. JGitInternalException, GitAPIException {
  145. File directory = createTempDirectory("testCloneRepository");
  146. File gDir = createTempDirectory("testCloneRepository.git");
  147. CloneCommand command = Git.cloneRepository();
  148. command.setDirectory(directory);
  149. command.setGitDir(gDir);
  150. command.setURI(fileUri());
  151. Git git2 = command.call();
  152. addRepoToClose(git2.getRepository());
  153. assertEquals(directory, git2.getRepository().getWorkTree());
  154. assertEquals(gDir, git2.getRepository()
  155. .getDirectory());
  156. assertTrue(new File(directory, ".git").isFile());
  157. assertFalse(new File(gDir, ".git").exists());
  158. }
  159. @Test
  160. public void testCloneRepositoryExplicitGitDirBare() throws IOException,
  161. JGitInternalException, GitAPIException {
  162. File gDir = createTempDirectory("testCloneRepository.git");
  163. CloneCommand command = Git.cloneRepository();
  164. command.setBare(true);
  165. command.setGitDir(gDir);
  166. command.setURI(fileUri());
  167. Git git2 = command.call();
  168. addRepoToClose(git2.getRepository());
  169. try {
  170. assertNull(null, git2.getRepository().getWorkTree());
  171. fail("Expected NoWorkTreeException");
  172. } catch (NoWorkTreeException e) {
  173. assertEquals(gDir, git2.getRepository().getDirectory());
  174. }
  175. }
  176. @Test
  177. public void testBareCloneRepository() throws IOException,
  178. JGitInternalException, GitAPIException, URISyntaxException {
  179. File directory = createTempDirectory("testCloneRepository_bare");
  180. CloneCommand command = Git.cloneRepository();
  181. command.setBare(true);
  182. command.setDirectory(directory);
  183. command.setURI(fileUri());
  184. Git git2 = command.call();
  185. addRepoToClose(git2.getRepository());
  186. assertEquals(new RefSpec("+refs/heads/*:refs/heads/*"),
  187. fetchRefSpec(git2.getRepository()));
  188. }
  189. @Test
  190. public void testCloneRepositoryCustomRemote() throws Exception {
  191. File directory = createTempDirectory("testCloneRemoteUpstream");
  192. CloneCommand command = Git.cloneRepository();
  193. command.setDirectory(directory);
  194. command.setRemote("upstream");
  195. command.setURI(fileUri());
  196. Git git2 = command.call();
  197. addRepoToClose(git2.getRepository());
  198. assertEquals("+refs/heads/*:refs/remotes/upstream/*",
  199. git2.getRepository()
  200. .getConfig()
  201. .getStringList("remote", "upstream",
  202. "fetch")[0]);
  203. assertEquals("upstream",
  204. git2.getRepository()
  205. .getConfig()
  206. .getString("branch", "test", "remote"));
  207. assertEquals(db.resolve("test"),
  208. git2.getRepository().resolve("upstream/test"));
  209. }
  210. @Test
  211. public void testBareCloneRepositoryCustomRemote() throws Exception {
  212. File directory = createTempDirectory("testCloneRemoteUpstream_bare");
  213. CloneCommand command = Git.cloneRepository();
  214. command.setBare(true);
  215. command.setDirectory(directory);
  216. command.setRemote("upstream");
  217. command.setURI(fileUri());
  218. Git git2 = command.call();
  219. addRepoToClose(git2.getRepository());
  220. assertEquals("+refs/heads/*:refs/heads/*",
  221. git2.getRepository()
  222. .getConfig()
  223. .getStringList("remote", "upstream",
  224. "fetch")[0]);
  225. assertEquals("upstream",
  226. git2.getRepository()
  227. .getConfig()
  228. .getString("branch", "test", "remote"));
  229. assertNull(git2.getRepository().resolve("upstream/test"));
  230. }
  231. @Test
  232. public void testBareCloneRepositoryNullRemote() throws Exception {
  233. File directory = createTempDirectory("testCloneRemoteNull_bare");
  234. CloneCommand command = Git.cloneRepository();
  235. command.setBare(true);
  236. command.setDirectory(directory);
  237. command.setRemote(null);
  238. command.setURI(fileUri());
  239. Git git2 = command.call();
  240. addRepoToClose(git2.getRepository());
  241. assertEquals("+refs/heads/*:refs/heads/*", git2.getRepository()
  242. .getConfig().getStringList("remote", "origin", "fetch")[0]);
  243. assertEquals("origin", git2.getRepository().getConfig()
  244. .getString("branch", "test", "remote"));
  245. }
  246. public static RefSpec fetchRefSpec(Repository r) throws URISyntaxException {
  247. RemoteConfig remoteConfig =
  248. new RemoteConfig(r.getConfig(), Constants.DEFAULT_REMOTE_NAME);
  249. return remoteConfig.getFetchRefSpecs().get(0);
  250. }
  251. @Test
  252. public void testCloneRepositoryWithBranch() throws IOException,
  253. JGitInternalException, GitAPIException {
  254. File directory = createTempDirectory("testCloneRepositoryWithBranch");
  255. CloneCommand command = Git.cloneRepository();
  256. command.setBranch("refs/heads/master");
  257. command.setDirectory(directory);
  258. command.setURI(fileUri());
  259. Git git2 = command.call();
  260. addRepoToClose(git2.getRepository());
  261. assertNotNull(git2);
  262. assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
  263. assertEquals(
  264. "refs/heads/master, refs/remotes/origin/master, refs/remotes/origin/test",
  265. allRefNames(git2.branchList().setListMode(ListMode.ALL).call()));
  266. // Same thing, but now without checkout
  267. directory = createTempDirectory("testCloneRepositoryWithBranch_bare");
  268. command = Git.cloneRepository();
  269. command.setBranch("refs/heads/master");
  270. command.setDirectory(directory);
  271. command.setURI(fileUri());
  272. command.setNoCheckout(true);
  273. git2 = command.call();
  274. addRepoToClose(git2.getRepository());
  275. assertNotNull(git2);
  276. assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
  277. assertEquals("refs/remotes/origin/master, refs/remotes/origin/test",
  278. allRefNames(git2.branchList().setListMode(ListMode.ALL).call()));
  279. // Same thing, but now test with bare repo
  280. directory = createTempDirectory("testCloneRepositoryWithBranch_bare");
  281. command = Git.cloneRepository();
  282. command.setBranch("refs/heads/master");
  283. command.setDirectory(directory);
  284. command.setURI(fileUri());
  285. command.setBare(true);
  286. git2 = command.call();
  287. addRepoToClose(git2.getRepository());
  288. assertNotNull(git2);
  289. assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
  290. assertEquals("refs/heads/master, refs/heads/test", allRefNames(git2
  291. .branchList().setListMode(ListMode.ALL).call()));
  292. }
  293. @Test
  294. public void testCloneRepositoryWithBranchShortName() throws Exception {
  295. File directory = createTempDirectory("testCloneRepositoryWithBranch");
  296. CloneCommand command = Git.cloneRepository();
  297. command.setBranch("test");
  298. command.setDirectory(directory);
  299. command.setURI(fileUri());
  300. Git git2 = command.call();
  301. addRepoToClose(git2.getRepository());
  302. assertNotNull(git2);
  303. assertEquals("refs/heads/test", git2.getRepository().getFullBranch());
  304. }
  305. @Test
  306. public void testCloneRepositoryWithTagName() throws Exception {
  307. File directory = createTempDirectory("testCloneRepositoryWithBranch");
  308. CloneCommand command = Git.cloneRepository();
  309. command.setBranch("tag-initial");
  310. command.setDirectory(directory);
  311. command.setURI(fileUri());
  312. Git git2 = command.call();
  313. addRepoToClose(git2.getRepository());
  314. assertNotNull(git2);
  315. ObjectId taggedCommit = db.resolve("tag-initial^{commit}");
  316. assertEquals(taggedCommit.name(), git2
  317. .getRepository().getFullBranch());
  318. }
  319. @Test
  320. public void testCloneRepositoryOnlyOneBranch() throws IOException,
  321. JGitInternalException, GitAPIException {
  322. File directory = createTempDirectory("testCloneRepositoryWithBranch");
  323. CloneCommand command = Git.cloneRepository();
  324. command.setBranch("refs/heads/master");
  325. command.setBranchesToClone(Collections
  326. .singletonList("refs/heads/master"));
  327. command.setDirectory(directory);
  328. command.setURI(fileUri());
  329. Git git2 = command.call();
  330. addRepoToClose(git2.getRepository());
  331. assertNotNull(git2);
  332. assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
  333. assertEquals("refs/remotes/origin/master", allRefNames(git2
  334. .branchList().setListMode(ListMode.REMOTE).call()));
  335. // Same thing, but now test with bare repo
  336. directory = createTempDirectory("testCloneRepositoryWithBranch_bare");
  337. command = Git.cloneRepository();
  338. command.setBranch("refs/heads/master");
  339. command.setBranchesToClone(Collections
  340. .singletonList("refs/heads/master"));
  341. command.setDirectory(directory);
  342. command.setURI(fileUri());
  343. command.setBare(true);
  344. git2 = command.call();
  345. addRepoToClose(git2.getRepository());
  346. assertNotNull(git2);
  347. assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
  348. assertEquals("refs/heads/master", allRefNames(git2.branchList()
  349. .setListMode(ListMode.ALL).call()));
  350. }
  351. public static String allRefNames(List<Ref> refs) {
  352. StringBuilder sb = new StringBuilder();
  353. for (Ref f : refs) {
  354. if (sb.length() > 0)
  355. sb.append(", ");
  356. sb.append(f.getName());
  357. }
  358. return sb.toString();
  359. }
  360. @Test
  361. public void testCloneRepositoryWhenDestinationDirectoryExistsAndIsNotEmpty()
  362. throws IOException, JGitInternalException, GitAPIException {
  363. String dirName = "testCloneTargetDirectoryNotEmpty";
  364. File directory = createTempDirectory(dirName);
  365. CloneCommand command = Git.cloneRepository();
  366. command.setDirectory(directory);
  367. command.setURI(fileUri());
  368. Git git2 = command.call();
  369. addRepoToClose(git2.getRepository());
  370. assertNotNull(git2);
  371. // clone again
  372. command = Git.cloneRepository();
  373. command.setDirectory(directory);
  374. command.setURI(fileUri());
  375. try {
  376. git2 = command.call();
  377. // we shouldn't get here
  378. fail("destination directory already exists and is not an empty folder, cloning should fail");
  379. } catch (JGitInternalException e) {
  380. assertTrue(e.getMessage().contains("not an empty directory"));
  381. assertTrue(e.getMessage().contains(dirName));
  382. }
  383. }
  384. @Test
  385. public void testCloneRepositoryWithMultipleHeadBranches() throws Exception {
  386. git.checkout().setName(Constants.MASTER).call();
  387. git.branchCreate().setName("a").call();
  388. File directory = createTempDirectory("testCloneRepositoryWithMultipleHeadBranches");
  389. CloneCommand clone = Git.cloneRepository();
  390. clone.setDirectory(directory);
  391. clone.setURI(fileUri());
  392. Git git2 = clone.call();
  393. addRepoToClose(git2.getRepository());
  394. assertNotNull(git2);
  395. assertEquals(Constants.MASTER, git2.getRepository().getBranch());
  396. }
  397. @Test
  398. public void testCloneRepositoryWithSubmodules() throws Exception {
  399. git.checkout().setName(Constants.MASTER).call();
  400. String file = "file.txt";
  401. writeTrashFile(file, "content");
  402. git.add().addFilepattern(file).call();
  403. RevCommit commit = git.commit().setMessage("create file").call();
  404. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  405. String path = "sub";
  406. command.setPath(path);
  407. String uri = db.getDirectory().toURI().toString();
  408. command.setURI(uri);
  409. Repository repo = command.call();
  410. assertNotNull(repo);
  411. addRepoToClose(repo);
  412. git.add().addFilepattern(path)
  413. .addFilepattern(Constants.DOT_GIT_MODULES).call();
  414. git.commit().setMessage("adding submodule").call();
  415. try (SubmoduleWalk walk = SubmoduleWalk.forIndex(git.getRepository())) {
  416. assertTrue(walk.next());
  417. Repository subRepo = walk.getRepository();
  418. addRepoToClose(subRepo);
  419. assertNotNull(subRepo);
  420. assertEquals(
  421. new File(git.getRepository().getWorkTree(), walk.getPath()),
  422. subRepo.getWorkTree());
  423. assertEquals(new File(new File(git.getRepository().getDirectory(),
  424. "modules"), walk.getPath()), subRepo.getDirectory());
  425. }
  426. File directory = createTempDirectory("testCloneRepositoryWithSubmodules");
  427. CloneCommand clone = Git.cloneRepository();
  428. clone.setDirectory(directory);
  429. clone.setCloneSubmodules(true);
  430. clone.setURI(fileUri());
  431. Git git2 = clone.call();
  432. addRepoToClose(git2.getRepository());
  433. assertNotNull(git2);
  434. assertEquals(Constants.MASTER, git2.getRepository().getBranch());
  435. assertTrue(new File(git2.getRepository().getWorkTree(), path
  436. + File.separatorChar + file).exists());
  437. SubmoduleStatusCommand status = new SubmoduleStatusCommand(
  438. git2.getRepository());
  439. Map<String, SubmoduleStatus> statuses = status.call();
  440. SubmoduleStatus pathStatus = statuses.get(path);
  441. assertNotNull(pathStatus);
  442. assertEquals(SubmoduleStatusType.INITIALIZED, pathStatus.getType());
  443. assertEquals(commit, pathStatus.getHeadId());
  444. assertEquals(commit, pathStatus.getIndexId());
  445. try (SubmoduleWalk walk = SubmoduleWalk
  446. .forIndex(git2.getRepository())) {
  447. assertTrue(walk.next());
  448. Repository clonedSub1 = walk.getRepository();
  449. addRepoToClose(clonedSub1);
  450. assertNotNull(clonedSub1);
  451. assertEquals(new File(git2.getRepository().getWorkTree(),
  452. walk.getPath()), clonedSub1.getWorkTree());
  453. assertEquals(
  454. new File(new File(git2.getRepository().getDirectory(),
  455. "modules"), walk.getPath()),
  456. clonedSub1.getDirectory());
  457. }
  458. }
  459. @Test
  460. public void testCloneRepositoryWithNestedSubmodules() throws Exception {
  461. git.checkout().setName(Constants.MASTER).call();
  462. // Create submodule 1
  463. File submodule1 = createTempDirectory("testCloneRepositoryWithNestedSubmodules1");
  464. Git sub1Git = Git.init().setDirectory(submodule1).call();
  465. assertNotNull(sub1Git);
  466. Repository sub1 = sub1Git.getRepository();
  467. assertNotNull(sub1);
  468. addRepoToClose(sub1);
  469. String file = "file.txt";
  470. String path = "sub";
  471. write(new File(sub1.getWorkTree(), file), "content");
  472. sub1Git.add().addFilepattern(file).call();
  473. RevCommit commit = sub1Git.commit().setMessage("create file").call();
  474. assertNotNull(commit);
  475. // Create submodule 2
  476. File submodule2 = createTempDirectory("testCloneRepositoryWithNestedSubmodules2");
  477. Git sub2Git = Git.init().setDirectory(submodule2).call();
  478. assertNotNull(sub2Git);
  479. Repository sub2 = sub2Git.getRepository();
  480. assertNotNull(sub2);
  481. addRepoToClose(sub2);
  482. write(new File(sub2.getWorkTree(), file), "content");
  483. sub2Git.add().addFilepattern(file).call();
  484. RevCommit sub2Head = sub2Git.commit().setMessage("create file").call();
  485. assertNotNull(sub2Head);
  486. // Add submodule 2 to submodule 1
  487. Repository r = sub1Git.submoduleAdd().setPath(path)
  488. .setURI(sub2.getDirectory().toURI().toString()).call();
  489. assertNotNull(r);
  490. addRepoToClose(r);
  491. RevCommit sub1Head = sub1Git.commit().setAll(true)
  492. .setMessage("Adding submodule").call();
  493. assertNotNull(sub1Head);
  494. // Add submodule 1 to default repository
  495. r = git.submoduleAdd().setPath(path)
  496. .setURI(sub1.getDirectory().toURI().toString()).call();
  497. assertNotNull(r);
  498. addRepoToClose(r);
  499. assertNotNull(git.commit().setAll(true).setMessage("Adding submodule")
  500. .call());
  501. // Clone default repository and include submodules
  502. File directory = createTempDirectory("testCloneRepositoryWithNestedSubmodules");
  503. CloneCommand clone = Git.cloneRepository();
  504. clone.setDirectory(directory);
  505. clone.setCloneSubmodules(true);
  506. clone.setURI(git.getRepository().getDirectory().toURI().toString());
  507. Git git2 = clone.call();
  508. addRepoToClose(git2.getRepository());
  509. assertNotNull(git2);
  510. assertEquals(Constants.MASTER, git2.getRepository().getBranch());
  511. assertTrue(new File(git2.getRepository().getWorkTree(), path
  512. + File.separatorChar + file).exists());
  513. assertTrue(new File(git2.getRepository().getWorkTree(), path
  514. + File.separatorChar + path + File.separatorChar + file)
  515. .exists());
  516. SubmoduleStatusCommand status = new SubmoduleStatusCommand(
  517. git2.getRepository());
  518. Map<String, SubmoduleStatus> statuses = status.call();
  519. SubmoduleStatus pathStatus = statuses.get(path);
  520. assertNotNull(pathStatus);
  521. assertEquals(SubmoduleStatusType.INITIALIZED, pathStatus.getType());
  522. assertEquals(sub1Head, pathStatus.getHeadId());
  523. assertEquals(sub1Head, pathStatus.getIndexId());
  524. SubmoduleWalk walk = SubmoduleWalk.forIndex(git2.getRepository());
  525. assertTrue(walk.next());
  526. Repository clonedSub1 = walk.getRepository();
  527. assertNotNull(clonedSub1);
  528. assertEquals(
  529. new File(git2.getRepository().getWorkTree(), walk.getPath()),
  530. clonedSub1.getWorkTree());
  531. assertEquals(new File(new File(git2.getRepository().getDirectory(),
  532. "modules"), walk.getPath()),
  533. clonedSub1.getDirectory());
  534. status = new SubmoduleStatusCommand(clonedSub1);
  535. statuses = status.call();
  536. clonedSub1.close();
  537. pathStatus = statuses.get(path);
  538. assertNotNull(pathStatus);
  539. assertEquals(SubmoduleStatusType.INITIALIZED, pathStatus.getType());
  540. assertEquals(sub2Head, pathStatus.getHeadId());
  541. assertEquals(sub2Head, pathStatus.getIndexId());
  542. assertFalse(walk.next());
  543. }
  544. @Test
  545. public void testCloneWithAutoSetupRebase() throws Exception {
  546. File directory = createTempDirectory("testCloneRepository1");
  547. CloneCommand command = Git.cloneRepository();
  548. command.setDirectory(directory);
  549. command.setURI(fileUri());
  550. Git git2 = command.call();
  551. addRepoToClose(git2.getRepository());
  552. assertFalse(git2
  553. .getRepository()
  554. .getConfig()
  555. .getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, "test",
  556. ConfigConstants.CONFIG_KEY_REBASE, false));
  557. FileBasedConfig userConfig = SystemReader.getInstance().openUserConfig(
  558. null, git.getRepository().getFS());
  559. userConfig.setString(ConfigConstants.CONFIG_BRANCH_SECTION, null,
  560. ConfigConstants.CONFIG_KEY_AUTOSETUPREBASE,
  561. ConfigConstants.CONFIG_KEY_ALWAYS);
  562. userConfig.save();
  563. directory = createTempDirectory("testCloneRepository2");
  564. command = Git.cloneRepository();
  565. command.setDirectory(directory);
  566. command.setURI(fileUri());
  567. git2 = command.call();
  568. addRepoToClose(git2.getRepository());
  569. assertTrue(git2
  570. .getRepository()
  571. .getConfig()
  572. .getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, "test",
  573. ConfigConstants.CONFIG_KEY_REBASE, false));
  574. userConfig.setString(ConfigConstants.CONFIG_BRANCH_SECTION, null,
  575. ConfigConstants.CONFIG_KEY_AUTOSETUPREBASE,
  576. ConfigConstants.CONFIG_KEY_REMOTE);
  577. userConfig.save();
  578. directory = createTempDirectory("testCloneRepository2");
  579. command = Git.cloneRepository();
  580. command.setDirectory(directory);
  581. command.setURI(fileUri());
  582. git2 = command.call();
  583. addRepoToClose(git2.getRepository());
  584. assertTrue(git2
  585. .getRepository()
  586. .getConfig()
  587. .getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, "test",
  588. ConfigConstants.CONFIG_KEY_REBASE, false));
  589. }
  590. private String fileUri() {
  591. return "file://" + git.getRepository().getWorkTree().getAbsolutePath();
  592. }
  593. }