Fix util.shellquote on windows.
authorAlexis S. L. Carvalho <alexis@cecm.usp.br>
Thu, 15 Feb 2007 05:38:00 -0200
changeset 4087 587c6c652f82
parent 4086 cc8a52229620
child 4088 18dcc22666a0
Fix util.shellquote on windows.
mercurial/util.py
--- a/mercurial/util.py	Thu Feb 15 05:18:23 2007 -0200
+++ b/mercurial/util.py	Thu Feb 15 05:38:00 2007 -0200
@@ -798,8 +798,23 @@
     def samestat(s1, s2):
         return False
 
+    # A sequence of backslashes is special iff it precedes a double quote:
+    # - if there's an even number of backslashes, the double quote is not
+    #   quoted (i.e. it ends the quoted region)
+    # - if there's an odd number of backslashes, the double quote is quoted
+    # - in both cases, every pair of backslashes is unquoted into a single
+    #   backslash
+    # (See http://msdn2.microsoft.com/en-us/library/a1y7w461.aspx )
+    # So, to quote a string, we must surround it in double quotes, double
+    # the number of backslashes that preceed double quotes and add another
+    # backslash before every double quote (being careful with the double
+    # quote we've appended to the end)
+    _quotere = None
     def shellquote(s):
-        return '"%s"' % s.replace('"', '\\"')
+        global _quotere
+        if _quotere is None:
+            _quotere = re.compile(r'(\\*)("|\\$)')
+        return '"%s"' % _quotere.sub(r'\1\1\\\2', s)
 
     def explain_exit(code):
         return _("exited with status %d") % code, code