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