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