/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/_groupcompress_py.py

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
from __future__ import absolute_import
24
24
 
25
 
from .. import osutils
26
 
from ..sixish import (
27
 
    indexbytes,
28
 
    int2byte,
29
 
    range,
30
 
    )
 
25
from bzrlib import osutils
31
26
 
32
27
 
33
28
class _OutputHandler(object):
43
38
    def add_copy(self, start_byte, end_byte):
44
39
        # The data stream allows >64kB in a copy, but to match the compiled
45
40
        # code, we will also limit it to a 64kB copy
46
 
        for start_byte in range(start_byte, end_byte, 64*1024):
 
41
        for start_byte in xrange(start_byte, end_byte, 64*1024):
47
42
            num_bytes = min(64*1024, end_byte - start_byte)
48
43
            copy_bytes = encode_copy_instruction(start_byte, num_bytes)
49
44
            self.out_lines.append(copy_bytes)
55
50
        if self.cur_insert_len > 127:
56
51
            raise AssertionError('We cannot insert more than 127 bytes'
57
52
                                 ' at a time.')
58
 
        self.out_lines.append(int2byte(self.cur_insert_len))
 
53
        self.out_lines.append(chr(self.cur_insert_len))
59
54
        self.index_lines.append(False)
60
55
        self.out_lines.extend(self.cur_insert_lines)
61
56
        if self.cur_insert_len < self.min_len_to_index:
69
64
        # Flush out anything pending
70
65
        self._flush_insert()
71
66
        line_len = len(line)
72
 
        for start_index in range(0, line_len, 127):
 
67
        for start_index in xrange(0, line_len, 127):
73
68
            next_len = min(127, line_len - start_index)
74
 
            self.out_lines.append(int2byte(next_len))
 
69
            self.out_lines.append(chr(next_len))
75
70
            self.index_lines.append(False)
76
71
            self.out_lines.append(line[start_index:start_index+next_len])
77
72
            # We don't index long lines, because we won't be able to match
132
127
            try:
133
128
                matches[line].add(start_idx + idx)
134
129
            except KeyError:
135
 
                matches[line] = {start_idx + idx}
 
130
                matches[line] = set([start_idx + idx])
136
131
 
137
132
    def get_matches(self, line):
138
133
        """Return the lines which match the line in right."""
258
253
    def _flush_insert(self, start_linenum, end_linenum,
259
254
                      new_lines, out_lines, index_lines):
260
255
        """Add an 'insert' request to the data stream."""
261
 
        bytes_to_insert = b''.join(new_lines[start_linenum:end_linenum])
 
256
        bytes_to_insert = ''.join(new_lines[start_linenum:end_linenum])
262
257
        insert_length = len(bytes_to_insert)
263
258
        # Each insert instruction is at most 127 bytes long
264
 
        for start_byte in range(0, insert_length, 127):
 
259
        for start_byte in xrange(0, insert_length, 127):
265
260
            insert_count = min(insert_length - start_byte, 127)
266
 
            out_lines.append(int2byte(insert_count))
 
261
            out_lines.append(chr(insert_count))
267
262
            # Don't index the 'insert' instruction
268
263
            index_lines.append(False)
269
264
            insert = bytes_to_insert[start_byte:start_byte+insert_count]
281
276
        num_bytes = stop_byte - first_byte
282
277
        # The data stream allows >64kB in a copy, but to match the compiled
283
278
        # code, we will also limit it to a 64kB copy
284
 
        for start_byte in range(first_byte, stop_byte, 64*1024):
 
279
        for start_byte in xrange(first_byte, stop_byte, 64*1024):
285
280
            num_bytes = min(64*1024, stop_byte - start_byte)
286
281
            copy_bytes = encode_copy_instruction(start_byte, num_bytes)
287
282
            out_lines.append(copy_bytes)
292
287
        if bytes_length is None:
293
288
            bytes_length = sum(map(len, new_lines))
294
289
        # reserved for content type, content length
295
 
        out_lines = [b'', b'', encode_base128_int(bytes_length)]
 
290
        out_lines = ['', '', encode_base128_int(bytes_length)]
296
291
        index_lines = [False, False, False]
297
292
        output_handler = _OutputHandler(out_lines, index_lines,
298
293
                                        self._MIN_MATCH_BYTES)
318
313
 
319
314
def encode_base128_int(val):
320
315
    """Convert an integer into a 7-bit lsb encoding."""
321
 
    data = bytearray()
 
316
    bytes = []
322
317
    count = 0
323
318
    while val >= 0x80:
324
 
        data.append((val | 0x80) & 0xFF)
 
319
        bytes.append(chr((val | 0x80) & 0xFF))
325
320
        val >>= 7
326
 
    data.append(val)
327
 
    return bytes(data)
328
 
 
329
 
 
330
 
def decode_base128_int(data):
 
321
    bytes.append(chr(val))
 
322
    return ''.join(bytes)
 
323
 
 
324
 
 
325
def decode_base128_int(bytes):
331
326
    """Decode an integer from a 7-bit lsb encoding."""
332
327
    offset = 0
333
328
    val = 0
334
329
    shift = 0
335
 
    bval = indexbytes(data, offset)
 
330
    bval = ord(bytes[offset])
