View Javadoc
1   /*
2    * Copyright (C) 2011, 2013 Chris Aniszczyk <caniszczyk@gmail.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.api;
44  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertFalse;
47  import static org.junit.Assert.assertNotNull;
48  import static org.junit.Assert.assertNull;
49  import static org.junit.Assert.assertTrue;
50  import static org.junit.Assert.fail;
51  
52  import java.io.File;
53  import java.io.IOException;
54  import java.net.URISyntaxException;
55  import java.util.Collections;
56  import java.util.List;
57  import java.util.Map;
58  
59  import org.eclipse.jgit.api.ListBranchCommand.ListMode;
60  import org.eclipse.jgit.api.errors.GitAPIException;
61  import org.eclipse.jgit.api.errors.JGitInternalException;
62  import org.eclipse.jgit.errors.NoWorkTreeException;
63  import org.eclipse.jgit.junit.RepositoryTestCase;
64  import org.eclipse.jgit.junit.TestRepository;
65  import org.eclipse.jgit.lib.BranchConfig.BranchRebaseMode;
66  import org.eclipse.jgit.lib.ConfigConstants;
67  import org.eclipse.jgit.lib.Constants;
68  import org.eclipse.jgit.lib.ObjectId;
69  import org.eclipse.jgit.lib.Ref;
70  import org.eclipse.jgit.lib.Repository;
71  import org.eclipse.jgit.lib.StoredConfig;
72  import org.eclipse.jgit.revwalk.RevBlob;
73  import org.eclipse.jgit.revwalk.RevCommit;
74  import org.eclipse.jgit.submodule.SubmoduleStatus;
75  import org.eclipse.jgit.submodule.SubmoduleStatusType;
76  import org.eclipse.jgit.submodule.SubmoduleWalk;
77  import org.eclipse.jgit.transport.RefSpec;
78  import org.eclipse.jgit.transport.RemoteConfig;
79  import org.eclipse.jgit.transport.URIish;
80  import org.eclipse.jgit.util.SystemReader;
81  import org.junit.Test;
82  
83  public class CloneCommandTest extends RepositoryTestCase {
84  
85  	private Git git;
86  
87  	private TestRepository<Repository> tr;
88  
89  	@Override
90  	public void setUp() throws Exception {
91  		super.setUp();
92  		tr = new TestRepository<>(db);
93  
94  		git = new Git(db);
95  		// commit something
96  		writeTrashFile("Test.txt", "Hello world");
97  		git.add().addFilepattern("Test.txt").call();
98  		git.commit().setMessage("Initial commit").call();
99  		git.tag().setName("tag-initial").setMessage("Tag initial").call();
100 
101 		// create a test branch and switch to it
102 		git.checkout().setCreateBranch(true).setName("test").call();
103 
104 		// commit something on the test branch
105 		writeTrashFile("Test.txt", "Some change");
106 		git.add().addFilepattern("Test.txt").call();
107 		git.commit().setMessage("Second commit").call();
108 		RevBlob blob = tr.blob("blob-not-in-master-branch");
109 		git.tag().setName("tag-for-blob").setObjectId(blob).call();
110 	}
111 
112 	@Test
113 	public void testCloneRepository() throws IOException,
114 			JGitInternalException, GitAPIException, URISyntaxException {
115 		File directory = createTempDirectory("testCloneRepository");
116 		CloneCommand command = Git.cloneRepository();
117 		command.setDirectory(directory);
118 		command.setURI(fileUri());
119 		Git git2 = command.call();
120 		addRepoToClose(git2.getRepository());
121 		assertNotNull(git2);
122 		ObjectId id = git2.getRepository().resolve("tag-for-blob");
123 		assertNotNull(id);
124 		assertEquals(git2.getRepository().getFullBranch(), "refs/heads/test");
125 		assertEquals(
126 				"origin",
127 				git2.getRepository()
128 						.getConfig()
129 						.getString(ConfigConstants.CONFIG_BRANCH_SECTION,
130 								"test", ConfigConstants.CONFIG_KEY_REMOTE));
131 		assertEquals(
132 				"refs/heads/test",
133 				git2.getRepository()
134 						.getConfig()
135 						.getString(ConfigConstants.CONFIG_BRANCH_SECTION,
136 								"test", ConfigConstants.CONFIG_KEY_MERGE));
137 		assertEquals(2, git2.branchList().setListMode(ListMode.REMOTE).call()
138 				.size());
139 		assertEquals(new RefSpec("+refs/heads/*:refs/remotes/origin/*"),
140 				fetchRefSpec(git2.getRepository()));
141 	}
142 
143 	@Test
144 	public void testCloneRepositoryExplicitGitDir() throws IOException,
145 			JGitInternalException, GitAPIException {
146 		File directory = createTempDirectory("testCloneRepository");
147 		CloneCommand command = Git.cloneRepository();
148 		command.setDirectory(directory);
149 		command.setGitDir(new File(directory, Constants.DOT_GIT));
150 		command.setURI(fileUri());
151 		Git git2 = command.call();
152 		addRepoToClose(git2.getRepository());
153 		assertEquals(directory, git2.getRepository().getWorkTree());
154 		assertEquals(new File(directory, Constants.DOT_GIT), git2.getRepository()
155 				.getDirectory());
156 	}
157 
158 	@Test
159 	public void testCloneRepositoryDefaultDirectory()
160 			throws URISyntaxException, JGitInternalException {
161 		CloneCommand command = Git.cloneRepository().setURI(fileUri());
162 
163 		command.verifyDirectories(new URIish(fileUri()));
164 		File directory = command.getDirectory();
165 		assertEquals(git.getRepository().getWorkTree().getName(), directory.getName());
166 	}
167 
168 	@Test
169 	public void testCloneBareRepositoryDefaultDirectory()
170 			throws URISyntaxException, JGitInternalException {
171 		CloneCommand command = Git.cloneRepository().setURI(fileUri()).setBare(true);
172 
173 		command.verifyDirectories(new URIish(fileUri()));
174 		File directory = command.getDirectory();
175 		assertEquals(git.getRepository().getWorkTree().getName() + Constants.DOT_GIT_EXT, directory.getName());
176 	}
177 
178 	@Test
179 	public void testCloneRepositoryExplicitGitDirNonStd() throws IOException,
180 			JGitInternalException, GitAPIException {
181 		File directory = createTempDirectory("testCloneRepository");
182 		File gDir = createTempDirectory("testCloneRepository.git");
183 		CloneCommand command = Git.cloneRepository();
184 		command.setDirectory(directory);
185 		command.setGitDir(gDir);
186 		command.setURI(fileUri());
187 		Git git2 = command.call();
188 		addRepoToClose(git2.getRepository());
189 		assertEquals(directory, git2.getRepository().getWorkTree());
190 		assertEquals(gDir, git2.getRepository()
191 				.getDirectory());
192 		assertTrue(new File(directory, Constants.DOT_GIT).isFile());
193 		assertFalse(new File(gDir, Constants.DOT_GIT).exists());
194 	}
195 
196 	@Test
197 	public void testCloneRepositoryExplicitGitDirBare() throws IOException,
198 			JGitInternalException, GitAPIException {
199 		File gDir = createTempDirectory("testCloneRepository.git");
200 		CloneCommand command = Git.cloneRepository();
201 		command.setBare(true);
202 		command.setGitDir(gDir);
203 		command.setURI(fileUri());
204 		Git git2 = command.call();
205 		addRepoToClose(git2.getRepository());
206 		try {
207 			assertNull(null, git2.getRepository().getWorkTree());
208 			fail("Expected NoWorkTreeException");
209 		} catch (NoWorkTreeException e) {
210 			assertEquals(gDir, git2.getRepository().getDirectory());
211 		}
212 	}
213 
214 	@Test
215 	public void testBareCloneRepository() throws IOException,
216 			JGitInternalException, GitAPIException, URISyntaxException {
217 		File directory = createTempDirectory("testCloneRepository_bare");
218 		CloneCommand command = Git.cloneRepository();
219 		command.setBare(true);
220 		command.setDirectory(directory);
221 		command.setURI(fileUri());
222 		Git git2 = command.call();
223 		addRepoToClose(git2.getRepository());
224 		assertEquals(new RefSpec("+refs/heads/*:refs/heads/*"),
225 				fetchRefSpec(git2.getRepository()));
226 	}
227 
228 	@Test
229 	public void testCloneRepositoryCustomRemote() throws Exception {
230 		File directory = createTempDirectory("testCloneRemoteUpstream");
231 		CloneCommand command = Git.cloneRepository();
232 		command.setDirectory(directory);
233 		command.setRemote("upstream");
234 		command.setURI(fileUri());
235 		Git git2 = command.call();
236 		addRepoToClose(git2.getRepository());
237 		assertEquals("+refs/heads/*:refs/remotes/upstream/*",
238 				git2.getRepository()
239 					.getConfig()
240 					.getStringList("remote", "upstream",
241 							"fetch")[0]);
242 		assertEquals("upstream",
243 				git2.getRepository()
244 					.getConfig()
245 					.getString("branch", "test", "remote"));
246 		assertEquals(db.resolve("test"),
247 				git2.getRepository().resolve("upstream/test"));
248 	}
249 
250 	@Test
251 	public void testBareCloneRepositoryCustomRemote() throws Exception {
252 		File directory = createTempDirectory("testCloneRemoteUpstream_bare");
253 		CloneCommand command = Git.cloneRepository();
254 		command.setBare(true);
255 		command.setDirectory(directory);
256 		command.setRemote("upstream");
257 		command.setURI(fileUri());
258 		Git git2 = command.call();
259 		addRepoToClose(git2.getRepository());
260 		assertEquals("+refs/heads/*:refs/heads/*",
261 				git2.getRepository()
262 					.getConfig()
263 					.getStringList("remote", "upstream",
264 							"fetch")[0]);
265 		assertEquals("upstream",
266 				git2.getRepository()
267 					.getConfig()
268 					.getString("branch", "test", "remote"));
269 		assertNull(git2.getRepository().resolve("upstream/test"));
270 	}
271 
272 	@Test
273 	public void testBareCloneRepositoryNullRemote() throws Exception {
274 		File directory = createTempDirectory("testCloneRemoteNull_bare");
275 		CloneCommand command = Git.cloneRepository();
276 		command.setBare(true);
277 		command.setDirectory(directory);
278 		command.setRemote(null);
279 		command.setURI(fileUri());
280 		Git git2 = command.call();
281 		addRepoToClose(git2.getRepository());
282 		assertEquals("+refs/heads/*:refs/heads/*", git2.getRepository()
283 				.getConfig().getStringList("remote", "origin", "fetch")[0]);
284 		assertEquals("origin", git2.getRepository().getConfig()
285 				.getString("branch", "test", "remote"));
286 	}
287 
288 	public static RefSpec fetchRefSpec(Repository r) throws URISyntaxException {
289 		RemoteConfig remoteConfig =
290 				new RemoteConfig(r.getConfig(), Constants.DEFAULT_REMOTE_NAME);
291 		return remoteConfig.getFetchRefSpecs().get(0);
292 	}
293 
294 	@Test
295 	public void testCloneRepositoryWithBranch() throws IOException,
296 			JGitInternalException, GitAPIException {
297 		File directory = createTempDirectory("testCloneRepositoryWithBranch");
298 		CloneCommand command = Git.cloneRepository();
299 		command.setBranch("refs/heads/master");
300 		command.setDirectory(directory);
301 		command.setURI(fileUri());
302 		Git git2 = command.call();
303 		addRepoToClose(git2.getRepository());
304 
305 		assertNotNull(git2);
306 		assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
307 		assertEquals(
308 				"refs/heads/master, refs/remotes/origin/master, refs/remotes/origin/test",
309 				allRefNames(git2.branchList().setListMode(ListMode.ALL).call()));
310 
311 		// Same thing, but now without checkout
312 		directory = createTempDirectory("testCloneRepositoryWithBranch_bare");
313 		command = Git.cloneRepository();
314 		command.setBranch("refs/heads/master");
315 		command.setDirectory(directory);
316 		command.setURI(fileUri());
317 		command.setNoCheckout(true);
318 		git2 = command.call();
319 		addRepoToClose(git2.getRepository());
320 
321 		assertNotNull(git2);
322 		assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
323 		assertEquals("refs/remotes/origin/master, refs/remotes/origin/test",
324 				allRefNames(git2.branchList().setListMode(ListMode.ALL).call()));
325 
326 		// Same thing, but now test with bare repo
327 		directory = createTempDirectory("testCloneRepositoryWithBranch_bare");
328 		command = Git.cloneRepository();
329 		command.setBranch("refs/heads/master");
330 		command.setDirectory(directory);
331 		command.setURI(fileUri());
332 		command.setBare(true);
333 		git2 = command.call();
334 		addRepoToClose(git2.getRepository());
335 
336 		assertNotNull(git2);
337 		assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
338 		assertEquals("refs/heads/master, refs/heads/test", allRefNames(git2
339 				.branchList().setListMode(ListMode.ALL).call()));
340 	}
341 
342 	@Test
343 	public void testCloneRepositoryWithBranchShortName() throws Exception {
344 		File directory = createTempDirectory("testCloneRepositoryWithBranch");
345 		CloneCommand command = Git.cloneRepository();
346 		command.setBranch("test");
347 		command.setDirectory(directory);
348 		command.setURI(fileUri());
349 		Git git2 = command.call();
350 		addRepoToClose(git2.getRepository());
351 
352 		assertNotNull(git2);
353 		assertEquals("refs/heads/test", git2.getRepository().getFullBranch());
354 	}
355 
356 	@Test
357 	public void testCloneRepositoryWithTagName() throws Exception {
358 		File directory = createTempDirectory("testCloneRepositoryWithBranch");
359 		CloneCommand command = Git.cloneRepository();
360 		command.setBranch("tag-initial");
361 		command.setDirectory(directory);
362 		command.setURI(fileUri());
363 		Git git2 = command.call();
364 		addRepoToClose(git2.getRepository());
365 
366 		assertNotNull(git2);
367 		ObjectId taggedCommit = db.resolve("tag-initial^{commit}");
368 		assertEquals(taggedCommit.name(), git2
369 				.getRepository().getFullBranch());
370 	}
371 
372 	@Test
373 	public void testCloneRepositoryOnlyOneBranch() throws Exception {
374 		File directory = createTempDirectory("testCloneRepositoryWithBranch");
375 		CloneCommand command = Git.cloneRepository();
376 		command.setBranch("refs/heads/master");
377 		command.setBranchesToClone(Collections
378 				.singletonList("refs/heads/master"));
379 		command.setDirectory(directory);
380 		command.setURI(fileUri());
381 		Git git2 = command.call();
382 		addRepoToClose(git2.getRepository());
383 		assertNotNull(git2);
384 		assertNull(git2.getRepository().resolve("tag-for-blob"));
385 		assertNotNull(git2.getRepository().resolve("tag-initial"));
386 		assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
387 		assertEquals("refs/remotes/origin/master", allRefNames(git2
388 				.branchList().setListMode(ListMode.REMOTE).call()));
389 		RemoteConfig cfg = new RemoteConfig(git2.getRepository().getConfig(),
390 				Constants.DEFAULT_REMOTE_NAME);
391 		List<RefSpec> specs = cfg.getFetchRefSpecs();
392 		assertEquals(1, specs.size());
393 		assertEquals(
394 				new RefSpec("+refs/heads/master:refs/remotes/origin/master"),
395 				specs.get(0));
396 	}
397 
398 	@Test
399 	public void testBareCloneRepositoryOnlyOneBranch() throws Exception {
400 		// Same thing, but now test with bare repo
401 		File directory = createTempDirectory(
402 				"testCloneRepositoryWithBranch_bare");
403 		CloneCommand command = Git.cloneRepository();
404 		command.setBranch("refs/heads/master");
405 		command.setBranchesToClone(Collections
406 				.singletonList("refs/heads/master"));
407 		command.setDirectory(directory);
408 		command.setURI(fileUri());
409 		command.setBare(true);
410 		Git git2 = command.call();
411 		addRepoToClose(git2.getRepository());
412 		assertNotNull(git2);
413 		assertNull(git2.getRepository().resolve("tag-for-blob"));
414 		assertNotNull(git2.getRepository().resolve("tag-initial"));
415 		assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
416 		assertEquals("refs/heads/master", allRefNames(git2.branchList()
417 				.setListMode(ListMode.ALL).call()));
418 		RemoteConfig cfg = new RemoteConfig(git2.getRepository().getConfig(),
419 				Constants.DEFAULT_REMOTE_NAME);
420 		List<RefSpec> specs = cfg.getFetchRefSpecs();
421 		assertEquals(1, specs.size());
422 		assertEquals(
423 				new RefSpec("+refs/heads/master:refs/heads/master"),
424 				specs.get(0));
425 	}
426 
427 	@Test
428 	public void testCloneRepositoryOnlyOneTag() throws Exception {
429 		File directory = createTempDirectory("testCloneRepositoryWithBranch");
430 		CloneCommand command = Git.cloneRepository();
431 		command.setBranch("tag-initial");
432 		command.setBranchesToClone(
433 				Collections.singletonList("refs/tags/tag-initial"));
434 		command.setDirectory(directory);
435 		command.setURI(fileUri());
436 		Git git2 = command.call();
437 		addRepoToClose(git2.getRepository());
438 		assertNotNull(git2);
439 		assertNull(git2.getRepository().resolve("tag-for-blob"));
440 		assertNull(git2.getRepository().resolve("refs/heads/master"));
441 		assertNotNull(git2.getRepository().resolve("tag-initial"));
442 		ObjectId taggedCommit = db.resolve("tag-initial^{commit}");
443 		assertEquals(taggedCommit.name(), git2.getRepository().getFullBranch());
444 		RemoteConfig cfg = new RemoteConfig(git2.getRepository().getConfig(),
445 				Constants.DEFAULT_REMOTE_NAME);
446 		List<RefSpec> specs = cfg.getFetchRefSpecs();
447 		assertEquals(1, specs.size());
448 		assertEquals(
449 				new RefSpec("+refs/tags/tag-initial:refs/tags/tag-initial"),
450 				specs.get(0));
451 	}
452 
453 	public static String allRefNames(List<Ref> refs) {
454 		StringBuilder sb = new StringBuilder();
455 		for (Ref f : refs) {
456 			if (sb.length() > 0)
457 				sb.append(", ");
458 			sb.append(f.getName());
459 		}
460 		return sb.toString();
461 	}
462 
463 	@Test
464 	public void testCloneRepositoryWhenDestinationDirectoryExistsAndIsNotEmpty()
465 			throws IOException, JGitInternalException, GitAPIException {
466 		String dirName = "testCloneTargetDirectoryNotEmpty";
467 		File directory = createTempDirectory(dirName);
468 		CloneCommand command = Git.cloneRepository();
469 		command.setDirectory(directory);
470 		command.setURI(fileUri());
471 		Git git2 = command.call();
472 		addRepoToClose(git2.getRepository());
473 		assertNotNull(git2);
474 		// clone again
475 		command = Git.cloneRepository();
476 		command.setDirectory(directory);
477 		command.setURI(fileUri());
478 		try {
479 			git2 = command.call();
480 			// we shouldn't get here
481 			fail("destination directory already exists and is not an empty folder, cloning should fail");
482 		} catch (JGitInternalException e) {
483 			assertTrue(e.getMessage().contains("not an empty directory"));
484 			assertTrue(e.getMessage().contains(dirName));
485 		}
486 	}
487 
488 	@Test
489 	public void testCloneRepositoryWithMultipleHeadBranches() throws Exception {
490 		git.checkout().setName(Constants.MASTER).call();
491 		git.branchCreate().setName("a").call();
492 
493 		File directory = createTempDirectory("testCloneRepositoryWithMultipleHeadBranches");
494 		CloneCommand clone = Git.cloneRepository();
495 		clone.setDirectory(directory);
496 		clone.setURI(fileUri());
497 		Git git2 = clone.call();
498 		addRepoToClose(git2.getRepository());
499 		assertNotNull(git2);
500 
501 		assertEquals(Constants.MASTER, git2.getRepository().getBranch());
502 	}
503 
504 	@Test
505 	public void testCloneRepositoryWithSubmodules() throws Exception {
506 		git.checkout().setName(Constants.MASTER).call();
507 
508 		String file = "file.txt";
509 		writeTrashFile(file, "content");
510 		git.add().addFilepattern(file).call();
511 		RevCommit commit = git.commit().setMessage("create file").call();
512 
513 		SubmoduleAddCommand command = new SubmoduleAddCommand(db);
514 		String path = "sub";
515 		command.setPath(path);
516 		String uri = db.getDirectory().toURI().toString();
517 		command.setURI(uri);
518 		Repository repo = command.call();
519 		assertNotNull(repo);
520 		addRepoToClose(repo);
521 		git.add().addFilepattern(path)
522 				.addFilepattern(Constants.DOT_GIT_MODULES).call();
523 		git.commit().setMessage("adding submodule").call();
524 		try (SubmoduleWalk walk = SubmoduleWalk.forIndex(git.getRepository())) {
525 			assertTrue(walk.next());
526 			Repository subRepo = walk.getRepository();
527 			addRepoToClose(subRepo);
528 			assertNotNull(subRepo);
529 			assertEquals(
530 					new File(git.getRepository().getWorkTree(), walk.getPath()),
531 					subRepo.getWorkTree());
532 			assertEquals(new File(new File(git.getRepository().getDirectory(),
533 					"modules"), walk.getPath()), subRepo.getDirectory());
534 		}
535 
536 		File directory = createTempDirectory("testCloneRepositoryWithSubmodules");
537 		CloneCommand clone = Git.cloneRepository();
538 		clone.setDirectory(directory);
539 		clone.setCloneSubmodules(true);
540 		clone.setURI(fileUri());
541 		Git git2 = clone.call();
542 		addRepoToClose(git2.getRepository());
543 		assertNotNull(git2);
544 
545 		assertEquals(Constants.MASTER, git2.getRepository().getBranch());
546 		assertTrue(new File(git2.getRepository().getWorkTree(), path
547 				+ File.separatorChar + file).exists());
548 
549 		SubmoduleStatusCommand status = new SubmoduleStatusCommand(
550 				git2.getRepository());
551 		Map<String, SubmoduleStatus> statuses = status.call();
552 		SubmoduleStatus pathStatus = statuses.get(path);
553 		assertNotNull(pathStatus);
554 		assertEquals(SubmoduleStatusType.INITIALIZED, pathStatus.getType());
555 		assertEquals(commit, pathStatus.getHeadId());
556 		assertEquals(commit, pathStatus.getIndexId());
557 
558 		try (SubmoduleWalk walk = SubmoduleWalk
559 				.forIndex(git2.getRepository())) {
560 			assertTrue(walk.next());
561 			Repository clonedSub1 = walk.getRepository();
562 			addRepoToClose(clonedSub1);
563 			assertNotNull(clonedSub1);
564 			assertEquals(new File(git2.getRepository().getWorkTree(),
565 					walk.getPath()), clonedSub1.getWorkTree());
566 			assertEquals(
567 					new File(new File(git2.getRepository().getDirectory(),
568 							"modules"), walk.getPath()),
569 					clonedSub1.getDirectory());
570 		}
571 	}
572 
573 	@Test
574 	public void testCloneRepositoryWithNestedSubmodules() throws Exception {
575 		git.checkout().setName(Constants.MASTER).call();
576 
577 		// Create submodule 1
578 		File submodule1 = createTempDirectory("testCloneRepositoryWithNestedSubmodules1");
579 		Git sub1Git = Git.init().setDirectory(submodule1).call();
580 		assertNotNull(sub1Git);
581 		Repository sub1 = sub1Git.getRepository();
582 		assertNotNull(sub1);
583 		addRepoToClose(sub1);
584 
585 		String file = "file.txt";
586 		String path = "sub";
587 
588 		write(new File(sub1.getWorkTree(), file), "content");
589 		sub1Git.add().addFilepattern(file).call();
590 		RevCommit commit = sub1Git.commit().setMessage("create file").call();
591 		assertNotNull(commit);
592 
593 		// Create submodule 2
594 		File submodule2 = createTempDirectory("testCloneRepositoryWithNestedSubmodules2");
595 		Git sub2Git = Git.init().setDirectory(submodule2).call();
596 		assertNotNull(sub2Git);
597 		Repository sub2 = sub2Git.getRepository();
598 		assertNotNull(sub2);
599 		addRepoToClose(sub2);
600 
601 		write(new File(sub2.getWorkTree(), file), "content");
602 		sub2Git.add().addFilepattern(file).call();
603 		RevCommit sub2Head = sub2Git.commit().setMessage("create file").call();
604 		assertNotNull(sub2Head);
605 
606 		// Add submodule 2 to submodule 1
607 		Repository r = sub1Git.submoduleAdd().setPath(path)
608 				.setURI(sub2.getDirectory().toURI().toString()).call();
609 		assertNotNull(r);
610 		addRepoToClose(r);
611 		RevCommit sub1Head = sub1Git.commit().setAll(true)
612 				.setMessage("Adding submodule").call();
613 		assertNotNull(sub1Head);
614 
615 		// Add submodule 1 to default repository
616 		r = git.submoduleAdd().setPath(path)
617 				.setURI(sub1.getDirectory().toURI().toString()).call();
618 		assertNotNull(r);
619 		addRepoToClose(r);
620 		assertNotNull(git.commit().setAll(true).setMessage("Adding submodule")
621 				.call());
622 
623 		// Clone default repository and include submodules
624 		File directory = createTempDirectory("testCloneRepositoryWithNestedSubmodules");
625 		CloneCommand clone = Git.cloneRepository();
626 		clone.setDirectory(directory);
627 		clone.setCloneSubmodules(true);
628 		clone.setURI(git.getRepository().getDirectory().toURI().toString());
629 		Git git2 = clone.call();
630 		addRepoToClose(git2.getRepository());
631 		assertNotNull(git2);
632 
633 		assertEquals(Constants.MASTER, git2.getRepository().getBranch());
634 		assertTrue(new File(git2.getRepository().getWorkTree(), path
635 				+ File.separatorChar + file).exists());
636 		assertTrue(new File(git2.getRepository().getWorkTree(), path
637 				+ File.separatorChar + path + File.separatorChar + file)
638 				.exists());
639 
640 		SubmoduleStatusCommand status = new SubmoduleStatusCommand(
641 				git2.getRepository());
642 		Map<String, SubmoduleStatus> statuses = status.call();
643 		SubmoduleStatus pathStatus = statuses.get(path);
644 		assertNotNull(pathStatus);
645 		assertEquals(SubmoduleStatusType.INITIALIZED, pathStatus.getType());
646 		assertEquals(sub1Head, pathStatus.getHeadId());
647 		assertEquals(sub1Head, pathStatus.getIndexId());
648 
649 		SubmoduleWalk walk = SubmoduleWalk.forIndex(git2.getRepository());
650 		assertTrue(walk.next());
651 		try (Repository clonedSub1 = walk.getRepository()) {
652 			assertNotNull(clonedSub1);
653 			assertEquals(new File(git2.getRepository().getWorkTree(),
654 					walk.getPath()), clonedSub1.getWorkTree());
655 			assertEquals(
656 					new File(new File(git2.getRepository().getDirectory(),
657 							"modules"), walk.getPath()),
658 					clonedSub1.getDirectory());
659 			status = new SubmoduleStatusCommand(clonedSub1);
660 			statuses = status.call();
661 		}
662 		pathStatus = statuses.get(path);
663 		assertNotNull(pathStatus);
664 		assertEquals(SubmoduleStatusType.INITIALIZED, pathStatus.getType());
665 		assertEquals(sub2Head, pathStatus.getHeadId());
666 		assertEquals(sub2Head, pathStatus.getIndexId());
667 		assertFalse(walk.next());
668 	}
669 
670 	@Test
671 	public void testCloneWithAutoSetupRebase() throws Exception {
672 		File directory = createTempDirectory("testCloneRepository1");
673 		CloneCommand command = Git.cloneRepository();
674 		command.setDirectory(directory);
675 		command.setURI(fileUri());
676 		Git git2 = command.call();
677 		addRepoToClose(git2.getRepository());
678 		assertNull(git2.getRepository().getConfig().getEnum(
679 				BranchRebaseMode.values(),
680 				ConfigConstants.CONFIG_BRANCH_SECTION, "test",
681 				ConfigConstants.CONFIG_KEY_REBASE, null));
682 
683 		StoredConfig userConfig = SystemReader.getInstance()
684 				.getUserConfig();
685 		userConfig.setString(ConfigConstants.CONFIG_BRANCH_SECTION, null,
686 				ConfigConstants.CONFIG_KEY_AUTOSETUPREBASE,
687 				ConfigConstants.CONFIG_KEY_ALWAYS);
688 		userConfig.save();
689 		directory = createTempDirectory("testCloneRepository2");
690 		command = Git.cloneRepository();
691 		command.setDirectory(directory);
692 		command.setURI(fileUri());
693 		git2 = command.call();
694 		addRepoToClose(git2.getRepository());
695 		assertEquals(BranchRebaseMode.REBASE,
696 				git2.getRepository().getConfig().getEnum(
697 						BranchRebaseMode.values(),
698 						ConfigConstants.CONFIG_BRANCH_SECTION, "test",
699 						ConfigConstants.CONFIG_KEY_REBASE,
700 						BranchRebaseMode.NONE));
701 
702 		userConfig.setString(ConfigConstants.CONFIG_BRANCH_SECTION, null,
703 				ConfigConstants.CONFIG_KEY_AUTOSETUPREBASE,
704 				ConfigConstants.CONFIG_KEY_REMOTE);
705 		userConfig.save();
706 		directory = createTempDirectory("testCloneRepository2");
707 		command = Git.cloneRepository();
708 		command.setDirectory(directory);
709 		command.setURI(fileUri());
710 		git2 = command.call();
711 		addRepoToClose(git2.getRepository());
712 		assertEquals(BranchRebaseMode.REBASE,
713 				git2.getRepository().getConfig().getEnum(
714 						BranchRebaseMode.values(),
715 						ConfigConstants.CONFIG_BRANCH_SECTION, "test",
716 						ConfigConstants.CONFIG_KEY_REBASE,
717 						BranchRebaseMode.NONE));
718 
719 	}
720 
721 	private String fileUri() {
722 		return "file://" + git.getRepository().getWorkTree().getAbsolutePath();
723 	}
724 }