913
919
tree.add([''], ['TREE_ROOT'])
914
920
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
915
921
self.b1 = tree.branch
916
bundle_sio, revision_ids = self.create_bundle_text(None, 'rev1')
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')
1066
def test_skip_file(self):
1067
"""Make sure we don't accidentally write to the wrong versionedfile"""
1068
self.tree1 = self.make_branch_and_tree('tree')
1069
self.b1 = self.tree1.branch
1070
# rev1 is not present in bundle, done by fetch
1071
self.build_tree_contents([('tree/file2', 'contents1')])
1072
self.tree1.add('file2', 'file2-id')
1073
self.tree1.commit('rev1', rev_id='reva')
1074
self.build_tree_contents([('tree/file3', 'contents2')])
1075
# rev2 is present in bundle, and done by fetch
1076
# having file1 in the bunle causes file1's versionedfile to be opened.
1077
self.tree1.add('file3', 'file3-id')
1078
self.tree1.commit('rev2')
1079
# Updating file2 should not cause an attempt to add to file1's vf
1080
target = self.tree1.bzrdir.sprout('target').open_workingtree()
1081
self.build_tree_contents([('tree/file2', 'contents3')])
1082
self.tree1.commit('rev3', rev_id='rev3')
1083
bundle = self.get_valid_bundle('reva', 'rev3')
1084
if getattr(bundle, 'get_bundle_reader', None) is None:
1085
raise TestSkipped('Bundle format cannot provide reader')
1086
# be sure that file1 comes before file2
1087
for b, m, k, r, f in bundle.get_bundle_reader().iter_records():
1090
self.assertNotEqual(f, 'file2-id')
1091
bundle.install_revisions(target.branch.repository)
1094
class V08BundleTester(BundleTester, TestCaseWithTransport):
1098
def test_bundle_empty_property(self):
1099
"""Test serializing revision properties with an empty value."""
1100
tree = self.make_branch_and_memory_tree('tree')
1102
self.addCleanup(tree.unlock)
1103
tree.add([''], ['TREE_ROOT'])
1104
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
1105
self.b1 = tree.branch
1106
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
917
1107
self.assertContainsRe(bundle_sio.getvalue(),
918
1108
'# properties:\n'
919
1109
'# branch-nick: tree\n'
1039
class MungedBundleTester(TestCaseWithTransport):
1233
class V4BundleTester(BundleTester, TestCaseWithTransport):
1237
def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
1238
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1239
Make sure that the text generated is valid, and that it
1240
can be applied against the base, and generate the same information.
1242
:return: The in-memory bundle
1244
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1246
# This should also validate the generated bundle
1247
bundle = read_bundle(bundle_txt)
1248
repository = self.b1.repository
1249
for bundle_rev in bundle.real_revisions:
1250
# These really should have already been checked when we read the
1251
# bundle, since it computes the sha1 hash for the revision, which
1252
# only will match if everything is okay, but lets be explicit about
1254
branch_rev = repository.get_revision(bundle_rev.revision_id)
1255
for a in ('inventory_sha1', 'revision_id', 'parent_ids',
1256
'timestamp', 'timezone', 'message', 'committer',
1257
'parent_ids', 'properties'):
1258
self.assertEqual(getattr(branch_rev, a),
1259
getattr(bundle_rev, a))
1260
self.assertEqual(len(branch_rev.parent_ids),
1261
len(bundle_rev.parent_ids))
1262
self.assertEqual(set(rev_ids),
1263
set([r.revision_id for r in bundle.real_revisions]))
1264
self.valid_apply_bundle(base_rev_id, bundle,
1265
checkout_dir=checkout_dir)
1269
def get_invalid_bundle(self, base_rev_id, rev_id):
1270
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1271
Munge the text so that it's invalid.
1273
:return: The in-memory bundle
1275
from bzrlib.bundle import serializer
1276
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1277
new_text = self.get_raw(StringIO(''.join(bundle_txt)))
1278
new_text = new_text.replace('<file file_id="exe-1"',
1279
'<file executable="y" file_id="exe-1"')
1280
new_text = new_text.replace('B222', 'B237')
1281
bundle_txt = StringIO()
1282
bundle_txt.write(serializer._get_bundle_header('4'))
1283
bundle_txt.write('\n')
1284
bundle_txt.write(new_text.encode('bz2'))
1286
bundle = read_bundle(bundle_txt)
1287
self.valid_apply_bundle(base_rev_id, bundle)
1290
def create_bundle_text(self, base_rev_id, rev_id):
1291
bundle_txt = StringIO()
1292
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
1293
bundle_txt, format=self.format)
1295
self.assertEqual(bundle_txt.readline(),
1296
'# Bazaar revision bundle v%s\n' % self.format)
1297
self.assertEqual(bundle_txt.readline(), '#\n')
1298
rev = self.b1.repository.get_revision(rev_id)
1300
return bundle_txt, rev_ids
1302
def get_bundle_tree(self, bundle, revision_id):
1303
repository = self.make_repository('repo')
1304
bundle.install_revisions(repository)
1305
return repository.revision_tree(revision_id)
1307
def test_creation(self):
1308
tree = self.make_branch_and_tree('tree')
1309
self.build_tree_contents([('tree/file', 'contents1\nstatic\n')])
1310
tree.add('file', 'fileid-2')
1311
tree.commit('added file', rev_id='rev1')
1312
self.build_tree_contents([('tree/file', 'contents2\nstatic\n')])
1313
tree.commit('changed file', rev_id='rev2')
1315
serializer = BundleSerializerV4('1.0')
1316
serializer.write(tree.branch.repository, ['rev1', 'rev2'], {}, s)
1318
tree2 = self.make_branch_and_tree('target')
1319
target_repo = tree2.branch.repository
1320
install_bundle(target_repo, serializer.read(s))
1321
vf = target_repo.weave_store.get_weave('fileid-2',
1322
target_repo.get_transaction())
1323
self.assertEqual('contents1\nstatic\n', vf.get_text('rev1'))
1324
self.assertEqual('contents2\nstatic\n', vf.get_text('rev2'))
1325
rtree = target_repo.revision_tree('rev2')
1326
inventory_vf = target_repo.get_inventory_weave()
1327
self.assertEqual(['rev1'], inventory_vf.get_parents('rev2'))
1328
self.assertEqual('changed file',
1329
target_repo.get_revision('rev2').message)
1332
def get_raw(bundle_file):
1334
line = bundle_file.readline()
1335
line = bundle_file.readline()
1336
lines = bundle_file.readlines()
1337
return ''.join(lines).decode('bz2')
1339
def test_copy_signatures(self):
1340
tree_a = self.make_branch_and_tree('tree_a')
1342
import bzrlib.commit as commit
1343
oldstrategy = bzrlib.gpg.GPGStrategy
1344
branch = tree_a.branch
1345
repo_a = branch.repository
1346
tree_a.commit("base", allow_pointless=True, rev_id='A')
1347
self.failIf(branch.repository.has_signature_for_revision_id('A'))
1349
from bzrlib.testament import Testament
1350
# monkey patch gpg signing mechanism
1351
bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
1352
new_config = test_commit.MustSignConfig(branch)
1353
commit.Commit(config=new_config).commit(message="base",
1354
allow_pointless=True,
1356
working_tree=tree_a)
1358
return bzrlib.gpg.LoopbackGPGStrategy(None).sign(text)
1359
self.assertTrue(repo_a.has_signature_for_revision_id('B'))
1361
bzrlib.gpg.GPGStrategy = oldstrategy
1362
tree_b = self.make_branch_and_tree('tree_b')
1363
repo_b = tree_b.branch.repository
1365
serializer = BundleSerializerV4('4')
1366
serializer.write(tree_a.branch.repository, ['A', 'B'], {}, s)
1368
install_bundle(repo_b, serializer.read(s))
1369
self.assertTrue(repo_b.has_signature_for_revision_id('B'))
1370
self.assertEqual(repo_b.get_signature_text('B'),
1371
repo_a.get_signature_text('B'))
1373
# ensure repeat installs are harmless
1374
install_bundle(repo_b, serializer.read(s))
1377
class V4WeaveBundleTester(V4BundleTester):
1379
def bzrdir_format(self):
1383
class MungedBundleTester(object):
1041
1385
def build_test_bundle(self):
1042
1386
wt = self.make_branch_and_tree('b1')
1120
1469
bundle = read_bundle(bundle_txt)
1121
1470
self.check_valid(bundle)
1473
class MungedBundleTesterV4(TestCaseWithTransport, MungedBundleTester):
1478
class TestBundleWriterReader(TestCase):
1480
def test_roundtrip_record(self):
1481
fileobj = StringIO()
1482
writer = v4.BundleWriter(fileobj)
1484
writer.add_info_record(foo='bar')
1485
writer._add_record("Record body", {'parents': ['1', '3'],
1486
'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
1489
reader = v4.BundleReader(fileobj, stream_input=True)
1490
record_iter = reader.iter_records()
1491
record = record_iter.next()
1492
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1493
'info', None, None), record)
1494
record = record_iter.next()
1495
self.assertEqual(("Record body", {'storage_kind': 'fulltext',
1496
'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
1499
def test_roundtrip_record_memory_hungry(self):
1500
fileobj = StringIO()
1501
writer = v4.BundleWriter(fileobj)
1503
writer.add_info_record(foo='bar')
1504
writer._add_record("Record body", {'parents': ['1', '3'],
1505
'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
1508
reader = v4.BundleReader(fileobj, stream_input=False)
1509
record_iter = reader.iter_records()
1510
record = record_iter.next()
1511
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1512
'info', None, None), record)
1513
record = record_iter.next()
1514
self.assertEqual(("Record body", {'storage_kind': 'fulltext',
1515
'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
1518
def test_encode_name(self):
1519
self.assertEqual('revision/rev1',
1520
v4.BundleWriter.encode_name('revision', 'rev1'))
1521
self.assertEqual('file/rev//1/file-id-1',
1522
v4.BundleWriter.encode_name('file', 'rev/1', 'file-id-1'))
1523
self.assertEqual('info',
1524
v4.BundleWriter.encode_name('info', None, None))
1526
def test_decode_name(self):
1527
self.assertEqual(('revision', 'rev1', None),
1528
v4.BundleReader.decode_name('revision/rev1'))
1529
self.assertEqual(('file', 'rev/1', 'file-id-1'),
1530
v4.BundleReader.decode_name('file/rev//1/file-id-1'))
1531
self.assertEqual(('info', None, None),
1532
v4.BundleReader.decode_name('info'))
1534
def test_too_many_names(self):
1535
fileobj = StringIO()
1536
writer = v4.BundleWriter(fileobj)
1538
writer.add_info_record(foo='bar')
1539
writer._container.add_bytes_record('blah', ['two', 'names'])
1542
record_iter = v4.BundleReader(fileobj).iter_records()
1543
record = record_iter.next()
1544
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1545
'info', None, None), record)
1546
self.assertRaises(BadBundle, record_iter.next)