518
636
tt.set_executability(False, trans_id)
520
638
self.tree1.commit('removed', rev_id='a@cset-0-3')
522
640
bundle = self.get_valid_bundle('a@cset-0-2', 'a@cset-0-3')
523
self.assertRaises(TestamentMismatch, self.get_invalid_bundle,
524
'a@cset-0-2', 'a@cset-0-3')
525
# Check a rollup bundle
526
bundle = self.get_valid_bundle(None, 'a@cset-0-3')
641
self.assertRaises((errors.TestamentMismatch,
642
errors.VersionedFileInvalidChecksum,
643
errors.BadBundle), self.get_invalid_bundle,
644
'a@cset-0-2', 'a@cset-0-3')
645
# Check a rollup bundle
646
bundle = self.get_valid_bundle('null:', 'a@cset-0-3')
529
648
# Now move the directory
530
649
self.tree1.rename_one('dir', 'sub/dir')
531
650
self.tree1.commit('rename dir', rev_id='a@cset-0-4')
533
652
bundle = self.get_valid_bundle('a@cset-0-3', 'a@cset-0-4')
534
# Check a rollup bundle
535
bundle = self.get_valid_bundle(None, 'a@cset-0-4')
653
# Check a rollup bundle
654
bundle = self.get_valid_bundle('null:', 'a@cset-0-4')
538
657
open('b1/sub/dir/WithCaps.txt', 'ab').write('\nAdding some text\n')
539
open('b1/sub/dir/trailing space ', 'ab').write('\nAdding some\nDOS format lines\n')
658
open('b1/sub/dir/ pre space', 'ab').write(
659
'\r\nAdding some\r\nDOS format lines\r\n')
540
660
open('b1/sub/dir/nolastnewline.txt', 'ab').write('\n')
541
self.tree1.rename_one('sub/dir/trailing space ',
542
'sub/ start and end space ')
661
self.tree1.rename_one('sub/dir/ pre space',
543
663
self.tree1.commit('Modified files', rev_id='a@cset-0-5')
544
664
bundle = self.get_valid_bundle('a@cset-0-4', 'a@cset-0-5')
546
# Handle international characters
548
f = open(u'b1/with Dod\xe9', 'wb')
549
except UnicodeEncodeError:
550
raise TestSkipped("Filesystem doesn't support unicode")
552
u'With international man of mystery\n'
553
u'William Dod\xe9\n').encode('utf-8'))
554
self.tree1.add([u'with Dod\xe9'])
555
# BUG: (sort of) You must set verbose=False, so that python doesn't try
556
# and print the name of William Dode as part of the commit
557
self.tree1.commit(u'i18n commit from William Dod\xe9',
558
rev_id='a@cset-0-6', committer=u'William Dod\xe9',
560
bundle = self.get_valid_bundle('a@cset-0-5', 'a@cset-0-6')
561
666
self.tree1.rename_one('sub/dir/WithCaps.txt', 'temp')
562
667
self.tree1.rename_one('with space.txt', 'WithCaps.txt')
563
668
self.tree1.rename_one('temp', 'with space.txt')
564
self.tree1.commit(u'swap filenames', rev_id='a@cset-0-7',
669
self.tree1.commit(u'swap filenames', rev_id='a@cset-0-6',
671
bundle = self.get_valid_bundle('a@cset-0-5', 'a@cset-0-6')
672
other = self.get_checkout('a@cset-0-5')
673
tree1_inv = get_inventory_text(self.tree1.branch.repository,
675
tree2_inv = get_inventory_text(other.branch.repository,
677
self.assertEqualDiff(tree1_inv, tree2_inv)
678
other.rename_one('sub/dir/nolastnewline.txt', 'sub/nolastnewline.txt')
679
other.commit('rename file', rev_id='a@cset-0-6b')
680
self.tree1.merge_from_branch(other.branch)
681
self.tree1.commit(u'Merge', rev_id='a@cset-0-7',
566
683
bundle = self.get_valid_bundle('a@cset-0-6', 'a@cset-0-7')
567
other = self.get_checkout('a@cset-0-6')
568
other.rename_one('sub/dir/nolastnewline.txt', 'sub/nolastnewline.txt')
569
other.commit('rename file', rev_id='a@cset-0-7b')
570
merge([other.basedir, -1], [None, None], this_dir=self.tree1.basedir)
571
self.tree1.commit(u'Merge', rev_id='a@cset-0-8',
573
bundle = self.get_valid_bundle('a@cset-0-7', 'a@cset-0-8')
575
def test_symlink_bundle(self):
576
if not has_symlinks():
577
raise TestSkipped("No symlink support")
578
self.tree1 = BzrDir.create_standalone_workingtree('b1')
685
def _test_symlink_bundle(self, link_name, link_target, new_link_target):
688
self.requireFeature(features.SymlinkFeature)
689
self.tree1 = self.make_branch_and_tree('b1')
579
690
self.b1 = self.tree1.branch
580
692
tt = TreeTransform(self.tree1)
581
tt.new_symlink('link', tt.root, 'bar/foo', 'link-1')
693
tt.new_symlink(link_name, tt.root, link_target, link_id)
583
695
self.tree1.commit('add symlink', rev_id='l@cset-0-1')
584
self.get_valid_bundle(None, 'l@cset-0-1')
696
bundle = self.get_valid_bundle('null:', 'l@cset-0-1')
697
if getattr(bundle ,'revision_tree', None) is not None:
698
# Not all bundle formats supports revision_tree
699
bund_tree = bundle.revision_tree(self.b1.repository, 'l@cset-0-1')
700
self.assertEqual(link_target, bund_tree.get_symlink_target(link_id))
585
702
tt = TreeTransform(self.tree1)
586
trans_id = tt.trans_id_tree_file_id('link-1')
703
trans_id = tt.trans_id_tree_file_id(link_id)
587
704
tt.adjust_path('link2', tt.root, trans_id)
588
705
tt.delete_contents(trans_id)
589
tt.create_symlink('mars', trans_id)
706
tt.create_symlink(new_link_target, trans_id)
591
708
self.tree1.commit('rename and change symlink', rev_id='l@cset-0-2')
592
self.get_valid_bundle('l@cset-0-1', 'l@cset-0-2')
709
bundle = self.get_valid_bundle('l@cset-0-1', 'l@cset-0-2')
710
if getattr(bundle ,'revision_tree', None) is not None:
711
# Not all bundle formats supports revision_tree
712
bund_tree = bundle.revision_tree(self.b1.repository, 'l@cset-0-2')
713
self.assertEqual(new_link_target,
714
bund_tree.get_symlink_target(link_id))
593
716
tt = TreeTransform(self.tree1)
594
trans_id = tt.trans_id_tree_file_id('link-1')
717
trans_id = tt.trans_id_tree_file_id(link_id)
595
718
tt.delete_contents(trans_id)
596
719
tt.create_symlink('jupiter', trans_id)
598
721
self.tree1.commit('just change symlink target', rev_id='l@cset-0-3')
599
self.get_valid_bundle('l@cset-0-2', 'l@cset-0-3')
722
bundle = self.get_valid_bundle('l@cset-0-2', 'l@cset-0-3')
600
724
tt = TreeTransform(self.tree1)
601
trans_id = tt.trans_id_tree_file_id('link-1')
725
trans_id = tt.trans_id_tree_file_id(link_id)
602
726
tt.delete_contents(trans_id)
604
728
self.tree1.commit('Delete symlink', rev_id='l@cset-0-4')
605
self.get_valid_bundle('l@cset-0-3', 'l@cset-0-4')
729
bundle = self.get_valid_bundle('l@cset-0-3', 'l@cset-0-4')
731
def test_symlink_bundle(self):
732
self._test_symlink_bundle('link', 'bar/foo', 'mars')
734
def test_unicode_symlink_bundle(self):
735
self.requireFeature(features.UnicodeFilenameFeature)
736
self._test_symlink_bundle(u'\N{Euro Sign}link',
737
u'bar/\N{Euro Sign}foo',
738
u'mars\N{Euro Sign}')
607
740
def test_binary_bundle(self):
608
self.tree1 = BzrDir.create_standalone_workingtree('b1')
741
self.tree1 = self.make_branch_and_tree('b1')
609
742
self.b1 = self.tree1.branch
610
743
tt = TreeTransform(self.tree1)
611
tt.new_file('file', tt.root, '\x00\xff', 'binary-1')
612
tt.new_file('file2', tt.root, '\x00\xff', 'binary-2')
746
tt.new_file('file', tt.root, '\x00\n\x00\r\x01\n\x02\r\xff', 'binary-1')
747
tt.new_file('file2', tt.root, '\x01\n\x02\r\x03\n\x04\r\xff',
614
750
self.tree1.commit('add binary', rev_id='b@cset-0-1')
615
self.get_valid_bundle(None, 'b@cset-0-1')
751
self.get_valid_bundle('null:', 'b@cset-0-1')
616
754
tt = TreeTransform(self.tree1)
617
755
trans_id = tt.trans_id_tree_file_id('binary-1')
618
756
tt.delete_contents(trans_id)
620
758
self.tree1.commit('delete binary', rev_id='b@cset-0-2')
621
759
self.get_valid_bundle('b@cset-0-1', 'b@cset-0-2')
622
762
tt = TreeTransform(self.tree1)
623
763
trans_id = tt.trans_id_tree_file_id('binary-2')
624
764
tt.adjust_path('file3', tt.root, trans_id)
625
765
tt.delete_contents(trans_id)
626
tt.create_file('filecontents\x00', trans_id)
766
tt.create_file('file\rcontents\x00\n\x00', trans_id)
628
768
self.tree1.commit('rename and modify binary', rev_id='b@cset-0-3')
629
769
self.get_valid_bundle('b@cset-0-2', 'b@cset-0-3')
630
772
tt = TreeTransform(self.tree1)
631
773
trans_id = tt.trans_id_tree_file_id('binary-2')
632
774
tt.delete_contents(trans_id)
633
tt.create_file('\x00filecontents', trans_id)
775
tt.create_file('\x00file\rcontents', trans_id)
635
777
self.tree1.commit('just modify binary', rev_id='b@cset-0-4')
636
778
self.get_valid_bundle('b@cset-0-3', 'b@cset-0-4')
781
self.get_valid_bundle('null:', 'b@cset-0-4')
638
783
def test_last_modified(self):
639
self.tree1 = BzrDir.create_standalone_workingtree('b1')
784
self.tree1 = self.make_branch_and_tree('b1')
640
785
self.b1 = self.tree1.branch
641
786
tt = TreeTransform(self.tree1)
642
787
tt.new_file('file', tt.root, 'file', 'file')
657
802
tt.create_file('file2', trans_id)
659
804
other.commit('modify text in another tree', rev_id='a@lmod-0-2b')
660
merge([other.basedir, -1], [None, None], this_dir=self.tree1.basedir)
805
self.tree1.merge_from_branch(other.branch)
661
806
self.tree1.commit(u'Merge', rev_id='a@lmod-0-3',
663
808
self.tree1.commit(u'Merge', rev_id='a@lmod-0-4')
664
809
bundle = self.get_valid_bundle('a@lmod-0-2a', 'a@lmod-0-4')
811
def test_hide_history(self):
812
self.tree1 = self.make_branch_and_tree('b1')
813
self.b1 = self.tree1.branch
815
open('b1/one', 'wb').write('one\n')
816
self.tree1.add('one')
817
self.tree1.commit('add file', rev_id='a@cset-0-1')
818
open('b1/one', 'wb').write('two\n')
819
self.tree1.commit('modify', rev_id='a@cset-0-2')
820
open('b1/one', 'wb').write('three\n')
821
self.tree1.commit('modify', rev_id='a@cset-0-3')
822
bundle_file = StringIO()
823
rev_ids = write_bundle(self.tree1.branch.repository, 'a@cset-0-3',
824
'a@cset-0-1', bundle_file, format=self.format)
825
self.assertNotContainsRe(bundle_file.getvalue(), '\btwo\b')
826
self.assertContainsRe(self.get_raw(bundle_file), 'one')
827
self.assertContainsRe(self.get_raw(bundle_file), 'three')
829
def test_bundle_same_basis(self):
830
"""Ensure using the basis as the target doesn't cause an error"""
831
self.tree1 = self.make_branch_and_tree('b1')
832
self.tree1.commit('add file', rev_id='a@cset-0-1')
833
bundle_file = StringIO()
834
rev_ids = write_bundle(self.tree1.branch.repository, 'a@cset-0-1',
835
'a@cset-0-1', bundle_file)
838
def get_raw(bundle_file):
839
return bundle_file.getvalue()
841
def test_unicode_bundle(self):
842
self.requireFeature(features.UnicodeFilenameFeature)
843
# Handle international characters
845
f = open(u'b1/with Dod\N{Euro Sign}', 'wb')
847
self.tree1 = self.make_branch_and_tree('b1')
848
self.b1 = self.tree1.branch
851
u'With international man of mystery\n'
852
u'William Dod\xe9\n').encode('utf-8'))
855
self.tree1.add([u'with Dod\N{Euro Sign}'], ['withdod-id'])
856
self.tree1.commit(u'i18n commit from William Dod\xe9',
857
rev_id='i18n-1', committer=u'William Dod\xe9')
860
bundle = self.get_valid_bundle('null:', 'i18n-1')
863
f = open(u'b1/with Dod\N{Euro Sign}', 'wb')
864
f.write(u'Modified \xb5\n'.encode('utf8'))
866
self.tree1.commit(u'modified', rev_id='i18n-2')
868
bundle = self.get_valid_bundle('i18n-1', 'i18n-2')
871
self.tree1.rename_one(u'with Dod\N{Euro Sign}', u'B\N{Euro Sign}gfors')
872
self.tree1.commit(u'renamed, the new i18n man', rev_id='i18n-3',
873
committer=u'Erik B\xe5gfors')
875
bundle = self.get_valid_bundle('i18n-2', 'i18n-3')
878
self.tree1.remove([u'B\N{Euro Sign}gfors'])
879
self.tree1.commit(u'removed', rev_id='i18n-4')
881
bundle = self.get_valid_bundle('i18n-3', 'i18n-4')
884
bundle = self.get_valid_bundle('null:', 'i18n-4')
887
def test_whitespace_bundle(self):
888
if sys.platform in ('win32', 'cygwin'):
889
raise tests.TestSkipped('Windows doesn\'t support filenames'
890
' with tabs or trailing spaces')
891
self.tree1 = self.make_branch_and_tree('b1')
892
self.b1 = self.tree1.branch
894
self.build_tree(['b1/trailing space '])
895
self.tree1.add(['trailing space '])
896
# TODO: jam 20060701 Check for handling files with '\t' characters
897
# once we actually support them
900
self.tree1.commit('funky whitespace', rev_id='white-1')
902
bundle = self.get_valid_bundle('null:', 'white-1')
905
open('b1/trailing space ', 'ab').write('add some text\n')
906
self.tree1.commit('add text', rev_id='white-2')
908
bundle = self.get_valid_bundle('white-1', 'white-2')
911
self.tree1.rename_one('trailing space ', ' start and end space ')
912
self.tree1.commit('rename', rev_id='white-3')
914
bundle = self.get_valid_bundle('white-2', 'white-3')
917
self.tree1.remove([' start and end space '])
918
self.tree1.commit('removed', rev_id='white-4')
920
bundle = self.get_valid_bundle('white-3', 'white-4')
922
# Now test a complet roll-up
923
bundle = self.get_valid_bundle('null:', 'white-4')
925
def test_alt_timezone_bundle(self):
926
self.tree1 = self.make_branch_and_memory_tree('b1')
927
self.b1 = self.tree1.branch
928
builder = treebuilder.TreeBuilder()
930
self.tree1.lock_write()
931
builder.start_tree(self.tree1)
932
builder.build(['newfile'])
933
builder.finish_tree()
935
# Asia/Colombo offset = 5 hours 30 minutes
936
self.tree1.commit('non-hour offset timezone', rev_id='tz-1',
937
timezone=19800, timestamp=1152544886.0)
939
bundle = self.get_valid_bundle('null:', 'tz-1')
941
rev = bundle.revisions[0]
942
self.assertEqual('Mon 2006-07-10 20:51:26.000000000 +0530', rev.date)
943
self.assertEqual(19800, rev.timezone)
944
self.assertEqual(1152544886.0, rev.timestamp)
947
def test_bundle_root_id(self):
948
self.tree1 = self.make_branch_and_tree('b1')
949
self.b1 = self.tree1.branch
950
self.tree1.commit('message', rev_id='revid1')
951
bundle = self.get_valid_bundle('null:', 'revid1')
952
tree = self.get_bundle_tree(bundle, 'revid1')
953
self.assertEqual('revid1', tree.inventory.root.revision)
955
def test_install_revisions(self):
956
self.tree1 = self.make_branch_and_tree('b1')
957
self.b1 = self.tree1.branch
958
self.tree1.commit('message', rev_id='rev2a')
959
bundle = self.get_valid_bundle('null:', 'rev2a')
960
branch2 = self.make_branch('b2')
961
self.assertFalse(branch2.repository.has_revision('rev2a'))
962
target_revision = bundle.install_revisions(branch2.repository)
963
self.assertTrue(branch2.repository.has_revision('rev2a'))
964
self.assertEqual('rev2a', target_revision)
966
def test_bundle_empty_property(self):
967
"""Test serializing revision properties with an empty value."""
968
tree = self.make_branch_and_memory_tree('tree')
970
self.addCleanup(tree.unlock)
971
tree.add([''], ['TREE_ROOT'])
972
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
973
self.b1 = tree.branch
974
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
975
bundle = read_bundle(bundle_sio)
976
revision_info = bundle.revisions[0]
977
self.assertEqual('rev1', revision_info.revision_id)
978
rev = revision_info.as_revision()
979
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
982
def test_bundle_sorted_properties(self):
983
"""For stability the writer should write properties in sorted order."""
984
tree = self.make_branch_and_memory_tree('tree')
986
self.addCleanup(tree.unlock)
988
tree.add([''], ['TREE_ROOT'])
989
tree.commit('One', rev_id='rev1',
990
revprops={'a':'4', 'b':'3', 'c':'2', 'd':'1'})
991
self.b1 = tree.branch
992
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
993
bundle = read_bundle(bundle_sio)
994
revision_info = bundle.revisions[0]
995
self.assertEqual('rev1', revision_info.revision_id)
996
rev = revision_info.as_revision()
997
self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
998
'd':'1'}, rev.properties)
1000
def test_bundle_unicode_properties(self):
1001
"""We should be able to round trip a non-ascii property."""
1002
tree = self.make_branch_and_memory_tree('tree')
1004
self.addCleanup(tree.unlock)
1006
tree.add([''], ['TREE_ROOT'])
1007
# Revisions themselves do not require anything about revision property
1008
# keys, other than that they are a basestring, and do not contain
1010
# However, Testaments assert than they are str(), and thus should not
1012
tree.commit('One', rev_id='rev1',
1013
revprops={'omega':u'\u03a9', 'alpha':u'\u03b1'})
1014
self.b1 = tree.branch
1015
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1016
bundle = read_bundle(bundle_sio)
1017
revision_info = bundle.revisions[0]
1018
self.assertEqual('rev1', revision_info.revision_id)
1019
rev = revision_info.as_revision()
1020
self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
1021
'alpha':u'\u03b1'}, rev.properties)
1023
def test_bundle_with_ghosts(self):
1024
tree = self.make_branch_and_tree('tree')
1025
self.b1 = tree.branch
1026
self.build_tree_contents([('tree/file', 'content1')])
1029
self.build_tree_contents([('tree/file', 'content2')])
1030
tree.add_parent_tree_id('ghost')
1031
tree.commit('rev2', rev_id='rev2')
1032
bundle = self.get_valid_bundle('null:', 'rev2')
1034
def make_simple_tree(self, format=None):
1035
tree = self.make_branch_and_tree('b1', format=format)
1036
self.b1 = tree.branch
1037
self.build_tree(['b1/file'])
1041
def test_across_serializers(self):
1042
tree = self.make_simple_tree('knit')
1043
tree.commit('hello', rev_id='rev1')
1044
tree.commit('hello', rev_id='rev2')
1045
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1046
repo = self.make_repository('repo', format='dirstate-with-subtree')
1047
bundle.install_revisions(repo)
1048
inv_text = repo._get_inventory_xml('rev2')
1049
self.assertNotContainsRe(inv_text, 'format="5"')
1050
self.assertContainsRe(inv_text, 'format="7"')
1052
def make_repo_with_installed_revisions(self):
1053
tree = self.make_simple_tree('knit')
1054
tree.commit('hello', rev_id='rev1')
1055
tree.commit('hello', rev_id='rev2')
1056
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1057
repo = self.make_repository('repo', format='dirstate-with-subtree')
1058
bundle.install_revisions(repo)
1061
def test_across_models(self):
1062
repo = self.make_repo_with_installed_revisions()
1063
inv = repo.get_inventory('rev2')
1064
self.assertEqual('rev2', inv.root.revision)
1065
root_id = inv.root.file_id
1067
self.addCleanup(repo.unlock)
1068
self.assertEqual({(root_id, 'rev1'):(),
1069
(root_id, 'rev2'):((root_id, 'rev1'),)},
1070
repo.texts.get_parent_map([(root_id, 'rev1'), (root_id, 'rev2')]))
1072
def test_inv_hash_across_serializers(self):
1073
repo = self.make_repo_with_installed_revisions()
1074
recorded_inv_sha1 = repo.get_revision('rev2').inventory_sha1
1075
xml = repo._get_inventory_xml('rev2')
1076
self.assertEqual(osutils.sha_string(xml), recorded_inv_sha1)
1078
def test_across_models_incompatible(self):
1079
tree = self.make_simple_tree('dirstate-with-subtree')
1080
tree.commit('hello', rev_id='rev1')
1081
tree.commit('hello', rev_id='rev2')
1083
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1084
except errors.IncompatibleBundleFormat:
1085
raise tests.TestSkipped("Format 0.8 doesn't work with knit3")
1086
repo = self.make_repository('repo', format='knit')
1087
bundle.install_revisions(repo)
1089
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1090
self.assertRaises(errors.IncompatibleRevision,
1091
bundle.install_revisions, repo)
1093
def test_get_merge_request(self):
1094
tree = self.make_simple_tree()
1095
tree.commit('hello', rev_id='rev1')
1096
tree.commit('hello', rev_id='rev2')
1097
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1098
result = bundle.get_merge_request(tree.branch.repository)
1099
self.assertEqual((None, 'rev1', 'inapplicable'), result)
1101
def test_with_subtree(self):
1102
tree = self.make_branch_and_tree('tree',
1103
format='dirstate-with-subtree')
1104
self.b1 = tree.branch
1105
subtree = self.make_branch_and_tree('tree/subtree',
1106
format='dirstate-with-subtree')
1108
tree.commit('hello', rev_id='rev1')
1110
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1111
except errors.IncompatibleBundleFormat:
1112
raise tests.TestSkipped("Format 0.8 doesn't work with knit3")
1113
if isinstance(bundle, v09.BundleInfo09):
1114
raise tests.TestSkipped("Format 0.9 doesn't work with subtrees")
1115
repo = self.make_repository('repo', format='knit')
1116
self.assertRaises(errors.IncompatibleRevision,
1117
bundle.install_revisions, repo)
1118
repo2 = self.make_repository('repo2', format='dirstate-with-subtree')
1119
bundle.install_revisions(repo2)
1121
def test_revision_id_with_slash(self):
1122
self.tree1 = self.make_branch_and_tree('tree')
1123
self.b1 = self.tree1.branch
1125
self.tree1.commit('Revision/id/with/slashes', rev_id='rev/id')
1127
raise tests.TestSkipped(
1128
"Repository doesn't support revision ids with slashes")
1129
bundle = self.get_valid_bundle('null:', 'rev/id')
1131
def test_skip_file(self):
1132
"""Make sure we don't accidentally write to the wrong versionedfile"""
1133
self.tree1 = self.make_branch_and_tree('tree')
1134
self.b1 = self.tree1.branch
1135
# rev1 is not present in bundle, done by fetch
1136
self.build_tree_contents([('tree/file2', 'contents1')])
1137
self.tree1.add('file2', 'file2-id')
1138
self.tree1.commit('rev1', rev_id='reva')
1139
self.build_tree_contents([('tree/file3', 'contents2')])
1140
# rev2 is present in bundle, and done by fetch
1141
# having file1 in the bunle causes file1's versionedfile to be opened.
1142
self.tree1.add('file3', 'file3-id')
1143
self.tree1.commit('rev2')
1144
# Updating file2 should not cause an attempt to add to file1's vf
1145
target = self.tree1.bzrdir.sprout('target').open_workingtree()
1146
self.build_tree_contents([('tree/file2', 'contents3')])
1147
self.tree1.commit('rev3', rev_id='rev3')
1148
bundle = self.get_valid_bundle('reva', 'rev3')
1149
if getattr(bundle, 'get_bundle_reader', None) is None:
1150
raise tests.TestSkipped('Bundle format cannot provide reader')
1151
# be sure that file1 comes before file2
1152
for b, m, k, r, f in bundle.get_bundle_reader().iter_records():
1155
self.assertNotEqual(f, 'file2-id')
1156
bundle.install_revisions(target.branch.repository)
1159
class V08BundleTester(BundleTester, tests.TestCaseWithTransport):
1163
def test_bundle_empty_property(self):
1164
"""Test serializing revision properties with an empty value."""
1165
tree = self.make_branch_and_memory_tree('tree')
1167
self.addCleanup(tree.unlock)
1168
tree.add([''], ['TREE_ROOT'])
1169
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
1170
self.b1 = tree.branch
1171
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1172
self.assertContainsRe(bundle_sio.getvalue(),
1174
'# branch-nick: tree\n'
1178
bundle = read_bundle(bundle_sio)
1179
revision_info = bundle.revisions[0]
1180
self.assertEqual('rev1', revision_info.revision_id)
1181
rev = revision_info.as_revision()
1182
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
1185
def get_bundle_tree(self, bundle, revision_id):
1186
repository = self.make_repository('repo')
1187
return bundle.revision_tree(repository, 'revid1')
1189
def test_bundle_empty_property_alt(self):
1190
"""Test serializing revision properties with an empty value.
1192
Older readers had a bug when reading an empty property.
1193
They assumed that all keys ended in ': \n'. However they would write an
1194
empty value as ':\n'. This tests make sure that all newer bzr versions
1195
can handle th second form.
1197
tree = self.make_branch_and_memory_tree('tree')
1199
self.addCleanup(tree.unlock)
1200
tree.add([''], ['TREE_ROOT'])
1201
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id='rev1')
1202
self.b1 = tree.branch
1203
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1204
txt = bundle_sio.getvalue()
1205
loc = txt.find('# empty: ') + len('# empty:')
1206
# Create a new bundle, which strips the trailing space after empty
1207
bundle_sio = StringIO(txt[:loc] + txt[loc+1:])
1209
self.assertContainsRe(bundle_sio.getvalue(),
1211
'# branch-nick: tree\n'
1215
bundle = read_bundle(bundle_sio)
1216
revision_info = bundle.revisions[0]
1217
self.assertEqual('rev1', revision_info.revision_id)
1218
rev = revision_info.as_revision()
1219
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
1222
def test_bundle_sorted_properties(self):
1223
"""For stability the writer should write properties in sorted order."""
1224
tree = self.make_branch_and_memory_tree('tree')
1226
self.addCleanup(tree.unlock)
1228
tree.add([''], ['TREE_ROOT'])
1229
tree.commit('One', rev_id='rev1',
1230
revprops={'a':'4', 'b':'3', 'c':'2', 'd':'1'})
1231
self.b1 = tree.branch
1232
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1233
self.assertContainsRe(bundle_sio.getvalue(),
1237
'# branch-nick: tree\n'
1241
bundle = read_bundle(bundle_sio)
1242
revision_info = bundle.revisions[0]
1243
self.assertEqual('rev1', revision_info.revision_id)
1244
rev = revision_info.as_revision()
1245
self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
1246
'd':'1'}, rev.properties)
1248
def test_bundle_unicode_properties(self):
1249
"""We should be able to round trip a non-ascii property."""
1250
tree = self.make_branch_and_memory_tree('tree')
1252
self.addCleanup(tree.unlock)
1254
tree.add([''], ['TREE_ROOT'])
1255
# Revisions themselves do not require anything about revision property
1256
# keys, other than that they are a basestring, and do not contain
1258
# However, Testaments assert than they are str(), and thus should not
1260
tree.commit('One', rev_id='rev1',
1261
revprops={'omega':u'\u03a9', 'alpha':u'\u03b1'})
1262
self.b1 = tree.branch
1263
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1264
self.assertContainsRe(bundle_sio.getvalue(),
1266
'# alpha: \xce\xb1\n'
1267
'# branch-nick: tree\n'
1268
'# omega: \xce\xa9\n'
1270
bundle = read_bundle(bundle_sio)
1271
revision_info = bundle.revisions[0]
1272
self.assertEqual('rev1', revision_info.revision_id)
1273
rev = revision_info.as_revision()
1274
self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
1275
'alpha':u'\u03b1'}, rev.properties)
1278
class V09BundleKnit2Tester(V08BundleTester):
1282
def bzrdir_format(self):
1283
format = bzrdir.BzrDirMetaFormat1()
1284
format.repository_format = knitrepo.RepositoryFormatKnit3()
1288
class V09BundleKnit1Tester(V08BundleTester):
1292
def bzrdir_format(self):
1293
format = bzrdir.BzrDirMetaFormat1()
1294
format.repository_format = knitrepo.RepositoryFormatKnit1()
1298
class V4BundleTester(BundleTester, tests.TestCaseWithTransport):
1302
def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
1303
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1304
Make sure that the text generated is valid, and that it
1305
can be applied against the base, and generate the same information.
1307
:return: The in-memory bundle
1309
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1311
# This should also validate the generated bundle
1312
bundle = read_bundle(bundle_txt)
1313
repository = self.b1.repository
1314
for bundle_rev in bundle.real_revisions:
1315
# These really should have already been checked when we read the
1316
# bundle, since it computes the sha1 hash for the revision, which
1317
# only will match if everything is okay, but lets be explicit about
1319
branch_rev = repository.get_revision(bundle_rev.revision_id)
1320
for a in ('inventory_sha1', 'revision_id', 'parent_ids',
1321
'timestamp', 'timezone', 'message', 'committer',
1322
'parent_ids', 'properties'):
1323
self.assertEqual(getattr(branch_rev, a),
1324
getattr(bundle_rev, a))
1325
self.assertEqual(len(branch_rev.parent_ids),
1326
len(bundle_rev.parent_ids))
1327
self.assertEqual(set(rev_ids),
1328
set([r.revision_id for r in bundle.real_revisions]))
1329
self.valid_apply_bundle(base_rev_id, bundle,
1330
checkout_dir=checkout_dir)
1334
def get_invalid_bundle(self, base_rev_id, rev_id):
1335
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1336
Munge the text so that it's invalid.
1338
:return: The in-memory bundle
1340
from bzrlib.bundle import serializer
1341
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1342
new_text = self.get_raw(StringIO(''.join(bundle_txt)))
1343
new_text = new_text.replace('<file file_id="exe-1"',
1344
'<file executable="y" file_id="exe-1"')
1345
new_text = new_text.replace('B260', 'B275')
1346
bundle_txt = StringIO()
1347
bundle_txt.write(serializer._get_bundle_header('4'))
1348
bundle_txt.write('\n')
1349
bundle_txt.write(new_text.encode('bz2'))
1351
bundle = read_bundle(bundle_txt)
1352
self.valid_apply_bundle(base_rev_id, bundle)
1355
def create_bundle_text(self, base_rev_id, rev_id):
1356
bundle_txt = StringIO()
1357
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
1358
bundle_txt, format=self.format)
1360
self.assertEqual(bundle_txt.readline(),
1361
'# Bazaar revision bundle v%s\n' % self.format)
1362
self.assertEqual(bundle_txt.readline(), '#\n')
1363
rev = self.b1.repository.get_revision(rev_id)
1365
return bundle_txt, rev_ids
1367
def get_bundle_tree(self, bundle, revision_id):
1368
repository = self.make_repository('repo')
1369
bundle.install_revisions(repository)
1370
return repository.revision_tree(revision_id)
1372
def test_creation(self):
1373
tree = self.make_branch_and_tree('tree')
1374
self.build_tree_contents([('tree/file', 'contents1\nstatic\n')])
1375
tree.add('file', 'fileid-2')
1376
tree.commit('added file', rev_id='rev1')
1377
self.build_tree_contents([('tree/file', 'contents2\nstatic\n')])
1378
tree.commit('changed file', rev_id='rev2')
1380
serializer = BundleSerializerV4('1.0')
1381
serializer.write(tree.branch.repository, ['rev1', 'rev2'], {}, s)
1383
tree2 = self.make_branch_and_tree('target')
1384
target_repo = tree2.branch.repository
1385
install_bundle(target_repo, serializer.read(s))
1386
target_repo.lock_read()
1387
self.addCleanup(target_repo.unlock)
1388
# Turn the 'iterators_of_bytes' back into simple strings for comparison
1389
repo_texts = dict((i, ''.join(content)) for i, content
1390
in target_repo.iter_files_bytes(
1391
[('fileid-2', 'rev1', '1'),
1392
('fileid-2', 'rev2', '2')]))
1393
self.assertEqual({'1':'contents1\nstatic\n',
1394
'2':'contents2\nstatic\n'},
1396
rtree = target_repo.revision_tree('rev2')
1397
inventory_vf = target_repo.inventories
1398
# If the inventory store has a graph, it must match the revision graph.
1400
[inventory_vf.get_parent_map([('rev2',)])[('rev2',)]],
1401
[None, (('rev1',),)])
1402
self.assertEqual('changed file',
1403
target_repo.get_revision('rev2').message)
1406
def get_raw(bundle_file):
1408
line = bundle_file.readline()
1409
line = bundle_file.readline()
1410
lines = bundle_file.readlines()
1411
return ''.join(lines).decode('bz2')
1413
def test_copy_signatures(self):
1414
tree_a = self.make_branch_and_tree('tree_a')
1416
import bzrlib.commit as commit
1417
oldstrategy = bzrlib.gpg.GPGStrategy
1418
branch = tree_a.branch
1419
repo_a = branch.repository
1420
tree_a.commit("base", allow_pointless=True, rev_id='A')
1421
self.assertFalse(branch.repository.has_signature_for_revision_id('A'))
1423
from bzrlib.testament import Testament
1424
# monkey patch gpg signing mechanism
1425
bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
1426
new_config = test_commit.MustSignConfig(branch)
1427
commit.Commit(config=new_config).commit(message="base",
1428
allow_pointless=True,
1430
working_tree=tree_a)
1432
return bzrlib.gpg.LoopbackGPGStrategy(None).sign(text)
1433
self.assertTrue(repo_a.has_signature_for_revision_id('B'))
1435
bzrlib.gpg.GPGStrategy = oldstrategy
1436
tree_b = self.make_branch_and_tree('tree_b')
1437
repo_b = tree_b.branch.repository
1439
serializer = BundleSerializerV4('4')
1440
serializer.write(tree_a.branch.repository, ['A', 'B'], {}, s)
1442
install_bundle(repo_b, serializer.read(s))
1443
self.assertTrue(repo_b.has_signature_for_revision_id('B'))
1444
self.assertEqual(repo_b.get_signature_text('B'),
1445
repo_a.get_signature_text('B'))
1447
# ensure repeat installs are harmless
1448
install_bundle(repo_b, serializer.read(s))
1451
class V4_2aBundleTester(V4BundleTester):
1453
def bzrdir_format(self):
1456
def get_invalid_bundle(self, base_rev_id, rev_id):
1457
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1458
Munge the text so that it's invalid.
1460
:return: The in-memory bundle
1462
from bzrlib.bundle import serializer
1463
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1464
new_text = self.get_raw(StringIO(''.join(bundle_txt)))
1465
# We are going to be replacing some text to set the executable bit on a
1466
# file. Make sure the text replacement actually works correctly.
1467
self.assertContainsRe(new_text, '(?m)B244\n\ni 1\n<inventory')
1468
new_text = new_text.replace('<file file_id="exe-1"',
1469
'<file executable="y" file_id="exe-1"')
1470
new_text = new_text.replace('B244', 'B259')
1471
bundle_txt = StringIO()
1472
bundle_txt.write(serializer._get_bundle_header('4'))
1473
bundle_txt.write('\n')
1474
bundle_txt.write(new_text.encode('bz2'))
1476
bundle = read_bundle(bundle_txt)
1477
self.valid_apply_bundle(base_rev_id, bundle)
1480
def make_merged_branch(self):
1481
builder = self.make_branch_builder('source')
1482
builder.start_series()
1483
builder.build_snapshot('a@cset-0-1', None, [
1484
('add', ('', 'root-id', 'directory', None)),
1485
('add', ('file', 'file-id', 'file', 'original content\n')),
1487
builder.build_snapshot('a@cset-0-2a', ['a@cset-0-1'], [
1488
('modify', ('file-id', 'new-content\n')),
1490
builder.build_snapshot('a@cset-0-2b', ['a@cset-0-1'], [
1491
('add', ('other-file', 'file2-id', 'file', 'file2-content\n')),
1493
builder.build_snapshot('a@cset-0-3', ['a@cset-0-2a', 'a@cset-0-2b'], [
1494
('add', ('other-file', 'file2-id', 'file', 'file2-content\n')),
1496
builder.finish_series()
1497
self.b1 = builder.get_branch()
1499
self.addCleanup(self.b1.unlock)
1501
def make_bundle_just_inventories(self, base_revision_id,
1505
writer = v4.BundleWriteOperation(base_revision_id, target_revision_id,
1506
self.b1.repository, sio)
1507
writer.bundle.begin()
1508
writer._add_inventory_mpdiffs_from_serializer(revision_ids)
1513
def test_single_inventory_multiple_parents_as_xml(self):
1514
self.make_merged_branch()
1515
sio = self.make_bundle_just_inventories('a@cset-0-1', 'a@cset-0-3',
1517
reader = v4.BundleReader(sio, stream_input=False)
1518
records = list(reader.iter_records())
1519
self.assertEqual(1, len(records))
1520
(bytes, metadata, repo_kind, revision_id,
1521
file_id) = records[0]
1522
self.assertIs(None, file_id)
1523
self.assertEqual('a@cset-0-3', revision_id)
1524
self.assertEqual('inventory', repo_kind)
1525
self.assertEqual({'parents': ['a@cset-0-2a', 'a@cset-0-2b'],
1526
'sha1': '09c53b0c4de0895e11a2aacc34fef60a6e70865c',
1527
'storage_kind': 'mpdiff',
1529
# We should have an mpdiff that takes some lines from both parents.
1530
self.assertEqualDiff(
1532
'<inventory format="10" revision_id="a@cset-0-3">\n'
1535
'c 1 3 3 2\n', bytes)
1537
def test_single_inv_no_parents_as_xml(self):
1538
self.make_merged_branch()
1539
sio = self.make_bundle_just_inventories('null:', 'a@cset-0-1',
1541
reader = v4.BundleReader(sio, stream_input=False)
1542
records = list(reader.iter_records())
1543
self.assertEqual(1, len(records))
1544
(bytes, metadata, repo_kind, revision_id,
1545
file_id) = records[0]
1546
self.assertIs(None, file_id)
1547
self.assertEqual('a@cset-0-1', revision_id)
1548
self.assertEqual('inventory', repo_kind)
1549
self.assertEqual({'parents': [],
1550
'sha1': 'a13f42b142d544aac9b085c42595d304150e31a2',
1551
'storage_kind': 'mpdiff',
1553
# We should have an mpdiff that takes some lines from both parents.
1554
self.assertEqualDiff(
1556
'<inventory format="10" revision_id="a@cset-0-1">\n'
1557
'<directory file_id="root-id" name=""'
1558
' revision="a@cset-0-1" />\n'
1559
'<file file_id="file-id" name="file" parent_id="root-id"'
1560
' revision="a@cset-0-1"'
1561
' text_sha1="09c2f8647e14e49e922b955c194102070597c2d1"'
1562
' text_size="17" />\n'
1566
def test_multiple_inventories_as_xml(self):
1567
self.make_merged_branch()
1568
sio = self.make_bundle_just_inventories('a@cset-0-1', 'a@cset-0-3',
1569
['a@cset-0-2a', 'a@cset-0-2b', 'a@cset-0-3'])
1570
reader = v4.BundleReader(sio, stream_input=False)
1571
records = list(reader.iter_records())
1572
self.assertEqual(3, len(records))
1573
revision_ids = [rev_id for b, m, k, rev_id, f in records]
1574
self.assertEqual(['a@cset-0-2a', 'a@cset-0-2b', 'a@cset-0-3'],
1576
metadata_2a = records[0][1]
1577
self.assertEqual({'parents': ['a@cset-0-1'],
1578
'sha1': '1e105886d62d510763e22885eec733b66f5f09bf',
1579
'storage_kind': 'mpdiff',
1581
metadata_2b = records[1][1]
1582
self.assertEqual({'parents': ['a@cset-0-1'],
1583
'sha1': 'f03f12574bdb5ed2204c28636c98a8547544ccd8',
1584
'storage_kind': 'mpdiff',
1586
metadata_3 = records[2][1]
1587
self.assertEqual({'parents': ['a@cset-0-2a', 'a@cset-0-2b'],
1588
'sha1': '09c53b0c4de0895e11a2aacc34fef60a6e70865c',
1589
'storage_kind': 'mpdiff',
1591
bytes_2a = records[0][0]
1592
self.assertEqualDiff(
1594
'<inventory format="10" revision_id="a@cset-0-2a">\n'
1598
'<file file_id="file-id" name="file" parent_id="root-id"'
1599
' revision="a@cset-0-2a"'
1600
' text_sha1="50f545ff40e57b6924b1f3174b267ffc4576e9a9"'
1601
' text_size="12" />\n'
1603
'c 0 3 3 1\n', bytes_2a)
1604
bytes_2b = records[1][0]
1605
self.assertEqualDiff(
1607
'<inventory format="10" revision_id="a@cset-0-2b">\n'
1611
'<file file_id="file2-id" name="other-file" parent_id="root-id"'
1612
' revision="a@cset-0-2b"'
1613
' text_sha1="b46c0c8ea1e5ef8e46fc8894bfd4752a88ec939e"'
1614
' text_size="14" />\n'
1616
'c 0 3 4 1\n', bytes_2b)
1617
bytes_3 = records[2][0]
1618
self.assertEqualDiff(
1620
'<inventory format="10" revision_id="a@cset-0-3">\n'
1623
'c 1 3 3 2\n', bytes_3)
1625
def test_creating_bundle_preserves_chk_pages(self):
1626
self.make_merged_branch()
1627
target = self.b1.bzrdir.sprout('target',
1628
revision_id='a@cset-0-2a').open_branch()
1629
bundle_txt, rev_ids = self.create_bundle_text('a@cset-0-2a',
1631
self.assertEqual(['a@cset-0-2b', 'a@cset-0-3'], rev_ids)
1632
bundle = read_bundle(bundle_txt)
1634
self.addCleanup(target.unlock)
1635
install_bundle(target.repository, bundle)
1636
inv1 = self.b1.repository.inventories.get_record_stream([
1637
('a@cset-0-3',)], 'unordered',
1638
True).next().get_bytes_as('fulltext')
1639
inv2 = target.repository.inventories.get_record_stream([
1640
('a@cset-0-3',)], 'unordered',
1641
True).next().get_bytes_as('fulltext')
1642
self.assertEqualDiff(inv1, inv2)
1645
class MungedBundleTester(object):
1647
def build_test_bundle(self):
1648
wt = self.make_branch_and_tree('b1')
1650
self.build_tree(['b1/one'])
1652
wt.commit('add one', rev_id='a@cset-0-1')
1653
self.build_tree(['b1/two'])
1655
wt.commit('add two', rev_id='a@cset-0-2',
1656
revprops={'branch-nick':'test'})
1658
bundle_txt = StringIO()
1659
rev_ids = write_bundle(wt.branch.repository, 'a@cset-0-2',
1660
'a@cset-0-1', bundle_txt, self.format)
1661
self.assertEqual(set(['a@cset-0-2']), set(rev_ids))
1662
bundle_txt.seek(0, 0)
1665
def check_valid(self, bundle):
1666
"""Check that after whatever munging, the final object is valid."""
1667
self.assertEqual(['a@cset-0-2'],
1668
[r.revision_id for r in bundle.real_revisions])
1670
def test_extra_whitespace(self):
1671
bundle_txt = self.build_test_bundle()
1673
# Seek to the end of the file
1674
# Adding one extra newline used to give us
1675
# TypeError: float() argument must be a string or a number
1676
bundle_txt.seek(0, 2)
1677
bundle_txt.write('\n')
1680
bundle = read_bundle(bundle_txt)
1681
self.check_valid(bundle)
1683
def test_extra_whitespace_2(self):
1684
bundle_txt = self.build_test_bundle()
1686
# Seek to the end of the file
1687
# Adding two extra newlines used to give us
1688
# MalformedPatches: The first line of all patches should be ...
1689
bundle_txt.seek(0, 2)
1690
bundle_txt.write('\n\n')
1693
bundle = read_bundle(bundle_txt)
1694
self.check_valid(bundle)
1697
class MungedBundleTesterV09(tests.TestCaseWithTransport, MungedBundleTester):
1701
def test_missing_trailing_whitespace(self):
1702
bundle_txt = self.build_test_bundle()
1704
# Remove a trailing newline, it shouldn't kill the parser
1705
raw = bundle_txt.getvalue()
1706
# The contents of the bundle don't have to be this, but this
1707
# test is concerned with the exact case where the serializer
1708
# creates a blank line at the end, and fails if that
1710
self.assertEqual('\n\n', raw[-2:])
1711
bundle_txt = StringIO(raw[:-1])
1713
bundle = read_bundle(bundle_txt)
1714
self.check_valid(bundle)
1716
def test_opening_text(self):
1717
bundle_txt = self.build_test_bundle()
1719
bundle_txt = StringIO("Some random\nemail comments\n"
1720
+ bundle_txt.getvalue())
1722
bundle = read_bundle(bundle_txt)
1723
self.check_valid(bundle)
1725
def test_trailing_text(self):
1726
bundle_txt = self.build_test_bundle()
1728
bundle_txt = StringIO(bundle_txt.getvalue() +
1729
"Some trailing\nrandom\ntext\n")
1731
bundle = read_bundle(bundle_txt)
1732
self.check_valid(bundle)
1735
class MungedBundleTesterV4(tests.TestCaseWithTransport, MungedBundleTester):
1740
class TestBundleWriterReader(tests.TestCase):
1742
def test_roundtrip_record(self):
1743
fileobj = StringIO()
1744
writer = v4.BundleWriter(fileobj)
1746
writer.add_info_record(foo='bar')
1747
writer._add_record("Record body", {'parents': ['1', '3'],
1748
'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
1751
reader = v4.BundleReader(fileobj, stream_input=True)
1752
record_iter = reader.iter_records()
1753
record = record_iter.next()
1754
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1755
'info', None, None), record)
1756
record = record_iter.next()
1757
self.assertEqual(("Record body", {'storage_kind': 'fulltext',
1758
'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
1761
def test_roundtrip_record_memory_hungry(self):
1762
fileobj = StringIO()
1763
writer = v4.BundleWriter(fileobj)
1765
writer.add_info_record(foo='bar')
1766
writer._add_record("Record body", {'parents': ['1', '3'],
1767
'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
1770
reader = v4.BundleReader(fileobj, stream_input=False)
1771
record_iter = reader.iter_records()
1772
record = record_iter.next()
1773
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1774
'info', None, None), record)
1775
record = record_iter.next()
1776
self.assertEqual(("Record body", {'storage_kind': 'fulltext',
1777
'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
1780
def test_encode_name(self):
1781
self.assertEqual('revision/rev1',
1782
v4.BundleWriter.encode_name('revision', 'rev1'))
1783
self.assertEqual('file/rev//1/file-id-1',
1784
v4.BundleWriter.encode_name('file', 'rev/1', 'file-id-1'))
1785
self.assertEqual('info',
1786
v4.BundleWriter.encode_name('info', None, None))
1788
def test_decode_name(self):
1789
self.assertEqual(('revision', 'rev1', None),
1790
v4.BundleReader.decode_name('revision/rev1'))
1791
self.assertEqual(('file', 'rev/1', 'file-id-1'),
1792
v4.BundleReader.decode_name('file/rev//1/file-id-1'))
1793
self.assertEqual(('info', None, None),
1794
v4.BundleReader.decode_name('info'))
1796
def test_too_many_names(self):
1797
fileobj = StringIO()
1798
writer = v4.BundleWriter(fileobj)
1800
writer.add_info_record(foo='bar')
1801
writer._container.add_bytes_record('blah', ['two', 'names'])
1804
record_iter = v4.BundleReader(fileobj).iter_records()
1805
record = record_iter.next()
1806
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1807
'info', None, None), record)
1808
self.assertRaises(errors.BadBundle, record_iter.next)
1811
class TestReadMergeableFromUrl(tests.TestCaseWithTransport):
1813
def test_read_mergeable_skips_local(self):
1814
"""A local bundle named like the URL should not be read.
1816
out, wt = test_read_bundle.create_bundle_file(self)
1817
class FooService(object):
1818
"""A directory service that always returns source"""
1820
def look_up(self, name, url):
1822
directories.register('foo:', FooService, 'Testing directory service')
1823
self.addCleanup(directories.remove, 'foo:')
1824
self.build_tree_contents([('./foo:bar', out.getvalue())])
1825
self.assertRaises(errors.NotABundle, read_mergeable_from_url,
1828
def test_infinite_redirects_are_not_a_bundle(self):
1829
"""If a URL causes TooManyRedirections then NotABundle is raised.
1831
from bzrlib.tests.blackbox.test_push import RedirectingMemoryServer
1832
server = RedirectingMemoryServer()
1833
self.start_server(server)
1834
url = server.get_url() + 'infinite-loop'
1835
self.assertRaises(errors.NotABundle, read_mergeable_from_url, url)
1837
def test_smart_server_connection_reset(self):
1838
"""If a smart server connection fails during the attempt to read a
1839
bundle, then the ConnectionReset error should be propagated.
1841
# Instantiate a server that will provoke a ConnectionReset
1842
sock_server = _DisconnectingTCPServer()
1843
self.start_server(sock_server)
1844
# We don't really care what the url is since the server will close the
1845
# connection without interpreting it
1846
url = sock_server.get_url()
1847
self.assertRaises(errors.ConnectionReset, read_mergeable_from_url, url)
1850
class _DisconnectingTCPServer(object):
1851
"""A TCP server that immediately closes any connection made to it."""
1853
def start_server(self):
1854
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1855
self.sock.bind(('127.0.0.1', 0))
1857
self.port = self.sock.getsockname()[1]
1858
self.thread = threading.Thread(
1859
name='%s (port %d)' % (self.__class__.__name__, self.port),
1860
target=self.accept_and_close)
1863
def accept_and_close(self):
1864
conn, addr = self.sock.accept()
1865
conn.shutdown(socket.SHUT_RDWR)
1869
return 'bzr://127.0.0.1:%d/' % (self.port,)
1871
def stop_server(self):
1873
# make sure the thread dies by connecting to the listening socket,
1874
# just in case the test failed to do so.
1875
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1876
conn.connect(self.sock.getsockname())
1878
except socket.error: