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