/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_repository.py

merge with get_file_sha1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
739
739
        self.assertRaises(errors.RevisionNotPresent, empty_repo.fetch, broken_repo)
740
740
 
741
741
 
742
 
class TestKnitPackNoSubtrees(TestCaseWithTransport):
743
 
 
744
 
    def get_format(self):
745
 
        return bzrdir.format_registry.make_bzrdir('pack-0.92')
746
 
 
747
 
    def test_attribute__fetch_order(self):
748
 
        """Packs do not need ordered data retrieval."""
749
 
        format = self.get_format()
750
 
        repo = self.make_repository('.', format=format)
751
 
        self.assertEqual('unsorted', repo._fetch_order)
752
 
 
753
 
    def test_attribute__fetch_uses_deltas(self):
754
 
        """Packs reuse deltas."""
755
 
        format = self.get_format()
756
 
        repo = self.make_repository('.', format=format)
757
 
        self.assertEqual(True, repo._fetch_uses_deltas)
758
 
 
759
 
    def test_disk_layout(self):
760
 
        format = self.get_format()
761
 
        repo = self.make_repository('.', format=format)
762
 
        # in case of side effects of locking.
763
 
        repo.lock_write()
764
 
        repo.unlock()
765
 
        t = repo.bzrdir.get_repository_transport(None)
766
 
        self.check_format(t)
767
 
        # XXX: no locks left when unlocked at the moment
768
 
        # self.assertEqualDiff('', t.get('lock').read())
769
 
        self.check_databases(t)
770
 
 
771
 
    def check_format(self, t):
772
 
        self.assertEqualDiff(
773
 
            "Bazaar pack repository format 1 (needs bzr 0.92)\n",
774
 
                             t.get('format').read())
775
 
 
776
 
    def assertHasNoKndx(self, t, knit_name):
777
 
        """Assert that knit_name has no index on t."""
778
 
        self.assertFalse(t.has(knit_name + '.kndx'))
779
 
 
780
 
    def assertHasNoKnit(self, t, knit_name):
781
 
        """Assert that knit_name exists on t."""
782
 
        # no default content
783
 
        self.assertFalse(t.has(knit_name + '.knit'))
784
 
 
785
 
    def check_databases(self, t):
786
 
        """check knit content for a repository."""
787
 
        # check conversion worked
788
 
        self.assertHasNoKndx(t, 'inventory')
789
 
        self.assertHasNoKnit(t, 'inventory')
790
 
        self.assertHasNoKndx(t, 'revisions')
791
 
        self.assertHasNoKnit(t, 'revisions')
792
 
        self.assertHasNoKndx(t, 'signatures')
793
 
        self.assertHasNoKnit(t, 'signatures')
794
 
        self.assertFalse(t.has('knits'))
795
 
        # revision-indexes file-container directory
796
 
        self.assertEqual([],
797
 
            list(GraphIndex(t, 'pack-names', None).iter_all_entries()))
798
 
        self.assertTrue(S_ISDIR(t.stat('packs').st_mode))
799
 
        self.assertTrue(S_ISDIR(t.stat('upload').st_mode))
800
 
        self.assertTrue(S_ISDIR(t.stat('indices').st_mode))
801
 
        self.assertTrue(S_ISDIR(t.stat('obsolete_packs').st_mode))
802
 
 
803
 
    def test_shared_disk_layout(self):
804
 
        format = self.get_format()
805
 
        repo = self.make_repository('.', shared=True, format=format)
806
 
        # we want:
807
 
        t = repo.bzrdir.get_repository_transport(None)
808
 
        self.check_format(t)
809
 
        # XXX: no locks left when unlocked at the moment
810
 
        # self.assertEqualDiff('', t.get('lock').read())
811
 
        # We should have a 'shared-storage' marker file.
812
 
        self.assertEqualDiff('', t.get('shared-storage').read())
813
 
        self.check_databases(t)
814
 
 
815
 
    def test_shared_no_tree_disk_layout(self):
816
 
        format = self.get_format()
817
 
        repo = self.make_repository('.', shared=True, format=format)
818
 
        repo.set_make_working_trees(False)
819
 
        # we want:
820
 
        t = repo.bzrdir.get_repository_transport(None)
821
 
        self.check_format(t)
822
 
        # XXX: no locks left when unlocked at the moment
