13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
from cStringIO import StringIO
22
22
from bzrlib import (
27
29
revision as _mod_revision,
30
from bzrlib.builtins import _merge_helper
31
from bzrlib.bzrdir import BzrDir
33
from bzrlib.bundle import read_mergeable_from_url
32
34
from bzrlib.bundle.apply_bundle import install_bundle, merge_bundle
33
35
from bzrlib.bundle.bundle_data import BundleTree
36
from bzrlib.directory_service import directories
34
37
from bzrlib.bundle.serializer import write_bundle, read_bundle, v09, v4
35
38
from bzrlib.bundle.serializer.v08 import BundleSerializerV08
36
39
from bzrlib.bundle.serializer.v09 import BundleSerializerV09
37
40
from bzrlib.bundle.serializer.v4 import BundleSerializerV4
38
from bzrlib.branch import Branch
39
from bzrlib.diff import internal_diff
40
from bzrlib.errors import (BzrError, TestamentMismatch, NotABundle, BadBundle,
42
from bzrlib.merge import Merge3Merger
43
41
from bzrlib.repofmt import knitrepo
44
from bzrlib.osutils import has_symlinks, sha_file
45
from bzrlib.tests import (TestCaseInTempDir, TestCaseWithTransport,
46
TestCase, TestSkipped, test_commit)
42
from bzrlib.tests import (
47
48
from bzrlib.transform import TreeTransform
48
from bzrlib.workingtree import WorkingTree
51
def get_text(vf, key):
52
"""Get the fulltext for a given revision id that is present in the vf"""
53
stream = vf.get_record_stream([key], 'unordered', True)
54
record = stream.next()
55
return record.get_bytes_as('fulltext')
58
def get_inventory_text(repo, revision_id):
59
"""Get the fulltext for the inventory at revision id"""
62
return get_text(repo.inventories, (revision_id,))
51
67
class MockTree(object):
52
69
def __init__(self):
53
70
from bzrlib.inventory import InventoryDirectory, ROOT_ID
54
71
object.__init__(self)
205
239
self.assertEqual(btree.path2id("grandparent2/parent2"), "b")
206
240
self.assertEqual(btree.path2id("grandparent2/parent2/file2"), "c")
208
assert btree.path2id("grandparent2/parent2/file") is None
242
self.assertTrue(btree.path2id("grandparent2/parent2/file") is None)
210
244
def test_moves(self):
211
245
"""Ensure that file moves have the proper effect on children"""
212
246
btree = self.make_tree_1()[0]
213
btree.note_rename("grandparent/parent/file",
247
btree.note_rename("grandparent/parent/file",
214
248
"grandparent/alt_parent/file")
215
249
self.assertEqual(btree.id2path("c"), "grandparent/alt_parent/file")
216
250
self.assertEqual(btree.path2id("grandparent/alt_parent/file"), "c")
217
assert btree.path2id("grandparent/parent/file") is None
251
self.assertTrue(btree.path2id("grandparent/parent/file") is None)
219
253
def unified_diff(self, old, new):
221
internal_diff("old", old, "new", new, out)
255
diff.internal_diff("old", old, "new", new, out)
223
257
return out.read()
225
259
def make_tree_2(self):
226
260
btree = self.make_tree_1()[0]
227
btree.note_rename("grandparent/parent/file",
261
btree.note_rename("grandparent/parent/file",
228
262
"grandparent/alt_parent/file")
229
assert btree.id2path("e") is None
230
assert btree.path2id("grandparent/parent/file") is None
263
self.assertTrue(btree.id2path("e") is None)
264
self.assertTrue(btree.path2id("grandparent/parent/file") is None)
231
265
btree.note_id("e", "grandparent/parent/file")
416
451
def get_invalid_bundle(self, base_rev_id, rev_id):
417
452
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
418
453
Munge the text so that it's invalid.
420
455
:return: The in-memory bundle
422
457
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
423
new_text = bundle_txt.getvalue().replace('executable:no',
458
new_text = bundle_txt.getvalue().replace('executable:no',
424
459
'executable:yes')
425
460
bundle_txt = StringIO(new_text)
426
461
bundle = read_bundle(bundle_txt)
427
462
self.valid_apply_bundle(base_rev_id, bundle)
430
465
def test_non_bundle(self):
431
self.assertRaises(NotABundle, read_bundle, StringIO('#!/bin/sh\n'))
466
self.assertRaises(errors.NotABundle,
467
read_bundle, StringIO('#!/bin/sh\n'))
433
469
def test_malformed(self):
434
self.assertRaises(BadBundle, read_bundle,
470
self.assertRaises(errors.BadBundle, read_bundle,
435
471
StringIO('# Bazaar revision bundle v'))
437
473
def test_crlf_bundle(self):
439
475
read_bundle(StringIO('# Bazaar revision bundle v0.8\r\n'))
476
except errors.BadBundle:
441
477
# It is currently permitted for bundles with crlf line endings to
442
478
# make read_bundle raise a BadBundle, but this should be fixed.
443
479
# Anything else, especially NotABundle, is an error.
457
493
ancestors = write_bundle(self.b1.repository, rev_id, 'null:', s,
458
494
format=self.format)
460
assert isinstance(s.getvalue(), str), (
461
"Bundle isn't a bytestring:\n %s..." % repr(s.getvalue())[:40])
496
self.assertIsInstance(s.getvalue(), str)
462
497
install_bundle(tree.branch.repository, read_bundle(s))
463
498
for ancestor in ancestors:
464
499
old = self.b1.repository.revision_tree(ancestor)
465
500
new = tree.branch.repository.revision_tree(ancestor)
467
# Check that there aren't any inventory level changes
468
delta = new.changes_from(old)
469
self.assertFalse(delta.has_changed(),
470
'Revision %s not copied correctly.'
473
# Now check that the file contents are all correct
474
for inventory_id in old:
476
old_file = old.get_file(inventory_id)
481
self.assertEqual(old_file.read(),
482
new.get_file(inventory_id).read())
504
# Check that there aren't any inventory level changes
505
delta = new.changes_from(old)
506
self.assertFalse(delta.has_changed(),
507
'Revision %s not copied correctly.'
510
# Now check that the file contents are all correct
511
for inventory_id in old.all_file_ids():
513
old_file = old.get_file(inventory_id)
514
except errors.NoSuchFile:
518
self.assertEqual(old_file.read(),
519
new.get_file(inventory_id).read())
483
523
if not _mod_revision.is_null(rev_id):
484
rh = self.b1.revision_history()
485
tree.branch.set_revision_history(rh[:rh.index(rev_id)+1])
524
tree.branch.generate_revision_history(rev_id)
487
526
delta = tree.changes_from(self.b1.repository.revision_tree(rev_id))
488
527
self.assertFalse(delta.has_changed(),
494
533
sure everything matches the builtin branch.
496
535
to_tree = self.get_checkout(base_rev_id, checkout_dir=checkout_dir)
538
self._valid_apply_bundle(base_rev_id, info, to_tree)
542
def _valid_apply_bundle(self, base_rev_id, info, to_tree):
497
543
original_parents = to_tree.get_parent_ids()
498
544
repository = to_tree.branch.repository
499
545
original_parents = to_tree.get_parent_ids()
500
546
self.assertIs(repository.has_revision(base_rev_id), True)
501
547
for rev in info.real_revisions:
502
self.assert_(not repository.has_revision(rev.revision_id),
503
'Revision {%s} present before applying bundle'
505
merge_bundle(info, to_tree, True, Merge3Merger, False, False)
548
self.assertTrue(not repository.has_revision(rev.revision_id),
549
'Revision {%s} present before applying bundle'
551
merge_bundle(info, to_tree, True, merge.Merge3Merger, False, False)
507
553
for rev in info.real_revisions:
508
self.assert_(repository.has_revision(rev.revision_id),
509
'Missing revision {%s} after applying bundle'
554
self.assertTrue(repository.has_revision(rev.revision_id),
555
'Missing revision {%s} after applying bundle'
512
self.assert_(to_tree.branch.repository.has_revision(info.target))
558
self.assertTrue(to_tree.branch.repository.has_revision(info.target))
513
559
# Do we also want to verify that all the texts have been added?
515
561
self.assertEqual(original_parents + [info.target],
516
to_tree.get_parent_ids())
562
to_tree.get_parent_ids())
518
564
rev = info.real_revisions[-1]
519
565
base_tree = self.b1.repository.revision_tree(rev.revision_id)
520
566
to_tree = to_tree.branch.repository.revision_tree(rev.revision_id)
522
568
# TODO: make sure the target tree is identical to base tree
523
569
# we might also check the working tree.
632
680
bundle = self.get_valid_bundle('a@cset-0-5', 'a@cset-0-6')
633
681
other = self.get_checkout('a@cset-0-5')
634
tree1_inv = self.tree1.branch.repository.get_inventory_xml(
636
tree2_inv = other.branch.repository.get_inventory_xml('a@cset-0-5')
682
tree1_inv = get_inventory_text(self.tree1.branch.repository,
684
tree2_inv = get_inventory_text(other.branch.repository,
637
686
self.assertEqualDiff(tree1_inv, tree2_inv)
638
687
other.rename_one('sub/dir/nolastnewline.txt', 'sub/nolastnewline.txt')
639
688
other.commit('rename file', rev_id='a@cset-0-6b')
640
_merge_helper([other.basedir, -1], [None, None],
641
this_dir=self.tree1.basedir)
689
self.tree1.merge_from_branch(other.branch)
642
690
self.tree1.commit(u'Merge', rev_id='a@cset-0-7',
644
692
bundle = self.get_valid_bundle('a@cset-0-6', 'a@cset-0-7')
646
def test_symlink_bundle(self):
647
if not has_symlinks():
648
raise TestSkipped("No symlink support")
694
def _test_symlink_bundle(self, link_name, link_target, new_link_target):
697
self.requireFeature(features.SymlinkFeature)
649
698
self.tree1 = self.make_branch_and_tree('b1')
650
699
self.b1 = self.tree1.branch
651
701
tt = TreeTransform(self.tree1)
652
tt.new_symlink('link', tt.root, 'bar/foo', 'link-1')
702
tt.new_symlink(link_name, tt.root, link_target, link_id)
654
704
self.tree1.commit('add symlink', rev_id='l@cset-0-1')
655
self.get_valid_bundle('null:', 'l@cset-0-1')
705
bundle = self.get_valid_bundle('null:', 'l@cset-0-1')
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-1')
709
self.assertEqual(link_target, bund_tree.get_symlink_target(link_id))
656
711
tt = TreeTransform(self.tree1)
657
trans_id = tt.trans_id_tree_file_id('link-1')
712
trans_id = tt.trans_id_tree_file_id(link_id)
658
713
tt.adjust_path('link2', tt.root, trans_id)
659
714
tt.delete_contents(trans_id)
660
tt.create_symlink('mars', trans_id)
715
tt.create_symlink(new_link_target, trans_id)
662
717
self.tree1.commit('rename and change symlink', rev_id='l@cset-0-2')
663
self.get_valid_bundle('l@cset-0-1', 'l@cset-0-2')
718
bundle = self.get_valid_bundle('l@cset-0-1', 'l@cset-0-2')
719
if getattr(bundle ,'revision_tree', None) is not None:
720
# Not all bundle formats supports revision_tree
721
bund_tree = bundle.revision_tree(self.b1.repository, 'l@cset-0-2')
722
self.assertEqual(new_link_target,
723
bund_tree.get_symlink_target(link_id))
664
725
tt = TreeTransform(self.tree1)
665
trans_id = tt.trans_id_tree_file_id('link-1')
726
trans_id = tt.trans_id_tree_file_id(link_id)
666
727
tt.delete_contents(trans_id)
667
728
tt.create_symlink('jupiter', trans_id)
669
730
self.tree1.commit('just change symlink target', rev_id='l@cset-0-3')
670
self.get_valid_bundle('l@cset-0-2', 'l@cset-0-3')
731
bundle = self.get_valid_bundle('l@cset-0-2', 'l@cset-0-3')
671
733
tt = TreeTransform(self.tree1)
672
trans_id = tt.trans_id_tree_file_id('link-1')
734
trans_id = tt.trans_id_tree_file_id(link_id)
673
735
tt.delete_contents(trans_id)
675
737
self.tree1.commit('Delete symlink', rev_id='l@cset-0-4')
676
self.get_valid_bundle('l@cset-0-3', 'l@cset-0-4')
738
bundle = self.get_valid_bundle('l@cset-0-3', 'l@cset-0-4')
740
def test_symlink_bundle(self):
741
self._test_symlink_bundle('link', 'bar/foo', 'mars')
743
def test_unicode_symlink_bundle(self):
744
self.requireFeature(features.UnicodeFilenameFeature)
745
self._test_symlink_bundle(u'\N{Euro Sign}link',
746
u'bar/\N{Euro Sign}foo',
747
u'mars\N{Euro Sign}')
678
749
def test_binary_bundle(self):
679
750
self.tree1 = self.make_branch_and_tree('b1')
680
751
self.b1 = self.tree1.branch
681
752
tt = TreeTransform(self.tree1)
684
755
tt.new_file('file', tt.root, '\x00\n\x00\r\x01\n\x02\r\xff', 'binary-1')
685
756
tt.new_file('file2', tt.root, '\x01\n\x02\r\x03\n\x04\r\xff',
793
861
u'William Dod\xe9\n').encode('utf-8'))
796
self.tree1.add([u'with Dod\xe9'], ['withdod-id'])
864
self.tree1.add([u'with Dod\N{Euro Sign}'], ['withdod-id'])
797
865
self.tree1.commit(u'i18n commit from William Dod\xe9',
798
866
rev_id='i18n-1', committer=u'William Dod\xe9')
800
if sys.platform == 'darwin':
801
# On Mac the '\xe9' gets changed to 'e\u0301'
802
self.assertEqual([u'.bzr', u'with Dode\u0301'],
803
sorted(os.listdir(u'b1')))
804
delta = self.tree1.changes_from(self.tree1.basis_tree())
805
self.assertEqual([(u'with Dod\xe9', 'withdod-id', 'file')],
807
self.knownFailure("Mac OSX doesn't preserve unicode"
808
" combining characters.")
811
869
bundle = self.get_valid_bundle('null:', 'i18n-1')
814
f = open(u'b1/with Dod\xe9', 'wb')
872
f = open(u'b1/with Dod\N{Euro Sign}', 'wb')
815
873
f.write(u'Modified \xb5\n'.encode('utf8'))
817
875
self.tree1.commit(u'modified', rev_id='i18n-2')
819
877
bundle = self.get_valid_bundle('i18n-1', 'i18n-2')
822
self.tree1.rename_one(u'with Dod\xe9', u'B\xe5gfors')
880
self.tree1.rename_one(u'with Dod\N{Euro Sign}', u'B\N{Euro Sign}gfors')
823
881
self.tree1.commit(u'renamed, the new i18n man', rev_id='i18n-3',
824
882
committer=u'Erik B\xe5gfors')
826
884
bundle = self.get_valid_bundle('i18n-2', 'i18n-3')
829
self.tree1.remove([u'B\xe5gfors'])
887
self.tree1.remove([u'B\N{Euro Sign}gfors'])
830
888
self.tree1.commit(u'removed', rev_id='i18n-4')
832
890
bundle = self.get_valid_bundle('i18n-3', 'i18n-4')
996
1055
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
997
1056
repo = self.make_repository('repo', format='dirstate-with-subtree')
998
1057
bundle.install_revisions(repo)
999
inv_text = repo.get_inventory_xml('rev2')
1058
inv_text = repo._get_inventory_xml('rev2')
1000
1059
self.assertNotContainsRe(inv_text, 'format="5"')
1001
1060
self.assertContainsRe(inv_text, 'format="7"')
1062
def make_repo_with_installed_revisions(self):
1063
tree = self.make_simple_tree('knit')
1064
tree.commit('hello', rev_id='rev1')
1065
tree.commit('hello', rev_id='rev2')
1066
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1067
repo = self.make_repository('repo', format='dirstate-with-subtree')
1068
bundle.install_revisions(repo)
1003
1071
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)
1072
repo = self.make_repo_with_installed_revisions()
1010
1073
inv = repo.get_inventory('rev2')
1011
1074
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'])
1075
root_id = inv.root.file_id
1077
self.addCleanup(repo.unlock)
1078
self.assertEqual({(root_id, 'rev1'):(),
1079
(root_id, 'rev2'):((root_id, 'rev1'),)},
1080
repo.texts.get_parent_map([(root_id, 'rev1'), (root_id, 'rev2')]))
1082
def test_inv_hash_across_serializers(self):
1083
repo = self.make_repo_with_installed_revisions()
1084
recorded_inv_sha1 = repo.get_revision('rev2').inventory_sha1
1085
xml = repo._get_inventory_xml('rev2')
1086
self.assertEqual(osutils.sha_string(xml), recorded_inv_sha1)
1016
1088
def test_across_models_incompatible(self):
1017
1089
tree = self.make_simple_tree('dirstate-with-subtree')
1063
1135
self.tree1.commit('Revision/id/with/slashes', rev_id='rev/id')
1064
1136
except ValueError:
1065
raise TestSkipped("Repository doesn't support revision ids with"
1137
raise tests.TestSkipped(
1138
"Repository doesn't support revision ids with slashes")
1067
1139
bundle = self.get_valid_bundle('null:', 'rev/id')
1070
class V08BundleTester(BundleTester, TestCaseWithTransport):
1141
def test_skip_file(self):
1142
"""Make sure we don't accidentally write to the wrong versionedfile"""
1143
self.tree1 = self.make_branch_and_tree('tree')
1144
self.b1 = self.tree1.branch
1145
# rev1 is not present in bundle, done by fetch
1146
self.build_tree_contents([('tree/file2', 'contents1')])
1147
self.tree1.add('file2', 'file2-id')
1148
self.tree1.commit('rev1', rev_id='reva')
1149
self.build_tree_contents([('tree/file3', 'contents2')])
1150
# rev2 is present in bundle, and done by fetch
1151
# having file1 in the bunle causes file1's versionedfile to be opened.
1152
self.tree1.add('file3', 'file3-id')
1153
self.tree1.commit('rev2')
1154
# Updating file2 should not cause an attempt to add to file1's vf
1155
target = self.tree1.bzrdir.sprout('target').open_workingtree()
1156
self.build_tree_contents([('tree/file2', 'contents3')])
1157
self.tree1.commit('rev3', rev_id='rev3')
1158
bundle = self.get_valid_bundle('reva', 'rev3')
1159
if getattr(bundle, 'get_bundle_reader', None) is None:
1160
raise tests.TestSkipped('Bundle format cannot provide reader')
1161
# be sure that file1 comes before file2
1162
for b, m, k, r, f in bundle.get_bundle_reader().iter_records():
1165
self.assertNotEqual(f, 'file2-id')
1166
bundle.install_revisions(target.branch.repository)
1169
class V08BundleTester(BundleTester, tests.TestCaseWithTransport):
1294
1393
tree2 = self.make_branch_and_tree('target')
1295
1394
target_repo = tree2.branch.repository
1296
1395
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'))
1396
target_repo.lock_read()
1397
self.addCleanup(target_repo.unlock)
1398
# Turn the 'iterators_of_bytes' back into simple strings for comparison
1399
repo_texts = dict((i, ''.join(content)) for i, content
1400
in target_repo.iter_files_bytes(
1401
[('fileid-2', 'rev1', '1'),
1402
('fileid-2', 'rev2', '2')]))
1403
self.assertEqual({'1':'contents1\nstatic\n',
1404
'2':'contents2\nstatic\n'},
1301
1406
rtree = target_repo.revision_tree('rev2')
1302
inventory_vf = target_repo.get_inventory_weave()
1303
self.assertEqual(['rev1'], inventory_vf.get_parents('rev2'))
1407
inventory_vf = target_repo.inventories
1408
# If the inventory store has a graph, it must match the revision graph.
1410
[inventory_vf.get_parent_map([('rev2',)])[('rev2',)]],
1411
[None, (('rev1',),)])
1304
1412
self.assertEqual('changed file',
1305
1413
target_repo.get_revision('rev2').message)
1350
1458
install_bundle(repo_b, serializer.read(s))
1353
class V4WeaveBundleTester(V4BundleTester):
1461
class V4_2aBundleTester(V4BundleTester):
1355
1463
def bzrdir_format(self):
1466
def get_invalid_bundle(self, base_rev_id, rev_id):
1467
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1468
Munge the text so that it's invalid.
1470
:return: The in-memory bundle
1472
from bzrlib.bundle import serializer
1473
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1474
new_text = self.get_raw(StringIO(''.join(bundle_txt)))
1475
# We are going to be replacing some text to set the executable bit on a
1476
# file. Make sure the text replacement actually works correctly.
1477
self.assertContainsRe(new_text, '(?m)B244\n\ni 1\n<inventory')
1478
new_text = new_text.replace('<file file_id="exe-1"',
1479
'<file executable="y" file_id="exe-1"')
1480
new_text = new_text.replace('B244', 'B259')
1481
bundle_txt = StringIO()
1482
bundle_txt.write(serializer._get_bundle_header('4'))
1483
bundle_txt.write('\n')
1484
bundle_txt.write(new_text.encode('bz2'))
1486
bundle = read_bundle(bundle_txt)
1487
self.valid_apply_bundle(base_rev_id, bundle)
1490
def make_merged_branch(self):
1491
builder = self.make_branch_builder('source')
1492
builder.start_series()
1493
builder.build_snapshot('a@cset-0-1', None, [
1494
('add', ('', 'root-id', 'directory', None)),
1495
('add', ('file', 'file-id', 'file', 'original content\n')),
1497
builder.build_snapshot('a@cset-0-2a', ['a@cset-0-1'], [
1498
('modify', ('file-id', 'new-content\n')),
1500
builder.build_snapshot('a@cset-0-2b', ['a@cset-0-1'], [
1501
('add', ('other-file', 'file2-id', 'file', 'file2-content\n')),
1503
builder.build_snapshot('a@cset-0-3', ['a@cset-0-2a', 'a@cset-0-2b'], [
1504
('add', ('other-file', 'file2-id', 'file', 'file2-content\n')),
1506
builder.finish_series()
1507
self.b1 = builder.get_branch()
1509
self.addCleanup(self.b1.unlock)
1511
def make_bundle_just_inventories(self, base_revision_id,
1515
writer = v4.BundleWriteOperation(base_revision_id, target_revision_id,
1516
self.b1.repository, sio)
1517
writer.bundle.begin()
1518
writer._add_inventory_mpdiffs_from_serializer(revision_ids)
1523
def test_single_inventory_multiple_parents_as_xml(self):
1524
self.make_merged_branch()
1525
sio = self.make_bundle_just_inventories('a@cset-0-1', 'a@cset-0-3',
1527
reader = v4.BundleReader(sio, stream_input=False)
1528
records = list(reader.iter_records())
1529
self.assertEqual(1, len(records))
1530
(bytes, metadata, repo_kind, revision_id,
1531
file_id) = records[0]
1532
self.assertIs(None, file_id)
1533
self.assertEqual('a@cset-0-3', revision_id)
1534
self.assertEqual('inventory', repo_kind)
1535
self.assertEqual({'parents': ['a@cset-0-2a', 'a@cset-0-2b'],
1536
'sha1': '09c53b0c4de0895e11a2aacc34fef60a6e70865c',
1537
'storage_kind': 'mpdiff',
1539
# We should have an mpdiff that takes some lines from both parents.
1540
self.assertEqualDiff(
1542
'<inventory format="10" revision_id="a@cset-0-3">\n'
1545
'c 1 3 3 2\n', bytes)
1547
def test_single_inv_no_parents_as_xml(self):
1548
self.make_merged_branch()
1549
sio = self.make_bundle_just_inventories('null:', 'a@cset-0-1',
1551
reader = v4.BundleReader(sio, stream_input=False)
1552
records = list(reader.iter_records())
1553
self.assertEqual(1, len(records))
1554
(bytes, metadata, repo_kind, revision_id,
1555
file_id) = records[0]
1556
self.assertIs(None, file_id)
1557
self.assertEqual('a@cset-0-1', revision_id)
1558
self.assertEqual('inventory', repo_kind)
1559
self.assertEqual({'parents': [],
1560
'sha1': 'a13f42b142d544aac9b085c42595d304150e31a2',
1561
'storage_kind': 'mpdiff',
1563
# We should have an mpdiff that takes some lines from both parents.
1564
self.assertEqualDiff(
1566
'<inventory format="10" revision_id="a@cset-0-1">\n'
1567
'<directory file_id="root-id" name=""'
1568
' revision="a@cset-0-1" />\n'
1569
'<file file_id="file-id" name="file" parent_id="root-id"'
1570
' revision="a@cset-0-1"'
1571
' text_sha1="09c2f8647e14e49e922b955c194102070597c2d1"'
1572
' text_size="17" />\n'
1576
def test_multiple_inventories_as_xml(self):
1577
self.make_merged_branch()
1578
sio = self.make_bundle_just_inventories('a@cset-0-1', 'a@cset-0-3',
1579
['a@cset-0-2a', 'a@cset-0-2b', 'a@cset-0-3'])
1580
reader = v4.BundleReader(sio, stream_input=False)
1581
records = list(reader.iter_records())
1582
self.assertEqual(3, len(records))
1583
revision_ids = [rev_id for b, m, k, rev_id, f in records]
1584
self.assertEqual(['a@cset-0-2a', 'a@cset-0-2b', 'a@cset-0-3'],
1586
metadata_2a = records[0][1]
1587
self.assertEqual({'parents': ['a@cset-0-1'],
1588
'sha1': '1e105886d62d510763e22885eec733b66f5f09bf',
1589
'storage_kind': 'mpdiff',
1591
metadata_2b = records[1][1]
1592
self.assertEqual({'parents': ['a@cset-0-1'],
1593
'sha1': 'f03f12574bdb5ed2204c28636c98a8547544ccd8',
1594
'storage_kind': 'mpdiff',
1596
metadata_3 = records[2][1]
1597
self.assertEqual({'parents': ['a@cset-0-2a', 'a@cset-0-2b'],
1598
'sha1': '09c53b0c4de0895e11a2aacc34fef60a6e70865c',
1599
'storage_kind': 'mpdiff',
1601
bytes_2a = records[0][0]
1602
self.assertEqualDiff(
1604
'<inventory format="10" revision_id="a@cset-0-2a">\n'
1608
'<file file_id="file-id" name="file" parent_id="root-id"'
1609
' revision="a@cset-0-2a"'
1610
' text_sha1="50f545ff40e57b6924b1f3174b267ffc4576e9a9"'
1611
' text_size="12" />\n'
1613
'c 0 3 3 1\n', bytes_2a)
1614
bytes_2b = records[1][0]
1615
self.assertEqualDiff(
1617
'<inventory format="10" revision_id="a@cset-0-2b">\n'
1621
'<file file_id="file2-id" name="other-file" parent_id="root-id"'
1622
' revision="a@cset-0-2b"'
1623
' text_sha1="b46c0c8ea1e5ef8e46fc8894bfd4752a88ec939e"'
1624
' text_size="14" />\n'
1626
'c 0 3 4 1\n', bytes_2b)
1627
bytes_3 = records[2][0]
1628
self.assertEqualDiff(
1630
'<inventory format="10" revision_id="a@cset-0-3">\n'
1633
'c 1 3 3 2\n', bytes_3)
1635
def test_creating_bundle_preserves_chk_pages(self):
1636
self.make_merged_branch()
1637
target = self.b1.bzrdir.sprout('target',
1638
revision_id='a@cset-0-2a').open_branch()
1639
bundle_txt, rev_ids = self.create_bundle_text('a@cset-0-2a',
1641
self.assertEqual(['a@cset-0-2b', 'a@cset-0-3'], rev_ids)
1642
bundle = read_bundle(bundle_txt)
1644
self.addCleanup(target.unlock)
1645
install_bundle(target.repository, bundle)
1646
inv1 = self.b1.repository.inventories.get_record_stream([
1647
('a@cset-0-3',)], 'unordered',
1648
True).next().get_bytes_as('fulltext')
1649
inv2 = target.repository.inventories.get_record_stream([
1650
('a@cset-0-3',)], 'unordered',
1651
True).next().get_bytes_as('fulltext')
1652
self.assertEqualDiff(inv1, inv2)
1359
1655
class MungedBundleTester(object):
1462
1758
'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
1464
1760
fileobj.seek(0)
1465
record_iter = v4.BundleReader(fileobj).iter_records()
1761
reader = v4.BundleReader(fileobj, stream_input=True)
1762
record_iter = reader.iter_records()
1763
record = record_iter.next()
1764
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1765
'info', None, None), record)
1766
record = record_iter.next()
1767
self.assertEqual(("Record body", {'storage_kind': 'fulltext',
1768
'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
1771
def test_roundtrip_record_memory_hungry(self):
1772
fileobj = StringIO()
1773
writer = v4.BundleWriter(fileobj)
1775
writer.add_info_record(foo='bar')
1776
writer._add_record("Record body", {'parents': ['1', '3'],
1777
'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
1780
reader = v4.BundleReader(fileobj, stream_input=False)
1781
record_iter = reader.iter_records()
1466
1782
record = record_iter.next()
1467
1783
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1468
1784
'info', None, None), record)
1499
1815
record = record_iter.next()
1500
1816
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1501
1817
'info', None, None), record)
1502
self.assertRaises(BadBundle, record_iter.next)
1818
self.assertRaises(errors.BadBundle, record_iter.next)
1821
class TestReadMergeableFromUrl(tests.TestCaseWithTransport):
1823
def test_read_mergeable_skips_local(self):
1824
"""A local bundle named like the URL should not be read.
1826
out, wt = test_read_bundle.create_bundle_file(self)
1827
class FooService(object):
1828
"""A directory service that always returns source"""
1830
def look_up(self, name, url):
1832
directories.register('foo:', FooService, 'Testing directory service')
1833
self.addCleanup(directories.remove, 'foo:')
1834
self.build_tree_contents([('./foo:bar', out.getvalue())])
1835
self.assertRaises(errors.NotABundle, read_mergeable_from_url,
1838
def test_infinite_redirects_are_not_a_bundle(self):
1839
"""If a URL causes TooManyRedirections then NotABundle is raised.
1841
from bzrlib.tests.blackbox.test_push import RedirectingMemoryServer
1842
server = RedirectingMemoryServer()
1843
self.start_server(server)
1844
url = server.get_url() + 'infinite-loop'
1845
self.assertRaises(errors.NotABundle, read_mergeable_from_url, url)
1847
def test_smart_server_connection_reset(self):
1848
"""If a smart server connection fails during the attempt to read a
1849
bundle, then the ConnectionReset error should be propagated.
1851
# Instantiate a server that will provoke a ConnectionReset
1852
sock_server = DisconnectingServer()
1853
self.start_server(sock_server)
1854
# We don't really care what the url is since the server will close the
1855
# connection without interpreting it
1856
url = sock_server.get_url()
1857
self.assertRaises(errors.ConnectionReset, read_mergeable_from_url, url)
1860
class DisconnectingHandler(SocketServer.BaseRequestHandler):
1861
"""A request handler that immediately closes any connection made to it."""
1864
self.request.close()
1867
class DisconnectingServer(test_server.TestingTCPServerInAThread):
1870
super(DisconnectingServer, self).__init__(
1872
test_server.TestingTCPServer,
1873
DisconnectingHandler)
1876
"""Return the url of the server"""
1877
return "bzr://%s:%d/" % self.server.server_address