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 0d7009dca..0ad067f6a 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 @@ -370,8 +370,7 @@ public class CloneCommandTest extends RepositoryTestCase { } @Test - public void testCloneRepositoryOnlyOneBranch() throws IOException, - JGitInternalException, GitAPIException { + public void testCloneRepositoryOnlyOneBranch() throws Exception { File directory = createTempDirectory("testCloneRepositoryWithBranch"); CloneCommand command = Git.cloneRepository(); command.setBranch("refs/heads/master"); @@ -385,22 +384,40 @@ public class CloneCommandTest extends RepositoryTestCase { assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master"); assertEquals("refs/remotes/origin/master", allRefNames(git2 .branchList().setListMode(ListMode.REMOTE).call())); + RemoteConfig cfg = new RemoteConfig(git2.getRepository().getConfig(), + Constants.DEFAULT_REMOTE_NAME); + List specs = cfg.getFetchRefSpecs(); + assertEquals(1, specs.size()); + assertEquals( + new RefSpec("+refs/heads/master:refs/remotes/origin/master"), + specs.get(0)); + } + @Test + public void testBareCloneRepositoryOnlyOneBranch() throws Exception { // Same thing, but now test with bare repo - directory = createTempDirectory("testCloneRepositoryWithBranch_bare"); - command = Git.cloneRepository(); + File directory = createTempDirectory( + "testCloneRepositoryWithBranch_bare"); + CloneCommand command = Git.cloneRepository(); command.setBranch("refs/heads/master"); command.setBranchesToClone(Collections .singletonList("refs/heads/master")); command.setDirectory(directory); command.setURI(fileUri()); command.setBare(true); - git2 = command.call(); + Git git2 = command.call(); addRepoToClose(git2.getRepository()); assertNotNull(git2); assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master"); assertEquals("refs/heads/master", allRefNames(git2.branchList() .setListMode(ListMode.ALL).call())); + RemoteConfig cfg = new RemoteConfig(git2.getRepository().getConfig(), + Constants.DEFAULT_REMOTE_NAME); + List specs = cfg.getFetchRefSpecs(); + assertEquals(1, specs.size()); + assertEquals( + new RefSpec("+refs/heads/master:refs/heads/master"), + specs.get(0)); } public static String allRefNames(List refs) { 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 5c06bac1f..5ea7cf871 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java @@ -284,11 +284,9 @@ public class CloneCommand extends TransportCommand { final String dst = (bare ? Constants.R_HEADS : Constants.R_REMOTES + config.getName() + "/") + "*"; //$NON-NLS-1$//$NON-NLS-2$ - RefSpec refSpec = new RefSpec(); - refSpec = refSpec.setForceUpdate(true); - refSpec = refSpec.setSourceDestination(Constants.R_HEADS + "*", dst); //$NON-NLS-1$ + List refSpecs = calculateRefSpecs(dst); - config.addFetchRefSpec(refSpec); + config.setFetchRefSpecs(refSpecs); config.update(clonedRepo.getConfig()); clonedRepo.getConfig().save(); @@ -300,9 +298,6 @@ public class CloneCommand extends TransportCommand { command.setTagOpt(TagOpt.FETCH_TAGS); configure(command); - List specs = calculateRefSpecs(dst); - command.setRefSpecs(specs); - return command.call(); } @@ -311,13 +306,15 @@ public class CloneCommand extends TransportCommand { wcrs = wcrs.setForceUpdate(true); wcrs = wcrs.setSourceDestination(Constants.R_HEADS + "*", dst); //$NON-NLS-1$ List specs = new ArrayList<>(); - if (cloneAllBranches) - specs.add(wcrs); - else if (branchesToClone != null - && branchesToClone.size() > 0) { - for (String selectedRef : branchesToClone) - if (wcrs.matchSource(selectedRef)) + if (!cloneAllBranches && branchesToClone != null + && !branchesToClone.isEmpty()) { + for (String selectedRef : branchesToClone) { + if (wcrs.matchSource(selectedRef)) { specs.add(wcrs.expandFromSource(selectedRef)); + } + } + } else { + specs.add(wcrs); } return specs; }