diff options
author | Thomas Wolf <thomas.wolf@paranor.ch> | 2015-07-23 14:14:46 -0400 |
---|---|---|
committer | Matthias Sohn <matthias.sohn@sap.com> | 2018-09-15 00:57:13 +0200 |
commit | 54a502f6c6f416dc570f023ffb48f05efd3af3ff (patch) | |
tree | 49b9eb121cc8d1ab20e2baef8daec7e46206ce72 | |
parent | cb4de02e5e7502705ef52264eb76d03a90c950d1 (diff) | |
download | jgit-54a502f6c6f416dc570f023ffb48f05efd3af3ff.tar.gz jgit-54a502f6c6f416dc570f023ffb48f05efd3af3ff.zip |
Fix fetch refspecs when not cloning all branches
When not all branches are cloned, the fetch refspec for the
remote should not be "+refs/heads/*:refs/remotes/origin/*":
that would fetch all branches on the very next fetch, thus
making a clone with only a subset of the branches rather
pointless.
Instead, produce refspecs for the cloned branches only.
Canonical git also does this for its --single-branch case;
it doesn't have an option to clone only a subset of the branches
(only one or all).
Bug: 466858
Change-Id: Ie871880f757663437efac1e8b3313094f9e629b3
Also-by: Julian Enoch <julian.enoch@ericsson.com>
Signed-off-by: Julian Enoch <julian.enoch@ericsson.com>
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java | 27 | ||||
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java | 23 |
2 files changed, 32 insertions, 18 deletions
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 0d7009dca4..0ad067f6a7 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<RefSpec> 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<RefSpec> specs = cfg.getFetchRefSpecs(); + assertEquals(1, specs.size()); + assertEquals( + new RefSpec("+refs/heads/master:refs/heads/master"), + specs.get(0)); } public static String allRefNames(List<Ref> 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 5c06bac1f2..5ea7cf8718 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<CloneCommand, Git> { 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<RefSpec> 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<CloneCommand, Git> { command.setTagOpt(TagOpt.FETCH_TAGS); configure(command); - List<RefSpec> specs = calculateRefSpecs(dst); - command.setRefSpecs(specs); - return command.call(); } @@ -311,13 +306,15 @@ public class CloneCommand extends TransportCommand<CloneCommand, Git> { wcrs = wcrs.setForceUpdate(true); wcrs = wcrs.setSourceDestination(Constants.R_HEADS + "*", dst); //$NON-NLS-1$ List<RefSpec> 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; } |