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