Rev 5687: (jelmer) Allow the lazy registration of DWIM revspecs. (Jelmer Vernooij) in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Fri Feb 25 01:23:47 UTC 2011


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 5687 [merge]
revision-id: pqm at pqm.ubuntu.com-20110225012344-r6e63nps6u13g7yr
parent: pqm at pqm.ubuntu.com-20110225004747-jct2ae1o9clrbjqn
parent: jelmer at samba.org-20110225001201-qjw17tihti8vxpkk
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Fri 2011-02-25 01:23:44 +0000
message:
  (jelmer) Allow the lazy registration of DWIM revspecs. (Jelmer Vernooij)
modified:
  bzrlib/revisionspec.py         revisionspec.py-20050907152633-17567659fd5c0ddb
  bzrlib/tests/test_revisionspec.py testrevisionnamespaces.py-20050711050225-8b4af89e6b1efe84
  doc/en/release-notes/bzr-2.4.txt bzr2.4.txt-20110114053217-k7ym9jfz243fddjm-1
=== modified file 'bzrlib/revisionspec.py'
--- a/bzrlib/revisionspec.py	2011-02-21 14:33:37 +0000
+++ b/bzrlib/revisionspec.py	2011-02-25 00:12:01 +0000
@@ -303,6 +303,9 @@
     # each revspec we try.
     wants_revision_history = False
 
+    # The revspecs to try
+    _possible_revspecs = []
+
     def _try_spectype(self, rstype, branch):
         rs = rstype(self.spec, _internal=True)
         # Hit in_history to find out if it exists, or we need to try the
@@ -323,6 +326,14 @@
                 pass
 
         # Next see what has been registered
+        for objgetter in self._possible_revspecs:
+            rs_class = objgetter.get_obj()
+            try:
+                return self._try_spectype(rs_class, branch)
+            except rs_class.dwim_catchable_exceptions:
+                pass
+
+        # Try the old (deprecated) dwim list:
         for rs_class in dwim_revspecs:
             try:
                 return self._try_spectype(rs_class, branch)
@@ -334,6 +345,24 @@
         # really relevant.
         raise errors.InvalidRevisionSpec(self.spec, branch)
 
+    @classmethod
+    def append_possible_revspec(cls, revspec):
+        """Append a possible DWIM revspec.
+
+        :param revspec: Revision spec to try.
+        """
+        cls._possible_revspecs.append(registry._ObjectGetter(revspec))
+
+    @classmethod
+    def append_possible_lazy_revspec(cls, module_name, member_name):
+        """Append a possible lazily loaded DWIM revspec.
+
+        :param module_name: Name of the module with the revspec
+        :param member_name: Name of the revspec within the module
+        """
+        cls._possible_revspecs.append(
+            registry._LazyObjectGetter(module_name, member_name))
+
 
 class RevisionSpec_revno(RevisionSpec):
     """Selects a revision using a number."""
@@ -970,13 +999,13 @@
 # The order in which we want to DWIM a revision spec without any prefix.
 # revno is always tried first and isn't listed here, this is used by
 # RevisionSpec_dwim._match_on
-dwim_revspecs = [
-    RevisionSpec_tag, # Let's try for a tag
-    RevisionSpec_revid, # Maybe it's a revid?
-    RevisionSpec_date, # Perhaps a date?
-    RevisionSpec_branch, # OK, last try, maybe it's a branch
-    ]
+dwim_revspecs = symbol_versioning.deprecated_list(
+    symbol_versioning.deprecated_in((2, 4, 0)), "dwim_revspecs", [])
 
+RevisionSpec_dwim.append_possible_revspec(RevisionSpec_tag)
+RevisionSpec_dwim.append_possible_revspec(RevisionSpec_revid)
+RevisionSpec_dwim.append_possible_revspec(RevisionSpec_date)
+RevisionSpec_dwim.append_possible_revspec(RevisionSpec_branch)
 
 revspec_registry = registry.Registry()
 def _register_revspec(revspec):

=== modified file 'bzrlib/tests/test_revisionspec.py'
--- a/bzrlib/tests/test_revisionspec.py	2011-01-12 01:01:53 +0000
+++ b/bzrlib/tests/test_revisionspec.py	2011-02-25 00:12:01 +0000
@@ -23,7 +23,9 @@
     )
 from bzrlib.tests import TestCaseWithTransport
 from bzrlib.revisionspec import (
+    RevisionInfo,
     RevisionSpec,
+    RevisionSpec_dwim,
     RevisionSpec_tag,
     )
 
@@ -142,6 +144,17 @@
         self.assertRaises(TypeError, RevisionSpec.from_string, object())
 
 
+class RevisionSpec_bork(RevisionSpec):
+
+    prefix = 'irrelevant:'
+
+    def _match_on(self, branch, revs):
+        if self.spec == "bork":
+            return RevisionInfo.from_revision_id(branch, "r1", revs)
+        else:
+            raise errors.InvalidRevisionSpec(self.spec, branch)
+
+
 class TestRevisionSpec_dwim(TestRevisionSpec):
 
     # Don't need to test revno's explicitly since TRS_revno already
@@ -185,6 +198,23 @@
         self.assertInvalid('1.2..1', invalid_as_revision_id=False)
         self.assertInvalid('1.', invalid_as_revision_id=False)
 
+    def test_append_dwim_revspec(self):
+        original_dwim_revspecs = list(RevisionSpec_dwim._possible_revspecs)
+        def reset_dwim_revspecs():
+            RevisionSpec_dwim._possible_revspecs = original_dwim_revspecs
+        self.addCleanup(reset_dwim_revspecs)
+        RevisionSpec_dwim.append_possible_revspec(RevisionSpec_bork)
+        self.assertAsRevisionId('r1', 'bork')
+
+    def test_append_lazy_dwim_revspec(self):
+        original_dwim_revspecs = list(RevisionSpec_dwim._possible_revspecs)
+        def reset_dwim_revspecs():
+            RevisionSpec_dwim._possible_revspecs = original_dwim_revspecs
+        self.addCleanup(reset_dwim_revspecs)
+        RevisionSpec_dwim.append_possible_lazy_revspec(
+            "bzrlib.tests.test_revisionspec", "RevisionSpec_bork")
+        self.assertAsRevisionId('r1', 'bork')
+
 
 class TestRevisionSpec_revno(TestRevisionSpec):
 

=== modified file 'doc/en/release-notes/bzr-2.4.txt'
--- a/doc/en/release-notes/bzr-2.4.txt	2011-02-24 16:58:20 +0000
+++ b/doc/en/release-notes/bzr-2.4.txt	2011-02-25 01:23:44 +0000
@@ -146,6 +146,11 @@
   ``import_last_revision_info_and_tags`` method instead.
   (Andrew Bennetts)
 
+* ``bzrlib.revionspec.dwim_revspecs`` is deprecated.
+  Use ``bzrlib.revisionspec.RevisionSpec_dwim.append_possible_revspec`` and
+  ``bzrlib.revisionspec.RevisionSpec_dwim.append_possible_lazy_revspec``
+  instead.  (Jelmer Vernooij, #721971)
+
 * ``BzrDirFormat`` has a new attribute ``fixed_components`` that
   indicates whether the components of the bzrdir can be upgraded
   independent of the ``BzrDir``. (Jelmer Vernooij)




More information about the bazaar-commits mailing list