]> source.dussan.org Git - jgit.git/commitdiff
Fetch(Process): should tolerate duplicate refspecs 69/127669/3
authorMarc Strapetz <marc.strapetz@syntevo.com>
Sat, 30 Dec 2017 10:33:41 +0000 (11:33 +0100)
committerThomas Wolf <thomas.wolf@paranor.ch>
Wed, 22 Aug 2018 09:09:00 +0000 (11:09 +0200)
Bug: 529314
Change-Id: I91eaeda8a988d4786908fba6de00478cfc47a2a2
Signed-off-by: Marc Strapetz <marc.strapetz@syntevo.com>
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/FetchCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/FetchProcess.java

index 4a89a84cac4a35d456b2586752841a03a7342197..0622851822ea3cb5cc3a867e1d55e54d219bf713 100644 (file)
@@ -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));
+       }
 }
index dd26fe59ada80c5b39b6760d69eab9e015ea919d..39f0eea274aadb3bd94170e0df8d3a72185f1bfd 100644 (file)
@@ -481,12 +481,14 @@ class FetchProcess {
 
        private void deleteStaleTrackingRefs(FetchResult result,
                        BatchRefUpdate batch) throws IOException {
+               final Set<Ref> 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);
                                        }
                                }