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

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