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