~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__dirstate_helpers.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-09-26 05:47:03 UTC
  • mfrom: (3696.5.4 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20080926054703-nxn5f1h7z7gvur96
(robertc) Improve the handling of the sha1 cache by updating it
        during commit and avoiding some of the sha generation during
        iter_changes. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
import bisect
20
20
import os
 
21
import time
21
22
 
22
23
from bzrlib import (
23
24
    dirstate,
800
801
    def set_update_entry(self):
801
802
        self.update_entry = dirstate.py_update_entry
802
803
 
 
804
    def test_observed_sha1_cachable(self):
 
805
        state, entry = self.get_state_with_a()
 
806
        atime = time.time() - 10
 
807
        self.build_tree(['a'])
 
808
        statvalue = os.lstat('a')
 
809
        statvalue = test_dirstate._FakeStat(statvalue.st_size, atime, atime,
 
810
            statvalue.st_dev, statvalue.st_ino, statvalue.st_mode)
 
811
        state._observed_sha1(entry, "foo", statvalue)
 
812
        self.assertEqual('foo', entry[1][0][1])
 
813
        packed_stat = dirstate.pack_stat(statvalue)
 
814
        self.assertEqual(packed_stat, entry[1][0][4])
 
815
 
 
816
    def test_observed_sha1_not_cachable(self):
 
817
        state, entry = self.get_state_with_a()
 
818
        oldval = entry[1][0][1]
 
819
        oldstat = entry[1][0][4]
 
820
        self.build_tree(['a'])
 
821
        statvalue = os.lstat('a')
 
822
        state._observed_sha1(entry, "foo", statvalue)
 
823
        self.assertEqual(oldval, entry[1][0][1])
 
824
        self.assertEqual(oldstat, entry[1][0][4])
 
825
 
803
826
    def test_update_entry(self):
804
 
        state, entry = self.get_state_with_a()
 
827
        state, _ = self.get_state_with_a()
 
828
        tree = self.make_branch_and_tree('tree')
 
829
        tree.lock_write()
 
830
        empty_revid = tree.commit('empty')
 
831
        self.build_tree(['tree/a'])
 
832
        tree.add(['a'], ['a-id'])
 
833
        with_a_id = tree.commit('with_a')
 
834
        self.addCleanup(tree.unlock)
 
835
        state.set_parent_trees(
 
836
            [(empty_revid, tree.branch.repository.revision_tree(empty_revid))],
 
837
            [])
 
838
        entry = state._get_entry(0, path_utf8='a')
805
839
        self.build_tree(['a'])
806
840
        # Add one where we don't provide the stat or sha already
807
841
        self.assertEqual(('', 'a', 'a-id'), entry[0])
808
 
        self.assertEqual([('f', '', 0, False, dirstate.DirState.NULLSTAT)],
809
 
                         entry[1])
 
842
        self.assertEqual(('f', '', 0, False, dirstate.DirState.NULLSTAT),
 
843
                         entry[1][0])
810
844
        # Flush the buffers to disk
811
845
        state.save()
812
846
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
816
850
        packed_stat = dirstate.pack_stat(stat_value)
817
851
        link_or_sha1 = self.update_entry(state, entry, abspath='a',
818
852
                                          stat_value=stat_value)
819
 
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
820
 
                         link_or_sha1)
 
853
        self.assertEqual(None, link_or_sha1)
821
854
 
822
 
        # The dirblock entry should not cache the file's sha1
823
 
        self.assertEqual([('f', '', 14, False, dirstate.DirState.NULLSTAT)],
824
 
                         entry[1])
 
855
        # The dirblock entry should not have cached the file's sha1 (too new)
 
856
        self.assertEqual(('f', '', 14, False, dirstate.DirState.NULLSTAT),
 
857
                         entry[1][0])
825
858
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
826
859
                         state._dirblock_state)
827
860
        mode = stat_value.st_mode
828
 
        self.assertEqual([('sha1', 'a'), ('is_exec', mode, False)], state._log)
 
861
        self.assertEqual([('is_exec', mode, False)], state._log)
829
862
 
830
863
        state.save()
831
864
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
835
868
        # so we will re-read the file. Roll the clock back so the file is
836
869
        # guaranteed to look too new.
837
870
        state.adjust_time(-10)
 
871
        del state._log[:]
838
872
 
839
873
        link_or_sha1 = self.update_entry(state, entry, abspath='a',
840
874
                                          stat_value=stat_value)
841
 
        self.assertEqual([('sha1', 'a'), ('is_exec', mode, False),
842
 
                          ('sha1', 'a'), ('is_exec', mode, False),
843
 
                         ], state._log)
844
 
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
845
 
                         link_or_sha1)
 
875
        self.assertEqual([('is_exec', mode, False)], state._log)
 
876
        self.assertEqual(None, link_or_sha1)
846
877
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
847
878
                         state._dirblock_state)
848
 
        self.assertEqual([('f', '', 14, False, dirstate.DirState.NULLSTAT)],
849
 
                         entry[1])
 
879
        self.assertEqual(('f', '', 14, False, dirstate.DirState.NULLSTAT),
 
880
                         entry[1][0])
850
881
        state.save()
851
882
 
852
 
        # However, if we move the clock forward so the file is considered
853
 
        # "stable", it should just cache the value.
 
883
        # If it is cachable (the clock has moved forward) but new it still
 
884
        # won't calculate the sha or cache it.