336
331
    while bval >= 0x80:
337
332
        val |= (bval & 0x7F) << shift
338
333
        shift += 7
339
334
        offset += 1
340
 
        bval = indexbytes(data, offset)
 
335
        bval = ord(bytes[offset])
341
336
    val |= bval << shift
342
337
    offset += 1
343
338
    return val, offset
352
347
        base_byte = offset & 0xff
353
348
        if base_byte:
354
349
            copy_command |= copy_bit
355
 
            copy_bytes.append(int2byte(base_byte))
 
350
            copy_bytes.append(chr(base_byte))
356
351
        offset >>= 8
357
352
    if length is None:
358
353
        raise ValueError("cannot supply a length of None")
367
362
            base_byte = length & 0xff
368
363
            if base_byte:
369
364
                copy_command |= copy_bit
370
 
                copy_bytes.append(int2byte(base_byte))
 
365
                copy_bytes.append(chr(base_byte))
371
366
            length >>= 8
372
 
    copy_bytes[0] = int2byte(copy_command)
373
 
    return b''.join(copy_bytes)
 
367
    copy_bytes[0] = chr(copy_command)
 
368
    return ''.join(copy_bytes)
374
369
 
375
370
 
376
371
def decode_copy_instruction(bytes, cmd, pos):
392
387
    offset = 0
393
388
    length = 0
394
389
    if (cmd & 0x01):
395
 
        offset = indexbytes(bytes, pos)
 
390
        offset = ord(bytes[pos])
396
391
        pos += 1
397
392
    if (cmd & 0x02):
398
 
        offset = offset | (indexbytes(bytes, pos) << 8)
 
393
        offset = offset | (ord(bytes[pos]) << 8)
399
394
        pos += 1
400
395
    if (cmd & 0x04):
401
 
        offset = offset | (indexbytes(bytes, pos) << 16)
 
396
        offset = offset | (ord(bytes[pos]) << 16)
402
397
        pos += 1
403
398
    if (cmd & 0x08):
404
 
        offset = offset | (indexbytes(bytes, pos) << 24)
 
399
        offset = offset | (ord(bytes[pos]) << 24)
405
400
        pos += 1
406
401
    if (cmd & 0x10):
407
 
        length = indexbytes(bytes, pos)
 
402
        length = ord(bytes[pos])
408
403
        pos += 1
409
404
    if (cmd & 0x20):
410
 
        length = length | (indexbytes(bytes, pos) << 8)
 
405
        length = length | (ord(bytes[pos]) << 8)
411
406
        pos += 1
412
407
    if (cmd & 0x40):
413
 
        length = length | (indexbytes(bytes, pos) << 16)
 
408
        length = length | (ord(bytes[pos]) << 16)
414
409
        pos += 1
415
410
    if length == 0:
416
411
        length = 65536
419
414
 
420
415
def make_delta(source_bytes, target_bytes):
421
416
    """Create a delta from source to target."""
422
 
    if not isinstance(source_bytes, bytes):
423
 
        raise TypeError('source is not bytes')
424
 
    if not isinstance(target_bytes, bytes):
425
 
        raise TypeError('target is not bytes')
 
417
    if type(source_bytes) is not str:
 
418
        raise TypeError('source is not a str')
 
419
    if type(target_bytes) is not str:
 
420
        raise TypeError('target is not a str')
426
421
    line_locations = LinesDeltaIndex(osutils.split_lines(source_bytes))
427
422
    delta, _ = line_locations.make_delta(osutils.split_lines(target_bytes),
428
423
                                         bytes_length=len(target_bytes))
429
 
    return b''.join(delta)
 
424
    return ''.join(delta)
430
425
 
431
426
 
432
427
def apply_delta(basis, delta):
433
428
    """Apply delta to this object to become new_version_id."""
434
 
    if not isinstance(basis, bytes):
435
 
        raise TypeError('basis is not bytes')
436
 
    if not isinstance(delta, bytes):
437
 
        raise TypeError('delta is not bytes')
 
429
    if type(basis) is not str:
 
430
        raise TypeError('basis is not a str')
 
431
    if type(delta) is not str:
 
432
        raise TypeError('delta is not a str')
438
433
    target_length, pos = decode_base128_int(delta)
439
434
    lines = []
440
435
    len_delta = len(delta)
441
436
    while pos < len_delta:
442
 
        cmd = indexbytes(delta, pos)
 
437
        cmd = ord(delta[pos])
443
438
        pos += 1
444
439
        if cmd & 0x80:
445
440
            offset, length, pos = decode_copy_instruction(delta, cmd, pos)
453
448
                raise ValueError('Command == 0 not supported yet')
454
449
            lines.append(delta[pos:pos+cmd])
455
450
            pos += cmd
456
 
    data = b''.join(lines)
457
 
    if len(data) != target_length:
 
451
    bytes = ''.join(lines)
 
452
    if len(bytes) != target_length:
458
453
        raise ValueError('Delta claimed to be %d long, but ended up'
459
454
                         ' %d long' % (target_length, len(bytes)))
460
 
    return data
 
455
    return bytes
461
456
 
462
457
 
463
458
def apply_delta_to_source(source, delta_start, delta_end):