]>
Commit | Line | Data |
---|---|---|
5d4a6003 | 1 | #include "builtin.h" |
32a8f510 | 2 | #include "environment.h" |
f394e093 | 3 | #include "gettext.h" |
41771fa4 | 4 | #include "hex.h" |
a80d72db | 5 | #include "repository.h" |
b2141fc1 | 6 | #include "config.h" |
a74db82e | 7 | #include "attr.h" |
c323ac7d | 8 | #include "object.h" |
8e440259 PE |
9 | #include "commit.h" |
10 | #include "tag.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 | 19 | #include "list-objects-filter-options.h" |
2834bc27 | 20 | #include "pack-objects.h" |
96a02f8f | 21 | #include "progress.h" |
f0a24aa5 | 22 | #include "refs.h" |
cf2ba13a | 23 | #include "streaming.h" |
93749194 | 24 | #include "thread-utils.h" |
6b8fda2d | 25 | #include "pack-bitmap.h" |
28b8a730 | 26 | #include "delta-islands.h" |
abcb8655 | 27 | #include "reachable.h" |
fe299ec5 | 28 | #include "oid-array.h" |
dbbcd44f | 29 | #include "strvec.h" |
ec2dd32c | 30 | #include "list.h" |
0317f455 | 31 | #include "packfile.h" |
87bed179 | 32 | #include "object-file.h" |
a034e910 | 33 | #include "object-store-ll.h" |
cbeab747 | 34 | #include "replace-object.h" |
ed7e5fc3 | 35 | #include "dir.h" |
6a22d521 | 36 | #include "midx.h" |
ae417807 | 37 | #include "trace2.h" |
120ad2b0 | 38 | #include "shallow.h" |
e00549aa | 39 | #include "promisor-remote.h" |
b7573536 | 40 | #include "pack-mtimes.h" |
49fd5511 | 41 | #include "parse-options.h" |
8ecce684 | 42 | |
7d089fb9 ÆAB |
43 | /* |
44 | * Objects we are going to pack are collected in the `to_pack` structure. | |
45 | * It contains an array (dynamically expanded) of the object data, and a map | |
46 | * that can resolve SHA1s to their position in the array. | |
47 | */ | |
48 | static struct packing_data to_pack; | |
49 | ||
50 | static inline struct object_entry *oe_delta( | |
51 | const struct packing_data *pack, | |
52 | const struct object_entry *e) | |
53 | { | |
54 | if (!e->delta_idx) | |
55 | return NULL; | |
56 | if (e->ext_base) | |
57 | return &pack->ext_bases[e->delta_idx - 1]; | |
58 | else | |
59 | return &pack->objects[e->delta_idx - 1]; | |
60 | } | |
61 | ||
62 | static inline unsigned long oe_delta_size(struct packing_data *pack, | |
63 | const struct object_entry *e) | |
64 | { | |
65 | if (e->delta_size_valid) | |
66 | return e->delta_size_; | |
67 | ||
68 | /* | |
69 | * pack->delta_size[] can't be NULL because oe_set_delta_size() | |
70 | * must have been called when a new delta is saved with | |
71 | * oe_set_delta(). | |
72 | * If oe_delta() returns NULL (i.e. default state, which means | |
73 | * delta_size_valid is also false), then the caller must never | |
74 | * call oe_delta_size(). | |
75 | */ | |
76 | return pack->delta_size[e - pack->objects]; | |
77 | } | |
78 | ||
79 | unsigned long oe_get_size_slow(struct packing_data *pack, | |
80 | const struct object_entry *e); | |
81 | ||
82 | static inline unsigned long oe_size(struct packing_data *pack, | |
83 | const struct object_entry *e) | |
84 | { | |
85 | if (e->size_valid) | |
86 | return e->size_; | |
87 | ||
88 | return oe_get_size_slow(pack, e); | |
89 | } | |
90 | ||
91 | static inline void oe_set_delta(struct packing_data *pack, | |
92 | struct object_entry *e, | |
93 | struct object_entry *delta) | |
94 | { | |
95 | if (delta) | |
96 | e->delta_idx = (delta - pack->objects) + 1; | |
97 | else | |
98 | e->delta_idx = 0; | |
99 | } | |
100 | ||
101 | static inline struct object_entry *oe_delta_sibling( | |
102 | const struct packing_data *pack, | |
103 | const struct object_entry *e) | |
104 | { | |
105 | if (e->delta_sibling_idx) | |
106 | return &pack->objects[e->delta_sibling_idx - 1]; | |
107 | return NULL; | |
108 | } | |
109 | ||
110 | static inline struct object_entry *oe_delta_child( | |
111 | const struct packing_data *pack, | |
112 | const struct object_entry *e) | |
113 | { | |
114 | if (e->delta_child_idx) | |
115 | return &pack->objects[e->delta_child_idx - 1]; | |
116 | return NULL; | |
117 | } | |
118 | ||
119 | static inline void oe_set_delta_child(struct packing_data *pack, | |
120 | struct object_entry *e, | |
121 | struct object_entry *delta) | |
122 | { | |
123 | if (delta) | |
124 | e->delta_child_idx = (delta - pack->objects) + 1; | |
125 | else | |
126 | e->delta_child_idx = 0; | |
127 | } | |
128 | ||
129 | static inline void oe_set_delta_sibling(struct packing_data *pack, | |
130 | struct object_entry *e, | |
131 | struct object_entry *delta) | |
132 | { | |
133 | if (delta) | |
134 | e->delta_sibling_idx = (delta - pack->objects) + 1; | |
135 | else | |
136 | e->delta_sibling_idx = 0; | |
137 | } | |
138 | ||
139 | static inline void oe_set_size(struct packing_data *pack, | |
140 | struct object_entry *e, | |
141 | unsigned long size) | |
142 | { | |
143 | if (size < pack->oe_size_limit) { | |
144 | e->size_ = size; | |
145 | e->size_valid = 1; | |
146 | } else { | |
147 | e->size_valid = 0; | |
148 | if (oe_get_size_slow(pack, e) != size) | |
149 | BUG("'size' is supposed to be the object size!"); | |
150 | } | |
151 | } | |
152 | ||
153 | static inline void oe_set_delta_size(struct packing_data *pack, | |
154 | struct object_entry *e, | |
155 | unsigned long size) | |
156 | { | |
157 | if (size < pack->oe_delta_size_limit) { | |
158 | e->delta_size_ = size; | |
159 | e->delta_size_valid = 1; | |
160 | } else { | |
161 | packing_data_lock(pack); | |
162 | if (!pack->delta_size) | |
163 | ALLOC_ARRAY(pack->delta_size, pack->nr_alloc); | |
164 | packing_data_unlock(pack); | |
165 | ||
166 | pack->delta_size[e - pack->objects] = size; | |
167 | e->delta_size_valid = 0; | |
168 | } | |
169 | } | |
170 | ||
43fa44fa | 171 | #define IN_PACK(obj) oe_in_pack(&to_pack, obj) |
ac77d0c3 NTND |
172 | #define SIZE(obj) oe_size(&to_pack, obj) |
173 | #define SET_SIZE(obj,size) oe_set_size(&to_pack, obj, size) | |
0aca34e8 | 174 | #define DELTA_SIZE(obj) oe_delta_size(&to_pack, obj) |
898eba5e NTND |
175 | #define DELTA(obj) oe_delta(&to_pack, obj) |
176 | #define DELTA_CHILD(obj) oe_delta_child(&to_pack, obj) | |
177 | #define DELTA_SIBLING(obj) oe_delta_sibling(&to_pack, obj) | |
178 | #define SET_DELTA(obj, val) oe_set_delta(&to_pack, obj, val) | |
6a1e32d5 | 179 | #define SET_DELTA_EXT(obj, oid) oe_set_delta_ext(&to_pack, obj, oid) |
0aca34e8 | 180 | #define SET_DELTA_SIZE(obj, val) oe_set_delta_size(&to_pack, obj, val) |
898eba5e NTND |
181 | #define SET_DELTA_CHILD(obj, val) oe_set_delta_child(&to_pack, obj, val) |
182 | #define SET_DELTA_SIBLING(obj, val) oe_set_delta_sibling(&to_pack, obj, val) | |
43fa44fa | 183 | |
99fb6e04 | 184 | static const char *pack_usage[] = { |
23a9235d ÆAB |
185 | N_("git pack-objects --stdout [<options>] [< <ref-list> | < <object-list>]"), |
186 | N_("git pack-objects [<options>] <base-name> [< <ref-list> | < <object-list>]"), | |
99fb6e04 NTND |
187 | NULL |
188 | }; | |
c323ac7d | 189 | |
79814f42 | 190 | static struct pack_idx_entry **written_list; |
5af05043 | 191 | static uint32_t nr_result, nr_written, nr_seen; |
6a1e32d5 | 192 | static struct bitmap_index *bitmap_git; |
28b8a730 | 193 | static uint32_t write_layer; |
3f9ac8d2 | 194 | |
96f1e58f | 195 | static int non_empty; |
a7de7130 | 196 | static int reuse_delta = 1, reuse_object = 1; |
e5e9714a | 197 | static int keep_unreachable, unpack_unreachable, include_tag; |
dddbad72 | 198 | static timestamp_t unpack_unreachable_expiration; |
e26a8c47 | 199 | static int pack_loose_unreachable; |
b7573536 TB |
200 | static int cruft; |
201 | static timestamp_t cruft_expiration; | |
96f1e58f | 202 | static int local; |
56dfeb62 | 203 | static int have_non_local_packs; |
96f1e58f | 204 | static int incremental; |
ed7e5fc3 NTND |
205 | static int ignore_packed_keep_on_disk; |
206 | static int ignore_packed_keep_in_core; | |
be6b1914 | 207 | static int allow_ofs_delta; |
ebcfb379 | 208 | static struct pack_idx_option pack_idx_opts; |
d01fb92f | 209 | static const char *base_name; |
024701f1 | 210 | static int progress = 1; |
4812a93a | 211 | static int window = 10; |
568508e7 | 212 | static unsigned long pack_size_limit; |
618e613a | 213 | static int depth = 50; |
43cc2b42 | 214 | static int delta_search_threads; |
df6d6101 | 215 | static int pack_to_stdout; |
4f6d26b1 | 216 | static int sparse; |
6a1e32d5 | 217 | static int thin; |
8d1d8f83 | 218 | static int num_preferred_base; |
dc6a0757 | 219 | static struct progress *progress_state; |
c323ac7d | 220 | |
073b40eb TB |
221 | static struct bitmapped_pack *reuse_packfiles; |
222 | static size_t reuse_packfiles_nr; | |
b96289a1 | 223 | static size_t reuse_packfiles_used_nr; |
6b8fda2d | 224 | static uint32_t reuse_packfile_objects; |
bb514de3 | 225 | static struct bitmap *reuse_packfile_bitmap; |
6b8fda2d | 226 | |
645c432d KS |
227 | static int use_bitmap_index_default = 1; |
228 | static int use_bitmap_index = -1; | |
94107413 TB |
229 | static enum { |
230 | NO_PACK_REUSE = 0, | |
231 | SINGLE_PACK_REUSE, | |
af626ac0 | 232 | MULTI_PACK_REUSE, |
94107413 | 233 | } allow_pack_reuse = SINGLE_PACK_REUSE; |
25575015 JK |
234 | static enum { |
235 | WRITE_BITMAP_FALSE = 0, | |
236 | WRITE_BITMAP_QUIET, | |
237 | WRITE_BITMAP_TRUE, | |
238 | } write_bitmap_index; | |
d4316604 | 239 | static uint16_t write_bitmap_options = BITMAP_OPT_HASH_CACHE; |
6b8fda2d | 240 | |
0c16cd49 JT |
241 | static int exclude_promisor_objects; |
242 | ||
28b8a730 JK |
243 | static int use_delta_islands; |
244 | ||
074b2eea | 245 | static unsigned long delta_cache_size = 0; |
9806f5a7 | 246 | static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE; |
e3dfddb3 | 247 | static unsigned long cache_max_small_delta_size = 1000; |
074b2eea | 248 | |
a97773ce BD |
249 | static unsigned long window_memory_limit = 0; |
250 | ||
dd4b732d JT |
251 | static struct string_list uri_protocols = STRING_LIST_INIT_NODUP; |
252 | ||
9535ce73 | 253 | enum missing_action { |
0c16cd49 JT |
254 | MA_ERROR = 0, /* fail if any missing objects are encountered */ |
255 | MA_ALLOW_ANY, /* silently allow ALL missing objects */ | |
256 | MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */ | |
9535ce73 JH |
257 | }; |
258 | static enum missing_action arg_missing_action; | |
259 | static show_object_fn fn_show_object; | |
260 | ||
dd4b732d JT |
261 | struct configured_exclusion { |
262 | struct oidmap_entry e; | |
263 | char *pack_hash_hex; | |
264 | char *uri; | |
265 | }; | |
266 | static struct oidmap configured_exclusions; | |
267 | ||
268 | static struct oidset excluded_by_config; | |
269 | ||
3f9ac8d2 JH |
270 | /* |
271 | * stats | |
272 | */ | |
7cadf491 SP |
273 | static uint32_t written, written_delta; |
274 | static uint32_t reused, reused_delta; | |
3f9ac8d2 | 275 | |
7cc8f971 VM |
276 | /* |
277 | * Indexed commits | |
278 | */ | |
279 | static struct commit **indexed_commits; | |
280 | static unsigned int indexed_commits_nr; | |
281 | static unsigned int indexed_commits_alloc; | |
282 | ||
283 | static void index_commit_for_bitmap(struct commit *commit) | |
284 | { | |
285 | if (indexed_commits_nr >= indexed_commits_alloc) { | |
286 | indexed_commits_alloc = (indexed_commits_alloc + 32) * 2; | |
2756ca43 | 287 | REALLOC_ARRAY(indexed_commits, indexed_commits_alloc); |
7cc8f971 VM |
288 | } |
289 | ||
290 | indexed_commits[indexed_commits_nr++] = commit; | |
291 | } | |
780e6e73 | 292 | |
3613f9b4 | 293 | static void *get_delta(struct object_entry *entry) |
c323ac7d | 294 | { |
3613f9b4 NP |
295 | unsigned long size, base_size, delta_size; |
296 | void *buf, *base_buf, *delta_buf; | |
21666f1a | 297 | enum object_type type; |
c323ac7d | 298 | |
bc726bd0 ÆAB |
299 | buf = repo_read_object_file(the_repository, &entry->idx.oid, &type, |
300 | &size); | |
3613f9b4 | 301 | if (!buf) |
f616db6a | 302 | die(_("unable to read %s"), oid_to_hex(&entry->idx.oid)); |
bc726bd0 ÆAB |
303 | base_buf = repo_read_object_file(the_repository, |
304 | &DELTA(entry)->idx.oid, &type, | |
305 | &base_size); | |
3613f9b4 | 306 | if (!base_buf) |
e6a492b7 | 307 | die("unable to read %s", |
898eba5e | 308 | oid_to_hex(&DELTA(entry)->idx.oid)); |
3613f9b4 | 309 | delta_buf = diff_delta(base_buf, base_size, |
dcde55bc | 310 | buf, size, &delta_size, 0); |
1a07e59c | 311 | /* |
15beaaa3 | 312 | * We successfully computed this delta once but dropped it for |
1a07e59c NTND |
313 | * memory reasons. Something is very wrong if this time we |
314 | * recompute and create a different delta. | |
315 | */ | |
0aca34e8 | 316 | if (!delta_buf || delta_size != DELTA_SIZE(entry)) |
1a07e59c | 317 | BUG("delta size changed"); |
3613f9b4 NP |
318 | free(buf); |
319 | free(base_buf); | |
c323ac7d LT |
320 | return delta_buf; |
321 | } | |
322 | ||
30ebb40a NP |
323 | static unsigned long do_compress(void **pptr, unsigned long size) |
324 | { | |
ef49a7a0 | 325 | git_zstream stream; |
30ebb40a NP |
326 | void *in, *out; |
327 | unsigned long maxsize; | |
328 | ||
55bb5c91 | 329 | git_deflate_init(&stream, pack_compression_level); |
225a6f10 | 330 | maxsize = git_deflate_bound(&stream, size); |
30ebb40a NP |
331 | |
332 | in = *pptr; | |
333 | out = xmalloc(maxsize); | |
334 | *pptr = out; | |
335 | ||
336 | stream.next_in = in; | |
337 | stream.avail_in = size; | |
338 | stream.next_out = out; | |
339 | stream.avail_out = maxsize; | |
55bb5c91 | 340 | while (git_deflate(&stream, Z_FINISH) == Z_OK) |
30ebb40a | 341 | ; /* nothing */ |
55bb5c91 | 342 | git_deflate_end(&stream); |
30ebb40a NP |
343 | |
344 | free(in); | |
345 | return stream.total_out; | |
346 | } | |
347 | ||
98a3beab | 348 | static unsigned long write_large_blob_data(struct git_istream *st, struct hashfile *f, |
188960b4 | 349 | const struct object_id *oid) |
cf2ba13a NTND |
350 | { |
351 | git_zstream stream; | |
352 | unsigned char ibuf[1024 * 16]; | |
353 | unsigned char obuf[1024 * 16]; | |
354 | unsigned long olen = 0; | |
355 | ||
cf2ba13a NTND |
356 | git_deflate_init(&stream, pack_compression_level); |
357 | ||
358 | for (;;) { | |
359 | ssize_t readlen; | |
360 | int zret = Z_OK; | |
361 | readlen = read_istream(st, ibuf, sizeof(ibuf)); | |
362 | if (readlen == -1) | |
188960b4 | 363 | die(_("unable to read %s"), oid_to_hex(oid)); |
cf2ba13a NTND |
364 | |
365 | stream.next_in = ibuf; | |
366 | stream.avail_in = readlen; | |
367 | while ((stream.avail_in || readlen == 0) && | |
368 | (zret == Z_OK || zret == Z_BUF_ERROR)) { | |
369 | stream.next_out = obuf; | |
370 | stream.avail_out = sizeof(obuf); | |
371 | zret = git_deflate(&stream, readlen ? 0 : Z_FINISH); | |
98a3beab | 372 | hashwrite(f, obuf, stream.next_out - obuf); |
cf2ba13a NTND |
373 | olen += stream.next_out - obuf; |
374 | } | |
375 | if (stream.avail_in) | |
376 | die(_("deflate error (%d)"), zret); | |
377 | if (readlen == 0) { | |
378 | if (zret != Z_STREAM_END) | |
379 | die(_("deflate error (%d)"), zret); | |
380 | break; | |
381 | } | |
382 | } | |
383 | git_deflate_end(&stream); | |
384 | return olen; | |
385 | } | |
386 | ||
780e6e73 NP |
387 | /* |
388 | * we are going to reuse the existing object data as is. make | |
389 | * sure it is not corrupt. | |
390 | */ | |
079afb18 SP |
391 | static int check_pack_inflate(struct packed_git *p, |
392 | struct pack_window **w_curs, | |
6777a59f SP |
393 | off_t offset, |
394 | off_t len, | |
079afb18 SP |
395 | unsigned long expect) |
396 | { | |
ef49a7a0 | 397 | git_zstream stream; |
079afb18 SP |
398 | unsigned char fakebuf[4096], *in; |
399 | int st; | |
400 | ||
401 | memset(&stream, 0, sizeof(stream)); | |
39c68542 | 402 | git_inflate_init(&stream); |
079afb18 SP |
403 | do { |
404 | in = use_pack(p, w_curs, offset, &stream.avail_in); | |
405 | stream.next_in = in; | |
406 | stream.next_out = fakebuf; | |
407 | stream.avail_out = sizeof(fakebuf); | |
39c68542 | 408 | st = git_inflate(&stream, Z_FINISH); |
079afb18 SP |
409 | offset += stream.next_in - in; |
410 | } while (st == Z_OK || st == Z_BUF_ERROR); | |
39c68542 | 411 | git_inflate_end(&stream); |
079afb18 SP |
412 | return (st == Z_STREAM_END && |
413 | stream.total_out == expect && | |
414 | stream.total_in == len) ? 0 : -1; | |
415 | } | |
416 | ||
98a3beab | 417 | static void copy_pack_data(struct hashfile *f, |
079afb18 SP |
418 | struct packed_git *p, |
419 | struct pack_window **w_curs, | |
6777a59f SP |
420 | off_t offset, |
421 | off_t len) | |
079afb18 SP |
422 | { |
423 | unsigned char *in; | |
ef49a7a0 | 424 | unsigned long avail; |
079afb18 SP |
425 | |
426 | while (len) { | |
427 | in = use_pack(p, w_curs, offset, &avail); | |
428 | if (avail > len) | |
ef49a7a0 | 429 | avail = (unsigned long)len; |
98a3beab | 430 | hashwrite(f, in, avail); |
079afb18 SP |
431 | offset += avail; |
432 | len -= avail; | |
433 | } | |
434 | } | |
435 | ||
7d089fb9 ÆAB |
436 | static inline int oe_size_greater_than(struct packing_data *pack, |
437 | const struct object_entry *lhs, | |
438 | unsigned long rhs) | |
439 | { | |
440 | if (lhs->size_valid) | |
441 | return lhs->size_ > rhs; | |
442 | if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */ | |
443 | return 1; | |
444 | return oe_get_size_slow(pack, lhs) > rhs; | |
445 | } | |
446 | ||
1b4bb16b | 447 | /* Return 0 if we will bust the pack-size limit */ |
98a3beab | 448 | static unsigned long write_no_reuse_object(struct hashfile *f, struct object_entry *entry, |
c9018b03 | 449 | unsigned long limit, int usable_delta) |
c323ac7d | 450 | { |
c9018b03 | 451 | unsigned long size, datalen; |
2c5e2865 JK |
452 | unsigned char header[MAX_PACK_OBJECT_HEADER], |
453 | dheader[MAX_PACK_OBJECT_HEADER]; | |
6777a59f | 454 | unsigned hdrlen; |
2c5ef824 | 455 | enum object_type type; |
c9018b03 | 456 | void *buf; |
cf2ba13a | 457 | struct git_istream *st = NULL; |
41179100 | 458 | const unsigned hashsz = the_hash_algo->rawsz; |
c9018b03 NTND |
459 | |
460 | if (!usable_delta) { | |
fd9b1bae | 461 | if (oe_type(entry) == OBJ_BLOB && |
ac77d0c3 | 462 | oe_size_greater_than(&to_pack, entry, big_file_threshold) && |
c8123e72 MT |
463 | (st = open_istream(the_repository, &entry->idx.oid, &type, |
464 | &size, NULL)) != NULL) | |
cf2ba13a NTND |
465 | buf = NULL; |
466 | else { | |
bc726bd0 ÆAB |
467 | buf = repo_read_object_file(the_repository, |
468 | &entry->idx.oid, &type, | |
469 | &size); | |
cf2ba13a | 470 | if (!buf) |
e6a492b7 | 471 | die(_("unable to read %s"), |
472 | oid_to_hex(&entry->idx.oid)); | |
cf2ba13a | 473 | } |
c9018b03 NTND |
474 | /* |
475 | * make sure no cached delta data remains from a | |
476 | * previous attempt before a pack split occurred. | |
477 | */ | |
6a83d902 | 478 | FREE_AND_NULL(entry->delta_data); |
c9018b03 NTND |
479 | entry->z_delta_size = 0; |
480 | } else if (entry->delta_data) { | |
0aca34e8 | 481 | size = DELTA_SIZE(entry); |
c9018b03 NTND |
482 | buf = entry->delta_data; |
483 | entry->delta_data = NULL; | |
898eba5e | 484 | type = (allow_ofs_delta && DELTA(entry)->idx.offset) ? |
c9018b03 NTND |
485 | OBJ_OFS_DELTA : OBJ_REF_DELTA; |
486 | } else { | |
487 | buf = get_delta(entry); | |
0aca34e8 | 488 | size = DELTA_SIZE(entry); |
898eba5e | 489 | type = (allow_ofs_delta && DELTA(entry)->idx.offset) ? |
c9018b03 NTND |
490 | OBJ_OFS_DELTA : OBJ_REF_DELTA; |
491 | } | |
492 | ||
cf2ba13a NTND |
493 | if (st) /* large blob case, just assume we don't compress well */ |
494 | datalen = size; | |
495 | else if (entry->z_delta_size) | |
c9018b03 NTND |
496 | datalen = entry->z_delta_size; |
497 | else | |
498 | datalen = do_compress(&buf, size); | |
499 | ||
500 | /* | |
501 | * The object header is a byte of 'type' followed by zero or | |
502 | * more bytes of length. | |
503 | */ | |
7202a6fa JK |
504 | hdrlen = encode_in_pack_object_header(header, sizeof(header), |
505 | type, size); | |
c9018b03 NTND |
506 | |
507 | if (type == OBJ_OFS_DELTA) { | |
508 | /* | |
509 | * Deltas with relative base contain an additional | |
510 | * encoding of the relative offset for the delta | |
511 | * base from this object's position in the pack. | |
512 | */ | |
898eba5e | 513 | off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset; |
c9018b03 NTND |
514 | unsigned pos = sizeof(dheader) - 1; |
515 | dheader[pos] = ofs & 127; | |
516 | while (ofs >>= 7) | |
517 | dheader[--pos] = 128 | (--ofs & 127); | |
41179100 | 518 | if (limit && hdrlen + sizeof(dheader) - pos + datalen + hashsz >= limit) { |
cf2ba13a NTND |
519 | if (st) |
520 | close_istream(st); | |
c9018b03 NTND |
521 | free(buf); |
522 | return 0; | |
523 | } | |
98a3beab | 524 | hashwrite(f, header, hdrlen); |
525 | hashwrite(f, dheader + pos, sizeof(dheader) - pos); | |
c9018b03 NTND |
526 | hdrlen += sizeof(dheader) - pos; |
527 | } else if (type == OBJ_REF_DELTA) { | |
528 | /* | |
529 | * Deltas with a base reference contain | |
41179100 | 530 | * additional bytes for the base object ID. |
c9018b03 | 531 | */ |
41179100 | 532 | if (limit && hdrlen + hashsz + datalen + hashsz >= limit) { |
cf2ba13a NTND |
533 | if (st) |
534 | close_istream(st); | |
c9018b03 NTND |
535 | free(buf); |
536 | return 0; | |
537 | } | |
98a3beab | 538 | hashwrite(f, header, hdrlen); |
42c8ce1c | 539 | hashwrite(f, DELTA(entry)->idx.oid.hash, hashsz); |
41179100 | 540 | hdrlen += hashsz; |
c9018b03 | 541 | } else { |
41179100 | 542 | if (limit && hdrlen + datalen + hashsz >= limit) { |
cf2ba13a NTND |
543 | if (st) |
544 | close_istream(st); | |
c9018b03 NTND |
545 | free(buf); |
546 | return 0; | |
547 | } | |
98a3beab | 548 | hashwrite(f, header, hdrlen); |
c9018b03 | 549 | } |
cf2ba13a | 550 | if (st) { |
188960b4 | 551 | datalen = write_large_blob_data(st, f, &entry->idx.oid); |
cf2ba13a NTND |
552 | close_istream(st); |
553 | } else { | |
98a3beab | 554 | hashwrite(f, buf, datalen); |
cf2ba13a NTND |
555 | free(buf); |
556 | } | |
c9018b03 NTND |
557 | |
558 | return hdrlen + datalen; | |
559 | } | |
560 | ||
561 | /* Return 0 if we will bust the pack-size limit */ | |
98a3beab | 562 | static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry, |
af92a645 | 563 | unsigned long limit, int usable_delta) |
c9018b03 | 564 | { |
43fa44fa | 565 | struct packed_git *p = IN_PACK(entry); |
c9018b03 | 566 | struct pack_window *w_curs = NULL; |
952fc687 | 567 | uint32_t pos; |
c9018b03 | 568 | off_t offset; |
fd9b1bae | 569 | enum object_type type = oe_type(entry); |
211c61c6 | 570 | off_t datalen; |
2c5e2865 JK |
571 | unsigned char header[MAX_PACK_OBJECT_HEADER], |
572 | dheader[MAX_PACK_OBJECT_HEADER]; | |
c9018b03 | 573 | unsigned hdrlen; |
41179100 | 574 | const unsigned hashsz = the_hash_algo->rawsz; |
ac77d0c3 | 575 | unsigned long entry_size = SIZE(entry); |
c9018b03 | 576 | |
898eba5e NTND |
577 | if (DELTA(entry)) |
578 | type = (allow_ofs_delta && DELTA(entry)->idx.offset) ? | |
c9018b03 | 579 | OBJ_OFS_DELTA : OBJ_REF_DELTA; |
7202a6fa | 580 | hdrlen = encode_in_pack_object_header(header, sizeof(header), |
ac77d0c3 | 581 | type, entry_size); |
c9018b03 NTND |
582 | |
583 | offset = entry->in_pack_offset; | |
952fc687 TB |
584 | if (offset_to_pack_pos(p, offset, &pos) < 0) |
585 | die(_("write_reuse_object: could not locate %s, expected at " | |
586 | "offset %"PRIuMAX" in pack %s"), | |
587 | oid_to_hex(&entry->idx.oid), (uintmax_t)offset, | |
588 | p->pack_name); | |
589 | datalen = pack_pos_to_offset(p, pos + 1) - offset; | |
c9018b03 | 590 | if (!pack_to_stdout && p->index_version > 1 && |
952fc687 TB |
591 | check_pack_crc(p, &w_curs, offset, datalen, |
592 | pack_pos_to_index(p, pos))) { | |
f616db6a | 593 | error(_("bad packed object CRC for %s"), |
e6a492b7 | 594 | oid_to_hex(&entry->idx.oid)); |
c9018b03 NTND |
595 | unuse_pack(&w_curs); |
596 | return write_no_reuse_object(f, entry, limit, usable_delta); | |
597 | } | |
598 | ||
599 | offset += entry->in_pack_header_size; | |
600 | datalen -= entry->in_pack_header_size; | |
601 | ||
602 | if (!pack_to_stdout && p->index_version == 1 && | |
ac77d0c3 | 603 | check_pack_inflate(p, &w_curs, offset, datalen, entry_size)) { |
f616db6a | 604 | error(_("corrupt packed object for %s"), |
e6a492b7 | 605 | oid_to_hex(&entry->idx.oid)); |
c9018b03 NTND |
606 | unuse_pack(&w_curs); |
607 | return write_no_reuse_object(f, entry, limit, usable_delta); | |
608 | } | |
609 | ||
610 | if (type == OBJ_OFS_DELTA) { | |
898eba5e | 611 | off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset; |
c9018b03 NTND |
612 | unsigned pos = sizeof(dheader) - 1; |
613 | dheader[pos] = ofs & 127; | |
614 | while (ofs >>= 7) | |
615 | dheader[--pos] = 128 | (--ofs & 127); | |
41179100 | 616 | if (limit && hdrlen + sizeof(dheader) - pos + datalen + hashsz >= limit) { |
c9018b03 NTND |
617 | unuse_pack(&w_curs); |
618 | return 0; | |
619 | } | |
98a3beab | 620 | hashwrite(f, header, hdrlen); |
621 | hashwrite(f, dheader + pos, sizeof(dheader) - pos); | |
c9018b03 NTND |
622 | hdrlen += sizeof(dheader) - pos; |
623 | reused_delta++; | |
624 | } else if (type == OBJ_REF_DELTA) { | |
41179100 | 625 | if (limit && hdrlen + hashsz + datalen + hashsz >= limit) { |
c9018b03 NTND |
626 | unuse_pack(&w_curs); |
627 | return 0; | |
628 | } | |
98a3beab | 629 | hashwrite(f, header, hdrlen); |
42c8ce1c | 630 | hashwrite(f, DELTA(entry)->idx.oid.hash, hashsz); |
41179100 | 631 | hdrlen += hashsz; |
c9018b03 NTND |
632 | reused_delta++; |
633 | } else { | |
41179100 | 634 | if (limit && hdrlen + datalen + hashsz >= limit) { |
c9018b03 NTND |
635 | unuse_pack(&w_curs); |
636 | return 0; | |
637 | } | |
98a3beab | 638 | hashwrite(f, header, hdrlen); |
c9018b03 NTND |
639 | } |
640 | copy_pack_data(f, p, &w_curs, offset, datalen); | |
641 | unuse_pack(&w_curs); | |
642 | reused++; | |
643 | return hdrlen + datalen; | |
644 | } | |
645 | ||
646 | /* Return 0 if we will bust the pack-size limit */ | |
98a3beab | 647 | static off_t write_object(struct hashfile *f, |
af92a645 NTND |
648 | struct object_entry *entry, |
649 | off_t write_offset) | |
c9018b03 | 650 | { |
af92a645 NTND |
651 | unsigned long limit; |
652 | off_t len; | |
2c5ef824 | 653 | int usable_delta, to_reuse; |
c323ac7d | 654 | |
78d1e84f NP |
655 | if (!pack_to_stdout) |
656 | crc32_begin(f); | |
657 | ||
a2430dde | 658 | /* apply size limit if limited packsize and not first object */ |
a1e4760f NP |
659 | if (!pack_size_limit || !nr_written) |
660 | limit = 0; | |
661 | else if (pack_size_limit <= write_offset) | |
662 | /* | |
663 | * the earlier object did not fit the limit; avoid | |
664 | * mistaking this with unlimited (i.e. limit = 0). | |
665 | */ | |
666 | limit = 1; | |
667 | else | |
668 | limit = pack_size_limit - write_offset; | |
2c5ef824 | 669 | |
898eba5e | 670 | if (!DELTA(entry)) |
2c5ef824 NP |
671 | usable_delta = 0; /* no delta */ |
672 | else if (!pack_size_limit) | |
673 | usable_delta = 1; /* unlimited packfile */ | |
898eba5e | 674 | else if (DELTA(entry)->idx.offset == (off_t)-1) |
2c5ef824 | 675 | usable_delta = 0; /* base was written to another pack */ |
898eba5e | 676 | else if (DELTA(entry)->idx.offset) |
2c5ef824 NP |
677 | usable_delta = 1; /* base already exists in this pack */ |
678 | else | |
679 | usable_delta = 0; /* base could end up in another pack */ | |
680 | ||
a7de7130 | 681 | if (!reuse_object) |
fa736f72 | 682 | to_reuse = 0; /* explicit */ |
43fa44fa | 683 | else if (!IN_PACK(entry)) |
ab7cd7bb | 684 | to_reuse = 0; /* can't reuse what we don't have */ |
fd9b1bae NTND |
685 | else if (oe_type(entry) == OBJ_REF_DELTA || |
686 | oe_type(entry) == OBJ_OFS_DELTA) | |
17b08f2c DH |
687 | /* check_object() decided it for us ... */ |
688 | to_reuse = usable_delta; | |
689 | /* ... but pack split may override that */ | |
fd9b1bae | 690 | else if (oe_type(entry) != entry->in_pack_type) |
ab7cd7bb | 691 | to_reuse = 0; /* pack has delta which is unusable */ |
898eba5e | 692 | else if (DELTA(entry)) |
ab7cd7bb JH |
693 | to_reuse = 0; /* we want to pack afresh */ |
694 | else | |
695 | to_reuse = 1; /* we have it in-pack undeltified, | |
696 | * and we do not need to deltify it. | |
697 | */ | |
698 | ||
c9018b03 NTND |
699 | if (!to_reuse) |
700 | len = write_no_reuse_object(f, entry, limit, usable_delta); | |
701 | else | |
702 | len = write_reuse_object(f, entry, limit, usable_delta); | |
703 | if (!len) | |
704 | return 0; | |
64bd76b1 | 705 | |
17b08f2c | 706 | if (usable_delta) |
ab7cd7bb | 707 | written_delta++; |
3f9ac8d2 | 708 | written++; |
78d1e84f | 709 | if (!pack_to_stdout) |
aa7e44bf | 710 | entry->idx.crc32 = crc32_end(f); |
c9018b03 | 711 | return len; |
c323ac7d LT |
712 | } |
713 | ||
f63c79db JH |
714 | enum write_one_status { |
715 | WRITE_ONE_SKIP = -1, /* already written */ | |
716 | WRITE_ONE_BREAK = 0, /* writing this will bust the limit; not written */ | |
717 | WRITE_ONE_WRITTEN = 1, /* normal */ | |
718 | WRITE_ONE_RECURSIVE = 2 /* already scheduled to be written */ | |
719 | }; | |
720 | ||
98a3beab | 721 | static enum write_one_status write_one(struct hashfile *f, |
f63c79db JH |
722 | struct object_entry *e, |
723 | off_t *offset) | |
9d5ab962 | 724 | { |
af92a645 | 725 | off_t size; |
f63c79db | 726 | int recursing; |
d7dd0223 | 727 | |
f63c79db JH |
728 | /* |
729 | * we set offset to 1 (which is an impossible value) to mark | |
730 | * the fact that this object is involved in "write its base | |
731 | * first before writing a deltified object" recursion. | |
732 | */ | |
733 | recursing = (e->idx.offset == 1); | |
734 | if (recursing) { | |
f616db6a | 735 | warning(_("recursive delta detected for object %s"), |
e6a492b7 | 736 | oid_to_hex(&e->idx.oid)); |
f63c79db JH |
737 | return WRITE_ONE_RECURSIVE; |
738 | } else if (e->idx.offset || e->preferred_base) { | |
739 | /* offset is non zero if object is written already. */ | |
740 | return WRITE_ONE_SKIP; | |
741 | } | |
d7dd0223 | 742 | |
720c9f7b | 743 | /* if we are deltified, write out base object first. */ |
898eba5e | 744 | if (DELTA(e)) { |
f63c79db | 745 | e->idx.offset = 1; /* now recurse */ |
898eba5e | 746 | switch (write_one(f, DELTA(e), offset)) { |
f63c79db JH |
747 | case WRITE_ONE_RECURSIVE: |
748 | /* we cannot depend on this one */ | |
898eba5e | 749 | SET_DELTA(e, NULL); |
f63c79db JH |
750 | break; |
751 | default: | |
752 | break; | |
753 | case WRITE_ONE_BREAK: | |
754 | e->idx.offset = recursing; | |
755 | return WRITE_ONE_BREAK; | |
756 | } | |
757 | } | |
d7dd0223 | 758 | |
6ed7f25e NP |
759 | e->idx.offset = *offset; |
760 | size = write_object(f, e, *offset); | |
17b08f2c | 761 | if (!size) { |
f63c79db JH |
762 | e->idx.offset = recursing; |
763 | return WRITE_ONE_BREAK; | |
17b08f2c | 764 | } |
79814f42 | 765 | written_list[nr_written++] = &e->idx; |
d7dd0223 NP |
766 | |
767 | /* make sure off_t is sufficiently large not to wrap */ | |
c03c8315 | 768 | if (signed_add_overflows(*offset, size)) |
f616db6a | 769 | die(_("pack too large for current definition of off_t")); |
6ed7f25e | 770 | *offset += size; |
f63c79db | 771 | return WRITE_ONE_WRITTEN; |
9d5ab962 JH |
772 | } |
773 | ||
5cf88fd8 ÆAB |
774 | static int mark_tagged(const char *path UNUSED, const struct object_id *oid, |
775 | int flag UNUSED, void *cb_data UNUSED) | |
1b4bb16b | 776 | { |
188960b4 | 777 | struct object_id peeled; |
3a37876b | 778 | struct object_entry *entry = packlist_find(&to_pack, oid); |
1b4bb16b JH |
779 | |
780 | if (entry) | |
781 | entry->tagged = 1; | |
36a31792 | 782 | if (!peel_iterated_oid(oid, &peeled)) { |
3a37876b | 783 | entry = packlist_find(&to_pack, &peeled); |
1b4bb16b JH |
784 | if (entry) |
785 | entry->tagged = 1; | |
786 | } | |
787 | return 0; | |
788 | } | |
789 | ||
7d089fb9 ÆAB |
790 | static inline unsigned char oe_layer(struct packing_data *pack, |
791 | struct object_entry *e) | |
792 | { | |
793 | if (!pack->layer) | |
794 | return 0; | |
795 | return pack->layer[e - pack->objects]; | |
796 | } | |
797 | ||
be126818 | 798 | static inline void add_to_write_order(struct object_entry **wo, |
92bef1a1 | 799 | unsigned int *endp, |
1b4bb16b JH |
800 | struct object_entry *e) |
801 | { | |
fe0ac2fb | 802 | if (e->filled || oe_layer(&to_pack, e) != write_layer) |
1b4bb16b JH |
803 | return; |
804 | wo[(*endp)++] = e; | |
805 | e->filled = 1; | |
806 | } | |
807 | ||
808 | static void add_descendants_to_write_order(struct object_entry **wo, | |
92bef1a1 | 809 | unsigned int *endp, |
1b4bb16b JH |
810 | struct object_entry *e) |
811 | { | |
f380872f DM |
812 | int add_to_order = 1; |
813 | while (e) { | |
814 | if (add_to_order) { | |
815 | struct object_entry *s; | |
816 | /* add this node... */ | |
817 | add_to_write_order(wo, endp, e); | |
818 | /* all its siblings... */ | |
898eba5e | 819 | for (s = DELTA_SIBLING(e); s; s = DELTA_SIBLING(s)) { |
f380872f DM |
820 | add_to_write_order(wo, endp, s); |
821 | } | |
822 | } | |
823 | /* drop down a level to add left subtree nodes if possible */ | |
898eba5e | 824 | if (DELTA_CHILD(e)) { |
f380872f | 825 | add_to_order = 1; |
898eba5e | 826 | e = DELTA_CHILD(e); |
f380872f DM |
827 | } else { |
828 | add_to_order = 0; | |
829 | /* our sibling might have some children, it is next */ | |
898eba5e NTND |
830 | if (DELTA_SIBLING(e)) { |
831 | e = DELTA_SIBLING(e); | |
f380872f DM |
832 | continue; |
833 | } | |
834 | /* go back to our parent node */ | |
898eba5e NTND |
835 | e = DELTA(e); |
836 | while (e && !DELTA_SIBLING(e)) { | |
f380872f DM |
837 | /* we're on the right side of a subtree, keep |
838 | * going up until we can go right again */ | |
898eba5e | 839 | e = DELTA(e); |
f380872f DM |
840 | } |
841 | if (!e) { | |
842 | /* done- we hit our original root node */ | |
843 | return; | |
844 | } | |
845 | /* pass it off to sibling at this level */ | |
898eba5e | 846 | e = DELTA_SIBLING(e); |
f380872f DM |
847 | } |
848 | }; | |
1b4bb16b JH |
849 | } |
850 | ||
851 | static void add_family_to_write_order(struct object_entry **wo, | |
92bef1a1 | 852 | unsigned int *endp, |
1b4bb16b JH |
853 | struct object_entry *e) |
854 | { | |
855 | struct object_entry *root; | |
856 | ||
898eba5e | 857 | for (root = e; DELTA(root); root = DELTA(root)) |
1b4bb16b | 858 | ; /* nothing */ |
1b4bb16b JH |
859 | add_descendants_to_write_order(wo, endp, root); |
860 | } | |
861 | ||
f64ba53e | 862 | static void compute_layer_order(struct object_entry **wo, unsigned int *wo_end) |
1b4bb16b | 863 | { |
f64ba53e | 864 | unsigned int i, last_untagged; |
2834bc27 | 865 | struct object_entry *objects = to_pack.objects; |
1b4bb16b | 866 | |
2834bc27 | 867 | for (i = 0; i < to_pack.nr_objects; i++) { |
1b4bb16b JH |
868 | if (objects[i].tagged) |
869 | break; | |
f64ba53e | 870 | add_to_write_order(wo, wo_end, &objects[i]); |
1b4bb16b | 871 | } |
38d4debb | 872 | last_untagged = i; |
1b4bb16b JH |
873 | |
874 | /* | |
875 | * Then fill all the tagged tips. | |
876 | */ | |
2834bc27 | 877 | for (; i < to_pack.nr_objects; i++) { |
1b4bb16b | 878 | if (objects[i].tagged) |
f64ba53e | 879 | add_to_write_order(wo, wo_end, &objects[i]); |
1b4bb16b JH |
880 | } |
881 | ||
882 | /* | |
883 | * And then all remaining commits and tags. | |
884 | */ | |
2834bc27 | 885 | for (i = last_untagged; i < to_pack.nr_objects; i++) { |
fd9b1bae NTND |
886 | if (oe_type(&objects[i]) != OBJ_COMMIT && |
887 | oe_type(&objects[i]) != OBJ_TAG) | |
1b4bb16b | 888 | continue; |
f64ba53e | 889 | add_to_write_order(wo, wo_end, &objects[i]); |
1b4bb16b JH |
890 | } |
891 | ||
892 | /* | |
893 | * And then all the trees. | |
894 | */ | |
2834bc27 | 895 | for (i = last_untagged; i < to_pack.nr_objects; i++) { |
fd9b1bae | 896 | if (oe_type(&objects[i]) != OBJ_TREE) |
1b4bb16b | 897 | continue; |
f64ba53e | 898 | add_to_write_order(wo, wo_end, &objects[i]); |
1b4bb16b JH |
899 | } |
900 | ||
901 | /* | |
902 | * Finally all the rest in really tight order | |
903 | */ | |
2834bc27 | 904 | for (i = last_untagged; i < to_pack.nr_objects; i++) { |
fe0ac2fb | 905 | if (!objects[i].filled && oe_layer(&to_pack, &objects[i]) == write_layer) |
f64ba53e | 906 | add_family_to_write_order(wo, wo_end, &objects[i]); |
38d4debb | 907 | } |
f64ba53e CC |
908 | } |
909 | ||
910 | static struct object_entry **compute_write_order(void) | |
911 | { | |
28b8a730 | 912 | uint32_t max_layers = 1; |
f64ba53e CC |
913 | unsigned int i, wo_end; |
914 | ||
915 | struct object_entry **wo; | |
916 | struct object_entry *objects = to_pack.objects; | |
917 | ||
918 | for (i = 0; i < to_pack.nr_objects; i++) { | |
919 | objects[i].tagged = 0; | |
920 | objects[i].filled = 0; | |
921 | SET_DELTA_CHILD(&objects[i], NULL); | |
922 | SET_DELTA_SIBLING(&objects[i], NULL); | |
923 | } | |
924 | ||
925 | /* | |
926 | * Fully connect delta_child/delta_sibling network. | |
927 | * Make sure delta_sibling is sorted in the original | |
928 | * recency order. | |
929 | */ | |
930 | for (i = to_pack.nr_objects; i > 0;) { | |
931 | struct object_entry *e = &objects[--i]; | |
932 | if (!DELTA(e)) | |
933 | continue; | |
934 | /* Mark me as the first child */ | |
935 | e->delta_sibling_idx = DELTA(e)->delta_child_idx; | |
936 | SET_DELTA_CHILD(DELTA(e), e); | |
38d4debb DM |
937 | } |
938 | ||
f64ba53e CC |
939 | /* |
940 | * Mark objects that are at the tip of tags. | |
941 | */ | |
942 | for_each_tag_ref(mark_tagged, NULL); | |
943 | ||
647982bb | 944 | if (use_delta_islands) { |
28b8a730 | 945 | max_layers = compute_pack_layers(&to_pack); |
647982bb EW |
946 | free_island_marks(); |
947 | } | |
28b8a730 | 948 | |
f64ba53e CC |
949 | ALLOC_ARRAY(wo, to_pack.nr_objects); |
950 | wo_end = 0; | |
951 | ||
28b8a730 JK |
952 | for (; write_layer < max_layers; ++write_layer) |
953 | compute_layer_order(wo, &wo_end); | |
38d4debb | 954 | |
2834bc27 | 955 | if (wo_end != to_pack.nr_objects) |
f616db6a NTND |
956 | die(_("ordered %u objects, expected %"PRIu32), |
957 | wo_end, to_pack.nr_objects); | |
1b4bb16b JH |
958 | |
959 | return wo; | |
960 | } | |
961 | ||
bb514de3 JK |
962 | |
963 | /* | |
964 | * A reused set of objects. All objects in a chunk have the same | |
965 | * relative position in the original packfile and the generated | |
966 | * packfile. | |
967 | */ | |
968 | ||
969 | static struct reused_chunk { | |
970 | /* The offset of the first object of this chunk in the original | |
971 | * packfile. */ | |
972 | off_t original; | |
bf12013f HX |
973 | /* The difference for "original" minus the offset of the first object of |
974 | * this chunk in the generated packfile. */ | |
bb514de3 JK |
975 | off_t difference; |
976 | } *reused_chunks; | |
977 | static int reused_chunks_nr; | |
978 | static int reused_chunks_alloc; | |
979 | ||
980 | static void record_reused_object(off_t where, off_t offset) | |
981 | { | |
982 | if (reused_chunks_nr && reused_chunks[reused_chunks_nr-1].difference == offset) | |
983 | return; | |
984 | ||
985 | ALLOC_GROW(reused_chunks, reused_chunks_nr + 1, | |
986 | reused_chunks_alloc); | |
987 | reused_chunks[reused_chunks_nr].original = where; | |
988 | reused_chunks[reused_chunks_nr].difference = offset; | |
989 | reused_chunks_nr++; | |
990 | } | |
991 | ||
992 | /* | |
993 | * Binary search to find the chunk that "where" is in. Note | |
994 | * that we're not looking for an exact match, just the first | |
995 | * chunk that contains it (which implicitly ends at the start | |
996 | * of the next chunk. | |
997 | */ | |
998 | static off_t find_reused_offset(off_t where) | |
6b8fda2d | 999 | { |
bb514de3 JK |
1000 | int lo = 0, hi = reused_chunks_nr; |
1001 | while (lo < hi) { | |
1002 | int mi = lo + ((hi - lo) / 2); | |
1003 | if (where == reused_chunks[mi].original) | |
1004 | return reused_chunks[mi].difference; | |
1005 | if (where < reused_chunks[mi].original) | |
1006 | hi = mi; | |
1007 | else | |
1008 | lo = mi + 1; | |
1009 | } | |
6b8fda2d | 1010 | |
bb514de3 JK |
1011 | /* |
1012 | * The first chunk starts at zero, so we can't have gone below | |
1013 | * there. | |
1014 | */ | |
1015 | assert(lo); | |
1016 | return reused_chunks[lo-1].difference; | |
1017 | } | |
6b8fda2d | 1018 | |
5e29c3f7 TB |
1019 | static void write_reused_pack_one(struct packed_git *reuse_packfile, |
1020 | size_t pos, struct hashfile *out, | |
d1d701eb | 1021 | off_t pack_start, |
bb514de3 | 1022 | struct pack_window **w_curs) |
6b8fda2d | 1023 | { |
bb514de3 JK |
1024 | off_t offset, next, cur; |
1025 | enum object_type type; | |
1026 | unsigned long size; | |
6b8fda2d | 1027 | |
66cbd3e2 TB |
1028 | offset = pack_pos_to_offset(reuse_packfile, pos); |
1029 | next = pack_pos_to_offset(reuse_packfile, pos + 1); | |
6b8fda2d | 1030 | |
d1d701eb TB |
1031 | record_reused_object(offset, |
1032 | offset - (hashfile_total(out) - pack_start)); | |
6b8fda2d | 1033 | |
bb514de3 JK |
1034 | cur = offset; |
1035 | type = unpack_object_header(reuse_packfile, w_curs, &cur, &size); | |
1036 | assert(type >= 0); | |
6b8fda2d | 1037 | |
bb514de3 JK |
1038 | if (type == OBJ_OFS_DELTA) { |
1039 | off_t base_offset; | |
1040 | off_t fixup; | |
1041 | ||
1042 | unsigned char header[MAX_PACK_OBJECT_HEADER]; | |
1043 | unsigned len; | |
1044 | ||
1045 | base_offset = get_delta_base(reuse_packfile, w_curs, &cur, type, offset); | |
1046 | assert(base_offset != 0); | |
1047 | ||
1048 | /* Convert to REF_DELTA if we must... */ | |
1049 | if (!allow_ofs_delta) { | |
66cbd3e2 | 1050 | uint32_t base_pos; |
f66d4e02 JK |
1051 | struct object_id base_oid; |
1052 | ||
66cbd3e2 TB |
1053 | if (offset_to_pack_pos(reuse_packfile, base_offset, &base_pos) < 0) |
1054 | die(_("expected object at offset %"PRIuMAX" " | |
1055 | "in pack %s"), | |
1056 | (uintmax_t)base_offset, | |
1057 | reuse_packfile->pack_name); | |
1058 | ||
f66d4e02 | 1059 | nth_packed_object_id(&base_oid, reuse_packfile, |
66cbd3e2 | 1060 | pack_pos_to_index(reuse_packfile, base_pos)); |
bb514de3 JK |
1061 | |
1062 | len = encode_in_pack_object_header(header, sizeof(header), | |
1063 | OBJ_REF_DELTA, size); | |
1064 | hashwrite(out, header, len); | |
f8cb64e3 | 1065 | hashwrite(out, base_oid.hash, the_hash_algo->rawsz); |
bb514de3 JK |
1066 | copy_pack_data(out, reuse_packfile, w_curs, cur, next - cur); |
1067 | return; | |
1068 | } | |
6b8fda2d | 1069 | |
bb514de3 JK |
1070 | /* Otherwise see if we need to rewrite the offset... */ |
1071 | fixup = find_reused_offset(offset) - | |
1072 | find_reused_offset(base_offset); | |
1073 | if (fixup) { | |
1074 | unsigned char ofs_header[10]; | |
1075 | unsigned i, ofs_len; | |
1076 | off_t ofs = offset - base_offset - fixup; | |
6b8fda2d | 1077 | |
bb514de3 JK |
1078 | len = encode_in_pack_object_header(header, sizeof(header), |
1079 | OBJ_OFS_DELTA, size); | |
6b8fda2d | 1080 | |
bb514de3 JK |
1081 | i = sizeof(ofs_header) - 1; |
1082 | ofs_header[i] = ofs & 127; | |
1083 | while (ofs >>= 7) | |
1084 | ofs_header[--i] = 128 | (--ofs & 127); | |
6b8fda2d | 1085 | |
bb514de3 | 1086 | ofs_len = sizeof(ofs_header) - i; |
6b8fda2d | 1087 | |
bb514de3 JK |
1088 | hashwrite(out, header, len); |
1089 | hashwrite(out, ofs_header + sizeof(ofs_header) - ofs_len, ofs_len); | |
1090 | copy_pack_data(out, reuse_packfile, w_curs, cur, next - cur); | |
1091 | return; | |
1092 | } | |
1093 | ||
1094 | /* ...otherwise we have no fixup, and can write it verbatim */ | |
1095 | } | |
1096 | ||
1097 | copy_pack_data(out, reuse_packfile, w_curs, offset, next - offset); | |
1098 | } | |
1099 | ||
073b40eb | 1100 | static size_t write_reused_pack_verbatim(struct bitmapped_pack *reuse_packfile, |
5e29c3f7 | 1101 | struct hashfile *out, |
ca0fd69e | 1102 | off_t pack_start, |
bb514de3 JK |
1103 | struct pack_window **w_curs) |
1104 | { | |
ca0fd69e TB |
1105 | size_t pos = reuse_packfile->bitmap_pos; |
1106 | size_t end; | |
bb514de3 | 1107 | |
ca0fd69e TB |
1108 | if (pos % BITS_IN_EWORD) { |
1109 | size_t word_pos = (pos / BITS_IN_EWORD); | |
1110 | size_t offset = pos % BITS_IN_EWORD; | |
1111 | size_t last; | |
1112 | eword_t word = reuse_packfile_bitmap->words[word_pos]; | |
bb514de3 | 1113 | |
ca0fd69e TB |
1114 | if (offset + reuse_packfile->bitmap_nr < BITS_IN_EWORD) |
1115 | last = offset + reuse_packfile->bitmap_nr; | |
1116 | else | |
1117 | last = BITS_IN_EWORD; | |
1118 | ||
1119 | for (; offset < last; offset++) { | |
1120 | if (word >> offset == 0) | |
1121 | return word_pos; | |
1122 | if (!bitmap_get(reuse_packfile_bitmap, | |
1123 | word_pos * BITS_IN_EWORD + offset)) | |
1124 | return word_pos; | |
1125 | } | |
bb514de3 | 1126 | |
ca0fd69e TB |
1127 | pos += BITS_IN_EWORD - (pos % BITS_IN_EWORD); |
1128 | } | |
1129 | ||
1130 | /* | |
1131 | * Now we're going to copy as many whole eword_t's as possible. | |
1132 | * "end" is the index of the last whole eword_t we copy, but | |
1133 | * there may be additional bits to process. Those are handled | |
1134 | * individually by write_reused_pack(). | |
1135 | * | |
1136 | * Begin by advancing to the first word boundary in range of the | |
1137 | * bit positions occupied by objects in "reuse_packfile". Then | |
1138 | * pick the last word boundary in the same range. If we have at | |
1139 | * least one word's worth of bits to process, continue on. | |
1140 | */ | |
1141 | end = reuse_packfile->bitmap_pos + reuse_packfile->bitmap_nr; | |
1142 | if (end % BITS_IN_EWORD) | |
1143 | end -= end % BITS_IN_EWORD; | |
1144 | if (pos >= end) | |
1145 | return reuse_packfile->bitmap_pos / BITS_IN_EWORD; | |
bb514de3 | 1146 | |
ca0fd69e TB |
1147 | while (pos < end && |
1148 | reuse_packfile_bitmap->words[pos / BITS_IN_EWORD] == (eword_t)~0) | |
1149 | pos += BITS_IN_EWORD; | |
bb514de3 | 1150 | |
ca0fd69e TB |
1151 | if (pos > end) |
1152 | pos = end; | |
1153 | ||
1154 | if (reuse_packfile->bitmap_pos < pos) { | |
1155 | off_t pack_start_off = pack_pos_to_offset(reuse_packfile->p, 0); | |
1156 | off_t pack_end_off = pack_pos_to_offset(reuse_packfile->p, | |
1157 | pos - reuse_packfile->bitmap_pos); | |
bb514de3 | 1158 | |
ca0fd69e | 1159 | written += pos - reuse_packfile->bitmap_pos; |
bb514de3 JK |
1160 | |
1161 | /* We're recording one chunk, not one object. */ | |
ca0fd69e TB |
1162 | record_reused_object(pack_start_off, |
1163 | pack_start_off - (hashfile_total(out) - pack_start)); | |
bb514de3 | 1164 | hashflush(out); |
073b40eb | 1165 | copy_pack_data(out, reuse_packfile->p, w_curs, |
ca0fd69e | 1166 | pack_start_off, pack_end_off - pack_start_off); |
657673f1 | 1167 | |
657673f1 | 1168 | display_progress(progress_state, written); |
6b8fda2d | 1169 | } |
ca0fd69e TB |
1170 | if (pos % BITS_IN_EWORD) |
1171 | BUG("attempted to jump past a word boundary to %"PRIuMAX, | |
1172 | (uintmax_t)pos); | |
1173 | return pos / BITS_IN_EWORD; | |
bb514de3 JK |
1174 | } |
1175 | ||
073b40eb | 1176 | static void write_reused_pack(struct bitmapped_pack *reuse_packfile, |
5e29c3f7 | 1177 | struct hashfile *f) |
bb514de3 | 1178 | { |
48051257 | 1179 | size_t i = reuse_packfile->bitmap_pos / BITS_IN_EWORD; |
bb514de3 | 1180 | uint32_t offset; |
d1d701eb | 1181 | off_t pack_start = hashfile_total(f) - sizeof(struct pack_header); |
bb514de3 JK |
1182 | struct pack_window *w_curs = NULL; |
1183 | ||
1184 | if (allow_ofs_delta) | |
d1d701eb TB |
1185 | i = write_reused_pack_verbatim(reuse_packfile, f, pack_start, |
1186 | &w_curs); | |
bb514de3 JK |
1187 | |
1188 | for (; i < reuse_packfile_bitmap->word_alloc; ++i) { | |
1189 | eword_t word = reuse_packfile_bitmap->words[i]; | |
1190 | size_t pos = (i * BITS_IN_EWORD); | |
6b8fda2d | 1191 | |
bb514de3 JK |
1192 | for (offset = 0; offset < BITS_IN_EWORD; ++offset) { |
1193 | if ((word >> offset) == 0) | |
1194 | break; | |
1195 | ||
1196 | offset += ewah_bit_ctz64(word >> offset); | |
48051257 TB |
1197 | if (pos + offset < reuse_packfile->bitmap_pos) |
1198 | continue; | |
1199 | if (pos + offset >= reuse_packfile->bitmap_pos + reuse_packfile->bitmap_nr) | |
1200 | goto done; | |
0f533c72 TB |
1201 | /* |
1202 | * Can use bit positions directly, even for MIDX | |
1203 | * bitmaps. See comment in try_partial_reuse() | |
1204 | * for why. | |
1205 | */ | |
48051257 TB |
1206 | write_reused_pack_one(reuse_packfile->p, |
1207 | pos + offset - reuse_packfile->bitmap_pos, | |
073b40eb | 1208 | f, pack_start, &w_curs); |
bb514de3 JK |
1209 | display_progress(progress_state, ++written); |
1210 | } | |
1211 | } | |
6b8fda2d | 1212 | |
48051257 | 1213 | done: |
bb514de3 | 1214 | unuse_pack(&w_curs); |
6b8fda2d VM |
1215 | } |
1216 | ||
dd4b732d JT |
1217 | static void write_excluded_by_configs(void) |
1218 | { | |
1219 | struct oidset_iter iter; | |
1220 | const struct object_id *oid; | |
1221 | ||
1222 | oidset_iter_init(&excluded_by_config, &iter); | |
1223 | while ((oid = oidset_iter_next(&iter))) { | |
1224 | struct configured_exclusion *ex = | |
1225 | oidmap_get(&configured_exclusions, oid); | |
1226 | ||
1227 | if (!ex) | |
1228 | BUG("configured exclusion wasn't configured"); | |
1229 | write_in_full(1, ex->pack_hash_hex, strlen(ex->pack_hash_hex)); | |
1230 | write_in_full(1, " ", 1); | |
1231 | write_in_full(1, ex->uri, strlen(ex->uri)); | |
1232 | write_in_full(1, "\n", 1); | |
1233 | } | |
1234 | } | |
1235 | ||
9cea46cd EW |
1236 | static const char no_split_warning[] = N_( |
1237 | "disabling bitmap writing, packs are split due to pack.packSizeLimit" | |
1238 | ); | |
1239 | ||
d01fb92f | 1240 | static void write_pack_file(void) |
c323ac7d | 1241 | { |
ebe27b13 | 1242 | uint32_t i = 0, j; |
98a3beab | 1243 | struct hashfile *f; |
6ed7f25e | 1244 | off_t offset; |
ebe27b13 | 1245 | uint32_t nr_remaining = nr_result; |
f746bae8 | 1246 | time_t last_mtime = 0; |
1b4bb16b | 1247 | struct object_entry **write_order; |
81a216a5 | 1248 | |
bcd7954e | 1249 | if (progress > pack_to_stdout) |
754dbc43 | 1250 | progress_state = start_progress(_("Writing objects"), nr_result); |
b32fa95f | 1251 | ALLOC_ARRAY(written_list, to_pack.nr_objects); |
1b4bb16b | 1252 | write_order = compute_write_order(); |
183bdb2c | 1253 | |
ebe27b13 | 1254 | do { |
71b7672b | 1255 | unsigned char hash[GIT_MAX_RAWSZ]; |
7ba502c4 | 1256 | char *pack_tmp_name = NULL; |
aa7e44bf | 1257 | |
cdf9db3c | 1258 | if (pack_to_stdout) |
98a3beab | 1259 | f = hashfd_throughput(1, "<stdout>", progress_state); |
cdf9db3c JH |
1260 | else |
1261 | f = create_tmp_packfile(&pack_tmp_name); | |
ebe27b13 | 1262 | |
c0ad4657 | 1263 | offset = write_pack_header(f, nr_remaining); |
6b8fda2d | 1264 | |
073b40eb | 1265 | if (reuse_packfiles_nr) { |
6b8fda2d | 1266 | assert(pack_to_stdout); |
073b40eb TB |
1267 | for (j = 0; j < reuse_packfiles_nr; j++) { |
1268 | reused_chunks_nr = 0; | |
1269 | write_reused_pack(&reuse_packfiles[j], f); | |
b96289a1 TB |
1270 | if (reused_chunks_nr) |
1271 | reuse_packfiles_used_nr++; | |
073b40eb | 1272 | } |
bb514de3 | 1273 | offset = hashfile_total(f); |
6b8fda2d VM |
1274 | } |
1275 | ||
ebe27b13 | 1276 | nr_written = 0; |
2834bc27 | 1277 | for (; i < to_pack.nr_objects; i++) { |
1b4bb16b | 1278 | struct object_entry *e = write_order[i]; |
cddec4f8 | 1279 | if (write_one(f, e, &offset) == WRITE_ONE_BREAK) |
720c9f7b NP |
1280 | break; |
1281 | display_progress(progress_state, written); | |
1282 | } | |
c553ca25 | 1283 | |
54352bb2 | 1284 | if (pack_to_stdout) { |
020406ea NS |
1285 | /* |
1286 | * We never fsync when writing to stdout since we may | |
1287 | * not be writing to an actual pack file. For instance, | |
1288 | * the upload-pack code passes a pipe here. Calling | |
1289 | * fsync on a pipe results in unnecessary | |
1290 | * synchronization with the reader on some platforms. | |
1291 | */ | |
1292 | finalize_hashfile(f, hash, FSYNC_COMPONENT_NONE, | |
1293 | CSUM_HASH_IN_STREAM | CSUM_CLOSE); | |
54352bb2 | 1294 | } else if (nr_written == nr_remaining) { |
020406ea NS |
1295 | finalize_hashfile(f, hash, FSYNC_COMPONENT_PACK, |
1296 | CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE); | |
ebe27b13 | 1297 | } else { |
020406ea NS |
1298 | /* |
1299 | * If we wrote the wrong number of entries in the | |
1300 | * header, rewrite it like in fast-import. | |
1301 | */ | |
1302 | ||
1303 | int fd = finalize_hashfile(f, hash, FSYNC_COMPONENT_PACK, 0); | |
71b7672b | 1304 | fixup_pack_header_footer(fd, hash, pack_tmp_name, |
1305 | nr_written, hash, offset); | |
7ba502c4 | 1306 | close(fd); |
9cea46cd | 1307 | if (write_bitmap_index) { |
25575015 JK |
1308 | if (write_bitmap_index != WRITE_BITMAP_QUIET) |
1309 | warning(_(no_split_warning)); | |
9cea46cd EW |
1310 | write_bitmap_index = 0; |
1311 | } | |
ebe27b13 DH |
1312 | } |
1313 | ||
1314 | if (!pack_to_stdout) { | |
f746bae8 | 1315 | struct stat st; |
58892711 | 1316 | struct strbuf tmpname = STRBUF_INIT; |
2ec02dd5 | 1317 | char *idx_tmp_name = NULL; |
d01fb92f | 1318 | |
f746bae8 NP |
1319 | /* |
1320 | * Packs are runtime accessed in their mtime | |
1321 | * order since newer packs are more likely to contain | |
1322 | * younger objects. So if we are creating multiple | |
1323 | * packs then we should modify the mtime of later ones | |
1324 | * to preserve this property. | |
1325 | */ | |
0e990530 | 1326 | if (stat(pack_tmp_name, &st) < 0) { |
f616db6a | 1327 | warning_errno(_("failed to stat %s"), pack_tmp_name); |
f746bae8 NP |
1328 | } else if (!last_mtime) { |
1329 | last_mtime = st.st_mtime; | |
1330 | } else { | |
1331 | struct utimbuf utb; | |
1332 | utb.actime = st.st_atime; | |
1333 | utb.modtime = --last_mtime; | |
0e990530 | 1334 | if (utime(pack_tmp_name, &utb) < 0) |
f616db6a | 1335 | warning_errno(_("failed utime() on %s"), pack_tmp_name); |
f746bae8 NP |
1336 | } |
1337 | ||
66833f0e ÆAB |
1338 | strbuf_addf(&tmpname, "%s-%s.", base_name, |
1339 | hash_to_hex(hash)); | |
7cc8f971 VM |
1340 | |
1341 | if (write_bitmap_index) { | |
71b7672b | 1342 | bitmap_writer_set_checksum(hash); |
06af3bba NTND |
1343 | bitmap_writer_build_type_index( |
1344 | &to_pack, written_list, nr_written); | |
7cc8f971 VM |
1345 | } |
1346 | ||
b7573536 TB |
1347 | if (cruft) |
1348 | pack_idx_opts.flags |= WRITE_MTIMES; | |
1349 | ||
2ec02dd5 | 1350 | stage_tmp_packfiles(&tmpname, pack_tmp_name, |
0e990530 | 1351 | written_list, nr_written, |
1c573cdd TB |
1352 | &to_pack, &pack_idx_opts, hash, |
1353 | &idx_tmp_name); | |
7cc8f971 VM |
1354 | |
1355 | if (write_bitmap_index) { | |
66833f0e | 1356 | size_t tmpname_len = tmpname.len; |
7cc8f971 | 1357 | |
66833f0e | 1358 | strbuf_addstr(&tmpname, "bitmap"); |
7cc8f971 VM |
1359 | stop_progress(&progress_state); |
1360 | ||
1361 | bitmap_writer_show_progress(progress); | |
7cc8f971 | 1362 | bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1); |
3ba3d062 TB |
1363 | if (bitmap_writer_build(&to_pack) < 0) |
1364 | die(_("failed to write bitmap index")); | |
ae4f07fb | 1365 | bitmap_writer_finish(written_list, nr_written, |
58892711 | 1366 | tmpname.buf, write_bitmap_options); |
7cc8f971 | 1367 | write_bitmap_index = 0; |
66833f0e | 1368 | strbuf_setlen(&tmpname, tmpname_len); |
7cc8f971 VM |
1369 | } |
1370 | ||
4bc1fd6e ÆAB |
1371 | rename_tmp_packfile_idx(&tmpname, &idx_tmp_name); |
1372 | ||
2ec02dd5 | 1373 | free(idx_tmp_name); |
58892711 | 1374 | strbuf_release(&tmpname); |
7ba502c4 | 1375 | free(pack_tmp_name); |
71b7672b | 1376 | puts(hash_to_hex(hash)); |
ebe27b13 DH |
1377 | } |
1378 | ||
1379 | /* mark written objects as written to previous pack */ | |
1380 | for (j = 0; j < nr_written; j++) { | |
79814f42 | 1381 | written_list[j]->offset = (off_t)-1; |
ebe27b13 DH |
1382 | } |
1383 | nr_remaining -= nr_written; | |
2834bc27 | 1384 | } while (nr_remaining && i < to_pack.nr_objects); |
ebe27b13 DH |
1385 | |
1386 | free(written_list); | |
1b4bb16b | 1387 | free(write_order); |
4d4fcc54 | 1388 | stop_progress(&progress_state); |
67c08ce1 | 1389 | if (written != nr_result) |
f616db6a NTND |
1390 | die(_("wrote %"PRIu32" objects while expecting %"PRIu32), |
1391 | written, nr_result); | |
9ed87902 JT |
1392 | trace2_data_intmax("pack-objects", the_repository, |
1393 | "write_pack_file/wrote", nr_result); | |
c323ac7d LT |
1394 | } |
1395 | ||
a74db82e JH |
1396 | static int no_try_delta(const char *path) |
1397 | { | |
2aef63d3 | 1398 | static struct attr_check *check; |
a74db82e | 1399 | |
2aef63d3 JH |
1400 | if (!check) |
1401 | check = attr_check_initl("delta", NULL); | |
44451a2e | 1402 | git_check_attr(the_repository->index, path, check); |
2aef63d3 | 1403 | if (ATTR_FALSE(check->items[0].value)) |
a74db82e JH |
1404 | return 1; |
1405 | return 0; | |
1406 | } | |
1407 | ||
ce2bc424 JK |
1408 | /* |
1409 | * When adding an object, check whether we have already added it | |
1410 | * to our packing list. If so, we can skip. However, if we are | |
1411 | * being asked to excludei t, but the previous mention was to include | |
1412 | * it, make sure to adjust its flags and tweak our numbers accordingly. | |
1413 | * | |
1414 | * As an optimization, we pass out the index position where we would have | |
1415 | * found the item, since that saves us from having to look it up again a | |
1416 | * few lines later when we want to add the new entry. | |
1417 | */ | |
188960b4 | 1418 | static int have_duplicate_entry(const struct object_id *oid, |
3a37876b | 1419 | int exclude) |
c323ac7d | 1420 | { |
c323ac7d | 1421 | struct object_entry *entry; |
29b734e4 | 1422 | |
92fb0db9 JK |
1423 | if (reuse_packfile_bitmap && |
1424 | bitmap_walk_contains(bitmap_git, reuse_packfile_bitmap, oid)) | |
1425 | return 1; | |
1426 | ||
3a37876b | 1427 | entry = packlist_find(&to_pack, oid); |
ce2bc424 | 1428 | if (!entry) |
29b734e4 | 1429 | return 0; |
ce2bc424 JK |
1430 | |
1431 | if (exclude) { | |
1432 | if (!entry->preferred_base) | |
1433 | nr_result--; | |
1434 | entry->preferred_base = 1; | |
29b734e4 | 1435 | } |
c323ac7d | 1436 | |
ce2bc424 JK |
1437 | return 1; |
1438 | } | |
1439 | ||
6325da14 JK |
1440 | static int want_found_object(const struct object_id *oid, int exclude, |
1441 | struct packed_git *p) | |
702d1b95 KS |
1442 | { |
1443 | if (exclude) | |
1444 | return 1; | |
1445 | if (incremental) | |
1446 | return 0; | |
1447 | ||
4090511e TB |
1448 | if (!is_pack_valid(p)) |
1449 | return -1; | |
1450 | ||
702d1b95 KS |
1451 | /* |
1452 | * When asked to do --local (do not include an object that appears in a | |
1453 | * pack we borrow from elsewhere) or --honor-pack-keep (do not include | |
1454 | * an object that appears in a pack marked with .keep), finding a pack | |
1455 | * that matches the criteria is sufficient for us to decide to omit it. | |
1456 | * However, even if this pack does not satisfy the criteria, we need to | |
1457 | * make sure no copy of this object appears in _any_ pack that makes us | |
1458 | * to omit the object, so we need to check all the packs. | |
1459 | * | |
6325da14 | 1460 | * We can however first check whether these options can possibly matter; |
702d1b95 KS |
1461 | * if they do not matter we know we want the object in generated pack. |
1462 | * Otherwise, we signal "-1" at the end to tell the caller that we do | |
1463 | * not know either way, and it needs to check more packs. | |
1464 | */ | |
702d1b95 | 1465 | |
6325da14 JK |
1466 | /* |
1467 | * Objects in packs borrowed from elsewhere are discarded regardless of | |
1468 | * if they appear in other packs that weren't borrowed. | |
1469 | */ | |
702d1b95 KS |
1470 | if (local && !p->pack_local) |
1471 | return 0; | |
6325da14 JK |
1472 | |
1473 | /* | |
1474 | * Then handle .keep first, as we have a fast(er) path there. | |
1475 | */ | |
1476 | if (ignore_packed_keep_on_disk || ignore_packed_keep_in_core) { | |
1477 | /* | |
1478 | * Set the flags for the kept-pack cache to be the ones we want | |
1479 | * to ignore. | |
1480 | * | |
1481 | * That is, if we are ignoring objects in on-disk keep packs, | |
1482 | * then we want to search through the on-disk keep and ignore | |
1483 | * the in-core ones. | |
1484 | */ | |
1485 | unsigned flags = 0; | |
1486 | if (ignore_packed_keep_on_disk) | |
1487 | flags |= ON_DISK_KEEP_PACKS; | |
1488 | if (ignore_packed_keep_in_core) | |
1489 | flags |= IN_CORE_KEEP_PACKS; | |
1490 | ||
1491 | if (ignore_packed_keep_on_disk && p->pack_keep) | |
1492 | return 0; | |
1493 | if (ignore_packed_keep_in_core && p->pack_keep_in_core) | |
1494 | return 0; | |
1495 | if (has_object_kept_pack(oid, flags)) | |
1496 | return 0; | |
1497 | } | |
1498 | ||
1499 | /* | |
1500 | * At this point we know definitively that either we don't care about | |
1501 | * keep-packs, or the object is not in one. Keep checking other | |
1502 | * conditions... | |
1503 | */ | |
1504 | if (!local || !have_non_local_packs) | |
1505 | return 1; | |
702d1b95 KS |
1506 | |
1507 | /* we don't know yet; keep looking for more packs */ | |
1508 | return -1; | |
1509 | } | |
1510 | ||
6325da14 JK |
1511 | static int want_object_in_pack_one(struct packed_git *p, |
1512 | const struct object_id *oid, | |
1513 | int exclude, | |
1514 | struct packed_git **found_pack, | |
1515 | off_t *found_offset) | |
1516 | { | |
1517 | off_t offset; | |
1518 | ||
1519 | if (p == *found_pack) | |
1520 | offset = *found_offset; | |
1521 | else | |
1522 | offset = find_pack_entry_one(oid->hash, p); | |
1523 | ||
1524 | if (offset) { | |
1525 | if (!*found_pack) { | |
1526 | if (!is_pack_valid(p)) | |
1527 | return -1; | |
1528 | *found_offset = offset; | |
1529 | *found_pack = p; | |
1530 | } | |
1531 | return want_found_object(oid, exclude, p); | |
1532 | } | |
1533 | return -1; | |
1534 | } | |
1535 | ||
ce2bc424 JK |
1536 | /* |
1537 | * Check whether we want the object in the pack (e.g., we do not want | |
1538 | * objects found in non-local stores if the "--local" option was used). | |
1539 | * | |
702d1b95 KS |
1540 | * If the caller already knows an existing pack it wants to take the object |
1541 | * from, that is passed in *found_pack and *found_offset; otherwise this | |
1542 | * function finds if there is any pack that has the object and returns the pack | |
1543 | * and its offset in these variables. | |
ce2bc424 | 1544 | */ |
188960b4 | 1545 | static int want_object_in_pack(const struct object_id *oid, |
ce2bc424 JK |
1546 | int exclude, |
1547 | struct packed_git **found_pack, | |
1548 | off_t *found_offset) | |
1549 | { | |
702d1b95 | 1550 | int want; |
8865859d | 1551 | struct list_head *pos; |
6a22d521 | 1552 | struct multi_pack_index *m; |
ce2bc424 | 1553 | |
6862ebbf | 1554 | if (!exclude && local && has_loose_object_nonlocal(oid)) |
daae0625 BC |
1555 | return 0; |
1556 | ||
702d1b95 KS |
1557 | /* |
1558 | * If we already know the pack object lives in, start checks from that | |
1559 | * pack - in the usual case when neither --local was given nor .keep files | |
1560 | * are present we will determine the answer right now. | |
1561 | */ | |
1562 | if (*found_pack) { | |
6325da14 | 1563 | want = want_found_object(oid, exclude, *found_pack); |
702d1b95 KS |
1564 | if (want != -1) |
1565 | return want; | |
4090511e TB |
1566 | |
1567 | *found_pack = NULL; | |
1568 | *found_offset = 0; | |
702d1b95 | 1569 | } |
6a22d521 DS |
1570 | |
1571 | for (m = get_multi_pack_index(the_repository); m; m = m->next) { | |
1572 | struct pack_entry e; | |
64404a24 | 1573 | if (fill_midx_entry(the_repository, oid, &e, m)) { |
6325da14 JK |
1574 | want = want_object_in_pack_one(e.p, oid, exclude, found_pack, found_offset); |
1575 | if (want != -1) | |
1576 | return want; | |
6a22d521 DS |
1577 | } |
1578 | } | |
1579 | ||
a80d72db | 1580 | list_for_each(pos, get_packed_git_mru(the_repository)) { |
ec2dd32c | 1581 | struct packed_git *p = list_entry(pos, struct packed_git, mru); |
6325da14 JK |
1582 | want = want_object_in_pack_one(p, oid, exclude, found_pack, found_offset); |
1583 | if (!exclude && want > 0) | |
1584 | list_move(&p->mru, | |
1585 | get_packed_git_mru(the_repository)); | |
1586 | if (want != -1) | |
1587 | return want; | |
64560374 | 1588 | } |
eb019375 | 1589 | |
dd4b732d JT |
1590 | if (uri_protocols.nr) { |
1591 | struct configured_exclusion *ex = | |
1592 | oidmap_get(&configured_exclusions, oid); | |
1593 | int i; | |
1594 | const char *p; | |
1595 | ||
1596 | if (ex) { | |
1597 | for (i = 0; i < uri_protocols.nr; i++) { | |
1598 | if (skip_prefix(ex->uri, | |
1599 | uri_protocols.items[i].string, | |
1600 | &p) && | |
1601 | *p == ':') { | |
1602 | oidset_insert(&excluded_by_config, oid); | |
1603 | return 0; | |
1604 | } | |
1605 | } | |
1606 | } | |
1607 | } | |
1608 | ||
ce2bc424 JK |
1609 | return 1; |
1610 | } | |
1611 | ||
fa23090b TB |
1612 | static struct object_entry *create_object_entry(const struct object_id *oid, |
1613 | enum object_type type, | |
1614 | uint32_t hash, | |
1615 | int exclude, | |
1616 | int no_try_delta, | |
1617 | struct packed_git *found_pack, | |
1618 | off_t found_offset) | |
ce2bc424 JK |
1619 | { |
1620 | struct object_entry *entry; | |
29b734e4 | 1621 | |
3a37876b | 1622 | entry = packlist_alloc(&to_pack, oid); |
27225f2e | 1623 | entry->hash = hash; |
fd9b1bae | 1624 | oe_set_type(entry, type); |
29b734e4 NP |
1625 | if (exclude) |
1626 | entry->preferred_base = 1; | |
81a216a5 NP |
1627 | else |
1628 | nr_result++; | |
29b734e4 | 1629 | if (found_pack) { |
43fa44fa | 1630 | oe_set_in_pack(&to_pack, entry, found_pack); |
29b734e4 NP |
1631 | entry->in_pack_offset = found_offset; |
1632 | } | |
7a979d99 | 1633 | |
ce2bc424 | 1634 | entry->no_try_delta = no_try_delta; |
fa23090b TB |
1635 | |
1636 | return entry; | |
ce2bc424 | 1637 | } |
29b734e4 | 1638 | |
373c67da JK |
1639 | static const char no_closure_warning[] = N_( |
1640 | "disabling bitmap writing, as some objects are not being packed" | |
1641 | ); | |
1642 | ||
188960b4 | 1643 | static int add_object_entry(const struct object_id *oid, enum object_type type, |
ce2bc424 JK |
1644 | const char *name, int exclude) |
1645 | { | |
702d1b95 KS |
1646 | struct packed_git *found_pack = NULL; |
1647 | off_t found_offset = 0; | |
7a979d99 | 1648 | |
5af05043 NTND |
1649 | display_progress(progress_state, ++nr_seen); |
1650 | ||
3a37876b | 1651 | if (have_duplicate_entry(oid, exclude)) |
ce2bc424 | 1652 | return 0; |
a74db82e | 1653 | |
188960b4 | 1654 | if (!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) { |
373c67da JK |
1655 | /* The pack is missing an object, so it will not have closure */ |
1656 | if (write_bitmap_index) { | |
25575015 JK |
1657 | if (write_bitmap_index != WRITE_BITMAP_QUIET) |
1658 | warning(_(no_closure_warning)); | |
373c67da JK |
1659 | write_bitmap_index = 0; |
1660 | } | |
ce2bc424 | 1661 | return 0; |
373c67da | 1662 | } |
29b734e4 | 1663 | |
188960b4 | 1664 | create_object_entry(oid, type, pack_name_hash(name), |
ce2bc424 | 1665 | exclude, name && no_try_delta(name), |
3a37876b | 1666 | found_pack, found_offset); |
29b734e4 | 1667 | return 1; |
c323ac7d LT |
1668 | } |
1669 | ||
20664967 | 1670 | static int add_object_entry_from_bitmap(const struct object_id *oid, |
6b8fda2d | 1671 | enum object_type type, |
c50dca2a | 1672 | int flags UNUSED, uint32_t name_hash, |
6b8fda2d VM |
1673 | struct packed_git *pack, off_t offset) |
1674 | { | |
5af05043 NTND |
1675 | display_progress(progress_state, ++nr_seen); |
1676 | ||
3a37876b | 1677 | if (have_duplicate_entry(oid, 0)) |
6b8fda2d VM |
1678 | return 0; |
1679 | ||
188960b4 | 1680 | if (!want_object_in_pack(oid, 0, &pack, &offset)) |
702d1b95 KS |
1681 | return 0; |
1682 | ||
3a37876b | 1683 | create_object_entry(oid, type, name_hash, 0, 0, pack, offset); |
29b734e4 | 1684 | return 1; |
c323ac7d LT |
1685 | } |
1686 | ||
5379a5c5 | 1687 | struct pbase_tree_cache { |
188960b4 | 1688 | struct object_id oid; |
5379a5c5 JH |
1689 | int ref; |
1690 | int temporary; | |
1691 | void *tree_data; | |
1692 | unsigned long tree_size; | |
1693 | }; | |
1694 | ||
1695 | static struct pbase_tree_cache *(pbase_tree_cache[256]); | |
188960b4 | 1696 | static int pbase_tree_cache_ix(const struct object_id *oid) |
5379a5c5 | 1697 | { |
188960b4 | 1698 | return oid->hash[0] % ARRAY_SIZE(pbase_tree_cache); |
5379a5c5 JH |
1699 | } |
1700 | static int pbase_tree_cache_ix_incr(int ix) | |
1701 | { | |
1702 | return (ix+1) % ARRAY_SIZE(pbase_tree_cache); | |
1703 | } | |
1704 | ||
1705 | static struct pbase_tree { | |
1706 | struct pbase_tree *next; | |
1707 | /* This is a phony "cache" entry; we are not | |
01689909 | 1708 | * going to evict it or find it through _get() |
5379a5c5 JH |
1709 | * mechanism -- this is for the toplevel node that |
1710 | * would almost always change with any commit. | |
1711 | */ | |
1712 | struct pbase_tree_cache pcache; | |
1713 | } *pbase_tree; | |
1714 | ||
188960b4 | 1715 | static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid) |
5379a5c5 JH |
1716 | { |
1717 | struct pbase_tree_cache *ent, *nent; | |
1718 | void *data; | |
1719 | unsigned long size; | |
21666f1a | 1720 | enum object_type type; |
5379a5c5 | 1721 | int neigh; |
188960b4 | 1722 | int my_ix = pbase_tree_cache_ix(oid); |
5379a5c5 JH |
1723 | int available_ix = -1; |
1724 | ||
1725 | /* pbase-tree-cache acts as a limited hashtable. | |
1726 | * your object will be found at your index or within a few | |
1727 | * slots after that slot if it is cached. | |
1728 | */ | |
1729 | for (neigh = 0; neigh < 8; neigh++) { | |
1730 | ent = pbase_tree_cache[my_ix]; | |
4a7e27e9 | 1731 | if (ent && oideq(&ent->oid, oid)) { |
5379a5c5 JH |
1732 | ent->ref++; |
1733 | return ent; | |
1734 | } | |
1735 | else if (((available_ix < 0) && (!ent || !ent->ref)) || | |
1736 | ((0 <= available_ix) && | |
1737 | (!ent && pbase_tree_cache[available_ix]))) | |
1738 | available_ix = my_ix; | |
1739 | if (!ent) | |
1740 | break; | |
1741 | my_ix = pbase_tree_cache_ix_incr(my_ix); | |
1742 | } | |
1743 | ||
1744 | /* Did not find one. Either we got a bogus request or | |
1745 | * we need to read and perhaps cache. | |
1746 | */ | |
bc726bd0 | 1747 | data = repo_read_object_file(the_repository, oid, &type, &size); |
5379a5c5 JH |
1748 | if (!data) |
1749 | return NULL; | |
21666f1a | 1750 | if (type != OBJ_TREE) { |
5379a5c5 JH |
1751 | free(data); |
1752 | return NULL; | |
1753 | } | |
1754 | ||
1755 | /* We need to either cache or return a throwaway copy */ | |
1756 | ||
1757 | if (available_ix < 0) | |
1758 | ent = NULL; | |
1759 | else { | |
1760 | ent = pbase_tree_cache[available_ix]; | |
1761 | my_ix = available_ix; | |
1762 | } | |
1763 | ||
1764 | if (!ent) { | |
1765 | nent = xmalloc(sizeof(*nent)); | |
1766 | nent->temporary = (available_ix < 0); | |
1767 | } | |
1768 | else { | |
1769 | /* evict and reuse */ | |
1770 | free(ent->tree_data); | |
1771 | nent = ent; | |
1772 | } | |
188960b4 | 1773 | oidcpy(&nent->oid, oid); |
5379a5c5 JH |
1774 | nent->tree_data = data; |
1775 | nent->tree_size = size; | |
1776 | nent->ref = 1; | |
1777 | if (!nent->temporary) | |
1778 | pbase_tree_cache[my_ix] = nent; | |
1779 | return nent; | |
1780 | } | |
1781 | ||
1782 | static void pbase_tree_put(struct pbase_tree_cache *cache) | |
1783 | { | |
1784 | if (!cache->temporary) { | |
1785 | cache->ref--; | |
1786 | return; | |
1787 | } | |
1788 | free(cache->tree_data); | |
1789 | free(cache); | |
1790 | } | |
1791 | ||
e65b868d | 1792 | static size_t name_cmp_len(const char *name) |
5379a5c5 | 1793 | { |
e65b868d | 1794 | return strcspn(name, "\n/"); |
5379a5c5 JH |
1795 | } |
1796 | ||
1797 | static void add_pbase_object(struct tree_desc *tree, | |
5379a5c5 | 1798 | const char *name, |
e65b868d | 1799 | size_t cmplen, |
ce0bd642 | 1800 | const char *fullname) |
3f9ac8d2 | 1801 | { |
4c068a98 | 1802 | struct name_entry entry; |
8a5a8d6c | 1803 | int cmp; |
4c068a98 LT |
1804 | |
1805 | while (tree_entry(tree,&entry)) { | |
1211be6b LT |
1806 | if (S_ISGITLINK(entry.mode)) |
1807 | continue; | |
0de16337 | 1808 | cmp = tree_entry_len(&entry) != cmplen ? 1 : |
8a5a8d6c NP |
1809 | memcmp(name, entry.path, cmplen); |
1810 | if (cmp > 0) | |
7a979d99 | 1811 | continue; |
8a5a8d6c NP |
1812 | if (cmp < 0) |
1813 | return; | |
5379a5c5 | 1814 | if (name[cmplen] != '/') { |
ea82b2a0 | 1815 | add_object_entry(&entry.oid, |
4d1012c3 | 1816 | object_type(entry.mode), |
bc32fed5 | 1817 | fullname, 1); |
5379a5c5 JH |
1818 | return; |
1819 | } | |
8a5a8d6c | 1820 | if (S_ISDIR(entry.mode)) { |
7a979d99 | 1821 | struct tree_desc sub; |
5379a5c5 JH |
1822 | struct pbase_tree_cache *tree; |
1823 | const char *down = name+cmplen+1; | |
e65b868d | 1824 | size_t downlen = name_cmp_len(down); |
5379a5c5 | 1825 | |
ea82b2a0 | 1826 | tree = pbase_tree_get(&entry.oid); |
5379a5c5 JH |
1827 | if (!tree) |
1828 | return; | |
6fda5e51 | 1829 | init_tree_desc(&sub, tree->tree_data, tree->tree_size); |
5379a5c5 | 1830 | |
ce0bd642 | 1831 | add_pbase_object(&sub, down, downlen, fullname); |
5379a5c5 JH |
1832 | pbase_tree_put(tree); |
1833 | } | |
1834 | } | |
1835 | } | |
1d6b38cc | 1836 | |
5379a5c5 JH |
1837 | static unsigned *done_pbase_paths; |
1838 | static int done_pbase_paths_num; | |
1839 | static int done_pbase_paths_alloc; | |
1840 | static int done_pbase_path_pos(unsigned hash) | |
1841 | { | |
1842 | int lo = 0; | |
1843 | int hi = done_pbase_paths_num; | |
1844 | while (lo < hi) { | |
19716b21 | 1845 | int mi = lo + (hi - lo) / 2; |
5379a5c5 JH |
1846 | if (done_pbase_paths[mi] == hash) |
1847 | return mi; | |
1848 | if (done_pbase_paths[mi] < hash) | |
1849 | hi = mi; | |
1850 | else | |
1851 | lo = mi + 1; | |
1852 | } | |
1853 | return -lo-1; | |
1854 | } | |
1855 | ||
1856 | static int check_pbase_path(unsigned hash) | |
1857 | { | |
c7b07805 | 1858 | int pos = done_pbase_path_pos(hash); |
5379a5c5 JH |
1859 | if (0 <= pos) |
1860 | return 1; | |
1861 | pos = -pos - 1; | |
25e19407 DD |
1862 | ALLOC_GROW(done_pbase_paths, |
1863 | done_pbase_paths_num + 1, | |
1864 | done_pbase_paths_alloc); | |
5379a5c5 JH |
1865 | done_pbase_paths_num++; |
1866 | if (pos < done_pbase_paths_num) | |
f331ab9d RS |
1867 | MOVE_ARRAY(done_pbase_paths + pos + 1, done_pbase_paths + pos, |
1868 | done_pbase_paths_num - pos - 1); | |
5379a5c5 JH |
1869 | done_pbase_paths[pos] = hash; |
1870 | return 0; | |
1871 | } | |
1872 | ||
bc32fed5 | 1873 | static void add_preferred_base_object(const char *name) |
5379a5c5 JH |
1874 | { |
1875 | struct pbase_tree *it; | |
e65b868d | 1876 | size_t cmplen; |
68fb36eb | 1877 | unsigned hash = pack_name_hash(name); |
5379a5c5 | 1878 | |
8a5a8d6c | 1879 | if (!num_preferred_base || check_pbase_path(hash)) |
5379a5c5 JH |
1880 | return; |
1881 | ||
8a5a8d6c | 1882 | cmplen = name_cmp_len(name); |
5379a5c5 JH |
1883 | for (it = pbase_tree; it; it = it->next) { |
1884 | if (cmplen == 0) { | |
188960b4 | 1885 | add_object_entry(&it->pcache.oid, OBJ_TREE, NULL, 1); |
5379a5c5 JH |
1886 | } |
1887 | else { | |
1888 | struct tree_desc tree; | |
6fda5e51 | 1889 | init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size); |
ce0bd642 | 1890 | add_pbase_object(&tree, name, cmplen, name); |
7a979d99 | 1891 | } |
3f9ac8d2 | 1892 | } |
3f9ac8d2 JH |
1893 | } |
1894 | ||
188960b4 | 1895 | static void add_preferred_base(struct object_id *oid) |
3f9ac8d2 | 1896 | { |
5379a5c5 JH |
1897 | struct pbase_tree *it; |
1898 | void *data; | |
1899 | unsigned long size; | |
188960b4 | 1900 | struct object_id tree_oid; |
1d6b38cc | 1901 | |
8d1d8f83 JH |
1902 | if (window <= num_preferred_base++) |
1903 | return; | |
1904 | ||
d3b4705a | 1905 | data = read_object_with_reference(the_repository, oid, |
6aea6bae | 1906 | OBJ_TREE, &size, &tree_oid); |
5379a5c5 | 1907 | if (!data) |
7a979d99 | 1908 | return; |
5379a5c5 JH |
1909 | |
1910 | for (it = pbase_tree; it; it = it->next) { | |
4a7e27e9 | 1911 | if (oideq(&it->pcache.oid, &tree_oid)) { |
5379a5c5 JH |
1912 | free(data); |
1913 | return; | |
1914 | } | |
1915 | } | |
1916 | ||
ca56dadb | 1917 | CALLOC_ARRAY(it, 1); |
5379a5c5 JH |
1918 | it->next = pbase_tree; |
1919 | pbase_tree = it; | |
1920 | ||
188960b4 | 1921 | oidcpy(&it->pcache.oid, &tree_oid); |
5379a5c5 JH |
1922 | it->pcache.tree_data = data; |
1923 | it->pcache.tree_size = size; | |
3f9ac8d2 JH |
1924 | } |
1925 | ||
0ef95f72 NP |
1926 | static void cleanup_preferred_base(void) |
1927 | { | |
1928 | struct pbase_tree *it; | |
1929 | unsigned i; | |
1930 | ||
1931 | it = pbase_tree; | |
1932 | pbase_tree = NULL; | |
1933 | while (it) { | |
095b3b2c BW |
1934 | struct pbase_tree *tmp = it; |
1935 | it = tmp->next; | |
1936 | free(tmp->pcache.tree_data); | |
1937 | free(tmp); | |
0ef95f72 NP |
1938 | } |
1939 | ||
1940 | for (i = 0; i < ARRAY_SIZE(pbase_tree_cache); i++) { | |
1941 | if (!pbase_tree_cache[i]) | |
1942 | continue; | |
1943 | free(pbase_tree_cache[i]->tree_data); | |
6a83d902 | 1944 | FREE_AND_NULL(pbase_tree_cache[i]); |
0ef95f72 NP |
1945 | } |
1946 | ||
6a83d902 | 1947 | FREE_AND_NULL(done_pbase_paths); |
0ef95f72 NP |
1948 | done_pbase_paths_num = done_pbase_paths_alloc = 0; |
1949 | } | |
1950 | ||
2fa233a5 JK |
1951 | /* |
1952 | * Return 1 iff the object specified by "delta" can be sent | |
1953 | * literally as a delta against the base in "base_sha1". If | |
1954 | * so, then *base_out will point to the entry in our packing | |
1955 | * list, or NULL if we must use the external-base list. | |
1956 | * | |
1957 | * Depth value does not matter - find_deltas() will | |
1958 | * never consider reused delta as the base object to | |
1959 | * deltify other objects against, in order to avoid | |
1960 | * circular deltas. | |
1961 | */ | |
3f83fd5e | 1962 | static int can_reuse_delta(const struct object_id *base_oid, |
2fa233a5 JK |
1963 | struct object_entry *delta, |
1964 | struct object_entry **base_out) | |
1965 | { | |
1966 | struct object_entry *base; | |
3df28cae | 1967 | |
2fa233a5 JK |
1968 | /* |
1969 | * First see if we're already sending the base (or it's explicitly in | |
1970 | * our "excluded" list). | |
1971 | */ | |
3f83fd5e | 1972 | base = packlist_find(&to_pack, base_oid); |
2fa233a5 JK |
1973 | if (base) { |
1974 | if (!in_same_island(&delta->idx.oid, &base->idx.oid)) | |
1975 | return 0; | |
1976 | *base_out = base; | |
1977 | return 1; | |
1978 | } | |
1979 | ||
1980 | /* | |
1981 | * Otherwise, reachability bitmaps may tell us if the receiver has it, | |
1982 | * even if it was buried too deep in history to make it into the | |
1983 | * packing list. | |
1984 | */ | |
3f83fd5e | 1985 | if (thin && bitmap_has_oid_in_uninteresting(bitmap_git, base_oid)) { |
2fa233a5 | 1986 | if (use_delta_islands) { |
3f83fd5e | 1987 | if (!in_same_island(&delta->idx.oid, base_oid)) |
2fa233a5 JK |
1988 | return 0; |
1989 | } | |
1990 | *base_out = NULL; | |
1991 | return 1; | |
1992 | } | |
1993 | ||
1994 | return 0; | |
1995 | } | |
1996 | ||
e00549aa JT |
1997 | static void prefetch_to_pack(uint32_t object_index_start) { |
1998 | struct oid_array to_fetch = OID_ARRAY_INIT; | |
1999 | uint32_t i; | |
2000 | ||
2001 | for (i = object_index_start; i < to_pack.nr_objects; i++) { | |
2002 | struct object_entry *entry = to_pack.objects + i; | |
2003 | ||
2004 | if (!oid_object_info_extended(the_repository, | |
2005 | &entry->idx.oid, | |
2006 | NULL, | |
2007 | OBJECT_INFO_FOR_PREFETCH)) | |
2008 | continue; | |
2009 | oid_array_append(&to_fetch, &entry->idx.oid); | |
2010 | } | |
2011 | promisor_remote_get_direct(the_repository, | |
2012 | to_fetch.oid, to_fetch.nr); | |
2013 | oid_array_clear(&to_fetch); | |
2014 | } | |
2015 | ||
2016 | static void check_object(struct object_entry *entry, uint32_t object_index) | |
c323ac7d | 2017 | { |
ac77d0c3 | 2018 | unsigned long canonical_size; |
8d5cf957 JT |
2019 | enum object_type type; |
2020 | struct object_info oi = {.typep = &type, .sizep = &canonical_size}; | |
ac77d0c3 | 2021 | |
43fa44fa NTND |
2022 | if (IN_PACK(entry)) { |
2023 | struct packed_git *p = IN_PACK(entry); | |
03e79c88 | 2024 | struct pack_window *w_curs = NULL; |
3f83fd5e JK |
2025 | int have_base = 0; |
2026 | struct object_id base_ref; | |
5c49c116 NP |
2027 | struct object_entry *base_entry; |
2028 | unsigned long used, used_0; | |
ef49a7a0 | 2029 | unsigned long avail; |
5c49c116 NP |
2030 | off_t ofs; |
2031 | unsigned char *buf, c; | |
fd9b1bae | 2032 | enum object_type type; |
27a7d067 | 2033 | unsigned long in_pack_size; |
780e6e73 | 2034 | |
6777a59f | 2035 | buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail); |
ab7cd7bb | 2036 | |
5c49c116 | 2037 | /* |
fa736f72 NP |
2038 | * We want in_pack_type even if we do not reuse delta |
2039 | * since non-delta representations could still be reused. | |
ab7cd7bb | 2040 | */ |
09ded04b | 2041 | used = unpack_object_header_buffer(buf, avail, |
fd9b1bae | 2042 | &type, |
27a7d067 | 2043 | &in_pack_size); |
03d66015 NP |
2044 | if (used == 0) |
2045 | goto give_up; | |
ab7cd7bb | 2046 | |
fd9b1bae NTND |
2047 | if (type < 0) |
2048 | BUG("invalid type %d", type); | |
2049 | entry->in_pack_type = type; | |
2050 | ||
5c49c116 NP |
2051 | /* |
2052 | * Determine if this is a delta and if so whether we can | |
2053 | * reuse it or not. Otherwise let's find out as cheaply as | |
2054 | * possible what the actual type and size for this object is. | |
3f9ac8d2 | 2055 | */ |
5c49c116 NP |
2056 | switch (entry->in_pack_type) { |
2057 | default: | |
2058 | /* Not a delta hence we've already got all we need. */ | |
fd9b1bae | 2059 | oe_set_type(entry, entry->in_pack_type); |
ac77d0c3 | 2060 | SET_SIZE(entry, in_pack_size); |
5c49c116 | 2061 | entry->in_pack_header_size = used; |
fd9b1bae | 2062 | if (oe_type(entry) < OBJ_COMMIT || oe_type(entry) > OBJ_BLOB) |
03d66015 | 2063 | goto give_up; |
5c49c116 NP |
2064 | unuse_pack(&w_curs); |
2065 | return; | |
2066 | case OBJ_REF_DELTA: | |
3f83fd5e JK |
2067 | if (reuse_delta && !entry->preferred_base) { |
2068 | oidread(&base_ref, | |
2069 | use_pack(p, &w_curs, | |
2070 | entry->in_pack_offset + used, | |
2071 | NULL)); | |
2072 | have_base = 1; | |
2073 | } | |
41179100 | 2074 | entry->in_pack_header_size = used + the_hash_algo->rawsz; |
5c49c116 NP |
2075 | break; |
2076 | case OBJ_OFS_DELTA: | |
2077 | buf = use_pack(p, &w_curs, | |
2078 | entry->in_pack_offset + used, NULL); | |
2079 | used_0 = 0; | |
2080 | c = buf[used_0++]; | |
2081 | ofs = c & 127; | |
2082 | while (c & 128) { | |
2083 | ofs += 1; | |
03d66015 | 2084 | if (!ofs || MSB(ofs, 7)) { |
f616db6a | 2085 | error(_("delta base offset overflow in pack for %s"), |
e6a492b7 | 2086 | oid_to_hex(&entry->idx.oid)); |
03d66015 NP |
2087 | goto give_up; |
2088 | } | |
5c49c116 NP |
2089 | c = buf[used_0++]; |
2090 | ofs = (ofs << 7) + (c & 127); | |
780e6e73 | 2091 | } |
5c49c116 | 2092 | ofs = entry->in_pack_offset - ofs; |
03d66015 | 2093 | if (ofs <= 0 || ofs >= entry->in_pack_offset) { |
f616db6a | 2094 | error(_("delta base offset out of bound for %s"), |
e6a492b7 | 2095 | oid_to_hex(&entry->idx.oid)); |
03d66015 NP |
2096 | goto give_up; |
2097 | } | |
a7de7130 | 2098 | if (reuse_delta && !entry->preferred_base) { |
eb3fd99e TB |
2099 | uint32_t pos; |
2100 | if (offset_to_pack_pos(p, ofs, &pos) < 0) | |
08698b1e | 2101 | goto give_up; |
eb3fd99e TB |
2102 | if (!nth_packed_object_id(&base_ref, p, |
2103 | pack_pos_to_index(p, pos))) | |
3f83fd5e | 2104 | have_base = 1; |
3449f8c4 | 2105 | } |
5c49c116 NP |
2106 | entry->in_pack_header_size = used + used_0; |
2107 | break; | |
780e6e73 | 2108 | } |
780e6e73 | 2109 | |
3f83fd5e JK |
2110 | if (have_base && |
2111 | can_reuse_delta(&base_ref, entry, &base_entry)) { | |
fd9b1bae | 2112 | oe_set_type(entry, entry->in_pack_type); |
ac77d0c3 | 2113 | SET_SIZE(entry, in_pack_size); /* delta size */ |
0aca34e8 | 2114 | SET_DELTA_SIZE(entry, in_pack_size); |
6a1e32d5 JK |
2115 | |
2116 | if (base_entry) { | |
2117 | SET_DELTA(entry, base_entry); | |
2118 | entry->delta_sibling_idx = base_entry->delta_child_idx; | |
2119 | SET_DELTA_CHILD(base_entry, entry); | |
2120 | } else { | |
a93c141d | 2121 | SET_DELTA_EXT(entry, &base_ref); |
6a1e32d5 JK |
2122 | } |
2123 | ||
5c49c116 NP |
2124 | unuse_pack(&w_curs); |
2125 | return; | |
2126 | } | |
ab7cd7bb | 2127 | |
fd9b1bae | 2128 | if (oe_type(entry)) { |
27a7d067 NTND |
2129 | off_t delta_pos; |
2130 | ||
5c49c116 NP |
2131 | /* |
2132 | * This must be a delta and we already know what the | |
2133 | * final object type is. Let's extract the actual | |
2134 | * object size from the delta header. | |
2135 | */ | |
27a7d067 | 2136 | delta_pos = entry->in_pack_offset + entry->in_pack_header_size; |
ac77d0c3 NTND |
2137 | canonical_size = get_size_from_delta(p, &w_curs, delta_pos); |
2138 | if (canonical_size == 0) | |
03d66015 | 2139 | goto give_up; |
ac77d0c3 | 2140 | SET_SIZE(entry, canonical_size); |
5c49c116 | 2141 | unuse_pack(&w_curs); |
3f9ac8d2 JH |
2142 | return; |
2143 | } | |
5c49c116 NP |
2144 | |
2145 | /* | |
2146 | * No choice but to fall back to the recursive delta walk | |
c93206b4 | 2147 | * with oid_object_info() to find about the object type |
5c49c116 NP |
2148 | * at this point... |
2149 | */ | |
03d66015 | 2150 | give_up: |
5c49c116 | 2151 | unuse_pack(&w_curs); |
36e4d74a | 2152 | } |
3f9ac8d2 | 2153 | |
8d5cf957 | 2154 | if (oid_object_info_extended(the_repository, &entry->idx.oid, &oi, |
e00549aa | 2155 | OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_LOOKUP_REPLACE) < 0) { |
a5183d76 | 2156 | if (repo_has_promisor_remote(the_repository)) { |
e00549aa JT |
2157 | prefetch_to_pack(object_index); |
2158 | if (oid_object_info_extended(the_repository, &entry->idx.oid, &oi, | |
2159 | OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_LOOKUP_REPLACE) < 0) | |
2160 | type = -1; | |
2161 | } else { | |
2162 | type = -1; | |
2163 | } | |
2164 | } | |
8d5cf957 | 2165 | oe_set_type(entry, type); |
ac77d0c3 NTND |
2166 | if (entry->type_valid) { |
2167 | SET_SIZE(entry, canonical_size); | |
2168 | } else { | |
2169 | /* | |
2170 | * Bad object type is checked in prepare_pack(). This is | |
2171 | * to permit a missing preferred base object to be ignored | |
2172 | * as a preferred base. Doing so can result in a larger | |
2173 | * pack file, but the transfer will still take place. | |
2174 | */ | |
2175 | } | |
3f9ac8d2 JH |
2176 | } |
2177 | ||
5c49c116 NP |
2178 | static int pack_offset_sort(const void *_a, const void *_b) |
2179 | { | |
2180 | const struct object_entry *a = *(struct object_entry **)_a; | |
2181 | const struct object_entry *b = *(struct object_entry **)_b; | |
43fa44fa NTND |
2182 | const struct packed_git *a_in_pack = IN_PACK(a); |
2183 | const struct packed_git *b_in_pack = IN_PACK(b); | |
5c49c116 NP |
2184 | |
2185 | /* avoid filesystem trashing with loose objects */ | |
43fa44fa | 2186 | if (!a_in_pack && !b_in_pack) |
e6a492b7 | 2187 | return oidcmp(&a->idx.oid, &b->idx.oid); |
5c49c116 | 2188 | |
43fa44fa | 2189 | if (a_in_pack < b_in_pack) |
5c49c116 | 2190 | return -1; |
43fa44fa | 2191 | if (a_in_pack > b_in_pack) |
5c49c116 NP |
2192 | return 1; |
2193 | return a->in_pack_offset < b->in_pack_offset ? -1 : | |
2194 | (a->in_pack_offset > b->in_pack_offset); | |
2195 | } | |
2196 | ||
4cf2143e JK |
2197 | /* |
2198 | * Drop an on-disk delta we were planning to reuse. Naively, this would | |
2199 | * just involve blanking out the "delta" field, but we have to deal | |
2200 | * with some extra book-keeping: | |
2201 | * | |
2202 | * 1. Removing ourselves from the delta_sibling linked list. | |
2203 | * | |
2204 | * 2. Updating our size/type to the non-delta representation. These were | |
2205 | * either not recorded initially (size) or overwritten with the delta type | |
2206 | * (type) when check_object() decided to reuse the delta. | |
7dbabbbe JK |
2207 | * |
2208 | * 3. Resetting our delta depth, as we are now a base object. | |
4cf2143e JK |
2209 | */ |
2210 | static void drop_reused_delta(struct object_entry *entry) | |
2211 | { | |
898eba5e | 2212 | unsigned *idx = &to_pack.objects[entry->delta_idx - 1].delta_child_idx; |
4cf2143e | 2213 | struct object_info oi = OBJECT_INFO_INIT; |
fd9b1bae | 2214 | enum object_type type; |
ac77d0c3 | 2215 | unsigned long size; |
4cf2143e | 2216 | |
898eba5e NTND |
2217 | while (*idx) { |
2218 | struct object_entry *oe = &to_pack.objects[*idx - 1]; | |
4cf2143e | 2219 | |
898eba5e NTND |
2220 | if (oe == entry) |
2221 | *idx = oe->delta_sibling_idx; | |
4cf2143e | 2222 | else |
898eba5e | 2223 | idx = &oe->delta_sibling_idx; |
4cf2143e | 2224 | } |
898eba5e | 2225 | SET_DELTA(entry, NULL); |
7dbabbbe | 2226 | entry->depth = 0; |
4cf2143e | 2227 | |
ac77d0c3 | 2228 | oi.sizep = &size; |
fd9b1bae | 2229 | oi.typep = &type; |
ad635e82 | 2230 | if (packed_object_info(the_repository, IN_PACK(entry), entry->in_pack_offset, &oi) < 0) { |
4cf2143e JK |
2231 | /* |
2232 | * We failed to get the info from this pack for some reason; | |
c93206b4 | 2233 | * fall back to oid_object_info, which may find another copy. |
fd9b1bae | 2234 | * And if that fails, the error will be recorded in oe_type(entry) |
4cf2143e JK |
2235 | * and dealt with in prepare_pack(). |
2236 | */ | |
ad635e82 JH |
2237 | oe_set_type(entry, |
2238 | oid_object_info(the_repository, &entry->idx.oid, &size)); | |
fd9b1bae NTND |
2239 | } else { |
2240 | oe_set_type(entry, type); | |
4cf2143e | 2241 | } |
ac77d0c3 | 2242 | SET_SIZE(entry, size); |
4cf2143e JK |
2243 | } |
2244 | ||
2245 | /* | |
2246 | * Follow the chain of deltas from this entry onward, throwing away any links | |
2247 | * that cause us to hit a cycle (as determined by the DFS state flags in | |
2248 | * the entries). | |
7dbabbbe JK |
2249 | * |
2250 | * We also detect too-long reused chains that would violate our --depth | |
2251 | * limit. | |
4cf2143e JK |
2252 | */ |
2253 | static void break_delta_chains(struct object_entry *entry) | |
2254 | { | |
42b766d7 JK |
2255 | /* |
2256 | * The actual depth of each object we will write is stored as an int, | |
2257 | * as it cannot exceed our int "depth" limit. But before we break | |
2258 | * changes based no that limit, we may potentially go as deep as the | |
2259 | * number of objects, which is elsewhere bounded to a uint32_t. | |
2260 | */ | |
2261 | uint32_t total_depth; | |
2262 | struct object_entry *cur, *next; | |
2263 | ||
2264 | for (cur = entry, total_depth = 0; | |
2265 | cur; | |
898eba5e | 2266 | cur = DELTA(cur), total_depth++) { |
42b766d7 JK |
2267 | if (cur->dfs_state == DFS_DONE) { |
2268 | /* | |
2269 | * We've already seen this object and know it isn't | |
2270 | * part of a cycle. We do need to append its depth | |
2271 | * to our count. | |
2272 | */ | |
2273 | total_depth += cur->depth; | |
2274 | break; | |
2275 | } | |
4cf2143e | 2276 | |
4cf2143e | 2277 | /* |
42b766d7 JK |
2278 | * We break cycles before looping, so an ACTIVE state (or any |
2279 | * other cruft which made its way into the state variable) | |
2280 | * is a bug. | |
4cf2143e | 2281 | */ |
42b766d7 | 2282 | if (cur->dfs_state != DFS_NONE) |
033abf97 | 2283 | BUG("confusing delta dfs state in first pass: %d", |
42b766d7 | 2284 | cur->dfs_state); |
4cf2143e | 2285 | |
4cf2143e | 2286 | /* |
42b766d7 JK |
2287 | * Now we know this is the first time we've seen the object. If |
2288 | * it's not a delta, we're done traversing, but we'll mark it | |
2289 | * done to save time on future traversals. | |
4cf2143e | 2290 | */ |
898eba5e | 2291 | if (!DELTA(cur)) { |
42b766d7 JK |
2292 | cur->dfs_state = DFS_DONE; |
2293 | break; | |
2294 | } | |
4cf2143e | 2295 | |
4cf2143e | 2296 | /* |
42b766d7 JK |
2297 | * Mark ourselves as active and see if the next step causes |
2298 | * us to cycle to another active object. It's important to do | |
2299 | * this _before_ we loop, because it impacts where we make the | |
2300 | * cut, and thus how our total_depth counter works. | |
2301 | * E.g., We may see a partial loop like: | |
2302 | * | |
2303 | * A -> B -> C -> D -> B | |
2304 | * | |
2305 | * Cutting B->C breaks the cycle. But now the depth of A is | |
2306 | * only 1, and our total_depth counter is at 3. The size of the | |
2307 | * error is always one less than the size of the cycle we | |
2308 | * broke. Commits C and D were "lost" from A's chain. | |
2309 | * | |
2310 | * If we instead cut D->B, then the depth of A is correct at 3. | |
2311 | * We keep all commits in the chain that we examined. | |
4cf2143e | 2312 | */ |
42b766d7 | 2313 | cur->dfs_state = DFS_ACTIVE; |
898eba5e | 2314 | if (DELTA(cur)->dfs_state == DFS_ACTIVE) { |
42b766d7 JK |
2315 | drop_reused_delta(cur); |
2316 | cur->dfs_state = DFS_DONE; | |
2317 | break; | |
7dbabbbe | 2318 | } |
42b766d7 | 2319 | } |
7dbabbbe | 2320 | |
42b766d7 JK |
2321 | /* |
2322 | * And now that we've gone all the way to the bottom of the chain, we | |
2323 | * need to clear the active flags and set the depth fields as | |
2324 | * appropriate. Unlike the loop above, which can quit when it drops a | |
2325 | * delta, we need to keep going to look for more depth cuts. So we need | |
2326 | * an extra "next" pointer to keep going after we reset cur->delta. | |
2327 | */ | |
2328 | for (cur = entry; cur; cur = next) { | |
898eba5e | 2329 | next = DELTA(cur); |
4cf2143e | 2330 | |
42b766d7 JK |
2331 | /* |
2332 | * We should have a chain of zero or more ACTIVE states down to | |
2333 | * a final DONE. We can quit after the DONE, because either it | |
2334 | * has no bases, or we've already handled them in a previous | |
2335 | * call. | |
2336 | */ | |
2337 | if (cur->dfs_state == DFS_DONE) | |
2338 | break; | |
2339 | else if (cur->dfs_state != DFS_ACTIVE) | |
033abf97 | 2340 | BUG("confusing delta dfs state in second pass: %d", |
42b766d7 | 2341 | cur->dfs_state); |
4cf2143e | 2342 | |
4cf2143e | 2343 | /* |
42b766d7 JK |
2344 | * If the total_depth is more than depth, then we need to snip |
2345 | * the chain into two or more smaller chains that don't exceed | |
2346 | * the maximum depth. Most of the resulting chains will contain | |
2347 | * (depth + 1) entries (i.e., depth deltas plus one base), and | |
2348 | * the last chain (i.e., the one containing entry) will contain | |
2349 | * whatever entries are left over, namely | |
2350 | * (total_depth % (depth + 1)) of them. | |
2351 | * | |
2352 | * Since we are iterating towards decreasing depth, we need to | |
2353 | * decrement total_depth as we go, and we need to write to the | |
2354 | * entry what its final depth will be after all of the | |
2355 | * snipping. Since we're snipping into chains of length (depth | |
2356 | * + 1) entries, the final depth of an entry will be its | |
2357 | * original depth modulo (depth + 1). Any time we encounter an | |
2358 | * entry whose final depth is supposed to be zero, we snip it | |
2359 | * from its delta base, thereby making it so. | |
4cf2143e | 2360 | */ |
42b766d7 JK |
2361 | cur->depth = (total_depth--) % (depth + 1); |
2362 | if (!cur->depth) | |
2363 | drop_reused_delta(cur); | |
2364 | ||
2365 | cur->dfs_state = DFS_DONE; | |
4cf2143e JK |
2366 | } |
2367 | } | |
2368 | ||
c323ac7d LT |
2369 | static void get_object_details(void) |
2370 | { | |
7cadf491 | 2371 | uint32_t i; |
5c49c116 NP |
2372 | struct object_entry **sorted_by_offset; |
2373 | ||
5af05043 NTND |
2374 | if (progress) |
2375 | progress_state = start_progress(_("Counting objects"), | |
2376 | to_pack.nr_objects); | |
2377 | ||
ca56dadb | 2378 | CALLOC_ARRAY(sorted_by_offset, to_pack.nr_objects); |
2834bc27 VM |
2379 | for (i = 0; i < to_pack.nr_objects; i++) |
2380 | sorted_by_offset[i] = to_pack.objects + i; | |
9ed0d8d6 | 2381 | QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort); |
c323ac7d | 2382 | |
2834bc27 | 2383 | for (i = 0; i < to_pack.nr_objects; i++) { |
15366280 | 2384 | struct object_entry *entry = sorted_by_offset[i]; |
e00549aa | 2385 | check_object(entry, i); |
ac77d0c3 NTND |
2386 | if (entry->type_valid && |
2387 | oe_size_greater_than(&to_pack, entry, big_file_threshold)) | |
15366280 | 2388 | entry->no_try_delta = 1; |
5af05043 | 2389 | display_progress(progress_state, i + 1); |
15366280 | 2390 | } |
5af05043 | 2391 | stop_progress(&progress_state); |
3449f8c4 | 2392 | |
4cf2143e JK |
2393 | /* |
2394 | * This must happen in a second pass, since we rely on the delta | |
2395 | * information for the whole list being completed. | |
2396 | */ | |
2397 | for (i = 0; i < to_pack.nr_objects; i++) | |
2398 | break_delta_chains(&to_pack.objects[i]); | |
2399 | ||
5c49c116 | 2400 | free(sorted_by_offset); |
c323ac7d LT |
2401 | } |
2402 | ||
b904166c NP |
2403 | /* |
2404 | * We search for deltas in a list sorted by type, by filename hash, and then | |
2405 | * by size, so that we see progressively smaller and smaller files. | |
2406 | * That's because we prefer deltas to be from the bigger file | |
2407 | * to the smaller -- deletes are potentially cheaper, but perhaps | |
2408 | * more importantly, the bigger file is likely the more recent | |
2409 | * one. The deepest deltas are therefore the oldest objects which are | |
2410 | * less susceptible to be accessed often. | |
2411 | */ | |
9668cf59 | 2412 | static int type_size_sort(const void *_a, const void *_b) |
c323ac7d | 2413 | { |
9668cf59 NP |
2414 | const struct object_entry *a = *(struct object_entry **)_a; |
2415 | const struct object_entry *b = *(struct object_entry **)_b; | |
33de80b1 SL |
2416 | const enum object_type a_type = oe_type(a); |
2417 | const enum object_type b_type = oe_type(b); | |
2418 | const unsigned long a_size = SIZE(a); | |
2419 | const unsigned long b_size = SIZE(b); | |
9668cf59 | 2420 | |
fd9b1bae | 2421 | if (a_type > b_type) |
27225f2e | 2422 | return -1; |
fd9b1bae | 2423 | if (a_type < b_type) |
27225f2e | 2424 | return 1; |
b904166c | 2425 | if (a->hash > b->hash) |
7a979d99 | 2426 | return -1; |
b904166c | 2427 | if (a->hash < b->hash) |
7a979d99 | 2428 | return 1; |
b904166c | 2429 | if (a->preferred_base > b->preferred_base) |
c323ac7d | 2430 | return -1; |
b904166c NP |
2431 | if (a->preferred_base < b->preferred_base) |
2432 | return 1; | |
28b8a730 | 2433 | if (use_delta_islands) { |
33de80b1 | 2434 | const int island_cmp = island_delta_cmp(&a->idx.oid, &b->idx.oid); |
28b8a730 JK |
2435 | if (island_cmp) |
2436 | return island_cmp; | |
2437 | } | |
ac77d0c3 | 2438 | if (a_size > b_size) |
b904166c | 2439 | return -1; |
ac77d0c3 | 2440 | if (a_size < b_size) |
c323ac7d | 2441 | return 1; |
b904166c | 2442 | return a < b ? -1 : (a > b); /* newest first */ |
c323ac7d LT |
2443 | } |
2444 | ||
2445 | struct unpacked { | |
2446 | struct object_entry *entry; | |
2447 | void *data; | |
f6c7081a | 2448 | struct delta_index *index; |
5a235b5e | 2449 | unsigned depth; |
c323ac7d LT |
2450 | }; |
2451 | ||
d250626c NP |
2452 | static int delta_cacheable(unsigned long src_size, unsigned long trg_size, |
2453 | unsigned long delta_size) | |
074b2eea MK |
2454 | { |
2455 | if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size) | |
2456 | return 0; | |
2457 | ||
e3dfddb3 MK |
2458 | if (delta_size < cache_max_small_delta_size) |
2459 | return 1; | |
2460 | ||
074b2eea MK |
2461 | /* cache delta, if objects are large enough compared to delta size */ |
2462 | if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10)) | |
2463 | return 1; | |
2464 | ||
2465 | return 0; | |
2466 | } | |
2467 | ||
ffbd51cc | 2468 | /* Protect delta_cache_size */ |
44626dc7 | 2469 | static pthread_mutex_t cache_mutex; |
3c701839 NP |
2470 | #define cache_lock() pthread_mutex_lock(&cache_mutex) |
2471 | #define cache_unlock() pthread_mutex_unlock(&cache_mutex) | |
2472 | ||
ffbd51cc NTND |
2473 | /* |
2474 | * Protect object list partitioning (e.g. struct thread_param) and | |
2475 | * progress_state | |
2476 | */ | |
44626dc7 | 2477 | static pthread_mutex_t progress_mutex; |
8ecce684 NP |
2478 | #define progress_lock() pthread_mutex_lock(&progress_mutex) |
2479 | #define progress_unlock() pthread_mutex_unlock(&progress_mutex) | |
2480 | ||
ffbd51cc NTND |
2481 | /* |
2482 | * Access to struct object_entry is unprotected since each thread owns | |
2483 | * a portion of the main object list. Just don't access object entries | |
2484 | * ahead in the list because they can be stolen and would need | |
2485 | * progress_mutex for protection. | |
2486 | */ | |
8ecce684 | 2487 | |
7d089fb9 ÆAB |
2488 | static inline int oe_size_less_than(struct packing_data *pack, |
2489 | const struct object_entry *lhs, | |
2490 | unsigned long rhs) | |
2491 | { | |
2492 | if (lhs->size_valid) | |
2493 | return lhs->size_ < rhs; | |
2494 | if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */ | |
2495 | return 0; | |
2496 | return oe_get_size_slow(pack, lhs) < rhs; | |
2497 | } | |
2498 | ||
2499 | static inline void oe_set_tree_depth(struct packing_data *pack, | |
2500 | struct object_entry *e, | |
2501 | unsigned int tree_depth) | |
2502 | { | |
2503 | if (!pack->tree_depth) | |
2504 | CALLOC_ARRAY(pack->tree_depth, pack->nr_alloc); | |
2505 | pack->tree_depth[e - pack->objects] = tree_depth; | |
2506 | } | |
2507 | ||
ac77d0c3 NTND |
2508 | /* |
2509 | * Return the size of the object without doing any delta | |
2510 | * reconstruction (so non-deltas are true object sizes, but deltas | |
2511 | * return the size of the delta data). | |
2512 | */ | |
2513 | unsigned long oe_get_size_slow(struct packing_data *pack, | |
2514 | const struct object_entry *e) | |
2515 | { | |
2516 | struct packed_git *p; | |
2517 | struct pack_window *w_curs; | |
2518 | unsigned char *buf; | |
2519 | enum object_type type; | |
2520 | unsigned long used, avail, size; | |
2521 | ||
2522 | if (e->type_ != OBJ_OFS_DELTA && e->type_ != OBJ_REF_DELTA) { | |
edb673cf | 2523 | packing_data_lock(&to_pack); |
ad635e82 | 2524 | if (oid_object_info(the_repository, &e->idx.oid, &size) < 0) |
ac77d0c3 NTND |
2525 | die(_("unable to get size of %s"), |
2526 | oid_to_hex(&e->idx.oid)); | |
edb673cf | 2527 | packing_data_unlock(&to_pack); |
ac77d0c3 NTND |
2528 | return size; |
2529 | } | |
2530 | ||
2531 | p = oe_in_pack(pack, e); | |
2532 | if (!p) | |
2533 | BUG("when e->type is a delta, it must belong to a pack"); | |
2534 | ||
edb673cf | 2535 | packing_data_lock(&to_pack); |
ac77d0c3 NTND |
2536 | w_curs = NULL; |
2537 | buf = use_pack(p, &w_curs, e->in_pack_offset, &avail); | |
2538 | used = unpack_object_header_buffer(buf, avail, &type, &size); | |
2539 | if (used == 0) | |
2540 | die(_("unable to parse object header of %s"), | |
2541 | oid_to_hex(&e->idx.oid)); | |
2542 | ||
2543 | unuse_pack(&w_curs); | |
edb673cf | 2544 | packing_data_unlock(&to_pack); |
ac77d0c3 NTND |
2545 | return size; |
2546 | } | |
2547 | ||
f6c7081a | 2548 | static int try_delta(struct unpacked *trg, struct unpacked *src, |
ef0316fc | 2549 | unsigned max_depth, unsigned long *mem_usage) |
c323ac7d | 2550 | { |
f6c7081a NP |
2551 | struct object_entry *trg_entry = trg->entry; |
2552 | struct object_entry *src_entry = src->entry; | |
560b25a8 | 2553 | unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz; |
c83f032e | 2554 | unsigned ref_depth; |
21666f1a | 2555 | enum object_type type; |
c323ac7d LT |
2556 | void *delta_buf; |
2557 | ||
2558 | /* Don't bother doing diffs between different types */ | |
fd9b1bae | 2559 | if (oe_type(trg_entry) != oe_type(src_entry)) |
c323ac7d LT |
2560 | return -1; |
2561 | ||
51d1e83f | 2562 | /* |
15f07e06 JK |
2563 | * We do not bother to try a delta that we discarded on an |
2564 | * earlier try, but only when reusing delta data. Note that | |
2565 | * src_entry that is marked as the preferred_base should always | |
2566 | * be considered, as even if we produce a suboptimal delta against | |
2567 | * it, we will still save the transfer cost, as we already know | |
2568 | * the other side has it and we won't send src_entry at all. | |
51d1e83f | 2569 | */ |
43fa44fa NTND |
2570 | if (reuse_delta && IN_PACK(trg_entry) && |
2571 | IN_PACK(trg_entry) == IN_PACK(src_entry) && | |
15f07e06 | 2572 | !src_entry->preferred_base && |
e9195b58 JH |
2573 | trg_entry->in_pack_type != OBJ_REF_DELTA && |
2574 | trg_entry->in_pack_type != OBJ_OFS_DELTA) | |
51d1e83f LT |
2575 | return 0; |
2576 | ||
898b14ce | 2577 | /* Let's not bust the allowed depth. */ |
5a235b5e | 2578 | if (src->depth >= max_depth) |
d116a45a | 2579 | return 0; |
c323ac7d | 2580 | |
c3b06a69 | 2581 | /* Now some size filtering heuristics. */ |
ac77d0c3 | 2582 | trg_size = SIZE(trg_entry); |
898eba5e | 2583 | if (!DELTA(trg_entry)) { |
41179100 | 2584 | max_size = trg_size/2 - the_hash_algo->rawsz; |
c83f032e NP |
2585 | ref_depth = 1; |
2586 | } else { | |
0aca34e8 | 2587 | max_size = DELTA_SIZE(trg_entry); |
5a235b5e | 2588 | ref_depth = trg->depth; |
c83f032e | 2589 | } |
720fe22d | 2590 | max_size = (uint64_t)max_size * (max_depth - src->depth) / |
c83f032e | 2591 | (max_depth - ref_depth + 1); |
c3b06a69 NP |
2592 | if (max_size == 0) |
2593 | return 0; | |
ac77d0c3 | 2594 | src_size = SIZE(src_entry); |
560b25a8 | 2595 | sizediff = src_size < trg_size ? trg_size - src_size : 0; |
27225f2e | 2596 | if (sizediff >= max_size) |
f527cb8c | 2597 | return 0; |
a1dab41a BD |
2598 | if (trg_size < src_size / 32) |
2599 | return 0; | |
f6c7081a | 2600 | |
28b8a730 JK |
2601 | if (!in_same_island(&trg->entry->idx.oid, &src->entry->idx.oid)) |
2602 | return 0; | |
2603 | ||
560b25a8 NP |
2604 | /* Load data if not already done */ |
2605 | if (!trg->data) { | |
edb673cf | 2606 | packing_data_lock(&to_pack); |
bc726bd0 ÆAB |
2607 | trg->data = repo_read_object_file(the_repository, |
2608 | &trg_entry->idx.oid, &type, | |
2609 | &sz); | |
edb673cf | 2610 | packing_data_unlock(&to_pack); |
2e3404c3 | 2611 | if (!trg->data) |
f616db6a | 2612 | die(_("object %s cannot be read"), |
e6a492b7 | 2613 | oid_to_hex(&trg_entry->idx.oid)); |
560b25a8 | 2614 | if (sz != trg_size) |
ca473cef TB |
2615 | die(_("object %s inconsistent object length (%"PRIuMAX" vs %"PRIuMAX")"), |
2616 | oid_to_hex(&trg_entry->idx.oid), (uintmax_t)sz, | |
2617 | (uintmax_t)trg_size); | |
ef0316fc | 2618 | *mem_usage += sz; |
560b25a8 NP |
2619 | } |
2620 | if (!src->data) { | |
edb673cf | 2621 | packing_data_lock(&to_pack); |
bc726bd0 ÆAB |
2622 | src->data = repo_read_object_file(the_repository, |
2623 | &src_entry->idx.oid, &type, | |
2624 | &sz); | |
edb673cf | 2625 | packing_data_unlock(&to_pack); |
71064a95 NP |
2626 | if (!src->data) { |
2627 | if (src_entry->preferred_base) { | |
2628 | static int warned = 0; | |
2629 | if (!warned++) | |
f616db6a | 2630 | warning(_("object %s cannot be read"), |
e6a492b7 | 2631 | oid_to_hex(&src_entry->idx.oid)); |
71064a95 NP |
2632 | /* |
2633 | * Those objects are not included in the | |
2634 | * resulting pack. Be resilient and ignore | |
2635 | * them if they can't be read, in case the | |
2636 | * pack could be created nevertheless. | |
2637 | */ | |
2638 | return 0; | |
2639 | } | |
f616db6a | 2640 | die(_("object %s cannot be read"), |
e6a492b7 | 2641 | oid_to_hex(&src_entry->idx.oid)); |
71064a95 | 2642 | } |
560b25a8 | 2643 | if (sz != src_size) |
ca473cef TB |
2644 | die(_("object %s inconsistent object length (%"PRIuMAX" vs %"PRIuMAX")"), |
2645 | oid_to_hex(&src_entry->idx.oid), (uintmax_t)sz, | |
2646 | (uintmax_t)src_size); | |
ef0316fc | 2647 | *mem_usage += sz; |
560b25a8 NP |
2648 | } |
2649 | if (!src->index) { | |
2650 | src->index = create_delta_index(src->data, src_size); | |
a588d88a MK |
2651 | if (!src->index) { |
2652 | static int warned = 0; | |
2653 | if (!warned++) | |
f616db6a | 2654 | warning(_("suboptimal pack - out of memory")); |
a588d88a MK |
2655 | return 0; |
2656 | } | |
ef0316fc | 2657 | *mem_usage += sizeof_delta_index(src->index); |
560b25a8 NP |
2658 | } |
2659 | ||
2660 | delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size); | |
c323ac7d | 2661 | if (!delta_buf) |
75c42d8c | 2662 | return 0; |
f6c7081a | 2663 | |
898eba5e | 2664 | if (DELTA(trg_entry)) { |
848d732c | 2665 | /* Prefer only shallower same-sized deltas. */ |
0aca34e8 | 2666 | if (delta_size == DELTA_SIZE(trg_entry) && |
5a235b5e | 2667 | src->depth + 1 >= trg->depth) { |
848d732c BD |
2668 | free(delta_buf); |
2669 | return 0; | |
2670 | } | |
074b2eea | 2671 | } |
9e2d57a0 | 2672 | |
3c701839 NP |
2673 | /* |
2674 | * Handle memory allocation outside of the cache | |
2675 | * accounting lock. Compiler will optimize the strangeness | |
7eb151d6 | 2676 | * away when NO_PTHREADS is defined. |
3c701839 | 2677 | */ |
8e0f7003 | 2678 | free(trg_entry->delta_data); |
3c701839 | 2679 | cache_lock(); |
9e2d57a0 | 2680 | if (trg_entry->delta_data) { |
0aca34e8 | 2681 | delta_cache_size -= DELTA_SIZE(trg_entry); |
9e2d57a0 NP |
2682 | trg_entry->delta_data = NULL; |
2683 | } | |
d250626c | 2684 | if (delta_cacheable(src_size, trg_size, delta_size)) { |
b7a28f78 | 2685 | delta_cache_size += delta_size; |
3c701839 NP |
2686 | cache_unlock(); |
2687 | trg_entry->delta_data = xrealloc(delta_buf, delta_size); | |
2688 | } else { | |
2689 | cache_unlock(); | |
074b2eea | 2690 | free(delta_buf); |
3c701839 NP |
2691 | } |
2692 | ||
898eba5e | 2693 | SET_DELTA(trg_entry, src_entry); |
0aca34e8 | 2694 | SET_DELTA_SIZE(trg_entry, delta_size); |
b7a28f78 NP |
2695 | trg->depth = src->depth + 1; |
2696 | ||
f6c7081a | 2697 | return 1; |
c323ac7d LT |
2698 | } |
2699 | ||
898b14ce | 2700 | static unsigned int check_delta_limit(struct object_entry *me, unsigned int n) |
b2504a0d | 2701 | { |
898eba5e | 2702 | struct object_entry *child = DELTA_CHILD(me); |
898b14ce NP |
2703 | unsigned int m = n; |
2704 | while (child) { | |
33de80b1 | 2705 | const unsigned int c = check_delta_limit(child, n + 1); |
898b14ce NP |
2706 | if (m < c) |
2707 | m = c; | |
898eba5e | 2708 | child = DELTA_SIBLING(child); |
898b14ce NP |
2709 | } |
2710 | return m; | |
b2504a0d NP |
2711 | } |
2712 | ||
75ad235c | 2713 | static unsigned long free_unpacked(struct unpacked *n) |
a97773ce | 2714 | { |
ef0316fc | 2715 | unsigned long freed_mem = sizeof_delta_index(n->index); |
a97773ce BD |
2716 | free_delta_index(n->index); |
2717 | n->index = NULL; | |
2718 | if (n->data) { | |
ac77d0c3 | 2719 | freed_mem += SIZE(n->entry); |
6a83d902 | 2720 | FREE_AND_NULL(n->data); |
a97773ce BD |
2721 | } |
2722 | n->entry = NULL; | |
7d7baa5e | 2723 | n->depth = 0; |
ef0316fc | 2724 | return freed_mem; |
a97773ce BD |
2725 | } |
2726 | ||
384b32c0 | 2727 | static void find_deltas(struct object_entry **list, unsigned *list_size, |
e334977d | 2728 | int window, int depth, unsigned *processed) |
c323ac7d | 2729 | { |
384b32c0 | 2730 | uint32_t i, idx = 0, count = 0; |
7cadf491 | 2731 | struct unpacked *array; |
ef0316fc | 2732 | unsigned long mem_usage = 0; |
c323ac7d | 2733 | |
ca56dadb | 2734 | CALLOC_ARRAY(array, window); |
21fcd1bd | 2735 | |
384b32c0 | 2736 | for (;;) { |
421b488a | 2737 | struct object_entry *entry; |
c323ac7d | 2738 | struct unpacked *n = array + idx; |
ef0316fc | 2739 | int j, max_depth, best_base = -1; |
c323ac7d | 2740 | |
384b32c0 NP |
2741 | progress_lock(); |
2742 | if (!*list_size) { | |
2743 | progress_unlock(); | |
2744 | break; | |
2745 | } | |
421b488a | 2746 | entry = *list++; |
384b32c0 NP |
2747 | (*list_size)--; |
2748 | if (!entry->preferred_base) { | |
2749 | (*processed)++; | |
2750 | display_progress(progress_state, *processed); | |
2751 | } | |
2752 | progress_unlock(); | |
2753 | ||
ef0316fc | 2754 | mem_usage -= free_unpacked(n); |
c323ac7d | 2755 | n->entry = entry; |
ab7cd7bb | 2756 | |
a97773ce | 2757 | while (window_memory_limit && |
ef0316fc | 2758 | mem_usage > window_memory_limit && |
a97773ce | 2759 | count > 1) { |
33de80b1 | 2760 | const uint32_t tail = (idx + window - count) % window; |
75ad235c | 2761 | mem_usage -= free_unpacked(array + tail); |
a97773ce BD |
2762 | count--; |
2763 | } | |
2764 | ||
75d39853 NP |
2765 | /* We do not compute delta to *create* objects we are not |
2766 | * going to pack. | |
2767 | */ | |
2768 | if (entry->preferred_base) | |
2769 | goto next; | |
2770 | ||
898b14ce NP |
2771 | /* |
2772 | * If the current object is at pack edge, take the depth the | |
2773 | * objects that depend on the current object into account | |
2774 | * otherwise they would become too deep. | |
2775 | */ | |
2776 | max_depth = depth; | |
898eba5e | 2777 | if (DELTA_CHILD(entry)) { |
898b14ce NP |
2778 | max_depth -= check_delta_limit(entry, 0); |
2779 | if (max_depth <= 0) | |
2780 | goto next; | |
2781 | } | |
2782 | ||
78817c15 LT |
2783 | j = window; |
2784 | while (--j > 0) { | |
77639870 | 2785 | int ret; |
7cadf491 | 2786 | uint32_t other_idx = idx + j; |
c323ac7d | 2787 | struct unpacked *m; |
78817c15 LT |
2788 | if (other_idx >= window) |
2789 | other_idx -= window; | |
c323ac7d LT |
2790 | m = array + other_idx; |
2791 | if (!m->entry) | |
2792 | break; | |
ef0316fc | 2793 | ret = try_delta(n, m, max_depth, &mem_usage); |
77639870 | 2794 | if (ret < 0) |
c323ac7d | 2795 | break; |
77639870 JH |
2796 | else if (ret > 0) |
2797 | best_base = other_idx; | |
c323ac7d | 2798 | } |
898b14ce | 2799 | |
ed4a9031 NP |
2800 | /* |
2801 | * If we decided to cache the delta data, then it is best | |
2802 | * to compress it right away. First because we have to do | |
2803 | * it anyway, and doing it here while we're threaded will | |
2804 | * save a lot of time in the non threaded write phase, | |
2805 | * as well as allow for caching more deltas within | |
2806 | * the same cache size limit. | |
2807 | * ... | |
2808 | * But only if not writing to stdout, since in that case | |
2809 | * the network is most likely throttling writes anyway, | |
2810 | * and therefore it is best to go to the write phase ASAP | |
2811 | * instead, as we can afford spending more time compressing | |
2812 | * between writes at that moment. | |
2813 | */ | |
2814 | if (entry->delta_data && !pack_to_stdout) { | |
0cb3c142 NTND |
2815 | unsigned long size; |
2816 | ||
0aca34e8 | 2817 | size = do_compress(&entry->delta_data, DELTA_SIZE(entry)); |
0cb3c142 NTND |
2818 | if (size < (1U << OE_Z_DELTA_BITS)) { |
2819 | entry->z_delta_size = size; | |
2820 | cache_lock(); | |
0aca34e8 | 2821 | delta_cache_size -= DELTA_SIZE(entry); |
0cb3c142 NTND |
2822 | delta_cache_size += entry->z_delta_size; |
2823 | cache_unlock(); | |
2824 | } else { | |
2825 | FREE_AND_NULL(entry->delta_data); | |
2826 | entry->z_delta_size = 0; | |
2827 | } | |
ed4a9031 NP |
2828 | } |
2829 | ||
70ca1a3f JH |
2830 | /* if we made n a delta, and if n is already at max |
2831 | * depth, leaving it in the window is pointless. we | |
2832 | * should evict it first. | |
70ca1a3f | 2833 | */ |
898eba5e | 2834 | if (DELTA(entry) && max_depth <= n->depth) |
70ca1a3f | 2835 | continue; |
ff45715c | 2836 | |
77639870 JH |
2837 | /* |
2838 | * Move the best delta base up in the window, after the | |
2839 | * currently deltified object, to keep it longer. It will | |
2840 | * be the first base object to be attempted next. | |
2841 | */ | |
898eba5e | 2842 | if (DELTA(entry)) { |
77639870 JH |
2843 | struct unpacked swap = array[best_base]; |
2844 | int dist = (window + idx - best_base) % window; | |
2845 | int dst = best_base; | |
2846 | while (dist--) { | |
2847 | int src = (dst + 1) % window; | |
2848 | array[dst] = array[src]; | |
2849 | dst = src; | |
2850 | } | |
2851 | array[dst] = swap; | |
2852 | } | |
2853 | ||
898b14ce | 2854 | next: |
521a4f4c | 2855 | idx++; |
a97773ce BD |
2856 | if (count + 1 < window) |
2857 | count++; | |
521a4f4c LT |
2858 | if (idx >= window) |
2859 | idx = 0; | |
384b32c0 | 2860 | } |
adee7bdf | 2861 | |
f6c7081a | 2862 | for (i = 0; i < window; ++i) { |
ff45715c | 2863 | free_delta_index(array[i].index); |
adee7bdf | 2864 | free(array[i].data); |
f6c7081a | 2865 | } |
adee7bdf | 2866 | free(array); |
c323ac7d LT |
2867 | } |
2868 | ||
50f22ada | 2869 | /* |
ffbd51cc NTND |
2870 | * The main object list is split into smaller lists, each is handed to |
2871 | * one worker. | |
2872 | * | |
50f22ada JS |
2873 | * The main thread waits on the condition that (at least) one of the workers |
2874 | * has stopped working (which is indicated in the .working member of | |
2875 | * struct thread_params). | |
ffbd51cc | 2876 | * |
50f22ada JS |
2877 | * When a work thread has completed its work, it sets .working to 0 and |
2878 | * signals the main thread and waits on the condition that .data_ready | |
2879 | * becomes 1. | |
ffbd51cc NTND |
2880 | * |
2881 | * The main thread steals half of the work from the worker that has | |
2882 | * most work left to hand it to the idle worker. | |
50f22ada JS |
2883 | */ |
2884 | ||
8ecce684 NP |
2885 | struct thread_params { |
2886 | pthread_t thread; | |
2887 | struct object_entry **list; | |
2888 | unsigned list_size; | |
384b32c0 | 2889 | unsigned remaining; |
8ecce684 NP |
2890 | int window; |
2891 | int depth; | |
50f22ada JS |
2892 | int working; |
2893 | int data_ready; | |
2894 | pthread_mutex_t mutex; | |
2895 | pthread_cond_t cond; | |
8ecce684 NP |
2896 | unsigned *processed; |
2897 | }; | |
2898 | ||
44626dc7 AH |
2899 | static pthread_cond_t progress_cond; |
2900 | ||
2901 | /* | |
2902 | * Mutex and conditional variable can't be statically-initialized on Windows. | |
2903 | */ | |
2904 | static void init_threaded_search(void) | |
2905 | { | |
44626dc7 AH |
2906 | pthread_mutex_init(&cache_mutex, NULL); |
2907 | pthread_mutex_init(&progress_mutex, NULL); | |
2908 | pthread_cond_init(&progress_cond, NULL); | |
2909 | } | |
2910 | ||
2911 | static void cleanup_threaded_search(void) | |
2912 | { | |
2913 | pthread_cond_destroy(&progress_cond); | |
44626dc7 AH |
2914 | pthread_mutex_destroy(&cache_mutex); |
2915 | pthread_mutex_destroy(&progress_mutex); | |
2916 | } | |
c2a33679 | 2917 | |
8ecce684 NP |
2918 | static void *threaded_find_deltas(void *arg) |
2919 | { | |
c2a33679 NP |
2920 | struct thread_params *me = arg; |
2921 | ||
0c2ad00b | 2922 | progress_lock(); |
50f22ada | 2923 | while (me->remaining) { |
0c2ad00b MÅ |
2924 | progress_unlock(); |
2925 | ||
384b32c0 | 2926 | find_deltas(me->list, &me->remaining, |
c2a33679 | 2927 | me->window, me->depth, me->processed); |
50f22ada JS |
2928 | |
2929 | progress_lock(); | |
2930 | me->working = 0; | |
2931 | pthread_cond_signal(&progress_cond); | |
2932 | progress_unlock(); | |
2933 | ||
2934 | /* | |
2935 | * We must not set ->data_ready before we wait on the | |
2936 | * condition because the main thread may have set it to 1 | |
2937 | * before we get here. In order to be sure that new | |
2938 | * work is available if we see 1 in ->data_ready, it | |
2939 | * was initialized to 0 before this thread was spawned | |
2940 | * and we reset it to 0 right away. | |
2941 | */ | |
2942 | pthread_mutex_lock(&me->mutex); | |
2943 | while (!me->data_ready) | |
2944 | pthread_cond_wait(&me->cond, &me->mutex); | |
2945 | me->data_ready = 0; | |
2946 | pthread_mutex_unlock(&me->mutex); | |
0c2ad00b MÅ |
2947 | |
2948 | progress_lock(); | |
c2a33679 | 2949 | } |
0c2ad00b | 2950 | progress_unlock(); |
50f22ada JS |
2951 | /* leave ->working 1 so that this doesn't get more work assigned */ |
2952 | return NULL; | |
8ecce684 NP |
2953 | } |
2954 | ||
8ecce684 NP |
2955 | static void ll_find_deltas(struct object_entry **list, unsigned list_size, |
2956 | int window, int depth, unsigned *processed) | |
2957 | { | |
dcda3614 | 2958 | struct thread_params *p; |
384b32c0 | 2959 | int i, ret, active_threads = 0; |
c2a33679 | 2960 | |
44626dc7 AH |
2961 | init_threaded_search(); |
2962 | ||
367f4a43 | 2963 | if (delta_search_threads <= 1) { |
384b32c0 | 2964 | find_deltas(list, &list_size, window, depth, processed); |
44626dc7 | 2965 | cleanup_threaded_search(); |
367f4a43 NP |
2966 | return; |
2967 | } | |
43cc2b42 | 2968 | if (progress > pack_to_stdout) |
f616db6a | 2969 | fprintf_ln(stderr, _("Delta compression using up to %d threads"), |
1a07e59c | 2970 | delta_search_threads); |
ca56dadb | 2971 | CALLOC_ARRAY(p, delta_search_threads); |
367f4a43 | 2972 | |
50f22ada | 2973 | /* Partition the work amongst work threads. */ |
367f4a43 | 2974 | for (i = 0; i < delta_search_threads; i++) { |
50f22ada JS |
2975 | unsigned sub_size = list_size / (delta_search_threads - i); |
2976 | ||
bf874896 NP |
2977 | /* don't use too small segments or no deltas will be found */ |
2978 | if (sub_size < 2*window && i+1 < delta_search_threads) | |
2979 | sub_size = 0; | |
2980 | ||
8ecce684 NP |
2981 | p[i].window = window; |
2982 | p[i].depth = depth; | |
2983 | p[i].processed = processed; | |
50f22ada JS |
2984 | p[i].working = 1; |
2985 | p[i].data_ready = 0; | |
c2a33679 | 2986 | |
59921b4b | 2987 | /* try to split chunks on "path" boundaries */ |
6fc74703 NP |
2988 | while (sub_size && sub_size < list_size && |
2989 | list[sub_size]->hash && | |
384b32c0 NP |
2990 | list[sub_size]->hash == list[sub_size-1]->hash) |
2991 | sub_size++; | |
2992 | ||
50f22ada JS |
2993 | p[i].list = list; |
2994 | p[i].list_size = sub_size; | |
2995 | p[i].remaining = sub_size; | |
59921b4b | 2996 | |
384b32c0 NP |
2997 | list += sub_size; |
2998 | list_size -= sub_size; | |
2999 | } | |
3000 | ||
50f22ada JS |
3001 | /* Start work threads. */ |
3002 | for (i = 0; i < delta_search_threads; i++) { | |
3003 | if (!p[i].list_size) | |
3004 | continue; | |
68e6a4f8 JS |
3005 | pthread_mutex_init(&p[i].mutex, NULL); |
3006 | pthread_cond_init(&p[i].cond, NULL); | |
50f22ada JS |
3007 | ret = pthread_create(&p[i].thread, NULL, |
3008 | threaded_find_deltas, &p[i]); | |
3009 | if (ret) | |
f616db6a | 3010 | die(_("unable to create thread: %s"), strerror(ret)); |
50f22ada JS |
3011 | active_threads++; |
3012 | } | |
3013 | ||
384b32c0 NP |
3014 | /* |
3015 | * Now let's wait for work completion. Each time a thread is done | |
3016 | * with its work, we steal half of the remaining work from the | |
3017 | * thread with the largest number of unprocessed objects and give | |
3018 | * it to that newly idle thread. This ensure good load balancing | |
3019 | * until the remaining object list segments are simply too short | |
3020 | * to be worth splitting anymore. | |
3021 | */ | |
50f22ada JS |
3022 | while (active_threads) { |
3023 | struct thread_params *target = NULL; | |
384b32c0 NP |
3024 | struct thread_params *victim = NULL; |
3025 | unsigned sub_size = 0; | |
384b32c0 NP |
3026 | |
3027 | progress_lock(); | |
50f22ada JS |
3028 | for (;;) { |
3029 | for (i = 0; !target && i < delta_search_threads; i++) | |
3030 | if (!p[i].working) | |
3031 | target = &p[i]; | |
3032 | if (target) | |
3033 | break; | |
3034 | pthread_cond_wait(&progress_cond, &progress_mutex); | |
3035 | } | |
3036 | ||
384b32c0 NP |
3037 | for (i = 0; i < delta_search_threads; i++) |
3038 | if (p[i].remaining > 2*window && | |
3039 | (!victim || victim->remaining < p[i].remaining)) | |
3040 | victim = &p[i]; | |
3041 | if (victim) { | |
3042 | sub_size = victim->remaining / 2; | |
3043 | list = victim->list + victim->list_size - sub_size; | |
3044 | while (sub_size && list[0]->hash && | |
3045 | list[0]->hash == list[-1]->hash) { | |
3046 | list++; | |
3047 | sub_size--; | |
3048 | } | |
eb9688ff NP |
3049 | if (!sub_size) { |
3050 | /* | |
3051 | * It is possible for some "paths" to have | |
3052 | * so many objects that no hash boundary | |
3053 | * might be found. Let's just steal the | |
3054 | * exact half in that case. | |
3055 | */ | |
3056 | sub_size = victim->remaining / 2; | |
3057 | list -= sub_size; | |
3058 | } | |
384b32c0 NP |
3059 | target->list = list; |
3060 | victim->list_size -= sub_size; | |
3061 | victim->remaining -= sub_size; | |
3062 | } | |
384b32c0 NP |
3063 | target->list_size = sub_size; |
3064 | target->remaining = sub_size; | |
50f22ada JS |
3065 | target->working = 1; |
3066 | progress_unlock(); | |
3067 | ||
3068 | pthread_mutex_lock(&target->mutex); | |
3069 | target->data_ready = 1; | |
3070 | pthread_cond_signal(&target->cond); | |
3071 | pthread_mutex_unlock(&target->mutex); | |
c2a33679 | 3072 | |
384b32c0 | 3073 | if (!sub_size) { |
b81d9af7 | 3074 | pthread_join(target->thread, NULL); |
50f22ada JS |
3075 | pthread_cond_destroy(&target->cond); |
3076 | pthread_mutex_destroy(&target->mutex); | |
384b32c0 | 3077 | active_threads--; |
c2a33679 | 3078 | } |
50f22ada | 3079 | } |
44626dc7 | 3080 | cleanup_threaded_search(); |
dcda3614 | 3081 | free(p); |
8ecce684 NP |
3082 | } |
3083 | ||
ff483026 JK |
3084 | static int obj_is_packed(const struct object_id *oid) |
3085 | { | |
a14aebea | 3086 | return packlist_find(&to_pack, oid) || |
92fb0db9 JK |
3087 | (reuse_packfile_bitmap && |
3088 | bitmap_walk_contains(bitmap_git, reuse_packfile_bitmap, oid)); | |
ff483026 JK |
3089 | } |
3090 | ||
b773ddea JK |
3091 | static void add_tag_chain(const struct object_id *oid) |
3092 | { | |
3093 | struct tag *tag; | |
3094 | ||
3095 | /* | |
3096 | * We catch duplicates already in add_object_entry(), but we'd | |
3097 | * prefer to do this extra check to avoid having to parse the | |
3098 | * tag at all if we already know that it's being packed (e.g., if | |
3099 | * it was included via bitmaps, we would not have parsed it | |
3100 | * previously). | |
3101 | */ | |
ff483026 | 3102 | if (obj_is_packed(oid)) |
b773ddea JK |
3103 | return; |
3104 | ||
ce71efb7 | 3105 | tag = lookup_tag(the_repository, oid); |
b773ddea JK |
3106 | while (1) { |
3107 | if (!tag || parse_tag(tag) || !tag->tagged) | |
f616db6a | 3108 | die(_("unable to pack objects reachable from tag %s"), |
b773ddea JK |
3109 | oid_to_hex(oid)); |
3110 | ||
188960b4 | 3111 | add_object_entry(&tag->object.oid, OBJ_TAG, NULL, 0); |
b773ddea JK |
3112 | |
3113 | if (tag->tagged->type != OBJ_TAG) | |
3114 | return; | |
3115 | ||
3116 | tag = (struct tag *)tag->tagged; | |
3117 | } | |
3118 | } | |
3119 | ||
5cf88fd8 ÆAB |
3120 | static int add_ref_tag(const char *tag UNUSED, const struct object_id *oid, |
3121 | int flag UNUSED, void *cb_data UNUSED) | |
f0a24aa5 | 3122 | { |
d155254c | 3123 | struct object_id peeled; |
f0a24aa5 | 3124 | |
77db59c2 | 3125 | if (!peel_iterated_oid(oid, &peeled) && obj_is_packed(&peeled)) |
b773ddea | 3126 | add_tag_chain(oid); |
f0a24aa5 SP |
3127 | return 0; |
3128 | } | |
3129 | ||
f3123c4a JH |
3130 | static void prepare_pack(int window, int depth) |
3131 | { | |
9668cf59 | 3132 | struct object_entry **delta_list; |
6e1c2344 RJ |
3133 | uint32_t i, nr_deltas; |
3134 | unsigned n; | |
9668cf59 | 3135 | |
28b8a730 | 3136 | if (use_delta_islands) |
385cb64f | 3137 | resolve_tree_islands(the_repository, progress, &to_pack); |
28b8a730 | 3138 | |
3f9ac8d2 | 3139 | get_object_details(); |
9668cf59 | 3140 | |
0e8189e2 NP |
3141 | /* |
3142 | * If we're locally repacking then we need to be doubly careful | |
3143 | * from now on in order to make sure no stealth corruption gets | |
3144 | * propagated to the new pack. Clients receiving streamed packs | |
3145 | * should validate everything they get anyway so no need to incur | |
3146 | * the additional cost here in that case. | |
3147 | */ | |
3148 | if (!pack_to_stdout) | |
3149 | do_check_packed_object_crc = 1; | |
3150 | ||
2834bc27 | 3151 | if (!to_pack.nr_objects || !window || !depth) |
9668cf59 NP |
3152 | return; |
3153 | ||
b32fa95f | 3154 | ALLOC_ARRAY(delta_list, to_pack.nr_objects); |
75d39853 NP |
3155 | nr_deltas = n = 0; |
3156 | ||
2834bc27 VM |
3157 | for (i = 0; i < to_pack.nr_objects; i++) { |
3158 | struct object_entry *entry = to_pack.objects + i; | |
75d39853 | 3159 | |
898eba5e | 3160 | if (DELTA(entry)) |
75d39853 | 3161 | /* This happens if we decided to reuse existing |
a7de7130 | 3162 | * delta from a pack. "reuse_delta &&" is implied. |
75d39853 NP |
3163 | */ |
3164 | continue; | |
3165 | ||
ac77d0c3 NTND |
3166 | if (!entry->type_valid || |
3167 | oe_size_less_than(&to_pack, entry, 50)) | |
75d39853 NP |
3168 | continue; |
3169 | ||
3170 | if (entry->no_try_delta) | |
3171 | continue; | |
3172 | ||
6d6f9cdd | 3173 | if (!entry->preferred_base) { |
75d39853 | 3174 | nr_deltas++; |
fd9b1bae | 3175 | if (oe_type(entry) < 0) |
f616db6a | 3176 | die(_("unable to get type of object %s"), |
e6a492b7 | 3177 | oid_to_hex(&entry->idx.oid)); |
eede9f42 | 3178 | } else { |
fd9b1bae | 3179 | if (oe_type(entry) < 0) { |
eede9f42 NP |
3180 | /* |
3181 | * This object is not found, but we | |
3182 | * don't have to include it anyway. | |
3183 | */ | |
3184 | continue; | |
3185 | } | |
6d6f9cdd | 3186 | } |
75d39853 NP |
3187 | |
3188 | delta_list[n++] = entry; | |
3189 | } | |
3190 | ||
2f8b8947 | 3191 | if (nr_deltas && n > 1) { |
e334977d | 3192 | unsigned nr_done = 0; |
bb514de3 | 3193 | |
e334977d | 3194 | if (progress) |
754dbc43 | 3195 | progress_state = start_progress(_("Compressing objects"), |
dc6a0757 | 3196 | nr_deltas); |
9ed0d8d6 | 3197 | QSORT(delta_list, n, type_size_sort); |
8ecce684 | 3198 | ll_find_deltas(delta_list, n, window+1, depth, &nr_done); |
4d4fcc54 | 3199 | stop_progress(&progress_state); |
e334977d | 3200 | if (nr_done != nr_deltas) |
f616db6a | 3201 | die(_("inconsistency with delta count")); |
75d39853 | 3202 | } |
9668cf59 | 3203 | free(delta_list); |
f3123c4a JH |
3204 | } |
3205 | ||
a4e7e317 GC |
3206 | static int git_pack_config(const char *k, const char *v, |
3207 | const struct config_context *ctx, void *cb) | |
4812a93a | 3208 | { |
eeefa7c9 | 3209 | if (!strcmp(k, "pack.window")) { |
8868b1eb | 3210 | window = git_config_int(k, v, ctx->kvi); |
4812a93a JK |
3211 | return 0; |
3212 | } | |
a97773ce | 3213 | if (!strcmp(k, "pack.windowmemory")) { |
8868b1eb | 3214 | window_memory_limit = git_config_ulong(k, v, ctx->kvi); |
a97773ce BD |
3215 | return 0; |
3216 | } | |
3217 | if (!strcmp(k, "pack.depth")) { | |
8868b1eb | 3218 | depth = git_config_int(k, v, ctx->kvi); |
842aaf93 TT |
3219 | return 0; |
3220 | } | |
074b2eea | 3221 | if (!strcmp(k, "pack.deltacachesize")) { |
8868b1eb | 3222 | max_delta_cache_size = git_config_int(k, v, ctx->kvi); |
074b2eea MK |
3223 | return 0; |
3224 | } | |
e3dfddb3 | 3225 | if (!strcmp(k, "pack.deltacachelimit")) { |
8868b1eb | 3226 | cache_max_small_delta_size = git_config_int(k, v, ctx->kvi); |
e3dfddb3 MK |
3227 | return 0; |
3228 | } | |
ae4f07fb VM |
3229 | if (!strcmp(k, "pack.writebitmaphashcache")) { |
3230 | if (git_config_bool(k, v)) | |
3231 | write_bitmap_options |= BITMAP_OPT_HASH_CACHE; | |
3232 | else | |
3233 | write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE; | |
3234 | } | |
76f14b77 AC |
3235 | |
3236 | if (!strcmp(k, "pack.writebitmaplookuptable")) { | |
3237 | if (git_config_bool(k, v)) | |
3238 | write_bitmap_options |= BITMAP_OPT_LOOKUP_TABLE; | |
3239 | else | |
3240 | write_bitmap_options &= ~BITMAP_OPT_LOOKUP_TABLE; | |
3241 | } | |
3242 | ||
6b8fda2d | 3243 | if (!strcmp(k, "pack.usebitmaps")) { |
645c432d | 3244 | use_bitmap_index_default = git_config_bool(k, v); |
6b8fda2d VM |
3245 | return 0; |
3246 | } | |
e704fc79 | 3247 | if (!strcmp(k, "pack.allowpackreuse")) { |
94107413 TB |
3248 | int res = git_parse_maybe_bool_text(v); |
3249 | if (res < 0) { | |
3250 | if (!strcasecmp(v, "single")) | |
3251 | allow_pack_reuse = SINGLE_PACK_REUSE; | |
af626ac0 TB |
3252 | else if (!strcasecmp(v, "multi")) |
3253 | allow_pack_reuse = MULTI_PACK_REUSE; | |
94107413 TB |
3254 | else |
3255 | die(_("invalid pack.allowPackReuse value: '%s'"), v); | |
3256 | } else if (res) { | |
3257 | allow_pack_reuse = SINGLE_PACK_REUSE; | |
3258 | } else { | |
3259 | allow_pack_reuse = NO_PACK_REUSE; | |
3260 | } | |
e704fc79 JK |
3261 | return 0; |
3262 | } | |
693b86ff | 3263 | if (!strcmp(k, "pack.threads")) { |
8868b1eb | 3264 | delta_search_threads = git_config_int(k, v, ctx->kvi); |
833e3df1 | 3265 | if (delta_search_threads < 0) |
f616db6a | 3266 | die(_("invalid number of threads specified (%d)"), |
693b86ff | 3267 | delta_search_threads); |
9c897c5c | 3268 | if (!HAVE_THREADS && delta_search_threads != 1) { |
f616db6a | 3269 | warning(_("no threads support, ignoring %s"), k); |
2e96d815 ÆAB |
3270 | delta_search_threads = 0; |
3271 | } | |
693b86ff NP |
3272 | return 0; |
3273 | } | |
4d00bda2 | 3274 | if (!strcmp(k, "pack.indexversion")) { |
8868b1eb | 3275 | pack_idx_opts.version = git_config_int(k, v, ctx->kvi); |
ebcfb379 | 3276 | if (pack_idx_opts.version > 2) |
b4eda05d | 3277 | die(_("bad pack.indexVersion=%"PRIu32), |
ebcfb379 | 3278 | pack_idx_opts.version); |
4d00bda2 NP |
3279 | return 0; |
3280 | } | |
c9773343 TB |
3281 | if (!strcmp(k, "pack.writereverseindex")) { |
3282 | if (git_config_bool(k, v)) | |
3283 | pack_idx_opts.flags |= WRITE_REV; | |
3284 | else | |
3285 | pack_idx_opts.flags &= ~WRITE_REV; | |
3286 | return 0; | |
3287 | } | |
dd4b732d | 3288 | if (!strcmp(k, "uploadpack.blobpackfileuri")) { |
ba176db5 | 3289 | struct configured_exclusion *ex; |
dd4b732d JT |
3290 | const char *oid_end, *pack_end; |
3291 | /* | |
3292 | * Stores the pack hash. This is not a true object ID, but is | |
3293 | * of the same form. | |
3294 | */ | |
3295 | struct object_id pack_hash; | |
3296 | ||
ba176db5 JK |
3297 | if (!v) |
3298 | return config_error_nonbool(k); | |
3299 | ||
3300 | ex = xmalloc(sizeof(*ex)); | |
dd4b732d JT |
3301 | if (parse_oid_hex(v, &ex->e.oid, &oid_end) || |
3302 | *oid_end != ' ' || | |
3303 | parse_oid_hex(oid_end + 1, &pack_hash, &pack_end) || | |
3304 | *pack_end != ' ') | |
3305 | die(_("value of uploadpack.blobpackfileuri must be " | |
3306 | "of the form '<object-hash> <pack-hash> <uri>' (got '%s')"), v); | |
3307 | if (oidmap_get(&configured_exclusions, &ex->e.oid)) | |
3308 | die(_("object already configured in another " | |
3309 | "uploadpack.blobpackfileuri (got '%s')"), v); | |
3310 | ex->pack_hash_hex = xcalloc(1, pack_end - oid_end); | |
3311 | memcpy(ex->pack_hash_hex, oid_end + 1, pack_end - oid_end - 1); | |
3312 | ex->uri = xstrdup(pack_end + 1); | |
3313 | oidmap_put(&configured_exclusions, ex); | |
3314 | } | |
a4e7e317 | 3315 | return git_default_config(k, v, ctx, cb); |
4812a93a JK |
3316 | } |
3317 | ||
339bce27 TB |
3318 | /* Counters for trace2 output when in --stdin-packs mode. */ |
3319 | static int stdin_packs_found_nr; | |
3320 | static int stdin_packs_hints_nr; | |
3321 | ||
3322 | static int add_object_entry_from_pack(const struct object_id *oid, | |
3323 | struct packed_git *p, | |
3324 | uint32_t pos, | |
3325 | void *_data) | |
3326 | { | |
339bce27 | 3327 | off_t ofs; |
5045759d | 3328 | enum object_type type = OBJ_NONE; |
339bce27 TB |
3329 | |
3330 | display_progress(progress_state, ++nr_seen); | |
3331 | ||
3332 | if (have_duplicate_entry(oid, 0)) | |
3333 | return 0; | |
3334 | ||
3335 | ofs = nth_packed_object_offset(p, pos); | |
3336 | if (!want_object_in_pack(oid, 0, &p, &ofs)) | |
3337 | return 0; | |
3338 | ||
5045759d TB |
3339 | if (p) { |
3340 | struct rev_info *revs = _data; | |
3341 | struct object_info oi = OBJECT_INFO_INIT; | |
339bce27 | 3342 | |
5045759d TB |
3343 | oi.typep = &type; |
3344 | if (packed_object_info(the_repository, p, ofs, &oi) < 0) { | |
3345 | die(_("could not get type of object %s in pack %s"), | |
3346 | oid_to_hex(oid), p->pack_name); | |
3347 | } else if (type == OBJ_COMMIT) { | |
3348 | /* | |
3349 | * commits in included packs are used as starting points for the | |
3350 | * subsequent revision walk | |
3351 | */ | |
3352 | add_pending_oid(revs, NULL, oid, 0); | |
3353 | } | |
339bce27 | 3354 | |
5045759d TB |
3355 | stdin_packs_found_nr++; |
3356 | } | |
339bce27 TB |
3357 | |
3358 | create_object_entry(oid, type, 0, 0, 0, p, ofs); | |
3359 | ||
3360 | return 0; | |
3361 | } | |
3362 | ||
be252d33 JK |
3363 | static void show_commit_pack_hint(struct commit *commit UNUSED, |
3364 | void *data UNUSED) | |
339bce27 TB |
3365 | { |
3366 | /* nothing to do; commits don't have a namehash */ | |
3367 | } | |
3368 | ||
3369 | static void show_object_pack_hint(struct object *object, const char *name, | |
be252d33 | 3370 | void *data UNUSED) |
339bce27 TB |
3371 | { |
3372 | struct object_entry *oe = packlist_find(&to_pack, &object->oid); | |
3373 | if (!oe) | |
3374 | return; | |
3375 | ||
3376 | /* | |
3377 | * Our 'to_pack' list was constructed by iterating all objects packed in | |
3378 | * included packs, and so doesn't have a non-zero hash field that you | |
3379 | * would typically pick up during a reachability traversal. | |
3380 | * | |
3381 | * Make a best-effort attempt to fill in the ->hash and ->no_try_delta | |
3382 | * here using a now in order to perhaps improve the delta selection | |
3383 | * process. | |
3384 | */ | |
3385 | oe->hash = pack_name_hash(name); | |
3386 | oe->no_try_delta = name && no_try_delta(name); | |
3387 | ||
3388 | stdin_packs_hints_nr++; | |
3389 | } | |
3390 | ||
3391 | static int pack_mtime_cmp(const void *_a, const void *_b) | |
3392 | { | |
3393 | struct packed_git *a = ((const struct string_list_item*)_a)->util; | |
3394 | struct packed_git *b = ((const struct string_list_item*)_b)->util; | |
3395 | ||
3396 | /* | |
3397 | * order packs by descending mtime so that objects are laid out | |
3398 | * roughly as newest-to-oldest | |
3399 | */ | |
3400 | if (a->mtime < b->mtime) | |
3401 | return 1; | |
3402 | else if (b->mtime < a->mtime) | |
3403 | return -1; | |
3404 | else | |
3405 | return 0; | |
3406 | } | |
3407 | ||
3408 | static void read_packs_list_from_stdin(void) | |
3409 | { | |
3410 | struct strbuf buf = STRBUF_INIT; | |
3411 | struct string_list include_packs = STRING_LIST_INIT_DUP; | |
3412 | struct string_list exclude_packs = STRING_LIST_INIT_DUP; | |
3413 | struct string_list_item *item = NULL; | |
3414 | ||
3415 | struct packed_git *p; | |
3416 | struct rev_info revs; | |
3417 | ||
3418 | repo_init_revisions(the_repository, &revs, NULL); | |
3419 | /* | |
3420 | * Use a revision walk to fill in the namehash of objects in the include | |
3421 | * packs. To save time, we'll avoid traversing through objects that are | |
3422 | * in excluded packs. | |
3423 | * | |
3424 | * That may cause us to avoid populating all of the namehash fields of | |
3425 | * all included objects, but our goal is best-effort, since this is only | |
3426 | * an optimization during delta selection. | |
3427 | */ | |
3428 | revs.no_kept_objects = 1; | |
3429 | revs.keep_pack_cache_flags |= IN_CORE_KEEP_PACKS; | |
3430 | revs.blob_objects = 1; | |
3431 | revs.tree_objects = 1; | |
3432 | revs.tag_objects = 1; | |
14e7b834 | 3433 | revs.ignore_missing_links = 1; |
339bce27 TB |
3434 | |
3435 | while (strbuf_getline(&buf, stdin) != EOF) { | |
3436 | if (!buf.len) | |
3437 | continue; | |
3438 | ||
3439 | if (*buf.buf == '^') | |
3440 | string_list_append(&exclude_packs, buf.buf + 1); | |
3441 | else | |
3442 | string_list_append(&include_packs, buf.buf); | |
3443 | ||
3444 | strbuf_reset(&buf); | |
3445 | } | |
3446 | ||
3447 | string_list_sort(&include_packs); | |
732194b5 | 3448 | string_list_remove_duplicates(&include_packs, 0); |
339bce27 | 3449 | string_list_sort(&exclude_packs); |
732194b5 | 3450 | string_list_remove_duplicates(&exclude_packs, 0); |
339bce27 TB |
3451 | |
3452 | for (p = get_all_packs(the_repository); p; p = p->next) { | |
3453 | const char *pack_name = pack_basename(p); | |
3454 | ||
752b465c PS |
3455 | if ((item = string_list_lookup(&include_packs, pack_name))) |
3456 | item->util = p; | |
3457 | if ((item = string_list_lookup(&exclude_packs, pack_name))) | |
339bce27 TB |
3458 | item->util = p; |
3459 | } | |
3460 | ||
3461 | /* | |
561fa035 ÆAB |
3462 | * Arguments we got on stdin may not even be packs. First |
3463 | * check that to avoid segfaulting later on in | |
3464 | * e.g. pack_mtime_cmp(), excluded packs are handled below. | |
3465 | * | |
3466 | * Since we first parsed our STDIN and then sorted the input | |
3467 | * lines the pack we error on will be whatever line happens to | |
3468 | * sort first. This is lazy, it's enough that we report one | |
3469 | * bad case here, we don't need to report the first/last one, | |
3470 | * or all of them. | |
3471 | */ | |
3472 | for_each_string_list_item(item, &include_packs) { | |
3473 | struct packed_git *p = item->util; | |
3474 | if (!p) | |
3475 | die(_("could not find pack '%s'"), item->string); | |
5045759d TB |
3476 | if (!is_pack_valid(p)) |
3477 | die(_("packfile %s cannot be accessed"), p->pack_name); | |
561fa035 ÆAB |
3478 | } |
3479 | ||
3480 | /* | |
3481 | * Then, handle all of the excluded packs, marking them as | |
3482 | * kept in-core so that later calls to add_object_entry() | |
3483 | * discards any objects that are also found in excluded packs. | |
339bce27 TB |
3484 | */ |
3485 | for_each_string_list_item(item, &exclude_packs) { | |
3486 | struct packed_git *p = item->util; | |
3487 | if (!p) | |
3488 | die(_("could not find pack '%s'"), item->string); | |
3489 | p->pack_keep_in_core = 1; | |
3490 | } | |
3491 | ||
3492 | /* | |
3493 | * Order packs by ascending mtime; use QSORT directly to access the | |
3494 | * string_list_item's ->util pointer, which string_list_sort() does not | |
3495 | * provide. | |
3496 | */ | |
3497 | QSORT(include_packs.items, include_packs.nr, pack_mtime_cmp); | |
3498 | ||
3499 | for_each_string_list_item(item, &include_packs) { | |
3500 | struct packed_git *p = item->util; | |
339bce27 TB |
3501 | for_each_object_in_pack(p, |
3502 | add_object_entry_from_pack, | |
3503 | &revs, | |
3504 | FOR_EACH_OBJECT_PACK_ORDER); | |
3505 | } | |
3506 | ||
3507 | if (prepare_revision_walk(&revs)) | |
3508 | die(_("revision walk setup failed")); | |
3509 | traverse_commit_list(&revs, | |
3510 | show_commit_pack_hint, | |
3511 | show_object_pack_hint, | |
3512 | NULL); | |
3513 | ||
3514 | trace2_data_intmax("pack-objects", the_repository, "stdin_packs_found", | |
3515 | stdin_packs_found_nr); | |
3516 | trace2_data_intmax("pack-objects", the_repository, "stdin_packs_hints", | |
3517 | stdin_packs_hints_nr); | |
3518 | ||
3519 | strbuf_release(&buf); | |
3520 | string_list_clear(&include_packs, 0); | |
3521 | string_list_clear(&exclude_packs, 0); | |
3522 | } | |
3523 | ||
b7573536 TB |
3524 | static void add_cruft_object_entry(const struct object_id *oid, enum object_type type, |
3525 | struct packed_git *pack, off_t offset, | |
3526 | const char *name, uint32_t mtime) | |
3527 | { | |
3528 | struct object_entry *entry; | |
3529 | ||
3530 | display_progress(progress_state, ++nr_seen); | |
3531 | ||
3532 | entry = packlist_find(&to_pack, oid); | |
3533 | if (entry) { | |
3534 | if (name) { | |
3535 | entry->hash = pack_name_hash(name); | |
3536 | entry->no_try_delta = no_try_delta(name); | |
3537 | } | |
3538 | } else { | |
3539 | if (!want_object_in_pack(oid, 0, &pack, &offset)) | |
3540 | return; | |
3541 | if (!pack && type == OBJ_BLOB && !has_loose_object(oid)) { | |
3542 | /* | |
3543 | * If a traversed tree has a missing blob then we want | |
3544 | * to avoid adding that missing object to our pack. | |
3545 | * | |
3546 | * This only applies to missing blobs, not trees, | |
3547 | * because the traversal needs to parse sub-trees but | |
3548 | * not blobs. | |
3549 | * | |
3550 | * Note we only perform this check when we couldn't | |
3551 | * already find the object in a pack, so we're really | |
3552 | * limited to "ensure non-tip blobs which don't exist in | |
3553 | * packs do exist via loose objects". Confused? | |
3554 | */ | |
3555 | return; | |
3556 | } | |
3557 | ||
3558 | entry = create_object_entry(oid, type, pack_name_hash(name), | |
3559 | 0, name && no_try_delta(name), | |
3560 | pack, offset); | |
3561 | } | |
3562 | ||
3563 | if (mtime > oe_cruft_mtime(&to_pack, entry)) | |
3564 | oe_set_cruft_mtime(&to_pack, entry, mtime); | |
3565 | return; | |
3566 | } | |
3567 | ||
c50dca2a | 3568 | static void show_cruft_object(struct object *obj, const char *name, void *data UNUSED) |
a7d49383 TB |
3569 | { |
3570 | /* | |
3571 | * if we did not record it earlier, it's at least as old as our | |
3572 | * expiration value. Rather than find it exactly, just use that | |
3573 | * value. This may bump it forward from its real mtime, but it | |
3574 | * will still be "too old" next time we run with the same | |
3575 | * expiration. | |
3576 | * | |
3577 | * if obj does appear in the packing list, this call is a noop (or may | |
3578 | * set the namehash). | |
3579 | */ | |
3580 | add_cruft_object_entry(&obj->oid, obj->type, NULL, 0, name, cruft_expiration); | |
3581 | } | |
3582 | ||
3583 | static void show_cruft_commit(struct commit *commit, void *data) | |
3584 | { | |
3585 | show_cruft_object((struct object*)commit, NULL, data); | |
3586 | } | |
3587 | ||
c50dca2a | 3588 | static int cruft_include_check_obj(struct object *obj, void *data UNUSED) |
a7d49383 TB |
3589 | { |
3590 | return !has_object_kept_pack(&obj->oid, IN_CORE_KEEP_PACKS); | |
3591 | } | |
3592 | ||
3593 | static int cruft_include_check(struct commit *commit, void *data) | |
3594 | { | |
3595 | return cruft_include_check_obj((struct object*)commit, data); | |
3596 | } | |
3597 | ||
3598 | static void set_cruft_mtime(const struct object *object, | |
3599 | struct packed_git *pack, | |
3600 | off_t offset, time_t mtime) | |
3601 | { | |
3602 | add_cruft_object_entry(&object->oid, object->type, pack, offset, NULL, | |
3603 | mtime); | |
3604 | } | |
3605 | ||
b7573536 TB |
3606 | static void mark_pack_kept_in_core(struct string_list *packs, unsigned keep) |
3607 | { | |
3608 | struct string_list_item *item = NULL; | |
3609 | for_each_string_list_item(item, packs) { | |
3610 | struct packed_git *p = item->util; | |
3611 | if (!p) | |
3612 | die(_("could not find pack '%s'"), item->string); | |
3613 | p->pack_keep_in_core = keep; | |
3614 | } | |
3615 | } | |
3616 | ||
3617 | static void add_unreachable_loose_objects(void); | |
3618 | static void add_objects_in_unpacked_packs(void); | |
3619 | ||
3620 | static void enumerate_cruft_objects(void) | |
3621 | { | |
3622 | if (progress) | |
3623 | progress_state = start_progress(_("Enumerating cruft objects"), 0); | |
3624 | ||
3625 | add_objects_in_unpacked_packs(); | |
3626 | add_unreachable_loose_objects(); | |
3627 | ||
3628 | stop_progress(&progress_state); | |
3629 | } | |
3630 | ||
a7d49383 TB |
3631 | static void enumerate_and_traverse_cruft_objects(struct string_list *fresh_packs) |
3632 | { | |
3633 | struct packed_git *p; | |
3634 | struct rev_info revs; | |
3635 | int ret; | |
3636 | ||
3637 | repo_init_revisions(the_repository, &revs, NULL); | |
3638 | ||
3639 | revs.tag_objects = 1; | |
3640 | revs.tree_objects = 1; | |
3641 | revs.blob_objects = 1; | |
3642 | ||
3643 | revs.include_check = cruft_include_check; | |
3644 | revs.include_check_obj = cruft_include_check_obj; | |
3645 | ||
3646 | revs.ignore_missing_links = 1; | |
3647 | ||
3648 | if (progress) | |
3649 | progress_state = start_progress(_("Enumerating cruft objects"), 0); | |
3650 | ret = add_unseen_recent_objects_to_traversal(&revs, cruft_expiration, | |
3651 | set_cruft_mtime, 1); | |
3652 | stop_progress(&progress_state); | |
3653 | ||
3654 | if (ret) | |
3655 | die(_("unable to add cruft objects")); | |
3656 | ||
3657 | /* | |
3658 | * Re-mark only the fresh packs as kept so that objects in | |
3659 | * unknown packs do not halt the reachability traversal early. | |
3660 | */ | |
3661 | for (p = get_all_packs(the_repository); p; p = p->next) | |
3662 | p->pack_keep_in_core = 0; | |
3663 | mark_pack_kept_in_core(fresh_packs, 1); | |
3664 | ||
3665 | if (prepare_revision_walk(&revs)) | |
3666 | die(_("revision walk setup failed")); | |
3667 | if (progress) | |
3668 | progress_state = start_progress(_("Traversing cruft objects"), 0); | |
3669 | nr_seen = 0; | |
3670 | traverse_commit_list(&revs, show_cruft_commit, show_cruft_object, NULL); | |
3671 | ||
3672 | stop_progress(&progress_state); | |
3673 | } | |
3674 | ||
b7573536 TB |
3675 | static void read_cruft_objects(void) |
3676 | { | |
3677 | struct strbuf buf = STRBUF_INIT; | |
3678 | struct string_list discard_packs = STRING_LIST_INIT_DUP; | |
3679 | struct string_list fresh_packs = STRING_LIST_INIT_DUP; | |
3680 | struct packed_git *p; | |
3681 | ||
3682 | ignore_packed_keep_in_core = 1; | |
3683 | ||
3684 | while (strbuf_getline(&buf, stdin) != EOF) { | |
3685 | if (!buf.len) | |
3686 | continue; | |
3687 | ||
3688 | if (*buf.buf == '-') | |
3689 | string_list_append(&discard_packs, buf.buf + 1); | |
3690 | else | |
3691 | string_list_append(&fresh_packs, buf.buf); | |
b7573536 TB |
3692 | } |
3693 | ||
3694 | string_list_sort(&discard_packs); | |
3695 | string_list_sort(&fresh_packs); | |
3696 | ||
3697 | for (p = get_all_packs(the_repository); p; p = p->next) { | |
3698 | const char *pack_name = pack_basename(p); | |
3699 | struct string_list_item *item; | |
3700 | ||
3701 | item = string_list_lookup(&fresh_packs, pack_name); | |
3702 | if (!item) | |
3703 | item = string_list_lookup(&discard_packs, pack_name); | |
3704 | ||
3705 | if (item) { | |
3706 | item->util = p; | |
3707 | } else { | |
3708 | /* | |
3709 | * This pack wasn't mentioned in either the "fresh" or | |
3710 | * "discard" list, so the caller didn't know about it. | |
3711 | * | |
3712 | * Mark it as kept so that its objects are ignored by | |
3713 | * add_unseen_recent_objects_to_traversal(). We'll | |
3714 | * unmark it before starting the traversal so it doesn't | |
3715 | * halt the traversal early. | |
3716 | */ | |
3717 | p->pack_keep_in_core = 1; | |
3718 | } | |
3719 | } | |
3720 | ||
3721 | mark_pack_kept_in_core(&fresh_packs, 1); | |
3722 | mark_pack_kept_in_core(&discard_packs, 0); | |
3723 | ||
3724 | if (cruft_expiration) | |
a7d49383 | 3725 | enumerate_and_traverse_cruft_objects(&fresh_packs); |
b7573536 TB |
3726 | else |
3727 | enumerate_cruft_objects(); | |
3728 | ||
3729 | strbuf_release(&buf); | |
3730 | string_list_clear(&discard_packs, 0); | |
3731 | string_list_clear(&fresh_packs, 0); | |
3732 | } | |
3733 | ||
b5d97e6b | 3734 | static void read_object_list_from_stdin(void) |
c323ac7d | 3735 | { |
188960b4 | 3736 | char line[GIT_MAX_HEXSZ + 1 + PATH_MAX + 2]; |
3737 | struct object_id oid; | |
3738 | const char *p; | |
b2504a0d | 3739 | |
da93d12b | 3740 | for (;;) { |
da93d12b LT |
3741 | if (!fgets(line, sizeof(line), stdin)) { |
3742 | if (feof(stdin)) | |
3743 | break; | |
3744 | if (!ferror(stdin)) | |
5867757d | 3745 | BUG("fgets returned NULL, not EOF, not error!"); |
687dd75c | 3746 | if (errno != EINTR) |
d824cbba | 3747 | die_errno("fgets"); |
687dd75c JH |
3748 | clearerr(stdin); |
3749 | continue; | |
da93d12b | 3750 | } |
7a979d99 | 3751 | if (line[0] == '-') { |
188960b4 | 3752 | if (get_oid_hex(line+1, &oid)) |
f616db6a | 3753 | die(_("expected edge object ID, got garbage:\n %s"), |
b5d97e6b | 3754 | line); |
188960b4 | 3755 | add_preferred_base(&oid); |
7a979d99 | 3756 | continue; |
21fcd1bd | 3757 | } |
188960b4 | 3758 | if (parse_oid_hex(line, &oid, &p)) |
f616db6a | 3759 | die(_("expected object ID, got garbage:\n %s"), line); |
b5d97e6b | 3760 | |
188960b4 | 3761 | add_preferred_base_object(p + 1); |
fd9b1bae | 3762 | add_object_entry(&oid, OBJ_NONE, p + 1, 0); |
c323ac7d | 3763 | } |
b5d97e6b JH |
3764 | } |
3765 | ||
c50dca2a | 3766 | static void show_commit(struct commit *commit, void *data UNUSED) |
b5d97e6b | 3767 | { |
188960b4 | 3768 | add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL, 0); |
7cc8f971 VM |
3769 | |
3770 | if (write_bitmap_index) | |
3771 | index_commit_for_bitmap(commit); | |
28b8a730 JK |
3772 | |
3773 | if (use_delta_islands) | |
3774 | propagate_island_marks(commit); | |
b5d97e6b JH |
3775 | } |
3776 | ||
c50dca2a JK |
3777 | static void show_object(struct object *obj, const char *name, |
3778 | void *data UNUSED) | |
b5d97e6b | 3779 | { |
8d2dfc49 | 3780 | add_preferred_base_object(name); |
188960b4 | 3781 | add_object_entry(&obj->oid, obj->type, name, 0); |
28b8a730 JK |
3782 | |
3783 | if (use_delta_islands) { | |
3784 | const char *p; | |
39490536 | 3785 | unsigned depth; |
28b8a730 JK |
3786 | struct object_entry *ent; |
3787 | ||
39490536 JK |
3788 | /* the empty string is a root tree, which is depth 0 */ |
3789 | depth = *name ? 1 : 0; | |
28b8a730 JK |
3790 | for (p = strchr(name, '/'); p; p = strchr(p + 1, '/')) |
3791 | depth++; | |
3792 | ||
3a37876b | 3793 | ent = packlist_find(&to_pack, &obj->oid); |
108f5303 CC |
3794 | if (ent && depth > oe_tree_depth(&to_pack, ent)) |
3795 | oe_set_tree_depth(&to_pack, ent, depth); | |
28b8a730 | 3796 | } |
b5d97e6b JH |
3797 | } |
3798 | ||
9535ce73 JH |
3799 | static void show_object__ma_allow_any(struct object *obj, const char *name, void *data) |
3800 | { | |
3801 | assert(arg_missing_action == MA_ALLOW_ANY); | |
3802 | ||
3803 | /* | |
3804 | * Quietly ignore ALL missing objects. This avoids problems with | |
3805 | * staging them now and getting an odd error later. | |
3806 | */ | |
ee47243d | 3807 | if (!has_object(the_repository, &obj->oid, 0)) |
9535ce73 JH |
3808 | return; |
3809 | ||
3810 | show_object(obj, name, data); | |
3811 | } | |
3812 | ||
0c16cd49 JT |
3813 | static void show_object__ma_allow_promisor(struct object *obj, const char *name, void *data) |
3814 | { | |
3815 | assert(arg_missing_action == MA_ALLOW_PROMISOR); | |
3816 | ||
3817 | /* | |
3818 | * Quietly ignore EXPECTED missing objects. This avoids problems with | |
3819 | * staging them now and getting an odd error later. | |
3820 | */ | |
ee47243d | 3821 | if (!has_object(the_repository, &obj->oid, 0) && is_promisor_object(&obj->oid)) |
0c16cd49 JT |
3822 | return; |
3823 | ||
3824 | show_object(obj, name, data); | |
3825 | } | |
3826 | ||
34bf44f2 | 3827 | static int option_parse_missing_action(const struct option *opt UNUSED, |
9535ce73 JH |
3828 | const char *arg, int unset) |
3829 | { | |
3830 | assert(arg); | |
3831 | assert(!unset); | |
3832 | ||
3833 | if (!strcmp(arg, "error")) { | |
3834 | arg_missing_action = MA_ERROR; | |
3835 | fn_show_object = show_object; | |
3836 | return 0; | |
3837 | } | |
3838 | ||
3839 | if (!strcmp(arg, "allow-any")) { | |
3840 | arg_missing_action = MA_ALLOW_ANY; | |
0c16cd49 | 3841 | fetch_if_missing = 0; |
9535ce73 JH |
3842 | fn_show_object = show_object__ma_allow_any; |
3843 | return 0; | |
3844 | } | |
3845 | ||
0c16cd49 JT |
3846 | if (!strcmp(arg, "allow-promisor")) { |
3847 | arg_missing_action = MA_ALLOW_PROMISOR; | |
3848 | fetch_if_missing = 0; | |
3849 | fn_show_object = show_object__ma_allow_promisor; | |
3850 | return 0; | |
3851 | } | |
3852 | ||
1a8aea85 | 3853 | die(_("invalid value for '%s': '%s'"), "--missing", arg); |
9535ce73 JH |
3854 | return 0; |
3855 | } | |
3856 | ||
8d1d8f83 JH |
3857 | static void show_edge(struct commit *commit) |
3858 | { | |
188960b4 | 3859 | add_preferred_base(&commit->object.oid); |
8d1d8f83 JH |
3860 | } |
3861 | ||
a9fd2f20 TB |
3862 | static int add_object_in_unpacked_pack(const struct object_id *oid, |
3863 | struct packed_git *pack, | |
3864 | uint32_t pos, | |
be252d33 | 3865 | void *data UNUSED) |
08cdfb13 | 3866 | { |
b7573536 TB |
3867 | if (cruft) { |
3868 | off_t offset; | |
3869 | time_t mtime; | |
3870 | ||
3871 | if (pack->is_cruft) { | |
3872 | if (load_pack_mtimes(pack) < 0) | |
3873 | die(_("could not load cruft pack .mtimes")); | |
3874 | mtime = nth_packed_mtime(pack, pos); | |
3875 | } else { | |
3876 | mtime = pack->mtime; | |
3877 | } | |
3878 | offset = nth_packed_object_offset(pack, pos); | |
3879 | ||
3880 | add_cruft_object_entry(oid, OBJ_NONE, pack, offset, | |
3881 | NULL, mtime); | |
3882 | } else { | |
3883 | add_object_entry(oid, OBJ_NONE, "", 0); | |
3884 | } | |
a9fd2f20 | 3885 | return 0; |
08cdfb13 JH |
3886 | } |
3887 | ||
7a2a7216 | 3888 | static void add_objects_in_unpacked_packs(void) |
08cdfb13 | 3889 | { |
a9fd2f20 TB |
3890 | if (for_each_packed_object(add_object_in_unpacked_pack, NULL, |
3891 | FOR_EACH_OBJECT_PACK_ORDER | | |
3892 | FOR_EACH_OBJECT_LOCAL_ONLY | | |
3893 | FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS | | |
3894 | FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS)) | |
3895 | die(_("cannot open pack index")); | |
08cdfb13 JH |
3896 | } |
3897 | ||
76c1d9a0 | 3898 | static int add_loose_object(const struct object_id *oid, const char *path, |
be252d33 | 3899 | void *data UNUSED) |
e26a8c47 | 3900 | { |
0df8e965 | 3901 | enum object_type type = oid_object_info(the_repository, oid, NULL); |
e26a8c47 JK |
3902 | |
3903 | if (type < 0) { | |
f616db6a | 3904 | warning(_("loose object at %s could not be examined"), path); |
e26a8c47 JK |
3905 | return 0; |
3906 | } | |
3907 | ||
b7573536 TB |
3908 | if (cruft) { |
3909 | struct stat st; | |
3910 | if (stat(path, &st) < 0) { | |
3911 | if (errno == ENOENT) | |
3912 | return 0; | |
3913 | return error_errno("unable to stat %s", oid_to_hex(oid)); | |
3914 | } | |
3915 | ||
3916 | add_cruft_object_entry(oid, type, NULL, 0, NULL, | |
3917 | st.st_mtime); | |
3918 | } else { | |
3919 | add_object_entry(oid, type, "", 0); | |
3920 | } | |
e26a8c47 JK |
3921 | return 0; |
3922 | } | |
3923 | ||
3924 | /* | |
3925 | * We actually don't even have to worry about reachability here. | |
3926 | * add_object_entry will weed out duplicates, so we just add every | |
3927 | * loose object we find. | |
3928 | */ | |
3929 | static void add_unreachable_loose_objects(void) | |
3930 | { | |
3931 | for_each_loose_file_in_objdir(get_object_directory(), | |
3932 | add_loose_object, | |
3933 | NULL, NULL, NULL); | |
3934 | } | |
3935 | ||
188960b4 | 3936 | static int has_sha1_pack_kept_or_nonlocal(const struct object_id *oid) |
094085e3 BC |
3937 | { |
3938 | static struct packed_git *last_found = (void *)1; | |
3939 | struct packed_git *p; | |
3940 | ||
a80d72db | 3941 | p = (last_found != (void *)1) ? last_found : |
454ea2e4 | 3942 | get_all_packs(the_repository); |
094085e3 BC |
3943 | |
3944 | while (p) { | |
ed7e5fc3 NTND |
3945 | if ((!p->pack_local || p->pack_keep || |
3946 | p->pack_keep_in_core) && | |
188960b4 | 3947 | find_pack_entry_one(oid->hash, p)) { |
094085e3 BC |
3948 | last_found = p; |
3949 | return 1; | |
3950 | } | |
3951 | if (p == last_found) | |
454ea2e4 | 3952 | p = get_all_packs(the_repository); |
094085e3 BC |
3953 | else |
3954 | p = p->next; | |
3955 | if (p == last_found) | |
3956 | p = p->next; | |
3957 | } | |
3958 | return 0; | |
3959 | } | |
3960 | ||
abcb8655 JK |
3961 | /* |
3962 | * Store a list of sha1s that are should not be discarded | |
3963 | * because they are either written too recently, or are | |
3964 | * reachable from another object that was. | |
3965 | * | |
3966 | * This is filled by get_object_list. | |
3967 | */ | |
910650d2 | 3968 | static struct oid_array recent_objects; |
abcb8655 | 3969 | |
4ce3621a | 3970 | static int loosened_object_can_be_discarded(const struct object_id *oid, |
dddbad72 | 3971 | timestamp_t mtime) |
d0d46abc JK |
3972 | { |
3973 | if (!unpack_unreachable_expiration) | |
3974 | return 0; | |
3975 | if (mtime > unpack_unreachable_expiration) | |
3976 | return 0; | |
910650d2 | 3977 | if (oid_array_lookup(&recent_objects, oid) >= 0) |
abcb8655 | 3978 | return 0; |
d0d46abc JK |
3979 | return 1; |
3980 | } | |
3981 | ||
7a2a7216 | 3982 | static void loosen_unused_packed_objects(void) |
ca11b212 NP |
3983 | { |
3984 | struct packed_git *p; | |
3985 | uint32_t i; | |
a643157d | 3986 | uint32_t loosened_objects_nr = 0; |
4ce3621a | 3987 | struct object_id oid; |
ca11b212 | 3988 | |
454ea2e4 | 3989 | for (p = get_all_packs(the_repository); p; p = p->next) { |
ed7e5fc3 | 3990 | if (!p->pack_local || p->pack_keep || p->pack_keep_in_core) |
ca11b212 NP |
3991 | continue; |
3992 | ||
3993 | if (open_pack_index(p)) | |
f616db6a | 3994 | die(_("cannot open pack index")); |
ca11b212 NP |
3995 | |
3996 | for (i = 0; i < p->num_objects; i++) { | |
0763671b | 3997 | nth_packed_object_id(&oid, p, i); |
3a37876b | 3998 | if (!packlist_find(&to_pack, &oid) && |
188960b4 | 3999 | !has_sha1_pack_kept_or_nonlocal(&oid) && |
a643157d | 4000 | !loosened_object_can_be_discarded(&oid, p->mtime)) { |
4bdb70a4 | 4001 | if (force_object_loose(&oid, p->mtime)) |
f616db6a | 4002 | die(_("unable to force loose object")); |
a643157d RS |
4003 | loosened_objects_nr++; |
4004 | } | |
ca11b212 NP |
4005 | } |
4006 | } | |
a643157d RS |
4007 | |
4008 | trace2_data_intmax("pack-objects", the_repository, | |
4009 | "loosen_unused_packed_objects/loosened", loosened_objects_nr); | |
ca11b212 NP |
4010 | } |
4011 | ||
69e4b342 | 4012 | /* |
645c432d KS |
4013 | * This tracks any options which pack-reuse code expects to be on, or which a |
4014 | * reader of the pack might not understand, and which would therefore prevent | |
4015 | * blind reuse of what we have on disk. | |
69e4b342 JK |
4016 | */ |
4017 | static int pack_options_allow_reuse(void) | |
4018 | { | |
94107413 | 4019 | return allow_pack_reuse != NO_PACK_REUSE && |
e704fc79 | 4020 | pack_to_stdout && |
ed7e5fc3 NTND |
4021 | !ignore_packed_keep_on_disk && |
4022 | !ignore_packed_keep_in_core && | |
9df4a607 JK |
4023 | (!local || !have_non_local_packs) && |
4024 | !incremental; | |
69e4b342 JK |
4025 | } |
4026 | ||
6b8fda2d VM |
4027 | static int get_object_list_from_bitmap(struct rev_info *revs) |
4028 | { | |
09d4a79e | 4029 | if (!(bitmap_git = prepare_bitmap_walk(revs, 0))) |
6b8fda2d VM |
4030 | return -1; |
4031 | ||
35e156b9 | 4032 | if (pack_options_allow_reuse()) |
073b40eb TB |
4033 | reuse_partial_packfile_from_bitmap(bitmap_git, |
4034 | &reuse_packfiles, | |
4035 | &reuse_packfiles_nr, | |
af626ac0 TB |
4036 | &reuse_packfile_bitmap, |
4037 | allow_pack_reuse == MULTI_PACK_REUSE); | |
35e156b9 | 4038 | |
073b40eb | 4039 | if (reuse_packfiles) { |
35e156b9 TB |
4040 | reuse_packfile_objects = bitmap_popcount(reuse_packfile_bitmap); |
4041 | if (!reuse_packfile_objects) | |
4042 | BUG("expected non-empty reuse bitmap"); | |
4043 | ||
6b8fda2d | 4044 | nr_result += reuse_packfile_objects; |
8e118e84 JK |
4045 | nr_seen += reuse_packfile_objects; |
4046 | display_progress(progress_state, nr_seen); | |
6b8fda2d VM |
4047 | } |
4048 | ||
4eb707eb JK |
4049 | traverse_bitmap_commit_list(bitmap_git, revs, |
4050 | &add_object_entry_from_bitmap); | |
6b8fda2d VM |
4051 | return 0; |
4052 | } | |
4053 | ||
abcb8655 | 4054 | static void record_recent_object(struct object *obj, |
be252d33 JK |
4055 | const char *name UNUSED, |
4056 | void *data UNUSED) | |
abcb8655 | 4057 | { |
910650d2 | 4058 | oid_array_append(&recent_objects, &obj->oid); |
abcb8655 JK |
4059 | } |
4060 | ||
be252d33 | 4061 | static void record_recent_commit(struct commit *commit, void *data UNUSED) |
abcb8655 | 4062 | { |
910650d2 | 4063 | oid_array_append(&recent_objects, &commit->object.oid); |
abcb8655 JK |
4064 | } |
4065 | ||
3f267a11 | 4066 | static int mark_bitmap_preferred_tip(const char *refname, |
63e14ee2 | 4067 | const struct object_id *oid, |
5cf88fd8 ÆAB |
4068 | int flags UNUSED, |
4069 | void *data UNUSED) | |
3f267a11 TB |
4070 | { |
4071 | struct object_id peeled; | |
4072 | struct object *object; | |
4073 | ||
4074 | if (!peel_iterated_oid(oid, &peeled)) | |
4075 | oid = &peeled; | |
4076 | ||
4077 | object = parse_object_or_die(oid, refname); | |
4078 | if (object->type == OBJ_COMMIT) | |
4079 | object->flags |= NEEDS_BITMAP; | |
4080 | ||
4081 | return 0; | |
4082 | } | |
4083 | ||
4084 | static void mark_bitmap_preferred_tips(void) | |
4085 | { | |
4086 | struct string_list_item *item; | |
4087 | const struct string_list *preferred_tips; | |
4088 | ||
4089 | preferred_tips = bitmap_preferred_tips(the_repository); | |
4090 | if (!preferred_tips) | |
4091 | return; | |
4092 | ||
4093 | for_each_string_list_item(item, preferred_tips) { | |
4094 | for_each_ref_in(item->string, mark_bitmap_preferred_tip, NULL); | |
4095 | } | |
4096 | } | |
4097 | ||
80f6de4f | 4098 | static void get_object_list(struct rev_info *revs, int ac, const char **av) |
b5d97e6b | 4099 | { |
bbcde41a MD |
4100 | struct setup_revision_opt s_r_opt = { |
4101 | .allow_exclude_promisor_objects = 1, | |
4102 | }; | |
b5d97e6b | 4103 | char line[1000]; |
b5d97e6b | 4104 | int flags = 0; |
a4544b31 | 4105 | int save_warning; |
b5d97e6b | 4106 | |
b5d97e6b | 4107 | save_commit_buffer = 0; |
80f6de4f | 4108 | setup_revisions(ac, av, revs, &s_r_opt); |
b5d97e6b | 4109 | |
b790e0f6 | 4110 | /* make sure shallows are read */ |
c8813487 | 4111 | is_repository_shallow(the_repository); |
b790e0f6 | 4112 | |
a4544b31 DS |
4113 | save_warning = warn_on_object_refname_ambiguity; |
4114 | warn_on_object_refname_ambiguity = 0; | |
4115 | ||
b5d97e6b JH |
4116 | while (fgets(line, sizeof(line), stdin) != NULL) { |
4117 | int len = strlen(line); | |
872c930d | 4118 | if (len && line[len - 1] == '\n') |
b5d97e6b JH |
4119 | line[--len] = 0; |
4120 | if (!len) | |
4121 | break; | |
4122 | if (*line == '-') { | |
4123 | if (!strcmp(line, "--not")) { | |
4124 | flags ^= UNINTERESTING; | |
7cc8f971 | 4125 | write_bitmap_index = 0; |
b5d97e6b JH |
4126 | continue; |
4127 | } | |
b790e0f6 | 4128 | if (starts_with(line, "--shallow ")) { |
e92b848c | 4129 | struct object_id oid; |
4130 | if (get_oid_hex(line + 10, &oid)) | |
4279000d | 4131 | die("not an object name '%s'", line + 10); |
19143f13 | 4132 | register_shallow(the_repository, &oid); |
f7f91086 | 4133 | use_bitmap_index = 0; |
b790e0f6 NTND |
4134 | continue; |
4135 | } | |
f616db6a | 4136 | die(_("not a rev '%s'"), line); |
b5d97e6b | 4137 | } |
80f6de4f | 4138 | if (handle_revision_arg(line, revs, flags, REVARG_CANNOT_BE_FILENAME)) |
f616db6a | 4139 | die(_("bad revision '%s'"), line); |
b5d97e6b JH |
4140 | } |
4141 | ||
a4544b31 DS |
4142 | warn_on_object_refname_ambiguity = save_warning; |
4143 | ||
80f6de4f | 4144 | if (use_bitmap_index && !get_object_list_from_bitmap(revs)) |
6b8fda2d VM |
4145 | return; |
4146 | ||
28b8a730 | 4147 | if (use_delta_islands) |
bdbdf42f | 4148 | load_delta_islands(the_repository, progress); |
28b8a730 | 4149 | |
3f267a11 TB |
4150 | if (write_bitmap_index) |
4151 | mark_bitmap_preferred_tips(); | |
4152 | ||
80f6de4f | 4153 | if (prepare_revision_walk(revs)) |
f616db6a | 4154 | die(_("revision walk setup failed")); |
80f6de4f | 4155 | mark_edges_uninteresting(revs, show_edge, sparse); |
9535ce73 JH |
4156 | |
4157 | if (!fn_show_object) | |
4158 | fn_show_object = show_object; | |
80f6de4f | 4159 | traverse_commit_list(revs, |
3e0370a8 DS |
4160 | show_commit, fn_show_object, |
4161 | NULL); | |
08cdfb13 | 4162 | |
abcb8655 | 4163 | if (unpack_unreachable_expiration) { |
80f6de4f DS |
4164 | revs->ignore_missing_links = 1; |
4165 | if (add_unseen_recent_objects_to_traversal(revs, | |
2fb90409 | 4166 | unpack_unreachable_expiration, NULL, 0)) |
f616db6a | 4167 | die(_("unable to add recent objects")); |
80f6de4f | 4168 | if (prepare_revision_walk(revs)) |
f616db6a | 4169 | die(_("revision walk setup failed")); |
80f6de4f | 4170 | traverse_commit_list(revs, record_recent_commit, |
abcb8655 JK |
4171 | record_recent_object, NULL); |
4172 | } | |
4173 | ||
08cdfb13 | 4174 | if (keep_unreachable) |
7a2a7216 | 4175 | add_objects_in_unpacked_packs(); |
e26a8c47 JK |
4176 | if (pack_loose_unreachable) |
4177 | add_unreachable_loose_objects(); | |
ca11b212 | 4178 | if (unpack_unreachable) |
7a2a7216 | 4179 | loosen_unused_packed_objects(); |
abcb8655 | 4180 | |
910650d2 | 4181 | oid_array_clear(&recent_objects); |
b5d97e6b JH |
4182 | } |
4183 | ||
ed7e5fc3 NTND |
4184 | static void add_extra_kept_packs(const struct string_list *names) |
4185 | { | |
4186 | struct packed_git *p; | |
4187 | ||
4188 | if (!names->nr) | |
4189 | return; | |
4190 | ||
454ea2e4 | 4191 | for (p = get_all_packs(the_repository); p; p = p->next) { |
ed7e5fc3 NTND |
4192 | const char *name = basename(p->pack_name); |
4193 | int i; | |
4194 | ||
4195 | if (!p->pack_local) | |
4196 | continue; | |
4197 | ||
4198 | for (i = 0; i < names->nr; i++) | |
4199 | if (!fspathcmp(name, names->items[i].string)) | |
4200 | break; | |
4201 | ||
4202 | if (i < names->nr) { | |
4203 | p->pack_keep_in_core = 1; | |
4204 | ignore_packed_keep_in_core = 1; | |
4205 | continue; | |
4206 | } | |
4207 | } | |
4208 | } | |
4209 | ||
36f76d2a RS |
4210 | static int option_parse_quiet(const struct option *opt, const char *arg, |
4211 | int unset) | |
4212 | { | |
66e33092 JK |
4213 | int *val = opt->value; |
4214 | ||
36f76d2a RS |
4215 | BUG_ON_OPT_ARG(arg); |
4216 | ||
4217 | if (!unset) | |
66e33092 JK |
4218 | *val = 0; |
4219 | else if (!*val) | |
4220 | *val = 1; | |
36f76d2a RS |
4221 | return 0; |
4222 | } | |
4223 | ||
99fb6e04 NTND |
4224 | static int option_parse_index_version(const struct option *opt, |
4225 | const char *arg, int unset) | |
4226 | { | |
66e33092 | 4227 | struct pack_idx_option *popts = opt->value; |
99fb6e04 NTND |
4228 | char *c; |
4229 | const char *val = arg; | |
517fe807 JK |
4230 | |
4231 | BUG_ON_OPT_NEG(unset); | |
4232 | ||
66e33092 JK |
4233 | popts->version = strtoul(val, &c, 10); |
4234 | if (popts->version > 2) | |
99fb6e04 NTND |
4235 | die(_("unsupported index version %s"), val); |
4236 | if (*c == ',' && c[1]) | |
66e33092 JK |
4237 | popts->off32_limit = strtoul(c+1, &c, 0); |
4238 | if (*c || popts->off32_limit & 0x80000000) | |
99fb6e04 NTND |
4239 | die(_("bad index version '%s'"), val); |
4240 | return 0; | |
4241 | } | |
4242 | ||
34bf44f2 | 4243 | static int option_parse_unpack_unreachable(const struct option *opt UNUSED, |
7e52f566 JK |
4244 | const char *arg, int unset) |
4245 | { | |
4246 | if (unset) { | |
4247 | unpack_unreachable = 0; | |
4248 | unpack_unreachable_expiration = 0; | |
4249 | } | |
4250 | else { | |
4251 | unpack_unreachable = 1; | |
4252 | if (arg) | |
4253 | unpack_unreachable_expiration = approxidate(arg); | |
4254 | } | |
4255 | return 0; | |
4256 | } | |
4257 | ||
34bf44f2 | 4258 | static int option_parse_cruft_expiration(const struct option *opt UNUSED, |
b7573536 TB |
4259 | const char *arg, int unset) |
4260 | { | |
4261 | if (unset) { | |
4262 | cruft = 0; | |
4263 | cruft_expiration = 0; | |
4264 | } else { | |
4265 | cruft = 1; | |
4266 | if (arg) | |
4267 | cruft_expiration = approxidate(arg); | |
4268 | } | |
4269 | return 0; | |
4270 | } | |
4271 | ||
b5d97e6b JH |
4272 | int cmd_pack_objects(int argc, const char **argv, const char *prefix) |
4273 | { | |
b5d97e6b | 4274 | int use_internal_rev_list = 0; |
2dacf26d | 4275 | int shallow = 0; |
4f366275 | 4276 | int all_progress_implied = 0; |
22f9b7f3 | 4277 | struct strvec rp = STRVEC_INIT; |
99fb6e04 | 4278 | int rev_list_unpacked = 0, rev_list_all = 0, rev_list_reflog = 0; |
c90f9e13 | 4279 | int rev_list_index = 0; |
339bce27 | 4280 | int stdin_packs = 0; |
ed7e5fc3 | 4281 | struct string_list keep_pack_list = STRING_LIST_INIT_NODUP; |
0d5448a5 RS |
4282 | struct list_objects_filter_options filter_options = |
4283 | LIST_OBJECTS_FILTER_INIT; | |
80f6de4f | 4284 | |
99fb6e04 | 4285 | struct option pack_objects_options[] = { |
66e33092 | 4286 | OPT_CALLBACK_F('q', "quiet", &progress, NULL, |
36f76d2a RS |
4287 | N_("do not show progress meter"), |
4288 | PARSE_OPT_NOARG, option_parse_quiet), | |
99fb6e04 | 4289 | OPT_SET_INT(0, "progress", &progress, |
4c688120 | 4290 | N_("show progress meter"), 1), |
99fb6e04 | 4291 | OPT_SET_INT(0, "all-progress", &progress, |
4c688120 | 4292 | N_("show progress meter during object writing phase"), 2), |
99fb6e04 NTND |
4293 | OPT_BOOL(0, "all-progress-implied", |
4294 | &all_progress_implied, | |
4c688120 | 4295 | N_("similar to --all-progress when progress meter is shown")), |
66e33092 | 4296 | OPT_CALLBACK_F(0, "index-version", &pack_idx_opts, N_("<version>[,<offset>]"), |
4c688120 | 4297 | N_("write the pack index file in the specified idx format version"), |
203c8533 | 4298 | PARSE_OPT_NONEG, option_parse_index_version), |
2a514ed8 CB |
4299 | OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit, |
4300 | N_("maximum size of each output pack file")), | |
99fb6e04 | 4301 | OPT_BOOL(0, "local", &local, |
4c688120 | 4302 | N_("ignore borrowed objects from alternate object store")), |
99fb6e04 | 4303 | OPT_BOOL(0, "incremental", &incremental, |
4c688120 | 4304 | N_("ignore packed objects")), |
99fb6e04 | 4305 | OPT_INTEGER(0, "window", &window, |
4c688120 | 4306 | N_("limit pack window by objects")), |
2a514ed8 CB |
4307 | OPT_MAGNITUDE(0, "window-memory", &window_memory_limit, |
4308 | N_("limit pack window by memory in addition to object limit")), | |
99fb6e04 | 4309 | OPT_INTEGER(0, "depth", &depth, |
4c688120 | 4310 | N_("maximum length of delta chain allowed in the resulting pack")), |
99fb6e04 | 4311 | OPT_BOOL(0, "reuse-delta", &reuse_delta, |
4c688120 | 4312 | N_("reuse existing deltas")), |
99fb6e04 | 4313 | OPT_BOOL(0, "reuse-object", &reuse_object, |
4c688120 | 4314 | N_("reuse existing objects")), |
99fb6e04 | 4315 | OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta, |
4c688120 | 4316 | N_("use OFS_DELTA objects")), |
99fb6e04 | 4317 | OPT_INTEGER(0, "threads", &delta_search_threads, |
4c688120 | 4318 | N_("use threads when searching for best delta matches")), |
99fb6e04 | 4319 | OPT_BOOL(0, "non-empty", &non_empty, |
4c688120 | 4320 | N_("do not create an empty pack output")), |
99fb6e04 | 4321 | OPT_BOOL(0, "revs", &use_internal_rev_list, |
4c688120 | 4322 | N_("read revision arguments from standard input")), |
3e4a67b4 NTND |
4323 | OPT_SET_INT_F(0, "unpacked", &rev_list_unpacked, |
4324 | N_("limit the objects to those that are not yet packed"), | |
4325 | 1, PARSE_OPT_NONEG), | |
4326 | OPT_SET_INT_F(0, "all", &rev_list_all, | |
4327 | N_("include objects reachable from any reference"), | |
4328 | 1, PARSE_OPT_NONEG), | |
4329 | OPT_SET_INT_F(0, "reflog", &rev_list_reflog, | |
4330 | N_("include objects referred by reflog entries"), | |
4331 | 1, PARSE_OPT_NONEG), | |
4332 | OPT_SET_INT_F(0, "indexed-objects", &rev_list_index, | |
4333 | N_("include objects referred to by the index"), | |
4334 | 1, PARSE_OPT_NONEG), | |
339bce27 TB |
4335 | OPT_BOOL(0, "stdin-packs", &stdin_packs, |
4336 | N_("read packs from stdin")), | |
99fb6e04 | 4337 | OPT_BOOL(0, "stdout", &pack_to_stdout, |
4c688120 | 4338 | N_("output pack to stdout")), |
99fb6e04 | 4339 | OPT_BOOL(0, "include-tag", &include_tag, |
4c688120 | 4340 | N_("include tag objects that refer to objects to be packed")), |
99fb6e04 | 4341 | OPT_BOOL(0, "keep-unreachable", &keep_unreachable, |
4c688120 | 4342 | N_("keep unreachable objects")), |
e26a8c47 JK |
4343 | OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable, |
4344 | N_("pack loose unreachable objects")), | |
203c8533 | 4345 | OPT_CALLBACK_F(0, "unpack-unreachable", NULL, N_("time"), |
4c688120 | 4346 | N_("unpack unreachable objects newer than <time>"), |
203c8533 | 4347 | PARSE_OPT_OPTARG, option_parse_unpack_unreachable), |
b7573536 TB |
4348 | OPT_BOOL(0, "cruft", &cruft, N_("create a cruft pack")), |
4349 | OPT_CALLBACK_F(0, "cruft-expiration", NULL, N_("time"), | |
4350 | N_("expire cruft objects older than <time>"), | |
4351 | PARSE_OPT_OPTARG, option_parse_cruft_expiration), | |
4f6d26b1 DS |
4352 | OPT_BOOL(0, "sparse", &sparse, |
4353 | N_("use the sparse reachability algorithm")), | |
99fb6e04 | 4354 | OPT_BOOL(0, "thin", &thin, |
4c688120 | 4355 | N_("create thin packs")), |
2dacf26d | 4356 | OPT_BOOL(0, "shallow", &shallow, |
4357 | N_("create packs suitable for shallow fetches")), | |
ed7e5fc3 | 4358 | OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep_on_disk, |
4c688120 | 4359 | N_("ignore packs that have companion .keep file")), |
ed7e5fc3 NTND |
4360 | OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"), |
4361 | N_("ignore this pack")), | |
99fb6e04 | 4362 | OPT_INTEGER(0, "compression", &pack_compression_level, |
4c688120 | 4363 | N_("pack compression level")), |
3a5f3087 RS |
4364 | OPT_BOOL(0, "keep-true-parents", &grafts_keep_true_parents, |
4365 | N_("do not hide commits by grafts")), | |
6b8fda2d VM |
4366 | OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index, |
4367 | N_("use a bitmap index if available to speed up counting objects")), | |
25575015 JK |
4368 | OPT_SET_INT(0, "write-bitmap-index", &write_bitmap_index, |
4369 | N_("write a bitmap index together with the pack index"), | |
4370 | WRITE_BITMAP_TRUE), | |
4371 | OPT_SET_INT_F(0, "write-bitmap-index-quiet", | |
4372 | &write_bitmap_index, | |
4373 | N_("write a bitmap index if possible"), | |
4374 | WRITE_BITMAP_QUIET, PARSE_OPT_HIDDEN), | |
0d5448a5 | 4375 | OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options), |
203c8533 | 4376 | OPT_CALLBACK_F(0, "missing", NULL, N_("action"), |
9535ce73 | 4377 | N_("handling for missing objects"), PARSE_OPT_NONEG, |
203c8533 | 4378 | option_parse_missing_action), |
0c16cd49 JT |
4379 | OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects, |
4380 | N_("do not pack objects in promisor packfiles")), | |
28b8a730 JK |
4381 | OPT_BOOL(0, "delta-islands", &use_delta_islands, |
4382 | N_("respect islands during delta compression")), | |
dd4b732d JT |
4383 | OPT_STRING_LIST(0, "uri-protocol", &uri_protocols, |
4384 | N_("protocol"), | |
4385 | N_("exclude any configured uploadpack.blobpackfileuri with this protocol")), | |
99fb6e04 NTND |
4386 | OPT_END(), |
4387 | }; | |
8d1d8f83 | 4388 | |
0c6804ab NTND |
4389 | if (DFS_NUM_STATES > (1 << OE_DFS_STATE_BITS)) |
4390 | BUG("too many dfs states, increase OE_DFS_STATE_BITS"); | |
4391 | ||
d24eda4e | 4392 | disable_replace_refs(); |
dae556bd | 4393 | |
2d657ab9 | 4394 | sparse = git_env_bool("GIT_TEST_PACK_SPARSE", -1); |
059fda19 JS |
4395 | if (the_repository->gitdir) { |
4396 | prepare_repo_settings(the_repository); | |
4397 | if (sparse < 0) | |
4398 | sparse = the_repository->settings.pack_use_sparse; | |
4399 | } | |
7211b9e7 | 4400 | |
ebcfb379 | 4401 | reset_pack_idx_option(&pack_idx_opts); |
a8dd7e05 | 4402 | pack_idx_opts.flags |= WRITE_REV; |
ef90d6d4 | 4403 | git_config(git_pack_config, NULL); |
9f7f10a2 TB |
4404 | if (git_env_bool(GIT_TEST_NO_WRITE_REV_INDEX, 0)) |
4405 | pack_idx_opts.flags &= ~WRITE_REV; | |
b5d97e6b JH |
4406 | |
4407 | progress = isatty(2); | |
99fb6e04 NTND |
4408 | argc = parse_options(argc, argv, prefix, pack_objects_options, |
4409 | pack_usage, 0); | |
b5d97e6b | 4410 | |
99fb6e04 NTND |
4411 | if (argc) { |
4412 | base_name = argv[0]; | |
4413 | argc--; | |
b5d97e6b | 4414 | } |
99fb6e04 NTND |
4415 | if (pack_to_stdout != !base_name || argc) |
4416 | usage_with_options(pack_usage, pack_objects_options); | |
b5d97e6b | 4417 | |
6d52b6a5 JK |
4418 | if (depth < 0) |
4419 | depth = 0; | |
b5c0cbd8 NTND |
4420 | if (depth >= (1 << OE_DEPTH_BITS)) { |
4421 | warning(_("delta chain depth %d is too deep, forcing %d"), | |
4422 | depth, (1 << OE_DEPTH_BITS) - 1); | |
4423 | depth = (1 << OE_DEPTH_BITS) - 1; | |
4424 | } | |
0cb3c142 NTND |
4425 | if (cache_max_small_delta_size >= (1U << OE_Z_DELTA_BITS)) { |
4426 | warning(_("pack.deltaCacheLimit is too high, forcing %d"), | |
4427 | (1U << OE_Z_DELTA_BITS) - 1); | |
4428 | cache_max_small_delta_size = (1U << OE_Z_DELTA_BITS) - 1; | |
4429 | } | |
953aa54e JK |
4430 | if (window < 0) |
4431 | window = 0; | |
b5c0cbd8 | 4432 | |
22f9b7f3 | 4433 | strvec_push(&rp, "pack-objects"); |
99fb6e04 NTND |
4434 | if (thin) { |
4435 | use_internal_rev_list = 1; | |
22f9b7f3 | 4436 | strvec_push(&rp, shallow |
2dacf26d | 4437 | ? "--objects-edge-aggressive" |
4438 | : "--objects-edge"); | |
99fb6e04 | 4439 | } else |
22f9b7f3 | 4440 | strvec_push(&rp, "--objects"); |
b5d97e6b | 4441 | |
99fb6e04 NTND |
4442 | if (rev_list_all) { |
4443 | use_internal_rev_list = 1; | |
22f9b7f3 | 4444 | strvec_push(&rp, "--all"); |
99fb6e04 NTND |
4445 | } |
4446 | if (rev_list_reflog) { | |
4447 | use_internal_rev_list = 1; | |
22f9b7f3 | 4448 | strvec_push(&rp, "--reflog"); |
99fb6e04 | 4449 | } |
c90f9e13 JK |
4450 | if (rev_list_index) { |
4451 | use_internal_rev_list = 1; | |
22f9b7f3 | 4452 | strvec_push(&rp, "--indexed-objects"); |
99fb6e04 | 4453 | } |
339bce27 | 4454 | if (rev_list_unpacked && !stdin_packs) { |
99fb6e04 | 4455 | use_internal_rev_list = 1; |
22f9b7f3 | 4456 | strvec_push(&rp, "--unpacked"); |
99fb6e04 | 4457 | } |
b5d97e6b | 4458 | |
0c16cd49 JT |
4459 | if (exclude_promisor_objects) { |
4460 | use_internal_rev_list = 1; | |
4461 | fetch_if_missing = 0; | |
22f9b7f3 | 4462 | strvec_push(&rp, "--exclude-promisor-objects"); |
0c16cd49 | 4463 | } |
58bd77b6 NTND |
4464 | if (unpack_unreachable || keep_unreachable || pack_loose_unreachable) |
4465 | use_internal_rev_list = 1; | |
0c16cd49 | 4466 | |
99fb6e04 NTND |
4467 | if (!reuse_object) |
4468 | reuse_delta = 0; | |
4469 | if (pack_compression_level == -1) | |
4470 | pack_compression_level = Z_DEFAULT_COMPRESSION; | |
4471 | else if (pack_compression_level < 0 || pack_compression_level > Z_BEST_COMPRESSION) | |
f616db6a | 4472 | die(_("bad pack compression level %d"), pack_compression_level); |
0c45d258 JH |
4473 | |
4474 | if (!delta_search_threads) /* --threads=0 means autodetect */ | |
4475 | delta_search_threads = online_cpus(); | |
4476 | ||
9c897c5c | 4477 | if (!HAVE_THREADS && delta_search_threads != 1) |
f616db6a | 4478 | warning(_("no threads support, ignoring --threads")); |
61568efa | 4479 | if (!pack_to_stdout && !pack_size_limit) |
2b84b5a8 | 4480 | pack_size_limit = pack_size_limit_cfg; |
01c12a23 | 4481 | if (pack_to_stdout && pack_size_limit) |
f616db6a | 4482 | die(_("--max-pack-size cannot be used to build a pack for transfer")); |
07cf0f24 | 4483 | if (pack_size_limit && pack_size_limit < 1024*1024) { |
f616db6a | 4484 | warning(_("minimum pack size limit is 1 MiB")); |
07cf0f24 NP |
4485 | pack_size_limit = 1024*1024; |
4486 | } | |
01c12a23 | 4487 | |
8d1d8f83 | 4488 | if (!pack_to_stdout && thin) |
f616db6a | 4489 | die(_("--thin cannot be used to build an indexable pack")); |
b5d97e6b | 4490 | |
ca11b212 | 4491 | if (keep_unreachable && unpack_unreachable) |
12909b6b | 4492 | die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "--unpack-unreachable"); |
b1e757f3 JK |
4493 | if (!rev_list_all || !rev_list_reflog || !rev_list_index) |
4494 | unpack_unreachable_expiration = 0; | |
ca11b212 | 4495 | |
6cfcabfb CC |
4496 | if (stdin_packs && filter_options.choice) |
4497 | die(_("cannot use --filter with --stdin-packs")); | |
9535ce73 | 4498 | |
339bce27 TB |
4499 | if (stdin_packs && use_internal_rev_list) |
4500 | die(_("cannot use internal rev list with --stdin-packs")); | |
4501 | ||
b7573536 TB |
4502 | if (cruft) { |
4503 | if (use_internal_rev_list) | |
4504 | die(_("cannot use internal rev list with --cruft")); | |
4505 | if (stdin_packs) | |
4506 | die(_("cannot use --stdin-packs with --cruft")); | |
b7573536 TB |
4507 | } |
4508 | ||
645c432d KS |
4509 | /* |
4510 | * "soft" reasons not to use bitmaps - for on-disk repack by default we want | |
4511 | * | |
4512 | * - to produce good pack (with bitmap index not-yet-packed objects are | |
4513 | * packed in suboptimal order). | |
4514 | * | |
4515 | * - to use more robust pack-generation codepath (avoiding possible | |
4516 | * bugs in bitmap code and possible bitmap index corruption). | |
4517 | */ | |
4518 | if (!pack_to_stdout) | |
4519 | use_bitmap_index_default = 0; | |
4520 | ||
4521 | if (use_bitmap_index < 0) | |
4522 | use_bitmap_index = use_bitmap_index_default; | |
4523 | ||
4524 | /* "hard" reasons not to use bitmaps; these just won't work at all */ | |
c8813487 | 4525 | if (!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) || is_repository_shallow(the_repository)) |
6b8fda2d VM |
4526 | use_bitmap_index = 0; |
4527 | ||
7cc8f971 VM |
4528 | if (pack_to_stdout || !rev_list_all) |
4529 | write_bitmap_index = 0; | |
4530 | ||
28b8a730 | 4531 | if (use_delta_islands) |
22f9b7f3 | 4532 | strvec_push(&rp, "--topo-order"); |
28b8a730 | 4533 | |
4f366275 NP |
4534 | if (progress && all_progress_implied) |
4535 | progress = 2; | |
4536 | ||
ed7e5fc3 NTND |
4537 | add_extra_kept_packs(&keep_pack_list); |
4538 | if (ignore_packed_keep_on_disk) { | |
56dfeb62 | 4539 | struct packed_git *p; |
454ea2e4 | 4540 | for (p = get_all_packs(the_repository); p; p = p->next) |
56dfeb62 JK |
4541 | if (p->pack_local && p->pack_keep) |
4542 | break; | |
4543 | if (!p) /* no keep-able packs found */ | |
ed7e5fc3 | 4544 | ignore_packed_keep_on_disk = 0; |
56dfeb62 JK |
4545 | } |
4546 | if (local) { | |
4547 | /* | |
ed7e5fc3 NTND |
4548 | * unlike ignore_packed_keep_on_disk above, we do not |
4549 | * want to unset "local" based on looking at packs, as | |
4550 | * it also covers non-local objects | |
56dfeb62 JK |
4551 | */ |
4552 | struct packed_git *p; | |
454ea2e4 | 4553 | for (p = get_all_packs(the_repository); p; p = p->next) { |
56dfeb62 JK |
4554 | if (!p->pack_local) { |
4555 | have_non_local_packs = 1; | |
4556 | break; | |
4557 | } | |
4558 | } | |
4559 | } | |
b5d97e6b | 4560 | |
ae417807 DS |
4561 | trace2_region_enter("pack-objects", "enumerate-objects", |
4562 | the_repository); | |
7c141127 | 4563 | prepare_packing_data(the_repository, &to_pack); |
43fa44fa | 4564 | |
b7573536 | 4565 | if (progress && !cruft) |
5af05043 | 4566 | progress_state = start_progress(_("Enumerating objects"), 0); |
339bce27 TB |
4567 | if (stdin_packs) { |
4568 | /* avoids adding objects in excluded packs */ | |
4569 | ignore_packed_keep_in_core = 1; | |
4570 | read_packs_list_from_stdin(); | |
4571 | if (rev_list_unpacked) | |
4572 | add_unreachable_loose_objects(); | |
b7573536 TB |
4573 | } else if (cruft) { |
4574 | read_cruft_objects(); | |
9e39acc9 | 4575 | } else if (!use_internal_rev_list) { |
b5d97e6b | 4576 | read_object_list_from_stdin(); |
9e39acc9 | 4577 | } else { |
5cb28270 ÆAB |
4578 | struct rev_info revs; |
4579 | ||
4580 | repo_init_revisions(the_repository, &revs, NULL); | |
0d5448a5 | 4581 | list_objects_filter_copy(&revs.filter, &filter_options); |
80f6de4f | 4582 | get_object_list(&revs, rp.nr, rp.v); |
2108fe4a | 4583 | release_revisions(&revs); |
8d1d8f83 | 4584 | } |
0ef95f72 | 4585 | cleanup_preferred_base(); |
d155254c | 4586 | if (include_tag && nr_result) |
be18153b | 4587 | for_each_tag_ref(add_ref_tag, NULL); |
4d4fcc54 | 4588 | stop_progress(&progress_state); |
ae417807 DS |
4589 | trace2_region_leave("pack-objects", "enumerate-objects", |
4590 | the_repository); | |
96a02f8f | 4591 | |
f0b0af1b | 4592 | if (non_empty && !nr_result) |
9e39acc9 | 4593 | goto cleanup; |
ae417807 DS |
4594 | if (nr_result) { |
4595 | trace2_region_enter("pack-objects", "prepare-pack", | |
4596 | the_repository); | |
f7ae6a93 | 4597 | prepare_pack(window, depth); |
ae417807 DS |
4598 | trace2_region_leave("pack-objects", "prepare-pack", |
4599 | the_repository); | |
4600 | } | |
4601 | ||
4602 | trace2_region_enter("pack-objects", "write-pack-file", the_repository); | |
dd4b732d | 4603 | write_excluded_by_configs(); |
d01fb92f | 4604 | write_pack_file(); |
ae417807 DS |
4605 | trace2_region_leave("pack-objects", "write-pack-file", the_repository); |
4606 | ||
ab7cd7bb | 4607 | if (progress) |
f616db6a NTND |
4608 | fprintf_ln(stderr, |
4609 | _("Total %"PRIu32" (delta %"PRIu32")," | |
bab28d9f | 4610 | " reused %"PRIu32" (delta %"PRIu32")," |
b96289a1 | 4611 | " pack-reused %"PRIu32" (from %"PRIuMAX")"), |
bab28d9f | 4612 | written, written_delta, reused, reused_delta, |
b96289a1 TB |
4613 | reuse_packfile_objects, |
4614 | (uintmax_t)reuse_packfiles_used_nr); | |
9e39acc9 | 4615 | |
54393e4e TB |
4616 | trace2_data_intmax("pack-objects", the_repository, "written", written); |
4617 | trace2_data_intmax("pack-objects", the_repository, "written/delta", written_delta); | |
4618 | trace2_data_intmax("pack-objects", the_repository, "reused", reused); | |
4619 | trace2_data_intmax("pack-objects", the_repository, "reused/delta", reused_delta); | |
4620 | trace2_data_intmax("pack-objects", the_repository, "pack-reused", reuse_packfile_objects); | |
4621 | trace2_data_intmax("pack-objects", the_repository, "packs-reused", reuse_packfiles_used_nr); | |
9e39acc9 TB |
4622 | |
4623 | cleanup: | |
66f0c710 | 4624 | clear_packing_data(&to_pack); |
0d5448a5 | 4625 | list_objects_filter_release(&filter_options); |
9e39acc9 TB |
4626 | strvec_clear(&rp); |
4627 | ||
c323ac7d LT |
4628 | return 0; |
4629 | } |