Merge lp:~parthm/bzr-grep/bzr-grep-1st-full-review into lp:bzr-grep
- bzr-grep-1st-full-review
- Merge into trunk
Status: | Rejected |
---|---|
Rejected by: | Parth Malwankar |
Proposed branch: | lp:~parthm/bzr-grep/bzr-grep-1st-full-review |
Merge into: | lp:bzr-grep |
Diff against target: |
801 lines (+0/-782) 4 files modified
__init__.py (+0/-202) grep.py (+0/-114) setup.py (+0/-19) test_grep.py (+0/-447) |
To merge this branch: | bzr merge lp:~parthm/bzr-grep/bzr-grep-1st-full-review |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Jelmer Vernooij (community) | code | Needs Information | |
Review via email:
|
Commit message
Description of the change
Proposal for reviewing initial version of bzr-grep. This plugin was created in response to bug #503670.
Unfortunately, for lack of a better way to do a first review, I have deleted all files in this mp (so once review comments are closed I will reject this mp).
[~]% bzr help grep
Purpose: Print lines matching PATTERN for specified files and revisions.
Usage: bzr grep PATTERN [PATH...]
Options:
--from-root Search for pattern starting from the root of the
-Z, --null Write an ascii NUL (\0) separator between output lines
-v, --verbose Display more information.
-R, --recursive Recurse into subdirectories.
-h, --help Show help message.
-q, --quiet Only display errors and warnings.
-i, --ignore-case ignore case distinctions while matching.
--levels=N Number of levels to display - 0 for all, 1 for
--usage Show usage message and options.
-n, --line-number show 1-based line number.
-r ARG, --revision=ARG
Description:
This command searches the specified files and revisions for a given pattern.
The pattern is specified as a Python regular expressions[1].
If the file name is not specified the file revisions in the current directory
are searched. If the revision number is not specified, the latest revision is
searched.
Note that this command is different from POSIX grep in that it searches the
revisions of the branch and not the working copy. Unversioned files and
uncommitted changes are not seen.
When searching a pattern, the output is shown in the 'filepath:string' format.
If a revision is explicitly searched, the output is shown as 'filepath~
where N is the revision number.
[1] http://
From: plugin "grep"
See also: plugins/grep

Jelmer Vernooij (jelmer) wrote : | # |

Jelmer Vernooij (jelmer) : | # |

