871
780
bundle = self.get_valid_bundle('white-3', 'white-4')
873
782
# Now test a complet roll-up
874
bundle = self.get_valid_bundle('null:', 'white-4')
783
bundle = self.get_valid_bundle(None, 'white-4')
876
785
def test_alt_timezone_bundle(self):
877
self.tree1 = self.make_branch_and_memory_tree('b1')
786
self.tree1 = self.make_branch_and_tree('b1')
878
787
self.b1 = self.tree1.branch
879
builder = treebuilder.TreeBuilder()
881
self.tree1.lock_write()
882
builder.start_tree(self.tree1)
883
builder.build(['newfile'])
884
builder.finish_tree()
789
self.build_tree(['b1/newfile'])
790
self.tree1.add(['newfile'])
886
792
# Asia/Colombo offset = 5 hours 30 minutes
887
793
self.tree1.commit('non-hour offset timezone', rev_id='tz-1',
888
794
timezone=19800, timestamp=1152544886.0)
890
bundle = self.get_valid_bundle('null:', 'tz-1')
796
bundle = self.get_valid_bundle(None, 'tz-1')
892
798
rev = bundle.revisions[0]
893
799
self.assertEqual('Mon 2006-07-10 20:51:26.000000000 +0530', rev.date)
894
800
self.assertEqual(19800, rev.timezone)
895
801
self.assertEqual(1152544886.0, rev.timestamp)
898
803
def test_bundle_root_id(self):
899
804
self.tree1 = self.make_branch_and_tree('b1')
900
805
self.b1 = self.tree1.branch
901
806
self.tree1.commit('message', rev_id='revid1')
902
bundle = self.get_valid_bundle('null:', 'revid1')
903
tree = self.get_bundle_tree(bundle, 'revid1')
807
bundle = self.get_valid_bundle(None, 'revid1')
808
tree = bundle.revision_tree(self.b1.repository, 'revid1')
904
809
self.assertEqual('revid1', tree.inventory.root.revision)
906
def test_install_revisions(self):
907
self.tree1 = self.make_branch_and_tree('b1')
908
self.b1 = self.tree1.branch
909
self.tree1.commit('message', rev_id='rev2a')
910
bundle = self.get_valid_bundle('null:', 'rev2a')
911
branch2 = self.make_branch('b2')
912
self.assertFalse(branch2.repository.has_revision('rev2a'))
913
target_revision = bundle.install_revisions(branch2.repository)
914
self.assertTrue(branch2.repository.has_revision('rev2a'))
915
self.assertEqual('rev2a', target_revision)
917
def test_bundle_empty_property(self):
918
"""Test serializing revision properties with an empty value."""
919
tree = self.make_branch_and_memory_tree('tree')
921
self.addCleanup(tree.unlock)
922
tree.add([''], ['TREE_ROOT'])
923
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
924
self.b1 = tree.branch
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')
1069
def test_skip_file(self):
1070
"""Make sure we don't accidentally write to the wrong versionedfile"""
1071
self.tree1 = self.make_branch_and_tree('tree')
1072
self.b1 = self.tree1.branch
1073
# rev1 is not present in bundle, done by fetch
1074
self.build_tree_contents([('tree/file2', 'contents1')])
1075
self.tree1.add('file2', 'file2-id')
1076
self.tree1.commit('rev1', rev_id='reva')
1077
self.build_tree_contents([('tree/file3', 'contents2')])
1078
# rev2 is present in bundle, and done by fetch
1079
# having file1 in the bunle causes file1's versionedfile to be opened.
1080
self.tree1.add('file3', 'file3-id')
1081
self.tree1.commit('rev2')
1082
# Updating file2 should not cause an attempt to add to file1's vf
1083
target = self.tree1.bzrdir.sprout('target').open_workingtree()
1084
self.build_tree_contents([('tree/file2', 'contents3')])
1085
self.tree1.commit('rev3', rev_id='rev3')
1086
bundle = self.get_valid_bundle('reva', 'rev3')
1087
if getattr(bundle, 'get_bundle_reader', None) is None:
1088
raise TestSkipped('Bundle format cannot provide reader')
1089
# be sure that file1 comes before file2
1090
for b, m, k, r, f in bundle.get_bundle_reader().iter_records():
1093
self.assertNotEqual(f, 'file2-id')
1094
bundle.install_revisions(target.branch.repository)
1097
class V08BundleTester(BundleTester, TestCaseWithTransport):
1101
def test_bundle_empty_property(self):
1102
"""Test serializing revision properties with an empty value."""
1103
tree = self.make_branch_and_memory_tree('tree')
1105
self.addCleanup(tree.unlock)
1106
tree.add([''], ['TREE_ROOT'])
1107
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
1108
self.b1 = tree.branch
1109
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1110
self.assertContainsRe(bundle_sio.getvalue(),
1112
'# branch-nick: tree\n'
1116
bundle = read_bundle(bundle_sio)
1117
revision_info = bundle.revisions[0]
1118
self.assertEqual('rev1', revision_info.revision_id)
1119
rev = revision_info.as_revision()
1120
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
1123
def get_bundle_tree(self, bundle, revision_id):
1124
repository = self.make_repository('repo')
1125
return bundle.revision_tree(repository, 'revid1')
1127
def test_bundle_empty_property_alt(self):
1128
"""Test serializing revision properties with an empty value.
1130
Older readers had a bug when reading an empty property.
1131
They assumed that all keys ended in ': \n'. However they would write an
1132
empty value as ':\n'. This tests make sure that all newer bzr versions
1133
can handle th second form.
1135
tree = self.make_branch_and_memory_tree('tree')
1137
self.addCleanup(tree.unlock)
1138
tree.add([''], ['TREE_ROOT'])
1139
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
1140
self.b1 = tree.branch
1141
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1142
txt = bundle_sio.getvalue()
1143
loc = txt.find('# empty: ') + len('# empty:')
1144
# Create a new bundle, which strips the trailing space after empty
1145
bundle_sio = StringIO(txt[:loc] + txt[loc+1:])
1147
self.assertContainsRe(bundle_sio.getvalue(),
1149
'# branch-nick: tree\n'
1153
bundle = read_bundle(bundle_sio)
1154
revision_info = bundle.revisions[0]
1155
self.assertEqual('rev1', revision_info.revision_id)
1156
rev = revision_info.as_revision()
1157
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
1160
def test_bundle_sorted_properties(self):
1161
"""For stability the writer should write properties in sorted order."""
1162
tree = self.make_branch_and_memory_tree('tree')
1164
self.addCleanup(tree.unlock)
1166
tree.add([''], ['TREE_ROOT'])
1167
tree.commit('One', rev_id='rev1',
1168
revprops={'a':'4', 'b':'3', 'c':'2', 'd':'1'})
1169
self.b1 = tree.branch
1170
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1171
self.assertContainsRe(bundle_sio.getvalue(),
1175
'# branch-nick: tree\n'
1179
bundle = read_bundle(bundle_sio)
1180
revision_info = bundle.revisions[0]
1181
self.assertEqual('rev1', revision_info.revision_id)
1182
rev = revision_info.as_revision()
1183
self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
1184
'd':'1'}, rev.properties)
1186
def test_bundle_unicode_properties(self):
1187
"""We should be able to round trip a non-ascii property."""
1188
tree = self.make_branch_and_memory_tree('tree')
1190
self.addCleanup(tree.unlock)
1192
tree.add([''], ['TREE_ROOT'])
1193
# Revisions themselves do not require anything about revision property
1194
# keys, other than that they are a basestring, and do not contain
1196
# However, Testaments assert than they are str(), and thus should not
1198
tree.commit('One', rev_id='rev1',
1199
revprops={'omega':u'\u03a9', 'alpha':u'\u03b1'})
1200
self.b1 = tree.branch
1201
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1202
self.assertContainsRe(bundle_sio.getvalue(),
1204
'# alpha: \xce\xb1\n'
1205
'# branch-nick: tree\n'
1206
'# omega: \xce\xa9\n'
1208
bundle = read_bundle(bundle_sio)
1209
revision_info = bundle.revisions[0]
1210
self.assertEqual('rev1', revision_info.revision_id)
1211
rev = revision_info.as_revision()
1212
self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
1213
'alpha':u'\u03b1'}, rev.properties)
1216
class V09BundleKnit2Tester(V08BundleTester):
1220
def bzrdir_format(self):
1221
format = bzrdir.BzrDirMetaFormat1()
1222
format.repository_format = knitrepo.RepositoryFormatKnit3()
1226
class V09BundleKnit1Tester(V08BundleTester):
1230
def bzrdir_format(self):
1231
format = bzrdir.BzrDirMetaFormat1()
1232
format.repository_format = knitrepo.RepositoryFormatKnit1()
1236
class V4BundleTester(BundleTester, TestCaseWithTransport):
1240
def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
1241
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1242
Make sure that the text generated is valid, and that it
1243
can be applied against the base, and generate the same information.
1245
:return: The in-memory bundle
1247
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1249
# This should also validate the generated bundle
1250
bundle = read_bundle(bundle_txt)
1251
repository = self.b1.repository
1252
for bundle_rev in bundle.real_revisions:
1253
# These really should have already been checked when we read the
1254
# bundle, since it computes the sha1 hash for the revision, which
1255
# only will match if everything is okay, but lets be explicit about
1257
branch_rev = repository.get_revision(bundle_rev.revision_id)
1258
for a in ('inventory_sha1', 'revision_id', 'parent_ids',
1259
'timestamp', 'timezone', 'message', 'committer',
1260
'parent_ids', 'properties'):
1261
self.assertEqual(getattr(branch_rev, a),
1262
getattr(bundle_rev, a))
1263
self.assertEqual(len(branch_rev.parent_ids),
1264
len(bundle_rev.parent_ids))
1265
self.assertEqual(set(rev_ids),
1266
set([r.revision_id for r in bundle.real_revisions]))
1267
self.valid_apply_bundle(base_rev_id, bundle,
1268
checkout_dir=checkout_dir)
1272
def get_invalid_bundle(self, base_rev_id, rev_id):
1273
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1274
Munge the text so that it's invalid.
1276
:return: The in-memory bundle
1278
from bzrlib.bundle import serializer
1279
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1280
new_text = self.get_raw(StringIO(''.join(bundle_txt)))
1281
new_text = new_text.replace('<file file_id="exe-1"',
1282
'<file executable="y" file_id="exe-1"')
1283
new_text = new_text.replace('B222', 'B237')
1284
bundle_txt = StringIO()
1285
bundle_txt.write(serializer._get_bundle_header('4'))
1286
bundle_txt.write('\n')
1287
bundle_txt.write(new_text.encode('bz2'))
1289
bundle = read_bundle(bundle_txt)
1290
self.valid_apply_bundle(base_rev_id, bundle)
1293
def create_bundle_text(self, base_rev_id, rev_id):
1294
bundle_txt = StringIO()
1295
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
1296
bundle_txt, format=self.format)
1298
self.assertEqual(bundle_txt.readline(),
1299
'# Bazaar revision bundle v%s\n' % self.format)
1300
self.assertEqual(bundle_txt.readline(), '#\n')
1301
rev = self.b1.repository.get_revision(rev_id)
1303
return bundle_txt, rev_ids
1305
def get_bundle_tree(self, bundle, revision_id):
1306
repository = self.make_repository('repo')
1307
bundle.install_revisions(repository)
1308
return repository.revision_tree(revision_id)
1310
def test_creation(self):
1311
tree = self.make_branch_and_tree('tree')
1312
self.build_tree_contents([('tree/file', 'contents1\nstatic\n')])
1313
tree.add('file', 'fileid-2')
1314
tree.commit('added file', rev_id='rev1')
1315
self.build_tree_contents([('tree/file', 'contents2\nstatic\n')])
1316
tree.commit('changed file', rev_id='rev2')
1318
serializer = BundleSerializerV4('1.0')
1319
serializer.write(tree.branch.repository, ['rev1', 'rev2'], {}, s)
1321
tree2 = self.make_branch_and_tree('target')
1322
target_repo = tree2.branch.repository
1323
install_bundle(target_repo, serializer.read(s))
1324
vf = target_repo.weave_store.get_weave('fileid-2',
1325
target_repo.get_transaction())
1326
self.assertEqual('contents1\nstatic\n', vf.get_text('rev1'))
1327
self.assertEqual('contents2\nstatic\n', vf.get_text('rev2'))
1328
rtree = target_repo.revision_tree('rev2')
1329
inventory_vf = target_repo.get_inventory_weave()
1330
self.assertEqual(['rev1'], inventory_vf.get_parents('rev2'))
1331
self.assertEqual('changed file',
1332
target_repo.get_revision('rev2').message)
1335
def get_raw(bundle_file):
1337
line = bundle_file.readline()
1338
line = bundle_file.readline()
1339
lines = bundle_file.readlines()
1340
return ''.join(lines).decode('bz2')
1342
def test_copy_signatures(self):
1343
tree_a = self.make_branch_and_tree('tree_a')
1345
import bzrlib.commit as commit
1346
oldstrategy = bzrlib.gpg.GPGStrategy
1347
branch = tree_a.branch
1348
repo_a = branch.repository
1349
tree_a.commit("base", allow_pointless=True, rev_id='A')
1350
self.failIf(branch.repository.has_signature_for_revision_id('A'))
1352
from bzrlib.testament import Testament
1353
# monkey patch gpg signing mechanism
1354
bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
1355
new_config = test_commit.MustSignConfig(branch)
1356
commit.Commit(config=new_config).commit(message="base",
1357
allow_pointless=True,
1359
working_tree=tree_a)
1361
return bzrlib.gpg.LoopbackGPGStrategy(None).sign(text)
1362
self.assertTrue(repo_a.has_signature_for_revision_id('B'))
1364
bzrlib.gpg.GPGStrategy = oldstrategy
1365
tree_b = self.make_branch_and_tree('tree_b')
1366
repo_b = tree_b.branch.repository
1368
serializer = BundleSerializerV4('4')
1369
serializer.write(tree_a.branch.repository, ['A', 'B'], {}, s)
1371
install_bundle(repo_b, serializer.read(s))
1372
self.assertTrue(repo_b.has_signature_for_revision_id('B'))
1373
self.assertEqual(repo_b.get_signature_text('B'),
1374
repo_a.get_signature_text('B'))
1376
# ensure repeat installs are harmless
1377
install_bundle(repo_b, serializer.read(s))
1380
class V4WeaveBundleTester(V4BundleTester):
1382
def bzrdir_format(self):
1386
class MungedBundleTester(object):
812
class MungedBundleTester(TestCaseWithTransport):
1388
814
def build_test_bundle(self):
1389
815
wt = self.make_branch_and_tree('b1')