2
#include "environment.h"
13
#include "pack-revindex.h"
18
#include "list-objects.h"
19
#include "list-objects-filter-options.h"
20
#include "pack-objects.h"
24
#include "thread-utils.h"
25
#include "pack-bitmap.h"
26
#include "delta-islands.h"
32
#include "object-file.h"
33
#include "object-store-ll.h"
34
#include "replace-object.h"
39
#include "promisor-remote.h"
40
#include "pack-mtimes.h"
41
#include "parse-options.h"
48
static struct packing_data to_pack;
50
static inline struct object_entry *oe_delta(
51
const struct packing_data *pack,
52
const struct object_entry *e)
57
return &pack->ext_bases[e->delta_idx - 1];
59
return &pack->objects[e->delta_idx - 1];
62
static inline unsigned long oe_delta_size(struct packing_data *pack,
63
const struct object_entry *e)
65
if (e->delta_size_valid)
66
return e->delta_size_;
76
return pack->delta_size[e - pack->objects];
79
unsigned long oe_get_size_slow(struct packing_data *pack,
80
const struct object_entry *e);
82
static inline unsigned long oe_size(struct packing_data *pack,
83
const struct object_entry *e)
88
return oe_get_size_slow(pack, e);
91
static inline void oe_set_delta(struct packing_data *pack,
92
struct object_entry *e,
93
struct object_entry *delta)
96
e->delta_idx = (delta - pack->objects) + 1;
101
static inline struct object_entry *oe_delta_sibling(
102
const struct packing_data *pack,
103
const struct object_entry *e)
105
if (e->delta_sibling_idx)
106
return &pack->objects[e->delta_sibling_idx - 1];
110
static inline struct object_entry *oe_delta_child(
111
const struct packing_data *pack,
112
const struct object_entry *e)
114
if (e->delta_child_idx)
115
return &pack->objects[e->delta_child_idx - 1];
119
static inline void oe_set_delta_child(struct packing_data *pack,
120
struct object_entry *e,
121
struct object_entry *delta)
124
e->delta_child_idx = (delta - pack->objects) + 1;
126
e->delta_child_idx = 0;
129
static inline void oe_set_delta_sibling(struct packing_data *pack,
130
struct object_entry *e,
131
struct object_entry *delta)
134
e->delta_sibling_idx = (delta - pack->objects) + 1;
136
e->delta_sibling_idx = 0;
139
static inline void oe_set_size(struct packing_data *pack,
140
struct object_entry *e,
143
if (size < pack->oe_size_limit) {
148
if (oe_get_size_slow(pack, e) != size)
149
BUG("'size' is supposed to be the object size!");
153
static inline void oe_set_delta_size(struct packing_data *pack,
154
struct object_entry *e,
157
if (size < pack->oe_delta_size_limit) {
158
e->delta_size_ = size;
159
e->delta_size_valid = 1;
161
packing_data_lock(pack);
162
if (!pack->delta_size)
163
ALLOC_ARRAY(pack->delta_size, pack->nr_alloc);
164
packing_data_unlock(pack);
166
pack->delta_size[e - pack->objects] = size;
167
e->delta_size_valid = 0;
171
#define IN_PACK(obj) oe_in_pack(&to_pack, obj)
172
#define SIZE(obj) oe_size(&to_pack, obj)
173
#define SET_SIZE(obj,size) oe_set_size(&to_pack, obj, size)
174
#define DELTA_SIZE(obj) oe_delta_size(&to_pack, obj)
175
#define DELTA(obj) oe_delta(&to_pack, obj)
176
#define DELTA_CHILD(obj) oe_delta_child(&to_pack, obj)
177
#define DELTA_SIBLING(obj) oe_delta_sibling(&to_pack, obj)
178
#define SET_DELTA(obj, val) oe_set_delta(&to_pack, obj, val)
179
#define SET_DELTA_EXT(obj, oid) oe_set_delta_ext(&to_pack, obj, oid)
180
#define SET_DELTA_SIZE(obj, val) oe_set_delta_size(&to_pack, obj, val)
181
#define SET_DELTA_CHILD(obj, val) oe_set_delta_child(&to_pack, obj, val)
182
#define SET_DELTA_SIBLING(obj, val) oe_set_delta_sibling(&to_pack, obj, val)
184
static const char *pack_usage[] = {
185
N_("git pack-objects --stdout [<options>] [< <ref-list> | < <object-list>]"),
186
N_("git pack-objects [<options>] <base-name> [< <ref-list> | < <object-list>]"),
190
static struct pack_idx_entry **written_list;
191
static uint32_t nr_result, nr_written, nr_seen;
192
static struct bitmap_index *bitmap_git;
193
static uint32_t write_layer;
196
static int reuse_delta = 1, reuse_object = 1;
197
static int keep_unreachable, unpack_unreachable, include_tag;
198
static timestamp_t unpack_unreachable_expiration;
199
static int pack_loose_unreachable;
201
static timestamp_t cruft_expiration;
203
static int have_non_local_packs;
204
static int incremental;
205
static int ignore_packed_keep_on_disk;
206
static int ignore_packed_keep_in_core;
207
static int allow_ofs_delta;
208
static struct pack_idx_option pack_idx_opts;
209
static const char *base_name;
210
static int progress = 1;
211
static int window = 10;
212
static unsigned long pack_size_limit;
213
static int depth = 50;
214
static int delta_search_threads;
215
static int pack_to_stdout;
218
static int num_preferred_base;
219
static struct progress *progress_state;
221
static struct bitmapped_pack *reuse_packfiles;
222
static size_t reuse_packfiles_nr;
223
static size_t reuse_packfiles_used_nr;
224
static uint32_t reuse_packfile_objects;
225
static struct bitmap *reuse_packfile_bitmap;
227
static int use_bitmap_index_default = 1;
228
static int use_bitmap_index = -1;
233
} allow_pack_reuse = SINGLE_PACK_REUSE;
235
WRITE_BITMAP_FALSE = 0,
239
static uint16_t write_bitmap_options = BITMAP_OPT_HASH_CACHE;
241
static int exclude_promisor_objects;
243
static int use_delta_islands;
245
static unsigned long delta_cache_size = 0;
246
static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
247
static unsigned long cache_max_small_delta_size = 1000;
249
static unsigned long window_memory_limit = 0;
251
static struct string_list uri_protocols = STRING_LIST_INIT_NODUP;
258
static enum missing_action arg_missing_action;
259
static show_object_fn fn_show_object;
261
struct configured_exclusion {
262
struct oidmap_entry e;
266
static struct oidmap configured_exclusions;
268
static struct oidset excluded_by_config;
273
static uint32_t written, written_delta;
274
static uint32_t reused, reused_delta;
279
static struct commit **indexed_commits;
280
static unsigned int indexed_commits_nr;
281
static unsigned int indexed_commits_alloc;
283
static void index_commit_for_bitmap(struct commit *commit)
285
if (indexed_commits_nr >= indexed_commits_alloc) {
286
indexed_commits_alloc = (indexed_commits_alloc + 32) * 2;
287
REALLOC_ARRAY(indexed_commits, indexed_commits_alloc);
290
indexed_commits[indexed_commits_nr++] = commit;
293
static void *get_delta(struct object_entry *entry)
295
unsigned long size, base_size, delta_size;
296
void *buf, *base_buf, *delta_buf;
297
enum object_type type;
299
buf = repo_read_object_file(the_repository, &entry->idx.oid, &type,
302
die(_("unable to read %s"), oid_to_hex(&entry->idx.oid));
303
base_buf = repo_read_object_file(the_repository,
304
&DELTA(entry)->idx.oid, &type,
307
die("unable to read %s",
308
oid_to_hex(&DELTA(entry)->idx.oid));
309
delta_buf = diff_delta(base_buf, base_size,
310
buf, size, &delta_size, 0);
316
if (!delta_buf || delta_size != DELTA_SIZE(entry))
317
BUG("delta size changed");
323
static unsigned long do_compress(void **pptr, unsigned long size)
327
unsigned long maxsize;
329
git_deflate_init(&stream, pack_compression_level);
330
maxsize = git_deflate_bound(&stream, size);
333
out = xmalloc(maxsize);
337
stream.avail_in = size;
338
stream.next_out = out;
339
stream.avail_out = maxsize;
340
while (git_deflate(&stream, Z_FINISH) == Z_OK)
342
git_deflate_end(&stream);
345
return stream.total_out;
348
static unsigned long write_large_blob_data(struct git_istream *st, struct hashfile *f,
349
const struct object_id *oid)
352
unsigned char ibuf[1024 * 16];
353
unsigned char obuf[1024 * 16];
354
unsigned long olen = 0;
356
git_deflate_init(&stream, pack_compression_level);
361
readlen = read_istream(st, ibuf, sizeof(ibuf));
363
die(_("unable to read %s"), oid_to_hex(oid));
365
stream.next_in = ibuf;
366
stream.avail_in = readlen;
367
while ((stream.avail_in || readlen == 0) &&
368
(zret == Z_OK || zret == Z_BUF_ERROR)) {
369
stream.next_out = obuf;
370
stream.avail_out = sizeof(obuf);
371
zret = git_deflate(&stream, readlen ? 0 : Z_FINISH);
372
hashwrite(f, obuf, stream.next_out - obuf);
373
olen += stream.next_out - obuf;
376
die(_("deflate error (%d)"), zret);
378
if (zret != Z_STREAM_END)
379
die(_("deflate error (%d)"), zret);
383
git_deflate_end(&stream);
391
static int check_pack_inflate(struct packed_git *p,
392
struct pack_window **w_curs,
395
unsigned long expect)
398
unsigned char fakebuf[4096], *in;
401
memset(&stream, 0, sizeof(stream));
402
git_inflate_init(&stream);
404
in = use_pack(p, w_curs, offset, &stream.avail_in);
406
stream.next_out = fakebuf;
407
stream.avail_out = sizeof(fakebuf);
408
st = git_inflate(&stream, Z_FINISH);
409
offset += stream.next_in - in;
410
} while (st == Z_OK || st == Z_BUF_ERROR);
411
git_inflate_end(&stream);
412
return (st == Z_STREAM_END &&
413
stream.total_out == expect &&
414
stream.total_in == len) ? 0 : -1;
417
static void copy_pack_data(struct hashfile *f,
418
struct packed_git *p,
419
struct pack_window **w_curs,
427
in = use_pack(p, w_curs, offset, &avail);
429
avail = (unsigned long)len;
430
hashwrite(f, in, avail);
436
static inline int oe_size_greater_than(struct packing_data *pack,
437
const struct object_entry *lhs,
441
return lhs->size_ > rhs;
442
if (rhs < pack->oe_size_limit)
444
return oe_get_size_slow(pack, lhs) > rhs;
448
static unsigned long write_no_reuse_object(struct hashfile *f, struct object_entry *entry,
449
unsigned long limit, int usable_delta)
451
unsigned long size, datalen;
452
unsigned char header[MAX_PACK_OBJECT_HEADER],
453
dheader[MAX_PACK_OBJECT_HEADER];
455
enum object_type type;
457
struct git_istream *st = NULL;
458
const unsigned hashsz = the_hash_algo->rawsz;
461
if (oe_type(entry) == OBJ_BLOB &&
462
oe_size_greater_than(&to_pack, entry, big_file_threshold) &&
463
(st = open_istream(the_repository, &entry->idx.oid, &type,
464
&size, NULL)) != NULL)
467
buf = repo_read_object_file(the_repository,
468
&entry->idx.oid, &type,
471
die(_("unable to read %s"),
472
oid_to_hex(&entry->idx.oid));
478
FREE_AND_NULL(entry->delta_data);
479
entry->z_delta_size = 0;
480
} else if (entry->delta_data) {
481
size = DELTA_SIZE(entry);
482
buf = entry->delta_data;
483
entry->delta_data = NULL;
484
type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
485
OBJ_OFS_DELTA : OBJ_REF_DELTA;
487
buf = get_delta(entry);
488
size = DELTA_SIZE(entry);
489
type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
490
OBJ_OFS_DELTA : OBJ_REF_DELTA;
495
else if (entry->z_delta_size)
496
datalen = entry->z_delta_size;
498
datalen = do_compress(&buf, size);
504
hdrlen = encode_in_pack_object_header(header, sizeof(header),
507
if (type == OBJ_OFS_DELTA) {
513
off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset;
514
unsigned pos = sizeof(dheader) - 1;
515
dheader[pos] = ofs & 127;
517
dheader[--pos] = 128 | (--ofs & 127);
518
if (limit && hdrlen + sizeof(dheader) - pos + datalen + hashsz >= limit) {
524
hashwrite(f, header, hdrlen);
525
hashwrite(f, dheader + pos, sizeof(dheader) - pos);
526
hdrlen += sizeof(dheader) - pos;
527
} else if (type == OBJ_REF_DELTA) {
532
if (limit && hdrlen + hashsz + datalen + hashsz >= limit) {
538
hashwrite(f, header, hdrlen);
539
hashwrite(f, DELTA(entry)->idx.oid.hash, hashsz);
542
if (limit && hdrlen + datalen + hashsz >= limit) {
548
hashwrite(f, header, hdrlen);
551
datalen = write_large_blob_data(st, f, &entry->idx.oid);
554
hashwrite(f, buf, datalen);
558
return hdrlen + datalen;
562
static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry,
563
unsigned long limit, int usable_delta)
565
struct packed_git *p = IN_PACK(entry);
566
struct pack_window *w_curs = NULL;
569
enum object_type type = oe_type(entry);
571
unsigned char header[MAX_PACK_OBJECT_HEADER],
572
dheader[MAX_PACK_OBJECT_HEADER];
574
const unsigned hashsz = the_hash_algo->rawsz;
575
unsigned long entry_size = SIZE(entry);
578
type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
579
OBJ_OFS_DELTA : OBJ_REF_DELTA;
580
hdrlen = encode_in_pack_object_header(header, sizeof(header),
583
offset = entry->in_pack_offset;
584
if (offset_to_pack_pos(p, offset, &pos) < 0)
585
die(_("write_reuse_object: could not locate %s, expected at "
586
"offset %"PRIuMAX" in pack %s"),
587
oid_to_hex(&entry->idx.oid), (uintmax_t)offset,
589
datalen = pack_pos_to_offset(p, pos + 1) - offset;
590
if (!pack_to_stdout && p->index_version > 1 &&
591
check_pack_crc(p, &w_curs, offset, datalen,
592
pack_pos_to_index(p, pos))) {
593
error(_("bad packed object CRC for %s"),
594
oid_to_hex(&entry->idx.oid));
596
return write_no_reuse_object(f, entry, limit, usable_delta);
599
offset += entry->in_pack_header_size;
600
datalen -= entry->in_pack_header_size;
602
if (!pack_to_stdout && p->index_version == 1 &&
603
check_pack_inflate(p, &w_curs, offset, datalen, entry_size)) {
604
error(_("corrupt packed object for %s"),
605
oid_to_hex(&entry->idx.oid));
607
return write_no_reuse_object(f, entry, limit, usable_delta);
610
if (type == OBJ_OFS_DELTA) {
611
off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset;
612
unsigned pos = sizeof(dheader) - 1;
613
dheader[pos] = ofs & 127;
615
dheader[--pos] = 128 | (--ofs & 127);
616
if (limit && hdrlen + sizeof(dheader) - pos + datalen + hashsz >= limit) {
620
hashwrite(f, header, hdrlen);
621
hashwrite(f, dheader + pos, sizeof(dheader) - pos);
622
hdrlen += sizeof(dheader) - pos;
624
} else if (type == OBJ_REF_DELTA) {
625
if (limit && hdrlen + hashsz + datalen + hashsz >= limit) {
629
hashwrite(f, header, hdrlen);
630
hashwrite(f, DELTA(entry)->idx.oid.hash, hashsz);
634
if (limit && hdrlen + datalen + hashsz >= limit) {
638
hashwrite(f, header, hdrlen);
640
copy_pack_data(f, p, &w_curs, offset, datalen);
643
return hdrlen + datalen;
647
static off_t write_object(struct hashfile *f,
648
struct object_entry *entry,
653
int usable_delta, to_reuse;
659
if (!pack_size_limit || !nr_written)
661
else if (pack_size_limit <= write_offset)
668
limit = pack_size_limit - write_offset;
672
else if (!pack_size_limit)
674
else if (DELTA(entry)->idx.offset == (off_t)-1)
676
else if (DELTA(entry)->idx.offset)
683
else if (!IN_PACK(entry))
685
else if (oe_type(entry) == OBJ_REF_DELTA ||
686
oe_type(entry) == OBJ_OFS_DELTA)
688
to_reuse = usable_delta;
690
else if (oe_type(entry) != entry->in_pack_type)
692
else if (DELTA(entry))
700
len = write_no_reuse_object(f, entry, limit, usable_delta);
702
len = write_reuse_object(f, entry, limit, usable_delta);
710
entry->idx.crc32 = crc32_end(f);
714
enum write_one_status {
717
WRITE_ONE_WRITTEN = 1,
718
WRITE_ONE_RECURSIVE = 2
721
static enum write_one_status write_one(struct hashfile *f,
722
struct object_entry *e,
733
recursing = (e->idx.offset == 1);
735
warning(_("recursive delta detected for object %s"),
736
oid_to_hex(&e->idx.oid));
737
return WRITE_ONE_RECURSIVE;
738
} else if (e->idx.offset || e->preferred_base) {
740
return WRITE_ONE_SKIP;
746
switch (write_one(f, DELTA(e), offset)) {
747
case WRITE_ONE_RECURSIVE:
753
case WRITE_ONE_BREAK:
754
e->idx.offset = recursing;
755
return WRITE_ONE_BREAK;
759
e->idx.offset = *offset;
760
size = write_object(f, e, *offset);
762
e->idx.offset = recursing;
763
return WRITE_ONE_BREAK;
765
written_list[nr_written++] = &e->idx;
768
if (signed_add_overflows(*offset, size))
769
die(_("pack too large for current definition of off_t"));
771
return WRITE_ONE_WRITTEN;
774
static int mark_tagged(const char *path UNUSED, const char *referent UNUSED, const struct object_id *oid,
775
int flag UNUSED, void *cb_data UNUSED)
777
struct object_id peeled;
778
struct object_entry *entry = packlist_find(&to_pack, oid);
782
if (!peel_iterated_oid(the_repository, oid, &peeled)) {
783
entry = packlist_find(&to_pack, &peeled);
790
static inline unsigned char oe_layer(struct packing_data *pack,
791
struct object_entry *e)
795
return pack->layer[e - pack->objects];
798
static inline void add_to_write_order(struct object_entry **wo,
800
struct object_entry *e)
802
if (e->filled || oe_layer(&to_pack, e) != write_layer)
808
static void add_descendants_to_write_order(struct object_entry **wo,
810
struct object_entry *e)
812
int add_to_order = 1;
815
struct object_entry *s;
817
add_to_write_order(wo, endp, e);
819
for (s = DELTA_SIBLING(e); s; s = DELTA_SIBLING(s)) {
820
add_to_write_order(wo, endp, s);
824
if (DELTA_CHILD(e)) {
830
if (DELTA_SIBLING(e)) {
831
e = DELTA_SIBLING(e);
836
while (e && !DELTA_SIBLING(e)) {
846
e = DELTA_SIBLING(e);
851
static void add_family_to_write_order(struct object_entry **wo,
853
struct object_entry *e)
855
struct object_entry *root;
857
for (root = e; DELTA(root); root = DELTA(root))
859
add_descendants_to_write_order(wo, endp, root);
862
static void compute_layer_order(struct object_entry **wo, unsigned int *wo_end)
864
unsigned int i, last_untagged;
865
struct object_entry *objects = to_pack.objects;
867
for (i = 0; i < to_pack.nr_objects; i++) {
868
if (objects[i].tagged)
870
add_to_write_order(wo, wo_end, &objects[i]);
877
for (; i < to_pack.nr_objects; i++) {
878
if (objects[i].tagged)
879
add_to_write_order(wo, wo_end, &objects[i]);
885
for (i = last_untagged; i < to_pack.nr_objects; i++) {
886
if (oe_type(&objects[i]) != OBJ_COMMIT &&
887
oe_type(&objects[i]) != OBJ_TAG)
889
add_to_write_order(wo, wo_end, &objects[i]);
895
for (i = last_untagged; i < to_pack.nr_objects; i++) {
896
if (oe_type(&objects[i]) != OBJ_TREE)
898
add_to_write_order(wo, wo_end, &objects[i]);
904
for (i = last_untagged; i < to_pack.nr_objects; i++) {
905
if (!objects[i].filled && oe_layer(&to_pack, &objects[i]) == write_layer)
906
add_family_to_write_order(wo, wo_end, &objects[i]);
910
static struct object_entry **compute_write_order(void)
912
uint32_t max_layers = 1;
913
unsigned int i, wo_end;
915
struct object_entry **wo;
916
struct object_entry *objects = to_pack.objects;
918
for (i = 0; i < to_pack.nr_objects; i++) {
919
objects[i].tagged = 0;
920
objects[i].filled = 0;
921
SET_DELTA_CHILD(&objects[i], NULL);
922
SET_DELTA_SIBLING(&objects[i], NULL);
930
for (i = to_pack.nr_objects; i > 0;) {
931
struct object_entry *e = &objects[--i];
935
e->delta_sibling_idx = DELTA(e)->delta_child_idx;
936
SET_DELTA_CHILD(DELTA(e), e);
942
refs_for_each_tag_ref(get_main_ref_store(the_repository), mark_tagged,
945
if (use_delta_islands) {
946
max_layers = compute_pack_layers(&to_pack);
950
ALLOC_ARRAY(wo, to_pack.nr_objects);
953
for (; write_layer < max_layers; ++write_layer)
954
compute_layer_order(wo, &wo_end);
956
if (wo_end != to_pack.nr_objects)
957
die(_("ordered %u objects, expected %"PRIu32),
958
wo_end, to_pack.nr_objects);
970
static struct reused_chunk {
978
static int reused_chunks_nr;
979
static int reused_chunks_alloc;
981
static void record_reused_object(off_t where, off_t offset)
983
if (reused_chunks_nr && reused_chunks[reused_chunks_nr-1].difference == offset)
986
ALLOC_GROW(reused_chunks, reused_chunks_nr + 1,
987
reused_chunks_alloc);
988
reused_chunks[reused_chunks_nr].original = where;
989
reused_chunks[reused_chunks_nr].difference = offset;
999
static off_t find_reused_offset(off_t where)
1001
int lo = 0, hi = reused_chunks_nr;
1003
int mi = lo + ((hi - lo) / 2);
1004
if (where == reused_chunks[mi].original)
1005
return reused_chunks[mi].difference;
1006
if (where < reused_chunks[mi].original)
1017
return reused_chunks[lo-1].difference;
1020
static void write_reused_pack_one(struct packed_git *reuse_packfile,
1021
size_t pos, struct hashfile *out,
1023
struct pack_window **w_curs)
1025
off_t offset, next, cur;
1026
enum object_type type;
1029
offset = pack_pos_to_offset(reuse_packfile, pos);
1030
next = pack_pos_to_offset(reuse_packfile, pos + 1);
1032
record_reused_object(offset,
1033
offset - (hashfile_total(out) - pack_start));
1036
type = unpack_object_header(reuse_packfile, w_curs, &cur, &size);
1039
if (type == OBJ_OFS_DELTA) {
1043
unsigned char header[MAX_PACK_OBJECT_HEADER];
1046
base_offset = get_delta_base(reuse_packfile, w_curs, &cur, type, offset);
1047
assert(base_offset != 0);
1050
if (!allow_ofs_delta) {
1052
struct object_id base_oid;
1054
if (offset_to_pack_pos(reuse_packfile, base_offset, &base_pos) < 0)
1055
die(_("expected object at offset %"PRIuMAX" "
1057
(uintmax_t)base_offset,
1058
reuse_packfile->pack_name);
1060
nth_packed_object_id(&base_oid, reuse_packfile,
1061
pack_pos_to_index(reuse_packfile, base_pos));
1063
len = encode_in_pack_object_header(header, sizeof(header),
1064
OBJ_REF_DELTA, size);
1065
hashwrite(out, header, len);
1066
hashwrite(out, base_oid.hash, the_hash_algo->rawsz);
1067
copy_pack_data(out, reuse_packfile, w_curs, cur, next - cur);
1072
fixup = find_reused_offset(offset) -
1073
find_reused_offset(base_offset);
1075
unsigned char ofs_header[10];
1076
unsigned i, ofs_len;
1077
off_t ofs = offset - base_offset - fixup;
1079
len = encode_in_pack_object_header(header, sizeof(header),
1080
OBJ_OFS_DELTA, size);
1082
i = sizeof(ofs_header) - 1;
1083
ofs_header[i] = ofs & 127;
1085
ofs_header[--i] = 128 | (--ofs & 127);
1087
ofs_len = sizeof(ofs_header) - i;
1089
hashwrite(out, header, len);
1090
hashwrite(out, ofs_header + sizeof(ofs_header) - ofs_len, ofs_len);
1091
copy_pack_data(out, reuse_packfile, w_curs, cur, next - cur);
1098
copy_pack_data(out, reuse_packfile, w_curs, offset, next - offset);
1101
static size_t write_reused_pack_verbatim(struct bitmapped_pack *reuse_packfile,
1102
struct hashfile *out,
1104
struct pack_window **w_curs)
1106
size_t pos = reuse_packfile->bitmap_pos;
1109
if (pos % BITS_IN_EWORD) {
1110
size_t word_pos = (pos / BITS_IN_EWORD);
1111
size_t offset = pos % BITS_IN_EWORD;
1113
eword_t word = reuse_packfile_bitmap->words[word_pos];
1115
if (offset + reuse_packfile->bitmap_nr < BITS_IN_EWORD)
1116
last = offset + reuse_packfile->bitmap_nr;
1118
last = BITS_IN_EWORD;
1120
for (; offset < last; offset++) {
1121
if (word >> offset == 0)
1123
if (!bitmap_get(reuse_packfile_bitmap,
1124
word_pos * BITS_IN_EWORD + offset))
1128
pos += BITS_IN_EWORD - (pos % BITS_IN_EWORD);
1142
end = reuse_packfile->bitmap_pos + reuse_packfile->bitmap_nr;
1143
if (end % BITS_IN_EWORD)
1144
end -= end % BITS_IN_EWORD;
1146
return reuse_packfile->bitmap_pos / BITS_IN_EWORD;
1149
reuse_packfile_bitmap->words[pos / BITS_IN_EWORD] == (eword_t)~0)
1150
pos += BITS_IN_EWORD;
1155
if (reuse_packfile->bitmap_pos < pos) {
1156
off_t pack_start_off = pack_pos_to_offset(reuse_packfile->p, 0);
1157
off_t pack_end_off = pack_pos_to_offset(reuse_packfile->p,
1158
pos - reuse_packfile->bitmap_pos);
1160
written += pos - reuse_packfile->bitmap_pos;
1163
record_reused_object(pack_start_off,
1164
pack_start_off - (hashfile_total(out) - pack_start));
1166
copy_pack_data(out, reuse_packfile->p, w_curs,
1167
pack_start_off, pack_end_off - pack_start_off);
1169
display_progress(progress_state, written);
1171
if (pos % BITS_IN_EWORD)
1172
BUG("attempted to jump past a word boundary to %"PRIuMAX,
1174
return pos / BITS_IN_EWORD;
1177
static void write_reused_pack(struct bitmapped_pack *reuse_packfile,
1180
size_t i = reuse_packfile->bitmap_pos / BITS_IN_EWORD;
1182
off_t pack_start = hashfile_total(f) - sizeof(struct pack_header);
1183
struct pack_window *w_curs = NULL;
1185
if (allow_ofs_delta)
1186
i = write_reused_pack_verbatim(reuse_packfile, f, pack_start,
1189
for (; i < reuse_packfile_bitmap->word_alloc; ++i) {
1190
eword_t word = reuse_packfile_bitmap->words[i];
1191
size_t pos = (i * BITS_IN_EWORD);
1193
for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1194
if ((word >> offset) == 0)
1197
offset += ewah_bit_ctz64(word >> offset);
1198
if (pos + offset < reuse_packfile->bitmap_pos)
1200
if (pos + offset >= reuse_packfile->bitmap_pos + reuse_packfile->bitmap_nr)
1207
write_reused_pack_one(reuse_packfile->p,
1208
pos + offset - reuse_packfile->bitmap_pos,
1209
f, pack_start, &w_curs);
1210
display_progress(progress_state, ++written);
1215
unuse_pack(&w_curs);
1218
static void write_excluded_by_configs(void)
1220
struct oidset_iter iter;
1221
const struct object_id *oid;
1223
oidset_iter_init(&excluded_by_config, &iter);
1224
while ((oid = oidset_iter_next(&iter))) {
1225
struct configured_exclusion *ex =
1226
oidmap_get(&configured_exclusions, oid);
1229
BUG("configured exclusion wasn't configured");
1230
write_in_full(1, ex->pack_hash_hex, strlen(ex->pack_hash_hex));
1231
write_in_full(1, " ", 1);
1232
write_in_full(1, ex->uri, strlen(ex->uri));
1233
write_in_full(1, "\n", 1);
1237
static const char no_split_warning[] = N_(
1238
"disabling bitmap writing, packs are split due to pack.packSizeLimit"
1241
static void write_pack_file(void)
1246
uint32_t nr_remaining = nr_result;
1247
time_t last_mtime = 0;
1248
struct object_entry **write_order;
1250
if (progress > pack_to_stdout)
1251
progress_state = start_progress(_("Writing objects"), nr_result);
1252
ALLOC_ARRAY(written_list, to_pack.nr_objects);
1253
write_order = compute_write_order();
1256
unsigned char hash[GIT_MAX_RAWSZ];
1257
char *pack_tmp_name = NULL;
1260
f = hashfd_throughput(1, "<stdout>", progress_state);
1262
f = create_tmp_packfile(&pack_tmp_name);
1264
offset = write_pack_header(f, nr_remaining);
1266
if (reuse_packfiles_nr) {
1267
assert(pack_to_stdout);
1268
for (j = 0; j < reuse_packfiles_nr; j++) {
1269
reused_chunks_nr = 0;
1270
write_reused_pack(&reuse_packfiles[j], f);
1271
if (reused_chunks_nr)
1272
reuse_packfiles_used_nr++;
1274
offset = hashfile_total(f);
1278
for (; i < to_pack.nr_objects; i++) {
1279
struct object_entry *e = write_order[i];
1280
if (write_one(f, e, &offset) == WRITE_ONE_BREAK)
1282
display_progress(progress_state, written);
1285
if (pack_to_stdout) {
1293
finalize_hashfile(f, hash, FSYNC_COMPONENT_NONE,
1294
CSUM_HASH_IN_STREAM | CSUM_CLOSE);
1295
} else if (nr_written == nr_remaining) {
1296
finalize_hashfile(f, hash, FSYNC_COMPONENT_PACK,
1297
CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
1304
int fd = finalize_hashfile(f, hash, FSYNC_COMPONENT_PACK, 0);
1305
fixup_pack_header_footer(fd, hash, pack_tmp_name,
1306
nr_written, hash, offset);
1308
if (write_bitmap_index) {
1309
if (write_bitmap_index != WRITE_BITMAP_QUIET)
1310
warning(_(no_split_warning));
1311
write_bitmap_index = 0;
1315
if (!pack_to_stdout) {
1317
struct strbuf tmpname = STRBUF_INIT;
1318
struct bitmap_writer bitmap_writer;
1319
char *idx_tmp_name = NULL;
1328
if (stat(pack_tmp_name, &st) < 0) {
1329
warning_errno(_("failed to stat %s"), pack_tmp_name);
1330
} else if (!last_mtime) {
1331
last_mtime = st.st_mtime;
1334
utb.actime = st.st_atime;
1335
utb.modtime = --last_mtime;
1336
if (utime(pack_tmp_name, &utb) < 0)
1337
warning_errno(_("failed utime() on %s"), pack_tmp_name);
1340
strbuf_addf(&tmpname, "%s-%s.", base_name,
1343
if (write_bitmap_index) {
1344
bitmap_writer_init(&bitmap_writer,
1345
the_repository, &to_pack);
1346
bitmap_writer_set_checksum(&bitmap_writer, hash);
1347
bitmap_writer_build_type_index(&bitmap_writer,
1352
pack_idx_opts.flags |= WRITE_MTIMES;
1354
stage_tmp_packfiles(&tmpname, pack_tmp_name,
1355
written_list, nr_written,
1356
&to_pack, &pack_idx_opts, hash,
1359
if (write_bitmap_index) {
1360
size_t tmpname_len = tmpname.len;
1362
strbuf_addstr(&tmpname, "bitmap");
1363
stop_progress(&progress_state);
1365
bitmap_writer_show_progress(&bitmap_writer,
1367
bitmap_writer_select_commits(&bitmap_writer,
1369
indexed_commits_nr);
1370
if (bitmap_writer_build(&bitmap_writer) < 0)
1371
die(_("failed to write bitmap index"));
1372
bitmap_writer_finish(&bitmap_writer,
1374
tmpname.buf, write_bitmap_options);
1375
bitmap_writer_free(&bitmap_writer);
1376
write_bitmap_index = 0;
1377
strbuf_setlen(&tmpname, tmpname_len);
1380
rename_tmp_packfile_idx(&tmpname, &idx_tmp_name);
1383
strbuf_release(&tmpname);
1384
free(pack_tmp_name);
1385
puts(hash_to_hex(hash));
1389
for (j = 0; j < nr_written; j++) {
1390
written_list[j]->offset = (off_t)-1;
1392
nr_remaining -= nr_written;
1393
} while (nr_remaining && i < to_pack.nr_objects);
1397
stop_progress(&progress_state);
1398
if (written != nr_result)
1399
die(_("wrote %"PRIu32" objects while expecting %"PRIu32),
1400
written, nr_result);
1401
trace2_data_intmax("pack-objects", the_repository,
1402
"write_pack_file/wrote", nr_result);
1405
static int no_try_delta(const char *path)
1407
static struct attr_check *check;
1410
check = attr_check_initl("delta", NULL);
1411
git_check_attr(the_repository->index, path, check);
1412
if (ATTR_FALSE(check->items[0].value))
1427
static int have_duplicate_entry(const struct object_id *oid,
1430
struct object_entry *entry;
1432
if (reuse_packfile_bitmap &&
1433
bitmap_walk_contains(bitmap_git, reuse_packfile_bitmap, oid))
1436
entry = packlist_find(&to_pack, oid);
1441
if (!entry->preferred_base)
1443
entry->preferred_base = 1;
1449
static int want_found_object(const struct object_id *oid, int exclude,
1450
struct packed_git *p)
1457
if (!is_pack_valid(p))
1479
if (local && !p->pack_local)
1485
if (ignore_packed_keep_on_disk || ignore_packed_keep_in_core) {
1495
if (ignore_packed_keep_on_disk)
1496
flags |= ON_DISK_KEEP_PACKS;
1497
if (ignore_packed_keep_in_core)
1498
flags |= IN_CORE_KEEP_PACKS;
1500
if (ignore_packed_keep_on_disk && p->pack_keep)
1502
if (ignore_packed_keep_in_core && p->pack_keep_in_core)
1504
if (has_object_kept_pack(oid, flags))
1513
if (!local || !have_non_local_packs)
1520
static int want_object_in_pack_one(struct packed_git *p,
1521
const struct object_id *oid,
1523
struct packed_git **found_pack,
1524
off_t *found_offset)
1528
if (p == *found_pack)
1529
offset = *found_offset;
1531
offset = find_pack_entry_one(oid->hash, p);
1535
if (!is_pack_valid(p))
1537
*found_offset = offset;
1540
return want_found_object(oid, exclude, p);
1554
static int want_object_in_pack(const struct object_id *oid,
1556
struct packed_git **found_pack,
1557
off_t *found_offset)
1560
struct list_head *pos;
1561
struct multi_pack_index *m;
1563
if (!exclude && local && has_loose_object_nonlocal(oid))
1572
want = want_found_object(oid, exclude, *found_pack);
1580
for (m = get_multi_pack_index(the_repository); m; m = m->next) {
1581
struct pack_entry e;
1582
if (fill_midx_entry(the_repository, oid, &e, m)) {
1583
want = want_object_in_pack_one(e.p, oid, exclude, found_pack, found_offset);
1589
list_for_each(pos, get_packed_git_mru(the_repository)) {
1590
struct packed_git *p = list_entry(pos, struct packed_git, mru);
1591
want = want_object_in_pack_one(p, oid, exclude, found_pack, found_offset);
1592
if (!exclude && want > 0)
1594
get_packed_git_mru(the_repository));
1599
if (uri_protocols.nr) {
1600
struct configured_exclusion *ex =
1601
oidmap_get(&configured_exclusions, oid);
1606
for (i = 0; i < uri_protocols.nr; i++) {
1607
if (skip_prefix(ex->uri,
1608
uri_protocols.items[i].string,
1611
oidset_insert(&excluded_by_config, oid);
1621
static struct object_entry *create_object_entry(const struct object_id *oid,
1622
enum object_type type,
1626
struct packed_git *found_pack,
1629
struct object_entry *entry;
1631
entry = packlist_alloc(&to_pack, oid);
1633
oe_set_type(entry, type);
1635
entry->preferred_base = 1;
1639
oe_set_in_pack(&to_pack, entry, found_pack);
1640
entry->in_pack_offset = found_offset;
1643
entry->no_try_delta = no_try_delta;
1648
static const char no_closure_warning[] = N_(
1649
"disabling bitmap writing, as some objects are not being packed"
1652
static int add_object_entry(const struct object_id *oid, enum object_type type,
1653
const char *name, int exclude)
1655
struct packed_git *found_pack = NULL;
1656
off_t found_offset = 0;
1658
display_progress(progress_state, ++nr_seen);
1660
if (have_duplicate_entry(oid, exclude))
1663
if (!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {
1665
if (write_bitmap_index) {
1666
if (write_bitmap_index != WRITE_BITMAP_QUIET)
1667
warning(_(no_closure_warning));
1668
write_bitmap_index = 0;
1673
create_object_entry(oid, type, pack_name_hash(name),
1674
exclude, name && no_try_delta(name),
1675
found_pack, found_offset);
1679
static int add_object_entry_from_bitmap(const struct object_id *oid,
1680
enum object_type type,
1681
int flags UNUSED, uint32_t name_hash,
1682
struct packed_git *pack, off_t offset)
1684
display_progress(progress_state, ++nr_seen);
1686
if (have_duplicate_entry(oid, 0))
1689
if (!want_object_in_pack(oid, 0, &pack, &offset))
1692
create_object_entry(oid, type, name_hash, 0, 0, pack, offset);
1696
struct pbase_tree_cache {
1697
struct object_id oid;
1701
unsigned long tree_size;
1704
static struct pbase_tree_cache *(pbase_tree_cache[256]);
1705
static int pbase_tree_cache_ix(const struct object_id *oid)
1707
return oid->hash[0] % ARRAY_SIZE(pbase_tree_cache);
1709
static int pbase_tree_cache_ix_incr(int ix)
1711
return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
1714
static struct pbase_tree {
1715
struct pbase_tree *next;
1721
struct pbase_tree_cache pcache;
1724
static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)
1726
struct pbase_tree_cache *ent, *nent;
1729
enum object_type type;
1731
int my_ix = pbase_tree_cache_ix(oid);
1732
int available_ix = -1;
1738
for (neigh = 0; neigh < 8; neigh++) {
1739
ent = pbase_tree_cache[my_ix];
1740
if (ent && oideq(&ent->oid, oid)) {
1744
else if (((available_ix < 0) && (!ent || !ent->ref)) ||
1745
((0 <= available_ix) &&
1746
(!ent && pbase_tree_cache[available_ix])))
1747
available_ix = my_ix;
1750
my_ix = pbase_tree_cache_ix_incr(my_ix);
1756
data = repo_read_object_file(the_repository, oid, &type, &size);
1759
if (type != OBJ_TREE) {
1766
if (available_ix < 0)
1769
ent = pbase_tree_cache[available_ix];
1770
my_ix = available_ix;
1774
nent = xmalloc(sizeof(*nent));
1775
nent->temporary = (available_ix < 0);
1779
free(ent->tree_data);
1782
oidcpy(&nent->oid, oid);
1783
nent->tree_data = data;
1784
nent->tree_size = size;
1786
if (!nent->temporary)
1787
pbase_tree_cache[my_ix] = nent;
1791
static void pbase_tree_put(struct pbase_tree_cache *cache)
1793
if (!cache->temporary) {
1797
free(cache->tree_data);
1801
static size_t name_cmp_len(const char *name)
1803
return strcspn(name, "\n/");
1806
static void add_pbase_object(struct tree_desc *tree,
1809
const char *fullname)
1811
struct name_entry entry;
1814
while (tree_entry(tree,&entry)) {
1815
if (S_ISGITLINK(entry.mode))
1817
cmp = tree_entry_len(&entry) != cmplen ? 1 :
1818
memcmp(name, entry.path, cmplen);
1823
if (name[cmplen] != '/') {
1824
add_object_entry(&entry.oid,
1825
object_type(entry.mode),
1829
if (S_ISDIR(entry.mode)) {
1830
struct tree_desc sub;
1831
struct pbase_tree_cache *tree;
1832
const char *down = name+cmplen+1;
1833
size_t downlen = name_cmp_len(down);
1835
tree = pbase_tree_get(&entry.oid);
1838
init_tree_desc(&sub, &tree->oid,
1839
tree->tree_data, tree->tree_size);
1841
add_pbase_object(&sub, down, downlen, fullname);
1842
pbase_tree_put(tree);
1847
static unsigned *done_pbase_paths;
1848
static int done_pbase_paths_num;
1849
static int done_pbase_paths_alloc;
1850
static int done_pbase_path_pos(unsigned hash)
1853
int hi = done_pbase_paths_num;
1855
int mi = lo + (hi - lo) / 2;
1856
if (done_pbase_paths[mi] == hash)
1858
if (done_pbase_paths[mi] < hash)
1866
static int check_pbase_path(unsigned hash)
1868
int pos = done_pbase_path_pos(hash);
1872
ALLOC_GROW(done_pbase_paths,
1873
done_pbase_paths_num + 1,
1874
done_pbase_paths_alloc);
1875
done_pbase_paths_num++;
1876
if (pos < done_pbase_paths_num)
1877
MOVE_ARRAY(done_pbase_paths + pos + 1, done_pbase_paths + pos,
1878
done_pbase_paths_num - pos - 1);
1879
done_pbase_paths[pos] = hash;
1883
static void add_preferred_base_object(const char *name)
1885
struct pbase_tree *it;
1887
unsigned hash = pack_name_hash(name);
1889
if (!num_preferred_base || check_pbase_path(hash))
1892
cmplen = name_cmp_len(name);
1893
for (it = pbase_tree; it; it = it->next) {
1895
add_object_entry(&it->pcache.oid, OBJ_TREE, NULL, 1);
1898
struct tree_desc tree;
1899
init_tree_desc(&tree, &it->pcache.oid,
1900
it->pcache.tree_data, it->pcache.tree_size);
1901
add_pbase_object(&tree, name, cmplen, name);
1906
static void add_preferred_base(struct object_id *oid)
1908
struct pbase_tree *it;
1911
struct object_id tree_oid;
1913
if (window <= num_preferred_base++)
1916
data = read_object_with_reference(the_repository, oid,
1917
OBJ_TREE, &size, &tree_oid);
1921
for (it = pbase_tree; it; it = it->next) {
1922
if (oideq(&it->pcache.oid, &tree_oid)) {
1928
CALLOC_ARRAY(it, 1);
1929
it->next = pbase_tree;
1932
oidcpy(&it->pcache.oid, &tree_oid);
1933
it->pcache.tree_data = data;
1934
it->pcache.tree_size = size;
1937
static void cleanup_preferred_base(void)
1939
struct pbase_tree *it;
1945
struct pbase_tree *tmp = it;
1947
free(tmp->pcache.tree_data);
1951
for (i = 0; i < ARRAY_SIZE(pbase_tree_cache); i++) {
1952
if (!pbase_tree_cache[i])
1954
free(pbase_tree_cache[i]->tree_data);
1955
FREE_AND_NULL(pbase_tree_cache[i]);
1958
FREE_AND_NULL(done_pbase_paths);
1959
done_pbase_paths_num = done_pbase_paths_alloc = 0;
1973
static int can_reuse_delta(const struct object_id *base_oid,
1974
struct object_entry *delta,
1975
struct object_entry **base_out)
1977
struct object_entry *base;
1983
base = packlist_find(&to_pack, base_oid);
1985
if (!in_same_island(&delta->idx.oid, &base->idx.oid))
1996
if (thin && bitmap_has_oid_in_uninteresting(bitmap_git, base_oid)) {
1997
if (use_delta_islands) {
1998
if (!in_same_island(&delta->idx.oid, base_oid))
2008
static void prefetch_to_pack(uint32_t object_index_start) {
2009
struct oid_array to_fetch = OID_ARRAY_INIT;
2012
for (i = object_index_start; i < to_pack.nr_objects; i++) {
2013
struct object_entry *entry = to_pack.objects + i;
2015
if (!oid_object_info_extended(the_repository,
2018
OBJECT_INFO_FOR_PREFETCH))
2020
oid_array_append(&to_fetch, &entry->idx.oid);
2022
promisor_remote_get_direct(the_repository,
2023
to_fetch.oid, to_fetch.nr);
2024
oid_array_clear(&to_fetch);
2027
static void check_object(struct object_entry *entry, uint32_t object_index)
2029
unsigned long canonical_size;
2030
enum object_type type;
2031
struct object_info oi = {.typep = &type, .sizep = &canonical_size};
2033
if (IN_PACK(entry)) {
2034
struct packed_git *p = IN_PACK(entry);
2035
struct pack_window *w_curs = NULL;
2037
struct object_id base_ref;
2038
struct object_entry *base_entry;
2039
unsigned long used, used_0;
2040
unsigned long avail;
2042
unsigned char *buf, c;
2043
enum object_type type;
2044
unsigned long in_pack_size;
2046
buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail);
2052
used = unpack_object_header_buffer(buf, avail,
2059
BUG("invalid type %d", type);
2060
entry->in_pack_type = type;
2067
switch (entry->in_pack_type) {
2070
oe_set_type(entry, entry->in_pack_type);
2071
SET_SIZE(entry, in_pack_size);
2072
entry->in_pack_header_size = used;
2073
if (oe_type(entry) < OBJ_COMMIT || oe_type(entry) > OBJ_BLOB)
2075
unuse_pack(&w_curs);
2078
if (reuse_delta && !entry->preferred_base) {
2080
use_pack(p, &w_curs,
2081
entry->in_pack_offset + used,
2083
the_repository->hash_algo);
2086
entry->in_pack_header_size = used + the_hash_algo->rawsz;
2089
buf = use_pack(p, &w_curs,
2090
entry->in_pack_offset + used, NULL);
2096
if (!ofs || MSB(ofs, 7)) {
2097
error(_("delta base offset overflow in pack for %s"),
2098
oid_to_hex(&entry->idx.oid));
2102
ofs = (ofs << 7) + (c & 127);
2104
ofs = entry->in_pack_offset - ofs;
2105
if (ofs <= 0 || ofs >= entry->in_pack_offset) {
2106
error(_("delta base offset out of bound for %s"),
2107
oid_to_hex(&entry->idx.oid));
2110
if (reuse_delta && !entry->preferred_base) {
2112
if (offset_to_pack_pos(p, ofs, &pos) < 0)
2114
if (!nth_packed_object_id(&base_ref, p,
2115
pack_pos_to_index(p, pos)))
2118
entry->in_pack_header_size = used + used_0;
2123
can_reuse_delta(&base_ref, entry, &base_entry)) {
2124
oe_set_type(entry, entry->in_pack_type);
2125
SET_SIZE(entry, in_pack_size);
2126
SET_DELTA_SIZE(entry, in_pack_size);
2129
SET_DELTA(entry, base_entry);
2130
entry->delta_sibling_idx = base_entry->delta_child_idx;
2131
SET_DELTA_CHILD(base_entry, entry);
2133
SET_DELTA_EXT(entry, &base_ref);
2136
unuse_pack(&w_curs);
2140
if (oe_type(entry)) {
2148
delta_pos = entry->in_pack_offset + entry->in_pack_header_size;
2149
canonical_size = get_size_from_delta(p, &w_curs, delta_pos);
2150
if (canonical_size == 0)
2152
SET_SIZE(entry, canonical_size);
2153
unuse_pack(&w_curs);
2163
unuse_pack(&w_curs);
2166
if (oid_object_info_extended(the_repository, &entry->idx.oid, &oi,
2167
OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_LOOKUP_REPLACE) < 0) {
2168
if (repo_has_promisor_remote(the_repository)) {
2169
prefetch_to_pack(object_index);
2170
if (oid_object_info_extended(the_repository, &entry->idx.oid, &oi,
2171
OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_LOOKUP_REPLACE) < 0)
2177
oe_set_type(entry, type);
2178
if (entry->type_valid) {
2179
SET_SIZE(entry, canonical_size);
2190
static int pack_offset_sort(const void *_a, const void *_b)
2192
const struct object_entry *a = *(struct object_entry **)_a;
2193
const struct object_entry *b = *(struct object_entry **)_b;
2194
const struct packed_git *a_in_pack = IN_PACK(a);
2195
const struct packed_git *b_in_pack = IN_PACK(b);
2198
if (!a_in_pack && !b_in_pack)
2199
return oidcmp(&a->idx.oid, &b->idx.oid);
2201
if (a_in_pack < b_in_pack)
2203
if (a_in_pack > b_in_pack)
2205
return a->in_pack_offset < b->in_pack_offset ? -1 :
2206
(a->in_pack_offset > b->in_pack_offset);
2222
static void drop_reused_delta(struct object_entry *entry)
2224
unsigned *idx = &to_pack.objects[entry->delta_idx - 1].delta_child_idx;
2225
struct object_info oi = OBJECT_INFO_INIT;
2226
enum object_type type;
2230
struct object_entry *oe = &to_pack.objects[*idx - 1];
2233
*idx = oe->delta_sibling_idx;
2235
idx = &oe->delta_sibling_idx;
2237
SET_DELTA(entry, NULL);
2242
if (packed_object_info(the_repository, IN_PACK(entry), entry->in_pack_offset, &oi) < 0) {
2250
oid_object_info(the_repository, &entry->idx.oid, &size));
2252
oe_set_type(entry, type);
2254
SET_SIZE(entry, size);
2265
static void break_delta_chains(struct object_entry *entry)
2273
uint32_t total_depth;
2274
struct object_entry *cur, *next;
2276
for (cur = entry, total_depth = 0;
2278
cur = DELTA(cur), total_depth++) {
2279
if (cur->dfs_state == DFS_DONE) {
2285
total_depth += cur->depth;
2294
if (cur->dfs_state != DFS_NONE)
2295
BUG("confusing delta dfs state in first pass: %d",
2304
cur->dfs_state = DFS_DONE;
2325
cur->dfs_state = DFS_ACTIVE;
2326
if (DELTA(cur)->dfs_state == DFS_ACTIVE) {
2327
drop_reused_delta(cur);
2328
cur->dfs_state = DFS_DONE;
2340
for (cur = entry; cur; cur = next) {
2349
if (cur->dfs_state == DFS_DONE)
2351
else if (cur->dfs_state != DFS_ACTIVE)
2352
BUG("confusing delta dfs state in second pass: %d",
2373
cur->depth = (total_depth--) % (depth + 1);
2375
drop_reused_delta(cur);
2377
cur->dfs_state = DFS_DONE;
2381
static void get_object_details(void)
2384
struct object_entry **sorted_by_offset;
2387
progress_state = start_progress(_("Counting objects"),
2388
to_pack.nr_objects);
2390
CALLOC_ARRAY(sorted_by_offset, to_pack.nr_objects);
2391
for (i = 0; i < to_pack.nr_objects; i++)
2392
sorted_by_offset[i] = to_pack.objects + i;
2393
QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);
2395
for (i = 0; i < to_pack.nr_objects; i++) {
2396
struct object_entry *entry = sorted_by_offset[i];
2397
check_object(entry, i);
2398
if (entry->type_valid &&
2399
oe_size_greater_than(&to_pack, entry, big_file_threshold))
2400
entry->no_try_delta = 1;
2401
display_progress(progress_state, i + 1);
2403
stop_progress(&progress_state);
2409
for (i = 0; i < to_pack.nr_objects; i++)
2410
break_delta_chains(&to_pack.objects[i]);
2412
free(sorted_by_offset);
2424
static int type_size_sort(const void *_a, const void *_b)
2426
const struct object_entry *a = *(struct object_entry **)_a;
2427
const struct object_entry *b = *(struct object_entry **)_b;
2428
const enum object_type a_type = oe_type(a);
2429
const enum object_type b_type = oe_type(b);
2430
const unsigned long a_size = SIZE(a);
2431
const unsigned long b_size = SIZE(b);
2433
if (a_type > b_type)
2435
if (a_type < b_type)
2437
if (a->hash > b->hash)
2439
if (a->hash < b->hash)
2441
if (a->preferred_base > b->preferred_base)
2443
if (a->preferred_base < b->preferred_base)
2445
if (use_delta_islands) {
2446
const int island_cmp = island_delta_cmp(&a->idx.oid, &b->idx.oid);
2450
if (a_size > b_size)
2452
if (a_size < b_size)
2454
return a < b ? -1 : (a > b);
2458
struct object_entry *entry;
2460
struct delta_index *index;
2464
static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
2465
unsigned long delta_size)
2467
if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)
2470
if (delta_size < cache_max_small_delta_size)
2474
if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10))
2481
static pthread_mutex_t cache_mutex;
2482
#define cache_lock() pthread_mutex_lock(&cache_mutex)
2483
#define cache_unlock() pthread_mutex_unlock(&cache_mutex)
2489
static pthread_mutex_t progress_mutex;
2490
#define progress_lock() pthread_mutex_lock(&progress_mutex)
2491
#define progress_unlock() pthread_mutex_unlock(&progress_mutex)
2500
static inline int oe_size_less_than(struct packing_data *pack,
2501
const struct object_entry *lhs,
2504
if (lhs->size_valid)
2505
return lhs->size_ < rhs;
2506
if (rhs < pack->oe_size_limit)
2508
return oe_get_size_slow(pack, lhs) < rhs;
2511
static inline void oe_set_tree_depth(struct packing_data *pack,
2512
struct object_entry *e,
2513
unsigned int tree_depth)
2515
if (!pack->tree_depth)
2516
CALLOC_ARRAY(pack->tree_depth, pack->nr_alloc);
2517
pack->tree_depth[e - pack->objects] = tree_depth;
2525
unsigned long oe_get_size_slow(struct packing_data *pack,
2526
const struct object_entry *e)
2528
struct packed_git *p;
2529
struct pack_window *w_curs;
2531
enum object_type type;
2532
unsigned long used, avail, size;
2534
if (e->type_ != OBJ_OFS_DELTA && e->type_ != OBJ_REF_DELTA) {
2535
packing_data_lock(&to_pack);
2536
if (oid_object_info(the_repository, &e->idx.oid, &size) < 0)
2537
die(_("unable to get size of %s"),
2538
oid_to_hex(&e->idx.oid));
2539
packing_data_unlock(&to_pack);
2543
p = oe_in_pack(pack, e);
2545
BUG("when e->type is a delta, it must belong to a pack");
2547
packing_data_lock(&to_pack);
2549
buf = use_pack(p, &w_curs, e->in_pack_offset, &avail);
2550
used = unpack_object_header_buffer(buf, avail, &type, &size);
2552
die(_("unable to parse object header of %s"),
2553
oid_to_hex(&e->idx.oid));
2555
unuse_pack(&w_curs);
2556
packing_data_unlock(&to_pack);
2560
static int try_delta(struct unpacked *trg, struct unpacked *src,
2561
unsigned max_depth, unsigned long *mem_usage)
2563
struct object_entry *trg_entry = trg->entry;
2564
struct object_entry *src_entry = src->entry;
2565
unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
2567
enum object_type type;
2571
if (oe_type(trg_entry) != oe_type(src_entry))
2582
if (reuse_delta && IN_PACK(trg_entry) &&
2583
IN_PACK(trg_entry) == IN_PACK(src_entry) &&
2584
!src_entry->preferred_base &&
2585
trg_entry->in_pack_type != OBJ_REF_DELTA &&
2586
trg_entry->in_pack_type != OBJ_OFS_DELTA)
2590
if (src->depth >= max_depth)
2594
trg_size = SIZE(trg_entry);
2595
if (!DELTA(trg_entry)) {
2596
max_size = trg_size/2 - the_hash_algo->rawsz;
2599
max_size = DELTA_SIZE(trg_entry);
2600
ref_depth = trg->depth;
2602
max_size = (uint64_t)max_size * (max_depth - src->depth) /
2603
(max_depth - ref_depth + 1);
2606
src_size = SIZE(src_entry);
2607
sizediff = src_size < trg_size ? trg_size - src_size : 0;
2608
if (sizediff >= max_size)
2610
if (trg_size < src_size / 32)
2613
if (!in_same_island(&trg->entry->idx.oid, &src->entry->idx.oid))
2618
packing_data_lock(&to_pack);
2619
trg->data = repo_read_object_file(the_repository,
2620
&trg_entry->idx.oid, &type,
2622
packing_data_unlock(&to_pack);
2624
die(_("object %s cannot be read"),
2625
oid_to_hex(&trg_entry->idx.oid));
2627
die(_("object %s inconsistent object length (%"PRIuMAX" vs %"PRIuMAX")"),
2628
oid_to_hex(&trg_entry->idx.oid), (uintmax_t)sz,
2629
(uintmax_t)trg_size);
2633
packing_data_lock(&to_pack);
2634
src->data = repo_read_object_file(the_repository,
2635
&src_entry->idx.oid, &type,
2637
packing_data_unlock(&to_pack);
2639
if (src_entry->preferred_base) {
2640
static int warned = 0;
2642
warning(_("object %s cannot be read"),
2643
oid_to_hex(&src_entry->idx.oid));
2652
die(_("object %s cannot be read"),
2653
oid_to_hex(&src_entry->idx.oid));
2656
die(_("object %s inconsistent object length (%"PRIuMAX" vs %"PRIuMAX")"),
2657
oid_to_hex(&src_entry->idx.oid), (uintmax_t)sz,
2658
(uintmax_t)src_size);
2662
src->index = create_delta_index(src->data, src_size);
2664
static int warned = 0;
2666
warning(_("suboptimal pack - out of memory"));
2669
*mem_usage += sizeof_delta_index(src->index);
2672
delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
2676
if (DELTA(trg_entry)) {
2678
if (delta_size == DELTA_SIZE(trg_entry) &&
2679
src->depth + 1 >= trg->depth) {
2690
free(trg_entry->delta_data);
2692
if (trg_entry->delta_data) {
2693
delta_cache_size -= DELTA_SIZE(trg_entry);
2694
trg_entry->delta_data = NULL;
2696
if (delta_cacheable(src_size, trg_size, delta_size)) {
2697
delta_cache_size += delta_size;
2699
trg_entry->delta_data = xrealloc(delta_buf, delta_size);
2705
SET_DELTA(trg_entry, src_entry);
2706
SET_DELTA_SIZE(trg_entry, delta_size);
2707
trg->depth = src->depth + 1;
2712
static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
2714
struct object_entry *child = DELTA_CHILD(me);
2717
const unsigned int c = check_delta_limit(child, n + 1);
2720
child = DELTA_SIBLING(child);
2725
static unsigned long free_unpacked(struct unpacked *n)
2727
unsigned long freed_mem = sizeof_delta_index(n->index);
2728
free_delta_index(n->index);
2731
freed_mem += SIZE(n->entry);
2732
FREE_AND_NULL(n->data);
2739
static void find_deltas(struct object_entry **list, unsigned *list_size,
2740
int window, int depth, unsigned *processed)
2742
uint32_t i, idx = 0, count = 0;
2743
struct unpacked *array;
2744
unsigned long mem_usage = 0;
2746
CALLOC_ARRAY(array, window);
2749
struct object_entry *entry;
2750
struct unpacked *n = array + idx;
2751
int j, max_depth, best_base = -1;
2760
if (!entry->preferred_base) {
2762
display_progress(progress_state, *processed);
2766
mem_usage -= free_unpacked(n);
2769
while (window_memory_limit &&
2770
mem_usage > window_memory_limit &&
2772
const uint32_t tail = (idx + window - count) % window;
2773
mem_usage -= free_unpacked(array + tail);
2780
if (entry->preferred_base)
2789
if (DELTA_CHILD(entry)) {
2790
max_depth -= check_delta_limit(entry, 0);
2798
uint32_t other_idx = idx + j;
2800
if (other_idx >= window)
2801
other_idx -= window;
2802
m = array + other_idx;
2805
ret = try_delta(n, m, max_depth, &mem_usage);
2809
best_base = other_idx;
2826
if (entry->delta_data && !pack_to_stdout) {
2829
size = do_compress(&entry->delta_data, DELTA_SIZE(entry));
2830
if (size < (1U << OE_Z_DELTA_BITS)) {
2831
entry->z_delta_size = size;
2833
delta_cache_size -= DELTA_SIZE(entry);
2834
delta_cache_size += entry->z_delta_size;
2837
FREE_AND_NULL(entry->delta_data);
2838
entry->z_delta_size = 0;
2846
if (DELTA(entry) && max_depth <= n->depth)
2855
struct unpacked swap = array[best_base];
2856
int dist = (window + idx - best_base) % window;
2857
int dst = best_base;
2859
int src = (dst + 1) % window;
2860
array[dst] = array[src];
2868
if (count + 1 < window)
2874
for (i = 0; i < window; ++i) {
2875
free_delta_index(array[i].index);
2876
free(array[i].data);
2897
struct thread_params {
2899
struct object_entry **list;
2906
pthread_mutex_t mutex;
2907
pthread_cond_t cond;
2908
unsigned *processed;
2911
static pthread_cond_t progress_cond;
2916
static void init_threaded_search(void)
2918
pthread_mutex_init(&cache_mutex, NULL);
2919
pthread_mutex_init(&progress_mutex, NULL);
2920
pthread_cond_init(&progress_cond, NULL);
2923
static void cleanup_threaded_search(void)
2925
pthread_cond_destroy(&progress_cond);
2926
pthread_mutex_destroy(&cache_mutex);
2927
pthread_mutex_destroy(&progress_mutex);
2930
static void *threaded_find_deltas(void *arg)
2932
struct thread_params *me = arg;
2935
while (me->remaining) {
2938
find_deltas(me->list, &me->remaining,
2939
me->window, me->depth, me->processed);
2943
pthread_cond_signal(&progress_cond);
2954
pthread_mutex_lock(&me->mutex);
2955
while (!me->data_ready)
2956
pthread_cond_wait(&me->cond, &me->mutex);
2958
pthread_mutex_unlock(&me->mutex);
2967
static void ll_find_deltas(struct object_entry **list, unsigned list_size,
2968
int window, int depth, unsigned *processed)
2970
struct thread_params *p;
2971
int i, ret, active_threads = 0;
2973
init_threaded_search();
2975
if (delta_search_threads <= 1) {
2976
find_deltas(list, &list_size, window, depth, processed);
2977
cleanup_threaded_search();
2980
if (progress > pack_to_stdout)
2981
fprintf_ln(stderr, _("Delta compression using up to %d threads"),
2982
delta_search_threads);
2983
CALLOC_ARRAY(p, delta_search_threads);
2986
for (i = 0; i < delta_search_threads; i++) {
2987
unsigned sub_size = list_size / (delta_search_threads - i);
2990
if (sub_size < 2*window && i+1 < delta_search_threads)
2993
p[i].window = window;
2995
p[i].processed = processed;
2997
p[i].data_ready = 0;
3000
while (sub_size && sub_size < list_size &&
3001
list[sub_size]->hash &&
3002
list[sub_size]->hash == list[sub_size-1]->hash)
3006
p[i].list_size = sub_size;
3007
p[i].remaining = sub_size;
3010
list_size -= sub_size;
3014
for (i = 0; i < delta_search_threads; i++) {
3015
if (!p[i].list_size)
3017
pthread_mutex_init(&p[i].mutex, NULL);
3018
pthread_cond_init(&p[i].cond, NULL);
3019
ret = pthread_create(&p[i].thread, NULL,
3020
threaded_find_deltas, &p[i]);
3022
die(_("unable to create thread: %s"), strerror(ret));
3034
while (active_threads) {
3035
struct thread_params *target = NULL;
3036
struct thread_params *victim = NULL;
3037
unsigned sub_size = 0;
3041
for (i = 0; !target && i < delta_search_threads; i++)
3046
pthread_cond_wait(&progress_cond, &progress_mutex);
3049
for (i = 0; i < delta_search_threads; i++)
3050
if (p[i].remaining > 2*window &&
3051
(!victim || victim->remaining < p[i].remaining))
3054
sub_size = victim->remaining / 2;
3055
list = victim->list + victim->list_size - sub_size;
3056
while (sub_size && list[0]->hash &&
3057
list[0]->hash == list[-1]->hash) {
3068
sub_size = victim->remaining / 2;
3071
target->list = list;
3072
victim->list_size -= sub_size;
3073
victim->remaining -= sub_size;
3075
target->list_size = sub_size;
3076
target->remaining = sub_size;
3077
target->working = 1;
3080
pthread_mutex_lock(&target->mutex);
3081
target->data_ready = 1;
3082
pthread_cond_signal(&target->cond);
3083
pthread_mutex_unlock(&target->mutex);
3086
pthread_join(target->thread, NULL);
3087
pthread_cond_destroy(&target->cond);
3088
pthread_mutex_destroy(&target->mutex);
3092
cleanup_threaded_search();
3096
static int obj_is_packed(const struct object_id *oid)
3098
return packlist_find(&to_pack, oid) ||
3099
(reuse_packfile_bitmap &&
3100
bitmap_walk_contains(bitmap_git, reuse_packfile_bitmap, oid));
3103
static void add_tag_chain(const struct object_id *oid)
3114
if (obj_is_packed(oid))
3117
tag = lookup_tag(the_repository, oid);
3119
if (!tag || parse_tag(tag) || !tag->tagged)
3120
die(_("unable to pack objects reachable from tag %s"),
3123
add_object_entry(&tag->object.oid, OBJ_TAG, NULL, 0);
3125
if (tag->tagged->type != OBJ_TAG)
3128
tag = (struct tag *)tag->tagged;
3132
static int add_ref_tag(const char *tag UNUSED, const char *referent UNUSED, const struct object_id *oid,
3133
int flag UNUSED, void *cb_data UNUSED)
3135
struct object_id peeled;
3137
if (!peel_iterated_oid(the_repository, oid, &peeled) && obj_is_packed(&peeled))
3142
static void prepare_pack(int window, int depth)
3144
struct object_entry **delta_list;
3145
uint32_t i, nr_deltas;
3148
if (use_delta_islands)
3149
resolve_tree_islands(the_repository, progress, &to_pack);
3151
get_object_details();
3160
if (!pack_to_stdout)
3161
do_check_packed_object_crc = 1;
3163
if (!to_pack.nr_objects || !window || !depth)
3166
ALLOC_ARRAY(delta_list, to_pack.nr_objects);
3169
for (i = 0; i < to_pack.nr_objects; i++) {
3170
struct object_entry *entry = to_pack.objects + i;
3178
if (!entry->type_valid ||
3179
oe_size_less_than(&to_pack, entry, 50))
3182
if (entry->no_try_delta)
3185
if (!entry->preferred_base) {
3187
if (oe_type(entry) < 0)
3188
die(_("unable to get type of object %s"),
3189
oid_to_hex(&entry->idx.oid));
3191
if (oe_type(entry) < 0) {
3200
delta_list[n++] = entry;
3203
if (nr_deltas && n > 1) {
3204
unsigned nr_done = 0;
3207
progress_state = start_progress(_("Compressing objects"),
3209
QSORT(delta_list, n, type_size_sort);
3210
ll_find_deltas(delta_list, n, window+1, depth, &nr_done);
3211
stop_progress(&progress_state);
3212
if (nr_done != nr_deltas)
3213
die(_("inconsistency with delta count"));
3218
static int git_pack_config(const char *k, const char *v,
3219
const struct config_context *ctx, void *cb)
3221
if (!strcmp(k, "pack.window")) {
3222
window = git_config_int(k, v, ctx->kvi);
3225
if (!strcmp(k, "pack.windowmemory")) {
3226
window_memory_limit = git_config_ulong(k, v, ctx->kvi);
3229
if (!strcmp(k, "pack.depth")) {
3230
depth = git_config_int(k, v, ctx->kvi);
3233
if (!strcmp(k, "pack.deltacachesize")) {
3234
max_delta_cache_size = git_config_int(k, v, ctx->kvi);
3237
if (!strcmp(k, "pack.deltacachelimit")) {
3238
cache_max_small_delta_size = git_config_int(k, v, ctx->kvi);
3241
if (!strcmp(k, "pack.writebitmaphashcache")) {
3242
if (git_config_bool(k, v))
3243
write_bitmap_options |= BITMAP_OPT_HASH_CACHE;
3245
write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;
3248
if (!strcmp(k, "pack.writebitmaplookuptable")) {
3249
if (git_config_bool(k, v))
3250
write_bitmap_options |= BITMAP_OPT_LOOKUP_TABLE;
3252
write_bitmap_options &= ~BITMAP_OPT_LOOKUP_TABLE;
3255
if (!strcmp(k, "pack.usebitmaps")) {
3256
use_bitmap_index_default = git_config_bool(k, v);
3259
if (!strcmp(k, "pack.allowpackreuse")) {
3260
int res = git_parse_maybe_bool_text(v);
3262
if (!strcasecmp(v, "single"))
3263
allow_pack_reuse = SINGLE_PACK_REUSE;
3264
else if (!strcasecmp(v, "multi"))
3265
allow_pack_reuse = MULTI_PACK_REUSE;
3267
die(_("invalid pack.allowPackReuse value: '%s'"), v);
3269
allow_pack_reuse = SINGLE_PACK_REUSE;
3271
allow_pack_reuse = NO_PACK_REUSE;
3275
if (!strcmp(k, "pack.threads")) {
3276
delta_search_threads = git_config_int(k, v, ctx->kvi);
3277
if (delta_search_threads < 0)
3278
die(_("invalid number of threads specified (%d)"),
3279
delta_search_threads);
3280
if (!HAVE_THREADS && delta_search_threads != 1) {
3281
warning(_("no threads support, ignoring %s"), k);
3282
delta_search_threads = 0;
3286
if (!strcmp(k, "pack.indexversion")) {
3287
pack_idx_opts.version = git_config_int(k, v, ctx->kvi);
3288
if (pack_idx_opts.version > 2)
3289
die(_("bad pack.indexVersion=%"PRIu32),
3290
pack_idx_opts.version);
3293
if (!strcmp(k, "pack.writereverseindex")) {
3294
if (git_config_bool(k, v))
3295
pack_idx_opts.flags |= WRITE_REV;
3297
pack_idx_opts.flags &= ~WRITE_REV;
3300
if (!strcmp(k, "uploadpack.blobpackfileuri")) {
3301
struct configured_exclusion *ex;
3302
const char *oid_end, *pack_end;
3307
struct object_id pack_hash;
3310
return config_error_nonbool(k);
3312
ex = xmalloc(sizeof(*ex));
3313
if (parse_oid_hex(v, &ex->e.oid, &oid_end) ||
3315
parse_oid_hex(oid_end + 1, &pack_hash, &pack_end) ||
3317
die(_("value of uploadpack.blobpackfileuri must be "
3318
"of the form '<object-hash> <pack-hash> <uri>' (got '%s')"), v);
3319
if (oidmap_get(&configured_exclusions, &ex->e.oid))
3320
die(_("object already configured in another "
3321
"uploadpack.blobpackfileuri (got '%s')"), v);
3322
ex->pack_hash_hex = xcalloc(1, pack_end - oid_end);
3323
memcpy(ex->pack_hash_hex, oid_end + 1, pack_end - oid_end - 1);
3324
ex->uri = xstrdup(pack_end + 1);
3325
oidmap_put(&configured_exclusions, ex);
3327
return git_default_config(k, v, ctx, cb);
3331
static int stdin_packs_found_nr;
3332
static int stdin_packs_hints_nr;
3334
static int add_object_entry_from_pack(const struct object_id *oid,
3335
struct packed_git *p,
3340
enum object_type type = OBJ_NONE;
3342
display_progress(progress_state, ++nr_seen);
3344
if (have_duplicate_entry(oid, 0))
3347
ofs = nth_packed_object_offset(p, pos);
3348
if (!want_object_in_pack(oid, 0, &p, &ofs))
3352
struct rev_info *revs = _data;
3353
struct object_info oi = OBJECT_INFO_INIT;
3356
if (packed_object_info(the_repository, p, ofs, &oi) < 0) {
3357
die(_("could not get type of object %s in pack %s"),
3358
oid_to_hex(oid), p->pack_name);
3359
} else if (type == OBJ_COMMIT) {
3364
add_pending_oid(revs, NULL, oid, 0);
3367
stdin_packs_found_nr++;
3370
create_object_entry(oid, type, 0, 0, 0, p, ofs);
3375
static void show_commit_pack_hint(struct commit *commit UNUSED,
3381
static void show_object_pack_hint(struct object *object, const char *name,
3384
struct object_entry *oe = packlist_find(&to_pack, &object->oid);
3397
oe->hash = pack_name_hash(name);
3398
oe->no_try_delta = name && no_try_delta(name);
3400
stdin_packs_hints_nr++;
3403
static int pack_mtime_cmp(const void *_a, const void *_b)
3405
struct packed_git *a = ((const struct string_list_item*)_a)->util;
3406
struct packed_git *b = ((const struct string_list_item*)_b)->util;
3412
if (a->mtime < b->mtime)
3414
else if (b->mtime < a->mtime)
3420
static void read_packs_list_from_stdin(void)
3422
struct strbuf buf = STRBUF_INIT;
3423
struct string_list include_packs = STRING_LIST_INIT_DUP;
3424
struct string_list exclude_packs = STRING_LIST_INIT_DUP;
3425
struct string_list_item *item = NULL;
3427
struct packed_git *p;
3428
struct rev_info revs;
3430
repo_init_revisions(the_repository, &revs, NULL);
3440
revs.no_kept_objects = 1;
3441
revs.keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
3442
revs.blob_objects = 1;
3443
revs.tree_objects = 1;
3444
revs.tag_objects = 1;
3445
revs.ignore_missing_links = 1;
3447
while (strbuf_getline(&buf, stdin) != EOF) {
3451
if (*buf.buf == '^')
3452
string_list_append(&exclude_packs, buf.buf + 1);
3454
string_list_append(&include_packs, buf.buf);
3459
string_list_sort(&include_packs);
3460
string_list_remove_duplicates(&include_packs, 0);
3461
string_list_sort(&exclude_packs);
3462
string_list_remove_duplicates(&exclude_packs, 0);
3464
for (p = get_all_packs(the_repository); p; p = p->next) {
3465
const char *pack_name = pack_basename(p);
3467
if ((item = string_list_lookup(&include_packs, pack_name)))
3469
if ((item = string_list_lookup(&exclude_packs, pack_name)))
3484
for_each_string_list_item(item, &include_packs) {
3485
struct packed_git *p = item->util;
3487
die(_("could not find pack '%s'"), item->string);
3488
if (!is_pack_valid(p))
3489
die(_("packfile %s cannot be accessed"), p->pack_name);
3497
for_each_string_list_item(item, &exclude_packs) {
3498
struct packed_git *p = item->util;
3500
die(_("could not find pack '%s'"), item->string);
3501
p->pack_keep_in_core = 1;
3509
QSORT(include_packs.items, include_packs.nr, pack_mtime_cmp);
3511
for_each_string_list_item(item, &include_packs) {
3512
struct packed_git *p = item->util;
3513
for_each_object_in_pack(p,
3514
add_object_entry_from_pack,
3516
FOR_EACH_OBJECT_PACK_ORDER);
3519
if (prepare_revision_walk(&revs))
3520
die(_("revision walk setup failed"));
3521
traverse_commit_list(&revs,
3522
show_commit_pack_hint,
3523
show_object_pack_hint,
3526
trace2_data_intmax("pack-objects", the_repository, "stdin_packs_found",
3527
stdin_packs_found_nr);
3528
trace2_data_intmax("pack-objects", the_repository, "stdin_packs_hints",
3529
stdin_packs_hints_nr);
3531
strbuf_release(&buf);
3532
string_list_clear(&include_packs, 0);
3533
string_list_clear(&exclude_packs, 0);
3536
static void add_cruft_object_entry(const struct object_id *oid, enum object_type type,
3537
struct packed_git *pack, off_t offset,
3538
const char *name, uint32_t mtime)
3540
struct object_entry *entry;
3542
display_progress(progress_state, ++nr_seen);
3544
entry = packlist_find(&to_pack, oid);
3547
entry->hash = pack_name_hash(name);
3548
entry->no_try_delta = no_try_delta(name);
3551
if (!want_object_in_pack(oid, 0, &pack, &offset))
3553
if (!pack && type == OBJ_BLOB && !has_loose_object(oid)) {
3570
entry = create_object_entry(oid, type, pack_name_hash(name),
3571
0, name && no_try_delta(name),
3575
if (mtime > oe_cruft_mtime(&to_pack, entry))
3576
oe_set_cruft_mtime(&to_pack, entry, mtime);
3580
static void show_cruft_object(struct object *obj, const char *name, void *data UNUSED)
3592
add_cruft_object_entry(&obj->oid, obj->type, NULL, 0, name, cruft_expiration);
3595
static void show_cruft_commit(struct commit *commit, void *data)
3597
show_cruft_object((struct object*)commit, NULL, data);
3600
static int cruft_include_check_obj(struct object *obj, void *data UNUSED)
3602
return !has_object_kept_pack(&obj->oid, IN_CORE_KEEP_PACKS);
3605
static int cruft_include_check(struct commit *commit, void *data)
3607
return cruft_include_check_obj((struct object*)commit, data);
3610
static void set_cruft_mtime(const struct object *object,
3611
struct packed_git *pack,
3612
off_t offset, time_t mtime)
3614
add_cruft_object_entry(&object->oid, object->type, pack, offset, NULL,
3618
static void mark_pack_kept_in_core(struct string_list *packs, unsigned keep)
3620
struct string_list_item *item = NULL;
3621
for_each_string_list_item(item, packs) {
3622
struct packed_git *p = item->util;
3624
die(_("could not find pack '%s'"), item->string);
3625
p->pack_keep_in_core = keep;
3629
static void add_unreachable_loose_objects(void);
3630
static void add_objects_in_unpacked_packs(void);
3632
static void enumerate_cruft_objects(void)
3635
progress_state = start_progress(_("Enumerating cruft objects"), 0);
3637
add_objects_in_unpacked_packs();
3638
add_unreachable_loose_objects();
3640
stop_progress(&progress_state);
3643
static void enumerate_and_traverse_cruft_objects(struct string_list *fresh_packs)
3645
struct packed_git *p;
3646
struct rev_info revs;
3649
repo_init_revisions(the_repository, &revs, NULL);
3651
revs.tag_objects = 1;
3652
revs.tree_objects = 1;
3653
revs.blob_objects = 1;
3655
revs.include_check = cruft_include_check;
3656
revs.include_check_obj = cruft_include_check_obj;
3658
revs.ignore_missing_links = 1;
3661
progress_state = start_progress(_("Enumerating cruft objects"), 0);
3662
ret = add_unseen_recent_objects_to_traversal(&revs, cruft_expiration,
3663
set_cruft_mtime, 1);
3664
stop_progress(&progress_state);
3667
die(_("unable to add cruft objects"));
3673
for (p = get_all_packs(the_repository); p; p = p->next)
3674
p->pack_keep_in_core = 0;
3675
mark_pack_kept_in_core(fresh_packs, 1);
3677
if (prepare_revision_walk(&revs))
3678
die(_("revision walk setup failed"));
3680
progress_state = start_progress(_("Traversing cruft objects"), 0);
3682
traverse_commit_list(&revs, show_cruft_commit, show_cruft_object, NULL);
3684
stop_progress(&progress_state);
3687
static void read_cruft_objects(void)
3689
struct strbuf buf = STRBUF_INIT;
3690
struct string_list discard_packs = STRING_LIST_INIT_DUP;
3691
struct string_list fresh_packs = STRING_LIST_INIT_DUP;
3692
struct packed_git *p;
3694
ignore_packed_keep_in_core = 1;
3696
while (strbuf_getline(&buf, stdin) != EOF) {
3700
if (*buf.buf == '-')
3701
string_list_append(&discard_packs, buf.buf + 1);
3703
string_list_append(&fresh_packs, buf.buf);
3706
string_list_sort(&discard_packs);
3707
string_list_sort(&fresh_packs);
3709
for (p = get_all_packs(the_repository); p; p = p->next) {
3710
const char *pack_name = pack_basename(p);
3711
struct string_list_item *item;
3713
item = string_list_lookup(&fresh_packs, pack_name);
3715
item = string_list_lookup(&discard_packs, pack_name);
3729
p->pack_keep_in_core = 1;
3733
mark_pack_kept_in_core(&fresh_packs, 1);
3734
mark_pack_kept_in_core(&discard_packs, 0);
3736
if (cruft_expiration)
3737
enumerate_and_traverse_cruft_objects(&fresh_packs);
3739
enumerate_cruft_objects();
3741
strbuf_release(&buf);
3742
string_list_clear(&discard_packs, 0);
3743
string_list_clear(&fresh_packs, 0);
3746
static void read_object_list_from_stdin(void)
3748
char line[GIT_MAX_HEXSZ + 1 + PATH_MAX + 2];
3749
struct object_id oid;
3753
if (!fgets(line, sizeof(line), stdin)) {
3757
BUG("fgets returned NULL, not EOF, not error!");
3763
if (line[0] == '-') {
3764
if (get_oid_hex(line+1, &oid))
3765
die(_("expected edge object ID, got garbage:\n %s"),
3767
add_preferred_base(&oid);
3770
if (parse_oid_hex(line, &oid, &p))
3771
die(_("expected object ID, got garbage:\n %s"), line);
3773
add_preferred_base_object(p + 1);
3774
add_object_entry(&oid, OBJ_NONE, p + 1, 0);
3778
static void show_commit(struct commit *commit, void *data UNUSED)
3780
add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL, 0);
3782
if (write_bitmap_index)
3783
index_commit_for_bitmap(commit);
3785
if (use_delta_islands)
3786
propagate_island_marks(commit);
3789
static void show_object(struct object *obj, const char *name,
3792
add_preferred_base_object(name);
3793
add_object_entry(&obj->oid, obj->type, name, 0);
3795
if (use_delta_islands) {
3798
struct object_entry *ent;
3801
depth = *name ? 1 : 0;
3802
for (p = strchr(name, '/'); p; p = strchr(p + 1, '/'))
3805
ent = packlist_find(&to_pack, &obj->oid);
3806
if (ent && depth > oe_tree_depth(&to_pack, ent))
3807
oe_set_tree_depth(&to_pack, ent, depth);
3811
static void show_object__ma_allow_any(struct object *obj, const char *name, void *data)
3813
assert(arg_missing_action == MA_ALLOW_ANY);
3819
if (!has_object(the_repository, &obj->oid, 0))
3822
show_object(obj, name, data);
3825
static void show_object__ma_allow_promisor(struct object *obj, const char *name, void *data)
3827
assert(arg_missing_action == MA_ALLOW_PROMISOR);
3833
if (!has_object(the_repository, &obj->oid, 0) && is_promisor_object(&obj->oid))
3836
show_object(obj, name, data);
3839
static int option_parse_missing_action(const struct option *opt UNUSED,
3840
const char *arg, int unset)
3845
if (!strcmp(arg, "error")) {
3846
arg_missing_action = MA_ERROR;
3847
fn_show_object = show_object;
3851
if (!strcmp(arg, "allow-any")) {
3852
arg_missing_action = MA_ALLOW_ANY;
3853
fetch_if_missing = 0;
3854
fn_show_object = show_object__ma_allow_any;
3858
if (!strcmp(arg, "allow-promisor")) {
3859
arg_missing_action = MA_ALLOW_PROMISOR;
3860
fetch_if_missing = 0;
3861
fn_show_object = show_object__ma_allow_promisor;
3865
die(_("invalid value for '%s': '%s'"), "--missing", arg);
3869
static void show_edge(struct commit *commit)
3871
add_preferred_base(&commit->object.oid);
3874
static int add_object_in_unpacked_pack(const struct object_id *oid,
3875
struct packed_git *pack,
3883
if (pack->is_cruft) {
3884
if (load_pack_mtimes(pack) < 0)
3885
die(_("could not load cruft pack .mtimes"));
3886
mtime = nth_packed_mtime(pack, pos);
3888
mtime = pack->mtime;
3890
offset = nth_packed_object_offset(pack, pos);
3892
add_cruft_object_entry(oid, OBJ_NONE, pack, offset,
3895
add_object_entry(oid, OBJ_NONE, "", 0);
3900
static void add_objects_in_unpacked_packs(void)
3902
if (for_each_packed_object(add_object_in_unpacked_pack, NULL,
3903
FOR_EACH_OBJECT_PACK_ORDER |
3904
FOR_EACH_OBJECT_LOCAL_ONLY |
3905
FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS |
3906
FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS))
3907
die(_("cannot open pack index"));
3910
static int add_loose_object(const struct object_id *oid, const char *path,
3913
enum object_type type = oid_object_info(the_repository, oid, NULL);
3916
warning(_("loose object at %s could not be examined"), path);
3922
if (stat(path, &st) < 0) {
3923
if (errno == ENOENT)
3925
return error_errno("unable to stat %s", oid_to_hex(oid));
3928
add_cruft_object_entry(oid, type, NULL, 0, NULL,
3931
add_object_entry(oid, type, "", 0);
3941
static void add_unreachable_loose_objects(void)
3943
for_each_loose_file_in_objdir(get_object_directory(),
3948
static int has_sha1_pack_kept_or_nonlocal(const struct object_id *oid)
3950
static struct packed_git *last_found = (void *)1;
3951
struct packed_git *p;
3953
p = (last_found != (void *)1) ? last_found :
3954
get_all_packs(the_repository);
3957
if ((!p->pack_local || p->pack_keep ||
3958
p->pack_keep_in_core) &&
3959
find_pack_entry_one(oid->hash, p)) {
3963
if (p == last_found)
3964
p = get_all_packs(the_repository);
3967
if (p == last_found)
3980
static struct oid_array recent_objects;
3982
static int loosened_object_can_be_discarded(const struct object_id *oid,
3985
if (!unpack_unreachable_expiration)
3987
if (mtime > unpack_unreachable_expiration)
3989
if (oid_array_lookup(&recent_objects, oid) >= 0)
3994
static void loosen_unused_packed_objects(void)
3996
struct packed_git *p;
3998
uint32_t loosened_objects_nr = 0;
3999
struct object_id oid;
4001
for (p = get_all_packs(the_repository); p; p = p->next) {
4002
if (!p->pack_local || p->pack_keep || p->pack_keep_in_core)
4005
if (open_pack_index(p))
4006
die(_("cannot open pack index"));
4008
for (i = 0; i < p->num_objects; i++) {
4009
nth_packed_object_id(&oid, p, i);
4010
if (!packlist_find(&to_pack, &oid) &&
4011
!has_sha1_pack_kept_or_nonlocal(&oid) &&
4012
!loosened_object_can_be_discarded(&oid, p->mtime)) {
4013
if (force_object_loose(&oid, p->mtime))
4014
die(_("unable to force loose object"));
4015
loosened_objects_nr++;
4020
trace2_data_intmax("pack-objects", the_repository,
4021
"loosen_unused_packed_objects/loosened", loosened_objects_nr);
4029
static int pack_options_allow_reuse(void)
4031
return allow_pack_reuse != NO_PACK_REUSE &&
4033
!ignore_packed_keep_on_disk &&
4034
!ignore_packed_keep_in_core &&
4035
(!local || !have_non_local_packs) &&
4039
static int get_object_list_from_bitmap(struct rev_info *revs)
4041
if (!(bitmap_git = prepare_bitmap_walk(revs, 0)))
4044
if (pack_options_allow_reuse())
4045
reuse_partial_packfile_from_bitmap(bitmap_git,
4047
&reuse_packfiles_nr,
4048
&reuse_packfile_bitmap,
4049
allow_pack_reuse == MULTI_PACK_REUSE);
4051
if (reuse_packfiles) {
4052
reuse_packfile_objects = bitmap_popcount(reuse_packfile_bitmap);
4053
if (!reuse_packfile_objects)
4054
BUG("expected non-empty reuse bitmap");
4056
nr_result += reuse_packfile_objects;
4057
nr_seen += reuse_packfile_objects;
4058
display_progress(progress_state, nr_seen);
4061
traverse_bitmap_commit_list(bitmap_git, revs,
4062
&add_object_entry_from_bitmap);
4066
static void record_recent_object(struct object *obj,
4067
const char *name UNUSED,
4070
oid_array_append(&recent_objects, &obj->oid);
4073
static void record_recent_commit(struct commit *commit, void *data UNUSED)
4075
oid_array_append(&recent_objects, &commit->object.oid);
4078
static int mark_bitmap_preferred_tip(const char *refname,
4079
const char *referent UNUSED,
4080
const struct object_id *oid,
4084
struct object_id peeled;
4085
struct object *object;
4087
if (!peel_iterated_oid(the_repository, oid, &peeled))
4090
object = parse_object_or_die(oid, refname);
4091
if (object->type == OBJ_COMMIT)
4092
object->flags |= NEEDS_BITMAP;
4097
static void mark_bitmap_preferred_tips(void)
4099
struct string_list_item *item;
4100
const struct string_list *preferred_tips;
4102
preferred_tips = bitmap_preferred_tips(the_repository);
4103
if (!preferred_tips)
4106
for_each_string_list_item(item, preferred_tips) {
4107
refs_for_each_ref_in(get_main_ref_store(the_repository),
4108
item->string, mark_bitmap_preferred_tip,
4113
static void get_object_list(struct rev_info *revs, int ac, const char **av)
4115
struct setup_revision_opt s_r_opt = {
4116
.allow_exclude_promisor_objects = 1,
4122
save_commit_buffer = 0;
4123
setup_revisions(ac, av, revs, &s_r_opt);
4126
is_repository_shallow(the_repository);
4128
save_warning = warn_on_object_refname_ambiguity;
4129
warn_on_object_refname_ambiguity = 0;
4131
while (fgets(line, sizeof(line), stdin) != NULL) {
4132
int len = strlen(line);
4133
if (len && line[len - 1] == '\n')
4138
if (!strcmp(line, "--not")) {
4139
flags ^= UNINTERESTING;
4140
write_bitmap_index = 0;
4143
if (starts_with(line, "--shallow ")) {
4144
struct object_id oid;
4145
if (get_oid_hex(line + 10, &oid))
4146
die("not an object name '%s'", line + 10);
4147
register_shallow(the_repository, &oid);
4148
use_bitmap_index = 0;
4151
die(_("not a rev '%s'"), line);
4153
if (handle_revision_arg(line, revs, flags, REVARG_CANNOT_BE_FILENAME))
4154
die(_("bad revision '%s'"), line);
4157
warn_on_object_refname_ambiguity = save_warning;
4159
if (use_bitmap_index && !get_object_list_from_bitmap(revs))
4162
if (use_delta_islands)
4163
load_delta_islands(the_repository, progress);
4165
if (write_bitmap_index)
4166
mark_bitmap_preferred_tips();
4168
if (prepare_revision_walk(revs))
4169
die(_("revision walk setup failed"));
4170
mark_edges_uninteresting(revs, show_edge, sparse);
4172
if (!fn_show_object)
4173
fn_show_object = show_object;
4174
traverse_commit_list(revs,
4175
show_commit, fn_show_object,
4178
if (unpack_unreachable_expiration) {
4179
revs->ignore_missing_links = 1;
4180
if (add_unseen_recent_objects_to_traversal(revs,
4181
unpack_unreachable_expiration, NULL, 0))
4182
die(_("unable to add recent objects"));
4183
if (prepare_revision_walk(revs))
4184
die(_("revision walk setup failed"));
4185
traverse_commit_list(revs, record_recent_commit,
4186
record_recent_object, NULL);
4189
if (keep_unreachable)
4190
add_objects_in_unpacked_packs();
4191
if (pack_loose_unreachable)
4192
add_unreachable_loose_objects();
4193
if (unpack_unreachable)
4194
loosen_unused_packed_objects();
4196
oid_array_clear(&recent_objects);
4199
static void add_extra_kept_packs(const struct string_list *names)
4201
struct packed_git *p;
4206
for (p = get_all_packs(the_repository); p; p = p->next) {
4207
const char *name = basename(p->pack_name);
4213
for (i = 0; i < names->nr; i++)
4214
if (!fspathcmp(name, names->items[i].string))
4217
if (i < names->nr) {
4218
p->pack_keep_in_core = 1;
4219
ignore_packed_keep_in_core = 1;
4225
static int option_parse_quiet(const struct option *opt, const char *arg,
4228
int *val = opt->value;
4230
BUG_ON_OPT_ARG(arg);
4239
static int option_parse_index_version(const struct option *opt,
4240
const char *arg, int unset)
4242
struct pack_idx_option *popts = opt->value;
4244
const char *val = arg;
4246
BUG_ON_OPT_NEG(unset);
4248
popts->version = strtoul(val, &c, 10);
4249
if (popts->version > 2)
4250
die(_("unsupported index version %s"), val);
4251
if (*c == ',' && c[1])
4252
popts->off32_limit = strtoul(c+1, &c, 0);
4253
if (*c || popts->off32_limit & 0x80000000)
4254
die(_("bad index version '%s'"), val);
4258
static int option_parse_unpack_unreachable(const struct option *opt UNUSED,
4259
const char *arg, int unset)
4262
unpack_unreachable = 0;
4263
unpack_unreachable_expiration = 0;
4266
unpack_unreachable = 1;
4268
unpack_unreachable_expiration = approxidate(arg);
4273
static int option_parse_cruft_expiration(const struct option *opt UNUSED,
4274
const char *arg, int unset)
4278
cruft_expiration = 0;
4282
cruft_expiration = approxidate(arg);
4287
int cmd_pack_objects(int argc, const char **argv, const char *prefix)
4289
int use_internal_rev_list = 0;
4291
int all_progress_implied = 0;
4292
struct strvec rp = STRVEC_INIT;
4293
int rev_list_unpacked = 0, rev_list_all = 0, rev_list_reflog = 0;
4294
int rev_list_index = 0;
4295
int stdin_packs = 0;
4296
struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
4297
struct list_objects_filter_options filter_options =
4298
LIST_OBJECTS_FILTER_INIT;
4300
struct option pack_objects_options[] = {
4301
OPT_CALLBACK_F('q', "quiet", &progress, NULL,
4302
N_("do not show progress meter"),
4303
PARSE_OPT_NOARG, option_parse_quiet),
4304
OPT_SET_INT(0, "progress", &progress,
4305
N_("show progress meter"), 1),
4306
OPT_SET_INT(0, "all-progress", &progress,
4307
N_("show progress meter during object writing phase"), 2),
4308
OPT_BOOL(0, "all-progress-implied",
4309
&all_progress_implied,
4310
N_("similar to --all-progress when progress meter is shown")),
4311
OPT_CALLBACK_F(0, "index-version", &pack_idx_opts, N_("<version>[,<offset>]"),
4312
N_("write the pack index file in the specified idx format version"),
4313
PARSE_OPT_NONEG, option_parse_index_version),
4314
OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit,
4315
N_("maximum size of each output pack file")),
4316
OPT_BOOL(0, "local", &local,
4317
N_("ignore borrowed objects from alternate object store")),
4318
OPT_BOOL(0, "incremental", &incremental,
4319
N_("ignore packed objects")),
4320
OPT_INTEGER(0, "window", &window,
4321
N_("limit pack window by objects")),
4322
OPT_MAGNITUDE(0, "window-memory", &window_memory_limit,
4323
N_("limit pack window by memory in addition to object limit")),
4324
OPT_INTEGER(0, "depth", &depth,
4325
N_("maximum length of delta chain allowed in the resulting pack")),
4326
OPT_BOOL(0, "reuse-delta", &reuse_delta,
4327
N_("reuse existing deltas")),
4328
OPT_BOOL(0, "reuse-object", &reuse_object,
4329
N_("reuse existing objects")),
4330
OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta,
4331
N_("use OFS_DELTA objects")),
4332
OPT_INTEGER(0, "threads", &delta_search_threads,
4333
N_("use threads when searching for best delta matches")),
4334
OPT_BOOL(0, "non-empty", &non_empty,
4335
N_("do not create an empty pack output")),
4336
OPT_BOOL(0, "revs", &use_internal_rev_list,
4337
N_("read revision arguments from standard input")),
4338
OPT_SET_INT_F(0, "unpacked", &rev_list_unpacked,
4339
N_("limit the objects to those that are not yet packed"),
4340
1, PARSE_OPT_NONEG),
4341
OPT_SET_INT_F(0, "all", &rev_list_all,
4342
N_("include objects reachable from any reference"),
4343
1, PARSE_OPT_NONEG),
4344
OPT_SET_INT_F(0, "reflog", &rev_list_reflog,
4345
N_("include objects referred by reflog entries"),
4346
1, PARSE_OPT_NONEG),
4347
OPT_SET_INT_F(0, "indexed-objects", &rev_list_index,
4348
N_("include objects referred to by the index"),
4349
1, PARSE_OPT_NONEG),
4350
OPT_BOOL(0, "stdin-packs", &stdin_packs,
4351
N_("read packs from stdin")),
4352
OPT_BOOL(0, "stdout", &pack_to_stdout,
4353
N_("output pack to stdout")),
4354
OPT_BOOL(0, "include-tag", &include_tag,
4355
N_("include tag objects that refer to objects to be packed")),
4356
OPT_BOOL(0, "keep-unreachable", &keep_unreachable,
4357
N_("keep unreachable objects")),
4358
OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable,
4359
N_("pack loose unreachable objects")),
4360
OPT_CALLBACK_F(0, "unpack-unreachable", NULL, N_("time"),
4361
N_("unpack unreachable objects newer than <time>"),
4362
PARSE_OPT_OPTARG, option_parse_unpack_unreachable),
4363
OPT_BOOL(0, "cruft", &cruft, N_("create a cruft pack")),
4364
OPT_CALLBACK_F(0, "cruft-expiration", NULL, N_("time"),
4365
N_("expire cruft objects older than <time>"),
4366
PARSE_OPT_OPTARG, option_parse_cruft_expiration),
4367
OPT_BOOL(0, "sparse", &sparse,
4368
N_("use the sparse reachability algorithm")),
4369
OPT_BOOL(0, "thin", &thin,
4370
N_("create thin packs")),
4371
OPT_BOOL(0, "shallow", &shallow,
4372
N_("create packs suitable for shallow fetches")),
4373
OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep_on_disk,
4374
N_("ignore packs that have companion .keep file")),
4375
OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
4376
N_("ignore this pack")),
4377
OPT_INTEGER(0, "compression", &pack_compression_level,
4378
N_("pack compression level")),
4379
OPT_BOOL(0, "keep-true-parents", &grafts_keep_true_parents,
4380
N_("do not hide commits by grafts")),
4381
OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index,
4382
N_("use a bitmap index if available to speed up counting objects")),
4383
OPT_SET_INT(0, "write-bitmap-index", &write_bitmap_index,
4384
N_("write a bitmap index together with the pack index"),
4386
OPT_SET_INT_F(0, "write-bitmap-index-quiet",
4387
&write_bitmap_index,
4388
N_("write a bitmap index if possible"),
4389
WRITE_BITMAP_QUIET, PARSE_OPT_HIDDEN),
4390
OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
4391
OPT_CALLBACK_F(0, "missing", NULL, N_("action"),
4392
N_("handling for missing objects"), PARSE_OPT_NONEG,
4393
option_parse_missing_action),
4394
OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
4395
N_("do not pack objects in promisor packfiles")),
4396
OPT_BOOL(0, "delta-islands", &use_delta_islands,
4397
N_("respect islands during delta compression")),
4398
OPT_STRING_LIST(0, "uri-protocol", &uri_protocols,
4400
N_("exclude any configured uploadpack.blobpackfileuri with this protocol")),
4404
if (DFS_NUM_STATES > (1 << OE_DFS_STATE_BITS))
4405
BUG("too many dfs states, increase OE_DFS_STATE_BITS");
4407
disable_replace_refs();
4409
sparse = git_env_bool("GIT_TEST_PACK_SPARSE", -1);
4410
if (the_repository->gitdir) {
4411
prepare_repo_settings(the_repository);
4413
sparse = the_repository->settings.pack_use_sparse;
4414
if (the_repository->settings.pack_use_multi_pack_reuse)
4415
allow_pack_reuse = MULTI_PACK_REUSE;
4418
reset_pack_idx_option(&pack_idx_opts);
4419
pack_idx_opts.flags |= WRITE_REV;
4420
git_config(git_pack_config, NULL);
4421
if (git_env_bool(GIT_TEST_NO_WRITE_REV_INDEX, 0))
4422
pack_idx_opts.flags &= ~WRITE_REV;
4424
progress = isatty(2);
4425
argc = parse_options(argc, argv, prefix, pack_objects_options,
4429
base_name = argv[0];
4432
if (pack_to_stdout != !base_name || argc)
4433
usage_with_options(pack_usage, pack_objects_options);
4437
if (depth >= (1 << OE_DEPTH_BITS)) {
4438
warning(_("delta chain depth %d is too deep, forcing %d"),
4439
depth, (1 << OE_DEPTH_BITS) - 1);
4440
depth = (1 << OE_DEPTH_BITS) - 1;
4442
if (cache_max_small_delta_size >= (1U << OE_Z_DELTA_BITS)) {
4443
warning(_("pack.deltaCacheLimit is too high, forcing %d"),
4444
(1U << OE_Z_DELTA_BITS) - 1);
4445
cache_max_small_delta_size = (1U << OE_Z_DELTA_BITS) - 1;
4450
strvec_push(&rp, "pack-objects");
4452
use_internal_rev_list = 1;
4453
strvec_push(&rp, shallow
4454
? "--objects-edge-aggressive"
4455
: "--objects-edge");
4457
strvec_push(&rp, "--objects");
4460
use_internal_rev_list = 1;
4461
strvec_push(&rp, "--all");
4463
if (rev_list_reflog) {
4464
use_internal_rev_list = 1;
4465
strvec_push(&rp, "--reflog");
4467
if (rev_list_index) {
4468
use_internal_rev_list = 1;
4469
strvec_push(&rp, "--indexed-objects");
4471
if (rev_list_unpacked && !stdin_packs) {
4472
use_internal_rev_list = 1;
4473
strvec_push(&rp, "--unpacked");
4476
if (exclude_promisor_objects) {
4477
use_internal_rev_list = 1;
4478
fetch_if_missing = 0;
4479
strvec_push(&rp, "--exclude-promisor-objects");
4481
if (unpack_unreachable || keep_unreachable || pack_loose_unreachable)
4482
use_internal_rev_list = 1;
4486
if (pack_compression_level == -1)
4487
pack_compression_level = Z_DEFAULT_COMPRESSION;
4488
else if (pack_compression_level < 0 || pack_compression_level > Z_BEST_COMPRESSION)
4489
die(_("bad pack compression level %d"), pack_compression_level);
4491
if (!delta_search_threads)
4492
delta_search_threads = online_cpus();
4494
if (!HAVE_THREADS && delta_search_threads != 1)
4495
warning(_("no threads support, ignoring --threads"));
4496
if (!pack_to_stdout && !pack_size_limit)
4497
pack_size_limit = pack_size_limit_cfg;
4498
if (pack_to_stdout && pack_size_limit)
4499
die(_("--max-pack-size cannot be used to build a pack for transfer"));
4500
if (pack_size_limit && pack_size_limit < 1024*1024) {
4501
warning(_("minimum pack size limit is 1 MiB"));
4502
pack_size_limit = 1024*1024;
4505
if (!pack_to_stdout && thin)
4506
die(_("--thin cannot be used to build an indexable pack"));
4508
if (keep_unreachable && unpack_unreachable)
4509
die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "--unpack-unreachable");
4510
if (!rev_list_all || !rev_list_reflog || !rev_list_index)
4511
unpack_unreachable_expiration = 0;
4513
if (stdin_packs && filter_options.choice)
4514
die(_("cannot use --filter with --stdin-packs"));
4516
if (stdin_packs && use_internal_rev_list)
4517
die(_("cannot use internal rev list with --stdin-packs"));
4520
if (use_internal_rev_list)
4521
die(_("cannot use internal rev list with --cruft"));
4523
die(_("cannot use --stdin-packs with --cruft"));
4535
if (!pack_to_stdout)
4536
use_bitmap_index_default = 0;
4538
if (use_bitmap_index < 0)
4539
use_bitmap_index = use_bitmap_index_default;
4542
if (!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) || is_repository_shallow(the_repository))
4543
use_bitmap_index = 0;
4545
if (pack_to_stdout || !rev_list_all)
4546
write_bitmap_index = 0;
4548
if (use_delta_islands)
4549
strvec_push(&rp, "--topo-order");
4551
if (progress && all_progress_implied)
4554
add_extra_kept_packs(&keep_pack_list);
4555
if (ignore_packed_keep_on_disk) {
4556
struct packed_git *p;
4557
for (p = get_all_packs(the_repository); p; p = p->next)
4558
if (p->pack_local && p->pack_keep)
4561
ignore_packed_keep_on_disk = 0;
4569
struct packed_git *p;
4570
for (p = get_all_packs(the_repository); p; p = p->next) {
4571
if (!p->pack_local) {
4572
have_non_local_packs = 1;
4578
trace2_region_enter("pack-objects", "enumerate-objects",
4580
prepare_packing_data(the_repository, &to_pack);
4582
if (progress && !cruft)
4583
progress_state = start_progress(_("Enumerating objects"), 0);
4586
ignore_packed_keep_in_core = 1;
4587
read_packs_list_from_stdin();
4588
if (rev_list_unpacked)
4589
add_unreachable_loose_objects();
4591
read_cruft_objects();
4592
} else if (!use_internal_rev_list) {
4593
read_object_list_from_stdin();
4595
struct rev_info revs;
4597
repo_init_revisions(the_repository, &revs, NULL);
4598
list_objects_filter_copy(&revs.filter, &filter_options);
4599
get_object_list(&revs, rp.nr, rp.v);
4600
release_revisions(&revs);
4602
cleanup_preferred_base();
4603
if (include_tag && nr_result)
4604
refs_for_each_tag_ref(get_main_ref_store(the_repository),
4606
stop_progress(&progress_state);
4607
trace2_region_leave("pack-objects", "enumerate-objects",
4610
if (non_empty && !nr_result)
4613
trace2_region_enter("pack-objects", "prepare-pack",
4615
prepare_pack(window, depth);
4616
trace2_region_leave("pack-objects", "prepare-pack",
4620
trace2_region_enter("pack-objects", "write-pack-file", the_repository);
4621
write_excluded_by_configs();
4623
trace2_region_leave("pack-objects", "write-pack-file", the_repository);
4627
_("Total %"PRIu32" (delta %"PRIu32"),"
4628
" reused %"PRIu32" (delta %"PRIu32"),"
4629
" pack-reused %"PRIu32" (from %"PRIuMAX")"),
4630
written, written_delta, reused, reused_delta,
4631
reuse_packfile_objects,
4632
(uintmax_t)reuse_packfiles_used_nr);
4634
trace2_data_intmax("pack-objects", the_repository, "written", written);
4635
trace2_data_intmax("pack-objects", the_repository, "written/delta", written_delta);
4636
trace2_data_intmax("pack-objects", the_repository, "reused", reused);
4637
trace2_data_intmax("pack-objects", the_repository, "reused/delta", reused_delta);
4638
trace2_data_intmax("pack-objects", the_repository, "pack-reused", reuse_packfile_objects);
4639
trace2_data_intmax("pack-objects", the_repository, "packs-reused", reuse_packfiles_used_nr);
4642
clear_packing_data(&to_pack);
4643
list_objects_filter_release(&filter_options);