823
 
        # self.assertEqualDiff('', t.get('lock').read())
824
 
        # We should have a 'shared-storage' marker file.
825
 
        self.assertEqualDiff('', t.get('shared-storage').read())
826
 
        # We should have a marker for the no-working-trees flag.
827
 
        self.assertEqualDiff('', t.get('no-working-trees').read())
828
 
        # The marker should go when we toggle the setting.
829
 
        repo.set_make_working_trees(True)
830
 
        self.assertFalse(t.has('no-working-trees'))
831
 
        self.check_databases(t)
832
 
 
833
 
    def test_adding_revision_creates_pack_indices(self):
834
 
        format = self.get_format()
835
 
        tree = self.make_branch_and_tree('.', format=format)
836
 
        trans = tree.branch.repository.bzrdir.get_repository_transport(None)
837
 
        self.assertEqual([],
838
 
            list(GraphIndex(trans, 'pack-names', None).iter_all_entries()))
839
 
        tree.commit('foobarbaz')
840
 
        index = GraphIndex(trans, 'pack-names', None)
841
 
        index_nodes = list(index.iter_all_entries())
842
 
        self.assertEqual(1, len(index_nodes))
843
 
        node = index_nodes[0]
844
 
        name = node[1][0]
845
 
        # the pack sizes should be listed in the index
846
 
        pack_value = node[2]
847
 
        sizes = [int(digits) for digits in pack_value.split(' ')]
848
 
        for size, suffix in zip(sizes, ['.rix', '.iix', '.tix', '.six']):
849
 
            stat = trans.stat('indices/%s%s' % (name, suffix))
850
 
            self.assertEqual(size, stat.st_size)
851
 
 
852
 
    def test_pulling_nothing_leads_to_no_new_names(self):
853
 
        format = self.get_format()
854
 
        tree1 = self.make_branch_and_tree('1', format=format)
855
 
        tree2 = self.make_branch_and_tree('2', format=format)
856
 
        tree1.branch.repository.fetch(tree2.branch.repository)
857
 
        trans = tree1.branch.repository.bzrdir.get_repository_transport(None)
858
 
        self.assertEqual([],
859
 
            list(GraphIndex(trans, 'pack-names', None).iter_all_entries()))
860
 
 
861
 
    def test_commit_across_pack_shape_boundary_autopacks(self):
862
 
        format = self.get_format()
863
 
        tree = self.make_branch_and_tree('.', format=format)
864
 
        trans = tree.branch.repository.bzrdir.get_repository_transport(None)
865
 
        # This test could be a little cheaper by replacing the packs
866
 
        # attribute on the repository to allow a different pack distribution
867
 
        # and max packs policy - so we are checking the policy is honoured
868
 
        # in the test. But for now 11 commits is not a big deal in a single
869
 
        # test.
870
 
        for x in range(9):
871
 
            tree.commit('commit %s' % x)
872
 
        # there should be 9 packs:
873
 
        index = GraphIndex(trans, 'pack-names', None)
874
 
        self.assertEqual(9, len(list(index.iter_all_entries())))
875
 
        # insert some files in obsolete_packs which should be removed by pack.
876
 
        trans.put_bytes('obsolete_packs/foo', '123')
877
 
        trans.put_bytes('obsolete_packs/bar', '321')
878
 
        # committing one more should coalesce to 1 of 10.
879
 
        tree.commit('commit triggering pack')
880
 
        index = GraphIndex(trans, 'pack-names', None)
881
 
        self.assertEqual(1, len(list(index.iter_all_entries())))
882
 
        # packing should not damage data
883
 
        tree = tree.bzrdir.open_workingtree()
884
 
        check_result = tree.branch.repository.check(
885
 
            [tree.branch.last_revision()])
886
 
        # We should have 50 (10x5) files in the obsolete_packs directory.
887
 
        obsolete_files = list(trans.list_dir('obsolete_packs'))
888
 
        self.assertFalse('foo' in obsolete_files)
889
 
        self.assertFalse('bar' in obsolete_files)
890
 
        self.assertEqual(50, len(obsolete_files))
891
 
        # XXX: Todo check packs obsoleted correctly - old packs and indices
892
 
        # in the obsolete_packs directory.
893
 
        large_pack_name = list(index.iter_all_entries())[0][1][0]
