Skip to content

Commit 15ee5a5

Browse files
committed
fix(misc): various cleanup
Just went through all changes and adjusted them to the best of my abilities. As there are no tests to claim otherwise, I believe this is correct enough. However, it becomes evident that it's no longer possible to just make changes without backing them with a respective test.
1 parent da86442 commit 15ee5a5

File tree

4 files changed

+18
-49
lines changed

4 files changed

+18
-49
lines changed

Diff for: git/cmd.py

+6-25
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from git.compat import (
3737
string_types,
3838
defenc,
39+
force_bytes,
3940
PY3,
4041
bchr,
4142
# just to satisfy flake8 on py3
@@ -69,10 +70,6 @@ def _bchr(c):
6970
# Documentation
7071
## @{
7172

72-
def _drop_output_handler(line):
73-
pass
74-
75-
7673
def handle_process_output(process, stdout_handler, stderr_handler, finalizer):
7774
"""Registers for notifications to lean that process output is ready to read, and dispatches lines to
7875
the respective line handlers. We are able to handle carriage returns in case progress is sent by that
@@ -83,13 +80,6 @@ def handle_process_output(process, stdout_handler, stderr_handler, finalizer):
8380
:param stdout_handler: f(stdout_line_string), or None
8481
:param stderr_hanlder: f(stderr_line_string), or None
8582
:param finalizer: f(proc) - wait for proc to finish"""
86-
87-
log.debug('handle_process_output( process=%r, stdout_handler=%r, stderr_handler=%r, finalizer=%r'
88-
% (process, stdout_handler, stderr_handler, finalizer))
89-
90-
if stdout_handler is None:
91-
stdout_handler = _drop_output_handler
92-
9383
fdmap = {process.stdout.fileno(): (stdout_handler, [b'']),
9484
process.stderr.fileno(): (stderr_handler, [b''])}
9585

@@ -130,7 +120,6 @@ def _dispatch_single_line(line, handler):
130120
# end single line helper
131121

132122
def _dispatch_lines(fno, handler, buf_list):
133-
log.debug('fno=%d, handler=%r, buf_list=%r' % (fno, handler, buf_list))
134123
lc = 0
135124
for line in _read_lines_from_fno(fno, buf_list):
136125
_dispatch_single_line(line, handler)
@@ -325,23 +314,15 @@ def wait(self, stderr=b''):
325314
:param stderr: Previously read value of stderr, in case stderr is already closed.
326315
:warn: may deadlock if output or error pipes are used and not handled separately.
327316
:raise GitCommandError: if the return status is not 0"""
328-
329-
# stderr must be a bytes object as it will
330-
# combined with more data from the process and
331-
# decoded by the caller
332317
if stderr is None:
333318
stderr = b''
334-
elif type(stderr) == unicode:
335-
stderr = stderr.encode(defenc)
336-
319+
stderr = force_bytes(stderr)
320+
337321
status = self.proc.wait()
338322

339323
def read_all_from_possibly_closed_stream(stream):
340324
try:
341-
last_stderr = stream.read()
342-
if type(last_stderr) == unicode:
343-
last_stderr = last_stderr.encode(defenc)
344-
return stderr + last_stderr
325+
return stderr + force_bytes(stream.read())
345326
except ValueError:
346327
return stderr or b''
347328

@@ -633,8 +614,8 @@ def execute(self, command,
633614
cwd=cwd,
634615
bufsize=-1,
635616
stdin=istream,
636-
stderr=PIPE,
637-
stdout=PIPE,
617+
stderr=PIPE,
618+
stdout=PIPE if with_stdout else open(os.devnull, 'wb'),
638619
shell=self.USE_SHELL,
639620
close_fds=(os.name == 'posix'), # unsupported on windows
640621
universal_newlines=universal_newlines,

Diff for: git/ext/gitdb

Diff for: git/remote.py

+3-19
Original file line numberDiff line numberDiff line change
@@ -570,37 +570,25 @@ def _get_fetch_info_from_stderr(self, proc, progress):
570570

571571
progress_handler = progress.new_message_handler()
572572

573-
error_message = None
574573
stderr_text = None
575574

576575
for line in proc.stderr:
577576
line = force_text(line)
578577
for pline in progress_handler(line):
579-
if line.startswith('fatal:') or line.startswith('error:'):
580-
error_message = "Error when fetching: %s" % (line,)
581-
break
582-
583578
# END handle special messages
584579
for cmd in cmds:
585580
if len(line) > 1 and line[0] == ' ' and line[1] == cmd:
586581
fetch_info_lines.append(line)
587582
continue
588583
# end find command code
589584
# end for each comand code we know
590-
591-
if error_message is not None:
592-
break
593585
# end for each line progress didn't handle
594-
595-
if error_message is not None:
596-
stderr_text = proc.stderr.read()
597-
598586
# end
587+
if progress.error_lines():
588+
stderr_text = '\n'.join(progress.error_lines())
589+
599590
finalize_process(proc, stderr=stderr_text)
600591

601-
if error_message is not None:
602-
raise GitCommandError(error_message, 2, stderr=stderr_text)
603-
604592
# read head information
605593
fp = open(join(self.repo.git_dir, 'FETCH_HEAD'), 'rb')
606594
fetch_head_info = [l.decode(defenc) for l in fp.readlines()]
@@ -646,10 +634,6 @@ def stdout_handler(line):
646634

647635
try:
648636
handle_process_output(proc, stdout_handler, progress_handler, finalize_process)
649-
except GitCommandError as err:
650-
# convert any error from wait() into the same error with stdout lines
651-
raise GitCommandError(err.command, err.status, progress.get_stderr())
652-
653637
except Exception:
654638
if len(output) == 0:
655639
raise

Diff for: git/util.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,16 @@ def __init__(self):
182182
self._cur_line = None
183183
self._error_lines = []
184184

185-
def get_stderr(self):
186-
return '\n'.join(self._error_lines)
185+
def error_lines(self):
186+
"""Returns all lines that started with error: or fatal:"""
187+
return self._error_lines
187188

188189
def _parse_progress_line(self, line):
189190
"""Parse progress information from the given line as retrieved by git-push
190-
or git-fetch
191+
or git-fetch.
192+
193+
Lines that seem to contain an error (i.e. start with error: or fatal:) are stored
194+
separately and can be queried using `error_lines()`.
191195
192196
:return: list(line, ...) list of lines that could not be processed"""
193197
# handle
@@ -775,7 +779,7 @@ def done(self):
775779
def wait(self, stderr=b''):
776780
self.cv.acquire()
777781
while self.count > 0:
778-
self.cv.wait(strerr=stderr)
782+
self.cv.wait()
779783
self.cv.release()
780784

781785

0 commit comments

Comments
 (0)