621
429
repo = repo_dir.open_repository()
622
430
self.assertTrue(isinstance(target_format, repo._format.__class__))
625
class TestMisc(TestCase):
627
def test_unescape_xml(self):
628
"""We get some kind of error when malformed entities are passed"""
629
self.assertRaises(KeyError, repository._unescape_xml, 'foo&bar;')
632
class TestRepositoryFormatKnit3(TestCaseWithTransport):
634
def test_attribute__fetch_order(self):
635
"""Knits need topological data insertion."""
636
format = bzrdir.BzrDirMetaFormat1()
637
format.repository_format = knitrepo.RepositoryFormatKnit3()
638
repo = self.make_repository('.', format=format)
639
self.assertEqual('topological', repo._format._fetch_order)
641
def test_attribute__fetch_uses_deltas(self):
642
"""Knits reuse deltas."""
643
format = bzrdir.BzrDirMetaFormat1()
644
format.repository_format = knitrepo.RepositoryFormatKnit3()
645
repo = self.make_repository('.', format=format)
646
self.assertEqual(True, repo._format._fetch_uses_deltas)
648
def test_convert(self):
649
"""Ensure the upgrade adds weaves for roots"""
650
format = bzrdir.BzrDirMetaFormat1()
651
format.repository_format = knitrepo.RepositoryFormatKnit1()
652
tree = self.make_branch_and_tree('.', format)
653
tree.commit("Dull commit", rev_id="dull")
654
revision_tree = tree.branch.repository.revision_tree('dull')
655
revision_tree.lock_read()
657
self.assertRaises(errors.NoSuchFile, revision_tree.get_file_lines,
658
revision_tree.inventory.root.file_id)
660
revision_tree.unlock()
661
format = bzrdir.BzrDirMetaFormat1()
662
format.repository_format = knitrepo.RepositoryFormatKnit3()
663
upgrade.Convert('.', format)
664
tree = workingtree.WorkingTree.open('.')
665
revision_tree = tree.branch.repository.revision_tree('dull')
666
revision_tree.lock_read()
668
revision_tree.get_file_lines(revision_tree.inventory.root.file_id)
670
revision_tree.unlock()
671
tree.commit("Another dull commit", rev_id='dull2')
672
revision_tree = tree.branch.repository.revision_tree('dull2')
673
revision_tree.lock_read()
674
self.addCleanup(revision_tree.unlock)
675
self.assertEqual('dull', revision_tree.inventory.root.revision)
677
def test_supports_external_lookups(self):
678
format = bzrdir.BzrDirMetaFormat1()
679
format.repository_format = knitrepo.RepositoryFormatKnit3()
680
repo = self.make_repository('.', format=format)
681
self.assertFalse(repo._format.supports_external_lookups)
684
class Test2a(tests.TestCaseWithMemoryTransport):
686
def test_fetch_combines_groups(self):
687
builder = self.make_branch_builder('source', format='2a')
688
builder.start_series()
689
builder.build_snapshot('1', None, [
690
('add', ('', 'root-id', 'directory', '')),
691
('add', ('file', 'file-id', 'file', 'content\n'))])
692
builder.build_snapshot('2', ['1'], [
693
('modify', ('file-id', 'content-2\n'))])
694
builder.finish_series()
695
source = builder.get_branch()
696
target = self.make_repository('target', format='2a')
697
target.fetch(source.repository)
699
self.addCleanup(target.unlock)
700
details = target.texts._index.get_build_details(
701
[('file-id', '1',), ('file-id', '2',)])
702
file_1_details = details[('file-id', '1')]
703
file_2_details = details[('file-id', '2')]
704
# The index, and what to read off disk, should be the same for both
705
# versions of the file.
706
self.assertEqual(file_1_details[0][:3], file_2_details[0][:3])
708
def test_fetch_combines_groups(self):
709
builder = self.make_branch_builder('source', format='2a')
710
builder.start_series()
711
builder.build_snapshot('1', None, [
712
('add', ('', 'root-id', 'directory', '')),
713
('add', ('file', 'file-id', 'file', 'content\n'))])
714
builder.build_snapshot('2', ['1'], [
715
('modify', ('file-id', 'content-2\n'))])
716
builder.finish_series()
717
source = builder.get_branch()
718
target = self.make_repository('target', format='2a')
719
target.fetch(source.repository)
721
self.addCleanup(target.unlock)
722
details = target.texts._index.get_build_details(
723
[('file-id', '1',), ('file-id', '2',)])
724
file_1_details = details[('file-id', '1')]
725
file_2_details = details[('file-id', '2')]
726
# The index, and what to read off disk, should be the same for both
727
# versions of the file.
728
self.assertEqual(file_1_details[0][:3], file_2_details[0][:3])
730
def test_format_pack_compresses_True(self):
731
repo = self.make_repository('repo', format='2a')
732
self.assertTrue(repo._format.pack_compresses)
734
def test_inventories_use_chk_map_with_parent_base_dict(self):
735
tree = self.make_branch_and_memory_tree('repo', format="2a")
737
tree.add([''], ['TREE_ROOT'])
738
revid = tree.commit("foo")
741
self.addCleanup(tree.unlock)
742
inv = tree.branch.repository.get_inventory(revid)
743
self.assertNotEqual(None, inv.parent_id_basename_to_file_id)
744
inv.parent_id_basename_to_file_id._ensure_root()
745
inv.id_to_entry._ensure_root()
746
self.assertEqual(65536, inv.id_to_entry._root_node.maximum_size)
747
self.assertEqual(65536,
748
inv.parent_id_basename_to_file_id._root_node.maximum_size)
750
def test_autopack_unchanged_chk_nodes(self):
751
# at 20 unchanged commits, chk pages are packed that are split into
752
# two groups such that the new pack being made doesn't have all its
753
# pages in the source packs (though they are in the repository).
754
# Use a memory backed repository, we don't need to hit disk for this
755
tree = self.make_branch_and_memory_tree('tree', format='2a')
757
self.addCleanup(tree.unlock)
758
tree.add([''], ['TREE_ROOT'])
759
for pos in range(20):
760
tree.commit(str(pos))
762
def test_pack_with_hint(self):
763
tree = self.make_branch_and_memory_tree('tree', format='2a')
765
self.addCleanup(tree.unlock)
766
tree.add([''], ['TREE_ROOT'])
767
# 1 commit to leave untouched
769
to_keep = tree.branch.repository._pack_collection.names()
773
all = tree.branch.repository._pack_collection.names()
774
combine = list(set(all) - set(to_keep))
775
self.assertLength(3, all)
776
self.assertLength(2, combine)
777
tree.branch.repository.pack(hint=combine)
778
final = tree.branch.repository._pack_collection.names()
779
self.assertLength(2, final)
780
self.assertFalse(combine[0] in final)
781
self.assertFalse(combine[1] in final)
782
self.assertSubset(to_keep, final)
784
def test_stream_source_to_gc(self):
785
source = self.make_repository('source', format='2a')
786
target = self.make_repository('target', format='2a')
787
stream = source._get_source(target._format)
788
self.assertIsInstance(stream, groupcompress_repo.GroupCHKStreamSource)
790
def test_stream_source_to_non_gc(self):
791
source = self.make_repository('source', format='2a')
792
target = self.make_repository('target', format='rich-root-pack')
793
stream = source._get_source(target._format)
794
# We don't want the child GroupCHKStreamSource
795
self.assertIs(type(stream), repository.StreamSource)
797
def test_get_stream_for_missing_keys_includes_all_chk_refs(self):
798
source_builder = self.make_branch_builder('source',
800
# We have to build a fairly large tree, so that we are sure the chk
801
# pages will have split into multiple pages.
802
entries = [('add', ('', 'a-root-id', 'directory', None))]
803
for i in 'abcdefghijklmnopqrstuvwxyz123456789':
804
for j in 'abcdefghijklmnopqrstuvwxyz123456789':
807
content = 'content for %s\n' % (fname,)
808
entries.append(('add', (fname, fid, 'file', content)))
809
source_builder.start_series()
810
source_builder.build_snapshot('rev-1', None, entries)
811
# Now change a few of them, so we get a few new pages for the second
813
source_builder.build_snapshot('rev-2', ['rev-1'], [
814
('modify', ('aa-id', 'new content for aa-id\n')),
815
('modify', ('cc-id', 'new content for cc-id\n')),
816
('modify', ('zz-id', 'new content for zz-id\n')),
818
source_builder.finish_series()
819
source_branch = source_builder.get_branch()
820
source_branch.lock_read()
821
self.addCleanup(source_branch.unlock)
822
target = self.make_repository('target', format='2a')
823
source = source_branch.repository._get_source(target._format)
824
self.assertIsInstance(source, groupcompress_repo.GroupCHKStreamSource)
826
# On a regular pass, getting the inventories and chk pages for rev-2
827
# would only get the newly created chk pages
828
search = graph.SearchResult(set(['rev-2']), set(['rev-1']), 1,
830
simple_chk_records = []
831
for vf_name, substream in source.get_stream(search):
832
if vf_name == 'chk_bytes':
833
for record in substream:
834
simple_chk_records.append(record.key)
838
# 3 pages, the root (InternalNode), + 2 pages which actually changed
839
self.assertEqual([('sha1:91481f539e802c76542ea5e4c83ad416bf219f73',),
840
('sha1:4ff91971043668583985aec83f4f0ab10a907d3f',),
841
('sha1:81e7324507c5ca132eedaf2d8414ee4bb2226187',),
842
('sha1:b101b7da280596c71a4540e9a1eeba8045985ee0',)],
844
# Now, when we do a similar call using 'get_stream_for_missing_keys'
845
# we should get a much larger set of pages.
846
missing = [('inventories', 'rev-2')]
847
full_chk_records = []
848
for vf_name, substream in source.get_stream_for_missing_keys(missing):
849
if vf_name == 'inventories':
850
for record in substream:
851
self.assertEqual(('rev-2',), record.key)
852
elif vf_name == 'chk_bytes':
853
for record in substream:
854
full_chk_records.append(record.key)
856
self.fail('Should not be getting a stream of %s' % (vf_name,))
857
# We have 257 records now. This is because we have 1 root page, and 256
858
# leaf pages in a complete listing.
859
self.assertEqual(257, len(full_chk_records))
860
self.assertSubset(simple_chk_records, full_chk_records)
862
def test_inconsistency_fatal(self):
863
repo = self.make_repository('repo', format='2a')
864
self.assertTrue(repo.revisions._index._inconsistency_fatal)
865
self.assertFalse(repo.texts._index._inconsistency_fatal)
866
self.assertFalse(repo.inventories._index._inconsistency_fatal)
867
self.assertFalse(repo.signatures._index._inconsistency_fatal)
868
self.assertFalse(repo.chk_bytes._index._inconsistency_fatal)
871
class TestKnitPackStreamSource(tests.TestCaseWithMemoryTransport):
873
def test_source_to_exact_pack_092(self):
874
source = self.make_repository('source', format='pack-0.92')
875
target = self.make_repository('target', format='pack-0.92')
876
stream_source = source._get_source(target._format)
877
self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
879
def test_source_to_exact_pack_rich_root_pack(self):
880
source = self.make_repository('source', format='rich-root-pack')
881
target = self.make_repository('target', format='rich-root-pack')
882
stream_source = source._get_source(target._format)
883
self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
885
def test_source_to_exact_pack_19(self):
886
source = self.make_repository('source', format='1.9')
887
target = self.make_repository('target', format='1.9')
888
stream_source = source._get_source(target._format)
889
self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
891
def test_source_to_exact_pack_19_rich_root(self):
892
source = self.make_repository('source', format='1.9-rich-root')
893
target = self.make_repository('target', format='1.9-rich-root')
894
stream_source = source._get_source(target._format)
895
self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
897
def test_source_to_remote_exact_pack_19(self):
898
trans = self.make_smart_server('target')
900
source = self.make_repository('source', format='1.9')
901
target = self.make_repository('target', format='1.9')
902
target = repository.Repository.open(trans.base)
903
stream_source = source._get_source(target._format)
904
self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
906
def test_stream_source_to_non_exact(self):
907
source = self.make_repository('source', format='pack-0.92')
908
target = self.make_repository('target', format='1.9')
909
stream = source._get_source(target._format)
910
self.assertIs(type(stream), repository.StreamSource)
912
def test_stream_source_to_non_exact_rich_root(self):
913
source = self.make_repository('source', format='1.9')
914
target = self.make_repository('target', format='1.9-rich-root')
915
stream = source._get_source(target._format)
916
self.assertIs(type(stream), repository.StreamSource)
918
def test_source_to_remote_non_exact_pack_19(self):
919
trans = self.make_smart_server('target')
921
source = self.make_repository('source', format='1.9')
922
target = self.make_repository('target', format='1.6')
923
target = repository.Repository.open(trans.base)
924
stream_source = source._get_source(target._format)
925
self.assertIs(type(stream_source), repository.StreamSource)
927
def test_stream_source_to_knit(self):
928
source = self.make_repository('source', format='pack-0.92')
929
target = self.make_repository('target', format='dirstate')
930
stream = source._get_source(target._format)
931
self.assertIs(type(stream), repository.StreamSource)
934
class TestDevelopment6FindParentIdsOfRevisions(TestCaseWithTransport):
935
"""Tests for _find_parent_ids_of_revisions."""
938
super(TestDevelopment6FindParentIdsOfRevisions, self).setUp()
939
self.builder = self.make_branch_builder('source',
940
format='development6-rich-root')
941
self.builder.start_series()
942
self.builder.build_snapshot('initial', None,
943
[('add', ('', 'tree-root', 'directory', None))])
944
self.repo = self.builder.get_branch().repository
945
self.addCleanup(self.builder.finish_series)
947
def assertParentIds(self, expected_result, rev_set):
948
self.assertEqual(sorted(expected_result),
949
sorted(self.repo._find_parent_ids_of_revisions(rev_set)))
951
def test_simple(self):
952
self.builder.build_snapshot('revid1', None, [])
953
self.builder.build_snapshot('revid2', ['revid1'], [])
955
self.assertParentIds(['revid1'], rev_set)
957
def test_not_first_parent(self):
958
self.builder.build_snapshot('revid1', None, [])
959
self.builder.build_snapshot('revid2', ['revid1'], [])
960
self.builder.build_snapshot('revid3', ['revid2'], [])
961
rev_set = ['revid3', 'revid2']
962
self.assertParentIds(['revid1'], rev_set)
964
def test_not_null(self):
965
rev_set = ['initial']
966
self.assertParentIds([], rev_set)
968
def test_not_null_set(self):
969
self.builder.build_snapshot('revid1', None, [])
970
rev_set = [_mod_revision.NULL_REVISION]
971
self.assertParentIds([], rev_set)
973
def test_ghost(self):
974
self.builder.build_snapshot('revid1', None, [])
975
rev_set = ['ghost', 'revid1']
976
self.assertParentIds(['initial'], rev_set)
978
def test_ghost_parent(self):
979
self.builder.build_snapshot('revid1', None, [])
980
self.builder.build_snapshot('revid2', ['revid1', 'ghost'], [])
981
rev_set = ['revid2', 'revid1']
982
self.assertParentIds(['ghost', 'initial'], rev_set)
984
def test_righthand_parent(self):
985
self.builder.build_snapshot('revid1', None, [])
986
self.builder.build_snapshot('revid2a', ['revid1'], [])
987
self.builder.build_snapshot('revid2b', ['revid1'], [])
988
self.builder.build_snapshot('revid3', ['revid2a', 'revid2b'], [])
989
rev_set = ['revid3', 'revid2a']
990
self.assertParentIds(['revid1', 'revid2b'], rev_set)
993
class TestWithBrokenRepo(TestCaseWithTransport):
994
"""These tests seem to be more appropriate as interface tests?"""
996
def make_broken_repository(self):
997
# XXX: This function is borrowed from Aaron's "Reconcile can fix bad
998
# parent references" branch which is due to land in bzr.dev soon. Once
999
# it does, this duplication should be removed.
1000
repo = self.make_repository('broken-repo')
1004
cleanups.append(repo.unlock)
1005
repo.start_write_group()
1006
cleanups.append(repo.commit_write_group)
1007
# make rev1a: A well-formed revision, containing 'file1'
1008
inv = inventory.Inventory(revision_id='rev1a')
1009
inv.root.revision = 'rev1a'
1010
self.add_file(repo, inv, 'file1', 'rev1a', [])
1011
repo.add_inventory('rev1a', inv, [])
1012
revision = _mod_revision.Revision('rev1a',
1013
committer='jrandom@example.com', timestamp=0,
1014
inventory_sha1='', timezone=0, message='foo', parent_ids=[])
1015
repo.add_revision('rev1a',revision, inv)
1017
# make rev1b, which has no Revision, but has an Inventory, and
1019
inv = inventory.Inventory(revision_id='rev1b')
1020
inv.root.revision = 'rev1b'
1021
self.add_file(repo, inv, 'file1', 'rev1b', [])
1022
repo.add_inventory('rev1b', inv, [])
1024
# make rev2, with file1 and file2
1026
# file1 has 'rev1b' as an ancestor, even though this is not
1027
# mentioned by 'rev1a', making it an unreferenced ancestor
1028
inv = inventory.Inventory()
1029
self.add_file(repo, inv, 'file1', 'rev2', ['rev1a', 'rev1b'])
1030
self.add_file(repo, inv, 'file2', 'rev2', [])
1031
self.add_revision(repo, 'rev2', inv, ['rev1a'])
1033
# make ghost revision rev1c
1034
inv = inventory.Inventory()
1035
self.add_file(repo, inv, 'file2', 'rev1c', [])
1037
# make rev3 with file2
1038
# file2 refers to 'rev1c', which is a ghost in this repository, so
1039
# file2 cannot have rev1c as its ancestor.
1040
inv = inventory.Inventory()
1041
self.add_file(repo, inv, 'file2', 'rev3', ['rev1c'])
1042
self.add_revision(repo, 'rev3', inv, ['rev1c'])
1045
for cleanup in reversed(cleanups):
1048
def add_revision(self, repo, revision_id, inv, parent_ids):
1049
inv.revision_id = revision_id
1050
inv.root.revision = revision_id
1051
repo.add_inventory(revision_id, inv, parent_ids)
1052
revision = _mod_revision.Revision(revision_id,
1053
committer='jrandom@example.com', timestamp=0, inventory_sha1='',
1054
timezone=0, message='foo', parent_ids=parent_ids)
1055
repo.add_revision(revision_id,revision, inv)
1057
def add_file(self, repo, inv, filename, revision, parents):
1058
file_id = filename + '-id'
1059
entry = inventory.InventoryFile(file_id, filename, 'TREE_ROOT')
1060
entry.revision = revision
1063
text_key = (file_id, revision)
1064
parent_keys = [(file_id, parent) for parent in parents]
1065
repo.texts.add_lines(text_key, parent_keys, ['line\n'])
1067
def test_insert_from_broken_repo(self):
1068
"""Inserting a data stream from a broken repository won't silently
1069
corrupt the target repository.
1071
broken_repo = self.make_broken_repository()
1072
empty_repo = self.make_repository('empty-repo')
1074
empty_repo.fetch(broken_repo)
1075
except (errors.RevisionNotPresent, errors.BzrCheckError):
1076
# Test successful: compression parent not being copied leads to
1079
empty_repo.lock_read()
1080
self.addCleanup(empty_repo.unlock)
1081
text = empty_repo.texts.get_record_stream(
1082
[('file2-id', 'rev3')], 'topological', True).next()
1083
self.assertEqual('line\n', text.get_bytes_as('fulltext'))
1086
class TestRepositoryPackCollection(TestCaseWithTransport):
1088
def get_format(self):
1089
return bzrdir.format_registry.make_bzrdir('pack-0.92')
1091
def get_packs(self):
1092
format = self.get_format()
1093
repo = self.make_repository('.', format=format)
1094
return repo._pack_collection
1096
def make_packs_and_alt_repo(self, write_lock=False):
1097
"""Create a pack repo with 3 packs, and access it via a second repo."""
1098
tree = self.make_branch_and_tree('.', format=self.get_format())
1100
self.addCleanup(tree.unlock)
1101
rev1 = tree.commit('one')
1102
rev2 = tree.commit('two')
1103
rev3 = tree.commit('three')
1104
r = repository.Repository.open('.')
1109
self.addCleanup(r.unlock)
1110
packs = r._pack_collection
1111
packs.ensure_loaded()
1112
return tree, r, packs, [rev1, rev2, rev3]
1114
def test__max_pack_count(self):
1115
"""The maximum pack count is a function of the number of revisions."""
1116
# no revisions - one pack, so that we can have a revision free repo
1117
# without it blowing up
1118
packs = self.get_packs()
1119
self.assertEqual(1, packs._max_pack_count(0))
1120
# after that the sum of the digits, - check the first 1-9
1121
self.assertEqual(1, packs._max_pack_count(1))
1122
self.assertEqual(2, packs._max_pack_count(2))
1123
self.assertEqual(3, packs._max_pack_count(3))
1124
self.assertEqual(4, packs._max_pack_count(4))
1125
self.assertEqual(5, packs._max_pack_count(5))
1126
self.assertEqual(6, packs._max_pack_count(6))
1127
self.assertEqual(7, packs._max_pack_count(7))
1128
self.assertEqual(8, packs._max_pack_count(8))
1129
self.assertEqual(9, packs._max_pack_count(9))
1130
# check the boundary cases with two digits for the next decade
1131
self.assertEqual(1, packs._max_pack_count(10))
1132
self.assertEqual(2, packs._max_pack_count(11))
1133
self.assertEqual(10, packs._max_pack_count(19))
1134
self.assertEqual(2, packs._max_pack_count(20))
1135
self.assertEqual(3, packs._max_pack_count(21))
1136
# check some arbitrary big numbers
1137
self.assertEqual(25, packs._max_pack_count(112894))
1139
def test_pack_distribution_zero(self):
1140
packs = self.get_packs()
1141
self.assertEqual([0], packs.pack_distribution(0))
1143
def test_ensure_loaded_unlocked(self):
1144
packs = self.get_packs()
1145
self.assertRaises(errors.ObjectNotLocked,
1146
packs.ensure_loaded)
1148
def test_pack_distribution_one_to_nine(self):
1149
packs = self.get_packs()
1150
self.assertEqual([1],
1151
packs.pack_distribution(1))
1152
self.assertEqual([1, 1],
1153
packs.pack_distribution(2))
1154
self.assertEqual([1, 1, 1],
1155
packs.pack_distribution(3))
1156
self.assertEqual([1, 1, 1, 1],
1157
packs.pack_distribution(4))
1158
self.assertEqual([1, 1, 1, 1, 1],
1159
packs.pack_distribution(5))
1160
self.assertEqual([1, 1, 1, 1, 1, 1],
1161
packs.pack_distribution(6))
1162
self.assertEqual([1, 1, 1, 1, 1, 1, 1],
1163
packs.pack_distribution(7))
1164
self.assertEqual([1, 1, 1, 1, 1, 1, 1, 1],
1165
packs.pack_distribution(8))
1166
self.assertEqual([1, 1, 1, 1, 1, 1, 1, 1, 1],
1167
packs.pack_distribution(9))
1169
def test_pack_distribution_stable_at_boundaries(self):
1170
"""When there are multi-rev packs the counts are stable."""
1171
packs = self.get_packs()
1173
self.assertEqual([10], packs.pack_distribution(10))
1174
self.assertEqual([10, 1], packs.pack_distribution(11))
1175
self.assertEqual([10, 10], packs.pack_distribution(20))
1176
self.assertEqual([10, 10, 1], packs.pack_distribution(21))
1178
self.assertEqual([100], packs.pack_distribution(100))
1179
self.assertEqual([100, 1], packs.pack_distribution(101))
1180
self.assertEqual([100, 10, 1], packs.pack_distribution(111))
1181
self.assertEqual([100, 100], packs.pack_distribution(200))
1182
self.assertEqual([100, 100, 1], packs.pack_distribution(201))
1183
self.assertEqual([100, 100, 10, 1], packs.pack_distribution(211))
1185
def test_plan_pack_operations_2009_revisions_skip_all_packs(self):
1186
packs = self.get_packs()
1187
existing_packs = [(2000, "big"), (9, "medium")]
1188
# rev count - 2009 -> 2x1000 + 9x1
1189
pack_operations = packs.plan_autopack_combinations(
1190
existing_packs, [1000, 1000, 1, 1, 1, 1, 1, 1, 1, 1, 1])
1191
self.assertEqual([], pack_operations)
1193
def test_plan_pack_operations_2010_revisions_skip_all_packs(self):
1194
packs = self.get_packs()
1195
existing_packs = [(2000, "big"), (9, "medium"), (1, "single")]
1196
# rev count - 2010 -> 2x1000 + 1x10
1197
pack_operations = packs.plan_autopack_combinations(
1198
existing_packs, [1000, 1000, 10])
1199
self.assertEqual([], pack_operations)
1201
def test_plan_pack_operations_2010_combines_smallest_two(self):
1202
packs = self.get_packs()
1203
existing_packs = [(1999, "big"), (9, "medium"), (1, "single2"),
1205
# rev count - 2010 -> 2x1000 + 1x10 (3)
1206
pack_operations = packs.plan_autopack_combinations(
1207
existing_packs, [1000, 1000, 10])
1208
self.assertEqual([[2, ["single2", "single1"]]], pack_operations)
1210
def test_plan_pack_operations_creates_a_single_op(self):
1211
packs = self.get_packs()
1212
existing_packs = [(50, 'a'), (40, 'b'), (30, 'c'), (10, 'd'),
1213
(10, 'e'), (6, 'f'), (4, 'g')]
1214
# rev count 150 -> 1x100 and 5x10
1215
# The two size 10 packs do not need to be touched. The 50, 40, 30 would
1216
# be combined into a single 120 size pack, and the 6 & 4 would
1217
# becombined into a size 10 pack. However, if we have to rewrite them,
1218
# we save a pack file with no increased I/O by putting them into the
1220
distribution = packs.pack_distribution(150)
1221
pack_operations = packs.plan_autopack_combinations(existing_packs,
1223
self.assertEqual([[130, ['a', 'b', 'c', 'f', 'g']]], pack_operations)
1225
def test_all_packs_none(self):
1226
format = self.get_format()
1227
tree = self.make_branch_and_tree('.', format=format)
1229
self.addCleanup(tree.unlock)
1230
packs = tree.branch.repository._pack_collection
1231
packs.ensure_loaded()
1232
self.assertEqual([], packs.all_packs())
1234
def test_all_packs_one(self):
1235
format = self.get_format()
1236
tree = self.make_branch_and_tree('.', format=format)
1237
tree.commit('start')
1239
self.addCleanup(tree.unlock)
1240
packs = tree.branch.repository._pack_collection
1241
packs.ensure_loaded()
1243
packs.get_pack_by_name(packs.names()[0])],
1246
def test_all_packs_two(self):
1247
format = self.get_format()
1248
tree = self.make_branch_and_tree('.', format=format)
1249
tree.commit('start')
1250
tree.commit('continue')
1252
self.addCleanup(tree.unlock)
1253
packs = tree.branch.repository._pack_collection
1254
packs.ensure_loaded()
1256
packs.get_pack_by_name(packs.names()[0]),
1257
packs.get_pack_by_name(packs.names()[1]),
1258
], packs.all_packs())
1260
def test_get_pack_by_name(self):
1261
format = self.get_format()
1262
tree = self.make_branch_and_tree('.', format=format)
1263
tree.commit('start')
1265
self.addCleanup(tree.unlock)
1266
packs = tree.branch.repository._pack_collection
1268
packs.ensure_loaded()
1269
name = packs.names()[0]
1270
pack_1 = packs.get_pack_by_name(name)
1271
# the pack should be correctly initialised
1272
sizes = packs._names[name]
1273
rev_index = GraphIndex(packs._index_transport, name + '.rix', sizes[0])
1274
inv_index = GraphIndex(packs._index_transport, name + '.iix', sizes[1])
1275
txt_index = GraphIndex(packs._index_transport, name + '.tix', sizes[2])
1276
sig_index = GraphIndex(packs._index_transport, name + '.six', sizes[3])
1277
self.assertEqual(pack_repo.ExistingPack(packs._pack_transport,
1278
name, rev_index, inv_index, txt_index, sig_index), pack_1)
1279
# and the same instance should be returned on successive calls.
1280
self.assertTrue(pack_1 is packs.get_pack_by_name(name))
1282
def test_reload_pack_names_new_entry(self):
1283
tree, r, packs, revs = self.make_packs_and_alt_repo()
1284
names = packs.names()
1285
# Add a new pack file into the repository
1286
rev4 = tree.commit('four')
1287
new_names = tree.branch.repository._pack_collection.names()
1288
new_name = set(new_names).difference(names)
1289
self.assertEqual(1, len(new_name))
1290
new_name = new_name.pop()
1291
# The old collection hasn't noticed yet
1292
self.assertEqual(names, packs.names())
1293
self.assertTrue(packs.reload_pack_names())
1294
self.assertEqual(new_names, packs.names())
1295
# And the repository can access the new revision
1296
self.assertEqual({rev4:(revs[-1],)}, r.get_parent_map([rev4]))
1297
self.assertFalse(packs.reload_pack_names())
1299
def test_reload_pack_names_added_and_removed(self):
1300
tree, r, packs, revs = self.make_packs_and_alt_repo()
1301
names = packs.names()
1302
# Now repack the whole thing
1303
tree.branch.repository.pack()
1304
new_names = tree.branch.repository._pack_collection.names()
1305
# The other collection hasn't noticed yet
1306
self.assertEqual(names, packs.names())
1307
self.assertTrue(packs.reload_pack_names())
1308
self.assertEqual(new_names, packs.names())
1309
self.assertEqual({revs[-1]:(revs[-2],)}, r.get_parent_map([revs[-1]]))
1310
self.assertFalse(packs.reload_pack_names())
1312
def test_autopack_reloads_and_stops(self):
1313
tree, r, packs, revs = self.make_packs_and_alt_repo(write_lock=True)
1314
# After we have determined what needs to be autopacked, trigger a
1315
# full-pack via the other repo which will cause us to re-evaluate and
1316
# decide we don't need to do anything
1317
orig_execute = packs._execute_pack_operations
1318
def _munged_execute_pack_ops(*args, **kwargs):
1319
tree.branch.repository.pack()
1320
return orig_execute(*args, **kwargs)
1321
packs._execute_pack_operations = _munged_execute_pack_ops
1322
packs._max_pack_count = lambda x: 1
1323
packs.pack_distribution = lambda x: [10]
1324
self.assertFalse(packs.autopack())
1325
self.assertEqual(1, len(packs.names()))
1326
self.assertEqual(tree.branch.repository._pack_collection.names(),
1330
class TestPack(TestCaseWithTransport):
1331
"""Tests for the Pack object."""
1333
def assertCurrentlyEqual(self, left, right):
1334
self.assertTrue(left == right)
1335
self.assertTrue(right == left)
1336
self.assertFalse(left != right)
1337
self.assertFalse(right != left)
1339
def assertCurrentlyNotEqual(self, left, right):
1340
self.assertFalse(left == right)
1341
self.assertFalse(right == left)
1342
self.assertTrue(left != right)
1343
self.assertTrue(right != left)
1345
def test___eq____ne__(self):
1346
left = pack_repo.ExistingPack('', '', '', '', '', '')
1347
right = pack_repo.ExistingPack('', '', '', '', '', '')
1348
self.assertCurrentlyEqual(left, right)
1349
# change all attributes and ensure equality changes as we do.
1350
left.revision_index = 'a'
1351
self.assertCurrentlyNotEqual(left, right)
1352
right.revision_index = 'a'
1353
self.assertCurrentlyEqual(left, right)
1354
left.inventory_index = 'a'
1355
self.assertCurrentlyNotEqual(left, right)
1356
right.inventory_index = 'a'
1357
self.assertCurrentlyEqual(left, right)
1358
left.text_index = 'a'
1359
self.assertCurrentlyNotEqual(left, right)
1360
right.text_index = 'a'
1361
self.assertCurrentlyEqual(left, right)
1362
left.signature_index = 'a'
1363
self.assertCurrentlyNotEqual(left, right)
1364
right.signature_index = 'a'
1365
self.assertCurrentlyEqual(left, right)
1367
self.assertCurrentlyNotEqual(left, right)
1369
self.assertCurrentlyEqual(left, right)
1370
left.transport = 'a'
1371
self.assertCurrentlyNotEqual(left, right)
1372
right.transport = 'a'
1373
self.assertCurrentlyEqual(left, right)
1375
def test_file_name(self):
1376
pack = pack_repo.ExistingPack('', 'a_name', '', '', '', '')
1377
self.assertEqual('a_name.pack', pack.file_name())
1380
class TestNewPack(TestCaseWithTransport):
1381
"""Tests for pack_repo.NewPack."""
1383
def test_new_instance_attributes(self):
1384
upload_transport = self.get_transport('upload')
1385
pack_transport = self.get_transport('pack')
1386
index_transport = self.get_transport('index')
1387
upload_transport.mkdir('.')
1388
collection = pack_repo.RepositoryPackCollection(
1390
transport=self.get_transport('.'),
1391
index_transport=index_transport,
1392
upload_transport=upload_transport,
1393
pack_transport=pack_transport,
1394
index_builder_class=BTreeBuilder,
1395
index_class=BTreeGraphIndex,
1396
use_chk_index=False)
1397
pack = pack_repo.NewPack(collection)
1398
self.assertIsInstance(pack.revision_index, BTreeBuilder)
1399
self.assertIsInstance(pack.inventory_index, BTreeBuilder)
1400
self.assertIsInstance(pack._hash, type(osutils.md5()))
1401
self.assertTrue(pack.upload_transport is upload_transport)
1402
self.assertTrue(pack.index_transport is index_transport)
1403
self.assertTrue(pack.pack_transport is pack_transport)
1404
self.assertEqual(None, pack.index_sizes)
1405
self.assertEqual(20, len(pack.random_name))
1406
self.assertIsInstance(pack.random_name, str)
1407
self.assertIsInstance(pack.start_time, float)
1410
class TestPacker(TestCaseWithTransport):
1411
"""Tests for the packs repository Packer class."""
1413
def test_pack_optimizes_pack_order(self):
1414
builder = self.make_branch_builder('.', format="1.9")
1415
builder.start_series()
1416
builder.build_snapshot('A', None, [
1417
('add', ('', 'root-id', 'directory', None)),
1418
('add', ('f', 'f-id', 'file', 'content\n'))])
1419
builder.build_snapshot('B', ['A'],
1420
[('modify', ('f-id', 'new-content\n'))])
1421
builder.build_snapshot('C', ['B'],
1422
[('modify', ('f-id', 'third-content\n'))])
1423
builder.build_snapshot('D', ['C'],
1424
[('modify', ('f-id', 'fourth-content\n'))])
1425
b = builder.get_branch()
1427
builder.finish_series()
1428
self.addCleanup(b.unlock)
1429
# At this point, we should have 4 pack files available
1430
# Because of how they were built, they correspond to
1431
# ['D', 'C', 'B', 'A']
1432
packs = b.repository._pack_collection.packs
1433
packer = pack_repo.Packer(b.repository._pack_collection,
1435
revision_ids=['B', 'C'])
1436
# Now, when we are copying the B & C revisions, their pack files should
1437
# be moved to the front of the stack
1438
# The new ordering moves B & C to the front of the .packs attribute,
1439
# and leaves the others in the original order.
1440
new_packs = [packs[1], packs[2], packs[0], packs[3]]
1441
new_pack = packer.pack()
1442
self.assertEqual(new_packs, packer.packs)
1445
class TestOptimisingPacker(TestCaseWithTransport):
1446
"""Tests for the OptimisingPacker class."""
1448
def get_pack_collection(self):
1449
repo = self.make_repository('.')
1450
return repo._pack_collection
1452
def test_open_pack_will_optimise(self):
1453
packer = pack_repo.OptimisingPacker(self.get_pack_collection(),
1455
new_pack = packer.open_pack()
1456
self.assertIsInstance(new_pack, pack_repo.NewPack)
1457
self.assertTrue(new_pack.revision_index._optimize_for_size)
1458
self.assertTrue(new_pack.inventory_index._optimize_for_size)
1459
self.assertTrue(new_pack.text_index._optimize_for_size)
1460
self.assertTrue(new_pack.signature_index._optimize_for_size)
1463
class TestCrossFormatPacks(TestCaseWithTransport):
1465
def log_pack(self, hint=None):
1466
self.calls.append(('pack', hint))
1467
self.orig_pack(hint=hint)
1468
if self.expect_hint:
1469
self.assertTrue(hint)
1471
def run_stream(self, src_fmt, target_fmt, expect_pack_called):
1472
self.expect_hint = expect_pack_called
1474
source_tree = self.make_branch_and_tree('src', format=src_fmt)
1475
source_tree.lock_write()
1476
self.addCleanup(source_tree.unlock)
1477
tip = source_tree.commit('foo')
1478
target = self.make_repository('target', format=target_fmt)
1480
self.addCleanup(target.unlock)
1481
source = source_tree.branch.repository._get_source(target._format)
1482
self.orig_pack = target.pack
1483
target.pack = self.log_pack
1484
search = target.search_missing_revision_ids(
1485
source_tree.branch.repository, tip)
1486
stream = source.get_stream(search)
1487
from_format = source_tree.branch.repository._format
1488
sink = target._get_sink()
1489
sink.insert_stream(stream, from_format, [])
1490
if expect_pack_called:
1491
self.assertLength(1, self.calls)
1493
self.assertLength(0, self.calls)
1495
def run_fetch(self, src_fmt, target_fmt, expect_pack_called):
1496
self.expect_hint = expect_pack_called
1498
source_tree = self.make_branch_and_tree('src', format=src_fmt)
1499
source_tree.lock_write()
1500
self.addCleanup(source_tree.unlock)
1501
tip = source_tree.commit('foo')
1502
target = self.make_repository('target', format=target_fmt)
1504
self.addCleanup(target.unlock)
1505
source = source_tree.branch.repository
1506
self.orig_pack = target.pack
1507
target.pack = self.log_pack
1508
target.fetch(source)
1509
if expect_pack_called:
1510
self.assertLength(1, self.calls)
1512
self.assertLength(0, self.calls)
1514
def test_sink_format_hint_no(self):
1515
# When the target format says packing makes no difference, pack is not
1517
self.run_stream('1.9', 'rich-root-pack', False)
1519
def test_sink_format_hint_yes(self):
1520
# When the target format says packing makes a difference, pack is
1522
self.run_stream('1.9', '2a', True)
1524
def test_sink_format_same_no(self):
1525
# When the formats are the same, pack is not called.
1526
self.run_stream('2a', '2a', False)
1528
def test_IDS_format_hint_no(self):
1529
# When the target format says packing makes no difference, pack is not
1531
self.run_fetch('1.9', 'rich-root-pack', False)
1533
def test_IDS_format_hint_yes(self):
1534
# When the target format says packing makes a difference, pack is
1536
self.run_fetch('1.9', '2a', True)
1538
def test_IDS_format_same_no(self):
1539
# When the formats are the same, pack is not called.
1540
self.run_fetch('2a', '2a', False)