util: factor out shellsplit() function
authorYuya Nishihara <yuya@tcha.org>
Wed, 21 Feb 2018 22:20:27 +0900
changeset 36415 0cb09c322647
parent 36414 44c4a38bf563
child 36416 07e207e88b42
util: factor out shellsplit() function It turned out to be more than the simple posix=True|False flag, so let's introduce a platform function. I also made it py3 ready.
mercurial/debugcommands.py
mercurial/posix.py
mercurial/util.py
mercurial/windows.py
--- a/mercurial/debugcommands.py	Fri Feb 23 23:09:58 2018 +0900
+++ b/mercurial/debugcommands.py	Wed Feb 21 22:20:27 2018 +0900
@@ -1240,9 +1240,7 @@
     # editor
     editor = ui.geteditor()
     editor = util.expandpath(editor)
-    editorbin = pycompat.shlexsplit(editor, posix=not pycompat.iswindows)[0]
-    if pycompat.iswindows and editorbin[0] == '"' and editorbin[-1] == '"':
-        editorbin = editorbin[1:-1]
+    editorbin = util.shellsplit(editor)[0]
     fm.write('editor', _("checking commit editor... (%s)\n"), editorbin)
     cmdpath = util.findexe(editorbin)
     fm.condwrite(not cmdpath and editor == 'vi', 'vinotfound',
--- a/mercurial/posix.py	Fri Feb 23 23:09:58 2018 +0900
+++ b/mercurial/posix.py	Wed Feb 21 22:20:27 2018 +0900
@@ -461,6 +461,10 @@
     else:
         return "'%s'" % s.replace("'", "'\\''")
 
+def shellsplit(s):
+    """Parse a command string in POSIX shell way (best-effort)"""
+    return pycompat.shlexsplit(s, posix=True)
+
 def quotecommand(cmd):
     return cmd
 
--- a/mercurial/util.py	Fri Feb 23 23:09:58 2018 +0900
+++ b/mercurial/util.py	Wed Feb 21 22:20:27 2018 +0900
@@ -147,6 +147,7 @@
 setflags = platform.setflags
 setsignalhandler = platform.setsignalhandler
 shellquote = platform.shellquote
+shellsplit = platform.shellsplit
 spawndetached = platform.spawndetached
 split = platform.split
 sshargs = platform.sshargs
--- a/mercurial/windows.py	Fri Feb 23 23:09:58 2018 +0900
+++ b/mercurial/windows.py	Wed Feb 21 22:20:27 2018 +0900
@@ -296,6 +296,15 @@
         return s
     return '"%s"' % _quotere.sub(r'\1\1\\\2', s)
 
+def _unquote(s):
+    if s.startswith(b'"') and s.endswith(b'"'):
+        return s[1:-1]
+    return s
+
+def shellsplit(s):
+    """Parse a command string in cmd.exe way (best-effort)"""
+    return pycompat.maplist(_unquote, pycompat.shlexsplit(s, posix=False))
+
 def quotecommand(cmd):
     """Build a command string suitable for os.popen* calls."""
     if sys.version_info < (2, 7, 1):