Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

CloneCommandTest.java 29KB

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