/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.49.1 by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup.
1
# Copyright (C) 2010 Canonical Ltd
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
"""Print lines matching PATTERN for specified files and revisions."""
18
0.40.147 by Jelmer Vernooij
Fix compatibility with newer versions of bzr: don't use relative imports in lazy imports, and import features from bzrlib.tests.features.
19
from __future__ import absolute_import
20
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
21
from ... import errors
22
from ...commands import Command, display_command
23
from ...option import Option, ListOption
24
from ...config import GlobalConfig
0.49.1 by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup.
25
6808 by Jelmer Vernooij
merge lp:~jelmer/brz/options-unicode/
26
from ...sixish import (
27
    text_type,
28
    )
29
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
30
# FIXME: _parse_levels should be shared with breezy.builtins. this is a copy
0.49.1 by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup.
31
# to avoid the error
32
#   "IllegalUseOfScopeReplacer: ScopeReplacer object '_parse_levels' was used
33
#   incorrectly: Object already cleaned up, did you assign it to another
34
#   variable?: _factory
35
# with lazy import
7143.15.2 by Jelmer Vernooij
Run autopep8.
36
37
0.49.1 by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup.
38
def _parse_levels(s):
39
    try:
40
        return int(s)
41
    except ValueError:
42
        msg = "The levels argument must be an integer."
43
        raise errors.BzrCommandError(msg)
44
45
46
class GrepOptions(object):
47
    """Container to pass around grep options.
48
49
    This class is used as a container to pass around user option and
50
    some other params (like outf) to processing functions. This makes
51
    it easier to add more options as grep evolves.
52
    """
53
    verbose = False
54
    ignore_case = False
55
    no_recursive = False
56
    from_root = False
57
    null = False
58
    levels = None
59
    line_number = False
60
    path_list = None
61
    revision = None
62
    pattern = None
63
    include = None
64
    exclude = None
65
    fixed_string = False
66
    files_with_matches = False
67
    files_without_match = False
68
    color = None
69
    diff = False
70
71
    # derived options
72
    recursive = None
73
    eol_marker = None
74
    patternc = None
75
    sub_patternc = None
76
    print_revno = None
77
    fixed_string = None
78
    outf = None
79
    show_color = False
80
81
82
class cmd_grep(Command):
83
    """Print lines matching PATTERN for specified files and revisions.
84
85
    This command searches the specified files and revisions for a given
86
    pattern.  The pattern is specified as a Python regular expressions[1].
87
88
    If the file name is not specified, the revisions starting with the
89
    current directory are searched recursively. If the revision number is
90
    not specified, the working copy is searched. To search the last committed
91
    revision, use the '-r -1' or '-r last:1' option.
92
93
    Unversioned files are not searched unless explicitly specified on the
94
    command line. Unversioned directores are not searched.
95
96
    When searching a pattern, the output is shown in the 'filepath:string'
97
    format. If a revision is explicitly searched, the output is shown as
98
    'filepath~N:string', where N is the revision number.
99
100
    --include and --exclude options can be used to search only (or exclude
101
    from search) files with base name matches the specified Unix style GLOB
102
    pattern.  The GLOB pattern an use *, ?, and [...] as wildcards, and \\
103
    to quote wildcard or backslash character literally. Note that the glob
104
    pattern is not a regular expression.
105
106
    [1] http://docs.python.org/library/re.html#regular-expression-syntax
107
    """
108
109
    encoding_type = 'replace'
110
    takes_args = ['pattern', 'path*']
111
    takes_options = [
112
        'verbose',
113
        'revision',
6805.1.2 by Jelmer Vernooij
Text type.
114
        Option('color', type=text_type, argname='when',
0.49.1 by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup.
115
               help='Show match in color. WHEN is never, always or auto.'),
116
        Option('diff', short_name='p',
117
               help='Grep for pattern in changeset for each revision.'),
6805.1.2 by Jelmer Vernooij
Text type.
118
        ListOption('exclude', type=text_type, argname='glob', short_name='X',
7143.15.2 by Jelmer Vernooij
Run autopep8.
119
                   help="Skip files whose base name matches GLOB."),
6805.1.2 by Jelmer Vernooij
Text type.
120
        ListOption('include', type=text_type, argname='glob', short_name='I',
7143.15.2 by Jelmer Vernooij
Run autopep8.
121
                   help="Search only files whose base name matches GLOB."),
0.49.1 by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup.
122
        Option('files-with-matches', short_name='l',
123
               help='Print only the name of each input file in '
124
               'which PATTERN is found.'),
125
        Option('files-without-match', short_name='L',
126
               help='Print only the name of each input file in '
127
               'which PATTERN is not found.'),
128
        Option('fixed-string', short_name='F',
129
               help='Interpret PATTERN is a single fixed string (not regex).'),
130
        Option('from-root',
131
               help='Search for pattern starting from the root of the branch. '
132
               '(implies --recursive)'),
133
        Option('ignore-case', short_name='i',
134
               help='ignore case distinctions while matching.'),
135
        Option('levels',
7143.15.2 by Jelmer Vernooij
Run autopep8.
136
               help='Number of levels to display - 0 for all, 1 for collapsed '
137
               '(1 is default).',
138
               argname='N',
139
               type=_parse_levels),
0.49.1 by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup.
140
        Option('line-number', short_name='n',
141
               help='show 1-based line number.'),
142
        Option('no-recursive',
143
               help="Don't recurse into subdirectories. (default is --recursive)"),
144
        Option('null', short_name='Z',
145
               help='Write an ASCII NUL (\\0) separator '
146
               'between output lines rather than a newline.'),
147
        ]
