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