1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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 }