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