1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.api;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16
17 import java.io.File;
18 import java.io.IOException;
19
20 import org.eclipse.jgit.api.errors.GitAPIException;
21 import org.eclipse.jgit.api.errors.JGitInternalException;
22 import org.eclipse.jgit.errors.NoWorkTreeException;
23 import org.eclipse.jgit.junit.MockSystemReader;
24 import org.eclipse.jgit.junit.RepositoryTestCase;
25 import org.eclipse.jgit.lib.ConfigConstants;
26 import org.eclipse.jgit.lib.Constants;
27 import org.eclipse.jgit.lib.Repository;
28 import org.eclipse.jgit.lib.StoredConfig;
29 import org.eclipse.jgit.util.SystemReader;
30 import org.junit.Before;
31 import org.junit.Test;
32
33 public class InitCommandTest extends RepositoryTestCase {
34
35 @Override
36 @Before
37 public void setUp() throws Exception {
38 super.setUp();
39 }
40
41 @Test
42 public void testInitRepository()
43 throws IOException, JGitInternalException, GitAPIException {
44 File directory = createTempDirectory("testInitRepository");
45 InitCommand command = new InitCommand();
46 command.setDirectory(directory);
47 try (Git git = command.call()) {
48 Repository r = git.getRepository();
49 assertNotNull(r);
50 assertEquals("refs/heads/master", r.getFullBranch());
51 }
52 }
53
54 @Test
55 public void testInitRepositoryMainInitialBranch()
56 throws IOException, JGitInternalException, GitAPIException {
57 File directory = createTempDirectory("testInitRepository");
58 InitCommand command = new InitCommand();
59 command.setDirectory(directory);
60 command.setInitialBranch("main");
61 try (Git git = command.call()) {
62 Repository r = git.getRepository();
63 assertNotNull(r);
64 assertEquals("refs/heads/main", r.getFullBranch());
65 }
66 }
67
68 @Test
69 public void testInitRepositoryCustomDefaultBranch()
70 throws Exception {
71 File directory = createTempDirectory("testInitRepository");
72 InitCommand command = new InitCommand();
73 command.setDirectory(directory);
74 MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
75 StoredConfig c = reader.getUserConfig();
76 String old = c.getString(ConfigConstants.CONFIG_INIT_SECTION, null,
77 ConfigConstants.CONFIG_KEY_DEFAULT_BRANCH);
78 c.setString(ConfigConstants.CONFIG_INIT_SECTION, null,
79 ConfigConstants.CONFIG_KEY_DEFAULT_BRANCH, "main");
80 try (Git git = command.call()) {
81 Repository r = git.getRepository();
82 assertNotNull(r);
83 assertEquals("refs/heads/main", r.getFullBranch());
84 } finally {
85 c.setString(ConfigConstants.CONFIG_INIT_SECTION, null,
86 ConfigConstants.CONFIG_KEY_DEFAULT_BRANCH, old);
87 }
88 }
89
90 @Test
91 public void testInitRepositoryNullInitialBranch() throws Exception {
92 File directory = createTempDirectory("testInitRepository");
93 InitCommand command = new InitCommand();
94 command.setDirectory(directory);
95 command.setInitialBranch("main");
96 command.setInitialBranch(null);
97 try (Git git = command.call()) {
98 Repository r = git.getRepository();
99 assertNotNull(r);
100 assertEquals("refs/heads/master", r.getFullBranch());
101 }
102 }
103
104 @Test
105 public void testInitRepositoryEmptyInitialBranch() throws Exception {
106 File directory = createTempDirectory("testInitRepository");
107 InitCommand command = new InitCommand();
108 command.setDirectory(directory);
109 command.setInitialBranch("main");
110 command.setInitialBranch("");
111 try (Git git = command.call()) {
112 Repository r = git.getRepository();
113 assertNotNull(r);
114 assertEquals("refs/heads/master", r.getFullBranch());
115 }
116 }
117
118 @Test
119 public void testInitNonEmptyRepository() throws IOException,
120 JGitInternalException, GitAPIException {
121 File directory = createTempDirectory("testInitRepository2");
122 File someFile = new File(directory, "someFile");
123 someFile.createNewFile();
124 assertTrue(someFile.exists());
125 assertTrue(directory.listFiles().length > 0);
126 InitCommand command = new InitCommand();
127 command.setDirectory(directory);
128 try (Git git = command.call()) {
129 assertNotNull(git.getRepository());
130 }
131 }
132
133 @Test
134 public void testInitBareRepository() throws IOException,
135 JGitInternalException, GitAPIException {
136 File directory = createTempDirectory("testInitBareRepository");
137 InitCommand command = new InitCommand();
138 command.setDirectory(directory);
139 command.setBare(true);
140 try (Git git = command.call()) {
141 Repository repository = git.getRepository();
142 assertNotNull(repository);
143 assertTrue(repository.isBare());
144 assertEquals("refs/heads/master", repository.getFullBranch());
145 }
146 }
147
148 @Test
149 public void testInitBareRepositoryMainInitialBranch()
150 throws IOException, JGitInternalException, GitAPIException {
151 File directory = createTempDirectory("testInitBareRepository");
152 InitCommand command = new InitCommand();
153 command.setDirectory(directory);
154 command.setBare(true);
155 command.setInitialBranch("main");
156 try (Git git = command.call()) {
157 Repository repository = git.getRepository();
158 assertNotNull(repository);
159 assertTrue(repository.isBare());
160 assertEquals("refs/heads/main", repository.getFullBranch());
161 }
162 }
163
164
165
166 @Test
167 public void testInitWithExplicitGitDir() throws IOException,
168 JGitInternalException, GitAPIException {
169 File wt = createTempDirectory("testInitRepositoryWT");
170 File gitDir = createTempDirectory("testInitRepositoryGIT");
171 InitCommand command = new InitCommand();
172 command.setDirectory(wt);
173 command.setGitDir(gitDir);
174 try (Git git = command.call()) {
175 Repository repository = git.getRepository();
176 assertNotNull(repository);
177 assertEqualsFile(wt, repository.getWorkTree());
178 assertEqualsFile(gitDir, repository.getDirectory());
179 }
180 }
181
182
183
184 @Test
185 public void testInitWithOnlyExplicitGitDir() throws IOException,
186 JGitInternalException, GitAPIException {
187 MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
188 reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
189 .getAbsolutePath());
190 File gitDir = createTempDirectory("testInitRepository/.git");
191 InitCommand command = new InitCommand();
192 command.setGitDir(gitDir);
193 try (Git git = command.call()) {
194 Repository repository = git.getRepository();
195 assertNotNull(repository);
196 assertEqualsFile(gitDir, repository.getDirectory());
197 assertEqualsFile(new File(reader.getProperty("user.dir")),
198 repository.getWorkTree());
199 }
200 }
201
202
203
204
205
206 @Test(expected = IllegalStateException.class)
207 public void testInitBare_DirAndGitDirMustBeEqual() throws IOException,
208 JGitInternalException, GitAPIException {
209 File gitDir = createTempDirectory("testInitRepository.git");
210 InitCommand command = new InitCommand();
211 command.setBare(true);
212 command.setDirectory(gitDir);
213 command.setGitDir(new File(gitDir, ".."));
214 command.call();
215 }
216
217
218
219
220 @Test
221 public void testInitWithDefaultsNonBare() throws JGitInternalException,
222 GitAPIException, IOException {
223 MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
224 reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
225 .getAbsolutePath());
226 InitCommand command = new InitCommand();
227 command.setBare(false);
228 try (Git git = command.call()) {
229 Repository repository = git.getRepository();
230 assertNotNull(repository);
231 assertEqualsFile(new File(reader.getProperty("user.dir"), ".git"),
232 repository.getDirectory());
233 assertEqualsFile(new File(reader.getProperty("user.dir")),
234 repository.getWorkTree());
235 }
236 }
237
238
239
240
241 @Test(expected = NoWorkTreeException.class)
242 public void testInitWithDefaultsBare() throws JGitInternalException,
243 GitAPIException, IOException {
244 MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
245 reader.setProperty(Constants.OS_USER_DIR, getTemporaryDirectory()
246 .getAbsolutePath());
247 InitCommand command = new InitCommand();
248 command.setBare(true);
249 try (Git git = command.call()) {
250 Repository repository = git.getRepository();
251 assertNotNull(repository);
252 assertEqualsFile(new File(reader.getProperty("user.dir")),
253 repository.getDirectory());
254 assertNull(repository.getWorkTree());
255 }
256 }
257
258
259
260
261
262 @Test(expected = IllegalStateException.class)
263 public void testInitNonBare_GitdirAndDirShouldntBeSame()
264 throws JGitInternalException, GitAPIException, IOException {
265 File gitDir = createTempDirectory("testInitRepository.git");
266 InitCommand command = new InitCommand();
267 command.setBare(false);
268 command.setGitDir(gitDir);
269 command.setDirectory(gitDir);
270 command.call().getRepository();
271 }
272 }