changeset 15581:d8fa35c28335

ssh: quote remote paths (issue2983)
author Mads Kiilerich <mads@kiilerich.com>
date Sat, 26 Nov 2011 00:10:31 +0100
parents 5a7733563c2e
children 926a06f7a353
files mercurial/sshrepo.py tests/test-init.t tests/test-ssh.t tests/test-subrepo-relative-path.t
diffstat 4 files changed, 52 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/sshrepo.py	Sat Nov 26 00:10:12 2011 +0100
+++ b/mercurial/sshrepo.py	Sat Nov 26 00:10:31 2011 +0100
@@ -18,6 +18,10 @@
         if self.repo:
             self.release()
 
+def _serverquote(s):
+    '''quote a string for the remote shell ... which we assume is sh'''
+    return "'%s'" % s.replace("'", "'\\''")
+
 class sshrepository(wireproto.wirerepository):
     def __init__(self, ui, path, create=False):
         self._url = path
@@ -40,9 +44,9 @@
         args = util.sshargs(sshcmd, self.host, self.user, self.port)
 
         if create:
-            cmd = '%s %s "%s init %s"'
-            cmd = cmd % (sshcmd, args, remotecmd, self.path)
-
+            cmd = '%s %s %s' % (sshcmd, args,
+                util.shellquote("%s init %s" %
+                    (_serverquote(remotecmd), _serverquote(self.path))))
             ui.note(_('running %s\n') % cmd)
             res = util.system(cmd)
             if res != 0:
@@ -57,11 +61,11 @@
         # cleanup up previous run
         self.cleanup()
 
-        cmd = '%s %s "%s -R %s serve --stdio"'
-        cmd = cmd % (sshcmd, args, remotecmd, self.path)
-
+        cmd = '%s %s %s' % (sshcmd, args,
+            util.shellquote("%s -R %s serve --stdio" %
+                (_serverquote(remotecmd), _serverquote(self.path))))
+        ui.note(_('running %s\n') % cmd)
         cmd = util.quotecommand(cmd)
-        ui.note(_('running %s\n') % cmd)
         self.pipeo, self.pipei, self.pipee = util.popen3(cmd)
 
         # skip any noise generated by remote shell
--- a/tests/test-init.t	Sat Nov 26 00:10:12 2011 +0100
+++ b/tests/test-init.t	Sat Nov 26 00:10:31 2011 +0100
@@ -105,13 +105,13 @@
 output of dummyssh
 
   $ cat dummylog
-  Got arguments 1:user@dummy 2:hg init remote2
-  Got arguments 1:user@dummy 2:hg -R remote2 serve --stdio
-  Got arguments 1:user@dummy 2:hg -R remote2 serve --stdio
-  Got arguments 1:user@dummy 2:hg init remote1
-  Got arguments 1:user@dummy 2:hg -R remote1 serve --stdio
-  Got arguments 1:user@dummy 2:hg init remote1
-  Got arguments 1:user@dummy 2:hg init remote1
+  Got arguments 1:user@dummy 2:'hg' init 'remote2'
+  Got arguments 1:user@dummy 2:'hg' -R 'remote2' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R 'remote2' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' init 'remote1'
+  Got arguments 1:user@dummy 2:'hg' -R 'remote1' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' init 'remote1'
+  Got arguments 1:user@dummy 2:'hg' init 'remote1'
 
 comparing repositories
 
--- a/tests/test-ssh.t	Sat Nov 26 00:10:12 2011 +0100
+++ b/tests/test-ssh.t	Sat Nov 26 00:10:31 2011 +0100
@@ -266,25 +266,36 @@
   [255]
 
   $ cd ..
+
+Test remote paths with spaces (issue2983):
+
+  $ hg init --ssh "python $TESTDIR/dummyssh" "ssh://user@dummy/a repo"
+  $ hg -R 'a repo' tag tag
+  $ hg id --ssh "python $TESTDIR/dummyssh" "ssh://user@dummy/a repo"
+  3fb238f49e8c
+
   $ cat dummylog
-  Got arguments 1:user@dummy 2:hg -R nonexistent serve --stdio
-  Got arguments 1:user@dummy 2:hg -R /$TESTTMP/nonexistent serve --stdio
-  Got arguments 1:user@dummy 2:hg -R remote serve --stdio
-  Got arguments 1:user@dummy 2:hg -R remote serve --stdio
-  Got arguments 1:user@dummy 2:hg -R remote serve --stdio
-  Got arguments 1:user@dummy 2:hg -R remote serve --stdio
-  Got arguments 1:user@dummy 2:hg -R local serve --stdio
-  Got arguments 1:user@dummy 2:hg -R $TESTTMP/local serve --stdio
-  Got arguments 1:user@dummy 2:hg -R remote serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R 'nonexistent' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R '/$TESTTMP/nonexistent' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R 'remote' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R 'remote' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R 'remote' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R 'remote' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R 'local' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R '$TESTTMP/local' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R 'remote' serve --stdio
   changegroup-in-remote hook: HG_NODE=a28a9d1a809cab7d4e2fde4bee738a9ede948b60 HG_SOURCE=serve HG_URL=remote:ssh:127.0.0.1 
-  Got arguments 1:user@dummy 2:hg -R remote serve --stdio
-  Got arguments 1:user@dummy 2:hg -R remote serve --stdio
-  Got arguments 1:user@dummy 2:hg -R remote serve --stdio
-  Got arguments 1:user@dummy 2:hg -R remote serve --stdio
-  Got arguments 1:user@dummy 2:hg -R remote serve --stdio
-  Got arguments 1:user@dummy 2:hg -R remote serve --stdio
-  Got arguments 1:user@dummy 2:hg -R remote serve --stdio
-  Got arguments 1:user@dummy 2:hg -R remote serve --stdio
-  Got arguments 1:user@dummy 2:hg -R remote serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R 'remote' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R 'remote' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R 'remote' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R 'remote' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R 'remote' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R 'remote' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R 'remote' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R 'remote' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R 'remote' serve --stdio
   changegroup-in-remote hook: HG_NODE=1383141674ec756a6056f6a9097618482fe0f4a6 HG_SOURCE=serve HG_URL=remote:ssh:127.0.0.1 
-  Got arguments 1:user@dummy 2:hg -R remote serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R 'remote' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' init 'a repo'
+  Got arguments 1:user@dummy 2:'hg' -R 'a repo' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R 'a repo' serve --stdio
--- a/tests/test-subrepo-relative-path.t	Sat Nov 26 00:10:12 2011 +0100
+++ b/tests/test-subrepo-relative-path.t	Sat Nov 26 00:10:31 2011 +0100
@@ -98,8 +98,8 @@
   no changes found
 
   $ cat dummylog
-  Got arguments 1:user@dummy 2:hg -R cloned serve --stdio
-  Got arguments 1:user@dummy 2:hg -R sub serve --stdio
-  Got arguments 1:user@dummy 2:hg -R $TESTTMP/cloned serve --stdio
-  Got arguments 1:user@dummy 2:hg -R $TESTTMP/sub serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R 'cloned' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R 'sub' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R '$TESTTMP/cloned' serve --stdio
+  Got arguments 1:user@dummy 2:'hg' -R '$TESTTMP/sub' serve --stdio
   $ rm $BINDIR/ssh