621
428
repo = repo_dir.open_repository()
622
429
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(TestCaseWithTransport):
686
def test_format_pack_compresses_True(self):
687
repo = self.make_repository('repo', format='2a')
688
self.assertTrue(repo._format.pack_compresses)
690
def test_inventories_use_chk_map_with_parent_base_dict(self):
691
tree = self.make_branch_and_tree('repo', format="2a")
692
revid = tree.commit("foo")
694
self.addCleanup(tree.unlock)
695
inv = tree.branch.repository.get_inventory(revid)
696
self.assertNotEqual(None, inv.parent_id_basename_to_file_id)
697
inv.parent_id_basename_to_file_id._ensure_root()
698
inv.id_to_entry._ensure_root()
699
self.assertEqual(65536, inv.id_to_entry._root_node.maximum_size)
700
self.assertEqual(65536,
701
inv.parent_id_basename_to_file_id._root_node.maximum_size)
703
def test_autopack_unchanged_chk_nodes(self):
704
# at 20 unchanged commits, chk pages are packed that are split into
705
# two groups such that the new pack being made doesn't have all its
706
# pages in the source packs (though they are in the repository).
707
tree = self.make_branch_and_tree('tree', format='2a')
708
for pos in range(20):
709
tree.commit(str(pos))
711
def test_pack_with_hint(self):
712
tree = self.make_branch_and_tree('tree', format='2a')
713
# 1 commit to leave untouched
715
to_keep = tree.branch.repository._pack_collection.names()
719
all = tree.branch.repository._pack_collection.names()
720
combine = list(set(all) - set(to_keep))
721
self.assertLength(3, all)
722
self.assertLength(2, combine)
723
tree.branch.repository.pack(hint=combine)
724
final = tree.branch.repository._pack_collection.names()
725
self.assertLength(2, final)
726
self.assertFalse(combine[0] in final)
727
self.assertFalse(combine[1] in final)
728
self.assertSubset(to_keep, final)
730
def test_stream_source_to_gc(self):
731
source = self.make_repository('source', format='2a')
732
target = self.make_repository('target', format='2a')
733
stream = source._get_source(target._format)
734
self.assertIsInstance(stream, groupcompress_repo.GroupCHKStreamSource)
736
def test_stream_source_to_non_gc(self):
737
source = self.make_repository('source', format='2a')
738
target = self.make_repository('target', format='rich-root-pack')
739
stream = source._get_source(target._format)
740
# We don't want the child GroupCHKStreamSource
741
self.assertIs(type(stream), repository.StreamSource)
743
def test_get_stream_for_missing_keys_includes_all_chk_refs(self):
744
source_builder = self.make_branch_builder('source',
746
# We have to build a fairly large tree, so that we are sure the chk
747
# pages will have split into multiple pages.
748
entries = [('add', ('', 'a-root-id', 'directory', None))]
749
for i in 'abcdefghijklmnopqrstuvwxyz123456789':
750
for j in 'abcdefghijklmnopqrstuvwxyz123456789':
753
content = 'content for %s\n' % (fname,)
754
entries.append(('add', (fname, fid, 'file', content)))
755
source_builder.start_series()
756
source_builder.build_snapshot('rev-1', None, entries)
757
# Now change a few of them, so we get a few new pages for the second
759
source_builder.build_snapshot('rev-2', ['rev-1'], [
760
('modify', ('aa-id', 'new content for aa-id\n')),
761
('modify', ('cc-id', 'new content for cc-id\n')),
762
('modify', ('zz-id', 'new content for zz-id\n')),
764
source_builder.finish_series()
765
source_branch = source_builder.get_branch()
766
source_branch.lock_read()
767
self.addCleanup(source_branch.unlock)
768
target = self.make_repository('target', format='2a')
769
source = source_branch.repository._get_source(target._format)
770
self.assertIsInstance(source, groupcompress_repo.GroupCHKStreamSource)
772
# On a regular pass, getting the inventories and chk pages for rev-2
773
# would only get the newly created chk pages
774
search = graph.SearchResult(set(['rev-2']), set(['rev-1']), 1,
776
simple_chk_records = []
777
for vf_name, substream in source.get_stream(search):
778
if vf_name == 'chk_bytes':
779
for record in substream:
780
simple_chk_records.append(record.key)
784
# 3 pages, the root (InternalNode), + 2 pages which actually changed
785
self.assertEqual([('sha1:91481f539e802c76542ea5e4c83ad416bf219f73',),
786
('sha1:4ff91971043668583985aec83f4f0ab10a907d3f',),
787
('sha1:81e7324507c5ca132eedaf2d8414ee4bb2226187',),
788
('sha1:b101b7da280596c71a4540e9a1eeba8045985ee0',)],
790
# Now, when we do a similar call using 'get_stream_for_missing_keys'
791
# we should get a much larger set of pages.
792
missing = [('inventories', 'rev-2')]
793
full_chk_records = []
794
for vf_name, substream in source.get_stream_for_missing_keys(missing):
795
if vf_name == 'inventories':
796
for record in substream:
797
self.assertEqual(('rev-2',), record.key)
798
elif vf_name == 'chk_bytes':
799
for record in substream:
800
full_chk_records.append(record.key)
802
self.fail('Should not be getting a stream of %s' % (vf_name,))
803
# We have 257 records now. This is because we have 1 root page, and 256
804
# leaf pages in a complete listing.
805
self.assertEqual(257, len(full_chk_records))
806
self.assertSubset(simple_chk_records, full_chk_records)
808
def test_inconsistency_fatal(self):
809
repo = self.make_repository('repo', format='2a')
810
self.assertTrue(repo.revisions._index._inconsistency_fatal)
811
self.assertFalse(repo.texts._index._inconsistency_fatal)
812
self.assertFalse(repo.inventories._index._inconsistency_fatal)
813
self.assertFalse(repo.signatures._index._inconsistency_fatal)
814
self.assertFalse(repo.chk_bytes._index._inconsistency_fatal)
817
class TestKnitPackStreamSource(tests.TestCaseWithMemoryTransport):
819
def test_source_to_exact_pack_092(self):
820
source = self.make_repository('source', format='pack-0.92')
821
target = self.make_repository('target', format='pack-0.92')
822
stream_source = source._get_source(target._format)
823
self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
825
def test_source_to_exact_pack_rich_root_pack(self):
826
source = self.make_repository('source', format='rich-root-pack')
827
target = self.make_repository('target', format='rich-root-pack')
828
stream_source = source._get_source(target._format)
829
self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
831
def test_source_to_exact_pack_19(self):
832
source = self.make_repository('source', format='1.9')
833
target = self.make_repository('target', format='1.9')
834
stream_source = source._get_source(target._format)
835
self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
837
def test_source_to_exact_pack_19_rich_root(self):
838
source = self.make_repository('source', format='1.9-rich-root')
839
target = self.make_repository('target', format='1.9-rich-root')
840
stream_source = source._get_source(target._format)
841
self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
843
def test_source_to_remote_exact_pack_19(self):
844
trans = self.make_smart_server('target')
846
source = self.make_repository('source', format='1.9')
847
target = self.make_repository('target', format='1.9')
848
target = repository.Repository.open(trans.base)
849
stream_source = source._get_source(target._format)
850
self.assertIsInstance(stream_source, pack_repo.KnitPackStreamSource)
852
def test_stream_source_to_non_exact(self):
853
source = self.make_repository('source', format='pack-0.92')
854
target = self.make_repository('target', format='1.9')
855
stream = source._get_source(target._format)
856
self.assertIs(type(stream), repository.StreamSource)
858
def test_stream_source_to_non_exact_rich_root(self):
859
source = self.make_repository('source', format='1.9')
860
target = self.make_repository('target', format='1.9-rich-root')
861
stream = source._get_source(target._format)
862
self.assertIs(type(stream), repository.StreamSource)
864
def test_source_to_remote_non_exact_pack_19(self):
865
trans = self.make_smart_server('target')
867
source = self.make_repository('source', format='1.9')
868
target = self.make_repository('target', format='1.6')
869
target = repository.Repository.open(trans.base)
870
stream_source = source._get_source(target._format)
871
self.assertIs(type(stream_source), repository.StreamSource)
873
def test_stream_source_to_knit(self):
874
source = self.make_repository('source', format='pack-0.92')
875
target = self.make_repository('target', format='dirstate')
876
stream = source._get_source(target._format)
877
self.assertIs(type(stream), repository.StreamSource)
880
class TestDevelopment6FindParentIdsOfRevisions(TestCaseWithTransport):
881
"""Tests for _find_parent_ids_of_revisions."""
884
super(TestDevelopment6FindParentIdsOfRevisions, self).setUp()
885
self.builder = self.make_branch_builder('source',
886
format='development6-rich-root')
887
self.builder.start_series()
888
self.builder.build_snapshot('initial', None,
889
[('add', ('', 'tree-root', 'directory', None))])
890
self.repo = self.builder.get_branch().repository
891
self.addCleanup(self.builder.finish_series)
893
def assertParentIds(self, expected_result, rev_set):
894
self.assertEqual(sorted(expected_result),
895
sorted(self.repo._find_parent_ids_of_revisions(rev_set)))
897
def test_simple(self):
898
self.builder.build_snapshot('revid1', None, [])
899
self.builder.build_snapshot('revid2', ['revid1'], [])
901
self.assertParentIds(['revid1'], rev_set)
903
def test_not_first_parent(self):
904
self.builder.build_snapshot('revid1', None, [])
905
self.builder.build_snapshot('revid2', ['revid1'], [])
906
self.builder.build_snapshot('revid3', ['revid2'], [])
907
rev_set = ['revid3', 'revid2']
908
self.assertParentIds(['revid1'], rev_set)
910
def test_not_null(self):
911
rev_set = ['initial']
912
self.assertParentIds([], rev_set)
914
def test_not_null_set(self):
915
self.builder.build_snapshot('revid1', None, [])
916
rev_set = [_mod_revision.NULL_REVISION]
917
self.assertParentIds([], rev_set)
919
def test_ghost(self):
920
self.builder.build_snapshot('revid1', None, [])
921
rev_set = ['ghost', 'revid1']
922
self.assertParentIds(['initial'], rev_set)
924
def test_ghost_parent(self):
925
self.builder.build_snapshot('revid1', None, [])
926
self.builder.build_snapshot('revid2', ['revid1', 'ghost'], [])
927
rev_set = ['revid2', 'revid1']
928
self.assertParentIds(['ghost', 'initial'], rev_set)
930
def test_righthand_parent(self):
931
self.builder.build_snapshot('revid1', None, [])
932
self.builder.build_snapshot('revid2a', ['revid1'], [])
933
self.builder.build_snapshot('revid2b', ['revid1'], [])
934
self.builder.build_snapshot('revid3', ['revid2a', 'revid2b'], [])
935
rev_set = ['revid3', 'revid2a']
936
self.assertParentIds(['revid1', 'revid2b'], rev_set)
939
class TestWithBrokenRepo(TestCaseWithTransport):
940
"""These tests seem to be more appropriate as interface tests?"""
942
def make_broken_repository(self):
943
# XXX: This function is borrowed from Aaron's "Reconcile can fix bad
944
# parent references" branch which is due to land in bzr.dev soon. Once
945
# it does, this duplication should be removed.
946
repo = self.make_repository('broken-repo')
950
cleanups.append(repo.unlock)
951
repo.start_write_group()
952
cleanups.append(repo.commit_write_group)
953
# make rev1a: A well-formed revision, containing 'file1'
954
inv = inventory.Inventory(revision_id='rev1a')
955
inv.root.revision = 'rev1a'
956
self.add_file(repo, inv, 'file1', 'rev1a', [])
957
repo.add_inventory('rev1a', inv, [])
958
revision = _mod_revision.Revision('rev1a',
959
committer='jrandom@example.com', timestamp=0,
960
inventory_sha1='', timezone=0, message='foo', parent_ids=[])
961
repo.add_revision('rev1a',revision, inv)
963
# make rev1b, which has no Revision, but has an Inventory, and
965
inv = inventory.Inventory(revision_id='rev1b')
966
inv.root.revision = 'rev1b'
967
self.add_file(repo, inv, 'file1', 'rev1b', [])
968
repo.add_inventory('rev1b', inv, [])
970
# make rev2, with file1 and file2
972
# file1 has 'rev1b' as an ancestor, even though this is not
973
# mentioned by 'rev1a', making it an unreferenced ancestor
974
inv = inventory.Inventory()
975
self.add_file(repo, inv, 'file1', 'rev2', ['rev1a', 'rev1b'])
976
self.add_file(repo, inv, 'file2', 'rev2', [])
977
self.add_revision(repo, 'rev2', inv, ['rev1a'])
979
# make ghost revision rev1c
980
inv = inventory.Inventory()
981
self.add_file(repo, inv, 'file2', 'rev1c', [])
983
# make rev3 with file2
984
# file2 refers to 'rev1c', which is a ghost in this repository, so
985
# file2 cannot have rev1c as its ancestor.
986
inv = inventory.Inventory()
987
self.add_file(repo, inv, 'file2', 'rev3', ['rev1c'])
988
self.add_revision(repo, 'rev3', inv, ['rev1c'])
991
for cleanup in reversed(cleanups):
994
def add_revision(self, repo, revision_id, inv, parent_ids):
995
inv.revision_id = revision_id
996
inv.root.revision = revision_id
997
repo.add_inventory(revision_id, inv, parent_ids)
998
revision = _mod_revision.Revision(revision_id,
999
committer='jrandom@example.com', timestamp=0, inventory_sha1='',
1000
timezone=0, message='foo', parent_ids=parent_ids)
1001
repo.add_revision(revision_id,revision, inv)
1003
def add_file(self, repo, inv, filename, revision, parents):
1004
file_id = filename + '-id'
1005
entry = inventory.InventoryFile(file_id, filename, 'TREE_ROOT')
1006
entry.revision = revision
1009
text_key = (file_id, revision)
1010
parent_keys = [(file_id, parent) for parent in parents]
1011
repo.texts.add_lines(text_key, parent_keys, ['line\n'])
1013
def test_insert_from_broken_repo(self):
1014
"""Inserting a data stream from a broken repository won't silently
1015
corrupt the target repository.
1017
broken_repo = self.make_broken_repository()
1018
empty_repo = self.make_repository('empty-repo')
1020
empty_repo.fetch(broken_repo)
1021
except (errors.RevisionNotPresent, errors.BzrCheckError):
1022
# Test successful: compression parent not being copied leads to
1025
empty_repo.lock_read()
1026
self.addCleanup(empty_repo.unlock)
1027
text = empty_repo.texts.get_record_stream(
1028
[('file2-id', 'rev3')], 'topological', True).next()
1029
self.assertEqual('line\n', text.get_bytes_as('fulltext'))
1032
class TestRepositoryPackCollection(TestCaseWithTransport):
1034
def get_format(self):
1035
return bzrdir.format_registry.make_bzrdir('pack-0.92')
1037
def get_packs(self):
1038
format = self.get_format()
1039
repo = self.make_repository('.', format=format)
1040
return repo._pack_collection
1042
def make_packs_and_alt_repo(self, write_lock=False):
1043
"""Create a pack repo with 3 packs, and access it via a second repo."""
1044
tree = self.make_branch_and_tree('.', format=self.get_format())
1046
self.addCleanup(tree.unlock)
1047
rev1 = tree.commit('one')
1048
rev2 = tree.commit('two')
1049
rev3 = tree.commit('three')
1050
r = repository.Repository.open('.')
1055
self.addCleanup(r.unlock)
1056
packs = r._pack_collection
1057
packs.ensure_loaded()
1058
return tree, r, packs, [rev1, rev2, rev3]
1060
def test__max_pack_count(self):
1061
"""The maximum pack count is a function of the number of revisions."""
1062
# no revisions - one pack, so that we can have a revision free repo
1063
# without it blowing up
1064
packs = self.get_packs()
1065
self.assertEqual(1, packs._max_pack_count(0))
1066
# after that the sum of the digits, - check the first 1-9
1067
self.assertEqual(1, packs._max_pack_count(1))
1068
self.assertEqual(2, packs._max_pack_count(2))
1069
self.assertEqual(3, packs._max_pack_count(3))
1070
self.assertEqual(4, packs._max_pack_count(4))
1071
self.assertEqual(5, packs._max_pack_count(5))
1072
self.assertEqual(6, packs._max_pack_count(6))
1073
self.assertEqual(7, packs._max_pack_count(7))
1074
self.assertEqual(8, packs._max_pack_count(8))
1075
self.assertEqual(9, packs._max_pack_count(9))
1076
# check the boundary cases with two digits for the next decade
1077
self.assertEqual(1, packs._max_pack_count(10))
1078
self.assertEqual(2, packs._max_pack_count(11))
1079
self.assertEqual(10, packs._max_pack_count(19))
1080
self.assertEqual(2, packs._max_pack_count(20))
1081
self.assertEqual(3, packs._max_pack_count(21))
1082
# check some arbitrary big numbers
1083
self.assertEqual(25, packs._max_pack_count(112894))
1085
def test_pack_distribution_zero(self):
1086
packs = self.get_packs()
1087
self.assertEqual([0], packs.pack_distribution(0))
1089
def test_ensure_loaded_unlocked(self):
1090
packs = self.get_packs()
1091
self.assertRaises(errors.ObjectNotLocked,
1092
packs.ensure_loaded)
1094
def test_pack_distribution_one_to_nine(self):
1095
packs = self.get_packs()
1096
self.assertEqual([1],
1097
packs.pack_distribution(1))
1098
self.assertEqual([1, 1],
1099
packs.pack_distribution(2))
1100
self.assertEqual([1, 1, 1],
1101
packs.pack_distribution(3))
1102
self.assertEqual([1, 1, 1, 1],
1103
packs.pack_distribution(4))
1104
self.assertEqual([1, 1, 1, 1, 1],
1105
packs.pack_distribution(5))
1106
self.assertEqual([1, 1, 1, 1, 1, 1],
1107
packs.pack_distribution(6))
1108
self.assertEqual([1, 1, 1, 1, 1, 1, 1],
1109
packs.pack_distribution(7))
1110
self.assertEqual([1, 1, 1, 1, 1, 1, 1, 1],
1111
packs.pack_distribution(8))
1112
self.assertEqual([1, 1, 1, 1, 1, 1, 1, 1, 1],
1113
packs.pack_distribution(9))
1115
def test_pack_distribution_stable_at_boundaries(self):
1116
"""When there are multi-rev packs the counts are stable."""
1117
packs = self.get_packs()
1119
self.assertEqual([10], packs.pack_distribution(10))
1120
self.assertEqual([10, 1], packs.pack_distribution(11))
1121
self.assertEqual([10, 10], packs.pack_distribution(20))
1122
self.assertEqual([10, 10, 1], packs.pack_distribution(21))
1124
self.assertEqual([100], packs.pack_distribution(100))
1125
self.assertEqual([100, 1], packs.pack_distribution(101))
1126
self.assertEqual([100, 10, 1], packs.pack_distribution(111))
1127
self.assertEqual([100, 100], packs.pack_distribution(200))
1128
self.assertEqual([100, 100, 1], packs.pack_distribution(201))
1129
self.assertEqual([100, 100, 10, 1], packs.pack_distribution(211))
1131
def test_plan_pack_operations_2009_revisions_skip_all_packs(self):
1132
packs = self.get_packs()
1133
existing_packs = [(2000, "big"), (9, "medium")]
1134
# rev count - 2009 -> 2x1000 + 9x1
1135
pack_operations = packs.plan_autopack_combinations(
1136
existing_packs, [1000, 1000, 1, 1, 1, 1, 1, 1, 1, 1, 1])
1137
self.assertEqual([], pack_operations)
1139
def test_plan_pack_operations_2010_revisions_skip_all_packs(self):
1140
packs = self.get_packs()
1141
existing_packs = [(2000, "big"), (9, "medium"), (1, "single")]
1142
# rev count - 2010 -> 2x1000 + 1x10
1143
pack_operations = packs.plan_autopack_combinations(
1144
existing_packs, [1000, 1000, 10])
1145
self.assertEqual([], pack_operations)
1147
def test_plan_pack_operations_2010_combines_smallest_two(self):
1148
packs = self.get_packs()
1149
existing_packs = [(1999, "big"), (9, "medium"), (1, "single2"),
1151
# rev count - 2010 -> 2x1000 + 1x10 (3)
1152
pack_operations = packs.plan_autopack_combinations(
1153
existing_packs, [1000, 1000, 10])
1154
self.assertEqual([[2, ["single2", "single1"]]], pack_operations)
1156
def test_plan_pack_operations_creates_a_single_op(self):
1157
packs = self.get_packs()
1158
existing_packs = [(50, 'a'), (40, 'b'), (30, 'c'), (10, 'd'),
1159
(10, 'e'), (6, 'f'), (4, 'g')]
1160
# rev count 150 -> 1x100 and 5x10
1161
# The two size 10 packs do not need to be touched. The 50, 40, 30 would
1162
# be combined into a single 120 size pack, and the 6 & 4 would
1163
# becombined into a size 10 pack. However, if we have to rewrite them,
1164
# we save a pack file with no increased I/O by putting them into the
1166
distribution = packs.pack_distribution(150)
1167
pack_operations = packs.plan_autopack_combinations(existing_packs,
1169
self.assertEqual([[130, ['a', 'b', 'c', 'f', 'g']]], pack_operations)
1171
def test_all_packs_none(self):
1172
format = self.get_format()
1173
tree = self.make_branch_and_tree('.', format=format)
1175
self.addCleanup(tree.unlock)
1176
packs = tree.branch.repository._pack_collection
1177
packs.ensure_loaded()
1178
self.assertEqual([], packs.all_packs())
1180
def test_all_packs_one(self):
1181
format = self.get_format()
1182
tree = self.make_branch_and_tree('.', format=format)
1183
tree.commit('start')
1185
self.addCleanup(tree.unlock)
1186
packs = tree.branch.repository._pack_collection
1187
packs.ensure_loaded()
1189
packs.get_pack_by_name(packs.names()[0])],
1192
def test_all_packs_two(self):
1193
format = self.get_format()
1194
tree = self.make_branch_and_tree('.', format=format)
1195
tree.commit('start')
1196
tree.commit('continue')
1198
self.addCleanup(tree.unlock)
1199
packs = tree.branch.repository._pack_collection
1200
packs.ensure_loaded()
1202
packs.get_pack_by_name(packs.names()[0]),
1203
packs.get_pack_by_name(packs.names()[1]),
1204
], packs.all_packs())
1206
def test_get_pack_by_name(self):
1207
format = self.get_format()
1208
tree = self.make_branch_and_tree('.', format=format)
1209
tree.commit('start')
1211
self.addCleanup(tree.unlock)
1212
packs = tree.branch.repository._pack_collection
1214
packs.ensure_loaded()
1215
name = packs.names()[0]
1216
pack_1 = packs.get_pack_by_name(name)
1217
# the pack should be correctly initialised
1218
sizes = packs._names[name]
1219
rev_index = GraphIndex(packs._index_transport, name + '.rix', sizes[0])
1220
inv_index = GraphIndex(packs._index_transport, name + '.iix', sizes[1])
1221
txt_index = GraphIndex(packs._index_transport, name + '.tix', sizes[2])
1222
sig_index = GraphIndex(packs._index_transport, name + '.six', sizes[3])
1223
self.assertEqual(pack_repo.ExistingPack(packs._pack_transport,
1224
name, rev_index, inv_index, txt_index, sig_index), pack_1)
1225
# and the same instance should be returned on successive calls.
1226
self.assertTrue(pack_1 is packs.get_pack_by_name(name))
1228
def test_reload_pack_names_new_entry(self):
1229
tree, r, packs, revs = self.make_packs_and_alt_repo()
1230
names = packs.names()
1231
# Add a new pack file into the repository
1232
rev4 = tree.commit('four')
1233
new_names = tree.branch.repository._pack_collection.names()
1234
new_name = set(new_names).difference(names)
1235
self.assertEqual(1, len(new_name))
1236
new_name = new_name.pop()
1237
# The old collection hasn't noticed yet
1238
self.assertEqual(names, packs.names())
1239
self.assertTrue(packs.reload_pack_names())
1240
self.assertEqual(new_names, packs.names())
1241
# And the repository can access the new revision
1242
self.assertEqual({rev4:(revs[-1],)}, r.get_parent_map([rev4]))
1243
self.assertFalse(packs.reload_pack_names())
1245
def test_reload_pack_names_added_and_removed(self):
1246
tree, r, packs, revs = self.make_packs_and_alt_repo()
1247
names = packs.names()
1248
# Now repack the whole thing
1249
tree.branch.repository.pack()
1250
new_names = tree.branch.repository._pack_collection.names()
1251
# The other collection hasn't noticed yet
1252
self.assertEqual(names, packs.names())
1253
self.assertTrue(packs.reload_pack_names())
1254
self.assertEqual(new_names, packs.names())
1255
self.assertEqual({revs[-1]:(revs[-2],)}, r.get_parent_map([revs[-1]]))
1256
self.assertFalse(packs.reload_pack_names())
1258
def test_autopack_reloads_and_stops(self):
1259
tree, r, packs, revs = self.make_packs_and_alt_repo(write_lock=True)
1260
# After we have determined what needs to be autopacked, trigger a
1261
# full-pack via the other repo which will cause us to re-evaluate and
1262
# decide we don't need to do anything
1263
orig_execute = packs._execute_pack_operations
1264
def _munged_execute_pack_ops(*args, **kwargs):
1265
tree.branch.repository.pack()
1266
return orig_execute(*args, **kwargs)
1267
packs._execute_pack_operations = _munged_execute_pack_ops
1268
packs._max_pack_count = lambda x: 1
1269
packs.pack_distribution = lambda x: [10]
1270
self.assertFalse(packs.autopack())
1271
self.assertEqual(1, len(packs.names()))
1272
self.assertEqual(tree.branch.repository._pack_collection.names(),
1276
class TestPack(TestCaseWithTransport):
1277
"""Tests for the Pack object."""
1279
def assertCurrentlyEqual(self, left, right):
1280
self.assertTrue(left == right)
1281
self.assertTrue(right == left)
1282
self.assertFalse(left != right)
1283
self.assertFalse(right != left)
1285
def assertCurrentlyNotEqual(self, left, right):
1286
self.assertFalse(left == right)
1287
self.assertFalse(right == left)
1288
self.assertTrue(left != right)
1289
self.assertTrue(right != left)
1291
def test___eq____ne__(self):
1292
left = pack_repo.ExistingPack('', '', '', '', '', '')
1293
right = pack_repo.ExistingPack('', '', '', '', '', '')
1294
self.assertCurrentlyEqual(left, right)
1295
# change all attributes and ensure equality changes as we do.
1296
left.revision_index = 'a'
1297
self.assertCurrentlyNotEqual(left, right)
1298
right.revision_index = 'a'
1299
self.assertCurrentlyEqual(left, right)
1300
left.inventory_index = 'a'
1301
self.assertCurrentlyNotEqual(left, right)
1302
right.inventory_index = 'a'
1303
self.assertCurrentlyEqual(left, right)
1304
left.text_index = 'a'
1305
self.assertCurrentlyNotEqual(left, right)
1306
right.text_index = 'a'
1307
self.assertCurrentlyEqual(left, right)
1308
left.signature_index = 'a'
1309
self.assertCurrentlyNotEqual(left, right)
1310
right.signature_index = 'a'
1311
self.assertCurrentlyEqual(left, right)
1313
self.assertCurrentlyNotEqual(left, right)
1315
self.assertCurrentlyEqual(left, right)
1316
left.transport = 'a'
1317
self.assertCurrentlyNotEqual(left, right)
1318
right.transport = 'a'
1319
self.assertCurrentlyEqual(left, right)
1321
def test_file_name(self):
1322
pack = pack_repo.ExistingPack('', 'a_name', '', '', '', '')
1323
self.assertEqual('a_name.pack', pack.file_name())
1326
class TestNewPack(TestCaseWithTransport):
1327
"""Tests for pack_repo.NewPack."""
1329
def test_new_instance_attributes(self):
1330
upload_transport = self.get_transport('upload')
1331
pack_transport = self.get_transport('pack')
1332
index_transport = self.get_transport('index')
1333
upload_transport.mkdir('.')
1334
collection = pack_repo.RepositoryPackCollection(
1336
transport=self.get_transport('.'),
1337
index_transport=index_transport,
1338
upload_transport=upload_transport,
1339
pack_transport=pack_transport,
1340
index_builder_class=BTreeBuilder,
1341
index_class=BTreeGraphIndex,
1342
use_chk_index=False)
1343
pack = pack_repo.NewPack(collection)
1344
self.assertIsInstance(pack.revision_index, BTreeBuilder)
1345
self.assertIsInstance(pack.inventory_index, BTreeBuilder)
1346
self.assertIsInstance(pack._hash, type(osutils.md5()))
1347
self.assertTrue(pack.upload_transport is upload_transport)
1348
self.assertTrue(pack.index_transport is index_transport)
1349
self.assertTrue(pack.pack_transport is pack_transport)
1350
self.assertEqual(None, pack.index_sizes)
1351
self.assertEqual(20, len(pack.random_name))
1352
self.assertIsInstance(pack.random_name, str)
1353
self.assertIsInstance(pack.start_time, float)
1356
class TestPacker(TestCaseWithTransport):
1357
"""Tests for the packs repository Packer class."""
1359
def test_pack_optimizes_pack_order(self):
1360
builder = self.make_branch_builder('.', format="1.9")
1361
builder.start_series()
1362
builder.build_snapshot('A', None, [
1363
('add', ('', 'root-id', 'directory', None)),
1364
('add', ('f', 'f-id', 'file', 'content\n'))])
1365
builder.build_snapshot('B', ['A'],
1366
[('modify', ('f-id', 'new-content\n'))])
1367
builder.build_snapshot('C', ['B'],
1368
[('modify', ('f-id', 'third-content\n'))])
1369
builder.build_snapshot('D', ['C'],
1370
[('modify', ('f-id', 'fourth-content\n'))])
1371
b = builder.get_branch()
1373
builder.finish_series()
1374
self.addCleanup(b.unlock)
1375
# At this point, we should have 4 pack files available
1376
# Because of how they were built, they correspond to
1377
# ['D', 'C', 'B', 'A']
1378
packs = b.repository._pack_collection.packs
1379
packer = pack_repo.Packer(b.repository._pack_collection,
1381
revision_ids=['B', 'C'])
1382
# Now, when we are copying the B & C revisions, their pack files should
1383
# be moved to the front of the stack
1384
# The new ordering moves B & C to the front of the .packs attribute,
1385
# and leaves the others in the original order.
1386
new_packs = [packs[1], packs[2], packs[0], packs[3]]
1387
new_pack = packer.pack()
1388
self.assertEqual(new_packs, packer.packs)
1391
class TestOptimisingPacker(TestCaseWithTransport):
1392
"""Tests for the OptimisingPacker class."""
1394
def get_pack_collection(self):
1395
repo = self.make_repository('.')
1396
return repo._pack_collection
1398
def test_open_pack_will_optimise(self):
1399
packer = pack_repo.OptimisingPacker(self.get_pack_collection(),
1401
new_pack = packer.open_pack()
1402
self.assertIsInstance(new_pack, pack_repo.NewPack)
1403
self.assertTrue(new_pack.revision_index._optimize_for_size)
1404
self.assertTrue(new_pack.inventory_index._optimize_for_size)
1405
self.assertTrue(new_pack.text_index._optimize_for_size)
1406
self.assertTrue(new_pack.signature_index._optimize_for_size)
1409
class TestCrossFormatPacks(TestCaseWithTransport):
1411
def log_pack(self, hint=None):
1412
self.calls.append(('pack', hint))
1413
self.orig_pack(hint=hint)
1414
if self.expect_hint:
1415
self.assertTrue(hint)
1417
def run_stream(self, src_fmt, target_fmt, expect_pack_called):
1418
self.expect_hint = expect_pack_called
1420
source_tree = self.make_branch_and_tree('src', format=src_fmt)
1421
source_tree.lock_write()
1422
self.addCleanup(source_tree.unlock)
1423
tip = source_tree.commit('foo')
1424
target = self.make_repository('target', format=target_fmt)
1426
self.addCleanup(target.unlock)
1427
source = source_tree.branch.repository._get_source(target._format)
1428
self.orig_pack = target.pack
1429
target.pack = self.log_pack
1430
search = target.search_missing_revision_ids(
1431
source_tree.branch.repository, tip)
1432
stream = source.get_stream(search)
1433
from_format = source_tree.branch.repository._format
1434
sink = target._get_sink()
1435
sink.insert_stream(stream, from_format, [])
1436
if expect_pack_called:
1437
self.assertLength(1, self.calls)
1439
self.assertLength(0, self.calls)
1441
def run_fetch(self, src_fmt, target_fmt, expect_pack_called):
1442
self.expect_hint = expect_pack_called
1444
source_tree = self.make_branch_and_tree('src', format=src_fmt)
1445
source_tree.lock_write()
1446
self.addCleanup(source_tree.unlock)
1447
tip = source_tree.commit('foo')
1448
target = self.make_repository('target', format=target_fmt)
1450
self.addCleanup(target.unlock)
1451
source = source_tree.branch.repository
1452
self.orig_pack = target.pack
1453
target.pack = self.log_pack
1454
target.fetch(source)
1455
if expect_pack_called:
1456
self.assertLength(1, self.calls)
1458
self.assertLength(0, self.calls)
1460
def test_sink_format_hint_no(self):
1461
# When the target format says packing makes no difference, pack is not
1463
self.run_stream('1.9', 'rich-root-pack', False)
1465
def test_sink_format_hint_yes(self):
1466
# When the target format says packing makes a difference, pack is
1468
self.run_stream('1.9', '2a', True)
1470
def test_sink_format_same_no(self):
1471
# When the formats are the same, pack is not called.
1472
self.run_stream('2a', '2a', False)
1474
def test_IDS_format_hint_no(self):
1475
# When the target format says packing makes no difference, pack is not
1477
self.run_fetch('1.9', 'rich-root-pack', False)
1479
def test_IDS_format_hint_yes(self):
1480
# When the target format says packing makes a difference, pack is
1482
self.run_fetch('1.9', '2a', True)
1484
def test_IDS_format_same_no(self):
1485
# When the formats are the same, pack is not called.
1486
self.run_fetch('2a', '2a', False)