/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.46 by Parth Malwankar
--levels=0 now implicitly prints revnos. added test for --levels=0.
35
from bzrlib.builtins import _get_revision_range
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
0.40.46 by Parth Malwankar
--levels=0 now implicitly prints revnos. added test for --levels=0.
48
# FIXME: _parse_levels should be shared with bzrlib.builtins. this is a copy
49
# to avoid the error
50
#   "IllegalUseOfScopeReplacer: ScopeReplacer object '_parse_levels' was used
51
#   incorrectly: Object already cleaned up, did you assign it to another
52
#   variable?: _factory
53
# with lazy import
54
def _parse_levels(s):
55
    try:
56
        return int(s)
57
    except ValueError:
58
        msg = "The levels argument must be an integer."
59
        raise errors.BzrCommandError(msg)
60
61
0.40.1 by Parth Malwankar
initial skeleton
62
class cmd_grep(Command):
0.40.36 by Parth Malwankar
improved docs and minor code cleanup
63
    """Print lines matching PATTERN for specified files and revisions.
64
65
    This command searches the specified files and revisions for a given pattern.
66
    The pattern is specified as a Python regular expressions[1].
67
    If the file name is not specified the file revisions in the current directory
68
    are searched. If the revision number is not specified, the latest revision is
69
    searched.
70
71
    Note that this command is different from POSIX grep in that it searches the
72
    revisions of the branch and not the working copy. Unversioned files and
73
    uncommitted changes are not seen.
74
75
    When searching a pattern, the output is shown in the 'filepath:string' format.
76
    If a revision is explicitly searched, the output is shown as 'filepath~N:string',
77
    where N is the revision number.
78
79
    [1] http://docs.python.org/library/re.html#regular-expression-syntax
0.40.2 by Parth Malwankar
initial framework for grep
80
    """
81
82
    takes_args = ['pattern', 'path*']
83
    takes_options = [
84
        'verbose',
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
85
        'revision',
0.40.24 by Parth Malwankar
added support for --line-number.
86
        Option('line-number', short_name='n',
87
               help='show 1-based line number.'),
0.40.2 by Parth Malwankar
initial framework for grep
88
        Option('ignore-case', short_name='i',
89
               help='ignore case distinctions while matching.'),
90
        Option('recursive', short_name='R',
91
               help='Recurse into subdirectories.'),
92
        Option('from-root',
0.40.18 by Parth Malwankar
improved help string
93
               help='Search for pattern starting from the root of the branch. '
94
               '(implies --recursive)'),
0.40.19 by Parth Malwankar
null option should be -Z instead of -z
95
        Option('null', short_name='Z',
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
96
               help='Write an ascii NUL (\\0) separator '
97
               'between output lines rather than a newline.'),
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
98
        Option('levels',
99
           help='Number of levels to display - 0 for all, 1 for collapsed (default).',
100
           argname='N',
101
           type=_parse_levels),
0.40.2 by Parth Malwankar
initial framework for grep
102
        ]
103
104
105
    @display_command
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
106
    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.
107
            null=False, levels=None, line_number=False, path_list=None, revision=None, pattern=None):
108
109
        if levels==None:
110
            levels=1
111
0.40.2 by Parth Malwankar
initial framework for grep
112
        if path_list == None:
113
            path_list = ['.']
114
        else:
115
            if from_root:
116
                raise errors.BzrCommandError('cannot specify both --from-root and PATH.')
117
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
118
        print_revno = False
0.40.46 by Parth Malwankar
--levels=0 now implicitly prints revnos. added test for --levels=0.
119
        if revision != None or levels == 0:
120
            # print revision numbers as we may be showing multiple revisions
121
            print_revno = True
122
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
123
        if revision == None:
0.40.36 by Parth Malwankar
improved docs and minor code cleanup
124
            # grep on latest revision by default
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
125
            revision = [RevisionSpec.from_string("last:1")]
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
126
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
127
        start_rev = revision[0]
0.40.37 by Parth Malwankar
grep now accepts rev range
128
        end_rev = revision[0]
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
129
        if len(revision) == 2:
130
            end_rev = revision[1]
131
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
132
        eol_marker = '\n'
133
        if null:
134
            eol_marker = '\0'
