/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.40.10 by Parth Malwankar
assigned copyright to canonical
1
# Copyright (C) 2010 Canonical Ltd
0.40.1 by Parth Malwankar
initial skeleton
2
# Copyright (C) 2010 Parth Malwankar <parth.malwankar@gmail.com>
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
"""bzr grep"""
18
19
import os
0.40.3 by Parth Malwankar
basic file grep is working
20
import sys
0.40.1 by Parth Malwankar
initial skeleton
21
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
22
from bzrlib import errors
0.40.2 by Parth Malwankar
initial framework for grep
23
from bzrlib.commands import Command, register_command, display_command
24
from bzrlib.option import (
25
    Option,
26
    )
27
28
from bzrlib.lazy_import import lazy_import
29
lazy_import(globals(), """
0.40.3 by Parth Malwankar
basic file grep is working
30
import re
31
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
32
import grep
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
33
0.40.2 by Parth Malwankar
initial framework for grep
34
import bzrlib
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
35
from bzrlib.builtins import _get_revision_range, _parse_levels
0.40.37 by Parth Malwankar
grep now accepts rev range
36
from bzrlib.revisionspec import RevisionSpec, RevisionSpec_revid
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
37
from bzrlib.workingtree import WorkingTree
0.40.37 by Parth Malwankar
grep now accepts rev range
38
from bzrlib import log as logcmd
0.40.2 by Parth Malwankar
initial framework for grep
39
from bzrlib import (
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
40
    osutils,
0.40.2 by Parth Malwankar
initial framework for grep
41
    bzrdir,
0.40.7 by Parth Malwankar
issue a warning while searching unversioned file
42
    trace,
0.40.2 by Parth Malwankar
initial framework for grep
43
    )
44
""")
0.40.1 by Parth Malwankar
initial skeleton
45
46
version_info = (0, 1)
47
48
class cmd_grep(Command):
0.40.36 by Parth Malwankar
improved docs and minor code cleanup
49
    """Print lines matching PATTERN for specified files and revisions.
50
51
    This command searches the specified files and revisions for a given pattern.
52
    The pattern is specified as a Python regular expressions[1].
53
    If the file name is not specified the file revisions in the current directory
54
    are searched. If the revision number is not specified, the latest revision is
55
    searched.
56
57
    Note that this command is different from POSIX grep in that it searches the
58
    revisions of the branch and not the working copy. Unversioned files and
59
    uncommitted changes are not seen.
60
61
    When searching a pattern, the output is shown in the 'filepath:string' format.
62
    If a revision is explicitly searched, the output is shown as 'filepath~N:string',
63
    where N is the revision number.
64
65
    [1] http://docs.python.org/library/re.html#regular-expression-syntax
0.40.2 by Parth Malwankar
initial framework for grep
66
    """
67
68
    takes_args = ['pattern', 'path*']
69
    takes_options = [
70
        'verbose',
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
71
        'revision',
0.40.24 by Parth Malwankar
added support for --line-number.
72
        Option('line-number', short_name='n',
73
               help='show 1-based line number.'),
0.40.2 by Parth Malwankar
initial framework for grep
74
        Option('ignore-case', short_name='i',
75
               help='ignore case distinctions while matching.'),
76
        Option('recursive', short_name='R',
77
               help='Recurse into subdirectories.'),
78
        Option('from-root',
0.40.18 by Parth Malwankar
improved help string
79
               help='Search for pattern starting from the root of the branch. '
80
               '(implies --recursive)'),
0.40.19 by Parth Malwankar
null option should be -Z instead of -z
81
        Option('null', short_name='Z',
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
82
               help='Write an ascii NUL (\\0) separator '
83
               'between output lines rather than a newline.'),
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
84
        Option('levels',
85
           help='Number of levels to display - 0 for all, 1 for collapsed (default).',
86
           argname='N',
87
           type=_parse_levels),
0.40.2 by Parth Malwankar
initial framework for grep
88
        ]
89
90
91
    @display_command
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
92
    def run(self, verbose=False, ignore_case=False, recursive=False, from_root=False,
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
93
            null=False, levels=None, line_number=False, path_list=None, revision=None, pattern=None):
94
95
        if levels==None:
96
            levels=1
97
0.40.2 by Parth Malwankar
initial framework for grep
98
        if path_list == None:
99
            path_list = ['.']
100
        else:
101
            if from_root:
102
                raise errors.BzrCommandError('cannot specify both --from-root and PATH.')
103
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
104
        print_revno = False
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
105
        if revision == None:
0.40.36 by Parth Malwankar
improved docs and minor code cleanup
106
            # grep on latest revision by default
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
107
            revision = [RevisionSpec.from_string("last:1")]
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
108
        else:
109
            print_revno = True # used to print revno in output.
110
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
111
        start_rev = revision[0]
0.40.37 by Parth Malwankar
grep now accepts rev range
112
        end_rev = revision[0]
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
113
        if len(revision) == 2:
114
            end_rev = revision[1]
115
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
116
        eol_marker = '\n'
117
        if null:
118
            eol_marker = '\0'
119
0.40.3 by Parth Malwankar
basic file grep is working
120
        re_flags = 0