894
 
        # finally, committing again should not touch the large pack.
895
 
        tree.commit('commit not triggering pack')
896
 
        index = GraphIndex(trans, 'pack-names', None)
897
 
        self.assertEqual(2, len(list(index.iter_all_entries())))
898
 
        pack_names = [node[1][0] for node in index.iter_all_entries()]
899
 
        self.assertTrue(large_pack_name in pack_names)
900
 
 
901
 
    def test_fail_obsolete_deletion(self):
902
 
        # failing to delete obsolete packs is not fatal
903
 
        format = self.get_format()
904
 
        server = fakenfs.FakeNFSServer()
905
 
        server.setUp()
906
 
        self.addCleanup(server.tearDown)
907
 
        transport = get_transport(server.get_url())
908
 
        bzrdir = self.get_format().initialize_on_transport(transport)
909
 
        repo = bzrdir.create_repository()
910
 
        repo_transport = bzrdir.get_repository_transport(None)
911
 
        self.assertTrue(repo_transport.has('obsolete_packs'))
912
 
        # these files are in use by another client and typically can't be deleted
913
 
        repo_transport.put_bytes('obsolete_packs/.nfsblahblah', 'contents')
914
 
        repo._pack_collection._clear_obsolete_packs()
915
 
        self.assertTrue(repo_transport.has('obsolete_packs/.nfsblahblah'))
916
 
 
917
 
    def test_pack_after_two_commits_packs_everything(self):
918
 
        format = self.get_format()
919
 
        tree = self.make_branch_and_tree('.', format=format)
920
 
        trans = tree.branch.repository.bzrdir.get_repository_transport(None)
921
 
        tree.commit('start')
922
 
        tree.commit('more work')
923
 
        tree.branch.repository.pack()
924
 
        # there should be 1 pack:
925
 
        index = GraphIndex(trans, 'pack-names', None)
926
 
        self.assertEqual(1, len(list(index.iter_all_entries())))
927
 
        self.assertEqual(2, len(tree.branch.repository.all_revision_ids()))
928
 
 
929
 
    def test_pack_layout(self):
930
 
        format = self.get_format()
931
 
        tree = self.make_branch_and_tree('.', format=format)
932
 
        trans = tree.branch.repository.bzrdir.get_repository_transport(None)
933
 
        tree.commit('start', rev_id='1')
934
 
        tree.commit('more work', rev_id='2')
935
 
        tree.branch.repository.pack()
936
 
        tree.lock_read()
937
 
        self.addCleanup(tree.unlock)
938
 
        pack = tree.branch.repository._pack_collection.get_pack_by_name(
939
 
            tree.branch.repository._pack_collection.names()[0])
940
 
        # revision access tends to be tip->ancestor, so ordering that way on 
941
 
        # disk is a good idea.
942
 
        for _1, key, val, refs in pack.revision_index.iter_all_entries():
943
 
            if key == ('1',):
944
 
                pos_1 = int(val[1:].split()[0])
945
 
            else:
946
 
                pos_2 = int(val[1:].split()[0])
947
 
        self.assertTrue(pos_2 < pos_1)
948
 
 
949
 
    def test_pack_repositories_support_multiple_write_locks(self):
950
 
        format = self.get_format()
951
 
        self.make_repository('.', shared=True, format=format)
952
 
        r1 = repository.Repository.open('.')
953
 
        r2 = repository.Repository.open('.')
954
 
        r1.lock_write()
955
 
        self.addCleanup(r1.unlock)
956
 
        r2.lock_write()
957
 
        r2.unlock()
958
 
 
959
 
    def _add_text(self, repo, fileid):
960
 
        """Add a text to the repository within a write group."""
961
 
        repo.texts.add_lines((fileid, 'samplerev+'+fileid), [], [])
962
 
 
963
 
    def test_concurrent_writers_merge_new_packs(self):
964
 
        format = self.get_format()
965
 
        self.make_repository('.', shared=True, format=format)
966
 
        r1 = repository.Repository.open('.')
967
 
        r2 = repository.Repository.open('.')
968
 
        r1.lock_write()
969
 
        try:
970
 
            # access enough data to load the names list
971
 
            list(r1.all_revision_ids())
972
 
            r2.lock_write()
973
 
            try:
