View Javadoc
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  
45  import static org.junit.Assert.assertArrayEquals;
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertTrue;
48  
49  import java.io.File;
50  import java.util.List;
51  
52  import org.eclipse.jgit.api.Git;
53  import org.eclipse.jgit.junit.JGitTestUtil;
54  import org.eclipse.jgit.junit.MockSystemReader;
55  import org.eclipse.jgit.lib.CLIRepositoryTestCase;
56  import org.eclipse.jgit.lib.Constants;
57  import org.eclipse.jgit.lib.Ref;
58  import org.eclipse.jgit.transport.URIish;
59  import org.eclipse.jgit.util.SystemReader;
60  import org.junit.Before;
61  import org.junit.Test;
62  
63  public class CloneTest extends CLIRepositoryTestCase {
64  
65  	private Git git;
66  
67  	@Override
68  	@Before
69  	public void setUp() throws Exception {
70  		super.setUp();
71  		git = new Git(db);
72  	}
73  
74  	@Test
75  	public void testClone() throws Exception {
76  		createInitialCommit();
77  
78  		File gitDir = db.getDirectory();
79  		String sourceURI = gitDir.toURI().toString();
80  		File target = createTempDirectory("target");
81  		StringBuilder cmd = new StringBuilder("git clone ").append(sourceURI
82  				+ " " + target.getPath());
83  		String[] result = execute(cmd.toString());
84  		assertArrayEquals(new String[] {
85  				"Cloning into '" + target.getPath() + "'...",
86  						"", "" }, result);
87  
88  		Git git2 = Git.open(target);
89  		List<Ref> branches = git2.branchList().call();
90  		assertEquals("expected 1 branch", 1, branches.size());
91  	}
92  
93  	private void createInitialCommit() throws Exception {
94  		JGitTestUtil.writeTrashFile(db, "hello.txt", "world");
95  		git.add().addFilepattern("hello.txt").call();
96  		git.commit().setMessage("Initial commit").call();
97  	}
98  
99  	@Test
100 	public void testCloneEmpty() throws Exception {
101 		File gitDir = db.getDirectory();
102 		String sourceURI = gitDir.toURI().toString();
103 		File target = createTempDirectory("target");
104 		StringBuilder cmd = new StringBuilder("git clone ").append(sourceURI
105 				+ " " + target.getPath());
106 		String[] result = execute(cmd.toString());
107 		assertArrayEquals(new String[] {
108 				"Cloning into '" + target.getPath() + "'...",
109 				"warning: You appear to have cloned an empty repository.", "",
110 				"" }, result);
111 
112 		Git git2 = Git.open(target);
113 		List<Ref> branches = git2.branchList().call();
114 		assertEquals("expected 0 branch", 0, branches.size());
115 	}
116 
117 	@Test
118 	public void testCloneIntoCurrentDir() throws Exception {
119 		createInitialCommit();
120 		File target = createTempDirectory("target");
121 
122 		MockSystemReader sr = (MockSystemReader) SystemReader.getInstance();
123 		sr.setProperty(Constants.OS_USER_DIR, target.getAbsolutePath());
124 
125 		File gitDir = db.getDirectory();
126 		String sourceURI = gitDir.toURI().toString();
127 		String name = new URIish(sourceURI).getHumanishName();
128 		StringBuilder cmd = new StringBuilder("git clone ").append(sourceURI);
129 		String[] result = execute(cmd.toString());
130 		assertArrayEquals(new String[] {
131 				"Cloning into '" + new File(target, name).getName() + "'...",
132 				"", "" }, result);
133 		Git git2 = Git.open(new File(target, name));
134 		List<Ref> branches = git2.branchList().call();
135 		assertEquals("expected 1 branch", 1, branches.size());
136 	}
137 
138 	@Test
139 	public void testCloneBare() throws Exception {
140 		createInitialCommit();
141 
142 		File gitDir = db.getDirectory();
143 		String sourcePath = gitDir.getAbsolutePath();
144 		String targetPath = (new File(sourcePath)).getParentFile()
145 				.getParentFile().getAbsolutePath()
146 				+ "/target.git";
147 		StringBuilder cmd = new StringBuilder("git clone --bare ")
148 				.append(sourcePath + " " + targetPath);
149 		String[] result = execute(cmd.toString());
150 		assertArrayEquals(new String[] {
151 				"Cloning into '" + targetPath + "'...", "", "" }, result);
152 		Git git2 = Git.open(new File(targetPath));
153 		List<Ref> branches = git2.branchList().call();
154 		assertEquals("expected 1 branch", 1, branches.size());
155 		assertTrue("expected bare repository", git2.getRepository().isBare());
156 	}
157 }