Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 1 | # Copyright (C) 2009 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Mike Frysinger | d246d1f | 2021-02-24 12:50:30 -0500 | [diff] [blame] | 15 | import functools |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 16 | import sys |
Mike Frysinger | 72ab852 | 2019-10-01 00:18:46 -0400 | [diff] [blame] | 17 | |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 18 | from color import Coloring |
Mike Frysinger | b5d075d | 2021-03-01 00:56:38 -0500 | [diff] [blame] | 19 | from command import DEFAULT_LOCAL_JOBS, PagedCommand |
Mike Frysinger | 72ab852 | 2019-10-01 00:18:46 -0400 | [diff] [blame] | 20 | from error import GitError |
Mike Frysinger | 6f1c626 | 2020-02-04 00:09:23 -0500 | [diff] [blame] | 21 | from git_command import GitCommand |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 22 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 23 | |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 24 | class GrepColoring(Coloring): |
| 25 | def __init__(self, config): |
| 26 | Coloring.__init__(self, config, 'grep') |
| 27 | self.project = self.printer('project', attr='bold') |
Mike Frysinger | 72ab852 | 2019-10-01 00:18:46 -0400 | [diff] [blame] | 28 | self.fail = self.printer('fail', fg='red') |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 29 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 30 | |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 31 | class Grep(PagedCommand): |
Mike Frysinger | 4f21054 | 2021-06-14 16:05:19 -0400 | [diff] [blame] | 32 | COMMON = True |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 33 | helpSummary = "Print lines matching a pattern" |
| 34 | helpUsage = """ |
| 35 | %prog {pattern | -e pattern} [<project>...] |
| 36 | """ |
| 37 | helpDescription = """ |
| 38 | Search for the specified patterns in all project files. |
| 39 | |
Mike Frysinger | b8f7bb0 | 2018-10-10 01:05:11 -0400 | [diff] [blame] | 40 | # Boolean Options |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 41 | |
| 42 | The following options can appear as often as necessary to express |
| 43 | the pattern to locate: |
| 44 | |
| 45 | -e PATTERN |
| 46 | --and, --or, --not, -(, -) |
| 47 | |
| 48 | Further, the -r/--revision option may be specified multiple times |
| 49 | in order to scan multiple trees. If the same file matches in more |
| 50 | than one tree, only the first result is reported, prefixed by the |
| 51 | revision name it was found under. |
| 52 | |
Mike Frysinger | b8f7bb0 | 2018-10-10 01:05:11 -0400 | [diff] [blame] | 53 | # Examples |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 54 | |
| 55 | Look for a line that has '#define' and either 'MAX_PATH or 'PATH_MAX': |
| 56 | |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 57 | repo grep -e '#define' --and -\\( -e MAX_PATH -e PATH_MAX \\) |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 58 | |
| 59 | Look for a line that has 'NODE' or 'Unexpected' in files that |
| 60 | contain a line that matches both expressions: |
| 61 | |
| 62 | repo grep --all-match -e NODE -e Unexpected |
| 63 | |
| 64 | """ |
Mike Frysinger | d246d1f | 2021-02-24 12:50:30 -0500 | [diff] [blame] | 65 | PARALLEL_JOBS = DEFAULT_LOCAL_JOBS |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 66 | |
Mike Frysinger | 45ad154 | 2021-02-24 12:47:01 -0500 | [diff] [blame] | 67 | @staticmethod |
| 68 | def _carry_option(_option, opt_str, value, parser): |
| 69 | pt = getattr(parser.values, 'cmd_argv', None) |
| 70 | if pt is None: |
| 71 | pt = [] |
| 72 | setattr(parser.values, 'cmd_argv', pt) |
| 73 | |
| 74 | if opt_str == '-(': |
| 75 | pt.append('(') |
| 76 | elif opt_str == '-)': |
| 77 | pt.append(')') |
| 78 | else: |
| 79 | pt.append(opt_str) |
| 80 | |
| 81 | if value is not None: |
| 82 | pt.append(value) |
| 83 | |
Mike Frysinger | 9180a07 | 2021-04-13 14:57:40 -0400 | [diff] [blame] | 84 | def _CommonOptions(self, p): |
| 85 | """Override common options slightly.""" |
| 86 | super()._CommonOptions(p, opt_v=False) |
| 87 | |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 88 | def _Options(self, p): |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 89 | g = p.add_option_group('Sources') |
| 90 | g.add_option('--cached', |
Mike Frysinger | 45ad154 | 2021-02-24 12:47:01 -0500 | [diff] [blame] | 91 | action='callback', callback=self._carry_option, |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 92 | help='Search the index, instead of the work tree') |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 93 | g.add_option('-r', '--revision', |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 94 | dest='revision', action='append', metavar='TREEish', |
| 95 | help='Search TREEish, instead of the work tree') |
| 96 | |
| 97 | g = p.add_option_group('Pattern') |
| 98 | g.add_option('-e', |
Mike Frysinger | 45ad154 | 2021-02-24 12:47:01 -0500 | [diff] [blame] | 99 | action='callback', callback=self._carry_option, |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 100 | metavar='PATTERN', type='str', |
| 101 | help='Pattern to search for') |
| 102 | g.add_option('-i', '--ignore-case', |
Mike Frysinger | 45ad154 | 2021-02-24 12:47:01 -0500 | [diff] [blame] | 103 | action='callback', callback=self._carry_option, |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 104 | help='Ignore case differences') |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 105 | g.add_option('-a', '--text', |
Mike Frysinger | 45ad154 | 2021-02-24 12:47:01 -0500 | [diff] [blame] | 106 | action='callback', callback=self._carry_option, |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 107 | help="Process binary files as if they were text") |
| 108 | g.add_option('-I', |
Mike Frysinger | 45ad154 | 2021-02-24 12:47:01 -0500 | [diff] [blame] | 109 | action='callback', callback=self._carry_option, |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 110 | help="Don't match the pattern in binary files") |
| 111 | g.add_option('-w', '--word-regexp', |
Mike Frysinger | 45ad154 | 2021-02-24 12:47:01 -0500 | [diff] [blame] | 112 | action='callback', callback=self._carry_option, |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 113 | help='Match the pattern only at word boundaries') |
| 114 | g.add_option('-v', '--invert-match', |
Mike Frysinger | 45ad154 | 2021-02-24 12:47:01 -0500 | [diff] [blame] | 115 | action='callback', callback=self._carry_option, |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 116 | help='Select non-matching lines') |
| 117 | g.add_option('-G', '--basic-regexp', |
Mike Frysinger | 45ad154 | 2021-02-24 12:47:01 -0500 | [diff] [blame] | 118 | action='callback', callback=self._carry_option, |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 119 | help='Use POSIX basic regexp for patterns (default)') |
| 120 | g.add_option('-E', '--extended-regexp', |
Mike Frysinger | 45ad154 | 2021-02-24 12:47:01 -0500 | [diff] [blame] | 121 | action='callback', callback=self._carry_option, |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 122 | help='Use POSIX extended regexp for patterns') |
| 123 | g.add_option('-F', '--fixed-strings', |
Mike Frysinger | 45ad154 | 2021-02-24 12:47:01 -0500 | [diff] [blame] | 124 | action='callback', callback=self._carry_option, |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 125 | help='Use fixed strings (not regexp) for pattern') |
| 126 | |
| 127 | g = p.add_option_group('Pattern Grouping') |
| 128 | g.add_option('--all-match', |
Mike Frysinger | 45ad154 | 2021-02-24 12:47:01 -0500 | [diff] [blame] | 129 | action='callback', callback=self._carry_option, |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 130 | help='Limit match to lines that have all patterns') |
| 131 | g.add_option('--and', '--or', '--not', |
Mike Frysinger | 45ad154 | 2021-02-24 12:47:01 -0500 | [diff] [blame] | 132 | action='callback', callback=self._carry_option, |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 133 | help='Boolean operators to combine patterns') |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 134 | g.add_option('-(', '-)', |
Mike Frysinger | 45ad154 | 2021-02-24 12:47:01 -0500 | [diff] [blame] | 135 | action='callback', callback=self._carry_option, |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 136 | help='Boolean operator grouping') |
| 137 | |
| 138 | g = p.add_option_group('Output') |
| 139 | g.add_option('-n', |
Mike Frysinger | 45ad154 | 2021-02-24 12:47:01 -0500 | [diff] [blame] | 140 | action='callback', callback=self._carry_option, |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 141 | help='Prefix the line number to matching lines') |
| 142 | g.add_option('-C', |
Mike Frysinger | 45ad154 | 2021-02-24 12:47:01 -0500 | [diff] [blame] | 143 | action='callback', callback=self._carry_option, |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 144 | metavar='CONTEXT', type='str', |
| 145 | help='Show CONTEXT lines around match') |
| 146 | g.add_option('-B', |
Mike Frysinger | 45ad154 | 2021-02-24 12:47:01 -0500 | [diff] [blame] | 147 | action='callback', callback=self._carry_option, |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 148 | metavar='CONTEXT', type='str', |
| 149 | help='Show CONTEXT lines before match') |
| 150 | g.add_option('-A', |
Mike Frysinger | 45ad154 | 2021-02-24 12:47:01 -0500 | [diff] [blame] | 151 | action='callback', callback=self._carry_option, |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 152 | metavar='CONTEXT', type='str', |
| 153 | help='Show CONTEXT lines after match') |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 154 | g.add_option('-l', '--name-only', '--files-with-matches', |
Mike Frysinger | 45ad154 | 2021-02-24 12:47:01 -0500 | [diff] [blame] | 155 | action='callback', callback=self._carry_option, |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 156 | help='Show only file names containing matching lines') |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 157 | g.add_option('-L', '--files-without-match', |
Mike Frysinger | 45ad154 | 2021-02-24 12:47:01 -0500 | [diff] [blame] | 158 | action='callback', callback=self._carry_option, |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 159 | help='Show only file names not containing matching lines') |
| 160 | |
Mike Frysinger | d246d1f | 2021-02-24 12:50:30 -0500 | [diff] [blame] | 161 | def _ExecuteOne(self, cmd_argv, project): |
| 162 | """Process one project.""" |
| 163 | try: |
| 164 | p = GitCommand(project, |
| 165 | cmd_argv, |
| 166 | bare=False, |
| 167 | capture_stdout=True, |
| 168 | capture_stderr=True) |
| 169 | except GitError as e: |
| 170 | return (project, -1, None, str(e)) |
| 171 | |
| 172 | return (project, p.Wait(), p.stdout, p.stderr) |
| 173 | |
| 174 | @staticmethod |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 175 | def _ProcessResults(full_name, have_rev, opt, _pool, out, results): |
Mike Frysinger | d246d1f | 2021-02-24 12:50:30 -0500 | [diff] [blame] | 176 | git_failed = False |
| 177 | bad_rev = False |
| 178 | have_match = False |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 179 | _RelPath = lambda p: p.RelPath(local=opt.this_manifest_only) |
Mike Frysinger | d246d1f | 2021-02-24 12:50:30 -0500 | [diff] [blame] | 180 | |
| 181 | for project, rc, stdout, stderr in results: |
| 182 | if rc < 0: |
| 183 | git_failed = True |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 184 | out.project('--- project %s ---' % _RelPath(project)) |
Mike Frysinger | d246d1f | 2021-02-24 12:50:30 -0500 | [diff] [blame] | 185 | out.nl() |
| 186 | out.fail('%s', stderr) |
| 187 | out.nl() |
| 188 | continue |
| 189 | |
| 190 | if rc: |
| 191 | # no results |
| 192 | if stderr: |
| 193 | if have_rev and 'fatal: ambiguous argument' in stderr: |
| 194 | bad_rev = True |
| 195 | else: |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 196 | out.project('--- project %s ---' % _RelPath(project)) |
Mike Frysinger | d246d1f | 2021-02-24 12:50:30 -0500 | [diff] [blame] | 197 | out.nl() |
| 198 | out.fail('%s', stderr.strip()) |
| 199 | out.nl() |
| 200 | continue |
| 201 | have_match = True |
| 202 | |
| 203 | # We cut the last element, to avoid a blank line. |
| 204 | r = stdout.split('\n') |
| 205 | r = r[0:-1] |
| 206 | |
| 207 | if have_rev and full_name: |
| 208 | for line in r: |
| 209 | rev, line = line.split(':', 1) |
| 210 | out.write("%s", rev) |
| 211 | out.write(':') |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 212 | out.project(_RelPath(project)) |
Mike Frysinger | d246d1f | 2021-02-24 12:50:30 -0500 | [diff] [blame] | 213 | out.write('/') |
| 214 | out.write("%s", line) |
| 215 | out.nl() |
| 216 | elif full_name: |
| 217 | for line in r: |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 218 | out.project(_RelPath(project)) |
Mike Frysinger | d246d1f | 2021-02-24 12:50:30 -0500 | [diff] [blame] | 219 | out.write('/') |
| 220 | out.write("%s", line) |
| 221 | out.nl() |
| 222 | else: |
| 223 | for line in r: |
| 224 | print(line) |
| 225 | |
| 226 | return (git_failed, bad_rev, have_match) |
| 227 | |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 228 | def Execute(self, opt, args): |
| 229 | out = GrepColoring(self.manifest.manifestProject.config) |
| 230 | |
| 231 | cmd_argv = ['grep'] |
Mike Frysinger | 6f1c626 | 2020-02-04 00:09:23 -0500 | [diff] [blame] | 232 | if out.is_on: |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 233 | cmd_argv.append('--color') |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 234 | cmd_argv.extend(getattr(opt, 'cmd_argv', [])) |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 235 | |
| 236 | if '-e' not in cmd_argv: |
| 237 | if not args: |
| 238 | self.Usage() |
| 239 | cmd_argv.append('-e') |
| 240 | cmd_argv.append(args[0]) |
| 241 | args = args[1:] |
| 242 | |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 243 | projects = self.GetProjects(args, all_manifests=not opt.this_manifest_only) |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 244 | |
| 245 | full_name = False |
| 246 | if len(projects) > 1: |
| 247 | cmd_argv.append('--full-name') |
| 248 | full_name = True |
| 249 | |
| 250 | have_rev = False |
| 251 | if opt.revision: |
| 252 | if '--cached' in cmd_argv: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 253 | print('fatal: cannot combine --cached and --revision', file=sys.stderr) |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 254 | sys.exit(1) |
| 255 | have_rev = True |
| 256 | cmd_argv.extend(opt.revision) |
| 257 | cmd_argv.append('--') |
| 258 | |
Mike Frysinger | b5d075d | 2021-03-01 00:56:38 -0500 | [diff] [blame] | 259 | git_failed, bad_rev, have_match = self.ExecuteInParallel( |
| 260 | opt.jobs, |
| 261 | functools.partial(self._ExecuteOne, cmd_argv), |
| 262 | projects, |
LaMont Jones | cc879a9 | 2021-11-18 22:40:18 +0000 | [diff] [blame] | 263 | callback=functools.partial(self._ProcessResults, full_name, have_rev, opt), |
Mike Frysinger | b5d075d | 2021-03-01 00:56:38 -0500 | [diff] [blame] | 264 | output=out, |
| 265 | ordered=True) |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 266 | |
Mike Frysinger | 72ab852 | 2019-10-01 00:18:46 -0400 | [diff] [blame] | 267 | if git_failed: |
| 268 | sys.exit(1) |
| 269 | elif have_match: |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 270 | sys.exit(0) |
| 271 | elif have_rev and bad_rev: |
| 272 | for r in opt.revision: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 273 | print("error: can't search revision %s" % r, file=sys.stderr) |
Shawn O. Pearce | b812a36 | 2009-04-10 20:37:47 -0700 | [diff] [blame] | 274 | sys.exit(1) |
| 275 | else: |
| 276 | sys.exit(1) |