From 986de051566aa4d585be8613ba8b4028a3271be6 Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Tue, 30 Jul 2019 12:04:16 +0300 Subject: [PATCH] hg: simplify: don't use the asynchronous HgCommandExecutor#execute This method just wraps the code into BackgroundTaskUtil.executeOnPooledThread(). On the other hand, its signature is not easy to read and understand. So better to inline the method and call executeOnPooledThread() directly. The method is used in a 3-party plugin => keep and deprecate it. GitOrigin-RevId: d467ca103a3f9e9a073e5ec87a70bed6eb8aaca1 --- .../action/mq/HgQFinishFromLogAction.java | 22 ++++++-- .../hg4idea/command/HgResolveCommand.java | 25 ++++----- .../hg4idea/command/HgTagCreateCommand.java | 10 +++- .../hg4idea/command/mq/HgQFinishCommand.java | 51 ------------------- .../hg4idea/command/mq/HgQRenameCommand.java | 31 +++++------ .../hg4idea/execution/HgCommandExecutor.java | 6 +++ 6 files changed, 59 insertions(+), 86 deletions(-) delete mode 100644 plugins/hg4idea/src/org/zmlx/hg4idea/command/mq/HgQFinishCommand.java diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/action/mq/HgQFinishFromLogAction.java b/plugins/hg4idea/src/org/zmlx/hg4idea/action/mq/HgQFinishFromLogAction.java index fcb3e5bfb650..f9e9970f697d 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/action/mq/HgQFinishFromLogAction.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/action/mq/HgQFinishFromLogAction.java @@ -15,15 +15,31 @@ */ package org.zmlx.hg4idea.action.mq; +import com.intellij.openapi.progress.util.BackgroundTaskUtil; +import com.intellij.openapi.project.Project; import com.intellij.vcs.log.Hash; import org.jetbrains.annotations.NotNull; -import org.zmlx.hg4idea.command.mq.HgQFinishCommand; +import org.zmlx.hg4idea.action.HgCommandResultNotifier; +import org.zmlx.hg4idea.execution.HgCommandExecutor; +import org.zmlx.hg4idea.execution.HgCommandResult; import org.zmlx.hg4idea.repo.HgRepository; +import org.zmlx.hg4idea.util.HgErrorUtil; + +import static java.util.Collections.singletonList; public class HgQFinishFromLogAction extends HgMqAppliedPatchAction { @Override protected void actionPerformed(@NotNull HgRepository repository, @NotNull Hash commit) { - String revisionHash = commit.asString(); - new HgQFinishCommand(repository).execute(revisionHash); + String revision = commit.asString(); + Project project = repository.getProject(); + BackgroundTaskUtil.executeOnPooledThread(project, () -> { + HgCommandExecutor executor = new HgCommandExecutor(project); + HgCommandResult result = executor.executeInCurrentThread(repository.getRoot(), "qfinish", singletonList("qbase:" + revision)); + if (HgErrorUtil.hasErrorsInCommandExecution(result)) { + new HgCommandResultNotifier(project).notifyError(result, "QFinish command failed", + "Could not apply patches into repository history."); + } + repository.update(); + }); } } diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgResolveCommand.java b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgResolveCommand.java index df66b1befa5f..1613ca564119 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgResolveCommand.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgResolveCommand.java @@ -12,6 +12,7 @@ // limitations under the License. package org.zmlx.hg4idea.command; +import com.intellij.openapi.progress.util.BackgroundTaskUtil; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vcs.FilePath; @@ -20,11 +21,9 @@ import com.intellij.util.Consumer; import com.intellij.vcsUtil.VcsFileUtil; import com.intellij.vcsUtil.VcsUtil; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.zmlx.hg4idea.HgFile; import org.zmlx.hg4idea.execution.HgCommandExecutor; import org.zmlx.hg4idea.execution.HgCommandResult; -import org.zmlx.hg4idea.execution.HgCommandResultHandler; import java.io.File; import java.util.*; @@ -57,18 +56,15 @@ public class HgResolveCommand { if (repo == null) { resultHandler.consume(Collections.emptyMap()); } - final HgCommandExecutor executor = new HgCommandExecutor(myProject); + HgCommandExecutor executor = new HgCommandExecutor(myProject); executor.setSilent(true); - executor.execute(repo, "resolve", Collections.singletonList("--list"), new HgCommandResultHandler() { - @Override - public void process(@Nullable HgCommandResult result) { - if (result == null) { - resultHandler.consume(Collections.emptyMap()); - } - - final Map resolveStatus = handleResult(repo, result); - resultHandler.consume(resolveStatus); + BackgroundTaskUtil.executeOnPooledThread(myProject, () -> { + HgCommandResult result = executor.executeInCurrentThread(repo, "resolve", Collections.singletonList("--list")); + if (result == null) { + resultHandler.consume(Collections.emptyMap()); } + Map resolveStatus = handleResult(repo, result); + resultHandler.consume(resolveStatus); }); } @@ -93,10 +89,11 @@ public class HgResolveCommand { public void markResolved(@NotNull VirtualFile repo, @NotNull Collection paths) { for (List chunk : VcsFileUtil.chunkPaths(repo, paths)) { - final List args = new ArrayList<>(); + List args = new ArrayList<>(); args.add("--mark"); args.addAll(chunk); - new HgCommandExecutor(myProject).execute(repo, "resolve", args, null); + BackgroundTaskUtil.executeOnPooledThread(myProject, () -> + new HgCommandExecutor(myProject).executeInCurrentThread(repo, "resolve", args)); } } diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgTagCreateCommand.java b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgTagCreateCommand.java index 416ae735cd3c..19b6d8f68554 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgTagCreateCommand.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/command/HgTagCreateCommand.java @@ -12,12 +12,14 @@ // limitations under the License. package org.zmlx.hg4idea.command; +import com.intellij.openapi.progress.util.BackgroundTaskUtil; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VirtualFile; import org.jetbrains.annotations.NotNull; import org.zmlx.hg4idea.execution.HgCommandException; import org.zmlx.hg4idea.execution.HgCommandExecutor; +import org.zmlx.hg4idea.execution.HgCommandResult; import org.zmlx.hg4idea.execution.HgCommandResultHandler; import org.zmlx.hg4idea.repo.HgRepositoryManager; import org.zmlx.hg4idea.util.HgUtil; @@ -49,7 +51,13 @@ public class HgTagCreateCommand { arguments.add("--rev"); arguments.add(revisionNumberOrHash); } - new HgCommandExecutor(project).execute(repo, "tag", arguments, resultHandler); + HgCommandExecutor executor = new HgCommandExecutor(project); + BackgroundTaskUtil.executeOnPooledThread(project, () -> { + HgCommandResult result = executor.executeInCurrentThread(repo, "tag", arguments); + if (resultHandler != null) { + resultHandler.process(result); + } + }); if (!project.isDisposed()) { HgRepositoryManager manager = HgUtil.getRepositoryManager(project); manager.updateRepository(repo); diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/command/mq/HgQFinishCommand.java b/plugins/hg4idea/src/org/zmlx/hg4idea/command/mq/HgQFinishCommand.java deleted file mode 100644 index 8d7da8b4e8ee..000000000000 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/command/mq/HgQFinishCommand.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2000-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.zmlx.hg4idea.command.mq; - -import com.intellij.openapi.project.Project; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.zmlx.hg4idea.action.HgCommandResultNotifier; -import org.zmlx.hg4idea.execution.HgCommandExecutor; -import org.zmlx.hg4idea.execution.HgCommandResult; -import org.zmlx.hg4idea.execution.HgCommandResultHandler; -import org.zmlx.hg4idea.repo.HgRepository; -import org.zmlx.hg4idea.util.HgErrorUtil; - -import java.util.Collections; - -public class HgQFinishCommand { - @NotNull private final HgRepository myRepository; - - public HgQFinishCommand(@NotNull HgRepository repository) { - myRepository = repository; - } - - public void execute(@NotNull final String revision) { - final Project project = myRepository.getProject(); - new HgCommandExecutor(project) - .execute(myRepository.getRoot(), "qfinish", Collections.singletonList("qbase:" + revision), new HgCommandResultHandler() { - @Override - public void process(@Nullable HgCommandResult result) { - if (HgErrorUtil.hasErrorsInCommandExecution(result)) { - new HgCommandResultNotifier(project) - .notifyError(result, "QFinish command failed", "Could not apply patches into repository history."); - } - myRepository.update(); - } - }); - } -} diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/command/mq/HgQRenameCommand.java b/plugins/hg4idea/src/org/zmlx/hg4idea/command/mq/HgQRenameCommand.java index e6181d6b4220..2cd9dbe46ca7 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/command/mq/HgQRenameCommand.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/command/mq/HgQRenameCommand.java @@ -16,17 +16,16 @@ package org.zmlx.hg4idea.command.mq; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.progress.util.BackgroundTaskUtil; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.Messages; import com.intellij.util.containers.ContainerUtil; import com.intellij.vcs.log.Hash; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.zmlx.hg4idea.HgNameWithHashInfo; import org.zmlx.hg4idea.action.HgCommandResultNotifier; import org.zmlx.hg4idea.execution.HgCommandExecutor; import org.zmlx.hg4idea.execution.HgCommandResult; -import org.zmlx.hg4idea.execution.HgCommandResultHandler; import org.zmlx.hg4idea.repo.HgRepository; import org.zmlx.hg4idea.util.HgErrorUtil; import org.zmlx.hg4idea.util.HgPatchReferenceValidator; @@ -58,21 +57,19 @@ public class HgQRenameCommand { } } - public static void performPatchRename(@NotNull final HgRepository repository, - @NotNull final String oldName, - @NotNull final String newName) { + public static void performPatchRename(@NotNull HgRepository repository, + @NotNull String oldName, + @NotNull String newName) { if (oldName.equals(newName)) return; - final Project project = repository.getProject(); - new HgCommandExecutor(project) - .execute(repository.getRoot(), "qrename", Arrays.asList(oldName, newName), new HgCommandResultHandler() { - @Override - public void process(@Nullable HgCommandResult result) { - if (HgErrorUtil.hasErrorsInCommandExecution(result)) { - new HgCommandResultNotifier(project) - .notifyError(result, "Qrename command failed", "Could not rename patch " + oldName + " to " + newName); - } - repository.update(); - } - }); + Project project = repository.getProject(); + BackgroundTaskUtil.executeOnPooledThread(project, () -> { + HgCommandExecutor executor = new HgCommandExecutor(project); + HgCommandResult result = executor.executeInCurrentThread(repository.getRoot(), "qrename", Arrays.asList(oldName, newName)); + if (HgErrorUtil.hasErrorsInCommandExecution(result)) { + new HgCommandResultNotifier(project).notifyError(result, "Qrename command failed", + "Could not rename patch " + oldName + " to " + newName); + } + repository.update(); + }); } } diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/execution/HgCommandExecutor.java b/plugins/hg4idea/src/org/zmlx/hg4idea/execution/HgCommandExecutor.java index 8dd51ea85280..9d5e6e5818d0 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/execution/HgCommandExecutor.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/execution/HgCommandExecutor.java @@ -16,6 +16,7 @@ package org.zmlx.hg4idea.execution; import com.intellij.execution.ui.ConsoleViewContentType; +import com.intellij.openapi.Disposable; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.progress.util.BackgroundTaskUtil; import com.intellij.openapi.project.Project; @@ -99,6 +100,11 @@ public class HgCommandExecutor { myOutputAlwaysSuppressed = outputAlwaysSuppressed; } + /** + * @deprecated Use synchronous versions of "execute", e.g. {@link #executeInCurrentThread(VirtualFile, String, List)}. + * Use {@link BackgroundTaskUtil#executeOnPooledThread(Disposable, Runnable)} if need to execute on a pooled thread. + */ + @Deprecated public void execute(@Nullable final VirtualFile repo, @NotNull final String operation, @Nullable final List arguments,