135
0.40.3 by Parth Malwankar
basic file grep is working
136
        re_flags = 0
137
        if ignore_case:
138
            re_flags = re.IGNORECASE
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
139
        patternc = grep.compile_pattern(pattern, re_flags)
0.40.3 by Parth Malwankar
basic file grep is working
140
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
141
        wt, relpath = WorkingTree.open_containing('.')
0.40.37 by Parth Malwankar
grep now accepts rev range
142
143
        start_revid = start_rev.as_revision_id(wt.branch)
144
        end_revid   = end_rev.as_revision_id(wt.branch)
145
146
        given_revs = logcmd._graph_view_revisions(wt.branch, start_revid, end_revid)
147
148
        # edge case: we have a repo created with 'bzr init' and it has no
149
        # revisions (revno: 0)
0.40.32 by Parth Malwankar
used try - finally instead of add_cleanup to get plugin to work with 2.0
150
        try:
0.40.37 by Parth Malwankar
grep now accepts rev range
151
            given_revs = list(given_revs)
0.40.45 by Parth Malwankar
fixed case where only revno:0 is present. added test.
152
        except errors.NoSuchRevision, e:
153
            raise errors.BzrCommandError('No revisions found for grep.')
0.40.32 by Parth Malwankar
used try - finally instead of add_cleanup to get plugin to work with 2.0
154
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
155
        for revid, revno, merge_depth in given_revs:
156
            if levels == 1 and merge_depth != 0:
157
                # with level=1 show only top level
158
                continue
159
0.40.37 by Parth Malwankar
grep now accepts rev range
160
            wt.lock_read()
161
            rev = RevisionSpec_revid.from_string("revid:"+revid)
162
            try:
163
                for path in path_list:
164
                    tree = rev.as_tree(wt.branch)
0.40.42 by Parth Malwankar
fix to make grep paths relative to cwd
165
                    path_for_id = osutils.pathjoin(relpath, path)
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
166
                    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
167
                    if not id:
0.40.33 by Parth Malwankar
better message while skipping unversioned file
168
                        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
169
                        continue
0.40.37 by Parth Malwankar
grep now accepts rev range
170
171
                    if osutils.isdir(path):
0.40.44 by Parth Malwankar
improved display of path when dir is given as argument
172
                        path_prefix = path
0.40.43 by Parth Malwankar
moved cmd_grep._grep_dir to grep.dir_grep
173
                        grep.dir_grep(tree, path, relpath, recursive, line_number,
0.40.44 by Parth Malwankar
improved display of path when dir is given as argument
174
                            patternc, from_root, eol_marker, revno, print_revno,
175
                            self.outf, path_prefix)
0.40.37 by Parth Malwankar
grep now accepts rev range
176
                    else:
177
                        tree.lock_read()
178
                        try:
179
                            grep.file_grep(tree, id, '.', path, patternc, eol_marker,
0.40.43 by Parth Malwankar
moved cmd_grep._grep_dir to grep.dir_grep
180
                                line_number, revno, print_revno, self.outf)
0.40.37 by Parth Malwankar
grep now accepts rev range
181
                        finally:
182
                            tree.unlock()
183
            finally:
184
                wt.unlock()
0.40.1 by Parth Malwankar
initial skeleton
185
0.40.33 by Parth Malwankar
better message while skipping unversioned file
186
    def _skip_file(self, path):
0.40.37 by Parth Malwankar
grep now accepts rev range
187
        trace.warning("warning: skipped unknown file '%s'." % path)
0.40.33 by Parth Malwankar
better message while skipping unversioned file
188
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
189
0.40.1 by Parth Malwankar
initial skeleton
190
register_command(cmd_grep)
0.40.2 by Parth Malwankar
initial framework for grep
191
0.40.11 by Parth Malwankar
added basic test
192
def test_suite():
193
    from bzrlib.tests import TestUtil
194
195
    suite = TestUtil.TestSuite()
196
    loader = TestUtil.TestLoader()
197
    testmod_names = [
198
        'test_grep',
199
        ]
200
201
    suite.addTest(loader.loadTestsFromModuleNames(
202
            ["%s.%s" % (__name__, tmn) for tmn in testmod_names]))
203
    return suite
204