148
149
    @display_command
150
    def run(self, verbose=False, ignore_case=False, no_recursive=False,
151
            from_root=False, null=False, levels=None, line_number=False,
152
            path_list=None, revision=None, pattern=None, include=None,
153
            exclude=None, fixed_string=False, files_with_matches=False,
0.50.1 by jdahlin at com
Add a global grep_color option
154
            files_without_match=False, color=None, diff=False):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
155
        from breezy import _termcolor
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
156
        from . import grep
0.49.1 by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup.
157
        import re
6531.3.8 by Jelmer Vernooij
Move color feature into bzrlib.tests.features.
158
        if path_list is None:
0.49.1 by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup.
159
            path_list = ['.']
160
        else:
161
            if from_root:
7143.15.2 by Jelmer Vernooij
Run autopep8.
162
                raise errors.BzrCommandError(
163
                    'cannot specify both --from-root and PATH.')
0.49.1 by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup.
164
165
        if files_with_matches and files_without_match:
166
            raise errors.BzrCommandError('cannot specify both '
7143.15.2 by Jelmer Vernooij
Run autopep8.
167
                                         '-l/--files-with-matches and -L/--files-without-matches.')
0.49.1 by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup.
168
0.50.1 by jdahlin at com
Add a global grep_color option
169
        global_config = GlobalConfig()
170
171
        if color is None:
172
            color = global_config.get_user_option('grep_color')
173
174
        if color is None:
175
            color = 'never'
176
0.49.1 by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup.
177
        if color not in ['always', 'never', 'auto']:
178
            raise errors.BzrCommandError('Valid values for --color are '
7143.15.2 by Jelmer Vernooij
Run autopep8.
179
                                         '"always", "never" or "auto".')
0.49.1 by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup.
180
7183.3.1 by Martin
Fix E71* lint errors
181
        if levels is None:
7143.15.2 by Jelmer Vernooij
Run autopep8.
182
            levels = 1
0.49.1 by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup.
183
184
        print_revno = False
7183.3.1 by Martin
Fix E71* lint errors
185
        if revision is not None or levels == 0:
0.49.1 by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup.
186
            # print revision numbers as we may be showing multiple revisions
187
            print_revno = True
188
189
        eol_marker = '\n'
190
        if null:
191
            eol_marker = '\0'
192
193
        if not ignore_case and grep.is_fixed_string(pattern):
194
            # if the pattern isalnum, implicitly use to -F for faster grep
195
            fixed_string = True
196
        elif ignore_case and fixed_string:
197
            # GZ 2010-06-02: Fall back to regexp rather than lowercasing
198
            #                pattern and text which will cause pain later
199
            fixed_string = False
200
            pattern = re.escape(pattern)
201
202
        patternc = None
203
        re_flags = re.MULTILINE
204
        if ignore_case:
205
            re_flags |= re.IGNORECASE
206
207
        if not fixed_string:
7143.15.2 by Jelmer Vernooij
Run autopep8.
208
            patternc = grep.compile_pattern(
209
                pattern.encode(grep._user_encoding), re_flags)
0.49.1 by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup.
210
211
        if color == 'always':
212
            show_color = True
213
        elif color == 'never':
214
            show_color = False
215
        elif color == 'auto':
6531.3.10 by Jelmer Vernooij
Rename termcolor to _termcolor.
216
            show_color = _termcolor.allow_color()
0.49.1 by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup.
217
218
        GrepOptions.verbose = verbose
219
        GrepOptions.ignore_case = ignore_case
220
        GrepOptions.no_recursive = no_recursive
221
        GrepOptions.from_root = from_root
222
        GrepOptions.null = null
223
        GrepOptions.levels = levels
224
        GrepOptions.line_number = line_number
225
        GrepOptions.path_list = path_list
226
        GrepOptions.revision = revision
227
        GrepOptions.pattern = pattern
228
        GrepOptions.include = include
229
        GrepOptions.exclude = exclude
230
        GrepOptions.fixed_string = fixed_string
231
        GrepOptions.files_with_matches = files_with_matches
232
        GrepOptions.files_without_match = files_without_match
233
        GrepOptions.color = color
234
        GrepOptions.diff = False
235
236
        GrepOptions.eol_marker = eol_marker
237
        GrepOptions.print_revno = print_revno
238
        GrepOptions.patternc = patternc
239
        GrepOptions.recursive = not no_recursive
240
        GrepOptions.fixed_string = fixed_string
241
        GrepOptions.outf = self.outf
242
        GrepOptions.show_color = show_color
243
6531.3.8 by Jelmer Vernooij
Move color feature into bzrlib.tests.features.
244
        if diff:
0.49.1 by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup.
245
            # options not used:
246
            # files_with_matches, files_without_match
247
            # levels(?), line_number, from_root
248
            # include, exclude
249
            # These are silently ignored.
250
            grep.grep_diff(GrepOptions)
6531.3.8 by Jelmer Vernooij
Move color feature into bzrlib.tests.features.
251
        elif revision is None:
0.49.1 by Jelmer Vernooij
Lazily load commands, saves (some) time loading at bzr startup.
252
            grep.workingtree_grep(GrepOptions)
253
        else:
254
            grep.versioned_grep(GrepOptions)