/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 dulwich/dulwich/pack.py

Merge changes, open index.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# pack.py -- For dealing wih packed git objects.
 
2
# Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
 
3
# Copryight (C) 2008 Jelmer Vernooij <jelmer@samba.org>
 
4
# The code is loosely based on that in the sha1_file.c file from git itself,
 
5
# which is Copyright (C) Linus Torvalds, 2005 and distributed under the
 
6
# GPL version 2.
 
7
 
8
# This program is free software; you can redistribute it and/or
 
9
# modify it under the terms of the GNU General Public License
 
10
# as published by the Free Software Foundation; version 2
 
11
# of the License.
 
12
 
13
# This program is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
# GNU General Public License for more details.
 
17
 
18
# You should have received a copy of the GNU General Public License
 
19
# along with this program; if not, write to the Free Software
 
20
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 
21
# MA  02110-1301, USA.
 
22
 
 
23
"""Classes for dealing with packed git objects.
 
24
 
 
25
A pack is a compact representation of a bunch of objects, stored
 
26
using deltas where possible.
 
27
 
 
28
They have two parts, the pack file, which stores the data, and an index
 
29
that tells you where the data is.
 
30
 
 
31
To find an object you look in all of the index files 'til you find a
 
32
match for the object name. You then use the pointer got from this as
 
33
a pointer in to the corresponding packfile.
 
34
"""
 
35
 
 
36
from collections import defaultdict
 
37
import hashlib
 
38
from itertools import imap, izip
 
39
import mmap
 
40
import os
 
41
import sha
 
42
import struct
 
43
import sys
 
44
import zlib
 
45
 
 
46
from objects import (
 
47
        ShaFile,
 
48
        )
 
49
from errors import ApplyDeltaError
 
50
 
 
51
supports_mmap_offset = (sys.version_info[0] >= 3 or 
 
52
        (sys.version_info[0] == 2 and sys.version_info[1] >= 6))
 
53
 
 
54
 
 
55
def take_msb_bytes(map, offset):
 
56
    ret = []
 
57
    while len(ret) == 0 or ret[-1] & 0x80:
 
58
        ret.append(ord(map[offset]))
 
59
        offset += 1
 
60
    return ret
 
61
 
 
62
 
 
63
def read_zlib(data, offset, dec_size):
 
64
    obj = zlib.decompressobj()
 
65
    x = ""
 
66
    fed = 0
 
67
    while obj.unused_data == "":
 
68
        base = offset+fed
 
69
        add = data[base:base+1024]
 
70
        fed += len(add)
 
71
        x += obj.decompress(add)
 
72
    assert len(x) == dec_size
 
73
    comp_len = fed-len(obj.unused_data)
 
74
    return x, comp_len
 
75
 
 
76
 
 
77
def iter_sha1(iter):
 
78
    sha = hashlib.sha1()
 
79
    for name in iter:
 
80
        sha.update(name)
 
81
    return sha.hexdigest()
 
82
 
 
83
 
 
84
def hex_to_sha(hex):
 
85
  """Convert a hex string to a binary sha string."""
 
86
  ret = ""
 
87
  for i in range(0, len(hex), 2):
 
88
    ret += chr(int(hex[i:i+2], 16))
 
89
  return ret
 
90
 
 
91
 
 
92
def sha_to_hex(sha):
 
93
  """Convert a binary sha string to a hex sha string."""
 
94
  ret = ""
 
95
  for i in sha:
 
96
      ret += "%02x" % ord(i)
 
97
  return ret
 
98
 
 
99
 
 
100
MAX_MMAP_SIZE = 256 * 1024 * 1024
 
101
 
 
102
def simple_mmap(f, offset, size, access=mmap.ACCESS_READ):
 
103
    """Simple wrapper for mmap() which always supports the offset parameter.
 
104
 
 
105
    :param f: File object.
 
106
    :param offset: Offset in the file, from the beginning of the file.
 
107
    :param size: Size of the mmap'ed area
 
108
    :param access: Access mechanism.
 
109
    :return: MMAP'd area.
 
110
    """
 
111
    if offset+size > MAX_MMAP_SIZE and not supports_mmap_offset:
 
