]> source.dussan.org Git - jgit.git/commitdiff
Always checkout master when it matches the advertised HEAD 87/4687/2
authorKevin Sawicki <kevin@github.com>
Mon, 28 Nov 2011 19:00:15 +0000 (11:00 -0800)
committerKevin Sawicki <kevin@github.com>
Mon, 28 Nov 2011 19:00:15 +0000 (11:00 -0800)
This parallels the CGit behavior of always using refs/heads/master
when it matches the remote advertised HEAD commit.

Change-Id: I5a5cd1516b58d116e334056aba1ef7990697ec30

org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java

index f16fe83d6a0e4e38edeaa25f4de5301182582889..0e9f6721c7534ea1bd9ed7b3a4ef3616f69f602c 100644 (file)
@@ -236,4 +236,20 @@ public class CloneCommandTest extends RepositoryTestCase {
                        assertTrue(e.getMessage().contains(dirName));
                }
        }
+
+       @Test
+       public void testCloneRepositoryWithMultipleHeadBranches() throws Exception {
+               git.checkout().setName(Constants.MASTER).call();
+               git.branchCreate().setName("a").call();
+
+               File directory = createTempDirectory("testCloneRepositoryWithMultipleHeadBranches");
+               CloneCommand clone = Git.cloneRepository();
+               clone.setDirectory(directory);
+               clone.setURI("file://" + git.getRepository().getWorkTree().getPath());
+               Git git2 = clone.call();
+               addRepoToClose(git2.getRepository());
+               assertNotNull(git2);
+
+               assertEquals(Constants.MASTER, git2.getRepository().getBranch());
+       }
 }
index dbc126e895a74e2f93f086534f8fd7bd05d484fa..b779c488aadcfc355346a06005c7eefc70354157 100644 (file)
@@ -229,6 +229,12 @@ public class CloneCommand extends TransportCommand<CloneCommand, Git> {
                final Ref idHEAD = result.getAdvertisedRef(Constants.HEAD);
                if (idHEAD == null)
                        return null;
+
+               Ref master = result.getAdvertisedRef(Constants.R_HEADS
+                               + Constants.MASTER);
+               if (master != null && master.getObjectId().equals(idHEAD.getObjectId()))
+                       return master;
+
                Ref foundBranch = null;
                for (final Ref r : result.getAdvertisedRefs()) {
                        final String n = r.getName();