791
689
text = state._state_file.read()
792
690
# TODO: check the crc checksums. crc_measured = zlib.crc32(text)
794
reader = Reader(text, state)
692
reader = Reader(text)
796
reader._parse_dirblocks()
694
reader._parse_dirblocks(state)
797
695
state._dirblock_state = DirState.IN_MEMORY_UNMODIFIED
800
cdef int minikind_from_mode(int mode): # cannot_raise
801
# in order of frequency:
811
_encode = binascii.b2a_base64
814
from struct import pack
815
cdef _pack_stat(stat_value):
816
"""return a string representing the stat value's key fields.
818
:param stat_value: A stat oject with st_size, st_mtime, st_ctime, st_dev,
819
st_ino and st_mode fields.
821
cdef char result[6*4] # 6 long ints
823
aliased = <int *>result
824
aliased[0] = htonl(stat_value.st_size)
825
aliased[1] = htonl(int(stat_value.st_mtime))
826
aliased[2] = htonl(int(stat_value.st_ctime))
827
aliased[3] = htonl(stat_value.st_dev)
828
aliased[4] = htonl(stat_value.st_ino & 0xFFFFFFFF)
829
aliased[5] = htonl(stat_value.st_mode)
830
packed = PyString_FromStringAndSize(result, 6*4)
831
return _encode(packed)[:-1]
834
def update_entry(self, entry, abspath, stat_value):
835
"""Update the entry based on what is actually on disk.
837
This function only calculates the sha if it needs to - if the entry is
838
uncachable, or clearly different to the first parent's entry, no sha
839
is calculated, and None is returned.
841
:param entry: This is the dirblock entry for the file in question.
842
:param abspath: The path on disk for this file.
843
:param stat_value: (optional) if we already have done a stat on the
845
:return: None, or The sha1 hexdigest of the file (40 bytes) or link
848
return _update_entry(self, entry, abspath, stat_value)
851
cdef _update_entry(self, entry, abspath, stat_value):
852
"""Update the entry based on what is actually on disk.
854
This function only calculates the sha if it needs to - if the entry is
855
uncachable, or clearly different to the first parent's entry, no sha
856
is calculated, and None is returned.
858
:param self: The dirstate object this is operating on.
859
:param entry: This is the dirblock entry for the file in question.
860
:param abspath: The path on disk for this file.
861
:param stat_value: The stat value done on the path.
862
:return: None, or The sha1 hexdigest of the file (40 bytes) or link
865
# TODO - require pyrex 0.9.8, then use a pyd file to define access to the
866
# _st mode of the compiled stat objects.
867
cdef int minikind, saved_minikind
869
cdef int worth_saving
870
minikind = minikind_from_mode(stat_value.st_mode)
873
packed_stat = _pack_stat(stat_value)
874
details = PyList_GetItem_void_void(PyTuple_GetItem_void_void(<void *>entry, 1), 0)
875
saved_minikind = PyString_AsString_obj(<PyObject *>PyTuple_GetItem_void_void(details, 0))[0]
876
if minikind == c'd' and saved_minikind == c't':
878
saved_link_or_sha1 = PyTuple_GetItem_void_object(details, 1)
879
saved_file_size = PyTuple_GetItem_void_object(details, 2)
880
saved_executable = PyTuple_GetItem_void_object(details, 3)
881
saved_packed_stat = PyTuple_GetItem_void_object(details, 4)
882
# Deal with pyrex decrefing the objects
883
Py_INCREF(saved_link_or_sha1)
884
Py_INCREF(saved_file_size)
885
Py_INCREF(saved_executable)
886
Py_INCREF(saved_packed_stat)
887
#(saved_minikind, saved_link_or_sha1, saved_file_size,
888
# saved_executable, saved_packed_stat) = entry[1][0]
890
if (minikind == saved_minikind
891
and packed_stat == saved_packed_stat):
892
# The stat hasn't changed since we saved, so we can re-use the
897
# size should also be in packed_stat
898
if saved_file_size == stat_value.st_size:
899
return saved_link_or_sha1
901
# If we have gotten this far, that means that we need to actually
902
# process this entry.
906
executable = self._is_executable(stat_value.st_mode,
908
if self._cutoff_time is None:
909
self._sha_cutoff_time()
910
if (stat_value.st_mtime < self._cutoff_time
911
and stat_value.st_ctime < self._cutoff_time
912
and len(entry[1]) > 1
913
and entry[1][1][0] != 'a'):
914
# Could check for size changes for further optimised
915
# avoidance of sha1's. However the most prominent case of
916
# over-shaing is during initial add, which this catches.
917
link_or_sha1 = self._sha1_file(abspath)
918
entry[1][0] = ('f', link_or_sha1, stat_value.st_size,
919
executable, packed_stat)
921
# This file is not worth caching the sha1. Either it is too new, or
922
# it is newly added. Regardless, the only things we are changing
923
# are derived from the stat, and so are not worth caching. So we do
924
# *not* set the IN_MEMORY_MODIFIED flag. (But we'll save the
925
# updated values if there is *other* data worth saving.)
926
entry[1][0] = ('f', '', stat_value.st_size, executable,
929
elif minikind == c'd':
930
entry[1][0] = ('d', '', 0, False, packed_stat)
931
if saved_minikind != c'd':
932
# This changed from something into a directory. Make sure we
933
# have a directory block for it. This doesn't happen very
934
# often, so this doesn't have to be super fast.
935
block_index, entry_index, dir_present, file_present = \
936
self._get_block_entry_index(entry[0][0], entry[0][1], 0)
937
self._ensure_block(block_index, entry_index,
938
pathjoin(entry[0][0], entry[0][1]))
940
# Any changes are derived trivially from the stat object, not worth
941
# re-writing a dirstate for just this
943
elif minikind == c'l':
944
if saved_minikind == c'l':
945
# If the object hasn't changed kind, it isn't worth saving the
946
# dirstate just for a symlink. The default is 'fast symlinks' which
947
# save the target in the inode entry, rather than separately. So to
948
# stat, we've already read everything off disk.
950
link_or_sha1 = self._read_link(abspath, saved_link_or_sha1)
951
if self._cutoff_time is None:
952
self._sha_cutoff_time()
953
if (stat_value.st_mtime < self._cutoff_time
954
and stat_value.st_ctime < self._cutoff_time):
955
entry[1][0] = ('l', link_or_sha1, stat_value.st_size,
958
entry[1][0] = ('l', '', stat_value.st_size,
959
False, DirState.NULLSTAT)
961
# Note, even though _mark_modified will only set
962
# IN_MEMORY_HASH_MODIFIED, it still isn't worth
963
self._mark_modified([entry])
967
# TODO: Do we want to worry about exceptions here?
968
cdef char _minikind_from_string(object string) except? -1:
969
"""Convert a python string to a char."""
970
return PyString_AsString(string)[0]
973
cdef object _kind_absent
974
cdef object _kind_file
975
cdef object _kind_directory
976
cdef object _kind_symlink
977
cdef object _kind_relocated
978
cdef object _kind_tree_reference
979
_kind_absent = "absent"
981
_kind_directory = "directory"
982
_kind_symlink = "symlink"
983
_kind_relocated = "relocated"
984
_kind_tree_reference = "tree-reference"
987
cdef object _minikind_to_kind(char minikind):
988
"""Create a string kind for minikind."""
989
cdef char _minikind[1]
992
elif minikind == c'd':
993
return _kind_directory
994
elif minikind == c'a':
996
elif minikind == c'r':
997
return _kind_relocated
998
elif minikind == c'l':
1000
elif minikind == c't':
1001
return _kind_tree_reference
1002
_minikind[0] = minikind
1003
raise KeyError(PyString_FromStringAndSize(_minikind, 1))
1006
cdef int _versioned_minikind(char minikind): # cannot_raise
1007
"""Return non-zero if minikind is in fltd"""
1008
return (minikind == c'f' or
1014
cdef class ProcessEntryC:
1016
cdef int doing_consistency_expansion
1017
cdef object old_dirname_to_file_id # dict
1018
cdef object new_dirname_to_file_id # dict
1019
cdef object last_source_parent
1020
cdef object last_target_parent
1021
cdef int include_unchanged
1023
cdef object use_filesystem_for_exec
1024
cdef object utf8_decode
1025
cdef readonly object searched_specific_files
1026
cdef readonly object searched_exact_paths
1027
cdef object search_specific_files
1028
# The parents up to the root of the paths we are searching.
1029
# After all normal paths are returned, these specific items are returned.
1030
cdef object search_specific_file_parents
1032
# Current iteration variables:
1033
cdef object current_root
1034
cdef object current_root_unicode
1035
cdef object root_entries
1036
cdef int root_entries_pos, root_entries_len
1037
cdef object root_abspath
1038
cdef int source_index, target_index
1039
cdef int want_unversioned
1041
cdef object dir_iterator
1042
cdef int block_index
1043
cdef object current_block
1044
cdef int current_block_pos
1045
cdef object current_block_list
1046
cdef object current_dir_info
1047
cdef object current_dir_list
1048
cdef object _pending_consistent_entries # list
1050
cdef object root_dir_info
1051
cdef object bisect_left
1052
cdef object pathjoin
1054
# A set of the ids we've output when doing partial output.
1055
cdef object seen_ids
1056
cdef object sha_file
1058
def __init__(self, include_unchanged, use_filesystem_for_exec,
1059
search_specific_files, state, source_index, target_index,
1060
want_unversioned, tree):
1061
self.doing_consistency_expansion = 0
1062
self.old_dirname_to_file_id = {}
1063
self.new_dirname_to_file_id = {}
1064
# Are we doing a partial iter_changes?
1065
self.partial = set(['']).__ne__(search_specific_files)
1066
# Using a list so that we can access the values and change them in
1067
# nested scope. Each one is [path, file_id, entry]
1068
self.last_source_parent = [None, None]
1069
self.last_target_parent = [None, None]
1070
if include_unchanged is None:
1071
self.include_unchanged = False
1073
self.include_unchanged = int(include_unchanged)
1074
self.use_filesystem_for_exec = use_filesystem_for_exec
1075
self.utf8_decode = cache_utf8._utf8_decode
1076
# for all search_indexs in each path at or under each element of
1077
# search_specific_files, if the detail is relocated: add the id, and
1078
# add the relocated path as one to search if its not searched already.
1079
# If the detail is not relocated, add the id.
1080
self.searched_specific_files = set()
1081
# When we search exact paths without expanding downwards, we record
1083
self.searched_exact_paths = set()
1084
self.search_specific_files = search_specific_files
1085
# The parents up to the root of the paths we are searching.
1086
# After all normal paths are returned, these specific items are returned.
1087
self.search_specific_file_parents = set()
1088
# The ids we've sent out in the delta.
1089
self.seen_ids = set()
1091
self.current_root = None
1092
self.current_root_unicode = None
1093
self.root_entries = None
1094
self.root_entries_pos = 0
1095
self.root_entries_len = 0
1096
self.root_abspath = None
1097
if source_index is None:
1098
self.source_index = -1
1100
self.source_index = source_index
1101
self.target_index = target_index
1102
self.want_unversioned = want_unversioned
1104
self.dir_iterator = None
1105
self.block_index = -1
1106
self.current_block = None
1107
self.current_block_list = None
1108
self.current_block_pos = -1
1109
self.current_dir_info = None
1110
self.current_dir_list = None
1111
self._pending_consistent_entries = []
1113
self.root_dir_info = None
1114
self.bisect_left = bisect.bisect_left
1115
self.pathjoin = osutils.pathjoin
1116
self.fstat = os.fstat
1117
self.sha_file = osutils.sha_file
1118
if target_index != 0:
1119
# A lot of code in here depends on target_index == 0
1120
raise errors.BzrError('unsupported target index')
1122
cdef _process_entry(self, entry, path_info):
1123
"""Compare an entry and real disk to generate delta information.
1125
:param path_info: top_relpath, basename, kind, lstat, abspath for
1126
the path of entry. If None, then the path is considered absent in
1127
the target (Perhaps we should pass in a concrete entry for this ?)
1128
Basename is returned as a utf8 string because we expect this
1129
tuple will be ignored, and don't want to take the time to
1131
:return: (iter_changes_result, changed). If the entry has not been
1132
handled then changed is None. Otherwise it is False if no content
1133
or metadata changes have occured, and True if any content or
1134
metadata change has occurred. If self.include_unchanged is True then
1135
if changed is not None, iter_changes_result will always be a result
1136
tuple. Otherwise, iter_changes_result is None unless changed is
1139
cdef char target_minikind
1140
cdef char source_minikind
1142
cdef int content_change
1143
cdef object details_list
1145
details_list = entry[1]
1146
if -1 == self.source_index:
1147
source_details = DirState.NULL_PARENT_DETAILS
1149
source_details = details_list[self.source_index]
1150
target_details = details_list[self.target_index]
1151
target_minikind = _minikind_from_string(target_details[0])
1152
if path_info is not None and _versioned_minikind(target_minikind):
1153
if self.target_index != 0:
1154
raise AssertionError("Unsupported target index %d" %
1156
link_or_sha1 = _update_entry(self.state, entry, path_info[4], path_info[3])
1157
# The entry may have been modified by update_entry
1158
target_details = details_list[self.target_index]
1159
target_minikind = _minikind_from_string(target_details[0])
1162
# the rest of this function is 0.3 seconds on 50K paths, or
1163
# 0.000006 seconds per call.
1164
source_minikind = _minikind_from_string(source_details[0])
1165
if ((_versioned_minikind(source_minikind) or source_minikind == c'r')
1166
and _versioned_minikind(target_minikind)):
1167
# claimed content in both: diff
1168
# r | fdlt | | add source to search, add id path move and perform
1169
# | | | diff check on source-target
1170
# r | fdlt | a | dangling file that was present in the basis.
1172
if source_minikind != c'r':
1173
old_dirname = entry[0][0]
1174
old_basename = entry[0][1]
1175
old_path = path = None
1177
# add the source to the search path to find any children it
1178
# has. TODO ? : only add if it is a container ?
1179
if (not self.doing_consistency_expansion and
1180
not osutils.is_inside_any(self.searched_specific_files,
1181
source_details[1])):
1182
self.search_specific_files.add(source_details[1])
1183
# expanding from a user requested path, parent expansion
1184
# for delta consistency happens later.
1185
# generate the old path; this is needed for stating later
1187
old_path = source_details[1]
1188
old_dirname, old_basename = os.path.split(old_path)
1189
path = self.pathjoin(entry[0][0], entry[0][1])
1190
old_entry = self.state._get_entry(self.source_index,
1192
# update the source details variable to be the real
1194
if old_entry == (None, None):
1195
raise errors.CorruptDirstate(self.state._filename,
1196
"entry '%s/%s' is considered renamed from %r"
1197
" but source does not exist\n"
1198
"entry: %s" % (entry[0][0], entry[0][1], old_path, entry))
1199
source_details = old_entry[1][self.source_index]
1200
source_minikind = _minikind_from_string(source_details[0])
1201
if path_info is None:
1202
# the file is missing on disk, show as removed.
1207
# source and target are both versioned and disk file is present.
1208
target_kind = path_info[2]
1209
if target_kind == 'directory':
1211
old_path = path = self.pathjoin(old_dirname, old_basename)
1212
file_id = entry[0][2]
1213
self.new_dirname_to_file_id[path] = file_id
1214
if source_minikind != c'd':
1217
# directories have no fingerprint
1220
elif target_kind == 'file':
1221
if source_minikind != c'f':
1224
# Check the sha. We can't just rely on the size as
1225
# content filtering may mean differ sizes actually
1226
# map to the same content
1227
if link_or_sha1 is None:
1229
statvalue, link_or_sha1 = \
1230
self.state._sha1_provider.stat_and_sha1(
1232
self.state._observed_sha1(entry, link_or_sha1,
1234
content_change = (link_or_sha1 != source_details[1])
1235
# Target details is updated at update_entry time
1236
if self.use_filesystem_for_exec:
1237
# We don't need S_ISREG here, because we are sure
1238
# we are dealing with a file.
1239
target_exec = bool(S_IXUSR & path_info[3].st_mode)
1241
target_exec = target_details[3]
1242
elif target_kind == 'symlink':
1243
if source_minikind != c'l':
1246
content_change = (link_or_sha1 != source_details[1])
1248
elif target_kind == 'tree-reference':
1249
if source_minikind != c't':
1256
path = self.pathjoin(old_dirname, old_basename)
1257
raise errors.BadFileKindError(path, path_info[2])
1258
if source_minikind == c'd':
1260
old_path = path = self.pathjoin(old_dirname, old_basename)
1262
file_id = entry[0][2]
1263
self.old_dirname_to_file_id[old_path] = file_id
1264
# parent id is the entry for the path in the target tree
1265
if old_basename and old_dirname == self.last_source_parent[0]:
1266
# use a cached hit for non-root source entries.
1267
source_parent_id = self.last_source_parent[1]
1270
source_parent_id = self.old_dirname_to_file_id[old_dirname]
1272
source_parent_entry = self.state._get_entry(self.source_index,
1273
path_utf8=old_dirname)
1274
source_parent_id = source_parent_entry[0][2]
1275
if source_parent_id == entry[0][2]:
1276
# This is the root, so the parent is None
1277
source_parent_id = None
1279
self.last_source_parent[0] = old_dirname
1280
self.last_source_parent[1] = source_parent_id
1281
new_dirname = entry[0][0]
1282
if entry[0][1] and new_dirname == self.last_target_parent[0]:
1283
# use a cached hit for non-root target entries.
1284
target_parent_id = self.last_target_parent[1]
1287
target_parent_id = self.new_dirname_to_file_id[new_dirname]
1289
# TODO: We don't always need to do the lookup, because the
1290
# parent entry will be the same as the source entry.
1291
target_parent_entry = self.state._get_entry(self.target_index,
1292
path_utf8=new_dirname)
1293
if target_parent_entry == (None, None):
1294
raise AssertionError(
1295
"Could not find target parent in wt: %s\nparent of: %s"
1296
% (new_dirname, entry))
1297
target_parent_id = target_parent_entry[0][2]
1298
if target_parent_id == entry[0][2]:
1299
# This is the root, so the parent is None
1300
target_parent_id = None
1302
self.last_target_parent[0] = new_dirname
1303
self.last_target_parent[1] = target_parent_id
1305
source_exec = source_details[3]
1306
changed = (content_change
1307
or source_parent_id != target_parent_id
1308
or old_basename != entry[0][1]
1309
or source_exec != target_exec
1311
if not changed and not self.include_unchanged:
1314
if old_path is None:
1315
path = self.pathjoin(old_dirname, old_basename)
1317
old_path_u = self.utf8_decode(old_path)[0]
1320
old_path_u = self.utf8_decode(old_path)[0]
1321
if old_path == path:
1324
path_u = self.utf8_decode(path)[0]
1325
source_kind = _minikind_to_kind(source_minikind)
1326
return (entry[0][2],
1327
(old_path_u, path_u),
1330
(source_parent_id, target_parent_id),
1331
(self.utf8_decode(old_basename)[0], self.utf8_decode(entry[0][1])[0]),
1332
(source_kind, target_kind),
1333
(source_exec, target_exec)), changed
1334
elif source_minikind == c'a' and _versioned_minikind(target_minikind):
1335
# looks like a new file
1336
path = self.pathjoin(entry[0][0], entry[0][1])
1337
# parent id is the entry for the path in the target tree
1338
# TODO: these are the same for an entire directory: cache em.
1339
parent_entry = self.state._get_entry(self.target_index,
1340
path_utf8=entry[0][0])
1341
if parent_entry is None:
1342
raise errors.DirstateCorrupt(self.state,
1343
"We could not find the parent entry in index %d"
1344
" for the entry: %s"
1345
% (self.target_index, entry[0]))
1346
parent_id = parent_entry[0][2]
1347
if parent_id == entry[0][2]:
1349
if path_info is not None:
1351
if self.use_filesystem_for_exec:
1352
# We need S_ISREG here, because we aren't sure if this
1355
S_ISREG(path_info[3].st_mode)
1356
and S_IXUSR & path_info[3].st_mode)
1358
target_exec = target_details[3]
1359
return (entry[0][2],
1360
(None, self.utf8_decode(path)[0]),
1364
(None, self.utf8_decode(entry[0][1])[0]),
1365
(None, path_info[2]),
1366
(None, target_exec)), True
1368
# Its a missing file, report it as such.
1369
return (entry[0][2],
1370
(None, self.utf8_decode(path)[0]),
1374
(None, self.utf8_decode(entry[0][1])[0]),
1376
(None, False)), True
1377
elif _versioned_minikind(source_minikind) and target_minikind == c'a':
1378
# unversioned, possibly, or possibly not deleted: we dont care.
1379
# if its still on disk, *and* theres no other entry at this
1380
# path [we dont know this in this routine at the moment -
1381
# perhaps we should change this - then it would be an unknown.
1382
old_path = self.pathjoin(entry[0][0], entry[0][1])
1383
# parent id is the entry for the path in the target tree
1384
parent_id = self.state._get_entry(self.source_index, path_utf8=entry[0][0])[0][2]
1385
if parent_id == entry[0][2]:
1387
return (entry[0][2],
1388
(self.utf8_decode(old_path)[0], None),
1392
(self.utf8_decode(entry[0][1])[0], None),
1393
(_minikind_to_kind(source_minikind), None),
1394
(source_details[3], None)), True
1395
elif _versioned_minikind(source_minikind) and target_minikind == c'r':
1396
# a rename; could be a true rename, or a rename inherited from
1397
# a renamed parent. TODO: handle this efficiently. Its not
1398
# common case to rename dirs though, so a correct but slow
1399
# implementation will do.
1400
if (not self.doing_consistency_expansion and
1401
not osutils.is_inside_any(self.searched_specific_files,
1402
target_details[1])):
1403
self.search_specific_files.add(target_details[1])
1404
# We don't expand the specific files parents list here as
1405
# the path is absent in target and won't create a delta with
1407
elif ((source_minikind == c'r' or source_minikind == c'a') and
1408
(target_minikind == c'r' or target_minikind == c'a')):
1409
# neither of the selected trees contain this path,
1410
# so skip over it. This is not currently directly tested, but
1411
# is indirectly via test_too_much.TestCommands.test_conflicts.
1414
raise AssertionError("don't know how to compare "
1415
"source_minikind=%r, target_minikind=%r"
1416
% (source_minikind, target_minikind))
1417
## import pdb;pdb.set_trace()
1423
def iter_changes(self):
1426
cdef int _gather_result_for_consistency(self, result) except -1:
1427
"""Check a result we will yield to make sure we are consistent later.
1429
This gathers result's parents into a set to output later.
1431
:param result: A result tuple.
1433
if not self.partial or not result[0]:
1435
self.seen_ids.add(result[0])
1436
new_path = result[1][1]
1438
# Not the root and not a delete: queue up the parents of the path.
1439
self.search_specific_file_parents.update(
1440
osutils.parent_directories(new_path.encode('utf8')))
1441
# Add the root directory which parent_directories does not
1443
self.search_specific_file_parents.add('')
1446
cdef int _update_current_block(self) except -1:
1447
if (self.block_index < len(self.state._dirblocks) and
1448
osutils.is_inside(self.current_root, self.state._dirblocks[self.block_index][0])):
1449
self.current_block = self.state._dirblocks[self.block_index]
1450
self.current_block_list = self.current_block[1]
1451
self.current_block_pos = 0
1453
self.current_block = None
1454
self.current_block_list = None
1458
# Simple thunk to allow tail recursion without pyrex confusion
1459
return self._iter_next()
1461
cdef _iter_next(self):
1462
"""Iterate over the changes."""
1463
# This function single steps through an iterator. As such while loops
1464
# are often exited by 'return' - the code is structured so that the
1465
# next call into the function will return to the same while loop. Note
1466
# that all flow control needed to re-reach that step is reexecuted,
1467
# which can be a performance problem. It has not yet been tuned to
1468
# minimise this; a state machine is probably the simplest restructuring
1469
# to both minimise this overhead and make the code considerably more
1473
# compare source_index and target_index at or under each element of search_specific_files.
1474
# follow the following comparison table. Note that we only want to do diff operations when
1475
# the target is fdl because thats when the walkdirs logic will have exposed the pathinfo
1479
# Source | Target | disk | action
1480
# r | fdlt | | add source to search, add id path move and perform
1481
# | | | diff check on source-target
1482
# r | fdlt | a | dangling file that was present in the basis.
1484
# r | a | | add source to search
1486
# r | r | | this path is present in a non-examined tree, skip.
1487
# r | r | a | this path is present in a non-examined tree, skip.
1488
# a | fdlt | | add new id
1489
# a | fdlt | a | dangling locally added file, skip
1490
# a | a | | not present in either tree, skip
1491
# a | a | a | not present in any tree, skip
1492
# a | r | | not present in either tree at this path, skip as it
1493
# | | | may not be selected by the users list of paths.
1494
# a | r | a | not present in either tree at this path, skip as it
1495
# | | | may not be selected by the users list of paths.
1496
# fdlt | fdlt | | content in both: diff them
1497
# fdlt | fdlt | a | deleted locally, but not unversioned - show as deleted ?
1498
# fdlt | a | | unversioned: output deleted id for now
1499
# fdlt | a | a | unversioned and deleted: output deleted id
1500
# fdlt | r | | relocated in this tree, so add target to search.
1501
# | | | Dont diff, we will see an r,fd; pair when we reach
1502
# | | | this id at the other path.
1503
# fdlt | r | a | relocated in this tree, so add target to search.
1504
# | | | Dont diff, we will see an r,fd; pair when we reach
1505
# | | | this id at the other path.
1507
# TODO: jam 20070516 - Avoid the _get_entry lookup overhead by
1508
# keeping a cache of directories that we have seen.
1509
cdef object current_dirname, current_blockname
1510
cdef char * current_dirname_c, * current_blockname_c
1511
cdef int advance_entry, advance_path
1512
cdef int path_handled
1513
searched_specific_files = self.searched_specific_files
1514
# Are we walking a root?
1515
while self.root_entries_pos < self.root_entries_len:
1516
entry = self.root_entries[self.root_entries_pos]
1517
self.root_entries_pos = self.root_entries_pos + 1
1518
result, changed = self._process_entry(entry, self.root_dir_info)
1519
if changed is not None:
1521
self._gather_result_for_consistency(result)
1522
if changed or self.include_unchanged:
1524
# Have we finished the prior root, or never started one ?
1525
if self.current_root is None:
1526
# TODO: the pending list should be lexically sorted? the
1527
# interface doesn't require it.
1529
self.current_root = self.search_specific_files.pop()
1531
raise StopIteration()
1532
self.searched_specific_files.add(self.current_root)
1533
# process the entries for this containing directory: the rest will be
1534
# found by their parents recursively.
1535
self.root_entries = self.state._entries_for_path(self.current_root)
1536
self.root_entries_len = len(self.root_entries)
1537
self.current_root_unicode = self.current_root.decode('utf8')
1538
self.root_abspath = self.tree.abspath(self.current_root_unicode)
1540
root_stat = os.lstat(self.root_abspath)
1542
if e.errno == errno.ENOENT:
1543
# the path does not exist: let _process_entry know that.
1544
self.root_dir_info = None
1546
# some other random error: hand it up.
1549
self.root_dir_info = ('', self.current_root,
1550
osutils.file_kind_from_stat_mode(root_stat.st_mode), root_stat,
1552
if self.root_dir_info[2] == 'directory':
1553
if self.tree._directory_is_tree_reference(
1554
self.current_root_unicode):
1555
self.root_dir_info = self.root_dir_info[:2] + \
1556
('tree-reference',) + self.root_dir_info[3:]
1557
if not self.root_entries and not self.root_dir_info:
1558
# this specified path is not present at all, skip it.
1559
# (tail recursion, can do a loop once the full structure is
1561
return self._iter_next()
1563
self.root_entries_pos = 0
1564
# XXX Clarity: This loop is duplicated a out the self.current_root
1565
# is None guard above: if we return from it, it completes there
1566
# (and the following if block cannot trigger because
1567
# path_handled must be true, so the if block is not # duplicated.
1568
while self.root_entries_pos < self.root_entries_len:
1569
entry = self.root_entries[self.root_entries_pos]
1570
self.root_entries_pos = self.root_entries_pos + 1
1571
result, changed = self._process_entry(entry, self.root_dir_info)
1572
if changed is not None:
1575
self._gather_result_for_consistency(result)
1576
if changed or self.include_unchanged:
1578
# handle unversioned specified paths:
1579
if self.want_unversioned and not path_handled and self.root_dir_info:
1580
new_executable = bool(
1581
stat.S_ISREG(self.root_dir_info[3].st_mode)
1582
and stat.S_IEXEC & self.root_dir_info[3].st_mode)
1584
(None, self.current_root_unicode),
1588
(None, splitpath(self.current_root_unicode)[-1]),
1589
(None, self.root_dir_info[2]),
1590
(None, new_executable)
1592
# If we reach here, the outer flow continues, which enters into the
1593
# per-root setup logic.
1594
if (self.current_dir_info is None and self.current_block is None and not
1595
self.doing_consistency_expansion):
1596
# setup iteration of this root:
1597
self.current_dir_list = None
1598
if self.root_dir_info and self.root_dir_info[2] == 'tree-reference':
1599
self.current_dir_info = None
1601
self.dir_iterator = osutils._walkdirs_utf8(self.root_abspath,
1602
prefix=self.current_root)
1605
self.current_dir_info = self.dir_iterator.next()
1606
self.current_dir_list = self.current_dir_info[1]
1608
# there may be directories in the inventory even though
1609
# this path is not a file on disk: so mark it as end of
1611
if e.errno in (errno.ENOENT, errno.ENOTDIR, errno.EINVAL):
1612
self.current_dir_info = None
1613
elif sys.platform == 'win32':
1614
# on win32, python2.4 has e.errno == ERROR_DIRECTORY, but
1615
# python 2.5 has e.errno == EINVAL,
1616
# and e.winerror == ERROR_DIRECTORY
1618
e_winerror = e.winerror
1619
except AttributeError, _:
1621
win_errors = (ERROR_DIRECTORY, ERROR_PATH_NOT_FOUND)
1622
if (e.errno in win_errors or e_winerror in win_errors):
1623
self.current_dir_info = None
1625
# Will this really raise the right exception ?
1630
if self.current_dir_info[0][0] == '':
1631
# remove .bzr from iteration
1632
bzr_index = self.bisect_left(self.current_dir_list, ('.bzr',))
1633
if self.current_dir_list[bzr_index][0] != '.bzr':
1634
raise AssertionError()
1635
del self.current_dir_list[bzr_index]
1636
initial_key = (self.current_root, '', '')
1637
self.block_index, _ = self.state._find_block_index_from_key(initial_key)
1638
if self.block_index == 0:
1639
# we have processed the total root already, but because the
1640
# initial key matched it we should skip it here.
1641
self.block_index = self.block_index + 1
1642
self._update_current_block()
1643
# walk until both the directory listing and the versioned metadata
1645
while (self.current_dir_info is not None
1646
or self.current_block is not None):
1647
# Uncommon case - a missing directory or an unversioned directory:
1648
if (self.current_dir_info and self.current_block
1649
and self.current_dir_info[0][0] != self.current_block[0]):
1650
# Work around pyrex broken heuristic - current_dirname has
1651
# the same scope as current_dirname_c
1652
current_dirname = self.current_dir_info[0][0]
1653
current_dirname_c = PyString_AS_STRING_void(
1654
<void *>current_dirname)
1655
current_blockname = self.current_block[0]
1656
current_blockname_c = PyString_AS_STRING_void(
1657
<void *>current_blockname)
1658
# In the python generator we evaluate this if block once per
1659
# dir+block; because we reenter in the pyrex version its being
1660
# evaluated once per path: we could cache the result before
1661
# doing the while loop and probably save time.
1662
if _cmp_by_dirs(current_dirname_c,
1663
PyString_Size(current_dirname),
1664
current_blockname_c,
1665
PyString_Size(current_blockname)) < 0:
1666
# filesystem data refers to paths not covered by the
1667
# dirblock. this has two possibilities:
1668
# A) it is versioned but empty, so there is no block for it
1669
# B) it is not versioned.
1671
# if (A) then we need to recurse into it to check for
1672
# new unknown files or directories.
1673
# if (B) then we should ignore it, because we don't
1674
# recurse into unknown directories.
1675
# We are doing a loop
1676
while self.path_index < len(self.current_dir_list):
1677
current_path_info = self.current_dir_list[self.path_index]
1678
# dont descend into this unversioned path if it is
1680
if current_path_info[2] in ('directory',
1682
del self.current_dir_list[self.path_index]
1683
self.path_index = self.path_index - 1
1684
self.path_index = self.path_index + 1
1685
if self.want_unversioned:
1686
if current_path_info[2] == 'directory':
1687
if self.tree._directory_is_tree_reference(
1688
self.utf8_decode(current_path_info[0])[0]):
1689
current_path_info = current_path_info[:2] + \
1690
('tree-reference',) + current_path_info[3:]
1691
new_executable = bool(
1692
stat.S_ISREG(current_path_info[3].st_mode)
1693
and stat.S_IEXEC & current_path_info[3].st_mode)
1695
(None, self.utf8_decode(current_path_info[0])[0]),
1699
(None, self.utf8_decode(current_path_info[1])[0]),
1700
(None, current_path_info[2]),
1701
(None, new_executable))
1702
# This dir info has been handled, go to the next
1704
self.current_dir_list = None
1706
self.current_dir_info = self.dir_iterator.next()
1707
self.current_dir_list = self.current_dir_info[1]
1708
except StopIteration, _:
1709
self.current_dir_info = None
1711
# We have a dirblock entry for this location, but there
1712
# is no filesystem path for this. This is most likely
1713
# because a directory was removed from the disk.
1714
# We don't have to report the missing directory,
1715
# because that should have already been handled, but we
1716
# need to handle all of the files that are contained
1718
while self.current_block_pos < len(self.current_block_list):
1719
current_entry = self.current_block_list[self.current_block_pos]
1720
self.current_block_pos = self.current_block_pos + 1
1721
# entry referring to file not present on disk.
1722
# advance the entry only, after processing.
1723
result, changed = self._process_entry(current_entry, None)
1724
if changed is not None:
1726
self._gather_result_for_consistency(result)
1727
if changed or self.include_unchanged:
1729
self.block_index = self.block_index + 1
1730
self._update_current_block()
1731
continue # next loop-on-block/dir
1732
result = self._loop_one_block()
1733
if result is not None:
1735
if len(self.search_specific_files):
1736
# More supplied paths to process
1737
self.current_root = None
1738
return self._iter_next()
1739
# Start expanding more conservatively, adding paths the user may not
1740
# have intended but required for consistent deltas.
1741
self.doing_consistency_expansion = 1
1742
if not self._pending_consistent_entries:
1743
self._pending_consistent_entries = self._next_consistent_entries()
1744
while self._pending_consistent_entries:
1745
result, changed = self._pending_consistent_entries.pop()
1746
if changed is not None:
1748
raise StopIteration()
1750
cdef object _maybe_tree_ref(self, current_path_info):
1751
if self.tree._directory_is_tree_reference(
1752
self.utf8_decode(current_path_info[0])[0]):
1753
return current_path_info[:2] + \
1754
('tree-reference',) + current_path_info[3:]
1756
return current_path_info
1758
cdef object _loop_one_block(self):
1759
# current_dir_info and current_block refer to the same directory -
1760
# this is the common case code.
1761
# Assign local variables for current path and entry:
1762
cdef object current_entry
1763
cdef object current_path_info
1764
cdef int path_handled
1767
# cdef char * temp_str
1768
# cdef Py_ssize_t temp_str_length
1769
# PyString_AsStringAndSize(disk_kind, &temp_str, &temp_str_length)
1770
# if not strncmp(temp_str, "directory", temp_str_length):
1771
if (self.current_block is not None and
1772
self.current_block_pos < PyList_GET_SIZE(self.current_block_list)):
1773
current_entry = PyList_GET_ITEM(self.current_block_list,
1774
self.current_block_pos)
1776
Py_INCREF(current_entry)
1778
current_entry = None
1779
if (self.current_dir_info is not None and
1780
self.path_index < PyList_GET_SIZE(self.current_dir_list)):
1781
current_path_info = PyList_GET_ITEM(self.current_dir_list,
1784
Py_INCREF(current_path_info)
1785
disk_kind = PyTuple_GET_ITEM(current_path_info, 2)
1787
Py_INCREF(disk_kind)
1788
if disk_kind == "directory":
1789
current_path_info = self._maybe_tree_ref(current_path_info)
1791
current_path_info = None
1792
while (current_entry is not None or current_path_info is not None):
1797
if current_entry is None:
1798
# unversioned - the check for path_handled when the path
1799
# is advanced will yield this path if needed.
1801
elif current_path_info is None:
1802
# no path is fine: the per entry code will handle it.
1803
result, changed = self._process_entry(current_entry,
1806
minikind = _minikind_from_string(
1807
current_entry[1][self.target_index][0])
1808
cmp_result = cmp(current_path_info[1], current_entry[0][1])
1809
if (cmp_result or minikind == c'a' or minikind == c'r'):
1810
# The current path on disk doesn't match the dirblock
1811
# record. Either the dirblock record is marked as
1812
# absent/renamed, or the file on disk is not present at all
1813
# in the dirblock. Either way, report about the dirblock
1814
# entry, and let other code handle the filesystem one.
1816
# Compare the basename for these files to determine
1819
# extra file on disk: pass for now, but only
1820
# increment the path, not the entry
1823
# entry referring to file not present on disk.
1824
# advance the entry only, after processing.
1825
result, changed = self._process_entry(current_entry,
1829
# paths are the same,and the dirstate entry is not
1830
# absent or renamed.
1831
result, changed = self._process_entry(current_entry,
1833
if changed is not None:
1835
if not changed and not self.include_unchanged:
1837
# >- loop control starts here:
1839
if advance_entry and current_entry is not None:
1840
self.current_block_pos = self.current_block_pos + 1
1841
if self.current_block_pos < PyList_GET_SIZE(self.current_block_list):
1842
current_entry = self.current_block_list[self.current_block_pos]
1844
current_entry = None
1846
if advance_path and current_path_info is not None:
1847
if not path_handled:
1848
# unversioned in all regards
1849
if self.want_unversioned:
1850
new_executable = bool(
1851
stat.S_ISREG(current_path_info[3].st_mode)
1852
and stat.S_IEXEC & current_path_info[3].st_mode)
1854
relpath_unicode = self.utf8_decode(current_path_info[0])[0]
1855
except UnicodeDecodeError, _:
1856
raise errors.BadFilenameEncoding(
1857
current_path_info[0], osutils._fs_enc)
1858
if changed is not None:
1859
raise AssertionError(
1860
"result is not None: %r" % result)
1862
(None, relpath_unicode),
1866
(None, self.utf8_decode(current_path_info[1])[0]),
1867
(None, current_path_info[2]),
1868
(None, new_executable))
1870
# dont descend into this unversioned path if it is
1872
if current_path_info[2] in ('directory'):
1873
del self.current_dir_list[self.path_index]
1874
self.path_index = self.path_index - 1
1875
# dont descend the disk iterator into any tree
1877
if current_path_info[2] == 'tree-reference':
1878
del self.current_dir_list[self.path_index]
1879
self.path_index = self.path_index - 1
1880
self.path_index = self.path_index + 1
1881
if self.path_index < len(self.current_dir_list):
1882
current_path_info = self.current_dir_list[self.path_index]
1883
if current_path_info[2] == 'directory':
1884
current_path_info = self._maybe_tree_ref(
1887
current_path_info = None
1888
if changed is not None:
1889
# Found a result on this pass, yield it
1891
self._gather_result_for_consistency(result)
1892
if changed or self.include_unchanged:
1894
if self.current_block is not None:
1895
self.block_index = self.block_index + 1
1896
self._update_current_block()
1897
if self.current_dir_info is not None:
1899
self.current_dir_list = None
1901
self.current_dir_info = self.dir_iterator.next()
1902
self.current_dir_list = self.current_dir_info[1]
1903
except StopIteration, _:
1904
self.current_dir_info = None
1906
cdef object _next_consistent_entries(self):
1907
"""Grabs the next specific file parent case to consider.
1909
:return: A list of the results, each of which is as for _process_entry.
1912
while self.search_specific_file_parents:
1913
# Process the parent directories for the paths we were iterating.
1914
# Even in extremely large trees this should be modest, so currently
1915
# no attempt is made to optimise.
1916
path_utf8 = self.search_specific_file_parents.pop()
1917
if path_utf8 in self.searched_exact_paths:
1918
# We've examined this path.
1920
if osutils.is_inside_any(self.searched_specific_files, path_utf8):
1921
# We've examined this path.
1923
path_entries = self.state._entries_for_path(path_utf8)
1924
# We need either one or two entries. If the path in
1925
# self.target_index has moved (so the entry in source_index is in
1926
# 'ar') then we need to also look for the entry for this path in
1927
# self.source_index, to output the appropriate delete-or-rename.
1928
selected_entries = []
1930
for candidate_entry in path_entries:
1931
# Find entries present in target at this path:
1932
if candidate_entry[1][self.target_index][0] not in 'ar':
1934
selected_entries.append(candidate_entry)
1935
# Find entries present in source at this path:
1936
elif (self.source_index is not None and
1937
candidate_entry[1][self.source_index][0] not in 'ar'):
1939
if candidate_entry[1][self.target_index][0] == 'a':
1940
# Deleted, emit it here.
1941
selected_entries.append(candidate_entry)
1943
# renamed, emit it when we process the directory it
1945
self.search_specific_file_parents.add(
1946
candidate_entry[1][self.target_index][1])
1948
raise AssertionError(
1949
"Missing entry for specific path parent %r, %r" % (
1950
path_utf8, path_entries))
1951
path_info = self._path_info(path_utf8, path_utf8.decode('utf8'))
1952
for entry in selected_entries:
1953
if entry[0][2] in self.seen_ids:
1955
result, changed = self._process_entry(entry, path_info)
1957
raise AssertionError(
1958
"Got entry<->path mismatch for specific path "
1959
"%r entry %r path_info %r " % (
1960
path_utf8, entry, path_info))
1961
# Only include changes - we're outside the users requested
1964
self._gather_result_for_consistency(result)
1965
if (result[6][0] == 'directory' and
1966
result[6][1] != 'directory'):
1967
# This stopped being a directory, the old children have
1969
if entry[1][self.source_index][0] == 'r':
1970
# renamed, take the source path
1971
entry_path_utf8 = entry[1][self.source_index][1]
1973
entry_path_utf8 = path_utf8
1974
initial_key = (entry_path_utf8, '', '')
1975
block_index, _ = self.state._find_block_index_from_key(
1977
if block_index == 0:
1978
# The children of the root are in block index 1.
1979
block_index = block_index + 1
1980
current_block = None
1981
if block_index < len(self.state._dirblocks):
1982
current_block = self.state._dirblocks[block_index]
1983
if not osutils.is_inside(
1984
entry_path_utf8, current_block[0]):
1985
# No entries for this directory at all.
1986
current_block = None
1987
if current_block is not None:
1988
for entry in current_block[1]:
1989
if entry[1][self.source_index][0] in 'ar':
1990
# Not in the source tree, so doesn't have to be
1993
# Path of the entry itself.
1994
self.search_specific_file_parents.add(
1995
self.pathjoin(*entry[0][:2]))
1996
if changed or self.include_unchanged:
1997
results.append((result, changed))
1998
self.searched_exact_paths.add(path_utf8)
2001
cdef object _path_info(self, utf8_path, unicode_path):
2002
"""Generate path_info for unicode_path.
2004
:return: None if unicode_path does not exist, or a path_info tuple.
2006
abspath = self.tree.abspath(unicode_path)
2008
stat = os.lstat(abspath)
2010
if e.errno == errno.ENOENT:
2011
# the path does not exist.
2015
utf8_basename = utf8_path.rsplit('/', 1)[-1]
2016
dir_info = (utf8_path, utf8_basename,
2017
osutils.file_kind_from_stat_mode(stat.st_mode), stat,
2019
if dir_info[2] == 'directory':
2020
if self.tree._directory_is_tree_reference(
2022
self.root_dir_info = self.root_dir_info[:2] + \
2023
('tree-reference',) + self.root_dir_info[3:]