~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/dirstate.py

  • Committer: Andrew Bennetts
  • Date: 2007-03-26 06:24:01 UTC
  • mto: This revision was merged to the branch mainline in revision 2376.
  • Revision ID: andrew.bennetts@canonical.com-20070326062401-k3nbefzje5332jaf
Deal with review comments from Robert:

  * Add my name to the NEWS file
  * Move the test case to a new module in branch_implementations
  * Remove revision_history cruft from identitymap and test_identitymap
  * Improve some docstrings

Also, this fixes a bug where revision_history was not returning a copy of the
cached data, allowing the cache to be corrupted.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
 
1
# Copyright (C) 2006, 2007 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
29
29
 
30
30
dirstate format = header line, full checksum, row count, parent details,
31
31
 ghost_details, entries;
32
 
header line = "#bazaar dirstate flat format 3", NL;
 
32
header line = "#bazaar dirstate flat format 2", NL;
33
33
full checksum = "crc32: ", ["-"], WHOLE_NUMBER, NL;
34
 
row count = "num_entries: ", WHOLE_NUMBER, NL;
 
34
row count = "num_entries: ", digit, NL;
35
35
parent_details = WHOLE NUMBER, {REVISION_ID}* NL;
36
36
ghost_details = WHOLE NUMBER, {REVISION_ID}*, NL;
37
37
entries = {entry};
118
118
where we need id->path mapping; we also usually read the whole file, so
119
119
I'm going to skip that for the moment, as we have the ability to locate
120
120
via bisect any path in any tree, and if we lookup things by path, we can
121
 
accumulate an id->path mapping as we go, which will tend to match what we
 
121
accumulate a id->path mapping as we go, which will tend to match what we
122
122
looked for.
123
123
 
124
124
I plan to implement this asap, so please speak up now to alter/tweak the
143
143
Locking:
144
144
 Eventually reuse dirstate objects across locks IFF the dirstate file has not
145
145
 been modified, but will require that we flush/ignore cached stat-hit data
146
 
 because we won't want to restat all files on disk just because a lock was
 
146
 because we wont want to restat all files on disk just because a lock was
147
147
 acquired, yet we cannot trust the data after the previous lock was released.
148
148
 
149
149
Memory representation:
162
162
      manageable number. Will scale badly on trees with 10K entries in a 
163
163
      single directory. compare with Inventory.InventoryDirectory which has
164
164
      a dictionary for the children. No bisect capability, can only probe for
165
 
      exact matches, or grab all elements and sort.
166
 
    - What's the risk of error here? Once we have the base format being processed
 
165
      exact matches, or grab all elements and sorta.
 
166
    - Whats the risk of error here? Once we have the base format being processed
167
167
      we should have a net win regardless of optimality. So we are going to 
168
 
      go with what seems reasonable.
 
168
      go with what seems reasonably.
169
169
open questions:
170
170
 
171
 
Maybe we should do a test profile of the core structure - 10K simulated
172
 
searches/lookups/etc?
 
171
maybe we should do a test profile of these core structure - 10K simulated searches/lookups/etc?
173
172
 
174
173
Objects for each row?
175
174
The lifetime of Dirstate objects is current per lock, but see above for
200
199
 
201
200
"""
202
201
 
 
202
 
 
203
import base64
203
204
import bisect
204
 
import binascii
205
205
import errno
206
206
import os
207
207
from stat import S_IEXEC
208
 
import stat
209
208
import struct
210
209
import sys
211
210
import time
212
211
import zlib
213
212
 
214
213
from bzrlib import (
215
 
    cache_utf8,
216
 
    debug,
217
214
    errors,
218
215
    inventory,
219
216
    lock,
222
219
    )
223
220
 
224
221
 
225
 
def pack_stat(st, _encode=binascii.b2a_base64, _pack=struct.pack):
226
 
    """Convert stat values into a packed representation."""
227
 
    # jam 20060614 it isn't really worth removing more entries if we
228
 
    # are going to leave it in packed form.
229
 
    # With only st_mtime and st_mode filesize is 5.5M and read time is 275ms
230
 
    # With all entries, filesize is 5.9M and read time is maybe 280ms
231
 
    # well within the noise margin
232
 
 
233
 
    # base64 encoding always adds a final newline, so strip it off
234
 
    # The current version
235
 
    return _encode(_pack('>LLLLLL'
236
 
        , st.st_size, int(st.st_mtime), int(st.st_ctime)
237
 
        , st.st_dev, st.st_ino & 0xFFFFFFFF, st.st_mode))[:-1]
238
 
    # This is 0.060s / 1.520s faster by not encoding as much information
239
 
    # return _encode(_pack('>LL', int(st.st_mtime), st.st_mode))[:-1]
240
 
    # This is not strictly faster than _encode(_pack())[:-1]
241
 
    # return '%X.%X.%X.%X.%X.%X' % (
242
 
    #      st.st_size, int(st.st_mtime), int(st.st_ctime),
243
 
    #      st.st_dev, st.st_ino, st.st_mode)
244
 
    # Similar to the _encode(_pack('>LL'))
245
 
    # return '%X.%X' % (int(st.st_mtime), st.st_mode)
 
222
class _Bisector(object):
 
223
    """This just keeps track of information as we are bisecting."""
246
224
 
247
225
 
248
226
class DirState(object):
250
228
 
251
229
    A dirstate is a specialised data structure for managing local working
252
230
    tree state information. Its not yet well defined whether it is platform
253
 
    specific, and if it is how we detect/parameterize that.
 
231
    specific, and if it is how we detect/parameterise that.
254
232
 
255
233
    Dirstates use the usual lock_write, lock_read and unlock mechanisms.
256
234
    Unlike most bzr disk formats, DirStates must be locked for reading, using
277
255
            'r': 'relocated',
278
256
            't': 'tree-reference',
279
257
        }
280
 
    _stat_to_minikind = {
281
 
        stat.S_IFDIR:'d',
282
 
        stat.S_IFREG:'f',
283
 
        stat.S_IFLNK:'l',
284
 
    }
285
258
    _to_yesno = {True:'y', False: 'n'} # TODO profile the performance gain
286
259
     # of using int conversion rather than a dict here. AND BLAME ANDREW IF
287
260
     # it is faster.
306
279
    def __init__(self, path):
307
280
        """Create a  DirState object.
308
281
 
 
282
        Attributes of note:
 
283
 
 
284
        :attr _root_entrie: The root row of the directory/file information,
 
285
            - contains the path to / - '', ''
 
286
            - kind of 'directory',
 
287
            - the file id of the root in utf8
 
288
            - size of 0
 
289
            - a packed state
 
290
            - and no sha information.
309
291
        :param path: The path at which the dirstate file on disk should live.
310
292
        """
311
293
        # _header_state and _dirblock_state represent the current state
321
303
        # modified states.
322
304
        self._header_state = DirState.NOT_IN_MEMORY
323
305
        self._dirblock_state = DirState.NOT_IN_MEMORY
324
 
        # If true, an error has been detected while updating the dirstate, and 
325
 
        # for safety we're not going to commit to disk.
326
 
        self._changes_aborted = False
327
306
        self._dirblocks = []
328
307
        self._ghosts = []
329
308
        self._parents = []
332
311
        self._lock_token = None
333
312
        self._lock_state = None
334
313
        self._id_index = None
335
 
        # a map from packed_stat to sha's.
336
 
        self._packed_stat_index = None
337
314
        self._end_of_header = None
338
315
        self._cutoff_time = None
339
316
        self._split_path_cache = {}
340
317
        self._bisect_page_size = DirState.BISECT_PAGE_SIZE
341
 
        if 'hashcache' in debug.debug_flags:
342
 
            self._sha1_file = self._sha1_file_and_mutter
343
 
        else:
344
 
            self._sha1_file = osutils.sha_file_by_name
345
 
        # These two attributes provide a simple cache for lookups into the
346
 
        # dirstate in-memory vectors. By probing respectively for the last
347
 
        # block, and for the next entry, we save nearly 2 bisections per path
348
 
        # during commit.
349
 
        self._last_block_index = None
350
 
        self._last_entry_index = None
351
318
 
352
319
    def __repr__(self):
353
320
        return "%s(%r)" % \
373
340
        # find the location in the block.
374
341
        # check its not there
375
342
        # add it.
376
 
        #------- copied from inventory.ensure_normalized_name - keep synced.
 
343
        #------- copied from inventory.make_entry
377
344
        # --- normalized_filename wants a unicode basename only, so get one.
378
345
        dirname, basename = osutils.split(path)
379
346
        # we dont import normalized_filename directly because we want to be
393
360
        # faster than three separate encodes.
394
361
        utf8path = (dirname + '/' + basename).strip('/').encode('utf8')
395
362
        dirname, basename = osutils.split(utf8path)
396
 
        # uses __class__ for speed; the check is needed for safety
397
 
        if file_id.__class__ is not str:
398
 
            raise AssertionError(
399
 
                "must be a utf8 file_id not %s" % (type(file_id), ))
 
363
        assert file_id.__class__ == str, \
 
364
            "must be a utf8 file_id not %s" % (type(file_id))
400
365
        # Make sure the file_id does not exist in this tree
401
366
        file_id_entry = self._get_entry(0, fileid_utf8=file_id)
402
367
        if file_id_entry != (None, None):
458
423
        if not present:
459
424
            block.insert(entry_index, entry_data)
460
425
        else:
461
 
            if block[entry_index][1][0][0] != 'a':
462
 
                raise AssertionError(" %r(%r) already added" % (basename, file_id))
 
426
            assert block[entry_index][1][0][0] == 'a', " %r(%r) already added" % (basename, file_id)
463
427
            block[entry_index][1][0] = entry_data[1][0]
464
428
 
465
429
        if kind == 'directory':
469
433
        if self._id_index:
470
434
            self._id_index.setdefault(entry_key[2], set()).add(entry_key)
471
435
 
472
 
    def _bisect(self, paths):
 
436
    def _bisect(self, dir_name_list):
473
437
        """Bisect through the disk structure for specific rows.
474
438
 
475
 
        :param paths: A list of paths to find
476
 
        :return: A dict mapping path => entries for found entries. Missing
 
439
        :param dir_name_list: A list of (dir, name) pairs.
 
440
        :return: A dict mapping (dir, name) => entry for found entries. Missing
477
441
                 entries will not be in the map.
478
 
                 The list is not sorted, and entries will be populated
479
 
                 based on when they were read.
