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.

CloneTest.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (C) 2014 Matthias Sohn <matthias.sohn@sap.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.pgm;
  11. import static org.junit.Assert.assertArrayEquals;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertNotEquals;
  14. import static org.junit.Assert.assertNotNull;
  15. import static org.junit.Assert.assertThrows;
  16. import static org.junit.Assert.assertTrue;
  17. import java.io.File;
  18. import java.util.List;
  19. import org.eclipse.jgit.api.Git;
  20. import org.eclipse.jgit.junit.JGitTestUtil;
  21. import org.eclipse.jgit.junit.MockSystemReader;
  22. import org.eclipse.jgit.lib.CLIRepositoryTestCase;
  23. import org.eclipse.jgit.lib.Constants;
  24. import org.eclipse.jgit.lib.ObjectId;
  25. import org.eclipse.jgit.lib.Ref;
  26. import org.eclipse.jgit.lib.RefUpdate;
  27. import org.eclipse.jgit.lib.Repository;
  28. import org.eclipse.jgit.lib.StoredConfig;
  29. import org.eclipse.jgit.revwalk.RevCommit;
  30. import org.eclipse.jgit.transport.RefSpec;
  31. import org.eclipse.jgit.transport.RemoteConfig;
  32. import org.eclipse.jgit.transport.URIish;
  33. import org.eclipse.jgit.util.SystemReader;
  34. import org.junit.Before;
  35. import org.junit.Test;
  36. public class CloneTest extends CLIRepositoryTestCase {
  37. private Git git;
  38. @Override
  39. @Before
  40. public void setUp() throws Exception {
  41. super.setUp();
  42. git = new Git(db);
  43. }
  44. @Test
  45. public void testClone() throws Exception {
  46. createInitialCommit();
  47. File gitDir = db.getDirectory();
  48. String sourceURI = gitDir.toURI().toString();
  49. File target = createTempDirectory("target");
  50. String cmd = "git clone " + sourceURI + " "
  51. + shellQuote(target.getPath());
  52. String[] result = execute(cmd);
  53. assertArrayEquals(new String[] {
  54. "Cloning into '" + target.getPath() + "'...",
  55. "", "" }, result);
  56. Git git2 = Git.open(target);
  57. List<Ref> branches = git2.branchList().call();
  58. assertEquals("expected 1 branch", 1, branches.size());
  59. }
  60. @Test
  61. public void testCloneInitialBranch() throws Exception {
  62. createInitialCommit();
  63. File gitDir = db.getDirectory();
  64. String sourceURI = gitDir.toURI().toString();
  65. File target = createTempDirectory("target");
  66. String cmd = "git clone --branch master " + sourceURI + " "
  67. + shellQuote(target.getPath());
  68. String[] result = execute(cmd);
  69. assertArrayEquals(new String[] {
  70. "Cloning into '" + target.getPath() + "'...", "", "" }, result);
  71. Git git2 = Git.open(target);
  72. List<Ref> branches = git2.branchList().call();
  73. assertEquals("expected 1 branch", 1, branches.size());
  74. Repository db2 = git2.getRepository();
  75. ObjectId head = db2.resolve("HEAD");
  76. assertNotNull(head);
  77. assertNotEquals(ObjectId.zeroId(), head);
  78. ObjectId master = db2.resolve("master");
  79. assertEquals(head, master);
  80. }
  81. @Test
  82. public void testCloneInitialBranchMissing() throws Exception {
  83. createInitialCommit();
  84. File gitDir = db.getDirectory();
  85. String sourceURI = gitDir.toURI().toString();
  86. File target = createTempDirectory("target");
  87. String cmd = "git clone --branch foo " + sourceURI + " "
  88. + shellQuote(target.getPath());
  89. Die e = assertThrows(Die.class, () -> execute(cmd));
  90. assertEquals("Remote branch 'foo' not found in upstream origin",
  91. e.getMessage());
  92. }
  93. private RevCommit createInitialCommit() throws Exception {
  94. JGitTestUtil.writeTrashFile(db, "hello.txt", "world");
  95. git.add().addFilepattern("hello.txt").call();
  96. return git.commit().setMessage("Initial commit").call();
  97. }
  98. @Test
  99. public void testCloneEmpty() throws Exception {
  100. File gitDir = db.getDirectory();
  101. String sourceURI = gitDir.toURI().toString();
  102. File target = createTempDirectory("target");
  103. String cmd = "git clone " + sourceURI + " "
  104. + shellQuote(target.getPath());
  105. String[] result = execute(cmd);
  106. assertArrayEquals(new String[] {
  107. "Cloning into '" + target.getPath() + "'...",
  108. "warning: You appear to have cloned an empty repository.", "",
  109. "" }, result);
  110. Git git2 = Git.open(target);
  111. List<Ref> branches = git2.branchList().call();
  112. assertEquals("expected 0 branch", 0, branches.size());
  113. }
  114. @Test
  115. public void testCloneIntoCurrentDir() throws Exception {
  116. createInitialCommit();
  117. File target = createTempDirectory("target");
  118. MockSystemReader sr = (MockSystemReader) SystemReader.getInstance();
  119. sr.setProperty(Constants.OS_USER_DIR, target.getAbsolutePath());
  120. File gitDir = db.getDirectory();
  121. String sourceURI = gitDir.toURI().toString();
  122. String name = new URIish(sourceURI).getHumanishName();
  123. String cmd = "git clone " + sourceURI;
  124. String[] result = execute(cmd);
  125. assertArrayEquals(new String[] {
  126. "Cloning into '" + new File(target, name).getName() + "'...",
  127. "", "" }, result);
  128. Git git2 = Git.open(new File(target, name));
  129. List<Ref> branches = git2.branchList().call();
  130. assertEquals("expected 1 branch", 1, branches.size());
  131. }
  132. @Test
  133. public void testCloneBare() throws Exception {
  134. createInitialCommit();
  135. File gitDir = db.getDirectory();
  136. String sourcePath = gitDir.getAbsolutePath();
  137. String targetPath = (new File(sourcePath)).getParentFile()
  138. .getParentFile().getAbsolutePath()
  139. + File.separator + "target.git";
  140. String cmd = "git clone --bare " + shellQuote(sourcePath) + " "
  141. + shellQuote(targetPath);
  142. String[] result = execute(cmd);
  143. assertArrayEquals(new String[] {
  144. "Cloning into '" + targetPath + "'...", "", "" }, result);
  145. Git git2 = Git.open(new File(targetPath));
  146. List<Ref> branches = git2.branchList().call();
  147. assertEquals("expected 1 branch", 1, branches.size());
  148. assertTrue("expected bare repository", git2.getRepository().isBare());
  149. }
  150. @Test
  151. public void testCloneMirror() throws Exception {
  152. ObjectId head = createInitialCommit();
  153. // create a non-standard ref
  154. RefUpdate ru = db.updateRef("refs/meta/foo/bar");
  155. ru.setNewObjectId(head);
  156. ru.update();
  157. File gitDir = db.getDirectory();
  158. String sourcePath = gitDir.getAbsolutePath();
  159. String targetPath = (new File(sourcePath)).getParentFile()
  160. .getParentFile().getAbsolutePath() + File.separator
  161. + "target.git";
  162. String cmd = "git clone --mirror " + shellQuote(sourcePath) + " "
  163. + shellQuote(targetPath);
  164. String[] result = execute(cmd);
  165. assertArrayEquals(
  166. new String[] { "Cloning into '" + targetPath + "'...", "", "" },
  167. result);
  168. Git git2 = Git.open(new File(targetPath));
  169. List<Ref> branches = git2.branchList().call();
  170. assertEquals("expected 1 branch", 1, branches.size());
  171. assertTrue("expected bare repository", git2.getRepository().isBare());
  172. StoredConfig config = git2.getRepository().getConfig();
  173. RemoteConfig rc = new RemoteConfig(config, "origin");
  174. assertTrue("expected mirror configuration", rc.isMirror());
  175. RefSpec fetchRefSpec = rc.getFetchRefSpecs().get(0);
  176. assertTrue("exected force udpate", fetchRefSpec.isForceUpdate());
  177. assertEquals("refs/*", fetchRefSpec.getSource());
  178. assertEquals("refs/*", fetchRefSpec.getDestination());
  179. assertNotNull(git2.getRepository().exactRef("refs/meta/foo/bar"));
  180. }
  181. }