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

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