From 65a0cfc82a75146113b9e138cb9fdd5f04c018d0 Mon Sep 17 00:00:00 2001 From: Marc Strapetz Date: Sat, 30 Dec 2017 11:33:41 +0100 Subject: [PATCH] Fetch(Process): should tolerate duplicate refspecs Bug: 529314 Change-Id: I91eaeda8a988d4786908fba6de00478cfc47a2a2 Signed-off-by: Marc Strapetz Signed-off-by: Thomas Wolf --- .../eclipse/jgit/api/FetchCommandTest.java | 45 +++++++++++++++++++ .../eclipse/jgit/transport/FetchProcess.java | 4 +- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/FetchCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/FetchCommandTest.java index 4a89a84ca..062285182 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/FetchCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/FetchCommandTest.java @@ -56,6 +56,7 @@ import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.StoredConfig; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.transport.FetchResult; +import org.eclipse.jgit.transport.RefSpec; import org.eclipse.jgit.transport.RemoteConfig; import org.eclipse.jgit.transport.TagOpt; import org.eclipse.jgit.transport.TrackingRefUpdate; @@ -270,4 +271,48 @@ public class FetchCommandTest extends RepositoryTestCase { assertEquals(RefUpdate.Result.FORCED, update.getResult()); assertEquals(tagRef2.getObjectId(), db.resolve(tagName)); } + + @Test + public void fetchAddRefsWithDuplicateRefspec() throws Exception { + final String branchName = "branch"; + final String remoteBranchName = "test/" + branchName; + remoteGit.commit().setMessage("commit").call(); + Ref branchRef = remoteGit.branchCreate().setName(branchName).call(); + + final String spec1 = "+refs/heads/*:refs/remotes/test/*"; + final String spec2 = "refs/heads/*:refs/remotes/test/*"; + final StoredConfig config = db.getConfig(); + RemoteConfig remoteConfig = new RemoteConfig(config, "test"); + remoteConfig.addFetchRefSpec(new RefSpec(spec1)); + remoteConfig.addFetchRefSpec(new RefSpec(spec2)); + remoteConfig.update(config); + + git.fetch().setRemote("test").setRefSpecs(spec1).call(); + assertEquals(branchRef.getObjectId(), db.resolve(remoteBranchName)); + } + + @Test + public void fetchPruneRefsWithDuplicateRefspec() + throws Exception { + final String branchName = "branch"; + final String remoteBranchName = "test/" + branchName; + remoteGit.commit().setMessage("commit").call(); + Ref branchRef = remoteGit.branchCreate().setName(branchName).call(); + + final String spec1 = "+refs/heads/*:refs/remotes/test/*"; + final String spec2 = "refs/heads/*:refs/remotes/test/*"; + final StoredConfig config = db.getConfig(); + RemoteConfig remoteConfig = new RemoteConfig(config, "test"); + remoteConfig.addFetchRefSpec(new RefSpec(spec1)); + remoteConfig.addFetchRefSpec(new RefSpec(spec2)); + remoteConfig.update(config); + + git.fetch().setRemote("test").setRefSpecs(spec1).call(); + assertEquals(branchRef.getObjectId(), db.resolve(remoteBranchName)); + + remoteGit.branchDelete().setBranchNames(branchName).call(); + git.fetch().setRemote("test").setRefSpecs(spec1) + .setRemoveDeletedRefs(true).call(); + assertNull(db.resolve(remoteBranchName)); + } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/FetchProcess.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/FetchProcess.java index dd26fe59a..39f0eea27 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/FetchProcess.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/FetchProcess.java @@ -481,12 +481,14 @@ class FetchProcess { private void deleteStaleTrackingRefs(FetchResult result, BatchRefUpdate batch) throws IOException { + final Set processed = new HashSet<>(); for (final Ref ref : localRefs().values()) { final String refname = ref.getName(); for (final RefSpec spec : toFetch) { if (spec.matchDestination(refname)) { final RefSpec s = spec.expandFromDestination(refname); - if (result.getAdvertisedRef(s.getSource()) == null) { + if (result.getAdvertisedRef(s.getSource()) == null + && processed.add(ref)) { deleteTrackingRef(result, batch, s, ref); } }