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