112
        raise AssertionError("%s is larger than 256 meg, and this version "
 
113
            "of Python does not support the offset argument to mmap().")
 
114
    if supports_mmap_offset:
 
115
        return mmap.mmap(f.fileno(), size, access=access, offset=offset)
 
116
    else:
 
117
        class ArraySkipper(object):
 
118
 
 
119
            def __init__(self, array, offset):
 
120
                self.array = array
 
121
                self.offset = offset
 
122
 
 
123
            def __getslice__(self, i, j):
 
124
                return self.array[i+self.offset:j+self.offset]
 
125
 
 
126
            def __getitem__(self, i):
 
127
                return self.array[i+self.offset]
 
128
 
 
129
            def __len__(self):
 
130
                return len(self.array) - self.offset
 
131
 
 
132
            def __str__(self):
 
133
                return str(self.array[self.offset:])
 
134
 
 
135
        mem = mmap.mmap(f.fileno(), size+offset, access=access)
 
136
        if offset == 0:
 
137
            return mem
 
138
        return ArraySkipper(mem, offset)
 
139
 
 
140
 
 
141
def resolve_object(offset, type, obj, get_ref, get_offset):
 
142
  """Resolve an object, possibly resolving deltas when necessary."""
 
143
  if not type in (6, 7): # Not a delta
 
144
     return type, obj
 
145
 
 
146
  if type == 6: # offset delta
 
147
     (delta_offset, delta) = obj
 
148
     assert isinstance(delta_offset, int)
 
149
     assert isinstance(delta, str)
 
150
     offset = offset-delta_offset
 
151
     type, base_obj = get_offset(offset)
 
152
     assert isinstance(type, int)
 
153
  elif type == 7: # ref delta
 
154
     (basename, delta) = obj
 
155
     assert isinstance(basename, str) and len(basename) == 20
 
156
     assert isinstance(delta, str)
 
157
     type, base_obj = get_ref(basename)
 
158
     assert isinstance(type, int)
 
159
  type, base_text = resolve_object(offset, type, base_obj, get_ref, get_offset)
 
160
  return type, apply_delta(base_text, delta)
 
161
 
 
162
 
 
163
class PackIndex(object):
 
164
  """An index in to a packfile.
 
165
 
 
166
  Given a sha id of an object a pack index can tell you the location in the
 
167
  packfile of that object if it has it.
 
168
 
 
169
  To do the loop it opens the file, and indexes first 256 4 byte groups
 
170
  with the first byte of the sha id. The value in the four byte group indexed
 
171
  is the end of the group that shares the same starting byte. Subtract one
 
172
  from the starting byte and index again to find the start of the group.
 
173
  The values are sorted by sha id within the group, so do the math to find
 
174
  the start and end offset and then bisect in to find if the value is present.
 
175
  """
 
176
 
 
177
  def __init__(self, filename):
 
178
    """Create a pack index object.
 
179
 
 
180
    Provide it with the name of the index file to consider, and it will map
 
181
    it whenever required.
 
182
    """
 
183
    self._filename = filename
 
184
    # Take the size now, so it can be checked each time we map the file to
 
185
    # ensure that it hasn't changed.
 
186
    self._size = os.path.getsize(filename)
 
187
    self._file = open(filename, 'r')
 
188
    self._contents = simple_mmap(self._file, 0, self._size)
 
189
    if self._contents[:4] != '\377tOc':
 
190
        self.version = 1
 
191
        self._fan_out_table = self._read_fan_out_table(0)
 
192
    else:
 
193
        (self.version, ) = struct.unpack_from(">L", self._contents, 4)
 
194
        assert self.version in (2,), "Version was %d" % self.version
 
195
        self._fan_out_table = self._read_fan_out_table(8)
 
196
        self._name_table_offset = 8 + 0x100 * 4
 
197
        self._crc32_table_offset = self._name_table_offset + 20 * len(self)
 
198
        self._pack_offset_table_offset = self._crc32_table_offset + 4 * len(self)
 
199
 
 
200
  def __eq__(self, other):
 
201
    if type(self) != type(other):
 
202
        return False
 
203
 
 
204
    if self._fan_out_table != other._fan_out_table:
 
205
        return False
 
206
 
 
207
    for (name1, _, _), (name2, _, _) in izip(self.iterentries(), other.iterentries()):
 
