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