43
44
takes_args = ['pattern', 'path*']
46
Option('line-number', short_name='n',
47
help='prefix each line of output with 1-based line number.'),
48
47
Option('ignore-case', short_name='i',
49
48
help='ignore case distinctions while matching.'),
50
49
Option('recursive', short_name='R',
51
50
help='Recurse into subdirectories.'),
52
51
Option('from-root',
53
52
help='Search for pattern starting from the root of the branch.'),
53
Option('null', short_name='z',
54
help='Write an ascii NUL (\\0) separator '
55
'between output lines rather than a newline.'),
58
def run(self, verbose=False, line_number=False, null=False,
59
ignore_case=False, recursive=False, from_root=False,
60
path_list=None, pattern=None):
60
def run(self, verbose=False, ignore_case=False, recursive=False, from_root=False,
61
null=False, path_list=None, pattern=None):
61
62
if path_list == None:
65
66
raise errors.BzrCommandError('cannot specify both --from-root and PATH.')
67
print 'pattern:', pattern
68
print 'path_list:', path_list
69
print 'line-number:', line_number
71
print 'recursive:', recursive
72
print 'from-root:', from_root
75
68
tree, branch, relpath = bzrdir.BzrDir.open_containing_tree_or_branch('.')
79
72
re_flags = re.IGNORECASE
84
80
# use python's re.compile as we need to catch re.error in case of bad pattern
85
81
lazy_regex.reset_compile()
91
87
self.add_cleanup(tree.unlock)
92
88
for path in path_list:
93
for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
94
from_dir=relpath, recursive=recursive):
100
if fc == 'V' and fkind == 'file':
101
self.file_grep(fp, patternc)
89
if osutils.isdir(path):
90
from_dir = os.path.abspath(os.path.join(relpath, path))
91
for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
92
from_dir=from_dir, recursive=recursive):
93
if fc == 'V' and fkind == 'file':
94
self.file_grep(fp, patternc, eol_marker)
96
# if user has explicitly specified a file
97
# we don't care if its versioned
98
self.file_grep(path, patternc, eol_marker)
104
def file_grep(self, path, patternc):
100
def file_grep(self, path, patternc, eol_marker):
102
fmt = "%s:%d:%s" + eol_marker
106
103
for line in open(path):
107
104
res = patternc.search(line)
109
self.outf.write("%s:%d:%s\n" % (path, index, line.strip()))
106
self.outf.write( fmt % (path, index, line.strip()))
112
109
register_command(cmd_grep)