[hg] suppress output by default, make some commands silent.
authorKirill Likhodedov <kirill.likhodedov@jetbrains.com>
Thu, 7 Apr 2011 13:27:26 +0000 (17:27 +0400)
committerKirill Likhodedov <kirill.likhodedov@jetbrains.com>
Fri, 8 Apr 2011 10:22:20 +0000 (14:22 +0400)
plugins/hg4idea/src/org/zmlx/hg4idea/command/HgCloneCommand.java
plugins/hg4idea/src/org/zmlx/hg4idea/command/HgIdentifyCommand.java
plugins/hg4idea/src/org/zmlx/hg4idea/command/HgInitCommand.java
plugins/hg4idea/src/org/zmlx/hg4idea/command/HgMergeCommand.java
plugins/hg4idea/src/org/zmlx/hg4idea/command/HgParentsCommand.java
plugins/hg4idea/src/org/zmlx/hg4idea/command/HgPullCommand.java
plugins/hg4idea/src/org/zmlx/hg4idea/command/HgPushCommand.java
plugins/hg4idea/src/org/zmlx/hg4idea/command/HgResolveCommand.java
plugins/hg4idea/src/org/zmlx/hg4idea/command/HgUpdateCommand.java
plugins/hg4idea/src/org/zmlx/hg4idea/command/HgWorkingCopyRevisionsCommand.java
plugins/hg4idea/src/org/zmlx/hg4idea/execution/HgCommandExecutor.java

index e71e90b88e7c71892b5c604e90ee958b1e9519a9..16df8fc6235ced3200acfca4a5d83c3d27004856 100644 (file)
@@ -31,6 +31,8 @@ public class HgCloneCommand {
     final List<String> arguments = new ArrayList<String>(2);
     arguments.add(repositoryURL);
     arguments.add(directory);
-    return new HgCommandExecutor(project).executeInCurrentThread(null, "clone", arguments);
+    final HgCommandExecutor executor = new HgCommandExecutor(project);
+    executor.setShowOutput(true);
+    return executor.executeInCurrentThread(null, "clone", arguments);
   }
 }
index f0d2e4d16572f3a6a3a6bbaec05bf08bfe86008f..fe65c06bd869245878f08957051e3fe980f64c10 100644 (file)
@@ -29,6 +29,8 @@ public class HgIdentifyCommand {
   public HgCommandResult execute() {
     final List<String> arguments = new LinkedList<String>();
     arguments.add(source);
-    return new HgCommandExecutor(project).executeInCurrentThread(null, "identify", arguments);
+    final HgCommandExecutor executor = new HgCommandExecutor(project);
+    executor.setSilent(true);
+    return executor.executeInCurrentThread(null, "identify", arguments);
   }
 }
index b2a87ed6bb923d2b247549f2b4e81ebd462580c5..9043571410b097335638539d4bb6a5bb1c055c5a 100644 (file)
@@ -27,8 +27,11 @@ public class HgInitCommand {
   public void execute(@NotNull VirtualFile repositoryRoot, final Consumer<Boolean> booleanResultHandler) {
     final List<String> args = new ArrayList<String>(1);
     args.add(repositoryRoot.getPath());
-    new HgCommandExecutor(myProject).execute(null, "init", args, new HgCommandResultHandler() {
-      @Override public void process(@Nullable HgCommandResult result) {
+    final HgCommandExecutor executor = new HgCommandExecutor(myProject);
+    executor.setShowOutput(true);
+    executor.execute(null, "init", args, new HgCommandResultHandler() {
+      @Override
+      public void process(@Nullable HgCommandResult result) {
         booleanResultHandler.consume(result != null && !HgErrorUtil.isAbort(result));
       }
     });
index 3737f14b6590bdeec21946f5206ae586d8fb979d..600c8be8c4a30bdb24aba6f33466b9cf6b8ed7cf 100644 (file)
@@ -46,6 +46,7 @@ public class HgMergeCommand {
   @Nullable
   public HgCommandResult execute() {
     HgCommandExecutor commandExecutor = new HgCommandExecutor(project);
+    commandExecutor.setShowOutput(true);
     List<String> arguments = new LinkedList<String>();
     if (StringUtils.isNotBlank(revision)) {
       arguments.add("--rev");
index cc3a0339b90159037c8dbe49d1e091e8278e4151..d072540feb840209ddceeaf49f7537ed8ae53ad0 100644 (file)
@@ -36,4 +36,9 @@ public class HgParentsCommand extends HgChangesetsCommand{
       args.add(revision.getChangeset());
     }
   }
+
+  @Override
+  protected boolean isSilentCommand() {
+    return true;
+  }
 }
index fa9809ccc8e142ad971dbfed50fae0ab79bf4245..c3843396948ba5e0c8a56131e1a826e2ec0c5a47 100644 (file)
@@ -71,7 +71,9 @@ public class HgPullCommand {
 
     arguments.add(source);
 
-    new HgCommandExecutor(project).execute(repo, "pull", arguments, new HgCommandResultHandler() {
+    final HgCommandExecutor executor = new HgCommandExecutor(project);
+    executor.setShowOutput(true);
+    executor.execute(repo, "pull", arguments, new HgCommandResultHandler() {
       @Override
       public void process(@Nullable HgCommandResult result) {
         project.getMessageBus().syncPublisher(HgVcs.REMOTE_TOPIC).update(project);
index c241cca09e4af3ee7a6fe81b0536b1d57ba0d2ac..23ad409064a0012f5814398e0ba63fe8500c94f7 100644 (file)
@@ -68,7 +68,9 @@ public class HgPushCommand {
     }
     arguments.add(myDestination);
 
-    new HgCommandExecutor(myProject).execute(myRepo, "push", arguments, new HgCommandResultHandler() {
+    final HgCommandExecutor executor = new HgCommandExecutor(myProject);
+    executor.setShowOutput(true);
+    executor.execute(myRepo, "push", arguments, new HgCommandResultHandler() {
       @Override
       public void process(@Nullable HgCommandResult result) {
         myProject.getMessageBus().syncPublisher(HgVcs.REMOTE_TOPIC).update(myProject);
index 18babd1be74a81e304483b6e9336d77e67c96f3b..e38814773a1f60da4c97c8673833cfe2d705f341 100644 (file)
@@ -44,7 +44,9 @@ public class HgResolveCommand {
     if (repo == null) {
       return Collections.emptyMap();
     }
-    final HgCommandResult result = new HgCommandExecutor(myProject).executeInCurrentThread(repo, "resolve", Arrays.asList("--list"));
+    final HgCommandExecutor executor = new HgCommandExecutor(myProject);
+    executor.setSilent(true);
+    final HgCommandResult result = executor.executeInCurrentThread(repo, "resolve", Arrays.asList("--list"));
     if (result == null) {
       return Collections.emptyMap();
     }
@@ -55,7 +57,9 @@ public class HgResolveCommand {
     if (repo == null) {
       resultHandler.consume(Collections.<HgFile, HgResolveStatusEnum>emptyMap());
     }
-    new HgCommandExecutor(myProject).execute(repo, "resolve", Arrays.asList("--list"), new HgCommandResultHandler() {
+    final HgCommandExecutor executor = new HgCommandExecutor(myProject);
+    executor.setSilent(true);
+    executor.execute(repo, "resolve", Arrays.asList("--list"), new HgCommandResultHandler() {
       @Override
       public void process(@Nullable HgCommandResult result) {
         if (result == null) {
index 693201bab5ec4c11065f70d7f339015ae219f0c4..b4f6c28ab145ab724861059a852693e1689c13a2 100644 (file)
@@ -63,7 +63,9 @@ public class HgUpdateCommand {
       arguments.add(branch);
     }
 
-    return new HgCommandExecutor(project).executeInCurrentThread(repo, "update", arguments);
+    final HgCommandExecutor executor = new HgCommandExecutor(project);
+    executor.setShowOutput(true);
+    return executor.executeInCurrentThread(repo, "update", arguments);
   }
 
 }
index f83ee80ce858315c35436353a2199a4d61c9b060..db4e5de1ab98d8fdc9407dafb567d18f2fc1798d 100644 (file)
@@ -51,7 +51,7 @@ public class HgWorkingCopyRevisionsCommand {
    */
   @NotNull
   public List<HgRevisionNumber> parents(@NotNull VirtualFile repo) {
-    return getRevisions(repo, "parents", null, null);
+    return getRevisions(repo, "parents", null, null, true);
   }
 
   /**
@@ -95,7 +95,7 @@ public class HgWorkingCopyRevisionsCommand {
    */
   @NotNull
   public Pair<HgRevisionNumber, HgRevisionNumber> parents(@NotNull VirtualFile repo, @Nullable FilePath file, @Nullable HgRevisionNumber revision) {
-    final List<HgRevisionNumber> revisions = getRevisions(repo, "parents", file, revision);
+    final List<HgRevisionNumber> revisions = getRevisions(repo, "parents", file, revision, true);
     switch (revisions.size()) {
       case 1: return Pair.create(revisions.get(0), null);
       case 2: return Pair.create(revisions.get(0), revisions.get(1));
@@ -117,7 +117,7 @@ public class HgWorkingCopyRevisionsCommand {
 
   @Nullable
   public HgRevisionNumber tip(@NotNull VirtualFile repo) {
-    List<HgRevisionNumber> tips = getRevisions(repo, "tip", null, null);
+    List<HgRevisionNumber> tips = getRevisions(repo, "tip", null, null, true);
     if (tips.size() > 1) {
       throw new IllegalStateException("There cannot be multiple tips");
     }
@@ -130,6 +130,7 @@ public class HgWorkingCopyRevisionsCommand {
   @Nullable
   public HgRevisionNumber identify(@NotNull VirtualFile repo) {
     HgCommandExecutor commandExecutor = new HgCommandExecutor(myProject);
+    commandExecutor.setSilent(true);
     HgCommandResult result = commandExecutor.executeInCurrentThread(repo, "identify", Arrays.asList("--num", "--id"));
     if (result == null) {
       return HgRevisionNumber.NULL_REVISION_NUMBER;
@@ -154,12 +155,14 @@ public class HgWorkingCopyRevisionsCommand {
    * @param command  command to execute.
    * @param file     file which revisions are wanted. If <code><b>null</b></code> then repository revisions are considered.
    * @param revision revision to execute on. If <code><b>null</b></code> then executed without the '-r' parameter, i.e. on the latest revision.
+   * @param silent   pass true if this command shouldn't be mentioned in the VCS console.
    * @return List of revisions.
    */
   private List<HgRevisionNumber> getRevisions(@NotNull VirtualFile repo,
                                               @NotNull String command,
                                               @Nullable FilePath file,
-                                              @Nullable HgRevisionNumber revision) {
+                                              @Nullable HgRevisionNumber revision,
+                                              boolean silent) {
     final List<String> args = new LinkedList<String>();
     args.add("--template");
     args.add("{rev}|{node|short}\\n");
@@ -170,7 +173,9 @@ public class HgWorkingCopyRevisionsCommand {
     if (file != null) { // NB: this must be the last argument
       args.add(HgUtil.getOriginalFileName(file, ChangeListManager.getInstance(myProject)).getPath());
     }
-    final HgCommandResult result = new HgCommandExecutor(myProject).executeInCurrentThread(repo, command, args);
+    final HgCommandExecutor executor = new HgCommandExecutor(myProject);
+    executor.setSilent(silent);
+    final HgCommandResult result = executor.executeInCurrentThread(repo, command, args);
 
     if (result == null) {
       return new ArrayList<HgRevisionNumber>(0);
index 9cb286d23436c10b5eaa97c88d44767cedb8e8a7..c9cdfee3fb7d40812cefd9e9b7d5dfa7be2aaf09 100644 (file)
@@ -36,21 +36,29 @@ import java.util.Arrays;
 import java.util.LinkedList;
 import java.util.List;
 
+/**
+ * <p>Executes an hg external command synchronously or asynchronously with the consequent call of {@link HgCommandResultHandler}</p>
+ *
+ * <p>Silence policy:
+ * <li>if the command is silent, the fact of its execution will be recorded in the log, but not in the VCS console.
+ * <li>if the command is not silent, which is default, it is written in the log and console.
+ * <li>the command output is not written to the log or shown to console by default, but it can be changed via {@link #myShowOutput}
+ * <li>error output is logged to the console and log, if the command is not silent.
+ * </p>
+ */
 public final class HgCommandExecutor {
   
-  public static final List<String> DEFAULT_OPTIONS = Arrays.asList(
-    "--config", "ui.merge=internal:merge"
-  );
-
   private static final Logger LOG = Logger.getInstance(HgCommandExecutor.class.getName());
+  private static final List<String> DEFAULT_OPTIONS = Arrays.asList("--config", "ui.merge=internal:merge");
 
   private final Project myProject;
   private final HgGlobalSettings mySettings;
   private final HgExecutableValidator myValidator;
   private final HgVcs myVcs;
 
-  private Charset myCharset;
-  private boolean myIsSilent;
+  private Charset myCharset = Charset.defaultCharset();
+  private boolean myIsSilent = false;
+  private boolean myShowOutput = false;
   private List<String> myOptions = DEFAULT_OPTIONS;
 
   public HgCommandExecutor(Project project) {
@@ -58,8 +66,6 @@ public final class HgCommandExecutor {
     myVcs = HgVcs.getInstance(myProject);
     mySettings = myVcs.getGlobalSettings();
     myValidator = myVcs.getExecutableValidator();
-    myCharset = Charset.defaultCharset();
-    myIsSilent = false;
   }
 
   public void setCharset(Charset charset) {
@@ -70,6 +76,14 @@ public final class HgCommandExecutor {
     myIsSilent = isSilent;
   }
 
+  public void setOptions(List<String> options) {
+    myOptions = options;
+  }
+
+  public void setShowOutput(boolean showOutput) {
+    myShowOutput = showOutput;
+  }
+
   public void execute(@Nullable final VirtualFile repo, final String operation, final List<String> arguments, @Nullable final HgCommandResultHandler handler) {
     ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
       @Override
@@ -159,23 +173,32 @@ public final class HgCommandExecutor {
     String warnings = warningReceiver.getWarnings();
     result.setWarnings(warnings);
 
-    // logging to the Version Control console (without extensions and configs)
-    final String cmdString = String.format("%s %s %s", mySettings.isRunViaBash() ? "bash -c " + HgVcs.HG_EXECUTABLE_FILE_NAME : HgVcs.HG_EXECUTABLE_FILE_NAME, operation,
-            StringUtils.join(maskAuthInfoFromUrl(arguments), " "));
-    myVcs.showMessageInConsole(cmdString, ConsoleViewContentType.NORMAL_OUTPUT.getAttributes());
+    log(operation, arguments, result);
+    return result;
+  }
+
+  // logging to the Version Control console (without extensions and configs)
+  private void log(String operation, List<String> arguments, HgCommandResult result) {
+    final String executable = mySettings.isRunViaBash() ? "bash -c " + HgVcs.HG_EXECUTABLE_FILE_NAME : HgVcs.HG_EXECUTABLE_FILE_NAME;
+    final String cmdString = String.format("%s %s %s", executable, operation, StringUtils.join(maskAuthInfoFromUrl(arguments), " "));
+
+    // log command
     LOG.info(cmdString);
     if (!myIsSilent) {
-      myVcs.showMessageInConsole(result.getRawOutput(), ConsoleViewContentType.SYSTEM_OUTPUT.getAttributes());
-      LOG.info(result.getRawOutput());
+      myVcs.showMessageInConsole(cmdString, ConsoleViewContentType.NORMAL_OUTPUT.getAttributes());
     }
-    myVcs.showMessageInConsole(result.getRawError(), ConsoleViewContentType.ERROR_OUTPUT.getAttributes());
-    LOG.info(result.getRawError());
 
-    return result;
-  }
+    // log output if needed
+    if (!myIsSilent && myShowOutput) {
+      LOG.info(result.getRawOutput());
+      myVcs.showMessageInConsole(result.getRawOutput(), ConsoleViewContentType.SYSTEM_OUTPUT.getAttributes());
+    }
 
-  public void setOptions(List<String> options) {
-    myOptions = options;
+    // log error
+    if (!myIsSilent) {
+      LOG.info(result.getRawError());
+      myVcs.showMessageInConsole(result.getRawError(), ConsoleViewContentType.ERROR_OUTPUT.getAttributes());
+    }
   }
 
   /**