0.49.1
by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup. |
1 |
# Copyright (C) 2010 Canonical Ltd
|
2 |
#
|
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
17 |
"""Print lines matching PATTERN for specified files and revisions."""
|
|
18 |
||
0.40.147
by Jelmer Vernooij
Fix compatibility with newer versions of bzr: don't use relative imports in lazy imports, and import features from bzrlib.tests.features. |
19 |
from __future__ import absolute_import |
20 |
||
0.49.1
by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup. |
21 |
from bzrlib import errors |
22 |
from bzrlib.commands import Command, display_command |
|
23 |
from bzrlib.option import Option, ListOption |
|
0.50.1
by jdahlin at com
Add a global grep_color option |
24 |
from bzrlib.config import GlobalConfig |
0.49.1
by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup. |
25 |
|
26 |
# FIXME: _parse_levels should be shared with bzrlib.builtins. this is a copy
|
|
27 |
# to avoid the error
|
|
28 |
# "IllegalUseOfScopeReplacer: ScopeReplacer object '_parse_levels' was used
|
|
29 |
# incorrectly: Object already cleaned up, did you assign it to another
|
|
30 |
# variable?: _factory
|
|
31 |
# with lazy import
|
|
32 |
def _parse_levels(s): |
|
33 |
try: |
|
34 |
return int(s) |
|
35 |
except ValueError: |
|
36 |
msg = "The levels argument must be an integer." |
|
37 |
raise errors.BzrCommandError(msg) |
|
38 |
||
39 |
||
40 |
class GrepOptions(object): |
|
41 |
"""Container to pass around grep options.
|
|
42 |
||
43 |
This class is used as a container to pass around user option and
|
|
44 |
some other params (like outf) to processing functions. This makes
|
|
45 |
it easier to add more options as grep evolves.
|
|
46 |
"""
|
|
47 |
verbose = False |
|
48 |
ignore_case = False |
|
49 |
no_recursive = False |
|
50 |
from_root = False |
|
51 |
null = False |
|
52 |
levels = None |
|
53 |
line_number = False |
|
54 |
path_list = None |
|
55 |
revision = None |
|
56 |
pattern = None |
|
57 |
include = None |
|
58 |
exclude = None |
|
59 |
fixed_string = False |
|
60 |
files_with_matches = False |
|
61 |
files_without_match = False |
|
62 |
color = None |
|
63 |
diff = False |
|
64 |
||
65 |
# derived options
|
|
66 |
recursive = None |
|
67 |
eol_marker = None |
|
68 |
patternc = None |
|
69 |
sub_patternc = None |
|
70 |
print_revno = None |
|
71 |
fixed_string = None |
|
72 |
outf = None |
|
73 |
show_color = False |
|
74 |
||
75 |
||
76 |
class cmd_grep(Command): |
|
77 |
"""Print lines matching PATTERN for specified files and revisions.
|
|
78 |
||
79 |
This command searches the specified files and revisions for a given
|
|
80 |
pattern. The pattern is specified as a Python regular expressions[1].
|
|
81 |
||
82 |
If the file name is not specified, the revisions starting with the
|
|
83 |
current directory are searched recursively. If the revision number is
|
|
84 |
not specified, the working copy is searched. To search the last committed
|
|
85 |
revision, use the '-r -1' or '-r last:1' option.
|
|
86 |
||
87 |
Unversioned files are not searched unless explicitly specified on the
|
|
88 |
command line. Unversioned directores are not searched.
|
|
89 |
||
90 |
When searching a pattern, the output is shown in the 'filepath:string'
|
|
91 |
format. If a revision is explicitly searched, the output is shown as
|
|
92 |
'filepath~N:string', where N is the revision number.
|
|
93 |
||
94 |
--include and --exclude options can be used to search only (or exclude
|
|
95 |
from search) files with base name matches the specified Unix style GLOB
|
|
96 |
pattern. The GLOB pattern an use *, ?, and [...] as wildcards, and \\
|
|
97 |
to quote wildcard or backslash character literally. Note that the glob
|
|
98 |
pattern is not a regular expression.
|
|
99 |
||
100 |
[1] http://docs.python.org/library/re.html#regular-expression-syntax
|
|
101 |
"""
|
|
102 |
||
103 |
encoding_type = 'replace' |
|
104 |
takes_args = ['pattern', 'path*'] |
|
105 |
takes_options = [ |
|
106 |
'verbose', |
|
107 |
'revision', |
|
108 |
Option('color', type=str, argname='when', |
|
109 |
help='Show match in color. WHEN is never, always or auto.'), |
|
110 |
Option('diff', short_name='p', |
|
111 |
help='Grep for pattern in changeset for each revision.'), |
|
112 |
ListOption('exclude', type=str, argname='glob', short_name='X', |
|
113 |
help="Skip files whose base name matches GLOB."), |
|
114 |
ListOption('include', type=str, argname='glob', short_name='I', |
|
115 |
help="Search only files whose base name matches GLOB."), |
|
116 |
Option('files-with-matches', short_name='l', |
|
117 |
help='Print only the name of each input file in ' |
|
118 |
'which PATTERN is found.'), |
|
119 |
Option('files-without-match', short_name='L', |
|
120 |
help='Print only the name of each input file in ' |
|
121 |
'which PATTERN is not found.'), |
|
122 |
Option('fixed-string', short_name='F', |
|
123 |
help='Interpret PATTERN is a single fixed string (not regex).'), |
|
124 |
Option('from-root', |
|
125 |
help='Search for pattern starting from the root of the branch. ' |
|
126 |
'(implies --recursive)'), |
|
127 |
Option('ignore-case', short_name='i', |
|
128 |
help='ignore case distinctions while matching.'), |
|
129 |
Option('levels', |
|
130 |
help='Number of levels to display - 0 for all, 1 for collapsed ' |
|
131 |
'(1 is default).', |
|
132 |
argname='N', |
|
133 |
type=_parse_levels), |
|
134 |
Option('line-number', short_name='n', |
|
135 |
help='show 1-based line number.'), |
|
136 |
Option('no-recursive', |
|
137 |
help="Don't recurse into subdirectories. (default is --recursive)"), |
|
138 |
Option('null', short_name='Z', |
|
139 |
help='Write an ASCII NUL (\\0) separator ' |
|
140 |
'between output lines rather than a newline.'), |
|
141 |
]
|
|
142 |
||
143 |
||
144 |
@display_command
|
|
145 |
def run(self, verbose=False, ignore_case=False, no_recursive=False, |
|
146 |
from_root=False, null=False, levels=None, line_number=False, |
|
147 |
path_list=None, revision=None, pattern=None, include=None, |
|
148 |
exclude=None, fixed_string=False, files_with_matches=False, |
|
0.50.1
by jdahlin at com
Add a global grep_color option |
149 |
files_without_match=False, color=None, diff=False): |
6531.3.10
by Jelmer Vernooij
Rename termcolor to _termcolor. |
150 |
from bzrlib import _termcolor |
0.49.1
by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup. |
151 |
from bzrlib.plugins.grep import ( |
152 |
grep, |
|
153 |
)
|
|
154 |
import re |
|
6531.3.8
by Jelmer Vernooij
Move color feature into bzrlib.tests.features. |
155 |
if path_list is None: |
0.49.1
by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup. |
156 |
path_list = ['.'] |
157 |
else: |
|
158 |
if from_root: |
|
159 |
raise errors.BzrCommandError('cannot specify both --from-root and PATH.') |
|
160 |
||
161 |
if files_with_matches and files_without_match: |
|
162 |
raise errors.BzrCommandError('cannot specify both ' |
|
163 |
'-l/--files-with-matches and -L/--files-without-matches.') |
|
164 |
||
0.50.1
by jdahlin at com
Add a global grep_color option |
165 |
global_config = GlobalConfig() |
166 |
||
167 |
if color is None: |
|
168 |
color = global_config.get_user_option('grep_color') |
|
169 |
||
170 |
if color is None: |
|
171 |
color = 'never' |
|
172 |
||
0.49.1
by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup. |
173 |
if color not in ['always', 'never', 'auto']: |
174 |
raise errors.BzrCommandError('Valid values for --color are ' |
|
175 |
'"always", "never" or "auto".') |
|
176 |
||
177 |
if levels==None: |
|
178 |
levels=1 |
|
179 |
||
180 |
print_revno = False |
|
181 |
if revision != None or levels == 0: |
|
182 |
# print revision numbers as we may be showing multiple revisions
|
|
183 |
print_revno = True |
|
184 |
||
185 |
eol_marker = '\n' |
|
186 |
if null: |
|
187 |
eol_marker = '\0' |
|
188 |
||
189 |
if not ignore_case and grep.is_fixed_string(pattern): |
|
190 |
# if the pattern isalnum, implicitly use to -F for faster grep
|
|
191 |
fixed_string = True |
|
192 |
elif ignore_case and fixed_string: |
|
193 |
# GZ 2010-06-02: Fall back to regexp rather than lowercasing
|
|
194 |
# pattern and text which will cause pain later
|
|
195 |
fixed_string = False |
|
196 |
pattern = re.escape(pattern) |
|
197 |
||
198 |
patternc = None |
|
199 |
re_flags = re.MULTILINE |
|
200 |
if ignore_case: |
|
201 |
re_flags |= re.IGNORECASE |
|
202 |
||
203 |
if not fixed_string: |
|
204 |
patternc = grep.compile_pattern(pattern, re_flags) |
|
205 |
||
206 |
if color == 'always': |
|
207 |
show_color = True |
|
208 |
elif color == 'never': |
|
209 |
show_color = False |
|
210 |
elif color == 'auto': |
|
6531.3.10
by Jelmer Vernooij
Rename termcolor to _termcolor. |
211 |
show_color = _termcolor.allow_color() |
0.49.1
by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup. |
212 |
|
213 |
GrepOptions.verbose = verbose |
|
214 |
GrepOptions.ignore_case = ignore_case |
|
215 |
GrepOptions.no_recursive = no_recursive |
|
216 |
GrepOptions.from_root = from_root |
|
217 |
GrepOptions.null = null |
|
218 |
GrepOptions.levels = levels |
|
219 |
GrepOptions.line_number = line_number |
|
220 |
GrepOptions.path_list = path_list |
|
221 |
GrepOptions.revision = revision |
|
222 |
GrepOptions.pattern = pattern |
|
223 |
GrepOptions.include = include |
|
224 |
GrepOptions.exclude = exclude |
|
225 |
GrepOptions.fixed_string = fixed_string |
|
226 |
GrepOptions.files_with_matches = files_with_matches |
|
227 |
GrepOptions.files_without_match = files_without_match |
|
228 |
GrepOptions.color = color |
|
229 |
GrepOptions.diff = False |
|
230 |
||
231 |
GrepOptions.eol_marker = eol_marker |
|
232 |
GrepOptions.print_revno = print_revno |
|
233 |
GrepOptions.patternc = patternc |
|
234 |
GrepOptions.recursive = not no_recursive |
|
235 |
GrepOptions.fixed_string = fixed_string |
|
236 |
GrepOptions.outf = self.outf |
|
237 |
GrepOptions.show_color = show_color |
|
238 |
||
6531.3.8
by Jelmer Vernooij
Move color feature into bzrlib.tests.features. |
239 |
if diff: |
0.49.1
by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup. |
240 |
# options not used:
|
241 |
# files_with_matches, files_without_match
|
|
242 |
# levels(?), line_number, from_root
|
|
243 |
# include, exclude
|
|
244 |
# These are silently ignored.
|
|
245 |
grep.grep_diff(GrepOptions) |
|
6531.3.8
by Jelmer Vernooij
Move color feature into bzrlib.tests.features. |
246 |
elif revision is None: |
0.49.1
by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup. |
247 |
grep.workingtree_grep(GrepOptions) |
248 |
else: |
|
249 |
grep.versioned_grep(GrepOptions) |