101
102
# recursive section - that is, it appends the branch name.
104
class SampleBranchFormat(_mod_branch.BranchFormat):
105
class SampleBranchFormat(_mod_branch.BranchFormatMetadir):
105
106
"""A sample format
107
108
this format is initializable, unsupported to aid in testing the
108
109
open and open_downlevel routines.
111
def get_format_string(self):
113
def get_format_string(cls):
112
114
"""See BzrBranchFormat.get_format_string()."""
113
115
return "Sample branch format."
115
def initialize(self, a_bzrdir, name=None, repository=None):
117
def initialize(self, a_bzrdir, name=None, repository=None,
118
append_revisions_only=None):
116
119
"""Format 4 branches cannot be created."""
117
120
t = a_bzrdir.get_branch_transport(self, name=name)
118
121
t.put_bytes('format', self.get_format_string())
130
134
SampleSupportedBranchFormatString = "Sample supported branch format."
132
136
# And the format class can then reference the constant to avoid skew.
133
class SampleSupportedBranchFormat(_mod_branch.BranchFormat):
137
class SampleSupportedBranchFormat(_mod_branch.BranchFormatMetadir):
134
138
"""A sample supported format."""
136
def get_format_string(self):
141
def get_format_string(cls):
137
142
"""See BzrBranchFormat.get_format_string()."""
138
143
return SampleSupportedBranchFormatString
140
def initialize(self, a_bzrdir, name=None):
145
def initialize(self, a_bzrdir, name=None, append_revisions_only=None):
141
146
t = a_bzrdir.get_branch_transport(self, name=name)
142
147
t.put_bytes('format', self.get_format_string())
143
148
return 'A branch'
145
def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
150
def open(self, transport, name=None, _found=False, ignore_fallbacks=False,
151
possible_transports=None):
146
152
return "opened supported branch."
160
166
def initialize(self, a_bzrdir, name=None):
161
167
raise NotImplementedError(self.initialize)
163
def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
169
def open(self, transport, name=None, _found=False, ignore_fallbacks=False,
170
possible_transports=None):
164
171
raise NotImplementedError(self.open)
191
198
b = _mod_branch.Branch.open(self.get_url())
192
199
self.assertEqual(b, "opened supported branch.")
201
def test_from_string(self):
202
self.assertIsInstance(
203
SampleBranchFormat.from_string("Sample branch format."),
205
self.assertRaises(AssertionError,
206
SampleBranchFormat.from_string, "Different branch format.")
194
208
def test_find_format_not_branch(self):
195
209
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
196
210
self.assertRaises(errors.NotBranchError,
197
_mod_branch.BranchFormat.find_format,
211
_mod_branch.BranchFormatMetadir.find_format,
200
214
def test_find_format_unknown_format(self):
201
215
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
202
216
SampleBranchFormat().initialize(dir)
203
217
self.assertRaises(errors.UnknownFormatError,
204
_mod_branch.BranchFormat.find_format,
218
_mod_branch.BranchFormatMetadir.find_format,
207
def test_register_unregister_format(self):
208
# Test the deprecated format registration functions
209
format = SampleBranchFormat()
211
dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
213
format.initialize(dir)
214
# register a format for it.
215
self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
216
_mod_branch.BranchFormat.register_format, format)
217
# which branch.Open will refuse (not supported)
218
self.assertRaises(errors.UnsupportedFormatError,
219
_mod_branch.Branch.open, self.get_url())
220
self.make_branch_and_tree('foo')
221
# but open_downlevel will work
224
bzrdir.BzrDir.open(self.get_url()).open_branch(unsupported=True))
225
# unregister the format
226
self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
227
_mod_branch.BranchFormat.unregister_format, format)
228
self.make_branch_and_tree('bar')
221
def test_find_format_with_features(self):
222
tree = self.make_branch_and_tree('.', format='2a')
223
tree.branch.update_feature_flags({"name": "optional"})
224
found_format = _mod_branch.BranchFormatMetadir.find_format(tree.bzrdir)
225
self.assertIsInstance(found_format, _mod_branch.BranchFormatMetadir)
226
self.assertEquals(found_format.features.get("name"), "optional")
227
tree.branch.update_feature_flags({"name": None})
228
branch = _mod_branch.Branch.open('.')
229
self.assertEquals(branch._format.features, {})
231
232
class TestBranchFormatRegistry(tests.TestCase):
333
334
self.assertPathDoesNotExist('a/.bzr/branch/parent')
334
335
self.assertEqual('http://example.com', branch.get_parent())
335
336
branch.set_push_location('sftp://example.com')
336
config = branch.get_config()._get_branch_data_config()
337
self.assertEqual('sftp://example.com',
338
config.get_user_option('push_location'))
337
conf = branch.get_config_stack()
338
self.assertEqual('sftp://example.com', conf.get('push_location'))
339
339
branch.set_bound_location('ftp://example.com')
340
340
self.assertPathDoesNotExist('a/.bzr/branch/bound')
341
341
self.assertEqual('ftp://example.com', branch.get_bound_location())
343
def test_set_revision_history(self):
344
builder = self.make_branch_builder('.', format=self.get_format_name())
345
builder.build_snapshot('foo', None,
346
[('add', ('', None, 'directory', None))],
348
builder.build_snapshot('bar', None, [], message='bar')
349
branch = builder.get_branch()
351
self.addCleanup(branch.unlock)
352
self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
353
branch.set_revision_history, ['foo', 'bar'])
354
self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
355
branch.set_revision_history, ['foo'])
356
self.assertRaises(errors.NotLefthandHistory,
357
self.applyDeprecated, symbol_versioning.deprecated_in((2, 4, 0)),
358
branch.set_revision_history, ['bar'])
360
343
def do_checkout_test(self, lightweight=False):
361
344
tree = self.make_branch_and_tree('source',
362
345
format=self.get_format_name_subtree())
390
373
def test_light_checkout_with_references(self):
391
374
self.do_checkout_test(lightweight=True)
393
def test_set_push(self):
394
branch = self.make_branch('source', format=self.get_format_name())
395
branch.get_config().set_user_option('push_location', 'old',
396
store=config.STORE_LOCATION)
399
warnings.append(args[0] % args[1:])
400
_warning = trace.warning
401
trace.warning = warning
403
branch.set_push_location('new')
405
trace.warning = _warning
406
self.assertEqual(warnings[0], 'Value "new" is masked by "old" from '
410
377
class TestBranch6(TestBranch67, tests.TestCaseWithTransport):
540
507
self.assertEqual(('path3', 'location3'),
541
508
branch.get_reference_info('file-id'))
510
def _recordParentMapCalls(self, repo):
511
self._parent_map_calls = []
512
orig_get_parent_map = repo.revisions.get_parent_map
513
def get_parent_map(q):
515
self._parent_map_calls.extend([e[0] for e in q])
516
return orig_get_parent_map(q)
517
repo.revisions.get_parent_map = get_parent_map
543
520
class TestBranchReference(tests.TestCaseWithTransport):
544
521
"""Tests for the branch reference facility."""
559
536
self.assertEqual(opened_branch.base, target_branch.base)
561
538
def test_get_reference(self):
562
"""For a BranchReference, get_reference should reutrn the location."""
539
"""For a BranchReference, get_reference should return the location."""
563
540
branch = self.make_branch('target')
564
541
checkout = branch.create_checkout('checkout', lightweight=True)
565
542
reference_url = branch.bzrdir.root_transport.abspath('') + '/'
575
552
def test_constructor(self):
576
553
"""Check that creating a BranchHooks instance has the right defaults."""
577
554
hooks = _mod_branch.BranchHooks()
578
self.assertTrue("set_rh" in hooks, "set_rh not in %s" % hooks)
579
555
self.assertTrue("post_push" in hooks, "post_push not in %s" % hooks)
580
556
self.assertTrue("post_commit" in hooks, "post_commit not in %s" % hooks)
581
557
self.assertTrue("pre_commit" in hooks, "pre_commit not in %s" % hooks)
646
622
super(TestBranchOptions, self).setUp()
647
623
self.branch = self.make_branch('.')
648
self.config = self.branch.get_config()
624
self.config_stack = self.branch.get_config_stack()
650
626
def check_append_revisions_only(self, expected_value, value=None):
651
627
"""Set append_revisions_only in config and check its interpretation."""
652
628
if value is not None:
653
self.config.set_user_option('append_revisions_only', value)
629
self.config_stack.set('append_revisions_only', value)
654
630
self.assertEqual(expected_value,
655
self.branch._get_append_revisions_only())
631
self.branch.get_append_revisions_only())
657
633
def test_valid_append_revisions_only(self):
658
634
self.assertEquals(None,
659
self.config.get_user_option('append_revisions_only'))
635
self.config_stack.get('append_revisions_only'))
660
636
self.check_append_revisions_only(None)
661
637
self.check_append_revisions_only(False, 'False')
662
638
self.check_append_revisions_only(True, 'True')
674
650
self.check_append_revisions_only(None, 'not-a-bool')
675
651
self.assertLength(1, self.warnings)
676
652
self.assertEqual(
677
'Value "not-a-bool" is not a boolean for "append_revisions_only"',
653
'Value "not-a-bool" is not valid for "append_revisions_only"',
678
654
self.warnings[0])
656
def test_use_fresh_values(self):
657
copy = _mod_branch.Branch.open(self.branch.base)
660
copy.get_config_stack().set('foo', 'bar')
663
self.assertFalse(self.branch.is_locked())
664
# Since the branch is locked, the option value won't be saved on disk
665
# so trying to access the config of locked branch via another older
666
# non-locked branch object pointing to the same branch is not supported
667
self.assertEqual(None, self.branch.get_config_stack().get('foo'))
668
# Using a newly created branch object works as expected
669
fresh = _mod_branch.Branch.open(self.branch.base)
670
self.assertEqual('bar', fresh.get_config_stack().get('foo'))
672
def test_set_from_config_get_from_config_stack(self):
673
self.branch.lock_write()
674
self.addCleanup(self.branch.unlock)
675
self.branch.get_config().set_user_option('foo', 'bar')
676
result = self.branch.get_config_stack().get('foo')
677
# https://bugs.launchpad.net/bzr/+bug/948344
678
self.expectFailure('BranchStack uses cache after set_user_option',
679
self.assertEqual, 'bar', result)
681
def test_set_from_config_stack_get_from_config(self):
682
self.branch.lock_write()
683
self.addCleanup(self.branch.unlock)
684
self.branch.get_config_stack().set('foo', 'bar')
685
# Since the branch is locked, the option value won't be saved on disk
686
# so mixing get() and get_user_option() is broken by design.
687
self.assertEqual(None,
688
self.branch.get_config().get_user_option('foo'))
690
def test_set_delays_write_when_branch_is_locked(self):
691
self.branch.lock_write()
692
self.addCleanup(self.branch.unlock)
693
self.branch.get_config_stack().set('foo', 'bar')
694
copy = _mod_branch.Branch.open(self.branch.base)
695
result = copy.get_config_stack().get('foo')
696
# Accessing from a different branch object is like accessing from a
697
# different process: the option has not been saved yet and the new
698
# value cannot be seen.
699
self.assertIs(None, result)
681
702
class TestPullResult(tests.TestCase):
683
def test_pull_result_to_int(self):
684
# to support old code, the pull result can be used as an int
685
r = _mod_branch.PullResult()
688
# this usage of results is not recommended for new code (because it
689
# doesn't describe very well what happened), but for api stability
690
# it's still supported
691
self.assertEqual(self.applyDeprecated(
692
symbol_versioning.deprecated_in((2, 3, 0)),
696
704
def test_report_changed(self):
697
705
r = _mod_branch.PullResult()
698
706
r.old_revid = "old-revid"