changeset 824:f86c06f4ab8a

MercurialCommandLine now delegates to GeneralCommandLine
author eugene.petrenko@jetbrains.com
date Fri, 30 May 2014 11:49:44 +0200
parents 7a7a6e255c71
children 5fbc0d51bea3
files mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/AuthCommand.java mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommand.java mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CommandUtil.java mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/MercurialCommandLine.java mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/TagCommand.java mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/VersionCommand.java mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupportTest.java mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommandTest.java
diffstat 8 files changed, 98 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/AuthCommand.java	Fri May 30 11:32:12 2014 +0200
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/AuthCommand.java	Fri May 30 11:49:44 2014 +0200
@@ -16,7 +16,6 @@
 
 package jetbrains.buildServer.buildTriggers.vcs.mercurial.command;
 
-import com.intellij.execution.configurations.GeneralCommandLine;
 import org.jetbrains.annotations.NotNull;
 
 import java.io.File;
@@ -33,13 +32,14 @@
     super(commandSettings, hgPath, workingDir, authSettings);
   }
 
+  @NotNull
   protected MercurialCommandLine createCL() {
     MercurialCommandLine cmd = super.createCL();
     cmd.addParameters("--config", "ui.interactive=False");
     return cmd;
   }
 
-  protected void addHttpAuthParams(@NotNull GeneralCommandLine cmd) {
+  protected void addHttpAuthParams(@NotNull final MercurialCommandLine cmd) {
     if (myAuthSettings.getUsername() == null || myAuthSettings.getPassword() == null)
       return;
     cmd.addParameters("--config", "auth.tc.prefix=*");
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommand.java	Fri May 30 11:32:12 2014 +0200
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommand.java	Fri May 30 11:49:44 2014 +0200
@@ -58,7 +58,6 @@
     final MercurialCommandLine cl = new MercurialCommandLine(getPrivateData());
     cl.setExePath(myHgPath);
     cl.setEnvParams(myCommandSettings.getHgEnv());
-    cl.setPassParentEnvs(true);
 
     //include global arguments if any
     cl.addParameters(myCommandSettings.getGlobalArguments());
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CommandUtil.java	Fri May 30 11:32:12 2014 +0200
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CommandUtil.java	Fri May 30 11:49:44 2014 +0200
@@ -55,7 +55,7 @@
     } else {
       stderrBuffer = new ByteArrayOutputStream();
     }
-    ExecResult res = SimpleCommandLineProcessRunner.runCommandSecure(cli, command, null, new SimpleCommandLineProcessRunner.ProcessRunCallbackAdapter() {
+    ExecResult res = SimpleCommandLineProcessRunner.runCommandSecure(cli.toGeneralCommandLine(), command, null, new SimpleCommandLineProcessRunner.ProcessRunCallbackAdapter() {
       @Override
       public Integer getOutputIdleSecondsTimeout() {
         return executionTimeout;
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/MercurialCommandLine.java	Fri May 30 11:32:12 2014 +0200
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/MercurialCommandLine.java	Fri May 30 11:49:44 2014 +0200
@@ -22,10 +22,12 @@
 import org.jetbrains.annotations.NotNull;
 
 import java.nio.charset.Charset;
-import java.util.Collections;
-import java.util.Set;
+import java.util.*;
 
-public class MercurialCommandLine extends GeneralCommandLine {
+public class MercurialCommandLine {
+  private String myExePath;
+  private final List<String> myArguments = new ArrayList<String>();
+  private final Map<String, String> myEnvPatch = new TreeMap<String, String>();
 
   private String myWorkingDirectory;
   private final Set<String> myPrivateData;
@@ -35,36 +37,62 @@
     this(Collections.<String>emptySet());
   }
 
-  public MercurialCommandLine(@NotNull Set<String> privateData) {
+  public MercurialCommandLine(@NotNull final Set<String> privateData) {
     myPrivateData = privateData;
   }
 
-  @Override
+  @NotNull
   public String getCommandLineString() {
-    String original = super.getCommandLineString();
+    String original = toGeneralCommandLine().getCommandLineString();
     return CommandUtil.removePrivateData(original, myPrivateData);
   }
 
-  @Override
-  public void addParameter(@NotNull String parameter) {
-    String escaped = escape(parameter);
-    super.addParameter(escaped);
+  public void setExePath(@NotNull final String hgPath) {
+    myExePath = hgPath;
   }
 
-  @Override
+  public void addParameter(@NotNull final String parameter) {
+    String escaped = escape(parameter);
+    myArguments.add(escaped);
+  }
+
+  public void addParameters(@NotNull final Collection<String> argz) {
+    for (String s : argz) {
+      addParameter(s);
+    }
+  }
+
+  public void addParameters(@NotNull final String... argz) {
+    for (String s : argz) {
+      addParameter(s);
+    }
+  }
+
+  public void addParameters(@NotNull final String a, @NotNull final String b) {
+    addParameter(a);
+    addParameter(b);
+  }
+
+  public void setEnvParams(@NotNull final Map<String, String> hgEnv) {
+    myEnvPatch.clear();
+    myEnvPatch.putAll(hgEnv);
+  }
+
+  @NotNull
+  public Map<String, String> getEnvParams() {
+    return new TreeMap<String, String>(myEnvPatch);
+  }
+
+  public void addEnvParam(@NotNull final String key, @NotNull final String value) {
+    myEnvPatch.put(key, value);
+  }
+
   public void setCharset(@NotNull Charset charset) {
     myCharset = charset;
   }
 
-  @Override
-  public Charset getCharset() {
-    return myCharset != null ? myCharset : super.getCharset();
-  }
-
-  @Override
   public void setWorkDirectory(@NonNls String path) {
     myWorkingDirectory = path;
-    super.setWorkDirectory(path);
   }
 
   public String getWorkingDirectory() {
@@ -74,4 +102,42 @@
   private String escape(String s) {
     return StringUtil.escapeQuotesIfWindows(s);
   }
+
+  @NotNull
+  public GeneralCommandLine toGeneralCommandLine() {
+    final GeneralCommandLine cmd = new GeneralCommandLine() {
+      @Override
+      public String getCommandLineParams() {
+        return CommandUtil.removePrivateData(super.getCommandLineParams(), myPrivateData);
+      }
+
+      @Override
+      public String getCommandLineString() {
+        return CommandUtil.removePrivateData(super.getCommandLineString(), myPrivateData);
+      }
+    };
+
+    if (myExePath != null) {
+      cmd.setExePath(myExePath);
+    }
+
+    if (myWorkingDirectory != null) {
+      cmd.setWorkDirectory(myWorkingDirectory);
+    }
+
+    if (myCharset != null) {
+      cmd.setCharset(myCharset);
+    }
+
+    for (String argument : myArguments) {
+      cmd.addParameter(argument);
+    }
+
+    if (!myEnvPatch.isEmpty()) {
+      cmd.setPassParentEnvs(true);
+      cmd.setEnvParams(new TreeMap<String, String>(myEnvPatch));
+    }
+
+    return cmd;
+  }
 }
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/TagCommand.java	Fri May 30 11:32:12 2014 +0200
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/TagCommand.java	Fri May 30 11:49:44 2014 +0200
@@ -15,7 +15,6 @@
  */
 package jetbrains.buildServer.buildTriggers.vcs.mercurial.command;
 
-import com.intellij.execution.configurations.GeneralCommandLine;
 import jetbrains.buildServer.vcs.VcsException;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
@@ -52,15 +51,12 @@
   public void call() throws VcsException {
     MercurialCommandLine cli = createCommandLine();
     cli.addParameter("tag");
-    setUser(cli);
+    if (myUsername != null) {
+      cli.addParameters("--user", myUsername);
+    }
     cli.addParameter("-r");
     cli.addParameter(myRevId);
     cli.addParameter(myTag);
     runCommand(cli);
   }
-
-  private void setUser(GeneralCommandLine cli) {
-    if (myUsername != null)
-      cli.addParameters("--user", myUsername);
-  }
 }
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/VersionCommand.java	Fri May 30 11:32:12 2014 +0200
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/VersionCommand.java	Fri May 30 11:49:44 2014 +0200
@@ -16,15 +16,12 @@
 
 package jetbrains.buildServer.buildTriggers.vcs.mercurial.command;
 
-import com.intellij.execution.configurations.GeneralCommandLine;
 import jetbrains.buildServer.buildTriggers.vcs.mercurial.HgVersion;
 import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.exception.ParseHgVersionException;
 import jetbrains.buildServer.vcs.VcsException;
 import org.jetbrains.annotations.NotNull;
 
 import java.io.File;
-import java.util.HashMap;
-import java.util.Map;
 
 /**
  * @author dmitry.neverov
@@ -38,24 +35,16 @@
   }
 
 
+  @NotNull
   public HgVersion call() throws VcsException, ParseHgVersionException {
     MercurialCommandLine cli = createCommandLine();
     cli.addParameter("version");
     cli.addParameter("--quiet");
-    setDefaultLocale(cli);
+    cli.addEnvParam("LANG", "en_US");
+    cli.addEnvParam("LANGUAGE", "en_US");
+    cli.addEnvParam("LC_MESSAGE", "en_US");
+
     CommandResult result = runCommand(cli, myCommandSettings);
     return HgVersion.parse(result.getStdout());
   }
-
-
-  private void setDefaultLocale(GeneralCommandLine commandLine) {
-    Map<String, String> env = commandLine.getEnvParams();
-    if (env == null)
-      env = new HashMap<String, String>();
-    env.put("LANG", "en_US");
-    env.put("LANGUAGE", "en_US");
-    env.put("LC_MESSAGE", "en_US");
-    commandLine.setEnvParams(env);
-  }
-
 }
--- a/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupportTest.java	Fri May 30 11:32:12 2014 +0200
+++ b/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupportTest.java	Fri May 30 11:49:44 2014 +0200
@@ -268,7 +268,6 @@
     cli.setExePath(vcsRoot.getProperty(Constants.HG_COMMAND_PATH_PROP));
     cli.setWorkDirectory(vcsRoot.getProperty(Constants.REPOSITORY_PROP));
     cli.setEnvParams(map("HGRCPATH", ""));
-    cli.setPassParentEnvs(true);
     cli.addParameter("tags");
     CommandResult res = CommandUtil.runCommand(cli, new CommandSettings());
     assertTrue(res.getStdout().contains("new_tag"));
@@ -287,7 +286,6 @@
     cli.setWorkDirectory(vcsRoot.getProperty(Constants.REPOSITORY_PROP));
     cli.addParameter("tags");
     cli.setEnvParams(map("HGRCPATH", ""));
-    cli.setPassParentEnvs(true);
     CommandResult res = CommandUtil.runCommand(cli, new CommandSettings());
     assertTrue(res.getStdout().contains("branch_tag"));
     assertTrue(res.getStdout().contains("7:376dcf05cd2a"));
--- a/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommandTest.java	Fri May 30 11:32:12 2014 +0200
+++ b/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommandTest.java	Fri May 30 11:49:44 2014 +0200
@@ -16,7 +16,6 @@
 
 package jetbrains.buildServer.buildTriggers.vcs.mercurial.command;
 
-import com.intellij.execution.configurations.GeneralCommandLine;
 import com.intellij.openapi.util.SystemInfo;
 import junit.framework.TestCase;
 import org.testng.annotations.Test;
@@ -33,13 +32,15 @@
   public void should_quote_command_line_arguments() throws IOException {
     File workingDir = new File("some dir");
     BaseCommand command = new BaseCommand(new CommandSettings(), "/path/to/hg", workingDir);
-    GeneralCommandLine cl = command.createCommandLine();
+    MercurialCommandLine cl = command.createCommandLine();
     cl.addParameter("param with spaces");
     cl.addParameter("param with quote \" rm -rf /");
     if (SystemInfo.isWindows) {
       assertTrue(cl.getCommandLineString().endsWith(" \"param with spaces\" \"param with quote \\\" rm -rf /\""));
+      assertTrue(cl.toGeneralCommandLine().getCommandLineString().endsWith(" \"param with spaces\" \"param with quote \\\" rm -rf /\""));
     } else {
       assertTrue(cl.getCommandLineString().endsWith(" param with spaces param with quote \" rm -rf /"));
+      assertTrue(cl.toGeneralCommandLine().getCommandLineString().endsWith(" param with spaces param with quote \" rm -rf /"));
     }
   }