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