~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revisionspec.py

  • Committer: Danny van Heumen
  • Date: 2010-03-09 21:42:11 UTC
  • mto: (4634.139.5 2.0)
  • mto: This revision was merged to the branch mainline in revision 5160.
  • Revision ID: danny@dannyvanheumen.nl-20100309214211-iqh42x6qcikgd9p3
Reverted now-useless TODO list.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2007 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
113
113
        return RevisionInfo(branch, revno, revision_id)
114
114
 
115
115
 
 
116
# classes in this list should have a "prefix" attribute, against which
 
117
# string specs are matched
116
118
_revno_regex = None
117
119
 
118
120
 
121
123
 
122
124
    help_txt = """A parsed revision specification.
123
125
 
124
 
    A revision specification is a string, which may be unambiguous about
125
 
    what it represents by giving a prefix like 'date:' or 'revid:' etc,
126
 
    or it may have no prefix, in which case it's tried against several
127
 
    specifier types in sequence to determine what the user meant.
 
126
    A revision specification can be an integer, in which case it is
 
127
    assumed to be a revno (though this will translate negative values
 
128
    into positive ones); or it can be a string, in which case it is
 
129
    parsed for something like 'date:' or 'revid:' etc.
128
130
 
129
131
    Revision specs are an UI element, and they have been moved out
130
132
    of the branch class to leave "back-end" classes unaware of such
137
139
 
138
140
    prefix = None
139
141
    wants_revision_history = True
140
 
    dwim_catchable_exceptions = (errors.InvalidRevisionSpec,)
141
 
    """Exceptions that RevisionSpec_dwim._match_on will catch.
142
 
 
143
 
    If the revspec is part of ``dwim_revspecs``, it may be tried with an
144
 
    invalid revspec and raises some exception. The exceptions mentioned here
145
 
    will not be reported to the user but simply ignored without stopping the
146
 
    dwim processing.
147
 
    """
148
142
 
149
143
    @staticmethod
150
144
    def from_string(spec):
171
165
                    trace.mutter('Returning RevisionSpec %s for %s',
172
166
                                 spectype.__name__, spec)
173
167
                    return spectype(spec, _internal=True)
174
 
            # Otherwise treat it as a DWIM, build the RevisionSpec object and
175
 
            # wait for _match_on to be called.
176
 
            return RevisionSpec_dwim(spec, _internal=True)
 
168
            # RevisionSpec_revno is special cased, because it is the only
 
169
            # one that directly handles plain integers
 
170
            # TODO: This should not be special cased rather it should be
 
171
            # a method invocation on spectype.canparse()
 
172
            global _revno_regex
 
173
            if _revno_regex is None:
 
174
                _revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
 
175
            if _revno_regex.match(spec) is not None:
 
176
                return RevisionSpec_revno(spec, _internal=True)
 
177
 
 
178
            raise errors.NoSuchRevisionSpec(spec)
177
179
 
178
180
    def __init__(self, spec, _internal=False):
179
181
        """Create a RevisionSpec referring to the Null revision.
288
290
 
289
291
# private API
290
292
 
291
 
class RevisionSpec_dwim(RevisionSpec):
292
 
    """Provides a DWIMish revision specifier lookup.
293
 
 
294
 
    Note that this does not go in the revspec_registry because by definition
295
 
    there is no prefix to identify it.  It's solely called from
296
 
    RevisionSpec.from_string() because the DWIMification happen when _match_on
297
 
    is called so the string describing the revision is kept here until needed.
298
 
    """
299
 
 
300
 
    help_txt = None
301
 
    # We don't need to build the revision history ourself, that's delegated to
302
 
    # each revspec we try.
303
 
    wants_revision_history = False
304
 
 
305
 
    def _try_spectype(self, rstype, branch):
306
 
        rs = rstype(self.spec, _internal=True)
307
 
        # Hit in_history to find out if it exists, or we need to try the
308
 
        # next type.
309
 
        return rs.in_history(branch)
310
 
 
311
 
    def _match_on(self, branch, revs):
312
 
        """Run the lookup and see what we can get."""
313
 
 
314
 
        # First, see if it's a revno
315
 
        global _revno_regex
316
 
        if _revno_regex is None:
317
 
            _revno_regex = re.compile(r'^(?:(\d+(\.\d+)*)|-\d+)(:.*)?$')
318
 
        if _revno_regex.match(self.spec) is not None:
319
 
            try:
320
 
                return self._try_spectype(RevisionSpec_revno, branch)
