Mercurial > hg > mercurial
changeset 229:9e60b6d1e5fd build-snapshot-124
Escape quotes in command line parameters
* * *
Escape quotes in command line parameters
author | Dmitry Neverov <dmitry.neverov@jetbrains.com> |
---|---|
date | Thu, 14 Apr 2011 14:39:31 +0400 |
parents | 2b2dab847ac8 |
children | b454327bcb66 |
files | mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommand.java mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/IdentifyCommand.java mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommandTest.java mercurial-tests/src/testng.xml |
diffstat | 4 files changed, 62 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- 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 '<mercurial_install_dir>/bin' is 'hg.cmd' * which run hg.exe placed in the parent dir. GeneralCommandLine will not find hg.cmd, in the
--- 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());
--- /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 /")); + } + } + +}
--- 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 @@ <suite name="Mercurial Suite"> <test name="Mercurial Tests"> <classes> + <class name="jetbrains.buildServer.buildTriggers.vcs.mercurial.command.BaseCommandTest"/> <class name="jetbrains.buildServer.buildTriggers.vcs.mercurial.command.CloneCommandTest"/> <class name="jetbrains.buildServer.buildTriggers.vcs.mercurial.command.LogCommandTest"/> <class name="jetbrains.buildServer.buildTriggers.vcs.mercurial.command.StatusCommandTest"/>