974
 
                # access enough data to load the names list
975
 
                list(r2.all_revision_ids())
976
 
                r1.start_write_group()
977
 
                try:
978
 
                    r2.start_write_group()
979
 
                    try:
980
 
                        self._add_text(r1, 'fileidr1')
981
 
                        self._add_text(r2, 'fileidr2')
982
 
                    except:
983
 
                        r2.abort_write_group()
984
 
                        raise
985
 
                except:
986
 
                    r1.abort_write_group()
987
 
                    raise
988
 
                # both r1 and r2 have open write groups with data in them
989
 
                # created while the other's write group was open.
990
 
                # Commit both which requires a merge to the pack-names.
991
 
                try:
992
 
                    r1.commit_write_group()
993
 
                except:
994
 
                    r1.abort_write_group()
995
 
                    r2.abort_write_group()
996
 
                    raise
997
 
                r2.commit_write_group()
998
 
                # tell r1 to reload from disk
999
 
                r1._pack_collection.reset()
1000
 
                # Now both repositories should know about both names
1001
 
                r1._pack_collection.ensure_loaded()
1002
 
                r2._pack_collection.ensure_loaded()
1003
 
                self.assertEqual(r1._pack_collection.names(), r2._pack_collection.names())
1004
 
                self.assertEqual(2, len(r1._pack_collection.names()))
1005
 
            finally:
1006
 
                r2.unlock()
1007
 
        finally:
1008
 
            r1.unlock()
1009
 
 
1010
 
    def test_concurrent_writer_second_preserves_dropping_a_pack(self):
1011
 
        format = self.get_format()
1012
 
        self.make_repository('.', shared=True, format=format)
1013
 
        r1 = repository.Repository.open('.')
1014
 
        r2 = repository.Repository.open('.')
1015
 
        # add a pack to drop
1016
 
        r1.lock_write()
1017
 
        try:
1018
 
            r1.start_write_group()
1019
 
            try:
1020
 
                self._add_text(r1, 'fileidr1')
1021
 
            except:
1022
 
                r1.abort_write_group()
1023
 
                raise
1024
 
            else:
1025
 
                r1.commit_write_group()
1026
 
            r1._pack_collection.ensure_loaded()
1027
 
            name_to_drop = r1._pack_collection.all_packs()[0].name
1028
 
        finally:
1029
 
            r1.unlock()
1030
 
        r1.lock_write()
1031
 
        try:
1032
 
            # access enough data to load the names list
1033
 
            list(r1.all_revision_ids())
1034
 
            r2.lock_write()
1035
 
            try:
1036
 
                # access enough data to load the names list
1037
 
                list(r2.all_revision_ids())
1038
 
                r1._pack_collection.ensure_loaded()
1039
 
                try:
1040
 
                    r2.start_write_group()
1041
 
                    try:
1042
 
                        # in r1, drop the pack
1043
 
                        r1._pack_collection._remove_pack_from_memory(
1044
 
                            r1._pack_collection.get_pack_by_name(name_to_drop))
1045
 
                        # in r2, add a pack
1046
 
                        self._add_text(r2, 'fileidr2')
1047
 
                    except:
1048
 
                        r2.abort_write_group()
1049
 
                        raise
1050
 
                except:
1051
 
                    r1._pack_collection.reset()
1052
 
                    raise
1053
 
                # r1 has a changed names list, and r2 an open write groups with
1054
 
                # changes.
1055
 
                # save r1, and then commit the r2 write group, which requires a
1056
 
                # merge to the pack-names, which should not reinstate
1057
 
                # name_to_drop
1058
 
                try:
1059
 
                    r1._pack_collection._save_pack_names()
1060
 
                    r1._pack_collection.reset()
1061
 
                except:
1062
 
                    r2.abort_write_group()
1063
 
                    raise
1064
 
                try:
1065
 
                    r2.commit_write_group()
1066
 
                except:
1067
 
                    r2.abort_write_group()
1068
 
                    raise
1069
 
                # Now both repositories should now about just one name.
1070
 
                r1._pack_collection.ensure_loaded()
1071
 
                r2._pack_collection.ensure_loaded()
1072
 
                self.assertEqual(r1._pack_collection.names(), r2._pack_collection.names())
1073
 
                self.assertEqual(1, len(r1._pack_collection.names()))