480
442
        """
481
443
        self._requires_lock()
482
444
        # We need the file pointer to be right after the initial header block
484
446
        # If _dirblock_state was in memory, we should just return info from
485
447
        # there, this function is only meant to handle when we want to read
486
448
        # part of the disk.
487
 
        if self._dirblock_state != DirState.NOT_IN_MEMORY:
488
 
            raise AssertionError("bad dirblock state %r" % self._dirblock_state)
 
449
        assert self._dirblock_state == DirState.NOT_IN_MEMORY
489
450
 
490
451
        # The disk representation is generally info + '\0\n\0' at the end. But
491
452
        # for bisecting, it is easier to treat this as '\0' + info + '\0\n'
503
464
        found = {}
504
465
 
505
466
        # Avoid infinite seeking
506
 
        max_count = 30*len(paths)
 
467
        max_count = 30*len(dir_name_list)
507
468
        count = 0
508
469
        # pending is a list of places to look.
509
470
        # each entry is a tuple of low, high, dir_names
511
472
        #   high -> the last byte offset (inclusive)
512
473
        #   dir_names -> The list of (dir, name) pairs that should be found in
513
474
        #                the [low, high] range
514
 
        pending = [(low, high, paths)]
 
475
        pending = [(low, high, dir_name_list)]
515
476
 
516
477
        page_size = self._bisect_page_size
517
478
 
570
531
                # Find what entries we are looking for, which occur before and
571
532
                # after this first record.
572
533
                after = start
573
 
                if first_fields[1]:
574
 
                    first_path = first_fields[1] + '/' + first_fields[2]
575
 
                else:
576
 
                    first_path = first_fields[2]
577
 
                first_loc = _bisect_path_left(cur_files, first_path)
 
534
                first_dir_name = (first_fields[1], first_fields[2])
 
535
                first_loc = bisect.bisect_left(cur_files, first_dir_name)
578
536
 
579
537
                # These exist before the current location
580
538
                pre = cur_files[:first_loc]
597
555
                else:
598
556
                    after = mid + len(block)
599
557
 
600
 
                if last_fields[1]:
601
 
                    last_path = last_fields[1] + '/' + last_fields[2]
602
 
                else:
603
 
                    last_path = last_fields[2]
604
 
                last_loc = _bisect_path_right(post, last_path)
 
558
                last_dir_name = (last_fields[1], last_fields[2])
 
559
                last_loc = bisect.bisect_right(post, last_dir_name)
605
560
 
606
561
                middle_files = post[:last_loc]
607
562
                post = post[last_loc:]
612
567
                    # Either we will find them here, or we can mark them as
613
568
                    # missing.
614
569
 
615
 
                    if middle_files[0] == first_path:
 
570
                    if middle_files[0] == first_dir_name:
616
571
                        # We might need to go before this location
617
 
                        pre.append(first_path)
618
 
                    if middle_files[-1] == last_path:
619
 
                        post.insert(0, last_path)
 
572
                        pre.append(first_dir_name)
 
573
                    if middle_files[-1] == last_dir_name:
 
574
                        post.insert(0, last_dir_name)
620
575
 
621
576
                    # Find out what paths we have
622
 
                    paths = {first_path:[first_fields]}
623
 
                    # last_path might == first_path so we need to be
 
577
                    paths = {first_dir_name:[first_fields]}
 
578
                    # last_dir_name might == first_dir_name so we need to be
624
579
                    # careful if we should append rather than overwrite
625
580
                    if last_entry_num != first_entry_num:
626
 
                        paths.setdefault(last_path, []).append(last_fields)
 
581
                        paths.setdefault(last_dir_name, []).append(last_fields)
627
582
                    for num in xrange(first_entry_num+1, last_entry_num):
628
583
                        # TODO: jam 20070223 We are already splitting here, so
629
584
                        #       shouldn't we just split the whole thing rather
630
585
                        #       than doing the split again in add_one_record?
631
586
                        fields = entries[num].split('\0')
632
 
                        if fields[1]:
633
 
                            path = fields[1] + '/' + fields[2]
634
 
                        else:
635
 
                            path = fields[2]
636
 
                        paths.setdefault(path, []).append(fields)
 
587
                        dir_name = (fields[1], fields[2])
 
588
                        paths.setdefault(dir_name, []).append(fields)
637
589
 
638
 
                    for path in middle_files:
639
 
                        for fields in paths.get(path, []):
 
590
                    for dir_name in middle_files:
 
591
                        for fields in paths.get(dir_name, []):
640
592
                            # offset by 1 because of the opening '\0'
641
593
                            # consider changing fields_to_entry to avoid the
642
594
                            # extra list slice
643
595
                            entry = fields_to_entry(fields[1:])
644
 
                            found.setdefault(path, []).append(entry)
 
596
                            found.setdefault(dir_name, []).append(entry)
645
597
 
646
598
            # Now we have split up everything into pre, middle, and post, and
647
599
            # we have handled everything that fell in 'middle'.
664
616
        _bisect_dirblocks is meant to find the contents of directories, which
665
617
        differs from _bisect, which only finds individual entries.
666
618
 
667
 
        :param dir_list: A sorted list of directory names ['', 'dir', 'foo'].
 
619
        :param dir_list: An sorted list of directory names ['', 'dir', 'foo'].
668
620
        :return: A map from dir => entries_for_dir
669
621
        """
670
622
        # TODO: jam 20070223 A lot of the bisecting logic could be shared
677
629
        # If _dirblock_state was in memory, we should just return info from
678
630
        # there, this function is only meant to handle when we want to read
679
631
        # part of the disk.
680
 
        if self._dirblock_state != DirState.NOT_IN_MEMORY:
681
 
            raise AssertionError("bad dirblock state %r" % self._dirblock_state)
 
632
        assert self._dirblock_state == DirState.NOT_IN_MEMORY
 
633
 
682
634
        # The disk representation is generally info + '\0\n\0' at the end. But
683
635
        # for bisecting, it is easier to treat this as '\0' + info + '\0\n'
684
636
        # Because it means we can sync on the '\n'
837
789
 
838
790
        return found
839
791
 
840
 
    def _bisect_recursive(self, paths):
 
792
    def _bisect_recursive(self, dir_name_list):
841
793
        """Bisect for entries for all paths and their children.
842
794
 
843
795
        This will use bisect to find all records for the supplied paths. It
856
808
        # Directories that have been read
857
809
        processed_dirs = set()
858
810
        # Get the ball rolling with the first bisect for all entries.
859
 
        newly_found = self._bisect(paths)
 
811
        newly_found = self._bisect(dir_name_list)
860
812
 
861
813
        while newly_found:
862
814
            # Directories that need to be read
886
838
                            if dir_name[0] in pending_dirs:
887
839
                                # This entry will be found in the dir search
888
840
                                continue
 
841
                            # TODO: We need to check if this entry has
 
842
                            #       already been found. Otherwise we might be
 
843
                            #       hitting infinite recursion.
889
844
                            if dir_name not in found_dir_names:
890
 
                                paths_to_search.add(tree_info[1])
 
845
                                paths_to_search.add(dir_name)
891
846
            # Now we have a list of paths to look for directly, and
892
847
            # directory blocks that need to be read.
893
848
            # newly_found is mixing the keys between (dir, name) and path
898
853
            processed_dirs.update(pending_dirs)
899
854
        return found
900
855
 
901
 
    def _discard_merge_parents(self):
902
 
        """Discard any parents trees beyond the first.
903
 
        
904
 
        Note that if this fails the dirstate is corrupted.
905
 
 
906
 
        After this function returns the dirstate contains 2 trees, neither of
907
 
        which are ghosted.
908
 
        """
909
 
        self._read_header_if_needed()
910
 
        parents = self.get_parent_ids()
911
 
        if len(parents) < 1:
912
 
            return
913
 
        # only require all dirblocks if we are doing a full-pass removal.
914
 
        self._read_dirblocks_if_needed()
915
 
        dead_patterns = set([('a', 'r'), ('a', 'a'), ('r', 'r'), ('r', 'a')])
916
 
        def iter_entries_removable():
917
 
            for block in self._dirblocks:
918
 
                deleted_positions = []
919
 
                for pos, entry in enumerate(block[1]):
920
 
                    yield entry
921
 
                    if (entry[1][0][0], entry[1][1][0]) in dead_patterns:
922
 
                        deleted_positions.append(pos)
923
 
                if deleted_positions:
924
 
                    if len(deleted_positions) == len(block[1]):
925
 
                        del block[1][:]
926
 
                    else:
927
 
                        for pos in reversed(deleted_positions):
928
 
                            del block[1][pos]
929
 
        # if the first parent is a ghost:
930
 
        if parents[0] in self.get_ghosts():
931
 
            empty_parent = [DirState.NULL_PARENT_DETAILS]
932
 
            for entry in iter_entries_removable():
933
 
                entry[1][1:] = empty_parent
934
 
        else:
935
 
            for entry in iter_entries_removable():
936
 
                del entry[1][2:]
937
 
 
938
 
        self._ghosts = []
939
 
        self._parents = [parents[0]]
940
 
        self._dirblock_state = DirState.IN_MEMORY_MODIFIED
941
 
        self._header_state = DirState.IN_MEMORY_MODIFIED
942
 
 
943
856
    def _empty_parent_info(self):
944
857
        return [DirState.NULL_PARENT_DETAILS] * (len(self._parents) -
945
858
                                                    len(self._ghosts))
971
884
        # the basename of the directory must be the end of its full name.
972
885
        if not (parent_block_index == -1 and
973
886
            parent_block_index == -1 and dirname == ''):
974
 
            if not dirname.endswith(
975
 
                    self._dirblocks[parent_block_index][1][parent_row_index][0][1]):
976
 
                raise AssertionError("bad dirname %r" % dirname)
 
887
            assert dirname.endswith(
 
888
                self._dirblocks[parent_block_index][1][parent_row_index][0][1])
977
889
        block_index, present = self._find_block_index_from_key((dirname, '', ''))
978
890
        if not present:
979
891
            ## In future, when doing partial parsing, this should load and 
991
903
            to prevent unneeded overhead when callers have a sorted list already.
992
904
        :return: Nothing.
993
905
        """
994
 
        if new_entries[0][0][0:2] != ('', ''):
995
 
            raise AssertionError(
996
 
                "Missing root row %r" % (new_entries[0][0],))
 
906
        assert new_entries[0][0][0:2] == ('', ''), \
 
907
            "Missing root row %r" % (new_entries[0][0],)
997
908
        # The two blocks here are deliberate: the root block and the 
998
909
        # contents-of-root block.
999
910
        self._dirblocks = [('', []), ('', [])]
1021
932
        # The above loop leaves the "root block" entries mixed with the
1022
933
        # "contents-of-root block". But we don't want an if check on
1023
934
        # all entries, so instead we just fix it up here.
1024
 
        if self._dirblocks[1] != ('', []):
1025
 
            raise ValueError("bad dirblock start %r" % (self._dirblocks[1],))
 
935
        assert self._dirblocks[1] == ('', [])
1026
936
        root_block = []
1027
937
        contents_of_root_block = []
1028
938
        for entry in self._dirblocks[0][1]:
1094
1004
        """
1095
1005
        if key[0:2] == ('', ''):
1096
1006
            return 0, True
1097
 
        try:
1098
 
            if (self._last_block_index is not None and
1099
 
                self._dirblocks[self._last_block_index][0] == key[0]):
1100
 
                return self._last_block_index, True
1101
 
        except IndexError:
1102
 
            pass
1103
1007
        block_index = bisect_dirblock(self._dirblocks, key[0], 1,
1104
1008
                                      cache=self._split_path_cache)
1105
1009
        # _right returns one-past-where-key is so we have to subtract
1110
1014
        # simple and correct:
1111
1015
        present = (block_index < len(self._dirblocks) and
1112
1016
            self._dirblocks[block_index][0] == key[0])
1113
 
        self._last_block_index = block_index
1114
 
        # Reset the entry index cache to the beginning of the block.
1115
 
        self._last_entry_index = -1
1116
1017
        return block_index, present
1117
1018
 
1118
1019
    def _find_entry_index(self, key, block):
1120
1021
 
1121
1022
        :return: The entry index, True if the entry for the key is present.
1122
1023
        """
1123
 
        len_block = len(block)
1124
 
        try:
1125
 
            if self._last_entry_index is not None:
1126
 
                # mini-bisect here.
1127
 
                entry_index = self._last_entry_index + 1
1128
 
                # A hit is when the key is after the last slot, and before or
1129
 
                # equal to the next slot.
