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