854
885
        state.adjust_time(+20)
 
886
        del state._log[:]
 
887
        link_or_sha1 = dirstate.update_entry(state, entry, abspath='a',
 
888
                                          stat_value=stat_value)
 
889
        self.assertEqual(None, link_or_sha1)
 
890
        self.assertEqual([('is_exec', mode, False)], state._log)
 
891
        self.assertEqual(('f', '', 14, False, dirstate.DirState.NULLSTAT),
 
892
                         entry[1][0])
 
893
 
 
894
        # If the file is no longer new, and the clock has been moved forward
 
895
        # sufficiently, it will cache the sha.
 
896
        del state._log[:]
 
897
        state.set_parent_trees(
 
898
            [(with_a_id, tree.branch.repository.revision_tree(with_a_id))],
 
899
            [])
 
900
        entry = state._get_entry(0, path_utf8='a')
 
901
 
855
902
        link_or_sha1 = self.update_entry(state, entry, abspath='a',
856
903
                                          stat_value=stat_value)
857
904
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
858
905
                         link_or_sha1)
859
 
        self.assertEqual([('sha1', 'a'), ('is_exec', mode, False),
860
 
                          ('sha1', 'a'), ('is_exec', mode, False),
861
 
                          ('sha1', 'a'), ('is_exec', mode, False),
862
 
                         ], state._log)
863
 
        self.assertEqual([('f', link_or_sha1, 14, False, packed_stat)],
864
 
                         entry[1])
 
906
        self.assertEqual([('is_exec', mode, False), ('sha1', 'a')],
 
907
                          state._log)
 
908
        self.assertEqual(('f', link_or_sha1, 14, False, packed_stat),
 
909
                         entry[1][0])
865
910
 
866
911
        # Subsequent calls will just return the cached value
 
912
        del state._log[:]
867
913
        link_or_sha1 = self.update_entry(state, entry, abspath='a',
868
914
                                          stat_value=stat_value)
869
915
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
870
916
                         link_or_sha1)
871
 
        self.assertEqual([('sha1', 'a'), ('is_exec', mode, False),
872
 
                          ('sha1', 'a'), ('is_exec', mode, False),
873
 
                          ('sha1', 'a'), ('is_exec', mode, False),
874
 
                         ], state._log)
875
 
        self.assertEqual([('f', link_or_sha1, 14, False, packed_stat)],
876
 
                         entry[1])
 
917
        self.assertEqual([], state._log)
 
918
        self.assertEqual(('f', link_or_sha1, 14, False, packed_stat),
 
919
                         entry[1][0])
877
920
 
878
921
    def test_update_entry_symlink(self):
879
922
        """Update entry should read symlinks."""
953
996
                         state._dirblock_state)
954
997
 
955
998
    def test_update_entry_file_unchanged(self):
956
 
        state, entry = self.get_state_with_a()
 
999
        state, _ = self.get_state_with_a()
 
1000
        tree = self.make_branch_and_tree('tree')
 
1001
        tree.lock_write()
 
1002
        self.build_tree(['tree/a'])
 
1003
        tree.add(['a'], ['a-id'])
 
1004
        with_a_id = tree.commit('witha')
 
1005
        self.addCleanup(tree.unlock)
 
1006
        state.set_parent_trees(
 
1007
            [(with_a_id, tree.branch.repository.revision_tree(with_a_id))],
 
1008
            [])
 
1009
        entry = state._get_entry(0, path_utf8='a')
957
1010
        self.build_tree(['a'])
958
1011
        sha1sum = 'b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6'
959
1012
        state.adjust_time(+20)
968
1021
                         state._dirblock_state)
969
1022
 
970
1023
    def create_and_test_file(self, state, entry):
971
 
        """Create a file at 'a' and verify the state finds it.
 
1024
        """Create a file at 'a' and verify the state finds it during update.
972
1025
 
973
1026
        The state should already be versioning *something* at 'a'. This makes
974
1027
        sure that state.update_entry recognizes it as a file.
978
1031
        packed_stat = dirstate.pack_stat(stat_value)
979
1032
 
980
1033
        link_or_sha1 = self.do_update_entry(state, entry, abspath='a')
981
 
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
982
 
                         link_or_sha1)
983
 
        self.assertEqual([('f', link_or_sha1, 14, False, packed_stat)],
 
1034
        self.assertEqual(None, link_or_sha1)
 
1035
        self.assertEqual([('f', '', 14, False, dirstate.DirState.NULLSTAT)],
984
1036
                         entry[1])
985
1037
        return packed_stat
986
1038
 
1102
1154
        self.assertEqual([('f', '', 14, True, dirstate.DirState.NULLSTAT)],
1103
1155
                         entry[1])
1104
1156
 
1105
 
        # Make the disk object look old enough to cache
 
1157
        # Make the disk object look old enough to cache (but it won't cache the sha
 
1158
        # as it is a new file).
1106
1159
        state.adjust_time(+20)
1107
1160
        digest = 'b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6'
1108
1161
        self.update_entry(state, entry, abspath='a', stat_value=stat_value)
1109
 
        self.assertEqual([('f', digest, 14, True, packed_stat)], entry[1])
 
1162
        self.assertEqual([('f', '', 14, True, dirstate.DirState.NULLSTAT)],
 
1163
            entry[1])
1110
1164
 
1111
1165
 
1112
1166
class TestCompiledUpdateEntry(TestUpdateEntry):