1130
 
                if ((entry_index > 0 and block[entry_index - 1][0] < key) and
1131
 
                    key <= block[entry_index][0]):
1132
 
                    self._last_entry_index = entry_index
1133
 
                    present = (block[entry_index][0] == key)
1134
 
                    return entry_index, present
1135
 
        except IndexError:
1136
 
            pass
1137
1024
        entry_index = bisect.bisect_left(block, (key, []))
1138
 
        present = (entry_index < len_block and
 
1025
        present = (entry_index < len(block) and
1139
1026
            block[entry_index][0] == key)
1140
 
        self._last_entry_index = entry_index
1141
1027
        return entry_index, present
1142
1028
 
1143
1029
    @staticmethod
1173
1059
            raise
1174
1060
        return result
1175
1061
 
1176
 
    def update_by_delta(self, delta):
1177
 
        """Apply an inventory delta to the dirstate for tree 0
1178
 
 
1179
 
        :param delta: An inventory delta.  See Inventory.apply_delta for
1180
 
            details.
1181
 
        """
1182
 
        self._read_dirblocks_if_needed()
1183
 
        insertions = {}
1184
 
        removals = {}
1185
 
        for old_path, new_path, file_id, inv_entry in sorted(delta, reverse=True):
1186
 
            if (file_id in insertions) or (file_id in removals):
1187
 
                raise AssertionError("repeated file id in delta %r" % (file_id,))
1188
 
            if old_path is not None:
1189
 
                old_path = old_path.encode('utf-8')
1190
 
                removals[file_id] = old_path
1191
 
            if new_path is not None:
1192
 
                new_path = new_path.encode('utf-8')
1193
 
                dirname, basename = osutils.split(new_path)
1194
 
                key = (dirname, basename, file_id)
1195
 
                minikind = DirState._kind_to_minikind[inv_entry.kind]
1196
 
                if minikind == 't':
1197
 
                    fingerprint = inv_entry.reference_revision
1198
 
                else:
1199
 
                    fingerprint = ''
1200
 
                insertions[file_id] = (key, minikind, inv_entry.executable,
1201
 
                                       fingerprint, new_path)
1202
 
            # Transform moves into delete+add pairs
1203
 
            if None not in (old_path, new_path):
1204
 
                for child in self._iter_child_entries(0, old_path):
1205
 
                    if child[0][2] in insertions or child[0][2] in removals:
1206
 
                        continue
1207
 
                    child_dirname = child[0][0]
1208
 
                    child_basename = child[0][1]
1209
 
                    minikind = child[1][0][0]
1210
 
                    fingerprint = child[1][0][4]
1211
 
                    executable = child[1][0][3]
1212
 
                    old_child_path = osutils.pathjoin(child[0][0],
1213
 
                                                      child[0][1])
1214
 
                    removals[child[0][2]] = old_child_path
1215
 
                    child_suffix = child_dirname[len(old_path):]
1216
 
                    new_child_dirname = (new_path + child_suffix)
1217
 
                    key = (new_child_dirname, child_basename, child[0][2])
1218
 
                    new_child_path = os.path.join(new_child_dirname,
1219
 
                                                  child_basename)
1220
 
                    insertions[child[0][2]] = (key, minikind, executable,
1221
 
                                               fingerprint, new_child_path)
1222
 
        self._apply_removals(removals.values())
1223
 
        self._apply_insertions(insertions.values())
1224
 
 
1225
 
    def _apply_removals(self, removals):
1226
 
        for path in sorted(removals, reverse=True):
1227
 
            dirname, basename = osutils.split(path)
1228
 
            block_i, entry_i, d_present, f_present = \
1229
 
                self._get_block_entry_index(dirname, basename, 0)
1230
 
            entry = self._dirblocks[block_i][1][entry_i]
1231
 
            self._make_absent(entry)
1232
 
            # See if we have a malformed delta: deleting a directory must not
1233
 
            # leave crud behind. This increases the number of bisects needed
1234
 
            # substantially, but deletion or renames of large numbers of paths
1235
 
            # is rare enough it shouldn't be an issue (famous last words?) RBC
1236
 
            # 20080730.
1237
 
            block_i, entry_i, d_present, f_present = \
1238
 
                self._get_block_entry_index(path, '', 0)
1239
 
            if d_present:
1240
 
                # The dir block is still present in the dirstate; this could
1241
 
                # be due to it being in a parent tree, or a corrupt delta.
1242
 
                for child_entry in self._dirblocks[block_i][1]:
1243
 
                    if child_entry[1][0][0] not in ('r', 'a'):
1244
 
                        raise errors.InconsistentDelta(path, entry[0][2],
1245
 
                            "The file id was deleted but its children were "
1246
 
                            "not deleted.")
1247
 
 
1248
 
    def _apply_insertions(self, adds):
1249
 
        for key, minikind, executable, fingerprint, path_utf8 in sorted(adds):
1250
 
            self.update_minimal(key, minikind, executable, fingerprint,
1251
 
                                path_utf8=path_utf8)
1252
 
 
1253
 
    def update_basis_by_delta(self, delta, new_revid):
1254
 
        """Update the parents of this tree after a commit.
1255
 
 
1256
 
        This gives the tree one parent, with revision id new_revid. The
1257
 
        inventory delta is applied to the current basis tree to generate the
1258
 
        inventory for the parent new_revid, and all other parent trees are
1259
 
        discarded.
1260
 
 
1261
 
        Note that an exception during the operation of this method will leave
1262
 
        the dirstate in a corrupt state where it should not be saved.
1263
 
 
1264
 
        Finally, we expect all changes to be synchronising the basis tree with
1265
 
        the working tree.
1266
 
 
1267
 
        :param new_revid: The new revision id for the trees parent.
1268
 
        :param delta: An inventory delta (see apply_inventory_delta) describing
1269
 
            the changes from the current left most parent revision to new_revid.
1270
 
        """
1271
 
        self._read_dirblocks_if_needed()
1272
 
        self._discard_merge_parents()
1273
 
        if self._ghosts != []:
1274
 
            raise NotImplementedError(self.update_basis_by_delta)
1275
 
        if len(self._parents) == 0:
1276
 
            # setup a blank tree, the most simple way.
1277
 
            empty_parent = DirState.NULL_PARENT_DETAILS
1278
 
            for entry in self._iter_entries():
1279
 
                entry[1].append(empty_parent)
1280
 
            self._parents.append(new_revid)
1281
 
 
1282
 
        self._parents[0] = new_revid
1283
 
 
1284
 
        delta = sorted(delta, reverse=True)
1285
 
        adds = []
1286
 
        changes = []
1287
 
        deletes = []
1288
 
        # The paths this function accepts are unicode and must be encoded as we
1289
 
        # go.
1290
 
        encode = cache_utf8.encode
1291
 
        inv_to_entry = self._inv_entry_to_details
1292
 
        # delta is now (deletes, changes), (adds) in reverse lexographical
1293
 
        # order.
1294
 
        # deletes in reverse lexographic order are safe to process in situ.
1295
 
        # renames are not, as a rename from any path could go to a path
1296
 
        # lexographically lower, so we transform renames into delete, add pairs,
1297
 
        # expanding them recursively as needed.
1298
 
        # At the same time, to reduce interface friction we convert the input
1299
 
        # inventory entries to dirstate.
1300
 
        root_only = ('', '')
1301
 
        for old_path, new_path, file_id, inv_entry in delta:
1302
 
            if old_path is None:
1303
 
                adds.append((None, encode(new_path), file_id,
1304
 
                    inv_to_entry(inv_entry), True))
1305
 
            elif new_path is None:
1306
 
                deletes.append((encode(old_path), None, file_id, None, True))
1307
 
            elif (old_path, new_path) != root_only:
1308
 
                # Renames:
1309
 
                # Because renames must preserve their children we must have
1310
 
                # processed all relocations and removes before hand. The sort
1311
 
                # order ensures we've examined the child paths, but we also
1312
 
                # have to execute the removals, or the split to an add/delete
1313
 
                # pair will result in the deleted item being reinserted, or
1314
 
                # renamed items being reinserted twice - and possibly at the
1315
 
                # wrong place. Splitting into a delete/add pair also simplifies
1316
 
                # the handling of entries with ('f', ...), ('r' ...) because
1317
 
                # the target of the 'r' is old_path here, and we add that to
1318
 
                # deletes, meaning that the add handler does not need to check
1319
 
                # for 'r' items on every pass.
1320
 
                self._update_basis_apply_deletes(deletes)
1321
 
                deletes = []
1322
 
                new_path_utf8 = encode(new_path)
1323
 
                # Split into an add/delete pair recursively.
1324
 
                adds.append((None, new_path_utf8, file_id,
1325
 
                    inv_to_entry(inv_entry), False))
1326
 
                # Expunge deletes that we've seen so that deleted/renamed
1327
 
                # children of a rename directory are handled correctly.
1328
 
                new_deletes = reversed(list(self._iter_child_entries(1,
1329
 
                    encode(old_path))))
1330
 
                # Remove the current contents of the tree at orig_path, and
1331
 
                # reinsert at the correct new path.
1332
 
                for entry in new_deletes:
1333
 
                    if entry[0][0]:
1334
 
                        source_path = entry[0][0] + '/' + entry[0][1]
1335
 
                    else:
1336
 
                        source_path = entry[0][1]
1337
 
                    if new_path_utf8:
1338
 
                        target_path = new_path_utf8 + source_path[len(old_path):]
1339
 
                    else:
1340
 
                        if old_path == '':
1341
 
                            raise AssertionError("cannot rename directory to"
1342
 
                                " itself")
1343
 
                        target_path = source_path[len(old_path) + 1:]
1344
 
                    adds.append((None, target_path, entry[0][2], entry[1][1], False))
1345
 
                    deletes.append(
1346
 
                        (source_path, target_path, entry[0][2], None, False))
1347
 
                deletes.append(
1348
 
                    (encode(old_path), new_path, file_id, None, False))
1349
 
            else:
1350
 
                # changes to just the root should not require remove/insertion
1351
 
                # of everything.
1352
 
                changes.append((encode(old_path), encode(new_path), file_id,
1353
 
                    inv_to_entry(inv_entry)))
1354
 
 
1355
 
        # Finish expunging deletes/first half of renames.
1356
 
        self._update_basis_apply_deletes(deletes)
1357
 
        # Reinstate second half of renames and new paths.
1358
 
        self._update_basis_apply_adds(adds)
1359
 
        # Apply in-situ changes.
1360
 
        self._update_basis_apply_changes(changes)
1361
 
 
1362
 
        self._dirblock_state = DirState.IN_MEMORY_MODIFIED
1363
 
        self._header_state = DirState.IN_MEMORY_MODIFIED
1364
 
        self._id_index = None
1365
 
        return
1366
 
 
1367
 
    def _update_basis_apply_adds(self, adds):
1368
 
        """Apply a sequence of adds to tree 1 during update_basis_by_delta.
1369
 
 
1370
 
        They may be adds, or renames that have been split into add/delete
1371
 
        pairs.
1372
 
 
1373
 
        :param adds: A sequence of adds. Each add is a tuple:
1374
 
            (None, new_path_utf8, file_id, (entry_details), real_add). real_add
1375
 
            is False when the add is the second half of a remove-and-reinsert
1376
 
            pair created to handle renames and deletes.
1377
 
        """
1378
 
        # Adds are accumulated partly from renames, so can be in any input
1379
 
        # order - sort it.
1380
 
        adds.sort()
1381
 
        # adds is now in lexographic order, which places all parents before
1382
 
        # their children, so we can process it linearly.
1383
 
        absent = 'ar'
1384
 
        for old_path, new_path, file_id, new_details, real_add in adds:
1385
 
            # the entry for this file_id must be in tree 0.
1386
 
            entry = self._get_entry(0, file_id, new_path)
1387
 
            if entry[0] is None or entry[0][2] != file_id:
1388
 
                self._changes_aborted = True
1389
 
                raise errors.InconsistentDelta(new_path, file_id,
1390
 
                    'working tree does not contain new entry')
1391
 
            if real_add and entry[1][1][0] not in absent:
1392
 
                self._changes_aborted = True
1393
 
                raise errors.InconsistentDelta(new_path, file_id,
1394
 
                    'The entry was considered to be a genuinely new record,'
1395
 
                    ' but there was already an old record for it.')
1396
 
            # We don't need to update the target of an 'r' because the handling
1397
 
            # of renames turns all 'r' situations into a delete at the original
1398
 
            # location.
1399
 
            entry[1][1] = new_details
1400
 
 
1401
 
    def _update_basis_apply_changes(self, changes):
1402
 
        """Apply a sequence of changes to tree 1 during update_basis_by_delta.
1403
 
 
1404
 
        :param adds: A sequence of changes. Each change is a tuple:
1405
 
            (path_utf8, path_utf8, file_id, (entry_details))
1406
 
        """
1407
 
        absent = 'ar'
1408
 
        for old_path, new_path, file_id, new_details in changes:
1409
 
            # the entry for this file_id must be in tree 0.
1410
 
            entry = self._get_entry(0, file_id, new_path)
1411
 
            if entry[0] is None or entry[0][2] != file_id:
1412
 
                self._changes_aborted = True
1413
 
                raise errors.InconsistentDelta(new_path, file_id,
1414
 
                    'working tree does not contain new entry')
1415
 
            if (entry[1][0][0] in absent or
1416
 
                entry[1][1][0] in absent):
1417
 
                self._changes_aborted = True
1418
 
                raise errors.InconsistentDelta(new_path, file_id,
1419
 
                    'changed considered absent')
1420
 
            entry[1][1] = new_details
1421
 
 
1422
 
    def _update_basis_apply_deletes(self, deletes):
1423
 
        """Apply a sequence of deletes to tree 1 during update_basis_by_delta.
1424
 
 
1425
 
        They may be deletes, or renames that have been split into add/delete
1426
 
        pairs.
1427
 
 
1428
 
        :param deletes: A sequence of deletes. Each delete is a tuple:
1429
 
            (old_path_utf8, new_path_utf8, file_id, None, real_delete).
1430
 
            real_delete is True when the desired outcome is an actual deletion
1431
 
            rather than the rename handling logic temporarily deleting a path
1432
 
            during the replacement of a parent.
1433
 
        """
1434
 
        null = DirState.NULL_PARENT_DETAILS
1435
 
        for old_path, new_path, file_id, _, real_delete in deletes:
1436
 
            if real_delete != (new_path is None):
1437
 
                raise AssertionError("bad delete delta")
1438
 
            # the entry for this file_id must be in tree 1.
1439
 
            dirname, basename = osutils.split(old_path)
1440
 
            block_index, entry_index, dir_present, file_present = \
1441
 
                self._get_block_entry_index(dirname, basename, 1)
1442
 
            if not file_present:
1443
 
                self._changes_aborted = True
1444
 
                raise errors.InconsistentDelta(old_path, file_id,
1445
 
                    'basis tree does not contain removed entry')
1446
 
            entry = self._dirblocks[block_index][1][entry_index]
1447
 
            if entry[0][2] != file_id:
1448
 
                self._changes_aborted = True
1449
 
                raise errors.InconsistentDelta(old_path, file_id,
1450
 
                    'mismatched file_id in tree 1')
1451
 
            if real_delete:
1452
 
                if entry[1][0][0] != 'a':
1453
 
                    self._changes_aborted = True
1454
 
                    raise errors.InconsistentDelta(old_path, file_id,
1455
 
                            'This was marked as a real delete, but the WT state'
1456
 
                            ' claims that it still exists and is versioned.')
1457
 
                del self._dirblocks[block_index][1][entry_index]
1458
 
            else:
1459
 
                if entry[1][0][0] == 'a':
1460
 
                    self._changes_aborted = True
1461
 
                    raise errors.InconsistentDelta(old_path, file_id,
1462
 
                        'The entry was considered a rename, but the source path'
1463
 
                        ' is marked as absent.')
1464
 
                    # For whatever reason, we were asked to rename an entry
1465
 
                    # that was originally marked as deleted. This could be
1466
 
                    # because we are renaming the parent directory, and the WT
1467
 
                    # current state has the file marked as deleted.
1468
 
                elif entry[1][0][0] == 'r':
1469
 
                    # implement the rename
1470
 
                    del self._dirblocks[block_index][1][entry_index]
1471
 
                else:
1472
 
                    # it is being resurrected here, so blank it out temporarily.
1473
 
                    self._dirblocks[block_index][1][entry_index][1][1] = null
1474
 
 
1475
 
    def update_entry(self, entry, abspath, stat_value,
1476
 
                     _stat_to_minikind=_stat_to_minikind,
1477
 
                     _pack_stat=pack_stat):
 
1062
    def update_entry(self, entry, abspath, stat_value=None):
1478
1063
        """Update the entry based on what is actually on disk.
1479
1064
 
1480
1065
        :param entry: This is the dirblock entry for the file in question.
1484
1069
        :return: The sha1 hexdigest of the file (40 bytes) or link target of a
1485
1070
                symlink.
1486
1071
        """
 
1072
        # This code assumes that the entry passed in is directly held in one of
 
1073
        # the internal _dirblocks. So the dirblock state must have already been
 
1074
        # read.
 
1075
        assert self._dirblock_state != DirState.NOT_IN_MEMORY
 
1076
        if stat_value is None:
 
1077
            try:
 
1078
                # We could inline os.lstat but the common case is that
 
1079
                # stat_value will be passed in, not read here.
 
1080
                stat_value = self._lstat(abspath, entry)
 
1081
            except (OSError, IOError), e:
 
1082
                if e.errno in (errno.ENOENT, errno.EACCES,
 
1083
                               errno.EPERM):
 
1084
                    # The entry is missing, consider it gone
 
1085
                    return None
 
1086
                raise
 
1087
 
 
1088
        kind = osutils.file_kind_from_stat_mode(stat_value.st_mode)
1487
1089
        try:
1488
 
            minikind = _stat_to_minikind[stat_value.st_mode & 0170000]
1489
 
        except KeyError:
1490
 
            # Unhandled kind
 
1090
            minikind = DirState._kind_to_minikind[kind]
 
1091
        except KeyError: # Unknown kind
1491
1092
            return None
1492
 
        packed_stat = _pack_stat(stat_value)
 
1093
        packed_stat = pack_stat(stat_value)
1493
1094
        (saved_minikind, saved_link_or_sha1, saved_file_size,
1494
1095
         saved_executable, saved_packed_stat) = entry[1][0]
1495
1096
 
1496
1097
        if (minikind == saved_minikind
1497
 
            and packed_stat == saved_packed_stat):
1498
 
            # The stat hasn't changed since we saved, so we can re-use the
1499
 
            # saved sha hash.
 
1098
            and packed_stat == saved_packed_stat
 
1099
            # size should also be in packed_stat
 
1100
            and saved_file_size == stat_value.st_size):
 
