17
17
"""Python implementations of Dirstate Helper functions."""
19
from __future__ import absolute_import
25
21
# We cannot import the dirstate module, because it loads this module
26
22
# All we really need is the IN_MEMORY_MODIFIED constant
27
from .dirstate import DirState, DirstateCorrupt
28
from ..sixish import (
33
def pack_stat(st, _b64=binascii.b2a_base64, _pack=struct.Struct('>6L').pack):
34
"""Convert stat values into a packed representation
36
Not all of the fields from the stat included are strictly needed, and by
37
just encoding the mtime and mode a slight speed increase could be gained.
38
However, using the pyrex version instead is a bigger win.
40
# base64 encoding always adds a final newline, so strip it off
41
return _b64(_pack(st.st_size & 0xFFFFFFFF, int(st.st_mtime) & 0xFFFFFFFF,
42
int(st.st_ctime) & 0xFFFFFFFF, st.st_dev & 0xFFFFFFFF,
43
st.st_ino & 0xFFFFFFFF, st.st_mode))[:-1]
46
def _unpack_stat(packed_stat):
47
"""Turn a packed_stat back into the stat fields.
49
This is meant as a debugging tool, should not be used in real code.
51
(st_size, st_mtime, st_ctime, st_dev, st_ino,
52
st_mode) = struct.unpack('>6L', binascii.a2b_base64(packed_stat))
53
return dict(st_size=st_size, st_mtime=st_mtime, st_ctime=st_ctime,
54
st_dev=st_dev, st_ino=st_ino, st_mode=st_mode)
23
from bzrlib import errors
24
from bzrlib.dirstate import DirState
57
27
def _bisect_path_left(paths, path):
156
126
cur_split = cache[cur]
158
cur_split = cur.split(b'/')
128
cur_split = cur.split('/')
159
129
cache[cur] = cur_split
160
if cur_split < dirname_split:
130
if cur_split < dirname_split: lo = mid + 1
167
def lt_by_dirs(path1, path2):
135
def cmp_by_dirs(path1, path2):
168
136
"""Compare two paths directory by directory.
170
138
This is equivalent to doing::
172
operator.lt(path1.split('/'), path2.split('/'))
140
cmp(path1.split('/'), path2.split('/'))
174
142
The idea is that you should compare path components separately. This
175
differs from plain ``path1 < path2`` for paths like ``'a-b'`` and ``a/b``.
176
"a-b" comes after "a" but would come before "a/b" lexically.
143
differs from plain ``cmp(path1, path2)`` for paths like ``'a-b'`` and
144
``a/b``. "a-b" comes after "a" but would come before "a/b" lexically.
178
146
:param path1: first path
179
147
:param path2: second path
180
:return: True if path1 comes first, otherwise False
148
:return: negative number if ``path1`` comes first,
149
0 if paths are equal,
150
and positive number if ``path2`` sorts first
182
if not isinstance(path1, bytes):
183
raise TypeError("'path1' must be a byte string, not %s: %r"
152
if not isinstance(path1, str):
153
raise TypeError("'path1' must be a plain string, not %s: %r"
184
154
% (type(path1), path1))
185
if not isinstance(path2, bytes):
186
raise TypeError("'path2' must be a byte string, not %s: %r"
155
if not isinstance(path2, str):
156
raise TypeError("'path2' must be a plain string, not %s: %r"
187
157
% (type(path2), path2))
188
return path1.split(b'/') < path2.split(b'/')
191
def _lt_path_by_dirblock(path1, path2):
158
return cmp(path1.split('/'), path2.split('/'))
161
def _cmp_path_by_dirblock(path1, path2):
192
162
"""Compare two paths based on what directory they are in.
194
164
This generates a sort order, such that all children of a directory are
198
168
:param path1: first path
199
169
:param path2: the second path
200
:return: True if path1 comes first, otherwise False
170
:return: negative number if ``path1`` comes first,
172
and a positive number if ``path2`` sorts first
202
if not isinstance(path1, bytes):
174
if not isinstance(path1, str):
203
175
raise TypeError("'path1' must be a plain string, not %s: %r"
204
176
% (type(path1), path1))
205
if not isinstance(path2, bytes):
177
if not isinstance(path2, str):
206
178
raise TypeError("'path2' must be a plain string, not %s: %r"
207
179
% (type(path2), path2))
208
180
dirname1, basename1 = os.path.split(path1)
209
key1 = (dirname1.split(b'/'), basename1)
181
key1 = (dirname1.split('/'), basename1)
210
182
dirname2, basename2 = os.path.split(path2)
211
key2 = (dirname2.split(b'/'), basename2)
183
key2 = (dirname2.split('/'), basename2)
184
return cmp(key1, key2)
215
187
def _read_dirblocks(state):
226
198
text = state._state_file.read()
227
199
# TODO: check the crc checksums. crc_measured = zlib.crc32(text)
229
fields = text.split(b'\0')
201
fields = text.split('\0')
230
202
# Remove the last blank entry
231
203
trailing = fields.pop()
233
raise DirstateCorrupt(state,
234
'trailing garbage: %r' % (trailing,))
205
raise errors.DirstateCorrupt(state,
206
'trailing garbage: %r' % (trailing,))
235
207
# consider turning fields into a tuple.
237
209
# skip the first field which is the trailing null from the header.
249
221
field_count = len(fields)
250
222
# this checks our adjustment, and also catches file too short.
251
223
if field_count - cur != expected_field_count:
252
raise DirstateCorrupt(state,
253
'field count incorrect %s != %s, entry_size=%s, '
254
'num_entries=%s fields=%r' % (
255
field_count - cur, expected_field_count, entry_size,
256
state._num_entries, fields))
224
raise errors.DirstateCorrupt(state,
225
'field count incorrect %s != %s, entry_size=%s, '\
226
'num_entries=%s fields=%r' % (
227
field_count - cur, expected_field_count, entry_size,
228
state._num_entries, fields))
258
230
if num_present_parents == 1:
259
231
# Bind external functions to local names
262
234
# them. Grab an straight iterator over the fields. (We use an
263
235
# iterator because we don't want to do a lot of additions, nor
264
236
# do we want to do a lot of slicing)
266
# Get a local reference to the compatible next method
267
next = getattr(_iter, '__next__', None)
237
next = iter(fields).next
270
238
# Move the iterator to the current position
239
for x in xrange(cur):
273
241
# The two blocks here are deliberate: the root block and the
274
242
# contents-of-root block.
275
state._dirblocks = [(b'', []), (b'', [])]
243
state._dirblocks = [('', []), ('', [])]
276
244
current_block = state._dirblocks[0][1]
277
current_dirname = b''
278
246
append_entry = current_block.append
279
for count in range(state._num_entries):
247
for count in xrange(state._num_entries):
289
257
# we know current_dirname == dirname, so re-use it to avoid
290
258
# creating new strings
291
259
entry = ((current_dirname, name, file_id),
294
next(), # fingerprint
296
next() == b'y', # executable
297
next(), # packed_stat or revision_id
301
next(), # fingerprint
303
next() == b'y', # executable
304
next(), # packed_stat or revision_id
262
next(), # fingerprint
264
next() == 'y', # executable
265
next(), # packed_stat or revision_id
269
next(), # fingerprint
271
next() == 'y', # executable
272
next(), # packed_stat or revision_id
307
275
trailing = next()
308
if trailing != b'\n':
309
277
raise ValueError("trailing garbage in dirstate: %r" % trailing)
310
278
# append the entry to the current block
311
279
append_entry(entry)
312
280
state._split_root_dirblock_into_contents()
314
282
fields_to_entry = state._get_fields_to_entry()
315
entries = [fields_to_entry(fields[pos:pos + entry_size])
316
for pos in range(cur, field_count, entry_size)]
283
entries = [fields_to_entry(fields[pos:pos+entry_size])
284
for pos in xrange(cur, field_count, entry_size)]
317
285
state._entries_to_current_state(entries)
318
286
# To convert from format 2 => format 3
319
287
# state._dirblocks = sorted(state._dirblocks,