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