1101
            # The stat hasn't changed since we saved, so we can potentially
 
1102
            # re-use the saved sha hash.
1500
1103
            if minikind == 'd':
1501
1104
                return None
1502
1105
 
1503
 
            # size should also be in packed_stat
1504
 
            if saved_file_size == stat_value.st_size:
 
1106
            if self._cutoff_time is None:
 
1107
                self._sha_cutoff_time()
 
1108
 
 
1109
            if (stat_value.st_mtime < self._cutoff_time
 
1110
                and stat_value.st_ctime < self._cutoff_time):
 
1111
                # Return the existing fingerprint
1505
1112
                return saved_link_or_sha1
1506
1113
 
1507
1114
        # If we have gotten this far, that means that we need to actually
1508
1115
        # process this entry.
1509
1116
        link_or_sha1 = None
1510
1117
        if minikind == 'f':
1511
 
            link_or_sha1 = self._sha1_file(abspath)
 
1118
            link_or_sha1 = self._sha1_file(abspath, entry)
1512
1119
            executable = self._is_executable(stat_value.st_mode,
1513
1120
                                             saved_executable)
1514
 
            if self._cutoff_time is None:
1515
 
                self._sha_cutoff_time()
1516
 
            if (stat_value.st_mtime < self._cutoff_time
1517
 
                and stat_value.st_ctime < self._cutoff_time):
1518
 
                entry[1][0] = ('f', link_or_sha1, stat_value.st_size,
1519
 
                               executable, packed_stat)
1520
 
            else:
1521
 
                entry[1][0] = ('f', '', stat_value.st_size,
1522
 
                               executable, DirState.NULLSTAT)
 
1121
            entry[1][0] = ('f', link_or_sha1, stat_value.st_size,
 
1122
                           executable, packed_stat)
1523
1123
        elif minikind == 'd':
1524
1124
            link_or_sha1 = None
1525
1125
            entry[1][0] = ('d', '', 0, False, packed_stat)
1533
1133
                                   osutils.pathjoin(entry[0][0], entry[0][1]))
1534
1134
        elif minikind == 'l':
1535
1135
            link_or_sha1 = self._read_link(abspath, saved_link_or_sha1)
1536
 
            if self._cutoff_time is None:
1537
 
                self._sha_cutoff_time()
1538
 
            if (stat_value.st_mtime < self._cutoff_time
1539
 
                and stat_value.st_ctime < self._cutoff_time):
1540
 
                entry[1][0] = ('l', link_or_sha1, stat_value.st_size,
1541
 
                               False, packed_stat)
1542
 
            else:
1543
 
                entry[1][0] = ('l', '', stat_value.st_size,
1544
 
                               False, DirState.NULLSTAT)
 
1136
            entry[1][0] = ('l', link_or_sha1, stat_value.st_size,
 
1137
                           False, packed_stat)
1545
1138
        self._dirblock_state = DirState.IN_MEMORY_MODIFIED
1546
1139
        return link_or_sha1
1547
1140
 
1562
1155
        """Return the os.lstat value for this path."""
1563
1156
        return os.lstat(abspath)
1564
1157
 
1565
 
    def _sha1_file_and_mutter(self, abspath):
1566
 
        # when -Dhashcache is turned on, this is monkey-patched in to log
1567
 
        # file reads
1568
 
        trace.mutter("dirstate sha1 " + abspath)
1569
 
        return osutils.sha_file_by_name(abspath)
 
1158
    def _sha1_file(self, abspath, entry):
 
1159
        """Calculate the SHA1 of a file by reading the full text"""
 
1160
        f = file(abspath, 'rb', buffering=65000)
 
1161
        try:
 
1162
            return osutils.sha_file(f)
 
1163
        finally:
 
1164
            f.close()
1570
1165
 
1571
1166
    def _is_executable(self, mode, old_executable):
1572
1167
        """Is this file executable?"""
1709
1304
            be attempted.
1710
1305
        :return: A tuple describing where the path is located, or should be
1711
1306
            inserted. The tuple contains four fields: the block index, the row
1712
 
            index, the directory is present (boolean), the entire path is
