]> git.ipfire.org Git - thirdparty/git.git/blame - midx.c
reftable: support preset file mode for writing
[thirdparty/git.git] / midx.c
CommitLineData
a3407730 1#include "cache.h"
c4d25228 2#include "config.h"
fc59e748 3#include "csum-file.h"
396f2570 4#include "dir.h"
fc59e748 5#include "lockfile.h"
396f2570 6#include "packfile.h"
4d80560c 7#include "object-store.h"
bc626927 8#include "hash-lookup.h"
a3407730 9#include "midx.h"
144d7033 10#include "progress.h"
d829223a 11#include "trace2.h"
ce1e4a10 12#include "run-command.h"
18e449f8 13#include "repository.h"
63a8f0e9 14#include "chunk-format.h"
38ff7cab 15#include "pack.h"
c528e179
TB
16#include "pack-bitmap.h"
17#include "refs.h"
18#include "revision.h"
19#include "list-objects.h"
a3407730 20
fc59e748
DS
21#define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
22#define MIDX_VERSION 1
4d80560c
DS
23#define MIDX_BYTE_FILE_VERSION 4
24#define MIDX_BYTE_HASH_VERSION 5
25#define MIDX_BYTE_NUM_CHUNKS 6
26#define MIDX_BYTE_NUM_PACKS 8
fc59e748 27#define MIDX_HEADER_SIZE 12
aaa95dfa 28#define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
fc59e748 29
32f3c541
DS
30#define MIDX_CHUNK_ALIGNMENT 4
31#define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */
d7cacf29 32#define MIDX_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
0d5b3a5e 33#define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
662148c4
DS
34#define MIDX_CHUNKID_OBJECTOFFSETS 0x4f4f4646 /* "OOFF" */
35#define MIDX_CHUNKID_LARGEOFFSETS 0x4c4f4646 /* "LOFF" */
d7cacf29 36#define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256)
662148c4
DS
37#define MIDX_CHUNK_OFFSET_WIDTH (2 * sizeof(uint32_t))
38#define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t))
39#define MIDX_LARGE_OFFSET_NEEDED 0x80000000
32f3c541 40
19575c7c
DS
41#define PACK_EXPIRED UINT_MAX
42
d9607542
DS
43static uint8_t oid_version(void)
44{
45 switch (hash_algo_by_ptr(the_hash_algo)) {
46 case GIT_HASH_SHA1:
47 return 1;
48 case GIT_HASH_SHA256:
49 return 2;
50 default:
51 die(_("invalid hash version"));
52 }
53}
54
0f533c72 55const unsigned char *get_midx_checksum(struct multi_pack_index *m)
f894081d
TB
56{
57 return m->data + m->data_len - the_hash_algo->rawsz;
58}
59
60980aed 60void get_midx_filename(struct strbuf *out, const char *object_dir)
fc59e748 61{
60980aed 62 strbuf_addf(out, "%s/pack/multi-pack-index", object_dir);
fc59e748
DS
63}
64
60980aed 65void get_midx_rev_filename(struct strbuf *out, struct multi_pack_index *m)
f894081d 66{
60980aed
TB
67 get_midx_filename(out, m->object_dir);
68 strbuf_addf(out, "-%s.rev", hash_to_hex(get_midx_checksum(m)));
f894081d
TB
69}
70
6ab3b8b8
DS
71static int midx_read_oid_fanout(const unsigned char *chunk_start,
72 size_t chunk_size, void *data)
73{
74 struct multi_pack_index *m = data;
75 m->chunk_oid_fanout = (uint32_t *)chunk_start;
76
77 if (chunk_size != 4 * 256) {
78 error(_("multi-pack-index OID fanout is of the wrong size"));
79 return 1;
80 }
81 return 0;
82}
83
2cf489a3 84struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local)
4d80560c
DS
85{
86 struct multi_pack_index *m = NULL;
87 int fd;
88 struct stat st;
89 size_t midx_size;
90 void *midx_map = NULL;
91 uint32_t hash_version;
60980aed 92 struct strbuf midx_name = STRBUF_INIT;
32f3c541 93 uint32_t i;
3227565c 94 const char *cur_pack_name;
6ab3b8b8 95 struct chunkfile *cf = NULL;
4d80560c 96
60980aed
TB
97 get_midx_filename(&midx_name, object_dir);
98
99 fd = git_open(midx_name.buf);
4d80560c
DS
100
101 if (fd < 0)
102 goto cleanup_fail;
103 if (fstat(fd, &st)) {
60980aed 104 error_errno(_("failed to read %s"), midx_name.buf);
4d80560c
DS
105 goto cleanup_fail;
106 }
107
108 midx_size = xsize_t(st.st_size);
109
110 if (midx_size < MIDX_MIN_SIZE) {
60980aed 111 error(_("multi-pack-index file %s is too small"), midx_name.buf);
4d80560c
DS
112 goto cleanup_fail;
113 }
114
60980aed 115 strbuf_release(&midx_name);
4d80560c
DS
116
117 midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0);
6c7ff7cf 118 close(fd);
4d80560c 119
577314ca 120 FLEX_ALLOC_STR(m, object_dir, object_dir);
4d80560c
DS
121 m->data = midx_map;
122 m->data_len = midx_size;
2cf489a3 123 m->local = local;
4d80560c
DS
124
125 m->signature = get_be32(m->data);
53ad0407
DS
126 if (m->signature != MIDX_SIGNATURE)
127 die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
4d80560c 128 m->signature, MIDX_SIGNATURE);
4d80560c
DS
129
130 m->version = m->data[MIDX_BYTE_FILE_VERSION];
53ad0407
DS
131 if (m->version != MIDX_VERSION)
132 die(_("multi-pack-index version %d not recognized"),
4d80560c 133 m->version);
4d80560c
DS
134
135 hash_version = m->data[MIDX_BYTE_HASH_VERSION];
d9607542
DS
136 if (hash_version != oid_version()) {
137 error(_("multi-pack-index hash version %u does not match version %u"),
138 hash_version, oid_version());
139 goto cleanup_fail;
140 }
aaa95dfa 141 m->hash_len = the_hash_algo->rawsz;
4d80560c
DS
142
143 m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS];
144
145 m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS);
146
6ab3b8b8 147 cf = init_chunkfile(NULL);
32f3c541 148
6ab3b8b8
DS
149 if (read_table_of_contents(cf, m->data, midx_size,
150 MIDX_HEADER_SIZE, m->num_chunks))
151 goto cleanup_fail;
32f3c541 152
6ab3b8b8 153 if (pair_chunk(cf, MIDX_CHUNKID_PACKNAMES, &m->chunk_pack_names) == CHUNK_NOT_FOUND)
32f3c541 154 die(_("multi-pack-index missing required pack-name chunk"));
6ab3b8b8 155 if (read_chunk(cf, MIDX_CHUNKID_OIDFANOUT, midx_read_oid_fanout, m) == CHUNK_NOT_FOUND)
d7cacf29 156 die(_("multi-pack-index missing required OID fanout chunk"));
6ab3b8b8 157 if (pair_chunk(cf, MIDX_CHUNKID_OIDLOOKUP, &m->chunk_oid_lookup) == CHUNK_NOT_FOUND)
0d5b3a5e 158 die(_("multi-pack-index missing required OID lookup chunk"));
6ab3b8b8 159 if (pair_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS, &m->chunk_object_offsets) == CHUNK_NOT_FOUND)
662148c4 160 die(_("multi-pack-index missing required object offsets chunk"));
32f3c541 161
6ab3b8b8
DS
162 pair_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS, &m->chunk_large_offsets);
163
d7cacf29
DS
164 m->num_objects = ntohl(m->chunk_oid_fanout[255]);
165
ca56dadb
RS
166 CALLOC_ARRAY(m->pack_names, m->num_packs);
167 CALLOC_ARRAY(m->packs, m->num_packs);
3227565c
DS
168
169 cur_pack_name = (const char *)m->chunk_pack_names;
170 for (i = 0; i < m->num_packs; i++) {
171 m->pack_names[i] = cur_pack_name;
172
173 cur_pack_name += strlen(cur_pack_name) + 1;
174
8e72a3c3
DS
175 if (i && strcmp(m->pack_names[i], m->pack_names[i - 1]) <= 0)
176 die(_("multi-pack-index pack names out of order: '%s' before '%s'"),
3227565c
DS
177 m->pack_names[i - 1],
178 m->pack_names[i]);
3227565c
DS
179 }
180
d829223a
JH
181 trace2_data_intmax("midx", the_repository, "load/num_packs", m->num_packs);
182 trace2_data_intmax("midx", the_repository, "load/num_objects", m->num_objects);
183
692305ec 184 free_chunkfile(cf);
4d80560c
DS
185 return m;
186
187cleanup_fail:
188 free(m);
60980aed 189 strbuf_release(&midx_name);
692305ec 190 free_chunkfile(cf);
4d80560c
DS
191 if (midx_map)
192 munmap(midx_map, midx_size);
193 if (0 <= fd)
194 close(fd);
195 return NULL;
196}
197
1dcd9f20 198void close_midx(struct multi_pack_index *m)
a40498a1
DS
199{
200 uint32_t i;
1dcd9f20
DS
201
202 if (!m)
203 return;
204
9bb6c2e5
TB
205 close_midx(m->next);
206
a40498a1 207 munmap((unsigned char *)m->data, m->data_len);
a40498a1
DS
208
209 for (i = 0; i < m->num_packs; i++) {
af96fe33
DS
210 if (m->packs[i])
211 m->packs[i]->multi_pack_index = 0;
a40498a1
DS
212 }
213 FREE_AND_NULL(m->packs);
214 FREE_AND_NULL(m->pack_names);
9bb6c2e5 215 free(m);
a40498a1
DS
216}
217
64404a24 218int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id)
3715a633
DS
219{
220 struct strbuf pack_name = STRBUF_INIT;
af96fe33 221 struct packed_git *p;
3715a633
DS
222
223 if (pack_int_id >= m->num_packs)
d355e46a 224 die(_("bad pack-int-id: %u (%u total packs)"),
cc6af73c 225 pack_int_id, m->num_packs);
3715a633
DS
226
227 if (m->packs[pack_int_id])
228 return 0;
229
230 strbuf_addf(&pack_name, "%s/pack/%s", m->object_dir,
231 m->pack_names[pack_int_id]);
232
af96fe33 233 p = add_packed_git(pack_name.buf, pack_name.len, m->local);
3715a633 234 strbuf_release(&pack_name);
af96fe33
DS
235
236 if (!p)
237 return 1;
238
239 p->multi_pack_index = 1;
240 m->packs[pack_int_id] = p;
241 install_packed_git(r, p);
242 list_add_tail(&p->mru, &r->objects->packed_git_mru);
243
244 return 0;
3715a633
DS
245}
246
247int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result)
248{
249 return bsearch_hash(oid->hash, m->chunk_oid_fanout, m->chunk_oid_lookup,
aaa95dfa 250 the_hash_algo->rawsz, result);
3715a633
DS
251}
252
8aac67a1
DS
253struct object_id *nth_midxed_object_oid(struct object_id *oid,
254 struct multi_pack_index *m,
255 uint32_t n)
256{
257 if (n >= m->num_objects)
258 return NULL;
259
92e2cab9 260 oidread(oid, m->chunk_oid_lookup + m->hash_len * n);
8aac67a1
DS
261 return oid;
262}
263
62f2c1b5 264off_t nth_midxed_offset(struct multi_pack_index *m, uint32_t pos)
3715a633
DS
265{
266 const unsigned char *offset_data;
267 uint32_t offset32;
268
329fac3a 269 offset_data = m->chunk_object_offsets + (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH;
3715a633
DS
270 offset32 = get_be32(offset_data + sizeof(uint32_t));
271
272 if (m->chunk_large_offsets && offset32 & MIDX_LARGE_OFFSET_NEEDED) {
d8ac9ee1 273 if (sizeof(off_t) < sizeof(uint64_t))
3715a633
DS
274 die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
275
276 offset32 ^= MIDX_LARGE_OFFSET_NEEDED;
277 return get_be64(m->chunk_large_offsets + sizeof(uint64_t) * offset32);
278 }
279
280 return offset32;
281}
282
62f2c1b5 283uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos)
3715a633 284{
329fac3a
DS
285 return get_be32(m->chunk_object_offsets +
286 (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH);
3715a633
DS
287}
288
893b5635
RS
289int fill_midx_entry(struct repository * r,
290 const struct object_id *oid,
291 struct pack_entry *e,
292 struct multi_pack_index *m)
3715a633 293{
893b5635 294 uint32_t pos;
3715a633
DS
295 uint32_t pack_int_id;
296 struct packed_git *p;
297
893b5635
RS
298 if (!bsearch_midx(oid, m, &pos))
299 return 0;
300
3715a633
DS
301 if (pos >= m->num_objects)
302 return 0;
303
304 pack_int_id = nth_midxed_pack_int_id(m, pos);
305
64404a24 306 if (prepare_midx_pack(r, m, pack_int_id))
506ec2fb 307 return 0;
3715a633
DS
308 p = m->packs[pack_int_id];
309
310 /*
311 * We are about to tell the caller where they can locate the
312 * requested object. We better make sure the packfile is
313 * still here and can be accessed before supplying that
314 * answer, as it may have been deleted since the MIDX was
315 * loaded!
316 */
317 if (!is_pack_valid(p))
318 return 0;
319
09ef6617
RS
320 if (oidset_size(&p->bad_objects) &&
321 oidset_contains(&p->bad_objects, oid))
322 return 0;
c39b02ae 323
3715a633
DS
324 e->offset = nth_midxed_offset(m, pos);
325 e->p = p;
326
327 return 1;
328}
329
013fd7ad
JK
330/* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */
331static int cmp_idx_or_pack_name(const char *idx_or_pack_name,
332 const char *idx_name)
333{
334 /* Skip past any initial matching prefix. */
335 while (*idx_name && *idx_name == *idx_or_pack_name) {
336 idx_name++;
337 idx_or_pack_name++;
338 }
339
340 /*
341 * If we didn't match completely, we may have matched "pack-1234." and
342 * be left with "idx" and "pack" respectively, which is also OK. We do
343 * not have to check for "idx" and "idx", because that would have been
344 * a complete match (and in that case these strcmps will be false, but
345 * we'll correctly return 0 from the final strcmp() below.
346 *
347 * Technically this matches "fooidx" and "foopack", but we'd never have
348 * such names in the first place.
349 */
350 if (!strcmp(idx_name, "idx") && !strcmp(idx_or_pack_name, "pack"))
351 return 0;
352
353 /*
354 * This not only checks for a complete match, but also orders based on
355 * the first non-identical character, which means our ordering will
356 * match a raw strcmp(). That makes it OK to use this to binary search
357 * a naively-sorted list.
358 */
359 return strcmp(idx_or_pack_name, idx_name);
360}
361
362int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name)
a40498a1
DS
363{
364 uint32_t first = 0, last = m->num_packs;
365
366 while (first < last) {
367 uint32_t mid = first + (last - first) / 2;
368 const char *current;
369 int cmp;
370
371 current = m->pack_names[mid];
013fd7ad 372 cmp = cmp_idx_or_pack_name(idx_or_pack_name, current);
a40498a1
DS
373 if (!cmp)
374 return 1;
375 if (cmp > 0) {
376 first = mid + 1;
377 continue;
378 }
379 last = mid;
380 }
381
382 return 0;
383}
384
2cf489a3 385int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local)
c4d25228 386{
29e2016b 387 struct multi_pack_index *m;
c4d25228 388 struct multi_pack_index *m_search;
c4d25228 389
18e449f8
DS
390 prepare_repo_settings(r);
391 if (!r->settings.core_multi_pack_index)
c4d25228
DS
392 return 0;
393
29e2016b 394 for (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next)
c4d25228
DS
395 if (!strcmp(object_dir, m_search->object_dir))
396 return 1;
397
29e2016b 398 m = load_multi_pack_index(object_dir, local);
c4d25228 399
29e2016b 400 if (m) {
59552fb3
TB
401 struct multi_pack_index *mp = r->objects->multi_pack_index;
402 if (mp) {
403 m->next = mp->next;
404 mp->next = m;
405 } else
406 r->objects->multi_pack_index = m;
c4d25228
DS
407 return 1;
408 }
409
410 return 0;
411}
412
fc59e748
DS
413static size_t write_midx_header(struct hashfile *f,
414 unsigned char num_chunks,
415 uint32_t num_packs)
416{
fc59e748 417 hashwrite_be32(f, MIDX_SIGNATURE);
014f1447
RS
418 hashwrite_u8(f, MIDX_VERSION);
419 hashwrite_u8(f, oid_version());
420 hashwrite_u8(f, num_chunks);
421 hashwrite_u8(f, 0); /* unused */
fc59e748
DS
422 hashwrite_be32(f, num_packs);
423
424 return MIDX_HEADER_SIZE;
425}
426
d01bf2e6
DS
427struct pack_info {
428 uint32_t orig_pack_int_id;
429 char *pack_name;
430 struct packed_git *p;
19575c7c 431 unsigned expired : 1;
d01bf2e6
DS
432};
433
434static int pack_info_compare(const void *_a, const void *_b)
435{
436 struct pack_info *a = (struct pack_info *)_a;
437 struct pack_info *b = (struct pack_info *)_b;
438 return strcmp(a->pack_name, b->pack_name);
439}
440
9218c6a4
TB
441static int idx_or_pack_name_cmp(const void *_va, const void *_vb)
442{
443 const char *pack_name = _va;
444 const struct pack_info *compar = _vb;
445
446 return cmp_idx_or_pack_name(pack_name, compar->pack_name);
447}
448
577dc496 449struct write_midx_context {
d01bf2e6 450 struct pack_info *info;
396f2570 451 uint32_t nr;
d01bf2e6 452 uint32_t alloc;
a40498a1 453 struct multi_pack_index *m;
840cef0c
WB
454 struct progress *progress;
455 unsigned pack_paths_checked;
31bda9a2
DS
456
457 struct pack_midx_entry *entries;
458 uint32_t entries_nr;
7a3ada11
DS
459
460 uint32_t *pack_perm;
38ff7cab 461 uint32_t *pack_order;
7a3ada11 462 unsigned large_offsets_needed:1;
980f525c 463 uint32_t num_large_offsets;
9218c6a4
TB
464
465 int preferred_pack_idx;
56d863e9
TB
466
467 struct string_list *to_include;
396f2570
DS
468};
469
470static void add_pack_to_midx(const char *full_path, size_t full_path_len,
471 const char *file_name, void *data)
472{
577dc496 473 struct write_midx_context *ctx = data;
396f2570
DS
474
475 if (ends_with(file_name, ".idx")) {
577dc496 476 display_progress(ctx->progress, ++ctx->pack_paths_checked);
56d863e9
TB
477 /*
478 * Note that at most one of ctx->m and ctx->to_include are set,
479 * so we are testing midx_contains_pack() and
480 * string_list_has_string() independently (guarded by the
481 * appropriate NULL checks).
482 *
483 * We could support passing to_include while reusing an existing
484 * MIDX, but don't currently since the reuse process drags
485 * forward all packs from an existing MIDX (without checking
486 * whether or not they appear in the to_include list).
487 *
488 * If we added support for that, these next two conditional
489 * should be performed independently (likely checking
490 * to_include before the existing MIDX).
491 */
577dc496 492 if (ctx->m && midx_contains_pack(ctx->m, file_name))
a40498a1 493 return;
56d863e9
TB
494 else if (ctx->to_include &&
495 !string_list_has_string(ctx->to_include, file_name))
496 return;
a40498a1 497
577dc496 498 ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
396f2570 499
577dc496
DS
500 ctx->info[ctx->nr].p = add_packed_git(full_path,
501 full_path_len,
502 0);
fe1ed56f 503
577dc496 504 if (!ctx->info[ctx->nr].p) {
396f2570
DS
505 warning(_("failed to add packfile '%s'"),
506 full_path);
507 return;
508 }
509
577dc496 510 if (open_pack_index(ctx->info[ctx->nr].p)) {
fe1ed56f
DS
511 warning(_("failed to open pack-index '%s'"),
512 full_path);
577dc496
DS
513 close_pack(ctx->info[ctx->nr].p);
514 FREE_AND_NULL(ctx->info[ctx->nr].p);
fe1ed56f
DS
515 return;
516 }
517
577dc496
DS
518 ctx->info[ctx->nr].pack_name = xstrdup(file_name);
519 ctx->info[ctx->nr].orig_pack_int_id = ctx->nr;
520 ctx->info[ctx->nr].expired = 0;
521 ctx->nr++;
396f2570
DS
522 }
523}
524
fe1ed56f
DS
525struct pack_midx_entry {
526 struct object_id oid;
527 uint32_t pack_int_id;
528 time_t pack_mtime;
529 uint64_t offset;
9218c6a4 530 unsigned preferred : 1;
fe1ed56f
DS
531};
532
533static int midx_oid_compare(const void *_a, const void *_b)
534{
535 const struct pack_midx_entry *a = (const struct pack_midx_entry *)_a;
536 const struct pack_midx_entry *b = (const struct pack_midx_entry *)_b;
537 int cmp = oidcmp(&a->oid, &b->oid);
538
539 if (cmp)
540 return cmp;
541
9218c6a4
TB
542 /* Sort objects in a preferred pack first when multiple copies exist. */
543 if (a->preferred > b->preferred)
544 return -1;
545 if (a->preferred < b->preferred)
546 return 1;
547
fe1ed56f
DS
548 if (a->pack_mtime > b->pack_mtime)
549 return -1;
550 else if (a->pack_mtime < b->pack_mtime)
551 return 1;
552
553 return a->pack_int_id - b->pack_int_id;
554}
555
a40498a1 556static int nth_midxed_pack_midx_entry(struct multi_pack_index *m,
a40498a1
DS
557 struct pack_midx_entry *e,
558 uint32_t pos)
559{
560 if (pos >= m->num_objects)
561 return 1;
562
563 nth_midxed_object_oid(&e->oid, m, pos);
d01bf2e6 564 e->pack_int_id = nth_midxed_pack_int_id(m, pos);
a40498a1
DS
565 e->offset = nth_midxed_offset(m, pos);
566
567 /* consider objects in midx to be from "old" packs */
568 e->pack_mtime = 0;
569 return 0;
570}
571
fe1ed56f
DS
572static void fill_pack_entry(uint32_t pack_int_id,
573 struct packed_git *p,
574 uint32_t cur_object,
9218c6a4
TB
575 struct pack_midx_entry *entry,
576 int preferred)
fe1ed56f 577{
0763671b 578 if (nth_packed_object_id(&entry->oid, p, cur_object) < 0)
fe1ed56f
DS
579 die(_("failed to locate object %d in packfile"), cur_object);
580
581 entry->pack_int_id = pack_int_id;
582 entry->pack_mtime = p->mtime;
583
584 entry->offset = nth_packed_object_offset(p, cur_object);
9218c6a4 585 entry->preferred = !!preferred;
fe1ed56f
DS
586}
587
588/*
589 * It is possible to artificially get into a state where there are many
590 * duplicate copies of objects. That can create high memory pressure if
591 * we are to create a list of all objects before de-duplication. To reduce
592 * this memory pressure without a significant performance drop, automatically
593 * group objects by the first byte of their object id. Use the IDX fanout
594 * tables to group the data, copy to a local array, then sort.
595 *
596 * Copy only the de-duplicated entries (selected by most-recent modified time
597 * of a packfile containing the object).
598 */
a40498a1 599static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m,
d01bf2e6 600 struct pack_info *info,
fe1ed56f 601 uint32_t nr_packs,
9218c6a4
TB
602 uint32_t *nr_objects,
603 int preferred_pack)
fe1ed56f
DS
604{
605 uint32_t cur_fanout, cur_pack, cur_object;
606 uint32_t alloc_fanout, alloc_objects, total_objects = 0;
607 struct pack_midx_entry *entries_by_fanout = NULL;
608 struct pack_midx_entry *deduplicated_entries = NULL;
a40498a1 609 uint32_t start_pack = m ? m->num_packs : 0;
fe1ed56f 610
a40498a1 611 for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++)
d01bf2e6 612 total_objects += info[cur_pack].p->num_objects;
fe1ed56f
DS
613
614 /*
615 * As we de-duplicate by fanout value, we expect the fanout
616 * slices to be evenly distributed, with some noise. Hence,
617 * allocate slightly more than one 256th.
618 */
619 alloc_objects = alloc_fanout = total_objects > 3200 ? total_objects / 200 : 16;
620
621 ALLOC_ARRAY(entries_by_fanout, alloc_fanout);
622 ALLOC_ARRAY(deduplicated_entries, alloc_objects);
623 *nr_objects = 0;
624
625 for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) {
626 uint32_t nr_fanout = 0;
627
a40498a1
DS
628 if (m) {
629 uint32_t start = 0, end;
630
631 if (cur_fanout)
632 start = ntohl(m->chunk_oid_fanout[cur_fanout - 1]);
633 end = ntohl(m->chunk_oid_fanout[cur_fanout]);
634
635 for (cur_object = start; cur_object < end; cur_object++) {
636 ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout);
d01bf2e6 637 nth_midxed_pack_midx_entry(m,
a40498a1
DS
638 &entries_by_fanout[nr_fanout],
639 cur_object);
9218c6a4
TB
640 if (nth_midxed_pack_int_id(m, cur_object) == preferred_pack)
641 entries_by_fanout[nr_fanout].preferred = 1;
642 else
643 entries_by_fanout[nr_fanout].preferred = 0;
a40498a1
DS
644 nr_fanout++;
645 }
646 }
647
648 for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) {
fe1ed56f 649 uint32_t start = 0, end;
9218c6a4 650 int preferred = cur_pack == preferred_pack;
fe1ed56f
DS
651
652 if (cur_fanout)
d01bf2e6
DS
653 start = get_pack_fanout(info[cur_pack].p, cur_fanout - 1);
654 end = get_pack_fanout(info[cur_pack].p, cur_fanout);
fe1ed56f
DS
655
656 for (cur_object = start; cur_object < end; cur_object++) {
657 ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout);
9218c6a4
TB
658 fill_pack_entry(cur_pack,
659 info[cur_pack].p,
660 cur_object,
661 &entries_by_fanout[nr_fanout],
662 preferred);
fe1ed56f
DS
663 nr_fanout++;
664 }
665 }
666
667 QSORT(entries_by_fanout, nr_fanout, midx_oid_compare);
668
669 /*
670 * The batch is now sorted by OID and then mtime (descending).
671 * Take only the first duplicate.
672 */
673 for (cur_object = 0; cur_object < nr_fanout; cur_object++) {
e43d2dcc
JK
674 if (cur_object && oideq(&entries_by_fanout[cur_object - 1].oid,
675 &entries_by_fanout[cur_object].oid))
fe1ed56f
DS
676 continue;
677
678 ALLOC_GROW(deduplicated_entries, *nr_objects + 1, alloc_objects);
679 memcpy(&deduplicated_entries[*nr_objects],
680 &entries_by_fanout[cur_object],
681 sizeof(struct pack_midx_entry));
682 (*nr_objects)++;
683 }
684 }
685
686 free(entries_by_fanout);
687 return deduplicated_entries;
688}
689
0ccd713c 690static int write_midx_pack_names(struct hashfile *f, void *data)
32f3c541 691{
b4d94142 692 struct write_midx_context *ctx = data;
32f3c541
DS
693 uint32_t i;
694 unsigned char padding[MIDX_CHUNK_ALIGNMENT];
695 size_t written = 0;
696
b4d94142 697 for (i = 0; i < ctx->nr; i++) {
19575c7c
DS
698 size_t writelen;
699
b4d94142 700 if (ctx->info[i].expired)
19575c7c 701 continue;
32f3c541 702
b4d94142 703 if (i && strcmp(ctx->info[i].pack_name, ctx->info[i - 1].pack_name) <= 0)
32f3c541 704 BUG("incorrect pack-file order: %s before %s",
b4d94142
DS
705 ctx->info[i - 1].pack_name,
706 ctx->info[i].pack_name);
32f3c541 707
b4d94142
DS
708 writelen = strlen(ctx->info[i].pack_name) + 1;
709 hashwrite(f, ctx->info[i].pack_name, writelen);
32f3c541
DS
710 written += writelen;
711 }
712
713 /* add padding to be aligned */
714 i = MIDX_CHUNK_ALIGNMENT - (written % MIDX_CHUNK_ALIGNMENT);
715 if (i < MIDX_CHUNK_ALIGNMENT) {
716 memset(padding, 0, sizeof(padding));
717 hashwrite(f, padding, i);
32f3c541
DS
718 }
719
0ccd713c 720 return 0;
32f3c541
DS
721}
722
0ccd713c
DS
723static int write_midx_oid_fanout(struct hashfile *f,
724 void *data)
d7cacf29 725{
31bda9a2
DS
726 struct write_midx_context *ctx = data;
727 struct pack_midx_entry *list = ctx->entries;
728 struct pack_midx_entry *last = ctx->entries + ctx->entries_nr;
d7cacf29
DS
729 uint32_t count = 0;
730 uint32_t i;
731
732 /*
733 * Write the first-level table (the list is sorted,
734 * but we use a 256-entry lookup to be able to avoid
735 * having to do eight extra binary search iterations).
736 */
737 for (i = 0; i < 256; i++) {
738 struct pack_midx_entry *next = list;
739
740 while (next < last && next->oid.hash[0] == i) {
741 count++;
742 next++;
743 }
744
745 hashwrite_be32(f, count);
746 list = next;
747 }
748
0ccd713c 749 return 0;
d7cacf29
DS
750}
751
0ccd713c
DS
752static int write_midx_oid_lookup(struct hashfile *f,
753 void *data)
0d5b3a5e 754{
31bda9a2
DS
755 struct write_midx_context *ctx = data;
756 unsigned char hash_len = the_hash_algo->rawsz;
757 struct pack_midx_entry *list = ctx->entries;
0d5b3a5e 758 uint32_t i;
0d5b3a5e 759
31bda9a2 760 for (i = 0; i < ctx->entries_nr; i++) {
0d5b3a5e
DS
761 struct pack_midx_entry *obj = list++;
762
31bda9a2 763 if (i < ctx->entries_nr - 1) {
0d5b3a5e
DS
764 struct pack_midx_entry *next = list;
765 if (oidcmp(&obj->oid, &next->oid) >= 0)
766 BUG("OIDs not in order: %s >= %s",
767 oid_to_hex(&obj->oid),
768 oid_to_hex(&next->oid));
769 }
770
771 hashwrite(f, obj->oid.hash, (int)hash_len);
0d5b3a5e
DS
772 }
773
0ccd713c 774 return 0;
0d5b3a5e
DS
775}
776
0ccd713c
DS
777static int write_midx_object_offsets(struct hashfile *f,
778 void *data)
662148c4 779{
7a3ada11
DS
780 struct write_midx_context *ctx = data;
781 struct pack_midx_entry *list = ctx->entries;
662148c4 782 uint32_t i, nr_large_offset = 0;
662148c4 783
7a3ada11 784 for (i = 0; i < ctx->entries_nr; i++) {
662148c4
DS
785 struct pack_midx_entry *obj = list++;
786
7a3ada11 787 if (ctx->pack_perm[obj->pack_int_id] == PACK_EXPIRED)
19575c7c
DS
788 BUG("object %s is in an expired pack with int-id %d",
789 oid_to_hex(&obj->oid),
790 obj->pack_int_id);
791
7a3ada11 792 hashwrite_be32(f, ctx->pack_perm[obj->pack_int_id]);
662148c4 793
7a3ada11 794 if (ctx->large_offsets_needed && obj->offset >> 31)
662148c4 795 hashwrite_be32(f, MIDX_LARGE_OFFSET_NEEDED | nr_large_offset++);
7a3ada11 796 else if (!ctx->large_offsets_needed && obj->offset >> 32)
662148c4
DS
797 BUG("object %s requires a large offset (%"PRIx64") but the MIDX is not writing large offsets!",
798 oid_to_hex(&obj->oid),
799 obj->offset);
800 else
801 hashwrite_be32(f, (uint32_t)obj->offset);
662148c4
DS
802 }
803
0ccd713c 804 return 0;
662148c4
DS
805}
806
0ccd713c
DS
807static int write_midx_large_offsets(struct hashfile *f,
808 void *data)
662148c4 809{
980f525c
DS
810 struct write_midx_context *ctx = data;
811 struct pack_midx_entry *list = ctx->entries;
812 struct pack_midx_entry *end = ctx->entries + ctx->entries_nr;
980f525c 813 uint32_t nr_large_offset = ctx->num_large_offsets;
662148c4
DS
814
815 while (nr_large_offset) {
61b0fcbb
JK
816 struct pack_midx_entry *obj;
817 uint64_t offset;
818
819 if (list >= end)
820 BUG("too many large-offset objects");
821
822 obj = list++;
823 offset = obj->offset;
662148c4
DS
824
825 if (!(offset >> 31))
826 continue;
827
0ccd713c 828 hashwrite_be64(f, offset);
662148c4
DS
829
830 nr_large_offset--;
831 }
832
0ccd713c 833 return 0;
662148c4
DS
834}
835
30077524
JK
836struct midx_pack_order_data {
837 uint32_t nr;
838 uint32_t pack;
839 off_t offset;
840};
38ff7cab 841
30077524
JK
842static int midx_pack_order_cmp(const void *va, const void *vb)
843{
844 const struct midx_pack_order_data *a = va, *b = vb;
845 if (a->pack < b->pack)
38ff7cab 846 return -1;
30077524 847 else if (a->pack > b->pack)
38ff7cab 848 return 1;
30077524 849 else if (a->offset < b->offset)
38ff7cab 850 return -1;
30077524 851 else if (a->offset > b->offset)
38ff7cab 852 return 1;
30077524
JK
853 else
854 return 0;
38ff7cab
TB
855}
856
857static uint32_t *midx_pack_order(struct write_midx_context *ctx)
858{
30077524 859 struct midx_pack_order_data *data;
38ff7cab
TB
860 uint32_t *pack_order;
861 uint32_t i;
862
30077524
JK
863 ALLOC_ARRAY(data, ctx->entries_nr);
864 for (i = 0; i < ctx->entries_nr; i++) {
865 struct pack_midx_entry *e = &ctx->entries[i];
866 data[i].nr = i;
867 data[i].pack = ctx->pack_perm[e->pack_int_id];
868 if (!e->preferred)
869 data[i].pack |= (1U << 31);
870 data[i].offset = e->offset;
871 }
872
873 QSORT(data, ctx->entries_nr, midx_pack_order_cmp);
874
38ff7cab
TB
875 ALLOC_ARRAY(pack_order, ctx->entries_nr);
876 for (i = 0; i < ctx->entries_nr; i++)
30077524
JK
877 pack_order[i] = data[i].nr;
878 free(data);
38ff7cab
TB
879
880 return pack_order;
881}
882
883static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash,
884 struct write_midx_context *ctx)
885{
886 struct strbuf buf = STRBUF_INIT;
887 const char *tmp_file;
888
889 strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex(midx_hash));
890
891 tmp_file = write_rev_file_order(NULL, ctx->pack_order, ctx->entries_nr,
892 midx_hash, WRITE_REV);
893
894 if (finalize_object_file(tmp_file, buf.buf))
895 die(_("cannot store reverse index file"));
896
897 strbuf_release(&buf);
898}
899
426c00e4 900static void clear_midx_files_ext(const char *object_dir, const char *ext,
38ff7cab
TB
901 unsigned char *keep_hash);
902
ec1e28ef
TB
903static int midx_checksum_valid(struct multi_pack_index *m)
904{
905 return hashfile_checksum_valid(m->data, m->data_len);
906}
907
c528e179
TB
908static void prepare_midx_packing_data(struct packing_data *pdata,
909 struct write_midx_context *ctx)
910{
911 uint32_t i;
912
913 memset(pdata, 0, sizeof(struct packing_data));
914 prepare_packing_data(the_repository, pdata);
915
916 for (i = 0; i < ctx->entries_nr; i++) {
917 struct pack_midx_entry *from = &ctx->entries[ctx->pack_order[i]];
918 struct object_entry *to = packlist_alloc(pdata, &from->oid);
919
920 oe_set_in_pack(pdata, to,
921 ctx->info[ctx->pack_perm[from->pack_int_id]].p);
922 }
923}
924
925static int add_ref_to_pending(const char *refname,
926 const struct object_id *oid,
927 int flag, void *cb_data)
928{
929 struct rev_info *revs = (struct rev_info*)cb_data;
930 struct object *object;
931
932 if ((flag & REF_ISSYMREF) && (flag & REF_ISBROKEN)) {
933 warning("symbolic ref is dangling: %s", refname);
934 return 0;
935 }
936
937 object = parse_object_or_die(oid, refname);
938 if (object->type != OBJ_COMMIT)
939 return 0;
940
941 add_pending_object(revs, object, "");
942 if (bitmap_is_preferred_refname(revs->repo, refname))
943 object->flags |= NEEDS_BITMAP;
944 return 0;
945}
946
947struct bitmap_commit_cb {
948 struct commit **commits;
949 size_t commits_nr, commits_alloc;
950
951 struct write_midx_context *ctx;
952};
953
954static const struct object_id *bitmap_oid_access(size_t index,
955 const void *_entries)
956{
957 const struct pack_midx_entry *entries = _entries;
958 return &entries[index].oid;
959}
960
961static void bitmap_show_commit(struct commit *commit, void *_data)
962{
963 struct bitmap_commit_cb *data = _data;
964 int pos = oid_pos(&commit->object.oid, data->ctx->entries,
965 data->ctx->entries_nr,
966 bitmap_oid_access);
967 if (pos < 0)
968 return;
969
970 ALLOC_GROW(data->commits, data->commits_nr + 1, data->commits_alloc);
971 data->commits[data->commits_nr++] = commit;
972}
973
08944d1c
TB
974static int read_refs_snapshot(const char *refs_snapshot,
975 struct rev_info *revs)
976{
977 struct strbuf buf = STRBUF_INIT;
978 struct object_id oid;
979 FILE *f = xfopen(refs_snapshot, "r");
980
981 while (strbuf_getline(&buf, f) != EOF) {
982 struct object *object;
983 int preferred = 0;
984 char *hex = buf.buf;
985 const char *end = NULL;
986
987 if (buf.len && *buf.buf == '+') {
988 preferred = 1;
989 hex = &buf.buf[1];
990 }
991
992 if (parse_oid_hex(hex, &oid, &end) < 0)
993 die(_("could not parse line: %s"), buf.buf);
994 if (*end)
995 die(_("malformed line: %s"), buf.buf);
996
997 object = parse_object_or_die(&oid, NULL);
998 if (preferred)
999 object->flags |= NEEDS_BITMAP;
1000
1001 add_pending_object(revs, object, "");
1002 }
1003
1004 fclose(f);
1005 strbuf_release(&buf);
1006 return 0;
1007}
1008
c528e179 1009static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr_p,
08944d1c 1010 const char *refs_snapshot,
c528e179
TB
1011 struct write_midx_context *ctx)
1012{
1013 struct rev_info revs;
1014 struct bitmap_commit_cb cb = {0};
1015
1016 cb.ctx = ctx;
1017
1018 repo_init_revisions(the_repository, &revs, NULL);
08944d1c
TB
1019 if (refs_snapshot) {
1020 read_refs_snapshot(refs_snapshot, &revs);
1021 } else {
1022 setup_revisions(0, NULL, &revs, NULL);
1023 for_each_ref(add_ref_to_pending, &revs);
1024 }
c528e179
TB
1025
1026 /*
1027 * Skipping promisor objects here is intentional, since it only excludes
1028 * them from the list of reachable commits that we want to select from
1029 * when computing the selection of MIDX'd commits to receive bitmaps.
1030 *
1031 * Reachability bitmaps do require that their objects be closed under
1032 * reachability, but fetching any objects missing from promisors at this
1033 * point is too late. But, if one of those objects can be reached from
1034 * an another object that is included in the bitmap, then we will
1035 * complain later that we don't have reachability closure (and fail
1036 * appropriately).
1037 */
1038 fetch_if_missing = 0;
1039 revs.exclude_promisor_objects = 1;
1040
1041 if (prepare_revision_walk(&revs))
1042 die(_("revision walk setup failed"));
1043
1044 traverse_commit_list(&revs, bitmap_show_commit, NULL, &cb);
1045 if (indexed_commits_nr_p)
1046 *indexed_commits_nr_p = cb.commits_nr;
1047
1048 return cb.commits;
1049}
1050
1051static int write_midx_bitmap(char *midx_name, unsigned char *midx_hash,
1052 struct write_midx_context *ctx,
08944d1c 1053 const char *refs_snapshot,
c528e179
TB
1054 unsigned flags)
1055{
1056 struct packing_data pdata;
1057 struct pack_idx_entry **index;
1058 struct commit **commits = NULL;
1059 uint32_t i, commits_nr;
caca3c9f 1060 uint16_t options = 0;
c528e179
TB
1061 char *bitmap_name = xstrfmt("%s-%s.bitmap", midx_name, hash_to_hex(midx_hash));
1062 int ret;
1063
caca3c9f
TB
1064 if (flags & MIDX_WRITE_BITMAP_HASH_CACHE)
1065 options |= BITMAP_OPT_HASH_CACHE;
1066
c528e179
TB
1067 prepare_midx_packing_data(&pdata, ctx);
1068
08944d1c 1069 commits = find_commits_for_midx_bitmap(&commits_nr, refs_snapshot, ctx);
c528e179
TB
1070
1071 /*
1072 * Build the MIDX-order index based on pdata.objects (which is already
1073 * in MIDX order; c.f., 'midx_pack_order_cmp()' for the definition of
1074 * this order).
1075 */
1076 ALLOC_ARRAY(index, pdata.nr_objects);
1077 for (i = 0; i < pdata.nr_objects; i++)
1078 index[i] = &pdata.objects[i].idx;
1079
1080 bitmap_writer_show_progress(flags & MIDX_PROGRESS);
1081 bitmap_writer_build_type_index(&pdata, index, pdata.nr_objects);
1082
1083 /*
1084 * bitmap_writer_finish expects objects in lex order, but pack_order
1085 * gives us exactly that. use it directly instead of re-sorting the
1086 * array.
1087 *
1088 * This changes the order of objects in 'index' between
1089 * bitmap_writer_build_type_index and bitmap_writer_finish.
1090 *
1091 * The same re-ordering takes place in the single-pack bitmap code via
1092 * write_idx_file(), which is called by finish_tmp_packfile(), which
1093 * happens between bitmap_writer_build_type_index() and
1094 * bitmap_writer_finish().
1095 */
1096 for (i = 0; i < pdata.nr_objects; i++)
1097 index[ctx->pack_order[i]] = &pdata.objects[i].idx;
1098
1099 bitmap_writer_select_commits(commits, commits_nr, -1);
1100 ret = bitmap_writer_build(&pdata);
1101 if (ret < 0)
1102 goto cleanup;
1103
1104 bitmap_writer_set_checksum(midx_hash);
caca3c9f 1105 bitmap_writer_finish(index, pdata.nr_objects, bitmap_name, options);
c528e179
TB
1106
1107cleanup:
1108 free(index);
1109 free(bitmap_name);
1110 return ret;
1111}
1112
504131ac
TB
1113static struct multi_pack_index *lookup_multi_pack_index(struct repository *r,
1114 const char *object_dir)
1115{
1116 struct multi_pack_index *cur;
1117
1118 /* Ensure the given object_dir is local, or a known alternate. */
1119 find_odb(r, object_dir);
1120
1121 for (cur = get_multi_pack_index(r); cur; cur = cur->next) {
1122 if (!strcmp(object_dir, cur->object_dir))
1123 return cur;
1124 }
1125
1126 return NULL;
1127}
1128
f57a7396 1129static int write_midx_internal(const char *object_dir,
56d863e9 1130 struct string_list *packs_to_include,
9218c6a4
TB
1131 struct string_list *packs_to_drop,
1132 const char *preferred_pack_name,
08944d1c 1133 const char *refs_snapshot,
9218c6a4 1134 unsigned flags)
a3407730 1135{
60980aed 1136 struct strbuf midx_name = STRBUF_INIT;
9f191611 1137 unsigned char midx_hash[GIT_MAX_RAWSZ];
396f2570 1138 uint32_t i;
fc59e748
DS
1139 struct hashfile *f = NULL;
1140 struct lock_file lk;
577dc496 1141 struct write_midx_context ctx = { 0 };
dba6175c 1142 int pack_name_concat_len = 0;
19575c7c
DS
1143 int dropped_packs = 0;
1144 int result = 0;
63a8f0e9 1145 struct chunkfile *cf;
fc59e748 1146
60980aed
TB
1147 get_midx_filename(&midx_name, object_dir);
1148 if (safe_create_leading_directories(midx_name.buf))
fc59e748 1149 die_errno(_("unable to create leading directories of %s"),
60980aed 1150 midx_name.buf);
fc59e748 1151
56d863e9
TB
1152 if (!packs_to_include) {
1153 /*
1154 * Only reference an existing MIDX when not filtering which
1155 * packs to include, since all packs and objects are copied
1156 * blindly from an existing MIDX if one is present.
1157 */
504131ac 1158 ctx.m = lookup_multi_pack_index(the_repository, object_dir);
f57a7396 1159 }
577dc496 1160
ec1e28ef
TB
1161 if (ctx.m && !midx_checksum_valid(ctx.m)) {
1162 warning(_("ignoring existing multi-pack-index; checksum mismatch"));
1163 ctx.m = NULL;
1164 }
1165
577dc496
DS
1166 ctx.nr = 0;
1167 ctx.alloc = ctx.m ? ctx.m->num_packs : 16;
1168 ctx.info = NULL;
1169 ALLOC_ARRAY(ctx.info, ctx.alloc);
1170
1171 if (ctx.m) {
1172 for (i = 0; i < ctx.m->num_packs; i++) {
1173 ALLOC_GROW(ctx.info, ctx.nr + 1, ctx.alloc);
1174
1175 ctx.info[ctx.nr].orig_pack_int_id = i;
1176 ctx.info[ctx.nr].pack_name = xstrdup(ctx.m->pack_names[i]);
c528e179 1177 ctx.info[ctx.nr].p = ctx.m->packs[i];
577dc496 1178 ctx.info[ctx.nr].expired = 0;
5d3cd09a
TB
1179
1180 if (flags & MIDX_WRITE_REV_INDEX) {
1181 /*
1182 * If generating a reverse index, need to have
1183 * packed_git's loaded to compare their
1184 * mtimes and object count.
1185 */
1186 if (prepare_midx_pack(the_repository, ctx.m, i)) {
1187 error(_("could not load pack"));
1188 result = 1;
1189 goto cleanup;
1190 }
1191
1192 if (open_pack_index(ctx.m->packs[i]))
1193 die(_("could not open index for %s"),
1194 ctx.m->packs[i]->pack_name);
1195 ctx.info[ctx.nr].p = ctx.m->packs[i];
1196 }
1197
577dc496 1198 ctx.nr++;
a40498a1
DS
1199 }
1200 }
1201
577dc496 1202 ctx.pack_paths_checked = 0;
840cef0c 1203 if (flags & MIDX_PROGRESS)
577dc496 1204 ctx.progress = start_delayed_progress(_("Adding packfiles to multi-pack-index"), 0);
840cef0c 1205 else
577dc496 1206 ctx.progress = NULL;
840cef0c 1207
56d863e9
TB
1208 ctx.to_include = packs_to_include;
1209
577dc496
DS
1210 for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &ctx);
1211 stop_progress(&ctx.progress);
396f2570 1212
56d863e9
TB
1213 if ((ctx.m && ctx.nr == ctx.m->num_packs) &&
1214 !(packs_to_include || packs_to_drop)) {
c528e179
TB
1215 struct bitmap_index *bitmap_git;
1216 int bitmap_exists;
1217 int want_bitmap = flags & MIDX_WRITE_BITMAP;
1218
bfbb60d3 1219 bitmap_git = prepare_midx_bitmap_git(ctx.m);
c528e179
TB
1220 bitmap_exists = bitmap_git && bitmap_is_midx(bitmap_git);
1221 free_bitmap_index(bitmap_git);
1222
1223 if (bitmap_exists || !want_bitmap) {
1224 /*
1225 * The correct MIDX already exists, and so does a
1226 * corresponding bitmap (or one wasn't requested).
1227 */
1228 if (!want_bitmap)
1229 clear_midx_files_ext(object_dir, ".bitmap",
1230 NULL);
1231 goto cleanup;
1232 }
1233 }
a40498a1 1234
9218c6a4 1235 if (preferred_pack_name) {
177c0d6e 1236 int found = 0;
9218c6a4
TB
1237 for (i = 0; i < ctx.nr; i++) {
1238 if (!cmp_idx_or_pack_name(preferred_pack_name,
1239 ctx.info[i].pack_name)) {
1240 ctx.preferred_pack_idx = i;
177c0d6e 1241 found = 1;
9218c6a4
TB
1242 break;
1243 }
1244 }
177c0d6e
TB
1245
1246 if (!found)
1247 warning(_("unknown preferred pack: '%s'"),
1248 preferred_pack_name);
c528e179
TB
1249 } else if (ctx.nr &&
1250 (flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP))) {
177c0d6e
TB
1251 struct packed_git *oldest = ctx.info[ctx.preferred_pack_idx].p;
1252 ctx.preferred_pack_idx = 0;
1253
1254 if (packs_to_drop && packs_to_drop->nr)
1255 BUG("cannot write a MIDX bitmap during expiration");
1256
1257 /*
1258 * set a preferred pack when writing a bitmap to ensure that
1259 * the pack from which the first object is selected in pseudo
1260 * pack-order has all of its objects selected from that pack
1261 * (and not another pack containing a duplicate)
1262 */
1263 for (i = 1; i < ctx.nr; i++) {
1264 struct packed_git *p = ctx.info[i].p;
1265
1266 if (!oldest->num_objects || p->mtime < oldest->mtime) {
1267 oldest = p;
1268 ctx.preferred_pack_idx = i;
1269 }
1270 }
1271
1272 if (!oldest->num_objects) {
1273 /*
1274 * If all packs are empty; unset the preferred index.
1275 * This is acceptable since there will be no duplicate
1276 * objects to resolve, so the preferred value doesn't
1277 * matter.
1278 */
1279 ctx.preferred_pack_idx = -1;
1280 }
1281 } else {
1282 /*
1283 * otherwise don't mark any pack as preferred to avoid
1284 * interfering with expiration logic below
1285 */
1286 ctx.preferred_pack_idx = -1;
9218c6a4
TB
1287 }
1288
5d3cd09a
TB
1289 if (ctx.preferred_pack_idx > -1) {
1290 struct packed_git *preferred = ctx.info[ctx.preferred_pack_idx].p;
1291 if (!preferred->num_objects) {
1292 error(_("cannot select preferred pack %s with no objects"),
1293 preferred->pack_name);
1294 result = 1;
1295 goto cleanup;
1296 }
1297 }
1298
9218c6a4
TB
1299 ctx.entries = get_sorted_entries(ctx.m, ctx.info, ctx.nr, &ctx.entries_nr,
1300 ctx.preferred_pack_idx);
a40498a1 1301
7a3ada11 1302 ctx.large_offsets_needed = 0;
31bda9a2
DS
1303 for (i = 0; i < ctx.entries_nr; i++) {
1304 if (ctx.entries[i].offset > 0x7fffffff)
980f525c 1305 ctx.num_large_offsets++;
31bda9a2 1306 if (ctx.entries[i].offset > 0xffffffff)
7a3ada11 1307 ctx.large_offsets_needed = 1;
662148c4 1308 }
fe1ed56f 1309
577dc496 1310 QSORT(ctx.info, ctx.nr, pack_info_compare);
d01bf2e6 1311
19575c7c
DS
1312 if (packs_to_drop && packs_to_drop->nr) {
1313 int drop_index = 0;
1314 int missing_drops = 0;
1315
577dc496
DS
1316 for (i = 0; i < ctx.nr && drop_index < packs_to_drop->nr; i++) {
1317 int cmp = strcmp(ctx.info[i].pack_name,
19575c7c
DS
1318 packs_to_drop->items[drop_index].string);
1319
1320 if (!cmp) {
1321 drop_index++;
577dc496 1322 ctx.info[i].expired = 1;
19575c7c
DS
1323 } else if (cmp > 0) {
1324 error(_("did not see pack-file %s to drop"),
1325 packs_to_drop->items[drop_index].string);
1326 drop_index++;
1327 missing_drops++;
1328 i--;
1329 } else {
577dc496 1330 ctx.info[i].expired = 0;
19575c7c
DS
1331 }
1332 }
1333
1334 if (missing_drops) {
1335 result = 1;
1336 goto cleanup;
1337 }
1338 }
1339
d01bf2e6
DS
1340 /*
1341 * pack_perm stores a permutation between pack-int-ids from the
1342 * previous multi-pack-index to the new one we are writing:
1343 *
1344 * pack_perm[old_id] = new_id
1345 */
7a3ada11 1346 ALLOC_ARRAY(ctx.pack_perm, ctx.nr);
577dc496
DS
1347 for (i = 0; i < ctx.nr; i++) {
1348 if (ctx.info[i].expired) {
19575c7c 1349 dropped_packs++;
7a3ada11 1350 ctx.pack_perm[ctx.info[i].orig_pack_int_id] = PACK_EXPIRED;
19575c7c 1351 } else {
7a3ada11 1352 ctx.pack_perm[ctx.info[i].orig_pack_int_id] = i - dropped_packs;
19575c7c 1353 }
d01bf2e6
DS
1354 }
1355
577dc496
DS
1356 for (i = 0; i < ctx.nr; i++) {
1357 if (!ctx.info[i].expired)
1358 pack_name_concat_len += strlen(ctx.info[i].pack_name) + 1;
19575c7c 1359 }
dba6175c 1360
9218c6a4
TB
1361 /* Check that the preferred pack wasn't expired (if given). */
1362 if (preferred_pack_name) {
1363 struct pack_info *preferred = bsearch(preferred_pack_name,
1364 ctx.info, ctx.nr,
1365 sizeof(*ctx.info),
1366 idx_or_pack_name_cmp);
177c0d6e 1367 if (preferred) {
9218c6a4
TB
1368 uint32_t perm = ctx.pack_perm[preferred->orig_pack_int_id];
1369 if (perm == PACK_EXPIRED)
1370 warning(_("preferred pack '%s' is expired"),
1371 preferred_pack_name);
1372 }
1373 }
1374
dba6175c
DS
1375 if (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT)
1376 pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
1377 (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
1378
60980aed 1379 hold_lock_file_for_update(&lk, midx_name.buf, LOCK_DIE_ON_ERROR);
acd71602 1380 f = hashfd(get_lock_file_fd(&lk), get_lock_file_path(&lk));
fc59e748 1381
577dc496 1382 if (ctx.nr - dropped_packs == 0) {
796d61cd
DR
1383 error(_("no pack files to index."));
1384 result = 1;
1385 goto cleanup;
1386 }
1387
63a8f0e9 1388 cf = init_chunkfile(f);
32f3c541 1389
63a8f0e9
DS
1390 add_chunk(cf, MIDX_CHUNKID_PACKNAMES, pack_name_concat_len,
1391 write_midx_pack_names);
1392 add_chunk(cf, MIDX_CHUNKID_OIDFANOUT, MIDX_CHUNK_FANOUT_SIZE,
1393 write_midx_oid_fanout);
1394 add_chunk(cf, MIDX_CHUNKID_OIDLOOKUP,
329fac3a 1395 (size_t)ctx.entries_nr * the_hash_algo->rawsz,
63a8f0e9
DS
1396 write_midx_oid_lookup);
1397 add_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS,
329fac3a 1398 (size_t)ctx.entries_nr * MIDX_CHUNK_OFFSET_WIDTH,
63a8f0e9 1399 write_midx_object_offsets);
32f3c541 1400
63a8f0e9
DS
1401 if (ctx.large_offsets_needed)
1402 add_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS,
329fac3a 1403 (size_t)ctx.num_large_offsets * MIDX_CHUNK_LARGE_OFFSET_WIDTH,
63a8f0e9 1404 write_midx_large_offsets);
32f3c541 1405
63a8f0e9
DS
1406 write_midx_header(f, get_num_chunks(cf), ctx.nr - dropped_packs);
1407 write_chunkfile(cf, &ctx);
fc59e748 1408
9f191611 1409 finalize_hashfile(f, midx_hash, CSUM_FSYNC | CSUM_HASH_IN_STREAM);
63a8f0e9 1410 free_chunkfile(cf);
38ff7cab 1411
c528e179 1412 if (flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP))
38ff7cab
TB
1413 ctx.pack_order = midx_pack_order(&ctx);
1414
1415 if (flags & MIDX_WRITE_REV_INDEX)
60980aed 1416 write_midx_reverse_index(midx_name.buf, midx_hash, &ctx);
c528e179 1417 if (flags & MIDX_WRITE_BITMAP) {
60980aed 1418 if (write_midx_bitmap(midx_name.buf, midx_hash, &ctx,
08944d1c 1419 refs_snapshot, flags) < 0) {
c528e179
TB
1420 error(_("could not write multi-pack bitmap"));
1421 result = 1;
1422 goto cleanup;
1423 }
1424 }
1425
1426 if (ctx.m)
1427 close_object_store(the_repository->objects);
38ff7cab 1428
ae22e841
TB
1429 if (commit_lock_file(&lk) < 0)
1430 die_errno(_("could not write multi-pack-index"));
fc59e748 1431
c528e179 1432 clear_midx_files_ext(object_dir, ".bitmap", midx_hash);
f5909d34
TB
1433 clear_midx_files_ext(object_dir, ".rev", midx_hash);
1434
a40498a1 1435cleanup:
577dc496
DS
1436 for (i = 0; i < ctx.nr; i++) {
1437 if (ctx.info[i].p) {
1438 close_pack(ctx.info[i].p);
1439 free(ctx.info[i].p);
396f2570 1440 }
577dc496 1441 free(ctx.info[i].pack_name);
396f2570
DS
1442 }
1443
577dc496 1444 free(ctx.info);
31bda9a2 1445 free(ctx.entries);
7a3ada11 1446 free(ctx.pack_perm);
38ff7cab 1447 free(ctx.pack_order);
60980aed 1448 strbuf_release(&midx_name);
c528e179 1449
19575c7c
DS
1450 return result;
1451}
1452
9218c6a4
TB
1453int write_midx_file(const char *object_dir,
1454 const char *preferred_pack_name,
08944d1c 1455 const char *refs_snapshot,
9218c6a4 1456 unsigned flags)
19575c7c 1457{
56d863e9 1458 return write_midx_internal(object_dir, NULL, NULL, preferred_pack_name,
08944d1c 1459 refs_snapshot, flags);
56d863e9
TB
1460}
1461
1462int write_midx_file_only(const char *object_dir,
1463 struct string_list *packs_to_include,
1464 const char *preferred_pack_name,
08944d1c 1465 const char *refs_snapshot,
56d863e9
TB
1466 unsigned flags)
1467{
1468 return write_midx_internal(object_dir, packs_to_include, NULL,
08944d1c 1469 preferred_pack_name, refs_snapshot, flags);
a3407730 1470}
525e18c0 1471
38ff7cab
TB
1472struct clear_midx_data {
1473 char *keep;
1474 const char *ext;
1475};
1476
1477static void clear_midx_file_ext(const char *full_path, size_t full_path_len,
1478 const char *file_name, void *_data)
1479{
1480 struct clear_midx_data *data = _data;
1481
1482 if (!(starts_with(file_name, "multi-pack-index-") &&
1483 ends_with(file_name, data->ext)))
1484 return;
1485 if (data->keep && !strcmp(data->keep, file_name))
1486 return;
1487
1488 if (unlink(full_path))
1489 die_errno(_("failed to remove %s"), full_path);
1490}
1491
426c00e4 1492static void clear_midx_files_ext(const char *object_dir, const char *ext,
38ff7cab
TB
1493 unsigned char *keep_hash)
1494{
1495 struct clear_midx_data data;
1496 memset(&data, 0, sizeof(struct clear_midx_data));
1497
1498 if (keep_hash)
1499 data.keep = xstrfmt("multi-pack-index-%s%s",
1500 hash_to_hex(keep_hash), ext);
1501 data.ext = ext;
1502
426c00e4 1503 for_each_file_in_pack_dir(object_dir,
38ff7cab
TB
1504 clear_midx_file_ext,
1505 &data);
1506
1507 free(data.keep);
a3407730 1508}
525e18c0 1509
1dcd9f20 1510void clear_midx_file(struct repository *r)
525e18c0 1511{
60980aed
TB
1512 struct strbuf midx = STRBUF_INIT;
1513
1514 get_midx_filename(&midx, r->objects->odb->path);
1dcd9f20
DS
1515
1516 if (r->objects && r->objects->multi_pack_index) {
1517 close_midx(r->objects->multi_pack_index);
1518 r->objects->multi_pack_index = NULL;
1519 }
525e18c0 1520
60980aed
TB
1521 if (remove_path(midx.buf))
1522 die(_("failed to clear multi-pack-index at %s"), midx.buf);
525e18c0 1523
c528e179 1524 clear_midx_files_ext(r->objects->odb->path, ".bitmap", NULL);
426c00e4 1525 clear_midx_files_ext(r->objects->odb->path, ".rev", NULL);
38ff7cab 1526
60980aed 1527 strbuf_release(&midx);
525e18c0 1528}
56ee7ff1
DS
1529
1530static int verify_midx_error;
1531
48ca53ca 1532__attribute__((format (printf, 1, 2)))
d4bf1d88
DS
1533static void midx_report(const char *fmt, ...)
1534{
1535 va_list ap;
1536 verify_midx_error = 1;
1537 va_start(ap, fmt);
1538 vfprintf(stderr, fmt, ap);
1539 fprintf(stderr, "\n");
1540 va_end(ap);
1541}
1542
5ae18df9
JH
1543struct pair_pos_vs_id
1544{
1545 uint32_t pos;
1546 uint32_t pack_int_id;
1547};
1548
1549static int compare_pair_pos_vs_id(const void *_a, const void *_b)
1550{
1551 struct pair_pos_vs_id *a = (struct pair_pos_vs_id *)_a;
1552 struct pair_pos_vs_id *b = (struct pair_pos_vs_id *)_b;
1553
1554 return b->pack_int_id - a->pack_int_id;
1555}
1556
430efb8a
JH
1557/*
1558 * Limit calls to display_progress() for performance reasons.
1559 * The interval here was arbitrarily chosen.
1560 */
1561#define SPARSE_PROGRESS_INTERVAL (1 << 12)
1562#define midx_display_sparse_progress(progress, n) \
1563 do { \
1564 uint64_t _n = (n); \
1565 if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \
1566 display_progress(progress, _n); \
1567 } while (0)
1568
efbc3aee 1569int verify_midx_file(struct repository *r, const char *object_dir, unsigned flags)
56ee7ff1 1570{
5ae18df9 1571 struct pair_pos_vs_id *pairs = NULL;
d4bf1d88 1572 uint32_t i;
ad60096d 1573 struct progress *progress = NULL;
56ee7ff1
DS
1574 struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
1575 verify_midx_error = 0;
1576
d9607542
DS
1577 if (!m) {
1578 int result = 0;
1579 struct stat sb;
60980aed
TB
1580 struct strbuf filename = STRBUF_INIT;
1581
1582 get_midx_filename(&filename, object_dir);
1583
1584 if (!stat(filename.buf, &sb)) {
d9607542
DS
1585 error(_("multi-pack-index file exists, but failed to parse"));
1586 result = 1;
1587 }
60980aed 1588 strbuf_release(&filename);
d9607542
DS
1589 return result;
1590 }
56ee7ff1 1591
f89ecf79
TB
1592 if (!midx_checksum_valid(m))
1593 midx_report(_("incorrect checksum"));
1594
ad60096d 1595 if (flags & MIDX_PROGRESS)
efdd2f0d 1596 progress = start_delayed_progress(_("Looking for referenced packfiles"),
ad60096d 1597 m->num_packs);
d4bf1d88 1598 for (i = 0; i < m->num_packs; i++) {
64404a24 1599 if (prepare_midx_pack(r, m, i))
d4bf1d88 1600 midx_report("failed to load pack in position %d", i);
430efb8a
JH
1601
1602 display_progress(progress, i + 1);
d4bf1d88 1603 }
430efb8a 1604 stop_progress(&progress);
d4bf1d88 1605
2f23d3f3
DS
1606 for (i = 0; i < 255; i++) {
1607 uint32_t oid_fanout1 = ntohl(m->chunk_oid_fanout[i]);
1608 uint32_t oid_fanout2 = ntohl(m->chunk_oid_fanout[i + 1]);
1609
1610 if (oid_fanout1 > oid_fanout2)
1611 midx_report(_("oid fanout out of order: fanout[%d] = %"PRIx32" > %"PRIx32" = fanout[%d]"),
1612 i, oid_fanout1, oid_fanout2, i + 1);
1613 }
1614
796d61cd
DR
1615 if (m->num_objects == 0) {
1616 midx_report(_("the midx contains no oid"));
1617 /*
1618 * Remaining tests assume that we have objects, so we can
1619 * return here.
1620 */
492cb394 1621 goto cleanup;
796d61cd
DR
1622 }
1623
ad60096d
WB
1624 if (flags & MIDX_PROGRESS)
1625 progress = start_sparse_progress(_("Verifying OID order in multi-pack-index"),
1626 m->num_objects - 1);
55c5648d
DS
1627 for (i = 0; i < m->num_objects - 1; i++) {
1628 struct object_id oid1, oid2;
1629
1630 nth_midxed_object_oid(&oid1, m, i);
1631 nth_midxed_object_oid(&oid2, m, i + 1);
1632
1633 if (oidcmp(&oid1, &oid2) >= 0)
1634 midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"),
1635 i, oid_to_hex(&oid1), oid_to_hex(&oid2), i + 1);
430efb8a
JH
1636
1637 midx_display_sparse_progress(progress, i + 1);
55c5648d 1638 }
430efb8a 1639 stop_progress(&progress);
55c5648d 1640
5ae18df9
JH
1641 /*
1642 * Create an array mapping each object to its packfile id. Sort it
1643 * to group the objects by packfile. Use this permutation to visit
1644 * each of the objects and only require 1 packfile to be open at a
1645 * time.
1646 */
1647 ALLOC_ARRAY(pairs, m->num_objects);
1648 for (i = 0; i < m->num_objects; i++) {
1649 pairs[i].pos = i;
1650 pairs[i].pack_int_id = nth_midxed_pack_int_id(m, i);
1651 }
1652
ad60096d
WB
1653 if (flags & MIDX_PROGRESS)
1654 progress = start_sparse_progress(_("Sorting objects by packfile"),
1655 m->num_objects);
5ae18df9
JH
1656 display_progress(progress, 0); /* TODO: Measure QSORT() progress */
1657 QSORT(pairs, m->num_objects, compare_pair_pos_vs_id);
1658 stop_progress(&progress);
1659
ad60096d
WB
1660 if (flags & MIDX_PROGRESS)
1661 progress = start_sparse_progress(_("Verifying object offsets"), m->num_objects);
cc6af73c
DS
1662 for (i = 0; i < m->num_objects; i++) {
1663 struct object_id oid;
1664 struct pack_entry e;
1665 off_t m_offset, p_offset;
1666
5ae18df9
JH
1667 if (i > 0 && pairs[i-1].pack_int_id != pairs[i].pack_int_id &&
1668 m->packs[pairs[i-1].pack_int_id])
1669 {
1670 close_pack_fd(m->packs[pairs[i-1].pack_int_id]);
1671 close_pack_index(m->packs[pairs[i-1].pack_int_id]);
1672 }
1673
1674 nth_midxed_object_oid(&oid, m, pairs[i].pos);
1675
64404a24 1676 if (!fill_midx_entry(r, &oid, &e, m)) {
cc6af73c 1677 midx_report(_("failed to load pack entry for oid[%d] = %s"),
5ae18df9 1678 pairs[i].pos, oid_to_hex(&oid));
cc6af73c
DS
1679 continue;
1680 }
1681
1682 if (open_pack_index(e.p)) {
1683 midx_report(_("failed to load pack-index for packfile %s"),
1684 e.p->pack_name);
1685 break;
1686 }
1687
1688 m_offset = e.offset;
1689 p_offset = find_pack_entry_one(oid.hash, e.p);
1690
1691 if (m_offset != p_offset)
1692 midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64" != %"PRIx64),
5ae18df9 1693 pairs[i].pos, oid_to_hex(&oid), m_offset, p_offset);
144d7033 1694
430efb8a 1695 midx_display_sparse_progress(progress, i + 1);
cc6af73c 1696 }
144d7033 1697 stop_progress(&progress);
cc6af73c 1698
492cb394 1699cleanup:
5ae18df9 1700 free(pairs);
492cb394 1701 close_midx(m);
5ae18df9 1702
56ee7ff1
DS
1703 return verify_midx_error;
1704}
cff97116 1705
efbc3aee 1706int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags)
cff97116 1707{
19575c7c
DS
1708 uint32_t i, *count, result = 0;
1709 struct string_list packs_to_drop = STRING_LIST_INIT_DUP;
98926e0d 1710 struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
8dc18f89 1711 struct progress *progress = NULL;
19575c7c
DS
1712
1713 if (!m)
1714 return 0;
1715
ca56dadb 1716 CALLOC_ARRAY(count, m->num_packs);
8dc18f89
WB
1717
1718 if (flags & MIDX_PROGRESS)
efdd2f0d 1719 progress = start_delayed_progress(_("Counting referenced objects"),
8dc18f89 1720 m->num_objects);
19575c7c
DS
1721 for (i = 0; i < m->num_objects; i++) {
1722 int pack_int_id = nth_midxed_pack_int_id(m, i);
1723 count[pack_int_id]++;
8dc18f89 1724 display_progress(progress, i + 1);
19575c7c 1725 }
8dc18f89 1726 stop_progress(&progress);
19575c7c 1727
8dc18f89 1728 if (flags & MIDX_PROGRESS)
efdd2f0d 1729 progress = start_delayed_progress(_("Finding and deleting unreferenced packfiles"),
8dc18f89 1730 m->num_packs);
19575c7c
DS
1731 for (i = 0; i < m->num_packs; i++) {
1732 char *pack_name;
8dc18f89 1733 display_progress(progress, i + 1);
19575c7c
DS
1734
1735 if (count[i])
1736 continue;
1737
1738 if (prepare_midx_pack(r, m, i))
1739 continue;
1740
1741 if (m->packs[i]->pack_keep)
1742 continue;
1743
1744 pack_name = xstrdup(m->packs[i]->pack_name);
1745 close_pack(m->packs[i]);
1746
1747 string_list_insert(&packs_to_drop, m->pack_names[i]);
1748 unlink_pack_path(pack_name, 0);
1749 free(pack_name);
1750 }
8dc18f89 1751 stop_progress(&progress);
19575c7c
DS
1752
1753 free(count);
1754
98926e0d 1755 if (packs_to_drop.nr)
08944d1c 1756 result = write_midx_internal(object_dir, NULL, &packs_to_drop, NULL, NULL, flags);
19575c7c
DS
1757
1758 string_list_clear(&packs_to_drop, 0);
98926e0d 1759
19575c7c 1760 return result;
cff97116 1761}
2af890bb 1762
ce1e4a10
DS
1763struct repack_info {
1764 timestamp_t mtime;
1765 uint32_t referenced_objects;
1766 uint32_t pack_int_id;
1767};
1768
1769static int compare_by_mtime(const void *a_, const void *b_)
2af890bb 1770{
ce1e4a10
DS
1771 const struct repack_info *a, *b;
1772
1773 a = (const struct repack_info *)a_;
1774 b = (const struct repack_info *)b_;
1775
1776 if (a->mtime < b->mtime)
1777 return -1;
1778 if (a->mtime > b->mtime)
1779 return 1;
1780 return 0;
1781}
1782
3ce4ca0a
DS
1783static int fill_included_packs_all(struct repository *r,
1784 struct multi_pack_index *m,
ce1e4a10
DS
1785 unsigned char *include_pack)
1786{
3ce4ca0a
DS
1787 uint32_t i, count = 0;
1788 int pack_kept_objects = 0;
1789
1790 repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
1791
1792 for (i = 0; i < m->num_packs; i++) {
1793 if (prepare_midx_pack(r, m, i))
1794 continue;
1795 if (!pack_kept_objects && m->packs[i]->pack_keep)
1796 continue;
ce1e4a10 1797
ce1e4a10 1798 include_pack[i] = 1;
3ce4ca0a
DS
1799 count++;
1800 }
ce1e4a10 1801
3ce4ca0a 1802 return count < 2;
ce1e4a10
DS
1803}
1804
1805static int fill_included_packs_batch(struct repository *r,
1806 struct multi_pack_index *m,
1807 unsigned char *include_pack,
1808 size_t batch_size)
1809{
1810 uint32_t i, packs_to_repack;
1811 size_t total_size;
1812 struct repack_info *pack_info = xcalloc(m->num_packs, sizeof(struct repack_info));
3ce4ca0a
DS
1813 int pack_kept_objects = 0;
1814
1815 repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
ce1e4a10
DS
1816
1817 for (i = 0; i < m->num_packs; i++) {
1818 pack_info[i].pack_int_id = i;
1819
1820 if (prepare_midx_pack(r, m, i))
1821 continue;
1822
1823 pack_info[i].mtime = m->packs[i]->mtime;
1824 }
1825
1826 for (i = 0; batch_size && i < m->num_objects; i++) {
1827 uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
1828 pack_info[pack_int_id].referenced_objects++;
1829 }
1830
1831 QSORT(pack_info, m->num_packs, compare_by_mtime);
1832
1833 total_size = 0;
1834 packs_to_repack = 0;
1835 for (i = 0; total_size < batch_size && i < m->num_packs; i++) {
1836 int pack_int_id = pack_info[i].pack_int_id;
1837 struct packed_git *p = m->packs[pack_int_id];
1838 size_t expected_size;
1839
1840 if (!p)
1841 continue;
3ce4ca0a
DS
1842 if (!pack_kept_objects && p->pack_keep)
1843 continue;
ce1e4a10
DS
1844 if (open_pack_index(p) || !p->num_objects)
1845 continue;
1846
1847 expected_size = (size_t)(p->pack_size
1848 * pack_info[i].referenced_objects);
1849 expected_size /= p->num_objects;
1850
1851 if (expected_size >= batch_size)
1852 continue;
1853
1854 packs_to_repack++;
1855 total_size += expected_size;
1856 include_pack[pack_int_id] = 1;
1857 }
1858
1859 free(pack_info);
1860
1eb22c7d 1861 if (packs_to_repack < 2)
ce1e4a10
DS
1862 return 1;
1863
2af890bb
DS
1864 return 0;
1865}
ce1e4a10 1866
efbc3aee 1867int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, unsigned flags)
ce1e4a10
DS
1868{
1869 int result = 0;
1870 uint32_t i;
1871 unsigned char *include_pack;
1872 struct child_process cmd = CHILD_PROCESS_INIT;
6af3b00a 1873 FILE *cmd_in;
ce1e4a10 1874 struct strbuf base_name = STRBUF_INIT;
c0f1f9de 1875 struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
ce1e4a10 1876
e11d86de
SLN
1877 /*
1878 * When updating the default for these configuration
1879 * variables in builtin/repack.c, these must be adjusted
1880 * to match.
1881 */
1882 int delta_base_offset = 1;
1883 int use_delta_islands = 0;
1884
ce1e4a10
DS
1885 if (!m)
1886 return 0;
1887
ca56dadb 1888 CALLOC_ARRAY(include_pack, m->num_packs);
ce1e4a10
DS
1889
1890 if (batch_size) {
1891 if (fill_included_packs_batch(r, m, include_pack, batch_size))
1892 goto cleanup;
3ce4ca0a 1893 } else if (fill_included_packs_all(r, m, include_pack))
ce1e4a10
DS
1894 goto cleanup;
1895
e11d86de
SLN
1896 repo_config_get_bool(r, "repack.usedeltabaseoffset", &delta_base_offset);
1897 repo_config_get_bool(r, "repack.usedeltaislands", &use_delta_islands);
1898
c972bf4c 1899 strvec_push(&cmd.args, "pack-objects");
ce1e4a10
DS
1900
1901 strbuf_addstr(&base_name, object_dir);
1902 strbuf_addstr(&base_name, "/pack/pack");
c972bf4c 1903 strvec_push(&cmd.args, base_name.buf);
64d80e7d 1904
e11d86de 1905 if (delta_base_offset)
c972bf4c 1906 strvec_push(&cmd.args, "--delta-base-offset");
e11d86de 1907 if (use_delta_islands)
c972bf4c 1908 strvec_push(&cmd.args, "--delta-islands");
e11d86de 1909
64d80e7d 1910 if (flags & MIDX_PROGRESS)
c972bf4c 1911 strvec_push(&cmd.args, "--progress");
64d80e7d 1912 else
c972bf4c 1913 strvec_push(&cmd.args, "-q");
64d80e7d 1914
ce1e4a10
DS
1915 strbuf_release(&base_name);
1916
1917 cmd.git_cmd = 1;
1918 cmd.in = cmd.out = -1;
1919
1920 if (start_command(&cmd)) {
1921 error(_("could not start pack-objects"));
1922 result = 1;
1923 goto cleanup;
1924 }
1925
6af3b00a
RS
1926 cmd_in = xfdopen(cmd.in, "w");
1927
ce1e4a10
DS
1928 for (i = 0; i < m->num_objects; i++) {
1929 struct object_id oid;
1930 uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
1931
1932 if (!include_pack[pack_int_id])
1933 continue;
1934
1935 nth_midxed_object_oid(&oid, m, i);
6af3b00a 1936 fprintf(cmd_in, "%s\n", oid_to_hex(&oid));
ce1e4a10 1937 }
6af3b00a 1938 fclose(cmd_in);
ce1e4a10
DS
1939
1940 if (finish_command(&cmd)) {
1941 error(_("could not finish pack-objects"));
1942 result = 1;
1943 goto cleanup;
1944 }
1945
08944d1c 1946 result = write_midx_internal(object_dir, NULL, NULL, NULL, NULL, flags);
ce1e4a10
DS
1947
1948cleanup:
ce1e4a10
DS
1949 free(include_pack);
1950 return result;
1951}