~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_dirstate_helpers_py.py

  • Committer: John Arbash Meinel
  • Date: 2008-07-09 21:42:24 UTC
  • mto: This revision was merged to the branch mainline in revision 3543.
  • Revision ID: john@arbash-meinel.com-20080709214224-r75k87r6a01pfc3h
Restore a real weave merge to 'bzr merge --weave'.

To do so efficiently, we only add the simple LCAs to the final weave
object, unless we run into complexities with the merge graph.
This gives the same effective result as adding all the texts,
with the advantage of not having to extract all of them.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007, 2008 Canonical Ltd
 
1
# Copyright (C) 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
12
12
#
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
16
16
 
17
17
"""Python implementations of Dirstate Helper functions."""
18
18
 
19
 
from __future__ import absolute_import
20
 
 
21
 
import binascii
22
19
import os
23
 
import struct
24
20
 
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 bzrlib import errors
28
23
from bzrlib.dirstate import DirState
29
24
 
30
25
 
31
 
def pack_stat(st, _b64=binascii.b2a_base64, _pack=struct.Struct('>6L').pack):
32
 
    """Convert stat values into a packed representation
33
 
 
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.
37
 
    """
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]
42
 
 
43
 
 
44
 
def _unpack_stat(packed_stat):
45
 
    """Turn a packed_stat back into the stat fields.
46
 
 
47
 
    This is meant as a debugging tool, should not be used in real code.
48
 
    """
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)
53
 
 
54
 
 
55
 
def _bisect_path_left(paths, path):
 
26
def _bisect_path_left_py(paths, path):
56
27
    """Return the index where to insert path into paths.
57
28
 
58
29
    This uses the dirblock sorting. So all children in a directory come before
91
62
        mid = (lo + hi) // 2
92
63
        # Grab the dirname for the current dirblock
93
64
        cur = paths[mid]
94
 
        if _cmp_path_by_dirblock(cur, path) < 0:
 
65
        if _cmp_path_by_dirblock_py(cur, path) < 0:
95
66
            lo = mid + 1
96
67
        else:
97
68
            hi = mid
98
69
    return lo
99
70
 
100
71
 
101
 
def _bisect_path_right(paths, path):
 
72
def _bisect_path_right_py(paths, path):
102
73
    """Return the index where to insert path into paths.
103
74
 
104
75
    This uses a path-wise comparison so we get::
122
93
        mid = (lo+hi)//2
123
94
        # Grab the dirname for the current dirblock
124
95
        cur = paths[mid]
125
 
        if _cmp_path_by_dirblock(path, cur) < 0:
 
96
        if _cmp_path_by_dirblock_py(path, cur) < 0:
126
97
            hi = mid
127
98
        else:
128
99
            lo = mid + 1
129
100
    return lo
130
101
 
131
102
 
132
 
def bisect_dirblock(dirblocks, dirname, lo=0, hi=None, cache={}):
 
103
def bisect_dirblock_py(dirblocks, dirname, lo=0, hi=None, cache={}):
133
104
    """Return the index where to insert dirname into the dirblocks.
134
105
 
135
106
    The return value idx is such that all directories blocks in dirblock[:idx]
160
131
    return lo
161
132
 
162
133
 
163
 
def cmp_by_dirs(path1, path2):
 
134
def cmp_by_dirs_py(path1, path2):
164
135
    """Compare two paths directory by directory.
165
136
 
166
137
    This is equivalent to doing::
186
157
    return cmp(path1.split('/'), path2.split('/'))
187
158
 
188
159
 
189
 
def _cmp_path_by_dirblock(path1, path2):
 
160
def _cmp_path_by_dirblock_py(path1, path2):
190
161
    """Compare two paths based on what directory they are in.
191
162
 
192
163
    This generates a sort order, such that all children of a directory are
212
183
    return cmp(key1, key2)
213
184
 
214
185
 
215
 
def _read_dirblocks(state):
 
186
def _read_dirblocks_py(state):
216
187
    """Read in the dirblocks for the given DirState object.
217
188
 
218
189
    This is tightly bound to the DirState internal representation. It should be
230
201
    # Remove the last blank entry
231
202
    trailing = fields.pop()
232
203
    if trailing != '':
233
 
        raise errors.DirstateCorrupt(state,
234
 
            'trailing garbage: %r' % (trailing,))
 
204
        raise AssertionError("dirstate line %r has trailing garbage: %r" 
 
205
            % (trailing,))
235
206
    # consider turning fields into a tuple.
236
207
 
237
208
    # skip the first field which is the trailing null from the header.
249
220
    field_count = len(fields)
250
221
    # this checks our adjustment, and also catches file too short.
251
222
    if field_count - cur != expected_field_count:
252
 
        raise errors.DirstateCorrupt(state,
 
223
        raise AssertionError(
253
224
            'field count incorrect %s != %s, entry_size=%s, '\
254
225
            'num_entries=%s fields=%r' % (
255
226
            field_count - cur, expected_field_count, entry_size,
317
288
    # To convert from format 3 => format 2
318
289
    # state._dirblocks = sorted(state._dirblocks)
319
290
    state._dirblock_state = DirState.IN_MEMORY_UNMODIFIED
 
291