1713
 
            present (boolean).  There is no guarantee that either
 
1307
            index, anda two booleans are True when the directory is present, and
 
1308
            when the entire path is present.  There is no guarantee that either
1714
1309
            coordinate is currently reachable unless the found field for it is
1715
1310
            True. For instance, a directory not present in the searched tree
1716
1311
            may be returned with a value one greater than the current highest
1728
1323
            return block_index, 0, False, False
1729
1324
        block = self._dirblocks[block_index][1] # access the entries only
1730
1325
        entry_index, present = self._find_entry_index(key, block)
1731
 
        # linear search through entries at this path to find the one
 
1326
        # linear search through present entries at this path to find the one
1732
1327
        # requested.
1733
1328
        while entry_index < len(block) and block[entry_index][0][1] == basename:
1734
 
            if block[entry_index][1][tree_index][0] not in 'ar':
1735
 
                # neither absent or relocated
 
1329
            if block[entry_index][1][tree_index][0] not in \
 
1330
                       ('a', 'r'): # absent, relocated
1736
1331
                return block_index, entry_index, True, True
1737
1332
            entry_index += 1
1738
1333
        return block_index, entry_index, True, False
1739
1334
 
1740
1335
    def _get_entry(self, tree_index, fileid_utf8=None, path_utf8=None):
1741
 
        """Get the dirstate entry for path in tree tree_index.
 
1336
        """Get the dirstate entry for path in tree tree_index
1742
1337
 
1743
1338
        If either file_id or path is supplied, it is used as the key to lookup.
1744
1339
        If both are supplied, the fastest lookup is used, and an error is
1755
1350
        """
1756
1351
        self._read_dirblocks_if_needed()
1757
1352
        if path_utf8 is not None:
1758
 
            if type(path_utf8) is not str:
1759
 
                raise AssertionError('path_utf8 is not a str: %s %s'
1760
 
                    % (type(path_utf8), path_utf8))
 
1353
            assert path_utf8.__class__ == str, 'path_utf8 is not a str: %s %s' % (type(path_utf8), path_utf8)
1761
1354
            # path lookups are faster
1762
1355
            dirname, basename = osutils.split(path_utf8)
1763
1356
            block_index, entry_index, dir_present, file_present = \
1765
1358
            if not file_present:
1766
1359
                return None, None
1767
1360
            entry = self._dirblocks[block_index][1][entry_index]
1768
 
            if not (entry[0][2] and entry[1][tree_index][0] not in ('a', 'r')):
1769
 
                raise AssertionError('unversioned entry?')
 
1361
            assert entry[0][2] and entry[1][tree_index][0] not in ('a', 'r'), 'unversioned entry?!?!'
1770
1362
            if fileid_utf8:
1771
1363
                if entry[0][2] != fileid_utf8:
1772
 
                    self._changes_aborted = True
1773
1364
                    raise errors.BzrError('integrity error ? : mismatching'
1774
1365
                                          ' tree_index, file_id and path')
1775
1366
            return entry
1776
1367
        else:
 
1368
            assert fileid_utf8 is not None
1777
1369
            possible_keys = self._get_id_index().get(fileid_utf8, None)
1778
1370
            if not possible_keys:
1779
1371
                return None, None
1786
1378
                    continue
1787
1379
                # WARNING: DO not change this code to use _get_block_entry_index
1788
1380
                # as that function is not suitable: it does not use the key
1789
 
                # to lookup, and thus the wrong coordinates are returned.
 
1381
                # to lookup, and thus the wront coordinates are returned.
1790
1382
                block = self._dirblocks[block_index][1]
1791
1383
                entry_index, present = self._find_entry_index(key, block)
1792
1384
                if present:
1798
1390
                    if entry[1][tree_index][0] == 'a':
1799
1391
                        # there is no home for this entry in this tree
1800
1392
                        return None, None
1801
 
                    if entry[1][tree_index][0] != 'r':
1802
 
                        raise AssertionError(
1803
 
                            "entry %r has invalid minikind %r for tree %r" \
1804
 
                            % (entry,
1805
 
                               entry[1][tree_index][0],
1806
 
                               tree_index))
 
1393
                    assert entry[1][tree_index][0] == 'r', \
 
1394
                        "entry %r has invalid minikind %r for tree %r" \
 
1395
                        % (entry,
 
1396
                           entry[1][tree_index][0],
 
1397
                           tree_index)
1807
1398
                    real_path = entry[1][tree_index][1]
1808
1399
                    return self._get_entry(tree_index, fileid_utf8=fileid_utf8,
1809
1400
                        path_utf8=real_path)
1816
1407
        The new dirstate will be an empty tree - that is it has no parents,
1817
1408
        and only a root node - which has id ROOT_ID.
1818
1409
 
 
1410
        The object will be write locked when returned to the caller,
 
1411
        unless there was an exception in the writing, in which case it
 
1412
        will be unlocked.
 
1413
 
1819
1414
        :param path: The name of the file for the dirstate.
1820
 
        :return: A write-locked DirState object.
 
1415
        :return: A DirState object.
1821
1416
        """
1822
1417
        # This constructs a new DirState object on a path, sets the _state_file
1823
1418
        # to a new empty file for that path. It then calls _set_data() with our
1841
1436
            raise
1842
1437
        return result
1843
1438
 
1844
 
    @staticmethod
1845
 
    def _inv_entry_to_details(inv_entry):
 
1439
    def _inv_entry_to_details(self, inv_entry):
1846
1440
        """Convert an inventory entry (from a revision tree) to state details.
1847
1441
 
1848
1442
        :param inv_entry: An inventory entry whose sha1 and link targets can be
1853
1447
        kind = inv_entry.kind
1854
1448
        minikind = DirState._kind_to_minikind[kind]
1855
1449
        tree_data = inv_entry.revision
 
1450
        assert len(tree_data) > 0, 'empty revision for the inv_entry.'
1856
1451
        if kind == 'directory':
1857
1452
            fingerprint = ''
1858
1453
            size = 0
1859
1454
            executable = False
1860
1455
        elif kind == 'symlink':
1861
 
            # We don't support non-ascii targets for symlinks yet.
1862
 
            fingerprint = str(inv_entry.symlink_target or '')
 
1456
            fingerprint = inv_entry.symlink_target or ''
1863
1457
            size = 0
1864
1458
            executable = False
1865
1459
        elif kind == 'file':
1874
1468
            raise Exception("can't pack %s" % inv_entry)
1875
1469
        return (minikind, fingerprint, size, executable, tree_data)
1876
1470
 
1877
 
    def _iter_child_entries(self, tree_index, path_utf8):
1878
 
        """Iterate over all the entries that are children of path_utf.
1879
 
 
1880
 
        This only returns entries that are present (not in 'a', 'r') in 
1881
 
        tree_index. tree_index data is not refreshed, so if tree 0 is used,
1882
 
        results may differ from that obtained if paths were statted to
1883
 
        determine what ones were directories.
1884
 
 
1885
 
        Asking for the children of a non-directory will return an empty
1886
 
        iterator.
1887
 
        """
1888
 
        pending_dirs = []
1889
 
        next_pending_dirs = [path_utf8]
1890
 
        absent = 'ar'
1891
 
        while next_pending_dirs:
1892
 
            pending_dirs = next_pending_dirs
1893
 
            next_pending_dirs = []
1894
 
            for path in pending_dirs:
1895
 
                block_index, present = self._find_block_index_from_key(
1896
 
                    (path, '', ''))
1897
 
                if block_index == 0:
1898
 
                    block_index = 1
1899
 
                    if len(self._dirblocks) == 1:
1900
 
                        # asked for the children of the root with no other
1901
 
                        # contents.
1902
 
                        return
1903
 
                if not present:
1904
 
                    # children of a non-directory asked for.
1905
 
                    continue
1906
 
                block = self._dirblocks[block_index]
1907
 
                for entry in block[1]:
1908
 
                    kind = entry[1][tree_index][0]
1909
 
                    if kind not in absent:
1910
 
                        yield entry
1911
 
                    if kind == 'd':
1912
 
                        if entry[0][0]:
1913
 
                            path = entry[0][0] + '/' + entry[0][1]
1914
 
                        else:
1915
 
                            path = entry[0][1]
1916
 
                        next_pending_dirs.append(path)
1917
 
    
1918
1471
    def _iter_entries(self):
1919
1472
        """Iterate over all the entries in the dirstate.
1920
1473
 
1936
1489
        return self._id_index
1937
1490
 
1938
1491
    def _get_output_lines(self, lines):
1939
 
        """Format lines for final output.
 
1492
        """format lines for final output.
1940
1493
 
1941
 
        :param lines: A sequence of lines containing the parents list and the
 
1494
        :param lines: A sequece of lines containing the parents list and the
1942
1495
            path lines.
1943
1496
        """
1944
1497
        output_lines = [DirState.HEADER_FORMAT_3]
1952
1505
        return output_lines
1953
1506
 
1954
1507
    def _make_deleted_row(self, fileid_utf8, parents):
1955
 
        """Return a deleted row for fileid_utf8."""
 
1508
        """Return a deleted for for fileid_utf8."""
1956
1509
        return ('/', 'RECYCLED.BIN', 'file', fileid_utf8, 0, DirState.NULLSTAT,
1957
1510
            ''), parents
1958
1511
 
1978
1531
        """
1979
1532
        self._read_header_if_needed()
1980
1533
        if self._dirblock_state == DirState.NOT_IN_MEMORY:
1981
 
            _read_dirblocks(self)
 
1534
            # move the _state_file pointer to after the header (in case bisect
 
1535
            # has been called in the mean time)
 
1536
            self._state_file.seek(self._end_of_header)
 
1537
            text = self._state_file.read()
 
1538
            # TODO: check the crc checksums. crc_measured = zlib.crc32(text)
 
1539
 
 
1540
            fields = text.split('\0')
 
1541
            # Remove the last blank entry
 
1542
            trailing = fields.pop()
 
1543
            assert trailing == ''
 
1544
            # consider turning fields into a tuple.
 
1545
 
 
1546
            # skip the first field which is the trailing null from the header.
 
1547
            cur = 1
 
1548
            # Each line now has an extra '\n' field which is not used
 
1549
            # so we just skip over it
 
1550
            # entry size:
 
1551
            #  3 fields for the key
 
1552
            #  + number of fields per tree_data (5) * tree count
 
1553
            #  + newline
 
1554
            num_present_parents = self._num_present_parents()
 
1555
            tree_count = 1 + num_present_parents
 
1556
            entry_size = self._fields_per_entry()
 
1557
            expected_field_count = entry_size * self._num_entries
 
1558
            field_count = len(fields)
 
1559
            # this checks our adjustment, and also catches file too short.
 
1560
            assert field_count - cur == expected_field_count, \
 
1561
                'field count incorrect %s != %s, entry_size=%s, '\
 
1562
                'num_entries=%s fields=%r' % (
 
1563
                    field_count - cur, expected_field_count, entry_size,
 
1564
                    self._num_entries, fields)
 
1565
 
 
1566
            if num_present_parents == 1:
 
1567
                # Bind external functions to local names
 
1568
                _int = int
 
1569
                # We access all fields in order, so we can just iterate over
 
1570
                # them. Grab an straight iterator over the fields. (We use an
 
1571
                # iterator because we don't want to do a lot of additions, nor
 
1572
                # do we want to do a lot of slicing)
 
1573
                next = iter(fields).next
 
1574
                # Move the iterator to the current position
 
1575
                for x in xrange(cur):
 
