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