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