/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
130
        given_revs = logcmd._graph_view_revisions(wt.branch, start_revid, end_revid)
131
132
        # edge case: we have a repo created with 'bzr init' and it has no
133
        # revisions (revno: 0)
0.40.32 by Parth Malwankar
used try - finally instead of add_cleanup to get plugin to work with 2.0
134
        try:
0.40.37 by Parth Malwankar
grep now accepts rev range
135
            given_revs = list(given_revs)
136
        except erros.NoSuchRevision, e:
137
            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
138
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
139
        for revid, revno, merge_depth in given_revs:
140
            if levels == 1 and merge_depth != 0:
141
                # with level=1 show only top level
142
                continue
143
0.40.37 by Parth Malwankar
grep now accepts rev range
144
            wt.lock_read()
145
            rev = RevisionSpec_revid.from_string("revid:"+revid)
146
            try:
147
                for path in path_list:
148
                    tree = rev.as_tree(wt.branch)
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
149
150
                    path_for_id = path
151
                    if osutils.isdir(path):
152
                        # tweak path to get valid id
153
                        path_for_id = osutils.pathjoin(relpath, path)
154
155
                    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
156
                    if not id:
0.40.33 by Parth Malwankar
better message while skipping unversioned file
157
                        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
158
                        continue
0.40.37 by Parth Malwankar
grep now accepts rev range
159
160
                    if osutils.isdir(path):
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
161
                        self._grep_dir(tree, path, relpath, recursive, line_number,
0.40.37 by Parth Malwankar
grep now accepts rev range
162
                            patternc, from_root, eol_marker, revno, print_revno)
163
                    else:
164
                        tree.lock_read()
165
                        try:
166
                            grep.file_grep(tree, id, '.', path, patternc, eol_marker,
167
                                self.outf, line_number, revno, print_revno)
168
                        finally:
169
                            tree.unlock()
170
            finally:
171
                wt.unlock()
0.40.1 by Parth Malwankar
initial skeleton
172
0.40.33 by Parth Malwankar
better message while skipping unversioned file
173
    def _skip_file(self, path):
0.40.37 by Parth Malwankar
grep now accepts rev range
174
        trace.warning("warning: skipped unknown file '%s'." % path)
0.40.33 by Parth Malwankar
better message while skipping unversioned file
175
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
176
    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
177
        from_root, eol_marker, revno, print_revno):
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
178
            # setup relpath to open files relative to cwd
179
            rpath = relpath
180
            if relpath:
181
                rpath = osutils.pathjoin('..',relpath)
182
183
            tree.lock_read()
184
            try:
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
185
                from_dir = osutils.pathjoin(relpath, path)
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
186
                if from_root:
187
                    # start searching recursively from root
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
188
                    from_dir=None
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
189
                    recursive=True
190
191
                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.
192
                    from_dir=from_dir, recursive=recursive):
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
193
                    if fc == 'V' and fkind == 'file':
194
                        grep.file_grep(tree, fid, rpath, fp, compiled_pattern,
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
195
                            eol_marker, self.outf, line_number, revno, print_revno)
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
196
            finally:
197
                tree.unlock()
198
199
0.40.1 by Parth Malwankar
initial skeleton
200
register_command(cmd_grep)
0.40.2 by Parth Malwankar
initial framework for grep
201
0.40.11 by Parth Malwankar
added basic test
202
def test_suite():
203
    from bzrlib.tests import TestUtil
204
205
    suite = TestUtil.TestSuite()
206
    loader = TestUtil.TestLoader()
207
    testmod_names = [
208
        'test_grep',
209
        ]
210
211
    suite.addTest(loader.loadTestsFromModuleNames(
212
            ["%s.%s" % (__name__, tmn) for tmn in testmod_names]))
213
    return suite
214