Use locking and write groups properly, fixes compatibility with packs.
[jelmer/subvertpy.git] / tests / test_branch.py
index 09e5231921b833271f8e8ece7d053dcc8f8a990f..564354e64ca41cadc8f6f3f8f77d5fc1706d06de 100644 (file)
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
+"""Branch tests."""
+
 from bzrlib.branch import Branch
 from bzrlib.bzrdir import BzrDir
-from bzrlib.errors import NoSuchFile, NoSuchRevision
+from bzrlib.errors import NoSuchFile, NoSuchRevision, NotBranchError
 from bzrlib.repository import Repository
 from bzrlib.trace import mutter
 
@@ -26,7 +28,7 @@ from unittest import TestCase
 from branch import FakeControlFiles, SvnBranchFormat
 from convert import load_dumpfile
 from fileids import generate_svn_file_id
-from repository import MAPPING_VERSION, generate_svn_revision_id
+from repository import MAPPING_VERSION, generate_svn_revision_id, SVN_PROP_BZR_REVISION_ID
 from tests import TestCaseWithSubversionRepository
 
 class WorkingSubversionBranch(TestCaseWithSubversionRepository):
@@ -36,6 +38,32 @@ class WorkingSubversionBranch(TestCaseWithSubversionRepository):
         branch.revision_history()
         self.assertEqual(branch.generate_revision_id(0), branch.last_revision())
 
+    def test_get_branch_path_root(self):
+        repos_url = self.make_client("a", "dc")
+        branch = Branch.open(repos_url)
+        self.assertEqual("", branch.get_branch_path())
+
+    def test_get_branch_path_subdir(self):
+        repos_url = self.make_client("a", "dc")
+        self.build_tree({"dc/trunk": None})
+        self.client_add("dc/trunk")
+        self.client_commit("dc", "Add branch")
+        branch = Branch.open(repos_url+"/trunk")
+        self.assertEqual("trunk", branch.get_branch_path())
+
+    def test_open_nonexistant(self):
+        repos_url = self.make_client("a", "dc")
+        self.assertRaises(NotBranchError, Branch.open, repos_url + "/trunk")
+
+    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)
@@ -63,12 +91,13 @@ class WorkingSubversionBranch(TestCaseWithSubversionRepository):
         self.build_tree({'dc/foo': "data"})
         self.client_add("dc/foo")
         self.client_commit("dc", "My Message")
+        self.client_update("dc")
         
         bzrdir = BzrDir.open("svn+"+repos_url)
         branch = bzrdir.open_branch()
         repos = bzrdir.find_repository()
 
-        self.assertEqual(repos.generate_revision_id(1, ""), 
+        self.assertEqual(repos.generate_revision_id(1, "", "none"), 
                 branch.last_revision())
 
         self.build_tree({'dc/foo': "data2"})
@@ -77,7 +106,7 @@ class WorkingSubversionBranch(TestCaseWithSubversionRepository):
         branch = Branch.open("svn+"+repos_url)
         repos = Repository.open("svn+"+repos_url)
 
-        self.assertEqual(repos.generate_revision_id(2, ""),
+        self.assertEqual(repos.generate_revision_id(2, "", "none"),
                 branch.last_revision())
 
     def test_set_revision_history(self):
@@ -85,19 +114,6 @@ class WorkingSubversionBranch(TestCaseWithSubversionRepository):
         branch = Branch.open("svn+"+repos_url)
         self.assertRaises(NotImplementedError, branch.set_revision_history, [])
 
-    def test_get_root_id_empty(self):
-        repos_url = self.make_client('a', 'dc')
-        branch = Branch.open("svn+"+repos_url)
-        self.assertEqual(generate_svn_file_id(branch.repository.uuid, 0, "", ""), branch.get_root_id())
-
-    def test_get_root_id_trunk(self):
-        repos_url = self.make_client('a', 'dc')
-        self.build_tree({'dc/trunk': None})
-        self.client_add("dc/trunk")
-        self.client_commit("dc", "msg")
-        branch = Branch.open("svn+"+repos_url+"/trunk")
-        self.assertEqual(generate_svn_file_id(branch.repository.uuid, 1, "trunk", ""), branch.get_root_id())
-
     def test_break_lock(self):
         repos_url = self.make_client('a', 'dc')
         branch = Branch.open("svn+"+repos_url)
@@ -137,17 +153,21 @@ class WorkingSubversionBranch(TestCaseWithSubversionRepository):
         repos_url = self.make_client('a', 'dc')
 
         branch = Branch.open("svn+"+repos_url)
-        self.assertEqual([branch.generate_revision_id(0)], branch.revision_history())
+        self.assertEqual([branch.generate_revision_id(0)], 
+                branch.revision_history())
 
         self.build_tree({'dc/foo': "data"})
         self.client_add("dc/foo")
+        self.client_set_prop("dc", SVN_PROP_BZR_REVISION_ID+"none", 
+                "42 mycommit\n")
         self.client_commit("dc", "My Message")
+        self.client_update("dc")
         
         branch = Branch.open("svn+"+repos_url)
         repos = Repository.open("svn+"+repos_url)
 
-        self.assertEqual([repos.generate_revision_id(0, ""), 
-                    repos.generate_revision_id(1, "")], 
+        self.assertEqual([repos.generate_revision_id(0, "", "none"), 
+                    repos.generate_revision_id(1, "", "none")], 
                 branch.revision_history())
 
         self.build_tree({'dc/foo': "data34"})
@@ -157,11 +177,49 @@ class WorkingSubversionBranch(TestCaseWithSubversionRepository):
         repos = Repository.open("svn+"+repos_url)
 
         self.assertEqual([
-            repos.generate_revision_id(0, ""),
-            repos.generate_revision_id(1, ""),
-            repos.generate_revision_id(2, "")],
+            repos.generate_revision_id(0, "", "none"),
+            "mycommit",
+            repos.generate_revision_id(2, "", "none")],
             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-none" % MAPPING_VERSION, 
+                            "2 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-none" % MAPPING_VERSION, 
+                            "2 myrevid\n")
+        self.client_commit("dc", "My Message")
+        self.build_tree({'dc/foo': "someotherdata"})
+        self.client_set_prop("dc", "bzr:revision-id:v%d-none" % MAPPING_VERSION, 
+                            "2 myrevid\n3 mysecondrevid\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')
 
@@ -176,7 +234,7 @@ class WorkingSubversionBranch(TestCaseWithSubversionRepository):
     def test_get_nick_path(self):
         repos_url = self.make_client('a', 'dc')
 
-        self.build_tree({'dc/trunk': "data"})
+        self.build_tree({'dc/trunk': None})
         self.client_add("dc/trunk")
         self.client_commit("dc", "My Message")
 
@@ -189,7 +247,8 @@ class WorkingSubversionBranch(TestCaseWithSubversionRepository):
 
         self.build_tree({'dc/foo': "data"})
         self.client_add("dc/foo")
-        self.client_set_prop("dc", "bzr:revprop:branch-nick", "mybranch")
+        self.client_set_prop("dc", "bzr:revision-info", 
+                "properties: \n\tbranch-nick: mybranch\n")
         self.client_commit("dc", "My Message")
 
         branch = Branch.open("svn+"+repos_url)
@@ -429,14 +488,19 @@ foohosts""")
         newbranch = newdir.open_branch()
 
         uuid = "6f95bc5c-e18d-4021-aca8-49ed51dbcb75"
+        newbranch.lock_read()
         tree = newbranch.repository.revision_tree(
-                generate_svn_revision_id(uuid, 7, "branches/foobranch"))
+                generate_svn_revision_id(uuid, 7, "branches/foobranch", 
+                "trunk0"))
 
-        weave = tree.get_weave(tree.inventory.path2id("hosts"))
+        weave = newbranch.repository.weave_store.get_weave(
+            tree.inventory.path2id("hosts"),
+            newbranch.repository.get_transaction())
         self.assertEqual([
-            generate_svn_revision_id(uuid, 6, "branches/foobranch"),
-            generate_svn_revision_id(uuid, 7, "branches/foobranch")],
+            generate_svn_revision_id(uuid, 6, "branches/foobranch", "trunk0"),
+            generate_svn_revision_id(uuid, 7, "branches/foobranch", "trunk0")],
                           weave.versions())
+        newbranch.unlock()
  
 
     def test_fetch_odd(self):
@@ -475,15 +539,18 @@ foohosts""")
 
         uuid = olddir.find_repository().uuid
         tree = newbranch.repository.revision_tree(
-                generate_svn_revision_id(uuid, 6, "branches/foobranch"))
-
-        weave = tree.get_weave(tree.inventory.path2id("hosts"))
-        self.assertEqual([
-            generate_svn_revision_id(uuid, 1, "trunk"),
-            generate_svn_revision_id(uuid, 2, "trunk"),
-            generate_svn_revision_id(uuid, 3, "trunk"),
-            generate_svn_revision_id(uuid, 6, "branches/foobranch")],
-                          weave.versions())
+             generate_svn_revision_id(uuid, 6, "branches/foobranch", "trunk0"))
+        transaction = newbranch.repository.get_transaction()
+        newbranch.repository.lock_read()
+        weave = newbranch.repository.weave_store.get_weave(
+                tree.inventory.path2id("hosts"), transaction)
+        self.assertEqual(set([
+            generate_svn_revision_id(uuid, 1, "trunk", "trunk0"),
+            generate_svn_revision_id(uuid, 2, "trunk", "trunk0"),
+            generate_svn_revision_id(uuid, 3, "trunk", "trunk0"),
+            generate_svn_revision_id(uuid, 6, "branches/foobranch", "trunk0")]),
+                          set(weave.versions()))
+        newbranch.repository.unlock()
 
     def test_check(self):
         self.make_client('d', 'dc')
@@ -493,8 +560,11 @@ foohosts""")
  
     def test_generate_revision_id(self):
         self.make_client('d', 'dc')
+        self.build_tree({'dc/bla/bloe': None})
+        self.client_add("dc/bla")
+        self.client_commit("dc", "bla")
         branch = Branch.open('d')
-        self.assertEqual("svn-v%d-undefined:%s::1" % (MAPPING_VERSION, branch.repository.uuid),  branch.generate_revision_id(1))
+        self.assertEqual("svn-v%d-none:%s::1" % (MAPPING_VERSION, branch.repository.uuid),  branch.generate_revision_id(1))
 
     def test_create_checkout(self):
         repos_url = self.make_client('d', 'dc')
@@ -536,9 +606,11 @@ foohosts""")
                          'dc/trunk/hosts': 'hej1'})
         self.client_add("dc/trunk")
         self.client_commit("dc", "created trunk and added hosts") #1
+        self.client_update("dc")
         
         self.build_tree({'dc/trunk/hosts': 'bloe'})
         self.client_commit("dc", "added another revision")
+        self.client_update("dc")
 
         url = "svn+"+repos_url+"/trunk"
         oldbranch = Branch.open(url)
@@ -618,7 +690,7 @@ foohosts""")
 
         self.build_tree({'sc/foo/bla': "data"})
         self.client_add("sc/foo")
-        self.client_set_prop("sc", "bzr:merge", "some-ghost\n")
+        self.client_set_prop("sc", "bzr:ancestry:v3-none", "some-ghost\n")
         self.client_commit("sc", "foo")
 
         olddir = self.open_checkout_bzrdir("sc")