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