/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.37 by Parth Malwankar
grep now accepts rev range
35
from bzrlib.builtins import _get_revision_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.2 by Parth Malwankar
initial framework for grep
84
        ]
85
86
87
    @display_command
0.40.4 by Parth Malwankar
intermediate checkin. added support for --null
88
    def run(self, verbose=False, ignore_case=False, recursive=False, from_root=False,
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
89
            null=False, line_number=False, path_list=None, revision=None, pattern=None):
0.40.2 by Parth Malwankar
initial framework for grep
90
        if path_list == None:
91
            path_list = ['.']
92
        else:
93
            if from_root:
94
                raise errors.BzrCommandError('cannot specify both --from-root and PATH.')
95
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
96
        print_revno = False
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
97
        if revision == None:
0.40.36 by Parth Malwankar
improved docs and minor code cleanup
98
            # grep on latest revision by default
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
99
            revision = [RevisionSpec.from_string("last:1")]
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
100
        else:
101
            print_revno = True # used to print revno in output.
102
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
103
        start_rev = revision[0]
0.40.37 by Parth Malwankar
grep now accepts rev range
104
        end_rev = revision[0]
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
105
        if len(revision) == 2:
106
            end_rev = revision[1]
107
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
108
        eol_marker = '\n'
109
        if null:
110
            eol_marker = '\0'
111
0.40.3 by Parth Malwankar
basic file grep is working
112
        re_flags = 0
113
        if ignore_case:
114
            re_flags = re.IGNORECASE
0.40.9 by Parth Malwankar
factored out grep related code to grep.py
115
        patternc = grep.compile_pattern(pattern, re_flags)
0.40.3 by Parth Malwankar
basic file grep is working
116
0.40.28 by Parth Malwankar
basic revision spec is used for grepping
117
        wt, relpath = WorkingTree.open_containing('.')
0.40.37 by Parth Malwankar
grep now accepts rev range
118
119
        start_revid = start_rev.as_revision_id(wt.branch)
120
        end_revid   = end_rev.as_revision_id(wt.branch)
121
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
122
        id_to_revno = wt.branch.get_revision_id_to_revno_map()
0.40.37 by Parth Malwankar
grep now accepts rev range
123
        given_revs = logcmd._graph_view_revisions(wt.branch, start_revid, end_revid)
124
125
        # edge case: we have a repo created with 'bzr init' and it has no
126
        # revisions (revno: 0)
0.40.32 by Parth Malwankar
used try - finally instead of add_cleanup to get plugin to work with 2.0
127
        try:
0.40.37 by Parth Malwankar
grep now accepts rev range
128
            given_revs = list(given_revs)
129
        except erros.NoSuchRevision, e:
130
            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
131
0.40.37 by Parth Malwankar
grep now accepts rev range
132
        for revid, dotrev, merge_depth in given_revs:
133
            wt.lock_read()
134
            rev = RevisionSpec_revid.from_string("revid:"+revid)
135
            try:
136
                for path in path_list:
137
                    tree = rev.as_tree(wt.branch)
0.40.32 by Parth Malwankar
used try - finally instead of add_cleanup to get plugin to work with 2.0
138
                    id = tree.path2id(path)
139
                    if not id:
0.40.33 by Parth Malwankar
better message while skipping unversioned file
140
                        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
141
                        continue
0.40.37 by Parth Malwankar
grep now accepts rev range
142
143
                    revid = rev.as_revision_id(wt.branch)
144
                    revno = self._revno_str(id_to_revno, revid)
145
146
                    if osutils.isdir(path):
147
                        self._grep_dir(tree, relpath, recursive, line_number,
148
                            patternc, from_root, eol_marker, revno, print_revno)
149
                    else:
150
                        tree.lock_read()
151
                        try:
152
                            grep.file_grep(tree, id, '.', path, patternc, eol_marker,
153
                                self.outf, line_number, revno, print_revno)
154
                        finally:
155
                            tree.unlock()
156
            finally:
157
                wt.unlock()
0.40.1 by Parth Malwankar
initial skeleton
158
0.40.33 by Parth Malwankar
better message while skipping unversioned file
159
    def _skip_file(self, path):
0.40.37 by Parth Malwankar
grep now accepts rev range
160
        trace.warning("warning: skipped unknown file '%s'." % path)
0.40.33 by Parth Malwankar
better message while skipping unversioned file
161
0.40.36 by Parth Malwankar
improved docs and minor code cleanup
162
    def _revno_str(self, id_to_revno_dict, revid):
163
        revno = ".".join([str(n) for n in id_to_revno_dict[revid]])
164
        return revno
165
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
166
    def _grep_dir(self, tree, relpath, recursive, line_number, compiled_pattern,
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
167
        from_root, eol_marker, revno, print_revno):
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
168
            # setup relpath to open files relative to cwd
169
            rpath = relpath
170
            if relpath:
171
                rpath = osutils.pathjoin('..',relpath)
172
173
            tree.lock_read()
174
            try:
175
                if from_root:
176
                    # start searching recursively from root
177
                    relpath=None
178
                    recursive=True
179
180
                for fp, fc, fkind, fid, entry in tree.list_files(include_root=False,
181
                    from_dir=relpath, recursive=recursive):
182
                    if fc == 'V' and fkind == 'file':
183
                        grep.file_grep(tree, fid, rpath, fp, compiled_pattern,
0.40.30 by Parth Malwankar
revno is now printed when rspec is given
184
                            eol_marker, self.outf, line_number, revno, print_revno)
0.40.29 by Parth Malwankar
directory grepping is now the _grep_dir function
185
            finally:
186
                tree.unlock()
187
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