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.2KB

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