]> source.dussan.org Git - jgit.git/commitdiff
CloneCommand: set HEAD also when not checking out 82/195882/1
authorThomas Wolf <twolf@apache.org>
Sun, 18 Sep 2022 17:24:58 +0000 (19:24 +0200)
committerThomas Wolf <twolf@apache.org>
Sun, 18 Sep 2022 17:43:40 +0000 (19:43 +0200)
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>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java

index 6053c8c568bee5287d28fd66a4f693b7348f19d4..63ab8094ae92d864b62b1eda876c21af57487e8e 100644 (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 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
index 87c95ec83081bc829bc6bb5434a95208945bcb47..107b00e274374b518588819f899e52e612f929fc 100644 (file)
@@ -216,16 +216,14 @@ public class CloneCommand extends TransportCommand<CloneCommand, Git> {
                                // 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<CloneCommand, Git> {
                u.setNewObjectId(commit.getId());
                u.forceUpdate();
 
-               if (!bare) {
+               if (!bare && !noCheckout) {
                        DirCache dc = clonedRepo.lockDirCache();
                        DirCacheCheckout co = new DirCacheCheckout(clonedRepo, dc,
                                        commit.getTree());