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