Kiln » Dependencies » Dulwich Read More
Clone URL:  
Pushed to one repository · View In Graph Contained in master, master-1, and master-0

Skip useless lookup.

Changeset c5e578de1969

Parent 5eab0eaf52a5

by Jelmer Vernooij

Changes to 2 files · Browse files at c5e578de1969 Showing diff from parent 5eab0eaf52a5 Diff from another changeset...

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