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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.assertNotNull;
  46. import java.io.File;
  47. import java.io.IOException;
  48. import java.util.Collections;
  49. import java.util.List;
  50. import org.eclipse.jgit.api.ListBranchCommand.ListMode;
  51. import org.eclipse.jgit.junit.TestRepository;
  52. import org.eclipse.jgit.lib.ConfigConstants;
  53. import org.eclipse.jgit.lib.Constants;
  54. import org.eclipse.jgit.lib.ObjectId;
  55. import org.eclipse.jgit.lib.Ref;
  56. import org.eclipse.jgit.lib.RefUpdate;
  57. import org.eclipse.jgit.lib.Repository;
  58. import org.eclipse.jgit.lib.RepositoryTestCase;
  59. import org.eclipse.jgit.revwalk.RevBlob;
  60. import org.junit.Test;
  61. public class CloneCommandTest extends RepositoryTestCase {
  62. private Git git;
  63. private TestRepository<Repository> tr;
  64. public void setUp() throws Exception {
  65. super.setUp();
  66. tr = new TestRepository<Repository>(db);
  67. git = new Git(db);
  68. // commit something
  69. writeTrashFile("Test.txt", "Hello world");
  70. git.add().addFilepattern("Test.txt").call();
  71. git.commit().setMessage("Initial commit").call();
  72. // create a master branch and switch to it
  73. git.branchCreate().setName("test").call();
  74. RefUpdate rup = db.updateRef(Constants.HEAD);
  75. rup.link("refs/heads/test");
  76. // commit something on the test branch
  77. writeTrashFile("Test.txt", "Some change");
  78. git.add().addFilepattern("Test.txt").call();
  79. git.commit().setMessage("Second commit").call();
  80. RevBlob blob = tr.blob("blob-not-in-master-branch");
  81. git.tag().setName("tag-for-blob").setObjectId(blob).call();
  82. }
  83. @Test
  84. public void testCloneRepository() throws IOException {
  85. File directory = createTempDirectory("testCloneRepository");
  86. CloneCommand command = Git.cloneRepository();
  87. command.setDirectory(directory);
  88. command.setURI("file://" + git.getRepository().getWorkTree().getPath());
  89. Git git2 = command.call();
  90. addRepoToClose(git2.getRepository());
  91. assertNotNull(git2);
  92. ObjectId id = git2.getRepository().resolve("tag-for-blob");
  93. assertNotNull(id);
  94. assertEquals(git2.getRepository().getFullBranch(), "refs/heads/test");
  95. assertEquals(
  96. "origin",
  97. git2.getRepository()
  98. .getConfig()
  99. .getString(ConfigConstants.CONFIG_BRANCH_SECTION,
  100. "test", ConfigConstants.CONFIG_KEY_REMOTE));
  101. assertEquals(
  102. "refs/heads/test",
  103. git2.getRepository()
  104. .getConfig()
  105. .getString(ConfigConstants.CONFIG_BRANCH_SECTION,
  106. "test", ConfigConstants.CONFIG_KEY_MERGE));
  107. assertEquals(2, git2.branchList().setListMode(ListMode.REMOTE).call()
  108. .size());
  109. }
  110. @Test
  111. public void testCloneRepositoryWithBranch() throws IOException {
  112. File directory = createTempDirectory("testCloneRepositoryWithBranch");
  113. CloneCommand command = Git.cloneRepository();
  114. command.setBranch("refs/heads/master");
  115. command.setDirectory(directory);
  116. command.setURI("file://" + git.getRepository().getWorkTree().getPath());
  117. Git git2 = command.call();
  118. addRepoToClose(git2.getRepository());
  119. assertNotNull(git2);
  120. assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
  121. assertEquals(
  122. "refs/heads/master, refs/remotes/origin/master, refs/remotes/origin/test",
  123. allRefNames(git2.branchList().setListMode(ListMode.ALL).call()));
  124. // Same thing, but now without checkout
  125. directory = createTempDirectory("testCloneRepositoryWithBranch_bare");
  126. command = Git.cloneRepository();
  127. command.setBranch("refs/heads/master");
  128. command.setDirectory(directory);
  129. command.setURI("file://" + git.getRepository().getWorkTree().getPath());
  130. command.setNoCheckout(true);
  131. git2 = command.call();
  132. addRepoToClose(git2.getRepository());
  133. assertNotNull(git2);
  134. assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
  135. assertEquals("refs/remotes/origin/master, refs/remotes/origin/test",
  136. allRefNames(git2.branchList().setListMode(ListMode.ALL).call()));
  137. // Same thing, but now test with bare repo
  138. directory = createTempDirectory("testCloneRepositoryWithBranch_bare");
  139. command = Git.cloneRepository();
  140. command.setBranch("refs/heads/master");
  141. command.setDirectory(directory);
  142. command.setURI("file://" + git.getRepository().getWorkTree().getPath());
  143. command.setBare(true);
  144. git2 = command.call();
  145. addRepoToClose(git2.getRepository());
  146. assertNotNull(git2);
  147. assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
  148. assertEquals("refs/heads/master, refs/heads/test", allRefNames(git2
  149. .branchList().setListMode(ListMode.ALL).call()));
  150. }
  151. @Test
  152. public void testCloneRepositoryOnlyOneBranch() throws IOException {
  153. File directory = createTempDirectory("testCloneRepositoryWithBranch");
  154. CloneCommand command = Git.cloneRepository();
  155. command.setBranch("refs/heads/master");
  156. command.setBranchesToClone(Collections
  157. .singletonList("refs/heads/master"));
  158. command.setDirectory(directory);
  159. command.setURI("file://" + git.getRepository().getWorkTree().getPath());
  160. Git git2 = command.call();
  161. addRepoToClose(git2.getRepository());
  162. assertNotNull(git2);
  163. assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
  164. assertEquals("refs/remotes/origin/master", allRefNames(git2
  165. .branchList().setListMode(ListMode.REMOTE).call()));
  166. // Same thing, but now test with bare repo
  167. directory = createTempDirectory("testCloneRepositoryWithBranch_bare");
  168. command = Git.cloneRepository();
  169. command.setBranch("refs/heads/master");
  170. command.setBranchesToClone(Collections
  171. .singletonList("refs/heads/master"));
  172. command.setDirectory(directory);
  173. command.setURI("file://" + git.getRepository().getWorkTree().getPath());
  174. command.setBare(true);
  175. git2 = command.call();
  176. addRepoToClose(git2.getRepository());
  177. assertNotNull(git2);
  178. assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
  179. assertEquals("refs/heads/master", allRefNames(git2.branchList()
  180. .setListMode(ListMode.ALL).call()));
  181. }
  182. public static String allRefNames(List<Ref> refs) {
  183. StringBuilder sb = new StringBuilder();
  184. for (Ref f : refs) {
  185. if (sb.length() > 0)
  186. sb.append(", ");
  187. sb.append(f.getName());
  188. }
  189. return sb.toString();
  190. }
  191. public static File createTempDirectory(String name) throws IOException {
  192. final File temp;
  193. temp = File.createTempFile(name, Long.toString(System.nanoTime()));
  194. if (!(temp.delete())) {
  195. throw new IOException("Could not delete temp file: "
  196. + temp.getAbsolutePath());
  197. }
  198. if (!(temp.mkdir())) {
  199. throw new IOException("Could not create temp directory: "
  200. + temp.getAbsolutePath());
  201. }
  202. return temp;
  203. }
  204. }