Rev 479: Add tests for revision_id_to_revno() in preparation of a much faster implementation. in file:///home/jelmer/bzr-svn/0.4/

Jelmer Vernooij jelmer at samba.org
Sun Jun 17 12:40:45 BST 2007


------------------------------------------------------------
revno: 479
committer: Jelmer Vernooij <jelmer at samba.org>
branch nick: 0.4
timestamp: Sun 2007-06-17 13:40:28 +0200
message:
  Add tests for revision_id_to_revno() in preparation of a much faster implementation.
modified:
  branch.py
  tests/test_branch.py
=== modified file 'branch.py'
--- a/branch.py	2007-06-16 23:43:38 +0000
+++ b/branch.py	2007-06-17 11:40:28 +0000
@@ -17,7 +17,7 @@
 
 from bzrlib.branch import Branch, BranchFormat, BranchCheckResult, PullResult
 from bzrlib.bzrdir import BzrDir
-from bzrlib.errors import NoSuchFile, DivergedBranches
+from bzrlib.errors import NoSuchFile, DivergedBranches, NoSuchRevision
 from bzrlib.inventory import (Inventory)
 from bzrlib.trace import mutter
 from bzrlib.workingtree import WorkingTree
@@ -81,8 +81,8 @@
         return checkout.create_workingtree(revision_id)
 
     def lookup_revision_id(self, revid):
-        """Look up the matching revision number on the mainline of the 
-        branch.
+        """Look up the matching Subversion revision number on the mainline of 
+        the branch.
 
         :param revid: Revision id to look up.
         :return: Revision number on the branch. 
@@ -156,8 +156,17 @@
         pass
 
     def last_revision_info(self):
-        hist = self.revision_history()
-        return len(hist), hist[-1]
+        last_revid = self.last_revision()
+        return self.revision_id_to_revno(last_revid), last_revid
+
+    def revision_id_to_revno(self, revision_id):
+        if revision_id is None:
+            return 0
+        history = self.revision_history()
+        try:
+            return history.index(revision_id) + 1
+        except ValueError:
+            raise NoSuchRevision(self, revision_id)
 
     def set_push_location(self, location):
         raise NotImplementedError(self.set_push_location)

=== modified file 'tests/test_branch.py'
--- a/tests/test_branch.py	2007-05-18 15:11:30 +0000
+++ b/tests/test_branch.py	2007-06-17 11:40:28 +0000
@@ -38,6 +38,15 @@
         branch.revision_history()
         self.assertEqual(branch.generate_revision_id(0), branch.last_revision())
 
+    def test_last_rev_rev_info(self):
+        repos_url = self.make_client("a", "dc")
+        branch = Branch.open(repos_url)
+        self.assertEqual((1, branch.generate_revision_id(0)),
+                branch.last_revision_info())
+        branch.revision_history()
+        self.assertEqual((1, branch.generate_revision_id(0)),
+                branch.last_revision_info())
+
     def test_lookup_revision_id_unknown(self):
         repos_url = self.make_client("a", "dc")
         branch = Branch.open(repos_url)
@@ -165,6 +174,44 @@
             repos.generate_revision_id(2, "")],
             branch.revision_history())
 
+    def test_revision_id_to_revno_none(self):
+        """The None revid should map to revno 0."""
+        repos_url = self.make_client('a', 'dc')
+        branch = Branch.open(repos_url)
+        self.assertEquals(0, branch.revision_id_to_revno(None))
+
+    def test_revision_id_to_revno_nonexistant(self):
+        """revision_id_to_revno() should raise NoSuchRevision if
+        the specified revision did not exist in the branch history."""
+        repos_url = self.make_client('a', 'dc')
+        branch = Branch.open(repos_url)
+        self.assertRaises(NoSuchRevision, branch.revision_id_to_revno, "bla")
+    
+    def test_revision_id_to_revno_simple(self):
+        repos_url = self.make_client('a', 'dc')
+        self.build_tree({'dc/foo': "data"})
+        self.client_add("dc/foo")
+        self.client_set_prop("dc", "bzr:revision-id-v%d" % MAPPING_VERSION, 
+                            "myrevid\n")
+        self.client_commit("dc", "My Message")
+        branch = Branch.open(repos_url)
+        self.assertEquals(2, branch.revision_id_to_revno("myrevid"))
+
+    def test_revision_id_to_revno_older(self):
+        repos_url = self.make_client('a', 'dc')
+        self.build_tree({'dc/foo': "data"})
+        self.client_add("dc/foo")
+        self.client_set_prop("dc", "bzr:revision-id-v%d" % MAPPING_VERSION, 
+                            "myrevid\n")
+        self.client_commit("dc", "My Message")
+        self.build_tree({'dc/foo': "someotherdata"})
+        self.client_set_prop("dc", "bzr:revision-id-v%d" % MAPPING_VERSION, 
+                            "myrevid\nmysecondrevid\n")
+        self.client_commit("dc", "My Message")
+        branch = Branch.open(repos_url)
+        self.assertEquals(3, branch.revision_id_to_revno("mysecondrevid"))
+        self.assertEquals(2, branch.revision_id_to_revno("myrevid"))
+
     def test_get_nick_none(self):
         repos_url = self.make_client('a', 'dc')
 




More information about the bazaar-commits mailing list