13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
"""Python implementations of Dirstate Helper functions."""
23
21
# We cannot import the dirstate module, because it loads this module
24
22
# All we really need is the IN_MEMORY_MODIFIED constant
25
from bzrlib import errors
26
23
from bzrlib.dirstate import DirState
29
def pack_stat(st, _b64=binascii.b2a_base64, _pack=struct.Struct('>6L').pack):
30
"""Convert stat values into a packed representation
32
Not all of the fields from the stat included are strictly needed, and by
33
just encoding the mtime and mode a slight speed increase could be gained.
34
However, using the pyrex version instead is a bigger win.
36
# base64 encoding always adds a final newline, so strip it off
37
return _b64(_pack(st.st_size & 0xFFFFFFFF, int(st.st_mtime) & 0xFFFFFFFF,
38
int(st.st_ctime) & 0xFFFFFFFF, st.st_dev & 0xFFFFFFFF,
39
st.st_ino & 0xFFFFFFFF, st.st_mode))[:-1]
42
def _unpack_stat(packed_stat):
43
"""Turn a packed_stat back into the stat fields.
45
This is meant as a debugging tool, should not be used in real code.
47
(st_size, st_mtime, st_ctime, st_dev, st_ino,
48
st_mode) = struct.unpack('>6L', binascii.a2b_base64(packed_stat))
49
return dict(st_size=st_size, st_mtime=st_mtime, st_ctime=st_ctime,
50
st_dev=st_dev, st_ino=st_ino, st_mode=st_mode)
53
def _bisect_path_left(paths, path):
26
def _bisect_path_left_py(paths, path):
54
27
"""Return the index where to insert path into paths.
56
29
This uses the dirblock sorting. So all children in a directory come before
89
62
mid = (lo + hi) // 2
90
63
# Grab the dirname for the current dirblock
92
if _cmp_path_by_dirblock(cur, path) < 0:
65
if _cmp_path_by_dirblock_py(cur, path) < 0:
99
def _bisect_path_right(paths, path):
72
def _bisect_path_right_py(paths, path):
100
73
"""Return the index where to insert path into paths.
102
75
This uses a path-wise comparison so we get::
121
94
# Grab the dirname for the current dirblock
123
if _cmp_path_by_dirblock(path, cur) < 0:
96
if _cmp_path_by_dirblock_py(path, cur) < 0:
130
def bisect_dirblock(dirblocks, dirname, lo=0, hi=None, cache={}):
103
def bisect_dirblock_py(dirblocks, dirname, lo=0, hi=None, cache={}):
131
104
"""Return the index where to insert dirname into the dirblocks.
133
106
The return value idx is such that all directories blocks in dirblock[:idx]
184
157
return cmp(path1.split('/'), path2.split('/'))
187
def _cmp_path_by_dirblock(path1, path2):
160
def _cmp_path_by_dirblock_py(path1, path2):
188
161
"""Compare two paths based on what directory they are in.
190
163
This generates a sort order, such that all children of a directory are
227
200
fields = text.split('\0')
228
201
# Remove the last blank entry
229
202
trailing = fields.pop()
231
raise errors.DirstateCorrupt(state,
232
'trailing garbage: %r' % (trailing,))
203
assert trailing == ''
233
204
# consider turning fields into a tuple.
235
206
# skip the first field which is the trailing null from the header.
246
217
expected_field_count = entry_size * state._num_entries
247
218
field_count = len(fields)
248
219
# this checks our adjustment, and also catches file too short.
249
if field_count - cur != expected_field_count:
250
raise errors.DirstateCorrupt(state,
251
'field count incorrect %s != %s, entry_size=%s, '\
252
'num_entries=%s fields=%r' % (
220
assert field_count - cur == expected_field_count, \
221
'field count incorrect %s != %s, entry_size=%s, '\
222
'num_entries=%s fields=%r' % (
253
223
field_count - cur, expected_field_count, entry_size,
254
state._num_entries, fields))
224
state._num_entries, fields)
256
226
if num_present_parents == 1:
257
227
# Bind external functions to local names