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