Rev 3302: Introduce as_revision_id() as a function instead of in_branch(need_revno=False) in http://bzr.arbash-meinel.com/branches/bzr/1.4-dev/get_rev_id

John Arbash Meinel john at arbash-meinel.com
Thu Mar 20 21:16:53 GMT 2008


At http://bzr.arbash-meinel.com/branches/bzr/1.4-dev/get_rev_id

------------------------------------------------------------
revno: 3302
revision-id: john at arbash-meinel.com-20080320211426-5c3jff1u4fnfemtj
parent: john at arbash-meinel.com-20080320192535-1cqd3rnulm3hh36l
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: get_rev_id
timestamp: Thu 2008-03-20 16:14:26 -0500
message:
  Introduce as_revision_id() as a function instead of in_branch(need_revno=False)
modified:
  bzrlib/revisionspec.py         revisionspec.py-20050907152633-17567659fd5c0ddb
  bzrlib/tests/test_revisionnamespaces.py testrevisionnamespaces.py-20050711050225-8b4af89e6b1efe84
-------------- next part --------------
=== modified file 'bzrlib/revisionspec.py'
--- a/bzrlib/revisionspec.py	2008-03-20 17:19:26 +0000
+++ b/bzrlib/revisionspec.py	2008-03-20 21:14:26 +0000
@@ -232,18 +232,27 @@
     # aliases for now, when we fix the core logic, then they
     # will do what you expect.
     in_store = in_history
+    # in_branch = in_store
 
     def in_branch(self, branch, need_revno=True):
-        """Evaluate this revision spec and return a RevisionInfo object.
-
-        If need_revno is False, the returned RevisionInfo object might
-        have the revno attribute set as None (for performance reasons),
-        even if the revno exists in the specified branch.
-
-        The default implementation is an alias for RevisionSpec.in_history.
-        """
         return self.in_history(branch)
 
+    def as_revision_id(self, context_branch):
+        """Return just the revision_id for this revisions spec.
+
+        Some revision specs require a context_branch to be able to determine
+        their value. Not all specs will make use of it.
+        """
+        return self._as_revision_id(context_branch)
+
+    def _as_revision_id(self, context_branch):
+        """Implementation of as_revision_id()
+
+        Classes should override this function to provide appropriate
+        functionality. The default is to just call '.in_history().rev_id'
+        """
+        return self.in_history(context_branch).rev_id
+
     def __repr__(self):
         # this is mostly for helping with testing
         return '<%s %s>' % (self.__class__.__name__,

=== modified file 'bzrlib/tests/test_revisionnamespaces.py'
--- a/bzrlib/tests/test_revisionnamespaces.py	2008-03-20 19:25:35 +0000
+++ b/bzrlib/tests/test_revisionnamespaces.py	2008-03-20 21:14:26 +0000
@@ -114,6 +114,12 @@
         if info_false.revno is not None:
             self.assertEqual(info_true.revno, info_false.revno)
 
+    def assertAsRevisionId(self, revision_id, revision_spec):
+        """Calling as_revision_id() should return the specified id."""
+        spec = RevisionSpec.from_string(revision_spec)
+        self.assertEqual(revision_id,
+                         spec.as_revision_id(self.tree.branch))
+
 
 class TestOddRevisionSpec(TestRevisionSpec):
     """Test things that aren't normally thought of as revision specs"""
@@ -283,6 +289,13 @@
         self.assertInBranchSupportsNeedRevno('1.1.1',
             has_simple_revno=False)
 
+    def test_as_revision_id(self):
+        self.assertAsRevisionId('r1', '1')
+        self.assertAsRevisionId('r2', '2')
+        self.assertAsRevisionId('r1', '-2')
+        self.assertAsRevisionId('r2', '-1')
+        self.assertAsRevisionId('alt_r2', '1.1.1')
+
 
 class TestRevisionSpec_revid(TestRevisionSpec):
     
@@ -325,6 +338,11 @@
         self.assertInBranchSupportsNeedRevno('revid:alt_r2',
             has_simple_revno=False)
 
+    def test_as_revision_id(self):
+        self.assertAsRevisionId('r1', 'revid:r1')
+        self.assertAsRevisionId('r2', 'revid:r2')
+        self.assertAsRevisionId('alt_r2', 'revid:alt_r2')
+
 
 class TestRevisionSpec_last(TestRevisionSpec):
 
@@ -359,6 +377,10 @@
     def test_supports_need_revno(self):
         self.assertInBranchSupportsNeedRevno('last:1')
 
+    def test_as_revision_id(self):
+        self.assertAsRevisionId('r2', 'last:1')
+        self.assertAsRevisionId('r1', 'last:2')
+
 
 class TestRevisionSpec_before(TestRevisionSpec):
 
@@ -394,6 +416,12 @@
         self.assertInBranchSupportsNeedRevno('before:2')
         self.assertInBranchSupportsNeedRevno('before:revid:r2')
 
+    def test_as_revision_id(self):
+        self.assertAsRevisionId('r1', 'before:revid:r2')
+        self.assertAsRevisionId('r1', 'before:2')
+        self.assertAsRevisionId('r1', 'before:1.1.1')
+        self.assertAsRevisionId('r1', 'before:revid:alt_r2')
+
 
 class TestRevisionSpec_tag(TestRevisionSpec):
     
@@ -422,6 +450,11 @@
         self.tree.branch.tags.set_tag('bzr-0.14', 'r1')
         self.assertInBranchSupportsNeedRevno('tag:bzr-0.14')
 
+    def test_as_revision_id(self):
+        self.tree.branch.tags.set_tag('my-tag', 'r2')
+        self.assertAsRevisionId('r2', 'tag:my-tag')
+        self.assertAsRevisionId('r1', 'before:tag:my-tag')
+
 
 class TestRevisionSpec_date(TestRevisionSpec):
 
@@ -461,6 +494,9 @@
     def test_supports_need_revno(self):
         self.assertInBranchSupportsNeedRevno('date:today')
 
+    def test_as_revision_id(self):
+        self.assertAsRevisionId('new_r2', 'date:today')
+
 
 class TestRevisionSpec_ancestor(TestRevisionSpec):
     
@@ -508,6 +544,9 @@
         self.assertInBranchSupportsNeedRevno('ancestor:tree2',
             has_simple_revno=False)
 
+    def test_as_revision_id(self):
+        self.assertAsRevisionId('alt_r2', 'ancestor:tree2')
+
 
 class TestRevisionSpec_branch(TestRevisionSpec):
     
@@ -546,6 +585,9 @@
         self.assertInBranchSupportsNeedRevno('branch:tree2',
             has_simple_revno=False)
 
+    def test_as_revision_id(self):
+        self.assertAsRevisionId('alt_r2', 'branch:tree2')
+
 
 class TestRevisionSpec_submit(TestRevisionSpec):
 
@@ -566,3 +608,7 @@
         self.tree.branch.set_submit_branch('tree2')
         self.assertInBranchSupportsNeedRevno('submit:',
             has_simple_revno=False)
+
+    def test_as_revision_id(self):
+        self.tree.branch.set_submit_branch('tree2')
+        self.assertAsRevisionId('alt_r2', 'branch:tree2')



More information about the bazaar-commits mailing list