diff options
author | Thomas Wolf <thomas.wolf@paranor.ch> | 2018-09-08 14:59:08 +0200 |
---|---|---|
committer | Matthias Sohn <matthias.sohn@sap.com> | 2018-09-15 00:57:13 +0200 |
commit | 50deacdd57cd348554264fe26f3fd8177830071d (patch) | |
tree | c09cc1dff6377fb087ae332b77c048ca85dfc00d | |
parent | 54a502f6c6f416dc570f023ffb48f05efd3af3ff (diff) | |
download | jgit-50deacdd57cd348554264fe26f3fd8177830071d.tar.gz jgit-50deacdd57cd348554264fe26f3fd8177830071d.zip |
Set TagOpt.AUTO_FOLLOW when not cloning all branches
Otherwise fetching all tags may pull in commits not on the
specified branches. Canonical git also does this.[1]
[1] https://github.com/git/git/blob/b160b6e69/builtin/clone.c#L1124
Bug: 538768
Change-Id: If0ac75fb9fae0c95d1a48b22954c54d4c3c09a47
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CloneCommandTest.java | 4 | ||||
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java | 16 |
2 files changed, 12 insertions, 8 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 0ad067f6a7..6b5fe502b7 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 @@ -381,6 +381,8 @@ public class CloneCommandTest extends RepositoryTestCase { Git git2 = command.call(); addRepoToClose(git2.getRepository()); assertNotNull(git2); + assertNull(git2.getRepository().resolve("tag-for-blob")); + assertNotNull(git2.getRepository().resolve("tag-initial")); assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master"); assertEquals("refs/remotes/origin/master", allRefNames(git2 .branchList().setListMode(ListMode.REMOTE).call())); @@ -408,6 +410,8 @@ public class CloneCommandTest extends RepositoryTestCase { Git git2 = command.call(); addRepoToClose(git2.getRepository()); assertNotNull(git2); + assertNull(git2.getRepository().resolve("tag-for-blob")); + assertNotNull(git2.getRepository().resolve("tag-initial")); assertEquals(git2.getRepository().getFullBranch(), "refs/heads/master"); assertEquals("refs/heads/master", allRefNames(git2.branchList() .setListMode(ListMode.ALL).call())); 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 5ea7cf8718..73af8ba16d 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java @@ -283,10 +283,11 @@ public class CloneCommand extends TransportCommand<CloneCommand, Git> { config.addURI(u); final String dst = (bare ? Constants.R_HEADS : Constants.R_REMOTES - + config.getName() + "/") + "*"; //$NON-NLS-1$//$NON-NLS-2$ - List<RefSpec> refSpecs = calculateRefSpecs(dst); + + config.getName() + '/') + '*'; + boolean fetchAll = cloneAllBranches || branchesToClone == null + || branchesToClone.isEmpty(); - config.setFetchRefSpecs(refSpecs); + config.setFetchRefSpecs(calculateRefSpecs(fetchAll, dst)); config.update(clonedRepo.getConfig()); clonedRepo.getConfig().save(); @@ -295,19 +296,18 @@ public class CloneCommand extends TransportCommand<CloneCommand, Git> { FetchCommand command = new FetchCommand(clonedRepo); command.setRemote(remote); command.setProgressMonitor(monitor); - command.setTagOpt(TagOpt.FETCH_TAGS); + command.setTagOpt(fetchAll ? TagOpt.FETCH_TAGS : TagOpt.AUTO_FOLLOW); configure(command); return command.call(); } - private List<RefSpec> calculateRefSpecs(String dst) { + private List<RefSpec> calculateRefSpecs(boolean fetchAll, String dst) { RefSpec wcrs = new RefSpec(); wcrs = wcrs.setForceUpdate(true); - wcrs = wcrs.setSourceDestination(Constants.R_HEADS + "*", dst); //$NON-NLS-1$ + wcrs = wcrs.setSourceDestination(Constants.R_HEADS + '*', dst); List<RefSpec> specs = new ArrayList<>(); - if (!cloneAllBranches && branchesToClone != null - && !branchesToClone.isEmpty()) { + if (!fetchAll) { for (String selectedRef : branchesToClone) { if (wcrs.matchSource(selectedRef)) { specs.add(wcrs.expandFromSource(selectedRef)); |