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