208
        if name1 != name2:
 
209
            return False
 
210
    return True
 
211
 
 
212
  def close(self):
 
213
    self._file.close()
 
214
 
 
215
  def __len__(self):
 
216
    """Return the number of entries in this pack index."""
 
217
    return self._fan_out_table[-1]
 
218
 
 
219
  def _unpack_entry(self, i):
 
220
    """Unpack the i-th entry in the index file.
 
221
 
 
222
    :return: Tuple with object name (SHA), offset in pack file and 
 
223
          CRC32 checksum (if known)."""
 
224
    if self.version == 1:
 
225
        (offset, name) = struct.unpack_from(">L20s", self._contents, 
 
226
            (0x100 * 4) + (i * 24))
 
227
        return (name, offset, None)
 
228
    else:
 
229
        return (self._unpack_name(i), self._unpack_offset(i), 
 
230
                self._unpack_crc32_checksum(i))
 
231
 
 
232
  def _unpack_name(self, i):
 
233
    if self.version == 1:
 
234
        return self._unpack_entry(i)[0]
 
235
    else:
 
236
        return struct.unpack_from("20s", self._contents, 
 
237
                                  self._name_table_offset + i * 20)[0]
 
238
 
 
239
  def _unpack_offset(self, i):
 
240
    if self.version == 1:
 
241
        return self._unpack_entry(i)[1]
 
242
    else:
 
243
        return struct.unpack_from(">L", self._contents, 
 
244
                                  self._pack_offset_table_offset + i * 4)[0]
 
245
 
 
246
  def _unpack_crc32_checksum(self, i):
 
247
    if self.version == 1:
 
248
        return None
 
249
    else:
 
250
        return struct.unpack_from(">L", self._contents, 
 
251
                                  self._crc32_table_offset + i * 4)[0]
 
252
 
 
253
  def __iter__(self):
 
254
      return imap(sha_to_hex, self._itersha())
 
255
 
 
256
  def _itersha(self):
 
257
    for i in range(len(self)):
 
258
        yield self._unpack_name(i)
 
259
 
 
260
  def objects_sha1(self):
 
261
    return iter_sha1(self._itersha())
 
262
 
 
263
  def iterentries(self):
 
264
    """Iterate over the entries in this pack index.
 
265
   
 
266
    Will yield tuples with object name, offset in packfile and crc32 checksum.
 
267
    """
 
268
    for i in range(len(self)):
 
269
        yield self._unpack_entry(i)
 
270
 
 
271
  def _read_fan_out_table(self, start_offset):
 
272
    ret = []
 
273
    for i in range(0x100):
 
274
        ret.append(struct.unpack(">L", self._contents[start_offset+i*4:start_offset+(i+1)*4])[0])
 
275
    return ret
 
276
 
 
277
  def check(self):
 
278
    """Check that the stored checksum matches the actual checksum."""
 
279
    return self.calculate_checksum() == self.get_stored_checksums()[1]
 
280
 
 
281
  def calculate_checksum(self):
 
282
    f = open(self._filename, 'r')
 
283
    try:
 
284
        return hashlib.sha1(self._contents[:-20]).digest()
 
285
    finally:
 
286
        f.close()
 
287
 
 
288
  def get_stored_checksums(self):
 
289
    """Return the SHA1 checksums stored for the corresponding packfile and 
 
290
    this header file itself."""
 
291
    return str(self._contents[-40:-20]), str(self._contents[-20:])
 
292
 
 
293
  def object_index(self, sha):
 
294
    """Return the index in to the corresponding packfile for the object.
 
295
 
 
296
    Given the name of an object it will return the offset that object lives
 
297
    at within the corresponding pack file. If the pack file doesn't have the
 
298
    object then None will be returned.
 
299
    """
 
300
    size = os.path.getsize(self._filename)
 
301
    assert size == self._size, "Pack index %s has changed size, I don't " \
 
302
         "like that" % self._filename
 
303
    if len(sha) == 40:
 
304
        sha = hex_to_sha(sha)
 
305
    return self._object_index(sha)
 
306
 
 
307
  def _object_index(self, sha):
 
308
      """See object_index"""
 
309
      idx = ord(sha[0])
 
