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