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