~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revisionspec.py

  • Committer: Andrew Bennetts
  • Date: 2011-02-14 12:03:05 UTC
  • mto: This revision was merged to the branch mainline in revision 5664.
  • Revision ID: andrew.bennetts@canonical.com-20110214120305-7l7iu1h6f13voeo7
Add release note.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
 
18
import re
 
19
 
18
20
from bzrlib.lazy_import import lazy_import
19
21
lazy_import(globals(), """
20
22
import bisect
21
23
import datetime
 
24
""")
22
25
 
23
26
from bzrlib import (
24
27
    branch as _mod_branch,
 
28
    errors,
25
29
    osutils,
 
30
    registry,
26
31
    revision,
27
32
    symbol_versioning,
 
33
    trace,
28
34
    workingtree,
29
35
    )
30
 
""")
31
 
 
32
 
from bzrlib import (
33
 
    errors,
34
 
    lazy_regex,
35
 
    registry,
36
 
    trace,
37
 
    )
38
36
 
39
37
 
40
38
_marker = []
117
115
        return RevisionInfo(branch, revno, revision_id)
118
116
 
119
117
 
 
118
_revno_regex = None
 
119
 
 
120
 
120
121
class RevisionSpec(object):
121
122
    """A parsed revision specification."""
122
123
 
167
168
                         spectype.__name__, spec)
168
169
            return spectype(spec, _internal=True)
169
170
        else:
 
171
            for spectype in SPEC_TYPES:
 
172
                if spec.startswith(spectype.prefix):
 
173
                    trace.mutter('Returning RevisionSpec %s for %s',
 
174
                                 spectype.__name__, spec)
 
175
                    return spectype(spec, _internal=True)
170
176
            # Otherwise treat it as a DWIM, build the RevisionSpec object and
171
177
            # wait for _match_on to be called.
172
178
            return RevisionSpec_dwim(spec, _internal=True)
298
304
    # each revspec we try.
299
305
    wants_revision_history = False
300
306
 
301
 
    _revno_regex = lazy_regex.lazy_compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
302
 
 
303
 
    # The revspecs to try
304
 
    _possible_revspecs = []
305
 
 
306
307
    def _try_spectype(self, rstype, branch):
307
308
        rs = rstype(self.spec, _internal=True)
308
309
        # Hit in_history to find out if it exists, or we need to try the
313
314
        """Run the lookup and see what we can get."""
314
315
 
315
316
        # First, see if it's a revno
316
 
        if self._revno_regex.match(self.spec) is not None:
 
317
        global _revno_regex
 
318
        if _revno_regex is None:
 
319
            _revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
 
320
        if _revno_regex.match(self.spec) is not None:
317
321
            try:
318
322
                return self._try_spectype(RevisionSpec_revno, branch)
319
323
            except RevisionSpec_revno.dwim_catchable_exceptions:
320
324
                pass
321
325
 
322
326
        # Next see what has been registered
323
 
        for objgetter in self._possible_revspecs:
324
 
            rs_class = objgetter.get_obj()
325
 
            try:
326
 
                return self._try_spectype(rs_class, branch)
327
 
            except rs_class.dwim_catchable_exceptions:
328
 
                pass
329
 
 
330
 
        # Try the old (deprecated) dwim list:
331
327
        for rs_class in dwim_revspecs:
332
328
            try:
333
329
                return self._try_spectype(rs_class, branch)
339
335
        # really relevant.
340
336
        raise errors.InvalidRevisionSpec(self.spec, branch)
341
337
 
342
 
    @classmethod
343
 
    def append_possible_revspec(cls, revspec):
344
 
        """Append a possible DWIM revspec.
345
 
 
346
 
        :param revspec: Revision spec to try.
347
 
        """
348
 
        cls._possible_revspecs.append(registry._ObjectGetter(revspec))
349
 
 
350
 
    @classmethod
351
 
    def append_possible_lazy_revspec(cls, module_name, member_name):
352
 
        """Append a possible lazily loaded DWIM revspec.
353
 
 
354
 
        :param module_name: Name of the module with the revspec
355
 
        :param member_name: Name of the revspec within the module
356
 
        """
357
 
        cls._possible_revspecs.append(
358
 
            registry._LazyObjectGetter(module_name, member_name))
359
 
 
360
338
 
361
339
class RevisionSpec_revno(RevisionSpec):
362
340
    """Selects a revision using a number."""
685
663
                                   August 14th, 2006 at 5:10pm.
686
664
    """
687
665
    prefix = 'date:'
688
 
    _date_regex = lazy_regex.lazy_compile(
 
666
    _date_re = re.compile(
689
667
            r'(?P<date>(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d))?'
690
668
            r'(,|T)?\s*'
691
669
            r'(?P<time>(?P<hour>\d\d):(?P<minute>\d\d)(:(?P<second>\d\d))?)?'
709
687
        elif self.spec.lower() == 'tomorrow':
710
688
            dt = today + datetime.timedelta(days=1)
711
689
        else:
712
 
            m = self._date_regex.match(self.spec)
 
690
            m = self._date_re.match(self.spec)
713
691
            if not m or (not m.group('date') and not m.group('time')):
714
692
                raise errors.InvalidRevisionSpec(self.user_spec,
715
693
                                                 branch, 'invalid date')
993
971
# The order in which we want to DWIM a revision spec without any prefix.
994
972
# revno is always tried first and isn't listed here, this is used by
995
973
# RevisionSpec_dwim._match_on
996
 
dwim_revspecs = symbol_versioning.deprecated_list(
997
 
    symbol_versioning.deprecated_in((2, 4, 0)), "dwim_revspecs", [])
 
974
dwim_revspecs = [
 
975
    RevisionSpec_tag, # Let's try for a tag
 
976
    RevisionSpec_revid, # Maybe it's a revid?
 
977
    RevisionSpec_date, # Perhaps a date?
 
978
    RevisionSpec_branch, # OK, last try, maybe it's a branch
 
979
    ]
998
980
 
999
 
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_tag)
1000
 
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_revid)
1001
 
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_date)
1002
 
RevisionSpec_dwim.append_possible_revspec(RevisionSpec_branch)
1003
981
 
1004
982
revspec_registry = registry.Registry()
1005
983
def _register_revspec(revspec):
1016
994
_register_revspec(RevisionSpec_submit)
1017
995
_register_revspec(RevisionSpec_annotate)
1018
996
_register_revspec(RevisionSpec_mainline)
 
997
 
 
998
# classes in this list should have a "prefix" attribute, against which
 
999
# string specs are matched
 
1000
SPEC_TYPES = symbol_versioning.deprecated_list(
 
1001
    symbol_versioning.deprecated_in((1, 12, 0)), "SPEC_TYPES", [])