310
      if idx == 0:
 
311
          start = 0
 
312
      else:
 
313
          start = self._fan_out_table[idx-1]
 
314
      end = self._fan_out_table[idx]
 
315
      assert start <= end
 
316
      while start <= end:
 
317
        i = (start + end)/2
 
318
        file_sha = self._unpack_name(i)
 
319
        if file_sha < sha:
 
320
          start = i + 1
 
321
        elif file_sha > sha:
 
322
          end = i - 1
 
323
        else:
 
324
          return self._unpack_offset(i)
 
325
      return None
 
326
 
 
327
 
 
328
def read_pack_header(f):
 
329
    header = f.read(12)
 
330
    assert header[:4] == "PACK"
 
331
    (version,) = struct.unpack_from(">L", header, 4)
 
332
    assert version in (2, 3), "Version was %d" % version
 
333
    (num_objects,) = struct.unpack_from(">L", header, 8)
 
334
    return (version, num_objects)
 
335
 
 
336
 
 
337
def read_pack_tail(f):
 
338
    return (f.read(20),)
 
339
 
 
340
 
 
341
def unpack_object(map):
 
342
    bytes = take_msb_bytes(map, 0)
 
343
    type = (bytes[0] >> 4) & 0x07
 
344
    size = bytes[0] & 0x0f
 
345
    for i, byte in enumerate(bytes[1:]):
 
346
      size += (byte & 0x7f) << ((i * 7) + 4)
 
347
    raw_base = len(bytes)
 
348
    if type == 6: # offset delta
 
349
        bytes = take_msb_bytes(map, raw_base)
 
350
        assert not (bytes[-1] & 0x80)
 
351
        delta_base_offset = bytes[0] & 0x7f
 
352
        for byte in bytes[1:]:
 
353
            delta_base_offset += 1
 
354
            delta_base_offset <<= 7
 
355
            delta_base_offset += (byte & 0x7f)
 
356
        raw_base+=len(bytes)
 
357
        uncomp, comp_len = read_zlib(map, raw_base, size)
 
358
        assert size == len(uncomp)
 
359
        return type, (delta_base_offset, uncomp), comp_len+raw_base
 
360
    elif type == 7: # ref delta
 
361
        basename = map[raw_base:raw_base+20]
 
362
        uncomp, comp_len = read_zlib(map, raw_base+20, size)
 
363
        assert size == len(uncomp)
 
364
        return type, (basename, uncomp), comp_len+raw_base+20
 
365
    else:
 
366
        uncomp, comp_len = read_zlib(map, raw_base, size)
 
367
        assert len(uncomp) == size
 
368
        return type, uncomp, comp_len+raw_base
 
369
 
 
370
 
 
371
class PackData(object):
 
372
  """The data contained in a packfile.
 
373
 
 
374
  Pack files can be accessed both sequentially for exploding a pack, and
 
375
  directly with the help of an index to retrieve a specific object.
 
376
 
 
377
  The objects within are either complete or a delta aginst another.
 
378
 
 
379
  The header is variable length. If the MSB of each byte is set then it
 
380
  indicates that the subsequent byte is still part of the header.
 
381
  For the first byte the next MS bits are the type, which tells you the type
 
382
  of object, and whether it is a delta. The LS byte is the lowest bits of the
 
383
  size. For each subsequent byte the LS 7 bits are the next MS bits of the
 
384
  size, i.e. the last byte of the header contains the MS bits of the size.
 
385
 
 
386
  For the complete objects the data is stored as zlib deflated data.
 
387
  The size in the header is the uncompressed object size, so to uncompress
 
388
  you need to just keep feeding data to zlib until you get an object back,
 
389
  or it errors on bad data. This is done here by just giving the complete
 
390
  buffer from the start of the deflated object on. This is bad, but until I
 
391
  get mmap sorted out it will have to do.
 
392
 
 
393
  Currently there are no integrity checks done. Also no attempt is made to try
 
394
  and detect the delta case, or a request for an object at the wrong position.
 
395
  It will all just throw a zlib or KeyError.
 
396
  """
 
397
 
 
398
  def __init__(self, filename):
 
