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