13
13
# You should have received a copy of the GNU General Public License
14
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
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Tests for branch implementations - tests a branch format."""
22
19
from bzrlib import (
20
branch as _mod_branch,
33
from bzrlib.branch import Branch, needs_read_lock, needs_write_lock
34
from bzrlib.delta import TreeDelta
35
from bzrlib.errors import (FileExists,
38
UninitializableFormat,
41
from bzrlib.osutils import getcwd
42
import bzrlib.revision
43
34
from bzrlib.symbol_versioning import deprecated_in
44
from bzrlib.tests import TestCase, TestCaseWithTransport, TestSkipped
45
from bzrlib.tests.branch_implementations import TestCaseWithBranch
35
from bzrlib.tests import (
46
39
from bzrlib.tests.http_server import HttpServer
47
from bzrlib.trace import mutter
48
from bzrlib.transport import get_transport
49
from bzrlib.transport.memory import MemoryServer
50
from bzrlib.upgrade import upgrade
51
from bzrlib.workingtree import WorkingTree
54
class TestBranch(TestCaseWithBranch):
40
from bzrlib.transport import memory
43
class TestTestCaseWithBranch(per_branch.TestCaseWithBranch):
45
def test_branch_format_matches_bzrdir_branch_format(self):
46
bzrdir_branch_format = self.bzrdir_format.get_branch_format()
47
self.assertIs(self.branch_format.__class__,
48
bzrdir_branch_format.__class__)
50
def test_make_branch_gets_expected_format(self):
51
branch = self.make_branch('.')
52
self.assertIs(self.branch_format.__class__,
53
branch._format.__class__)
56
class TestBranch(per_branch.TestCaseWithBranch):
56
58
def test_create_tree_with_merge(self):
57
59
tree = self.create_tree_with_merge()
717
772
tree3.merge_from_branch(tree2.branch)
718
773
tree3.commit('empty commit 6')
719
774
tree2.pull(tree3.branch)
777
class TestIgnoreFallbacksParameter(per_branch.TestCaseWithBranch):
779
def make_branch_with_fallback(self):
780
fallback = self.make_branch('fallback')
781
if not fallback._format.supports_stacking():
782
raise tests.TestNotApplicable("format does not support stacking")
783
stacked = self.make_branch('stacked')
784
stacked.set_stacked_on_url(fallback.base)
787
def test_fallbacks_not_opened(self):
788
stacked = self.make_branch_with_fallback()
789
self.get_transport('').rename('fallback', 'moved')
790
reopened = stacked.bzrdir.open_branch(ignore_fallbacks=True)
791
self.assertEqual([], reopened.repository._fallback_repositories)
793
def test_fallbacks_are_opened(self):
794
stacked = self.make_branch_with_fallback()
795
reopened = stacked.bzrdir.open_branch(ignore_fallbacks=False)
796
self.assertLength(1, reopened.repository._fallback_repositories)
799
class TestReferenceLocation(per_branch.TestCaseWithBranch):
801
def test_reference_parent(self):
802
tree = self.make_branch_and_tree('tree')
803
subtree = self.make_branch_and_tree('tree/subtree')
804
subtree.set_root_id('subtree-id')
806
tree.add_reference(subtree)
807
except errors.UnsupportedOperation:
808
raise tests.TestNotApplicable('Tree cannot hold references.')
809
reference_parent = tree.branch.reference_parent('subtree-id',
811
self.assertEqual(subtree.branch.base, reference_parent.base)
813
def test_reference_parent_accepts_possible_transports(self):
814
tree = self.make_branch_and_tree('tree')
815
subtree = self.make_branch_and_tree('tree/subtree')
816
subtree.set_root_id('subtree-id')
818
tree.add_reference(subtree)
819
except errors.UnsupportedOperation:
820
raise tests.TestNotApplicable('Tree cannot hold references.')
821
reference_parent = tree.branch.reference_parent('subtree-id',
822
'subtree', possible_transports=[subtree.bzrdir.root_transport])
824
def test_get_reference_info(self):
825
branch = self.make_branch('branch')
827
path, loc = branch.get_reference_info('file-id')
828
except errors.UnsupportedOperation:
829
raise tests.TestNotApplicable('Branch cannot hold references.')
830
self.assertIs(None, path)
831
self.assertIs(None, loc)
833
def test_set_reference_info(self):
834
branch = self.make_branch('branch')
836
branch.set_reference_info('file-id', 'path/to/location',
838
except errors.UnsupportedOperation:
839
raise tests.TestNotApplicable('Branch cannot hold references.')
841
def test_set_get_reference_info(self):
842
branch = self.make_branch('branch')
844
branch.set_reference_info('file-id', 'path/to/file',
846
except errors.UnsupportedOperation:
847
raise tests.TestNotApplicable('Branch cannot hold references.')
848
# Create a new instance to ensure storage is permanent
849
branch = _mod_branch.Branch.open('branch')
850
tree_path, branch_location = branch.get_reference_info('file-id')
851
self.assertEqual('path/to/location', branch_location)
853
def test_set_null_reference_info(self):
854
branch = self.make_branch('branch')
856
branch.set_reference_info('file-id', 'path/to/file',
858
except errors.UnsupportedOperation:
859
raise tests.TestNotApplicable('Branch cannot hold references.')
860
branch.set_reference_info('file-id', None, None)
861
tree_path, branch_location = branch.get_reference_info('file-id')
862
self.assertIs(None, tree_path)
863
self.assertIs(None, branch_location)
865
def test_set_null_reference_info_when_null(self):
866
branch = self.make_branch('branch')
868
tree_path, branch_location = branch.get_reference_info('file-id')
869
except errors.UnsupportedOperation:
870
raise tests.TestNotApplicable('Branch cannot hold references.')
871
self.assertIs(None, tree_path)
872
self.assertIs(None, branch_location)
873
branch.set_reference_info('file-id', None, None)
875
def test_set_null_requires_two_nones(self):
876
branch = self.make_branch('branch')
878
e = self.assertRaises(ValueError, branch.set_reference_info,
879
'file-id', 'path', None)
880
except errors.UnsupportedOperation:
881
raise tests.TestNotApplicable('Branch cannot hold references.')
882
self.assertEqual('tree_path must be None when branch_location is'
884
e = self.assertRaises(ValueError, branch.set_reference_info,
885
'file-id', None, 'location')
886
self.assertEqual('branch_location must be None when tree_path is'
889
def make_branch_with_reference(self, location, reference_location,
891
branch = self.make_branch(location)
893
branch.set_reference_info(file_id, 'path/to/file',
895
except errors.UnsupportedOperation:
896
raise tests.TestNotApplicable('Branch cannot hold references.')
899
def test_reference_parent_from_reference_info_(self):
900
referenced_branch = self.make_branch('reference_branch')
901
branch = self.make_branch_with_reference('branch',
902
referenced_branch.base)
903
parent = branch.reference_parent('file-id', 'path/to/file')
904
self.assertEqual(parent.base, referenced_branch.base)
906
def test_branch_relative_reference_location(self):
907
branch = self.make_branch('branch')
909
branch.set_reference_info('file-id', 'path/to/file',
910
'../reference_branch')
911
except errors.UnsupportedOperation:
912
raise tests.TestNotApplicable('Branch cannot hold references.')
913
referenced_branch = self.make_branch('reference_branch')
914
parent = branch.reference_parent('file-id', 'path/to/file')
915
self.assertEqual(parent.base, referenced_branch.base)
917
def test_sprout_copies_reference_location(self):
918
branch = self.make_branch_with_reference('branch', '../reference')
919
new_branch = branch.bzrdir.sprout('new-branch').open_branch()
920
self.assertEqual('../reference',
921
new_branch.get_reference_info('file-id')[1])
923
def test_clone_copies_reference_location(self):
924
branch = self.make_branch_with_reference('branch', '../reference')
925
new_branch = branch.bzrdir.clone('new-branch').open_branch()
926
self.assertEqual('../reference',
927
new_branch.get_reference_info('file-id')[1])
929
def test_copied_locations_are_rebased(self):
930
branch = self.make_branch_with_reference('branch', 'reference')
931
new_branch = branch.bzrdir.sprout('branch/new-branch').open_branch()
932
self.assertEqual('../reference',
933
new_branch.get_reference_info('file-id')[1])
935
def test_update_references_retains_old_references(self):
936
branch = self.make_branch_with_reference('branch', 'reference')
937
new_branch = self.make_branch_with_reference(
938
'new_branch', 'reference', 'file-id2')
939
new_branch.update_references(branch)
940
self.assertEqual('reference',
941
branch.get_reference_info('file-id')[1])
943
def test_update_references_retains_known_references(self):
944
branch = self.make_branch_with_reference('branch', 'reference')
945
new_branch = self.make_branch_with_reference(
946
'new_branch', 'reference2')
947
new_branch.update_references(branch)
948
self.assertEqual('reference',
949
branch.get_reference_info('file-id')[1])
951
def test_update_references_skips_known_references(self):
952
branch = self.make_branch_with_reference('branch', 'reference')
953
new_branch = branch.bzrdir.sprout('branch/new-branch').open_branch()
954
new_branch.set_reference_info('file-id', '../foo', '../foo')
955
new_branch.update_references(branch)
956
self.assertEqual('reference',
957
branch.get_reference_info('file-id')[1])
959
def test_pull_updates_references(self):
960
branch = self.make_branch_with_reference('branch', 'reference')
961
new_branch = branch.bzrdir.sprout('branch/new-branch').open_branch()
962
new_branch.set_reference_info('file-id2', '../foo', '../foo')
963
branch.pull(new_branch)
964
self.assertEqual('foo',
965
branch.get_reference_info('file-id2')[1])
967
def test_push_updates_references(self):
968
branch = self.make_branch_with_reference('branch', 'reference')
969
new_branch = branch.bzrdir.sprout('branch/new-branch').open_branch()
970
new_branch.set_reference_info('file-id2', '../foo', '../foo')
971
new_branch.push(branch)
972
self.assertEqual('foo',
973
branch.get_reference_info('file-id2')[1])
975
def test_merge_updates_references(self):
976
branch = self.make_branch_with_reference('branch', 'reference')
977
tree = self.make_branch_and_tree('tree')
979
branch.pull(tree.branch)
980
checkout = branch.create_checkout('checkout', lightweight=True)
981
checkout.commit('bar')
983
self.addCleanup(tree.unlock)
984
merger = merge.Merger.from_revision_ids(None, tree,
985
branch.last_revision(),
987
merger.merge_type = merge.Merge3Merger
989
self.assertEqual('../branch/reference',
990
tree.branch.get_reference_info('file-id')[1])