399
    """Create a PackData object that represents the pack in the given filename.
 
400
 
 
401
    The file must exist and stay readable until the object is disposed of. It
 
402
    must also stay the same size. It will be mapped whenever needed.
 
403
 
 
404
    Currently there is a restriction on the size of the pack as the python
 
405
    mmap implementation is flawed.
 
406
    """
 
407
    self._filename = filename
 
408
    assert os.path.exists(filename), "%s is not a packfile" % filename
 
409
    self._size = os.path.getsize(filename)
 
410
    self._header_size = 12
 
411
    assert self._size >= self._header_size, "%s is too small for a packfile" % filename
 
412
    self._read_header()
 
413
 
 
414
  def _read_header(self):
 
415
    f = open(self._filename, 'rb')
 
416
    try:
 
417
        (version, self._num_objects) = \
 
418
                read_pack_header(f)
 
419
        f.seek(self._size-20)
 
420
        (self._stored_checksum,) = read_pack_tail(f)
 
421
    finally:
 
422
        f.close()
 
423
 
 
424
  def __len__(self):
 
425
      """Returns the number of objects in this pack."""
 
426
      return self._num_objects
 
427
 
 
428
  def calculate_checksum(self):
 
429
    f = open(self._filename, 'rb')
 
430
    try:
 
431
        map = simple_mmap(f, 0, self._size)
 
432
        return hashlib.sha1(map[:-20]).digest()
 
433
    finally:
 
434
        f.close()
 
435
 
 
436
  def iterobjects(self):
 
437
    offset = self._header_size
 
438
    f = open(self._filename, 'rb')
 
439
    for i in range(len(self)):
 
440
        map = simple_mmap(f, offset, self._size-offset)
 
441
        (type, obj, total_size) = unpack_object(map)
 
442
        yield offset, type, obj
 
443
        offset += total_size
 
444
    f.close()
 
445
 
 
446
  def iterentries(self, ext_resolve_ref=None):
 
447
    found = {}
 
448
    at = {}
 
449
    postponed = defaultdict(list)
 
450
    class Postpone(Exception):
 
451
        """Raised to postpone delta resolving."""
 
452
        
 
453
    def get_ref_text(sha):
 
454
        if sha in found:
 
455
            return found[sha]
 
456
        if ext_resolve_ref:
 
457
            try:
 
458
                return ext_resolve_ref(sha)
 
459
            except KeyError:
 
460
                pass
 
461
        raise Postpone, (sha, )
 
462
    todo = list(self.iterobjects())
 
463
    while todo:
 
464
      (offset, type, obj) = todo.pop(0)
 
465
      at[offset] = (type, obj)
 
466
      assert isinstance(offset, int)
 
467
      assert isinstance(type, int)
 
468
      assert isinstance(obj, tuple) or isinstance(obj, str)
 
469
      try:
 
470
        type, obj = resolve_object(offset, type, obj, get_ref_text,
 
471
            at.__getitem__)
 
472
      except Postpone, (sha, ):
 
473
        postponed[sha].append((offset, type, obj))
 
474
      else:
 
475
        shafile = ShaFile.from_raw_string(type, obj)
 
476
        sha = shafile.sha().digest()
 
477
        found[sha] = (type, obj)
 
478
        yield sha, offset, shafile.crc32()
 
479
        todo += postponed.get(sha, [])
 
480
    if postponed:
 
481
        raise KeyError([sha_to_hex(h) for h in postponed.keys()])
 
482
 
 
483
  def sorted_entries(self, resolve_ext_ref=None):
 
484
    ret = list(self.iterentries(resolve_ext_ref))
 
485
    ret.sort()
 
486
    return ret
 
487
 
 
488
  def create_index_v1(self, filename):
 
489
    entries = self.sorted_entries()
 
490
    write_pack_index_v1(filename, entries, self.calculate_checksum())
 
491
 
 
492
  def create_index_v2(self, filename):
 
493
    entries = self.sorted_entries()
 
494
    write_pack_index_v2(filename, entries, self.calculate_checksum())
 
495
 
 
496
  def get_stored_checksum(self):
 
497
    return self._stored_checksum
 
498
 
 
499
  def check(self):
 
500
    return (self.calculate_checksum() == self.get_stored_checksum())
 
501
 
 
502
  def get_object_at(self, offset):
 
