forked from libgit2/libgit2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpack-objects.c
1344 lines (1108 loc) · 30.4 KB
/
pack-objects.c
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
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "pack-objects.h"
#include "compress.h"
#include "delta.h"
#include "iterator.h"
#include "netops.h"
#include "pack.h"
#include "thread-utils.h"
#include "tree.h"
#include "git2/pack.h"
#include "git2/commit.h"
#include "git2/tag.h"
#include "git2/indexer.h"
#include "git2/config.h"
GIT__USE_OIDMAP;
struct unpacked {
git_pobject *object;
void *data;
struct git_delta_index *index;
unsigned int depth;
};
struct tree_walk_context {
git_packbuilder *pb;
git_buf buf;
};
#ifdef GIT_THREADS
#define GIT_PACKBUILDER__MUTEX_OP(pb, mtx, op) do { \
int result = git_mutex_##op(&(pb)->mtx); \
assert(!result); \
GIT_UNUSED(result); \
} while (0)
#else
#define GIT_PACKBUILDER__MUTEX_OP(pb,mtx,op) GIT_UNUSED(pb)
#endif /* GIT_THREADS */
#define git_packbuilder__cache_lock(pb) GIT_PACKBUILDER__MUTEX_OP(pb, cache_mutex, lock)
#define git_packbuilder__cache_unlock(pb) GIT_PACKBUILDER__MUTEX_OP(pb, cache_mutex, unlock)
#define git_packbuilder__progress_lock(pb) GIT_PACKBUILDER__MUTEX_OP(pb, progress_mutex, lock)
#define git_packbuilder__progress_unlock(pb) GIT_PACKBUILDER__MUTEX_OP(pb, progress_mutex, unlock)
static unsigned name_hash(const char *name)
{
unsigned c, hash = 0;
if (!name)
return 0;
/*
* This effectively just creates a sortable number from the
* last sixteen non-whitespace characters. Last characters
* count "most", so things that end in ".c" sort together.
*/
while ((c = *name++) != 0) {
if (git__isspace(c))
continue;
hash = (hash >> 2) + (c << 24);
}
return hash;
}
static int packbuilder_config(git_packbuilder *pb)
{
git_config *config;
int ret;
int64_t val;
if (git_repository_config__weakptr(&config, pb->repo) < 0)
return -1;
#define config_get(KEY,DST,DFLT) do { \
ret = git_config_get_int64(&val, config, KEY); \
if (!ret) (DST) = val; \
else if (ret == GIT_ENOTFOUND) (DST) = (DFLT); \
else if (ret < 0) return -1; } while (0)
config_get("pack.deltaCacheSize", pb->max_delta_cache_size,
GIT_PACK_DELTA_CACHE_SIZE);
config_get("pack.deltaCacheLimit", pb->cache_max_small_delta_size,
GIT_PACK_DELTA_CACHE_LIMIT);
config_get("pack.deltaCacheSize", pb->big_file_threshold,
GIT_PACK_BIG_FILE_THRESHOLD);
config_get("pack.windowMemory", pb->window_memory_limit, 0);
#undef config_get
return 0;
}
int git_packbuilder_new(git_packbuilder **out, git_repository *repo)
{
git_packbuilder *pb;
*out = NULL;
pb = git__calloc(1, sizeof(*pb));
GITERR_CHECK_ALLOC(pb);
pb->object_ix = git_oidmap_alloc();
if (!pb->object_ix)
goto on_error;
pb->repo = repo;
pb->nr_threads = 1; /* do not spawn any thread by default */
if (git_hash_ctx_init(&pb->ctx) < 0 ||
git_repository_odb(&pb->odb, repo) < 0 ||
packbuilder_config(pb) < 0)
goto on_error;
#ifdef GIT_THREADS
if (git_mutex_init(&pb->cache_mutex) ||
git_mutex_init(&pb->progress_mutex) ||
git_cond_init(&pb->progress_cond))
goto on_error;
#endif
*out = pb;
return 0;
on_error:
git_packbuilder_free(pb);
return -1;
}
unsigned int git_packbuilder_set_threads(git_packbuilder *pb, unsigned int n)
{
assert(pb);
#ifdef GIT_THREADS
pb->nr_threads = n;
#else
GIT_UNUSED(n);
assert(1 == pb->nr_threads);
#endif
return pb->nr_threads;
}
static void rehash(git_packbuilder *pb)
{
git_pobject *po;
khiter_t pos;
unsigned int i;
int ret;
kh_clear(oid, pb->object_ix);
for (i = 0, po = pb->object_list; i < pb->nr_objects; i++, po++) {
pos = kh_put(oid, pb->object_ix, &po->id, &ret);
kh_value(pb->object_ix, pos) = po;
}
}
int git_packbuilder_insert(git_packbuilder *pb, const git_oid *oid,
const char *name)
{
git_pobject *po;
khiter_t pos;
int ret;
assert(pb && oid);
/* If the object already exists in the hash table, then we don't
* have any work to do */
pos = kh_get(oid, pb->object_ix, oid);
if (pos != kh_end(pb->object_ix))
return 0;
if (pb->nr_objects >= pb->nr_alloc) {
pb->nr_alloc = (pb->nr_alloc + 1024) * 3 / 2;
pb->object_list = git__realloc(pb->object_list,
pb->nr_alloc * sizeof(*po));
GITERR_CHECK_ALLOC(pb->object_list);
rehash(pb);
}
po = pb->object_list + pb->nr_objects;
memset(po, 0x0, sizeof(*po));
if (git_odb_read_header(&po->size, &po->type, pb->odb, oid) < 0)
return -1;
pb->nr_objects++;
git_oid_cpy(&po->id, oid);
po->hash = name_hash(name);
pos = kh_put(oid, pb->object_ix, &po->id, &ret);
assert(ret != 0);
kh_value(pb->object_ix, pos) = po;
pb->done = false;
return 0;
}
/*
* The per-object header is a pretty dense thing, which is
* - first byte: low four bits are "size",
* then three bits of "type",
* with the high bit being "size continues".
* - each byte afterwards: low seven bits are size continuation,
* with the high bit being "size continues"
*/
static int gen_pack_object_header(
unsigned char *hdr,
unsigned long size,
git_otype type)
{
unsigned char *hdr_base;
unsigned char c;
assert(type >= GIT_OBJ_COMMIT && type <= GIT_OBJ_REF_DELTA);
/* TODO: add support for chunked objects; see git.git 6c0d19b1 */
c = (unsigned char)((type << 4) | (size & 15));
size >>= 4;
hdr_base = hdr;
while (size) {
*hdr++ = c | 0x80;
c = size & 0x7f;
size >>= 7;
}
*hdr++ = c;
return (int)(hdr - hdr_base);
}
static int get_delta(void **out, git_odb *odb, git_pobject *po)
{
git_odb_object *src = NULL, *trg = NULL;
unsigned long delta_size;
void *delta_buf;
*out = NULL;
if (git_odb_read(&src, odb, &po->delta->id) < 0 ||
git_odb_read(&trg, odb, &po->id) < 0)
goto on_error;
delta_buf = git_delta(
git_odb_object_data(src), (unsigned long)git_odb_object_size(src),
git_odb_object_data(trg), (unsigned long)git_odb_object_size(trg),
&delta_size, 0);
if (!delta_buf || delta_size != po->delta_size) {
giterr_set(GITERR_INVALID, "Delta size changed");
goto on_error;
}
*out = delta_buf;
git_odb_object_free(src);
git_odb_object_free(trg);
return 0;
on_error:
git_odb_object_free(src);
git_odb_object_free(trg);
return -1;
}
static int write_object(git_buf *buf, git_packbuilder *pb, git_pobject *po)
{
git_odb_object *obj = NULL;
git_buf zbuf = GIT_BUF_INIT;
git_otype type;
unsigned char hdr[10];
unsigned int hdr_len;
unsigned long size;
void *data;
if (po->delta) {
if (po->delta_data)
data = po->delta_data;
else if (get_delta(&data, pb->odb, po) < 0)
goto on_error;
size = po->delta_size;
type = GIT_OBJ_REF_DELTA;
} else {
if (git_odb_read(&obj, pb->odb, &po->id))
goto on_error;
data = (void *)git_odb_object_data(obj);
size = (unsigned long)git_odb_object_size(obj);
type = git_odb_object_type(obj);
}
/* Write header */
hdr_len = gen_pack_object_header(hdr, size, type);
if (git_buf_put(buf, (char *)hdr, hdr_len) < 0)
goto on_error;
if (git_hash_update(&pb->ctx, hdr, hdr_len) < 0)
goto on_error;
if (type == GIT_OBJ_REF_DELTA) {
if (git_buf_put(buf, (char *)po->delta->id.id, GIT_OID_RAWSZ) < 0 ||
git_hash_update(&pb->ctx, po->delta->id.id, GIT_OID_RAWSZ) < 0)
goto on_error;
}
/* Write data */
if (po->z_delta_size)
size = po->z_delta_size;
else if (git__compress(&zbuf, data, size) < 0)
goto on_error;
else {
if (po->delta)
git__free(data);
data = zbuf.ptr;
size = (unsigned long)zbuf.size;
}
if (git_buf_put(buf, data, size) < 0 ||
git_hash_update(&pb->ctx, data, size) < 0)
goto on_error;
if (po->delta_data)
git__free(po->delta_data);
git_odb_object_free(obj);
git_buf_free(&zbuf);
pb->nr_written++;
return 0;
on_error:
git_odb_object_free(obj);
git_buf_free(&zbuf);
return -1;
}
enum write_one_status {
WRITE_ONE_SKIP = -1, /* already written */
WRITE_ONE_BREAK = 0, /* writing this will bust the limit; not written */
WRITE_ONE_WRITTEN = 1, /* normal */
WRITE_ONE_RECURSIVE = 2 /* already scheduled to be written */
};
static int write_one(git_buf *buf, git_packbuilder *pb, git_pobject *po,
enum write_one_status *status)
{
if (po->recursing) {
*status = WRITE_ONE_RECURSIVE;
return 0;
} else if (po->written) {
*status = WRITE_ONE_SKIP;
return 0;
}
if (po->delta) {
po->recursing = 1;
if (write_one(buf, pb, po->delta, status) < 0)
return -1;
switch (*status) {
case WRITE_ONE_RECURSIVE:
/* we cannot depend on this one */
po->delta = NULL;
break;
default:
break;
}
}
po->written = 1;
po->recursing = 0;
return write_object(buf, pb, po);
}
GIT_INLINE(void) add_to_write_order(git_pobject **wo, unsigned int *endp,
git_pobject *po)
{
if (po->filled)
return;
wo[(*endp)++] = po;
po->filled = 1;
}
static void add_descendants_to_write_order(git_pobject **wo, unsigned int *endp,
git_pobject *po)
{
int add_to_order = 1;
while (po) {
if (add_to_order) {
git_pobject *s;
/* add this node... */
add_to_write_order(wo, endp, po);
/* all its siblings... */
for (s = po->delta_sibling; s; s = s->delta_sibling) {
add_to_write_order(wo, endp, s);
}
}
/* drop down a level to add left subtree nodes if possible */
if (po->delta_child) {
add_to_order = 1;
po = po->delta_child;
} else {
add_to_order = 0;
/* our sibling might have some children, it is next */
if (po->delta_sibling) {
po = po->delta_sibling;
continue;
}
/* go back to our parent node */
po = po->delta;
while (po && !po->delta_sibling) {
/* we're on the right side of a subtree, keep
* going up until we can go right again */
po = po->delta;
}
if (!po) {
/* done- we hit our original root node */
return;
}
/* pass it off to sibling at this level */
po = po->delta_sibling;
}
};
}
static void add_family_to_write_order(git_pobject **wo, unsigned int *endp,
git_pobject *po)
{
git_pobject *root;
for (root = po; root->delta; root = root->delta)
; /* nothing */
add_descendants_to_write_order(wo, endp, root);
}
static int cb_tag_foreach(const char *name, git_oid *oid, void *data)
{
git_packbuilder *pb = data;
git_pobject *po;
khiter_t pos;
GIT_UNUSED(name);
pos = kh_get(oid, pb->object_ix, oid);
if (pos == kh_end(pb->object_ix))
return 0;
po = kh_value(pb->object_ix, pos);
po->tagged = 1;
/* TODO: peel objects */
return 0;
}
static git_pobject **compute_write_order(git_packbuilder *pb)
{
unsigned int i, wo_end, last_untagged;
git_pobject **wo = git__malloc(sizeof(*wo) * pb->nr_objects);
for (i = 0; i < pb->nr_objects; i++) {
git_pobject *po = pb->object_list + i;
po->tagged = 0;
po->filled = 0;
po->delta_child = NULL;
po->delta_sibling = NULL;
}
/*
* Fully connect delta_child/delta_sibling network.
* Make sure delta_sibling is sorted in the original
* recency order.
*/
for (i = pb->nr_objects; i > 0;) {
git_pobject *po = &pb->object_list[--i];
if (!po->delta)
continue;
/* Mark me as the first child */
po->delta_sibling = po->delta->delta_child;
po->delta->delta_child = po;
}
/*
* Mark objects that are at the tip of tags.
*/
if (git_tag_foreach(pb->repo, &cb_tag_foreach, pb) < 0)
return NULL;
/*
* Give the objects in the original recency order until
* we see a tagged tip.
*/
for (i = wo_end = 0; i < pb->nr_objects; i++) {
git_pobject *po = pb->object_list + i;
if (po->tagged)
break;
add_to_write_order(wo, &wo_end, po);
}
last_untagged = i;
/*
* Then fill all the tagged tips.
*/
for (; i < pb->nr_objects; i++) {
git_pobject *po = pb->object_list + i;
if (po->tagged)
add_to_write_order(wo, &wo_end, po);
}
/*
* And then all remaining commits and tags.
*/
for (i = last_untagged; i < pb->nr_objects; i++) {
git_pobject *po = pb->object_list + i;
if (po->type != GIT_OBJ_COMMIT &&
po->type != GIT_OBJ_TAG)
continue;
add_to_write_order(wo, &wo_end, po);
}
/*
* And then all the trees.
*/
for (i = last_untagged; i < pb->nr_objects; i++) {
git_pobject *po = pb->object_list + i;
if (po->type != GIT_OBJ_TREE)
continue;
add_to_write_order(wo, &wo_end, po);
}
/*
* Finally all the rest in really tight order
*/
for (i = last_untagged; i < pb->nr_objects; i++) {
git_pobject *po = pb->object_list + i;
if (!po->filled)
add_family_to_write_order(wo, &wo_end, po);
}
if (wo_end != pb->nr_objects) {
giterr_set(GITERR_INVALID, "invalid write order");
return NULL;
}
return wo;
}
static int write_pack(git_packbuilder *pb,
int (*cb)(void *buf, size_t size, void *data),
void *data)
{
git_pobject **write_order;
git_pobject *po;
git_buf buf = GIT_BUF_INIT;
enum write_one_status status;
struct git_pack_header ph;
unsigned int i = 0;
write_order = compute_write_order(pb);
if (write_order == NULL)
goto on_error;
/* Write pack header */
ph.hdr_signature = htonl(PACK_SIGNATURE);
ph.hdr_version = htonl(PACK_VERSION);
ph.hdr_entries = htonl(pb->nr_objects);
if (cb(&ph, sizeof(ph), data) < 0)
goto on_error;
if (git_hash_update(&pb->ctx, &ph, sizeof(ph)) < 0)
goto on_error;
pb->nr_remaining = pb->nr_objects;
do {
pb->nr_written = 0;
for ( ; i < pb->nr_objects; ++i) {
po = write_order[i];
if (write_one(&buf, pb, po, &status) < 0)
goto on_error;
if (cb(buf.ptr, buf.size, data) < 0)
goto on_error;
git_buf_clear(&buf);
}
pb->nr_remaining -= pb->nr_written;
} while (pb->nr_remaining && i < pb->nr_objects);
git__free(write_order);
git_buf_free(&buf);
if (git_hash_final(&pb->pack_oid, &pb->ctx) < 0)
goto on_error;
return cb(pb->pack_oid.id, GIT_OID_RAWSZ, data);
on_error:
git__free(write_order);
git_buf_free(&buf);
return -1;
}
static int write_pack_buf(void *buf, size_t size, void *data)
{
git_buf *b = (git_buf *)data;
return git_buf_put(b, buf, size);
}
static int write_pack_to_file(void *buf, size_t size, void *data)
{
git_filebuf *file = (git_filebuf *)data;
return git_filebuf_write(file, buf, size);
}
static int write_pack_file(git_packbuilder *pb, const char *path)
{
git_filebuf file = GIT_FILEBUF_INIT;
if (git_filebuf_open(&file, path, 0) < 0 ||
write_pack(pb, &write_pack_to_file, &file) < 0 ||
git_filebuf_commit(&file, GIT_PACK_FILE_MODE) < 0) {
git_filebuf_cleanup(&file);
return -1;
}
return 0;
}
static int type_size_sort(const void *_a, const void *_b)
{
const git_pobject *a = (git_pobject *)_a;
const git_pobject *b = (git_pobject *)_b;
if (a->type > b->type)
return -1;
if (a->type < b->type)
return 1;
if (a->hash > b->hash)
return -1;
if (a->hash < b->hash)
return 1;
/*
* TODO
*
if (a->preferred_base > b->preferred_base)
return -1;
if (a->preferred_base < b->preferred_base)
return 1;
*/
if (a->size > b->size)
return -1;
if (a->size < b->size)
return 1;
return a < b ? -1 : (a > b); /* newest first */
}
static int delta_cacheable(git_packbuilder *pb, unsigned long src_size,
unsigned long trg_size, unsigned long delta_size)
{
if (pb->max_delta_cache_size &&
pb->delta_cache_size + delta_size > pb->max_delta_cache_size)
return 0;
if (delta_size < pb->cache_max_small_delta_size)
return 1;
/* cache delta, if objects are large enough compared to delta size */
if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10))
return 1;
return 0;
}
static int try_delta(git_packbuilder *pb, struct unpacked *trg,
struct unpacked *src, unsigned int max_depth,
unsigned long *mem_usage, int *ret)
{
git_pobject *trg_object = trg->object;
git_pobject *src_object = src->object;
git_odb_object *obj;
unsigned long trg_size, src_size, delta_size,
sizediff, max_size, sz;
unsigned int ref_depth;
void *delta_buf;
/* Don't bother doing diffs between different types */
if (trg_object->type != src_object->type) {
*ret = -1;
return 0;
}
*ret = 0;
/* TODO: support reuse-delta */
/* Let's not bust the allowed depth. */
if (src->depth >= max_depth)
return 0;
/* Now some size filtering heuristics. */
trg_size = (unsigned long)trg_object->size;
if (!trg_object->delta) {
max_size = trg_size/2 - 20;
ref_depth = 1;
} else {
max_size = trg_object->delta_size;
ref_depth = trg->depth;
}
max_size = (uint64_t)max_size * (max_depth - src->depth) /
(max_depth - ref_depth + 1);
if (max_size == 0)
return 0;
src_size = (unsigned long)src_object->size;
sizediff = src_size < trg_size ? trg_size - src_size : 0;
if (sizediff >= max_size)
return 0;
if (trg_size < src_size / 32)
return 0;
/* Load data if not already done */
if (!trg->data) {
if (git_odb_read(&obj, pb->odb, &trg_object->id) < 0)
return -1;
sz = (unsigned long)git_odb_object_size(obj);
trg->data = git__malloc(sz);
GITERR_CHECK_ALLOC(trg->data);
memcpy(trg->data, git_odb_object_data(obj), sz);
git_odb_object_free(obj);
if (sz != trg_size) {
giterr_set(GITERR_INVALID,
"Inconsistent target object length");
return -1;
}
*mem_usage += sz;
}
if (!src->data) {
if (git_odb_read(&obj, pb->odb, &src_object->id) < 0)
return -1;
sz = (unsigned long)git_odb_object_size(obj);
src->data = git__malloc(sz);
GITERR_CHECK_ALLOC(src->data);
memcpy(src->data, git_odb_object_data(obj), sz);
git_odb_object_free(obj);
if (sz != src_size) {
giterr_set(GITERR_INVALID,
"Inconsistent source object length");
return -1;
}
*mem_usage += sz;
}
if (!src->index) {
src->index = git_delta_create_index(src->data, src_size);
if (!src->index)
return 0; /* suboptimal pack - out of memory */
*mem_usage += git_delta_sizeof_index(src->index);
}
delta_buf = git_delta_create(src->index, trg->data, trg_size,
&delta_size, max_size);
if (!delta_buf)
return 0;
if (trg_object->delta) {
/* Prefer only shallower same-sized deltas. */
if (delta_size == trg_object->delta_size &&
src->depth + 1 >= trg->depth) {
git__free(delta_buf);
return 0;
}
}
git_packbuilder__cache_lock(pb);
if (trg_object->delta_data) {
git__free(trg_object->delta_data);
pb->delta_cache_size -= trg_object->delta_size;
trg_object->delta_data = NULL;
}
if (delta_cacheable(pb, src_size, trg_size, delta_size)) {
pb->delta_cache_size += delta_size;
git_packbuilder__cache_unlock(pb);
trg_object->delta_data = git__realloc(delta_buf, delta_size);
GITERR_CHECK_ALLOC(trg_object->delta_data);
} else {
/* create delta when writing the pack */
git_packbuilder__cache_unlock(pb);
git__free(delta_buf);
}
trg_object->delta = src_object;
trg_object->delta_size = delta_size;
trg->depth = src->depth + 1;
*ret = 1;
return 0;
}
static unsigned int check_delta_limit(git_pobject *me, unsigned int n)
{
git_pobject *child = me->delta_child;
unsigned int m = n;
while (child) {
unsigned int c = check_delta_limit(child, n + 1);
if (m < c)
m = c;
child = child->delta_sibling;
}
return m;
}
static unsigned long free_unpacked(struct unpacked *n)
{
unsigned long freed_mem = git_delta_sizeof_index(n->index);
git_delta_free_index(n->index);
n->index = NULL;
if (n->data) {
freed_mem += (unsigned long)n->object->size;
git__free(n->data);
n->data = NULL;
}
n->object = NULL;
n->depth = 0;
return freed_mem;
}
static int find_deltas(git_packbuilder *pb, git_pobject **list,
unsigned int *list_size, unsigned int window,
unsigned int depth)
{
git_pobject *po;
git_buf zbuf = GIT_BUF_INIT;
struct unpacked *array;
uint32_t idx = 0, count = 0;
unsigned long mem_usage = 0;
unsigned int i;
int error = -1;
array = git__calloc(window, sizeof(struct unpacked));
GITERR_CHECK_ALLOC(array);
for (;;) {
struct unpacked *n = array + idx;
unsigned int max_depth;
int j, best_base = -1;
git_packbuilder__progress_lock(pb);
if (!*list_size) {
git_packbuilder__progress_unlock(pb);
break;
}
po = *list++;
(*list_size)--;
git_packbuilder__progress_unlock(pb);
mem_usage -= free_unpacked(n);
n->object = po;
while (pb->window_memory_limit &&
mem_usage > pb->window_memory_limit &&
count > 1) {
uint32_t tail = (idx + window - count) % window;
mem_usage -= free_unpacked(array + tail);
count--;
}
/*
* If the current object is at pack edge, take the depth the
* objects that depend on the current object into account
* otherwise they would become too deep.
*/
max_depth = depth;
if (po->delta_child) {
max_depth -= check_delta_limit(po, 0);
if (max_depth <= 0)
goto next;
}
j = window;
while (--j > 0) {
int ret;
uint32_t other_idx = idx + j;
struct unpacked *m;
if (other_idx >= window)
other_idx -= window;
m = array + other_idx;
if (!m->object)
break;
if (try_delta(pb, n, m, max_depth, &mem_usage, &ret) < 0)
goto on_error;
if (ret < 0)
break;
else if (ret > 0)
best_base = other_idx;
}
/*
* If we decided to cache the delta data, then it is best
* to compress it right away. First because we have to do
* it anyway, and doing it here while we're threaded will
* save a lot of time in the non threaded write phase,
* as well as allow for caching more deltas within
* the same cache size limit.
* ...
* But only if not writing to stdout, since in that case
* the network is most likely throttling writes anyway,
* and therefore it is best to go to the write phase ASAP
* instead, as we can afford spending more time compressing
* between writes at that moment.
*/
if (po->delta_data) {
if (git__compress(&zbuf, po->delta_data, po->delta_size) < 0)
goto on_error;
git__free(po->delta_data);
po->delta_data = git__malloc(zbuf.size);
GITERR_CHECK_ALLOC(po->delta_data);
memcpy(po->delta_data, zbuf.ptr, zbuf.size);
po->z_delta_size = (unsigned long)zbuf.size;
git_buf_clear(&zbuf);
git_packbuilder__cache_lock(pb);
pb->delta_cache_size -= po->delta_size;
pb->delta_cache_size += po->z_delta_size;
git_packbuilder__cache_unlock(pb);
}
/*
* If we made n a delta, and if n is already at max
* depth, leaving it in the window is pointless. we
* should evict it first.
*/
if (po->delta && max_depth <= n->depth)
continue;
/*
* Move the best delta base up in the window, after the
* currently deltified object, to keep it longer. It will
* be the first base object to be attempted next.
*/
if (po->delta) {
struct unpacked swap = array[best_base];
int dist = (window + idx - best_base) % window;
int dst = best_base;
while (dist--) {
int src = (dst + 1) % window;
array[dst] = array[src];
dst = src;
}
array[dst] = swap;
}
next:
idx++;
if (count + 1 < window)
count++;
if (idx >= window)
idx = 0;
}
error = 0;
on_error:
for (i = 0; i < window; ++i) {
git__free(array[i].index);
git__free(array[i].data);
}
git__free(array);
git_buf_free(&zbuf);