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