503
    """Given an offset in to the packfile return the object that is there.
 
504
 
 
505
    Using the associated index the location of an object can be looked up, and
 
506
    then the packfile can be asked directly for that object using this
 
507
    function.
 
508
    """
 
509
    assert isinstance(offset, long) or isinstance(offset, int),\
 
510
            "offset was %r" % offset
 
511
    assert offset >= self._header_size
 
512
    size = os.path.getsize(self._filename)
 
513
    assert size == self._size, "Pack data %s has changed size, I don't " \
 
514
         "like that" % self._filename
 
515
    f = open(self._filename, 'rb')
 
516
    try:
 
517
      map = simple_mmap(f, offset, size-offset)
 
518
      return unpack_object(map)[:2]
 
519
    finally:
 
520
      f.close()
 
521
 
 
522
 
 
523
class SHA1Writer(object):
 
524
    
 
525
    def __init__(self, f):
 
526
        self.f = f
 
527
        self.sha1 = hashlib.sha1("")
 
528
 
 
529
    def write(self, data):
 
530
        self.sha1.update(data)
 
531
        self.f.write(data)
 
532
 
 
533
    def write_sha(self):
 
534
        sha = self.sha1.digest()
 
535
        assert len(sha) == 20
 
536
        self.f.write(sha)
 
537
        return sha
 
538
 
 
539
    def close(self):
 
540
        sha = self.write_sha()
 
541
        self.f.close()
 
542
        return sha
 
543
 
 
544
    def tell(self):
 
545
        return self.f.tell()
 
546
 
 
547
 
 
548
def write_pack_object(f, type, object):
 
549
    """Write pack object to a file.
 
550
 
 
551
    :param f: File to write to
 
552
    :param o: Object to write
 
553
    """
 
554
    ret = f.tell()
 
555
    if type == 6: # ref delta
 
556
        (delta_base_offset, object) = object
 
557
    elif type == 7: # offset delta
 
558
        (basename, object) = object
 
559
    size = len(object)
 
560
    c = (type << 4) | (size & 15)
 
561
    size >>= 4
 
562
    while size:
 
563
        f.write(chr(c | 0x80))
 
564
        c = size & 0x7f
 
565
        size >>= 7
 
566
    f.write(chr(c))
 
567
    if type == 6: # offset delta
 
568
        ret = [delta_base_offset & 0x7f]
 
569
        delta_base_offset >>= 7
 
570
        while delta_base_offset:
 
571
            delta_base_offset -= 1
 
572
            ret.insert(0, 0x80 | (delta_base_offset & 0x7f))
 
573
            delta_base_offset >>= 7
 
574
        f.write("".join([chr(x) for x in ret]))
 
575
    elif type == 7: # ref delta
 
576
        assert len(basename) == 20
 
577
        f.write(basename)
 
578
    f.write(zlib.compress(object))
 
579
    return f.tell()
 
580
 
 
581
 
 
582
def write_pack(filename, objects, num_objects):
 
583
    f = open(filename + ".pack", 'w')
 
584
    try:
 
585
        entries, data_sum = write_pack_data(f, objects, num_objects)
 
586
    except:
 
587
        f.close()
 
588
    entries.sort()
 
589
    write_pack_index_v2(filename + ".idx", entries, data_sum)
 
590
 
 
591
 
 
592
def write_pack_data(f, objects, num_objects):
 
593
    """Write a new pack file.
 
594
 
 
595
    :param filename: The filename of the new pack file.
 
596
    :param objects: List of objects to write.
 
597
    :return: List with (name, offset, crc32 checksum) entries, pack checksum
 
598
    """
 
599
    entries = []
 
600
    f = SHA1Writer(f)
 
601
    f.write("PACK")               # Pack header
 
602
    f.write(struct.pack(">L", 2)) # Pack version
 
603
    f.write(struct.pack(">L", num_objects)) # Number of objects in pack
 
604
    for o in objects:
 
605
        sha1 = o.sha().digest()
 
606
        crc32 = o.crc32()
 
607
        # FIXME: Delta !
 
608
        t, o = o.as_raw_string()
 
609
        offset = write_pack_object(f, t, o)
 
610
        entries.append((sha1, offset, crc32))
 
611
    return entries, f.write_sha()
 
