svn: Refactored "QuickMerge" - code simplified
authorKonstantin Kolosovsky <konstantin.kolosovsky@jetbrains.com>
Mon, 26 Sep 2016 19:27:25 +0000 (22:27 +0300)
committerKonstantin Kolosovsky <konstantin.kolosovsky@jetbrains.com>
Tue, 11 Oct 2016 01:28:23 +0000 (04:28 +0300)
plugins/svn4idea/src/org/jetbrains/idea/svn/dialogs/CopiesPanel.java
plugins/svn4idea/src/org/jetbrains/idea/svn/integrate/QuickMerge.java
plugins/svn4idea/testSource/org/jetbrains/idea/svn/SvnQuickMergeTest.java

index d40e91cd1c901367a92d68fafc00fdceeec48989..c80683689fb7c862dc691d12777a5c18e7a34d7f 100644 (file)
@@ -369,7 +369,7 @@ public class CopiesPanel {
         String workingCopyUrlInSelectedBranch = getCorrespondingUrlInOtherBranch(configuration, wcInfo.getUrl(), branchUrl);
         MergeContext mergeContext = new MergeContext(myVcs, workingCopyUrlInSelectedBranch, wcInfo, SVNPathUtil.tail(branchUrl), root);
 
-        new QuickMerge(mergeContext).execute(new QuickMergeInteractionImpl(mergeContext));
+        new QuickMerge(mergeContext, new QuickMergeInteractionImpl(mergeContext)).execute();
       }
     }, "Select branch", mergeLabel);
   }
index 462683f78e8f5955d380e4d3d245822dbf2c1279..a3a9a73798b6a9b0ea8fdbd1810702a4b24c7872 100644 (file)
@@ -17,62 +17,63 @@ package org.jetbrains.idea.svn.integrate;
 
 import com.intellij.openapi.fileEditor.FileDocumentManager;
 import com.intellij.openapi.vcs.VcsException;
-import com.intellij.util.Consumer;
 import com.intellij.util.continuation.Continuation;
 import com.intellij.util.continuation.TaskDescriptor;
 import org.jetbrains.annotations.CalledInAwt;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.jetbrains.annotations.TestOnly;
 import org.tmatesoft.svn.core.SVNException;
 
-import java.util.Arrays;
-import java.util.LinkedList;
-import java.util.List;
-
+import static com.intellij.util.Functions.identity;
 import static com.intellij.util.ObjectUtils.notNull;
+import static com.intellij.util.containers.ContainerUtil.mapNotNull;
 import static java.util.Collections.singletonList;
 
 public class QuickMerge {
 
   @NotNull private final MergeContext myMergeContext;
-  private final Continuation myContinuation;
-  private QuickMergeInteraction myInteraction;
+  @NotNull private final QuickMergeInteraction myInteraction;
 
-  public QuickMerge(@NotNull MergeContext mergeContext) {
+  public QuickMerge(@NotNull MergeContext mergeContext, @NotNull QuickMergeInteraction interaction) {
     myMergeContext = mergeContext;
-    myContinuation = Continuation.createFragmented(mergeContext.getProject(), true);
+    myInteraction = interaction;
   }
 
   @CalledInAwt
-  public void execute(@NotNull final QuickMergeInteraction interaction, @NotNull final TaskDescriptor... finalTasks) {
-    myInteraction = interaction;
+  public void execute() {
+    runMergeTasks(null);
+  }
+
+  @TestOnly
+  @CalledInAwt
+  public void execute(@NotNull TaskDescriptor finalTask) {
+    runMergeTasks(finalTask);
+  }
 
+  @CalledInAwt
+  private void runMergeTasks(@Nullable TaskDescriptor finalTask) {
     FileDocumentManager.getInstance().saveAllDocuments();
 
-    final List<TaskDescriptor> tasks = new LinkedList<>();
-    tasks.add(new MergeInitChecksTask(myMergeContext, myInteraction));
-    tasks.add(new CheckRepositorySupportsMergeInfoTask(myMergeContext, myInteraction));
-    if (finalTasks.length > 0) {
-      tasks.addAll(Arrays.asList(finalTasks));
-    }
+    TaskDescriptor[] tasks = {
+      new MergeInitChecksTask(myMergeContext, myInteraction),
+      new CheckRepositorySupportsMergeInfoTask(myMergeContext, myInteraction),
+      finalTask
+    };
+
+    createContinuation().run(mapNotNull(tasks, identity()));
+  }
+
+  @NotNull
+  private Continuation createContinuation() {
+    Continuation result = Continuation.createFragmented(myMergeContext.getProject(), true);
+
+    result.addExceptionHandler(VcsException.class, e -> myInteraction.showErrors(myMergeContext.getTitle(), singletonList(e)));
+    result.addExceptionHandler(SVNException.class,
+                               e -> myInteraction.showErrors(myMergeContext.getTitle(), singletonList(new VcsException(e))));
+    result.addExceptionHandler(RuntimeException.class, e -> myInteraction
+      .showErrors(notNull(e.getMessage(), e.getClass().getName()), singletonList(new VcsException(e))));
 
-    myContinuation.addExceptionHandler(VcsException.class, new Consumer<VcsException>() {
-      @Override
-      public void consume(VcsException e) {
-        myInteraction.showErrors(myMergeContext.getTitle(), singletonList(e));
-      }
-    });
-    myContinuation.addExceptionHandler(SVNException.class, new Consumer<SVNException>() {
-      @Override
-      public void consume(SVNException e) {
-        myInteraction.showErrors(myMergeContext.getTitle(), singletonList(new VcsException(e)));
-      }
-    });
-    myContinuation.addExceptionHandler(RuntimeException.class, new Consumer<RuntimeException>() {
-      @Override
-      public void consume(RuntimeException e) {
-        myInteraction.showErrors(notNull(e.getMessage(), e.getClass().getName()), singletonList(new VcsException(e)));
-      }
-    });
-    myContinuation.run(tasks);
+    return result;
   }
 }
index 2fb380cdf2a5851fffa21417275a0548ad87cde8..ec1373e09972babb69ec6bce8fa4c63bfaf638da 100644 (file)
@@ -379,10 +379,10 @@ public class SvnQuickMergeTest extends Svn17TestCase {
 
   private void waitQuickMerge(@NotNull String sourceUrl, @NotNull QuickMergeTestInteraction interaction) throws Exception {
     MergeContext mergeContext = new MergeContext(myVcs, sourceUrl, getWcInfo(), SVNPathUtil.tail(sourceUrl), myWorkingCopyDir);
-    QuickMerge quickMerge = new QuickMerge(mergeContext);
+    QuickMerge quickMerge = new QuickMerge(mergeContext, interaction);
     WaitingTaskDescriptor descriptor = new WaitingTaskDescriptor();
 
-    ApplicationManager.getApplication().invokeLater(() -> quickMerge.execute(interaction, descriptor));
+    ApplicationManager.getApplication().invokeLater(() -> quickMerge.execute(descriptor));
 
     descriptor.waitForCompletion();
     interaction.throwIfExceptions();