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 IOException,
374 			JGitInternalException, GitAPIException {
375 		File directory = createTempDirectory("testCloneRepositoryWithBranch");
376 		CloneCommand command = Git.cloneRepository();
377 		command.setBranch("refs/heads/master");
378 		command.setBranchesToClone(Collections
379 				.singletonList("refs/heads/master"));
380 		command.setDirectory(directory);
381 		command.setURI(fileUri());
382 		Git git2 = command.call();
383 		addRepoToClose(git2.getRepository());
384 		assertNotNull(git2);
385 		assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
386 		assertEquals("refs/remotes/origin/master", allRefNames(git2
387 				.branchList().setListMode(ListMode.REMOTE).call()));
388 
389 		// Same thing, but now test with bare repo
390 		directory = createTempDirectory("testCloneRepositoryWithBranch_bare");
391 		command = Git.cloneRepository();
392 		command.setBranch("refs/heads/master");
393 		command.setBranchesToClone(Collections
394 				.singletonList("refs/heads/master"));
395 		command.setDirectory(directory);
396 		command.setURI(fileUri());
397 		command.setBare(true);
398 		git2 = command.call();
399 		addRepoToClose(git2.getRepository());
400 		assertNotNull(git2);
401 		assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
402 		assertEquals("refs/heads/master", allRefNames(git2.branchList()
403 				.setListMode(ListMode.ALL).call()));
404 	}
405 
406 	public static String allRefNames(List<Ref> refs) {
407 		StringBuilder sb = new StringBuilder();
408 		for (Ref f : refs) {
409 			if (sb.length() > 0)
410 				sb.append(", ");
411 			sb.append(f.getName());
412 		}
413 		return sb.toString();
414 	}
415 
416 	@Test
417 	public void testCloneRepositoryWhenDestinationDirectoryExistsAndIsNotEmpty()
418 			throws IOException, JGitInternalException, GitAPIException {
419 		String dirName = "testCloneTargetDirectoryNotEmpty";
420 		File directory = createTempDirectory(dirName);
421 		CloneCommand command = Git.cloneRepository();
422 		command.setDirectory(directory);
423 		command.setURI(fileUri());
424 		Git git2 = command.call();
425 		addRepoToClose(git2.getRepository());
426 		assertNotNull(git2);
427 		// clone again
428 		command = Git.cloneRepository();
429 		command.setDirectory(directory);
430 		command.setURI(fileUri());
431 		try {
432 			git2 = command.call();
433 			// we shouldn't get here
434 			fail("destination directory already exists and is not an empty folder, cloning should fail");
435 		} catch (JGitInternalException e) {
436 			assertTrue(e.getMessage().contains("not an empty directory"));
437 			assertTrue(e.getMessage().contains(dirName));
438 		}
439 	}
440 
441 	@Test
442 	public void testCloneRepositoryWithMultipleHeadBranches() throws Exception {
443 		git.checkout().setName(Constants.MASTER).call();
444 		git.branchCreate().setName("a").call();
445 
446 		File directory = createTempDirectory("testCloneRepositoryWithMultipleHeadBranches");
447 		CloneCommand clone = Git.cloneRepository();
448 		clone.setDirectory(directory);
449 		clone.setURI(fileUri());
450 		Git git2 = clone.call();
451 		addRepoToClose(git2.getRepository());
452 		assertNotNull(git2);
453 
454 		assertEquals(Constants.MASTER, git2.getRepository().getBranch());
455 	}
456 
457 	@Test
458 	public void testCloneRepositoryWithSubmodules() throws Exception {
459 		git.checkout().setName(Constants.MASTER).call();
460 
461 		String file = "file.txt";
462 		writeTrashFile(file, "content");
463 		git.add().addFilepattern(file).call();
464 		RevCommit commit = git.commit().setMessage("create file").call();
465 
466 		SubmoduleAddCommand command = new SubmoduleAddCommand(db);
467 		String path = "sub";
468 		command.setPath(path);
469 		String uri = db.getDirectory().toURI().toString();
470 		command.setURI(uri);
471 		Repository repo = command.call();
472 		assertNotNull(repo);
473 		addRepoToClose(repo);
474 		git.add().addFilepattern(path)
475 				.addFilepattern(Constants.DOT_GIT_MODULES).call();
476 		git.commit().setMessage("adding submodule").call();
477 		try (SubmoduleWalk walk = SubmoduleWalk.forIndex(git.getRepository())) {
478 			assertTrue(walk.next());
479 			Repository subRepo = walk.getRepository();
480 			addRepoToClose(subRepo);
481 			assertNotNull(subRepo);
482 			assertEquals(
483 					new File(git.getRepository().getWorkTree(), walk.getPath()),
484 					subRepo.getWorkTree());
485 			assertEquals(new File(new File(git.getRepository().getDirectory(),
486 					"modules"), walk.getPath()), subRepo.getDirectory());
487 		}
488 
489 		File directory = createTempDirectory("testCloneRepositoryWithSubmodules");
490 		CloneCommand clone = Git.cloneRepository();
491 		clone.setDirectory(directory);
492 		clone.setCloneSubmodules(true);
493 		clone.setURI(fileUri());
494 		Git git2 = clone.call();
495 		addRepoToClose(git2.getRepository());
496 		assertNotNull(git2);
497 
498 		assertEquals(Constants.MASTER, git2.getRepository().getBranch());
499 		assertTrue(new File(git2.getRepository().getWorkTree(), path
500 				+ File.separatorChar + file).exists());
501 
502 		SubmoduleStatusCommand status = new SubmoduleStatusCommand(
503 				git2.getRepository());
504 		Map<String, SubmoduleStatus> statuses = status.call();
505 		SubmoduleStatus pathStatus = statuses.get(path);
506 		assertNotNull(pathStatus);
507 		assertEquals(SubmoduleStatusType.INITIALIZED, pathStatus.getType());
508 		assertEquals(commit, pathStatus.getHeadId());
509 		assertEquals(commit, pathStatus.getIndexId());
510 
511 		try (SubmoduleWalk walk = SubmoduleWalk
512 				.forIndex(git2.getRepository())) {
513 			assertTrue(walk.next());
514 			Repository clonedSub1 = walk.getRepository();
515 			addRepoToClose(clonedSub1);
516 			assertNotNull(clonedSub1);
517 			assertEquals(new File(git2.getRepository().getWorkTree(),
518 					walk.getPath()), clonedSub1.getWorkTree());
519 			assertEquals(
520 					new File(new File(git2.getRepository().getDirectory(),
521 							"modules"), walk.getPath()),
522 					clonedSub1.getDirectory());
523 		}
524 	}
525 
526 	@Test
527 	public void testCloneRepositoryWithNestedSubmodules() throws Exception {
528 		git.checkout().setName(Constants.MASTER).call();
529 
530 		// Create submodule 1
531 		File submodule1 = createTempDirectory("testCloneRepositoryWithNestedSubmodules1");
532 		Git sub1Git = Git.init().setDirectory(submodule1).call();
533 		assertNotNull(sub1Git);
534 		Repository sub1 = sub1Git.getRepository();
535 		assertNotNull(sub1);
536 		addRepoToClose(sub1);
537 
538 		String file = "file.txt";
539 		String path = "sub";
540 
541 		write(new File(sub1.getWorkTree(), file), "content");
542 		sub1Git.add().addFilepattern(file).call();
543 		RevCommit commit = sub1Git.commit().setMessage("create file").call();
544 		assertNotNull(commit);
545 
546 		// Create submodule 2
547 		File submodule2 = createTempDirectory("testCloneRepositoryWithNestedSubmodules2");
548 		Git sub2Git = Git.init().setDirectory(submodule2).call();
549 		assertNotNull(sub2Git);
550 		Repository sub2 = sub2Git.getRepository();
551 		assertNotNull(sub2);
552 		addRepoToClose(sub2);
553 
554 		write(new File(sub2.getWorkTree(), file), "content");
555 		sub2Git.add().addFilepattern(file).call();
556 		RevCommit sub2Head = sub2Git.commit().setMessage("create file").call();
557 		assertNotNull(sub2Head);
558 
559 		// Add submodule 2 to submodule 1
560 		Repository r = sub1Git.submoduleAdd().setPath(path)
561 				.setURI(sub2.getDirectory().toURI().toString()).call();
562 		assertNotNull(r);
563 		addRepoToClose(r);
564 		RevCommit sub1Head = sub1Git.commit().setAll(true)
565 				.setMessage("Adding submodule").call();
566 		assertNotNull(sub1Head);
567 
568 		// Add submodule 1 to default repository
569 		r = git.submoduleAdd().setPath(path)
570 				.setURI(sub1.getDirectory().toURI().toString()).call();
571 		assertNotNull(r);
572 		addRepoToClose(r);
573 		assertNotNull(git.commit().setAll(true).setMessage("Adding submodule")
574 				.call());
575 
576 		// Clone default repository and include submodules
577 		File directory = createTempDirectory("testCloneRepositoryWithNestedSubmodules");
578 		CloneCommand clone = Git.cloneRepository();
579 		clone.setDirectory(directory);
580 		clone.setCloneSubmodules(true);
581 		clone.setURI(git.getRepository().getDirectory().toURI().toString());
582 		Git git2 = clone.call();
583 		addRepoToClose(git2.getRepository());
584 		assertNotNull(git2);
585 
586 		assertEquals(Constants.MASTER, git2.getRepository().getBranch());
587 		assertTrue(new File(git2.getRepository().getWorkTree(), path
588 				+ File.separatorChar + file).exists());
589 		assertTrue(new File(git2.getRepository().getWorkTree(), path
590 				+ File.separatorChar + path + File.separatorChar + file)
591 				.exists());
592 
593 		SubmoduleStatusCommand status = new SubmoduleStatusCommand(
594 				git2.getRepository());
595 		Map<String, SubmoduleStatus> statuses = status.call();
596 		SubmoduleStatus pathStatus = statuses.get(path);
597 		assertNotNull(pathStatus);
598 		assertEquals(SubmoduleStatusType.INITIALIZED, pathStatus.getType());
599 		assertEquals(sub1Head, pathStatus.getHeadId());
600 		assertEquals(sub1Head, pathStatus.getIndexId());
601 
602 		SubmoduleWalk walk = SubmoduleWalk.forIndex(git2.getRepository());
603 		assertTrue(walk.next());
604 		try (Repository clonedSub1 = walk.getRepository()) {
605 			assertNotNull(clonedSub1);
606 			assertEquals(new File(git2.getRepository().getWorkTree(),
607 					walk.getPath()), clonedSub1.getWorkTree());
608 			assertEquals(
609 					new File(new File(git2.getRepository().getDirectory(),
610 							"modules"), walk.getPath()),
611 					clonedSub1.getDirectory());
612 			status = new SubmoduleStatusCommand(clonedSub1);
613 			statuses = status.call();
614 		}
615 		pathStatus = statuses.get(path);
616 		assertNotNull(pathStatus);
617 		assertEquals(SubmoduleStatusType.INITIALIZED, pathStatus.getType());
618 		assertEquals(sub2Head, pathStatus.getHeadId());
619 		assertEquals(sub2Head, pathStatus.getIndexId());
620 		assertFalse(walk.next());
621 	}
622 
623 	@Test
624 	public void testCloneWithAutoSetupRebase() throws Exception {
625 		File directory = createTempDirectory("testCloneRepository1");
626 		CloneCommand command = Git.cloneRepository();
627 		command.setDirectory(directory);
628 		command.setURI(fileUri());
629 		Git git2 = command.call();
630 		addRepoToClose(git2.getRepository());
631 		assertNull(git2.getRepository().getConfig().getEnum(
632 				BranchRebaseMode.values(),
633 				ConfigConstants.CONFIG_BRANCH_SECTION, "test",
634 				ConfigConstants.CONFIG_KEY_REBASE, null));
635 
636 		StoredConfig userConfig = SystemReader.getInstance()
637 				.getUserConfig();
638 		userConfig.setString(ConfigConstants.CONFIG_BRANCH_SECTION, null,
639 				ConfigConstants.CONFIG_KEY_AUTOSETUPREBASE,
640 				ConfigConstants.CONFIG_KEY_ALWAYS);
641 		userConfig.save();
642 		directory = createTempDirectory("testCloneRepository2");
643 		command = Git.cloneRepository();
644 		command.setDirectory(directory);
645 		command.setURI(fileUri());
646 		git2 = command.call();
647 		addRepoToClose(git2.getRepository());
648 		assertEquals(BranchRebaseMode.REBASE,
649 				git2.getRepository().getConfig().getEnum(
650 						BranchRebaseMode.values(),
651 						ConfigConstants.CONFIG_BRANCH_SECTION, "test",
652 						ConfigConstants.CONFIG_KEY_REBASE,
653 						BranchRebaseMode.NONE));
654 
655 		userConfig.setString(ConfigConstants.CONFIG_BRANCH_SECTION, null,
656 				ConfigConstants.CONFIG_KEY_AUTOSETUPREBASE,
657 				ConfigConstants.CONFIG_KEY_REMOTE);
658 		userConfig.save();
659 		directory = createTempDirectory("testCloneRepository2");
660 		command = Git.cloneRepository();
661 		command.setDirectory(directory);
662 		command.setURI(fileUri());
663 		git2 = command.call();
664 		addRepoToClose(git2.getRepository());
665 		assertEquals(BranchRebaseMode.REBASE,
666 				git2.getRepository().getConfig().getEnum(
667 						BranchRebaseMode.values(),
668 						ConfigConstants.CONFIG_BRANCH_SECTION, "test",
669 						ConfigConstants.CONFIG_KEY_REBASE,
670 						BranchRebaseMode.NONE));
671 
672 	}
673 
674 	private String fileUri() {
675 		return "file://" + git.getRepository().getWorkTree().getAbsolutePath();
676 	}
677 }