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