Parth Malwankar (parthm) wrote : | # |
> I'm not sure what you're asking for exactly - a review of bzr-grep in its
> entirety?
This request is very old - for the first version of bzr-grep, when I was new to bzr API. I don't think this review is needed any more. Martin [gz] also helped with some refactoring and cleanup at a later point.
I will go ahead and mark this as rejected.
Thanks.
Unmerged revisions
- 54. By Parth Malwankar
-
dummy delete to do first review with full source.
Preview Diff
1 | === removed file '__init__.py' |
2 | --- __init__.py 2010-03-03 17:07:15 +0000 |
3 | +++ __init__.py 1970-01-01 00:00:00 +0000 |
4 | @@ -1,202 +0,0 @@ |
5 | -# Copyright (C) 2010 Canonical Ltd |
6 | -# |
7 | -# This program is free software; you can redistribute it and/or modify |
8 | -# it under the terms of the GNU General Public License as published by |
9 | -# the Free Software Foundation; either version 2 of the License, or |
10 | -# (at your option) any later version. |
11 | -# |
12 | -# This program is distributed in the hope that it will be useful, |
13 | -# but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | -# GNU General Public License for more details. |
16 | -# |
17 | -# You should have received a copy of the GNU General Public License |
18 | -# along with this program; if not, write to the Free Software |
19 | -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 | - |
21 | -import os |
22 | -import sys |
23 | - |
24 | -from bzrlib import errors |
25 | -from bzrlib.commands import Command, register_command, display_command |
26 | -from bzrlib.option import ( |
27 | - Option, |
28 | - ) |
29 | - |
30 | -from bzrlib.lazy_import import lazy_import |
31 | -lazy_import(globals(), """ |
32 | -import re |
33 | - |
34 | -import grep |
35 | - |
36 | -import bzrlib |
37 | -from bzrlib.builtins import _get_revision_range |
38 | -from bzrlib.revisionspec import RevisionSpec, RevisionSpec_revid |
39 | -from bzrlib.workingtree import WorkingTree |
40 | -from bzrlib import log as logcmd |
41 | -from bzrlib import ( |
42 | - osutils, |
43 | - bzrdir, |
44 | - trace, |
45 | - ) |
46 | -""") |
47 | - |
48 | -version_info = (0, 0, 1, 'dev', 1) |
49 | - |
50 | -# FIXME: _parse_levels should be shared with bzrlib.builtins. this is a copy |
51 | -# to avoid the error |
52 | -# "IllegalUseOfScopeReplacer: ScopeReplacer object '_parse_levels' was used |
53 | -# incorrectly: Object already cleaned up, did you assign it to another |
54 | -# variable?: _factory |
55 | -# with lazy import |
56 | -def _parse_levels(s): |
57 | - try: |
58 | - return int(s) |
59 | - except ValueError: |
60 | - msg = "The levels argument must be an integer." |
61 | - raise errors.BzrCommandError(msg) |
62 | - |
63 | - |
64 | -class cmd_grep(Command): |
65 | - """Print lines matching PATTERN for specified files and revisions. |
66 | - |
67 | - This command searches the specified files and revisions for a given pattern. |
68 | - The pattern is specified as a Python regular expressions[1]. |
69 | - If the file name is not specified the file revisions in the current directory |
70 | - are searched. If the revision number is not specified, the latest revision is |
71 | - searched. |
72 | - |
73 | - Note that this command is different from POSIX grep in that it searches the |
74 | - revisions of the branch and not the working copy. Unversioned files and |
75 | - uncommitted changes are not seen. |
76 | - |
77 | - When searching a pattern, the output is shown in the 'filepath:string' format. |
78 | - If a revision is explicitly searched, the output is shown as 'filepath~N:string', |
79 | - where N is the revision number. |
80 | - |
81 | - [1] http://docs.python.org/library/re.html#regular-expression-syntax |
82 | - """ |
83 | - |
84 | - takes_args = ['pattern', 'path*'] |
85 | - takes_options = [ |
86 | - 'verbose', |
87 | - 'revision', |
88 | - Option('line-number', short_name='n', |
89 | - help='show 1-based line number.'), |
90 | - Option('ignore-case', short_name='i', |
91 | - help='ignore case distinctions while matching.'), |
92 | - Option('recursive', short_name='R', |
93 | - help='Recurse into subdirectories.'), |
94 | - Option('from-root', |
95 | - help='Search for pattern starting from the root of the branch. ' |
96 | - '(implies --recursive)'), |
97 | - Option('null', short_name='Z', |
98 | - help='Write an ascii NUL (\\0) separator ' |
99 | - 'between output lines rather than a newline.'), |
100 | - Option('levels', |
101 | - help='Number of levels to display - 0 for all, 1 for collapsed (default).', |
102 | - argname='N', |
103 | - type=_parse_levels), |
104 | - ] |
105 | - |
106 | - |
107 | - @display_command |
108 | - def run(self, verbose=False, ignore_case=False, recursive=False, from_root=False, |
109 | - null=False, levels=None, line_number=False, path_list=None, revision=None, pattern=None): |
110 | - |
111 | - if levels==None: |
112 | - levels=1 |
113 | - |
114 | - if path_list == None: |
115 | - path_list = ['.'] |
116 | - else: |
117 | - if from_root: |
118 | - raise errors.BzrCommandError('cannot specify both --from-root and PATH.') |
119 | - |
120 | - print_revno = False |
121 | - if revision != None or levels == 0: |
122 | - # print revision numbers as we may be showing multiple revisions |
123 | - print_revno = True |
124 | - |
125 | - if revision == None: |
126 | - # grep on latest revision by default |
127 | - revision = [RevisionSpec.from_string("last:1")] |
128 | - |
129 | - start_rev = revision[0] |
130 | - end_rev = revision[0] |
131 | - if len(revision) == 2: |
132 | - end_rev = revision[1] |
133 | - |
134 | - eol_marker = '\n' |
135 | - if null: |
136 | - eol_marker = '\0' |
137 | - |
138 | - re_flags = 0 |
139 | - if ignore_case: |
140 | - re_flags = re.IGNORECASE |
141 | - patternc = grep.compile_pattern(pattern, re_flags) |
142 | - |
143 | - wt, relpath = WorkingTree.open_containing('.') |
144 | - |
145 | - start_revid = start_rev.as_revision_id(wt.branch) |
146 | - end_revid = end_rev.as_revision_id(wt.branch) |
147 | - |
148 | - given_revs = logcmd._graph_view_revisions(wt.branch, start_revid, end_revid) |
149 | - |
150 | - # edge case: we have a repo created with 'bzr init' and it has no |
151 | - # revisions (revno: 0) |
152 | - try: |
153 | - given_revs = list(given_revs) |
154 | - except errors.NoSuchRevision, e: |
155 | - raise errors.BzrCommandError('No revisions found for grep.') |
156 | - |
157 | - for revid, revno, merge_depth in given_revs: |
158 | - if levels == 1 and merge_depth != 0: |
159 | - # with level=1 show only top level |
160 | - continue |
161 | - |
162 | - wt.lock_read() |
163 | - rev = RevisionSpec_revid.from_string("revid:"+revid) |
164 | - try: |
165 | - for path in path_list: |
166 | - tree = rev.as_tree(wt.branch) |
167 | - path_for_id = osutils.pathjoin(relpath, path) |
168 | - id = tree.path2id(path_for_id) |
169 | - if not id: |
170 | - self._skip_file(path) |
171 | - continue |
172 | - |
173 | - if osutils.isdir(path): |
174 | - path_prefix = path |
175 | - grep.dir_grep(tree, path, relpath, recursive, line_number, |
176 | - patternc, from_root, eol_marker, revno, print_revno, |
177 | - self.outf, path_prefix) |
178 | - else: |
179 | - tree.lock_read() |
180 | - try: |
181 | - grep.file_grep(tree, id, '.', path, patternc, eol_marker, |
182 | - line_number, revno, print_revno, self.outf) |
183 | - finally: |
184 | - tree.unlock() |
185 | - finally: |
186 | - wt.unlock() |
187 | - |
188 | - def _skip_file(self, path): |
189 | - trace.warning("warning: skipped unknown file '%s'." % path) |
190 | - |
191 | - |
192 | -register_command(cmd_grep) |
193 | - |
194 | -def test_suite(): |
195 | - from bzrlib.tests import TestUtil |
196 | - |
197 | - suite = TestUtil.TestSuite() |
198 | - loader = TestUtil.TestLoader() |
199 | - testmod_names = [ |
200 | - 'test_grep', |
201 | - ] |
202 | - |
203 | - suite.addTest(loader.loadTestsFromModuleNames( |
204 | - ["%s.%s" % (__name__, tmn) for tmn in testmod_names])) |
205 | - return suite |
206 | - |
207 | |
208 | === removed file 'grep.py' |
209 | --- grep.py 2010-03-03 17:07:15 +0000 |
210 | +++ grep.py 1970-01-01 00:00:00 +0000 |
211 | @@ -1,114 +0,0 @@ |
212 | -# Copyright (C) 2010 Canonical Ltd |
213 | -# |
214 | -# This program is free software; you can redistribute it and/or modify |
215 | -# it under the terms of the GNU General Public License as published by |
216 | -# the Free Software Foundation; either version 2 of the License, or |
217 | -# (at your option) any later version. |
218 | -# |
219 | -# This program is distributed in the hope that it will be useful, |
220 | -# but WITHOUT ANY WARRANTY; without even the implied warranty of |
221 | -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
222 | -# GNU General Public License for more details. |
223 | -# |
224 | -# You should have received a copy of the GNU General Public License |
225 | -# along with this program; if not, write to the Free Software |
226 | -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
227 | - |
228 | -from bzrlib.lazy_import import lazy_import |
229 | -lazy_import(globals(), """ |
230 | -import os |
231 | -import re |
232 | -import cStringIO |
233 | - |
234 | -from bzrlib import ( |
235 | - errors, |
236 | - lazy_regex, |
237 | - osutils, |
238 | - textfile, |
239 | - trace, |
240 | - ) |
241 | -""") |
242 | - |
243 | -def compile_pattern(pattern, flags=0): |
244 | - patternc = None |
245 | - try: |
246 | - # use python's re.compile as we need to catch re.error in case of bad pattern |
247 | - lazy_regex.reset_compile() |
248 | - patternc = re.compile(pattern, flags) |
249 | - except re.error, e: |
250 | - raise errors.BzrError("Invalid pattern: '%s'" % pattern) |
251 | - return patternc |
252 | - |
253 | -def dir_grep(tree, path, relpath, recursive, line_number, compiled_pattern, |
254 | - from_root, eol_marker, revno, print_revno, outf, path_prefix): |
255 | - # setup relpath to open files relative to cwd |
256 | - rpath = relpath |
257 | - if relpath: |
258 | - rpath = osutils.pathjoin('..',relpath) |
259 | - |
260 | - tree.lock_read() |
261 | - try: |
262 | - from_dir = osutils.pathjoin(relpath, path) |
263 | - if from_root: |
264 | - # start searching recursively from root |
265 | - from_dir=None |
266 | - recursive=True |
267 | - |
268 | - for fp, fc, fkind, fid, entry in tree.list_files(include_root=False, |
269 | - from_dir=from_dir, recursive=recursive): |
270 | - if fc == 'V' and fkind == 'file': |
271 | - file_grep(tree, fid, rpath, fp, compiled_pattern, |
272 | - eol_marker, line_number, revno, print_revno, outf, path_prefix) |
273 | - finally: |
274 | - tree.unlock() |
275 | - |
276 | - |
277 | -def file_grep(tree, id, relpath, path, patternc, eol_marker, |
278 | - line_number, revno, print_revno, outf, path_prefix = None): |
279 | - |
280 | - if relpath: |
281 | - # update path so to display it w.r.t cwd |
282 | - # handle windows slash separator |
283 | - path = osutils.normpath(osutils.pathjoin(relpath, path)) |
284 | - path = path.replace('\\', '/') |
285 | - path = path.replace(relpath + '/', '', 1) |
286 | - |
287 | - if path_prefix and path_prefix != '.': |
288 | - # user has passed a dir arg, show that as result prefix |
289 | - path = osutils.pathjoin(path_prefix, path) |
290 | - |
291 | - revfmt = '' |
292 | - if print_revno: |
293 | - revfmt = "~%s" |
294 | - |
295 | - fmt_with_n = path + revfmt + ":%d:%s" + eol_marker |
296 | - fmt_without_n = path + revfmt + ":%s" + eol_marker |
297 | - |
298 | - # test and skip binary files |
299 | - str_file = cStringIO.StringIO(tree.get_file_text(id)) |
300 | - try: |
301 | - iter_file = textfile.text_file(str_file) |
302 | - except errors.BinaryFile, e: |
303 | - trace.warning("Binary file '%s' skipped." % path) |
304 | - return |
305 | - |
306 | - index = 1 |
307 | - for line in iter_file: |
308 | - res = patternc.search(line) |
309 | - if res: |
310 | - if line_number: |
311 | - if print_revno: |
312 | - out = (revno, index, line.strip()) |
313 | - else: |
314 | - out = (index, line.strip()) |
315 | - outf.write(fmt_with_n % out) |
316 | - else: |
317 | - if print_revno: |
318 | - out = (revno, line.strip()) |
319 | - else: |
320 | - out = (line.strip(),) |
321 | - outf.write(fmt_without_n % out) |
322 | - |
323 | - index += 1 |
324 | - |
325 | - |
326 | |
327 | === removed file 'setup.py' |
328 | --- setup.py 2010-03-03 16:48:48 +0000 |
329 | +++ setup.py 1970-01-01 00:00:00 +0000 |
330 | @@ -1,19 +0,0 @@ |
331 | -#!/usr/bin/env python2.4 |
332 | -from distutils.core import setup |
333 | - |
334 | -bzr_plugin_name = 'grep' |
335 | - |
336 | -bzr_plugin_version = (0, 0, 1, 'dev', 1) |
337 | -bzr_commands = ['grep'] |
338 | - |
339 | -if __name__ == 'main': |
340 | - setup(name="bzr grep", |
341 | - version="0.1dev", |
342 | - description="Print lines matching pattern for specified " |
343 | - "files and revisions", |
344 | - author="Canonical Ltd", |
345 | - author_email="bazaar@lists.canonical.com", |
346 | - license = "GNU GPL v2", |
347 | - url="https://launchpad.net/bzr-grep", |
348 | - packages=['grep'], |
349 | - package_dir={'grep': '.'}) |
350 | |
351 | === removed file 'test_grep.py' |
352 | --- test_grep.py 2010-03-03 14:14:47 +0000 |
353 | +++ test_grep.py 1970-01-01 00:00:00 +0000 |
354 | @@ -1,447 +0,0 @@ |
355 | -# Copyright (C) 2010 Canonical Ltd |
356 | -# |
357 | -# This program is free software; you can redistribute it and/or modify |
358 | -# it under the terms of the GNU General Public License as published by |
359 | -# the Free Software Foundation; either version 2 of the License, or |
360 | -# (at your option) any later version. |
361 | -# |
362 | -# This program is distributed in the hope that it will be useful, |
363 | -# but WITHOUT ANY WARRANTY; without even the implied warranty of |
364 | -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
365 | -# GNU General Public License for more details. |
366 | -# |
367 | -# You should have received a copy of the GNU General Public License |
368 | -# along with this program; if not, write to the Free Software |
369 | -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
370 | - |
371 | -import os |
372 | -import re |
373 | - |
374 | -from bzrlib import tests, osutils |
375 | - |
376 | -class TestGrep(tests.TestCaseWithTransport): |
377 | - def _str_contains(self, base, pattern, flags=re.MULTILINE|re.DOTALL): |
378 | - res = re.findall(pattern, base, flags) |
379 | - return res != [] |
380 | - |
381 | - def _mk_file(self, path, line_prefix, total_lines, versioned): |
382 | - text='' |
383 | - for i in range(total_lines): |
384 | - text += line_prefix + str(i+1) + "\n" |
385 | - |
386 | - open(path, 'w').write(text) |
387 | - if versioned: |
388 | - self.run_bzr(['add', path]) |
389 | - self.run_bzr(['ci', '-m', '"' + path + '"']) |
390 | - |
391 | - def _update_file(self, path, text): |
392 | - """append text to file 'path' and check it in""" |
393 | - open(path, 'a').write(text) |
394 | - self.run_bzr(['ci', '-m', '"' + path + '"']) |
395 | - |
396 | - def _mk_unknown_file(self, path, line_prefix='line', total_lines=10): |
397 | - self._mk_file(path, line_prefix, total_lines, versioned=False) |
398 | - |
399 | - def _mk_versioned_file(self, path, line_prefix='line', total_lines=10): |
400 | - self._mk_file(path, line_prefix, total_lines, versioned=True) |
401 | - |
402 | - def _mk_dir(self, path, versioned): |
403 | - os.mkdir(path) |
404 | - if versioned: |
405 | - self.run_bzr(['add', path]) |
406 | - self.run_bzr(['ci', '-m', '"' + path + '"']) |
407 | - |
408 | - def _mk_unknown_dir(self, path): |
409 | - self._mk_dir(path, versioned=False) |
410 | - |
411 | - def _mk_versioned_dir(self, path): |
412 | - self._mk_dir(path, versioned=True) |
413 | - |
414 | - def test_basic_unknown_file(self): |
415 | - """search for pattern in specfic file. should issue warning.""" |
416 | - wd = 'foobar0' |
417 | - self.make_branch_and_tree(wd) |
418 | - os.chdir(wd) |
419 | - self._mk_versioned_file('filex.txt') # force rev to revno:1 and not revno:0 |
420 | - self._mk_unknown_file('file0.txt') |
421 | - out, err = self.run_bzr(['grep', 'line1', 'file0.txt']) |
422 | - self.assertFalse(self._str_contains(out, "file0.txt:line1")) |
423 | - self.assertTrue(self._str_contains(err, "warning: skipped.*file0.txt.*\.")) |
424 | - |
425 | - def test_revno0(self): |
426 | - """search for pattern in when only revno0 is present""" |
427 | - wd = 'foobar0' |
428 | - self.make_branch_and_tree(wd) # only revno 0 in branch |
429 | - os.chdir(wd) |
430 | - out, err = self.run_bzr(['grep', 'line1'], retcode=3) |
431 | - self.assertTrue(self._str_contains(err, "ERROR: No revisions found")) |
432 | - |
433 | - def test_basic_versioned_file(self): |
434 | - """search for pattern in specfic file""" |
435 | - wd = 'foobar0' |
436 | - self.make_branch_and_tree(wd) |
437 | - os.chdir(wd) |
438 | - self._mk_versioned_file('file0.txt') |
439 | - out, err = self.run_bzr(['grep', 'line1', 'file0.txt']) |
440 | - self.assertTrue(self._str_contains(out, "file0.txt:line1")) |
441 | - self.assertFalse(self._str_contains(err, "warning: skipped.*file0.txt.*\.")) |
442 | - |
443 | - def test_multiple_files(self): |
444 | - """search for pattern in multiple files""" |
445 | - wd = 'foobar0' |
446 | - self.make_branch_and_tree(wd) |
447 | - os.chdir(wd) |
448 | - self._mk_versioned_file('file0.txt', total_lines=2) |
449 | - self._mk_versioned_file('file1.txt', total_lines=2) |
450 | - self._mk_versioned_file('file2.txt', total_lines=2) |
451 | - out, err = self.run_bzr(['grep', 'line[1-2]']) |
452 | - |
453 | - self.assertTrue(self._str_contains(out, "file0.txt:line1")) |
454 | - self.assertTrue(self._str_contains(out, "file0.txt:line2")) |
455 | - self.assertTrue(self._str_contains(out, "file1.txt:line1")) |
456 | - self.assertTrue(self._str_contains(out, "file1.txt:line2")) |
457 | - self.assertTrue(self._str_contains(out, "file2.txt:line1")) |
458 | - self.assertTrue(self._str_contains(out, "file2.txt:line2")) |
459 | - |
460 | - def test_null_option(self): |
461 | - """--null option should use NUL instead of newline""" |
462 | - wd = 'foobar0' |
463 | - self.make_branch_and_tree(wd) |
464 | - os.chdir(wd) |
465 | - self._mk_versioned_file('file0.txt', total_lines=3) |
466 | - |
467 | - out, err = self.run_bzr(['grep', '--null', 'line[1-3]']) |
468 | - self.assertTrue(out == "file0.txt:line1\0file0.txt:line2\0file0.txt:line3\0") |
469 | - |
470 | - out, err = self.run_bzr(['grep', '-Z', 'line[1-3]']) |
471 | - self.assertTrue(out == "file0.txt:line1\0file0.txt:line2\0file0.txt:line3\0") |
472 | - |
473 | - def test_versioned_file_in_dir_no_recurse(self): |
474 | - """should not recurse without -R""" |
475 | - wd = 'foobar0' |
476 | - self.make_branch_and_tree(wd) |
477 | - os.chdir(wd) |
478 | - self._mk_versioned_dir('dir0') |
479 | - self._mk_versioned_file('dir0/file0.txt') |
480 | - out, err = self.run_bzr(['grep', 'line1']) |
481 | - self.assertFalse(self._str_contains(out, "file0.txt:line1")) |
482 | - |
483 | - def test_versioned_file_in_dir_recurse(self): |
484 | - """should find pattern in hierarchy with -R""" |
485 | - wd = 'foobar0' |
486 | - self.make_branch_and_tree(wd) |
487 | - os.chdir(wd) |
488 | - self._mk_versioned_dir('dir0') |
489 | - self._mk_versioned_file('dir0/file0.txt') |
490 | - out, err = self.run_bzr(['grep', '-R', 'line1']) |
491 | - self.assertTrue(self._str_contains(out, "^dir0/file0.txt:line1")) |
492 | - out, err = self.run_bzr(['grep', '--recursive', 'line1']) |
493 | - self.assertTrue(self._str_contains(out, "^dir0/file0.txt:line1")) |
494 | - |
495 | - def test_versioned_file_within_dir(self): |
496 | - """search for pattern while in nested dir""" |
497 | - wd = 'foobar0' |
498 | - self.make_branch_and_tree(wd) |
499 | - os.chdir(wd) |
500 | - self._mk_versioned_dir('dir0') |
501 | - self._mk_versioned_file('dir0/file0.txt') |
502 | - os.chdir('dir0') |
503 | - out, err = self.run_bzr(['grep', 'line1']) |
504 | - self.assertTrue(self._str_contains(out, "^file0.txt:line1")) |
505 | - |
506 | - def test_versioned_files_from_outside_dir(self): |
507 | - """grep for pattern with dirs passed as argument""" |
508 | - wd = 'foobar0' |
509 | - self.make_branch_and_tree(wd) |
510 | - os.chdir(wd) |
511 | - |
512 | - self._mk_versioned_dir('dir0') |
513 | - self._mk_versioned_file('dir0/file0.txt') |
514 | - |
515 | - self._mk_versioned_dir('dir1') |
516 | - self._mk_versioned_file('dir1/file1.txt') |
517 | - |
518 | - out, err = self.run_bzr(['grep', 'line1', 'dir0', 'dir1']) |
519 | - self.assertTrue(self._str_contains(out, "^dir0/file0.txt:line1")) |
520 | - self.assertTrue(self._str_contains(out, "^dir1/file1.txt:line1")) |
521 | - |
522 | - def test_versioned_files_from_outside_dir(self): |
523 | - """grep for pattern with dirs passed as argument""" |
524 | - wd = 'foobar0' |
525 | - self.make_branch_and_tree(wd) |
526 | - os.chdir(wd) |
527 | - |
528 | - self._mk_versioned_dir('dir0') |
529 | - self._mk_versioned_file('dir0/file0.txt') |
530 | - |
531 | - self._mk_versioned_dir('dir1') |
532 | - self._mk_versioned_file('dir1/file1.txt') |
533 | - |
534 | - out, err = self.run_bzr(['grep', 'line1', 'dir0', 'dir1']) |
535 | - self.assertTrue(self._str_contains(out, "^dir0/file0.txt:line1")) |
536 | - self.assertTrue(self._str_contains(out, "^dir1/file1.txt:line1")) |
537 | - |
538 | - def test_versioned_files_from_outside_two_dirs(self): |
539 | - """grep for pattern with two levels of nested dir""" |
540 | - wd = 'foobar0' |
541 | - self.make_branch_and_tree(wd) |
542 | - os.chdir(wd) |
543 | - |
544 | - self._mk_versioned_dir('dir0') |
545 | - self._mk_versioned_file('dir0/file0.txt') |
546 | - |
547 | - self._mk_versioned_dir('dir1') |
548 | - self._mk_versioned_file('dir1/file1.txt') |
549 | - |
550 | - self._mk_versioned_dir('dir0/dir00') |
551 | - self._mk_versioned_file('dir0/dir00/file0.txt') |
552 | - |
553 | - out, err = self.run_bzr(['grep', 'line1', 'dir0/dir00']) |
554 | - self.assertTrue(self._str_contains(out, "^dir0/dir00/file0.txt:line1")) |
555 | - |
556 | - out, err = self.run_bzr(['grep', '-R', 'line1']) |
557 | - self.assertTrue(self._str_contains(out, "^dir0/dir00/file0.txt:line1")) |
558 | - |
559 | - def test_versioned_file_within_dir_two_levels(self): |
560 | - """search for pattern while in nested dir (two levels)""" |
561 | - wd = 'foobar0' |
562 | - self.make_branch_and_tree(wd) |
563 | - os.chdir(wd) |
564 | - self._mk_versioned_dir('dir0') |
565 | - self._mk_versioned_dir('dir0/dir1') |
566 | - self._mk_versioned_file('dir0/dir1/file0.txt') |
567 | - os.chdir('dir0') |
568 | - out, err = self.run_bzr(['grep', '-R', 'line1']) |
569 | - self.assertTrue(self._str_contains(out, "^dir1/file0.txt:line1")) |
570 | - out, err = self.run_bzr(['grep', '--from-root', 'line1']) |
571 | - self.assertTrue(self._str_contains(out, "^dir0/dir1/file0.txt:line1")) |
572 | - out, err = self.run_bzr(['grep', 'line1']) |
573 | - self.assertFalse(self._str_contains(out, "file0.txt")) |
574 | - |
575 | - def test_ignore_case_no_match(self): |
576 | - """match fails without --ignore-case""" |
577 | - wd = 'foobar0' |
578 | - self.make_branch_and_tree(wd) |
579 | - os.chdir(wd) |
580 | - self._mk_versioned_file('file0.txt') |
581 | - out, err = self.run_bzr(['grep', 'LinE1', 'file0.txt']) |
582 | - self.assertFalse(self._str_contains(out, "file0.txt:line1")) |
583 | - |
584 | - def test_ignore_case_match(self): |
585 | - """match fails without --ignore-case""" |
586 | - wd = 'foobar0' |
587 | - self.make_branch_and_tree(wd) |
588 | - os.chdir(wd) |
589 | - self._mk_versioned_file('file0.txt') |
590 | - out, err = self.run_bzr(['grep', '-i', 'LinE1', 'file0.txt']) |
591 | - self.assertTrue(self._str_contains(out, "file0.txt:line1")) |
592 | - out, err = self.run_bzr(['grep', '--ignore-case', 'LinE1', 'file0.txt']) |
593 | - self.assertTrue(self._str_contains(out, "^file0.txt:line1")) |
594 | - |
595 | - def test_from_root_fail(self): |
596 | - """match should fail without --from-root""" |
597 | - wd = 'foobar0' |
598 | - self.make_branch_and_tree(wd) |
599 | - os.chdir(wd) |
600 | - self._mk_versioned_file('file0.txt') |
601 | - self._mk_versioned_dir('dir0') |
602 | - os.chdir('dir0') |
603 | - out, err = self.run_bzr(['grep', 'line1']) |
604 | - self.assertFalse(self._str_contains(out, ".*file0.txt:line1")) |
605 | - |
606 | - def test_from_root_pass(self): |
607 | - """match pass with --from-root""" |
608 | - wd = 'foobar0' |
609 | - self.make_branch_and_tree(wd) |
610 | - os.chdir(wd) |
611 | - self._mk_versioned_file('file0.txt') |
612 | - self._mk_versioned_dir('dir0') |
613 | - os.chdir('dir0') |
614 | - out, err = self.run_bzr(['grep', '--from-root', 'line1']) |
615 | - self.assertTrue(self._str_contains(out, ".*file0.txt:line1")) |
616 | - |
617 | - def test_with_line_number(self): |
618 | - """search for pattern with --line-number""" |
619 | - wd = 'foobar0' |
620 | - self.make_branch_and_tree(wd) |
621 | - os.chdir(wd) |
622 | - self._mk_versioned_file('file0.txt') |
623 | - |
624 | - out, err = self.run_bzr(['grep', '--line-number', 'line3', 'file0.txt']) |
625 | - self.assertTrue(self._str_contains(out, "file0.txt:3:line3")) |
626 | - |
627 | - out, err = self.run_bzr(['grep', '-n', 'line1', 'file0.txt']) |
628 | - self.assertTrue(self._str_contains(out, "file0.txt:1:line1")) |
629 | - |
630 | - def test_revno_basic_history_grep_file(self): |
631 | - """search for pattern in specific revision number in a file""" |
632 | - wd = 'foobar0' |
633 | - fname = 'file0.txt' |
634 | - self.make_branch_and_tree(wd) |
635 | - os.chdir(wd) |
636 | - self._mk_versioned_file(fname, total_lines=0) |
637 | - self._update_file(fname, text="v2 text\n") |
638 | - self._update_file(fname, text="v3 text\n") |
639 | - self._update_file(fname, text="v4 text\n") |
640 | - |
641 | - # rev 2 should not have text 'v3' |
642 | - out, err = self.run_bzr(['grep', '-r', '2', 'v3', fname]) |
643 | - self.assertFalse(self._str_contains(out, "file0.txt")) |
644 | - |
645 | - # rev 3 should not have text 'v3' |
646 | - out, err = self.run_bzr(['grep', '-r', '3', 'v3', fname]) |
647 | - self.assertTrue(self._str_contains(out, "file0.txt~3:v3.*")) |
648 | - |
649 | - # rev 3 should not have text 'v3' with line number |
650 | - out, err = self.run_bzr(['grep', '-r', '3', '-n', 'v3', fname]) |
651 | - self.assertTrue(self._str_contains(out, "file0.txt~3:2:v3.*")) |
652 | - |
653 | - def test_revno_basic_history_grep_full(self): |
654 | - """search for pattern in specific revision number in a file""" |
655 | - wd = 'foobar0' |
656 | - fname = 'file0.txt' |
657 | - self.make_branch_and_tree(wd) |
658 | - os.chdir(wd) |
659 | - self._mk_versioned_file(fname, total_lines=0) # rev1 |
660 | - self._mk_versioned_file('file1.txt') # rev2 |
661 | - self._update_file(fname, text="v3 text\n") # rev3 |
662 | - self._update_file(fname, text="v4 text\n") # rev4 |
663 | - self._update_file(fname, text="v5 text\n") # rev5 |
664 | - |
665 | - # rev 2 should not have text 'v3' |
666 | - out, err = self.run_bzr(['grep', '-r', '2', 'v3']) |
667 | - self.assertFalse(self._str_contains(out, "file0.txt")) |
668 | - |
669 | - # rev 3 should not have text 'v3' |
670 | - out, err = self.run_bzr(['grep', '-r', '3', 'v3']) |
671 | - self.assertTrue(self._str_contains(out, "file0.txt~3:v3")) |
672 | - |
673 | - # rev 3 should not have text 'v3' with line number |
674 | - out, err = self.run_bzr(['grep', '-r', '3', '-n', 'v3']) |
675 | - self.assertTrue(self._str_contains(out, "file0.txt~3:1:v3")) |
676 | - |
677 | - def test_revno_versioned_file_in_dir(self): |
678 | - """we create a file 'foobar0/dir0/file0.txt' and grep specific version of content""" |
679 | - wd = 'foobar0' |
680 | - self.make_branch_and_tree(wd) |
681 | - os.chdir(wd) |
682 | - self._mk_versioned_dir('dir0') # rev1 |
683 | - self._mk_versioned_file('dir0/file0.txt') # rev2 |
684 | - self._update_file('dir0/file0.txt', "v3 text\n") # rev3 |
685 | - self._update_file('dir0/file0.txt', "v4 text\n") # rev4 |
686 | - self._update_file('dir0/file0.txt', "v5 text\n") # rev5 |
687 | - |
688 | - # v4 should not be present in revno 3 |
689 | - out, err = self.run_bzr(['grep', '-r', 'last:3', '-R', 'v4']) |
690 | - self.assertFalse(self._str_contains(out, "^dir0/file0.txt")) |
691 | - |
692 | - # v4 should be present in revno 4 |
693 | - out, err = self.run_bzr(['grep', '-r', 'last:2', '-R', 'v4']) |
694 | - self.assertTrue(self._str_contains(out, "^dir0/file0.txt~4:v4")) |
695 | - |
696 | - def test_revno_range_basic_history_grep(self): |
697 | - """search for pattern in revision range for file""" |
698 | - wd = 'foobar0' |
699 | - fname = 'file0.txt' |
700 | - self.make_branch_and_tree(wd) |
701 | - os.chdir(wd) |
702 | - self._mk_versioned_file(fname, total_lines=0) # rev1 |
703 | - self._mk_versioned_file('file1.txt') # rev2 |
704 | - self._update_file(fname, text="v3 text\n") # rev3 |
705 | - self._update_file(fname, text="v4 text\n") # rev4 |
706 | - self._update_file(fname, text="v5 text\n") # rev5 |
707 | - self._update_file(fname, text="v6 text\n") # rev6 |
708 | - |
709 | - out, err = self.run_bzr(['grep', '-r', '1..', 'v3']) |
710 | - self.assertTrue(self._str_contains(out, "file0.txt~3:v3")) |
711 | - self.assertTrue(self._str_contains(out, "file0.txt~4:v3")) |
712 | - self.assertTrue(self._str_contains(out, "file0.txt~5:v3")) |
713 | - |
714 | - out, err = self.run_bzr(['grep', '-r', '1..5', 'v3']) |
715 | - self.assertTrue(self._str_contains(out, "file0.txt~3:v3")) |
716 | - self.assertTrue(self._str_contains(out, "file0.txt~4:v3")) |
717 | - self.assertTrue(self._str_contains(out, "file0.txt~5:v3")) |
718 | - self.assertFalse(self._str_contains(out, "file0.txt~6:v3")) |
719 | - |
720 | - def test_revno_range_versioned_file_in_dir(self): |
721 | - """we create a file 'foobar0/dir0/file0.txt' and grep rev-range for pattern""" |
722 | - wd = 'foobar0' |
723 | - self.make_branch_and_tree(wd) |
724 | - os.chdir(wd) |
725 | - self._mk_versioned_dir('dir0') # rev1 |
726 | - self._mk_versioned_file('dir0/file0.txt') # rev2 |
727 | - self._update_file('dir0/file0.txt', "v3 text\n") # rev3 |
728 | - self._update_file('dir0/file0.txt', "v4 text\n") # rev4 |
729 | - self._update_file('dir0/file0.txt', "v5 text\n") # rev5 |
730 | - self._update_file('dir0/file0.txt', "v6 text\n") # rev6 |
731 | - |
732 | - out, err = self.run_bzr(['grep', '-R', '-r', '2..5', 'v3']) |
733 | - self.assertTrue(self._str_contains(out, "^dir0/file0.txt~3:v3")) |
734 | - self.assertTrue(self._str_contains(out, "^dir0/file0.txt~4:v3")) |
735 | - self.assertTrue(self._str_contains(out, "^dir0/file0.txt~5:v3")) |
736 | - self.assertFalse(self._str_contains(out, "^dir0/file0.txt~6:v3")) |
737 | - |
738 | - def test_revno_range_versioned_file_from_outside_dir(self): |
739 | - """grep rev-range for pattern from outside dir""" |
740 | - wd = 'foobar0' |
741 | - self.make_branch_and_tree(wd) |
742 | - os.chdir(wd) |
743 | - self._mk_versioned_dir('dir0') # rev1 |
744 | - self._mk_versioned_file('dir0/file0.txt') # rev2 |
745 | - self._update_file('dir0/file0.txt', "v3 text\n") # rev3 |
746 | - self._update_file('dir0/file0.txt', "v4 text\n") # rev4 |
747 | - self._update_file('dir0/file0.txt', "v5 text\n") # rev5 |
748 | - self._update_file('dir0/file0.txt', "v6 text\n") # rev6 |
749 | - |
750 | - out, err = self.run_bzr(['grep', '-r', '2..5', 'v3', 'dir0']) |
751 | - self.assertTrue(self._str_contains(out, "^dir0/file0.txt~3:v3")) |
752 | - self.assertTrue(self._str_contains(out, "^dir0/file0.txt~4:v3")) |
753 | - self.assertTrue(self._str_contains(out, "^dir0/file0.txt~5:v3")) |
754 | - self.assertFalse(self._str_contains(out, "^dir0/file0.txt~6:v3")) |
755 | - |
756 | - def test_levels(self): |
757 | - """levels=0 should show findings from merged revision""" |
758 | - wd0 = 'foobar0' |
759 | - wd1 = 'foobar1' |
760 | - |
761 | - self.make_branch_and_tree(wd0) |
762 | - os.chdir(wd0) |
763 | - self._mk_versioned_file('file0.txt') |
764 | - os.chdir('..') |
765 | - |
766 | - out, err = self.run_bzr(['branch', wd0, wd1]) |
767 | - os.chdir(wd1) |
768 | - self._mk_versioned_file('file1.txt') |
769 | - os.chdir(osutils.pathjoin('..', wd0)) |
770 | - |
771 | - out, err = self.run_bzr(['merge', osutils.pathjoin('..', wd1)]) |
772 | - out, err = self.run_bzr(['ci', '-m', 'merged']) |
773 | - |
774 | - out, err = self.run_bzr(['grep', 'line1']) |
775 | - self.assertTrue(self._str_contains(out, "file0.txt:line1")) |
776 | - self.assertTrue(self._str_contains(out, "file1.txt:line1")) |
777 | - |
778 | - out, err = self.run_bzr(['grep', '--levels=0', 'line1']) |
779 | - self.assertTrue(self._str_contains(out, "file0.txt~2:line1")) |
780 | - self.assertTrue(self._str_contains(out, "file1.txt~2:line1")) |
781 | - self.assertTrue(self._str_contains(out, "file0.txt~1.1.1:line1")) |
782 | - self.assertTrue(self._str_contains(out, "file1.txt~1.1.1:line1")) |
783 | - |
784 | - out, err = self.run_bzr(['grep', '-n', '--levels=0', 'line1']) |
785 | - self.assertTrue(self._str_contains(out, "file0.txt~2:1:line1")) |
786 | - self.assertTrue(self._str_contains(out, "file1.txt~2:1:line1")) |
787 | - self.assertTrue(self._str_contains(out, "file0.txt~1.1.1:1:line1")) |
788 | - self.assertTrue(self._str_contains(out, "file1.txt~1.1.1:1:line1")) |
789 | - |
790 | - def test_binary_file_grep(self): |
791 | - """grep for pattern in binary file""" |
792 | - wd = 'foobar0' |
793 | - self.make_branch_and_tree(wd) |
794 | - os.chdir(wd) |
795 | - self._mk_versioned_file('file0.txt') |
796 | - self._update_file('file0.txt', "\x00lineNN\x00\n") |
797 | - out, err = self.run_bzr(['grep', 'lineNN', 'file0.txt']) |
798 | - self.assertFalse(self._str_contains(out, "file0.txt:line1")) |
799 | - self.assertTrue(self._str_contains(err, "Binary file.*file0.txt.*skipped")) |
800 | - |
801 | - |
I'm not sure what you're asking for exactly - a review of bzr-grep in its entirety?