Skip to content

Commit

Permalink
diff: handle errors gracefully
Browse files Browse the repository at this point in the history
If `git diff` fails in any project checkout (e.g. an incomplete
sync), make sure we print that error clearly rather than blowing
up, and exit non-zero in the process.

Bug: https://crbug.com/gerrit/11613
Change-Id: I12f278427cced20f23f8047e7e3dba8f442ee25e
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/239236
Reviewed-by: David Pursehouse <[email protected]>
Tested-by: Mike Frysinger <[email protected]>
  • Loading branch information
vapier committed Oct 1, 2019
1 parent dc1b59d commit 0a9265e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
19 changes: 14 additions & 5 deletions project.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ class DiffColoring(Coloring):
def __init__(self, config):
Coloring.__init__(self, config, 'diff')
self.project = self.printer('header', attr='bold')
self.fail = self.printer('fail', fg='red')


class _Annotation(object):
Expand Down Expand Up @@ -1136,10 +1137,18 @@ def PrintWorkTreeDiff(self, absolute_paths=False):
cmd.append('--src-prefix=a/%s/' % self.relpath)
cmd.append('--dst-prefix=b/%s/' % self.relpath)
cmd.append('--')
p = GitCommand(self,
cmd,
capture_stdout=True,
capture_stderr=True)
try:
p = GitCommand(self,
cmd,
capture_stdout=True,
capture_stderr=True)
except GitError as e:
out.nl()
out.project('project %s/' % self.relpath)
out.nl()
out.fail('%s', str(e))
out.nl()
return False
has_diff = False
for line in p.process.stdout:
if not hasattr(line, 'encode'):
Expand All @@ -1150,7 +1159,7 @@ def PrintWorkTreeDiff(self, absolute_paths=False):
out.nl()
has_diff = True
print(line[:-1])
p.Wait()
return p.Wait() == 0


# Publish / Upload ##
Expand Down
5 changes: 4 additions & 1 deletion subcmds/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@ def cmd(option, opt_str, value, parser):
help='Paths are relative to the repository root')

def Execute(self, opt, args):
ret = 0
for project in self.GetProjects(args):
project.PrintWorkTreeDiff(opt.absolute)
if not project.PrintWorkTreeDiff(opt.absolute):
ret = 1
return ret

0 comments on commit 0a9265e

Please sign in to comment.