# HG changeset patch # User Dmitry Neverov # Date 1302777571 -14400 # Node ID 9e60b6d1e5fd8ed9a0c4b9582facc727a16bdcdb # Parent 2b2dab847ac8b634869901b114b36eadcfc8971d Escape quotes in command line parameters * * * Escape quotes in command line parameters diff -r 2b2dab847ac8 -r 9e60b6d1e5fd mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommand.java --- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommand.java Wed Apr 06 11:47:30 2011 +0400 +++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommand.java Thu Apr 14 14:39:31 2011 +0400 @@ -56,13 +56,33 @@ } protected GeneralCommandLine createCommandLine() { - GeneralCommandLine cli = new GeneralCommandLine(); - setupExecutable(cli); + GeneralCommandLine cli = createCL(); cli.setWorkDirectory(myWorkDirectory); cli.setPassParentEnvs(true); return cli; } + protected GeneralCommandLine createCL() { + GeneralCommandLine cl = new EscapingCommandLineDecorator(); + setupExecutable(cl); + return cl; + } + + /** + * Escape its parameters + */ + private static class EscapingCommandLineDecorator extends GeneralCommandLine { + @Override + public void addParameter(@NotNull String parameter) { + String escaped = escape(parameter); + super.addParameter(escaped); + } + + private String escape(String s) { + return StringUtil.escapeQuotesIfWindows(s); + } + } + /** * Since mercurial 1.7 on Windows the only file inside '/bin' is 'hg.cmd' * which run hg.exe placed in the parent dir. GeneralCommandLine will not find hg.cmd, in the diff -r 2b2dab847ac8 -r 9e60b6d1e5fd mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/IdentifyCommand.java --- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/IdentifyCommand.java Wed Apr 06 11:47:30 2011 +0400 +++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/IdentifyCommand.java Thu Apr 14 14:39:31 2011 +0400 @@ -44,8 +44,7 @@ } public String execute() throws VcsException { - GeneralCommandLine cli = new GeneralCommandLine(); - cli.setExePath(getSettings().getHgCommandPath()); + GeneralCommandLine cli = createCL(); cli.addParameter("identify"); if (myInLocalRepository) { cli.setWorkDirectory(this.getWorkDirectory()); diff -r 2b2dab847ac8 -r 9e60b6d1e5fd mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommandTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommandTest.java Thu Apr 14 14:39:31 2011 +0400 @@ -0,0 +1,38 @@ +package jetbrains.buildServer.buildTriggers.vcs.mercurial.command; + +import com.intellij.execution.configurations.GeneralCommandLine; +import com.intellij.openapi.util.SystemInfo; +import jetbrains.buildServer.buildTriggers.vcs.mercurial.Constants; +import jetbrains.buildServer.buildTriggers.vcs.mercurial.Util; +import jetbrains.buildServer.vcs.impl.VcsRootImpl; +import junit.framework.TestCase; +import org.testng.annotations.Test; + +import java.io.File; +import java.io.IOException; + +/** + * @author dmitry.neverov + */ +@Test +public class BaseCommandTest extends TestCase { + + public void should_quote_command_line_arguments() throws IOException { + VcsRootImpl root = new VcsRootImpl(1, "rootForTest"); + root.addProperty(Constants.REPOSITORY_PROP, "http://some.org/repo.hg"); + root.addProperty(Constants.HG_COMMAND_PATH_PROP, Util.getHgPath()); + File workingDir = new File("some dir"); + Settings settings = new Settings(root); + + BaseCommand command = new BaseCommand(settings, workingDir); + GeneralCommandLine 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 /\"")); + } else { + assertTrue(cl.getCommandLineString().endsWith(" param with spaces param with quote \" rm -rf /")); + } + } + +} diff -r 2b2dab847ac8 -r 9e60b6d1e5fd mercurial-tests/src/testng.xml --- a/mercurial-tests/src/testng.xml Wed Apr 06 11:47:30 2011 +0400 +++ b/mercurial-tests/src/testng.xml Thu Apr 14 14:39:31 2011 +0400 @@ -2,6 +2,7 @@ +