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 <twolf@apache.org>
This commit is contained in:
Thomas Wolf 2022-09-18 19:24:58 +02:00
parent 4f4204914c
commit f71fcbf36b
2 changed files with 40 additions and 15 deletions

View File

@ -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

View File

@ -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());