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 f16fe83d6..0e9f6721c 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 @@ -236,4 +236,20 @@ public void testCloneRepositoryWhenDestinationDirectoryExistsAndIsNotEmpty() 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()); + } } 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 dbc126e89..b779c488a 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java @@ -229,6 +229,12 @@ private Ref findBranchToCheckout(FetchResult result) { 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();