1074
 
                self.assertFalse(name_to_drop in r1._pack_collection.names())
1075
 
            finally:
1076
 
                r2.unlock()
1077
 
        finally:
1078
 
            r1.unlock()
1079
 
 
1080
 
    def test_lock_write_does_not_physically_lock(self):
1081
 
        repo = self.make_repository('.', format=self.get_format())
1082
 
        repo.lock_write()
1083
 
        self.addCleanup(repo.unlock)
1084
 
        self.assertFalse(repo.get_physical_lock_status())
1085
 
 
1086
 
    def prepare_for_break_lock(self):
1087
 
        # Setup the global ui factory state so that a break-lock method call
1088
 
        # will find usable input in the input stream.
1089
 
        old_factory = bzrlib.ui.ui_factory
1090
 
        def restoreFactory():
1091
 
            bzrlib.ui.ui_factory = old_factory
1092
 
        self.addCleanup(restoreFactory)
1093
 
        bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
1094
 
        bzrlib.ui.ui_factory.stdin = StringIO("y\n")
1095
 
 
1096
 
    def test_break_lock_breaks_physical_lock(self):
1097
 
        repo = self.make_repository('.', format=self.get_format())
1098
 
        repo._pack_collection.lock_names()
1099
 
        repo2 = repository.Repository.open('.')
1100
 
        self.assertTrue(repo.get_physical_lock_status())
1101
 
        self.prepare_for_break_lock()
1102
 
        repo2.break_lock()
1103
 
        self.assertFalse(repo.get_physical_lock_status())
1104
 
 
1105
 
    def test_broken_physical_locks_error_on__unlock_names_lock(self):
1106
 
        repo = self.make_repository('.', format=self.get_format())
1107
 
        repo._pack_collection.lock_names()
1108
 
        self.assertTrue(repo.get_physical_lock_status())
1109
 
        repo2 = repository.Repository.open('.')
1110
 
        self.prepare_for_break_lock()
1111
 
        repo2.break_lock()
1112
 
        self.assertRaises(errors.LockBroken, repo._pack_collection._unlock_names)
1113
 
 
1114
 
    def test_fetch_without_find_ghosts_ignores_ghosts(self):
1115
 
        # we want two repositories at this point:
1116
 
        # one with a revision that is a ghost in the other
1117
 
        # repository.
1118
 
        # 'ghost' is present in has_ghost, 'ghost' is absent in 'missing_ghost'.
1119
 
        # 'references' is present in both repositories, and 'tip' is present
1120
 
        # just in has_ghost.
1121
 
        # has_ghost       missing_ghost
1122
 
        #------------------------------
1123
 
        # 'ghost'             -
1124
 
        # 'references'    'references'
1125
 
        # 'tip'               -
1126
 
        # In this test we fetch 'tip' which should not fetch 'ghost'
1127
 
        has_ghost = self.make_repository('has_ghost', format=self.get_format())
1128
 
        missing_ghost = self.make_repository('missing_ghost',
1129
 
            format=self.get_format())
1130
 
 
1131
 
        def add_commit(repo, revision_id, parent_ids):
1132
 
            repo.lock_write()
1133
 
            repo.start_write_group()
1134
 
            inv = inventory.Inventory(revision_id=revision_id)
1135
 
            inv.root.revision = revision_id
1136
 
            root_id = inv.root.file_id
1137
 
            sha1 = repo.add_inventory(revision_id, inv, [])
1138
 
            repo.texts.add_lines((root_id, revision_id), [], [])
1139
 
            rev = bzrlib.revision.Revision(timestamp=0,
1140
 
                                           timezone=None,
1141
 
                                           committer="Foo Bar <foo@example.com>",
1142
 
                                           message="Message",
1143
 
                                           inventory_sha1=sha1,
1144
 
                                           revision_id=revision_id)
1145
 
            rev.parent_ids = parent_ids
1146
 
            repo.add_revision(revision_id, rev)
1147
 
            repo.commit_write_group()
1148
 
            repo.unlock()
1149
 
        add_commit(has_ghost, 'ghost', [])
1150
 
        add_commit(has_ghost, 'references', ['ghost'])
1151
 
        add_commit(missing_ghost, 'references', ['ghost'])
1152
 
        add_commit(has_ghost, 'tip', ['references'])
1153
 
        missing_ghost.fetch(has_ghost, 'tip')
1154
 
        # missing ghost now has tip and not ghost.
1155
 
        rev = missing_ghost.get_revision('tip')
1156
 
        inv = missing_ghost.get_inventory('tip')
1157
 
        self.assertRaises(errors.NoSuchRevision,
1158
 
            missing_ghost.get_revision, 'ghost')
1159
 
        self.assertRaises(errors.NoSuchRevision,
1160
 
            missing_ghost.get_inventory, 'ghost')
1161
 
 
1162
 
    def test_supports_external_lookups(self):
1163
 
        repo = self.make_repository('.', format=self.get_format())
1164
 
        self.assertFalse(repo._format.supports_external_lookups)
1165
 
 
1166
 
 
1167
 
class TestKnitPackSubtrees(TestKnitPackNoSubtrees):
1168
 
 
1169
 
    def get_format(self):
1170
 
        return bzrdir.format_registry.make_bzrdir(
1171
 
            'pack-0.92-subtree')
1172
 
 
1173
 
    def check_format(self, t):
1174
 
        self.assertEqualDiff(
1175
 
            "Bazaar pack repository format 1 with subtree support (needs bzr 0.92)\n",
1176
 
            t.get('format').read())
1177
 
 
1178
 
 
1179
 
class TestDevelopment0(TestKnitPackNoSubtrees):
1180
 
 
1181
 
    def get_format(self):
1182
 
        return bzrdir.format_registry.make_bzrdir(
1183
 
            'development0')
1184
 
 
1185
 
    def check_format(self, t):
1186
 
        self.assertEqualDiff(
1187
 
            "Bazaar development format 0 (needs bzr.dev from before 1.3)\n",
1188
 
            t.get('format').read())
1189
 
 
1190
 
 
1191
 
class TestDevelopment0Subtree(TestKnitPackNoSubtrees):
1192
 
 
1193
 
    def get_format(self):
1194
 
        return bzrdir.format_registry.make_bzrdir(
1195
 
            'development0-subtree')
1196
 
 
1197
 
    def check_format(self, t):
1198
 
        self.assertEqualDiff(
1199
 
            "Bazaar development format 0 with subtree support "
1200
 
            "(needs bzr.dev from before 1.3)\n",
1201
 
            t.get('format').read())
1202
 
 
1203
 
 
1204
 
class TestExternalDevelopment1(object):
1205
 
 
1206
 
    # mixin class for testing stack-supporting development formats
1207
 
 
1208
 
    def test_compatible_cross_formats(self):
1209
 
        # early versions of the packing code relied on pack internals to
1210
 
        # stack, but the current version should be able to stack on any
1211
 
        # format.
1212
 
        repo = self.make_repository('repo', format=self.get_format())
1213
 
        if repo.supports_rich_root():
1214
 
            # can only stack on repositories that have compatible internal
1215
 
            # metadata
1216
 
            matching_format_name = 'pack-0.92-subtree'
1217
 
            mismatching_format_name = 'pack-0.92'
1218
 
        else:
1219
 
            matching_format_name = 'pack-0.92'
1220
 
            mismatching_format_name = 'pack-0.92-subtree'
1221
 
        base = self.make_repository('base', format=matching_format_name)
1222
 
        repo.add_fallback_repository(base)
1223
 
        # you can't stack on something with incompatible data
1224
 
        bad_repo = self.make_repository('mismatch',
1225
 
            format=mismatching_format_name)
1226
 
        self.assertRaises(errors.IncompatibleRepositories,
1227
 
            repo.add_fallback_repository, bad_repo)
1228
 
 
1229
 
    def test_adding_pack_does_not_record_pack_names_from_other_repositories(self):
1230
 
        base = self.make_branch_and_tree('base', format=self.get_format())
1231
 
        base.commit('foo')
1232
 
        referencing = self.make_branch_and_tree('repo', format=self.get_format())
1233
 
        referencing.branch.repository.add_fallback_repository(base.branch.repository)
1234
 
        referencing.commit('bar')
1235
 
        new_instance = referencing.bzrdir.open_repository()
1236
 
        new_instance.lock_read()
1237
 
        self.addCleanup(new_instance.unlock)
1238
 
        new_instance._pack_collection.ensure_loaded()
