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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * Copyright (C) 2011, 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.assertTrue;
  48. import static org.junit.Assert.fail;
  49. import java.io.File;
  50. import java.io.IOException;
  51. import java.util.Collections;
  52. import java.util.List;
  53. import java.util.Map;
  54. import org.eclipse.jgit.api.ListBranchCommand.ListMode;
  55. import org.eclipse.jgit.api.errors.JGitInternalException;
  56. import org.eclipse.jgit.junit.TestRepository;
  57. import org.eclipse.jgit.lib.ConfigConstants;
  58. import org.eclipse.jgit.lib.Constants;
  59. import org.eclipse.jgit.lib.ObjectId;
  60. import org.eclipse.jgit.lib.Ref;
  61. import org.eclipse.jgit.lib.RefUpdate;
  62. import org.eclipse.jgit.lib.Repository;
  63. import org.eclipse.jgit.lib.RepositoryTestCase;
  64. import org.eclipse.jgit.revwalk.RevBlob;
  65. import org.eclipse.jgit.revwalk.RevCommit;
  66. import org.eclipse.jgit.storage.file.FileBasedConfig;
  67. import org.eclipse.jgit.submodule.SubmoduleStatus;
  68. import org.eclipse.jgit.submodule.SubmoduleStatusType;
  69. import org.eclipse.jgit.submodule.SubmoduleWalk;
  70. import org.eclipse.jgit.util.SystemReader;
  71. import org.junit.Test;
  72. public class CloneCommandTest extends RepositoryTestCase {
  73. private Git git;
  74. private TestRepository<Repository> tr;
  75. public void setUp() throws Exception {
  76. super.setUp();
  77. tr = new TestRepository<Repository>(db);
  78. git = new Git(db);
  79. // commit something
  80. writeTrashFile("Test.txt", "Hello world");
  81. git.add().addFilepattern("Test.txt").call();
  82. git.commit().setMessage("Initial commit").call();
  83. // create a master branch and switch to it
  84. git.branchCreate().setName("test").call();
  85. RefUpdate rup = db.updateRef(Constants.HEAD);
  86. rup.link("refs/heads/test");
  87. // commit something on the test branch
  88. writeTrashFile("Test.txt", "Some change");
  89. git.add().addFilepattern("Test.txt").call();
  90. git.commit().setMessage("Second commit").call();
  91. RevBlob blob = tr.blob("blob-not-in-master-branch");
  92. git.tag().setName("tag-for-blob").setObjectId(blob).call();
  93. }
  94. @Test
  95. public void testCloneRepository() throws IOException {
  96. File directory = createTempDirectory("testCloneRepository");
  97. CloneCommand command = Git.cloneRepository();
  98. command.setDirectory(directory);
  99. command.setURI("file://" + git.getRepository().getWorkTree().getPath());
  100. Git git2 = command.call();
  101. addRepoToClose(git2.getRepository());
  102. assertNotNull(git2);
  103. ObjectId id = git2.getRepository().resolve("tag-for-blob");
  104. assertNotNull(id);
  105. assertEquals(git2.getRepository().getFullBranch(), "refs/heads/test");
  106. assertEquals(
  107. "origin",
  108. git2.getRepository()
  109. .getConfig()
  110. .getString(ConfigConstants.CONFIG_BRANCH_SECTION,
  111. "test", ConfigConstants.CONFIG_KEY_REMOTE));
  112. assertEquals(
  113. "refs/heads/test",
  114. git2.getRepository()
  115. .getConfig()
  116. .getString(ConfigConstants.CONFIG_BRANCH_SECTION,
  117. "test", ConfigConstants.CONFIG_KEY_MERGE));
  118. assertEquals(2, git2.branchList().setListMode(ListMode.REMOTE).call()
  119. .size());
  120. }
  121. @Test
  122. public void testCloneRepositoryWithBranch() throws IOException {
  123. File directory = createTempDirectory("testCloneRepositoryWithBranch");
  124. CloneCommand command = Git.cloneRepository();
  125. command.setBranch("refs/heads/master");
  126. command.setDirectory(directory);
  127. command.setURI("file://" + git.getRepository().getWorkTree().getPath());
  128. Git git2 = command.call();
  129. addRepoToClose(git2.getRepository());
  130. assertNotNull(git2);
  131. assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
  132. assertEquals(
  133. "refs/heads/master, refs/remotes/origin/master, refs/remotes/origin/test",
  134. allRefNames(git2.branchList().setListMode(ListMode.ALL).call()));
  135. // Same thing, but now without checkout
  136. directory = createTempDirectory("testCloneRepositoryWithBranch_bare");
  137. command = Git.cloneRepository();
  138. command.setBranch("refs/heads/master");
  139. command.setDirectory(directory);
  140. command.setURI("file://" + git.getRepository().getWorkTree().getPath());
  141. command.setNoCheckout(true);
  142. git2 = command.call();
  143. addRepoToClose(git2.getRepository());
  144. assertNotNull(git2);
  145. assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
  146. assertEquals("refs/remotes/origin/master, refs/remotes/origin/test",
  147. allRefNames(git2.branchList().setListMode(ListMode.ALL).call()));
  148. // Same thing, but now test with bare repo
  149. directory = createTempDirectory("testCloneRepositoryWithBranch_bare");
  150. command = Git.cloneRepository();
  151. command.setBranch("refs/heads/master");
  152. command.setDirectory(directory);
  153. command.setURI("file://" + git.getRepository().getWorkTree().getPath());
  154. command.setBare(true);
  155. git2 = command.call();
  156. addRepoToClose(git2.getRepository());
  157. assertNotNull(git2);
  158. assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
  159. assertEquals("refs/heads/master, refs/heads/test", allRefNames(git2
  160. .branchList().setListMode(ListMode.ALL).call()));
  161. }
  162. @Test
  163. public void testCloneRepositoryOnlyOneBranch() throws IOException {
  164. File directory = createTempDirectory("testCloneRepositoryWithBranch");
  165. CloneCommand command = Git.cloneRepository();
  166. command.setBranch("refs/heads/master");
  167. command.setBranchesToClone(Collections
  168. .singletonList("refs/heads/master"));
  169. command.setDirectory(directory);
  170. command.setURI("file://" + git.getRepository().getWorkTree().getPath());
  171. Git git2 = command.call();
  172. addRepoToClose(git2.getRepository());
  173. assertNotNull(git2);
  174. assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
  175. assertEquals("refs/remotes/origin/master", allRefNames(git2
  176. .branchList().setListMode(ListMode.REMOTE).call()));
  177. // Same thing, but now test with bare repo
  178. directory = createTempDirectory("testCloneRepositoryWithBranch_bare");
  179. command = Git.cloneRepository();
  180. command.setBranch("refs/heads/master");
  181. command.setBranchesToClone(Collections
  182. .singletonList("refs/heads/master"));
  183. command.setDirectory(directory);
  184. command.setURI("file://" + git.getRepository().getWorkTree().getPath());
  185. command.setBare(true);
  186. git2 = command.call();
  187. addRepoToClose(git2.getRepository());
  188. assertNotNull(git2);
  189. assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
  190. assertEquals("refs/heads/master", allRefNames(git2.branchList()
  191. .setListMode(ListMode.ALL).call()));
  192. }
  193. public static String allRefNames(List<Ref> refs) {
  194. StringBuilder sb = new StringBuilder();
  195. for (Ref f : refs) {
  196. if (sb.length() > 0)
  197. sb.append(", ");
  198. sb.append(f.getName());
  199. }
  200. return sb.toString();
  201. }
  202. @Test
  203. public void testCloneRepositoryWhenDestinationDirectoryExistsAndIsNotEmpty()
  204. throws IOException {
  205. String dirName = "testCloneTargetDirectoryNotEmpty";
  206. File directory = createTempDirectory(dirName);
  207. CloneCommand command = Git.cloneRepository();
  208. command.setDirectory(directory);
  209. command.setURI("file://" + git.getRepository().getWorkTree().getPath());
  210. Git git2 = command.call();
  211. addRepoToClose(git2.getRepository());
  212. assertNotNull(git2);
  213. // clone again
  214. command = Git.cloneRepository();
  215. command.setDirectory(directory);
  216. command.setURI("file://" + git.getRepository().getWorkTree().getPath());
  217. try {
  218. git2 = command.call();
  219. // we shouldn't get here
  220. fail("destination directory already exists and is not an empty folder, cloning should fail");
  221. } catch (JGitInternalException e) {
  222. assertTrue(e.getMessage().contains("not an empty directory"));
  223. assertTrue(e.getMessage().contains(dirName));
  224. }
  225. }
  226. @Test
  227. public void testCloneRepositoryWithMultipleHeadBranches() throws Exception {
  228. git.checkout().setName(Constants.MASTER).call();
  229. git.branchCreate().setName("a").call();
  230. File directory = createTempDirectory("testCloneRepositoryWithMultipleHeadBranches");
  231. CloneCommand clone = Git.cloneRepository();
  232. clone.setDirectory(directory);
  233. clone.setURI("file://" + git.getRepository().getWorkTree().getPath());
  234. Git git2 = clone.call();
  235. addRepoToClose(git2.getRepository());
  236. assertNotNull(git2);
  237. assertEquals(Constants.MASTER, git2.getRepository().getBranch());
  238. }
  239. @Test
  240. public void testCloneRepositoryWithSubmodules() throws Exception {
  241. git.checkout().setName(Constants.MASTER).call();
  242. String file = "file.txt";
  243. writeTrashFile(file, "content");
  244. git.add().addFilepattern(file).call();
  245. RevCommit commit = git.commit().setMessage("create file").call();
  246. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  247. String path = "sub";
  248. command.setPath(path);
  249. String uri = db.getDirectory().toURI().toString();
  250. command.setURI(uri);
  251. Repository repo = command.call();
  252. assertNotNull(repo);
  253. git.add().addFilepattern(path)
  254. .addFilepattern(Constants.DOT_GIT_MODULES).call();
  255. git.commit().setMessage("adding submodule").call();
  256. File directory = createTempDirectory("testCloneRepositoryWithSubmodules");
  257. CloneCommand clone = Git.cloneRepository();
  258. clone.setDirectory(directory);
  259. clone.setCloneSubmodules(true);
  260. clone.setURI("file://" + git.getRepository().getWorkTree().getPath());
  261. Git git2 = clone.call();
  262. addRepoToClose(git2.getRepository());
  263. assertNotNull(git2);
  264. assertEquals(Constants.MASTER, git2.getRepository().getBranch());
  265. assertTrue(new File(git2.getRepository().getWorkTree(), path
  266. + File.separatorChar + file).exists());
  267. SubmoduleStatusCommand status = new SubmoduleStatusCommand(
  268. git2.getRepository());
  269. Map<String, SubmoduleStatus> statuses = status.call();
  270. SubmoduleStatus pathStatus = statuses.get(path);
  271. assertNotNull(pathStatus);
  272. assertEquals(SubmoduleStatusType.INITIALIZED, pathStatus.getType());
  273. assertEquals(commit, pathStatus.getHeadId());
  274. assertEquals(commit, pathStatus.getIndexId());
  275. }
  276. @Test
  277. public void testCloneRepositoryWithNestedSubmodules() throws Exception {
  278. git.checkout().setName(Constants.MASTER).call();
  279. // Create submodule 1
  280. File submodule1 = createTempDirectory("testCloneRepositoryWithNestedSubmodules1");
  281. Git sub1Git = Git.init().setDirectory(submodule1).call();
  282. assertNotNull(sub1Git);
  283. Repository sub1 = sub1Git.getRepository();
  284. assertNotNull(sub1);
  285. addRepoToClose(sub1);
  286. String file = "file.txt";
  287. String path = "sub";
  288. write(new File(sub1.getWorkTree(), file), "content");
  289. sub1Git.add().addFilepattern(file).call();
  290. RevCommit commit = sub1Git.commit().setMessage("create file").call();
  291. assertNotNull(commit);
  292. // Create submodule 2
  293. File submodule2 = createTempDirectory("testCloneRepositoryWithNestedSubmodules2");
  294. Git sub2Git = Git.init().setDirectory(submodule2).call();
  295. assertNotNull(sub2Git);
  296. Repository sub2 = sub2Git.getRepository();
  297. assertNotNull(sub2);
  298. addRepoToClose(sub2);
  299. write(new File(sub2.getWorkTree(), file), "content");
  300. sub2Git.add().addFilepattern(file).call();
  301. RevCommit sub2Head = sub2Git.commit().setMessage("create file").call();
  302. assertNotNull(sub2Head);
  303. // Add submodule 2 to submodule 1
  304. assertNotNull(sub1Git.submoduleAdd().setPath(path)
  305. .setURI(sub2.getDirectory().toURI().toString()).call());
  306. RevCommit sub1Head = sub1Git.commit().setAll(true)
  307. .setMessage("Adding submodule").call();
  308. assertNotNull(sub1Head);
  309. // Add submodule 1 to default repository
  310. assertNotNull(git.submoduleAdd().setPath(path)
  311. .setURI(sub1.getDirectory().toURI().toString()).call());
  312. assertNotNull(git.commit().setAll(true).setMessage("Adding submodule")
  313. .call());
  314. // Clone default repository and include submodules
  315. File directory = createTempDirectory("testCloneRepositoryWithNestedSubmodules");
  316. CloneCommand clone = Git.cloneRepository();
  317. clone.setDirectory(directory);
  318. clone.setCloneSubmodules(true);
  319. clone.setURI(git.getRepository().getDirectory().toURI().toString());
  320. Git git2 = clone.call();
  321. addRepoToClose(git2.getRepository());
  322. assertNotNull(git2);
  323. assertEquals(Constants.MASTER, git2.getRepository().getBranch());
  324. assertTrue(new File(git2.getRepository().getWorkTree(), path
  325. + File.separatorChar + file).exists());
  326. assertTrue(new File(git2.getRepository().getWorkTree(), path
  327. + File.separatorChar + path + File.separatorChar + file)
  328. .exists());
  329. SubmoduleStatusCommand status = new SubmoduleStatusCommand(
  330. git2.getRepository());
  331. Map<String, SubmoduleStatus> statuses = status.call();
  332. SubmoduleStatus pathStatus = statuses.get(path);
  333. assertNotNull(pathStatus);
  334. assertEquals(SubmoduleStatusType.INITIALIZED, pathStatus.getType());
  335. assertEquals(sub1Head, pathStatus.getHeadId());
  336. assertEquals(sub1Head, pathStatus.getIndexId());
  337. SubmoduleWalk walk = SubmoduleWalk.forIndex(git2.getRepository());
  338. assertTrue(walk.next());
  339. Repository clonedSub1 = walk.getRepository();
  340. assertNotNull(clonedSub1);
  341. status = new SubmoduleStatusCommand(clonedSub1);
  342. statuses = status.call();
  343. pathStatus = statuses.get(path);
  344. assertNotNull(pathStatus);
  345. assertEquals(SubmoduleStatusType.INITIALIZED, pathStatus.getType());
  346. assertEquals(sub2Head, pathStatus.getHeadId());
  347. assertEquals(sub2Head, pathStatus.getIndexId());
  348. assertFalse(walk.next());
  349. }
  350. @Test
  351. public void testCloneWithAutoSetupRebase() throws Exception {
  352. File directory = createTempDirectory("testCloneRepository1");
  353. CloneCommand command = Git.cloneRepository();
  354. command.setDirectory(directory);
  355. command.setURI("file://" + git.getRepository().getWorkTree().getPath());
  356. Git git2 = command.call();
  357. addRepoToClose(git2.getRepository());
  358. assertFalse(git2
  359. .getRepository()
  360. .getConfig()
  361. .getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, "test",
  362. ConfigConstants.CONFIG_KEY_REBASE, false));
  363. FileBasedConfig userConfig = SystemReader.getInstance().openUserConfig(
  364. null, git.getRepository().getFS());
  365. userConfig.setString(ConfigConstants.CONFIG_BRANCH_SECTION, null,
  366. ConfigConstants.CONFIG_KEY_AUTOSETUPREBASE,
  367. ConfigConstants.CONFIG_KEY_ALWAYS);
  368. userConfig.save();
  369. directory = createTempDirectory("testCloneRepository2");
  370. command = Git.cloneRepository();
  371. command.setDirectory(directory);
  372. command.setURI("file://" + git.getRepository().getWorkTree().getPath());
  373. git2 = command.call();
  374. addRepoToClose(git2.getRepository());
  375. assertTrue(git2
  376. .getRepository()
  377. .getConfig()
  378. .getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, "test",
  379. ConfigConstants.CONFIG_KEY_REBASE, false));
  380. userConfig.setString(ConfigConstants.CONFIG_BRANCH_SECTION, null,
  381. ConfigConstants.CONFIG_KEY_AUTOSETUPREBASE,
  382. ConfigConstants.CONFIG_KEY_REMOTE);
  383. userConfig.save();
  384. directory = createTempDirectory("testCloneRepository2");
  385. command = Git.cloneRepository();
  386. command.setDirectory(directory);
  387. command.setURI("file://" + git.getRepository().getWorkTree().getPath());
  388. git2 = command.call();
  389. addRepoToClose(git2.getRepository());
  390. assertTrue(git2
  391. .getRepository()
  392. .getConfig()
  393. .getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, "test",
  394. ConfigConstants.CONFIG_KEY_REBASE, false));
  395. }
  396. }