]>
Commit | Line | Data |
---|---|---|
ec2f0269 | 1 | #include "cache.h" |
0b027f6c | 2 | #include "abspath.h" |
36bf1958 | 3 | #include "alloc.h" |
c4d25228 | 4 | #include "config.h" |
fc59e748 | 5 | #include "csum-file.h" |
396f2570 | 6 | #include "dir.h" |
f394e093 | 7 | #include "gettext.h" |
41771fa4 | 8 | #include "hex.h" |
fc59e748 | 9 | #include "lockfile.h" |
396f2570 | 10 | #include "packfile.h" |
87bed179 | 11 | #include "object-file.h" |
4d80560c | 12 | #include "object-store.h" |
bc626927 | 13 | #include "hash-lookup.h" |
a3407730 | 14 | #include "midx.h" |
144d7033 | 15 | #include "progress.h" |
d829223a | 16 | #include "trace2.h" |
ce1e4a10 | 17 | #include "run-command.h" |
18e449f8 | 18 | #include "repository.h" |
63a8f0e9 | 19 | #include "chunk-format.h" |
38ff7cab | 20 | #include "pack.h" |
c528e179 TB |
21 | #include "pack-bitmap.h" |
22 | #include "refs.h" | |
23 | #include "revision.h" | |
24 | #include "list-objects.h" | |
a3407730 | 25 | |
fc59e748 DS |
26 | #define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */ |
27 | #define MIDX_VERSION 1 | |
4d80560c DS |
28 | #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 | |
fc59e748 | 32 | #define MIDX_HEADER_SIZE 12 |
aaa95dfa | 33 | #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz) |
fc59e748 | 34 | |
32f3c541 DS |
35 | #define MIDX_CHUNK_ALIGNMENT 4 |
36 | #define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */ | |
d7cacf29 | 37 | #define MIDX_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */ |
0d5b3a5e | 38 | #define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */ |
662148c4 DS |
39 | #define MIDX_CHUNKID_OBJECTOFFSETS 0x4f4f4646 /* "OOFF" */ |
40 | #define MIDX_CHUNKID_LARGEOFFSETS 0x4c4f4646 /* "LOFF" */ | |
95e8383b | 41 | #define MIDX_CHUNKID_REVINDEX 0x52494458 /* "RIDX" */ |
d7cacf29 | 42 | #define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256) |
662148c4 DS |
43 | #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 | |
32f3c541 | 46 | |
19575c7c DS |
47 | #define PACK_EXPIRED UINT_MAX |
48 | ||
0f533c72 | 49 | const unsigned char *get_midx_checksum(struct multi_pack_index *m) |
f894081d TB |
50 | { |
51 | return m->data + m->data_len - the_hash_algo->rawsz; | |
52 | } | |
53 | ||
60980aed | 54 | void get_midx_filename(struct strbuf *out, const char *object_dir) |
fc59e748 | 55 | { |
60980aed | 56 | strbuf_addf(out, "%s/pack/multi-pack-index", object_dir); |
fc59e748 DS |
57 | } |
58 | ||
60980aed | 59 | void get_midx_rev_filename(struct strbuf *out, struct multi_pack_index *m) |
f894081d | 60 | { |
60980aed TB |
61 | get_midx_filename(out, m->object_dir); |
62 | strbuf_addf(out, "-%s.rev", hash_to_hex(get_midx_checksum(m))); | |
f894081d TB |
63 | } |
64 | ||
6ab3b8b8 DS |
65 | static 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 | ||
2cf489a3 | 78 | struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local) |
4d80560c DS |
79 | { |
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; | |
60980aed | 86 | struct strbuf midx_name = STRBUF_INIT; |
32f3c541 | 87 | uint32_t i; |
3227565c | 88 | const char *cur_pack_name; |
6ab3b8b8 | 89 | struct chunkfile *cf = NULL; |
4d80560c | 90 | |
60980aed TB |
91 | get_midx_filename(&midx_name, object_dir); |
92 | ||
93 | fd = git_open(midx_name.buf); | |
4d80560c DS |
94 | |
95 | if (fd < 0) | |
96 | goto cleanup_fail; | |
97 | if (fstat(fd, &st)) { | |
60980aed | 98 | error_errno(_("failed to read %s"), midx_name.buf); |
4d80560c DS |
99 | goto cleanup_fail; |
100 | } | |
101 | ||
102 | midx_size = xsize_t(st.st_size); | |
103 | ||
104 | if (midx_size < MIDX_MIN_SIZE) { | |
60980aed | 105 | error(_("multi-pack-index file %s is too small"), midx_name.buf); |
4d80560c DS |
106 | goto cleanup_fail; |
107 | } | |
108 | ||
60980aed | 109 | strbuf_release(&midx_name); |
4d80560c DS |
110 | |
111 | midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0); | |
6c7ff7cf | 112 | close(fd); |
4d80560c | 113 | |
577314ca | 114 | FLEX_ALLOC_STR(m, object_dir, object_dir); |
4d80560c DS |
115 | m->data = midx_map; |
116 | m->data_len = midx_size; | |
2cf489a3 | 117 | m->local = local; |
4d80560c DS |
118 | |
119 | m->signature = get_be32(m->data); | |
53ad0407 DS |
120 | if (m->signature != MIDX_SIGNATURE) |
121 | die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"), | |
4d80560c | 122 | m->signature, MIDX_SIGNATURE); |
4d80560c DS |
123 | |
124 | m->version = m->data[MIDX_BYTE_FILE_VERSION]; | |
53ad0407 DS |
125 | if (m->version != MIDX_VERSION) |
126 | die(_("multi-pack-index version %d not recognized"), | |
4d80560c | 127 | m->version); |
4d80560c DS |
128 | |
129 | hash_version = m->data[MIDX_BYTE_HASH_VERSION]; | |
d9fef9d9 | 130 | if (hash_version != oid_version(the_hash_algo)) { |
d9607542 | 131 | error(_("multi-pack-index hash version %u does not match version %u"), |
d9fef9d9 | 132 | hash_version, oid_version(the_hash_algo)); |
d9607542 DS |
133 | goto cleanup_fail; |
134 | } | |
aaa95dfa | 135 | m->hash_len = the_hash_algo->rawsz; |
4d80560c DS |
136 | |
137 | m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS]; | |
138 | ||
139 | m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS); | |
140 | ||
6ab3b8b8 | 141 | cf = init_chunkfile(NULL); |
32f3c541 | 142 | |
6ab3b8b8 DS |
143 | if (read_table_of_contents(cf, m->data, midx_size, |
144 | MIDX_HEADER_SIZE, m->num_chunks)) | |
145 | goto cleanup_fail; | |
32f3c541 | 146 | |
6ab3b8b8 | 147 | if (pair_chunk(cf, MIDX_CHUNKID_PACKNAMES, &m->chunk_pack_names) == CHUNK_NOT_FOUND) |
32f3c541 | 148 | die(_("multi-pack-index missing required pack-name chunk")); |
6ab3b8b8 | 149 | if (read_chunk(cf, MIDX_CHUNKID_OIDFANOUT, midx_read_oid_fanout, m) == CHUNK_NOT_FOUND) |
d7cacf29 | 150 | die(_("multi-pack-index missing required OID fanout chunk")); |
6ab3b8b8 | 151 | if (pair_chunk(cf, MIDX_CHUNKID_OIDLOOKUP, &m->chunk_oid_lookup) == CHUNK_NOT_FOUND) |
0d5b3a5e | 152 | die(_("multi-pack-index missing required OID lookup chunk")); |
6ab3b8b8 | 153 | if (pair_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS, &m->chunk_object_offsets) == CHUNK_NOT_FOUND) |
662148c4 | 154 | die(_("multi-pack-index missing required object offsets chunk")); |
32f3c541 | 155 | |
6ab3b8b8 DS |
156 | pair_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS, &m->chunk_large_offsets); |
157 | ||
7f514b7a TB |
158 | if (git_env_bool("GIT_TEST_MIDX_READ_RIDX", 1)) |
159 | pair_chunk(cf, MIDX_CHUNKID_REVINDEX, &m->chunk_revindex); | |
160 | ||
d7cacf29 DS |
161 | m->num_objects = ntohl(m->chunk_oid_fanout[255]); |
162 | ||
ca56dadb RS |
163 | CALLOC_ARRAY(m->pack_names, m->num_packs); |
164 | CALLOC_ARRAY(m->packs, m->num_packs); | |
3227565c DS |
165 | |
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 | ||
8e72a3c3 DS |
172 | 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'"), | |
3227565c DS |
174 | m->pack_names[i - 1], |
175 | m->pack_names[i]); | |
3227565c DS |
176 | } |
177 | ||
d829223a JH |
178 | 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 | ||
692305ec | 181 | free_chunkfile(cf); |
4d80560c DS |
182 | return m; |
183 | ||
184 | cleanup_fail: | |
185 | free(m); | |
60980aed | 186 | strbuf_release(&midx_name); |
692305ec | 187 | free_chunkfile(cf); |
4d80560c DS |
188 | if (midx_map) |
189 | munmap(midx_map, midx_size); | |
190 | if (0 <= fd) | |
191 | close(fd); | |
192 | return NULL; | |
193 | } | |
194 | ||
1dcd9f20 | 195 | void close_midx(struct multi_pack_index *m) |
a40498a1 DS |
196 | { |
197 | uint32_t i; | |
1dcd9f20 DS |
198 | |
199 | if (!m) | |
200 | return; | |
201 | ||
9bb6c2e5 TB |
202 | close_midx(m->next); |
203 | ||
a40498a1 | 204 | munmap((unsigned char *)m->data, m->data_len); |
a40498a1 DS |
205 | |
206 | for (i = 0; i < m->num_packs; i++) { | |
af96fe33 DS |
207 | if (m->packs[i]) |
208 | m->packs[i]->multi_pack_index = 0; | |
a40498a1 DS |
209 | } |
210 | FREE_AND_NULL(m->packs); | |
211 | FREE_AND_NULL(m->pack_names); | |
9bb6c2e5 | 212 | free(m); |
a40498a1 DS |
213 | } |
214 | ||
64404a24 | 215 | int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id) |
3715a633 DS |
216 | { |
217 | struct strbuf pack_name = STRBUF_INIT; | |
af96fe33 | 218 | struct packed_git *p; |
3715a633 DS |
219 | |
220 | if (pack_int_id >= m->num_packs) | |
d355e46a | 221 | die(_("bad pack-int-id: %u (%u total packs)"), |
cc6af73c | 222 | pack_int_id, m->num_packs); |
3715a633 DS |
223 | |
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 | ||
af96fe33 | 230 | p = add_packed_git(pack_name.buf, pack_name.len, m->local); |
3715a633 | 231 | strbuf_release(&pack_name); |
af96fe33 DS |
232 | |
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; | |
3715a633 DS |
242 | } |
243 | ||
244 | int 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, | |
aaa95dfa | 247 | the_hash_algo->rawsz, result); |
3715a633 DS |
248 | } |
249 | ||
8aac67a1 DS |
250 | struct 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 | ||
92e2cab9 | 257 | oidread(oid, m->chunk_oid_lookup + m->hash_len * n); |
8aac67a1 DS |
258 | return oid; |
259 | } | |
260 | ||
62f2c1b5 | 261 | off_t nth_midxed_offset(struct multi_pack_index *m, uint32_t pos) |
3715a633 DS |
262 | { |
263 | const unsigned char *offset_data; | |
264 | uint32_t offset32; | |
265 | ||
329fac3a | 266 | offset_data = m->chunk_object_offsets + (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH; |
3715a633 DS |
267 | offset32 = get_be32(offset_data + sizeof(uint32_t)); |
268 | ||
269 | if (m->chunk_large_offsets && offset32 & MIDX_LARGE_OFFSET_NEEDED) { | |
d8ac9ee1 | 270 | if (sizeof(off_t) < sizeof(uint64_t)) |
3715a633 DS |
271 | 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 | ||
62f2c1b5 | 280 | uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos) |
3715a633 | 281 | { |
329fac3a DS |
282 | return get_be32(m->chunk_object_offsets + |
283 | (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH); | |
3715a633 DS |
284 | } |
285 | ||
a8437f3c | 286 | int fill_midx_entry(struct repository *r, |
893b5635 RS |
287 | const struct object_id *oid, |
288 | struct pack_entry *e, | |
289 | struct multi_pack_index *m) | |
3715a633 | 290 | { |
893b5635 | 291 | uint32_t pos; |
3715a633 DS |
292 | uint32_t pack_int_id; |
293 | struct packed_git *p; | |
294 | ||
893b5635 RS |
295 | if (!bsearch_midx(oid, m, &pos)) |
296 | return 0; | |
297 | ||
3715a633 DS |
298 | if (pos >= m->num_objects) |
299 | return 0; | |
300 | ||
301 | pack_int_id = nth_midxed_pack_int_id(m, pos); | |
302 | ||
64404a24 | 303 | if (prepare_midx_pack(r, m, pack_int_id)) |
506ec2fb | 304 | return 0; |
3715a633 DS |
305 | 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 | ||
09ef6617 RS |
317 | if (oidset_size(&p->bad_objects) && |
318 | oidset_contains(&p->bad_objects, oid)) | |
319 | return 0; | |
c39b02ae | 320 | |
3715a633 DS |
321 | e->offset = nth_midxed_offset(m, pos); |
322 | e->p = p; | |
323 | ||
324 | return 1; | |
325 | } | |
326 | ||
013fd7ad JK |
327 | /* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */ |
328 | static 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 | ||
359 | int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name) | |
a40498a1 DS |
360 | { |
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]; | |
013fd7ad | 369 | cmp = cmp_idx_or_pack_name(idx_or_pack_name, current); |
a40498a1 DS |
370 | 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 | ||
2cf489a3 | 382 | int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local) |
c4d25228 | 383 | { |
29e2016b | 384 | struct multi_pack_index *m; |
c4d25228 | 385 | struct multi_pack_index *m_search; |
c4d25228 | 386 | |
18e449f8 DS |
387 | prepare_repo_settings(r); |
388 | if (!r->settings.core_multi_pack_index) | |
c4d25228 DS |
389 | return 0; |
390 | ||
29e2016b | 391 | for (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next) |
c4d25228 DS |
392 | if (!strcmp(object_dir, m_search->object_dir)) |
393 | return 1; | |
394 | ||
29e2016b | 395 | m = load_multi_pack_index(object_dir, local); |
c4d25228 | 396 | |
29e2016b | 397 | if (m) { |
59552fb3 TB |
398 | 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; | |
c4d25228 DS |
404 | return 1; |
405 | } | |
406 | ||
407 | return 0; | |
408 | } | |
409 | ||
fc59e748 DS |
410 | static size_t write_midx_header(struct hashfile *f, |
411 | unsigned char num_chunks, | |
412 | uint32_t num_packs) | |
413 | { | |
fc59e748 | 414 | hashwrite_be32(f, MIDX_SIGNATURE); |
014f1447 | 415 | hashwrite_u8(f, MIDX_VERSION); |
d9fef9d9 | 416 | hashwrite_u8(f, oid_version(the_hash_algo)); |
014f1447 RS |
417 | hashwrite_u8(f, num_chunks); |
418 | hashwrite_u8(f, 0); /* unused */ | |
fc59e748 DS |
419 | hashwrite_be32(f, num_packs); |
420 | ||
421 | return MIDX_HEADER_SIZE; | |
422 | } | |
423 | ||
d01bf2e6 DS |
424 | struct pack_info { |
425 | uint32_t orig_pack_int_id; | |
426 | char *pack_name; | |
427 | struct packed_git *p; | |
19575c7c | 428 | unsigned expired : 1; |
d01bf2e6 DS |
429 | }; |
430 | ||
431 | static 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 | ||
9218c6a4 TB |
438 | static 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 | ||
577dc496 | 446 | struct write_midx_context { |
d01bf2e6 | 447 | struct pack_info *info; |
396f2570 | 448 | uint32_t nr; |
d01bf2e6 | 449 | uint32_t alloc; |
a40498a1 | 450 | struct multi_pack_index *m; |
840cef0c WB |
451 | struct progress *progress; |
452 | unsigned pack_paths_checked; | |
31bda9a2 DS |
453 | |
454 | struct pack_midx_entry *entries; | |
455 | uint32_t entries_nr; | |
7a3ada11 DS |
456 | |
457 | uint32_t *pack_perm; | |
38ff7cab | 458 | uint32_t *pack_order; |
7a3ada11 | 459 | unsigned large_offsets_needed:1; |
980f525c | 460 | uint32_t num_large_offsets; |
9218c6a4 TB |
461 | |
462 | int preferred_pack_idx; | |
56d863e9 TB |
463 | |
464 | struct string_list *to_include; | |
396f2570 DS |
465 | }; |
466 | ||
467 | static void add_pack_to_midx(const char *full_path, size_t full_path_len, | |
468 | const char *file_name, void *data) | |
469 | { | |
577dc496 | 470 | struct write_midx_context *ctx = data; |
396f2570 DS |
471 | |
472 | if (ends_with(file_name, ".idx")) { | |
577dc496 | 473 | display_progress(ctx->progress, ++ctx->pack_paths_checked); |
56d863e9 TB |
474 | /* |
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 | */ | |
577dc496 | 489 | if (ctx->m && midx_contains_pack(ctx->m, file_name)) |
a40498a1 | 490 | return; |
56d863e9 TB |
491 | else if (ctx->to_include && |
492 | !string_list_has_string(ctx->to_include, file_name)) | |
493 | return; | |
a40498a1 | 494 | |
577dc496 | 495 | ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc); |
396f2570 | 496 | |
577dc496 DS |
497 | ctx->info[ctx->nr].p = add_packed_git(full_path, |
498 | full_path_len, | |
499 | 0); | |
fe1ed56f | 500 | |
577dc496 | 501 | if (!ctx->info[ctx->nr].p) { |
396f2570 DS |
502 | warning(_("failed to add packfile '%s'"), |
503 | full_path); | |
504 | return; | |
505 | } | |
506 | ||
577dc496 | 507 | if (open_pack_index(ctx->info[ctx->nr].p)) { |
fe1ed56f DS |
508 | warning(_("failed to open pack-index '%s'"), |
509 | full_path); | |
577dc496 DS |
510 | close_pack(ctx->info[ctx->nr].p); |
511 | FREE_AND_NULL(ctx->info[ctx->nr].p); | |
fe1ed56f DS |
512 | return; |
513 | } | |
514 | ||
577dc496 DS |
515 | 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++; | |
396f2570 DS |
519 | } |
520 | } | |
521 | ||
fe1ed56f DS |
522 | struct pack_midx_entry { |
523 | struct object_id oid; | |
524 | uint32_t pack_int_id; | |
525 | time_t pack_mtime; | |
526 | uint64_t offset; | |
9218c6a4 | 527 | unsigned preferred : 1; |
fe1ed56f DS |
528 | }; |
529 | ||
530 | static 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 | ||
9218c6a4 TB |
539 | /* 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 | ||
fe1ed56f DS |
545 | 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 | ||
a40498a1 | 553 | static int nth_midxed_pack_midx_entry(struct multi_pack_index *m, |
a40498a1 DS |
554 | 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); | |
d01bf2e6 | 561 | e->pack_int_id = nth_midxed_pack_int_id(m, pos); |
a40498a1 DS |
562 | 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 | ||
fe1ed56f DS |
569 | static void fill_pack_entry(uint32_t pack_int_id, |
570 | struct packed_git *p, | |
571 | uint32_t cur_object, | |
9218c6a4 TB |
572 | struct pack_midx_entry *entry, |
573 | int preferred) | |
fe1ed56f | 574 | { |
0763671b | 575 | if (nth_packed_object_id(&entry->oid, p, cur_object) < 0) |
fe1ed56f DS |
576 | 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); | |
9218c6a4 | 582 | entry->preferred = !!preferred; |
fe1ed56f DS |
583 | } |
584 | ||
989d9cbd TB |
585 | struct midx_fanout { |
586 | struct pack_midx_entry *entries; | |
587 | uint32_t nr; | |
588 | uint32_t alloc; | |
589 | }; | |
590 | ||
591 | static void midx_fanout_grow(struct midx_fanout *fanout, uint32_t nr) | |
592 | { | |
593 | ALLOC_GROW(fanout->entries, nr, fanout->alloc); | |
594 | } | |
595 | ||
596 | static void midx_fanout_sort(struct midx_fanout *fanout) | |
597 | { | |
598 | QSORT(fanout->entries, fanout->nr, midx_oid_compare); | |
599 | } | |
600 | ||
852c5301 TB |
601 | static void midx_fanout_add_midx_fanout(struct midx_fanout *fanout, |
602 | struct multi_pack_index *m, | |
99e4d084 TB |
603 | uint32_t cur_fanout, |
604 | int preferred_pack) | |
852c5301 TB |
605 | { |
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++) { | |
99e4d084 TB |
614 | 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 | ||
852c5301 TB |
623 | midx_fanout_grow(fanout, fanout->nr + 1); |
624 | nth_midxed_pack_midx_entry(m, | |
625 | &fanout->entries[fanout->nr], | |
626 | cur_object); | |
cdf517be | 627 | fanout->entries[fanout->nr].preferred = 0; |
852c5301 TB |
628 | fanout->nr++; |
629 | } | |
630 | } | |
631 | ||
1d6f4c64 TB |
632 | static 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 | ||
fe1ed56f DS |
657 | /* |
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 | */ | |
a40498a1 | 668 | static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m, |
d01bf2e6 | 669 | struct pack_info *info, |
fe1ed56f | 670 | uint32_t nr_packs, |
9218c6a4 TB |
671 | uint32_t *nr_objects, |
672 | int preferred_pack) | |
fe1ed56f DS |
673 | { |
674 | uint32_t cur_fanout, cur_pack, cur_object; | |
989d9cbd TB |
675 | uint32_t alloc_objects, total_objects = 0; |
676 | struct midx_fanout fanout = { 0 }; | |
fe1ed56f | 677 | struct pack_midx_entry *deduplicated_entries = NULL; |
a40498a1 | 678 | uint32_t start_pack = m ? m->num_packs : 0; |
fe1ed56f | 679 | |
a40498a1 | 680 | for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) |
d01bf2e6 | 681 | total_objects += info[cur_pack].p->num_objects; |
fe1ed56f DS |
682 | |
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 | */ | |
989d9cbd | 688 | alloc_objects = fanout.alloc = total_objects > 3200 ? total_objects / 200 : 16; |
fe1ed56f | 689 | |
989d9cbd | 690 | ALLOC_ARRAY(fanout.entries, fanout.alloc); |
fe1ed56f DS |
691 | ALLOC_ARRAY(deduplicated_entries, alloc_objects); |
692 | *nr_objects = 0; | |
693 | ||
694 | for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) { | |
989d9cbd | 695 | fanout.nr = 0; |
fe1ed56f | 696 | |
852c5301 | 697 | if (m) |
99e4d084 TB |
698 | midx_fanout_add_midx_fanout(&fanout, m, cur_fanout, |
699 | preferred_pack); | |
a40498a1 DS |
700 | |
701 | for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) { | |
9218c6a4 | 702 | int preferred = cur_pack == preferred_pack; |
1d6f4c64 TB |
703 | midx_fanout_add_pack_fanout(&fanout, |
704 | info, cur_pack, | |
705 | preferred, cur_fanout); | |
fe1ed56f DS |
706 | } |
707 | ||
cdf517be TB |
708 | if (-1 < preferred_pack && preferred_pack < start_pack) |
709 | midx_fanout_add_pack_fanout(&fanout, info, | |
710 | preferred_pack, 1, | |
711 | cur_fanout); | |
712 | ||
989d9cbd | 713 | midx_fanout_sort(&fanout); |
fe1ed56f DS |
714 | |
715 | /* | |
716 | * The batch is now sorted by OID and then mtime (descending). | |
717 | * Take only the first duplicate. | |
718 | */ | |
989d9cbd TB |
719 | 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)) | |
fe1ed56f DS |
722 | continue; |
723 | ||
724 | ALLOC_GROW(deduplicated_entries, *nr_objects + 1, alloc_objects); | |
725 | memcpy(&deduplicated_entries[*nr_objects], | |
989d9cbd | 726 | &fanout.entries[cur_object], |
fe1ed56f DS |
727 | sizeof(struct pack_midx_entry)); |
728 | (*nr_objects)++; | |
729 | } | |
730 | } | |
731 | ||
989d9cbd | 732 | free(fanout.entries); |
fe1ed56f DS |
733 | return deduplicated_entries; |
734 | } | |
735 | ||
0ccd713c | 736 | static int write_midx_pack_names(struct hashfile *f, void *data) |
32f3c541 | 737 | { |
b4d94142 | 738 | struct write_midx_context *ctx = data; |
32f3c541 DS |
739 | uint32_t i; |
740 | unsigned char padding[MIDX_CHUNK_ALIGNMENT]; | |
741 | size_t written = 0; | |
742 | ||
b4d94142 | 743 | for (i = 0; i < ctx->nr; i++) { |
19575c7c DS |
744 | size_t writelen; |
745 | ||
b4d94142 | 746 | if (ctx->info[i].expired) |
19575c7c | 747 | continue; |
32f3c541 | 748 | |
b4d94142 | 749 | if (i && strcmp(ctx->info[i].pack_name, ctx->info[i - 1].pack_name) <= 0) |
32f3c541 | 750 | BUG("incorrect pack-file order: %s before %s", |
b4d94142 DS |
751 | ctx->info[i - 1].pack_name, |
752 | ctx->info[i].pack_name); | |
32f3c541 | 753 | |
b4d94142 DS |
754 | writelen = strlen(ctx->info[i].pack_name) + 1; |
755 | hashwrite(f, ctx->info[i].pack_name, writelen); | |
32f3c541 DS |
756 | 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); | |
32f3c541 DS |
764 | } |
765 | ||
0ccd713c | 766 | return 0; |
32f3c541 DS |
767 | } |
768 | ||
0ccd713c DS |
769 | static int write_midx_oid_fanout(struct hashfile *f, |
770 | void *data) | |
d7cacf29 | 771 | { |
31bda9a2 DS |
772 | 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; | |
d7cacf29 DS |
775 | 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 | ||
0ccd713c | 795 | return 0; |
d7cacf29 DS |
796 | } |
797 | ||
0ccd713c DS |
798 | static int write_midx_oid_lookup(struct hashfile *f, |
799 | void *data) | |
0d5b3a5e | 800 | { |
31bda9a2 DS |
801 | struct write_midx_context *ctx = data; |
802 | unsigned char hash_len = the_hash_algo->rawsz; | |
803 | struct pack_midx_entry *list = ctx->entries; | |
0d5b3a5e | 804 | uint32_t i; |
0d5b3a5e | 805 | |
31bda9a2 | 806 | for (i = 0; i < ctx->entries_nr; i++) { |
0d5b3a5e DS |
807 | struct pack_midx_entry *obj = list++; |
808 | ||
31bda9a2 | 809 | if (i < ctx->entries_nr - 1) { |
0d5b3a5e DS |
810 | 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); | |
0d5b3a5e DS |
818 | } |
819 | ||
0ccd713c | 820 | return 0; |
0d5b3a5e DS |
821 | } |
822 | ||
0ccd713c DS |
823 | static int write_midx_object_offsets(struct hashfile *f, |
824 | void *data) | |
662148c4 | 825 | { |
7a3ada11 DS |
826 | struct write_midx_context *ctx = data; |
827 | struct pack_midx_entry *list = ctx->entries; | |
662148c4 | 828 | uint32_t i, nr_large_offset = 0; |
662148c4 | 829 | |
7a3ada11 | 830 | for (i = 0; i < ctx->entries_nr; i++) { |
662148c4 DS |
831 | struct pack_midx_entry *obj = list++; |
832 | ||
7a3ada11 | 833 | if (ctx->pack_perm[obj->pack_int_id] == PACK_EXPIRED) |
19575c7c DS |
834 | BUG("object %s is in an expired pack with int-id %d", |
835 | oid_to_hex(&obj->oid), | |
836 | obj->pack_int_id); | |
837 | ||
7a3ada11 | 838 | hashwrite_be32(f, ctx->pack_perm[obj->pack_int_id]); |
662148c4 | 839 | |
7a3ada11 | 840 | if (ctx->large_offsets_needed && obj->offset >> 31) |
662148c4 | 841 | hashwrite_be32(f, MIDX_LARGE_OFFSET_NEEDED | nr_large_offset++); |
7a3ada11 | 842 | else if (!ctx->large_offsets_needed && obj->offset >> 32) |
662148c4 DS |
843 | 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); | |
662148c4 DS |
848 | } |
849 | ||
0ccd713c | 850 | return 0; |
662148c4 DS |
851 | } |
852 | ||
0ccd713c DS |
853 | static int write_midx_large_offsets(struct hashfile *f, |
854 | void *data) | |
662148c4 | 855 | { |
980f525c DS |
856 | 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; | |
980f525c | 859 | uint32_t nr_large_offset = ctx->num_large_offsets; |
662148c4 DS |
860 | |
861 | while (nr_large_offset) { | |
61b0fcbb JK |
862 | 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; | |
662148c4 DS |
870 | |
871 | if (!(offset >> 31)) | |
872 | continue; | |
873 | ||
0ccd713c | 874 | hashwrite_be64(f, offset); |
662148c4 DS |
875 | |
876 | nr_large_offset--; | |
877 | } | |
878 | ||
0ccd713c | 879 | return 0; |
662148c4 DS |
880 | } |
881 | ||
95e8383b TB |
882 | static 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 | ||
30077524 JK |
894 | struct midx_pack_order_data { |
895 | uint32_t nr; | |
896 | uint32_t pack; | |
897 | off_t offset; | |
898 | }; | |
38ff7cab | 899 | |
30077524 JK |
900 | static int midx_pack_order_cmp(const void *va, const void *vb) |
901 | { | |
902 | const struct midx_pack_order_data *a = va, *b = vb; | |
903 | if (a->pack < b->pack) | |
38ff7cab | 904 | return -1; |
30077524 | 905 | else if (a->pack > b->pack) |
38ff7cab | 906 | return 1; |
30077524 | 907 | else if (a->offset < b->offset) |
38ff7cab | 908 | return -1; |
30077524 | 909 | else if (a->offset > b->offset) |
38ff7cab | 910 | return 1; |
30077524 JK |
911 | else |
912 | return 0; | |
38ff7cab TB |
913 | } |
914 | ||
915 | static uint32_t *midx_pack_order(struct write_midx_context *ctx) | |
916 | { | |
30077524 | 917 | struct midx_pack_order_data *data; |
38ff7cab TB |
918 | uint32_t *pack_order; |
919 | uint32_t i; | |
920 | ||
2dcff525 TB |
921 | trace2_region_enter("midx", "midx_pack_order", the_repository); |
922 | ||
30077524 JK |
923 | 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 | ||
38ff7cab TB |
935 | ALLOC_ARRAY(pack_order, ctx->entries_nr); |
936 | for (i = 0; i < ctx->entries_nr; i++) | |
30077524 JK |
937 | pack_order[i] = data[i].nr; |
938 | free(data); | |
38ff7cab | 939 | |
2dcff525 TB |
940 | trace2_region_leave("midx", "midx_pack_order", the_repository); |
941 | ||
38ff7cab TB |
942 | return pack_order; |
943 | } | |
944 | ||
945 | static 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 | ||
2dcff525 TB |
951 | trace2_region_enter("midx", "write_midx_reverse_index", the_repository); |
952 | ||
38ff7cab TB |
953 | 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); | |
2dcff525 TB |
962 | |
963 | trace2_region_leave("midx", "write_midx_reverse_index", the_repository); | |
38ff7cab TB |
964 | } |
965 | ||
426c00e4 | 966 | static void clear_midx_files_ext(const char *object_dir, const char *ext, |
38ff7cab TB |
967 | unsigned char *keep_hash); |
968 | ||
ec1e28ef TB |
969 | static int midx_checksum_valid(struct multi_pack_index *m) |
970 | { | |
971 | return hashfile_checksum_valid(m->data, m->data_len); | |
972 | } | |
973 | ||
c528e179 TB |
974 | static void prepare_midx_packing_data(struct packing_data *pdata, |
975 | struct write_midx_context *ctx) | |
976 | { | |
977 | uint32_t i; | |
978 | ||
2dcff525 TB |
979 | trace2_region_enter("midx", "prepare_midx_packing_data", the_repository); |
980 | ||
c528e179 TB |
981 | 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 | } | |
2dcff525 TB |
991 | |
992 | trace2_region_leave("midx", "prepare_midx_packing_data", the_repository); | |
c528e179 TB |
993 | } |
994 | ||
995 | static 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; | |
1dc4f1ef | 1000 | struct object_id peeled; |
c528e179 TB |
1001 | 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 | ||
1dc4f1ef TB |
1008 | if (!peel_iterated_oid(oid, &peeled)) |
1009 | oid = &peeled; | |
1010 | ||
c528e179 TB |
1011 | 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 | ||
1021 | struct bitmap_commit_cb { | |
1022 | struct commit **commits; | |
1023 | size_t commits_nr, commits_alloc; | |
1024 | ||
1025 | struct write_midx_context *ctx; | |
1026 | }; | |
1027 | ||
1028 | static 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 | ||
1035 | static 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 | ||
08944d1c TB |
1048 | static 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 | ||
c528e179 | 1083 | static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr_p, |
08944d1c | 1084 | const char *refs_snapshot, |
c528e179 TB |
1085 | struct write_midx_context *ctx) |
1086 | { | |
1087 | struct rev_info revs; | |
1088 | struct bitmap_commit_cb cb = {0}; | |
1089 | ||
2dcff525 TB |
1090 | trace2_region_enter("midx", "find_commits_for_midx_bitmap", |
1091 | the_repository); | |
1092 | ||
c528e179 TB |
1093 | cb.ctx = ctx; |
1094 | ||
1095 | repo_init_revisions(the_repository, &revs, NULL); | |
08944d1c TB |
1096 | 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 | } | |
c528e179 TB |
1102 | |
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 | ||
2108fe4a | 1125 | release_revisions(&revs); |
2dcff525 TB |
1126 | |
1127 | trace2_region_leave("midx", "find_commits_for_midx_bitmap", | |
1128 | the_repository); | |
1129 | ||
c528e179 TB |
1130 | return cb.commits; |
1131 | } | |
1132 | ||
90b2bb71 DS |
1133 | static 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, | |
c528e179 TB |
1139 | unsigned flags) |
1140 | { | |
90b2bb71 | 1141 | int ret, i; |
caca3c9f | 1142 | uint16_t options = 0; |
90b2bb71 DS |
1143 | struct pack_idx_entry **index; |
1144 | char *bitmap_name = xstrfmt("%s-%s.bitmap", midx_name, | |
1145 | hash_to_hex(midx_hash)); | |
eb57277b | 1146 | |
2dcff525 TB |
1147 | trace2_region_enter("midx", "write_midx_bitmap", the_repository); |
1148 | ||
caca3c9f TB |
1149 | if (flags & MIDX_WRITE_BITMAP_HASH_CACHE) |
1150 | options |= BITMAP_OPT_HASH_CACHE; | |
1151 | ||
76f14b77 AC |
1152 | if (flags & MIDX_WRITE_BITMAP_LOOKUP_TABLE) |
1153 | options |= BITMAP_OPT_LOOKUP_TABLE; | |
1154 | ||
c528e179 TB |
1155 | /* |
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 | */ | |
90b2bb71 DS |
1160 | ALLOC_ARRAY(index, pdata->nr_objects); |
1161 | for (i = 0; i < pdata->nr_objects; i++) | |
1162 | index[i] = &pdata->objects[i].idx; | |
c528e179 TB |
1163 | |
1164 | bitmap_writer_show_progress(flags & MIDX_PROGRESS); | |
90b2bb71 | 1165 | bitmap_writer_build_type_index(pdata, index, pdata->nr_objects); |
c528e179 TB |
1166 | |
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 | */ | |
90b2bb71 DS |
1180 | for (i = 0; i < pdata->nr_objects; i++) |
1181 | index[pack_order[i]] = &pdata->objects[i].idx; | |
c528e179 TB |
1182 | |
1183 | bitmap_writer_select_commits(commits, commits_nr, -1); | |
90b2bb71 | 1184 | ret = bitmap_writer_build(pdata); |
c528e179 TB |
1185 | if (ret < 0) |
1186 | goto cleanup; | |
1187 | ||
1188 | bitmap_writer_set_checksum(midx_hash); | |
90b2bb71 | 1189 | bitmap_writer_finish(index, pdata->nr_objects, bitmap_name, options); |
c528e179 TB |
1190 | |
1191 | cleanup: | |
1192 | free(index); | |
1193 | free(bitmap_name); | |
2dcff525 TB |
1194 | |
1195 | trace2_region_leave("midx", "write_midx_bitmap", the_repository); | |
1196 | ||
c528e179 TB |
1197 | return ret; |
1198 | } | |
1199 | ||
504131ac TB |
1200 | static struct multi_pack_index *lookup_multi_pack_index(struct repository *r, |
1201 | const char *object_dir) | |
1202 | { | |
eafcc6de | 1203 | struct multi_pack_index *result = NULL; |
504131ac | 1204 | struct multi_pack_index *cur; |
eafcc6de DS |
1205 | char *obj_dir_real = real_pathdup(object_dir, 1); |
1206 | struct strbuf cur_path_real = STRBUF_INIT; | |
504131ac TB |
1207 | |
1208 | /* Ensure the given object_dir is local, or a known alternate. */ | |
eafcc6de | 1209 | find_odb(r, obj_dir_real); |
504131ac TB |
1210 | |
1211 | for (cur = get_multi_pack_index(r); cur; cur = cur->next) { | |
eafcc6de DS |
1212 | 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 | } | |
504131ac TB |
1217 | } |
1218 | ||
eafcc6de DS |
1219 | cleanup: |
1220 | free(obj_dir_real); | |
1221 | strbuf_release(&cur_path_real); | |
1222 | return result; | |
504131ac TB |
1223 | } |
1224 | ||
f57a7396 | 1225 | static int write_midx_internal(const char *object_dir, |
56d863e9 | 1226 | struct string_list *packs_to_include, |
9218c6a4 TB |
1227 | struct string_list *packs_to_drop, |
1228 | const char *preferred_pack_name, | |
08944d1c | 1229 | const char *refs_snapshot, |
9218c6a4 | 1230 | unsigned flags) |
a3407730 | 1231 | { |
60980aed | 1232 | struct strbuf midx_name = STRBUF_INIT; |
9f191611 | 1233 | unsigned char midx_hash[GIT_MAX_RAWSZ]; |
396f2570 | 1234 | uint32_t i; |
fc59e748 DS |
1235 | struct hashfile *f = NULL; |
1236 | struct lock_file lk; | |
577dc496 | 1237 | struct write_midx_context ctx = { 0 }; |
dba6175c | 1238 | int pack_name_concat_len = 0; |
19575c7c DS |
1239 | int dropped_packs = 0; |
1240 | int result = 0; | |
63a8f0e9 | 1241 | struct chunkfile *cf; |
fc59e748 | 1242 | |
2dcff525 TB |
1243 | trace2_region_enter("midx", "write_midx_internal", the_repository); |
1244 | ||
60980aed TB |
1245 | get_midx_filename(&midx_name, object_dir); |
1246 | if (safe_create_leading_directories(midx_name.buf)) | |
fc59e748 | 1247 | die_errno(_("unable to create leading directories of %s"), |
60980aed | 1248 | midx_name.buf); |
fc59e748 | 1249 | |
56d863e9 TB |
1250 | 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 | */ | |
504131ac | 1256 | ctx.m = lookup_multi_pack_index(the_repository, object_dir); |
f57a7396 | 1257 | } |
577dc496 | 1258 | |
ec1e28ef TB |
1259 | if (ctx.m && !midx_checksum_valid(ctx.m)) { |
1260 | warning(_("ignoring existing multi-pack-index; checksum mismatch")); | |
1261 | ctx.m = NULL; | |
1262 | } | |
1263 | ||
577dc496 DS |
1264 | ctx.nr = 0; |
1265 | ctx.alloc = ctx.m ? ctx.m->num_packs : 16; | |
1266 | ctx.info = NULL; | |
1267 | ALLOC_ARRAY(ctx.info, ctx.alloc); | |
1268 | ||
1269 | if (ctx.m) { | |
1270 | for (i = 0; i < ctx.m->num_packs; i++) { | |
1271 | ALLOC_GROW(ctx.info, ctx.nr + 1, ctx.alloc); | |
1272 | ||
1273 | ctx.info[ctx.nr].orig_pack_int_id = i; | |
1274 | ctx.info[ctx.nr].pack_name = xstrdup(ctx.m->pack_names[i]); | |
c528e179 | 1275 | ctx.info[ctx.nr].p = ctx.m->packs[i]; |
577dc496 | 1276 | ctx.info[ctx.nr].expired = 0; |
5d3cd09a TB |
1277 | |
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 | ||
577dc496 | 1296 | ctx.nr++; |
a40498a1 DS |
1297 | } |
1298 | } | |
1299 | ||
577dc496 | 1300 | ctx.pack_paths_checked = 0; |
840cef0c | 1301 | if (flags & MIDX_PROGRESS) |
577dc496 | 1302 | ctx.progress = start_delayed_progress(_("Adding packfiles to multi-pack-index"), 0); |
840cef0c | 1303 | else |
577dc496 | 1304 | ctx.progress = NULL; |
840cef0c | 1305 | |
56d863e9 TB |
1306 | ctx.to_include = packs_to_include; |
1307 | ||
577dc496 DS |
1308 | for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &ctx); |
1309 | stop_progress(&ctx.progress); | |
396f2570 | 1310 | |
56d863e9 TB |
1311 | if ((ctx.m && ctx.nr == ctx.m->num_packs) && |
1312 | !(packs_to_include || packs_to_drop)) { | |
c528e179 TB |
1313 | struct bitmap_index *bitmap_git; |
1314 | int bitmap_exists; | |
1315 | int want_bitmap = flags & MIDX_WRITE_BITMAP; | |
1316 | ||
bfbb60d3 | 1317 | bitmap_git = prepare_midx_bitmap_git(ctx.m); |
c528e179 TB |
1318 | 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 | } | |
a40498a1 | 1332 | |
9218c6a4 | 1333 | if (preferred_pack_name) { |
177c0d6e | 1334 | int found = 0; |
9218c6a4 TB |
1335 | for (i = 0; i < ctx.nr; i++) { |
1336 | if (!cmp_idx_or_pack_name(preferred_pack_name, | |
1337 | ctx.info[i].pack_name)) { | |
1338 | ctx.preferred_pack_idx = i; | |
177c0d6e | 1339 | found = 1; |
9218c6a4 TB |
1340 | break; |
1341 | } | |
1342 | } | |
177c0d6e TB |
1343 | |
1344 | if (!found) | |
1345 | warning(_("unknown preferred pack: '%s'"), | |
1346 | preferred_pack_name); | |
c528e179 TB |
1347 | } else if (ctx.nr && |
1348 | (flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP))) { | |
177c0d6e TB |
1349 | 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; | |
9218c6a4 TB |
1385 | } |
1386 | ||
5d3cd09a TB |
1387 | 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 | ||
9218c6a4 TB |
1397 | ctx.entries = get_sorted_entries(ctx.m, ctx.info, ctx.nr, &ctx.entries_nr, |
1398 | ctx.preferred_pack_idx); | |
a40498a1 | 1399 | |
7a3ada11 | 1400 | ctx.large_offsets_needed = 0; |
31bda9a2 DS |
1401 | for (i = 0; i < ctx.entries_nr; i++) { |
1402 | if (ctx.entries[i].offset > 0x7fffffff) | |
980f525c | 1403 | ctx.num_large_offsets++; |
31bda9a2 | 1404 | if (ctx.entries[i].offset > 0xffffffff) |
7a3ada11 | 1405 | ctx.large_offsets_needed = 1; |
662148c4 | 1406 | } |
fe1ed56f | 1407 | |
577dc496 | 1408 | QSORT(ctx.info, ctx.nr, pack_info_compare); |
d01bf2e6 | 1409 | |
19575c7c DS |
1410 | if (packs_to_drop && packs_to_drop->nr) { |
1411 | int drop_index = 0; | |
1412 | int missing_drops = 0; | |
1413 | ||
577dc496 DS |
1414 | for (i = 0; i < ctx.nr && drop_index < packs_to_drop->nr; i++) { |
1415 | int cmp = strcmp(ctx.info[i].pack_name, | |
19575c7c DS |
1416 | packs_to_drop->items[drop_index].string); |
1417 | ||
1418 | if (!cmp) { | |
1419 | drop_index++; | |
577dc496 | 1420 | ctx.info[i].expired = 1; |
19575c7c DS |
1421 | } 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 { | |
577dc496 | 1428 | ctx.info[i].expired = 0; |
19575c7c DS |
1429 | } |
1430 | } | |
1431 | ||
1432 | if (missing_drops) { | |
1433 | result = 1; | |
1434 | goto cleanup; | |
1435 | } | |
1436 | } | |
1437 | ||
d01bf2e6 DS |
1438 | /* |
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 | */ | |
7a3ada11 | 1444 | ALLOC_ARRAY(ctx.pack_perm, ctx.nr); |
577dc496 DS |
1445 | for (i = 0; i < ctx.nr; i++) { |
1446 | if (ctx.info[i].expired) { | |
19575c7c | 1447 | dropped_packs++; |
7a3ada11 | 1448 | ctx.pack_perm[ctx.info[i].orig_pack_int_id] = PACK_EXPIRED; |
19575c7c | 1449 | } else { |
7a3ada11 | 1450 | ctx.pack_perm[ctx.info[i].orig_pack_int_id] = i - dropped_packs; |
19575c7c | 1451 | } |
d01bf2e6 DS |
1452 | } |
1453 | ||
577dc496 DS |
1454 | 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; | |
19575c7c | 1457 | } |
dba6175c | 1458 | |
9218c6a4 TB |
1459 | /* 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); | |
177c0d6e | 1465 | if (preferred) { |
9218c6a4 TB |
1466 | 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 | ||
dba6175c DS |
1473 | 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 | ||
60980aed | 1477 | hold_lock_file_for_update(&lk, midx_name.buf, LOCK_DIE_ON_ERROR); |
acd71602 | 1478 | f = hashfd(get_lock_file_fd(&lk), get_lock_file_path(&lk)); |
fc59e748 | 1479 | |
577dc496 | 1480 | if (ctx.nr - dropped_packs == 0) { |
796d61cd DR |
1481 | error(_("no pack files to index.")); |
1482 | result = 1; | |
1483 | goto cleanup; | |
1484 | } | |
1485 | ||
eb57277b TB |
1486 | 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 | ||
63a8f0e9 | 1492 | cf = init_chunkfile(f); |
32f3c541 | 1493 | |
63a8f0e9 DS |
1494 | 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, | |
329fac3a | 1499 | (size_t)ctx.entries_nr * the_hash_algo->rawsz, |
63a8f0e9 DS |
1500 | write_midx_oid_lookup); |
1501 | add_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS, | |
329fac3a | 1502 | (size_t)ctx.entries_nr * MIDX_CHUNK_OFFSET_WIDTH, |
63a8f0e9 | 1503 | write_midx_object_offsets); |
32f3c541 | 1504 | |
63a8f0e9 DS |
1505 | if (ctx.large_offsets_needed) |
1506 | add_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS, | |
329fac3a | 1507 | (size_t)ctx.num_large_offsets * MIDX_CHUNK_LARGE_OFFSET_WIDTH, |
63a8f0e9 | 1508 | write_midx_large_offsets); |
32f3c541 | 1509 | |
95e8383b TB |
1510 | 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 | ||
63a8f0e9 DS |
1517 | write_midx_header(f, get_num_chunks(cf), ctx.nr - dropped_packs); |
1518 | write_chunkfile(cf, &ctx); | |
fc59e748 | 1519 | |
020406ea NS |
1520 | finalize_hashfile(f, midx_hash, FSYNC_COMPONENT_PACK_METADATA, |
1521 | CSUM_FSYNC | CSUM_HASH_IN_STREAM); | |
63a8f0e9 | 1522 | free_chunkfile(cf); |
38ff7cab | 1523 | |
7f514b7a TB |
1524 | if (flags & MIDX_WRITE_REV_INDEX && |
1525 | git_env_bool("GIT_TEST_MIDX_WRITE_REV", 0)) | |
60980aed | 1526 | write_midx_reverse_index(midx_name.buf, midx_hash, &ctx); |
90b2bb71 | 1527 | |
c528e179 | 1528 | if (flags & MIDX_WRITE_BITMAP) { |
90b2bb71 DS |
1529 | 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 | ||
068fa54c DS |
1540 | /* |
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 | ||
90b2bb71 DS |
1549 | if (write_midx_bitmap(midx_name.buf, midx_hash, &pdata, |
1550 | commits, commits_nr, ctx.pack_order, | |
51d1b69a | 1551 | flags) < 0) { |
c528e179 TB |
1552 | error(_("could not write multi-pack bitmap")); |
1553 | result = 1; | |
1554 | goto cleanup; | |
1555 | } | |
1556 | } | |
068fa54c DS |
1557 | /* |
1558 | * NOTE: Do not use ctx.entries beyond this point, since it might | |
1559 | * have been freed in the previous if block. | |
1560 | */ | |
c528e179 TB |
1561 | |
1562 | if (ctx.m) | |
1563 | close_object_store(the_repository->objects); | |
38ff7cab | 1564 | |
ae22e841 TB |
1565 | if (commit_lock_file(&lk) < 0) |
1566 | die_errno(_("could not write multi-pack-index")); | |
fc59e748 | 1567 | |
c528e179 | 1568 | clear_midx_files_ext(object_dir, ".bitmap", midx_hash); |
f5909d34 TB |
1569 | clear_midx_files_ext(object_dir, ".rev", midx_hash); |
1570 | ||
a40498a1 | 1571 | cleanup: |
577dc496 DS |
1572 | 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); | |
396f2570 | 1576 | } |
577dc496 | 1577 | free(ctx.info[i].pack_name); |
396f2570 DS |
1578 | } |
1579 | ||
577dc496 | 1580 | free(ctx.info); |
31bda9a2 | 1581 | free(ctx.entries); |
7a3ada11 | 1582 | free(ctx.pack_perm); |
38ff7cab | 1583 | free(ctx.pack_order); |
60980aed | 1584 | strbuf_release(&midx_name); |
c528e179 | 1585 | |
2dcff525 TB |
1586 | trace2_region_leave("midx", "write_midx_internal", the_repository); |
1587 | ||
19575c7c DS |
1588 | return result; |
1589 | } | |
1590 | ||
9218c6a4 TB |
1591 | int write_midx_file(const char *object_dir, |
1592 | const char *preferred_pack_name, | |
08944d1c | 1593 | const char *refs_snapshot, |
9218c6a4 | 1594 | unsigned flags) |
19575c7c | 1595 | { |
56d863e9 | 1596 | return write_midx_internal(object_dir, NULL, NULL, preferred_pack_name, |
08944d1c | 1597 | refs_snapshot, flags); |
56d863e9 TB |
1598 | } |
1599 | ||
1600 | int write_midx_file_only(const char *object_dir, | |
1601 | struct string_list *packs_to_include, | |
1602 | const char *preferred_pack_name, | |
08944d1c | 1603 | const char *refs_snapshot, |
56d863e9 TB |
1604 | unsigned flags) |
1605 | { | |
1606 | return write_midx_internal(object_dir, packs_to_include, NULL, | |
08944d1c | 1607 | preferred_pack_name, refs_snapshot, flags); |
a3407730 | 1608 | } |
525e18c0 | 1609 | |
38ff7cab TB |
1610 | struct clear_midx_data { |
1611 | char *keep; | |
1612 | const char *ext; | |
1613 | }; | |
1614 | ||
be252d33 | 1615 | static void clear_midx_file_ext(const char *full_path, size_t full_path_len UNUSED, |
38ff7cab TB |
1616 | 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 | ||
426c00e4 | 1630 | static void clear_midx_files_ext(const char *object_dir, const char *ext, |
38ff7cab TB |
1631 | 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 | ||
426c00e4 | 1641 | for_each_file_in_pack_dir(object_dir, |
38ff7cab TB |
1642 | clear_midx_file_ext, |
1643 | &data); | |
1644 | ||
1645 | free(data.keep); | |
a3407730 | 1646 | } |
525e18c0 | 1647 | |
1dcd9f20 | 1648 | void clear_midx_file(struct repository *r) |
525e18c0 | 1649 | { |
60980aed TB |
1650 | struct strbuf midx = STRBUF_INIT; |
1651 | ||
1652 | get_midx_filename(&midx, r->objects->odb->path); | |
1dcd9f20 DS |
1653 | |
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 | } | |
525e18c0 | 1658 | |
60980aed TB |
1659 | if (remove_path(midx.buf)) |
1660 | die(_("failed to clear multi-pack-index at %s"), midx.buf); | |
525e18c0 | 1661 | |
c528e179 | 1662 | clear_midx_files_ext(r->objects->odb->path, ".bitmap", NULL); |
426c00e4 | 1663 | clear_midx_files_ext(r->objects->odb->path, ".rev", NULL); |
38ff7cab | 1664 | |
60980aed | 1665 | strbuf_release(&midx); |
525e18c0 | 1666 | } |
56ee7ff1 DS |
1667 | |
1668 | static int verify_midx_error; | |
1669 | ||
48ca53ca | 1670 | __attribute__((format (printf, 1, 2))) |
d4bf1d88 DS |
1671 | static 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 | ||
5ae18df9 JH |
1681 | struct pair_pos_vs_id |
1682 | { | |
1683 | uint32_t pos; | |
1684 | uint32_t pack_int_id; | |
1685 | }; | |
1686 | ||
1687 | static 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 | ||
430efb8a JH |
1695 | /* |
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 | ||
efbc3aee | 1707 | int verify_midx_file(struct repository *r, const char *object_dir, unsigned flags) |
56ee7ff1 | 1708 | { |
5ae18df9 | 1709 | struct pair_pos_vs_id *pairs = NULL; |
d4bf1d88 | 1710 | uint32_t i; |
ad60096d | 1711 | struct progress *progress = NULL; |
56ee7ff1 DS |
1712 | struct multi_pack_index *m = load_multi_pack_index(object_dir, 1); |
1713 | verify_midx_error = 0; | |
1714 | ||
d9607542 DS |
1715 | if (!m) { |
1716 | int result = 0; | |
1717 | struct stat sb; | |
60980aed TB |
1718 | struct strbuf filename = STRBUF_INIT; |
1719 | ||
1720 | get_midx_filename(&filename, object_dir); | |
1721 | ||
1722 | if (!stat(filename.buf, &sb)) { | |
d9607542 DS |
1723 | error(_("multi-pack-index file exists, but failed to parse")); |
1724 | result = 1; | |
1725 | } | |
60980aed | 1726 | strbuf_release(&filename); |
d9607542 DS |
1727 | return result; |
1728 | } | |
56ee7ff1 | 1729 | |
f89ecf79 TB |
1730 | if (!midx_checksum_valid(m)) |
1731 | midx_report(_("incorrect checksum")); | |
1732 | ||
ad60096d | 1733 | if (flags & MIDX_PROGRESS) |
efdd2f0d | 1734 | progress = start_delayed_progress(_("Looking for referenced packfiles"), |
ad60096d | 1735 | m->num_packs); |
d4bf1d88 | 1736 | for (i = 0; i < m->num_packs; i++) { |
64404a24 | 1737 | if (prepare_midx_pack(r, m, i)) |
d4bf1d88 | 1738 | midx_report("failed to load pack in position %d", i); |
430efb8a JH |
1739 | |
1740 | display_progress(progress, i + 1); | |
d4bf1d88 | 1741 | } |
430efb8a | 1742 | stop_progress(&progress); |
d4bf1d88 | 1743 | |
2f23d3f3 DS |
1744 | 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 | ||
796d61cd DR |
1753 | 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 | */ | |
492cb394 | 1759 | goto cleanup; |
796d61cd DR |
1760 | } |
1761 | ||
ad60096d WB |
1762 | if (flags & MIDX_PROGRESS) |
1763 | progress = start_sparse_progress(_("Verifying OID order in multi-pack-index"), | |
1764 | m->num_objects - 1); | |
55c5648d DS |
1765 | 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); | |
430efb8a JH |
1774 | |
1775 | midx_display_sparse_progress(progress, i + 1); | |
55c5648d | 1776 | } |
430efb8a | 1777 | stop_progress(&progress); |
55c5648d | 1778 | |
5ae18df9 JH |
1779 | /* |
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 | ||
ad60096d WB |
1791 | if (flags & MIDX_PROGRESS) |
1792 | progress = start_sparse_progress(_("Sorting objects by packfile"), | |
1793 | m->num_objects); | |
5ae18df9 JH |
1794 | display_progress(progress, 0); /* TODO: Measure QSORT() progress */ |
1795 | QSORT(pairs, m->num_objects, compare_pair_pos_vs_id); | |
1796 | stop_progress(&progress); | |
1797 | ||
ad60096d WB |
1798 | if (flags & MIDX_PROGRESS) |
1799 | progress = start_sparse_progress(_("Verifying object offsets"), m->num_objects); | |
cc6af73c DS |
1800 | 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 | ||
5ae18df9 JH |
1805 | 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 | ||
64404a24 | 1814 | if (!fill_midx_entry(r, &oid, &e, m)) { |
cc6af73c | 1815 | midx_report(_("failed to load pack entry for oid[%d] = %s"), |
5ae18df9 | 1816 | pairs[i].pos, oid_to_hex(&oid)); |
cc6af73c DS |
1817 | 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), | |
5ae18df9 | 1831 | pairs[i].pos, oid_to_hex(&oid), m_offset, p_offset); |
144d7033 | 1832 | |
430efb8a | 1833 | midx_display_sparse_progress(progress, i + 1); |
cc6af73c | 1834 | } |
144d7033 | 1835 | stop_progress(&progress); |
cc6af73c | 1836 | |
492cb394 | 1837 | cleanup: |
5ae18df9 | 1838 | free(pairs); |
492cb394 | 1839 | close_midx(m); |
5ae18df9 | 1840 | |
56ee7ff1 DS |
1841 | return verify_midx_error; |
1842 | } | |
cff97116 | 1843 | |
efbc3aee | 1844 | int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags) |
cff97116 | 1845 | { |
19575c7c DS |
1846 | uint32_t i, *count, result = 0; |
1847 | struct string_list packs_to_drop = STRING_LIST_INIT_DUP; | |
98926e0d | 1848 | struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir); |
8dc18f89 | 1849 | struct progress *progress = NULL; |
19575c7c DS |
1850 | |
1851 | if (!m) | |
1852 | return 0; | |
1853 | ||
ca56dadb | 1854 | CALLOC_ARRAY(count, m->num_packs); |
8dc18f89 WB |
1855 | |
1856 | if (flags & MIDX_PROGRESS) | |
efdd2f0d | 1857 | progress = start_delayed_progress(_("Counting referenced objects"), |
8dc18f89 | 1858 | m->num_objects); |
19575c7c DS |
1859 | 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]++; | |
8dc18f89 | 1862 | display_progress(progress, i + 1); |
19575c7c | 1863 | } |
8dc18f89 | 1864 | stop_progress(&progress); |
19575c7c | 1865 | |
8dc18f89 | 1866 | if (flags & MIDX_PROGRESS) |
efdd2f0d | 1867 | progress = start_delayed_progress(_("Finding and deleting unreferenced packfiles"), |
8dc18f89 | 1868 | m->num_packs); |
19575c7c DS |
1869 | for (i = 0; i < m->num_packs; i++) { |
1870 | char *pack_name; | |
8dc18f89 | 1871 | display_progress(progress, i + 1); |
19575c7c DS |
1872 | |
1873 | if (count[i]) | |
1874 | continue; | |
1875 | ||
1876 | if (prepare_midx_pack(r, m, i)) | |
1877 | continue; | |
1878 | ||
757d4579 | 1879 | if (m->packs[i]->pack_keep || m->packs[i]->is_cruft) |
19575c7c DS |
1880 | 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 | } | |
8dc18f89 | 1889 | stop_progress(&progress); |
19575c7c DS |
1890 | |
1891 | free(count); | |
1892 | ||
98926e0d | 1893 | if (packs_to_drop.nr) |
08944d1c | 1894 | result = write_midx_internal(object_dir, NULL, &packs_to_drop, NULL, NULL, flags); |
19575c7c DS |
1895 | |
1896 | string_list_clear(&packs_to_drop, 0); | |
98926e0d | 1897 | |
19575c7c | 1898 | return result; |
cff97116 | 1899 | } |
2af890bb | 1900 | |
ce1e4a10 DS |
1901 | struct repack_info { |
1902 | timestamp_t mtime; | |
1903 | uint32_t referenced_objects; | |
1904 | uint32_t pack_int_id; | |
1905 | }; | |
1906 | ||
1907 | static int compare_by_mtime(const void *a_, const void *b_) | |
2af890bb | 1908 | { |
ce1e4a10 DS |
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 | ||
3ce4ca0a DS |
1921 | static int fill_included_packs_all(struct repository *r, |
1922 | struct multi_pack_index *m, | |
ce1e4a10 DS |
1923 | unsigned char *include_pack) |
1924 | { | |
3ce4ca0a DS |
1925 | uint32_t i, count = 0; |
1926 | int pack_kept_objects = 0; | |
1927 | ||
1928 | 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; | |
d9f77214 TB |
1935 | if (m->packs[i]->is_cruft) |
1936 | continue; | |
ce1e4a10 | 1937 | |
ce1e4a10 | 1938 | include_pack[i] = 1; |
3ce4ca0a DS |
1939 | count++; |
1940 | } | |
ce1e4a10 | 1941 | |
3ce4ca0a | 1942 | return count < 2; |
ce1e4a10 DS |
1943 | } |
1944 | ||
1945 | static 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; | |
cb6c48cb | 1952 | struct repack_info *pack_info; |
3ce4ca0a DS |
1953 | int pack_kept_objects = 0; |
1954 | ||
cb6c48cb TB |
1955 | CALLOC_ARRAY(pack_info, m->num_packs); |
1956 | ||
3ce4ca0a | 1957 | repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects); |
ce1e4a10 DS |
1958 | |
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 | ||
0a8e5614 | 1968 | for (i = 0; i < m->num_objects; i++) { |
ce1e4a10 DS |
1969 | 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; | |
3ce4ca0a DS |
1984 | if (!pack_kept_objects && p->pack_keep) |
1985 | continue; | |
b62ad568 TB |
1986 | if (p->is_cruft) |
1987 | continue; | |
ce1e4a10 DS |
1988 | 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 | ||
1eb22c7d | 2005 | if (packs_to_repack < 2) |
ce1e4a10 DS |
2006 | return 1; |
2007 | ||
2af890bb DS |
2008 | return 0; |
2009 | } | |
ce1e4a10 | 2010 | |
efbc3aee | 2011 | int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, unsigned flags) |
ce1e4a10 DS |
2012 | { |
2013 | int result = 0; | |
2014 | uint32_t i; | |
2015 | unsigned char *include_pack; | |
2016 | struct child_process cmd = CHILD_PROCESS_INIT; | |
6af3b00a | 2017 | FILE *cmd_in; |
ce1e4a10 | 2018 | struct strbuf base_name = STRBUF_INIT; |
c0f1f9de | 2019 | struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir); |
ce1e4a10 | 2020 | |
e11d86de SLN |
2021 | /* |
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 | ||
ce1e4a10 DS |
2029 | if (!m) |
2030 | return 0; | |
2031 | ||
ca56dadb | 2032 | CALLOC_ARRAY(include_pack, m->num_packs); |
ce1e4a10 DS |
2033 | |
2034 | if (batch_size) { | |
2035 | if (fill_included_packs_batch(r, m, include_pack, batch_size)) | |
2036 | goto cleanup; | |
3ce4ca0a | 2037 | } else if (fill_included_packs_all(r, m, include_pack)) |
ce1e4a10 DS |
2038 | goto cleanup; |
2039 | ||
e11d86de SLN |
2040 | repo_config_get_bool(r, "repack.usedeltabaseoffset", &delta_base_offset); |
2041 | repo_config_get_bool(r, "repack.usedeltaislands", &use_delta_islands); | |
2042 | ||
c972bf4c | 2043 | strvec_push(&cmd.args, "pack-objects"); |
ce1e4a10 DS |
2044 | |
2045 | strbuf_addstr(&base_name, object_dir); | |
2046 | strbuf_addstr(&base_name, "/pack/pack"); | |
c972bf4c | 2047 | strvec_push(&cmd.args, base_name.buf); |
64d80e7d | 2048 | |
e11d86de | 2049 | if (delta_base_offset) |
c972bf4c | 2050 | strvec_push(&cmd.args, "--delta-base-offset"); |
e11d86de | 2051 | if (use_delta_islands) |
c972bf4c | 2052 | strvec_push(&cmd.args, "--delta-islands"); |
e11d86de | 2053 | |
64d80e7d | 2054 | if (flags & MIDX_PROGRESS) |
c972bf4c | 2055 | strvec_push(&cmd.args, "--progress"); |
64d80e7d | 2056 | else |
c972bf4c | 2057 | strvec_push(&cmd.args, "-q"); |
64d80e7d | 2058 | |
ce1e4a10 DS |
2059 | 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 | ||
6af3b00a RS |
2070 | cmd_in = xfdopen(cmd.in, "w"); |
2071 | ||
ce1e4a10 DS |
2072 | 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); | |
6af3b00a | 2080 | fprintf(cmd_in, "%s\n", oid_to_hex(&oid)); |
ce1e4a10 | 2081 | } |
6af3b00a | 2082 | fclose(cmd_in); |
ce1e4a10 DS |
2083 | |
2084 | if (finish_command(&cmd)) { | |
2085 | error(_("could not finish pack-objects")); | |
2086 | result = 1; | |
2087 | goto cleanup; | |
2088 | } | |
2089 | ||
08944d1c | 2090 | result = write_midx_internal(object_dir, NULL, NULL, NULL, NULL, flags); |
ce1e4a10 DS |
2091 | |
2092 | cleanup: | |
ce1e4a10 DS |
2093 | free(include_pack); |
2094 | return result; | |
2095 | } |