From f71fcbf36b9f9e21f8b796a0b2ea8226818e4780 Mon Sep 17 00:00:00 2001 From: Thomas Wolf Date: Sun, 18 Sep 2022 19:24:58 +0200 Subject: [PATCH] CloneCommand: set HEAD also when not checking out CloneCommand, when setNoCheckout(true) was set, did not set HEAD. With C git, "git clone --no-checkout" does. Change-Id: Ief3df7e904ce90829a6345a6c3e9ee6a68486ab0 Signed-off-by: Thomas Wolf --- .../eclipse/jgit/api/CloneCommandTest.java | 35 ++++++++++++++++--- .../org/eclipse/jgit/api/CloneCommand.java | 20 +++++------ 2 files changed, 40 insertions(+), 15 deletions(-) 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 6053c8c568..63ab8094ae 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 class CloneCommandTest extends RepositoryTestCase { } @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 class CloneCommandTest extends RepositoryTestCase { 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 class CloneCommandTest extends RepositoryTestCase { 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 87c95ec830..107b00e274 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 class CloneCommand extends TransportCommand { // 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 @@ public class CloneCommand extends TransportCommand { u.setNewObjectId(commit.getId()); u.forceUpdate(); - if (!bare) { + if (!bare && !noCheckout) { DirCache dc = clonedRepo.lockDirCache(); DirCacheCheckout co = new DirCacheCheckout(clonedRepo, dc, commit.getTree()); -- 2.39.5