~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/globbing.py

merge only needs a lock_tree_write() on the working tree, not a full lock_write()

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2008 Canonical Ltd
 
1
# Copyright (C) 2006 Canonical Ltd
2
2
 
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
108
108
    return _
109
109
 
110
110
 
111
 
def _trailing_backslashes_regex(m):
112
 
    """Check trailing backslashes.
113
 
 
114
 
    Does a head count on trailing backslashes to ensure there isn't an odd
115
 
    one on the end that would escape the brackets we wrap the RE in.
116
 
    """
117
 
    if (len(m) % 2) != 0:
118
 
        warning(u"Regular expressions cannot end with an odd number of '\\'. "
119
 
                "Dropping the final '\\'.")
120
 
        return m[:-1]
121
 
    return m
122
 
 
123
 
 
124
111
_sub_re = Replacer()
125
112
_sub_re.add(u'^RE:', u'')
126
113
_sub_re.add(u'\((?!\?)', u'(?:')
127
114
_sub_re.add(u'\(\?P<.*>', _invalid_regex(u'(?:'))
128
115
_sub_re.add(u'\(\?P=[^)]*\)', _invalid_regex(u''))
129
 
_sub_re.add(ur'\\+$', _trailing_backslashes_regex)
130
116
 
131
117
 
132
118
_sub_fullpath = Replacer()
183
169
        base_patterns = []
184
170
        ext_patterns = []
185
171
        for pat in patterns:
186
 
            pat = normalize_pattern(pat)
187
172
            if pat.startswith(u'RE:') or u'/' in pat:
188
173
                path_patterns.append(pat)
189
174
            elif pat.startswith(u'*.'):
214
199
            if match:
215
200
                return patterns[match.lastindex -1]
216
201
        return None
217
 
 
218
 
 
219
 
class _OrderedGlobster(Globster):
220
 
    """A Globster that keeps pattern order."""
221
 
 
222
 
    def __init__(self, patterns):
223
 
        """Constructor.
224
 
 
225
 
        :param patterns: sequence of glob patterns
226
 
        """
227
 
        # Note: This could be smarter by running like sequences together
228
 
        self._regex_patterns = []
229
 
        for pat in patterns:
230
 
            pat = normalize_pattern(pat)
231
 
            if pat.startswith(u'RE:') or u'/' in pat:
232
 
                self._add_patterns([pat], _sub_fullpath) 
233
 
            elif pat.startswith(u'*.'):
234
 
                self._add_patterns([pat], _sub_extension,
235
 
                    prefix=r'(?:.*/)?(?!.*/)(?:.*\.)')
236
 
            else:
237
 
                self._add_patterns([pat], _sub_basename, 
238
 
                    prefix=r'(?:.*/)?(?!.*/)')
239
 
 
240
 
 
241
 
def normalize_pattern(pattern):
242
 
    """Converts backslashes in path patterns to forward slashes.
243
 
    
244
 
    Doesn't normalize regular expressions - they may contain escapes.
245
 
    """
246
 
    if not pattern.startswith('RE:'):
247
 
        pattern = pattern.replace('\\','/')
248
 
    return pattern.rstrip('/')
 
202