612
 
 
613
 
 
614
def write_pack_index_v1(filename, entries, pack_checksum):
 
615
    """Write a new pack index file.
 
616
 
 
617
    :param filename: The filename of the new pack index file.
 
618
    :param entries: List of tuples with object name (sha), offset_in_pack,  and
 
619
            crc32_checksum.
 
620
    :param pack_checksum: Checksum of the pack file.
 
621
    """
 
622
    f = open(filename, 'w')
 
623
    f = SHA1Writer(f)
 
624
    fan_out_table = defaultdict(lambda: 0)
 
625
    for (name, offset, entry_checksum) in entries:
 
626
        fan_out_table[ord(name[0])] += 1
 
627
    # Fan-out table
 
628
    for i in range(0x100):
 
629
        f.write(struct.pack(">L", fan_out_table[i]))
 
630
        fan_out_table[i+1] += fan_out_table[i]
 
631
    for (name, offset, entry_checksum) in entries:
 
632
        f.write(struct.pack(">L20s", offset, name))
 
633
    assert len(pack_checksum) == 20
 
634
    f.write(pack_checksum)
 
635
    f.close()
 
636
 
 
637
 
 
638
def apply_delta(src_buf, delta):
 
639
    """Based on the similar function in git's patch-delta.c."""
 
640
    assert isinstance(src_buf, str), "was %r" % (src_buf,)
 
641
    assert isinstance(delta, str)
 
642
    out = ""
 
643
    def pop(delta):
 
644
        ret = delta[0]
 
645
        delta = delta[1:]
 
646
        return ord(ret), delta
 
647
    def get_delta_header_size(delta):
 
648
        size = 0
 
649
        i = 0
 
650
        while delta:
 
651
            cmd, delta = pop(delta)
 
652
            size |= (cmd & ~0x80) << i
 
653
            i += 7
 
654
            if not cmd & 0x80:
 
655
                break
 
656
        return size, delta
 
657
    src_size, delta = get_delta_header_size(delta)
 
658
    dest_size, delta = get_delta_header_size(delta)
 
659
    assert src_size == len(src_buf)
 
660
    while delta:
 
661
        cmd, delta = pop(delta)
 
662
        if cmd & 0x80:
 
663
            cp_off = 0
 
664
            for i in range(4):
 
665
                if cmd & (1 << i): 
 
666
                    x, delta = pop(delta)
 
667
                    cp_off |= x << (i * 8)
 
668
            cp_size = 0
 
669
            for i in range(3):
 
670
                if cmd & (1 << (4+i)): 
 
671
                    x, delta = pop(delta)
 
672
                    cp_size |= x << (i * 8)
 
673
            if cp_size == 0: 
 
674
                cp_size = 0x10000
 
675
            if (cp_off + cp_size < cp_size or
 
676
                cp_off + cp_size > src_size or
 
677
                cp_size > dest_size):
 
678
                break
 
679
            out += src_buf[cp_off:cp_off+cp_size]
 
680
        elif cmd != 0:
 
681
            out += delta[:cmd]
 
682
            delta = delta[cmd:]
 
683
        else:
 
684
            raise ApplyDeltaError("Invalid opcode 0")
 
685
    
 
686
    if delta != "":
 
687
        raise ApplyDeltaError("delta not empty: %r" % delta)
 
688
 
 
689
    if dest_size != len(out):
 
690
        raise ApplyDeltaError("dest size incorrect")
 
691
 
 
692
    return out
 
693
 
 
694
 
 
695
def write_pack_index_v2(filename, entries, pack_checksum):
 
696
    """Write a new pack index file.
 
697
 
 
698
    :param filename: The filename of the new pack index file.
 
699
    :param entries: List of tuples with object name (sha), offset_in_pack,  and
 
700
            crc32_checksum.
 
701
    :param pack_checksum: Checksum of the pack file.
 
702
    """
 
703
    f = open(filename, 'w')
 
704
    f = SHA1Writer(f)
 
705
    f.write('\377tOc') # Magic!
 
706
    f.write(struct.pack(">L", 2))
 
707
    fan_out_table = defaultdict(lambda: 0)
 
708
    for (name, offset, entry_checksum) in entries:
 
