[PATCH 1 of 4] hg-ssh: use shlex for shell-like parsing of SSH_ORIGINAL_COMMAND

Mads Kiilerich mads at kiilerich.com
Thu Dec 8 15:39:23 UTC 2011


# HG changeset patch
# User Mads Kiilerich <mads at kiilerich.com>
# Date 1323358098 -3600
# Node ID 4aa9809bdb092d02a7d60efb902e02b3c38788d7
# Parent  36d7a0c7505f7b526303bb140f7bc106f7df457e
hg-ssh: use shlex for shell-like parsing of SSH_ORIGINAL_COMMAND

This makes hg-ssh behave more like a normal shell with hg in the path.

diff --git a/contrib/hg-ssh b/contrib/hg-ssh
--- a/contrib/hg-ssh
+++ b/contrib/hg-ssh
@@ -31,15 +31,20 @@
 
 from mercurial import dispatch
 
-import sys, os
+import sys, os, shlex
 
 cwd = os.getcwd()
 allowed_paths = [os.path.normpath(os.path.join(cwd, os.path.expanduser(path)))
                  for path in sys.argv[1:]]
 orig_cmd = os.getenv('SSH_ORIGINAL_COMMAND', '?')
+try:
+    cmdargv = shlex.split(orig_cmd)
+except ValueError, e:
+    sys.stderr.write("Illegal command %r: %s\n" % (orig_cmd, e))
+    sys.exit(-1)
 
-if orig_cmd.startswith('hg -R ') and orig_cmd.endswith(' serve --stdio'):
-    path = orig_cmd[6:-14]
+if cmdargv[:2] == ['hg', '-R'] and cmdargv[3:] == ['serve', '--stdio']:
+    path = cmdargv[2]
     repo = os.path.normpath(os.path.join(cwd, os.path.expanduser(path)))
     if repo in allowed_paths:
         dispatch.dispatch(dispatch.request(['-R', repo, 'serve', '--stdio']))
diff --git a/tests/test-ssh.t b/tests/test-ssh.t
--- a/tests/test-ssh.t
+++ b/tests/test-ssh.t
@@ -274,6 +274,19 @@
   $ hg id --ssh "python $TESTDIR/dummyssh" "ssh://user@dummy/a repo"
   3fb238f49e8c
 
+Test hg-ssh:
+
+  $ SSH_ORIGINAL_COMMAND="'hg' -R 'a repo' serve --stdio" hg id --ssh "python \"$TESTDIR\"/../contrib/hg-ssh \"$TESTTMP/a repo\"" "ssh://user@dummy/a repo"
+  3fb238f49e8c
+  $ SSH_ORIGINAL_COMMAND="'hg' -R 'a repo' serve --stdio" hg id --ssh "python \"$TESTDIR\"/../contrib/hg-ssh \"$TESTTMP\"" "ssh://user@dummy/a repo"
+  remote: Illegal repository '$TESTTMP/a repo'
+  abort: no suitable response from remote hg!
+  [255]
+  $ SSH_ORIGINAL_COMMAND="'hg' -R 'a'repo' serve --stdio" hg id --ssh "python \"$TESTDIR\"/../contrib/hg-ssh \"$TESTTMP\"" "ssh://user@dummy/a repo"
+  remote: Illegal command "'hg' -R 'a'repo' serve --stdio": No closing quotation
+  abort: no suitable response from remote hg!
+  [255]
+
   $ cat dummylog
   Got arguments 1:user at dummy 2:'hg' -R 'nonexistent' serve --stdio
   Got arguments 1:user at dummy 2:'hg' -R '/$TESTTMP/nonexistent' serve --stdio


More information about the Mercurial-devel mailing list