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