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