1576
                    next()
 
1577
                # The two blocks here are deliberate: the root block and the
 
1578
                # contents-of-root block.
 
1579
                self._dirblocks = [('', []), ('', [])]
 
1580
                current_block = self._dirblocks[0][1]
 
1581
                current_dirname = ''
 
1582
                append_entry = current_block.append
 
1583
                for count in xrange(self._num_entries):
 
1584
                    dirname = next()
 
1585
                    name = next()
 
1586
                    file_id = next()
 
1587
                    if dirname != current_dirname:
 
1588
                        # new block - different dirname
 
1589
                        current_block = []
 
1590
                        current_dirname = dirname
 
1591
                        self._dirblocks.append((current_dirname, current_block))
 
1592
                        append_entry = current_block.append
 
1593
                    # we know current_dirname == dirname, so re-use it to avoid
 
1594
                    # creating new strings
 
1595
                    entry = ((current_dirname, name, file_id),
 
1596
                             [(# Current Tree
 
1597
                                 next(),                # minikind
 
1598
                                 next(),                # fingerprint
 
1599
                                 _int(next()),          # size
 
1600
                                 next() == 'y',         # executable
 
1601
                                 next(),                # packed_stat or revision_id
 
1602
                             ),
 
1603
                             ( # Parent 1
 
1604
                                 next(),                # minikind
 
1605
                                 next(),                # fingerprint
 
1606
                                 _int(next()),          # size
 
1607
                                 next() == 'y',         # executable
 
1608
                                 next(),                # packed_stat or revision_id
 
1609
                             ),
 
1610
                             ])
 
1611
                    trailing = next()
 
1612
                    assert trailing == '\n'
 
1613
                    # append the entry to the current block
 
1614
                    append_entry(entry)
 
1615
                self._split_root_dirblock_into_contents()
 
1616
            else:
 
1617
                fields_to_entry = self._get_fields_to_entry()
 
1618
                entries = [fields_to_entry(fields[pos:pos+entry_size])
 
1619
                           for pos in xrange(cur, field_count, entry_size)]
 
1620
                self._entries_to_current_state(entries)
 
1621
            # To convert from format 2  => format 3
 
1622
            # self._dirblocks = sorted(self._dirblocks,
 
1623
            #                          key=lambda blk:blk[0].split('/'))
 
1624
            # To convert from format 3 => format 2
 
1625
            # self._dirblocks = sorted(self._dirblocks)
 
1626
            self._dirblock_state = DirState.IN_MEMORY_UNMODIFIED
1982
1627
 
1983
1628
    def _read_header(self):
1984
1629
        """This reads in the metadata header, and the parent ids.
1992
1637
        parent_line = self._state_file.readline()
1993
1638
        info = parent_line.split('\0')
1994
1639
        num_parents = int(info[0])
 
1640
        assert num_parents == len(info)-2, 'incorrect parent info line'
1995
1641
        self._parents = info[1:-1]
 
1642
 
1996
1643
        ghost_line = self._state_file.readline()
1997
1644
        info = ghost_line.split('\0')
1998
1645
        num_ghosts = int(info[1])
 
1646
        assert num_ghosts == len(info)-3, 'incorrect ghost info line'
1999
1647
        self._ghosts = info[2:-1]
2000
1648
        self._header_state = DirState.IN_MEMORY_UNMODIFIED
2001
1649
        self._end_of_header = self._state_file.tell()
2009
1657
            self._read_header()
2010
1658
 
2011
1659
    def _read_prelude(self):
2012
 
        """Read in the prelude header of the dirstate file.
 
1660
        """Read in the prelude header of the dirstate file
2013
1661
 
2014
1662
        This only reads in the stuff that is not connected to the crc
2015
1663
        checksum. The position will be correct to read in the rest of
2018
1666
        and their ids. Followed by a newline.
2019
1667
        """
2020
1668
        header = self._state_file.readline()
2021
 
        if header != DirState.HEADER_FORMAT_3:
2022
 
            raise errors.BzrError(
2023
 
                'invalid header line: %r' % (header,))
 
1669
        assert header == DirState.HEADER_FORMAT_3, \
 
1670
            'invalid header line: %r' % (header,)
2024
1671
        crc_line = self._state_file.readline()
2025
 
        if not crc_line.startswith('crc32: '):
2026
 
            raise errors.BzrError('missing crc32 checksum: %r' % crc_line)
 
1672
        assert crc_line.startswith('crc32: '), 'missing crc32 checksum'
2027
1673
        self.crc_expected = int(crc_line[len('crc32: '):-1])
2028
1674
        num_entries_line = self._state_file.readline()
2029
 
        if not num_entries_line.startswith('num_entries: '):
2030
 
            raise errors.BzrError('missing num_entries line')
 
1675
        assert num_entries_line.startswith('num_entries: '), 'missing num_entries line'
2031
1676
        self._num_entries = int(num_entries_line[len('num_entries: '):-1])
2032
1677
 
2033
 
    def sha1_from_stat(self, path, stat_result, _pack_stat=pack_stat):
2034
 
        """Find a sha1 given a stat lookup."""
2035
 
        return self._get_packed_stat_index().get(_pack_stat(stat_result), None)
2036
 
 
2037
 
    def _get_packed_stat_index(self):
2038
 
        """Get a packed_stat index of self._dirblocks."""
2039
 
        if self._packed_stat_index is None:
2040
 
            index = {}
2041
 
            for key, tree_details in self._iter_entries():
2042
 
                if tree_details[0][0] == 'f':
2043
 
                    index[tree_details[0][4]] = tree_details[0][1]
2044
 
            self._packed_stat_index = index
2045
 
        return self._packed_stat_index
2046
 
 
2047
1678
    def save(self):
2048
1679
        """Save any pending changes created during this session.
2049
1680
 
2050
1681
        We reuse the existing file, because that prevents race conditions with
2051
1682
        file creation, and use oslocks on it to prevent concurrent modification
2052
 
        and reads - because dirstate's incremental data aggregation is not
 
1683
        and reads - because dirstates incremental data aggretation is not
2053
1684
        compatible with reading a modified file, and replacing a file in use by
2054
 
        another process is impossible on Windows.
 
1685
        another process is impossible on windows.
2055
1686
 
2056
1687
        A dirstate in read only mode should be smart enough though to validate
2057
1688
        that the file has not changed, and otherwise discard its cache and
2058
1689
        start over, to allow for fine grained read lock duration, so 'status'
2059
1690
        wont block 'commit' - for example.
2060
1691
        """
2061
 
        if self._changes_aborted:
2062
 
            # Should this be a warning? For now, I'm expecting that places that
2063
 
            # mark it inconsistent will warn, making a warning here redundant.
2064
 
            trace.mutter('Not saving DirState because '
2065
 
                    '_changes_aborted is set.')
2066
 
            return
2067
1692
        if (self._header_state == DirState.IN_MEMORY_MODIFIED or
2068
1693
            self._dirblock_state == DirState.IN_MEMORY_MODIFIED):
2069
1694
 
2111
1736
        self._dirblock_state = DirState.IN_MEMORY_MODIFIED
2112
1737
        self._parents = list(parent_ids)
2113
1738
        self._id_index = None
2114
 
        self._packed_stat_index = None
2115
1739
 
2116
1740
    def set_path_id(self, path, new_id):
2117
1741
        """Change the id of path to new_id in the current working tree.
2121
1745
        :param new_id: The new id to assign to the path. This must be a utf8
2122
1746
            file id (not unicode, and not None).
2123
1747
        """
 
1748
        assert new_id.__class__ == str, \
 
1749
            "path_id %r is not a plain string" % (new_id,)
2124
1750
        self._read_dirblocks_if_needed()
2125
1751
        if len(path):
2126
 
            # TODO: logic not written
 
1752
            # logic not written
2127
1753
            raise NotImplementedError(self.set_path_id)
2128
1754
        # TODO: check new id is unique
2129
1755
        entry = self._get_entry(0, path_utf8=path)
2147
1773
        :param ghosts: A list of the revision_ids that are ghosts at the time
2148
1774
            of setting.
2149
1775
        """ 
 
1776
        self._validate()
2150
1777
        # TODO: generate a list of parent indexes to preserve to save 
2151
1778
        # processing specific parent trees. In the common case one tree will
2152
1779
        # be preserved - the left most parent.
2191
1818
        # one: the current tree
2192
1819
        for entry in self._iter_entries():
2193
1820
            # skip entries not in the current tree
2194
 
            if entry[1][0][0] in 'ar': # absent, relocated
 
1821
            if entry[1][0][0] in ('a', 'r'): # absent, relocated
2195
1822
                continue
2196
1823
            by_path[entry[0]] = [entry[1][0]] + \
2197
1824
                [DirState.NULL_PARENT_DETAILS] * parent_count
2231
1858
                        # this file id is at a different path in one of the
2232
1859
                        # other trees, so put absent pointers there
2233
1860
                        # This is the vertical axis in the matrix, all pointing
2234
 
                        # to the real path.
 
1861
                        # tot he real path.
2235
1862
                        by_path[entry_key][tree_index] = ('r', path_utf8, 0, False, '')
2236
1863
                # by path consistency: Insert into an existing path record (trivial), or 
2237
1864
                # add a new one with relocation pointers for the other tree indexes.
2277
1904
        self._header_state = DirState.IN_MEMORY_MODIFIED
2278
1905
        self._dirblock_state = DirState.IN_MEMORY_MODIFIED
2279
1906
        self._id_index = id_index
 
1907
        self._validate()
2280
1908
 
2281
1909
    def _sort_entries(self, entry_list):
2282
1910
        """Given a list of entries, sort them into the right order.
2285
1913
        try to keep everything in sorted blocks all the time, but sometimes
2286
1914
        it's easier to sort after the fact.
2287
1915
        """
 
1916
        # TODO: Might be faster to do a schwartzian transform?
2288
1917
        def _key(entry):
2289
1918
            # sort by: directory parts, file name, file id
2290
1919
            return entry[0][0].split('/'), entry[0][1], entry[0][2]
2298
1927
 
2299
1928
        :param new_inv: The inventory object to set current state from.
2300
1929
        """
2301
 
        if 'evil' in debug.debug_flags:
2302
 
            trace.mutter_callsite(1,
2303
 
                "set_state_from_inventory called; please mutate the tree instead")
2304
1930
        self._read_dirblocks_if_needed()
2305
1931
        # sketch:
2306
 
        # Two iterators: current data and new data, both in dirblock order. 
2307
 
        # We zip them together, which tells about entries that are new in the
2308
 
        # inventory, or removed in the inventory, or present in both and
2309
 
        # possibly changed.  
2310
 
        #
2311
 
        # You might think we could just synthesize a new dirstate directly
2312
 
        # since we're processing it in the right order.  However, we need to
2313
 
        # also consider there may be any number of parent trees and relocation
2314
 
        # pointers, and we don't want to duplicate that here.
 
1932
        # incremental algorithm:
 
1933
        # two iterators: current data and new data, both in dirblock order. 
2315
1934
        new_iterator = new_inv.iter_entries_by_dir()
2316
1935
        # we will be modifying the dirstate, so we need a stable iterator. In
2317
1936
        # future we might write one, for now we just clone the state into a
2318
 
        # list - which is a shallow copy.
 
1937
        # list - which is a shallow copy, so each 
2319
1938
        old_iterator = iter(list(self._iter_entries()))
2320
1939
        # both must have roots so this is safe:
2321
1940
        current_new = new_iterator.next()
