/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].
0.41.5 by Parth Malwankar
updated help text.
66
    If the file name is not specified, the revisions starting with the current
67
    directory are searched recursively. If the revision number is not specified,
68
    the latest revision is searched.
0.40.36 by Parth Malwankar
improved docs and minor code cleanup
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.'),
0.41.6 by Parth Malwankar
renamed --recurse/--no-recurse => --recursive/--no-recursive
89
        Option('no-recursive',
90
               help="Don't recurse into subdirectories. (default is --recursive)"),
0.40.2 by Parth Malwankar
initial framework for grep
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. '
0.41.6 by Parth Malwankar
renamed --recurse/--no-recurse => --recursive/--no-recursive
93
               '(implies --recursive)'),
0.40.19 by Parth Malwankar
null option should be -Z instead of -z
94
        Option('null', short_name='Z',
0.41.3 by Parth Malwankar
minor fix in help text: ascii => ASCII
95
               help='Write an ASCII NUL (\\0) separator '
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
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',
0.41.5 by Parth Malwankar
updated help text.
98
           help='Number of levels to display - 0 for all, 1 for collapsed (1 is default).',
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
99
           argname='N',
100
           type=_parse_levels),
0.40.2 by Parth Malwankar
initial framework for grep
101
        ]
102
103
104
    @display_command
0.41.6 by Parth Malwankar
renamed --recurse/--no-recurse => --recursive/--no-recursive
105
    def run(self, verbose=False, ignore_case=False, no_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
0.41.6 by Parth Malwankar
renamed --recurse/--no-recurse => --recursive/--no-recursive
108
        recursive = not no_recursive
0.41.4 by Parth Malwankar
--recurse is default.
109
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
110
        if levels==None:
111
            levels=1
112
0.40.2 by Parth Malwankar
initial framework for grep
113
        if path_list == None:
114
            path_list = ['.']
115
        else:
116
            if from_root:
117
                raise errors.BzrCommandError('cannot specify both --from-root and PATH.')
118
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
119
        print_revno = False
0.40.46 by Parth Malwankar
--levels=0 now implicitly prints revnos. added test for --levels=0.
120
        if revision != None or levels == 0:
121
            # print revision numbers as we may be showing multiple revisions
122
            print_revno = True
123
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
124
        if revision == None:
0.40.36 by Parth Malwankar
improved docs and minor code cleanup
125
            # grep on latest revision by default
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
126
            revision = [RevisionSpec.from_string("last:1")]
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
127
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
128
        start_rev = revision[0]
0.40.37 by Parth Malwankar
grep now accepts rev range
129
        end_rev = revision[0]
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
130
        if len(revision) == 2:
131
            end_rev = revision[1]
132
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
133
        eol_marker = '\n'
134
        if null:
135
            eol_marker = '\0'
136
0.40.3 by Parth Malwankar
basic file grep is working
137
        re_flags = 0
138
        if ignore_case:
139
            re_flags = re.IGNORECASE
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
140
        patternc = grep.compile_pattern(pattern, re_flags)
0.40.3 by Parth Malwankar
basic file grep is working
141
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
142
        wt, relpath = WorkingTree.open_containing('.')
0.40.37 by Parth Malwankar
grep now accepts rev range
143
144
        start_revid = start_rev.as_revision_id(wt.branch)
145
        end_revid   = end_rev.as_revision_id(wt.branch)
146
147
        given_revs = logcmd._graph_view_revisions(wt.branch, start_revid, end_revid)
148
149
        # edge case: we have a repo created with 'bzr init' and it has no
150
        # revisions (revno: 0)
0.40.32 by Parth Malwankar
used try - finally instead of add_cleanup to get plugin to work with 2.0
151
        try:
0.40.37 by Parth Malwankar
grep now accepts rev range
152
            given_revs = list(given_revs)
0.40.45 by Parth Malwankar
fixed case where only revno:0 is present. added test.
153
        except errors.NoSuchRevision, e:
154
            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
155
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
156
        for revid, revno, merge_depth in given_revs:
157
            if levels == 1 and merge_depth != 0:
158
                # with level=1 show only top level
159
                continue
160
0.40.37 by Parth Malwankar
grep now accepts rev range
161
            wt.lock_read()
162
            rev = RevisionSpec_revid.from_string("revid:"+revid)
163
            try:
164
                for path in path_list:
165
                    tree = rev.as_tree(wt.branch)
0.40.42 by Parth Malwankar
fix to make grep paths relative to cwd
166
                    path_for_id = osutils.pathjoin(relpath, path)
0.40.38 by Parth Malwankar
added --levels options. added tests for range search.
167
                    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
168
                    if not id:
0.40.33 by Parth Malwankar
better message while skipping unversioned file
169
                        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
170
                        continue
0.40.37 by Parth Malwankar
grep now accepts rev range
171
172
                    if osutils.isdir(path):
0.40.44 by Parth Malwankar
improved display of path when dir is given as argument
173
                        path_prefix = path
0.40.43 by Parth Malwankar
moved cmd_grep._grep_dir to grep.dir_grep
174
                        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
175
                            patternc, from_root, eol_marker, revno, print_revno,
176
                            self.outf, path_prefix)
0.40.37 by Parth Malwankar
grep now accepts rev range
177
                    else:
178
                        tree.lock_read()
179
                        try:
180
                            grep.file_grep(tree, id, '.', path, patternc, eol_marker,
0.40.43 by Parth Malwankar
moved cmd_grep._grep_dir to grep.dir_grep
181
                                line_number, revno, print_revno, self.outf)
0.40.37 by Parth Malwankar
grep now accepts rev range
182
                        finally:
183
                            tree.unlock()
184
            finally:
185
                wt.unlock()
0.40.1 by Parth Malwankar
initial skeleton
186
0.40.33 by Parth Malwankar
better message while skipping unversioned file
187
    def _skip_file(self, path):
0.40.37 by Parth Malwankar
grep now accepts rev range
188
        trace.warning("warning: skipped unknown file '%s'." % path)
0.40.33 by Parth Malwankar
better message while skipping unversioned file
189
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
190
0.40.1 by Parth Malwankar
initial skeleton
191
register_command(cmd_grep)
0.40.2 by Parth Malwankar
initial framework for grep
192
0.40.11 by Parth Malwankar
added basic test
193
def test_suite():
194
    from bzrlib.tests import TestUtil
195
196
    suite = TestUtil.TestSuite()
197
    loader = TestUtil.TestLoader()
198
    testmod_names = [
199
        'test_grep',
200
        ]
201
202
    suite.addTest(loader.loadTestsFromModuleNames(
203
            ["%s.%s" % (__name__, tmn) for tmn in testmod_names]))
204
    return suite
205