121
        if ignore_case:
122
            re_flags = re.IGNORECASE
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
123
        patternc = grep.compile_pattern(pattern, re_flags)
0.40.3 by Parth Malwankar
basic file grep is working
124
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
125
        wt, relpath = WorkingTree.open_containing('.')
0.40.37 by Parth Malwankar
grep now accepts rev range
126
127
        start_revid = start_rev.as_revision_id(wt.branch)
128
        end_revid   = end_rev.as_revision_id(wt.branch)
129
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
130
        id_to_revno = wt.branch.get_revision_id_to_revno_map()
0.40.37 by Parth Malwankar
grep now accepts rev range
131
        given_revs = logcmd._graph_view_revisions(wt.branch, start_revid, end_revid)
132
133
        # edge case: we have a repo created with 'bzr init' and it has no
134
        # revisions (revno: 0)
0.40.32 by Parth Malwankar
used try - finally instead of add_cleanup to get plugin to work with 2.0
135
        try:
0.40.37 by Parth Malwankar
grep now accepts rev range
136
            given_revs = list(given_revs)
137
        except erros.NoSuchRevision, e:
138
            raise errors.BzrCommandError('no revisions found available to grep')
0.40.32 by Parth Malwankar
used try - finally instead of add_cleanup to get plugin to work with 2.0
139
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
140
        for revid, revno, merge_depth in given_revs:
141
            if levels == 1 and merge_depth != 0:
142
                # with level=1 show only top level
143
                continue
144
0.40.37 by Parth Malwankar
grep now accepts rev range
145
            wt.lock_read()
146
            rev = RevisionSpec_revid.from_string("revid:"+revid)
147
            try:
148
                for path in path_list:
149
                    tree = rev.as_tree(wt.branch)
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
150
151
                    path_for_id = path
152
                    if osutils.isdir(path):
153
                        # tweak path to get valid id
154
                        path_for_id = osutils.pathjoin(relpath, path)
155
156
                    id = tree.path2id(path_for_id)
0.40.32 by Parth Malwankar
used try - finally instead of add_cleanup to get plugin to work with 2.0
157
                    if not id:
0.40.33 by Parth Malwankar
better message while skipping unversioned file
158
                        self._skip_file(path)
0.40.32 by Parth Malwankar
used try - finally instead of add_cleanup to get plugin to work with 2.0
159
                        continue
0.40.37 by Parth Malwankar
grep now accepts rev range
160
161
                    if osutils.isdir(path):
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
162
                        self._grep_dir(tree, path, relpath, recursive, line_number,
0.40.37 by Parth Malwankar
grep now accepts rev range
163
                            patternc, from_root, eol_marker, revno, print_revno)
164
                    else:
165
                        tree.lock_read()
166
                        try:
167
                            grep.file_grep(tree, id, '.', path, patternc, eol_marker,
168
                                self.outf, line_number, revno, print_revno)
169
                        finally:
170
                            tree.unlock()
171
            finally:
172
                wt.unlock()
0.40.1 by Parth Malwankar
initial skeleton
173
0.40.33 by Parth Malwankar
better message while skipping unversioned file
174
    def _skip_file(self, path):
0.40.37 by Parth Malwankar
grep now accepts rev range
175
        trace.warning("warning: skipped unknown file '%s'." % path)
0.40.33 by Parth Malwankar
better message while skipping unversioned file
176
0.40.36 by Parth Malwankar
improved docs and minor code cleanup
177
    def _revno_str(self, id_to_revno_dict, revid):
178
        revno = ".".join([str(n) for n in id_to_revno_dict[revid]])
179
        return revno
180
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
181
    def _grep_dir(self, tree, path, relpath, recursive, line_number, compiled_pattern,
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
182
        from_root, eol_marker, revno, print_revno):
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
183
            # setup relpath to open files relative to cwd
184
            rpath = relpath
185
            if relpath:
186
                rpath = osutils.pathjoin('..',relpath)
187
188
            tree.lock_read()
189
            try:
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
190
                from_dir = osutils.pathjoin(relpath, path)
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
191
                if from_root:
192
                    # start searching recursively from root
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
193
                    from_dir=None
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
194
                    recursive=True
195
196
                for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
197
                    from_dir=from_dir, recursive=recursive):
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
198
                    if fc == 'V' and fkind == 'file':
199
                        grep.file_grep(tree, fid, rpath, fp, compiled_pattern,
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
200
                            eol_marker, self.outf, line_number, revno, print_revno)
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
201
            finally:
202
                tree.unlock()
203
204
0.40.1 by Parth Malwankar
initial skeleton
205
register_command(cmd_grep)
0.40.2 by Parth Malwankar
initial framework for grep
206
0.40.11 by Parth Malwankar
added basic test
207
def test_suite():
208
    from bzrlib.tests import TestUtil
209
210
    suite = TestUtil.TestSuite()
211
    loader = TestUtil.TestLoader()
212
    testmod_names = [
213
        'test_grep',
214
        ]
215
216
    suite.addTest(loader.loadTestsFromModuleNames(
217
            ["%s.%s" % (__name__, tmn) for tmn in testmod_names]))
218
    return suite
219