diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java index 6053c8c56..63ab8094a 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java @@ -9,8 +9,10 @@ */ package org.eclipse.jgit.api; +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -121,9 +123,32 @@ public void testCloneRepository() throws IOException, } @Test - public void testCloneRepository_refLogForLocalRefs() + public void testCloneRepositoryNoCheckout() throws IOException, JGitInternalException, GitAPIException { - File directory = createTempDirectory("testCloneRepository"); + File directory = createTempDirectory("testCloneRepositoryNoCheckout"); + CloneCommand command = Git.cloneRepository(); + command.setDirectory(directory); + command.setURI(fileUri()); + command.setNoCheckout(true); + try (Git git2 = command.call()) { + Repository clonedRepo = git2.getRepository(); + Ref main = clonedRepo.exactRef(Constants.R_HEADS + "test"); + assertNotNull(main); + ObjectId id = main.getObjectId(); + assertNotNull(id); + assertNotEquals(id, ObjectId.zeroId()); + ObjectId headId = clonedRepo.resolve(Constants.HEAD); + assertEquals(id, headId); + assertArrayEquals(new String[] { Constants.DOT_GIT }, + directory.list()); + } + } + + @Test + public void testCloneRepositoryRefLogForLocalRefs() + throws IOException, JGitInternalException, GitAPIException { + File directory = createTempDirectory( + "testCloneRepositoryRefLogForLocalRefs"); CloneCommand command = Git.cloneRepository(); command.setDirectory(directory); command.setURI(fileUri()); @@ -331,7 +356,8 @@ public void testCloneRepositoryWithBranch() throws IOException, allRefNames(git2.branchList().setListMode(ListMode.ALL).call())); // Same thing, but now without checkout - directory = createTempDirectory("testCloneRepositoryWithBranch_bare"); + directory = createTempDirectory( + "testCloneRepositoryWithBranch_noCheckout"); command = Git.cloneRepository(); command.setBranch("refs/heads/master"); command.setDirectory(directory); @@ -341,7 +367,8 @@ public void testCloneRepositoryWithBranch() throws IOException, addRepoToClose(git2.getRepository()); assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master"); - assertEquals("refs/remotes/origin/master, refs/remotes/origin/test", + assertEquals( + "refs/heads/master, refs/remotes/origin/master, refs/remotes/origin/test", allRefNames(git2.branchList().setListMode(ListMode.ALL).call())); // Same thing, but now test with bare repo diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java index 87c95ec83..107b00e27 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java @@ -216,16 +216,14 @@ public Git call() throws GitAPIException, InvalidRemoteException, // ignore - the VM is already shutting down } } - if (!noCheckout) { - try { - checkout(repository, fetchResult); - } catch (IOException ioe) { - repository.close(); - throw new JGitInternalException(ioe.getMessage(), ioe); - } catch (GitAPIException | RuntimeException e) { - repository.close(); - throw e; - } + try { + checkout(repository, fetchResult); + } catch (IOException ioe) { + repository.close(); + throw new JGitInternalException(ioe.getMessage(), ioe); + } catch (GitAPIException | RuntimeException e) { + repository.close(); + throw e; } return new Git(repository, true); } @@ -393,7 +391,7 @@ private void checkout(Repository clonedRepo, FetchResult result) u.setNewObjectId(commit.getId()); u.forceUpdate(); - if (!bare) { + if (!bare && !noCheckout) { DirCache dc = clonedRepo.lockDirCache(); DirCacheCheckout co = new DirCacheCheckout(clonedRepo, dc, commit.getTree());