Patchwork util: factor out shellsplit() function

login
register
mail settings
Submitter Yuya Nishihara
Date Feb. 21, 2018, 2:33 p.m.
Message ID <44e4662d7a61ff8272a9.1519223631@mimosa>
Download mbox | patch
Permalink /patch/28185/
State Accepted
Headers show

Comments

Yuya Nishihara - Feb. 21, 2018, 2:33 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1519219227 -32400
#      Wed Feb 21 22:20:27 2018 +0900
# Node ID 44e4662d7a61ff8272a96045a60c3b005a099f64
# Parent  0f36926b2651a65944bca6e4247600791e253438
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.
Matt Harbison - Feb. 22, 2018, 3:18 a.m.
On Wed, 21 Feb 2018 09:33:51 -0500, Yuya Nishihara <yuya@tcha.org> wrote:

> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1519219227 -32400
> #      Wed Feb 21 22:20:27 2018 +0900
> # Node ID 44e4662d7a61ff8272a96045a60c3b005a099f64
> # Parent  0f36926b2651a65944bca6e4247600791e253438
> util: factor out shellsplit() function

LGTM, thanks.

Apparently, there's quoting insanity too, but I don't think we need to  
worry about it for now.

http://daviddeley.com/autohotkey/parameters/parameters.htm#WINCRULESTDQ
Augie Fackler - Feb. 26, 2018, 4:15 a.m.
On Wed, Feb 21, 2018 at 11:33:51PM +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1519219227 -32400
> #      Wed Feb 21 22:20:27 2018 +0900
> # Node ID 44e4662d7a61ff8272a96045a60c3b005a099f64
> # Parent  0f36926b2651a65944bca6e4247600791e253438
> util: factor out shellsplit() function

queued, thanks

Patch

diff --git a/mercurial/debugcommands.py b/mercurial/debugcommands.py
--- a/mercurial/debugcommands.py
+++ b/mercurial/debugcommands.py
@@ -1240,9 +1240,7 @@  def debuginstall(ui, **opts):
     # 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',
diff --git a/mercurial/posix.py b/mercurial/posix.py
--- a/mercurial/posix.py
+++ b/mercurial/posix.py
@@ -461,6 +461,10 @@  def shellquote(s):
     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
 
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -147,6 +147,7 @@  setbinary = platform.setbinary
 setflags = platform.setflags
 setsignalhandler = platform.setsignalhandler
 shellquote = platform.shellquote
+shellsplit = platform.shellsplit
 spawndetached = platform.spawndetached
 split = platform.split
 sshargs = platform.sshargs
diff --git a/mercurial/windows.py b/mercurial/windows.py
--- a/mercurial/windows.py
+++ b/mercurial/windows.py
@@ -296,6 +296,15 @@  def shellquote(s):
         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):