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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Python implementations of Dirstate Helper functions."""
19
from __future__ import absolute_import
21
25
# We cannot import the dirstate module, because it loads this module
22
26
# All we really need is the IN_MEMORY_MODIFIED constant
27
from bzrlib import errors
23
28
from bzrlib.dirstate import DirState
26
def _bisect_path_left_py(paths, path):
31
def pack_stat(st, _b64=binascii.b2a_base64, _pack=struct.Struct('>6L').pack):
32
"""Convert stat values into a packed representation
34
Not all of the fields from the stat included are strictly needed, and by
35
just encoding the mtime and mode a slight speed increase could be gained.
36
However, using the pyrex version instead is a bigger win.
38
# base64 encoding always adds a final newline, so strip it off
39
return _b64(_pack(st.st_size & 0xFFFFFFFF, int(st.st_mtime) & 0xFFFFFFFF,
40
int(st.st_ctime) & 0xFFFFFFFF, st.st_dev & 0xFFFFFFFF,
41
st.st_ino & 0xFFFFFFFF, st.st_mode))[:-1]
44
def _unpack_stat(packed_stat):
45
"""Turn a packed_stat back into the stat fields.
47
This is meant as a debugging tool, should not be used in real code.
49
(st_size, st_mtime, st_ctime, st_dev, st_ino,
50
st_mode) = struct.unpack('>6L', binascii.a2b_base64(packed_stat))
51
return dict(st_size=st_size, st_mtime=st_mtime, st_ctime=st_ctime,
52
st_dev=st_dev, st_ino=st_ino, st_mode=st_mode)
55
def _bisect_path_left(paths, path):
27
56
"""Return the index where to insert path into paths.
29
58
This uses the dirblock sorting. So all children in a directory come before
62
91
mid = (lo + hi) // 2
63
92
# Grab the dirname for the current dirblock
65
if _cmp_path_by_dirblock_py(cur, path) < 0:
94
if _cmp_path_by_dirblock(cur, path) < 0:
72
def _bisect_path_right_py(paths, path):
101
def _bisect_path_right(paths, path):
73
102
"""Return the index where to insert path into paths.
75
104
This uses a path-wise comparison so we get::
94
123
# Grab the dirname for the current dirblock
96
if _cmp_path_by_dirblock_py(path, cur) < 0:
125
if _cmp_path_by_dirblock(path, cur) < 0:
103
def bisect_dirblock_py(dirblocks, dirname, lo=0, hi=None, cache={}):
132
def bisect_dirblock(dirblocks, dirname, lo=0, hi=None, cache={}):
104
133
"""Return the index where to insert dirname into the dirblocks.
106
135
The return value idx is such that all directories blocks in dirblock[:idx]
157
186
return cmp(path1.split('/'), path2.split('/'))
160
def _cmp_path_by_dirblock_py(path1, path2):
189
def _cmp_path_by_dirblock(path1, path2):
161
190
"""Compare two paths based on what directory they are in.
163
192
This generates a sort order, such that all children of a directory are
201
230
# Remove the last blank entry
202
231
trailing = fields.pop()
203
232
if trailing != '':
204
raise AssertionError("dirstate line %r has trailing garbage: %r"
233
raise errors.DirstateCorrupt(state,
234
'trailing garbage: %r' % (trailing,))
206
235
# consider turning fields into a tuple.
208
237
# skip the first field which is the trailing null from the header.
220
249
field_count = len(fields)
221
250
# this checks our adjustment, and also catches file too short.
222
251
if field_count - cur != expected_field_count:
223
raise AssertionError(
252
raise errors.DirstateCorrupt(state,
224
253
'field count incorrect %s != %s, entry_size=%s, '\
225
254
'num_entries=%s fields=%r' % (
226
255
field_count - cur, expected_field_count, entry_size,