Browse Source

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>
stable-5.2
Thomas Wolf 9 years ago committed by Matthias Sohn
parent
commit
54a502f6c6
  1. 27
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java
  2. 23
      org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java

27
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java

@ -370,8 +370,7 @@ public class CloneCommandTest extends RepositoryTestCase {
} }
@Test @Test
public void testCloneRepositoryOnlyOneBranch() throws IOException, public void testCloneRepositoryOnlyOneBranch() throws Exception {
JGitInternalException, GitAPIException {
File directory = createTempDirectory("testCloneRepositoryWithBranch"); File directory = createTempDirectory("testCloneRepositoryWithBranch");
CloneCommand command = Git.cloneRepository(); CloneCommand command = Git.cloneRepository();
command.setBranch("refs/heads/master"); command.setBranch("refs/heads/master");
@ -385,22 +384,40 @@ public class CloneCommandTest extends RepositoryTestCase {
assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master"); assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
assertEquals("refs/remotes/origin/master", allRefNames(git2 assertEquals("refs/remotes/origin/master", allRefNames(git2
.branchList().setListMode(ListMode.REMOTE).call())); .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 // Same thing, but now test with bare repo
directory = createTempDirectory("testCloneRepositoryWithBranch_bare"); File directory = createTempDirectory(
command = Git.cloneRepository(); "testCloneRepositoryWithBranch_bare");
CloneCommand command = Git.cloneRepository();
command.setBranch("refs/heads/master"); command.setBranch("refs/heads/master");
command.setBranchesToClone(Collections command.setBranchesToClone(Collections
.singletonList("refs/heads/master")); .singletonList("refs/heads/master"));
command.setDirectory(directory); command.setDirectory(directory);
command.setURI(fileUri()); command.setURI(fileUri());
command.setBare(true); command.setBare(true);
git2 = command.call(); Git git2 = command.call();
addRepoToClose(git2.getRepository()); addRepoToClose(git2.getRepository());
assertNotNull(git2); assertNotNull(git2);
assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master"); assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master");
assertEquals("refs/heads/master", allRefNames(git2.branchList() assertEquals("refs/heads/master", allRefNames(git2.branchList()
.setListMode(ListMode.ALL).call())); .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) { public static String allRefNames(List<Ref> refs) {

23
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 final String dst = (bare ? Constants.R_HEADS : Constants.R_REMOTES
+ config.getName() + "/") + "*"; //$NON-NLS-1$//$NON-NLS-2$ + config.getName() + "/") + "*"; //$NON-NLS-1$//$NON-NLS-2$
RefSpec refSpec = new RefSpec(); List<RefSpec> refSpecs = calculateRefSpecs(dst);
refSpec = refSpec.setForceUpdate(true);
refSpec = refSpec.setSourceDestination(Constants.R_HEADS + "*", dst); //$NON-NLS-1$
config.addFetchRefSpec(refSpec); config.setFetchRefSpecs(refSpecs);
config.update(clonedRepo.getConfig()); config.update(clonedRepo.getConfig());
clonedRepo.getConfig().save(); clonedRepo.getConfig().save();
@ -300,9 +298,6 @@ public class CloneCommand extends TransportCommand<CloneCommand, Git> {
command.setTagOpt(TagOpt.FETCH_TAGS); command.setTagOpt(TagOpt.FETCH_TAGS);
configure(command); configure(command);
List<RefSpec> specs = calculateRefSpecs(dst);
command.setRefSpecs(specs);
return command.call(); return command.call();
} }
@ -311,13 +306,15 @@ public class CloneCommand extends TransportCommand<CloneCommand, Git> {
wcrs = wcrs.setForceUpdate(true); wcrs = wcrs.setForceUpdate(true);
wcrs = wcrs.setSourceDestination(Constants.R_HEADS + "*", dst); //$NON-NLS-1$ wcrs = wcrs.setSourceDestination(Constants.R_HEADS + "*", dst); //$NON-NLS-1$
List<RefSpec> specs = new ArrayList<>(); List<RefSpec> specs = new ArrayList<>();
if (cloneAllBranches) if (!cloneAllBranches && branchesToClone != null
specs.add(wcrs); && !branchesToClone.isEmpty()) {
else if (branchesToClone != null for (String selectedRef : branchesToClone) {
&& branchesToClone.size() > 0) { if (wcrs.matchSource(selectedRef)) {
for (String selectedRef : branchesToClone)
if (wcrs.matchSource(selectedRef))
specs.add(wcrs.expandFromSource(selectedRef)); specs.add(wcrs.expandFromSource(selectedRef));
}
}
} else {
specs.add(wcrs);
} }
return specs; return specs;
} }

Loading…
Cancel
Save