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

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