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
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) {

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
+ 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;
}

Loading…
Cancel
Save