321
 
            except RevisionSpec_revno.dwim_catchable_exceptions:
322
 
                pass
323
 
 
324
 
        # Next see what has been registered
325
 
        for rs_class in dwim_revspecs:
326
 
            try:
327
 
                return self._try_spectype(rs_class, branch)
328
 
            except rs_class.dwim_catchable_exceptions:
329
 
                pass
330
 
 
331
 
        # Well, I dunno what it is. Note that we don't try to keep track of the
332
 
        # first of last exception raised during the DWIM tries as none seems
333
 
        # really relevant.
334
 
        raise errors.InvalidRevisionSpec(self.spec, branch)
335
 
 
336
 
 
337
293
class RevisionSpec_revno(RevisionSpec):
338
294
    """Selects a revision using a number."""
339
295
 
340
296
    help_txt = """Selects a revision using a number.
341
297
 
342
298
    Use an integer to specify a revision in the history of the branch.
343
 
    Optionally a branch can be specified.  A negative number will count
344
 
    from the end of the branch (-1 is the last revision, -2 the previous
345
 
    one). If the negative number is larger than the branch's history, the
346
 
    first revision is returned.
 
299
    Optionally a branch can be specified. The 'revno:' prefix is optional.
 
300
    A negative number will count from the end of the branch (-1 is the
 
301
    last revision, -2 the previous one). If the negative number is larger
 
302
    than the branch's history, the first revision is returned.
347
303
    Examples::
348
304
 
349
305
      revno:1                   -> return the first revision of this branch
605
561
    """
606
562
 
607
563
    prefix = 'tag:'
608
 
    dwim_catchable_exceptions = (errors.NoSuchTag, errors.TagsNotSupported)
609
564
 
610
565
    def _match_on(self, branch, revs):
611
566
        # Can raise tags not supported, NoSuchTag, etc
805
760
      branch:/path/to/branch
806
761
    """
807
762
    prefix = 'branch:'
808
 
    dwim_catchable_exceptions = (errors.NotBranchError,)
809
763
 
810
764
    def _match_on(self, branch, revs):
811
765
        from bzrlib.branch import Branch
813
767
        revision_b = other_branch.last_revision()
814
768
        if revision_b in (None, revision.NULL_REVISION):
815
769
            raise errors.NoCommits(other_branch)
816
 
        if branch is None:
817
 
            branch = other_branch
818
 
        else:
819
 
            try:
820
 
                # pull in the remote revisions so we can diff
821
 
                branch.fetch(other_branch, revision_b)
822
 
            except errors.ReadOnlyError:
823
 
                branch = other_branch
 
770
        # pull in the remote revisions so we can diff
 
771
        branch.fetch(other_branch, revision_b)
824
772
        try:
825
773
            revno = branch.revision_id_to_revno(revision_b)
826
774
        except errors.NoSuchRevision:
846
794
            raise errors.NoCommits(other_branch)
847
795
        return other_branch.repository.revision_tree(last_revision)
848
796
 
849
 
    def needs_branch(self):
850
 
        return False
851
 
 
852
 
    def get_branch(self):
853
 
        return self.spec
854
 
 
855
797
 
856
798
 
857
799
class RevisionSpec_submit(RevisionSpec_ancestor):
896
838
            self._get_submit_location(context_branch))
897
839
 
898
840
 
899
 
# The order in which we want to DWIM a revision spec without any prefix.
900
 
# revno is always tried first and isn't listed here, this is used by
901
 
# RevisionSpec_dwim._match_on
902
 
dwim_revspecs = [
903
 
    RevisionSpec_tag, # Let's try for a tag
904
 
    RevisionSpec_revid, # Maybe it's a revid?
905
 
    RevisionSpec_date, # Perhaps a date?
906
 
    RevisionSpec_branch, # OK, last try, maybe it's a branch
907
 
    ]
908
 
 
909
 
 
910
841
revspec_registry = registry.Registry()
911
842
def _register_revspec(revspec):
912
843
    revspec_registry.register(revspec.prefix, revspec)
921
852
_register_revspec(RevisionSpec_branch)
922
853
_register_revspec(RevisionSpec_submit)
923
854
 
924
 
# classes in this list should have a "prefix" attribute, against which
925
 
# string specs are matched
926
855
SPEC_TYPES = symbol_versioning.deprecated_list(
927
856
    symbol_versioning.deprecated_in((1, 12, 0)), "SPEC_TYPES", [])