]>
Commit | Line | Data |
---|---|---|
5d4a6003 | 1 | #include "builtin.h" |
c323ac7d | 2 | #include "cache.h" |
a80d72db | 3 | #include "repository.h" |
b2141fc1 | 4 | #include "config.h" |
a74db82e | 5 | #include "attr.h" |
c323ac7d | 6 | #include "object.h" |
8e440259 PE |
7 | #include "blob.h" |
8 | #include "commit.h" | |
9 | #include "tag.h" | |
10 | #include "tree.h" | |
c323ac7d | 11 | #include "delta.h" |
a733cb60 | 12 | #include "pack.h" |
3449f8c4 | 13 | #include "pack-revindex.h" |
c38138cd | 14 | #include "csum-file.h" |
1b0c7174 | 15 | #include "tree-walk.h" |
b5d97e6b JH |
16 | #include "diff.h" |
17 | #include "revision.h" | |
18 | #include "list-objects.h" | |
9535ce73 JH |
19 | #include "list-objects-filter.h" |
20 | #include "list-objects-filter-options.h" | |
2834bc27 | 21 | #include "pack-objects.h" |
96a02f8f | 22 | #include "progress.h" |
f0a24aa5 | 23 | #include "refs.h" |
cf2ba13a | 24 | #include "streaming.h" |
93749194 | 25 | #include "thread-utils.h" |
6b8fda2d | 26 | #include "pack-bitmap.h" |
abcb8655 JK |
27 | #include "reachable.h" |
28 | #include "sha1-array.h" | |
edfbb2aa | 29 | #include "argv-array.h" |
ec2dd32c | 30 | #include "list.h" |
0317f455 | 31 | #include "packfile.h" |
a80d72db | 32 | #include "object-store.h" |
8ecce684 | 33 | |
99fb6e04 | 34 | static const char *pack_usage[] = { |
b8c1d275 AH |
35 | N_("git pack-objects --stdout [<options>...] [< <ref-list> | < <object-list>]"), |
36 | N_("git pack-objects [<options>...] <base-name> [< <ref-list> | < <object-list>]"), | |
99fb6e04 NTND |
37 | NULL |
38 | }; | |
c323ac7d | 39 | |
3f9ac8d2 | 40 | /* |
2834bc27 VM |
41 | * Objects we are going to pack are collected in the `to_pack` structure. |
42 | * It contains an array (dynamically expanded) of the object data, and a map | |
43 | * that can resolve SHA1s to their position in the array. | |
3f9ac8d2 | 44 | */ |
2834bc27 VM |
45 | static struct packing_data to_pack; |
46 | ||
79814f42 | 47 | static struct pack_idx_entry **written_list; |
2834bc27 | 48 | static uint32_t nr_result, nr_written; |
3f9ac8d2 | 49 | |
96f1e58f | 50 | static int non_empty; |
a7de7130 | 51 | static int reuse_delta = 1, reuse_object = 1; |
e5e9714a | 52 | static int keep_unreachable, unpack_unreachable, include_tag; |
dddbad72 | 53 | static timestamp_t unpack_unreachable_expiration; |
e26a8c47 | 54 | static int pack_loose_unreachable; |
96f1e58f | 55 | static int local; |
56dfeb62 | 56 | static int have_non_local_packs; |
96f1e58f | 57 | static int incremental; |
e96fb9b8 | 58 | static int ignore_packed_keep; |
be6b1914 | 59 | static int allow_ofs_delta; |
ebcfb379 | 60 | static struct pack_idx_option pack_idx_opts; |
d01fb92f | 61 | static const char *base_name; |
024701f1 | 62 | static int progress = 1; |
4812a93a | 63 | static int window = 10; |
568508e7 | 64 | static unsigned long pack_size_limit; |
618e613a | 65 | static int depth = 50; |
43cc2b42 | 66 | static int delta_search_threads; |
df6d6101 | 67 | static int pack_to_stdout; |
8d1d8f83 | 68 | static int num_preferred_base; |
dc6a0757 | 69 | static struct progress *progress_state; |
c323ac7d | 70 | |
6b8fda2d VM |
71 | static struct packed_git *reuse_packfile; |
72 | static uint32_t reuse_packfile_objects; | |
73 | static off_t reuse_packfile_offset; | |
74 | ||
645c432d KS |
75 | static int use_bitmap_index_default = 1; |
76 | static int use_bitmap_index = -1; | |
7cc8f971 | 77 | static int write_bitmap_index; |
ae4f07fb | 78 | static uint16_t write_bitmap_options; |
6b8fda2d | 79 | |
0c16cd49 JT |
80 | static int exclude_promisor_objects; |
81 | ||
074b2eea | 82 | static unsigned long delta_cache_size = 0; |
5749b0b2 | 83 | static unsigned long max_delta_cache_size = 256 * 1024 * 1024; |
e3dfddb3 | 84 | static unsigned long cache_max_small_delta_size = 1000; |
074b2eea | 85 | |
a97773ce BD |
86 | static unsigned long window_memory_limit = 0; |
87 | ||
9535ce73 JH |
88 | static struct list_objects_filter_options filter_options; |
89 | ||
90 | enum missing_action { | |
0c16cd49 JT |
91 | MA_ERROR = 0, /* fail if any missing objects are encountered */ |
92 | MA_ALLOW_ANY, /* silently allow ALL missing objects */ | |
93 | MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */ | |
9535ce73 JH |
94 | }; |
95 | static enum missing_action arg_missing_action; | |
96 | static show_object_fn fn_show_object; | |
97 | ||
3f9ac8d2 JH |
98 | /* |
99 | * stats | |
100 | */ | |
7cadf491 SP |
101 | static uint32_t written, written_delta; |
102 | static uint32_t reused, reused_delta; | |
3f9ac8d2 | 103 | |
7cc8f971 VM |
104 | /* |
105 | * Indexed commits | |
106 | */ | |
107 | static struct commit **indexed_commits; | |
108 | static unsigned int indexed_commits_nr; | |
109 | static unsigned int indexed_commits_alloc; | |
110 | ||
111 | static void index_commit_for_bitmap(struct commit *commit) | |
112 | { | |
113 | if (indexed_commits_nr >= indexed_commits_alloc) { | |
114 | indexed_commits_alloc = (indexed_commits_alloc + 32) * 2; | |
2756ca43 | 115 | REALLOC_ARRAY(indexed_commits, indexed_commits_alloc); |
7cc8f971 VM |
116 | } |
117 | ||
118 | indexed_commits[indexed_commits_nr++] = commit; | |
119 | } | |
780e6e73 | 120 | |
3613f9b4 | 121 | static void *get_delta(struct object_entry *entry) |
c323ac7d | 122 | { |
3613f9b4 NP |
123 | unsigned long size, base_size, delta_size; |
124 | void *buf, *base_buf, *delta_buf; | |
21666f1a | 125 | enum object_type type; |
c323ac7d | 126 | |
b4f5aca4 | 127 | buf = read_object_file(&entry->idx.oid, &type, &size); |
3613f9b4 | 128 | if (!buf) |
e6a492b7 | 129 | die("unable to read %s", oid_to_hex(&entry->idx.oid)); |
b4f5aca4 | 130 | base_buf = read_object_file(&entry->delta->idx.oid, &type, &base_size); |
3613f9b4 | 131 | if (!base_buf) |
e6a492b7 | 132 | die("unable to read %s", |
133 | oid_to_hex(&entry->delta->idx.oid)); | |
3613f9b4 | 134 | delta_buf = diff_delta(base_buf, base_size, |
dcde55bc | 135 | buf, size, &delta_size, 0); |
3613f9b4 | 136 | if (!delta_buf || delta_size != entry->delta_size) |
a6080a0a | 137 | die("delta size changed"); |
3613f9b4 NP |
138 | free(buf); |
139 | free(base_buf); | |
c323ac7d LT |
140 | return delta_buf; |
141 | } | |
142 | ||
30ebb40a NP |
143 | static unsigned long do_compress(void **pptr, unsigned long size) |
144 | { | |
ef49a7a0 | 145 | git_zstream stream; |
30ebb40a NP |
146 | void *in, *out; |
147 | unsigned long maxsize; | |
148 | ||
55bb5c91 | 149 | git_deflate_init(&stream, pack_compression_level); |
225a6f10 | 150 | maxsize = git_deflate_bound(&stream, size); |
30ebb40a NP |
151 | |
152 | in = *pptr; | |
153 | out = xmalloc(maxsize); | |
154 | *pptr = out; | |
155 | ||
156 | stream.next_in = in; | |
157 | stream.avail_in = size; | |
158 | stream.next_out = out; | |
159 | stream.avail_out = maxsize; | |
55bb5c91 | 160 | while (git_deflate(&stream, Z_FINISH) == Z_OK) |
30ebb40a | 161 | ; /* nothing */ |
55bb5c91 | 162 | git_deflate_end(&stream); |
30ebb40a NP |
163 | |
164 | free(in); | |
165 | return stream.total_out; | |
166 | } | |
167 | ||
98a3beab | 168 | static unsigned long write_large_blob_data(struct git_istream *st, struct hashfile *f, |
188960b4 | 169 | const struct object_id *oid) |
cf2ba13a NTND |
170 | { |
171 | git_zstream stream; | |
172 | unsigned char ibuf[1024 * 16]; | |
173 | unsigned char obuf[1024 * 16]; | |
174 | unsigned long olen = 0; | |
175 | ||
cf2ba13a NTND |
176 | git_deflate_init(&stream, pack_compression_level); |
177 | ||
178 | for (;;) { | |
179 | ssize_t readlen; | |
180 | int zret = Z_OK; | |
181 | readlen = read_istream(st, ibuf, sizeof(ibuf)); | |
182 | if (readlen == -1) | |
188960b4 | 183 | die(_("unable to read %s"), oid_to_hex(oid)); |
cf2ba13a NTND |
184 | |
185 | stream.next_in = ibuf; | |
186 | stream.avail_in = readlen; | |
187 | while ((stream.avail_in || readlen == 0) && | |
188 | (zret == Z_OK || zret == Z_BUF_ERROR)) { | |
189 | stream.next_out = obuf; | |
190 | stream.avail_out = sizeof(obuf); | |
191 | zret = git_deflate(&stream, readlen ? 0 : Z_FINISH); | |
98a3beab | 192 | hashwrite(f, obuf, stream.next_out - obuf); |
cf2ba13a NTND |
193 | olen += stream.next_out - obuf; |
194 | } | |
195 | if (stream.avail_in) | |
196 | die(_("deflate error (%d)"), zret); | |
197 | if (readlen == 0) { | |
198 | if (zret != Z_STREAM_END) | |
199 | die(_("deflate error (%d)"), zret); | |
200 | break; | |
201 | } | |
202 | } | |
203 | git_deflate_end(&stream); | |
204 | return olen; | |
205 | } | |
206 | ||
780e6e73 NP |
207 | /* |
208 | * we are going to reuse the existing object data as is. make | |
209 | * sure it is not corrupt. | |
210 | */ | |
079afb18 SP |
211 | static int check_pack_inflate(struct packed_git *p, |
212 | struct pack_window **w_curs, | |
6777a59f SP |
213 | off_t offset, |
214 | off_t len, | |
079afb18 SP |
215 | unsigned long expect) |
216 | { | |
ef49a7a0 | 217 | git_zstream stream; |
079afb18 SP |
218 | unsigned char fakebuf[4096], *in; |
219 | int st; | |
220 | ||
221 | memset(&stream, 0, sizeof(stream)); | |
39c68542 | 222 | git_inflate_init(&stream); |
079afb18 SP |
223 | do { |
224 | in = use_pack(p, w_curs, offset, &stream.avail_in); | |
225 | stream.next_in = in; | |
226 | stream.next_out = fakebuf; | |
227 | stream.avail_out = sizeof(fakebuf); | |
39c68542 | 228 | st = git_inflate(&stream, Z_FINISH); |
079afb18 SP |
229 | offset += stream.next_in - in; |
230 | } while (st == Z_OK || st == Z_BUF_ERROR); | |
39c68542 | 231 | git_inflate_end(&stream); |
079afb18 SP |
232 | return (st == Z_STREAM_END && |
233 | stream.total_out == expect && | |
234 | stream.total_in == len) ? 0 : -1; | |
235 | } | |
236 | ||
98a3beab | 237 | static void copy_pack_data(struct hashfile *f, |
079afb18 SP |
238 | struct packed_git *p, |
239 | struct pack_window **w_curs, | |
6777a59f SP |
240 | off_t offset, |
241 | off_t len) | |
079afb18 SP |
242 | { |
243 | unsigned char *in; | |
ef49a7a0 | 244 | unsigned long avail; |
079afb18 SP |
245 | |
246 | while (len) { | |
247 | in = use_pack(p, w_curs, offset, &avail); | |
248 | if (avail > len) | |
ef49a7a0 | 249 | avail = (unsigned long)len; |
98a3beab | 250 | hashwrite(f, in, avail); |
079afb18 SP |
251 | offset += avail; |
252 | len -= avail; | |
253 | } | |
254 | } | |
255 | ||
1b4bb16b | 256 | /* Return 0 if we will bust the pack-size limit */ |
98a3beab | 257 | static unsigned long write_no_reuse_object(struct hashfile *f, struct object_entry *entry, |
c9018b03 | 258 | unsigned long limit, int usable_delta) |
c323ac7d | 259 | { |
c9018b03 | 260 | unsigned long size, datalen; |
2c5e2865 JK |
261 | unsigned char header[MAX_PACK_OBJECT_HEADER], |
262 | dheader[MAX_PACK_OBJECT_HEADER]; | |
6777a59f | 263 | unsigned hdrlen; |
2c5ef824 | 264 | enum object_type type; |
c9018b03 | 265 | void *buf; |
cf2ba13a | 266 | struct git_istream *st = NULL; |
c9018b03 NTND |
267 | |
268 | if (!usable_delta) { | |
fd9b1bae | 269 | if (oe_type(entry) == OBJ_BLOB && |
cf2ba13a | 270 | entry->size > big_file_threshold && |
ef7b5195 | 271 | (st = open_istream(&entry->idx.oid, &type, &size, NULL)) != NULL) |
cf2ba13a NTND |
272 | buf = NULL; |
273 | else { | |
b4f5aca4 | 274 | buf = read_object_file(&entry->idx.oid, &type, &size); |
cf2ba13a | 275 | if (!buf) |
e6a492b7 | 276 | die(_("unable to read %s"), |
277 | oid_to_hex(&entry->idx.oid)); | |
cf2ba13a | 278 | } |
c9018b03 NTND |
279 | /* |
280 | * make sure no cached delta data remains from a | |
281 | * previous attempt before a pack split occurred. | |
282 | */ | |
6a83d902 | 283 | FREE_AND_NULL(entry->delta_data); |
c9018b03 NTND |
284 | entry->z_delta_size = 0; |
285 | } else if (entry->delta_data) { | |
286 | size = entry->delta_size; | |
287 | buf = entry->delta_data; | |
288 | entry->delta_data = NULL; | |
289 | type = (allow_ofs_delta && entry->delta->idx.offset) ? | |
290 | OBJ_OFS_DELTA : OBJ_REF_DELTA; | |
291 | } else { | |
292 | buf = get_delta(entry); | |
293 | size = entry->delta_size; | |
294 | type = (allow_ofs_delta && entry->delta->idx.offset) ? | |
295 | OBJ_OFS_DELTA : OBJ_REF_DELTA; | |
296 | } | |
297 | ||
cf2ba13a NTND |
298 | if (st) /* large blob case, just assume we don't compress well */ |
299 | datalen = size; | |
300 | else if (entry->z_delta_size) | |
c9018b03 NTND |
301 | datalen = entry->z_delta_size; |
302 | else | |
303 | datalen = do_compress(&buf, size); | |
304 | ||
305 | /* | |
306 | * The object header is a byte of 'type' followed by zero or | |
307 | * more bytes of length. | |
308 | */ | |
7202a6fa JK |
309 | hdrlen = encode_in_pack_object_header(header, sizeof(header), |
310 | type, size); | |
c9018b03 NTND |
311 | |
312 | if (type == OBJ_OFS_DELTA) { | |
313 | /* | |
314 | * Deltas with relative base contain an additional | |
315 | * encoding of the relative offset for the delta | |
316 | * base from this object's position in the pack. | |
317 | */ | |
318 | off_t ofs = entry->idx.offset - entry->delta->idx.offset; | |
319 | unsigned pos = sizeof(dheader) - 1; | |
320 | dheader[pos] = ofs & 127; | |
321 | while (ofs >>= 7) | |
322 | dheader[--pos] = 128 | (--ofs & 127); | |
323 | if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) { | |
cf2ba13a NTND |
324 | if (st) |
325 | close_istream(st); | |
c9018b03 NTND |
326 | free(buf); |
327 | return 0; | |
328 | } | |
98a3beab | 329 | hashwrite(f, header, hdrlen); |
330 | hashwrite(f, dheader + pos, sizeof(dheader) - pos); | |
c9018b03 NTND |
331 | hdrlen += sizeof(dheader) - pos; |
332 | } else if (type == OBJ_REF_DELTA) { | |
333 | /* | |
334 | * Deltas with a base reference contain | |
335 | * an additional 20 bytes for the base sha1. | |
336 | */ | |
337 | if (limit && hdrlen + 20 + datalen + 20 >= limit) { | |
cf2ba13a NTND |
338 | if (st) |
339 | close_istream(st); | |
c9018b03 NTND |
340 | free(buf); |
341 | return 0; | |
342 | } | |
98a3beab | 343 | hashwrite(f, header, hdrlen); |
344 | hashwrite(f, entry->delta->idx.oid.hash, 20); | |
c9018b03 NTND |
345 | hdrlen += 20; |
346 | } else { | |
347 | if (limit && hdrlen + datalen + 20 >= limit) { | |
cf2ba13a NTND |
348 | if (st) |
349 | close_istream(st); | |
c9018b03 NTND |
350 | free(buf); |
351 | return 0; | |
352 | } | |
98a3beab | 353 | hashwrite(f, header, hdrlen); |
c9018b03 | 354 | } |
cf2ba13a | 355 | if (st) { |
188960b4 | 356 | datalen = write_large_blob_data(st, f, &entry->idx.oid); |
cf2ba13a NTND |
357 | close_istream(st); |
358 | } else { | |
98a3beab | 359 | hashwrite(f, buf, datalen); |
cf2ba13a NTND |
360 | free(buf); |
361 | } | |
c9018b03 NTND |
362 | |
363 | return hdrlen + datalen; | |
364 | } | |
365 | ||
366 | /* Return 0 if we will bust the pack-size limit */ | |
98a3beab | 367 | static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry, |
af92a645 | 368 | unsigned long limit, int usable_delta) |
c9018b03 NTND |
369 | { |
370 | struct packed_git *p = entry->in_pack; | |
371 | struct pack_window *w_curs = NULL; | |
372 | struct revindex_entry *revidx; | |
373 | off_t offset; | |
fd9b1bae | 374 | enum object_type type = oe_type(entry); |
211c61c6 | 375 | off_t datalen; |
2c5e2865 JK |
376 | unsigned char header[MAX_PACK_OBJECT_HEADER], |
377 | dheader[MAX_PACK_OBJECT_HEADER]; | |
c9018b03 NTND |
378 | unsigned hdrlen; |
379 | ||
380 | if (entry->delta) | |
381 | type = (allow_ofs_delta && entry->delta->idx.offset) ? | |
382 | OBJ_OFS_DELTA : OBJ_REF_DELTA; | |
7202a6fa JK |
383 | hdrlen = encode_in_pack_object_header(header, sizeof(header), |
384 | type, entry->size); | |
c9018b03 NTND |
385 | |
386 | offset = entry->in_pack_offset; | |
387 | revidx = find_pack_revindex(p, offset); | |
388 | datalen = revidx[1].offset - offset; | |
389 | if (!pack_to_stdout && p->index_version > 1 && | |
390 | check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) { | |
e6a492b7 | 391 | error("bad packed object CRC for %s", |
392 | oid_to_hex(&entry->idx.oid)); | |
c9018b03 NTND |
393 | unuse_pack(&w_curs); |
394 | return write_no_reuse_object(f, entry, limit, usable_delta); | |
395 | } | |
396 | ||
397 | offset += entry->in_pack_header_size; | |
398 | datalen -= entry->in_pack_header_size; | |
399 | ||
400 | if (!pack_to_stdout && p->index_version == 1 && | |
401 | check_pack_inflate(p, &w_curs, offset, datalen, entry->size)) { | |
e6a492b7 | 402 | error("corrupt packed object for %s", |
403 | oid_to_hex(&entry->idx.oid)); | |
c9018b03 NTND |
404 | unuse_pack(&w_curs); |
405 | return write_no_reuse_object(f, entry, limit, usable_delta); | |
406 | } | |
407 | ||
408 | if (type == OBJ_OFS_DELTA) { | |
409 | off_t ofs = entry->idx.offset - entry->delta->idx.offset; | |
410 | unsigned pos = sizeof(dheader) - 1; | |
411 | dheader[pos] = ofs & 127; | |
412 | while (ofs >>= 7) | |
413 | dheader[--pos] = 128 | (--ofs & 127); | |
414 | if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) { | |
415 | unuse_pack(&w_curs); | |
416 | return 0; | |
417 | } | |
98a3beab | 418 | hashwrite(f, header, hdrlen); |
419 | hashwrite(f, dheader + pos, sizeof(dheader) - pos); | |
c9018b03 NTND |
420 | hdrlen += sizeof(dheader) - pos; |
421 | reused_delta++; | |
422 | } else if (type == OBJ_REF_DELTA) { | |
423 | if (limit && hdrlen + 20 + datalen + 20 >= limit) { | |
424 | unuse_pack(&w_curs); | |
425 | return 0; | |
426 | } | |
98a3beab | 427 | hashwrite(f, header, hdrlen); |
428 | hashwrite(f, entry->delta->idx.oid.hash, 20); | |
c9018b03 NTND |
429 | hdrlen += 20; |
430 | reused_delta++; | |
431 | } else { | |
432 | if (limit && hdrlen + datalen + 20 >= limit) { | |
433 | unuse_pack(&w_curs); | |
434 | return 0; | |
435 | } | |
98a3beab | 436 | hashwrite(f, header, hdrlen); |
c9018b03 NTND |
437 | } |
438 | copy_pack_data(f, p, &w_curs, offset, datalen); | |
439 | unuse_pack(&w_curs); | |
440 | reused++; | |
441 | return hdrlen + datalen; | |
442 | } | |
443 | ||
444 | /* Return 0 if we will bust the pack-size limit */ | |
98a3beab | 445 | static off_t write_object(struct hashfile *f, |
af92a645 NTND |
446 | struct object_entry *entry, |
447 | off_t write_offset) | |
c9018b03 | 448 | { |
af92a645 NTND |
449 | unsigned long limit; |
450 | off_t len; | |
2c5ef824 | 451 | int usable_delta, to_reuse; |
c323ac7d | 452 | |
78d1e84f NP |
453 | if (!pack_to_stdout) |
454 | crc32_begin(f); | |
455 | ||
a2430dde | 456 | /* apply size limit if limited packsize and not first object */ |
a1e4760f NP |
457 | if (!pack_size_limit || !nr_written) |
458 | limit = 0; | |
459 | else if (pack_size_limit <= write_offset) | |
460 | /* | |
461 | * the earlier object did not fit the limit; avoid | |
462 | * mistaking this with unlimited (i.e. limit = 0). | |
463 | */ | |
464 | limit = 1; | |
465 | else | |
466 | limit = pack_size_limit - write_offset; | |
2c5ef824 NP |
467 | |
468 | if (!entry->delta) | |
469 | usable_delta = 0; /* no delta */ | |
470 | else if (!pack_size_limit) | |
471 | usable_delta = 1; /* unlimited packfile */ | |
472 | else if (entry->delta->idx.offset == (off_t)-1) | |
473 | usable_delta = 0; /* base was written to another pack */ | |
474 | else if (entry->delta->idx.offset) | |
475 | usable_delta = 1; /* base already exists in this pack */ | |
476 | else | |
477 | usable_delta = 0; /* base could end up in another pack */ | |
478 | ||
a7de7130 | 479 | if (!reuse_object) |
fa736f72 NP |
480 | to_reuse = 0; /* explicit */ |
481 | else if (!entry->in_pack) | |
ab7cd7bb | 482 | to_reuse = 0; /* can't reuse what we don't have */ |
fd9b1bae NTND |
483 | else if (oe_type(entry) == OBJ_REF_DELTA || |
484 | oe_type(entry) == OBJ_OFS_DELTA) | |
17b08f2c DH |
485 | /* check_object() decided it for us ... */ |
486 | to_reuse = usable_delta; | |
487 | /* ... but pack split may override that */ | |
fd9b1bae | 488 | else if (oe_type(entry) != entry->in_pack_type) |
ab7cd7bb JH |
489 | to_reuse = 0; /* pack has delta which is unusable */ |
490 | else if (entry->delta) | |
491 | to_reuse = 0; /* we want to pack afresh */ | |
492 | else | |
493 | to_reuse = 1; /* we have it in-pack undeltified, | |
494 | * and we do not need to deltify it. | |
495 | */ | |
496 | ||
c9018b03 NTND |
497 | if (!to_reuse) |
498 | len = write_no_reuse_object(f, entry, limit, usable_delta); | |
499 | else | |
500 | len = write_reuse_object(f, entry, limit, usable_delta); | |
501 | if (!len) | |
502 | return 0; | |
64bd76b1 | 503 | |
17b08f2c | 504 | if (usable_delta) |
ab7cd7bb | 505 | written_delta++; |
3f9ac8d2 | 506 | written++; |
78d1e84f | 507 | if (!pack_to_stdout) |
aa7e44bf | 508 | entry->idx.crc32 = crc32_end(f); |
c9018b03 | 509 | return len; |
c323ac7d LT |
510 | } |
511 | ||
f63c79db JH |
512 | enum write_one_status { |
513 | WRITE_ONE_SKIP = -1, /* already written */ | |
514 | WRITE_ONE_BREAK = 0, /* writing this will bust the limit; not written */ | |
515 | WRITE_ONE_WRITTEN = 1, /* normal */ | |
516 | WRITE_ONE_RECURSIVE = 2 /* already scheduled to be written */ | |
517 | }; | |
518 | ||
98a3beab | 519 | static enum write_one_status write_one(struct hashfile *f, |
f63c79db JH |
520 | struct object_entry *e, |
521 | off_t *offset) | |
9d5ab962 | 522 | { |
af92a645 | 523 | off_t size; |
f63c79db | 524 | int recursing; |
d7dd0223 | 525 | |
f63c79db JH |
526 | /* |
527 | * we set offset to 1 (which is an impossible value) to mark | |
528 | * the fact that this object is involved in "write its base | |
529 | * first before writing a deltified object" recursion. | |
530 | */ | |
531 | recursing = (e->idx.offset == 1); | |
532 | if (recursing) { | |
533 | warning("recursive delta detected for object %s", | |
e6a492b7 | 534 | oid_to_hex(&e->idx.oid)); |
f63c79db JH |
535 | return WRITE_ONE_RECURSIVE; |
536 | } else if (e->idx.offset || e->preferred_base) { | |
537 | /* offset is non zero if object is written already. */ | |
538 | return WRITE_ONE_SKIP; | |
539 | } | |
d7dd0223 | 540 | |
720c9f7b | 541 | /* if we are deltified, write out base object first. */ |
f63c79db JH |
542 | if (e->delta) { |
543 | e->idx.offset = 1; /* now recurse */ | |
544 | switch (write_one(f, e->delta, offset)) { | |
545 | case WRITE_ONE_RECURSIVE: | |
546 | /* we cannot depend on this one */ | |
547 | e->delta = NULL; | |
548 | break; | |
549 | default: | |
550 | break; | |
551 | case WRITE_ONE_BREAK: | |
552 | e->idx.offset = recursing; | |
553 | return WRITE_ONE_BREAK; | |
554 | } | |
555 | } | |
d7dd0223 | 556 | |
6ed7f25e NP |
557 | e->idx.offset = *offset; |
558 | size = write_object(f, e, *offset); | |
17b08f2c | 559 | if (!size) { |
f63c79db JH |
560 | e->idx.offset = recursing; |
561 | return WRITE_ONE_BREAK; | |
17b08f2c | 562 | } |
79814f42 | 563 | written_list[nr_written++] = &e->idx; |
d7dd0223 NP |
564 | |
565 | /* make sure off_t is sufficiently large not to wrap */ | |
c03c8315 | 566 | if (signed_add_overflows(*offset, size)) |
d7dd0223 | 567 | die("pack too large for current definition of off_t"); |
6ed7f25e | 568 | *offset += size; |
f63c79db | 569 | return WRITE_ONE_WRITTEN; |
9d5ab962 JH |
570 | } |
571 | ||
d155254c | 572 | static int mark_tagged(const char *path, const struct object_id *oid, int flag, |
1b4bb16b JH |
573 | void *cb_data) |
574 | { | |
188960b4 | 575 | struct object_id peeled; |
d155254c | 576 | struct object_entry *entry = packlist_find(&to_pack, oid->hash, NULL); |
1b4bb16b JH |
577 | |
578 | if (entry) | |
579 | entry->tagged = 1; | |
b420d909 | 580 | if (!peel_ref(path, &peeled)) { |
188960b4 | 581 | entry = packlist_find(&to_pack, peeled.hash, NULL); |
1b4bb16b JH |
582 | if (entry) |
583 | entry->tagged = 1; | |
584 | } | |
585 | return 0; | |
586 | } | |
587 | ||
be126818 | 588 | static inline void add_to_write_order(struct object_entry **wo, |
92bef1a1 | 589 | unsigned int *endp, |
1b4bb16b JH |
590 | struct object_entry *e) |
591 | { | |
592 | if (e->filled) | |
593 | return; | |
594 | wo[(*endp)++] = e; | |
595 | e->filled = 1; | |
596 | } | |
597 | ||
598 | static void add_descendants_to_write_order(struct object_entry **wo, | |
92bef1a1 | 599 | unsigned int *endp, |
1b4bb16b JH |
600 | struct object_entry *e) |
601 | { | |
f380872f DM |
602 | int add_to_order = 1; |
603 | while (e) { | |
604 | if (add_to_order) { | |
605 | struct object_entry *s; | |
606 | /* add this node... */ | |
607 | add_to_write_order(wo, endp, e); | |
608 | /* all its siblings... */ | |
609 | for (s = e->delta_sibling; s; s = s->delta_sibling) { | |
610 | add_to_write_order(wo, endp, s); | |
611 | } | |
612 | } | |
613 | /* drop down a level to add left subtree nodes if possible */ | |
614 | if (e->delta_child) { | |
615 | add_to_order = 1; | |
616 | e = e->delta_child; | |
617 | } else { | |
618 | add_to_order = 0; | |
619 | /* our sibling might have some children, it is next */ | |
620 | if (e->delta_sibling) { | |
621 | e = e->delta_sibling; | |
622 | continue; | |
623 | } | |
624 | /* go back to our parent node */ | |
625 | e = e->delta; | |
626 | while (e && !e->delta_sibling) { | |
627 | /* we're on the right side of a subtree, keep | |
628 | * going up until we can go right again */ | |
629 | e = e->delta; | |
630 | } | |
631 | if (!e) { | |
632 | /* done- we hit our original root node */ | |
633 | return; | |
634 | } | |
635 | /* pass it off to sibling at this level */ | |
636 | e = e->delta_sibling; | |
637 | } | |
638 | }; | |
1b4bb16b JH |
639 | } |
640 | ||
641 | static void add_family_to_write_order(struct object_entry **wo, | |
92bef1a1 | 642 | unsigned int *endp, |
1b4bb16b JH |
643 | struct object_entry *e) |
644 | { | |
645 | struct object_entry *root; | |
646 | ||
647 | for (root = e; root->delta; root = root->delta) | |
648 | ; /* nothing */ | |
1b4bb16b JH |
649 | add_descendants_to_write_order(wo, endp, root); |
650 | } | |
651 | ||
652 | static struct object_entry **compute_write_order(void) | |
653 | { | |
38d4debb | 654 | unsigned int i, wo_end, last_untagged; |
1b4bb16b | 655 | |
b32fa95f | 656 | struct object_entry **wo; |
2834bc27 | 657 | struct object_entry *objects = to_pack.objects; |
1b4bb16b | 658 | |
2834bc27 | 659 | for (i = 0; i < to_pack.nr_objects; i++) { |
1b4bb16b JH |
660 | objects[i].tagged = 0; |
661 | objects[i].filled = 0; | |
662 | objects[i].delta_child = NULL; | |
663 | objects[i].delta_sibling = NULL; | |
664 | } | |
665 | ||
666 | /* | |
667 | * Fully connect delta_child/delta_sibling network. | |
668 | * Make sure delta_sibling is sorted in the original | |
669 | * recency order. | |
670 | */ | |
2834bc27 | 671 | for (i = to_pack.nr_objects; i > 0;) { |
92bef1a1 | 672 | struct object_entry *e = &objects[--i]; |
1b4bb16b JH |
673 | if (!e->delta) |
674 | continue; | |
675 | /* Mark me as the first child */ | |
676 | e->delta_sibling = e->delta->delta_child; | |
677 | e->delta->delta_child = e; | |
678 | } | |
679 | ||
680 | /* | |
681 | * Mark objects that are at the tip of tags. | |
682 | */ | |
d155254c | 683 | for_each_tag_ref(mark_tagged, NULL); |
1b4bb16b JH |
684 | |
685 | /* | |
38d4debb | 686 | * Give the objects in the original recency order until |
1b4bb16b JH |
687 | * we see a tagged tip. |
688 | */ | |
b32fa95f | 689 | ALLOC_ARRAY(wo, to_pack.nr_objects); |
2834bc27 | 690 | for (i = wo_end = 0; i < to_pack.nr_objects; i++) { |
1b4bb16b JH |
691 | if (objects[i].tagged) |
692 | break; | |
693 | add_to_write_order(wo, &wo_end, &objects[i]); | |
694 | } | |
38d4debb | 695 | last_untagged = i; |
1b4bb16b JH |
696 | |
697 | /* | |
698 | * Then fill all the tagged tips. | |
699 | */ | |
2834bc27 | 700 | for (; i < to_pack.nr_objects; i++) { |
1b4bb16b JH |
701 | if (objects[i].tagged) |
702 | add_to_write_order(wo, &wo_end, &objects[i]); | |
703 | } | |
704 | ||
705 | /* | |
706 | * And then all remaining commits and tags. | |
707 | */ | |
2834bc27 | 708 | for (i = last_untagged; i < to_pack.nr_objects; i++) { |
fd9b1bae NTND |
709 | if (oe_type(&objects[i]) != OBJ_COMMIT && |
710 | oe_type(&objects[i]) != OBJ_TAG) | |
1b4bb16b JH |
711 | continue; |
712 | add_to_write_order(wo, &wo_end, &objects[i]); | |
713 | } | |
714 | ||
715 | /* | |
716 | * And then all the trees. | |
717 | */ | |
2834bc27 | 718 | for (i = last_untagged; i < to_pack.nr_objects; i++) { |
fd9b1bae | 719 | if (oe_type(&objects[i]) != OBJ_TREE) |
1b4bb16b JH |
720 | continue; |
721 | add_to_write_order(wo, &wo_end, &objects[i]); | |
722 | } | |
723 | ||
724 | /* | |
725 | * Finally all the rest in really tight order | |
726 | */ | |
2834bc27 | 727 | for (i = last_untagged; i < to_pack.nr_objects; i++) { |
38d4debb DM |
728 | if (!objects[i].filled) |
729 | add_family_to_write_order(wo, &wo_end, &objects[i]); | |
730 | } | |
731 | ||
2834bc27 VM |
732 | if (wo_end != to_pack.nr_objects) |
733 | die("ordered %u objects, expected %"PRIu32, wo_end, to_pack.nr_objects); | |
1b4bb16b JH |
734 | |
735 | return wo; | |
736 | } | |
737 | ||
98a3beab | 738 | static off_t write_reused_pack(struct hashfile *f) |
6b8fda2d VM |
739 | { |
740 | unsigned char buffer[8192]; | |
657673f1 | 741 | off_t to_write, total; |
6b8fda2d VM |
742 | int fd; |
743 | ||
744 | if (!is_pack_valid(reuse_packfile)) | |
745 | die("packfile is invalid: %s", reuse_packfile->pack_name); | |
746 | ||
a5436b57 | 747 | fd = git_open(reuse_packfile->pack_name); |
6b8fda2d VM |
748 | if (fd < 0) |
749 | die_errno("unable to open packfile for reuse: %s", | |
750 | reuse_packfile->pack_name); | |
751 | ||
752 | if (lseek(fd, sizeof(struct pack_header), SEEK_SET) == -1) | |
753 | die_errno("unable to seek in reused packfile"); | |
754 | ||
755 | if (reuse_packfile_offset < 0) | |
756 | reuse_packfile_offset = reuse_packfile->pack_size - 20; | |
757 | ||
657673f1 | 758 | total = to_write = reuse_packfile_offset - sizeof(struct pack_header); |
6b8fda2d VM |
759 | |
760 | while (to_write) { | |
761 | int read_pack = xread(fd, buffer, sizeof(buffer)); | |
762 | ||
763 | if (read_pack <= 0) | |
764 | die_errno("unable to read from reused packfile"); | |
765 | ||
766 | if (read_pack > to_write) | |
767 | read_pack = to_write; | |
768 | ||
98a3beab | 769 | hashwrite(f, buffer, read_pack); |
6b8fda2d | 770 | to_write -= read_pack; |
657673f1 JK |
771 | |
772 | /* | |
773 | * We don't know the actual number of objects written, | |
774 | * only how many bytes written, how many bytes total, and | |
775 | * how many objects total. So we can fake it by pretending all | |
776 | * objects we are writing are the same size. This gives us a | |
777 | * smooth progress meter, and at the end it matches the true | |
778 | * answer. | |
779 | */ | |
780 | written = reuse_packfile_objects * | |
781 | (((double)(total - to_write)) / total); | |
782 | display_progress(progress_state, written); | |
6b8fda2d VM |
783 | } |
784 | ||
785 | close(fd); | |
657673f1 JK |
786 | written = reuse_packfile_objects; |
787 | display_progress(progress_state, written); | |
6b8fda2d VM |
788 | return reuse_packfile_offset - sizeof(struct pack_header); |
789 | } | |
790 | ||
9cea46cd EW |
791 | static const char no_split_warning[] = N_( |
792 | "disabling bitmap writing, packs are split due to pack.packSizeLimit" | |
793 | ); | |
794 | ||
d01fb92f | 795 | static void write_pack_file(void) |
c323ac7d | 796 | { |
ebe27b13 | 797 | uint32_t i = 0, j; |
98a3beab | 798 | struct hashfile *f; |
6ed7f25e | 799 | off_t offset; |
ebe27b13 | 800 | uint32_t nr_remaining = nr_result; |
f746bae8 | 801 | time_t last_mtime = 0; |
1b4bb16b | 802 | struct object_entry **write_order; |
81a216a5 | 803 | |
bcd7954e | 804 | if (progress > pack_to_stdout) |
754dbc43 | 805 | progress_state = start_progress(_("Writing objects"), nr_result); |
b32fa95f | 806 | ALLOC_ARRAY(written_list, to_pack.nr_objects); |
1b4bb16b | 807 | write_order = compute_write_order(); |
183bdb2c | 808 | |
ebe27b13 | 809 | do { |
188960b4 | 810 | struct object_id oid; |
7ba502c4 | 811 | char *pack_tmp_name = NULL; |
aa7e44bf | 812 | |
cdf9db3c | 813 | if (pack_to_stdout) |
98a3beab | 814 | f = hashfd_throughput(1, "<stdout>", progress_state); |
cdf9db3c JH |
815 | else |
816 | f = create_tmp_packfile(&pack_tmp_name); | |
ebe27b13 | 817 | |
c0ad4657 | 818 | offset = write_pack_header(f, nr_remaining); |
6b8fda2d VM |
819 | |
820 | if (reuse_packfile) { | |
821 | off_t packfile_size; | |
822 | assert(pack_to_stdout); | |
823 | ||
824 | packfile_size = write_reused_pack(f); | |
825 | offset += packfile_size; | |
826 | } | |
827 | ||
ebe27b13 | 828 | nr_written = 0; |
2834bc27 | 829 | for (; i < to_pack.nr_objects; i++) { |
1b4bb16b | 830 | struct object_entry *e = write_order[i]; |
cddec4f8 | 831 | if (write_one(f, e, &offset) == WRITE_ONE_BREAK) |
720c9f7b NP |
832 | break; |
833 | display_progress(progress_state, written); | |
834 | } | |
c553ca25 | 835 | |
ebe27b13 DH |
836 | /* |
837 | * Did we write the wrong # entries in the header? | |
838 | * If so, rewrite it like in fast-import | |
839 | */ | |
54352bb2 | 840 | if (pack_to_stdout) { |
98a3beab | 841 | hashclose(f, oid.hash, CSUM_CLOSE); |
54352bb2 | 842 | } else if (nr_written == nr_remaining) { |
98a3beab | 843 | hashclose(f, oid.hash, CSUM_FSYNC); |
ebe27b13 | 844 | } else { |
98a3beab | 845 | int fd = hashclose(f, oid.hash, 0); |
188960b4 | 846 | fixup_pack_header_footer(fd, oid.hash, pack_tmp_name, |
847 | nr_written, oid.hash, offset); | |
7ba502c4 | 848 | close(fd); |
9cea46cd EW |
849 | if (write_bitmap_index) { |
850 | warning(_(no_split_warning)); | |
851 | write_bitmap_index = 0; | |
852 | } | |
ebe27b13 DH |
853 | } |
854 | ||
855 | if (!pack_to_stdout) { | |
f746bae8 | 856 | struct stat st; |
58892711 | 857 | struct strbuf tmpname = STRBUF_INIT; |
d01fb92f | 858 | |
f746bae8 NP |
859 | /* |
860 | * Packs are runtime accessed in their mtime | |
861 | * order since newer packs are more likely to contain | |
862 | * younger objects. So if we are creating multiple | |
863 | * packs then we should modify the mtime of later ones | |
864 | * to preserve this property. | |
865 | */ | |
0e990530 | 866 | if (stat(pack_tmp_name, &st) < 0) { |
54d47394 | 867 | warning_errno("failed to stat %s", pack_tmp_name); |
f746bae8 NP |
868 | } else if (!last_mtime) { |
869 | last_mtime = st.st_mtime; | |
870 | } else { | |
871 | struct utimbuf utb; | |
872 | utb.actime = st.st_atime; | |
873 | utb.modtime = --last_mtime; | |
0e990530 | 874 | if (utime(pack_tmp_name, &utb) < 0) |
54d47394 | 875 | warning_errno("failed utime() on %s", pack_tmp_name); |
f746bae8 NP |
876 | } |
877 | ||
58892711 | 878 | strbuf_addf(&tmpname, "%s-", base_name); |
7cc8f971 VM |
879 | |
880 | if (write_bitmap_index) { | |
188960b4 | 881 | bitmap_writer_set_checksum(oid.hash); |
06af3bba NTND |
882 | bitmap_writer_build_type_index( |
883 | &to_pack, written_list, nr_written); | |
7cc8f971 VM |
884 | } |
885 | ||
58892711 | 886 | finish_tmp_packfile(&tmpname, pack_tmp_name, |
0e990530 | 887 | written_list, nr_written, |
188960b4 | 888 | &pack_idx_opts, oid.hash); |
7cc8f971 VM |
889 | |
890 | if (write_bitmap_index) { | |
188960b4 | 891 | strbuf_addf(&tmpname, "%s.bitmap", oid_to_hex(&oid)); |
7cc8f971 VM |
892 | |
893 | stop_progress(&progress_state); | |
894 | ||
895 | bitmap_writer_show_progress(progress); | |
896 | bitmap_writer_reuse_bitmaps(&to_pack); | |
897 | bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1); | |
898 | bitmap_writer_build(&to_pack); | |
ae4f07fb | 899 | bitmap_writer_finish(written_list, nr_written, |
58892711 | 900 | tmpname.buf, write_bitmap_options); |
7cc8f971 VM |
901 | write_bitmap_index = 0; |
902 | } | |
903 | ||
58892711 | 904 | strbuf_release(&tmpname); |
7ba502c4 | 905 | free(pack_tmp_name); |
188960b4 | 906 | puts(oid_to_hex(&oid)); |
ebe27b13 DH |
907 | } |
908 | ||
909 | /* mark written objects as written to previous pack */ | |
910 | for (j = 0; j < nr_written; j++) { | |
79814f42 | 911 | written_list[j]->offset = (off_t)-1; |
ebe27b13 DH |
912 | } |
913 | nr_remaining -= nr_written; | |
2834bc27 | 914 | } while (nr_remaining && i < to_pack.nr_objects); |
ebe27b13 DH |
915 | |
916 | free(written_list); | |
1b4bb16b | 917 | free(write_order); |
4d4fcc54 | 918 | stop_progress(&progress_state); |
67c08ce1 | 919 | if (written != nr_result) |
6e1c2344 RJ |
920 | die("wrote %"PRIu32" objects while expecting %"PRIu32, |
921 | written, nr_result); | |
c323ac7d LT |
922 | } |
923 | ||
a74db82e JH |
924 | static int no_try_delta(const char *path) |
925 | { | |
2aef63d3 | 926 | static struct attr_check *check; |
a74db82e | 927 | |
2aef63d3 JH |
928 | if (!check) |
929 | check = attr_check_initl("delta", NULL); | |
930 | if (git_check_attr(path, check)) | |
a74db82e | 931 | return 0; |
2aef63d3 | 932 | if (ATTR_FALSE(check->items[0].value)) |
a74db82e JH |
933 | return 1; |
934 | return 0; | |
935 | } | |
936 | ||
ce2bc424 JK |
937 | /* |
938 | * When adding an object, check whether we have already added it | |
939 | * to our packing list. If so, we can skip. However, if we are | |
940 | * being asked to excludei t, but the previous mention was to include | |
941 | * it, make sure to adjust its flags and tweak our numbers accordingly. | |
942 | * | |
943 | * As an optimization, we pass out the index position where we would have | |
944 | * found the item, since that saves us from having to look it up again a | |
945 | * few lines later when we want to add the new entry. | |
946 | */ | |
188960b4 | 947 | static int have_duplicate_entry(const struct object_id *oid, |
ce2bc424 JK |
948 | int exclude, |
949 | uint32_t *index_pos) | |
c323ac7d | 950 | { |
c323ac7d | 951 | struct object_entry *entry; |
29b734e4 | 952 | |
188960b4 | 953 | entry = packlist_find(&to_pack, oid->hash, index_pos); |
ce2bc424 | 954 | if (!entry) |
29b734e4 | 955 | return 0; |
ce2bc424 JK |
956 | |
957 | if (exclude) { | |
958 | if (!entry->preferred_base) | |
959 | nr_result--; | |
960 | entry->preferred_base = 1; | |
29b734e4 | 961 | } |
c323ac7d | 962 | |
ce2bc424 JK |
963 | return 1; |
964 | } | |
965 | ||
702d1b95 KS |
966 | static int want_found_object(int exclude, struct packed_git *p) |
967 | { | |
968 | if (exclude) | |
969 | return 1; | |
970 | if (incremental) | |
971 | return 0; | |
972 | ||
973 | /* | |
974 | * When asked to do --local (do not include an object that appears in a | |
975 | * pack we borrow from elsewhere) or --honor-pack-keep (do not include | |
976 | * an object that appears in a pack marked with .keep), finding a pack | |
977 | * that matches the criteria is sufficient for us to decide to omit it. | |
978 | * However, even if this pack does not satisfy the criteria, we need to | |
979 | * make sure no copy of this object appears in _any_ pack that makes us | |
980 | * to omit the object, so we need to check all the packs. | |
981 | * | |
982 | * We can however first check whether these options can possible matter; | |
983 | * if they do not matter we know we want the object in generated pack. | |
984 | * Otherwise, we signal "-1" at the end to tell the caller that we do | |
985 | * not know either way, and it needs to check more packs. | |
986 | */ | |
987 | if (!ignore_packed_keep && | |
988 | (!local || !have_non_local_packs)) | |
989 | return 1; | |
990 | ||
991 | if (local && !p->pack_local) | |
992 | return 0; | |
993 | if (ignore_packed_keep && p->pack_local && p->pack_keep) | |
994 | return 0; | |
995 | ||
996 | /* we don't know yet; keep looking for more packs */ | |
997 | return -1; | |
998 | } | |
999 | ||
ce2bc424 JK |
1000 | /* |
1001 | * Check whether we want the object in the pack (e.g., we do not want | |
1002 | * objects found in non-local stores if the "--local" option was used). | |
1003 | * | |
702d1b95 KS |
1004 | * If the caller already knows an existing pack it wants to take the object |
1005 | * from, that is passed in *found_pack and *found_offset; otherwise this | |
1006 | * function finds if there is any pack that has the object and returns the pack | |
1007 | * and its offset in these variables. | |
ce2bc424 | 1008 | */ |
188960b4 | 1009 | static int want_object_in_pack(const struct object_id *oid, |
ce2bc424 JK |
1010 | int exclude, |
1011 | struct packed_git **found_pack, | |
1012 | off_t *found_offset) | |
1013 | { | |
702d1b95 | 1014 | int want; |
8865859d | 1015 | struct list_head *pos; |
ce2bc424 | 1016 | |
188960b4 | 1017 | if (!exclude && local && has_loose_object_nonlocal(oid->hash)) |
daae0625 BC |
1018 | return 0; |
1019 | ||
702d1b95 KS |
1020 | /* |
1021 | * If we already know the pack object lives in, start checks from that | |
1022 | * pack - in the usual case when neither --local was given nor .keep files | |
1023 | * are present we will determine the answer right now. | |
1024 | */ | |
1025 | if (*found_pack) { | |
1026 | want = want_found_object(exclude, *found_pack); | |
1027 | if (want != -1) | |
1028 | return want; | |
1029 | } | |
a80d72db | 1030 | list_for_each(pos, get_packed_git_mru(the_repository)) { |
ec2dd32c | 1031 | struct packed_git *p = list_entry(pos, struct packed_git, mru); |
702d1b95 KS |
1032 | off_t offset; |
1033 | ||
1034 | if (p == *found_pack) | |
1035 | offset = *found_offset; | |
1036 | else | |
188960b4 | 1037 | offset = find_pack_entry_one(oid->hash, p); |
702d1b95 | 1038 | |
5c49c116 | 1039 | if (offset) { |
ce2bc424 | 1040 | if (!*found_pack) { |
319b678a | 1041 | if (!is_pack_valid(p)) |
4c080182 | 1042 | continue; |
ce2bc424 JK |
1043 | *found_offset = offset; |
1044 | *found_pack = p; | |
64560374 | 1045 | } |
702d1b95 | 1046 | want = want_found_object(exclude, p); |
e6e24c94 | 1047 | if (!exclude && want > 0) |
a80d72db SB |
1048 | list_move(&p->mru, |
1049 | get_packed_git_mru(the_repository)); | |
702d1b95 KS |
1050 | if (want != -1) |
1051 | return want; | |
64560374 LT |
1052 | } |
1053 | } | |
eb019375 | 1054 | |
ce2bc424 JK |
1055 | return 1; |
1056 | } | |
1057 | ||
188960b4 | 1058 | static void create_object_entry(const struct object_id *oid, |
ce2bc424 JK |
1059 | enum object_type type, |
1060 | uint32_t hash, | |
1061 | int exclude, | |
1062 | int no_try_delta, | |
1063 | uint32_t index_pos, | |
1064 | struct packed_git *found_pack, | |
1065 | off_t found_offset) | |
1066 | { | |
1067 | struct object_entry *entry; | |
29b734e4 | 1068 | |
188960b4 | 1069 | entry = packlist_alloc(&to_pack, oid->hash, index_pos); |
27225f2e | 1070 | entry->hash = hash; |
fd9b1bae | 1071 | oe_set_type(entry, type); |
29b734e4 NP |
1072 | if (exclude) |
1073 | entry->preferred_base = 1; | |
81a216a5 NP |
1074 | else |
1075 | nr_result++; | |
29b734e4 NP |
1076 | if (found_pack) { |
1077 | entry->in_pack = found_pack; | |
1078 | entry->in_pack_offset = found_offset; | |
1079 | } | |
7a979d99 | 1080 | |
ce2bc424 JK |
1081 | entry->no_try_delta = no_try_delta; |
1082 | } | |
29b734e4 | 1083 | |
373c67da JK |
1084 | static const char no_closure_warning[] = N_( |
1085 | "disabling bitmap writing, as some objects are not being packed" | |
1086 | ); | |
1087 | ||
188960b4 | 1088 | static int add_object_entry(const struct object_id *oid, enum object_type type, |
ce2bc424 JK |
1089 | const char *name, int exclude) |
1090 | { | |
702d1b95 KS |
1091 | struct packed_git *found_pack = NULL; |
1092 | off_t found_offset = 0; | |
ce2bc424 | 1093 | uint32_t index_pos; |
7a979d99 | 1094 | |
188960b4 | 1095 | if (have_duplicate_entry(oid, exclude, &index_pos)) |
ce2bc424 | 1096 | return 0; |
a74db82e | 1097 | |
188960b4 | 1098 | if (!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) { |
373c67da JK |
1099 | /* The pack is missing an object, so it will not have closure */ |
1100 | if (write_bitmap_index) { | |
1101 | warning(_(no_closure_warning)); | |
1102 | write_bitmap_index = 0; | |
1103 | } | |
ce2bc424 | 1104 | return 0; |
373c67da | 1105 | } |
29b734e4 | 1106 | |
188960b4 | 1107 | create_object_entry(oid, type, pack_name_hash(name), |
ce2bc424 JK |
1108 | exclude, name && no_try_delta(name), |
1109 | index_pos, found_pack, found_offset); | |
a74db82e | 1110 | |
78d2214e | 1111 | display_progress(progress_state, nr_result); |
29b734e4 | 1112 | return 1; |
c323ac7d LT |
1113 | } |
1114 | ||
20664967 | 1115 | static int add_object_entry_from_bitmap(const struct object_id *oid, |
6b8fda2d VM |
1116 | enum object_type type, |
1117 | int flags, uint32_t name_hash, | |
1118 | struct packed_git *pack, off_t offset) | |
1119 | { | |
1120 | uint32_t index_pos; | |
1121 | ||
188960b4 | 1122 | if (have_duplicate_entry(oid, 0, &index_pos)) |
6b8fda2d VM |
1123 | return 0; |
1124 | ||
188960b4 | 1125 | if (!want_object_in_pack(oid, 0, &pack, &offset)) |
702d1b95 KS |
1126 | return 0; |
1127 | ||
188960b4 | 1128 | create_object_entry(oid, type, name_hash, 0, 0, index_pos, pack, offset); |
6b8fda2d | 1129 | |
78d2214e | 1130 | display_progress(progress_state, nr_result); |
29b734e4 | 1131 | return 1; |
c323ac7d LT |
1132 | } |
1133 | ||
5379a5c5 | 1134 | struct pbase_tree_cache { |
188960b4 | 1135 | struct object_id oid; |
5379a5c5 JH |
1136 | int ref; |
1137 | int temporary; | |
1138 | void *tree_data; | |
1139 | unsigned long tree_size; | |
1140 | }; | |
1141 | ||
1142 | static struct pbase_tree_cache *(pbase_tree_cache[256]); | |
188960b4 | 1143 | static int pbase_tree_cache_ix(const struct object_id *oid) |
5379a5c5 | 1144 | { |
188960b4 | 1145 | return oid->hash[0] % ARRAY_SIZE(pbase_tree_cache); |
5379a5c5 JH |
1146 | } |
1147 | static int pbase_tree_cache_ix_incr(int ix) | |
1148 | { | |
1149 | return (ix+1) % ARRAY_SIZE(pbase_tree_cache); | |
1150 | } | |
1151 | ||
1152 | static struct pbase_tree { | |
1153 | struct pbase_tree *next; | |
1154 | /* This is a phony "cache" entry; we are not | |
01689909 | 1155 | * going to evict it or find it through _get() |
5379a5c5 JH |
1156 | * mechanism -- this is for the toplevel node that |
1157 | * would almost always change with any commit. | |
1158 | */ | |
1159 | struct pbase_tree_cache pcache; | |
1160 | } *pbase_tree; | |
1161 | ||
188960b4 | 1162 | static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid) |
5379a5c5 JH |
1163 | { |
1164 | struct pbase_tree_cache *ent, *nent; | |
1165 | void *data; | |
1166 | unsigned long size; | |
21666f1a | 1167 | enum object_type type; |
5379a5c5 | 1168 | int neigh; |
188960b4 | 1169 | int my_ix = pbase_tree_cache_ix(oid); |
5379a5c5 JH |
1170 | int available_ix = -1; |
1171 | ||
1172 | /* pbase-tree-cache acts as a limited hashtable. | |
1173 | * your object will be found at your index or within a few | |
1174 | * slots after that slot if it is cached. | |
1175 | */ | |
1176 | for (neigh = 0; neigh < 8; neigh++) { | |
1177 | ent = pbase_tree_cache[my_ix]; | |
188960b4 | 1178 | if (ent && !oidcmp(&ent->oid, oid)) { |
5379a5c5 JH |
1179 | ent->ref++; |
1180 | return ent; | |
1181 | } | |
1182 | else if (((available_ix < 0) && (!ent || !ent->ref)) || | |
1183 | ((0 <= available_ix) && | |
1184 | (!ent && pbase_tree_cache[available_ix]))) | |
1185 | available_ix = my_ix; | |
1186 | if (!ent) | |
1187 | break; | |
1188 | my_ix = pbase_tree_cache_ix_incr(my_ix); | |
1189 | } | |
1190 | ||
1191 | /* Did not find one. Either we got a bogus request or | |
1192 | * we need to read and perhaps cache. | |
1193 | */ | |
b4f5aca4 | 1194 | data = read_object_file(oid, &type, &size); |
5379a5c5 JH |
1195 | if (!data) |
1196 | return NULL; | |
21666f1a | 1197 | if (type != OBJ_TREE) { |
5379a5c5 JH |
1198 | free(data); |
1199 | return NULL; | |
1200 | } | |
1201 | ||
1202 | /* We need to either cache or return a throwaway copy */ | |
1203 | ||
1204 | if (available_ix < 0) | |
1205 | ent = NULL; | |
1206 | else { | |
1207 | ent = pbase_tree_cache[available_ix]; | |
1208 | my_ix = available_ix; | |
1209 | } | |
1210 | ||
1211 | if (!ent) { | |
1212 | nent = xmalloc(sizeof(*nent)); | |
1213 | nent->temporary = (available_ix < 0); | |
1214 | } | |
1215 | else { | |
1216 | /* evict and reuse */ | |
1217 | free(ent->tree_data); | |
1218 | nent = ent; | |
1219 | } | |
188960b4 | 1220 | oidcpy(&nent->oid, oid); |
5379a5c5 JH |
1221 | nent->tree_data = data; |
1222 | nent->tree_size = size; | |
1223 | nent->ref = 1; | |
1224 | if (!nent->temporary) | |
1225 | pbase_tree_cache[my_ix] = nent; | |
1226 | return nent; | |
1227 | } | |
1228 | ||
1229 | static void pbase_tree_put(struct pbase_tree_cache *cache) | |
1230 | { | |
1231 | if (!cache->temporary) { | |
1232 | cache->ref--; | |
1233 | return; | |
1234 | } | |
1235 | free(cache->tree_data); | |
1236 | free(cache); | |
1237 | } | |
1238 | ||
1239 | static int name_cmp_len(const char *name) | |
1240 | { | |
1241 | int i; | |
1242 | for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++) | |
1243 | ; | |
1244 | return i; | |
1245 | } | |
1246 | ||
1247 | static void add_pbase_object(struct tree_desc *tree, | |
5379a5c5 | 1248 | const char *name, |
ce0bd642 LT |
1249 | int cmplen, |
1250 | const char *fullname) | |
3f9ac8d2 | 1251 | { |
4c068a98 | 1252 | struct name_entry entry; |
8a5a8d6c | 1253 | int cmp; |
4c068a98 LT |
1254 | |
1255 | while (tree_entry(tree,&entry)) { | |
1211be6b LT |
1256 | if (S_ISGITLINK(entry.mode)) |
1257 | continue; | |
0de16337 | 1258 | cmp = tree_entry_len(&entry) != cmplen ? 1 : |
8a5a8d6c NP |
1259 | memcmp(name, entry.path, cmplen); |
1260 | if (cmp > 0) | |
7a979d99 | 1261 | continue; |
8a5a8d6c NP |
1262 | if (cmp < 0) |
1263 | return; | |
5379a5c5 | 1264 | if (name[cmplen] != '/') { |
188960b4 | 1265 | add_object_entry(entry.oid, |
4d1012c3 | 1266 | object_type(entry.mode), |
bc32fed5 | 1267 | fullname, 1); |
5379a5c5 JH |
1268 | return; |
1269 | } | |
8a5a8d6c | 1270 | if (S_ISDIR(entry.mode)) { |
7a979d99 | 1271 | struct tree_desc sub; |
5379a5c5 JH |
1272 | struct pbase_tree_cache *tree; |
1273 | const char *down = name+cmplen+1; | |
1274 | int downlen = name_cmp_len(down); | |
1275 | ||
188960b4 | 1276 | tree = pbase_tree_get(entry.oid); |
5379a5c5 JH |
1277 | if (!tree) |
1278 | return; | |
6fda5e51 | 1279 | init_tree_desc(&sub, tree->tree_data, tree->tree_size); |
5379a5c5 | 1280 | |
ce0bd642 | 1281 | add_pbase_object(&sub, down, downlen, fullname); |
5379a5c5 JH |
1282 | pbase_tree_put(tree); |
1283 | } | |
1284 | } | |
1285 | } | |
1d6b38cc | 1286 | |
5379a5c5 JH |
1287 | static unsigned *done_pbase_paths; |
1288 | static int done_pbase_paths_num; | |
1289 | static int done_pbase_paths_alloc; | |
1290 | static int done_pbase_path_pos(unsigned hash) | |
1291 | { | |
1292 | int lo = 0; | |
1293 | int hi = done_pbase_paths_num; | |
1294 | while (lo < hi) { | |
19716b21 | 1295 | int mi = lo + (hi - lo) / 2; |
5379a5c5 JH |
1296 | if (done_pbase_paths[mi] == hash) |
1297 | return mi; | |
1298 | if (done_pbase_paths[mi] < hash) | |
1299 | hi = mi; | |
1300 | else | |
1301 | lo = mi + 1; | |
1302 | } | |
1303 | return -lo-1; | |
1304 | } | |
1305 | ||
1306 | static int check_pbase_path(unsigned hash) | |
1307 | { | |
c7b07805 | 1308 | int pos = done_pbase_path_pos(hash); |
5379a5c5 JH |
1309 | if (0 <= pos) |
1310 | return 1; | |
1311 | pos = -pos - 1; | |
25e19407 DD |
1312 | ALLOC_GROW(done_pbase_paths, |
1313 | done_pbase_paths_num + 1, | |
1314 | done_pbase_paths_alloc); | |
5379a5c5 JH |
1315 | done_pbase_paths_num++; |
1316 | if (pos < done_pbase_paths_num) | |
f331ab9d RS |
1317 | MOVE_ARRAY(done_pbase_paths + pos + 1, done_pbase_paths + pos, |
1318 | done_pbase_paths_num - pos - 1); | |
5379a5c5 JH |
1319 | done_pbase_paths[pos] = hash; |
1320 | return 0; | |
1321 | } | |
1322 | ||
bc32fed5 | 1323 | static void add_preferred_base_object(const char *name) |
5379a5c5 JH |
1324 | { |
1325 | struct pbase_tree *it; | |
8a5a8d6c | 1326 | int cmplen; |
68fb36eb | 1327 | unsigned hash = pack_name_hash(name); |
5379a5c5 | 1328 | |
8a5a8d6c | 1329 | if (!num_preferred_base || check_pbase_path(hash)) |
5379a5c5 JH |
1330 | return; |
1331 | ||
8a5a8d6c | 1332 | cmplen = name_cmp_len(name); |
5379a5c5 JH |
1333 | for (it = pbase_tree; it; it = it->next) { |
1334 | if (cmplen == 0) { | |
188960b4 | 1335 | add_object_entry(&it->pcache.oid, OBJ_TREE, NULL, 1); |
5379a5c5 JH |
1336 | } |
1337 | else { | |
1338 | struct tree_desc tree; | |
6fda5e51 | 1339 | init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size); |
ce0bd642 | 1340 | add_pbase_object(&tree, name, cmplen, name); |
7a979d99 | 1341 | } |
3f9ac8d2 | 1342 | } |
3f9ac8d2 JH |
1343 | } |
1344 | ||
188960b4 | 1345 | static void add_preferred_base(struct object_id *oid) |
3f9ac8d2 | 1346 | { |
5379a5c5 JH |
1347 | struct pbase_tree *it; |
1348 | void *data; | |
1349 | unsigned long size; | |
188960b4 | 1350 | struct object_id tree_oid; |
1d6b38cc | 1351 | |
8d1d8f83 JH |
1352 | if (window <= num_preferred_base++) |
1353 | return; | |
1354 | ||
02f0547e | 1355 | data = read_object_with_reference(oid, tree_type, &size, &tree_oid); |
5379a5c5 | 1356 | if (!data) |
7a979d99 | 1357 | return; |
5379a5c5 JH |
1358 | |
1359 | for (it = pbase_tree; it; it = it->next) { | |
188960b4 | 1360 | if (!oidcmp(&it->pcache.oid, &tree_oid)) { |
5379a5c5 JH |
1361 | free(data); |
1362 | return; | |
1363 | } | |
1364 | } | |
1365 | ||
1366 | it = xcalloc(1, sizeof(*it)); | |
1367 | it->next = pbase_tree; | |
1368 | pbase_tree = it; | |
1369 | ||
188960b4 | 1370 | oidcpy(&it->pcache.oid, &tree_oid); |
5379a5c5 JH |
1371 | it->pcache.tree_data = data; |
1372 | it->pcache.tree_size = size; | |
3f9ac8d2 JH |
1373 | } |
1374 | ||
0ef95f72 NP |
1375 | static void cleanup_preferred_base(void) |
1376 | { | |
1377 | struct pbase_tree *it; | |
1378 | unsigned i; | |
1379 | ||
1380 | it = pbase_tree; | |
1381 | pbase_tree = NULL; | |
1382 | while (it) { | |
095b3b2c BW |
1383 | struct pbase_tree *tmp = it; |
1384 | it = tmp->next; | |
1385 | free(tmp->pcache.tree_data); | |
1386 | free(tmp); | |
0ef95f72 NP |
1387 | } |
1388 | ||
1389 | for (i = 0; i < ARRAY_SIZE(pbase_tree_cache); i++) { | |
1390 | if (!pbase_tree_cache[i]) | |
1391 | continue; | |
1392 | free(pbase_tree_cache[i]->tree_data); | |
6a83d902 | 1393 | FREE_AND_NULL(pbase_tree_cache[i]); |
0ef95f72 NP |
1394 | } |
1395 | ||
6a83d902 | 1396 | FREE_AND_NULL(done_pbase_paths); |
0ef95f72 NP |
1397 | done_pbase_paths_num = done_pbase_paths_alloc = 0; |
1398 | } | |
1399 | ||
c323ac7d LT |
1400 | static void check_object(struct object_entry *entry) |
1401 | { | |
5c49c116 | 1402 | if (entry->in_pack) { |
780e6e73 | 1403 | struct packed_git *p = entry->in_pack; |
03e79c88 | 1404 | struct pack_window *w_curs = NULL; |
5c49c116 NP |
1405 | const unsigned char *base_ref = NULL; |
1406 | struct object_entry *base_entry; | |
1407 | unsigned long used, used_0; | |
ef49a7a0 | 1408 | unsigned long avail; |
5c49c116 NP |
1409 | off_t ofs; |
1410 | unsigned char *buf, c; | |
fd9b1bae | 1411 | enum object_type type; |
780e6e73 | 1412 | |
6777a59f | 1413 | buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail); |
ab7cd7bb | 1414 | |
5c49c116 | 1415 | /* |
fa736f72 NP |
1416 | * We want in_pack_type even if we do not reuse delta |
1417 | * since non-delta representations could still be reused. | |
ab7cd7bb | 1418 | */ |
09ded04b | 1419 | used = unpack_object_header_buffer(buf, avail, |
fd9b1bae | 1420 | &type, |
5c49c116 | 1421 | &entry->size); |
03d66015 NP |
1422 | if (used == 0) |
1423 | goto give_up; | |
ab7cd7bb | 1424 | |
fd9b1bae NTND |
1425 | if (type < 0) |
1426 | BUG("invalid type %d", type); | |
1427 | entry->in_pack_type = type; | |
1428 | ||
5c49c116 NP |
1429 | /* |
1430 | * Determine if this is a delta and if so whether we can | |
1431 | * reuse it or not. Otherwise let's find out as cheaply as | |
1432 | * possible what the actual type and size for this object is. | |
3f9ac8d2 | 1433 | */ |
5c49c116 NP |
1434 | switch (entry->in_pack_type) { |
1435 | default: | |
1436 | /* Not a delta hence we've already got all we need. */ | |
fd9b1bae | 1437 | oe_set_type(entry, entry->in_pack_type); |
5c49c116 | 1438 | entry->in_pack_header_size = used; |
fd9b1bae | 1439 | if (oe_type(entry) < OBJ_COMMIT || oe_type(entry) > OBJ_BLOB) |
03d66015 | 1440 | goto give_up; |
5c49c116 NP |
1441 | unuse_pack(&w_curs); |
1442 | return; | |
1443 | case OBJ_REF_DELTA: | |
a7de7130 | 1444 | if (reuse_delta && !entry->preferred_base) |
5c49c116 NP |
1445 | base_ref = use_pack(p, &w_curs, |
1446 | entry->in_pack_offset + used, NULL); | |
1447 | entry->in_pack_header_size = used + 20; | |
1448 | break; | |
1449 | case OBJ_OFS_DELTA: | |
1450 | buf = use_pack(p, &w_curs, | |
1451 | entry->in_pack_offset + used, NULL); | |
1452 | used_0 = 0; | |
1453 | c = buf[used_0++]; | |
1454 | ofs = c & 127; | |
1455 | while (c & 128) { | |
1456 | ofs += 1; | |
03d66015 NP |
1457 | if (!ofs || MSB(ofs, 7)) { |
1458 | error("delta base offset overflow in pack for %s", | |
e6a492b7 | 1459 | oid_to_hex(&entry->idx.oid)); |
03d66015 NP |
1460 | goto give_up; |
1461 | } | |
5c49c116 NP |
1462 | c = buf[used_0++]; |
1463 | ofs = (ofs << 7) + (c & 127); | |
780e6e73 | 1464 | } |
5c49c116 | 1465 | ofs = entry->in_pack_offset - ofs; |
03d66015 NP |
1466 | if (ofs <= 0 || ofs >= entry->in_pack_offset) { |
1467 | error("delta base offset out of bound for %s", | |
e6a492b7 | 1468 | oid_to_hex(&entry->idx.oid)); |
03d66015 NP |
1469 | goto give_up; |
1470 | } | |
a7de7130 | 1471 | if (reuse_delta && !entry->preferred_base) { |
3449f8c4 NP |
1472 | struct revindex_entry *revidx; |
1473 | revidx = find_pack_revindex(p, ofs); | |
08698b1e NP |
1474 | if (!revidx) |
1475 | goto give_up; | |
3449f8c4 NP |
1476 | base_ref = nth_packed_object_sha1(p, revidx->nr); |
1477 | } | |
5c49c116 NP |
1478 | entry->in_pack_header_size = used + used_0; |
1479 | break; | |
780e6e73 | 1480 | } |
780e6e73 | 1481 | |
2834bc27 | 1482 | if (base_ref && (base_entry = packlist_find(&to_pack, base_ref, NULL))) { |
5c49c116 NP |
1483 | /* |
1484 | * If base_ref was set above that means we wish to | |
1485 | * reuse delta data, and we even found that base | |
1486 | * in the list of objects we want to pack. Goodie! | |
1487 | * | |
1488 | * Depth value does not matter - find_deltas() will | |
1489 | * never consider reused delta as the base object to | |
1490 | * deltify other objects against, in order to avoid | |
1491 | * circular deltas. | |
3f9ac8d2 | 1492 | */ |
fd9b1bae | 1493 | oe_set_type(entry, entry->in_pack_type); |
5c49c116 | 1494 | entry->delta = base_entry; |
64bd76b1 | 1495 | entry->delta_size = entry->size; |
15b4d577 JH |
1496 | entry->delta_sibling = base_entry->delta_child; |
1497 | base_entry->delta_child = entry; | |
5c49c116 NP |
1498 | unuse_pack(&w_curs); |
1499 | return; | |
1500 | } | |
ab7cd7bb | 1501 | |
fd9b1bae | 1502 | if (oe_type(entry)) { |
5c49c116 NP |
1503 | /* |
1504 | * This must be a delta and we already know what the | |
1505 | * final object type is. Let's extract the actual | |
1506 | * object size from the delta header. | |
1507 | */ | |
1508 | entry->size = get_size_from_delta(p, &w_curs, | |
1509 | entry->in_pack_offset + entry->in_pack_header_size); | |
03d66015 NP |
1510 | if (entry->size == 0) |
1511 | goto give_up; | |
5c49c116 | 1512 | unuse_pack(&w_curs); |
3f9ac8d2 JH |
1513 | return; |
1514 | } | |
5c49c116 NP |
1515 | |
1516 | /* | |
1517 | * No choice but to fall back to the recursive delta walk | |
1518 | * with sha1_object_info() to find about the object type | |
1519 | * at this point... | |
1520 | */ | |
03d66015 | 1521 | give_up: |
5c49c116 | 1522 | unuse_pack(&w_curs); |
36e4d74a | 1523 | } |
3f9ac8d2 | 1524 | |
fd9b1bae | 1525 | oe_set_type(entry, oid_object_info(&entry->idx.oid, &entry->size)); |
6d6f9cdd SP |
1526 | /* |
1527 | * The error condition is checked in prepare_pack(). This is | |
1528 | * to permit a missing preferred base object to be ignored | |
1529 | * as a preferred base. Doing so can result in a larger | |
1530 | * pack file, but the transfer will still take place. | |
1531 | */ | |
3f9ac8d2 JH |
1532 | } |
1533 | ||
5c49c116 NP |
1534 | static int pack_offset_sort(const void *_a, const void *_b) |
1535 | { | |
1536 | const struct object_entry *a = *(struct object_entry **)_a; | |
1537 | const struct object_entry *b = *(struct object_entry **)_b; | |
1538 | ||
1539 | /* avoid filesystem trashing with loose objects */ | |
1540 | if (!a->in_pack && !b->in_pack) | |
e6a492b7 | 1541 | return oidcmp(&a->idx.oid, &b->idx.oid); |
5c49c116 NP |
1542 | |
1543 | if (a->in_pack < b->in_pack) | |
1544 | return -1; | |
1545 | if (a->in_pack > b->in_pack) | |
1546 | return 1; | |
1547 | return a->in_pack_offset < b->in_pack_offset ? -1 : | |
1548 | (a->in_pack_offset > b->in_pack_offset); | |
1549 | } | |
1550 | ||
4cf2143e JK |
1551 | /* |
1552 | * Drop an on-disk delta we were planning to reuse. Naively, this would | |
1553 | * just involve blanking out the "delta" field, but we have to deal | |
1554 | * with some extra book-keeping: | |
1555 | * | |
1556 | * 1. Removing ourselves from the delta_sibling linked list. | |
1557 | * | |
1558 | * 2. Updating our size/type to the non-delta representation. These were | |
1559 | * either not recorded initially (size) or overwritten with the delta type | |
1560 | * (type) when check_object() decided to reuse the delta. | |
7dbabbbe JK |
1561 | * |
1562 | * 3. Resetting our delta depth, as we are now a base object. | |
4cf2143e JK |
1563 | */ |
1564 | static void drop_reused_delta(struct object_entry *entry) | |
1565 | { | |
1566 | struct object_entry **p = &entry->delta->delta_child; | |
1567 | struct object_info oi = OBJECT_INFO_INIT; | |
fd9b1bae | 1568 | enum object_type type; |
4cf2143e JK |
1569 | |
1570 | while (*p) { | |
1571 | if (*p == entry) | |
1572 | *p = (*p)->delta_sibling; | |
1573 | else | |
1574 | p = &(*p)->delta_sibling; | |
1575 | } | |
1576 | entry->delta = NULL; | |
7dbabbbe | 1577 | entry->depth = 0; |
4cf2143e JK |
1578 | |
1579 | oi.sizep = &entry->size; | |
fd9b1bae | 1580 | oi.typep = &type; |
4cf2143e JK |
1581 | if (packed_object_info(entry->in_pack, entry->in_pack_offset, &oi) < 0) { |
1582 | /* | |
1583 | * We failed to get the info from this pack for some reason; | |
1584 | * fall back to sha1_object_info, which may find another copy. | |
fd9b1bae | 1585 | * And if that fails, the error will be recorded in oe_type(entry) |
4cf2143e JK |
1586 | * and dealt with in prepare_pack(). |
1587 | */ | |
fd9b1bae NTND |
1588 | oe_set_type(entry, oid_object_info(&entry->idx.oid, |
1589 | &entry->size)); | |
1590 | } else { | |
1591 | oe_set_type(entry, type); | |
4cf2143e JK |
1592 | } |
1593 | } | |
1594 | ||
1595 | /* | |
1596 | * Follow the chain of deltas from this entry onward, throwing away any links | |
1597 | * that cause us to hit a cycle (as determined by the DFS state flags in | |
1598 | * the entries). | |
7dbabbbe JK |
1599 | * |
1600 | * We also detect too-long reused chains that would violate our --depth | |
1601 | * limit. | |
4cf2143e JK |
1602 | */ |
1603 | static void break_delta_chains(struct object_entry *entry) | |
1604 | { | |
42b766d7 JK |
1605 | /* |
1606 | * The actual depth of each object we will write is stored as an int, | |
1607 | * as it cannot exceed our int "depth" limit. But before we break | |
1608 | * changes based no that limit, we may potentially go as deep as the | |
1609 | * number of objects, which is elsewhere bounded to a uint32_t. | |
1610 | */ | |
1611 | uint32_t total_depth; | |
1612 | struct object_entry *cur, *next; | |
1613 | ||
1614 | for (cur = entry, total_depth = 0; | |
1615 | cur; | |
1616 | cur = cur->delta, total_depth++) { | |
1617 | if (cur->dfs_state == DFS_DONE) { | |
1618 | /* | |
1619 | * We've already seen this object and know it isn't | |
1620 | * part of a cycle. We do need to append its depth | |
1621 | * to our count. | |
1622 | */ | |
1623 | total_depth += cur->depth; | |
1624 | break; | |
1625 | } | |
4cf2143e | 1626 | |
4cf2143e | 1627 | /* |
42b766d7 JK |
1628 | * We break cycles before looping, so an ACTIVE state (or any |
1629 | * other cruft which made its way into the state variable) | |
1630 | * is a bug. | |
4cf2143e | 1631 | */ |
42b766d7 JK |
1632 | if (cur->dfs_state != DFS_NONE) |
1633 | die("BUG: confusing delta dfs state in first pass: %d", | |
1634 | cur->dfs_state); | |
4cf2143e | 1635 | |
4cf2143e | 1636 | /* |
42b766d7 JK |
1637 | * Now we know this is the first time we've seen the object. If |
1638 | * it's not a delta, we're done traversing, but we'll mark it | |
1639 | * done to save time on future traversals. | |
4cf2143e | 1640 | */ |
42b766d7 JK |
1641 | if (!cur->delta) { |
1642 | cur->dfs_state = DFS_DONE; | |
1643 | break; | |
1644 | } | |
4cf2143e | 1645 | |
4cf2143e | 1646 | /* |
42b766d7 JK |
1647 | * Mark ourselves as active and see if the next step causes |
1648 | * us to cycle to another active object. It's important to do | |
1649 | * this _before_ we loop, because it impacts where we make the | |
1650 | * cut, and thus how our total_depth counter works. | |
1651 | * E.g., We may see a partial loop like: | |
1652 | * | |
1653 | * A -> B -> C -> D -> B | |
1654 | * | |
1655 | * Cutting B->C breaks the cycle. But now the depth of A is | |
1656 | * only 1, and our total_depth counter is at 3. The size of the | |
1657 | * error is always one less than the size of the cycle we | |
1658 | * broke. Commits C and D were "lost" from A's chain. | |
1659 | * | |
1660 | * If we instead cut D->B, then the depth of A is correct at 3. | |
1661 | * We keep all commits in the chain that we examined. | |
4cf2143e | 1662 | */ |
42b766d7 JK |
1663 | cur->dfs_state = DFS_ACTIVE; |
1664 | if (cur->delta->dfs_state == DFS_ACTIVE) { | |
1665 | drop_reused_delta(cur); | |
1666 | cur->dfs_state = DFS_DONE; | |
1667 | break; | |
7dbabbbe | 1668 | } |
42b766d7 | 1669 | } |
7dbabbbe | 1670 | |
42b766d7 JK |
1671 | /* |
1672 | * And now that we've gone all the way to the bottom of the chain, we | |
1673 | * need to clear the active flags and set the depth fields as | |
1674 | * appropriate. Unlike the loop above, which can quit when it drops a | |
1675 | * delta, we need to keep going to look for more depth cuts. So we need | |
1676 | * an extra "next" pointer to keep going after we reset cur->delta. | |
1677 | */ | |
1678 | for (cur = entry; cur; cur = next) { | |
1679 | next = cur->delta; | |
4cf2143e | 1680 | |
42b766d7 JK |
1681 | /* |
1682 | * We should have a chain of zero or more ACTIVE states down to | |
1683 | * a final DONE. We can quit after the DONE, because either it | |
1684 | * has no bases, or we've already handled them in a previous | |
1685 | * call. | |
1686 | */ | |
1687 | if (cur->dfs_state == DFS_DONE) | |
1688 | break; | |
1689 | else if (cur->dfs_state != DFS_ACTIVE) | |
1690 | die("BUG: confusing delta dfs state in second pass: %d", | |
1691 | cur->dfs_state); | |
4cf2143e | 1692 | |
4cf2143e | 1693 | /* |
42b766d7 JK |
1694 | * If the total_depth is more than depth, then we need to snip |
1695 | * the chain into two or more smaller chains that don't exceed | |
1696 | * the maximum depth. Most of the resulting chains will contain | |
1697 | * (depth + 1) entries (i.e., depth deltas plus one base), and | |
1698 | * the last chain (i.e., the one containing entry) will contain | |
1699 | * whatever entries are left over, namely | |
1700 | * (total_depth % (depth + 1)) of them. | |
1701 | * | |
1702 | * Since we are iterating towards decreasing depth, we need to | |
1703 | * decrement total_depth as we go, and we need to write to the | |
1704 | * entry what its final depth will be after all of the | |
1705 | * snipping. Since we're snipping into chains of length (depth | |
1706 | * + 1) entries, the final depth of an entry will be its | |
1707 | * original depth modulo (depth + 1). Any time we encounter an | |
1708 | * entry whose final depth is supposed to be zero, we snip it | |
1709 | * from its delta base, thereby making it so. | |
4cf2143e | 1710 | */ |
42b766d7 JK |
1711 | cur->depth = (total_depth--) % (depth + 1); |
1712 | if (!cur->depth) | |
1713 | drop_reused_delta(cur); | |
1714 | ||
1715 | cur->dfs_state = DFS_DONE; | |
4cf2143e JK |
1716 | } |
1717 | } | |
1718 | ||
c323ac7d LT |
1719 | static void get_object_details(void) |
1720 | { | |
7cadf491 | 1721 | uint32_t i; |
5c49c116 NP |
1722 | struct object_entry **sorted_by_offset; |
1723 | ||
2834bc27 VM |
1724 | sorted_by_offset = xcalloc(to_pack.nr_objects, sizeof(struct object_entry *)); |
1725 | for (i = 0; i < to_pack.nr_objects; i++) | |
1726 | sorted_by_offset[i] = to_pack.objects + i; | |
9ed0d8d6 | 1727 | QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort); |
c323ac7d | 1728 | |
2834bc27 | 1729 | for (i = 0; i < to_pack.nr_objects; i++) { |
15366280 JH |
1730 | struct object_entry *entry = sorted_by_offset[i]; |
1731 | check_object(entry); | |
754980d0 | 1732 | if (big_file_threshold < entry->size) |
15366280 JH |
1733 | entry->no_try_delta = 1; |
1734 | } | |
3449f8c4 | 1735 | |
4cf2143e JK |
1736 | /* |
1737 | * This must happen in a second pass, since we rely on the delta | |
1738 | * information for the whole list being completed. | |
1739 | */ | |
1740 | for (i = 0; i < to_pack.nr_objects; i++) | |
1741 | break_delta_chains(&to_pack.objects[i]); | |
1742 | ||
5c49c116 | 1743 | free(sorted_by_offset); |
c323ac7d LT |
1744 | } |
1745 | ||
b904166c NP |
1746 | /* |
1747 | * We search for deltas in a list sorted by type, by filename hash, and then | |
1748 | * by size, so that we see progressively smaller and smaller files. | |
1749 | * That's because we prefer deltas to be from the bigger file | |
1750 | * to the smaller -- deletes are potentially cheaper, but perhaps | |
1751 | * more importantly, the bigger file is likely the more recent | |
1752 | * one. The deepest deltas are therefore the oldest objects which are | |
1753 | * less susceptible to be accessed often. | |
1754 | */ | |
9668cf59 | 1755 | static int type_size_sort(const void *_a, const void *_b) |
c323ac7d | 1756 | { |
9668cf59 NP |
1757 | const struct object_entry *a = *(struct object_entry **)_a; |
1758 | const struct object_entry *b = *(struct object_entry **)_b; | |
fd9b1bae NTND |
1759 | enum object_type a_type = oe_type(a); |
1760 | enum object_type b_type = oe_type(b); | |
9668cf59 | 1761 | |
fd9b1bae | 1762 | if (a_type > b_type) |
27225f2e | 1763 | return -1; |
fd9b1bae | 1764 | if (a_type < b_type) |
27225f2e | 1765 | return 1; |
b904166c | 1766 | if (a->hash > b->hash) |
7a979d99 | 1767 | return -1; |
b904166c | 1768 | if (a->hash < b->hash) |
7a979d99 | 1769 | return 1; |
b904166c | 1770 | if (a->preferred_base > b->preferred_base) |
c323ac7d | 1771 | return -1; |
b904166c NP |
1772 | if (a->preferred_base < b->preferred_base) |
1773 | return 1; | |
c323ac7d | 1774 | if (a->size > b->size) |
b904166c NP |
1775 | return -1; |
1776 | if (a->size < b->size) | |
c323ac7d | 1777 | return 1; |
b904166c | 1778 | return a < b ? -1 : (a > b); /* newest first */ |
c323ac7d LT |
1779 | } |
1780 | ||
1781 | struct unpacked { | |
1782 | struct object_entry *entry; | |
1783 | void *data; | |
f6c7081a | 1784 | struct delta_index *index; |
5a235b5e | 1785 | unsigned depth; |
c323ac7d LT |
1786 | }; |
1787 | ||
d250626c NP |
1788 | static int delta_cacheable(unsigned long src_size, unsigned long trg_size, |
1789 | unsigned long delta_size) | |
074b2eea MK |
1790 | { |
1791 | if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size) | |
1792 | return 0; | |
1793 | ||
e3dfddb3 MK |
1794 | if (delta_size < cache_max_small_delta_size) |
1795 | return 1; | |
1796 | ||
074b2eea MK |
1797 | /* cache delta, if objects are large enough compared to delta size */ |
1798 | if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10)) | |
1799 | return 1; | |
1800 | ||
1801 | return 0; | |
1802 | } | |
1803 | ||
7eb151d6 | 1804 | #ifndef NO_PTHREADS |
8ecce684 | 1805 | |
44626dc7 | 1806 | static pthread_mutex_t read_mutex; |
8ecce684 NP |
1807 | #define read_lock() pthread_mutex_lock(&read_mutex) |
1808 | #define read_unlock() pthread_mutex_unlock(&read_mutex) | |
1809 | ||
44626dc7 | 1810 | static pthread_mutex_t cache_mutex; |
3c701839 NP |
1811 | #define cache_lock() pthread_mutex_lock(&cache_mutex) |
1812 | #define cache_unlock() pthread_mutex_unlock(&cache_mutex) | |
1813 | ||
44626dc7 | 1814 | static pthread_mutex_t progress_mutex; |
8ecce684 NP |
1815 | #define progress_lock() pthread_mutex_lock(&progress_mutex) |
1816 | #define progress_unlock() pthread_mutex_unlock(&progress_mutex) | |
1817 | ||
1818 | #else | |
1819 | ||
e1ef8673 JH |
1820 | #define read_lock() (void)0 |
1821 | #define read_unlock() (void)0 | |
1822 | #define cache_lock() (void)0 | |
1823 | #define cache_unlock() (void)0 | |
1824 | #define progress_lock() (void)0 | |
1825 | #define progress_unlock() (void)0 | |
8ecce684 NP |
1826 | |
1827 | #endif | |
1828 | ||
f6c7081a | 1829 | static int try_delta(struct unpacked *trg, struct unpacked *src, |
ef0316fc | 1830 | unsigned max_depth, unsigned long *mem_usage) |
c323ac7d | 1831 | { |
f6c7081a NP |
1832 | struct object_entry *trg_entry = trg->entry; |
1833 | struct object_entry *src_entry = src->entry; | |
560b25a8 | 1834 | unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz; |
c83f032e | 1835 | unsigned ref_depth; |
21666f1a | 1836 | enum object_type type; |
c323ac7d LT |
1837 | void *delta_buf; |
1838 | ||
1839 | /* Don't bother doing diffs between different types */ | |
fd9b1bae | 1840 | if (oe_type(trg_entry) != oe_type(src_entry)) |
c323ac7d LT |
1841 | return -1; |
1842 | ||
51d1e83f | 1843 | /* |
15f07e06 JK |
1844 | * We do not bother to try a delta that we discarded on an |
1845 | * earlier try, but only when reusing delta data. Note that | |
1846 | * src_entry that is marked as the preferred_base should always | |
1847 | * be considered, as even if we produce a suboptimal delta against | |
1848 | * it, we will still save the transfer cost, as we already know | |
1849 | * the other side has it and we won't send src_entry at all. | |
51d1e83f | 1850 | */ |
a7de7130 | 1851 | if (reuse_delta && trg_entry->in_pack && |
e9195b58 | 1852 | trg_entry->in_pack == src_entry->in_pack && |
15f07e06 | 1853 | !src_entry->preferred_base && |
e9195b58 JH |
1854 | trg_entry->in_pack_type != OBJ_REF_DELTA && |
1855 | trg_entry->in_pack_type != OBJ_OFS_DELTA) | |
51d1e83f LT |
1856 | return 0; |
1857 | ||
898b14ce | 1858 | /* Let's not bust the allowed depth. */ |
5a235b5e | 1859 | if (src->depth >= max_depth) |
d116a45a | 1860 | return 0; |
c323ac7d | 1861 | |
c3b06a69 | 1862 | /* Now some size filtering heuristics. */ |
560b25a8 | 1863 | trg_size = trg_entry->size; |
c83f032e NP |
1864 | if (!trg_entry->delta) { |
1865 | max_size = trg_size/2 - 20; | |
1866 | ref_depth = 1; | |
1867 | } else { | |
1868 | max_size = trg_entry->delta_size; | |
5a235b5e | 1869 | ref_depth = trg->depth; |
c83f032e | 1870 | } |
720fe22d | 1871 | max_size = (uint64_t)max_size * (max_depth - src->depth) / |
c83f032e | 1872 | (max_depth - ref_depth + 1); |
c3b06a69 NP |
1873 | if (max_size == 0) |
1874 | return 0; | |
f6c7081a | 1875 | src_size = src_entry->size; |
560b25a8 | 1876 | sizediff = src_size < trg_size ? trg_size - src_size : 0; |
27225f2e | 1877 | if (sizediff >= max_size) |
f527cb8c | 1878 | return 0; |
a1dab41a BD |
1879 | if (trg_size < src_size / 32) |
1880 | return 0; | |
f6c7081a | 1881 | |
560b25a8 NP |
1882 | /* Load data if not already done */ |
1883 | if (!trg->data) { | |
8ecce684 | 1884 | read_lock(); |
b4f5aca4 | 1885 | trg->data = read_object_file(&trg_entry->idx.oid, &type, &sz); |
8ecce684 | 1886 | read_unlock(); |
2e3404c3 JH |
1887 | if (!trg->data) |
1888 | die("object %s cannot be read", | |
e6a492b7 | 1889 | oid_to_hex(&trg_entry->idx.oid)); |
560b25a8 NP |
1890 | if (sz != trg_size) |
1891 | die("object %s inconsistent object length (%lu vs %lu)", | |
e6a492b7 | 1892 | oid_to_hex(&trg_entry->idx.oid), sz, |
1893 | trg_size); | |
ef0316fc | 1894 | *mem_usage += sz; |
560b25a8 NP |
1895 | } |
1896 | if (!src->data) { | |
8ecce684 | 1897 | read_lock(); |
b4f5aca4 | 1898 | src->data = read_object_file(&src_entry->idx.oid, &type, &sz); |
8ecce684 | 1899 | read_unlock(); |
71064a95 NP |
1900 | if (!src->data) { |
1901 | if (src_entry->preferred_base) { | |
1902 | static int warned = 0; | |
1903 | if (!warned++) | |
1904 | warning("object %s cannot be read", | |
e6a492b7 | 1905 | oid_to_hex(&src_entry->idx.oid)); |
71064a95 NP |
1906 | /* |
1907 | * Those objects are not included in the | |
1908 | * resulting pack. Be resilient and ignore | |
1909 | * them if they can't be read, in case the | |
1910 | * pack could be created nevertheless. | |
1911 | */ | |
1912 | return 0; | |
1913 | } | |
2e3404c3 | 1914 | die("object %s cannot be read", |
e6a492b7 | 1915 | oid_to_hex(&src_entry->idx.oid)); |
71064a95 | 1916 | } |
560b25a8 NP |
1917 | if (sz != src_size) |
1918 | die("object %s inconsistent object length (%lu vs %lu)", | |
e6a492b7 | 1919 | oid_to_hex(&src_entry->idx.oid), sz, |
1920 | src_size); | |
ef0316fc | 1921 | *mem_usage += sz; |
560b25a8 NP |
1922 | } |
1923 | if (!src->index) { | |
1924 | src->index = create_delta_index(src->data, src_size); | |
a588d88a MK |
1925 | if (!src->index) { |
1926 | static int warned = 0; | |
1927 | if (!warned++) | |
1928 | warning("suboptimal pack - out of memory"); | |
1929 | return 0; | |
1930 | } | |
ef0316fc | 1931 | *mem_usage += sizeof_delta_index(src->index); |
560b25a8 NP |
1932 | } |
1933 | ||
1934 | delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size); | |
c323ac7d | 1935 | if (!delta_buf) |
75c42d8c | 1936 | return 0; |
f6c7081a | 1937 | |
9e2d57a0 | 1938 | if (trg_entry->delta) { |
848d732c BD |
1939 | /* Prefer only shallower same-sized deltas. */ |
1940 | if (delta_size == trg_entry->delta_size && | |
5a235b5e | 1941 | src->depth + 1 >= trg->depth) { |
848d732c BD |
1942 | free(delta_buf); |
1943 | return 0; | |
1944 | } | |
074b2eea | 1945 | } |
9e2d57a0 | 1946 | |
3c701839 NP |
1947 | /* |
1948 | * Handle memory allocation outside of the cache | |
1949 | * accounting lock. Compiler will optimize the strangeness | |
7eb151d6 | 1950 | * away when NO_PTHREADS is defined. |
3c701839 | 1951 | */ |
8e0f7003 | 1952 | free(trg_entry->delta_data); |
3c701839 | 1953 | cache_lock(); |
9e2d57a0 NP |
1954 | if (trg_entry->delta_data) { |
1955 | delta_cache_size -= trg_entry->delta_size; | |
9e2d57a0 NP |
1956 | trg_entry->delta_data = NULL; |
1957 | } | |
d250626c | 1958 | if (delta_cacheable(src_size, trg_size, delta_size)) { |
b7a28f78 | 1959 | delta_cache_size += delta_size; |
3c701839 NP |
1960 | cache_unlock(); |
1961 | trg_entry->delta_data = xrealloc(delta_buf, delta_size); | |
1962 | } else { | |
1963 | cache_unlock(); | |
074b2eea | 1964 | free(delta_buf); |
3c701839 NP |
1965 | } |
1966 | ||
b7a28f78 NP |
1967 | trg_entry->delta = src_entry; |
1968 | trg_entry->delta_size = delta_size; | |
1969 | trg->depth = src->depth + 1; | |
1970 | ||
f6c7081a | 1971 | return 1; |
c323ac7d LT |
1972 | } |
1973 | ||
898b14ce | 1974 | static unsigned int check_delta_limit(struct object_entry *me, unsigned int n) |
b2504a0d | 1975 | { |
898b14ce NP |
1976 | struct object_entry *child = me->delta_child; |
1977 | unsigned int m = n; | |
1978 | while (child) { | |
1979 | unsigned int c = check_delta_limit(child, n + 1); | |
1980 | if (m < c) | |
1981 | m = c; | |
1982 | child = child->delta_sibling; | |
1983 | } | |
1984 | return m; | |
b2504a0d NP |
1985 | } |
1986 | ||
75ad235c | 1987 | static unsigned long free_unpacked(struct unpacked *n) |
a97773ce | 1988 | { |
ef0316fc | 1989 | unsigned long freed_mem = sizeof_delta_index(n->index); |
a97773ce BD |
1990 | free_delta_index(n->index); |
1991 | n->index = NULL; | |
1992 | if (n->data) { | |
ef0316fc | 1993 | freed_mem += n->entry->size; |
6a83d902 | 1994 | FREE_AND_NULL(n->data); |
a97773ce BD |
1995 | } |
1996 | n->entry = NULL; | |
7d7baa5e | 1997 | n->depth = 0; |
ef0316fc | 1998 | return freed_mem; |
a97773ce BD |
1999 | } |
2000 | ||
384b32c0 | 2001 | static void find_deltas(struct object_entry **list, unsigned *list_size, |
e334977d | 2002 | int window, int depth, unsigned *processed) |
c323ac7d | 2003 | { |
384b32c0 | 2004 | uint32_t i, idx = 0, count = 0; |
7cadf491 | 2005 | struct unpacked *array; |
ef0316fc | 2006 | unsigned long mem_usage = 0; |
c323ac7d | 2007 | |
19d4b416 | 2008 | array = xcalloc(window, sizeof(struct unpacked)); |
21fcd1bd | 2009 | |
384b32c0 | 2010 | for (;;) { |
421b488a | 2011 | struct object_entry *entry; |
c323ac7d | 2012 | struct unpacked *n = array + idx; |
ef0316fc | 2013 | int j, max_depth, best_base = -1; |
c323ac7d | 2014 | |
384b32c0 NP |
2015 | progress_lock(); |
2016 | if (!*list_size) { | |
2017 | progress_unlock(); | |
2018 | break; | |
2019 | } | |
421b488a | 2020 | entry = *list++; |
384b32c0 NP |
2021 | (*list_size)--; |
2022 | if (!entry->preferred_base) { | |
2023 | (*processed)++; | |
2024 | display_progress(progress_state, *processed); | |
2025 | } | |
2026 | progress_unlock(); | |
2027 | ||
ef0316fc | 2028 | mem_usage -= free_unpacked(n); |
c323ac7d | 2029 | n->entry = entry; |
ab7cd7bb | 2030 | |
a97773ce | 2031 | while (window_memory_limit && |
ef0316fc | 2032 | mem_usage > window_memory_limit && |
a97773ce BD |
2033 | count > 1) { |
2034 | uint32_t tail = (idx + window - count) % window; | |
75ad235c | 2035 | mem_usage -= free_unpacked(array + tail); |
a97773ce BD |
2036 | count--; |
2037 | } | |
2038 | ||
75d39853 NP |
2039 | /* We do not compute delta to *create* objects we are not |
2040 | * going to pack. | |
2041 | */ | |
2042 | if (entry->preferred_base) | |
2043 | goto next; | |
2044 | ||
898b14ce NP |
2045 | /* |
2046 | * If the current object is at pack edge, take the depth the | |
2047 | * objects that depend on the current object into account | |
2048 | * otherwise they would become too deep. | |
2049 | */ | |
2050 | max_depth = depth; | |
2051 | if (entry->delta_child) { | |
2052 | max_depth -= check_delta_limit(entry, 0); | |
2053 | if (max_depth <= 0) | |
2054 | goto next; | |
2055 | } | |
2056 | ||
78817c15 LT |
2057 | j = window; |
2058 | while (--j > 0) { | |
77639870 | 2059 | int ret; |
7cadf491 | 2060 | uint32_t other_idx = idx + j; |
c323ac7d | 2061 | struct unpacked *m; |
78817c15 LT |
2062 | if (other_idx >= window) |
2063 | other_idx -= window; | |
c323ac7d LT |
2064 | m = array + other_idx; |
2065 | if (!m->entry) | |
2066 | break; | |
ef0316fc | 2067 | ret = try_delta(n, m, max_depth, &mem_usage); |
77639870 | 2068 | if (ret < 0) |
c323ac7d | 2069 | break; |
77639870 JH |
2070 | else if (ret > 0) |
2071 | best_base = other_idx; | |
c323ac7d | 2072 | } |
898b14ce | 2073 | |
ed4a9031 NP |
2074 | /* |
2075 | * If we decided to cache the delta data, then it is best | |
2076 | * to compress it right away. First because we have to do | |
2077 | * it anyway, and doing it here while we're threaded will | |
2078 | * save a lot of time in the non threaded write phase, | |
2079 | * as well as allow for caching more deltas within | |
2080 | * the same cache size limit. | |
2081 | * ... | |
2082 | * But only if not writing to stdout, since in that case | |
2083 | * the network is most likely throttling writes anyway, | |
2084 | * and therefore it is best to go to the write phase ASAP | |
2085 | * instead, as we can afford spending more time compressing | |
2086 | * between writes at that moment. | |
2087 | */ | |
2088 | if (entry->delta_data && !pack_to_stdout) { | |
2089 | entry->z_delta_size = do_compress(&entry->delta_data, | |
2090 | entry->delta_size); | |
2091 | cache_lock(); | |
2092 | delta_cache_size -= entry->delta_size; | |
2093 | delta_cache_size += entry->z_delta_size; | |
2094 | cache_unlock(); | |
2095 | } | |
2096 | ||
70ca1a3f JH |
2097 | /* if we made n a delta, and if n is already at max |
2098 | * depth, leaving it in the window is pointless. we | |
2099 | * should evict it first. | |
70ca1a3f | 2100 | */ |
70baf5d4 | 2101 | if (entry->delta && max_depth <= n->depth) |
70ca1a3f | 2102 | continue; |
ff45715c | 2103 | |
77639870 JH |
2104 | /* |
2105 | * Move the best delta base up in the window, after the | |
2106 | * currently deltified object, to keep it longer. It will | |
2107 | * be the first base object to be attempted next. | |
2108 | */ | |
2109 | if (entry->delta) { | |
2110 | struct unpacked swap = array[best_base]; | |
2111 | int dist = (window + idx - best_base) % window; | |
2112 | int dst = best_base; | |
2113 | while (dist--) { | |
2114 | int src = (dst + 1) % window; | |
2115 | array[dst] = array[src]; | |
2116 | dst = src; | |
2117 | } | |
2118 | array[dst] = swap; | |
2119 | } | |
2120 | ||
898b14ce | 2121 | next: |
521a4f4c | 2122 | idx++; |
a97773ce BD |
2123 | if (count + 1 < window) |
2124 | count++; | |
521a4f4c LT |
2125 | if (idx >= window) |
2126 | idx = 0; | |
384b32c0 | 2127 | } |
adee7bdf | 2128 | |
f6c7081a | 2129 | for (i = 0; i < window; ++i) { |
ff45715c | 2130 | free_delta_index(array[i].index); |
adee7bdf | 2131 | free(array[i].data); |
f6c7081a | 2132 | } |
adee7bdf | 2133 | free(array); |
c323ac7d LT |
2134 | } |
2135 | ||
7eb151d6 | 2136 | #ifndef NO_PTHREADS |
8ecce684 | 2137 | |
a9a74636 NP |
2138 | static void try_to_free_from_threads(size_t size) |
2139 | { | |
2140 | read_lock(); | |
7c3ecb32 | 2141 | release_pack_memory(size); |
a9a74636 NP |
2142 | read_unlock(); |
2143 | } | |
2144 | ||
bc9b2175 | 2145 | static try_to_free_t old_try_to_free_routine; |
851c34b0 | 2146 | |
50f22ada JS |
2147 | /* |
2148 | * The main thread waits on the condition that (at least) one of the workers | |
2149 | * has stopped working (which is indicated in the .working member of | |
2150 | * struct thread_params). | |
2151 | * When a work thread has completed its work, it sets .working to 0 and | |
2152 | * signals the main thread and waits on the condition that .data_ready | |
2153 | * becomes 1. | |
2154 | */ | |
2155 | ||
8ecce684 NP |
2156 | struct thread_params { |
2157 | pthread_t thread; | |
2158 | struct object_entry **list; | |
2159 | unsigned list_size; | |
384b32c0 | 2160 | unsigned remaining; |
8ecce684 NP |
2161 | int window; |
2162 | int depth; | |
50f22ada JS |
2163 | int working; |
2164 | int data_ready; | |
2165 | pthread_mutex_t mutex; | |
2166 | pthread_cond_t cond; | |
8ecce684 NP |
2167 | unsigned *processed; |
2168 | }; | |
2169 | ||
44626dc7 AH |
2170 | static pthread_cond_t progress_cond; |
2171 | ||
2172 | /* | |
2173 | * Mutex and conditional variable can't be statically-initialized on Windows. | |
2174 | */ | |
2175 | static void init_threaded_search(void) | |
2176 | { | |
93749194 | 2177 | init_recursive_mutex(&read_mutex); |
44626dc7 AH |
2178 | pthread_mutex_init(&cache_mutex, NULL); |
2179 | pthread_mutex_init(&progress_mutex, NULL); | |
2180 | pthread_cond_init(&progress_cond, NULL); | |
851c34b0 | 2181 | old_try_to_free_routine = set_try_to_free_routine(try_to_free_from_threads); |
44626dc7 AH |
2182 | } |
2183 | ||
2184 | static void cleanup_threaded_search(void) | |
2185 | { | |
851c34b0 | 2186 | set_try_to_free_routine(old_try_to_free_routine); |
44626dc7 AH |
2187 | pthread_cond_destroy(&progress_cond); |
2188 | pthread_mutex_destroy(&read_mutex); | |
2189 | pthread_mutex_destroy(&cache_mutex); | |
2190 | pthread_mutex_destroy(&progress_mutex); | |
2191 | } | |
c2a33679 | 2192 | |
8ecce684 NP |
2193 | static void *threaded_find_deltas(void *arg) |
2194 | { | |
c2a33679 NP |
2195 | struct thread_params *me = arg; |
2196 | ||
0c2ad00b | 2197 | progress_lock(); |
50f22ada | 2198 | while (me->remaining) { |
0c2ad00b MÅ |
2199 | progress_unlock(); |
2200 | ||
384b32c0 | 2201 | find_deltas(me->list, &me->remaining, |
c2a33679 | 2202 | me->window, me->depth, me->processed); |
50f22ada JS |
2203 | |
2204 | progress_lock(); | |
2205 | me->working = 0; | |
2206 | pthread_cond_signal(&progress_cond); | |
2207 | progress_unlock(); | |
2208 | ||
2209 | /* | |
2210 | * We must not set ->data_ready before we wait on the | |
2211 | * condition because the main thread may have set it to 1 | |
2212 | * before we get here. In order to be sure that new | |
2213 | * work is available if we see 1 in ->data_ready, it | |
2214 | * was initialized to 0 before this thread was spawned | |
2215 | * and we reset it to 0 right away. | |
2216 | */ | |
2217 | pthread_mutex_lock(&me->mutex); | |
2218 | while (!me->data_ready) | |
2219 | pthread_cond_wait(&me->cond, &me->mutex); | |
2220 | me->data_ready = 0; | |
2221 | pthread_mutex_unlock(&me->mutex); | |
0c2ad00b MÅ |
2222 | |
2223 | progress_lock(); | |
c2a33679 | 2224 | } |
0c2ad00b | 2225 | progress_unlock(); |
50f22ada JS |
2226 | /* leave ->working 1 so that this doesn't get more work assigned */ |
2227 | return NULL; | |
8ecce684 NP |
2228 | } |
2229 | ||
8ecce684 NP |
2230 | static void ll_find_deltas(struct object_entry **list, unsigned list_size, |
2231 | int window, int depth, unsigned *processed) | |
2232 | { | |
dcda3614 | 2233 | struct thread_params *p; |
384b32c0 | 2234 | int i, ret, active_threads = 0; |
c2a33679 | 2235 | |
44626dc7 AH |
2236 | init_threaded_search(); |
2237 | ||
367f4a43 | 2238 | if (delta_search_threads <= 1) { |
384b32c0 | 2239 | find_deltas(list, &list_size, window, depth, processed); |
44626dc7 | 2240 | cleanup_threaded_search(); |
367f4a43 NP |
2241 | return; |
2242 | } | |
43cc2b42 | 2243 | if (progress > pack_to_stdout) |
b6c29915 | 2244 | fprintf(stderr, "Delta compression using up to %d threads.\n", |
43cc2b42 | 2245 | delta_search_threads); |
dcda3614 | 2246 | p = xcalloc(delta_search_threads, sizeof(*p)); |
367f4a43 | 2247 | |
50f22ada | 2248 | /* Partition the work amongst work threads. */ |
367f4a43 | 2249 | for (i = 0; i < delta_search_threads; i++) { |
50f22ada JS |
2250 | unsigned sub_size = list_size / (delta_search_threads - i); |
2251 | ||
bf874896 NP |
2252 | /* don't use too small segments or no deltas will be found */ |
2253 | if (sub_size < 2*window && i+1 < delta_search_threads) | |
2254 | sub_size = 0; | |
2255 | ||
8ecce684 NP |
2256 | p[i].window = window; |
2257 | p[i].depth = depth; | |
2258 | p[i].processed = processed; | |
50f22ada JS |
2259 | p[i].working = 1; |
2260 | p[i].data_ready = 0; | |
c2a33679 | 2261 | |
59921b4b | 2262 | /* try to split chunks on "path" boundaries */ |
6fc74703 NP |
2263 | while (sub_size && sub_size < list_size && |
2264 | list[sub_size]->hash && | |
384b32c0 NP |
2265 | list[sub_size]->hash == list[sub_size-1]->hash) |
2266 | sub_size++; | |
2267 | ||
50f22ada JS |
2268 | p[i].list = list; |
2269 | p[i].list_size = sub_size; | |
2270 | p[i].remaining = sub_size; | |
59921b4b | 2271 | |
384b32c0 NP |
2272 | list += sub_size; |
2273 | list_size -= sub_size; | |
2274 | } | |
2275 | ||
50f22ada JS |
2276 | /* Start work threads. */ |
2277 | for (i = 0; i < delta_search_threads; i++) { | |
2278 | if (!p[i].list_size) | |
2279 | continue; | |
68e6a4f8 JS |
2280 | pthread_mutex_init(&p[i].mutex, NULL); |
2281 | pthread_cond_init(&p[i].cond, NULL); | |
50f22ada JS |
2282 | ret = pthread_create(&p[i].thread, NULL, |
2283 | threaded_find_deltas, &p[i]); | |
2284 | if (ret) | |
2285 | die("unable to create thread: %s", strerror(ret)); | |
2286 | active_threads++; | |
2287 | } | |
2288 | ||
384b32c0 NP |
2289 | /* |
2290 | * Now let's wait for work completion. Each time a thread is done | |
2291 | * with its work, we steal half of the remaining work from the | |
2292 | * thread with the largest number of unprocessed objects and give | |
2293 | * it to that newly idle thread. This ensure good load balancing | |
2294 | * until the remaining object list segments are simply too short | |
2295 | * to be worth splitting anymore. | |
2296 | */ | |
50f22ada JS |
2297 | while (active_threads) { |
2298 | struct thread_params *target = NULL; | |
384b32c0 NP |
2299 | struct thread_params *victim = NULL; |
2300 | unsigned sub_size = 0; | |
384b32c0 NP |
2301 | |
2302 | progress_lock(); | |
50f22ada JS |
2303 | for (;;) { |
2304 | for (i = 0; !target && i < delta_search_threads; i++) | |
2305 | if (!p[i].working) | |
2306 | target = &p[i]; | |
2307 | if (target) | |
2308 | break; | |
2309 | pthread_cond_wait(&progress_cond, &progress_mutex); | |
2310 | } | |
2311 | ||
384b32c0 NP |
2312 | for (i = 0; i < delta_search_threads; i++) |
2313 | if (p[i].remaining > 2*window && | |
2314 | (!victim || victim->remaining < p[i].remaining)) | |
2315 | victim = &p[i]; | |
2316 | if (victim) { | |
2317 | sub_size = victim->remaining / 2; | |
2318 | list = victim->list + victim->list_size - sub_size; | |
2319 | while (sub_size && list[0]->hash && | |
2320 | list[0]->hash == list[-1]->hash) { | |
2321 | list++; | |
2322 | sub_size--; | |
2323 | } | |
eb9688ff NP |
2324 | if (!sub_size) { |
2325 | /* | |
2326 | * It is possible for some "paths" to have | |
2327 | * so many objects that no hash boundary | |
2328 | * might be found. Let's just steal the | |
2329 | * exact half in that case. | |
2330 | */ | |
2331 | sub_size = victim->remaining / 2; | |
2332 | list -= sub_size; | |
2333 | } | |
384b32c0 NP |
2334 | target->list = list; |
2335 | victim->list_size -= sub_size; | |
2336 | victim->remaining -= sub_size; | |
2337 | } | |
384b32c0 NP |
2338 | target->list_size = sub_size; |
2339 | target->remaining = sub_size; | |
50f22ada JS |
2340 | target->working = 1; |
2341 | progress_unlock(); | |
2342 | ||
2343 | pthread_mutex_lock(&target->mutex); | |
2344 | target->data_ready = 1; | |
2345 | pthread_cond_signal(&target->cond); | |
2346 | pthread_mutex_unlock(&target->mutex); | |
c2a33679 | 2347 | |
384b32c0 | 2348 | if (!sub_size) { |
b81d9af7 | 2349 | pthread_join(target->thread, NULL); |
50f22ada JS |
2350 | pthread_cond_destroy(&target->cond); |
2351 | pthread_mutex_destroy(&target->mutex); | |
384b32c0 | 2352 | active_threads--; |
c2a33679 | 2353 | } |
50f22ada | 2354 | } |
44626dc7 | 2355 | cleanup_threaded_search(); |
dcda3614 | 2356 | free(p); |
8ecce684 NP |
2357 | } |
2358 | ||
2359 | #else | |
384b32c0 | 2360 | #define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p) |
8ecce684 NP |
2361 | #endif |
2362 | ||
b773ddea JK |
2363 | static void add_tag_chain(const struct object_id *oid) |
2364 | { | |
2365 | struct tag *tag; | |
2366 | ||
2367 | /* | |
2368 | * We catch duplicates already in add_object_entry(), but we'd | |
2369 | * prefer to do this extra check to avoid having to parse the | |
2370 | * tag at all if we already know that it's being packed (e.g., if | |
2371 | * it was included via bitmaps, we would not have parsed it | |
2372 | * previously). | |
2373 | */ | |
2374 | if (packlist_find(&to_pack, oid->hash, NULL)) | |
2375 | return; | |
2376 | ||
d3101b53 | 2377 | tag = lookup_tag(oid); |
b773ddea JK |
2378 | while (1) { |
2379 | if (!tag || parse_tag(tag) || !tag->tagged) | |
2380 | die("unable to pack objects reachable from tag %s", | |
2381 | oid_to_hex(oid)); | |
2382 | ||
188960b4 | 2383 | add_object_entry(&tag->object.oid, OBJ_TAG, NULL, 0); |
b773ddea JK |
2384 | |
2385 | if (tag->tagged->type != OBJ_TAG) | |
2386 | return; | |
2387 | ||
2388 | tag = (struct tag *)tag->tagged; | |
2389 | } | |
2390 | } | |
2391 | ||
d155254c | 2392 | static int add_ref_tag(const char *path, const struct object_id *oid, int flag, void *cb_data) |
f0a24aa5 | 2393 | { |
d155254c | 2394 | struct object_id peeled; |
f0a24aa5 | 2395 | |
59556548 | 2396 | if (starts_with(path, "refs/tags/") && /* is a tag? */ |
b420d909 | 2397 | !peel_ref(path, &peeled) && /* peelable? */ |
d155254c | 2398 | packlist_find(&to_pack, peeled.hash, NULL)) /* object packed? */ |
b773ddea | 2399 | add_tag_chain(oid); |
f0a24aa5 SP |
2400 | return 0; |
2401 | } | |
2402 | ||
f3123c4a JH |
2403 | static void prepare_pack(int window, int depth) |
2404 | { | |
9668cf59 | 2405 | struct object_entry **delta_list; |
6e1c2344 RJ |
2406 | uint32_t i, nr_deltas; |
2407 | unsigned n; | |
9668cf59 | 2408 | |
3f9ac8d2 | 2409 | get_object_details(); |
9668cf59 | 2410 | |
0e8189e2 NP |
2411 | /* |
2412 | * If we're locally repacking then we need to be doubly careful | |
2413 | * from now on in order to make sure no stealth corruption gets | |
2414 | * propagated to the new pack. Clients receiving streamed packs | |
2415 | * should validate everything they get anyway so no need to incur | |
2416 | * the additional cost here in that case. | |
2417 | */ | |
2418 | if (!pack_to_stdout) | |
2419 | do_check_packed_object_crc = 1; | |
2420 | ||
2834bc27 | 2421 | if (!to_pack.nr_objects || !window || !depth) |
9668cf59 NP |
2422 | return; |
2423 | ||
b32fa95f | 2424 | ALLOC_ARRAY(delta_list, to_pack.nr_objects); |
75d39853 NP |
2425 | nr_deltas = n = 0; |
2426 | ||
2834bc27 VM |
2427 | for (i = 0; i < to_pack.nr_objects; i++) { |
2428 | struct object_entry *entry = to_pack.objects + i; | |
75d39853 NP |
2429 | |
2430 | if (entry->delta) | |
2431 | /* This happens if we decided to reuse existing | |
a7de7130 | 2432 | * delta from a pack. "reuse_delta &&" is implied. |
75d39853 NP |
2433 | */ |
2434 | continue; | |
2435 | ||
2436 | if (entry->size < 50) | |
2437 | continue; | |
2438 | ||
2439 | if (entry->no_try_delta) | |
2440 | continue; | |
2441 | ||
6d6f9cdd | 2442 | if (!entry->preferred_base) { |
75d39853 | 2443 | nr_deltas++; |
fd9b1bae | 2444 | if (oe_type(entry) < 0) |
6d6f9cdd | 2445 | die("unable to get type of object %s", |
e6a492b7 | 2446 | oid_to_hex(&entry->idx.oid)); |
eede9f42 | 2447 | } else { |
fd9b1bae | 2448 | if (oe_type(entry) < 0) { |
eede9f42 NP |
2449 | /* |
2450 | * This object is not found, but we | |
2451 | * don't have to include it anyway. | |
2452 | */ | |
2453 | continue; | |
2454 | } | |
6d6f9cdd | 2455 | } |
75d39853 NP |
2456 | |
2457 | delta_list[n++] = entry; | |
2458 | } | |
2459 | ||
2f8b8947 | 2460 | if (nr_deltas && n > 1) { |
e334977d NP |
2461 | unsigned nr_done = 0; |
2462 | if (progress) | |
754dbc43 | 2463 | progress_state = start_progress(_("Compressing objects"), |
dc6a0757 | 2464 | nr_deltas); |
9ed0d8d6 | 2465 | QSORT(delta_list, n, type_size_sort); |
8ecce684 | 2466 | ll_find_deltas(delta_list, n, window+1, depth, &nr_done); |
4d4fcc54 | 2467 | stop_progress(&progress_state); |
e334977d NP |
2468 | if (nr_done != nr_deltas) |
2469 | die("inconsistency with delta count"); | |
75d39853 | 2470 | } |
9668cf59 | 2471 | free(delta_list); |
f3123c4a JH |
2472 | } |
2473 | ||
ef90d6d4 | 2474 | static int git_pack_config(const char *k, const char *v, void *cb) |
4812a93a | 2475 | { |
eeefa7c9 | 2476 | if (!strcmp(k, "pack.window")) { |
4812a93a JK |
2477 | window = git_config_int(k, v); |
2478 | return 0; | |
2479 | } | |
a97773ce BD |
2480 | if (!strcmp(k, "pack.windowmemory")) { |
2481 | window_memory_limit = git_config_ulong(k, v); | |
2482 | return 0; | |
2483 | } | |
2484 | if (!strcmp(k, "pack.depth")) { | |
842aaf93 TT |
2485 | depth = git_config_int(k, v); |
2486 | return 0; | |
2487 | } | |
074b2eea MK |
2488 | if (!strcmp(k, "pack.deltacachesize")) { |
2489 | max_delta_cache_size = git_config_int(k, v); | |
2490 | return 0; | |
2491 | } | |
e3dfddb3 MK |
2492 | if (!strcmp(k, "pack.deltacachelimit")) { |
2493 | cache_max_small_delta_size = git_config_int(k, v); | |
2494 | return 0; | |
2495 | } | |
ae4f07fb VM |
2496 | if (!strcmp(k, "pack.writebitmaphashcache")) { |
2497 | if (git_config_bool(k, v)) | |
2498 | write_bitmap_options |= BITMAP_OPT_HASH_CACHE; | |
2499 | else | |
2500 | write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE; | |
2501 | } | |
6b8fda2d | 2502 | if (!strcmp(k, "pack.usebitmaps")) { |
645c432d | 2503 | use_bitmap_index_default = git_config_bool(k, v); |
6b8fda2d VM |
2504 | return 0; |
2505 | } | |
693b86ff NP |
2506 | if (!strcmp(k, "pack.threads")) { |
2507 | delta_search_threads = git_config_int(k, v); | |
833e3df1 | 2508 | if (delta_search_threads < 0) |
693b86ff NP |
2509 | die("invalid number of threads specified (%d)", |
2510 | delta_search_threads); | |
7eb151d6 | 2511 | #ifdef NO_PTHREADS |
2e96d815 | 2512 | if (delta_search_threads != 1) { |
693b86ff | 2513 | warning("no threads support, ignoring %s", k); |
2e96d815 ÆAB |
2514 | delta_search_threads = 0; |
2515 | } | |
693b86ff NP |
2516 | #endif |
2517 | return 0; | |
2518 | } | |
4d00bda2 | 2519 | if (!strcmp(k, "pack.indexversion")) { |
ebcfb379 JH |
2520 | pack_idx_opts.version = git_config_int(k, v); |
2521 | if (pack_idx_opts.version > 2) | |
6e1c2344 | 2522 | die("bad pack.indexversion=%"PRIu32, |
ebcfb379 | 2523 | pack_idx_opts.version); |
4d00bda2 NP |
2524 | return 0; |
2525 | } | |
ef90d6d4 | 2526 | return git_default_config(k, v, cb); |
4812a93a JK |
2527 | } |
2528 | ||
b5d97e6b | 2529 | static void read_object_list_from_stdin(void) |
c323ac7d | 2530 | { |
188960b4 | 2531 | char line[GIT_MAX_HEXSZ + 1 + PATH_MAX + 2]; |
2532 | struct object_id oid; | |
2533 | const char *p; | |
b2504a0d | 2534 | |
da93d12b | 2535 | for (;;) { |
da93d12b LT |
2536 | if (!fgets(line, sizeof(line), stdin)) { |
2537 | if (feof(stdin)) | |
2538 | break; | |
2539 | if (!ferror(stdin)) | |
2540 | die("fgets returned NULL, not EOF, not error!"); | |
687dd75c | 2541 | if (errno != EINTR) |
d824cbba | 2542 | die_errno("fgets"); |
687dd75c JH |
2543 | clearerr(stdin); |
2544 | continue; | |
da93d12b | 2545 | } |
7a979d99 | 2546 | if (line[0] == '-') { |
188960b4 | 2547 | if (get_oid_hex(line+1, &oid)) |
2548 | die("expected edge object ID, got garbage:\n %s", | |
b5d97e6b | 2549 | line); |
188960b4 | 2550 | add_preferred_base(&oid); |
7a979d99 | 2551 | continue; |
21fcd1bd | 2552 | } |
188960b4 | 2553 | if (parse_oid_hex(line, &oid, &p)) |
2554 | die("expected object ID, got garbage:\n %s", line); | |
b5d97e6b | 2555 | |
188960b4 | 2556 | add_preferred_base_object(p + 1); |
fd9b1bae | 2557 | add_object_entry(&oid, OBJ_NONE, p + 1, 0); |
c323ac7d | 2558 | } |
b5d97e6b JH |
2559 | } |
2560 | ||
95308d64 | 2561 | /* Remember to update object flag allocation in object.h */ |
08cdfb13 JH |
2562 | #define OBJECT_ADDED (1u<<20) |
2563 | ||
11c211fa | 2564 | static void show_commit(struct commit *commit, void *data) |
b5d97e6b | 2565 | { |
188960b4 | 2566 | add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL, 0); |
08cdfb13 | 2567 | commit->object.flags |= OBJECT_ADDED; |
7cc8f971 VM |
2568 | |
2569 | if (write_bitmap_index) | |
2570 | index_commit_for_bitmap(commit); | |
b5d97e6b JH |
2571 | } |
2572 | ||
de1e67d0 | 2573 | static void show_object(struct object *obj, const char *name, void *data) |
b5d97e6b | 2574 | { |
8d2dfc49 | 2575 | add_preferred_base_object(name); |
188960b4 | 2576 | add_object_entry(&obj->oid, obj->type, name, 0); |
8d2dfc49 | 2577 | obj->flags |= OBJECT_ADDED; |
b5d97e6b JH |
2578 | } |
2579 | ||
9535ce73 JH |
2580 | static void show_object__ma_allow_any(struct object *obj, const char *name, void *data) |
2581 | { | |
2582 | assert(arg_missing_action == MA_ALLOW_ANY); | |
2583 | ||
2584 | /* | |
2585 | * Quietly ignore ALL missing objects. This avoids problems with | |
2586 | * staging them now and getting an odd error later. | |
2587 | */ | |
2588 | if (!has_object_file(&obj->oid)) | |
2589 | return; | |
2590 | ||
2591 | show_object(obj, name, data); | |
2592 | } | |
2593 | ||
0c16cd49 JT |
2594 | static void show_object__ma_allow_promisor(struct object *obj, const char *name, void *data) |
2595 | { | |
2596 | assert(arg_missing_action == MA_ALLOW_PROMISOR); | |
2597 | ||
2598 | /* | |
2599 | * Quietly ignore EXPECTED missing objects. This avoids problems with | |
2600 | * staging them now and getting an odd error later. | |
2601 | */ | |
2602 | if (!has_object_file(&obj->oid) && is_promisor_object(&obj->oid)) | |
2603 | return; | |
2604 | ||
2605 | show_object(obj, name, data); | |
2606 | } | |
2607 | ||
9535ce73 JH |
2608 | static int option_parse_missing_action(const struct option *opt, |
2609 | const char *arg, int unset) | |
2610 | { | |
2611 | assert(arg); | |
2612 | assert(!unset); | |
2613 | ||
2614 | if (!strcmp(arg, "error")) { | |
2615 | arg_missing_action = MA_ERROR; | |
2616 | fn_show_object = show_object; | |
2617 | return 0; | |
2618 | } | |
2619 | ||
2620 | if (!strcmp(arg, "allow-any")) { | |
2621 | arg_missing_action = MA_ALLOW_ANY; | |
0c16cd49 | 2622 | fetch_if_missing = 0; |
9535ce73 JH |
2623 | fn_show_object = show_object__ma_allow_any; |
2624 | return 0; | |
2625 | } | |
2626 | ||
0c16cd49 JT |
2627 | if (!strcmp(arg, "allow-promisor")) { |
2628 | arg_missing_action = MA_ALLOW_PROMISOR; | |
2629 | fetch_if_missing = 0; | |
2630 | fn_show_object = show_object__ma_allow_promisor; | |
2631 | return 0; | |
2632 | } | |
2633 | ||
9535ce73 JH |
2634 | die(_("invalid value for --missing")); |
2635 | return 0; | |
2636 | } | |
2637 | ||
8d1d8f83 JH |
2638 | static void show_edge(struct commit *commit) |
2639 | { | |
188960b4 | 2640 | add_preferred_base(&commit->object.oid); |
8d1d8f83 JH |
2641 | } |
2642 | ||
08cdfb13 JH |
2643 | struct in_pack_object { |
2644 | off_t offset; | |
2645 | struct object *object; | |
2646 | }; | |
2647 | ||
2648 | struct in_pack { | |
071bcaab RJ |
2649 | unsigned int alloc; |
2650 | unsigned int nr; | |
08cdfb13 JH |
2651 | struct in_pack_object *array; |
2652 | }; | |
2653 | ||
2654 | static void mark_in_pack_object(struct object *object, struct packed_git *p, struct in_pack *in_pack) | |
2655 | { | |
ed1c9977 | 2656 | in_pack->array[in_pack->nr].offset = find_pack_entry_one(object->oid.hash, p); |
08cdfb13 JH |
2657 | in_pack->array[in_pack->nr].object = object; |
2658 | in_pack->nr++; | |
2659 | } | |
2660 | ||
2661 | /* | |
2662 | * Compare the objects in the offset order, in order to emulate the | |
f18d244a | 2663 | * "git rev-list --objects" output that produced the pack originally. |
08cdfb13 JH |
2664 | */ |
2665 | static int ofscmp(const void *a_, const void *b_) | |
2666 | { | |
2667 | struct in_pack_object *a = (struct in_pack_object *)a_; | |
2668 | struct in_pack_object *b = (struct in_pack_object *)b_; | |
2669 | ||
2670 | if (a->offset < b->offset) | |
2671 | return -1; | |
2672 | else if (a->offset > b->offset) | |
2673 | return 1; | |
2674 | else | |
f2fd0760 | 2675 | return oidcmp(&a->object->oid, &b->object->oid); |
08cdfb13 JH |
2676 | } |
2677 | ||
2678 | static void add_objects_in_unpacked_packs(struct rev_info *revs) | |
2679 | { | |
2680 | struct packed_git *p; | |
2681 | struct in_pack in_pack; | |
2682 | uint32_t i; | |
2683 | ||
2684 | memset(&in_pack, 0, sizeof(in_pack)); | |
2685 | ||
a80d72db | 2686 | for (p = get_packed_git(the_repository); p; p = p->next) { |
188960b4 | 2687 | struct object_id oid; |
08cdfb13 JH |
2688 | struct object *o; |
2689 | ||
79bc4c71 | 2690 | if (!p->pack_local || p->pack_keep) |
08cdfb13 JH |
2691 | continue; |
2692 | if (open_pack_index(p)) | |
2693 | die("cannot open pack index"); | |
2694 | ||
2695 | ALLOC_GROW(in_pack.array, | |
2696 | in_pack.nr + p->num_objects, | |
2697 | in_pack.alloc); | |
2698 | ||
2699 | for (i = 0; i < p->num_objects; i++) { | |
188960b4 | 2700 | nth_packed_object_oid(&oid, p, i); |
2701 | o = lookup_unknown_object(oid.hash); | |
08cdfb13 JH |
2702 | if (!(o->flags & OBJECT_ADDED)) |
2703 | mark_in_pack_object(o, p, &in_pack); | |
2704 | o->flags |= OBJECT_ADDED; | |
2705 | } | |
2706 | } | |
2707 | ||
2708 | if (in_pack.nr) { | |
9ed0d8d6 | 2709 | QSORT(in_pack.array, in_pack.nr, ofscmp); |
08cdfb13 JH |
2710 | for (i = 0; i < in_pack.nr; i++) { |
2711 | struct object *o = in_pack.array[i].object; | |
188960b4 | 2712 | add_object_entry(&o->oid, o->type, "", 0); |
08cdfb13 JH |
2713 | } |
2714 | } | |
2715 | free(in_pack.array); | |
2716 | } | |
2717 | ||
76c1d9a0 | 2718 | static int add_loose_object(const struct object_id *oid, const char *path, |
e26a8c47 JK |
2719 | void *data) |
2720 | { | |
abef9020 | 2721 | enum object_type type = oid_object_info(oid, NULL); |
e26a8c47 JK |
2722 | |
2723 | if (type < 0) { | |
2724 | warning("loose object at %s could not be examined", path); | |
2725 | return 0; | |
2726 | } | |
2727 | ||
188960b4 | 2728 | add_object_entry(oid, type, "", 0); |
e26a8c47 JK |
2729 | return 0; |
2730 | } | |
2731 | ||
2732 | /* | |
2733 | * We actually don't even have to worry about reachability here. | |
2734 | * add_object_entry will weed out duplicates, so we just add every | |
2735 | * loose object we find. | |
2736 | */ | |
2737 | static void add_unreachable_loose_objects(void) | |
2738 | { | |
2739 | for_each_loose_file_in_objdir(get_object_directory(), | |
2740 | add_loose_object, | |
2741 | NULL, NULL, NULL); | |
2742 | } | |
2743 | ||
188960b4 | 2744 | static int has_sha1_pack_kept_or_nonlocal(const struct object_id *oid) |
094085e3 BC |
2745 | { |
2746 | static struct packed_git *last_found = (void *)1; | |
2747 | struct packed_git *p; | |
2748 | ||
a80d72db SB |
2749 | p = (last_found != (void *)1) ? last_found : |
2750 | get_packed_git(the_repository); | |
094085e3 BC |
2751 | |
2752 | while (p) { | |
2753 | if ((!p->pack_local || p->pack_keep) && | |
188960b4 | 2754 | find_pack_entry_one(oid->hash, p)) { |
094085e3 BC |
2755 | last_found = p; |
2756 | return 1; | |
2757 | } | |
2758 | if (p == last_found) | |
a80d72db | 2759 | p = get_packed_git(the_repository); |
094085e3 BC |
2760 | else |
2761 | p = p->next; | |
2762 | if (p == last_found) | |
2763 | p = p->next; | |
2764 | } | |
2765 | return 0; | |
2766 | } | |
2767 | ||
abcb8655 JK |
2768 | /* |
2769 | * Store a list of sha1s that are should not be discarded | |
2770 | * because they are either written too recently, or are | |
2771 | * reachable from another object that was. | |
2772 | * | |
2773 | * This is filled by get_object_list. | |
2774 | */ | |
910650d2 | 2775 | static struct oid_array recent_objects; |
abcb8655 | 2776 | |
4ce3621a | 2777 | static int loosened_object_can_be_discarded(const struct object_id *oid, |
dddbad72 | 2778 | timestamp_t mtime) |
d0d46abc JK |
2779 | { |
2780 | if (!unpack_unreachable_expiration) | |
2781 | return 0; | |
2782 | if (mtime > unpack_unreachable_expiration) | |
2783 | return 0; | |
910650d2 | 2784 | if (oid_array_lookup(&recent_objects, oid) >= 0) |
abcb8655 | 2785 | return 0; |
d0d46abc JK |
2786 | return 1; |
2787 | } | |
2788 | ||
ca11b212 NP |
2789 | static void loosen_unused_packed_objects(struct rev_info *revs) |
2790 | { | |
2791 | struct packed_git *p; | |
2792 | uint32_t i; | |
4ce3621a | 2793 | struct object_id oid; |
ca11b212 | 2794 | |
a80d72db | 2795 | for (p = get_packed_git(the_repository); p; p = p->next) { |
79bc4c71 | 2796 | if (!p->pack_local || p->pack_keep) |
ca11b212 NP |
2797 | continue; |
2798 | ||
2799 | if (open_pack_index(p)) | |
2800 | die("cannot open pack index"); | |
2801 | ||
2802 | for (i = 0; i < p->num_objects; i++) { | |
4ce3621a | 2803 | nth_packed_object_oid(&oid, p, i); |
2804 | if (!packlist_find(&to_pack, oid.hash, NULL) && | |
188960b4 | 2805 | !has_sha1_pack_kept_or_nonlocal(&oid) && |
4ce3621a | 2806 | !loosened_object_can_be_discarded(&oid, p->mtime)) |
4bdb70a4 | 2807 | if (force_object_loose(&oid, p->mtime)) |
ca11b212 NP |
2808 | die("unable to force loose object"); |
2809 | } | |
2810 | } | |
2811 | } | |
2812 | ||
69e4b342 | 2813 | /* |
645c432d KS |
2814 | * This tracks any options which pack-reuse code expects to be on, or which a |
2815 | * reader of the pack might not understand, and which would therefore prevent | |
2816 | * blind reuse of what we have on disk. | |
69e4b342 JK |
2817 | */ |
2818 | static int pack_options_allow_reuse(void) | |
2819 | { | |
9df4a607 JK |
2820 | return pack_to_stdout && |
2821 | allow_ofs_delta && | |
2822 | !ignore_packed_keep && | |
2823 | (!local || !have_non_local_packs) && | |
2824 | !incremental; | |
69e4b342 JK |
2825 | } |
2826 | ||
6b8fda2d VM |
2827 | static int get_object_list_from_bitmap(struct rev_info *revs) |
2828 | { | |
2829 | if (prepare_bitmap_walk(revs) < 0) | |
2830 | return -1; | |
2831 | ||
69e4b342 JK |
2832 | if (pack_options_allow_reuse() && |
2833 | !reuse_partial_packfile_from_bitmap( | |
6b8fda2d VM |
2834 | &reuse_packfile, |
2835 | &reuse_packfile_objects, | |
2836 | &reuse_packfile_offset)) { | |
2837 | assert(reuse_packfile_objects); | |
2838 | nr_result += reuse_packfile_objects; | |
78d2214e | 2839 | display_progress(progress_state, nr_result); |
6b8fda2d VM |
2840 | } |
2841 | ||
2842 | traverse_bitmap_commit_list(&add_object_entry_from_bitmap); | |
2843 | return 0; | |
2844 | } | |
2845 | ||
abcb8655 | 2846 | static void record_recent_object(struct object *obj, |
de1e67d0 | 2847 | const char *name, |
abcb8655 JK |
2848 | void *data) |
2849 | { | |
910650d2 | 2850 | oid_array_append(&recent_objects, &obj->oid); |
abcb8655 JK |
2851 | } |
2852 | ||
2853 | static void record_recent_commit(struct commit *commit, void *data) | |
2854 | { | |
910650d2 | 2855 | oid_array_append(&recent_objects, &commit->object.oid); |
abcb8655 JK |
2856 | } |
2857 | ||
8d1d8f83 | 2858 | static void get_object_list(int ac, const char **av) |
b5d97e6b JH |
2859 | { |
2860 | struct rev_info revs; | |
2861 | char line[1000]; | |
b5d97e6b JH |
2862 | int flags = 0; |
2863 | ||
b5d97e6b JH |
2864 | init_revisions(&revs, NULL); |
2865 | save_commit_buffer = 0; | |
b5d97e6b JH |
2866 | setup_revisions(ac, av, &revs, NULL); |
2867 | ||
b790e0f6 NTND |
2868 | /* make sure shallows are read */ |
2869 | is_repository_shallow(); | |
2870 | ||
b5d97e6b JH |
2871 | while (fgets(line, sizeof(line), stdin) != NULL) { |
2872 | int len = strlen(line); | |
872c930d | 2873 | if (len && line[len - 1] == '\n') |
b5d97e6b JH |
2874 | line[--len] = 0; |
2875 | if (!len) | |
2876 | break; | |
2877 | if (*line == '-') { | |
2878 | if (!strcmp(line, "--not")) { | |
2879 | flags ^= UNINTERESTING; | |
7cc8f971 | 2880 | write_bitmap_index = 0; |
b5d97e6b JH |
2881 | continue; |
2882 | } | |
b790e0f6 | 2883 | if (starts_with(line, "--shallow ")) { |
e92b848c | 2884 | struct object_id oid; |
2885 | if (get_oid_hex(line + 10, &oid)) | |
b790e0f6 | 2886 | die("not an SHA-1 '%s'", line + 10); |
e92b848c | 2887 | register_shallow(&oid); |
f7f91086 | 2888 | use_bitmap_index = 0; |
b790e0f6 NTND |
2889 | continue; |
2890 | } | |
b5d97e6b JH |
2891 | die("not a rev '%s'", line); |
2892 | } | |
8e676e8b | 2893 | if (handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME)) |
b5d97e6b JH |
2894 | die("bad revision '%s'", line); |
2895 | } | |
2896 | ||
6b8fda2d VM |
2897 | if (use_bitmap_index && !get_object_list_from_bitmap(&revs)) |
2898 | return; | |
2899 | ||
3d51e1b5 MK |
2900 | if (prepare_revision_walk(&revs)) |
2901 | die("revision walk setup failed"); | |
e76a5fb4 | 2902 | mark_edges_uninteresting(&revs, show_edge); |
9535ce73 JH |
2903 | |
2904 | if (!fn_show_object) | |
2905 | fn_show_object = show_object; | |
2906 | traverse_commit_list_filtered(&filter_options, &revs, | |
2907 | show_commit, fn_show_object, NULL, | |
2908 | NULL); | |
08cdfb13 | 2909 | |
abcb8655 JK |
2910 | if (unpack_unreachable_expiration) { |
2911 | revs.ignore_missing_links = 1; | |
2912 | if (add_unseen_recent_objects_to_traversal(&revs, | |
2913 | unpack_unreachable_expiration)) | |
2914 | die("unable to add recent objects"); | |
2915 | if (prepare_revision_walk(&revs)) | |
2916 | die("revision walk setup failed"); | |
2917 | traverse_commit_list(&revs, record_recent_commit, | |
2918 | record_recent_object, NULL); | |
2919 | } | |
2920 | ||
08cdfb13 JH |
2921 | if (keep_unreachable) |
2922 | add_objects_in_unpacked_packs(&revs); | |
e26a8c47 JK |
2923 | if (pack_loose_unreachable) |
2924 | add_unreachable_loose_objects(); | |
ca11b212 NP |
2925 | if (unpack_unreachable) |
2926 | loosen_unused_packed_objects(&revs); | |
abcb8655 | 2927 | |
910650d2 | 2928 | oid_array_clear(&recent_objects); |
b5d97e6b JH |
2929 | } |
2930 | ||
99fb6e04 NTND |
2931 | static int option_parse_index_version(const struct option *opt, |
2932 | const char *arg, int unset) | |
2933 | { | |
2934 | char *c; | |
2935 | const char *val = arg; | |
2936 | pack_idx_opts.version = strtoul(val, &c, 10); | |
2937 | if (pack_idx_opts.version > 2) | |
2938 | die(_("unsupported index version %s"), val); | |
2939 | if (*c == ',' && c[1]) | |
2940 | pack_idx_opts.off32_limit = strtoul(c+1, &c, 0); | |
2941 | if (*c || pack_idx_opts.off32_limit & 0x80000000) | |
2942 | die(_("bad index version '%s'"), val); | |
2943 | return 0; | |
2944 | } | |
2945 | ||
7e52f566 JK |
2946 | static int option_parse_unpack_unreachable(const struct option *opt, |
2947 | const char *arg, int unset) | |
2948 | { | |
2949 | if (unset) { | |
2950 | unpack_unreachable = 0; | |
2951 | unpack_unreachable_expiration = 0; | |
2952 | } | |
2953 | else { | |
2954 | unpack_unreachable = 1; | |
2955 | if (arg) | |
2956 | unpack_unreachable_expiration = approxidate(arg); | |
2957 | } | |
2958 | return 0; | |
2959 | } | |
2960 | ||
b5d97e6b JH |
2961 | int cmd_pack_objects(int argc, const char **argv, const char *prefix) |
2962 | { | |
b5d97e6b | 2963 | int use_internal_rev_list = 0; |
8d1d8f83 | 2964 | int thin = 0; |
2dacf26d | 2965 | int shallow = 0; |
4f366275 | 2966 | int all_progress_implied = 0; |
edfbb2aa | 2967 | struct argv_array rp = ARGV_ARRAY_INIT; |
99fb6e04 | 2968 | int rev_list_unpacked = 0, rev_list_all = 0, rev_list_reflog = 0; |
c90f9e13 | 2969 | int rev_list_index = 0; |
99fb6e04 NTND |
2970 | struct option pack_objects_options[] = { |
2971 | OPT_SET_INT('q', "quiet", &progress, | |
4c688120 | 2972 | N_("do not show progress meter"), 0), |
99fb6e04 | 2973 | OPT_SET_INT(0, "progress", &progress, |
4c688120 | 2974 | N_("show progress meter"), 1), |
99fb6e04 | 2975 | OPT_SET_INT(0, "all-progress", &progress, |
4c688120 | 2976 | N_("show progress meter during object writing phase"), 2), |
99fb6e04 NTND |
2977 | OPT_BOOL(0, "all-progress-implied", |
2978 | &all_progress_implied, | |
4c688120 NTND |
2979 | N_("similar to --all-progress when progress meter is shown")), |
2980 | { OPTION_CALLBACK, 0, "index-version", NULL, N_("version[,offset]"), | |
2981 | N_("write the pack index file in the specified idx format version"), | |
99fb6e04 | 2982 | 0, option_parse_index_version }, |
2a514ed8 CB |
2983 | OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit, |
2984 | N_("maximum size of each output pack file")), | |
99fb6e04 | 2985 | OPT_BOOL(0, "local", &local, |
4c688120 | 2986 | N_("ignore borrowed objects from alternate object store")), |
99fb6e04 | 2987 | OPT_BOOL(0, "incremental", &incremental, |
4c688120 | 2988 | N_("ignore packed objects")), |
99fb6e04 | 2989 | OPT_INTEGER(0, "window", &window, |
4c688120 | 2990 | N_("limit pack window by objects")), |
2a514ed8 CB |
2991 | OPT_MAGNITUDE(0, "window-memory", &window_memory_limit, |
2992 | N_("limit pack window by memory in addition to object limit")), | |
99fb6e04 | 2993 | OPT_INTEGER(0, "depth", &depth, |
4c688120 | 2994 | N_("maximum length of delta chain allowed in the resulting pack")), |
99fb6e04 | 2995 | OPT_BOOL(0, "reuse-delta", &reuse_delta, |
4c688120 | 2996 | N_("reuse existing deltas")), |
99fb6e04 | 2997 | OPT_BOOL(0, "reuse-object", &reuse_object, |
4c688120 | 2998 | N_("reuse existing objects")), |
99fb6e04 | 2999 | OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta, |
4c688120 | 3000 | N_("use OFS_DELTA objects")), |
99fb6e04 | 3001 | OPT_INTEGER(0, "threads", &delta_search_threads, |
4c688120 | 3002 | N_("use threads when searching for best delta matches")), |
99fb6e04 | 3003 | OPT_BOOL(0, "non-empty", &non_empty, |
4c688120 | 3004 | N_("do not create an empty pack output")), |
99fb6e04 | 3005 | OPT_BOOL(0, "revs", &use_internal_rev_list, |
4c688120 | 3006 | N_("read revision arguments from standard input")), |
99fb6e04 | 3007 | { OPTION_SET_INT, 0, "unpacked", &rev_list_unpacked, NULL, |
4c688120 | 3008 | N_("limit the objects to those that are not yet packed"), |
99fb6e04 NTND |
3009 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 }, |
3010 | { OPTION_SET_INT, 0, "all", &rev_list_all, NULL, | |
4c688120 | 3011 | N_("include objects reachable from any reference"), |
99fb6e04 NTND |
3012 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 }, |
3013 | { OPTION_SET_INT, 0, "reflog", &rev_list_reflog, NULL, | |
4c688120 | 3014 | N_("include objects referred by reflog entries"), |
99fb6e04 | 3015 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 }, |
c90f9e13 JK |
3016 | { OPTION_SET_INT, 0, "indexed-objects", &rev_list_index, NULL, |
3017 | N_("include objects referred to by the index"), | |
3018 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 }, | |
99fb6e04 | 3019 | OPT_BOOL(0, "stdout", &pack_to_stdout, |
4c688120 | 3020 | N_("output pack to stdout")), |
99fb6e04 | 3021 | OPT_BOOL(0, "include-tag", &include_tag, |
4c688120 | 3022 | N_("include tag objects that refer to objects to be packed")), |
99fb6e04 | 3023 | OPT_BOOL(0, "keep-unreachable", &keep_unreachable, |
4c688120 | 3024 | N_("keep unreachable objects")), |
e26a8c47 JK |
3025 | OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable, |
3026 | N_("pack loose unreachable objects")), | |
4c688120 NTND |
3027 | { OPTION_CALLBACK, 0, "unpack-unreachable", NULL, N_("time"), |
3028 | N_("unpack unreachable objects newer than <time>"), | |
7e52f566 | 3029 | PARSE_OPT_OPTARG, option_parse_unpack_unreachable }, |
99fb6e04 | 3030 | OPT_BOOL(0, "thin", &thin, |
4c688120 | 3031 | N_("create thin packs")), |
2dacf26d | 3032 | OPT_BOOL(0, "shallow", &shallow, |
3033 | N_("create packs suitable for shallow fetches")), | |
99fb6e04 | 3034 | OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep, |
4c688120 | 3035 | N_("ignore packs that have companion .keep file")), |
99fb6e04 | 3036 | OPT_INTEGER(0, "compression", &pack_compression_level, |
4c688120 | 3037 | N_("pack compression level")), |
99fb6e04 | 3038 | OPT_SET_INT(0, "keep-true-parents", &grafts_replace_parents, |
4c688120 | 3039 | N_("do not hide commits by grafts"), 0), |
6b8fda2d VM |
3040 | OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index, |
3041 | N_("use a bitmap index if available to speed up counting objects")), | |
7cc8f971 VM |
3042 | OPT_BOOL(0, "write-bitmap-index", &write_bitmap_index, |
3043 | N_("write a bitmap index together with the pack index")), | |
9535ce73 JH |
3044 | OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options), |
3045 | { OPTION_CALLBACK, 0, "missing", NULL, N_("action"), | |
3046 | N_("handling for missing objects"), PARSE_OPT_NONEG, | |
3047 | option_parse_missing_action }, | |
0c16cd49 JT |
3048 | OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects, |
3049 | N_("do not pack objects in promisor packfiles")), | |
99fb6e04 NTND |
3050 | OPT_END(), |
3051 | }; | |
8d1d8f83 | 3052 | |
0c6804ab NTND |
3053 | if (DFS_NUM_STATES > (1 << OE_DFS_STATE_BITS)) |
3054 | BUG("too many dfs states, increase OE_DFS_STATE_BITS"); | |
3055 | ||
afc711b8 | 3056 | check_replace_refs = 0; |
dae556bd | 3057 | |
ebcfb379 | 3058 | reset_pack_idx_option(&pack_idx_opts); |
ef90d6d4 | 3059 | git_config(git_pack_config, NULL); |
b5d97e6b JH |
3060 | |
3061 | progress = isatty(2); | |
99fb6e04 NTND |
3062 | argc = parse_options(argc, argv, prefix, pack_objects_options, |
3063 | pack_usage, 0); | |
b5d97e6b | 3064 | |
99fb6e04 NTND |
3065 | if (argc) { |
3066 | base_name = argv[0]; | |
3067 | argc--; | |
b5d97e6b | 3068 | } |
99fb6e04 NTND |
3069 | if (pack_to_stdout != !base_name || argc) |
3070 | usage_with_options(pack_usage, pack_objects_options); | |
b5d97e6b | 3071 | |
b5c0cbd8 NTND |
3072 | if (depth >= (1 << OE_DEPTH_BITS)) { |
3073 | warning(_("delta chain depth %d is too deep, forcing %d"), | |
3074 | depth, (1 << OE_DEPTH_BITS) - 1); | |
3075 | depth = (1 << OE_DEPTH_BITS) - 1; | |
3076 | } | |
3077 | ||
edfbb2aa | 3078 | argv_array_push(&rp, "pack-objects"); |
99fb6e04 NTND |
3079 | if (thin) { |
3080 | use_internal_rev_list = 1; | |
2dacf26d | 3081 | argv_array_push(&rp, shallow |
3082 | ? "--objects-edge-aggressive" | |
3083 | : "--objects-edge"); | |
99fb6e04 | 3084 | } else |
edfbb2aa | 3085 | argv_array_push(&rp, "--objects"); |
b5d97e6b | 3086 | |
99fb6e04 NTND |
3087 | if (rev_list_all) { |
3088 | use_internal_rev_list = 1; | |
edfbb2aa | 3089 | argv_array_push(&rp, "--all"); |
99fb6e04 NTND |
3090 | } |
3091 | if (rev_list_reflog) { | |
3092 | use_internal_rev_list = 1; | |
edfbb2aa | 3093 | argv_array_push(&rp, "--reflog"); |
99fb6e04 | 3094 | } |
c90f9e13 JK |
3095 | if (rev_list_index) { |
3096 | use_internal_rev_list = 1; | |
3097 | argv_array_push(&rp, "--indexed-objects"); | |
99fb6e04 NTND |
3098 | } |
3099 | if (rev_list_unpacked) { | |
3100 | use_internal_rev_list = 1; | |
edfbb2aa | 3101 | argv_array_push(&rp, "--unpacked"); |
99fb6e04 | 3102 | } |
b5d97e6b | 3103 | |
0c16cd49 JT |
3104 | if (exclude_promisor_objects) { |
3105 | use_internal_rev_list = 1; | |
3106 | fetch_if_missing = 0; | |
3107 | argv_array_push(&rp, "--exclude-promisor-objects"); | |
3108 | } | |
3109 | ||
99fb6e04 NTND |
3110 | if (!reuse_object) |
3111 | reuse_delta = 0; | |
3112 | if (pack_compression_level == -1) | |
3113 | pack_compression_level = Z_DEFAULT_COMPRESSION; | |
3114 | else if (pack_compression_level < 0 || pack_compression_level > Z_BEST_COMPRESSION) | |
3115 | die("bad pack compression level %d", pack_compression_level); | |
0c45d258 JH |
3116 | |
3117 | if (!delta_search_threads) /* --threads=0 means autodetect */ | |
3118 | delta_search_threads = online_cpus(); | |
3119 | ||
99fb6e04 NTND |
3120 | #ifdef NO_PTHREADS |
3121 | if (delta_search_threads != 1) | |
2b34e486 | 3122 | warning("no threads support, ignoring --threads"); |
99fb6e04 | 3123 | #endif |
2b84b5a8 JS |
3124 | if (!pack_to_stdout && !pack_size_limit) |
3125 | pack_size_limit = pack_size_limit_cfg; | |
01c12a23 DH |
3126 | if (pack_to_stdout && pack_size_limit) |
3127 | die("--max-pack-size cannot be used to build a pack for transfer."); | |
07cf0f24 NP |
3128 | if (pack_size_limit && pack_size_limit < 1024*1024) { |
3129 | warning("minimum pack size limit is 1 MiB"); | |
3130 | pack_size_limit = 1024*1024; | |
3131 | } | |
01c12a23 | 3132 | |
8d1d8f83 JH |
3133 | if (!pack_to_stdout && thin) |
3134 | die("--thin cannot be used to build an indexable pack."); | |
b5d97e6b | 3135 | |
ca11b212 NP |
3136 | if (keep_unreachable && unpack_unreachable) |
3137 | die("--keep-unreachable and --unpack-unreachable are incompatible."); | |
b1e757f3 JK |
3138 | if (!rev_list_all || !rev_list_reflog || !rev_list_index) |
3139 | unpack_unreachable_expiration = 0; | |
ca11b212 | 3140 | |
9535ce73 JH |
3141 | if (filter_options.choice) { |
3142 | if (!pack_to_stdout) | |
3143 | die("cannot use --filter without --stdout."); | |
3144 | use_bitmap_index = 0; | |
3145 | } | |
3146 | ||
645c432d KS |
3147 | /* |
3148 | * "soft" reasons not to use bitmaps - for on-disk repack by default we want | |
3149 | * | |
3150 | * - to produce good pack (with bitmap index not-yet-packed objects are | |
3151 | * packed in suboptimal order). | |
3152 | * | |
3153 | * - to use more robust pack-generation codepath (avoiding possible | |
3154 | * bugs in bitmap code and possible bitmap index corruption). | |
3155 | */ | |
3156 | if (!pack_to_stdout) | |
3157 | use_bitmap_index_default = 0; | |
3158 | ||
3159 | if (use_bitmap_index < 0) | |
3160 | use_bitmap_index = use_bitmap_index_default; | |
3161 | ||
3162 | /* "hard" reasons not to use bitmaps; these just won't work at all */ | |
3163 | if (!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) || is_repository_shallow()) | |
6b8fda2d VM |
3164 | use_bitmap_index = 0; |
3165 | ||
7cc8f971 VM |
3166 | if (pack_to_stdout || !rev_list_all) |
3167 | write_bitmap_index = 0; | |
3168 | ||
4f366275 NP |
3169 | if (progress && all_progress_implied) |
3170 | progress = 2; | |
3171 | ||
56dfeb62 JK |
3172 | if (ignore_packed_keep) { |
3173 | struct packed_git *p; | |
a80d72db | 3174 | for (p = get_packed_git(the_repository); p; p = p->next) |
56dfeb62 JK |
3175 | if (p->pack_local && p->pack_keep) |
3176 | break; | |
3177 | if (!p) /* no keep-able packs found */ | |
3178 | ignore_packed_keep = 0; | |
3179 | } | |
3180 | if (local) { | |
3181 | /* | |
3182 | * unlike ignore_packed_keep above, we do not want to | |
3183 | * unset "local" based on looking at packs, as it | |
3184 | * also covers non-local objects | |
3185 | */ | |
3186 | struct packed_git *p; | |
a80d72db | 3187 | for (p = get_packed_git(the_repository); p; p = p->next) { |
56dfeb62 JK |
3188 | if (!p->pack_local) { |
3189 | have_non_local_packs = 1; | |
3190 | break; | |
3191 | } | |
3192 | } | |
3193 | } | |
b5d97e6b | 3194 | |
13aaf148 | 3195 | if (progress) |
754dbc43 | 3196 | progress_state = start_progress(_("Counting objects"), 0); |
b5d97e6b JH |
3197 | if (!use_internal_rev_list) |
3198 | read_object_list_from_stdin(); | |
8d1d8f83 | 3199 | else { |
edfbb2aa JK |
3200 | get_object_list(rp.argc, rp.argv); |
3201 | argv_array_clear(&rp); | |
8d1d8f83 | 3202 | } |
0ef95f72 | 3203 | cleanup_preferred_base(); |
d155254c MH |
3204 | if (include_tag && nr_result) |
3205 | for_each_ref(add_ref_tag, NULL); | |
4d4fcc54 | 3206 | stop_progress(&progress_state); |
96a02f8f | 3207 | |
f0b0af1b | 3208 | if (non_empty && !nr_result) |
1c4a2912 | 3209 | return 0; |
f7ae6a93 NP |
3210 | if (nr_result) |
3211 | prepare_pack(window, depth); | |
d01fb92f | 3212 | write_pack_file(); |
ab7cd7bb | 3213 | if (progress) |
6e1c2344 RJ |
3214 | fprintf(stderr, "Total %"PRIu32" (delta %"PRIu32")," |
3215 | " reused %"PRIu32" (delta %"PRIu32")\n", | |
67c08ce1 | 3216 | written, written_delta, reused, reused_delta); |
c323ac7d LT |
3217 | return 0; |
3218 | } |