Skip to content

Pr cmd raise with stderr on error #452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 13, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions git/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def __del__(self):
def __getattr__(self, attr):
return getattr(self.proc, attr)

def wait(self, stderr=None):
def wait(self, stderr=''):
"""Wait for the process and return its status code.

:param stderr: Previously read value of stderr, in case stderr is already closed.
Expand All @@ -317,7 +317,7 @@ def wait(self, stderr=None):

def read_all_from_possibly_closed_stream(stream):
try:
return stream.read()
return stderr + stream.read()
except ValueError:
return stderr or ''

Expand Down Expand Up @@ -678,7 +678,7 @@ def _kill_process(pid):
# strip trailing "\n"
if stderr_value.endswith(b"\n"):
stderr_value = stderr_value[:-1]
status = proc.wait()
status = proc.wait(stderr=stderr_value)
# END stdout handling
finally:
proc.stdout.close()
Expand Down
23 changes: 21 additions & 2 deletions git/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,21 +570,36 @@ def _get_fetch_info_from_stderr(self, proc, progress):

progress_handler = progress.new_message_handler()

error_message = None
stderr_text = None

for line in proc.stderr:
line = force_text(line)
for pline in progress_handler(line):
if line.startswith('fatal:') or line.startswith('error:'):
raise GitCommandError(("Error when fetching: %s" % line,), 2)
error_message = "Error when fetching: %s" % (line,)
break
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure it' s enough to break after the first line indicating a problem ? My intuition is that the lines that follow on stderr contain context, that you probably want to catch as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works because of the 2nd break at line 591.

Also I see this work in testing.


# END handle special messages
for cmd in cmds:
if len(line) > 1 and line[0] == ' ' and line[1] == cmd:
fetch_info_lines.append(line)
continue
# end find command code
# end for each comand code we know

if error_message is not None:
break
# end for each line progress didn't handle

if error_message is not None:
stderr_text = proc.stderr.read()

# end
finalize_process(proc)
finalize_process(proc, stderr=stderr_text)

if error_message is not None:
raise GitCommandError( error_message, 2, stderr=stderr_text )

# read head information
fp = open(join(self.repo.git_dir, 'FETCH_HEAD'), 'rb')
Expand Down Expand Up @@ -631,6 +646,10 @@ def stdout_handler(line):

try:
handle_process_output(proc, stdout_handler, progress_handler, finalize_process)
except GitCommandError as err:
# convert any error from wait() into the same error with stdout lines
raise GitCommandError( err.command, err.status, progress.get_stderr() )

except Exception:
if len(output) == 0:
raise
Expand Down
10 changes: 9 additions & 1 deletion git/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,17 @@ class RemoteProgress(object):
DONE_TOKEN = 'done.'
TOKEN_SEPARATOR = ', '

__slots__ = ("_cur_line", "_seen_ops")
__slots__ = ("_cur_line", "_seen_ops", "_error_lines")
re_op_absolute = re.compile(r"(remote: )?([\w\s]+):\s+()(\d+)()(.*)")
re_op_relative = re.compile(r"(remote: )?([\w\s]+):\s+(\d+)% \((\d+)/(\d+)\)(.*)")

def __init__(self):
self._seen_ops = list()
self._cur_line = None
self._error_lines = []

def get_stderr(self):
return '\n'.join(self._error_lines)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend providing the highest-value data available, which in this case in an array of lines. There should be no assumption about how the user will evaluate that data.
As in GitPython in the very majority of cases there is no get_ prefix, this accessor would be called something like error_lines().


def _parse_progress_line(self, line):
"""Parse progress information from the given line as retrieved by git-push
Expand All @@ -190,6 +194,10 @@ def _parse_progress_line(self, line):
# Counting objects: 4, done.
# Compressing objects: 50% (1/2) \rCompressing objects: 100% (2/2) \rCompressing objects: 100% (2/2), done.
self._cur_line = line
if len(self._error_lines) > 0 or self._cur_line.startswith( ('error:', 'fatal:') ):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this would be a breakage in the API, as now lines that would previously be passed to update, will be caught in the base-class. Existing sub-types could depend on errors being handed to update().
Do you think that makes sense, or is there another reason this has to be done in the base-class ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once you see error or fatal the progress either never started or will not continue.

The parsing will never pass an error to update. It only passes data from lines that pattern match against
a "progress" line.

self._error_lines.append( self._cur_line )
return []

sub_lines = line.split('\r')
failed_lines = list()
for sline in sub_lines:
Expand Down