Commit 8368c65f by Matthias Putz

Portable: pager and coloring, using subprocess and redirect sysin, sysout and syserr on Windows

parent 1790cd09
......@@ -20,6 +20,7 @@ import imp
import netrc
import optparse
import os
import portable
import sys
import time
......@@ -168,7 +169,10 @@ class _Repo(object):
if use_pager is None:
use_pager = cmd.WantPager(copts)
if use_pager:
RunPager(config)
# RunPager(cmd)
portable.RunPager(cmd)
else:
portable.NoPager(cmd)
start = time.time()
try:
......@@ -514,12 +518,14 @@ def _Main(argv):
argv = list(sys.argv)
argv.extend(rce.extra_args)
try:
os.execv(__file__, argv)
# os.execv(__file__, argv)
subprocess.call([__file__, argv])
except OSError as e:
print('fatal: cannot restart repo after upgrade', file=sys.stderr)
print('fatal: %s' % e, file=sys.stderr)
result = 128
portable.WaitForProcess()
sys.exit(result)
if __name__ == '__main__':
......
import os
import pager
import platform
import socket
import sys
import subprocess
import threading
from trace import Trace
......@@ -78,3 +81,61 @@ class socket_reader():
def fileno(self):
return self.server_socket.fileno()
child_process = None
def RunPager(cmd):
if isUnix():
pager.RunPager(cmd.manifest.globalConfig)
else:
RunWindowsPager(cmd)
def RunWindowsPager(cmd):
executable = pager._SelectPager(cmd.manifest.globalConfig)
redirect_all(executable)
pager.active = True
def NoPager(cmd):
if not isUnix():
RunWindowsShell(cmd)
def RunWindowsShell(cmd):
executable = _SelectCatenate(cmd.manifest.globalConfig)
redirect_all(executable)
def redirect_all(executable):
old_sysin = sys.stdin
old_sysout = sys.stdout
old_syserr = sys.stderr
Trace("redirecting to %s" % executable)
p = subprocess.Popen([executable], stdin=subprocess.PIPE, stdout=old_sysout, stderr=old_syserr)
sys.stdout = p.stdin
sys.stderr = p.stdin
old_sysout.close()
global child_process
child_process = p
def _SelectCatenate(globalConfig):
try:
return os.environ['GIT_CATENATE']
except KeyError:
pass
pager = globalConfig.GetString('core.catenate')
if pager:
return pager
try:
return os.environ['CATENATE']
except KeyError:
pass
return 'cat'
def WaitForProcess():
if not isUnix():
global child_process
if child_process:
child_process.stdin.close()
child_process.wait()
......@@ -775,7 +775,8 @@ def main(orig_args):
me.extend(orig_args)
me.extend(extra_args)
try:
os.execv(sys.executable, me)
# os.execv(sys.executable, me)
subprocess.call(me)
except OSError as e:
_print("fatal: unable to start %s" % repo_main, file=sys.stderr)
_print("fatal: %s" % e, file=sys.stderr)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment