Patchwork D1534: logtoprocess: add an option to parse script command

login
register
mail settings
Submitter phabricator
Date Nov. 28, 2017, 8:36 p.m.
Message ID <differential-rev-PHID-DREV-qg2ppijoxhzemoj5ybvp-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/25787/
State New
Headers show

Comments

phabricator - Nov. 28, 2017, 8:36 p.m.
lothiraldan created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  With `shell=False`, the logtoprocess script will not be parsed by a shell and
  we won't be able to pass arguments in logtoprocess configuration. Add a new
  option `logtoprocess.parsescript` that calls `shlex.split` on the script so we
  can pass arguments. As `shlex.split` is only designed for POSIX system, this
  option have no impact on Windows.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D1534

AFFECTED FILES
  hgext/logtoprocess.py
  mercurial/configitems.py

CHANGE DETAILS




To: lothiraldan, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/mercurial/configitems.py b/mercurial/configitems.py
--- a/mercurial/configitems.py
+++ b/mercurial/configitems.py
@@ -566,6 +566,9 @@ 
 coreconfigitem('logtoprocess', 'develwarn',
     default=None,
 )
+coreconfigitem('logtoprocess', 'parsescript',
+    default=False,
+)
 coreconfigitem('logtoprocess', 'shell',
     default=True,
 )
diff --git a/hgext/logtoprocess.py b/hgext/logtoprocess.py
--- a/hgext/logtoprocess.py
+++ b/hgext/logtoprocess.py
@@ -36,6 +36,7 @@ 
 
 import itertools
 import os
+import shlex
 import subprocess
 import sys
 
@@ -58,14 +59,14 @@ 
         DETACHED_PROCESS = 0x00000008
         _creationflags = DETACHED_PROCESS | subprocess.CREATE_NEW_PROCESS_GROUP
 
-        def runshellcommand(script, env, shell=True):
+        def runshellcommand(script, env, shell=True, _=False):
             # we can't use close_fds *and* redirect stdin. I'm not sure that we
             # need to because the detached process has no console connection.
             subprocess.Popen(
                 script, shell=shell, env=env, close_fds=True,
                 creationflags=_creationflags)
     else:
-        def runshellcommand(script, env, shell=True):
+        def runshellcommand(script, env, shell=True, parse_script=False):
             # double-fork to completely detach from the parent process
             # based on http://code.activestate.com/recipes/278731
             pid = os.fork()
@@ -78,6 +79,11 @@ 
                 newsession = {'preexec_fn': os.setsid}
             else:
                 newsession = {'start_new_session': True}
+
+            # Try to split the arguments when shell is False
+            if not shell and parse_script:
+                script = shlex.split(script)
+
             try:
                 # connect stdin to devnull to make sure the subprocess can't
                 # muck up that stream for mercurial.
@@ -125,7 +131,8 @@ 
                                            msgpairs, optpairs),
                            EVENT=event, HGPID=str(os.getpid()))
                 shell = self.configbool('logtoprocess', 'shell')
-                runshellcommand(script, env, shell)
+                parsescript = self.configbool('logtoprocess', 'parsescript')
+                runshellcommand(script, env, shell, parsescript)
             return super(logtoprocessui, self).log(event, *msg, **opts)
 
     # Replace the class for this instance and all clones created from it: