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