915
920
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
916
921
self.b1 = tree.branch
917
922
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
923
bundle = read_bundle(bundle_sio)
924
revision_info = bundle.revisions[0]
925
self.assertEqual('rev1', revision_info.revision_id)
926
rev = revision_info.as_revision()
927
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
930
def test_bundle_sorted_properties(self):
931
"""For stability the writer should write properties in sorted order."""
932
tree = self.make_branch_and_memory_tree('tree')
934
self.addCleanup(tree.unlock)
936
tree.add([''], ['TREE_ROOT'])
937
tree.commit('One', rev_id='rev1',
938
revprops={'a':'4', 'b':'3', 'c':'2', 'd':'1'})
939
self.b1 = tree.branch
940
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
941
bundle = read_bundle(bundle_sio)
942
revision_info = bundle.revisions[0]
943
self.assertEqual('rev1', revision_info.revision_id)
944
rev = revision_info.as_revision()
945
self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
946
'd':'1'}, rev.properties)
948
def test_bundle_unicode_properties(self):
949
"""We should be able to round trip a non-ascii property."""
950
tree = self.make_branch_and_memory_tree('tree')
952
self.addCleanup(tree.unlock)
954
tree.add([''], ['TREE_ROOT'])
955
# Revisions themselves do not require anything about revision property
956
# keys, other than that they are a basestring, and do not contain
958
# However, Testaments assert than they are str(), and thus should not
960
tree.commit('One', rev_id='rev1',
961
revprops={'omega':u'\u03a9', 'alpha':u'\u03b1'})
962
self.b1 = tree.branch
963
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
964
bundle = read_bundle(bundle_sio)
965
revision_info = bundle.revisions[0]
966
self.assertEqual('rev1', revision_info.revision_id)
967
rev = revision_info.as_revision()
968
self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
969
'alpha':u'\u03b1'}, rev.properties)
971
def test_bundle_with_ghosts(self):
972
tree = self.make_branch_and_tree('tree')
973
self.b1 = tree.branch
974
self.build_tree_contents([('tree/file', 'content1')])
977
self.build_tree_contents([('tree/file', 'content2')])
978
tree.add_parent_tree_id('ghost')
979
tree.commit('rev2', rev_id='rev2')
980
bundle = self.get_valid_bundle('null:', 'rev2')
982
def make_simple_tree(self, format=None):
983
tree = self.make_branch_and_tree('b1', format=format)
984
self.b1 = tree.branch
985
self.build_tree(['b1/file'])
989
def test_across_serializers(self):
990
tree = self.make_simple_tree('knit')
991
tree.commit('hello', rev_id='rev1')
992
tree.commit('hello', rev_id='rev2')
993
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
994
repo = self.make_repository('repo', format='dirstate-with-subtree')
995
bundle.install_revisions(repo)
996
inv_text = repo.get_inventory_xml('rev2')
997
self.assertNotContainsRe(inv_text, 'format="5"')
998
self.assertContainsRe(inv_text, 'format="7"')
1000
def test_across_models(self):
1001
tree = self.make_simple_tree('knit')
1002
tree.commit('hello', rev_id='rev1')
1003
tree.commit('hello', rev_id='rev2')
1004
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1005
repo = self.make_repository('repo', format='dirstate-with-subtree')
1006
bundle.install_revisions(repo)
1007
inv = repo.get_inventory('rev2')
1008
self.assertEqual('rev2', inv.root.revision)
1009
root_vf = repo.weave_store.get_weave(inv.root.file_id,
1010
repo.get_transaction())
1011
self.assertEqual(root_vf.versions(), ['rev1', 'rev2'])
1013
def test_across_models_incompatible(self):
1014
tree = self.make_simple_tree('dirstate-with-subtree')
1015
tree.commit('hello', rev_id='rev1')
1016
tree.commit('hello', rev_id='rev2')
1018
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1019
except errors.IncompatibleBundleFormat:
1020
raise TestSkipped("Format 0.8 doesn't work with knit3")
1021
repo = self.make_repository('repo', format='knit')
1022
bundle.install_revisions(repo)
1024
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1025
self.assertRaises(errors.IncompatibleRevision,
1026
bundle.install_revisions, repo)
1028
def test_get_merge_request(self):
1029
tree = self.make_simple_tree()
1030
tree.commit('hello', rev_id='rev1')
1031
tree.commit('hello', rev_id='rev2')
1032
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1033
result = bundle.get_merge_request(tree.branch.repository)
1034
self.assertEqual((None, 'rev1', 'inapplicable'), result)
1036
def test_with_subtree(self):
1037
tree = self.make_branch_and_tree('tree',
1038
format='dirstate-with-subtree')
1039
self.b1 = tree.branch
1040
subtree = self.make_branch_and_tree('tree/subtree',
1041
format='dirstate-with-subtree')
1043
tree.commit('hello', rev_id='rev1')
1045
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1046
except errors.IncompatibleBundleFormat:
1047
raise TestSkipped("Format 0.8 doesn't work with knit3")
1048
if isinstance(bundle, v09.BundleInfo09):
1049
raise TestSkipped("Format 0.9 doesn't work with subtrees")
1050
repo = self.make_repository('repo', format='knit')
1051
self.assertRaises(errors.IncompatibleRevision,
1052
bundle.install_revisions, repo)
1053
repo2 = self.make_repository('repo2', format='dirstate-with-subtree')
1054
bundle.install_revisions(repo2)
1056
def test_revision_id_with_slash(self):
1057
self.tree1 = self.make_branch_and_tree('tree')
1058
self.b1 = self.tree1.branch
1060
self.tree1.commit('Revision/id/with/slashes', rev_id='rev/id')
1062
raise TestSkipped("Repository doesn't support revision ids with"
1064
bundle = self.get_valid_bundle('null:', 'rev/id')
1067
class V08BundleTester(BundleTester, TestCaseWithTransport):
1071
def test_bundle_empty_property(self):
1072
"""Test serializing revision properties with an empty value."""
1073
tree = self.make_branch_and_memory_tree('tree')
1075
self.addCleanup(tree.unlock)
1076
tree.add([''], ['TREE_ROOT'])
1077
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
1078
self.b1 = tree.branch
1079
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
918
1080
self.assertContainsRe(bundle_sio.getvalue(),
919
1081
'# properties:\n'
920
1082
'# branch-nick: tree\n'
1040
class MungedBundleTester(TestCaseWithTransport):
1206
class V4BundleTester(BundleTester, TestCaseWithTransport):
1210
def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
1211
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1212
Make sure that the text generated is valid, and that it
1213
can be applied against the base, and generate the same information.
1215
:return: The in-memory bundle
1217
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1219
# This should also validate the generated bundle
1220
bundle = read_bundle(bundle_txt)
1221
repository = self.b1.repository
1222
for bundle_rev in bundle.real_revisions:
1223
# These really should have already been checked when we read the
1224
# bundle, since it computes the sha1 hash for the revision, which
1225
# only will match if everything is okay, but lets be explicit about
1227
branch_rev = repository.get_revision(bundle_rev.revision_id)
1228
for a in ('inventory_sha1', 'revision_id', 'parent_ids',
1229
'timestamp', 'timezone', 'message', 'committer',
1230
'parent_ids', 'properties'):
1231
self.assertEqual(getattr(branch_rev, a),
1232
getattr(bundle_rev, a))
1233
self.assertEqual(len(branch_rev.parent_ids),
1234
len(bundle_rev.parent_ids))
1235
self.assertEqual(set(rev_ids),
1236
set([r.revision_id for r in bundle.real_revisions]))
1237
self.valid_apply_bundle(base_rev_id, bundle,
1238
checkout_dir=checkout_dir)
1242
def get_invalid_bundle(self, base_rev_id, rev_id):
1243
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1244
Munge the text so that it's invalid.
1246
:return: The in-memory bundle
1248
from bzrlib.bundle import serializer
1249
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1250
new_text = self.get_raw(StringIO(''.join(bundle_txt)))
1251
new_text = new_text.replace('<file file_id="exe-1"',
1252
'<file executable="y" file_id="exe-1"')
1253
new_text = new_text.replace('B372', 'B387')
1254
bundle_txt = StringIO()
1255
bundle_txt.write(serializer._get_bundle_header('4'))
1256
bundle_txt.write('\n')
1257
bundle_txt.write(new_text.encode('bz2'))
1259
bundle = read_bundle(bundle_txt)
1260
self.valid_apply_bundle(base_rev_id, bundle)
1263
def create_bundle_text(self, base_rev_id, rev_id):
1264
bundle_txt = StringIO()
1265
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
1266
bundle_txt, format=self.format)
1268
self.assertEqual(bundle_txt.readline(),
1269
'# Bazaar revision bundle v%s\n' % self.format)
1270
self.assertEqual(bundle_txt.readline(), '#\n')
1271
rev = self.b1.repository.get_revision(rev_id)
1273
return bundle_txt, rev_ids
1275
def get_bundle_tree(self, bundle, revision_id):
1276
repository = self.make_repository('repo')
1277
bundle.install_revisions(repository)
1278
return repository.revision_tree(revision_id)
1280
def test_creation(self):
1281
tree = self.make_branch_and_tree('tree')
1282
self.build_tree_contents([('tree/file', 'contents1\nstatic\n')])
1283
tree.add('file', 'fileid-2')
1284
tree.commit('added file', rev_id='rev1')
1285
self.build_tree_contents([('tree/file', 'contents2\nstatic\n')])
1286
tree.commit('changed file', rev_id='rev2')
1288
serializer = BundleSerializerV4('1.0')
1289
serializer.write(tree.branch.repository, ['rev1', 'rev2'], {}, s)
1291
tree2 = self.make_branch_and_tree('target')
1292
target_repo = tree2.branch.repository
1293
install_bundle(target_repo, serializer.read(s))
1294
vf = target_repo.weave_store.get_weave('fileid-2',
1295
target_repo.get_transaction())
1296
self.assertEqual('contents1\nstatic\n', vf.get_text('rev1'))
1297
self.assertEqual('contents2\nstatic\n', vf.get_text('rev2'))
1298
rtree = target_repo.revision_tree('rev2')
1299
inventory_vf = target_repo.get_inventory_weave()
1300
self.assertEqual(['rev1'], inventory_vf.get_parents('rev2'))
1301
self.assertEqual('changed file',
1302
target_repo.get_revision('rev2').message)
1305
def get_raw(bundle_file):
1307
line = bundle_file.readline()
1308
line = bundle_file.readline()
1309
lines = bundle_file.readlines()
1310
return ''.join(lines).decode('bz2')
1312
def test_copy_signatures(self):
1313
tree_a = self.make_branch_and_tree('tree_a')
1315
import bzrlib.commit as commit
1316
oldstrategy = bzrlib.gpg.GPGStrategy
1317
branch = tree_a.branch
1318
repo_a = branch.repository
1319
tree_a.commit("base", allow_pointless=True, rev_id='A')
1320
self.failIf(branch.repository.has_signature_for_revision_id('A'))
1322
from bzrlib.testament import Testament
1323
# monkey patch gpg signing mechanism
1324
bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
1325
new_config = test_commit.MustSignConfig(branch)
1326
commit.Commit(config=new_config).commit(message="base",
1327
allow_pointless=True,
1329
working_tree=tree_a)
1331
return bzrlib.gpg.LoopbackGPGStrategy(None).sign(text)
1332
self.assertTrue(repo_a.has_signature_for_revision_id('B'))
1334
bzrlib.gpg.GPGStrategy = oldstrategy
1335
tree_b = self.make_branch_and_tree('tree_b')
1336
repo_b = tree_b.branch.repository
1338
serializer = BundleSerializerV4('4')
1339
serializer.write(tree_a.branch.repository, ['A', 'B'], {}, s)
1341
install_bundle(repo_b, serializer.read(s))
1342
self.assertTrue(repo_b.has_signature_for_revision_id('B'))
1343
self.assertEqual(repo_b.get_signature_text('B'),
1344
repo_a.get_signature_text('B'))
1346
# ensure repeat installs are harmless
1347
install_bundle(repo_b, serializer.read(s))
1350
class V4WeaveBundleTester(V4BundleTester):
1352
def bzrdir_format(self):
1356
class MungedBundleTester(object):
1042
1358
def build_test_bundle(self):
1043
1359
wt = self.make_branch_and_tree('b1')
1121
1442
bundle = read_bundle(bundle_txt)
1122
1443
self.check_valid(bundle)
1446
class MungedBundleTesterV4(TestCaseWithTransport, MungedBundleTester):
1451
class TestBundleWriterReader(TestCase):
1453
def test_roundtrip_record(self):
1454
fileobj = StringIO()
1455
writer = v4.BundleWriter(fileobj)
1457
writer.add_info_record(foo='bar')
1458
writer._add_record("Record body", {'parents': ['1', '3'],
1459
'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
1462
record_iter = v4.BundleReader(fileobj).iter_records()
1463
record = record_iter.next()
1464
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1465
'info', None, None), record)
1466
record = record_iter.next()
1467
self.assertEqual(("Record body", {'storage_kind': 'fulltext',
1468
'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
1471
def test_encode_name(self):
1472
self.assertEqual('revision/rev1',
1473
v4.BundleWriter.encode_name('revision', 'rev1'))
1474
self.assertEqual('file/rev//1/file-id-1',
1475
v4.BundleWriter.encode_name('file', 'rev/1', 'file-id-1'))
1476
self.assertEqual('info',
1477
v4.BundleWriter.encode_name('info', None, None))
1479
def test_decode_name(self):
1480
self.assertEqual(('revision', 'rev1', None),
1481
v4.BundleReader.decode_name('revision/rev1'))
1482
self.assertEqual(('file', 'rev/1', 'file-id-1'),
1483
v4.BundleReader.decode_name('file/rev//1/file-id-1'))
1484
self.assertEqual(('info', None, None),
1485
v4.BundleReader.decode_name('info'))
1487
def test_too_many_names(self):
1488
fileobj = StringIO()
1489
writer = v4.BundleWriter(fileobj)
1491
writer.add_info_record(foo='bar')
1492
writer._container.add_bytes_record('blah', ['two', 'names'])
1495
record_iter = v4.BundleReader(fileobj).iter_records()
1496
record = record_iter.next()
1497
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1498
'info', None, None), record)
1499
self.assertRaises(BadBundle, record_iter.next)