2327
1946
                return None
2328
1947
        while current_new or current_old:
2329
1948
            # skip entries in old that are not really there
2330
 
            if current_old and current_old[1][0][0] in 'ar':
 
1949
            if current_old and current_old[1][0][0] in ('r', 'a'):
2331
1950
                # relocated or absent
2332
1951
                current_old = advance(old_iterator)
2333
1952
                continue
2340
1959
                current_new_minikind = \
2341
1960
                    DirState._kind_to_minikind[current_new[1].kind]
2342
1961
                if current_new_minikind == 't':
2343
 
                    fingerprint = current_new[1].reference_revision or ''
 
1962
                    fingerprint = current_new[1].reference_revision
2344
1963
                else:
2345
 
                    # We normally only insert or remove records, or update
2346
 
                    # them when it has significantly changed.  Then we want to
2347
 
                    # erase its fingerprint.  Unaffected records should
2348
 
                    # normally not be updated at all.
2349
1964
                    fingerprint = ''
2350
1965
            else:
2351
1966
                # for safety disable variables
2352
 
                new_path_utf8 = new_dirname = new_basename = new_id = \
2353
 
                    new_entry_key = None
 
1967
                new_path_utf8 = new_dirname = new_basename = new_id = new_entry_key = None
2354
1968
            # 5 cases, we dont have a value that is strictly greater than everything, so
2355
1969
            # we make both end conditions explicit
2356
1970
            if not current_old:
2365
1979
                current_old = advance(old_iterator)
2366
1980
            elif new_entry_key == current_old[0]:
2367
1981
                # same -  common case
2368
 
                # We're looking at the same path and id in both the dirstate
2369
 
                # and inventory, so just need to update the fields in the
2370
 
                # dirstate from the one in the inventory.
2371
1982
                # TODO: update the record if anything significant has changed.
2372
1983
                # the minimal required trigger is if the execute bit or cached
2373
1984
                # kind has changed.
2379
1990
                # both sides are dealt with, move on
2380
1991
                current_old = advance(old_iterator)
2381
1992
                current_new = advance(new_iterator)
2382
 
            elif (cmp_by_dirs(new_dirname, current_old[0][0]) < 0
2383
 
                  or (new_dirname == current_old[0][0]
2384
 
                      and new_entry_key[1:] < current_old[0][1:])):
 
1993
            elif new_entry_key < current_old[0]:
2385
1994
                # new comes before:
2386
1995
                # add a entry for this and advance new
2387
1996
                self.update_minimal(new_entry_key, current_new_minikind,
2389
1998
                    path_utf8=new_path_utf8, fingerprint=fingerprint)
2390
1999
                current_new = advance(new_iterator)
2391
2000
            else:
2392
 
                # we've advanced past the place where the old key would be,
2393
 
                # without seeing it in the new list.  so it must be gone.
 
2001
                # old comes before:
2394
2002
                self._make_absent(current_old)
2395
2003
                current_old = advance(old_iterator)
2396
2004
        self._dirblock_state = DirState.IN_MEMORY_MODIFIED
2397
2005
        self._id_index = None
2398
 
        self._packed_stat_index = None
2399
2006
 
2400
2007
    def _make_absent(self, current_old):
2401
2008
        """Mark current_old - an entry - as absent for tree 0.
2402
2009
 
2403
 
        :return: True if this was the last details entry for the entry key:
 
2010
        :return: True if this was the last details entry for they entry key:
2404
2011
            that is, if the underlying block has had the entry removed, thus
2405
2012
            shrinking in length.
2406
2013
        """
2407
2014
        # build up paths that this id will be left at after the change is made,
2408
2015
        # so we can update their cross references in tree 0
2409
2016
        all_remaining_keys = set()
2410
 
        # Dont check the working tree, because it's going.
 
2017
        # Dont check the working tree, because its going.
2411
2018
        for details in current_old[1][1:]:
2412
 
            if details[0] not in 'ar': # absent, relocated
 
2019
            if details[0] not in ('a', 'r'): # absent, relocated
2413
2020
                all_remaining_keys.add(current_old[0])
2414
2021
            elif details[0] == 'r': # relocated
2415
2022
                # record the key for the real path.
2422
2029
            # Remove it, its meaningless.
2423
2030
            block = self._find_block(current_old[0])
2424
2031
            entry_index, present = self._find_entry_index(current_old[0], block[1])
2425
 
            if not present:
2426
 
                raise AssertionError('could not find entry for %s' % (current_old,))
 
2032
            assert present, 'could not find entry for %s' % (current_old,)
2427
2033
            block[1].pop(entry_index)
2428
2034
            # if we have an id_index in use, remove this key from it for this id.
2429
2035
            if self._id_index is not None:
2430
2036
                self._id_index[current_old[0][2]].remove(current_old[0])
2431
2037
        # update all remaining keys for this id to record it as absent. The
2432
 
        # existing details may either be the record we are marking as deleted
 
2038
        # existing details may either be the record we are making as deleted
2433
2039
        # (if there were other trees with the id present at this path), or may
2434
2040
        # be relocations.
2435
2041
        for update_key in all_remaining_keys:
2436
2042
            update_block_index, present = \
2437
2043
                self._find_block_index_from_key(update_key)
2438
 
            if not present:
2439
 
                raise AssertionError('could not find block for %s' % (update_key,))
 
2044
            assert present, 'could not find block for %s' % (update_key,)
2440
2045
            update_entry_index, present = \
2441
2046
                self._find_entry_index(update_key, self._dirblocks[update_block_index][1])
2442
 
            if not present:
2443
 
                raise AssertionError('could not find entry for %s' % (update_key,))
 
2047
            assert present, 'could not find entry for %s' % (update_key,)
2444
2048
            update_tree_details = self._dirblocks[update_block_index][1][update_entry_index][1]
2445
2049
            # it must not be absent at the moment
2446
 
            if update_tree_details[0][0] == 'a': # absent
2447
 
                raise AssertionError('bad row %r' % (update_tree_details,))
 
2050
            assert update_tree_details[0][0] != 'a' # absent
2448
2051
            update_tree_details[0] = DirState.NULL_PARENT_DETAILS
2449
2052
        self._dirblock_state = DirState.IN_MEMORY_MODIFIED
2450
2053
        return last_reference
2461
2064
        :param minikind: The type for the entry ('f' == 'file', 'd' ==
2462
2065
                'directory'), etc.
2463
2066
        :param executable: Should the executable bit be set?
2464
 
        :param fingerprint: Simple fingerprint for new entry: sha1 for files, 
2465
 
            referenced revision id for subtrees, etc.
2466
 
        :param packed_stat: Packed stat value for new entry.
 
2067
        :param fingerprint: Simple fingerprint for new entry.
 
2068
        :param packed_stat: packed stat value for new entry.
2467
2069
        :param size: Size information for new entry
2468
2070
        :param path_utf8: key[0] + '/' + key[1], just passed in to avoid doing
2469
2071
                extra computation.
2470
 
 
2471
 
        If packed_stat and fingerprint are not given, they're invalidated in
2472
 
        the entry.
2473
2072
        """
2474
2073
        block = self._find_block(key)[1]
2475
2074
        if packed_stat is None:
2476
2075
            packed_stat = DirState.NULLSTAT
2477
 
        # XXX: Some callers pass '' as the packed_stat, and it seems to be
2478
 
        # sometimes present in the dirstate - this seems oddly inconsistent.
2479
 
        # mbp 20071008
2480
2076
        entry_index, present = self._find_entry_index(key, block)
2481
2077
        new_details = (minikind, fingerprint, size, executable, packed_stat)
2482
2078
        id_index = self._get_id_index()
2498
2094
                    # the test for existing kinds is different: this can be
2499
2095
                    # factored out to a helper though.
2500
2096
                    other_block_index, present = self._find_block_index_from_key(other_key)
2501
 
                    if not present:
2502
 
                        raise AssertionError('could not find block for %s' % (other_key,))
 
2097
                    assert present, 'could not find block for %s' % (other_key,)
2503
2098
                    other_entry_index, present = self._find_entry_index(other_key,
2504
2099
                                            self._dirblocks[other_block_index][1])
2505
 
                    if not present:
2506
 
                        raise AssertionError('could not find entry for %s' % (other_key,))
2507
 
                    if path_utf8 is None:
2508
 
                        raise AssertionError('no path')
 
2100
                    assert present, 'could not find entry for %s' % (other_key,)
 
2101
                    assert path_utf8 is not None
2509
2102
                    self._dirblocks[other_block_index][1][other_entry_index][1][0] = \
2510
2103
                        ('r', path_utf8, 0, False, '')
2511
2104
 
2517
2110
                    # records.
2518
2111
                    update_block_index, present = \
2519
2112
                        self._find_block_index_from_key(other_key)
2520
 
                    if not present:
2521
 
                        raise AssertionError('could not find block for %s' % (other_key,))
 
2113
                    assert present, 'could not find block for %s' % (other_key,)
2522
2114
                    update_entry_index, present = \
2523
2115
                        self._find_entry_index(other_key, self._dirblocks[update_block_index][1])
2524
 
                    if not present:
2525
 
                        raise AssertionError('could not find entry for %s' % (other_key,))
 
2116
                    assert present, 'could not find entry for %s' % (other_key,)
2526
2117
                    update_details = self._dirblocks[update_block_index][1][update_entry_index][1][lookup_index]
2527
 
                    if update_details[0] in 'ar': # relocated, absent
 
2118
                    if update_details[0] in ('r', 'a'): # relocated, absent
2528
2119
                        # its a pointer or absent in lookup_index's tree, use
2529
2120
                        # it as is.
2530
2121
                        new_entry[1].append(update_details)
2546
2137
            # we may have passed entries in the state with this file id already
2547
2138
            # that were absent - where parent entries are - and they need to be
2548
2139
            # converted to relocated.
2549
 
            if path_utf8 is None:
2550
 
                raise AssertionError('no path')
 
2140
            assert path_utf8 is not None
2551
2141
            for entry_key in id_index.setdefault(key[2], set()):
2552
2142
                # TODO:PROFILING: It might be faster to just update
2553
2143
                # rather than checking if we need to, and then overwrite
2558
2148
                    # This is the vertical axis in the matrix, all pointing
2559
2149
                    # to the real path.
2560
2150
                    block_index, present = self._find_block_index_from_key(entry_key)
2561
 
                    if not present:
2562
 
                        raise AssertionError('not present: %r', entry_key)
 
2151
                    assert present
2563
2152
                    entry_index, present = self._find_entry_index(entry_key, self._dirblocks[block_index][1])
2564
 
                    if not present:
2565
 
                        raise AssertionError('not present: %r', entry_key)
 
2153
                    assert present
2566
2154
                    self._dirblocks[block_index][1][entry_index][1][0] = \
2567
2155
                        ('r', path_utf8, 0, False, '')
2568
2156
        # add a containing dirblock if needed.
2579
2167
 
2580
2168
        This can be useful in debugging; it shouldn't be necessary in 
2581
2169
        normal code.
2582
 
 
2583
 
        This must be called with a lock held.
2584
2170
        """
2585
 
        # NOTE: This must always raise AssertionError not just assert,
2586
 
        # otherwise it may not behave properly under python -O
2587
 
        #
2588
 
        # TODO: All entries must have some content that's not 'a' or 'r',
2589
 
        # otherwise it could just be removed.
2590
 
        #
2591
 
        # TODO: All relocations must point directly to a real entry.
2592
 
        #
2593
 
        # TODO: No repeated keys.