1239
 
        self.assertEqual(1, len(new_instance._pack_collection.all_packs()))
1240
 
 
1241
 
    def test_autopack_only_considers_main_repo_packs(self):
1242
 
        base = self.make_branch_and_tree('base', format=self.get_format())
1243
 
        base.commit('foo')
1244
 
        tree = self.make_branch_and_tree('repo', format=self.get_format())
1245
 
        tree.branch.repository.add_fallback_repository(base.branch.repository)
1246
 
        trans = tree.branch.repository.bzrdir.get_repository_transport(None)
1247
 
        # This test could be a little cheaper by replacing the packs
1248
 
        # attribute on the repository to allow a different pack distribution
1249
 
        # and max packs policy - so we are checking the policy is honoured
1250
 
        # in the test. But for now 11 commits is not a big deal in a single
1251
 
        # test.
1252
 
        for x in range(9):
1253
 
            tree.commit('commit %s' % x)
1254
 
        # there should be 9 packs:
1255
 
        index = GraphIndex(trans, 'pack-names', None)
1256
 
        self.assertEqual(9, len(list(index.iter_all_entries())))
1257
 
        # committing one more should coalesce to 1 of 10.
1258
 
        tree.commit('commit triggering pack')
1259
 
        index = GraphIndex(trans, 'pack-names', None)
1260
 
        self.assertEqual(1, len(list(index.iter_all_entries())))
1261
 
        # packing should not damage data
1262
 
        tree = tree.bzrdir.open_workingtree()
1263
 
        check_result = tree.branch.repository.check(
1264
 
            [tree.branch.last_revision()])
1265
 
        # We should have 50 (10x5) files in the obsolete_packs directory.
1266
 
        obsolete_files = list(trans.list_dir('obsolete_packs'))
1267
 
        self.assertFalse('foo' in obsolete_files)
1268
 
        self.assertFalse('bar' in obsolete_files)
1269
 
        self.assertEqual(50, len(obsolete_files))
1270
 
        # XXX: Todo check packs obsoleted correctly - old packs and indices
1271
 
        # in the obsolete_packs directory.
1272
 
        large_pack_name = list(index.iter_all_entries())[0][1][0]
1273
 
        # finally, committing again should not touch the large pack.
1274
 
        tree.commit('commit not triggering pack')
1275
 
        index = GraphIndex(trans, 'pack-names', None)
1276
 
        self.assertEqual(2, len(list(index.iter_all_entries())))
1277
 
        pack_names = [node[1][0] for node in index.iter_all_entries()]
1278
 
        self.assertTrue(large_pack_name in pack_names)
1279
 
 
1280
 
 
1281
 
class TestDevelopment1(TestKnitPackNoSubtrees, TestExternalDevelopment1):
1282
 
 
1283
 
    def get_format(self):
1284
 
        return bzrdir.format_registry.make_bzrdir(
1285
 
            'development')
1286
 
 
1287
 
    def check_format(self, t):
1288
 
        self.assertEqualDiff(
1289
 
            "Bazaar development format 1 (needs bzr.dev from before 1.6)\n",
1290
 
            t.get('format').read())
1291
 
 
1292
 
    def test_supports_external_lookups(self):
1293
 
        repo = self.make_repository('.', format=self.get_format())
1294
 
        self.assertTrue(repo._format.supports_external_lookups)
1295
 
 
1296
 
 
1297
 
class TestDevelopment1Subtree(TestKnitPackNoSubtrees, TestExternalDevelopment1):
1298
 
 
1299
 
    def get_format(self):
1300
 
        return bzrdir.format_registry.make_bzrdir(
1301
 
            'development-subtree')
1302
 
 
1303
 
    def check_format(self, t):
1304
 
        self.assertEqualDiff(
1305
 
            "Bazaar development format 1 with subtree support "
1306
 
            "(needs bzr.dev from before 1.6)\n",
1307
 
            t.get('format').read())
1308
 
 
1309
 
    def test_supports_external_lookups(self):
1310
 
        repo = self.make_repository('.', format=self.get_format())
1311
 
        self.assertTrue(repo._format.supports_external_lookups)
1312
 
 
1313
 
 
1314
742
class TestRepositoryPackCollection(TestCaseWithTransport):
1315
743
 
1316
744
    def get_format(self):