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