2594
 
        #
2595
 
        # -- mbp 20070325
2596
2171
        from pprint import pformat
2597
 
        self._read_dirblocks_if_needed()
2598
2172
        if len(self._dirblocks) > 0:
2599
 
            if not self._dirblocks[0][0] == '':
2600
 
                raise AssertionError(
 
2173
            assert self._dirblocks[0][0] == '', \
2601
2174
                    "dirblocks don't start with root block:\n" + \
2602
 
                    pformat(self._dirblocks))
 
2175
                    pformat(dirblocks)
2603
2176
        if len(self._dirblocks) > 1:
2604
 
            if not self._dirblocks[1][0] == '':
2605
 
                raise AssertionError(
 
2177
            assert self._dirblocks[1][0] == '', \
2606
2178
                    "dirblocks missing root directory:\n" + \
2607
 
                    pformat(self._dirblocks))
 
2179
                    pformat(dirblocks)
2608
2180
        # the dirblocks are sorted by their path components, name, and dir id
2609
2181
        dir_names = [d[0].split('/')
2610
2182
                for d in self._dirblocks[1:]]
2617
2189
        for dirblock in self._dirblocks:
2618
2190
            # within each dirblock, the entries are sorted by filename and
2619
2191
            # then by id.
2620
 
            for entry in dirblock[1]:
2621
 
                if dirblock[0] != entry[0][0]:
2622
 
                    raise AssertionError(
2623
 
                        "entry key for %r"
2624
 
                        "doesn't match directory name in\n%r" %
2625
 
                        (entry, pformat(dirblock)))
2626
 
            if dirblock[1] != sorted(dirblock[1]):
2627
 
                raise AssertionError(
2628
 
                    "dirblock for %r is not sorted:\n%s" % \
2629
 
                    (dirblock[0], pformat(dirblock)))
2630
 
 
2631
 
        def check_valid_parent():
2632
 
            """Check that the current entry has a valid parent.
2633
 
 
2634
 
            This makes sure that the parent has a record,
2635
 
            and that the parent isn't marked as "absent" in the
2636
 
            current tree. (It is invalid to have a non-absent file in an absent
2637
 
            directory.)
2638
 
            """
2639
 
            if entry[0][0:2] == ('', ''):
2640
 
                # There should be no parent for the root row
2641
 
                return
2642
 
            parent_entry = self._get_entry(tree_index, path_utf8=entry[0][0])
2643
 
            if parent_entry == (None, None):
2644
 
                raise AssertionError(
2645
 
                    "no parent entry for: %s in tree %s"
2646
 
                    % (this_path, tree_index))
2647
 
            if parent_entry[1][tree_index][0] != 'd':
2648
 
                raise AssertionError(
2649
 
                    "Parent entry for %s is not marked as a valid"
2650
 
                    " directory. %s" % (this_path, parent_entry,))
2651
 
 
2652
 
        # For each file id, for each tree: either
2653
 
        # the file id is not present at all; all rows with that id in the
2654
 
        # key have it marked as 'absent'
2655
 
        # OR the file id is present under exactly one name; any other entries 
2656
 
        # that mention that id point to the correct name.
2657
 
        #
2658
 
        # We check this with a dict per tree pointing either to the present
2659
 
        # name, or None if absent.
2660
 
        tree_count = self._num_present_parents() + 1
2661
 
        id_path_maps = [dict() for i in range(tree_count)]
2662
 
        # Make sure that all renamed entries point to the correct location.
2663
 
        for entry in self._iter_entries():
2664
 
            file_id = entry[0][2]
2665
 
            this_path = osutils.pathjoin(entry[0][0], entry[0][1])
2666
 
            if len(entry[1]) != tree_count:
2667
 
                raise AssertionError(
2668
 
                "wrong number of entry details for row\n%s" \
2669
 
                ",\nexpected %d" % \
2670
 
                (pformat(entry), tree_count))
2671
 
            absent_positions = 0
2672
 
            for tree_index, tree_state in enumerate(entry[1]):
2673
 
                this_tree_map = id_path_maps[tree_index]
2674
 
                minikind = tree_state[0]
2675
 
                if minikind in 'ar':
2676
 
                    absent_positions += 1
2677
 
                # have we seen this id before in this column?
2678
 
                if file_id in this_tree_map:
2679
 
                    previous_path, previous_loc = this_tree_map[file_id]
2680
 
                    # any later mention of this file must be consistent with
2681
 
                    # what was said before
2682
 
                    if minikind == 'a':
2683
 
                        if previous_path is not None:
2684
 
                            raise AssertionError(
2685
 
                            "file %s is absent in row %r but also present " \
2686
 
                            "at %r"% \
2687
 
                            (file_id, entry, previous_path))
2688
 
                    elif minikind == 'r':
2689
 
                        target_location = tree_state[1]
2690
 
                        if previous_path != target_location:
2691
 
                            raise AssertionError(
2692
 
                            "file %s relocation in row %r but also at %r" \
2693
 
                            % (file_id, entry, previous_path))
2694
 
                    else:
2695
 
                        # a file, directory, etc - may have been previously
2696
 
                        # pointed to by a relocation, which must point here
2697
 
                        if previous_path != this_path:
2698
 
                            raise AssertionError(
2699
 
                                "entry %r inconsistent with previous path %r "
2700
 
                                "seen at %r" %
2701
 
                                (entry, previous_path, previous_loc))
2702
 
                        check_valid_parent()
2703
 
                else:
2704
 
                    if minikind == 'a':
2705
 
                        # absent; should not occur anywhere else
2706
 
                        this_tree_map[file_id] = None, this_path
2707
 
                    elif minikind == 'r':
2708
 
                        # relocation, must occur at expected location 
2709
 
                        this_tree_map[file_id] = tree_state[1], this_path
2710
 
                    else:
2711
 
                        this_tree_map[file_id] = this_path, this_path
2712
 
                        check_valid_parent()
2713
 
            if absent_positions == tree_count:
2714
 
                raise AssertionError(
2715
 
                    "entry %r has no data for any tree." % (entry,))
 
2192
            assert dirblock[1] == sorted(dirblock[1]), \
 
2193
                "dirblock for %r is not sorted:\n%s" % \
 
2194
                (dirblock[0], pformat(dirblock))
2716
2195
 
2717
2196
    def _wipe_state(self):
2718
2197
        """Forget all state information about the dirstate."""
2719
2198
        self._header_state = DirState.NOT_IN_MEMORY
2720
2199
        self._dirblock_state = DirState.NOT_IN_MEMORY
2721
 
        self._changes_aborted = False
2722
2200
        self._parents = []
2723
2201
        self._ghosts = []
2724
2202
        self._dirblocks = []
2725
2203
        self._id_index = None
2726
 
        self._packed_stat_index = None
2727
2204
        self._end_of_header = None
2728
2205
        self._cutoff_time = None
2729
2206
        self._split_path_cache = {}
2730
2207
 
2731
2208
    def lock_read(self):
2732
 
        """Acquire a read lock on the dirstate."""
 
2209
        """Acquire a read lock on the dirstate"""
2733
2210
        if self._lock_token is not None:
2734
2211
            raise errors.LockContention(self._lock_token)
2735
2212
        # TODO: jam 20070301 Rather than wiping completely, if the blocks are
2742
2219
        self._wipe_state()
2743
2220
 
2744
2221
    def lock_write(self):
2745
 
        """Acquire a write lock on the dirstate."""
 
2222
        """Acquire a write lock on the dirstate"""
2746
2223
        if self._lock_token is not None:
2747
2224
            raise errors.LockContention(self._lock_token)
2748
2225
        # TODO: jam 20070301 Rather than wiping completely, if the blocks are
2755
2232
        self._wipe_state()
2756
2233
 
2757
2234
    def unlock(self):
2758
 
        """Drop any locks held on the dirstate."""
 
2235
        """Drop any locks held on the dirstate"""
2759
2236
        if self._lock_token is None:
2760
2237
            raise errors.LockNotHeld(self)
2761
2238
        # TODO: jam 20070301 Rather than wiping completely, if the blocks are
2769
2246
        self._split_path_cache = {}
2770
2247
 
2771
2248
    def _requires_lock(self):
2772
 
        """Check that a lock is currently held by someone on the dirstate."""
 
2249
        """Checks that a lock is currently held by someone on the dirstate"""
2773
2250
        if not self._lock_token:
2774
2251
            raise errors.ObjectNotLocked(self)
2775
2252
 
2776
2253
 
2777
 
# Try to load the compiled form if possible
2778
 
try:
2779
 
    from bzrlib._dirstate_helpers_c import (
2780
 
        _read_dirblocks_c as _read_dirblocks,
2781
 
        bisect_dirblock_c as bisect_dirblock,
2782
 
        _bisect_path_left_c as _bisect_path_left,
2783
 
        _bisect_path_right_c as _bisect_path_right,
2784
 
        cmp_by_dirs_c as cmp_by_dirs,
2785
 
        )
2786
 
except ImportError:
2787
 
    from bzrlib._dirstate_helpers_py import (
2788
 
        _read_dirblocks_py as _read_dirblocks,
2789
 
        bisect_dirblock_py as bisect_dirblock,
2790
 
        _bisect_path_left_py as _bisect_path_left,
2791
 
        _bisect_path_right_py as _bisect_path_right,
2792
 
        cmp_by_dirs_py as cmp_by_dirs,
2793
 
        )
 
2254
def bisect_dirblock(dirblocks, dirname, lo=0, hi=None, cache={}):
 
2255
    """Return the index where to insert dirname into the dirblocks.
 
2256
 
 
2257
    The return value idx is such that all directories blocks in dirblock[:idx]
 
2258
    have names < dirname, and all blocks in dirblock[idx:] have names >=
 
2259
    dirname.
 
2260
 
 
2261
    Optional args lo (default 0) and hi (default len(dirblocks)) bound the
 
2262
    slice of a to be searched.
 
2263
    """
 
2264
    if hi is None:
 
2265
        hi = len(dirblocks)
 
2266
    try:
 
2267
        dirname_split = cache[dirname]
 
2268
    except KeyError:
 
2269
        dirname_split = dirname.split('/')
 
2270
        cache[dirname] = dirname_split
 
2271
    while lo < hi:
 
2272
        mid = (lo+hi)//2
 
2273
        # Grab the dirname for the current dirblock
 
2274
        cur = dirblocks[mid][0]
 
2275
        try:
 
2276
            cur_split = cache[cur]
 
2277
        except KeyError:
 
2278
            cur_split = cur.split('/')
 
2279
            cache[cur] = cur_split
 
2280
        if cur_split < dirname_split: lo = mid+1
 
2281
        else: hi = mid
 
2282
    return lo
 
2283
 
 
2284
 
 
2285
 
 
2286
def pack_stat(st, _encode=base64.encodestring, _pack=struct.pack):
 
2287
    """Convert stat values into a packed representation."""
 
2288
    # jam 20060614 it isn't really worth removing more entries if we
 
2289
    # are going to leave it in packed form.
 
2290
    # With only st_mtime and st_mode filesize is 5.5M and read time is 275ms
 
2291
    # With all entries filesize is 5.9M and read time is mabye 280ms
 
2292
    # well within the noise margin
 
2293
 
 
2294
    # base64.encode always adds a final newline, so strip it off
 
2295
    return _encode(_pack('>llllll'
 
2296
        , st.st_size, int(st.st_mtime), int(st.st_ctime)
 
2297
        , st.st_dev, st.st_ino, st.st_mode))[:-1]