1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
# Copyright (C) 2010 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from bzrlib.lazy_import import lazy_import
lazy_import(globals(), """
import os
import re
import cStringIO
from fnmatch import fnmatch
from bzrlib import log as logcmd
from bzrlib import bzrdir
from bzrlib.workingtree import WorkingTree
from bzrlib.revisionspec import RevisionSpec, RevisionSpec_revid
from bzrlib import (
errors,
lazy_regex,
osutils,
textfile,
trace,
)
""")
def compile_pattern(pattern, flags=0):
patternc = None
try:
# use python's re.compile as we need to catch re.error in case of bad pattern
lazy_regex.reset_compile()
patternc = re.compile(pattern, flags)
except re.error, e:
raise errors.BzrError("Invalid pattern: '%s'" % pattern)
return patternc
def versioned_grep(revision, compiled_pattern, path_list, recursive,
line_number, from_root, eol_marker, print_revno, levels,
include, exclude, outf):
wt, relpath = WorkingTree.open_containing('.')
start_rev = revision[0]
end_rev = revision[0]
if len(revision) == 2:
end_rev = revision[1]
start_revid = start_rev.as_revision_id(wt.branch)
end_revid = end_rev.as_revision_id(wt.branch)
given_revs = logcmd._graph_view_revisions(wt.branch, start_revid, end_revid)
given_revs = list(given_revs)
for revid, revno, merge_depth in given_revs:
if levels == 1 and merge_depth != 0:
# with level=1 show only top level
continue
wt.lock_read()
rev = RevisionSpec_revid.from_string("revid:"+revid)
try:
for path in path_list:
tree = rev.as_tree(wt.branch)
path_for_id = osutils.pathjoin(relpath, path)
id = tree.path2id(path_for_id)
if not id:
trace.warning("Skipped unknown file '%s'." % path)
continue
if osutils.isdir(path):
path_prefix = path
dir_grep(tree, path, relpath, recursive, line_number,
compiled_pattern, from_root, eol_marker, revno, print_revno,
include, exclude, outf, path_prefix)
else:
tree.lock_read()
try:
versioned_file_grep(tree, id, '.', path,
compiled_pattern, eol_marker, line_number, revno,
print_revno, include, exclude, outf)
finally:
tree.unlock()
finally:
wt.unlock()
def workingtree_grep(compiled_pattern, path_list, recursive,
line_number, from_root, eol_marker, include, exclude, outf):
revno = print_revno = None # for working tree set revno to None
for path in path_list:
tree, branch, relpath = \
bzrdir.BzrDir.open_containing_tree_or_branch('.')
if osutils.isdir(path):
path_prefix = path
dir_grep(tree, path, relpath, recursive, line_number,
compiled_pattern, from_root, eol_marker, revno, print_revno,
include, exclude, outf, path_prefix)
else:
_file_grep(open(path).read(), '.', path, compiled_pattern, eol_marker,
line_number, revno, print_revno, include, exclude, outf)
def dir_grep(tree, path, relpath, recursive, line_number, compiled_pattern,
from_root, eol_marker, revno, print_revno, include, exclude, outf, path_prefix):
# setup relpath to open files relative to cwd
rpath = relpath
if relpath:
rpath = osutils.pathjoin('..',relpath)
from_dir = osutils.pathjoin(relpath, path)
if from_root:
# start searching recursively from root
from_dir=None
recursive=True
tree.lock_read()
try:
for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
from_dir=from_dir, recursive=recursive):
if fc == 'V' and fkind == 'file':
if revno != None:
versioned_file_grep(tree, fid, rpath, fp,
compiled_pattern, eol_marker, line_number,
revno, print_revno, include, exclude, outf, path_prefix)
else:
# we are grepping working tree.
if from_dir == None:
from_dir = '.'
path_for_file = osutils.pathjoin(tree.basedir, from_dir, fp)
_file_grep(open(path_for_file).read(), rpath, fp,
compiled_pattern, eol_marker, line_number, revno,
print_revno, include, exclude, outf, path_prefix)
finally:
tree.unlock()
def _make_display_path(relpath, path):
"""Return path string relative to user cwd.
Take tree's 'relpath' and user supplied 'path', and return path
that can be displayed to the user.
"""
if relpath:
# update path so to display it w.r.t cwd
# handle windows slash separator
path = osutils.normpath(osutils.pathjoin(relpath, path))
path = path.replace('\\', '/')
path = path.replace(relpath + '/', '', 1)
return path
def versioned_file_grep(tree, id, relpath, path, patternc, eol_marker,
line_number, revno, print_revno, include, exclude, outf,
path_prefix = None):
"""Create a file object for the specified id and pass it on to _file_grep.
"""
path = _make_display_path(relpath, path)
file_text = tree.get_file_text(id)
_file_grep(file_text, relpath, path, patternc, eol_marker,
line_number, revno, print_revno, include, exclude, outf, path_prefix)
def _path_in_glob_list(path, glob_list):
present = False
for glob in glob_list:
if fnmatch(path, glob):
present = True
break
return present
def _file_grep(file_text, relpath, path, patternc, eol_marker,
line_number, revno, print_revno, include, exclude, outf,
path_prefix=None):
if include and not _path_in_glob_list(path, include):
return
if exclude and _path_in_glob_list(path, exclude):
return
# test and skip binary files
str_file = cStringIO.StringIO(file_text)
try:
file_iter = textfile.text_file(str_file)
except errors.BinaryFile, e:
trace.warning("Binary file '%s' skipped." % path)
return
if path_prefix and path_prefix != '.':
# user has passed a dir arg, show that as result prefix
path = osutils.pathjoin(path_prefix, path)
revfmt = ''
if print_revno:
revfmt = "~%s"
fmt_with_n = path + revfmt + ":%d:%s" + eol_marker
fmt_without_n = path + revfmt + ":%s" + eol_marker
# grep through iterable file object and print out the lines
# matching the compiled pattern in the specified format.
index = 1
for line in file_iter:
res = patternc.search(line)
if res:
if line_number:
if print_revno:
out = (revno, index, line.strip())
else:
out = (index, line.strip())
outf.write(fmt_with_n % out)
else:
if print_revno:
out = (revno, line.strip())
else:
out = (line.strip(),)
outf.write(fmt_without_n % out)
index += 1
|