chg: refactor ui.system() to be partly overridden
authorYuya Nishihara <yuya@tcha.org>
Sun, 19 Feb 2017 01:00:10 +0900
changeset 31107 fbce78c58f1e
parent 31106 a185b903bda3
child 31108 3f8f53190d6a
chg: refactor ui.system() to be partly overridden Since fd598149112b changed the signature of ui.system(), chgui.system() should have been updated. This patch factors out the util.system() call so that chg can override how a shell command is executed.
mercurial/chgserver.py
mercurial/ui.py
tests/test-chg.t
--- a/mercurial/chgserver.py	Tue Feb 21 18:22:07 2017 +0100
+++ b/mercurial/chgserver.py	Sun Feb 19 01:00:10 2017 +0900
@@ -179,17 +179,16 @@
             else:
                 self._csystem = csystem
 
-        def system(self, cmd, environ=None, cwd=None, onerr=None,
-                   errprefix=None):
+        def _runsystem(self, cmd, environ, cwd, onerr, errprefix, out):
             # fallback to the original system method if the output needs to be
             # captured (to self._buffers), or the output stream is not stdout
             # (e.g. stderr, cStringIO), because the chg client is not aware of
             # these situations and will behave differently (write to stdout).
-            if (any(s[1] for s in self._bufferstates)
+            if (out is not self.fout
                 or not util.safehasattr(self.fout, 'fileno')
                 or self.fout.fileno() != util.stdout.fileno()):
-                return super(chgui, self).system(cmd, environ, cwd, onerr,
-                                                 errprefix)
+                return util.system(cmd, environ=environ, cwd=cwd, onerr=onerr,
+                                   errprefix=errprefix, out=out)
             self.flush()
             rc = self._csystem(cmd, util.shellenviron(environ), cwd)
             if rc and onerr:
--- a/mercurial/ui.py	Tue Feb 21 18:22:07 2017 +0100
+++ b/mercurial/ui.py	Sun Feb 19 01:00:10 2017 +0900
@@ -1288,8 +1288,14 @@
         if any(s[1] for s in self._bufferstates):
             out = self
         with self.timeblockedsection(blockedtag):
-            return util.system(cmd, environ=environ, cwd=cwd, onerr=onerr,
-                               errprefix=errprefix, out=out)
+            return self._runsystem(cmd, environ=environ, cwd=cwd, onerr=onerr,
+                                   errprefix=errprefix, out=out)
+
+    def _runsystem(self, cmd, environ, cwd, onerr, errprefix, out):
+        """actually execute the given shell command (can be overridden by
+        extensions like chg)"""
+        return util.system(cmd, environ=environ, cwd=cwd, onerr=onerr,
+                           errprefix=errprefix, out=out)
 
     def traceback(self, exc=None, force=False):
         '''print exception traceback if traceback printing enabled or forced.
--- a/tests/test-chg.t	Tue Feb 21 18:22:07 2017 +0100
+++ b/tests/test-chg.t	Sun Feb 19 01:00:10 2017 +0900
@@ -32,6 +32,46 @@
 
   $ cd ..
 
+editor
+------
+
+  $ cat >> pushbuffer.py <<EOF
+  > def reposetup(ui, repo):
+  >     repo.ui.pushbuffer(subproc=True)
+  > EOF
+
+  $ chg init editor
+  $ cd editor
+
+by default, system() should be redirected to the client:
+
+  $ touch foo
+  $ CHGDEBUG= HGEDITOR=cat chg ci -Am channeled --edit 2>&1 \
+  > | egrep "HG:|run 'cat"
+  chg: debug: run 'cat "*"' at '$TESTTMP/editor' (glob)
+  HG: Enter commit message.  Lines beginning with 'HG:' are removed.
+  HG: Leave message empty to abort commit.
+  HG: --
+  HG: user: test
+  HG: branch 'default'
+  HG: added foo
+
+but no redirection should be made if output is captured:
+
+  $ touch bar
+  $ CHGDEBUG= HGEDITOR=cat chg ci -Am bufferred --edit \
+  > --config extensions.pushbuffer="$TESTTMP/pushbuffer.py" 2>&1 \
+  > | egrep "HG:|run 'cat"
+  [1]
+
+check that commit commands succeeded:
+
+  $ hg log -T '{rev}:{desc}\n'
+  1:bufferred
+  0:channeled
+
+  $ cd ..
+
 pager
 -----