709
        fan_out_table[ord(name[0])] += 1
 
710
    # Fan-out table
 
711
    for i in range(0x100):
 
712
        f.write(struct.pack(">L", fan_out_table[i]))
 
713
        fan_out_table[i+1] += fan_out_table[i]
 
714
    for (name, offset, entry_checksum) in entries:
 
715
        f.write(name)
 
716
    for (name, offset, entry_checksum) in entries:
 
717
        f.write(struct.pack(">l", entry_checksum))
 
718
    for (name, offset, entry_checksum) in entries:
 
719
        # FIXME: handle if MSBit is set in offset
 
720
        f.write(struct.pack(">L", offset))
 
721
    # FIXME: handle table for pack files > 8 Gb
 
722
    assert len(pack_checksum) == 20
 
723
    f.write(pack_checksum)
 
724
    f.close()
 
725
 
 
726
 
 
727
class Pack(object):
 
728
 
 
729
    def __init__(self, basename):
 
730
        self._basename = basename
 
731
        self._data_path = self._basename + ".pack"
 
732
        self._idx_path = self._basename + ".idx"
 
733
        self._data = None
 
734
        self._idx = None
 
735
 
 
736
    def name(self):
 
737
        return self.idx.objects_sha1()
 
738
 
 
739
    @property
 
740
    def data(self):
 
741
        if self._data is None:
 
742
            self._data = PackData(self._data_path)
 
743
            assert len(self.idx) == len(self._data)
 
744
            assert self.idx.get_stored_checksums()[0] == self._data.get_stored_checksum()
 
745
        return self._data
 
746
 
 
747
    @property
 
748
    def idx(self):
 
749
        if self._idx is None:
 
750
            self._idx = PackIndex(self._idx_path)
 
751
        return self._idx
 
752
 
 
753
    def close(self):
 
754
        if self._data is not None:
 
755
            self._data.close()
 
756
        self.idx.close()
 
757
 
 
758
    def __eq__(self, other):
 
759
        return type(self) == type(other) and self.idx == other.idx
 
760
 
 
761
    def __len__(self):
 
762
        """Number of entries in this pack."""
 
763
        return len(self.idx)
 
764
 
 
765
    def __repr__(self):
 
766
        return "Pack(%r)" % self._basename
 
767
 
 
768
    def __iter__(self):
 
769
        """Iterate over all the sha1s of the objects in this pack."""
 
770
        return iter(self.idx)
 
771
 
 
772
    def check(self):
 
773
        return self.idx.check() and self.data.check()
 
774
 
 
775
    def get_stored_checksum(self):
 
776
        return self.data.get_stored_checksum()
 
777
 
 
778
    def __contains__(self, sha1):
 
779
        """Check whether this pack contains a particular SHA1."""
 
780
        return (self.idx.object_index(sha1) is not None)
 
781
 
 
782
    def get_raw(self, sha1, resolve_ref=None):
 
783
        if resolve_ref is None:
 
784
            resolve_ref = self.get_raw
 
785
        offset = self.idx.object_index(sha1)
 
786
        if offset is None:
 
787
            raise KeyError(sha1)
 
788
 
 
789
        type, obj = self.data.get_object_at(offset)
 
790
        assert isinstance(offset, int)
 
791
        return resolve_object(offset, type, obj, resolve_ref,
 
792
            self.data.get_object_at)
 
793
 
 
794
    def __getitem__(self, sha1):
 
795
        """Retrieve the specified SHA1."""
 
796
        type, uncomp = self.get_raw(sha1)
 
797
        return ShaFile.from_raw_string(type, uncomp)
 
798
 
 
799
    def iterobjects(self):
 
800
        for offset, type, obj in self.data.iterobjects():
 
801
            assert isinstance(offset, int)
 
802
            yield ShaFile.from_raw_string(
 
803
                    *resolve_object(offset, type, obj, self.get_raw, 
 
804
                self.data.get_object_at))
 
805
 
 
806
 
 
807
def load_packs(path):
 
808
    if not os.path.exists(path):
 
809
        return
 
810
    for name in os.listdir(path):
 
811
        if name.startswith("pack-") and name.endswith(".pack"):
 
812
            yield Pack(os.path.join(path, name[:-len(".pack")]))
 
813