blob: b500174d1f3d1a895463a13fb59850eb2f1b88f5 [file] [log] [blame]
Elijah Newren5e3f94d2023-04-22 20:17:23 +00001#include "git-compat-util.h"
Elijah Newren0b027f62023-03-21 06:25:58 +00002#include "abspath.h"
Elijah Newren36bf1952023-02-24 00:09:24 +00003#include "alloc.h"
Derrick Stoleec4d25222018-07-12 15:39:33 -04004#include "config.h"
Derrick Stoleefc59e742018-07-12 15:39:22 -04005#include "csum-file.h"
Derrick Stolee396f2572018-07-12 15:39:26 -04006#include "dir.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00007#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00008#include "hex.h"
Derrick Stoleefc59e742018-07-12 15:39:22 -04009#include "lockfile.h"
Derrick Stolee396f2572018-07-12 15:39:26 -040010#include "packfile.h"
Elijah Newren87bed172023-04-11 00:41:53 -070011#include "object-file.h"
Derrick Stolee4d805602018-07-12 15:39:23 -040012#include "object-store.h"
Martin Ågrenbc626922020-12-31 12:56:23 +010013#include "hash-lookup.h"
Derrick Stoleea3407732018-07-12 15:39:21 -040014#include "midx.h"
Derrick Stolee144d7032018-09-13 11:02:26 -070015#include "progress.h"
Jeff Hostetlerd8292232019-03-21 12:36:13 -070016#include "trace2.h"
Derrick Stoleece1e4a12019-06-10 16:35:27 -070017#include "run-command.h"
Derrick Stolee18e449f2020-09-25 12:33:34 +000018#include "repository.h"
Derrick Stolee63a8f0e2021-02-18 14:07:33 +000019#include "chunk-format.h"
Taylor Blau38ff7ca2021-03-30 11:04:32 -040020#include "pack.h"
Taylor Blauc528e172021-08-31 16:52:24 -040021#include "pack-bitmap.h"
22#include "refs.h"
23#include "revision.h"
24#include "list-objects.h"
Derrick Stoleea3407732018-07-12 15:39:21 -040025
Derrick Stoleefc59e742018-07-12 15:39:22 -040026#define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
27#define MIDX_VERSION 1
Derrick Stolee4d805602018-07-12 15:39:23 -040028#define MIDX_BYTE_FILE_VERSION 4
29#define MIDX_BYTE_HASH_VERSION 5
30#define MIDX_BYTE_NUM_CHUNKS 6
31#define MIDX_BYTE_NUM_PACKS 8
Derrick Stoleefc59e742018-07-12 15:39:22 -040032#define MIDX_HEADER_SIZE 12
brian m. carlsonaaa95df2019-08-18 20:04:27 +000033#define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
Derrick Stoleefc59e742018-07-12 15:39:22 -040034
Derrick Stolee32f3c542018-07-12 15:39:27 -040035#define MIDX_CHUNK_ALIGNMENT 4
36#define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */
Derrick Stoleed7cacf22018-07-12 15:39:31 -040037#define MIDX_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
Derrick Stolee0d5b3a52018-07-12 15:39:30 -040038#define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
Derrick Stolee662148c2018-07-12 15:39:32 -040039#define MIDX_CHUNKID_OBJECTOFFSETS 0x4f4f4646 /* "OOFF" */
40#define MIDX_CHUNKID_LARGEOFFSETS 0x4c4f4646 /* "LOFF" */
Taylor Blau95e83832022-01-25 17:41:03 -050041#define MIDX_CHUNKID_REVINDEX 0x52494458 /* "RIDX" */
Derrick Stoleed7cacf22018-07-12 15:39:31 -040042#define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256)
Derrick Stolee662148c2018-07-12 15:39:32 -040043#define MIDX_CHUNK_OFFSET_WIDTH (2 * sizeof(uint32_t))
44#define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t))
45#define MIDX_LARGE_OFFSET_NEEDED 0x80000000
Derrick Stolee32f3c542018-07-12 15:39:27 -040046
Derrick Stolee19575c72019-06-10 16:35:25 -070047#define PACK_EXPIRED UINT_MAX
48
Taylor Blau0f533c72021-08-31 16:52:21 -040049const unsigned char *get_midx_checksum(struct multi_pack_index *m)
Taylor Blauf8940812021-03-30 11:04:26 -040050{
51 return m->data + m->data_len - the_hash_algo->rawsz;
52}
53
Taylor Blau60980ae2021-10-26 17:01:21 -040054void get_midx_filename(struct strbuf *out, const char *object_dir)
Derrick Stoleefc59e742018-07-12 15:39:22 -040055{
Taylor Blau60980ae2021-10-26 17:01:21 -040056 strbuf_addf(out, "%s/pack/multi-pack-index", object_dir);
Derrick Stoleefc59e742018-07-12 15:39:22 -040057}
58
Taylor Blau60980ae2021-10-26 17:01:21 -040059void get_midx_rev_filename(struct strbuf *out, struct multi_pack_index *m)
Taylor Blauf8940812021-03-30 11:04:26 -040060{
Taylor Blau60980ae2021-10-26 17:01:21 -040061 get_midx_filename(out, m->object_dir);
62 strbuf_addf(out, "-%s.rev", hash_to_hex(get_midx_checksum(m)));
Taylor Blauf8940812021-03-30 11:04:26 -040063}
64
Derrick Stolee6ab3b8b2021-02-18 14:07:36 +000065static int midx_read_oid_fanout(const unsigned char *chunk_start,
66 size_t chunk_size, void *data)
67{
68 struct multi_pack_index *m = data;
69 m->chunk_oid_fanout = (uint32_t *)chunk_start;
70
71 if (chunk_size != 4 * 256) {
72 error(_("multi-pack-index OID fanout is of the wrong size"));
73 return 1;
74 }
75 return 0;
76}
77
Derrick Stolee2cf489a2018-08-20 16:51:55 +000078struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local)
Derrick Stolee4d805602018-07-12 15:39:23 -040079{
80 struct multi_pack_index *m = NULL;
81 int fd;
82 struct stat st;
83 size_t midx_size;
84 void *midx_map = NULL;
85 uint32_t hash_version;
Taylor Blau60980ae2021-10-26 17:01:21 -040086 struct strbuf midx_name = STRBUF_INIT;
Derrick Stolee32f3c542018-07-12 15:39:27 -040087 uint32_t i;
Derrick Stolee32275652018-07-12 15:39:28 -040088 const char *cur_pack_name;
Derrick Stolee6ab3b8b2021-02-18 14:07:36 +000089 struct chunkfile *cf = NULL;
Derrick Stolee4d805602018-07-12 15:39:23 -040090
Taylor Blau60980ae2021-10-26 17:01:21 -040091 get_midx_filename(&midx_name, object_dir);
92
93 fd = git_open(midx_name.buf);
Derrick Stolee4d805602018-07-12 15:39:23 -040094
95 if (fd < 0)
96 goto cleanup_fail;
97 if (fstat(fd, &st)) {
Taylor Blau60980ae2021-10-26 17:01:21 -040098 error_errno(_("failed to read %s"), midx_name.buf);
Derrick Stolee4d805602018-07-12 15:39:23 -040099 goto cleanup_fail;
100 }
101
102 midx_size = xsize_t(st.st_size);
103
104 if (midx_size < MIDX_MIN_SIZE) {
Taylor Blau60980ae2021-10-26 17:01:21 -0400105 error(_("multi-pack-index file %s is too small"), midx_name.buf);
Derrick Stolee4d805602018-07-12 15:39:23 -0400106 goto cleanup_fail;
107 }
108
Taylor Blau60980ae2021-10-26 17:01:21 -0400109 strbuf_release(&midx_name);
Derrick Stolee4d805602018-07-12 15:39:23 -0400110
111 midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0);
Derrick Stolee6c7ff7c2020-04-24 09:17:16 -0400112 close(fd);
Derrick Stolee4d805602018-07-12 15:39:23 -0400113
Denton Liu577314c2019-04-03 15:00:05 -0700114 FLEX_ALLOC_STR(m, object_dir, object_dir);
Derrick Stolee4d805602018-07-12 15:39:23 -0400115 m->data = midx_map;
116 m->data_len = midx_size;
Derrick Stolee2cf489a2018-08-20 16:51:55 +0000117 m->local = local;
Derrick Stolee4d805602018-07-12 15:39:23 -0400118
119 m->signature = get_be32(m->data);
Derrick Stolee53ad0402018-09-13 11:02:15 -0700120 if (m->signature != MIDX_SIGNATURE)
121 die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
Derrick Stolee4d805602018-07-12 15:39:23 -0400122 m->signature, MIDX_SIGNATURE);
Derrick Stolee4d805602018-07-12 15:39:23 -0400123
124 m->version = m->data[MIDX_BYTE_FILE_VERSION];
Derrick Stolee53ad0402018-09-13 11:02:15 -0700125 if (m->version != MIDX_VERSION)
126 die(_("multi-pack-index version %d not recognized"),
Derrick Stolee4d805602018-07-12 15:39:23 -0400127 m->version);
Derrick Stolee4d805602018-07-12 15:39:23 -0400128
129 hash_version = m->data[MIDX_BYTE_HASH_VERSION];
Taylor Blaud9fef9d2022-05-20 19:17:41 -0400130 if (hash_version != oid_version(the_hash_algo)) {
Derrick Stoleed9607542020-08-17 14:04:48 +0000131 error(_("multi-pack-index hash version %u does not match version %u"),
Taylor Blaud9fef9d2022-05-20 19:17:41 -0400132 hash_version, oid_version(the_hash_algo));
Derrick Stoleed9607542020-08-17 14:04:48 +0000133 goto cleanup_fail;
134 }
brian m. carlsonaaa95df2019-08-18 20:04:27 +0000135 m->hash_len = the_hash_algo->rawsz;
Derrick Stolee4d805602018-07-12 15:39:23 -0400136
137 m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS];
138
139 m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS);
140
Derrick Stolee6ab3b8b2021-02-18 14:07:36 +0000141 cf = init_chunkfile(NULL);
Derrick Stolee32f3c542018-07-12 15:39:27 -0400142
Derrick Stolee6ab3b8b2021-02-18 14:07:36 +0000143 if (read_table_of_contents(cf, m->data, midx_size,
144 MIDX_HEADER_SIZE, m->num_chunks))
145 goto cleanup_fail;
Derrick Stoleed3f8e212018-09-13 11:02:16 -0700146
Derrick Stolee6ab3b8b2021-02-18 14:07:36 +0000147 if (pair_chunk(cf, MIDX_CHUNKID_PACKNAMES, &m->chunk_pack_names) == CHUNK_NOT_FOUND)
Derrick Stolee32f3c542018-07-12 15:39:27 -0400148 die(_("multi-pack-index missing required pack-name chunk"));
Derrick Stolee6ab3b8b2021-02-18 14:07:36 +0000149 if (read_chunk(cf, MIDX_CHUNKID_OIDFANOUT, midx_read_oid_fanout, m) == CHUNK_NOT_FOUND)
Derrick Stoleed7cacf22018-07-12 15:39:31 -0400150 die(_("multi-pack-index missing required OID fanout chunk"));
Derrick Stolee6ab3b8b2021-02-18 14:07:36 +0000151 if (pair_chunk(cf, MIDX_CHUNKID_OIDLOOKUP, &m->chunk_oid_lookup) == CHUNK_NOT_FOUND)
Derrick Stolee0d5b3a52018-07-12 15:39:30 -0400152 die(_("multi-pack-index missing required OID lookup chunk"));
Derrick Stolee6ab3b8b2021-02-18 14:07:36 +0000153 if (pair_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS, &m->chunk_object_offsets) == CHUNK_NOT_FOUND)
Derrick Stolee662148c2018-07-12 15:39:32 -0400154 die(_("multi-pack-index missing required object offsets chunk"));
Derrick Stolee32f3c542018-07-12 15:39:27 -0400155
Derrick Stolee6ab3b8b2021-02-18 14:07:36 +0000156 pair_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS, &m->chunk_large_offsets);
157
Taylor Blau7f514b72022-01-25 17:41:17 -0500158 if (git_env_bool("GIT_TEST_MIDX_READ_RIDX", 1))
159 pair_chunk(cf, MIDX_CHUNKID_REVINDEX, &m->chunk_revindex);
160
Derrick Stoleed7cacf22018-07-12 15:39:31 -0400161 m->num_objects = ntohl(m->chunk_oid_fanout[255]);
162
René Scharfeca56dad2021-03-13 17:17:22 +0100163 CALLOC_ARRAY(m->pack_names, m->num_packs);
164 CALLOC_ARRAY(m->packs, m->num_packs);
Derrick Stolee32275652018-07-12 15:39:28 -0400165
166 cur_pack_name = (const char *)m->chunk_pack_names;
167 for (i = 0; i < m->num_packs; i++) {
168 m->pack_names[i] = cur_pack_name;
169
170 cur_pack_name += strlen(cur_pack_name) + 1;
171
Derrick Stolee8e72a3c2018-09-13 11:02:18 -0700172 if (i && strcmp(m->pack_names[i], m->pack_names[i - 1]) <= 0)
173 die(_("multi-pack-index pack names out of order: '%s' before '%s'"),
Derrick Stolee32275652018-07-12 15:39:28 -0400174 m->pack_names[i - 1],
175 m->pack_names[i]);
Derrick Stolee32275652018-07-12 15:39:28 -0400176 }
177
Jeff Hostetlerd8292232019-03-21 12:36:13 -0700178 trace2_data_intmax("midx", the_repository, "load/num_packs", m->num_packs);
179 trace2_data_intmax("midx", the_repository, "load/num_objects", m->num_objects);
180
Taylor Blau692305e2021-10-20 23:39:47 -0400181 free_chunkfile(cf);
Derrick Stolee4d805602018-07-12 15:39:23 -0400182 return m;
183
184cleanup_fail:
185 free(m);
Taylor Blau60980ae2021-10-26 17:01:21 -0400186 strbuf_release(&midx_name);
Taylor Blau692305e2021-10-20 23:39:47 -0400187 free_chunkfile(cf);
Derrick Stolee4d805602018-07-12 15:39:23 -0400188 if (midx_map)
189 munmap(midx_map, midx_size);
190 if (0 <= fd)
191 close(fd);
192 return NULL;
193}
194
Derrick Stolee1dcd9f22018-10-12 10:34:19 -0700195void close_midx(struct multi_pack_index *m)
Derrick Stoleea40498a2018-07-12 15:39:36 -0400196{
197 uint32_t i;
Derrick Stolee1dcd9f22018-10-12 10:34:19 -0700198
199 if (!m)
200 return;
201
Taylor Blau9bb6c2e2021-08-31 16:52:07 -0400202 close_midx(m->next);
203
Derrick Stoleea40498a2018-07-12 15:39:36 -0400204 munmap((unsigned char *)m->data, m->data_len);
Derrick Stoleea40498a2018-07-12 15:39:36 -0400205
206 for (i = 0; i < m->num_packs; i++) {
Derrick Stoleeaf96fe32019-04-29 09:18:56 -0700207 if (m->packs[i])
208 m->packs[i]->multi_pack_index = 0;
Derrick Stoleea40498a2018-07-12 15:39:36 -0400209 }
210 FREE_AND_NULL(m->packs);
211 FREE_AND_NULL(m->pack_names);
Taylor Blau9bb6c2e2021-08-31 16:52:07 -0400212 free(m);
Derrick Stoleea40498a2018-07-12 15:39:36 -0400213}
214
Derrick Stolee64404a22019-04-29 09:18:55 -0700215int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id)
Derrick Stolee3715a632018-07-12 15:39:34 -0400216{
217 struct strbuf pack_name = STRBUF_INIT;
Derrick Stoleeaf96fe32019-04-29 09:18:56 -0700218 struct packed_git *p;
Derrick Stolee3715a632018-07-12 15:39:34 -0400219
220 if (pack_int_id >= m->num_packs)
Jean-Noël Avilad355e462018-11-28 22:43:09 +0100221 die(_("bad pack-int-id: %u (%u total packs)"),
Derrick Stoleecc6af732018-09-13 11:02:25 -0700222 pack_int_id, m->num_packs);
Derrick Stolee3715a632018-07-12 15:39:34 -0400223
224 if (m->packs[pack_int_id])
225 return 0;
226
227 strbuf_addf(&pack_name, "%s/pack/%s", m->object_dir,
228 m->pack_names[pack_int_id]);
229
Derrick Stoleeaf96fe32019-04-29 09:18:56 -0700230 p = add_packed_git(pack_name.buf, pack_name.len, m->local);
Derrick Stolee3715a632018-07-12 15:39:34 -0400231 strbuf_release(&pack_name);
Derrick Stoleeaf96fe32019-04-29 09:18:56 -0700232
233 if (!p)
234 return 1;
235
236 p->multi_pack_index = 1;
237 m->packs[pack_int_id] = p;
238 install_packed_git(r, p);
239 list_add_tail(&p->mru, &r->objects->packed_git_mru);
240
241 return 0;
Derrick Stolee3715a632018-07-12 15:39:34 -0400242}
243
244int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result)
245{
246 return bsearch_hash(oid->hash, m->chunk_oid_fanout, m->chunk_oid_lookup,
brian m. carlsonaaa95df2019-08-18 20:04:27 +0000247 the_hash_algo->rawsz, result);
Derrick Stolee3715a632018-07-12 15:39:34 -0400248}
249
Derrick Stolee8aac67a2018-07-12 15:39:35 -0400250struct object_id *nth_midxed_object_oid(struct object_id *oid,
251 struct multi_pack_index *m,
252 uint32_t n)
253{
254 if (n >= m->num_objects)
255 return NULL;
256
brian m. carlson92e2cab2021-04-26 01:02:50 +0000257 oidread(oid, m->chunk_oid_lookup + m->hash_len * n);
Derrick Stolee8aac67a2018-07-12 15:39:35 -0400258 return oid;
259}
260
Taylor Blau62f2c1b2021-03-30 11:04:20 -0400261off_t nth_midxed_offset(struct multi_pack_index *m, uint32_t pos)
Derrick Stolee3715a632018-07-12 15:39:34 -0400262{
263 const unsigned char *offset_data;
264 uint32_t offset32;
265
Derrick Stolee329fac32021-02-18 14:07:37 +0000266 offset_data = m->chunk_object_offsets + (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH;
Derrick Stolee3715a632018-07-12 15:39:34 -0400267 offset32 = get_be32(offset_data + sizeof(uint32_t));
268
269 if (m->chunk_large_offsets && offset32 & MIDX_LARGE_OFFSET_NEEDED) {
Derrick Stoleed8ac9ee2018-09-13 11:02:23 -0700270 if (sizeof(off_t) < sizeof(uint64_t))
Derrick Stolee3715a632018-07-12 15:39:34 -0400271 die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
272
273 offset32 ^= MIDX_LARGE_OFFSET_NEEDED;
274 return get_be64(m->chunk_large_offsets + sizeof(uint64_t) * offset32);
275 }
276
277 return offset32;
278}
279
Taylor Blau62f2c1b2021-03-30 11:04:20 -0400280uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos)
Derrick Stolee3715a632018-07-12 15:39:34 -0400281{
Derrick Stolee329fac32021-02-18 14:07:37 +0000282 return get_be32(m->chunk_object_offsets +
283 (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH);
Derrick Stolee3715a632018-07-12 15:39:34 -0400284}
285
Taylor Blaua8437f32022-10-12 18:01:48 -0400286int fill_midx_entry(struct repository *r,
René Scharfe893b5632021-09-11 22:39:31 +0200287 const struct object_id *oid,
288 struct pack_entry *e,
289 struct multi_pack_index *m)
Derrick Stolee3715a632018-07-12 15:39:34 -0400290{
René Scharfe893b5632021-09-11 22:39:31 +0200291 uint32_t pos;
Derrick Stolee3715a632018-07-12 15:39:34 -0400292 uint32_t pack_int_id;
293 struct packed_git *p;
294
René Scharfe893b5632021-09-11 22:39:31 +0200295 if (!bsearch_midx(oid, m, &pos))
296 return 0;
297
Derrick Stolee3715a632018-07-12 15:39:34 -0400298 if (pos >= m->num_objects)
299 return 0;
300
301 pack_int_id = nth_midxed_pack_int_id(m, pos);
302
Derrick Stolee64404a22019-04-29 09:18:55 -0700303 if (prepare_midx_pack(r, m, pack_int_id))
Taylor Blau506ec2f2020-11-25 12:17:33 -0500304 return 0;
Derrick Stolee3715a632018-07-12 15:39:34 -0400305 p = m->packs[pack_int_id];
306
307 /*
308 * We are about to tell the caller where they can locate the
309 * requested object. We better make sure the packfile is
310 * still here and can be accessed before supplying that
311 * answer, as it may have been deleted since the MIDX was
312 * loaded!
313 */
314 if (!is_pack_valid(p))
315 return 0;
316
René Scharfe09ef6612021-09-11 22:43:26 +0200317 if (oidset_size(&p->bad_objects) &&
318 oidset_contains(&p->bad_objects, oid))
319 return 0;
Derrick Stoleec39b02a2018-08-20 16:51:57 +0000320
Derrick Stolee3715a632018-07-12 15:39:34 -0400321 e->offset = nth_midxed_offset(m, pos);
322 e->p = p;
323
324 return 1;
325}
326
Jeff King013fd7a2019-04-05 14:06:04 -0400327/* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */
328static int cmp_idx_or_pack_name(const char *idx_or_pack_name,
329 const char *idx_name)
330{
331 /* Skip past any initial matching prefix. */
332 while (*idx_name && *idx_name == *idx_or_pack_name) {
333 idx_name++;
334 idx_or_pack_name++;
335 }
336
337 /*
338 * If we didn't match completely, we may have matched "pack-1234." and
339 * be left with "idx" and "pack" respectively, which is also OK. We do
340 * not have to check for "idx" and "idx", because that would have been
341 * a complete match (and in that case these strcmps will be false, but
342 * we'll correctly return 0 from the final strcmp() below.
343 *
344 * Technically this matches "fooidx" and "foopack", but we'd never have
345 * such names in the first place.
346 */
347 if (!strcmp(idx_name, "idx") && !strcmp(idx_or_pack_name, "pack"))
348 return 0;
349
350 /*
351 * This not only checks for a complete match, but also orders based on
352 * the first non-identical character, which means our ordering will
353 * match a raw strcmp(). That makes it OK to use this to binary search
354 * a naively-sorted list.
355 */
356 return strcmp(idx_or_pack_name, idx_name);
357}
358
359int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name)
Derrick Stoleea40498a2018-07-12 15:39:36 -0400360{
361 uint32_t first = 0, last = m->num_packs;
362
363 while (first < last) {
364 uint32_t mid = first + (last - first) / 2;
365 const char *current;
366 int cmp;
367
368 current = m->pack_names[mid];
Jeff King013fd7a2019-04-05 14:06:04 -0400369 cmp = cmp_idx_or_pack_name(idx_or_pack_name, current);
Derrick Stoleea40498a2018-07-12 15:39:36 -0400370 if (!cmp)
371 return 1;
372 if (cmp > 0) {
373 first = mid + 1;
374 continue;
375 }
376 last = mid;
377 }
378
379 return 0;
380}
381
Derrick Stolee2cf489a2018-08-20 16:51:55 +0000382int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local)
Derrick Stoleec4d25222018-07-12 15:39:33 -0400383{
Derrick Stolee29e20162018-08-20 16:52:00 +0000384 struct multi_pack_index *m;
Derrick Stoleec4d25222018-07-12 15:39:33 -0400385 struct multi_pack_index *m_search;
Derrick Stoleec4d25222018-07-12 15:39:33 -0400386
Derrick Stolee18e449f2020-09-25 12:33:34 +0000387 prepare_repo_settings(r);
388 if (!r->settings.core_multi_pack_index)
Derrick Stoleec4d25222018-07-12 15:39:33 -0400389 return 0;
390
Derrick Stolee29e20162018-08-20 16:52:00 +0000391 for (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next)
Derrick Stoleec4d25222018-07-12 15:39:33 -0400392 if (!strcmp(object_dir, m_search->object_dir))
393 return 1;
394
Derrick Stolee29e20162018-08-20 16:52:00 +0000395 m = load_multi_pack_index(object_dir, local);
Derrick Stoleec4d25222018-07-12 15:39:33 -0400396
Derrick Stolee29e20162018-08-20 16:52:00 +0000397 if (m) {
Taylor Blau59552fb2020-08-28 16:22:13 -0400398 struct multi_pack_index *mp = r->objects->multi_pack_index;
399 if (mp) {
400 m->next = mp->next;
401 mp->next = m;
402 } else
403 r->objects->multi_pack_index = m;
Derrick Stoleec4d25222018-07-12 15:39:33 -0400404 return 1;
405 }
406
407 return 0;
408}
409
Derrick Stoleefc59e742018-07-12 15:39:22 -0400410static size_t write_midx_header(struct hashfile *f,
411 unsigned char num_chunks,
412 uint32_t num_packs)
413{
Derrick Stoleefc59e742018-07-12 15:39:22 -0400414 hashwrite_be32(f, MIDX_SIGNATURE);
René Scharfe014f1442020-09-06 10:59:02 +0200415 hashwrite_u8(f, MIDX_VERSION);
Taylor Blaud9fef9d2022-05-20 19:17:41 -0400416 hashwrite_u8(f, oid_version(the_hash_algo));
René Scharfe014f1442020-09-06 10:59:02 +0200417 hashwrite_u8(f, num_chunks);
418 hashwrite_u8(f, 0); /* unused */
Derrick Stoleefc59e742018-07-12 15:39:22 -0400419 hashwrite_be32(f, num_packs);
420
421 return MIDX_HEADER_SIZE;
422}
423
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700424struct pack_info {
425 uint32_t orig_pack_int_id;
426 char *pack_name;
427 struct packed_git *p;
Derrick Stolee19575c72019-06-10 16:35:25 -0700428 unsigned expired : 1;
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700429};
430
431static int pack_info_compare(const void *_a, const void *_b)
432{
433 struct pack_info *a = (struct pack_info *)_a;
434 struct pack_info *b = (struct pack_info *)_b;
435 return strcmp(a->pack_name, b->pack_name);
436}
437
Taylor Blau9218c6a2021-03-30 11:04:11 -0400438static int idx_or_pack_name_cmp(const void *_va, const void *_vb)
439{
440 const char *pack_name = _va;
441 const struct pack_info *compar = _vb;
442
443 return cmp_idx_or_pack_name(pack_name, compar->pack_name);
444}
445
Derrick Stolee577dc492021-02-18 14:07:26 +0000446struct write_midx_context {
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700447 struct pack_info *info;
Derrick Stolee396f2572018-07-12 15:39:26 -0400448 uint32_t nr;
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700449 uint32_t alloc;
Derrick Stoleea40498a2018-07-12 15:39:36 -0400450 struct multi_pack_index *m;
William Baker840cef02019-10-21 18:39:59 +0000451 struct progress *progress;
452 unsigned pack_paths_checked;
Derrick Stolee31bda9a2021-02-18 14:07:28 +0000453
454 struct pack_midx_entry *entries;
455 uint32_t entries_nr;
Derrick Stolee7a3ada12021-02-18 14:07:29 +0000456
457 uint32_t *pack_perm;
Taylor Blau38ff7ca2021-03-30 11:04:32 -0400458 uint32_t *pack_order;
Derrick Stolee7a3ada12021-02-18 14:07:29 +0000459 unsigned large_offsets_needed:1;
Derrick Stolee980f5252021-02-18 14:07:30 +0000460 uint32_t num_large_offsets;
Taylor Blau9218c6a2021-03-30 11:04:11 -0400461
462 int preferred_pack_idx;
Taylor Blau56d863e2021-09-28 21:55:01 -0400463
464 struct string_list *to_include;
Derrick Stolee396f2572018-07-12 15:39:26 -0400465};
466
467static void add_pack_to_midx(const char *full_path, size_t full_path_len,
468 const char *file_name, void *data)
469{
Derrick Stolee577dc492021-02-18 14:07:26 +0000470 struct write_midx_context *ctx = data;
Derrick Stolee396f2572018-07-12 15:39:26 -0400471
472 if (ends_with(file_name, ".idx")) {
Derrick Stolee577dc492021-02-18 14:07:26 +0000473 display_progress(ctx->progress, ++ctx->pack_paths_checked);
Taylor Blau56d863e2021-09-28 21:55:01 -0400474 /*
475 * Note that at most one of ctx->m and ctx->to_include are set,
476 * so we are testing midx_contains_pack() and
477 * string_list_has_string() independently (guarded by the
478 * appropriate NULL checks).
479 *
480 * We could support passing to_include while reusing an existing
481 * MIDX, but don't currently since the reuse process drags
482 * forward all packs from an existing MIDX (without checking
483 * whether or not they appear in the to_include list).
484 *
485 * If we added support for that, these next two conditional
486 * should be performed independently (likely checking
487 * to_include before the existing MIDX).
488 */
Derrick Stolee577dc492021-02-18 14:07:26 +0000489 if (ctx->m && midx_contains_pack(ctx->m, file_name))
Derrick Stoleea40498a2018-07-12 15:39:36 -0400490 return;
Taylor Blau56d863e2021-09-28 21:55:01 -0400491 else if (ctx->to_include &&
492 !string_list_has_string(ctx->to_include, file_name))
493 return;
Derrick Stoleea40498a2018-07-12 15:39:36 -0400494
Derrick Stolee577dc492021-02-18 14:07:26 +0000495 ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
Derrick Stolee396f2572018-07-12 15:39:26 -0400496
Derrick Stolee577dc492021-02-18 14:07:26 +0000497 ctx->info[ctx->nr].p = add_packed_git(full_path,
498 full_path_len,
499 0);
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400500
Derrick Stolee577dc492021-02-18 14:07:26 +0000501 if (!ctx->info[ctx->nr].p) {
Derrick Stolee396f2572018-07-12 15:39:26 -0400502 warning(_("failed to add packfile '%s'"),
503 full_path);
504 return;
505 }
506
Derrick Stolee577dc492021-02-18 14:07:26 +0000507 if (open_pack_index(ctx->info[ctx->nr].p)) {
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400508 warning(_("failed to open pack-index '%s'"),
509 full_path);
Derrick Stolee577dc492021-02-18 14:07:26 +0000510 close_pack(ctx->info[ctx->nr].p);
511 FREE_AND_NULL(ctx->info[ctx->nr].p);
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400512 return;
513 }
514
Derrick Stolee577dc492021-02-18 14:07:26 +0000515 ctx->info[ctx->nr].pack_name = xstrdup(file_name);
516 ctx->info[ctx->nr].orig_pack_int_id = ctx->nr;
517 ctx->info[ctx->nr].expired = 0;
518 ctx->nr++;
Derrick Stolee396f2572018-07-12 15:39:26 -0400519 }
520}
521
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400522struct pack_midx_entry {
523 struct object_id oid;
524 uint32_t pack_int_id;
525 time_t pack_mtime;
526 uint64_t offset;
Taylor Blau9218c6a2021-03-30 11:04:11 -0400527 unsigned preferred : 1;
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400528};
529
530static int midx_oid_compare(const void *_a, const void *_b)
531{
532 const struct pack_midx_entry *a = (const struct pack_midx_entry *)_a;
533 const struct pack_midx_entry *b = (const struct pack_midx_entry *)_b;
534 int cmp = oidcmp(&a->oid, &b->oid);
535
536 if (cmp)
537 return cmp;
538
Taylor Blau9218c6a2021-03-30 11:04:11 -0400539 /* Sort objects in a preferred pack first when multiple copies exist. */
540 if (a->preferred > b->preferred)
541 return -1;
542 if (a->preferred < b->preferred)
543 return 1;
544
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400545 if (a->pack_mtime > b->pack_mtime)
546 return -1;
547 else if (a->pack_mtime < b->pack_mtime)
548 return 1;
549
550 return a->pack_int_id - b->pack_int_id;
551}
552
Derrick Stoleea40498a2018-07-12 15:39:36 -0400553static int nth_midxed_pack_midx_entry(struct multi_pack_index *m,
Derrick Stoleea40498a2018-07-12 15:39:36 -0400554 struct pack_midx_entry *e,
555 uint32_t pos)
556{
557 if (pos >= m->num_objects)
558 return 1;
559
560 nth_midxed_object_oid(&e->oid, m, pos);
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700561 e->pack_int_id = nth_midxed_pack_int_id(m, pos);
Derrick Stoleea40498a2018-07-12 15:39:36 -0400562 e->offset = nth_midxed_offset(m, pos);
563
564 /* consider objects in midx to be from "old" packs */
565 e->pack_mtime = 0;
566 return 0;
567}
568
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400569static void fill_pack_entry(uint32_t pack_int_id,
570 struct packed_git *p,
571 uint32_t cur_object,
Taylor Blau9218c6a2021-03-30 11:04:11 -0400572 struct pack_midx_entry *entry,
573 int preferred)
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400574{
Jeff King07636712020-02-23 23:27:36 -0500575 if (nth_packed_object_id(&entry->oid, p, cur_object) < 0)
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400576 die(_("failed to locate object %d in packfile"), cur_object);
577
578 entry->pack_int_id = pack_int_id;
579 entry->pack_mtime = p->mtime;
580
581 entry->offset = nth_packed_object_offset(p, cur_object);
Taylor Blau9218c6a2021-03-30 11:04:11 -0400582 entry->preferred = !!preferred;
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400583}
584
Taylor Blau989d9cb2022-08-22 15:50:38 -0400585struct midx_fanout {
586 struct pack_midx_entry *entries;
587 uint32_t nr;
588 uint32_t alloc;
589};
590
591static void midx_fanout_grow(struct midx_fanout *fanout, uint32_t nr)
592{
593 ALLOC_GROW(fanout->entries, nr, fanout->alloc);
594}
595
596static void midx_fanout_sort(struct midx_fanout *fanout)
597{
598 QSORT(fanout->entries, fanout->nr, midx_oid_compare);
599}
600
Taylor Blau852c5302022-08-22 15:50:41 -0400601static void midx_fanout_add_midx_fanout(struct midx_fanout *fanout,
602 struct multi_pack_index *m,
Taylor Blau99e4d082022-08-22 15:50:49 -0400603 uint32_t cur_fanout,
604 int preferred_pack)
Taylor Blau852c5302022-08-22 15:50:41 -0400605{
606 uint32_t start = 0, end;
607 uint32_t cur_object;
608
609 if (cur_fanout)
610 start = ntohl(m->chunk_oid_fanout[cur_fanout - 1]);
611 end = ntohl(m->chunk_oid_fanout[cur_fanout]);
612
613 for (cur_object = start; cur_object < end; cur_object++) {
Taylor Blau99e4d082022-08-22 15:50:49 -0400614 if ((preferred_pack > -1) &&
615 (preferred_pack == nth_midxed_pack_int_id(m, cur_object))) {
616 /*
617 * Objects from preferred packs are added
618 * separately.
619 */
620 continue;
621 }
622
Taylor Blau852c5302022-08-22 15:50:41 -0400623 midx_fanout_grow(fanout, fanout->nr + 1);
624 nth_midxed_pack_midx_entry(m,
625 &fanout->entries[fanout->nr],
626 cur_object);
Taylor Blaucdf517b2022-08-22 15:50:46 -0400627 fanout->entries[fanout->nr].preferred = 0;
Taylor Blau852c5302022-08-22 15:50:41 -0400628 fanout->nr++;
629 }
630}
631
Taylor Blau1d6f4c62022-08-22 15:50:43 -0400632static void midx_fanout_add_pack_fanout(struct midx_fanout *fanout,
633 struct pack_info *info,
634 uint32_t cur_pack,
635 int preferred,
636 uint32_t cur_fanout)
637{
638 struct packed_git *pack = info[cur_pack].p;
639 uint32_t start = 0, end;
640 uint32_t cur_object;
641
642 if (cur_fanout)
643 start = get_pack_fanout(pack, cur_fanout - 1);
644 end = get_pack_fanout(pack, cur_fanout);
645
646 for (cur_object = start; cur_object < end; cur_object++) {
647 midx_fanout_grow(fanout, fanout->nr + 1);
648 fill_pack_entry(cur_pack,
649 info[cur_pack].p,
650 cur_object,
651 &fanout->entries[fanout->nr],
652 preferred);
653 fanout->nr++;
654 }
655}
656
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400657/*
658 * It is possible to artificially get into a state where there are many
659 * duplicate copies of objects. That can create high memory pressure if
660 * we are to create a list of all objects before de-duplication. To reduce
661 * this memory pressure without a significant performance drop, automatically
662 * group objects by the first byte of their object id. Use the IDX fanout
663 * tables to group the data, copy to a local array, then sort.
664 *
665 * Copy only the de-duplicated entries (selected by most-recent modified time
666 * of a packfile containing the object).
667 */
Derrick Stoleea40498a2018-07-12 15:39:36 -0400668static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m,
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700669 struct pack_info *info,
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400670 uint32_t nr_packs,
Taylor Blau9218c6a2021-03-30 11:04:11 -0400671 uint32_t *nr_objects,
672 int preferred_pack)
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400673{
674 uint32_t cur_fanout, cur_pack, cur_object;
Taylor Blau989d9cb2022-08-22 15:50:38 -0400675 uint32_t alloc_objects, total_objects = 0;
676 struct midx_fanout fanout = { 0 };
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400677 struct pack_midx_entry *deduplicated_entries = NULL;
Derrick Stoleea40498a2018-07-12 15:39:36 -0400678 uint32_t start_pack = m ? m->num_packs : 0;
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400679
Derrick Stoleea40498a2018-07-12 15:39:36 -0400680 for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++)
Derrick Stoleed01bf2e2019-06-10 16:35:24 -0700681 total_objects += info[cur_pack].p->num_objects;
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400682
683 /*
684 * As we de-duplicate by fanout value, we expect the fanout
685 * slices to be evenly distributed, with some noise. Hence,
686 * allocate slightly more than one 256th.
687 */
Taylor Blau989d9cb2022-08-22 15:50:38 -0400688 alloc_objects = fanout.alloc = total_objects > 3200 ? total_objects / 200 : 16;
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400689
Taylor Blau989d9cb2022-08-22 15:50:38 -0400690 ALLOC_ARRAY(fanout.entries, fanout.alloc);
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400691 ALLOC_ARRAY(deduplicated_entries, alloc_objects);
692 *nr_objects = 0;
693
694 for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) {
Taylor Blau989d9cb2022-08-22 15:50:38 -0400695 fanout.nr = 0;
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400696
Taylor Blau852c5302022-08-22 15:50:41 -0400697 if (m)
Taylor Blau99e4d082022-08-22 15:50:49 -0400698 midx_fanout_add_midx_fanout(&fanout, m, cur_fanout,
699 preferred_pack);
Derrick Stoleea40498a2018-07-12 15:39:36 -0400700
701 for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) {
Taylor Blau9218c6a2021-03-30 11:04:11 -0400702 int preferred = cur_pack == preferred_pack;
Taylor Blau1d6f4c62022-08-22 15:50:43 -0400703 midx_fanout_add_pack_fanout(&fanout,
704 info, cur_pack,
705 preferred, cur_fanout);
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400706 }
707
Taylor Blaucdf517b2022-08-22 15:50:46 -0400708 if (-1 < preferred_pack && preferred_pack < start_pack)
709 midx_fanout_add_pack_fanout(&fanout, info,
710 preferred_pack, 1,
711 cur_fanout);
712
Taylor Blau989d9cb2022-08-22 15:50:38 -0400713 midx_fanout_sort(&fanout);
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400714
715 /*
716 * The batch is now sorted by OID and then mtime (descending).
717 * Take only the first duplicate.
718 */
Taylor Blau989d9cb2022-08-22 15:50:38 -0400719 for (cur_object = 0; cur_object < fanout.nr; cur_object++) {
720 if (cur_object && oideq(&fanout.entries[cur_object - 1].oid,
721 &fanout.entries[cur_object].oid))
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400722 continue;
723
724 ALLOC_GROW(deduplicated_entries, *nr_objects + 1, alloc_objects);
725 memcpy(&deduplicated_entries[*nr_objects],
Taylor Blau989d9cb2022-08-22 15:50:38 -0400726 &fanout.entries[cur_object],
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400727 sizeof(struct pack_midx_entry));
728 (*nr_objects)++;
729 }
730 }
731
Taylor Blau989d9cb2022-08-22 15:50:38 -0400732 free(fanout.entries);
Derrick Stoleefe1ed562018-07-12 15:39:29 -0400733 return deduplicated_entries;
734}
735
Derrick Stolee0ccd7132021-02-18 14:07:31 +0000736static int write_midx_pack_names(struct hashfile *f, void *data)
Derrick Stolee32f3c542018-07-12 15:39:27 -0400737{
Derrick Stoleeb4d94142021-02-18 14:07:27 +0000738 struct write_midx_context *ctx = data;
Derrick Stolee32f3c542018-07-12 15:39:27 -0400739 uint32_t i;
740 unsigned char padding[MIDX_CHUNK_ALIGNMENT];
741 size_t written = 0;
742
Derrick Stoleeb4d94142021-02-18 14:07:27 +0000743 for (i = 0; i < ctx->nr; i++) {
Derrick Stolee19575c72019-06-10 16:35:25 -0700744 size_t writelen;
745
Derrick Stoleeb4d94142021-02-18 14:07:27 +0000746 if (ctx->info[i].expired)
Derrick Stolee19575c72019-06-10 16:35:25 -0700747 continue;
Derrick Stolee32f3c542018-07-12 15:39:27 -0400748
Derrick Stoleeb4d94142021-02-18 14:07:27 +0000749 if (i && strcmp(ctx->info[i].pack_name, ctx->info[i - 1].pack_name) <= 0)
Derrick Stolee32f3c542018-07-12 15:39:27 -0400750 BUG("incorrect pack-file order: %s before %s",
Derrick Stoleeb4d94142021-02-18 14:07:27 +0000751 ctx->info[i - 1].pack_name,
752 ctx->info[i].pack_name);
Derrick Stolee32f3c542018-07-12 15:39:27 -0400753
Derrick Stoleeb4d94142021-02-18 14:07:27 +0000754 writelen = strlen(ctx->info[i].pack_name) + 1;
755 hashwrite(f, ctx->info[i].pack_name, writelen);
Derrick Stolee32f3c542018-07-12 15:39:27 -0400756 written += writelen;
757 }
758
759 /* add padding to be aligned */
760 i = MIDX_CHUNK_ALIGNMENT - (written % MIDX_CHUNK_ALIGNMENT);
761 if (i < MIDX_CHUNK_ALIGNMENT) {
762 memset(padding, 0, sizeof(padding));
763 hashwrite(f, padding, i);
Derrick Stolee32f3c542018-07-12 15:39:27 -0400764 }
765
Derrick Stolee0ccd7132021-02-18 14:07:31 +0000766 return 0;
Derrick Stolee32f3c542018-07-12 15:39:27 -0400767}
768
Derrick Stolee0ccd7132021-02-18 14:07:31 +0000769static int write_midx_oid_fanout(struct hashfile *f,
770 void *data)
Derrick Stoleed7cacf22018-07-12 15:39:31 -0400771{
Derrick Stolee31bda9a2021-02-18 14:07:28 +0000772 struct write_midx_context *ctx = data;
773 struct pack_midx_entry *list = ctx->entries;
774 struct pack_midx_entry *last = ctx->entries + ctx->entries_nr;
Derrick Stoleed7cacf22018-07-12 15:39:31 -0400775 uint32_t count = 0;
776 uint32_t i;
777
778 /*
779 * Write the first-level table (the list is sorted,
780 * but we use a 256-entry lookup to be able to avoid
781 * having to do eight extra binary search iterations).
782 */
783 for (i = 0; i < 256; i++) {
784 struct pack_midx_entry *next = list;
785
786 while (next < last && next->oid.hash[0] == i) {
787 count++;
788 next++;
789 }
790
791 hashwrite_be32(f, count);
792 list = next;
793 }
794
Derrick Stolee0ccd7132021-02-18 14:07:31 +0000795 return 0;
Derrick Stoleed7cacf22018-07-12 15:39:31 -0400796}
797
Derrick Stolee0ccd7132021-02-18 14:07:31 +0000798static int write_midx_oid_lookup(struct hashfile *f,
799 void *data)
Derrick Stolee0d5b3a52018-07-12 15:39:30 -0400800{
Derrick Stolee31bda9a2021-02-18 14:07:28 +0000801 struct write_midx_context *ctx = data;
802 unsigned char hash_len = the_hash_algo->rawsz;
803 struct pack_midx_entry *list = ctx->entries;
Derrick Stolee0d5b3a52018-07-12 15:39:30 -0400804 uint32_t i;
Derrick Stolee0d5b3a52018-07-12 15:39:30 -0400805
Derrick Stolee31bda9a2021-02-18 14:07:28 +0000806 for (i = 0; i < ctx->entries_nr; i++) {
Derrick Stolee0d5b3a52018-07-12 15:39:30 -0400807 struct pack_midx_entry *obj = list++;
808
Derrick Stolee31bda9a2021-02-18 14:07:28 +0000809 if (i < ctx->entries_nr - 1) {
Derrick Stolee0d5b3a52018-07-12 15:39:30 -0400810 struct pack_midx_entry *next = list;
811 if (oidcmp(&obj->oid, &next->oid) >= 0)
812 BUG("OIDs not in order: %s >= %s",
813 oid_to_hex(&obj->oid),
814 oid_to_hex(&next->oid));
815 }
816
817 hashwrite(f, obj->oid.hash, (int)hash_len);
Derrick Stolee0d5b3a52018-07-12 15:39:30 -0400818 }
819
Derrick Stolee0ccd7132021-02-18 14:07:31 +0000820 return 0;
Derrick Stolee0d5b3a52018-07-12 15:39:30 -0400821}
822
Derrick Stolee0ccd7132021-02-18 14:07:31 +0000823static int write_midx_object_offsets(struct hashfile *f,
824 void *data)
Derrick Stolee662148c2018-07-12 15:39:32 -0400825{
Derrick Stolee7a3ada12021-02-18 14:07:29 +0000826 struct write_midx_context *ctx = data;
827 struct pack_midx_entry *list = ctx->entries;
Derrick Stolee662148c2018-07-12 15:39:32 -0400828 uint32_t i, nr_large_offset = 0;
Derrick Stolee662148c2018-07-12 15:39:32 -0400829
Derrick Stolee7a3ada12021-02-18 14:07:29 +0000830 for (i = 0; i < ctx->entries_nr; i++) {
Derrick Stolee662148c2018-07-12 15:39:32 -0400831 struct pack_midx_entry *obj = list++;
832
Derrick Stolee7a3ada12021-02-18 14:07:29 +0000833 if (ctx->pack_perm[obj->pack_int_id] == PACK_EXPIRED)
Derrick Stolee19575c72019-06-10 16:35:25 -0700834 BUG("object %s is in an expired pack with int-id %d",
835 oid_to_hex(&obj->oid),
836 obj->pack_int_id);
837
Derrick Stolee7a3ada12021-02-18 14:07:29 +0000838 hashwrite_be32(f, ctx->pack_perm[obj->pack_int_id]);
Derrick Stolee662148c2018-07-12 15:39:32 -0400839
Derrick Stolee7a3ada12021-02-18 14:07:29 +0000840 if (ctx->large_offsets_needed && obj->offset >> 31)
Derrick Stolee662148c2018-07-12 15:39:32 -0400841 hashwrite_be32(f, MIDX_LARGE_OFFSET_NEEDED | nr_large_offset++);
Derrick Stolee7a3ada12021-02-18 14:07:29 +0000842 else if (!ctx->large_offsets_needed && obj->offset >> 32)
Derrick Stolee662148c2018-07-12 15:39:32 -0400843 BUG("object %s requires a large offset (%"PRIx64") but the MIDX is not writing large offsets!",
844 oid_to_hex(&obj->oid),
845 obj->offset);
846 else
847 hashwrite_be32(f, (uint32_t)obj->offset);
Derrick Stolee662148c2018-07-12 15:39:32 -0400848 }
849
Derrick Stolee0ccd7132021-02-18 14:07:31 +0000850 return 0;
Derrick Stolee662148c2018-07-12 15:39:32 -0400851}
852
Derrick Stolee0ccd7132021-02-18 14:07:31 +0000853static int write_midx_large_offsets(struct hashfile *f,
854 void *data)
Derrick Stolee662148c2018-07-12 15:39:32 -0400855{
Derrick Stolee980f5252021-02-18 14:07:30 +0000856 struct write_midx_context *ctx = data;
857 struct pack_midx_entry *list = ctx->entries;
858 struct pack_midx_entry *end = ctx->entries + ctx->entries_nr;
Derrick Stolee980f5252021-02-18 14:07:30 +0000859 uint32_t nr_large_offset = ctx->num_large_offsets;
Derrick Stolee662148c2018-07-12 15:39:32 -0400860
861 while (nr_large_offset) {
Jeff King61b0fcb2018-11-03 22:27:46 -0400862 struct pack_midx_entry *obj;
863 uint64_t offset;
864
865 if (list >= end)
866 BUG("too many large-offset objects");
867
868 obj = list++;
869 offset = obj->offset;
Derrick Stolee662148c2018-07-12 15:39:32 -0400870
871 if (!(offset >> 31))
872 continue;
873
Derrick Stolee0ccd7132021-02-18 14:07:31 +0000874 hashwrite_be64(f, offset);
Derrick Stolee662148c2018-07-12 15:39:32 -0400875
876 nr_large_offset--;
877 }
878
Derrick Stolee0ccd7132021-02-18 14:07:31 +0000879 return 0;
Derrick Stolee662148c2018-07-12 15:39:32 -0400880}
881
Taylor Blau95e83832022-01-25 17:41:03 -0500882static int write_midx_revindex(struct hashfile *f,
883 void *data)
884{
885 struct write_midx_context *ctx = data;
886 uint32_t i;
887
888 for (i = 0; i < ctx->entries_nr; i++)
889 hashwrite_be32(f, ctx->pack_order[i]);
890
891 return 0;
892}
893
Jeff King30077522021-03-30 11:04:36 -0400894struct midx_pack_order_data {
895 uint32_t nr;
896 uint32_t pack;
897 off_t offset;
898};
899
900static int midx_pack_order_cmp(const void *va, const void *vb)
Taylor Blau38ff7ca2021-03-30 11:04:32 -0400901{
Jeff King30077522021-03-30 11:04:36 -0400902 const struct midx_pack_order_data *a = va, *b = vb;
903 if (a->pack < b->pack)
Taylor Blau38ff7ca2021-03-30 11:04:32 -0400904 return -1;
Jeff King30077522021-03-30 11:04:36 -0400905 else if (a->pack > b->pack)
Taylor Blau38ff7ca2021-03-30 11:04:32 -0400906 return 1;
Jeff King30077522021-03-30 11:04:36 -0400907 else if (a->offset < b->offset)
Taylor Blau38ff7ca2021-03-30 11:04:32 -0400908 return -1;
Jeff King30077522021-03-30 11:04:36 -0400909 else if (a->offset > b->offset)
Taylor Blau38ff7ca2021-03-30 11:04:32 -0400910 return 1;
Jeff King30077522021-03-30 11:04:36 -0400911 else
912 return 0;
Taylor Blau38ff7ca2021-03-30 11:04:32 -0400913}
914
915static uint32_t *midx_pack_order(struct write_midx_context *ctx)
916{
Jeff King30077522021-03-30 11:04:36 -0400917 struct midx_pack_order_data *data;
Taylor Blau38ff7ca2021-03-30 11:04:32 -0400918 uint32_t *pack_order;
919 uint32_t i;
920
Taylor Blau2dcff522022-10-12 18:01:55 -0400921 trace2_region_enter("midx", "midx_pack_order", the_repository);
922
Jeff King30077522021-03-30 11:04:36 -0400923 ALLOC_ARRAY(data, ctx->entries_nr);
924 for (i = 0; i < ctx->entries_nr; i++) {
925 struct pack_midx_entry *e = &ctx->entries[i];
926 data[i].nr = i;
927 data[i].pack = ctx->pack_perm[e->pack_int_id];
928 if (!e->preferred)
929 data[i].pack |= (1U << 31);
930 data[i].offset = e->offset;
931 }
932
933 QSORT(data, ctx->entries_nr, midx_pack_order_cmp);
934
Taylor Blau38ff7ca2021-03-30 11:04:32 -0400935 ALLOC_ARRAY(pack_order, ctx->entries_nr);
936 for (i = 0; i < ctx->entries_nr; i++)
Jeff King30077522021-03-30 11:04:36 -0400937 pack_order[i] = data[i].nr;
938 free(data);
Taylor Blau38ff7ca2021-03-30 11:04:32 -0400939
Taylor Blau2dcff522022-10-12 18:01:55 -0400940 trace2_region_leave("midx", "midx_pack_order", the_repository);
941
Taylor Blau38ff7ca2021-03-30 11:04:32 -0400942 return pack_order;
943}
944
945static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash,
946 struct write_midx_context *ctx)
947{
948 struct strbuf buf = STRBUF_INIT;
949 const char *tmp_file;
950
Taylor Blau2dcff522022-10-12 18:01:55 -0400951 trace2_region_enter("midx", "write_midx_reverse_index", the_repository);
952
Taylor Blau38ff7ca2021-03-30 11:04:32 -0400953 strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex(midx_hash));
954
955 tmp_file = write_rev_file_order(NULL, ctx->pack_order, ctx->entries_nr,
956 midx_hash, WRITE_REV);
957
958 if (finalize_object_file(tmp_file, buf.buf))
959 die(_("cannot store reverse index file"));
960
961 strbuf_release(&buf);
Taylor Blau2dcff522022-10-12 18:01:55 -0400962
963 trace2_region_leave("midx", "write_midx_reverse_index", the_repository);
Taylor Blau38ff7ca2021-03-30 11:04:32 -0400964}
965
Taylor Blau426c00e2021-08-31 16:51:55 -0400966static void clear_midx_files_ext(const char *object_dir, const char *ext,
Taylor Blau38ff7ca2021-03-30 11:04:32 -0400967 unsigned char *keep_hash);
968
Taylor Blauec1e28e2021-06-23 14:39:12 -0400969static int midx_checksum_valid(struct multi_pack_index *m)
970{
971 return hashfile_checksum_valid(m->data, m->data_len);
972}
973
Taylor Blauc528e172021-08-31 16:52:24 -0400974static void prepare_midx_packing_data(struct packing_data *pdata,
975 struct write_midx_context *ctx)
976{
977 uint32_t i;
978
Taylor Blau2dcff522022-10-12 18:01:55 -0400979 trace2_region_enter("midx", "prepare_midx_packing_data", the_repository);
980
Taylor Blauc528e172021-08-31 16:52:24 -0400981 memset(pdata, 0, sizeof(struct packing_data));
982 prepare_packing_data(the_repository, pdata);
983
984 for (i = 0; i < ctx->entries_nr; i++) {
985 struct pack_midx_entry *from = &ctx->entries[ctx->pack_order[i]];
986 struct object_entry *to = packlist_alloc(pdata, &from->oid);
987
988 oe_set_in_pack(pdata, to,
989 ctx->info[ctx->pack_perm[from->pack_int_id]].p);
990 }
Taylor Blau2dcff522022-10-12 18:01:55 -0400991
992 trace2_region_leave("midx", "prepare_midx_packing_data", the_repository);
Taylor Blauc528e172021-08-31 16:52:24 -0400993}
994
995static int add_ref_to_pending(const char *refname,
996 const struct object_id *oid,
997 int flag, void *cb_data)
998{
999 struct rev_info *revs = (struct rev_info*)cb_data;
Taylor Blau1dc4f1e2022-10-12 18:01:52 -04001000 struct object_id peeled;
Taylor Blauc528e172021-08-31 16:52:24 -04001001 struct object *object;
1002
1003 if ((flag & REF_ISSYMREF) && (flag & REF_ISBROKEN)) {
1004 warning("symbolic ref is dangling: %s", refname);
1005 return 0;
1006 }
1007
Taylor Blau1dc4f1e2022-10-12 18:01:52 -04001008 if (!peel_iterated_oid(oid, &peeled))
1009 oid = &peeled;
1010
Taylor Blauc528e172021-08-31 16:52:24 -04001011 object = parse_object_or_die(oid, refname);
1012 if (object->type != OBJ_COMMIT)
1013 return 0;
1014
1015 add_pending_object(revs, object, "");
1016 if (bitmap_is_preferred_refname(revs->repo, refname))
1017 object->flags |= NEEDS_BITMAP;
1018 return 0;
1019}
1020
1021struct bitmap_commit_cb {
1022 struct commit **commits;
1023 size_t commits_nr, commits_alloc;
1024
1025 struct write_midx_context *ctx;
1026};
1027
1028static const struct object_id *bitmap_oid_access(size_t index,
1029 const void *_entries)
1030{
1031 const struct pack_midx_entry *entries = _entries;
1032 return &entries[index].oid;
1033}
1034
1035static void bitmap_show_commit(struct commit *commit, void *_data)
1036{
1037 struct bitmap_commit_cb *data = _data;
1038 int pos = oid_pos(&commit->object.oid, data->ctx->entries,
1039 data->ctx->entries_nr,
1040 bitmap_oid_access);
1041 if (pos < 0)
1042 return;
1043
1044 ALLOC_GROW(data->commits, data->commits_nr + 1, data->commits_alloc);
1045 data->commits[data->commits_nr++] = commit;
1046}
1047
Taylor Blau08944d12021-09-28 21:55:07 -04001048static int read_refs_snapshot(const char *refs_snapshot,
1049 struct rev_info *revs)
1050{
1051 struct strbuf buf = STRBUF_INIT;
1052 struct object_id oid;
1053 FILE *f = xfopen(refs_snapshot, "r");
1054
1055 while (strbuf_getline(&buf, f) != EOF) {
1056 struct object *object;
1057 int preferred = 0;
1058 char *hex = buf.buf;
1059 const char *end = NULL;
1060
1061 if (buf.len && *buf.buf == '+') {
1062 preferred = 1;
1063 hex = &buf.buf[1];
1064 }
1065
1066 if (parse_oid_hex(hex, &oid, &end) < 0)
1067 die(_("could not parse line: %s"), buf.buf);
1068 if (*end)
1069 die(_("malformed line: %s"), buf.buf);
1070
1071 object = parse_object_or_die(&oid, NULL);
1072 if (preferred)
1073 object->flags |= NEEDS_BITMAP;
1074
1075 add_pending_object(revs, object, "");
1076 }
1077
1078 fclose(f);
1079 strbuf_release(&buf);
1080 return 0;
1081}
1082
Taylor Blauc528e172021-08-31 16:52:24 -04001083static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr_p,
Taylor Blau08944d12021-09-28 21:55:07 -04001084 const char *refs_snapshot,
Taylor Blauc528e172021-08-31 16:52:24 -04001085 struct write_midx_context *ctx)
1086{
1087 struct rev_info revs;
1088 struct bitmap_commit_cb cb = {0};
1089
Taylor Blau2dcff522022-10-12 18:01:55 -04001090 trace2_region_enter("midx", "find_commits_for_midx_bitmap",
1091 the_repository);
1092
Taylor Blauc528e172021-08-31 16:52:24 -04001093 cb.ctx = ctx;
1094
1095 repo_init_revisions(the_repository, &revs, NULL);
Taylor Blau08944d12021-09-28 21:55:07 -04001096 if (refs_snapshot) {
1097 read_refs_snapshot(refs_snapshot, &revs);
1098 } else {
1099 setup_revisions(0, NULL, &revs, NULL);
1100 for_each_ref(add_ref_to_pending, &revs);
1101 }
Taylor Blauc528e172021-08-31 16:52:24 -04001102
1103 /*
1104 * Skipping promisor objects here is intentional, since it only excludes
1105 * them from the list of reachable commits that we want to select from
1106 * when computing the selection of MIDX'd commits to receive bitmaps.
1107 *
1108 * Reachability bitmaps do require that their objects be closed under
1109 * reachability, but fetching any objects missing from promisors at this
1110 * point is too late. But, if one of those objects can be reached from
1111 * an another object that is included in the bitmap, then we will
1112 * complain later that we don't have reachability closure (and fail
1113 * appropriately).
1114 */
1115 fetch_if_missing = 0;
1116 revs.exclude_promisor_objects = 1;
1117
1118 if (prepare_revision_walk(&revs))
1119 die(_("revision walk setup failed"));
1120
1121 traverse_commit_list(&revs, bitmap_show_commit, NULL, &cb);
1122 if (indexed_commits_nr_p)
1123 *indexed_commits_nr_p = cb.commits_nr;
1124
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +02001125 release_revisions(&revs);
Taylor Blau2dcff522022-10-12 18:01:55 -04001126
1127 trace2_region_leave("midx", "find_commits_for_midx_bitmap",
1128 the_repository);
1129
Taylor Blauc528e172021-08-31 16:52:24 -04001130 return cb.commits;
1131}
1132
Derrick Stolee90b2bb72022-07-19 15:26:05 +00001133static int write_midx_bitmap(const char *midx_name,
1134 const unsigned char *midx_hash,
1135 struct packing_data *pdata,
1136 struct commit **commits,
1137 uint32_t commits_nr,
1138 uint32_t *pack_order,
Taylor Blauc528e172021-08-31 16:52:24 -04001139 unsigned flags)
1140{
Derrick Stolee90b2bb72022-07-19 15:26:05 +00001141 int ret, i;
Taylor Blaucaca3c92021-09-14 18:06:06 -04001142 uint16_t options = 0;
Derrick Stolee90b2bb72022-07-19 15:26:05 +00001143 struct pack_idx_entry **index;
1144 char *bitmap_name = xstrfmt("%s-%s.bitmap", midx_name,
1145 hash_to_hex(midx_hash));
Taylor Blaueb572772022-02-09 14:26:47 -05001146
Taylor Blau2dcff522022-10-12 18:01:55 -04001147 trace2_region_enter("midx", "write_midx_bitmap", the_repository);
1148
Taylor Blaucaca3c92021-09-14 18:06:06 -04001149 if (flags & MIDX_WRITE_BITMAP_HASH_CACHE)
1150 options |= BITMAP_OPT_HASH_CACHE;
1151
Abhradeep Chakraborty76f14b72022-08-14 16:55:09 +00001152 if (flags & MIDX_WRITE_BITMAP_LOOKUP_TABLE)
1153 options |= BITMAP_OPT_LOOKUP_TABLE;
1154
Taylor Blauc528e172021-08-31 16:52:24 -04001155 /*
1156 * Build the MIDX-order index based on pdata.objects (which is already
1157 * in MIDX order; c.f., 'midx_pack_order_cmp()' for the definition of
1158 * this order).
1159 */
Derrick Stolee90b2bb72022-07-19 15:26:05 +00001160 ALLOC_ARRAY(index, pdata->nr_objects);
1161 for (i = 0; i < pdata->nr_objects; i++)
1162 index[i] = &pdata->objects[i].idx;
Taylor Blauc528e172021-08-31 16:52:24 -04001163
1164 bitmap_writer_show_progress(flags & MIDX_PROGRESS);
Derrick Stolee90b2bb72022-07-19 15:26:05 +00001165 bitmap_writer_build_type_index(pdata, index, pdata->nr_objects);
Taylor Blauc528e172021-08-31 16:52:24 -04001166
1167 /*
1168 * bitmap_writer_finish expects objects in lex order, but pack_order
1169 * gives us exactly that. use it directly instead of re-sorting the
1170 * array.
1171 *
1172 * This changes the order of objects in 'index' between
1173 * bitmap_writer_build_type_index and bitmap_writer_finish.
1174 *
1175 * The same re-ordering takes place in the single-pack bitmap code via
1176 * write_idx_file(), which is called by finish_tmp_packfile(), which
1177 * happens between bitmap_writer_build_type_index() and
1178 * bitmap_writer_finish().
1179 */
Derrick Stolee90b2bb72022-07-19 15:26:05 +00001180 for (i = 0; i < pdata->nr_objects; i++)
1181 index[pack_order[i]] = &pdata->objects[i].idx;
Taylor Blauc528e172021-08-31 16:52:24 -04001182
1183 bitmap_writer_select_commits(commits, commits_nr, -1);
Derrick Stolee90b2bb72022-07-19 15:26:05 +00001184 ret = bitmap_writer_build(pdata);
Taylor Blauc528e172021-08-31 16:52:24 -04001185 if (ret < 0)
1186 goto cleanup;
1187
1188 bitmap_writer_set_checksum(midx_hash);
Derrick Stolee90b2bb72022-07-19 15:26:05 +00001189 bitmap_writer_finish(index, pdata->nr_objects, bitmap_name, options);
Taylor Blauc528e172021-08-31 16:52:24 -04001190
1191cleanup:
1192 free(index);
1193 free(bitmap_name);
Taylor Blau2dcff522022-10-12 18:01:55 -04001194
1195 trace2_region_leave("midx", "write_midx_bitmap", the_repository);
1196
Taylor Blauc528e172021-08-31 16:52:24 -04001197 return ret;
1198}
1199
Taylor Blau504131a2021-10-08 17:46:29 -04001200static struct multi_pack_index *lookup_multi_pack_index(struct repository *r,
1201 const char *object_dir)
1202{
Derrick Stoleeeafcc6d2022-04-25 18:27:12 +00001203 struct multi_pack_index *result = NULL;
Taylor Blau504131a2021-10-08 17:46:29 -04001204 struct multi_pack_index *cur;
Derrick Stoleeeafcc6d2022-04-25 18:27:12 +00001205 char *obj_dir_real = real_pathdup(object_dir, 1);
1206 struct strbuf cur_path_real = STRBUF_INIT;
Taylor Blau504131a2021-10-08 17:46:29 -04001207
1208 /* Ensure the given object_dir is local, or a known alternate. */
Derrick Stoleeeafcc6d2022-04-25 18:27:12 +00001209 find_odb(r, obj_dir_real);
Taylor Blau504131a2021-10-08 17:46:29 -04001210
1211 for (cur = get_multi_pack_index(r); cur; cur = cur->next) {
Derrick Stoleeeafcc6d2022-04-25 18:27:12 +00001212 strbuf_realpath(&cur_path_real, cur->object_dir, 1);
1213 if (!strcmp(obj_dir_real, cur_path_real.buf)) {
1214 result = cur;
1215 goto cleanup;
1216 }
Taylor Blau504131a2021-10-08 17:46:29 -04001217 }
1218
Derrick Stoleeeafcc6d2022-04-25 18:27:12 +00001219cleanup:
1220 free(obj_dir_real);
1221 strbuf_release(&cur_path_real);
1222 return result;
Taylor Blau504131a2021-10-08 17:46:29 -04001223}
1224
Taylor Blauf57a7392021-09-01 16:34:01 -04001225static int write_midx_internal(const char *object_dir,
Taylor Blau56d863e2021-09-28 21:55:01 -04001226 struct string_list *packs_to_include,
Taylor Blau9218c6a2021-03-30 11:04:11 -04001227 struct string_list *packs_to_drop,
1228 const char *preferred_pack_name,
Taylor Blau08944d12021-09-28 21:55:07 -04001229 const char *refs_snapshot,
Taylor Blau9218c6a2021-03-30 11:04:11 -04001230 unsigned flags)
Derrick Stoleea3407732018-07-12 15:39:21 -04001231{
Taylor Blau60980ae2021-10-26 17:01:21 -04001232 struct strbuf midx_name = STRBUF_INIT;
Taylor Blau9f191612021-03-30 11:04:17 -04001233 unsigned char midx_hash[GIT_MAX_RAWSZ];
Derrick Stolee396f2572018-07-12 15:39:26 -04001234 uint32_t i;
Derrick Stoleefc59e742018-07-12 15:39:22 -04001235 struct hashfile *f = NULL;
1236 struct lock_file lk;
Derrick Stolee577dc492021-02-18 14:07:26 +00001237 struct write_midx_context ctx = { 0 };
Derrick Stoleedba61752019-06-10 16:35:24 -07001238 int pack_name_concat_len = 0;
Derrick Stolee19575c72019-06-10 16:35:25 -07001239 int dropped_packs = 0;
1240 int result = 0;
Derrick Stolee63a8f0e2021-02-18 14:07:33 +00001241 struct chunkfile *cf;
Derrick Stoleefc59e742018-07-12 15:39:22 -04001242
Taylor Blau2dcff522022-10-12 18:01:55 -04001243 trace2_region_enter("midx", "write_midx_internal", the_repository);
1244
Taylor Blau60980ae2021-10-26 17:01:21 -04001245 get_midx_filename(&midx_name, object_dir);
1246 if (safe_create_leading_directories(midx_name.buf))
Derrick Stoleefc59e742018-07-12 15:39:22 -04001247 die_errno(_("unable to create leading directories of %s"),
Taylor Blau60980ae2021-10-26 17:01:21 -04001248 midx_name.buf);
Derrick Stoleefc59e742018-07-12 15:39:22 -04001249
Taylor Blau56d863e2021-09-28 21:55:01 -04001250 if (!packs_to_include) {
1251 /*
1252 * Only reference an existing MIDX when not filtering which
1253 * packs to include, since all packs and objects are copied
1254 * blindly from an existing MIDX if one is present.
1255 */
Taylor Blau504131a2021-10-08 17:46:29 -04001256 ctx.m = lookup_multi_pack_index(the_repository, object_dir);
Taylor Blauf57a7392021-09-01 16:34:01 -04001257 }
Derrick Stoleea40498a2018-07-12 15:39:36 -04001258
Taylor Blauec1e28e2021-06-23 14:39:12 -04001259 if (ctx.m && !midx_checksum_valid(ctx.m)) {
1260 warning(_("ignoring existing multi-pack-index; checksum mismatch"));
1261 ctx.m = NULL;
1262 }
1263
Derrick Stolee577dc492021-02-18 14:07:26 +00001264 ctx.nr = 0;
1265 ctx.alloc = ctx.m ? ctx.m->num_packs : 16;
1266 ctx.info = NULL;
1267 ALLOC_ARRAY(ctx.info, ctx.alloc);
Derrick Stolee396f2572018-07-12 15:39:26 -04001268
Derrick Stolee577dc492021-02-18 14:07:26 +00001269 if (ctx.m) {
1270 for (i = 0; i < ctx.m->num_packs; i++) {
1271 ALLOC_GROW(ctx.info, ctx.nr + 1, ctx.alloc);
Derrick Stoleea40498a2018-07-12 15:39:36 -04001272
Derrick Stolee577dc492021-02-18 14:07:26 +00001273 ctx.info[ctx.nr].orig_pack_int_id = i;
1274 ctx.info[ctx.nr].pack_name = xstrdup(ctx.m->pack_names[i]);
Taylor Blauc528e172021-08-31 16:52:24 -04001275 ctx.info[ctx.nr].p = ctx.m->packs[i];
Derrick Stolee577dc492021-02-18 14:07:26 +00001276 ctx.info[ctx.nr].expired = 0;
Taylor Blau5d3cd092021-08-31 16:52:02 -04001277
1278 if (flags & MIDX_WRITE_REV_INDEX) {
1279 /*
1280 * If generating a reverse index, need to have
1281 * packed_git's loaded to compare their
1282 * mtimes and object count.
1283 */
1284 if (prepare_midx_pack(the_repository, ctx.m, i)) {
1285 error(_("could not load pack"));
1286 result = 1;
1287 goto cleanup;
1288 }
1289
1290 if (open_pack_index(ctx.m->packs[i]))
1291 die(_("could not open index for %s"),
1292 ctx.m->packs[i]->pack_name);
1293 ctx.info[ctx.nr].p = ctx.m->packs[i];
1294 }
1295
Derrick Stolee577dc492021-02-18 14:07:26 +00001296 ctx.nr++;
Derrick Stoleea40498a2018-07-12 15:39:36 -04001297 }
1298 }
1299
Derrick Stolee577dc492021-02-18 14:07:26 +00001300 ctx.pack_paths_checked = 0;
William Baker840cef02019-10-21 18:39:59 +00001301 if (flags & MIDX_PROGRESS)
Derrick Stolee577dc492021-02-18 14:07:26 +00001302 ctx.progress = start_delayed_progress(_("Adding packfiles to multi-pack-index"), 0);
William Baker840cef02019-10-21 18:39:59 +00001303 else
Derrick Stolee577dc492021-02-18 14:07:26 +00001304 ctx.progress = NULL;
William Baker840cef02019-10-21 18:39:59 +00001305
Taylor Blau56d863e2021-09-28 21:55:01 -04001306 ctx.to_include = packs_to_include;
1307
Derrick Stolee577dc492021-02-18 14:07:26 +00001308 for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &ctx);
1309 stop_progress(&ctx.progress);
Derrick Stolee396f2572018-07-12 15:39:26 -04001310
Taylor Blau56d863e2021-09-28 21:55:01 -04001311 if ((ctx.m && ctx.nr == ctx.m->num_packs) &&
1312 !(packs_to_include || packs_to_drop)) {
Taylor Blauc528e172021-08-31 16:52:24 -04001313 struct bitmap_index *bitmap_git;
1314 int bitmap_exists;
1315 int want_bitmap = flags & MIDX_WRITE_BITMAP;
1316
Jeff Kingbfbb60d2021-09-09 15:56:58 -04001317 bitmap_git = prepare_midx_bitmap_git(ctx.m);
Taylor Blauc528e172021-08-31 16:52:24 -04001318 bitmap_exists = bitmap_git && bitmap_is_midx(bitmap_git);
1319 free_bitmap_index(bitmap_git);
1320
1321 if (bitmap_exists || !want_bitmap) {
1322 /*
1323 * The correct MIDX already exists, and so does a
1324 * corresponding bitmap (or one wasn't requested).
1325 */
1326 if (!want_bitmap)
1327 clear_midx_files_ext(object_dir, ".bitmap",
1328 NULL);
1329 goto cleanup;
1330 }
1331 }
Derrick Stoleea40498a2018-07-12 15:39:36 -04001332
Taylor Blau9218c6a2021-03-30 11:04:11 -04001333 if (preferred_pack_name) {
Patrick Steinhardtceb96a12023-04-14 08:01:31 +02001334 ctx.preferred_pack_idx = -1;
1335
Taylor Blau9218c6a2021-03-30 11:04:11 -04001336 for (i = 0; i < ctx.nr; i++) {
1337 if (!cmp_idx_or_pack_name(preferred_pack_name,
1338 ctx.info[i].pack_name)) {
1339 ctx.preferred_pack_idx = i;
1340 break;
1341 }
1342 }
Taylor Blau177c0d62021-08-31 16:52:04 -04001343
Patrick Steinhardtceb96a12023-04-14 08:01:31 +02001344 if (ctx.preferred_pack_idx == -1)
Taylor Blau177c0d62021-08-31 16:52:04 -04001345 warning(_("unknown preferred pack: '%s'"),
1346 preferred_pack_name);
Taylor Blauc528e172021-08-31 16:52:24 -04001347 } else if (ctx.nr &&
1348 (flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP))) {
Taylor Blau177c0d62021-08-31 16:52:04 -04001349 struct packed_git *oldest = ctx.info[ctx.preferred_pack_idx].p;
1350 ctx.preferred_pack_idx = 0;
1351
1352 if (packs_to_drop && packs_to_drop->nr)
1353 BUG("cannot write a MIDX bitmap during expiration");
1354
1355 /*
1356 * set a preferred pack when writing a bitmap to ensure that
1357 * the pack from which the first object is selected in pseudo
1358 * pack-order has all of its objects selected from that pack
1359 * (and not another pack containing a duplicate)
1360 */
1361 for (i = 1; i < ctx.nr; i++) {
1362 struct packed_git *p = ctx.info[i].p;
1363
1364 if (!oldest->num_objects || p->mtime < oldest->mtime) {
1365 oldest = p;
1366 ctx.preferred_pack_idx = i;
1367 }
1368 }
1369
1370 if (!oldest->num_objects) {
1371 /*
1372 * If all packs are empty; unset the preferred index.
1373 * This is acceptable since there will be no duplicate
1374 * objects to resolve, so the preferred value doesn't
1375 * matter.
1376 */
1377 ctx.preferred_pack_idx = -1;
1378 }
1379 } else {
1380 /*
1381 * otherwise don't mark any pack as preferred to avoid
1382 * interfering with expiration logic below
1383 */
1384 ctx.preferred_pack_idx = -1;
Taylor Blau9218c6a2021-03-30 11:04:11 -04001385 }
1386
Taylor Blau5d3cd092021-08-31 16:52:02 -04001387 if (ctx.preferred_pack_idx > -1) {
1388 struct packed_git *preferred = ctx.info[ctx.preferred_pack_idx].p;
1389 if (!preferred->num_objects) {
1390 error(_("cannot select preferred pack %s with no objects"),
1391 preferred->pack_name);
1392 result = 1;
1393 goto cleanup;
1394 }
1395 }
1396
Taylor Blau9218c6a2021-03-30 11:04:11 -04001397 ctx.entries = get_sorted_entries(ctx.m, ctx.info, ctx.nr, &ctx.entries_nr,
1398 ctx.preferred_pack_idx);
Derrick Stoleea40498a2018-07-12 15:39:36 -04001399
Derrick Stolee7a3ada12021-02-18 14:07:29 +00001400 ctx.large_offsets_needed = 0;
Derrick Stolee31bda9a2021-02-18 14:07:28 +00001401 for (i = 0; i < ctx.entries_nr; i++) {
1402 if (ctx.entries[i].offset > 0x7fffffff)
Derrick Stolee980f5252021-02-18 14:07:30 +00001403 ctx.num_large_offsets++;
Derrick Stolee31bda9a2021-02-18 14:07:28 +00001404 if (ctx.entries[i].offset > 0xffffffff)
Derrick Stolee7a3ada12021-02-18 14:07:29 +00001405 ctx.large_offsets_needed = 1;
Derrick Stolee662148c2018-07-12 15:39:32 -04001406 }
Derrick Stoleefe1ed562018-07-12 15:39:29 -04001407
Derrick Stolee577dc492021-02-18 14:07:26 +00001408 QSORT(ctx.info, ctx.nr, pack_info_compare);
Derrick Stoleed01bf2e2019-06-10 16:35:24 -07001409
Derrick Stolee19575c72019-06-10 16:35:25 -07001410 if (packs_to_drop && packs_to_drop->nr) {
1411 int drop_index = 0;
1412 int missing_drops = 0;
1413
Derrick Stolee577dc492021-02-18 14:07:26 +00001414 for (i = 0; i < ctx.nr && drop_index < packs_to_drop->nr; i++) {
1415 int cmp = strcmp(ctx.info[i].pack_name,
Derrick Stolee19575c72019-06-10 16:35:25 -07001416 packs_to_drop->items[drop_index].string);
1417
1418 if (!cmp) {
1419 drop_index++;
Derrick Stolee577dc492021-02-18 14:07:26 +00001420 ctx.info[i].expired = 1;
Derrick Stolee19575c72019-06-10 16:35:25 -07001421 } else if (cmp > 0) {
1422 error(_("did not see pack-file %s to drop"),
1423 packs_to_drop->items[drop_index].string);
1424 drop_index++;
1425 missing_drops++;
1426 i--;
1427 } else {
Derrick Stolee577dc492021-02-18 14:07:26 +00001428 ctx.info[i].expired = 0;
Derrick Stolee19575c72019-06-10 16:35:25 -07001429 }
1430 }
1431
1432 if (missing_drops) {
1433 result = 1;
1434 goto cleanup;
1435 }
1436 }
1437
Derrick Stoleed01bf2e2019-06-10 16:35:24 -07001438 /*
1439 * pack_perm stores a permutation between pack-int-ids from the
1440 * previous multi-pack-index to the new one we are writing:
1441 *
1442 * pack_perm[old_id] = new_id
1443 */
Derrick Stolee7a3ada12021-02-18 14:07:29 +00001444 ALLOC_ARRAY(ctx.pack_perm, ctx.nr);
Derrick Stolee577dc492021-02-18 14:07:26 +00001445 for (i = 0; i < ctx.nr; i++) {
1446 if (ctx.info[i].expired) {
Derrick Stolee19575c72019-06-10 16:35:25 -07001447 dropped_packs++;
Derrick Stolee7a3ada12021-02-18 14:07:29 +00001448 ctx.pack_perm[ctx.info[i].orig_pack_int_id] = PACK_EXPIRED;
Derrick Stolee19575c72019-06-10 16:35:25 -07001449 } else {
Derrick Stolee7a3ada12021-02-18 14:07:29 +00001450 ctx.pack_perm[ctx.info[i].orig_pack_int_id] = i - dropped_packs;
Derrick Stolee19575c72019-06-10 16:35:25 -07001451 }
Derrick Stoleed01bf2e2019-06-10 16:35:24 -07001452 }
1453
Derrick Stolee577dc492021-02-18 14:07:26 +00001454 for (i = 0; i < ctx.nr; i++) {
1455 if (!ctx.info[i].expired)
1456 pack_name_concat_len += strlen(ctx.info[i].pack_name) + 1;
Derrick Stolee19575c72019-06-10 16:35:25 -07001457 }
Derrick Stoleedba61752019-06-10 16:35:24 -07001458
Taylor Blau9218c6a2021-03-30 11:04:11 -04001459 /* Check that the preferred pack wasn't expired (if given). */
1460 if (preferred_pack_name) {
1461 struct pack_info *preferred = bsearch(preferred_pack_name,
1462 ctx.info, ctx.nr,
1463 sizeof(*ctx.info),
1464 idx_or_pack_name_cmp);
Taylor Blau177c0d62021-08-31 16:52:04 -04001465 if (preferred) {
Taylor Blau9218c6a2021-03-30 11:04:11 -04001466 uint32_t perm = ctx.pack_perm[preferred->orig_pack_int_id];
1467 if (perm == PACK_EXPIRED)
1468 warning(_("preferred pack '%s' is expired"),
1469 preferred_pack_name);
1470 }
1471 }
1472
Derrick Stoleedba61752019-06-10 16:35:24 -07001473 if (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT)
1474 pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
1475 (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
1476
Taylor Blau60980ae2021-10-26 17:01:21 -04001477 hold_lock_file_for_update(&lk, midx_name.buf, LOCK_DIE_ON_ERROR);
Martin Ågrenacd71602021-01-05 20:23:48 +01001478 f = hashfd(get_lock_file_fd(&lk), get_lock_file_path(&lk));
Derrick Stoleefc59e742018-07-12 15:39:22 -04001479
Derrick Stolee577dc492021-02-18 14:07:26 +00001480 if (ctx.nr - dropped_packs == 0) {
Damien Robert796d61c2020-03-28 23:18:22 +01001481 error(_("no pack files to index."));
1482 result = 1;
1483 goto cleanup;
1484 }
1485
Taylor Blaueb572772022-02-09 14:26:47 -05001486 if (!ctx.entries_nr) {
1487 if (flags & MIDX_WRITE_BITMAP)
1488 warning(_("refusing to write multi-pack .bitmap without any objects"));
1489 flags &= ~(MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP);
1490 }
1491
Derrick Stolee63a8f0e2021-02-18 14:07:33 +00001492 cf = init_chunkfile(f);
Derrick Stolee32f3c542018-07-12 15:39:27 -04001493
Derrick Stolee63a8f0e2021-02-18 14:07:33 +00001494 add_chunk(cf, MIDX_CHUNKID_PACKNAMES, pack_name_concat_len,
1495 write_midx_pack_names);
1496 add_chunk(cf, MIDX_CHUNKID_OIDFANOUT, MIDX_CHUNK_FANOUT_SIZE,
1497 write_midx_oid_fanout);
1498 add_chunk(cf, MIDX_CHUNKID_OIDLOOKUP,
Derrick Stolee329fac32021-02-18 14:07:37 +00001499 (size_t)ctx.entries_nr * the_hash_algo->rawsz,
Derrick Stolee63a8f0e2021-02-18 14:07:33 +00001500 write_midx_oid_lookup);
1501 add_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS,
Derrick Stolee329fac32021-02-18 14:07:37 +00001502 (size_t)ctx.entries_nr * MIDX_CHUNK_OFFSET_WIDTH,
Derrick Stolee63a8f0e2021-02-18 14:07:33 +00001503 write_midx_object_offsets);
Derrick Stolee32f3c542018-07-12 15:39:27 -04001504
Derrick Stolee63a8f0e2021-02-18 14:07:33 +00001505 if (ctx.large_offsets_needed)
1506 add_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS,
Derrick Stolee329fac32021-02-18 14:07:37 +00001507 (size_t)ctx.num_large_offsets * MIDX_CHUNK_LARGE_OFFSET_WIDTH,
Derrick Stolee63a8f0e2021-02-18 14:07:33 +00001508 write_midx_large_offsets);
Derrick Stolee32f3c542018-07-12 15:39:27 -04001509
Taylor Blau95e83832022-01-25 17:41:03 -05001510 if (flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP)) {
1511 ctx.pack_order = midx_pack_order(&ctx);
1512 add_chunk(cf, MIDX_CHUNKID_REVINDEX,
1513 ctx.entries_nr * sizeof(uint32_t),
1514 write_midx_revindex);
1515 }
1516
Derrick Stolee63a8f0e2021-02-18 14:07:33 +00001517 write_midx_header(f, get_num_chunks(cf), ctx.nr - dropped_packs);
1518 write_chunkfile(cf, &ctx);
Derrick Stoleefc59e742018-07-12 15:39:22 -04001519
Neeraj Singh020406e2022-03-10 22:43:21 +00001520 finalize_hashfile(f, midx_hash, FSYNC_COMPONENT_PACK_METADATA,
1521 CSUM_FSYNC | CSUM_HASH_IN_STREAM);
Derrick Stolee63a8f0e2021-02-18 14:07:33 +00001522 free_chunkfile(cf);
Taylor Blau38ff7ca2021-03-30 11:04:32 -04001523
Taylor Blau7f514b72022-01-25 17:41:17 -05001524 if (flags & MIDX_WRITE_REV_INDEX &&
1525 git_env_bool("GIT_TEST_MIDX_WRITE_REV", 0))
Taylor Blau60980ae2021-10-26 17:01:21 -04001526 write_midx_reverse_index(midx_name.buf, midx_hash, &ctx);
Derrick Stolee90b2bb72022-07-19 15:26:05 +00001527
Taylor Blauc528e172021-08-31 16:52:24 -04001528 if (flags & MIDX_WRITE_BITMAP) {
Derrick Stolee90b2bb72022-07-19 15:26:05 +00001529 struct packing_data pdata;
1530 struct commit **commits;
1531 uint32_t commits_nr;
1532
1533 if (!ctx.entries_nr)
1534 BUG("cannot write a bitmap without any objects");
1535
1536 prepare_midx_packing_data(&pdata, &ctx);
1537
1538 commits = find_commits_for_midx_bitmap(&commits_nr, refs_snapshot, &ctx);
1539
Derrick Stolee068fa542022-07-19 15:26:06 +00001540 /*
1541 * The previous steps translated the information from
1542 * 'entries' into information suitable for constructing
1543 * bitmaps. We no longer need that array, so clear it to
1544 * reduce memory pressure.
1545 */
1546 FREE_AND_NULL(ctx.entries);
1547 ctx.entries_nr = 0;
1548
Derrick Stolee90b2bb72022-07-19 15:26:05 +00001549 if (write_midx_bitmap(midx_name.buf, midx_hash, &pdata,
1550 commits, commits_nr, ctx.pack_order,
Jeff King51d1b692022-07-26 18:05:03 -04001551 flags) < 0) {
Taylor Blauc528e172021-08-31 16:52:24 -04001552 error(_("could not write multi-pack bitmap"));
1553 result = 1;
1554 goto cleanup;
1555 }
1556 }
Derrick Stolee068fa542022-07-19 15:26:06 +00001557 /*
1558 * NOTE: Do not use ctx.entries beyond this point, since it might
1559 * have been freed in the previous if block.
1560 */
Taylor Blauc528e172021-08-31 16:52:24 -04001561
1562 if (ctx.m)
1563 close_object_store(the_repository->objects);
Taylor Blau38ff7ca2021-03-30 11:04:32 -04001564
Taylor Blauae22e842021-10-08 17:46:38 -04001565 if (commit_lock_file(&lk) < 0)
1566 die_errno(_("could not write multi-pack-index"));
Derrick Stoleefc59e742018-07-12 15:39:22 -04001567
Taylor Blauc528e172021-08-31 16:52:24 -04001568 clear_midx_files_ext(object_dir, ".bitmap", midx_hash);
Taylor Blauf5909d32021-08-31 16:51:59 -04001569 clear_midx_files_ext(object_dir, ".rev", midx_hash);
1570
Derrick Stoleea40498a2018-07-12 15:39:36 -04001571cleanup:
Derrick Stolee577dc492021-02-18 14:07:26 +00001572 for (i = 0; i < ctx.nr; i++) {
1573 if (ctx.info[i].p) {
1574 close_pack(ctx.info[i].p);
1575 free(ctx.info[i].p);
Derrick Stolee396f2572018-07-12 15:39:26 -04001576 }
Derrick Stolee577dc492021-02-18 14:07:26 +00001577 free(ctx.info[i].pack_name);
Derrick Stolee396f2572018-07-12 15:39:26 -04001578 }
1579
Derrick Stolee577dc492021-02-18 14:07:26 +00001580 free(ctx.info);
Derrick Stolee31bda9a2021-02-18 14:07:28 +00001581 free(ctx.entries);
Derrick Stolee7a3ada12021-02-18 14:07:29 +00001582 free(ctx.pack_perm);
Taylor Blau38ff7ca2021-03-30 11:04:32 -04001583 free(ctx.pack_order);
Taylor Blau60980ae2021-10-26 17:01:21 -04001584 strbuf_release(&midx_name);
Taylor Blauc528e172021-08-31 16:52:24 -04001585
Taylor Blau2dcff522022-10-12 18:01:55 -04001586 trace2_region_leave("midx", "write_midx_internal", the_repository);
1587
Derrick Stolee19575c72019-06-10 16:35:25 -07001588 return result;
1589}
1590
Taylor Blau9218c6a2021-03-30 11:04:11 -04001591int write_midx_file(const char *object_dir,
1592 const char *preferred_pack_name,
Taylor Blau08944d12021-09-28 21:55:07 -04001593 const char *refs_snapshot,
Taylor Blau9218c6a2021-03-30 11:04:11 -04001594 unsigned flags)
Derrick Stolee19575c72019-06-10 16:35:25 -07001595{
Taylor Blau56d863e2021-09-28 21:55:01 -04001596 return write_midx_internal(object_dir, NULL, NULL, preferred_pack_name,
Taylor Blau08944d12021-09-28 21:55:07 -04001597 refs_snapshot, flags);
Taylor Blau56d863e2021-09-28 21:55:01 -04001598}
1599
1600int write_midx_file_only(const char *object_dir,
1601 struct string_list *packs_to_include,
1602 const char *preferred_pack_name,
Taylor Blau08944d12021-09-28 21:55:07 -04001603 const char *refs_snapshot,
Taylor Blau56d863e2021-09-28 21:55:01 -04001604 unsigned flags)
1605{
1606 return write_midx_internal(object_dir, packs_to_include, NULL,
Taylor Blau08944d12021-09-28 21:55:07 -04001607 preferred_pack_name, refs_snapshot, flags);
Derrick Stoleea3407732018-07-12 15:39:21 -04001608}
Derrick Stolee525e18c2018-07-12 15:39:40 -04001609
Taylor Blau38ff7ca2021-03-30 11:04:32 -04001610struct clear_midx_data {
1611 char *keep;
1612 const char *ext;
1613};
1614
Jeff Kingbe252d32023-02-24 01:39:24 -05001615static void clear_midx_file_ext(const char *full_path, size_t full_path_len UNUSED,
Taylor Blau38ff7ca2021-03-30 11:04:32 -04001616 const char *file_name, void *_data)
1617{
1618 struct clear_midx_data *data = _data;
1619
1620 if (!(starts_with(file_name, "multi-pack-index-") &&
1621 ends_with(file_name, data->ext)))
1622 return;
1623 if (data->keep && !strcmp(data->keep, file_name))
1624 return;
1625
1626 if (unlink(full_path))
1627 die_errno(_("failed to remove %s"), full_path);
1628}
1629
Taylor Blau426c00e2021-08-31 16:51:55 -04001630static void clear_midx_files_ext(const char *object_dir, const char *ext,
Taylor Blau38ff7ca2021-03-30 11:04:32 -04001631 unsigned char *keep_hash)
1632{
1633 struct clear_midx_data data;
1634 memset(&data, 0, sizeof(struct clear_midx_data));
1635
1636 if (keep_hash)
1637 data.keep = xstrfmt("multi-pack-index-%s%s",
1638 hash_to_hex(keep_hash), ext);
1639 data.ext = ext;
1640
Taylor Blau426c00e2021-08-31 16:51:55 -04001641 for_each_file_in_pack_dir(object_dir,
Taylor Blau38ff7ca2021-03-30 11:04:32 -04001642 clear_midx_file_ext,
1643 &data);
1644
1645 free(data.keep);
Derrick Stoleea3407732018-07-12 15:39:21 -04001646}
Derrick Stolee525e18c2018-07-12 15:39:40 -04001647
Derrick Stolee1dcd9f22018-10-12 10:34:19 -07001648void clear_midx_file(struct repository *r)
Derrick Stolee525e18c2018-07-12 15:39:40 -04001649{
Taylor Blau60980ae2021-10-26 17:01:21 -04001650 struct strbuf midx = STRBUF_INIT;
1651
1652 get_midx_filename(&midx, r->objects->odb->path);
Derrick Stolee1dcd9f22018-10-12 10:34:19 -07001653
1654 if (r->objects && r->objects->multi_pack_index) {
1655 close_midx(r->objects->multi_pack_index);
1656 r->objects->multi_pack_index = NULL;
1657 }
Derrick Stolee525e18c2018-07-12 15:39:40 -04001658
Taylor Blau60980ae2021-10-26 17:01:21 -04001659 if (remove_path(midx.buf))
1660 die(_("failed to clear multi-pack-index at %s"), midx.buf);
Derrick Stolee525e18c2018-07-12 15:39:40 -04001661
Taylor Blauc528e172021-08-31 16:52:24 -04001662 clear_midx_files_ext(r->objects->odb->path, ".bitmap", NULL);
Taylor Blau426c00e2021-08-31 16:51:55 -04001663 clear_midx_files_ext(r->objects->odb->path, ".rev", NULL);
Taylor Blau38ff7ca2021-03-30 11:04:32 -04001664
Taylor Blau60980ae2021-10-26 17:01:21 -04001665 strbuf_release(&midx);
Derrick Stolee525e18c2018-07-12 15:39:40 -04001666}
Derrick Stolee56ee7ff2018-09-13 11:02:13 -07001667
1668static int verify_midx_error;
1669
Ævar Arnfjörð Bjarmason48ca53c2021-07-13 10:05:18 +02001670__attribute__((format (printf, 1, 2)))
Derrick Stoleed4bf1d82018-09-13 11:02:19 -07001671static void midx_report(const char *fmt, ...)
1672{
1673 va_list ap;
1674 verify_midx_error = 1;
1675 va_start(ap, fmt);
1676 vfprintf(stderr, fmt, ap);
1677 fprintf(stderr, "\n");
1678 va_end(ap);
1679}
1680
Jeff Hostetler5ae18df2019-03-21 12:36:15 -07001681struct pair_pos_vs_id
1682{
1683 uint32_t pos;
1684 uint32_t pack_int_id;
1685};
1686
1687static int compare_pair_pos_vs_id(const void *_a, const void *_b)
1688{
1689 struct pair_pos_vs_id *a = (struct pair_pos_vs_id *)_a;
1690 struct pair_pos_vs_id *b = (struct pair_pos_vs_id *)_b;
1691
1692 return b->pack_int_id - a->pack_int_id;
1693}
1694
Jeff Hostetler430efb82019-03-21 12:36:14 -07001695/*
1696 * Limit calls to display_progress() for performance reasons.
1697 * The interval here was arbitrarily chosen.
1698 */
1699#define SPARSE_PROGRESS_INTERVAL (1 << 12)
1700#define midx_display_sparse_progress(progress, n) \
1701 do { \
1702 uint64_t _n = (n); \
1703 if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \
1704 display_progress(progress, _n); \
1705 } while (0)
1706
William Bakerefbc3ae2019-10-21 18:39:58 +00001707int verify_midx_file(struct repository *r, const char *object_dir, unsigned flags)
Derrick Stolee56ee7ff2018-09-13 11:02:13 -07001708{
Jeff Hostetler5ae18df2019-03-21 12:36:15 -07001709 struct pair_pos_vs_id *pairs = NULL;
Derrick Stoleed4bf1d82018-09-13 11:02:19 -07001710 uint32_t i;
William Bakerad600962019-10-21 18:40:01 +00001711 struct progress *progress = NULL;
Derrick Stolee56ee7ff2018-09-13 11:02:13 -07001712 struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
1713 verify_midx_error = 0;
1714
Derrick Stoleed9607542020-08-17 14:04:48 +00001715 if (!m) {
1716 int result = 0;
1717 struct stat sb;
Taylor Blau60980ae2021-10-26 17:01:21 -04001718 struct strbuf filename = STRBUF_INIT;
1719
1720 get_midx_filename(&filename, object_dir);
1721
1722 if (!stat(filename.buf, &sb)) {
Derrick Stoleed9607542020-08-17 14:04:48 +00001723 error(_("multi-pack-index file exists, but failed to parse"));
1724 result = 1;
1725 }
Taylor Blau60980ae2021-10-26 17:01:21 -04001726 strbuf_release(&filename);
Derrick Stoleed9607542020-08-17 14:04:48 +00001727 return result;
1728 }
Derrick Stolee56ee7ff2018-09-13 11:02:13 -07001729
Taylor Blauf89ecf72021-06-23 14:39:15 -04001730 if (!midx_checksum_valid(m))
1731 midx_report(_("incorrect checksum"));
1732
William Bakerad600962019-10-21 18:40:01 +00001733 if (flags & MIDX_PROGRESS)
Derrick Stoleeefdd2f02020-09-25 12:33:35 +00001734 progress = start_delayed_progress(_("Looking for referenced packfiles"),
William Bakerad600962019-10-21 18:40:01 +00001735 m->num_packs);
Derrick Stoleed4bf1d82018-09-13 11:02:19 -07001736 for (i = 0; i < m->num_packs; i++) {
Derrick Stolee64404a22019-04-29 09:18:55 -07001737 if (prepare_midx_pack(r, m, i))
Derrick Stoleed4bf1d82018-09-13 11:02:19 -07001738 midx_report("failed to load pack in position %d", i);
Jeff Hostetler430efb82019-03-21 12:36:14 -07001739
1740 display_progress(progress, i + 1);
Derrick Stoleed4bf1d82018-09-13 11:02:19 -07001741 }
Jeff Hostetler430efb82019-03-21 12:36:14 -07001742 stop_progress(&progress);
Derrick Stoleed4bf1d82018-09-13 11:02:19 -07001743
Derrick Stolee2f23d3f2018-09-13 11:02:20 -07001744 for (i = 0; i < 255; i++) {
1745 uint32_t oid_fanout1 = ntohl(m->chunk_oid_fanout[i]);
1746 uint32_t oid_fanout2 = ntohl(m->chunk_oid_fanout[i + 1]);
1747
1748 if (oid_fanout1 > oid_fanout2)
1749 midx_report(_("oid fanout out of order: fanout[%d] = %"PRIx32" > %"PRIx32" = fanout[%d]"),
1750 i, oid_fanout1, oid_fanout2, i + 1);
1751 }
1752
Damien Robert796d61c2020-03-28 23:18:22 +01001753 if (m->num_objects == 0) {
1754 midx_report(_("the midx contains no oid"));
1755 /*
1756 * Remaining tests assume that we have objects, so we can
1757 * return here.
1758 */
Taylor Blau492cb392021-10-26 17:01:08 -04001759 goto cleanup;
Damien Robert796d61c2020-03-28 23:18:22 +01001760 }
1761
William Bakerad600962019-10-21 18:40:01 +00001762 if (flags & MIDX_PROGRESS)
1763 progress = start_sparse_progress(_("Verifying OID order in multi-pack-index"),
1764 m->num_objects - 1);
Derrick Stolee55c56482018-09-13 11:02:22 -07001765 for (i = 0; i < m->num_objects - 1; i++) {
1766 struct object_id oid1, oid2;
1767
1768 nth_midxed_object_oid(&oid1, m, i);
1769 nth_midxed_object_oid(&oid2, m, i + 1);
1770
1771 if (oidcmp(&oid1, &oid2) >= 0)
1772 midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"),
1773 i, oid_to_hex(&oid1), oid_to_hex(&oid2), i + 1);
Derrick Stolee55c56482018-09-13 11:02:22 -07001774
Jeff Hostetler430efb82019-03-21 12:36:14 -07001775 midx_display_sparse_progress(progress, i + 1);
1776 }
1777 stop_progress(&progress);
1778
Jeff Hostetler5ae18df2019-03-21 12:36:15 -07001779 /*
1780 * Create an array mapping each object to its packfile id. Sort it
1781 * to group the objects by packfile. Use this permutation to visit
1782 * each of the objects and only require 1 packfile to be open at a
1783 * time.
1784 */
1785 ALLOC_ARRAY(pairs, m->num_objects);
1786 for (i = 0; i < m->num_objects; i++) {
1787 pairs[i].pos = i;
1788 pairs[i].pack_int_id = nth_midxed_pack_int_id(m, i);
1789 }
1790
William Bakerad600962019-10-21 18:40:01 +00001791 if (flags & MIDX_PROGRESS)
1792 progress = start_sparse_progress(_("Sorting objects by packfile"),
1793 m->num_objects);
Jeff Hostetler5ae18df2019-03-21 12:36:15 -07001794 display_progress(progress, 0); /* TODO: Measure QSORT() progress */
1795 QSORT(pairs, m->num_objects, compare_pair_pos_vs_id);
1796 stop_progress(&progress);
1797
William Bakerad600962019-10-21 18:40:01 +00001798 if (flags & MIDX_PROGRESS)
1799 progress = start_sparse_progress(_("Verifying object offsets"), m->num_objects);
Derrick Stoleecc6af732018-09-13 11:02:25 -07001800 for (i = 0; i < m->num_objects; i++) {
1801 struct object_id oid;
1802 struct pack_entry e;
1803 off_t m_offset, p_offset;
1804
Jeff Hostetler5ae18df2019-03-21 12:36:15 -07001805 if (i > 0 && pairs[i-1].pack_int_id != pairs[i].pack_int_id &&
1806 m->packs[pairs[i-1].pack_int_id])
1807 {
1808 close_pack_fd(m->packs[pairs[i-1].pack_int_id]);
1809 close_pack_index(m->packs[pairs[i-1].pack_int_id]);
1810 }
1811
1812 nth_midxed_object_oid(&oid, m, pairs[i].pos);
1813
Derrick Stolee64404a22019-04-29 09:18:55 -07001814 if (!fill_midx_entry(r, &oid, &e, m)) {
Derrick Stoleecc6af732018-09-13 11:02:25 -07001815 midx_report(_("failed to load pack entry for oid[%d] = %s"),
Jeff Hostetler5ae18df2019-03-21 12:36:15 -07001816 pairs[i].pos, oid_to_hex(&oid));
Derrick Stoleecc6af732018-09-13 11:02:25 -07001817 continue;
1818 }
1819
1820 if (open_pack_index(e.p)) {
1821 midx_report(_("failed to load pack-index for packfile %s"),
1822 e.p->pack_name);
1823 break;
1824 }
1825
1826 m_offset = e.offset;
1827 p_offset = find_pack_entry_one(oid.hash, e.p);
1828
1829 if (m_offset != p_offset)
1830 midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64" != %"PRIx64),
Jeff Hostetler5ae18df2019-03-21 12:36:15 -07001831 pairs[i].pos, oid_to_hex(&oid), m_offset, p_offset);
Derrick Stolee144d7032018-09-13 11:02:26 -07001832
Jeff Hostetler430efb82019-03-21 12:36:14 -07001833 midx_display_sparse_progress(progress, i + 1);
Derrick Stoleecc6af732018-09-13 11:02:25 -07001834 }
Derrick Stolee144d7032018-09-13 11:02:26 -07001835 stop_progress(&progress);
Derrick Stoleecc6af732018-09-13 11:02:25 -07001836
Taylor Blau492cb392021-10-26 17:01:08 -04001837cleanup:
Jeff Hostetler5ae18df2019-03-21 12:36:15 -07001838 free(pairs);
Taylor Blau492cb392021-10-26 17:01:08 -04001839 close_midx(m);
Jeff Hostetler5ae18df2019-03-21 12:36:15 -07001840
Derrick Stolee56ee7ff2018-09-13 11:02:13 -07001841 return verify_midx_error;
1842}
Derrick Stoleecff97112019-06-10 16:35:23 -07001843
William Bakerefbc3ae2019-10-21 18:39:58 +00001844int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags)
Derrick Stoleecff97112019-06-10 16:35:23 -07001845{
Derrick Stolee19575c72019-06-10 16:35:25 -07001846 uint32_t i, *count, result = 0;
1847 struct string_list packs_to_drop = STRING_LIST_INIT_DUP;
Taylor Blau98926e02021-10-08 17:46:32 -04001848 struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
William Baker8dc18f82019-10-21 18:40:00 +00001849 struct progress *progress = NULL;
Derrick Stolee19575c72019-06-10 16:35:25 -07001850
1851 if (!m)
1852 return 0;
1853
René Scharfeca56dad2021-03-13 17:17:22 +01001854 CALLOC_ARRAY(count, m->num_packs);
William Baker8dc18f82019-10-21 18:40:00 +00001855
1856 if (flags & MIDX_PROGRESS)
Derrick Stoleeefdd2f02020-09-25 12:33:35 +00001857 progress = start_delayed_progress(_("Counting referenced objects"),
William Baker8dc18f82019-10-21 18:40:00 +00001858 m->num_objects);
Derrick Stolee19575c72019-06-10 16:35:25 -07001859 for (i = 0; i < m->num_objects; i++) {
1860 int pack_int_id = nth_midxed_pack_int_id(m, i);
1861 count[pack_int_id]++;
William Baker8dc18f82019-10-21 18:40:00 +00001862 display_progress(progress, i + 1);
Derrick Stolee19575c72019-06-10 16:35:25 -07001863 }
William Baker8dc18f82019-10-21 18:40:00 +00001864 stop_progress(&progress);
Derrick Stolee19575c72019-06-10 16:35:25 -07001865
William Baker8dc18f82019-10-21 18:40:00 +00001866 if (flags & MIDX_PROGRESS)
Derrick Stoleeefdd2f02020-09-25 12:33:35 +00001867 progress = start_delayed_progress(_("Finding and deleting unreferenced packfiles"),
William Baker8dc18f82019-10-21 18:40:00 +00001868 m->num_packs);
Derrick Stolee19575c72019-06-10 16:35:25 -07001869 for (i = 0; i < m->num_packs; i++) {
1870 char *pack_name;
William Baker8dc18f82019-10-21 18:40:00 +00001871 display_progress(progress, i + 1);
Derrick Stolee19575c72019-06-10 16:35:25 -07001872
1873 if (count[i])
1874 continue;
1875
1876 if (prepare_midx_pack(r, m, i))
1877 continue;
1878
Taylor Blau757d4572022-09-19 21:55:45 -04001879 if (m->packs[i]->pack_keep || m->packs[i]->is_cruft)
Derrick Stolee19575c72019-06-10 16:35:25 -07001880 continue;
1881
1882 pack_name = xstrdup(m->packs[i]->pack_name);
1883 close_pack(m->packs[i]);
1884
1885 string_list_insert(&packs_to_drop, m->pack_names[i]);
1886 unlink_pack_path(pack_name, 0);
1887 free(pack_name);
1888 }
William Baker8dc18f82019-10-21 18:40:00 +00001889 stop_progress(&progress);
Derrick Stolee19575c72019-06-10 16:35:25 -07001890
1891 free(count);
1892
Taylor Blau98926e02021-10-08 17:46:32 -04001893 if (packs_to_drop.nr)
Taylor Blau08944d12021-09-28 21:55:07 -04001894 result = write_midx_internal(object_dir, NULL, &packs_to_drop, NULL, NULL, flags);
Derrick Stolee19575c72019-06-10 16:35:25 -07001895
1896 string_list_clear(&packs_to_drop, 0);
Taylor Blau98926e02021-10-08 17:46:32 -04001897
Derrick Stolee19575c72019-06-10 16:35:25 -07001898 return result;
Derrick Stoleecff97112019-06-10 16:35:23 -07001899}
Derrick Stolee2af890b2019-06-10 16:35:26 -07001900
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001901struct repack_info {
1902 timestamp_t mtime;
1903 uint32_t referenced_objects;
1904 uint32_t pack_int_id;
1905};
1906
1907static int compare_by_mtime(const void *a_, const void *b_)
1908{
1909 const struct repack_info *a, *b;
1910
1911 a = (const struct repack_info *)a_;
1912 b = (const struct repack_info *)b_;
1913
1914 if (a->mtime < b->mtime)
1915 return -1;
1916 if (a->mtime > b->mtime)
1917 return 1;
1918 return 0;
1919}
1920
Derrick Stolee3ce4ca02020-05-10 16:07:34 +00001921static int fill_included_packs_all(struct repository *r,
1922 struct multi_pack_index *m,
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001923 unsigned char *include_pack)
1924{
Derrick Stolee3ce4ca02020-05-10 16:07:34 +00001925 uint32_t i, count = 0;
1926 int pack_kept_objects = 0;
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001927
Derrick Stolee3ce4ca02020-05-10 16:07:34 +00001928 repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
1929
1930 for (i = 0; i < m->num_packs; i++) {
1931 if (prepare_midx_pack(r, m, i))
1932 continue;
1933 if (!pack_kept_objects && m->packs[i]->pack_keep)
1934 continue;
Taylor Blaud9f77212022-09-19 21:55:48 -04001935 if (m->packs[i]->is_cruft)
1936 continue;
Derrick Stolee3ce4ca02020-05-10 16:07:34 +00001937
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001938 include_pack[i] = 1;
Derrick Stolee3ce4ca02020-05-10 16:07:34 +00001939 count++;
1940 }
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001941
Derrick Stolee3ce4ca02020-05-10 16:07:34 +00001942 return count < 2;
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001943}
1944
1945static int fill_included_packs_batch(struct repository *r,
1946 struct multi_pack_index *m,
1947 unsigned char *include_pack,
1948 size_t batch_size)
1949{
1950 uint32_t i, packs_to_repack;
1951 size_t total_size;
Taylor Blaucb6c48c2022-09-19 21:55:50 -04001952 struct repack_info *pack_info;
Derrick Stolee3ce4ca02020-05-10 16:07:34 +00001953 int pack_kept_objects = 0;
1954
Taylor Blaucb6c48c2022-09-19 21:55:50 -04001955 CALLOC_ARRAY(pack_info, m->num_packs);
1956
Derrick Stolee3ce4ca02020-05-10 16:07:34 +00001957 repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001958
1959 for (i = 0; i < m->num_packs; i++) {
1960 pack_info[i].pack_int_id = i;
1961
1962 if (prepare_midx_pack(r, m, i))
1963 continue;
1964
1965 pack_info[i].mtime = m->packs[i]->mtime;
1966 }
1967
Taylor Blau0a8e5612022-09-19 21:55:53 -04001968 for (i = 0; i < m->num_objects; i++) {
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001969 uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
1970 pack_info[pack_int_id].referenced_objects++;
1971 }
1972
1973 QSORT(pack_info, m->num_packs, compare_by_mtime);
1974
1975 total_size = 0;
1976 packs_to_repack = 0;
1977 for (i = 0; total_size < batch_size && i < m->num_packs; i++) {
1978 int pack_int_id = pack_info[i].pack_int_id;
1979 struct packed_git *p = m->packs[pack_int_id];
1980 size_t expected_size;
1981
1982 if (!p)
1983 continue;
Derrick Stolee3ce4ca02020-05-10 16:07:34 +00001984 if (!pack_kept_objects && p->pack_keep)
1985 continue;
Taylor Blaub62ad562022-09-19 21:55:56 -04001986 if (p->is_cruft)
1987 continue;
Derrick Stoleece1e4a12019-06-10 16:35:27 -07001988 if (open_pack_index(p) || !p->num_objects)
1989 continue;
1990
1991 expected_size = (size_t)(p->pack_size
1992 * pack_info[i].referenced_objects);
1993 expected_size /= p->num_objects;
1994
1995 if (expected_size >= batch_size)
1996 continue;
1997
1998 packs_to_repack++;
1999 total_size += expected_size;
2000 include_pack[pack_int_id] = 1;
2001 }
2002
2003 free(pack_info);
2004
Derrick Stolee1eb22c72020-08-11 15:30:18 +00002005 if (packs_to_repack < 2)
Derrick Stoleece1e4a12019-06-10 16:35:27 -07002006 return 1;
2007
2008 return 0;
2009}
2010
William Bakerefbc3ae2019-10-21 18:39:58 +00002011int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, unsigned flags)
Derrick Stolee2af890b2019-06-10 16:35:26 -07002012{
Derrick Stoleece1e4a12019-06-10 16:35:27 -07002013 int result = 0;
2014 uint32_t i;
2015 unsigned char *include_pack;
2016 struct child_process cmd = CHILD_PROCESS_INIT;
René Scharfe6af3b002020-08-12 18:52:54 +02002017 FILE *cmd_in;
Derrick Stoleece1e4a12019-06-10 16:35:27 -07002018 struct strbuf base_name = STRBUF_INIT;
Taylor Blauc0f1f9d2021-10-08 17:46:35 -04002019 struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
Derrick Stoleece1e4a12019-06-10 16:35:27 -07002020
Son Luong Ngoce11d86d2020-05-10 16:07:33 +00002021 /*
2022 * When updating the default for these configuration
2023 * variables in builtin/repack.c, these must be adjusted
2024 * to match.
2025 */
2026 int delta_base_offset = 1;
2027 int use_delta_islands = 0;
2028
Derrick Stoleece1e4a12019-06-10 16:35:27 -07002029 if (!m)
2030 return 0;
2031
René Scharfeca56dad2021-03-13 17:17:22 +01002032 CALLOC_ARRAY(include_pack, m->num_packs);
Derrick Stoleece1e4a12019-06-10 16:35:27 -07002033
2034 if (batch_size) {
2035 if (fill_included_packs_batch(r, m, include_pack, batch_size))
2036 goto cleanup;
Derrick Stolee3ce4ca02020-05-10 16:07:34 +00002037 } else if (fill_included_packs_all(r, m, include_pack))
Derrick Stoleece1e4a12019-06-10 16:35:27 -07002038 goto cleanup;
2039
Son Luong Ngoce11d86d2020-05-10 16:07:33 +00002040 repo_config_get_bool(r, "repack.usedeltabaseoffset", &delta_base_offset);
2041 repo_config_get_bool(r, "repack.usedeltaislands", &use_delta_islands);
2042
Jeff Kingc972bf42020-07-28 16:25:12 -04002043 strvec_push(&cmd.args, "pack-objects");
Derrick Stoleece1e4a12019-06-10 16:35:27 -07002044
2045 strbuf_addstr(&base_name, object_dir);
2046 strbuf_addstr(&base_name, "/pack/pack");
Jeff Kingc972bf42020-07-28 16:25:12 -04002047 strvec_push(&cmd.args, base_name.buf);
William Baker64d80e72019-10-21 18:40:02 +00002048
Son Luong Ngoce11d86d2020-05-10 16:07:33 +00002049 if (delta_base_offset)
Jeff Kingc972bf42020-07-28 16:25:12 -04002050 strvec_push(&cmd.args, "--delta-base-offset");
Son Luong Ngoce11d86d2020-05-10 16:07:33 +00002051 if (use_delta_islands)
Jeff Kingc972bf42020-07-28 16:25:12 -04002052 strvec_push(&cmd.args, "--delta-islands");
Son Luong Ngoce11d86d2020-05-10 16:07:33 +00002053
William Baker64d80e72019-10-21 18:40:02 +00002054 if (flags & MIDX_PROGRESS)
Jeff Kingc972bf42020-07-28 16:25:12 -04002055 strvec_push(&cmd.args, "--progress");
William Baker64d80e72019-10-21 18:40:02 +00002056 else
Jeff Kingc972bf42020-07-28 16:25:12 -04002057 strvec_push(&cmd.args, "-q");
William Baker64d80e72019-10-21 18:40:02 +00002058
Derrick Stoleece1e4a12019-06-10 16:35:27 -07002059 strbuf_release(&base_name);
2060
2061 cmd.git_cmd = 1;
2062 cmd.in = cmd.out = -1;
2063
2064 if (start_command(&cmd)) {
2065 error(_("could not start pack-objects"));
2066 result = 1;
2067 goto cleanup;
2068 }
2069
René Scharfe6af3b002020-08-12 18:52:54 +02002070 cmd_in = xfdopen(cmd.in, "w");
2071
Derrick Stoleece1e4a12019-06-10 16:35:27 -07002072 for (i = 0; i < m->num_objects; i++) {
2073 struct object_id oid;
2074 uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
2075
2076 if (!include_pack[pack_int_id])
2077 continue;
2078
2079 nth_midxed_object_oid(&oid, m, i);
René Scharfe6af3b002020-08-12 18:52:54 +02002080 fprintf(cmd_in, "%s\n", oid_to_hex(&oid));
Derrick Stoleece1e4a12019-06-10 16:35:27 -07002081 }
René Scharfe6af3b002020-08-12 18:52:54 +02002082 fclose(cmd_in);
Derrick Stoleece1e4a12019-06-10 16:35:27 -07002083
2084 if (finish_command(&cmd)) {
2085 error(_("could not finish pack-objects"));
2086 result = 1;
2087 goto cleanup;
2088 }
2089
Taylor Blau08944d12021-09-28 21:55:07 -04002090 result = write_midx_internal(object_dir, NULL, NULL, NULL, NULL, flags);
Derrick Stoleece1e4a12019-06-10 16:35:27 -07002091
2092cleanup:
Derrick Stoleece1e4a12019-06-10 16:35:27 -07002093 free(include_pack);
2094 return result;
Derrick Stolee2af890b2019-06-10 16:35:26 -07002095}