915
923
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
916
924
self.b1 = tree.branch
917
925
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
926
bundle = read_bundle(bundle_sio)
927
revision_info = bundle.revisions[0]
928
self.assertEqual('rev1', revision_info.revision_id)
929
rev = revision_info.as_revision()
930
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
933
def test_bundle_sorted_properties(self):
934
"""For stability the writer should write properties in sorted order."""
935
tree = self.make_branch_and_memory_tree('tree')
937
self.addCleanup(tree.unlock)
939
tree.add([''], ['TREE_ROOT'])
940
tree.commit('One', rev_id='rev1',
941
revprops={'a':'4', 'b':'3', 'c':'2', 'd':'1'})
942
self.b1 = tree.branch
943
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
944
bundle = read_bundle(bundle_sio)
945
revision_info = bundle.revisions[0]
946
self.assertEqual('rev1', revision_info.revision_id)
947
rev = revision_info.as_revision()
948
self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
949
'd':'1'}, rev.properties)
951
def test_bundle_unicode_properties(self):
952
"""We should be able to round trip a non-ascii property."""
953
tree = self.make_branch_and_memory_tree('tree')
955
self.addCleanup(tree.unlock)
957
tree.add([''], ['TREE_ROOT'])
958
# Revisions themselves do not require anything about revision property
959
# keys, other than that they are a basestring, and do not contain
961
# However, Testaments assert than they are str(), and thus should not
963
tree.commit('One', rev_id='rev1',
964
revprops={'omega':u'\u03a9', 'alpha':u'\u03b1'})
965
self.b1 = tree.branch
966
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
967
bundle = read_bundle(bundle_sio)
968
revision_info = bundle.revisions[0]
969
self.assertEqual('rev1', revision_info.revision_id)
970
rev = revision_info.as_revision()
971
self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
972
'alpha':u'\u03b1'}, rev.properties)
974
def test_bundle_with_ghosts(self):
975
tree = self.make_branch_and_tree('tree')
976
self.b1 = tree.branch
977
self.build_tree_contents([('tree/file', 'content1')])
980
self.build_tree_contents([('tree/file', 'content2')])
981
tree.add_parent_tree_id('ghost')
982
tree.commit('rev2', rev_id='rev2')
983
bundle = self.get_valid_bundle('null:', 'rev2')
985
def make_simple_tree(self, format=None):
986
tree = self.make_branch_and_tree('b1', format=format)
987
self.b1 = tree.branch
988
self.build_tree(['b1/file'])
992
def test_across_serializers(self):
993
tree = self.make_simple_tree('knit')
994
tree.commit('hello', rev_id='rev1')
995
tree.commit('hello', rev_id='rev2')
996
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
997
repo = self.make_repository('repo', format='dirstate-with-subtree')
998
bundle.install_revisions(repo)
999
inv_text = repo.get_inventory_xml('rev2')
1000
self.assertNotContainsRe(inv_text, 'format="5"')
1001
self.assertContainsRe(inv_text, 'format="7"')
1003
def test_across_models(self):
1004
tree = self.make_simple_tree('knit')
1005
tree.commit('hello', rev_id='rev1')
1006
tree.commit('hello', rev_id='rev2')
1007
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1008
repo = self.make_repository('repo', format='dirstate-with-subtree')
1009
bundle.install_revisions(repo)
1010
inv = repo.get_inventory('rev2')
1011
self.assertEqual('rev2', inv.root.revision)
1012
root_vf = repo.weave_store.get_weave(inv.root.file_id,
1013
repo.get_transaction())
1014
self.assertEqual(root_vf.versions(), ['rev1', 'rev2'])
1016
def test_across_models_incompatible(self):
1017
tree = self.make_simple_tree('dirstate-with-subtree')
1018
tree.commit('hello', rev_id='rev1')
1019
tree.commit('hello', rev_id='rev2')
1021
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1022
except errors.IncompatibleBundleFormat:
1023
raise TestSkipped("Format 0.8 doesn't work with knit3")
1024
repo = self.make_repository('repo', format='knit')
1025
bundle.install_revisions(repo)
1027
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1028
self.assertRaises(errors.IncompatibleRevision,
1029
bundle.install_revisions, repo)
1031
def test_get_merge_request(self):
1032
tree = self.make_simple_tree()
1033
tree.commit('hello', rev_id='rev1')
1034
tree.commit('hello', rev_id='rev2')
1035
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1036
result = bundle.get_merge_request(tree.branch.repository)
1037
self.assertEqual((None, 'rev1', 'inapplicable'), result)
1039
def test_with_subtree(self):
1040
tree = self.make_branch_and_tree('tree',
1041
format='dirstate-with-subtree')
1042
self.b1 = tree.branch
1043
subtree = self.make_branch_and_tree('tree/subtree',
1044
format='dirstate-with-subtree')
1046
tree.commit('hello', rev_id='rev1')
1048
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1049
except errors.IncompatibleBundleFormat:
1050
raise TestSkipped("Format 0.8 doesn't work with knit3")
1051
if isinstance(bundle, v09.BundleInfo09):
1052
raise TestSkipped("Format 0.9 doesn't work with subtrees")
1053
repo = self.make_repository('repo', format='knit')
1054
self.assertRaises(errors.IncompatibleRevision,
1055
bundle.install_revisions, repo)
1056
repo2 = self.make_repository('repo2', format='dirstate-with-subtree')
1057
bundle.install_revisions(repo2)
1059
def test_revision_id_with_slash(self):
1060
self.tree1 = self.make_branch_and_tree('tree')
1061
self.b1 = self.tree1.branch
1063
self.tree1.commit('Revision/id/with/slashes', rev_id='rev/id')
1065
raise TestSkipped("Repository doesn't support revision ids with"
1067
bundle = self.get_valid_bundle('null:', 'rev/id')
1070
class V08BundleTester(BundleTester, TestCaseWithTransport):
1074
def test_bundle_empty_property(self):
1075
"""Test serializing revision properties with an empty value."""
1076
tree = self.make_branch_and_memory_tree('tree')
1078
self.addCleanup(tree.unlock)
1079
tree.add([''], ['TREE_ROOT'])
1080
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
1081
self.b1 = tree.branch
1082
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
918
1083
self.assertContainsRe(bundle_sio.getvalue(),
919
1084
'# properties:\n'
920
1085
'# branch-nick: tree\n'
1040
class MungedBundleTester(TestCaseWithTransport):
1209
class V4BundleTester(BundleTester, TestCaseWithTransport):
1213
def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
1214
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1215
Make sure that the text generated is valid, and that it
1216
can be applied against the base, and generate the same information.
1218
:return: The in-memory bundle
1220
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1222
# This should also validate the generated bundle
1223
bundle = read_bundle(bundle_txt)
1224
repository = self.b1.repository
1225
for bundle_rev in bundle.real_revisions:
1226
# These really should have already been checked when we read the
1227
# bundle, since it computes the sha1 hash for the revision, which
1228
# only will match if everything is okay, but lets be explicit about
1230
branch_rev = repository.get_revision(bundle_rev.revision_id)
1231
for a in ('inventory_sha1', 'revision_id', 'parent_ids',
1232
'timestamp', 'timezone', 'message', 'committer',
1233
'parent_ids', 'properties'):
1234
self.assertEqual(getattr(branch_rev, a),
1235
getattr(bundle_rev, a))
1236
self.assertEqual(len(branch_rev.parent_ids),
1237
len(bundle_rev.parent_ids))
1238
self.assertEqual(set(rev_ids),
1239
set([r.revision_id for r in bundle.real_revisions]))
1240
self.valid_apply_bundle(base_rev_id, bundle,
1241
checkout_dir=checkout_dir)
1245
def get_invalid_bundle(self, base_rev_id, rev_id):
1246
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1247
Munge the text so that it's invalid.
1249
:return: The in-memory bundle
1251
from bzrlib.bundle import serializer
1252
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1253
new_text = self.get_raw(StringIO(''.join(bundle_txt)))
1254
new_text = new_text.replace('<file file_id="exe-1"',
1255
'<file executable="y" file_id="exe-1"')
1256
new_text = new_text.replace('B372', 'B387')
1257
bundle_txt = StringIO()
1258
bundle_txt.write(serializer._get_bundle_header('4alpha'))
1259
bundle_txt.write('\n')
1260
bundle_txt.write(new_text.encode('bz2'))
1262
bundle = read_bundle(bundle_txt)
1263
self.valid_apply_bundle(base_rev_id, bundle)
1266
def create_bundle_text(self, base_rev_id, rev_id):
1267
bundle_txt = StringIO()
1268
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
1269
bundle_txt, format=self.format)
1271
self.assertEqual(bundle_txt.readline(),
1272
'# Bazaar revision bundle v%s\n' % self.format)
1273
self.assertEqual(bundle_txt.readline(), '#\n')
1274
rev = self.b1.repository.get_revision(rev_id)
1276
return bundle_txt, rev_ids
1278
def get_bundle_tree(self, bundle, revision_id):
1279
repository = self.make_repository('repo')
1280
bundle.install_revisions(repository)
1281
return repository.revision_tree(revision_id)
1283
def test_creation(self):
1284
tree = self.make_branch_and_tree('tree')
1285
self.build_tree_contents([('tree/file', 'contents1\nstatic\n')])
1286
tree.add('file', 'fileid-2')
1287
tree.commit('added file', rev_id='rev1')
1288
self.build_tree_contents([('tree/file', 'contents2\nstatic\n')])
1289
tree.commit('changed file', rev_id='rev2')
1291
serializer = BundleSerializerV4('1.0')
1292
serializer.write(tree.branch.repository, ['rev1', 'rev2'], {}, s)
1294
tree2 = self.make_branch_and_tree('target')
1295
target_repo = tree2.branch.repository
1296
install_bundle(target_repo, serializer.read(s))
1297
vf = target_repo.weave_store.get_weave('fileid-2',
1298
target_repo.get_transaction())
1299
self.assertEqual('contents1\nstatic\n', vf.get_text('rev1'))
1300
self.assertEqual('contents2\nstatic\n', vf.get_text('rev2'))
1301
rtree = target_repo.revision_tree('rev2')
1302
inventory_vf = target_repo.get_inventory_weave()
1303
self.assertEqual(['rev1'], inventory_vf.get_parents('rev2'))
1304
self.assertEqual('changed file',
1305
target_repo.get_revision('rev2').message)
1308
def get_raw(bundle_file):
1310
line = bundle_file.readline()
1311
line = bundle_file.readline()
1312
lines = bundle_file.readlines()
1313
return ''.join(lines).decode('bz2')
1315
def test_copy_signatures(self):
1316
tree_a = self.make_branch_and_tree('tree_a')
1318
import bzrlib.commit as commit
1319
oldstrategy = bzrlib.gpg.GPGStrategy
1320
branch = tree_a.branch
1321
repo_a = branch.repository
1322
tree_a.commit("base", allow_pointless=True, rev_id='A')
1323
self.failIf(branch.repository.has_signature_for_revision_id('A'))
1325
from bzrlib.testament import Testament
1326
# monkey patch gpg signing mechanism
1327
bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
1328
new_config = test_commit.MustSignConfig(branch)
1329
commit.Commit(config=new_config).commit(message="base",
1330
allow_pointless=True,
1332
working_tree=tree_a)
1334
return bzrlib.gpg.LoopbackGPGStrategy(None).sign(text)
1335
self.assertTrue(repo_a.has_signature_for_revision_id('B'))
1337
bzrlib.gpg.GPGStrategy = oldstrategy
1338
tree_b = self.make_branch_and_tree('tree_b')
1339
repo_b = tree_b.branch.repository
1341
serializer = BundleSerializerV4('4alpha')
1342
serializer.write(tree_a.branch.repository, ['A', 'B'], {}, s)
1344
install_bundle(repo_b, serializer.read(s))
1345
self.assertTrue(repo_b.has_signature_for_revision_id('B'))
1346
self.assertEqual(repo_b.get_signature_text('B'),
1347
repo_a.get_signature_text('B'))
1349
# ensure repeat installs are harmless
1350
install_bundle(repo_b, serializer.read(s))
1353
class V4WeaveBundleTester(V4BundleTester):
1355
def bzrdir_format(self):
1359
class MungedBundleTester(object):
1042
1361
def build_test_bundle(self):
1043
1362
wt = self.make_branch_and_tree('b1')
1121
1445
bundle = read_bundle(bundle_txt)
1122
1446
self.check_valid(bundle)
1449
class MungedBundleTesterV4(TestCaseWithTransport, MungedBundleTester):
1454
class TestBundleWriterReader(TestCase):
1456
def test_roundtrip_record(self):
1457
fileobj = StringIO()
1458
writer = v4.BundleWriter(fileobj)
1460
writer.add_info_record(foo='bar')
1461
writer._add_record("Record body", {'parents': ['1', '3'],
1462
'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
1465
record_iter = v4.BundleReader(fileobj).iter_records()
1466
record = record_iter.next()
1467
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1468
'info', None, None), record)
1469
record = record_iter.next()
1470
self.assertEqual(("Record body", {'storage_kind': 'fulltext',
1471
'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
1474
def test_encode_name(self):
1475
self.assertEqual('revision/rev1',
1476
v4.BundleWriter.encode_name('revision', 'rev1'))
1477
self.assertEqual('file/rev//1/file-id-1',
1478
v4.BundleWriter.encode_name('file', 'rev/1', 'file-id-1'))
1479
self.assertEqual('info',
1480
v4.BundleWriter.encode_name('info', None, None))
1482
def test_decode_name(self):
1483
self.assertEqual(('revision', 'rev1', None),
1484
v4.BundleReader.decode_name('revision/rev1'))
1485
self.assertEqual(('file', 'rev/1', 'file-id-1'),
1486
v4.BundleReader.decode_name('file/rev//1/file-id-1'))
1487
self.assertEqual(('info', None, None),
1488
v4.BundleReader.decode_name('info'))
1490
def test_too_many_names(self):
1491
fileobj = StringIO()
1492
writer = v4.BundleWriter(fileobj)
1494
writer.add_info_record(foo='bar')
1495
writer._container.add_bytes_record('blah', ['two', 'names'])
1498
record_iter = v4.BundleReader(fileobj).iter_records()
1499
record = record_iter.next()
1500
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1501
'info', None, None), record)
1502
self.assertRaises(BadBundle, record_iter.next)