~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to grep.py

  • Committer: Parth Malwankar
  • Date: 2010-05-22 15:11:00 UTC
  • mto: (0.44.2 grep) (6531.3.1 merge-grep)
  • mto: This revision was merged to the branch mainline in revision 6555.
  • Revision ID: parth.malwankar@gmail.com-20100522151100-y9198n1m20zxs8yn
bzr grep now allows grepping with -r even when no tree exists.

Show diffs side-by-side

added added

removed removed

Lines of Context:
129
129
 
130
130
 
131
131
def versioned_grep(opts):
132
 
    wt, relpath = WorkingTree.open_containing('.')
133
 
    wt.lock_read()
 
132
    wt, branch, relpath = \
 
133
        bzrdir.BzrDir.open_containing_tree_or_branch('.')
 
134
    branch.lock_read()
134
135
    try:
135
136
        # res_cache is used to cache results for dir grep based on fid.
136
137
        # If the fid is does not change between results, it means that
140
141
        res_cache = {}
141
142
 
142
143
        start_rev = opts.revision[0]
143
 
        start_revid = start_rev.as_revision_id(wt.branch)
 
144
        start_revid = start_rev.as_revision_id(branch)
144
145
        if start_revid == None:
145
146
            start_rev = RevisionSpec_revno.from_string("revno:1")
146
 
            start_revid = start_rev.as_revision_id(wt.branch)
147
 
        srevno_tuple = wt.branch.revision_id_to_dotted_revno(start_revid)
 
147
            start_revid = start_rev.as_revision_id(branch)
 
148
        srevno_tuple = branch.revision_id_to_dotted_revno(start_revid)
148
149
 
149
150
        if len(opts.revision) == 2:
150
151
            end_rev = opts.revision[1]
151
 
            end_revid = end_rev.as_revision_id(wt.branch)
 
152
            end_revid = end_rev.as_revision_id(branch)
152
153
            if end_revid == None:
153
 
                end_revno, end_revid = wt.branch.last_revision_info()
154
 
            erevno_tuple = wt.branch.revision_id_to_dotted_revno(end_revid)
 
154
                end_revno, end_revid = branch.last_revision_info()
 
155
            erevno_tuple = branch.revision_id_to_dotted_revno(end_revid)
155
156
 
156
157
            grep_mainline = (_rev_on_mainline(srevno_tuple) and
157
158
                _rev_on_mainline(erevno_tuple))
166
167
            # with _linear_view_revisions. If all revs are to be grepped we
167
168
            # use the slower _graph_view_revisions
168
169
            if opts.levels==1 and grep_mainline:
169
 
                given_revs = _linear_view_revisions(wt.branch, start_revid, end_revid)
 
170
                given_revs = _linear_view_revisions(branch, start_revid, end_revid)
170
171
            else:
171
 
                given_revs = _graph_view_revisions(wt.branch, start_revid, end_revid)
 
172
                given_revs = _graph_view_revisions(branch, start_revid, end_revid)
172
173
        else:
173
174
            # We do an optimization below. For grepping a specific revison
174
175
            # We don't need to call _graph_view_revisions which is slow.
184
185
                continue
185
186
 
186
187
            rev = RevisionSpec_revid.from_string("revid:"+revid)
187
 
            tree = rev.as_tree(wt.branch)
 
188
            tree = rev.as_tree(branch)
188
189
            for path in opts.path_list:
189
190
                path_for_id = osutils.pathjoin(relpath, path)
190
191
                id = tree.path2id(path_for_id)
199
200
                else:
200
201
                    versioned_file_grep(tree, id, '.', path, opts, revno)
201
202
    finally:
202
 
        wt.unlock()
 
203
        branch.unlock()
203
204
 
204
205
 
205
206
def workingtree_grep(opts):
208
209
    tree, branch, relpath = \
209
210
        bzrdir.BzrDir.open_containing_tree_or_branch('.')
210
211
    if not tree:
211
 
        msg = 'Cannot search for pattern. Working tree not found.'
 
212
        msg = ('Cannot search working tree. Working tree not found.\n'
 
213
            'To search for specific revision in history use the -r option.')
212
214
        raise errors.BzrCommandError(msg)
213
215
 
214
216
    tree.lock_read()