diff options
author | Mike Frysinger <vapier@google.com> | 2019-10-01 00:18:46 -0400 |
---|---|---|
committer | Mike Frysinger <vapier@google.com> | 2019-10-01 05:45:58 +0000 |
commit | 72ab852ca503d1c8e32e493edc3fa3e1157e930b (patch) | |
tree | 842079bd38846936cb6f3da6d73f17d20ea478db | |
parent | 0a9265e2d633b608090eff79ab4553f0e1c8c7c4 (diff) | |
download | git-repo-72ab852ca503d1c8e32e493edc3fa3e1157e930b.tar.gz |
grep: handle errors gracefully
If `git grep` 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: I31de1134fdcc7aaa9814cf2eb6a67d398eebf9cf
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/239237
Reviewed-by: David Pursehouse <dpursehouse@collab.net>
Tested-by: Mike Frysinger <vapier@google.com>
-rw-r--r-- | subcmds/grep.py | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/subcmds/grep.py b/subcmds/grep.py index a588a78e..4dd85d57 100644 --- a/subcmds/grep.py +++ b/subcmds/grep.py | |||
@@ -15,15 +15,19 @@ | |||
15 | # limitations under the License. | 15 | # limitations under the License. |
16 | 16 | ||
17 | from __future__ import print_function | 17 | from __future__ import print_function |
18 | |||
18 | import sys | 19 | import sys |
20 | |||
19 | from color import Coloring | 21 | from color import Coloring |
20 | from command import PagedCommand | 22 | from command import PagedCommand |
23 | from error import GitError | ||
21 | from git_command import git_require, GitCommand | 24 | from git_command import git_require, GitCommand |
22 | 25 | ||
23 | class GrepColoring(Coloring): | 26 | class GrepColoring(Coloring): |
24 | def __init__(self, config): | 27 | def __init__(self, config): |
25 | Coloring.__init__(self, config, 'grep') | 28 | Coloring.__init__(self, config, 'grep') |
26 | self.project = self.printer('project', attr='bold') | 29 | self.project = self.printer('project', attr='bold') |
30 | self.fail = self.printer('fail', fg='red') | ||
27 | 31 | ||
28 | class Grep(PagedCommand): | 32 | class Grep(PagedCommand): |
29 | common = True | 33 | common = True |
@@ -184,15 +188,25 @@ contain a line that matches both expressions: | |||
184 | cmd_argv.extend(opt.revision) | 188 | cmd_argv.extend(opt.revision) |
185 | cmd_argv.append('--') | 189 | cmd_argv.append('--') |
186 | 190 | ||
191 | git_failed = False | ||
187 | bad_rev = False | 192 | bad_rev = False |
188 | have_match = False | 193 | have_match = False |
189 | 194 | ||
190 | for project in projects: | 195 | for project in projects: |
191 | p = GitCommand(project, | 196 | try: |
192 | cmd_argv, | 197 | p = GitCommand(project, |
193 | bare = False, | 198 | cmd_argv, |
194 | capture_stdout = True, | 199 | bare=False, |
195 | capture_stderr = True) | 200 | capture_stdout=True, |
201 | capture_stderr=True) | ||
202 | except GitError as e: | ||
203 | git_failed = True | ||
204 | out.project('--- project %s ---' % project.relpath) | ||
205 | out.nl() | ||
206 | out.fail('%s', str(e)) | ||
207 | out.nl() | ||
208 | continue | ||
209 | |||
196 | if p.Wait() != 0: | 210 | if p.Wait() != 0: |
197 | # no results | 211 | # no results |
198 | # | 212 | # |
@@ -202,7 +216,7 @@ contain a line that matches both expressions: | |||
202 | else: | 216 | else: |
203 | out.project('--- project %s ---' % project.relpath) | 217 | out.project('--- project %s ---' % project.relpath) |
204 | out.nl() | 218 | out.nl() |
205 | out.write("%s", p.stderr) | 219 | out.fail('%s', p.stderr.strip()) |
206 | out.nl() | 220 | out.nl() |
207 | continue | 221 | continue |
208 | have_match = True | 222 | have_match = True |
@@ -231,7 +245,9 @@ contain a line that matches both expressions: | |||
231 | for line in r: | 245 | for line in r: |
232 | print(line) | 246 | print(line) |
233 | 247 | ||
234 | if have_match: | 248 | if git_failed: |
249 | sys.exit(1) | ||
250 | elif have_match: | ||
235 | sys.exit(0) | 251 | sys.exit(0) |
236 | elif have_rev and bad_rev: | 252 | elif have_rev and bad_rev: |
237 | for r in opt.revision: | 253 | for r in opt.revision: |