summaryrefslogtreecommitdiff
path: root/git/cmd.py
diff options
context:
space:
mode:
Diffstat (limited to 'git/cmd.py')
-rw-r--r--git/cmd.py33
1 files changed, 29 insertions, 4 deletions
diff --git a/git/cmd.py b/git/cmd.py
index a8afc144..633aedcb 100644
--- a/git/cmd.py
+++ b/git/cmd.py
@@ -69,6 +69,10 @@ else:
# Documentation
## @{
+def _drop_output_handler(line):
+ pass
+
+
def handle_process_output(process, stdout_handler, stderr_handler, finalizer):
"""Registers for notifications to lean that process output is ready to read, and dispatches lines to
the respective line handlers. We are able to handle carriage returns in case progress is sent by that
@@ -79,6 +83,13 @@ def handle_process_output(process, stdout_handler, stderr_handler, finalizer):
:param stdout_handler: f(stdout_line_string), or None
:param stderr_hanlder: f(stderr_line_string), or None
:param finalizer: f(proc) - wait for proc to finish"""
+
+ log.debug('handle_process_output( process=%r, stdout_handler=%r, stderr_handler=%r, finalizer=%r'
+ % (process, stdout_handler, stderr_handler, finalizer))
+
+ if stdout_handler is None:
+ stdout_handler = _drop_output_handler
+
fdmap = {process.stdout.fileno(): (stdout_handler, [b'']),
process.stderr.fileno(): (stderr_handler, [b''])}
@@ -119,6 +130,7 @@ def handle_process_output(process, stdout_handler, stderr_handler, finalizer):
# end single line helper
def _dispatch_lines(fno, handler, buf_list):
+ log.debug('fno=%d, handler=%r, buf_list=%r' % (fno, handler, buf_list))
lc = 0
for line in _read_lines_from_fno(fno, buf_list):
_dispatch_single_line(line, handler)
@@ -307,22 +319,35 @@ class Git(LazyMixin):
def __getattr__(self, attr):
return getattr(self.proc, attr)
- def wait(self, stderr=None):
+ def wait(self, stderr=b''):
"""Wait for the process and return its status code.
:param stderr: Previously read value of stderr, in case stderr is already closed.
:warn: may deadlock if output or error pipes are used and not handled separately.
:raise GitCommandError: if the return status is not 0"""
+
+ # stderr must be a bytes object as it will
+ # combined with more data from the process and
+ # decoded by the caller
+ if stderr is None:
+ stderr = b''
+ elif type(stderr) == unicode:
+ stderr = stderr.encode(defenc)
+
status = self.proc.wait()
def read_all_from_possibly_closed_stream(stream):
try:
- return stream.read()
+ last_stderr = stream.read()
+ if type(last_stderr) == unicode:
+ last_stderr = last_stderr.encode(defenc)
+ return stderr + last_stderr
except ValueError:
- return stderr or ''
+ return stderr or b''
if status != 0:
errstr = read_all_from_possibly_closed_stream(self.proc.stderr)
+ log.debug('AutoInterrupt wait stderr: %r' % (errstr,))
raise GitCommandError(self.args, status, errstr)
# END status handling
return status
@@ -609,7 +634,7 @@ class Git(LazyMixin):
bufsize=-1,
stdin=istream,
stderr=PIPE,
- stdout=PIPE if with_stdout else open(os.devnull, 'wb'),
+ stdout=PIPE,
shell=self.USE_SHELL,
close_fds=(os.name == 'posix'), # unsupported on windows
universal_newlines=universal_newlines,