1 # Copyright (C) 2006-2007 Jelmer Vernooij <jelmer@samba.org>
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 from bzrlib.branch import Branch
20 from bzrlib.bzrdir import BzrDir
21 from bzrlib.errors import NoSuchFile
22 from bzrlib.repository import Repository
23 from bzrlib.trace import mutter
26 from unittest import TestCase
28 from branch import FakeControlFiles, SvnBranchFormat
29 from convert import load_dumpfile
30 from fileids import generate_svn_file_id
31 from repository import MAPPING_VERSION, generate_svn_revision_id
32 from tests import TestCaseWithSubversionRepository
34 class WorkingSubversionBranch(TestCaseWithSubversionRepository):
35 def test_last_rev_rev_hist(self):
36 repos_url = self.make_client("a", "dc")
37 branch = Branch.open(repos_url)
38 branch.revision_history()
39 self.assertEqual(branch.generate_revision_id(0), branch.last_revision())
41 def test_set_parent(self):
42 repos_url = self.make_client('a', 'dc')
43 branch = Branch.open(repos_url)
44 branch.set_parent("foobar")
46 def test_num_revnums(self):
47 repos_url = self.make_client('a', 'dc')
48 bzrdir = BzrDir.open("svn+"+repos_url)
49 branch = bzrdir.open_branch()
50 self.assertEqual(branch.generate_revision_id(0),
51 branch.last_revision())
53 self.build_tree({'dc/foo': "data"})
54 self.client_add("dc/foo")
55 self.client_commit("dc", "My Message")
57 bzrdir = BzrDir.open("svn+"+repos_url)
58 branch = bzrdir.open_branch()
59 repos = bzrdir.find_repository()
61 self.assertEqual(repos.generate_revision_id(1, ""),
62 branch.last_revision())
64 self.build_tree({'dc/foo': "data2"})
65 self.client_commit("dc", "My Message")
67 branch = Branch.open("svn+"+repos_url)
68 repos = Repository.open("svn+"+repos_url)
70 self.assertEqual(repos.generate_revision_id(2, ""),
71 branch.last_revision())
73 def test_set_revision_history(self):
74 repos_url = self.make_client('a', 'dc')
75 branch = Branch.open("svn+"+repos_url)
76 self.assertRaises(NotImplementedError, branch.set_revision_history, [])
78 def test_get_root_id_empty(self):
79 repos_url = self.make_client('a', 'dc')
80 branch = Branch.open("svn+"+repos_url)
81 self.assertEqual(generate_svn_file_id(branch.repository.uuid, 0, "", ""), branch.get_root_id())
83 def test_get_root_id_trunk(self):
84 repos_url = self.make_client('a', 'dc')
85 self.build_tree({'dc/trunk': None})
86 self.client_add("dc/trunk")
87 self.client_commit("dc", "msg")
88 branch = Branch.open("svn+"+repos_url+"/trunk")
89 self.assertEqual(generate_svn_file_id(branch.repository.uuid, 1, "trunk", ""), branch.get_root_id())
91 def test_break_lock(self):
92 repos_url = self.make_client('a', 'dc')
93 branch = Branch.open("svn+"+repos_url)
94 branch.control_files.break_lock()
97 repos_url = self.make_client('a', 'dc')
98 branch = Branch.open("svn+"+repos_url)
99 self.assertEqual("SvnBranch('svn+%s')" % repos_url, branch.__repr__())
101 def test_get_physical_lock_status(self):
102 repos_url = self.make_client('a', 'dc')
103 branch = Branch.open("svn+"+repos_url)
104 self.assertFalse(branch.get_physical_lock_status())
106 def test_set_push_location(self):
107 repos_url = self.make_client('a', 'dc')
108 branch = Branch.open("svn+"+repos_url)
109 self.assertRaises(NotImplementedError, branch.set_push_location, [])
111 def test_get_parent(self):
112 repos_url = self.make_client('a', 'dc')
113 branch = Branch.open("svn+"+repos_url)
114 self.assertEqual("svn+"+repos_url, branch.get_parent())
116 def test_append_revision(self):
117 repos_url = self.make_client('a', 'dc')
118 branch = Branch.open("svn+"+repos_url)
119 branch.append_revision([])
121 def test_get_push_location(self):
122 repos_url = self.make_client('a', 'dc')
123 branch = Branch.open("svn+"+repos_url)
124 self.assertIs(None, branch.get_push_location())
126 def test_revision_history(self):
127 repos_url = self.make_client('a', 'dc')
129 branch = Branch.open("svn+"+repos_url)
130 self.assertEqual([branch.generate_revision_id(0)], branch.revision_history())
132 self.build_tree({'dc/foo': "data"})
133 self.client_add("dc/foo")
134 self.client_commit("dc", "My Message")
136 branch = Branch.open("svn+"+repos_url)
137 repos = Repository.open("svn+"+repos_url)
139 self.assertEqual([repos.generate_revision_id(0, ""),
140 repos.generate_revision_id(1, "")],
141 branch.revision_history())
143 self.build_tree({'dc/foo': "data34"})
144 self.client_commit("dc", "My Message")
146 branch = Branch.open("svn+"+repos_url)
147 repos = Repository.open("svn+"+repos_url)
150 repos.generate_revision_id(0, ""),
151 repos.generate_revision_id(1, ""),
152 repos.generate_revision_id(2, "")],
153 branch.revision_history())
155 def test_get_nick_none(self):
156 repos_url = self.make_client('a', 'dc')
158 self.build_tree({'dc/foo': "data"})
159 self.client_add("dc/foo")
160 self.client_commit("dc", "My Message")
162 branch = Branch.open("svn+"+repos_url)
164 self.assertIs(None, branch.nick)
166 def test_get_nick_path(self):
167 repos_url = self.make_client('a', 'dc')
169 self.build_tree({'dc/trunk': "data"})
170 self.client_add("dc/trunk")
171 self.client_commit("dc", "My Message")
173 branch = Branch.open("svn+"+repos_url+"/trunk")
175 self.assertEqual("trunk", branch.nick)
177 def test_get_revprops(self):
178 repos_url = self.make_client('a', 'dc')
180 self.build_tree({'dc/foo': "data"})
181 self.client_add("dc/foo")
182 self.client_set_prop("dc", "bzr:revprop:branch-nick", "mybranch")
183 self.client_commit("dc", "My Message")
185 branch = Branch.open("svn+"+repos_url)
187 rev = branch.repository.get_revision(branch.last_revision())
189 self.assertEqual("mybranch", rev.properties["branch-nick"])
191 def test_fetch_replace(self):
192 filename = os.path.join(self.test_dir, "dumpfile")
193 open(filename, 'w').write("""SVN-fs-dump-format-version: 2
195 UUID: 6f95bc5c-e18d-4021-aca8-49ed51dbcb75
198 Prop-content-length: 56
204 2006-07-30T12:41:25.270824Z
208 Prop-content-length: 94
222 2006-07-30T12:41:26.117512Z
228 Prop-content-length: 10
234 Node-path: trunk/hosts
237 Prop-content-length: 10
238 Text-content-length: 4
239 Text-content-md5: 771ec3328c29d17af5aacf7f895dd885
246 Prop-content-length: 94
260 2006-07-30T12:41:27.130044Z
263 Node-path: trunk/hosts
266 Text-content-length: 4
267 Text-content-md5: 6c2479dbb342b8df96d84db7ab92c412
273 Prop-content-length: 94
287 2006-07-30T12:41:28.114350Z
290 Node-path: trunk/hosts
293 Text-content-length: 4
294 Text-content-md5: 368cb8d3db6186e2e83d9434f165c525
300 Prop-content-length: 94
314 2006-07-30T12:41:29.129563Z
320 Prop-content-length: 10
327 Prop-content-length: 94
341 2006-07-30T12:41:31.130508Z
344 Node-path: branches/foobranch
348 Node-copyfrom-path: trunk
352 Prop-content-length: 94
366 2006-07-30T12:41:33.129149Z
369 Node-path: branches/foobranch/hosts
373 Node-path: branches/foobranch/hosts
377 Node-copyfrom-path: trunk/hosts
383 Prop-content-length: 94
397 2006-07-30T12:41:34.136423Z
400 Node-path: branches/foobranch/hosts
403 Text-content-length: 8
404 Text-content-md5: 0e328d3517a333a4879ebf3d88fd82bb
411 load_dumpfile("dumpfile", "old")
413 url = "old/branches/foobranch"
414 mutter('open %r' % url)
415 olddir = BzrDir.open(url)
417 newdir = olddir.sprout("new")
419 newbranch = newdir.open_branch()
421 uuid = "6f95bc5c-e18d-4021-aca8-49ed51dbcb75"
422 tree = newbranch.repository.revision_tree(
423 generate_svn_revision_id(uuid, 7, "branches/foobranch"))
425 weave = tree.get_weave(tree.inventory.path2id("hosts"))
427 generate_svn_revision_id(uuid, 6, "branches/foobranch"),
428 generate_svn_revision_id(uuid, 7, "branches/foobranch")],
432 def test_fetch_odd(self):
433 repos_url = self.make_client('d', 'dc')
435 self.build_tree({'dc/trunk': None,
436 'dc/trunk/hosts': 'hej1'})
437 self.client_add("dc/trunk")
438 self.client_commit("dc", "created trunk and added hosts") #1
440 self.build_tree({'dc/trunk/hosts': 'hej2'})
441 self.client_commit("dc", "rev 2") #2
443 self.build_tree({'dc/trunk/hosts': 'hej3'})
444 self.client_commit("dc", "rev 3") #3
446 self.build_tree({'dc/branches': None})
447 self.client_add("dc/branches")
448 self.client_commit("dc", "added branches") #4
450 self.client_copy("dc/trunk", "dc/branches/foobranch")
451 self.client_commit("dc", "added branch foobranch") #5
453 self.build_tree({'dc/branches/foobranch/hosts': 'foohosts'})
454 self.client_commit("dc", "foohosts") #6
458 url = "svn+"+repos_url+"/branches/foobranch"
459 mutter('open %r' % url)
460 olddir = BzrDir.open(url)
462 newdir = olddir.sprout("new")
464 newbranch = newdir.open_branch()
466 uuid = olddir.find_repository().uuid
467 tree = newbranch.repository.revision_tree(
468 generate_svn_revision_id(uuid, 6, "branches/foobranch"))
470 weave = tree.get_weave(tree.inventory.path2id("hosts"))
472 generate_svn_revision_id(uuid, 1, "trunk"),
473 generate_svn_revision_id(uuid, 2, "trunk"),
474 generate_svn_revision_id(uuid, 3, "trunk"),
475 generate_svn_revision_id(uuid, 6, "branches/foobranch")],
478 def test_check(self):
479 self.make_client('d', 'dc')
480 branch = Branch.open('d')
481 result = branch.check()
482 self.assertEqual(branch, result.branch)
484 def test_generate_revision_id(self):
485 self.make_client('d', 'dc')
486 branch = Branch.open('d')
487 self.assertEqual("svn-v%d-undefined:%s::1" % (MAPPING_VERSION, branch.repository.uuid), branch.generate_revision_id(1))
489 def test_create_checkout(self):
490 repos_url = self.make_client('d', 'dc')
492 self.build_tree({'dc/trunk': None, 'dc/trunk/hosts': 'hej1'})
493 self.client_add("dc/trunk")
494 self.client_commit("dc", "created trunk and added hosts") #1
496 url = "svn+"+repos_url+"/trunk"
497 oldbranch = Branch.open(url)
499 newtree = self.create_checkout(oldbranch, "e")
500 self.assertTrue(newtree.branch.repository.has_revision(
501 oldbranch.generate_revision_id(1)))
503 self.assertTrue(os.path.exists("e/.bzr"))
504 self.assertFalse(os.path.exists("e/.svn"))
506 def test_create_checkout_lightweight(self):
507 repos_url = self.make_client('d', 'dc')
509 self.build_tree({'dc/trunk': None,
510 'dc/trunk/hosts': 'hej1'})
511 self.client_add("dc/trunk")
512 self.client_commit("dc", "created trunk and added hosts") #1
514 url = "svn+"+repos_url+"/trunk"
515 oldbranch = Branch.open(url)
517 newtree = self.create_checkout(oldbranch, "e", lightweight=True)
518 self.assertEqual(oldbranch.generate_revision_id(1), newtree.base_revid)
519 self.assertTrue(os.path.exists("e/.svn"))
520 self.assertFalse(os.path.exists("e/.bzr"))
522 def test_create_checkout_lightweight_stop_rev(self):
523 repos_url = self.make_client('d', 'dc')
525 self.build_tree({'dc/trunk': None,
526 'dc/trunk/hosts': 'hej1'})
527 self.client_add("dc/trunk")
528 self.client_commit("dc", "created trunk and added hosts") #1
530 self.build_tree({'dc/trunk/hosts': 'bloe'})
531 self.client_commit("dc", "added another revision")
533 url = "svn+"+repos_url+"/trunk"
534 oldbranch = Branch.open(url)
536 newtree = self.create_checkout(oldbranch, "e", revision_id=
537 oldbranch.generate_revision_id(1), lightweight=True)
538 self.assertEqual(oldbranch.generate_revision_id(1),
540 self.assertTrue(os.path.exists("e/.svn"))
541 self.assertFalse(os.path.exists("e/.bzr"))
543 def test_fetch_branch(self):
544 self.make_client('d', 'sc')
546 self.build_tree({'sc/foo/bla': "data"})
547 self.client_add("sc/foo")
548 self.client_commit("sc", "foo")
550 olddir = self.open_checkout_bzrdir("sc")
554 newdir = olddir.sprout('dc')
557 olddir.open_branch().last_revision(),
558 newdir.open_branch().last_revision())
560 def test_fetch_dir_upgrade(self):
561 repos_url = self.make_client('d', 'sc')
563 self.build_tree({'sc/trunk/mylib/bla': "data", "sc/branches": None})
564 self.client_add("sc/trunk")
565 self.client_add("sc/branches")
566 self.client_commit("sc", "foo")
568 self.client_copy("sc/trunk/mylib", "sc/branches/abranch")
569 self.client_commit("sc", "Promote mylib")
571 olddir = self.open_checkout_bzrdir("sc/branches/abranch")
575 newdir = olddir.sprout('dc')
578 olddir.open_branch().last_revision(),
579 newdir.open_branch().last_revision())
581 def test_fetch_branch_downgrade(self):
582 repos_url = self.make_client('d', 'sc')
584 self.build_tree({'sc/trunk': None, "sc/branches/abranch/bla": 'foo'})
585 self.client_add("sc/trunk")
586 self.client_add("sc/branches")
587 self.client_commit("sc", "foo")
589 self.client_copy("sc/branches/abranch", "sc/trunk/mylib")
590 self.client_commit("sc", "Demote mylib")
592 olddir = self.open_checkout_bzrdir("sc/trunk")
596 newdir = olddir.sprout('dc')
599 olddir.open_branch().last_revision(),
600 newdir.open_branch().last_revision())
604 def test_ghost_workingtree(self):
605 # Looks like bazaar has trouble creating a working tree of a
606 # revision that has ghost parents
607 self.make_client('d', 'sc')
609 self.build_tree({'sc/foo/bla': "data"})
610 self.client_add("sc/foo")
611 self.client_set_prop("sc", "bzr:merge", "some-ghost\n")
612 self.client_commit("sc", "foo")
614 olddir = self.open_checkout_bzrdir("sc")
618 newdir = olddir.sprout('dc')
619 newdir.find_repository().get_revision(
620 newdir.open_branch().last_revision())
621 newdir.find_repository().get_revision_inventory(
622 newdir.open_branch().last_revision())
624 class TestFakeControlFiles(TestCase):
625 def test_get_utf8(self):
626 f = FakeControlFiles()
627 self.assertRaises(NoSuchFile, f.get_utf8, "foo")
631 f = FakeControlFiles()
632 self.assertRaises(NoSuchFile, f.get, "foobla")
634 class BranchFormatTests(TestCase):
636 self.format = SvnBranchFormat()
638 def test_initialize(self):
639 self.assertRaises(NotImplementedError, self.format.initialize, None)
641 def test_get_format_string(self):
642 self.assertEqual("Subversion Smart Server",
643 self.format.get_format_string())
645 def test_get_format_description(self):
646 self.assertEqual("Subversion Smart Server",
647 self.format.get_format_description())