352
330
def test_mismatched_model(self):
353
331
"""Try copying a bundle from knit2 to knit1"""
354
332
format = bzrdir.BzrDirMetaFormat1()
355
format.repository_format = knitrepo.RepositoryFormatKnit3()
333
format.repository_format = repository.RepositoryFormatKnit2()
356
334
source = self.make_branch_and_tree('source', format=format)
357
335
source.commit('one', rev_id='one-id')
358
336
source.commit('two', rev_id='two-id')
359
337
text = StringIO()
360
write_bundle(source.branch.repository, 'two-id', 'null:', text,
338
write_bundle(source.branch.repository, 'two-id', None, text,
364
342
format = bzrdir.BzrDirMetaFormat1()
365
format.repository_format = knitrepo.RepositoryFormatKnit1()
343
format.repository_format = repository.RepositoryFormatKnit1()
366
344
target = self.make_branch('target', format=format)
367
self.assertRaises(errors.IncompatibleRevision, install_bundle,
345
self.assertRaises(errors.IncompatibleRevision, install_bundle,
368
346
target.repository, read_bundle(text))
371
class BundleTester(object):
349
class V08BundleTester(TestCaseWithTransport):
373
353
def bzrdir_format(self):
374
354
format = bzrdir.BzrDirMetaFormat1()
375
format.repository_format = knitrepo.RepositoryFormatKnit1()
355
format.repository_format = repository.RepositoryFormatKnit1()
378
358
def make_branch_and_tree(self, path, format=None):
379
359
if format is None:
380
360
format = self.bzrdir_format()
381
return tests.TestCaseWithTransport.make_branch_and_tree(
361
return TestCaseWithTransport.make_branch_and_tree(self, path, format)
384
363
def make_branch(self, path, format=None):
385
364
if format is None:
386
365
format = self.bzrdir_format()
387
return tests.TestCaseWithTransport.make_branch(self, path, format)
366
return TestCaseWithTransport.make_branch(self, path, format)
389
368
def create_bundle_text(self, base_rev_id, rev_id):
390
369
bundle_txt = StringIO()
391
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
370
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
392
371
bundle_txt, format=self.format)
393
372
bundle_txt.seek(0)
394
self.assertEqual(bundle_txt.readline(),
373
self.assertEqual(bundle_txt.readline(),
395
374
'# Bazaar revision bundle v%s\n' % self.format)
396
375
self.assertEqual(bundle_txt.readline(), '#\n')
398
377
rev = self.b1.repository.get_revision(rev_id)
399
378
self.assertEqual(bundle_txt.readline().decode('utf-8'),
381
open(',,bundle', 'wb').write(bundle_txt.getvalue())
401
382
bundle_txt.seek(0)
402
383
return bundle_txt, rev_ids
667
635
bundle = self.get_valid_bundle('a@cset-0-5', 'a@cset-0-6')
668
636
other = self.get_checkout('a@cset-0-5')
669
tree1_inv = get_inventory_text(self.tree1.branch.repository,
671
tree2_inv = get_inventory_text(other.branch.repository,
637
tree1_inv = self.tree1.branch.repository.get_inventory_xml(
639
tree2_inv = other.branch.repository.get_inventory_xml('a@cset-0-5')
673
640
self.assertEqualDiff(tree1_inv, tree2_inv)
674
641
other.rename_one('sub/dir/nolastnewline.txt', 'sub/nolastnewline.txt')
675
642
other.commit('rename file', rev_id='a@cset-0-6b')
676
self.tree1.merge_from_branch(other.branch)
643
merge([other.basedir, -1], [None, None], this_dir=self.tree1.basedir)
677
644
self.tree1.commit(u'Merge', rev_id='a@cset-0-7',
679
646
bundle = self.get_valid_bundle('a@cset-0-6', 'a@cset-0-7')
681
def _test_symlink_bundle(self, link_name, link_target, new_link_target):
684
self.requireFeature(tests.SymlinkFeature)
648
def test_symlink_bundle(self):
649
if not has_symlinks():
650
raise TestSkipped("No symlink support")
685
651
self.tree1 = self.make_branch_and_tree('b1')
686
652
self.b1 = self.tree1.branch
688
653
tt = TreeTransform(self.tree1)
689
tt.new_symlink(link_name, tt.root, link_target, link_id)
654
tt.new_symlink('link', tt.root, 'bar/foo', 'link-1')
691
656
self.tree1.commit('add symlink', rev_id='l@cset-0-1')
692
bundle = self.get_valid_bundle('null:', 'l@cset-0-1')
693
if getattr(bundle ,'revision_tree', None) is not None:
694
# Not all bundle formats supports revision_tree
695
bund_tree = bundle.revision_tree(self.b1.repository, 'l@cset-0-1')
696
self.assertEqual(link_target, bund_tree.get_symlink_target(link_id))
657
self.get_valid_bundle(None, 'l@cset-0-1')
698
658
tt = TreeTransform(self.tree1)
699
trans_id = tt.trans_id_tree_file_id(link_id)
659
trans_id = tt.trans_id_tree_file_id('link-1')
700
660
tt.adjust_path('link2', tt.root, trans_id)
701
661
tt.delete_contents(trans_id)
702
tt.create_symlink(new_link_target, trans_id)
662
tt.create_symlink('mars', trans_id)
704
664
self.tree1.commit('rename and change symlink', rev_id='l@cset-0-2')
705
bundle = self.get_valid_bundle('l@cset-0-1', 'l@cset-0-2')
706
if getattr(bundle ,'revision_tree', None) is not None:
707
# Not all bundle formats supports revision_tree
708
bund_tree = bundle.revision_tree(self.b1.repository, 'l@cset-0-2')
709
self.assertEqual(new_link_target,
710
bund_tree.get_symlink_target(link_id))
665
self.get_valid_bundle('l@cset-0-1', 'l@cset-0-2')
712
666
tt = TreeTransform(self.tree1)
713
trans_id = tt.trans_id_tree_file_id(link_id)
667
trans_id = tt.trans_id_tree_file_id('link-1')
714
668
tt.delete_contents(trans_id)
715
669
tt.create_symlink('jupiter', trans_id)
717
671
self.tree1.commit('just change symlink target', rev_id='l@cset-0-3')
718
bundle = self.get_valid_bundle('l@cset-0-2', 'l@cset-0-3')
672
self.get_valid_bundle('l@cset-0-2', 'l@cset-0-3')
720
673
tt = TreeTransform(self.tree1)
721
trans_id = tt.trans_id_tree_file_id(link_id)
674
trans_id = tt.trans_id_tree_file_id('link-1')
722
675
tt.delete_contents(trans_id)
724
677
self.tree1.commit('Delete symlink', rev_id='l@cset-0-4')
725
bundle = self.get_valid_bundle('l@cset-0-3', 'l@cset-0-4')
727
def test_symlink_bundle(self):
728
self._test_symlink_bundle('link', 'bar/foo', 'mars')
730
def test_unicode_symlink_bundle(self):
731
self.requireFeature(tests.UnicodeFilenameFeature)
732
self._test_symlink_bundle(u'\N{Euro Sign}link',
733
u'bar/\N{Euro Sign}foo',
734
u'mars\N{Euro Sign}')
678
self.get_valid_bundle('l@cset-0-3', 'l@cset-0-4')
736
680
def test_binary_bundle(self):
737
681
self.tree1 = self.make_branch_and_tree('b1')
738
682
self.b1 = self.tree1.branch
739
683
tt = TreeTransform(self.tree1)
742
686
tt.new_file('file', tt.root, '\x00\n\x00\r\x01\n\x02\r\xff', 'binary-1')
743
tt.new_file('file2', tt.root, '\x01\n\x02\r\x03\n\x04\r\xff',
687
tt.new_file('file2', tt.root, '\x01\n\x02\r\x03\n\x04\r\xff', 'binary-2')
746
689
self.tree1.commit('add binary', rev_id='b@cset-0-1')
747
self.get_valid_bundle('null:', 'b@cset-0-1')
690
self.get_valid_bundle(None, 'b@cset-0-1')
750
693
tt = TreeTransform(self.tree1)
944
877
self.tree1 = self.make_branch_and_tree('b1')
945
878
self.b1 = self.tree1.branch
946
879
self.tree1.commit('message', rev_id='revid1')
947
bundle = self.get_valid_bundle('null:', 'revid1')
948
tree = self.get_bundle_tree(bundle, 'revid1')
880
bundle = self.get_valid_bundle(None, 'revid1')
881
tree = bundle.revision_tree(self.b1.repository, 'revid1')
949
882
self.assertEqual('revid1', tree.inventory.root.revision)
951
def test_install_revisions(self):
952
self.tree1 = self.make_branch_and_tree('b1')
953
self.b1 = self.tree1.branch
954
self.tree1.commit('message', rev_id='rev2a')
955
bundle = self.get_valid_bundle('null:', 'rev2a')
956
branch2 = self.make_branch('b2')
957
self.assertFalse(branch2.repository.has_revision('rev2a'))
958
target_revision = bundle.install_revisions(branch2.repository)
959
self.assertTrue(branch2.repository.has_revision('rev2a'))
960
self.assertEqual('rev2a', target_revision)
962
def test_bundle_empty_property(self):
963
"""Test serializing revision properties with an empty value."""
964
tree = self.make_branch_and_memory_tree('tree')
966
self.addCleanup(tree.unlock)
967
tree.add([''], ['TREE_ROOT'])
968
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
969
self.b1 = tree.branch
970
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
971
bundle = read_bundle(bundle_sio)
972
revision_info = bundle.revisions[0]
973
self.assertEqual('rev1', revision_info.revision_id)
974
rev = revision_info.as_revision()
975
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
978
def test_bundle_sorted_properties(self):
979
"""For stability the writer should write properties in sorted order."""
980
tree = self.make_branch_and_memory_tree('tree')
982
self.addCleanup(tree.unlock)
984
tree.add([''], ['TREE_ROOT'])
985
tree.commit('One', rev_id='rev1',
986
revprops={'a':'4', 'b':'3', 'c':'2', 'd':'1'})
987
self.b1 = tree.branch
988
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
989
bundle = read_bundle(bundle_sio)
990
revision_info = bundle.revisions[0]
991
self.assertEqual('rev1', revision_info.revision_id)
992
rev = revision_info.as_revision()
993
self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
994
'd':'1'}, rev.properties)
996
def test_bundle_unicode_properties(self):
997
"""We should be able to round trip a non-ascii property."""
998
tree = self.make_branch_and_memory_tree('tree')
1000
self.addCleanup(tree.unlock)
1002
tree.add([''], ['TREE_ROOT'])
1003
# Revisions themselves do not require anything about revision property
1004
# keys, other than that they are a basestring, and do not contain
1006
# However, Testaments assert than they are str(), and thus should not
1008
tree.commit('One', rev_id='rev1',
1009
revprops={'omega':u'\u03a9', 'alpha':u'\u03b1'})
1010
self.b1 = tree.branch
1011
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1012
bundle = read_bundle(bundle_sio)
1013
revision_info = bundle.revisions[0]
1014
self.assertEqual('rev1', revision_info.revision_id)
1015
rev = revision_info.as_revision()
1016
self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
1017
'alpha':u'\u03b1'}, rev.properties)
1019
def test_bundle_with_ghosts(self):
1020
tree = self.make_branch_and_tree('tree')
1021
self.b1 = tree.branch
1022
self.build_tree_contents([('tree/file', 'content1')])
1025
self.build_tree_contents([('tree/file', 'content2')])
1026
tree.add_parent_tree_id('ghost')
1027
tree.commit('rev2', rev_id='rev2')
1028
bundle = self.get_valid_bundle('null:', 'rev2')
1030
def make_simple_tree(self, format=None):
1031
tree = self.make_branch_and_tree('b1', format=format)
1032
self.b1 = tree.branch
1033
self.build_tree(['b1/file'])
1037
def test_across_serializers(self):
1038
tree = self.make_simple_tree('knit')
1039
tree.commit('hello', rev_id='rev1')
1040
tree.commit('hello', rev_id='rev2')
1041
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1042
repo = self.make_repository('repo', format='dirstate-with-subtree')
1043
bundle.install_revisions(repo)
1044
inv_text = repo._get_inventory_xml('rev2')
1045
self.assertNotContainsRe(inv_text, 'format="5"')
1046
self.assertContainsRe(inv_text, 'format="7"')
1048
def make_repo_with_installed_revisions(self):
1049
tree = self.make_simple_tree('knit')
1050
tree.commit('hello', rev_id='rev1')
1051
tree.commit('hello', rev_id='rev2')
1052
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1053
repo = self.make_repository('repo', format='dirstate-with-subtree')
1054
bundle.install_revisions(repo)
1057
def test_across_models(self):
1058
repo = self.make_repo_with_installed_revisions()
1059
inv = repo.get_inventory('rev2')
1060
self.assertEqual('rev2', inv.root.revision)
1061
root_id = inv.root.file_id
1063
self.addCleanup(repo.unlock)
1064
self.assertEqual({(root_id, 'rev1'):(),
1065
(root_id, 'rev2'):((root_id, 'rev1'),)},
1066
repo.texts.get_parent_map([(root_id, 'rev1'), (root_id, 'rev2')]))
1068
def test_inv_hash_across_serializers(self):
1069
repo = self.make_repo_with_installed_revisions()
1070
recorded_inv_sha1 = repo.get_revision('rev2').inventory_sha1
1071
xml = repo._get_inventory_xml('rev2')
1072
self.assertEqual(osutils.sha_string(xml), recorded_inv_sha1)
1074
def test_across_models_incompatible(self):
1075
tree = self.make_simple_tree('dirstate-with-subtree')
1076
tree.commit('hello', rev_id='rev1')
1077
tree.commit('hello', rev_id='rev2')
1079
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1080
except errors.IncompatibleBundleFormat:
1081
raise tests.TestSkipped("Format 0.8 doesn't work with knit3")
1082
repo = self.make_repository('repo', format='knit')
1083
bundle.install_revisions(repo)
1085
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1086
self.assertRaises(errors.IncompatibleRevision,
1087
bundle.install_revisions, repo)
1089
def test_get_merge_request(self):
1090
tree = self.make_simple_tree()
1091
tree.commit('hello', rev_id='rev1')
1092
tree.commit('hello', rev_id='rev2')
1093
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1094
result = bundle.get_merge_request(tree.branch.repository)
1095
self.assertEqual((None, 'rev1', 'inapplicable'), result)
1097
def test_with_subtree(self):
1098
tree = self.make_branch_and_tree('tree',
1099
format='dirstate-with-subtree')
1100
self.b1 = tree.branch
1101
subtree = self.make_branch_and_tree('tree/subtree',
1102
format='dirstate-with-subtree')
1104
tree.commit('hello', rev_id='rev1')
1106
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1107
except errors.IncompatibleBundleFormat:
1108
raise tests.TestSkipped("Format 0.8 doesn't work with knit3")
1109
if isinstance(bundle, v09.BundleInfo09):
1110
raise tests.TestSkipped("Format 0.9 doesn't work with subtrees")
1111
repo = self.make_repository('repo', format='knit')
1112
self.assertRaises(errors.IncompatibleRevision,
1113
bundle.install_revisions, repo)
1114
repo2 = self.make_repository('repo2', format='dirstate-with-subtree')
1115
bundle.install_revisions(repo2)
1117
def test_revision_id_with_slash(self):
1118
self.tree1 = self.make_branch_and_tree('tree')
1119
self.b1 = self.tree1.branch
1121
self.tree1.commit('Revision/id/with/slashes', rev_id='rev/id')
1123
raise tests.TestSkipped(
1124
"Repository doesn't support revision ids with slashes")
1125
bundle = self.get_valid_bundle('null:', 'rev/id')
1127
def test_skip_file(self):
1128
"""Make sure we don't accidentally write to the wrong versionedfile"""
1129
self.tree1 = self.make_branch_and_tree('tree')
1130
self.b1 = self.tree1.branch
1131
# rev1 is not present in bundle, done by fetch
1132
self.build_tree_contents([('tree/file2', 'contents1')])
1133
self.tree1.add('file2', 'file2-id')
1134
self.tree1.commit('rev1', rev_id='reva')
1135
self.build_tree_contents([('tree/file3', 'contents2')])
1136
# rev2 is present in bundle, and done by fetch
1137
# having file1 in the bunle causes file1's versionedfile to be opened.
1138
self.tree1.add('file3', 'file3-id')
1139
self.tree1.commit('rev2')
1140
# Updating file2 should not cause an attempt to add to file1's vf
1141
target = self.tree1.bzrdir.sprout('target').open_workingtree()
1142
self.build_tree_contents([('tree/file2', 'contents3')])
1143
self.tree1.commit('rev3', rev_id='rev3')
1144
bundle = self.get_valid_bundle('reva', 'rev3')
1145
if getattr(bundle, 'get_bundle_reader', None) is None:
1146
raise tests.TestSkipped('Bundle format cannot provide reader')
1147
# be sure that file1 comes before file2
1148
for b, m, k, r, f in bundle.get_bundle_reader().iter_records():
1151
self.assertNotEqual(f, 'file2-id')
1152
bundle.install_revisions(target.branch.repository)
1155
class V08BundleTester(BundleTester, tests.TestCaseWithTransport):
1159
def test_bundle_empty_property(self):
1160
"""Test serializing revision properties with an empty value."""
1161
tree = self.make_branch_and_memory_tree('tree')
1163
self.addCleanup(tree.unlock)
1164
tree.add([''], ['TREE_ROOT'])
1165
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
1166
self.b1 = tree.branch
1167
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1168
self.assertContainsRe(bundle_sio.getvalue(),
1170
'# branch-nick: tree\n'
1174
bundle = read_bundle(bundle_sio)
1175
revision_info = bundle.revisions[0]
1176
self.assertEqual('rev1', revision_info.revision_id)
1177
rev = revision_info.as_revision()
1178
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
1181
def get_bundle_tree(self, bundle, revision_id):
1182
repository = self.make_repository('repo')
1183
return bundle.revision_tree(repository, 'revid1')
1185
def test_bundle_empty_property_alt(self):
1186
"""Test serializing revision properties with an empty value.
1188
Older readers had a bug when reading an empty property.
1189
They assumed that all keys ended in ': \n'. However they would write an
1190
empty value as ':\n'. This tests make sure that all newer bzr versions
1191
can handle th second form.
1193
tree = self.make_branch_and_memory_tree('tree')
1195
self.addCleanup(tree.unlock)
1196
tree.add([''], ['TREE_ROOT'])
1197
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
1198
self.b1 = tree.branch
1199
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1200
txt = bundle_sio.getvalue()
1201
loc = txt.find('# empty: ') + len('# empty:')
1202
# Create a new bundle, which strips the trailing space after empty
1203
bundle_sio = StringIO(txt[:loc] + txt[loc+1:])
1205
self.assertContainsRe(bundle_sio.getvalue(),
1207
'# branch-nick: tree\n'
1211
bundle = read_bundle(bundle_sio)
1212
revision_info = bundle.revisions[0]
1213
self.assertEqual('rev1', revision_info.revision_id)
1214
rev = revision_info.as_revision()
1215
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
1218
def test_bundle_sorted_properties(self):
1219
"""For stability the writer should write properties in sorted order."""
1220
tree = self.make_branch_and_memory_tree('tree')
1222
self.addCleanup(tree.unlock)
1224
tree.add([''], ['TREE_ROOT'])
1225
tree.commit('One', rev_id='rev1',
1226
revprops={'a':'4', 'b':'3', 'c':'2', 'd':'1'})
1227
self.b1 = tree.branch
1228
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1229
self.assertContainsRe(bundle_sio.getvalue(),
1233
'# branch-nick: tree\n'
1237
bundle = read_bundle(bundle_sio)
1238
revision_info = bundle.revisions[0]
1239
self.assertEqual('rev1', revision_info.revision_id)
1240
rev = revision_info.as_revision()
1241
self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
1242
'd':'1'}, rev.properties)
1244
def test_bundle_unicode_properties(self):
1245
"""We should be able to round trip a non-ascii property."""
1246
tree = self.make_branch_and_memory_tree('tree')
1248
self.addCleanup(tree.unlock)
1250
tree.add([''], ['TREE_ROOT'])
1251
# Revisions themselves do not require anything about revision property
1252
# keys, other than that they are a basestring, and do not contain
1254
# However, Testaments assert than they are str(), and thus should not
1256
tree.commit('One', rev_id='rev1',
1257
revprops={'omega':u'\u03a9', 'alpha':u'\u03b1'})
1258
self.b1 = tree.branch
1259
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1260
self.assertContainsRe(bundle_sio.getvalue(),
1262
'# alpha: \xce\xb1\n'
1263
'# branch-nick: tree\n'
1264
'# omega: \xce\xa9\n'
1266
bundle = read_bundle(bundle_sio)
1267
revision_info = bundle.revisions[0]
1268
self.assertEqual('rev1', revision_info.revision_id)
1269
rev = revision_info.as_revision()
1270
self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
1271
'alpha':u'\u03b1'}, rev.properties)
1274
885
class V09BundleKnit2Tester(V08BundleTester):
1288
899
def bzrdir_format(self):
1289
900
format = bzrdir.BzrDirMetaFormat1()
1290
format.repository_format = knitrepo.RepositoryFormatKnit1()
901
format.repository_format = repository.RepositoryFormatKnit1()
1294
class V4BundleTester(BundleTester, tests.TestCaseWithTransport):
1298
def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
1299
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1300
Make sure that the text generated is valid, and that it
1301
can be applied against the base, and generate the same information.
1303
:return: The in-memory bundle
1305
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1307
# This should also validate the generated bundle
1308
bundle = read_bundle(bundle_txt)
1309
repository = self.b1.repository
1310
for bundle_rev in bundle.real_revisions:
1311
# These really should have already been checked when we read the
1312
# bundle, since it computes the sha1 hash for the revision, which
1313
# only will match if everything is okay, but lets be explicit about
1315
branch_rev = repository.get_revision(bundle_rev.revision_id)
1316
for a in ('inventory_sha1', 'revision_id', 'parent_ids',
1317
'timestamp', 'timezone', 'message', 'committer',
1318
'parent_ids', 'properties'):
1319
self.assertEqual(getattr(branch_rev, a),
1320
getattr(bundle_rev, a))
1321
self.assertEqual(len(branch_rev.parent_ids),
1322
len(bundle_rev.parent_ids))
1323
self.assertEqual(set(rev_ids),
1324
set([r.revision_id for r in bundle.real_revisions]))
1325
self.valid_apply_bundle(base_rev_id, bundle,
1326
checkout_dir=checkout_dir)
1330
def get_invalid_bundle(self, base_rev_id, rev_id):
1331
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1332
Munge the text so that it's invalid.
1334
:return: The in-memory bundle
1336
from bzrlib.bundle import serializer
1337
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1338
new_text = self.get_raw(StringIO(''.join(bundle_txt)))
1339
new_text = new_text.replace('<file file_id="exe-1"',
1340
'<file executable="y" file_id="exe-1"')
1341
new_text = new_text.replace('B260', 'B275')
1342
bundle_txt = StringIO()
1343
bundle_txt.write(serializer._get_bundle_header('4'))
1344
bundle_txt.write('\n')
1345
bundle_txt.write(new_text.encode('bz2'))
1347
bundle = read_bundle(bundle_txt)
1348
self.valid_apply_bundle(base_rev_id, bundle)
1351
def create_bundle_text(self, base_rev_id, rev_id):
1352
bundle_txt = StringIO()
1353
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
1354
bundle_txt, format=self.format)
1356
self.assertEqual(bundle_txt.readline(),
1357
'# Bazaar revision bundle v%s\n' % self.format)
1358
self.assertEqual(bundle_txt.readline(), '#\n')
1359
rev = self.b1.repository.get_revision(rev_id)
1361
return bundle_txt, rev_ids
1363
def get_bundle_tree(self, bundle, revision_id):
1364
repository = self.make_repository('repo')
1365
bundle.install_revisions(repository)
1366
return repository.revision_tree(revision_id)
1368
def test_creation(self):
1369
tree = self.make_branch_and_tree('tree')
1370
self.build_tree_contents([('tree/file', 'contents1\nstatic\n')])
1371
tree.add('file', 'fileid-2')
1372
tree.commit('added file', rev_id='rev1')
1373
self.build_tree_contents([('tree/file', 'contents2\nstatic\n')])
1374
tree.commit('changed file', rev_id='rev2')
1376
serializer = BundleSerializerV4('1.0')
1377
serializer.write(tree.branch.repository, ['rev1', 'rev2'], {}, s)
1379
tree2 = self.make_branch_and_tree('target')
1380
target_repo = tree2.branch.repository
1381
install_bundle(target_repo, serializer.read(s))
1382
target_repo.lock_read()
1383
self.addCleanup(target_repo.unlock)
1384
# Turn the 'iterators_of_bytes' back into simple strings for comparison
1385
repo_texts = dict((i, ''.join(content)) for i, content
1386
in target_repo.iter_files_bytes(
1387
[('fileid-2', 'rev1', '1'),
1388
('fileid-2', 'rev2', '2')]))
1389
self.assertEqual({'1':'contents1\nstatic\n',
1390
'2':'contents2\nstatic\n'},
1392
rtree = target_repo.revision_tree('rev2')
1393
inventory_vf = target_repo.inventories
1394
# If the inventory store has a graph, it must match the revision graph.
1396
[inventory_vf.get_parent_map([('rev2',)])[('rev2',)]],
1397
[None, (('rev1',),)])
1398
self.assertEqual('changed file',
1399
target_repo.get_revision('rev2').message)
1402
def get_raw(bundle_file):
1404
line = bundle_file.readline()
1405
line = bundle_file.readline()
1406
lines = bundle_file.readlines()
1407
return ''.join(lines).decode('bz2')
1409
def test_copy_signatures(self):
1410
tree_a = self.make_branch_and_tree('tree_a')
1412
import bzrlib.commit as commit
1413
oldstrategy = bzrlib.gpg.GPGStrategy
1414
branch = tree_a.branch
1415
repo_a = branch.repository
1416
tree_a.commit("base", allow_pointless=True, rev_id='A')
1417
self.assertFalse(branch.repository.has_signature_for_revision_id('A'))
1419
from bzrlib.testament import Testament
1420
# monkey patch gpg signing mechanism
1421
bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
1422
new_config = test_commit.MustSignConfig(branch)
1423
commit.Commit(config=new_config).commit(message="base",
1424
allow_pointless=True,
1426
working_tree=tree_a)
1428
return bzrlib.gpg.LoopbackGPGStrategy(None).sign(text)
1429
self.assertTrue(repo_a.has_signature_for_revision_id('B'))
1431
bzrlib.gpg.GPGStrategy = oldstrategy
1432
tree_b = self.make_branch_and_tree('tree_b')
1433
repo_b = tree_b.branch.repository
1435
serializer = BundleSerializerV4('4')
1436
serializer.write(tree_a.branch.repository, ['A', 'B'], {}, s)
1438
install_bundle(repo_b, serializer.read(s))
1439
self.assertTrue(repo_b.has_signature_for_revision_id('B'))
1440
self.assertEqual(repo_b.get_signature_text('B'),
1441
repo_a.get_signature_text('B'))
1443
# ensure repeat installs are harmless
1444
install_bundle(repo_b, serializer.read(s))
1447
class V4_2aBundleTester(V4BundleTester):
1449
def bzrdir_format(self):
1452
def get_invalid_bundle(self, base_rev_id, rev_id):
1453
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1454
Munge the text so that it's invalid.
1456
:return: The in-memory bundle
1458
from bzrlib.bundle import serializer
1459
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1460
new_text = self.get_raw(StringIO(''.join(bundle_txt)))
1461
# We are going to be replacing some text to set the executable bit on a
1462
# file. Make sure the text replacement actually works correctly.
1463
self.assertContainsRe(new_text, '(?m)B244\n\ni 1\n<inventory')
1464
new_text = new_text.replace('<file file_id="exe-1"',
1465
'<file executable="y" file_id="exe-1"')
1466
new_text = new_text.replace('B244', 'B259')
1467
bundle_txt = StringIO()
1468
bundle_txt.write(serializer._get_bundle_header('4'))
1469
bundle_txt.write('\n')
1470
bundle_txt.write(new_text.encode('bz2'))
1472
bundle = read_bundle(bundle_txt)
1473
self.valid_apply_bundle(base_rev_id, bundle)
1476
def make_merged_branch(self):
1477
builder = self.make_branch_builder('source')
1478
builder.start_series()
1479
builder.build_snapshot('a@cset-0-1', None, [
1480
('add', ('', 'root-id', 'directory', None)),
1481
('add', ('file', 'file-id', 'file', 'original content\n')),
1483
builder.build_snapshot('a@cset-0-2a', ['a@cset-0-1'], [
1484
('modify', ('file-id', 'new-content\n')),
1486
builder.build_snapshot('a@cset-0-2b', ['a@cset-0-1'], [
1487
('add', ('other-file', 'file2-id', 'file', 'file2-content\n')),
1489
builder.build_snapshot('a@cset-0-3', ['a@cset-0-2a', 'a@cset-0-2b'], [
1490
('add', ('other-file', 'file2-id', 'file', 'file2-content\n')),
1492
builder.finish_series()
1493
self.b1 = builder.get_branch()
1495
self.addCleanup(self.b1.unlock)
1497
def make_bundle_just_inventories(self, base_revision_id,
1501
writer = v4.BundleWriteOperation(base_revision_id, target_revision_id,
1502
self.b1.repository, sio)
1503
writer.bundle.begin()
1504
writer._add_inventory_mpdiffs_from_serializer(revision_ids)
1509
def test_single_inventory_multiple_parents_as_xml(self):
1510
self.make_merged_branch()
1511
sio = self.make_bundle_just_inventories('a@cset-0-1', 'a@cset-0-3',
1513
reader = v4.BundleReader(sio, stream_input=False)
1514
records = list(reader.iter_records())
1515
self.assertEqual(1, len(records))
1516
(bytes, metadata, repo_kind, revision_id,
1517
file_id) = records[0]
1518
self.assertIs(None, file_id)
1519
self.assertEqual('a@cset-0-3', revision_id)
1520
self.assertEqual('inventory', repo_kind)
1521
self.assertEqual({'parents': ['a@cset-0-2a', 'a@cset-0-2b'],
1522
'sha1': '09c53b0c4de0895e11a2aacc34fef60a6e70865c',
1523
'storage_kind': 'mpdiff',
1525
# We should have an mpdiff that takes some lines from both parents.
1526
self.assertEqualDiff(
1528
'<inventory format="10" revision_id="a@cset-0-3">\n'
1531
'c 1 3 3 2\n', bytes)
1533
def test_single_inv_no_parents_as_xml(self):
1534
self.make_merged_branch()
1535
sio = self.make_bundle_just_inventories('null:', 'a@cset-0-1',
1537
reader = v4.BundleReader(sio, stream_input=False)
1538
records = list(reader.iter_records())
1539
self.assertEqual(1, len(records))
1540
(bytes, metadata, repo_kind, revision_id,
1541
file_id) = records[0]
1542
self.assertIs(None, file_id)
1543
self.assertEqual('a@cset-0-1', revision_id)
1544
self.assertEqual('inventory', repo_kind)
1545
self.assertEqual({'parents': [],
1546
'sha1': 'a13f42b142d544aac9b085c42595d304150e31a2',
1547
'storage_kind': 'mpdiff',
1549
# We should have an mpdiff that takes some lines from both parents.
1550
self.assertEqualDiff(
1552
'<inventory format="10" revision_id="a@cset-0-1">\n'
1553
'<directory file_id="root-id" name=""'
1554
' revision="a@cset-0-1" />\n'
1555
'<file file_id="file-id" name="file" parent_id="root-id"'
1556
' revision="a@cset-0-1"'
1557
' text_sha1="09c2f8647e14e49e922b955c194102070597c2d1"'
1558
' text_size="17" />\n'
1562
def test_multiple_inventories_as_xml(self):
1563
self.make_merged_branch()
1564
sio = self.make_bundle_just_inventories('a@cset-0-1', 'a@cset-0-3',
1565
['a@cset-0-2a', 'a@cset-0-2b', 'a@cset-0-3'])
1566
reader = v4.BundleReader(sio, stream_input=False)
1567
records = list(reader.iter_records())
1568
self.assertEqual(3, len(records))
1569
revision_ids = [rev_id for b, m, k, rev_id, f in records]
1570
self.assertEqual(['a@cset-0-2a', 'a@cset-0-2b', 'a@cset-0-3'],
1572
metadata_2a = records[0][1]
1573
self.assertEqual({'parents': ['a@cset-0-1'],
1574
'sha1': '1e105886d62d510763e22885eec733b66f5f09bf',
1575
'storage_kind': 'mpdiff',
1577
metadata_2b = records[1][1]
1578
self.assertEqual({'parents': ['a@cset-0-1'],
1579
'sha1': 'f03f12574bdb5ed2204c28636c98a8547544ccd8',
1580
'storage_kind': 'mpdiff',
1582
metadata_3 = records[2][1]
1583
self.assertEqual({'parents': ['a@cset-0-2a', 'a@cset-0-2b'],
1584
'sha1': '09c53b0c4de0895e11a2aacc34fef60a6e70865c',
1585
'storage_kind': 'mpdiff',
1587
bytes_2a = records[0][0]
1588
self.assertEqualDiff(
1590
'<inventory format="10" revision_id="a@cset-0-2a">\n'
1594
'<file file_id="file-id" name="file" parent_id="root-id"'
1595
' revision="a@cset-0-2a"'
1596
' text_sha1="50f545ff40e57b6924b1f3174b267ffc4576e9a9"'
1597
' text_size="12" />\n'
1599
'c 0 3 3 1\n', bytes_2a)
1600
bytes_2b = records[1][0]
1601
self.assertEqualDiff(
1603
'<inventory format="10" revision_id="a@cset-0-2b">\n'
1607
'<file file_id="file2-id" name="other-file" parent_id="root-id"'
1608
' revision="a@cset-0-2b"'
1609
' text_sha1="b46c0c8ea1e5ef8e46fc8894bfd4752a88ec939e"'
1610
' text_size="14" />\n'
1612
'c 0 3 4 1\n', bytes_2b)
1613
bytes_3 = records[2][0]
1614
self.assertEqualDiff(
1616
'<inventory format="10" revision_id="a@cset-0-3">\n'
1619
'c 1 3 3 2\n', bytes_3)
1621
def test_creating_bundle_preserves_chk_pages(self):
1622
self.make_merged_branch()
1623
target = self.b1.bzrdir.sprout('target',
1624
revision_id='a@cset-0-2a').open_branch()
1625
bundle_txt, rev_ids = self.create_bundle_text('a@cset-0-2a',
1627
self.assertEqual(['a@cset-0-2b', 'a@cset-0-3'], rev_ids)
1628
bundle = read_bundle(bundle_txt)
1630
self.addCleanup(target.unlock)
1631
install_bundle(target.repository, bundle)
1632
inv1 = self.b1.repository.inventories.get_record_stream([
1633
('a@cset-0-3',)], 'unordered',
1634
True).next().get_bytes_as('fulltext')
1635
inv2 = target.repository.inventories.get_record_stream([
1636
('a@cset-0-3',)], 'unordered',
1637
True).next().get_bytes_as('fulltext')
1638
self.assertEqualDiff(inv1, inv2)
1641
class MungedBundleTester(object):
905
class MungedBundleTester(TestCaseWithTransport):
1643
907
def build_test_bundle(self):
1644
908
wt = self.make_branch_and_tree('b1')
1727
986
bundle = read_bundle(bundle_txt)
1728
987
self.check_valid(bundle)
1731
class MungedBundleTesterV4(tests.TestCaseWithTransport, MungedBundleTester):
1736
class TestBundleWriterReader(tests.TestCase):
1738
def test_roundtrip_record(self):
1739
fileobj = StringIO()
1740
writer = v4.BundleWriter(fileobj)
1742
writer.add_info_record(foo='bar')
1743
writer._add_record("Record body", {'parents': ['1', '3'],
1744
'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
1747
reader = v4.BundleReader(fileobj, stream_input=True)
1748
record_iter = reader.iter_records()
1749
record = record_iter.next()
1750
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1751
'info', None, None), record)
1752
record = record_iter.next()
1753
self.assertEqual(("Record body", {'storage_kind': 'fulltext',
1754
'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
1757
def test_roundtrip_record_memory_hungry(self):
1758
fileobj = StringIO()
1759
writer = v4.BundleWriter(fileobj)
1761
writer.add_info_record(foo='bar')
1762
writer._add_record("Record body", {'parents': ['1', '3'],
1763
'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
1766
reader = v4.BundleReader(fileobj, stream_input=False)
1767
record_iter = reader.iter_records()
1768
record = record_iter.next()
1769
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1770
'info', None, None), record)
1771
record = record_iter.next()
1772
self.assertEqual(("Record body", {'storage_kind': 'fulltext',
1773
'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
1776
def test_encode_name(self):
1777
self.assertEqual('revision/rev1',
1778
v4.BundleWriter.encode_name('revision', 'rev1'))
1779
self.assertEqual('file/rev//1/file-id-1',
1780
v4.BundleWriter.encode_name('file', 'rev/1', 'file-id-1'))
1781
self.assertEqual('info',
1782
v4.BundleWriter.encode_name('info', None, None))
1784
def test_decode_name(self):
1785
self.assertEqual(('revision', 'rev1', None),
1786
v4.BundleReader.decode_name('revision/rev1'))
1787
self.assertEqual(('file', 'rev/1', 'file-id-1'),
1788
v4.BundleReader.decode_name('file/rev//1/file-id-1'))
1789
self.assertEqual(('info', None, None),
1790
v4.BundleReader.decode_name('info'))
1792
def test_too_many_names(self):
1793
fileobj = StringIO()
1794
writer = v4.BundleWriter(fileobj)
1796
writer.add_info_record(foo='bar')
1797
writer._container.add_bytes_record('blah', ['two', 'names'])
1800
record_iter = v4.BundleReader(fileobj).iter_records()
1801
record = record_iter.next()
1802
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1803
'info', None, None), record)
1804
self.assertRaises(errors.BadBundle, record_iter.next)
1807
class TestReadMergeableFromUrl(tests.TestCaseWithTransport):
1809
def test_read_mergeable_skips_local(self):
1810
"""A local bundle named like the URL should not be read.
1812
out, wt = test_read_bundle.create_bundle_file(self)
1813
class FooService(object):
1814
"""A directory service that always returns source"""
1816
def look_up(self, name, url):
1818
directories.register('foo:', FooService, 'Testing directory service')
1819
self.addCleanup(directories.remove, 'foo:')
1820
self.build_tree_contents([('./foo:bar', out.getvalue())])
1821
self.assertRaises(errors.NotABundle, read_mergeable_from_url,
1824
def test_infinite_redirects_are_not_a_bundle(self):
1825
"""If a URL causes TooManyRedirections then NotABundle is raised.
1827
from bzrlib.tests.blackbox.test_push import RedirectingMemoryServer
1828
server = RedirectingMemoryServer()
1829
self.start_server(server)
1830
url = server.get_url() + 'infinite-loop'
1831
self.assertRaises(errors.NotABundle, read_mergeable_from_url, url)
1833
def test_smart_server_connection_reset(self):
1834
"""If a smart server connection fails during the attempt to read a
1835
bundle, then the ConnectionReset error should be propagated.
1837
# Instantiate a server that will provoke a ConnectionReset
1838
sock_server = _DisconnectingTCPServer()
1839
self.start_server(sock_server)
1840
# We don't really care what the url is since the server will close the
1841
# connection without interpreting it
1842
url = sock_server.get_url()
1843
self.assertRaises(errors.ConnectionReset, read_mergeable_from_url, url)
1846
class _DisconnectingTCPServer(object):
1847
"""A TCP server that immediately closes any connection made to it."""
1849
def start_server(self):
1850
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1851
self.sock.bind(('127.0.0.1', 0))
1853
self.port = self.sock.getsockname()[1]
1854
self.thread = threading.Thread(
1855
name='%s (port %d)' % (self.__class__.__name__, self.port),
1856
target=self.accept_and_close)
1859
def accept_and_close(self):
1860
conn, addr = self.sock.accept()
1861
conn.shutdown(socket.SHUT_RDWR)
1865
return 'bzr://127.0.0.1:%d/' % (self.port,)
1867
def stop_server(self):
1869
# make sure the thread dies by connecting to the listening socket,
1870
# just in case the test failed to do so.
1871
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1872
conn.connect(self.sock.getsockname())
1874
except socket.error: