]>
Commit | Line | Data |
---|---|---|
5d4a6003 | 1 | #include "builtin.h" |
c323ac7d | 2 | #include "cache.h" |
a74db82e | 3 | #include "attr.h" |
c323ac7d | 4 | #include "object.h" |
8e440259 PE |
5 | #include "blob.h" |
6 | #include "commit.h" | |
7 | #include "tag.h" | |
8 | #include "tree.h" | |
c323ac7d | 9 | #include "delta.h" |
a733cb60 | 10 | #include "pack.h" |
3449f8c4 | 11 | #include "pack-revindex.h" |
c38138cd | 12 | #include "csum-file.h" |
1b0c7174 | 13 | #include "tree-walk.h" |
b5d97e6b JH |
14 | #include "diff.h" |
15 | #include "revision.h" | |
16 | #include "list-objects.h" | |
96a02f8f | 17 | #include "progress.h" |
f0a24aa5 | 18 | #include "refs.h" |
cf2ba13a | 19 | #include "streaming.h" |
93749194 | 20 | #include "thread-utils.h" |
8ecce684 | 21 | |
99fb6e04 | 22 | static const char *pack_usage[] = { |
4c688120 NTND |
23 | N_("git pack-objects --stdout [options...] [< ref-list | < object-list]"), |
24 | N_("git pack-objects [options...] base-name [< ref-list | < object-list]"), | |
99fb6e04 NTND |
25 | NULL |
26 | }; | |
c323ac7d | 27 | |
c323ac7d | 28 | struct object_entry { |
aa7e44bf | 29 | struct pack_idx_entry idx; |
3f9ac8d2 | 30 | unsigned long size; /* uncompressed size */ |
3f9ac8d2 | 31 | struct packed_git *in_pack; /* already in pack */ |
6777a59f | 32 | off_t in_pack_offset; |
a3fbf4df | 33 | struct object_entry *delta; /* delta base object */ |
82e5a82f | 34 | struct object_entry *delta_child; /* deltified objects who bases me */ |
15b4d577 JH |
35 | struct object_entry *delta_sibling; /* other deltified objects who |
36 | * uses the same base as me | |
37 | */ | |
074b2eea | 38 | void *delta_data; /* cached delta (uncompressed) */ |
a3fbf4df | 39 | unsigned long delta_size; /* delta data size (uncompressed) */ |
ed4a9031 | 40 | unsigned long z_delta_size; /* delta data size (compressed) */ |
5a235b5e | 41 | unsigned int hash; /* name hint hash */ |
a3fbf4df NP |
42 | enum object_type type; |
43 | enum object_type in_pack_type; /* could be delta */ | |
44 | unsigned char in_pack_header_size; | |
45 | unsigned char preferred_base; /* we do not pack this, but is available | |
a74db82e | 46 | * to be used as the base object to delta |
a3fbf4df NP |
47 | * objects against. |
48 | */ | |
a74db82e | 49 | unsigned char no_try_delta; |
1b4bb16b JH |
50 | unsigned char tagged; /* near the very tip of refs */ |
51 | unsigned char filled; /* assigned write-order */ | |
c323ac7d LT |
52 | }; |
53 | ||
3f9ac8d2 | 54 | /* |
82e5a82f | 55 | * Objects we are going to pack are collected in objects array (dynamically |
3f9ac8d2 JH |
56 | * expanded). nr_objects & nr_alloc controls this array. They are stored |
57 | * in the order we see -- typically rev-list --objects order that gives us | |
58 | * nice "minimum seek" order. | |
3f9ac8d2 | 59 | */ |
e4d58311 | 60 | static struct object_entry *objects; |
79814f42 | 61 | static struct pack_idx_entry **written_list; |
d01fb92f | 62 | static uint32_t nr_objects, nr_alloc, nr_result, nr_written; |
3f9ac8d2 | 63 | |
96f1e58f | 64 | static int non_empty; |
a7de7130 | 65 | static int reuse_delta = 1, reuse_object = 1; |
e5e9714a | 66 | static int keep_unreachable, unpack_unreachable, include_tag; |
7e52f566 | 67 | static unsigned long unpack_unreachable_expiration; |
96f1e58f DR |
68 | static int local; |
69 | static int incremental; | |
e96fb9b8 | 70 | static int ignore_packed_keep; |
be6b1914 | 71 | static int allow_ofs_delta; |
ebcfb379 | 72 | static struct pack_idx_option pack_idx_opts; |
d01fb92f | 73 | static const char *base_name; |
024701f1 | 74 | static int progress = 1; |
4812a93a | 75 | static int window = 10; |
568508e7 | 76 | static unsigned long pack_size_limit; |
618e613a | 77 | static int depth = 50; |
43cc2b42 | 78 | static int delta_search_threads; |
df6d6101 | 79 | static int pack_to_stdout; |
8d1d8f83 | 80 | static int num_preferred_base; |
dc6a0757 | 81 | static struct progress *progress_state; |
960ccca6 DH |
82 | static int pack_compression_level = Z_DEFAULT_COMPRESSION; |
83 | static int pack_compression_seen; | |
c323ac7d | 84 | |
074b2eea | 85 | static unsigned long delta_cache_size = 0; |
5749b0b2 | 86 | static unsigned long max_delta_cache_size = 256 * 1024 * 1024; |
e3dfddb3 | 87 | static unsigned long cache_max_small_delta_size = 1000; |
074b2eea | 88 | |
a97773ce BD |
89 | static unsigned long window_memory_limit = 0; |
90 | ||
3f9ac8d2 JH |
91 | /* |
92 | * The object names in objects array are hashed with this hashtable, | |
e4d58311 | 93 | * to help looking up the entry by object name. |
3f9ac8d2 JH |
94 | * This hashtable is built after all the objects are seen. |
95 | */ | |
96f1e58f DR |
96 | static int *object_ix; |
97 | static int object_ix_hashsz; | |
1b4bb16b | 98 | static struct object_entry *locate_object_entry(const unsigned char *sha1); |
3f9ac8d2 | 99 | |
3f9ac8d2 JH |
100 | /* |
101 | * stats | |
102 | */ | |
7cadf491 SP |
103 | static uint32_t written, written_delta; |
104 | static uint32_t reused, reused_delta; | |
3f9ac8d2 | 105 | |
780e6e73 | 106 | |
3613f9b4 | 107 | static void *get_delta(struct object_entry *entry) |
c323ac7d | 108 | { |
3613f9b4 NP |
109 | unsigned long size, base_size, delta_size; |
110 | void *buf, *base_buf, *delta_buf; | |
21666f1a | 111 | enum object_type type; |
c323ac7d | 112 | |
3613f9b4 NP |
113 | buf = read_sha1_file(entry->idx.sha1, &type, &size); |
114 | if (!buf) | |
115 | die("unable to read %s", sha1_to_hex(entry->idx.sha1)); | |
116 | base_buf = read_sha1_file(entry->delta->idx.sha1, &type, &base_size); | |
117 | if (!base_buf) | |
aa7e44bf | 118 | die("unable to read %s", sha1_to_hex(entry->delta->idx.sha1)); |
3613f9b4 | 119 | delta_buf = diff_delta(base_buf, base_size, |
dcde55bc | 120 | buf, size, &delta_size, 0); |
3613f9b4 | 121 | if (!delta_buf || delta_size != entry->delta_size) |
a6080a0a | 122 | die("delta size changed"); |
3613f9b4 NP |
123 | free(buf); |
124 | free(base_buf); | |
c323ac7d LT |
125 | return delta_buf; |
126 | } | |
127 | ||
30ebb40a NP |
128 | static unsigned long do_compress(void **pptr, unsigned long size) |
129 | { | |
ef49a7a0 | 130 | git_zstream stream; |
30ebb40a NP |
131 | void *in, *out; |
132 | unsigned long maxsize; | |
133 | ||
134 | memset(&stream, 0, sizeof(stream)); | |
55bb5c91 | 135 | git_deflate_init(&stream, pack_compression_level); |
225a6f10 | 136 | maxsize = git_deflate_bound(&stream, size); |
30ebb40a NP |
137 | |
138 | in = *pptr; | |
139 | out = xmalloc(maxsize); | |
140 | *pptr = out; | |
141 | ||
142 | stream.next_in = in; | |
143 | stream.avail_in = size; | |
144 | stream.next_out = out; | |
145 | stream.avail_out = maxsize; | |
55bb5c91 | 146 | while (git_deflate(&stream, Z_FINISH) == Z_OK) |
30ebb40a | 147 | ; /* nothing */ |
55bb5c91 | 148 | git_deflate_end(&stream); |
30ebb40a NP |
149 | |
150 | free(in); | |
151 | return stream.total_out; | |
152 | } | |
153 | ||
cf2ba13a NTND |
154 | static unsigned long write_large_blob_data(struct git_istream *st, struct sha1file *f, |
155 | const unsigned char *sha1) | |
156 | { | |
157 | git_zstream stream; | |
158 | unsigned char ibuf[1024 * 16]; | |
159 | unsigned char obuf[1024 * 16]; | |
160 | unsigned long olen = 0; | |
161 | ||
162 | memset(&stream, 0, sizeof(stream)); | |
163 | git_deflate_init(&stream, pack_compression_level); | |
164 | ||
165 | for (;;) { | |
166 | ssize_t readlen; | |
167 | int zret = Z_OK; | |
168 | readlen = read_istream(st, ibuf, sizeof(ibuf)); | |
169 | if (readlen == -1) | |
170 | die(_("unable to read %s"), sha1_to_hex(sha1)); | |
171 | ||
172 | stream.next_in = ibuf; | |
173 | stream.avail_in = readlen; | |
174 | while ((stream.avail_in || readlen == 0) && | |
175 | (zret == Z_OK || zret == Z_BUF_ERROR)) { | |
176 | stream.next_out = obuf; | |
177 | stream.avail_out = sizeof(obuf); | |
178 | zret = git_deflate(&stream, readlen ? 0 : Z_FINISH); | |
179 | sha1write(f, obuf, stream.next_out - obuf); | |
180 | olen += stream.next_out - obuf; | |
181 | } | |
182 | if (stream.avail_in) | |
183 | die(_("deflate error (%d)"), zret); | |
184 | if (readlen == 0) { | |
185 | if (zret != Z_STREAM_END) | |
186 | die(_("deflate error (%d)"), zret); | |
187 | break; | |
188 | } | |
189 | } | |
190 | git_deflate_end(&stream); | |
191 | return olen; | |
192 | } | |
193 | ||
780e6e73 NP |
194 | /* |
195 | * we are going to reuse the existing object data as is. make | |
196 | * sure it is not corrupt. | |
197 | */ | |
079afb18 SP |
198 | static int check_pack_inflate(struct packed_git *p, |
199 | struct pack_window **w_curs, | |
6777a59f SP |
200 | off_t offset, |
201 | off_t len, | |
079afb18 SP |
202 | unsigned long expect) |
203 | { | |
ef49a7a0 | 204 | git_zstream stream; |
079afb18 SP |
205 | unsigned char fakebuf[4096], *in; |
206 | int st; | |
207 | ||
208 | memset(&stream, 0, sizeof(stream)); | |
39c68542 | 209 | git_inflate_init(&stream); |
079afb18 SP |
210 | do { |
211 | in = use_pack(p, w_curs, offset, &stream.avail_in); | |
212 | stream.next_in = in; | |
213 | stream.next_out = fakebuf; | |
214 | stream.avail_out = sizeof(fakebuf); | |
39c68542 | 215 | st = git_inflate(&stream, Z_FINISH); |
079afb18 SP |
216 | offset += stream.next_in - in; |
217 | } while (st == Z_OK || st == Z_BUF_ERROR); | |
39c68542 | 218 | git_inflate_end(&stream); |
079afb18 SP |
219 | return (st == Z_STREAM_END && |
220 | stream.total_out == expect && | |
221 | stream.total_in == len) ? 0 : -1; | |
222 | } | |
223 | ||
224 | static void copy_pack_data(struct sha1file *f, | |
225 | struct packed_git *p, | |
226 | struct pack_window **w_curs, | |
6777a59f SP |
227 | off_t offset, |
228 | off_t len) | |
079afb18 SP |
229 | { |
230 | unsigned char *in; | |
ef49a7a0 | 231 | unsigned long avail; |
079afb18 SP |
232 | |
233 | while (len) { | |
234 | in = use_pack(p, w_curs, offset, &avail); | |
235 | if (avail > len) | |
ef49a7a0 | 236 | avail = (unsigned long)len; |
079afb18 SP |
237 | sha1write(f, in, avail); |
238 | offset += avail; | |
239 | len -= avail; | |
240 | } | |
241 | } | |
242 | ||
1b4bb16b | 243 | /* Return 0 if we will bust the pack-size limit */ |
c9018b03 NTND |
244 | static unsigned long write_no_reuse_object(struct sha1file *f, struct object_entry *entry, |
245 | unsigned long limit, int usable_delta) | |
c323ac7d | 246 | { |
c9018b03 | 247 | unsigned long size, datalen; |
2c5ef824 | 248 | unsigned char header[10], dheader[10]; |
6777a59f | 249 | unsigned hdrlen; |
2c5ef824 | 250 | enum object_type type; |
c9018b03 | 251 | void *buf; |
cf2ba13a | 252 | struct git_istream *st = NULL; |
c9018b03 NTND |
253 | |
254 | if (!usable_delta) { | |
cf2ba13a NTND |
255 | if (entry->type == OBJ_BLOB && |
256 | entry->size > big_file_threshold && | |
257 | (st = open_istream(entry->idx.sha1, &type, &size, NULL)) != NULL) | |
258 | buf = NULL; | |
259 | else { | |
260 | buf = read_sha1_file(entry->idx.sha1, &type, &size); | |
261 | if (!buf) | |
262 | die(_("unable to read %s"), sha1_to_hex(entry->idx.sha1)); | |
263 | } | |
c9018b03 NTND |
264 | /* |
265 | * make sure no cached delta data remains from a | |
266 | * previous attempt before a pack split occurred. | |
267 | */ | |
268 | free(entry->delta_data); | |
269 | entry->delta_data = NULL; | |
270 | entry->z_delta_size = 0; | |
271 | } else if (entry->delta_data) { | |
272 | size = entry->delta_size; | |
273 | buf = entry->delta_data; | |
274 | entry->delta_data = NULL; | |
275 | type = (allow_ofs_delta && entry->delta->idx.offset) ? | |
276 | OBJ_OFS_DELTA : OBJ_REF_DELTA; | |
277 | } else { | |
278 | buf = get_delta(entry); | |
279 | size = entry->delta_size; | |
280 | type = (allow_ofs_delta && entry->delta->idx.offset) ? | |
281 | OBJ_OFS_DELTA : OBJ_REF_DELTA; | |
282 | } | |
283 | ||
cf2ba13a NTND |
284 | if (st) /* large blob case, just assume we don't compress well */ |
285 | datalen = size; | |
286 | else if (entry->z_delta_size) | |
c9018b03 NTND |
287 | datalen = entry->z_delta_size; |
288 | else | |
289 | datalen = do_compress(&buf, size); | |
290 | ||
291 | /* | |
292 | * The object header is a byte of 'type' followed by zero or | |
293 | * more bytes of length. | |
294 | */ | |
295 | hdrlen = encode_in_pack_object_header(type, size, header); | |
296 | ||
297 | if (type == OBJ_OFS_DELTA) { | |
298 | /* | |
299 | * Deltas with relative base contain an additional | |
300 | * encoding of the relative offset for the delta | |
301 | * base from this object's position in the pack. | |
302 | */ | |
303 | off_t ofs = entry->idx.offset - entry->delta->idx.offset; | |
304 | unsigned pos = sizeof(dheader) - 1; | |
305 | dheader[pos] = ofs & 127; | |
306 | while (ofs >>= 7) | |
307 | dheader[--pos] = 128 | (--ofs & 127); | |
308 | if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) { | |
cf2ba13a NTND |
309 | if (st) |
310 | close_istream(st); | |
c9018b03 NTND |
311 | free(buf); |
312 | return 0; | |
313 | } | |
314 | sha1write(f, header, hdrlen); | |
315 | sha1write(f, dheader + pos, sizeof(dheader) - pos); | |
316 | hdrlen += sizeof(dheader) - pos; | |
317 | } else if (type == OBJ_REF_DELTA) { | |
318 | /* | |
319 | * Deltas with a base reference contain | |
320 | * an additional 20 bytes for the base sha1. | |
321 | */ | |
322 | if (limit && hdrlen + 20 + datalen + 20 >= limit) { | |
cf2ba13a NTND |
323 | if (st) |
324 | close_istream(st); | |
c9018b03 NTND |
325 | free(buf); |
326 | return 0; | |
327 | } | |
328 | sha1write(f, header, hdrlen); | |
329 | sha1write(f, entry->delta->idx.sha1, 20); | |
330 | hdrlen += 20; | |
331 | } else { | |
332 | if (limit && hdrlen + datalen + 20 >= limit) { | |
cf2ba13a NTND |
333 | if (st) |
334 | close_istream(st); | |
c9018b03 NTND |
335 | free(buf); |
336 | return 0; | |
337 | } | |
338 | sha1write(f, header, hdrlen); | |
339 | } | |
cf2ba13a NTND |
340 | if (st) { |
341 | datalen = write_large_blob_data(st, f, entry->idx.sha1); | |
342 | close_istream(st); | |
343 | } else { | |
344 | sha1write(f, buf, datalen); | |
345 | free(buf); | |
346 | } | |
c9018b03 NTND |
347 | |
348 | return hdrlen + datalen; | |
349 | } | |
350 | ||
351 | /* Return 0 if we will bust the pack-size limit */ | |
352 | static unsigned long write_reuse_object(struct sha1file *f, struct object_entry *entry, | |
353 | unsigned long limit, int usable_delta) | |
354 | { | |
355 | struct packed_git *p = entry->in_pack; | |
356 | struct pack_window *w_curs = NULL; | |
357 | struct revindex_entry *revidx; | |
358 | off_t offset; | |
359 | enum object_type type = entry->type; | |
360 | unsigned long datalen; | |
361 | unsigned char header[10], dheader[10]; | |
362 | unsigned hdrlen; | |
363 | ||
364 | if (entry->delta) | |
365 | type = (allow_ofs_delta && entry->delta->idx.offset) ? | |
366 | OBJ_OFS_DELTA : OBJ_REF_DELTA; | |
367 | hdrlen = encode_in_pack_object_header(type, entry->size, header); | |
368 | ||
369 | offset = entry->in_pack_offset; | |
370 | revidx = find_pack_revindex(p, offset); | |
371 | datalen = revidx[1].offset - offset; | |
372 | if (!pack_to_stdout && p->index_version > 1 && | |
373 | check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) { | |
374 | error("bad packed object CRC for %s", sha1_to_hex(entry->idx.sha1)); | |
375 | unuse_pack(&w_curs); | |
376 | return write_no_reuse_object(f, entry, limit, usable_delta); | |
377 | } | |
378 | ||
379 | offset += entry->in_pack_header_size; | |
380 | datalen -= entry->in_pack_header_size; | |
381 | ||
382 | if (!pack_to_stdout && p->index_version == 1 && | |
383 | check_pack_inflate(p, &w_curs, offset, datalen, entry->size)) { | |
384 | error("corrupt packed object for %s", sha1_to_hex(entry->idx.sha1)); | |
385 | unuse_pack(&w_curs); | |
386 | return write_no_reuse_object(f, entry, limit, usable_delta); | |
387 | } | |
388 | ||
389 | if (type == OBJ_OFS_DELTA) { | |
390 | off_t ofs = entry->idx.offset - entry->delta->idx.offset; | |
391 | unsigned pos = sizeof(dheader) - 1; | |
392 | dheader[pos] = ofs & 127; | |
393 | while (ofs >>= 7) | |
394 | dheader[--pos] = 128 | (--ofs & 127); | |
395 | if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) { | |
396 | unuse_pack(&w_curs); | |
397 | return 0; | |
398 | } | |
399 | sha1write(f, header, hdrlen); | |
400 | sha1write(f, dheader + pos, sizeof(dheader) - pos); | |
401 | hdrlen += sizeof(dheader) - pos; | |
402 | reused_delta++; | |
403 | } else if (type == OBJ_REF_DELTA) { | |
404 | if (limit && hdrlen + 20 + datalen + 20 >= limit) { | |
405 | unuse_pack(&w_curs); | |
406 | return 0; | |
407 | } | |
408 | sha1write(f, header, hdrlen); | |
409 | sha1write(f, entry->delta->idx.sha1, 20); | |
410 | hdrlen += 20; | |
411 | reused_delta++; | |
412 | } else { | |
413 | if (limit && hdrlen + datalen + 20 >= limit) { | |
414 | unuse_pack(&w_curs); | |
415 | return 0; | |
416 | } | |
417 | sha1write(f, header, hdrlen); | |
418 | } | |
419 | copy_pack_data(f, p, &w_curs, offset, datalen); | |
420 | unuse_pack(&w_curs); | |
421 | reused++; | |
422 | return hdrlen + datalen; | |
423 | } | |
424 | ||
425 | /* Return 0 if we will bust the pack-size limit */ | |
426 | static unsigned long write_object(struct sha1file *f, | |
427 | struct object_entry *entry, | |
428 | off_t write_offset) | |
429 | { | |
430 | unsigned long limit, len; | |
2c5ef824 | 431 | int usable_delta, to_reuse; |
c323ac7d | 432 | |
78d1e84f NP |
433 | if (!pack_to_stdout) |
434 | crc32_begin(f); | |
435 | ||
a2430dde | 436 | /* apply size limit if limited packsize and not first object */ |
a1e4760f NP |
437 | if (!pack_size_limit || !nr_written) |
438 | limit = 0; | |
439 | else if (pack_size_limit <= write_offset) | |
440 | /* | |
441 | * the earlier object did not fit the limit; avoid | |
442 | * mistaking this with unlimited (i.e. limit = 0). | |
443 | */ | |
444 | limit = 1; | |
445 | else | |
446 | limit = pack_size_limit - write_offset; | |
2c5ef824 NP |
447 | |
448 | if (!entry->delta) | |
449 | usable_delta = 0; /* no delta */ | |
450 | else if (!pack_size_limit) | |
451 | usable_delta = 1; /* unlimited packfile */ | |
452 | else if (entry->delta->idx.offset == (off_t)-1) | |
453 | usable_delta = 0; /* base was written to another pack */ | |
454 | else if (entry->delta->idx.offset) | |
455 | usable_delta = 1; /* base already exists in this pack */ | |
456 | else | |
457 | usable_delta = 0; /* base could end up in another pack */ | |
458 | ||
a7de7130 | 459 | if (!reuse_object) |
fa736f72 NP |
460 | to_reuse = 0; /* explicit */ |
461 | else if (!entry->in_pack) | |
ab7cd7bb | 462 | to_reuse = 0; /* can't reuse what we don't have */ |
c9018b03 | 463 | else if (entry->type == OBJ_REF_DELTA || entry->type == OBJ_OFS_DELTA) |
17b08f2c DH |
464 | /* check_object() decided it for us ... */ |
465 | to_reuse = usable_delta; | |
466 | /* ... but pack split may override that */ | |
c9018b03 | 467 | else if (entry->type != entry->in_pack_type) |
ab7cd7bb JH |
468 | to_reuse = 0; /* pack has delta which is unusable */ |
469 | else if (entry->delta) | |
470 | to_reuse = 0; /* we want to pack afresh */ | |
471 | else | |
472 | to_reuse = 1; /* we have it in-pack undeltified, | |
473 | * and we do not need to deltify it. | |
474 | */ | |
475 | ||
c9018b03 NTND |
476 | if (!to_reuse) |
477 | len = write_no_reuse_object(f, entry, limit, usable_delta); | |
478 | else | |
479 | len = write_reuse_object(f, entry, limit, usable_delta); | |
480 | if (!len) | |
481 | return 0; | |
64bd76b1 | 482 | |
17b08f2c | 483 | if (usable_delta) |
ab7cd7bb | 484 | written_delta++; |
3f9ac8d2 | 485 | written++; |
78d1e84f | 486 | if (!pack_to_stdout) |
aa7e44bf | 487 | entry->idx.crc32 = crc32_end(f); |
c9018b03 | 488 | return len; |
c323ac7d LT |
489 | } |
490 | ||
f63c79db JH |
491 | enum write_one_status { |
492 | WRITE_ONE_SKIP = -1, /* already written */ | |
493 | WRITE_ONE_BREAK = 0, /* writing this will bust the limit; not written */ | |
494 | WRITE_ONE_WRITTEN = 1, /* normal */ | |
495 | WRITE_ONE_RECURSIVE = 2 /* already scheduled to be written */ | |
496 | }; | |
497 | ||
498 | static enum write_one_status write_one(struct sha1file *f, | |
499 | struct object_entry *e, | |
500 | off_t *offset) | |
9d5ab962 | 501 | { |
d7dd0223 | 502 | unsigned long size; |
f63c79db | 503 | int recursing; |
d7dd0223 | 504 | |
f63c79db JH |
505 | /* |
506 | * we set offset to 1 (which is an impossible value) to mark | |
507 | * the fact that this object is involved in "write its base | |
508 | * first before writing a deltified object" recursion. | |
509 | */ | |
510 | recursing = (e->idx.offset == 1); | |
511 | if (recursing) { | |
512 | warning("recursive delta detected for object %s", | |
513 | sha1_to_hex(e->idx.sha1)); | |
514 | return WRITE_ONE_RECURSIVE; | |
515 | } else if (e->idx.offset || e->preferred_base) { | |
516 | /* offset is non zero if object is written already. */ | |
517 | return WRITE_ONE_SKIP; | |
518 | } | |
d7dd0223 | 519 | |
720c9f7b | 520 | /* if we are deltified, write out base object first. */ |
f63c79db JH |
521 | if (e->delta) { |
522 | e->idx.offset = 1; /* now recurse */ | |
523 | switch (write_one(f, e->delta, offset)) { | |
524 | case WRITE_ONE_RECURSIVE: | |
525 | /* we cannot depend on this one */ | |
526 | e->delta = NULL; | |
527 | break; | |
528 | default: | |
529 | break; | |
530 | case WRITE_ONE_BREAK: | |
531 | e->idx.offset = recursing; | |
532 | return WRITE_ONE_BREAK; | |
533 | } | |
534 | } | |
d7dd0223 | 535 | |
6ed7f25e NP |
536 | e->idx.offset = *offset; |
537 | size = write_object(f, e, *offset); | |
17b08f2c | 538 | if (!size) { |
f63c79db JH |
539 | e->idx.offset = recursing; |
540 | return WRITE_ONE_BREAK; | |
17b08f2c | 541 | } |
79814f42 | 542 | written_list[nr_written++] = &e->idx; |
d7dd0223 NP |
543 | |
544 | /* make sure off_t is sufficiently large not to wrap */ | |
c03c8315 | 545 | if (signed_add_overflows(*offset, size)) |
d7dd0223 | 546 | die("pack too large for current definition of off_t"); |
6ed7f25e | 547 | *offset += size; |
f63c79db | 548 | return WRITE_ONE_WRITTEN; |
9d5ab962 JH |
549 | } |
550 | ||
1b4bb16b JH |
551 | static int mark_tagged(const char *path, const unsigned char *sha1, int flag, |
552 | void *cb_data) | |
553 | { | |
554 | unsigned char peeled[20]; | |
555 | struct object_entry *entry = locate_object_entry(sha1); | |
556 | ||
557 | if (entry) | |
558 | entry->tagged = 1; | |
559 | if (!peel_ref(path, peeled)) { | |
560 | entry = locate_object_entry(peeled); | |
561 | if (entry) | |
562 | entry->tagged = 1; | |
563 | } | |
564 | return 0; | |
565 | } | |
566 | ||
be126818 | 567 | static inline void add_to_write_order(struct object_entry **wo, |
92bef1a1 | 568 | unsigned int *endp, |
1b4bb16b JH |
569 | struct object_entry *e) |
570 | { | |
571 | if (e->filled) | |
572 | return; | |
573 | wo[(*endp)++] = e; | |
574 | e->filled = 1; | |
575 | } | |
576 | ||
577 | static void add_descendants_to_write_order(struct object_entry **wo, | |
92bef1a1 | 578 | unsigned int *endp, |
1b4bb16b JH |
579 | struct object_entry *e) |
580 | { | |
f380872f DM |
581 | int add_to_order = 1; |
582 | while (e) { | |
583 | if (add_to_order) { | |
584 | struct object_entry *s; | |
585 | /* add this node... */ | |
586 | add_to_write_order(wo, endp, e); | |
587 | /* all its siblings... */ | |
588 | for (s = e->delta_sibling; s; s = s->delta_sibling) { | |
589 | add_to_write_order(wo, endp, s); | |
590 | } | |
591 | } | |
592 | /* drop down a level to add left subtree nodes if possible */ | |
593 | if (e->delta_child) { | |
594 | add_to_order = 1; | |
595 | e = e->delta_child; | |
596 | } else { | |
597 | add_to_order = 0; | |
598 | /* our sibling might have some children, it is next */ | |
599 | if (e->delta_sibling) { | |
600 | e = e->delta_sibling; | |
601 | continue; | |
602 | } | |
603 | /* go back to our parent node */ | |
604 | e = e->delta; | |
605 | while (e && !e->delta_sibling) { | |
606 | /* we're on the right side of a subtree, keep | |
607 | * going up until we can go right again */ | |
608 | e = e->delta; | |
609 | } | |
610 | if (!e) { | |
611 | /* done- we hit our original root node */ | |
612 | return; | |
613 | } | |
614 | /* pass it off to sibling at this level */ | |
615 | e = e->delta_sibling; | |
616 | } | |
617 | }; | |
1b4bb16b JH |
618 | } |
619 | ||
620 | static void add_family_to_write_order(struct object_entry **wo, | |
92bef1a1 | 621 | unsigned int *endp, |
1b4bb16b JH |
622 | struct object_entry *e) |
623 | { | |
624 | struct object_entry *root; | |
625 | ||
626 | for (root = e; root->delta; root = root->delta) | |
627 | ; /* nothing */ | |
1b4bb16b JH |
628 | add_descendants_to_write_order(wo, endp, root); |
629 | } | |
630 | ||
631 | static struct object_entry **compute_write_order(void) | |
632 | { | |
38d4debb | 633 | unsigned int i, wo_end, last_untagged; |
1b4bb16b JH |
634 | |
635 | struct object_entry **wo = xmalloc(nr_objects * sizeof(*wo)); | |
636 | ||
637 | for (i = 0; i < nr_objects; i++) { | |
638 | objects[i].tagged = 0; | |
639 | objects[i].filled = 0; | |
640 | objects[i].delta_child = NULL; | |
641 | objects[i].delta_sibling = NULL; | |
642 | } | |
643 | ||
644 | /* | |
645 | * Fully connect delta_child/delta_sibling network. | |
646 | * Make sure delta_sibling is sorted in the original | |
647 | * recency order. | |
648 | */ | |
92bef1a1 DM |
649 | for (i = nr_objects; i > 0;) { |
650 | struct object_entry *e = &objects[--i]; | |
1b4bb16b JH |
651 | if (!e->delta) |
652 | continue; | |
653 | /* Mark me as the first child */ | |
654 | e->delta_sibling = e->delta->delta_child; | |
655 | e->delta->delta_child = e; | |
656 | } | |
657 | ||
658 | /* | |
659 | * Mark objects that are at the tip of tags. | |
660 | */ | |
661 | for_each_tag_ref(mark_tagged, NULL); | |
662 | ||
663 | /* | |
38d4debb | 664 | * Give the objects in the original recency order until |
1b4bb16b JH |
665 | * we see a tagged tip. |
666 | */ | |
667 | for (i = wo_end = 0; i < nr_objects; i++) { | |
668 | if (objects[i].tagged) | |
669 | break; | |
670 | add_to_write_order(wo, &wo_end, &objects[i]); | |
671 | } | |
38d4debb | 672 | last_untagged = i; |
1b4bb16b JH |
673 | |
674 | /* | |
675 | * Then fill all the tagged tips. | |
676 | */ | |
677 | for (; i < nr_objects; i++) { | |
678 | if (objects[i].tagged) | |
679 | add_to_write_order(wo, &wo_end, &objects[i]); | |
680 | } | |
681 | ||
682 | /* | |
683 | * And then all remaining commits and tags. | |
684 | */ | |
38d4debb | 685 | for (i = last_untagged; i < nr_objects; i++) { |
1b4bb16b JH |
686 | if (objects[i].type != OBJ_COMMIT && |
687 | objects[i].type != OBJ_TAG) | |
688 | continue; | |
689 | add_to_write_order(wo, &wo_end, &objects[i]); | |
690 | } | |
691 | ||
692 | /* | |
693 | * And then all the trees. | |
694 | */ | |
38d4debb | 695 | for (i = last_untagged; i < nr_objects; i++) { |
1b4bb16b JH |
696 | if (objects[i].type != OBJ_TREE) |
697 | continue; | |
698 | add_to_write_order(wo, &wo_end, &objects[i]); | |
699 | } | |
700 | ||
701 | /* | |
702 | * Finally all the rest in really tight order | |
703 | */ | |
38d4debb DM |
704 | for (i = last_untagged; i < nr_objects; i++) { |
705 | if (!objects[i].filled) | |
706 | add_family_to_write_order(wo, &wo_end, &objects[i]); | |
707 | } | |
708 | ||
709 | if (wo_end != nr_objects) | |
710 | die("ordered %u objects, expected %"PRIu32, wo_end, nr_objects); | |
1b4bb16b JH |
711 | |
712 | return wo; | |
713 | } | |
714 | ||
d01fb92f | 715 | static void write_pack_file(void) |
c323ac7d | 716 | { |
ebe27b13 | 717 | uint32_t i = 0, j; |
d22b9290 | 718 | struct sha1file *f; |
6ed7f25e | 719 | off_t offset; |
ebe27b13 | 720 | uint32_t nr_remaining = nr_result; |
f746bae8 | 721 | time_t last_mtime = 0; |
1b4bb16b | 722 | struct object_entry **write_order; |
81a216a5 | 723 | |
bcd7954e | 724 | if (progress > pack_to_stdout) |
dc6a0757 | 725 | progress_state = start_progress("Writing objects", nr_result); |
79814f42 | 726 | written_list = xmalloc(nr_objects * sizeof(*written_list)); |
1b4bb16b | 727 | write_order = compute_write_order(); |
183bdb2c | 728 | |
ebe27b13 | 729 | do { |
aa7e44bf | 730 | unsigned char sha1[20]; |
7ba502c4 | 731 | char *pack_tmp_name = NULL; |
aa7e44bf | 732 | |
cdf9db3c | 733 | if (pack_to_stdout) |
2a128d63 | 734 | f = sha1fd_throughput(1, "<stdout>", progress_state); |
cdf9db3c JH |
735 | else |
736 | f = create_tmp_packfile(&pack_tmp_name); | |
ebe27b13 | 737 | |
c0ad4657 JH |
738 | offset = write_pack_header(f, nr_remaining); |
739 | if (!offset) | |
740 | die_errno("unable to write pack header"); | |
ebe27b13 | 741 | nr_written = 0; |
720c9f7b | 742 | for (; i < nr_objects; i++) { |
1b4bb16b | 743 | struct object_entry *e = write_order[i]; |
cddec4f8 | 744 | if (write_one(f, e, &offset) == WRITE_ONE_BREAK) |
720c9f7b NP |
745 | break; |
746 | display_progress(progress_state, written); | |
747 | } | |
c553ca25 | 748 | |
ebe27b13 DH |
749 | /* |
750 | * Did we write the wrong # entries in the header? | |
751 | * If so, rewrite it like in fast-import | |
752 | */ | |
54352bb2 LT |
753 | if (pack_to_stdout) { |
754 | sha1close(f, sha1, CSUM_CLOSE); | |
755 | } else if (nr_written == nr_remaining) { | |
756 | sha1close(f, sha1, CSUM_FSYNC); | |
ebe27b13 | 757 | } else { |
ac0463ed | 758 | int fd = sha1close(f, sha1, 0); |
abeb40e5 | 759 | fixup_pack_header_footer(fd, sha1, pack_tmp_name, |
ac0463ed | 760 | nr_written, sha1, offset); |
7ba502c4 | 761 | close(fd); |
ebe27b13 DH |
762 | } |
763 | ||
764 | if (!pack_to_stdout) { | |
f746bae8 | 765 | struct stat st; |
3bb72562 | 766 | char tmpname[PATH_MAX]; |
d01fb92f | 767 | |
f746bae8 NP |
768 | /* |
769 | * Packs are runtime accessed in their mtime | |
770 | * order since newer packs are more likely to contain | |
771 | * younger objects. So if we are creating multiple | |
772 | * packs then we should modify the mtime of later ones | |
773 | * to preserve this property. | |
774 | */ | |
0e990530 | 775 | if (stat(pack_tmp_name, &st) < 0) { |
f746bae8 | 776 | warning("failed to stat %s: %s", |
0e990530 | 777 | pack_tmp_name, strerror(errno)); |
f746bae8 NP |
778 | } else if (!last_mtime) { |
779 | last_mtime = st.st_mtime; | |
780 | } else { | |
781 | struct utimbuf utb; | |
782 | utb.actime = st.st_atime; | |
783 | utb.modtime = --last_mtime; | |
0e990530 | 784 | if (utime(pack_tmp_name, &utb) < 0) |
f746bae8 NP |
785 | warning("failed utime() on %s: %s", |
786 | tmpname, strerror(errno)); | |
787 | } | |
788 | ||
0e990530 JH |
789 | /* Enough space for "-<sha-1>.pack"? */ |
790 | if (sizeof(tmpname) <= strlen(base_name) + 50) | |
791 | die("pack base name '%s' too long", base_name); | |
792 | snprintf(tmpname, sizeof(tmpname), "%s-", base_name); | |
793 | finish_tmp_packfile(tmpname, pack_tmp_name, | |
794 | written_list, nr_written, | |
795 | &pack_idx_opts, sha1); | |
7ba502c4 | 796 | free(pack_tmp_name); |
aa7e44bf | 797 | puts(sha1_to_hex(sha1)); |
ebe27b13 DH |
798 | } |
799 | ||
800 | /* mark written objects as written to previous pack */ | |
801 | for (j = 0; j < nr_written; j++) { | |
79814f42 | 802 | written_list[j]->offset = (off_t)-1; |
ebe27b13 DH |
803 | } |
804 | nr_remaining -= nr_written; | |
720c9f7b | 805 | } while (nr_remaining && i < nr_objects); |
ebe27b13 DH |
806 | |
807 | free(written_list); | |
1b4bb16b | 808 | free(write_order); |
4d4fcc54 | 809 | stop_progress(&progress_state); |
67c08ce1 | 810 | if (written != nr_result) |
6e1c2344 RJ |
811 | die("wrote %"PRIu32" objects while expecting %"PRIu32, |
812 | written, nr_result); | |
c323ac7d LT |
813 | } |
814 | ||
7a979d99 JH |
815 | static int locate_object_entry_hash(const unsigned char *sha1) |
816 | { | |
817 | int i; | |
818 | unsigned int ui; | |
819 | memcpy(&ui, sha1, sizeof(unsigned int)); | |
820 | i = ui % object_ix_hashsz; | |
821 | while (0 < object_ix[i]) { | |
aa7e44bf | 822 | if (!hashcmp(sha1, objects[object_ix[i] - 1].idx.sha1)) |
7a979d99 JH |
823 | return i; |
824 | if (++i == object_ix_hashsz) | |
825 | i = 0; | |
826 | } | |
827 | return -1 - i; | |
828 | } | |
829 | ||
830 | static struct object_entry *locate_object_entry(const unsigned char *sha1) | |
831 | { | |
832 | int i; | |
833 | ||
834 | if (!object_ix_hashsz) | |
835 | return NULL; | |
836 | ||
837 | i = locate_object_entry_hash(sha1); | |
838 | if (0 <= i) | |
839 | return &objects[object_ix[i]-1]; | |
840 | return NULL; | |
841 | } | |
842 | ||
843 | static void rehash_objects(void) | |
c323ac7d | 844 | { |
7cadf491 | 845 | uint32_t i; |
7a979d99 JH |
846 | struct object_entry *oe; |
847 | ||
848 | object_ix_hashsz = nr_objects * 3; | |
849 | if (object_ix_hashsz < 1024) | |
850 | object_ix_hashsz = 1024; | |
851 | object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz); | |
5379a5c5 | 852 | memset(object_ix, 0, sizeof(int) * object_ix_hashsz); |
7a979d99 | 853 | for (i = 0, oe = objects; i < nr_objects; i++, oe++) { |
aa7e44bf | 854 | int ix = locate_object_entry_hash(oe->idx.sha1); |
7a979d99 JH |
855 | if (0 <= ix) |
856 | continue; | |
857 | ix = -1 - ix; | |
858 | object_ix[ix] = i + 1; | |
859 | } | |
860 | } | |
861 | ||
ce0bd642 | 862 | static unsigned name_hash(const char *name) |
1d6b38cc | 863 | { |
48fb7deb | 864 | unsigned c, hash = 0; |
ce0bd642 | 865 | |
bc32fed5 JH |
866 | if (!name) |
867 | return 0; | |
868 | ||
eeef7135 | 869 | /* |
ce0bd642 LT |
870 | * This effectively just creates a sortable number from the |
871 | * last sixteen non-whitespace characters. Last characters | |
872 | * count "most", so things that end in ".c" sort together. | |
eeef7135 | 873 | */ |
ce0bd642 LT |
874 | while ((c = *name++) != 0) { |
875 | if (isspace(c)) | |
876 | continue; | |
877 | hash = (hash >> 2) + (c << 24); | |
878 | } | |
1d6b38cc JH |
879 | return hash; |
880 | } | |
881 | ||
a74db82e JH |
882 | static void setup_delta_attr_check(struct git_attr_check *check) |
883 | { | |
884 | static struct git_attr *attr_delta; | |
885 | ||
886 | if (!attr_delta) | |
7fb0eaa2 | 887 | attr_delta = git_attr("delta"); |
a74db82e JH |
888 | |
889 | check[0].attr = attr_delta; | |
890 | } | |
891 | ||
892 | static int no_try_delta(const char *path) | |
893 | { | |
894 | struct git_attr_check check[1]; | |
895 | ||
896 | setup_delta_attr_check(check); | |
d932f4eb | 897 | if (git_check_attr(path, ARRAY_SIZE(check), check)) |
a74db82e JH |
898 | return 0; |
899 | if (ATTR_FALSE(check->value)) | |
900 | return 1; | |
901 | return 0; | |
902 | } | |
903 | ||
5c49c116 | 904 | static int add_object_entry(const unsigned char *sha1, enum object_type type, |
bc32fed5 | 905 | const char *name, int exclude) |
c323ac7d | 906 | { |
c323ac7d | 907 | struct object_entry *entry; |
29b734e4 | 908 | struct packed_git *p, *found_pack = NULL; |
6777a59f | 909 | off_t found_offset = 0; |
29b734e4 | 910 | int ix; |
bc32fed5 | 911 | unsigned hash = name_hash(name); |
29b734e4 NP |
912 | |
913 | ix = nr_objects ? locate_object_entry_hash(sha1) : -1; | |
914 | if (ix >= 0) { | |
915 | if (exclude) { | |
916 | entry = objects + object_ix[ix] - 1; | |
81a216a5 NP |
917 | if (!entry->preferred_base) |
918 | nr_result--; | |
29b734e4 NP |
919 | entry->preferred_base = 1; |
920 | } | |
921 | return 0; | |
922 | } | |
c323ac7d | 923 | |
daae0625 BC |
924 | if (!exclude && local && has_loose_object_nonlocal(sha1)) |
925 | return 0; | |
926 | ||
5c49c116 NP |
927 | for (p = packed_git; p; p = p->next) { |
928 | off_t offset = find_pack_entry_one(sha1, p); | |
929 | if (offset) { | |
930 | if (!found_pack) { | |
4c080182 | 931 | if (!is_pack_valid(p)) { |
58a6a9cc | 932 | warning("packfile %s cannot be accessed", p->pack_name); |
4c080182 JK |
933 | continue; |
934 | } | |
5c49c116 NP |
935 | found_offset = offset; |
936 | found_pack = p; | |
64560374 | 937 | } |
5c49c116 NP |
938 | if (exclude) |
939 | break; | |
940 | if (incremental) | |
941 | return 0; | |
942 | if (local && !p->pack_local) | |
943 | return 0; | |
e96fb9b8 BC |
944 | if (ignore_packed_keep && p->pack_local && p->pack_keep) |
945 | return 0; | |
64560374 LT |
946 | } |
947 | } | |
eb019375 | 948 | |
29b734e4 NP |
949 | if (nr_objects >= nr_alloc) { |
950 | nr_alloc = (nr_alloc + 1024) * 3 / 2; | |
7cadf491 | 951 | objects = xrealloc(objects, nr_alloc * sizeof(*entry)); |
c323ac7d | 952 | } |
29b734e4 NP |
953 | |
954 | entry = objects + nr_objects++; | |
c323ac7d | 955 | memset(entry, 0, sizeof(*entry)); |
aa7e44bf | 956 | hashcpy(entry->idx.sha1, sha1); |
27225f2e | 957 | entry->hash = hash; |
5c49c116 NP |
958 | if (type) |
959 | entry->type = type; | |
29b734e4 NP |
960 | if (exclude) |
961 | entry->preferred_base = 1; | |
81a216a5 NP |
962 | else |
963 | nr_result++; | |
29b734e4 NP |
964 | if (found_pack) { |
965 | entry->in_pack = found_pack; | |
966 | entry->in_pack_offset = found_offset; | |
967 | } | |
7a979d99 JH |
968 | |
969 | if (object_ix_hashsz * 3 <= nr_objects * 4) | |
970 | rehash_objects(); | |
29b734e4 NP |
971 | else |
972 | object_ix[-1 - ix] = nr_objects; | |
7a979d99 | 973 | |
4d4fcc54 | 974 | display_progress(progress_state, nr_objects); |
29b734e4 | 975 | |
a74db82e JH |
976 | if (name && no_try_delta(name)) |
977 | entry->no_try_delta = 1; | |
978 | ||
29b734e4 | 979 | return 1; |
c323ac7d LT |
980 | } |
981 | ||
5379a5c5 JH |
982 | struct pbase_tree_cache { |
983 | unsigned char sha1[20]; | |
984 | int ref; | |
985 | int temporary; | |
986 | void *tree_data; | |
987 | unsigned long tree_size; | |
988 | }; | |
989 | ||
990 | static struct pbase_tree_cache *(pbase_tree_cache[256]); | |
991 | static int pbase_tree_cache_ix(const unsigned char *sha1) | |
992 | { | |
993 | return sha1[0] % ARRAY_SIZE(pbase_tree_cache); | |
994 | } | |
995 | static int pbase_tree_cache_ix_incr(int ix) | |
996 | { | |
997 | return (ix+1) % ARRAY_SIZE(pbase_tree_cache); | |
998 | } | |
999 | ||
1000 | static struct pbase_tree { | |
1001 | struct pbase_tree *next; | |
1002 | /* This is a phony "cache" entry; we are not | |
1003 | * going to evict it nor find it through _get() | |
1004 | * mechanism -- this is for the toplevel node that | |
1005 | * would almost always change with any commit. | |
1006 | */ | |
1007 | struct pbase_tree_cache pcache; | |
1008 | } *pbase_tree; | |
1009 | ||
1010 | static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1) | |
1011 | { | |
1012 | struct pbase_tree_cache *ent, *nent; | |
1013 | void *data; | |
1014 | unsigned long size; | |
21666f1a | 1015 | enum object_type type; |
5379a5c5 JH |
1016 | int neigh; |
1017 | int my_ix = pbase_tree_cache_ix(sha1); | |
1018 | int available_ix = -1; | |
1019 | ||
1020 | /* pbase-tree-cache acts as a limited hashtable. | |
1021 | * your object will be found at your index or within a few | |
1022 | * slots after that slot if it is cached. | |
1023 | */ | |
1024 | for (neigh = 0; neigh < 8; neigh++) { | |
1025 | ent = pbase_tree_cache[my_ix]; | |
a89fccd2 | 1026 | if (ent && !hashcmp(ent->sha1, sha1)) { |
5379a5c5 JH |
1027 | ent->ref++; |
1028 | return ent; | |
1029 | } | |
1030 | else if (((available_ix < 0) && (!ent || !ent->ref)) || | |
1031 | ((0 <= available_ix) && | |
1032 | (!ent && pbase_tree_cache[available_ix]))) | |
1033 | available_ix = my_ix; | |
1034 | if (!ent) | |
1035 | break; | |
1036 | my_ix = pbase_tree_cache_ix_incr(my_ix); | |
1037 | } | |
1038 | ||
1039 | /* Did not find one. Either we got a bogus request or | |
1040 | * we need to read and perhaps cache. | |
1041 | */ | |
21666f1a | 1042 | data = read_sha1_file(sha1, &type, &size); |
5379a5c5 JH |
1043 | if (!data) |
1044 | return NULL; | |
21666f1a | 1045 | if (type != OBJ_TREE) { |
5379a5c5 JH |
1046 | free(data); |
1047 | return NULL; | |
1048 | } | |
1049 | ||
1050 | /* We need to either cache or return a throwaway copy */ | |
1051 | ||
1052 | if (available_ix < 0) | |
1053 | ent = NULL; | |
1054 | else { | |
1055 | ent = pbase_tree_cache[available_ix]; | |
1056 | my_ix = available_ix; | |
1057 | } | |
1058 | ||
1059 | if (!ent) { | |
1060 | nent = xmalloc(sizeof(*nent)); | |
1061 | nent->temporary = (available_ix < 0); | |
1062 | } | |
1063 | else { | |
1064 | /* evict and reuse */ | |
1065 | free(ent->tree_data); | |
1066 | nent = ent; | |
1067 | } | |
e702496e | 1068 | hashcpy(nent->sha1, sha1); |
5379a5c5 JH |
1069 | nent->tree_data = data; |
1070 | nent->tree_size = size; | |
1071 | nent->ref = 1; | |
1072 | if (!nent->temporary) | |
1073 | pbase_tree_cache[my_ix] = nent; | |
1074 | return nent; | |
1075 | } | |
1076 | ||
1077 | static void pbase_tree_put(struct pbase_tree_cache *cache) | |
1078 | { | |
1079 | if (!cache->temporary) { | |
1080 | cache->ref--; | |
1081 | return; | |
1082 | } | |
1083 | free(cache->tree_data); | |
1084 | free(cache); | |
1085 | } | |
1086 | ||
1087 | static int name_cmp_len(const char *name) | |
1088 | { | |
1089 | int i; | |
1090 | for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++) | |
1091 | ; | |
1092 | return i; | |
1093 | } | |
1094 | ||
1095 | static void add_pbase_object(struct tree_desc *tree, | |
5379a5c5 | 1096 | const char *name, |
ce0bd642 LT |
1097 | int cmplen, |
1098 | const char *fullname) | |
3f9ac8d2 | 1099 | { |
4c068a98 | 1100 | struct name_entry entry; |
8a5a8d6c | 1101 | int cmp; |
4c068a98 LT |
1102 | |
1103 | while (tree_entry(tree,&entry)) { | |
1211be6b LT |
1104 | if (S_ISGITLINK(entry.mode)) |
1105 | continue; | |
0de16337 | 1106 | cmp = tree_entry_len(&entry) != cmplen ? 1 : |
8a5a8d6c NP |
1107 | memcmp(name, entry.path, cmplen); |
1108 | if (cmp > 0) | |
7a979d99 | 1109 | continue; |
8a5a8d6c NP |
1110 | if (cmp < 0) |
1111 | return; | |
5379a5c5 | 1112 | if (name[cmplen] != '/') { |
5c49c116 | 1113 | add_object_entry(entry.sha1, |
4d1012c3 | 1114 | object_type(entry.mode), |
bc32fed5 | 1115 | fullname, 1); |
5379a5c5 JH |
1116 | return; |
1117 | } | |
8a5a8d6c | 1118 | if (S_ISDIR(entry.mode)) { |
7a979d99 | 1119 | struct tree_desc sub; |
5379a5c5 JH |
1120 | struct pbase_tree_cache *tree; |
1121 | const char *down = name+cmplen+1; | |
1122 | int downlen = name_cmp_len(down); | |
1123 | ||
4c068a98 | 1124 | tree = pbase_tree_get(entry.sha1); |
5379a5c5 JH |
1125 | if (!tree) |
1126 | return; | |
6fda5e51 | 1127 | init_tree_desc(&sub, tree->tree_data, tree->tree_size); |
5379a5c5 | 1128 | |
ce0bd642 | 1129 | add_pbase_object(&sub, down, downlen, fullname); |
5379a5c5 JH |
1130 | pbase_tree_put(tree); |
1131 | } | |
1132 | } | |
1133 | } | |
1d6b38cc | 1134 | |
5379a5c5 JH |
1135 | static unsigned *done_pbase_paths; |
1136 | static int done_pbase_paths_num; | |
1137 | static int done_pbase_paths_alloc; | |
1138 | static int done_pbase_path_pos(unsigned hash) | |
1139 | { | |
1140 | int lo = 0; | |
1141 | int hi = done_pbase_paths_num; | |
1142 | while (lo < hi) { | |
1143 | int mi = (hi + lo) / 2; | |
1144 | if (done_pbase_paths[mi] == hash) | |
1145 | return mi; | |
1146 | if (done_pbase_paths[mi] < hash) | |
1147 | hi = mi; | |
1148 | else | |
1149 | lo = mi + 1; | |
1150 | } | |
1151 | return -lo-1; | |
1152 | } | |
1153 | ||
1154 | static int check_pbase_path(unsigned hash) | |
1155 | { | |
1156 | int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash); | |
1157 | if (0 <= pos) | |
1158 | return 1; | |
1159 | pos = -pos - 1; | |
1160 | if (done_pbase_paths_alloc <= done_pbase_paths_num) { | |
1161 | done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc); | |
1162 | done_pbase_paths = xrealloc(done_pbase_paths, | |
1163 | done_pbase_paths_alloc * | |
1164 | sizeof(unsigned)); | |
1165 | } | |
1166 | done_pbase_paths_num++; | |
1167 | if (pos < done_pbase_paths_num) | |
1168 | memmove(done_pbase_paths + pos + 1, | |
1169 | done_pbase_paths + pos, | |
1170 | (done_pbase_paths_num - pos - 1) * sizeof(unsigned)); | |
1171 | done_pbase_paths[pos] = hash; | |
1172 | return 0; | |
1173 | } | |
1174 | ||
bc32fed5 | 1175 | static void add_preferred_base_object(const char *name) |
5379a5c5 JH |
1176 | { |
1177 | struct pbase_tree *it; | |
8a5a8d6c | 1178 | int cmplen; |
bc32fed5 | 1179 | unsigned hash = name_hash(name); |
5379a5c5 | 1180 | |
8a5a8d6c | 1181 | if (!num_preferred_base || check_pbase_path(hash)) |
5379a5c5 JH |
1182 | return; |
1183 | ||
8a5a8d6c | 1184 | cmplen = name_cmp_len(name); |
5379a5c5 JH |
1185 | for (it = pbase_tree; it; it = it->next) { |
1186 | if (cmplen == 0) { | |
bc32fed5 | 1187 | add_object_entry(it->pcache.sha1, OBJ_TREE, NULL, 1); |
5379a5c5 JH |
1188 | } |
1189 | else { | |
1190 | struct tree_desc tree; | |
6fda5e51 | 1191 | init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size); |
ce0bd642 | 1192 | add_pbase_object(&tree, name, cmplen, name); |
7a979d99 | 1193 | } |
3f9ac8d2 | 1194 | } |
3f9ac8d2 JH |
1195 | } |
1196 | ||
7a979d99 | 1197 | static void add_preferred_base(unsigned char *sha1) |
3f9ac8d2 | 1198 | { |
5379a5c5 JH |
1199 | struct pbase_tree *it; |
1200 | void *data; | |
1201 | unsigned long size; | |
1202 | unsigned char tree_sha1[20]; | |
1d6b38cc | 1203 | |
8d1d8f83 JH |
1204 | if (window <= num_preferred_base++) |
1205 | return; | |
1206 | ||
5379a5c5 JH |
1207 | data = read_object_with_reference(sha1, tree_type, &size, tree_sha1); |
1208 | if (!data) | |
7a979d99 | 1209 | return; |
5379a5c5 JH |
1210 | |
1211 | for (it = pbase_tree; it; it = it->next) { | |
a89fccd2 | 1212 | if (!hashcmp(it->pcache.sha1, tree_sha1)) { |
5379a5c5 JH |
1213 | free(data); |
1214 | return; | |
1215 | } | |
1216 | } | |
1217 | ||
1218 | it = xcalloc(1, sizeof(*it)); | |
1219 | it->next = pbase_tree; | |
1220 | pbase_tree = it; | |
1221 | ||
e702496e | 1222 | hashcpy(it->pcache.sha1, tree_sha1); |
5379a5c5 JH |
1223 | it->pcache.tree_data = data; |
1224 | it->pcache.tree_size = size; | |
3f9ac8d2 JH |
1225 | } |
1226 | ||
0ef95f72 NP |
1227 | static void cleanup_preferred_base(void) |
1228 | { | |
1229 | struct pbase_tree *it; | |
1230 | unsigned i; | |
1231 | ||
1232 | it = pbase_tree; | |
1233 | pbase_tree = NULL; | |
1234 | while (it) { | |
1235 | struct pbase_tree *this = it; | |
1236 | it = this->next; | |
1237 | free(this->pcache.tree_data); | |
1238 | free(this); | |
1239 | } | |
1240 | ||
1241 | for (i = 0; i < ARRAY_SIZE(pbase_tree_cache); i++) { | |
1242 | if (!pbase_tree_cache[i]) | |
1243 | continue; | |
1244 | free(pbase_tree_cache[i]->tree_data); | |
1245 | free(pbase_tree_cache[i]); | |
1246 | pbase_tree_cache[i] = NULL; | |
1247 | } | |
1248 | ||
1249 | free(done_pbase_paths); | |
1250 | done_pbase_paths = NULL; | |
1251 | done_pbase_paths_num = done_pbase_paths_alloc = 0; | |
1252 | } | |
1253 | ||
c323ac7d LT |
1254 | static void check_object(struct object_entry *entry) |
1255 | { | |
5c49c116 | 1256 | if (entry->in_pack) { |
780e6e73 | 1257 | struct packed_git *p = entry->in_pack; |
03e79c88 | 1258 | struct pack_window *w_curs = NULL; |
5c49c116 NP |
1259 | const unsigned char *base_ref = NULL; |
1260 | struct object_entry *base_entry; | |
1261 | unsigned long used, used_0; | |
ef49a7a0 | 1262 | unsigned long avail; |
5c49c116 NP |
1263 | off_t ofs; |
1264 | unsigned char *buf, c; | |
780e6e73 | 1265 | |
6777a59f | 1266 | buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail); |
ab7cd7bb | 1267 | |
5c49c116 | 1268 | /* |
fa736f72 NP |
1269 | * We want in_pack_type even if we do not reuse delta |
1270 | * since non-delta representations could still be reused. | |
ab7cd7bb | 1271 | */ |
09ded04b | 1272 | used = unpack_object_header_buffer(buf, avail, |
5c49c116 NP |
1273 | &entry->in_pack_type, |
1274 | &entry->size); | |
03d66015 NP |
1275 | if (used == 0) |
1276 | goto give_up; | |
ab7cd7bb | 1277 | |
5c49c116 NP |
1278 | /* |
1279 | * Determine if this is a delta and if so whether we can | |
1280 | * reuse it or not. Otherwise let's find out as cheaply as | |
1281 | * possible what the actual type and size for this object is. | |
3f9ac8d2 | 1282 | */ |
5c49c116 NP |
1283 | switch (entry->in_pack_type) { |
1284 | default: | |
1285 | /* Not a delta hence we've already got all we need. */ | |
1286 | entry->type = entry->in_pack_type; | |
1287 | entry->in_pack_header_size = used; | |
03d66015 NP |
1288 | if (entry->type < OBJ_COMMIT || entry->type > OBJ_BLOB) |
1289 | goto give_up; | |
5c49c116 NP |
1290 | unuse_pack(&w_curs); |
1291 | return; | |
1292 | case OBJ_REF_DELTA: | |
a7de7130 | 1293 | if (reuse_delta && !entry->preferred_base) |
5c49c116 NP |
1294 | base_ref = use_pack(p, &w_curs, |
1295 | entry->in_pack_offset + used, NULL); | |
1296 | entry->in_pack_header_size = used + 20; | |
1297 | break; | |
1298 | case OBJ_OFS_DELTA: | |
1299 | buf = use_pack(p, &w_curs, | |
1300 | entry->in_pack_offset + used, NULL); | |
1301 | used_0 = 0; | |
1302 | c = buf[used_0++]; | |
1303 | ofs = c & 127; | |
1304 | while (c & 128) { | |
1305 | ofs += 1; | |
03d66015 NP |
1306 | if (!ofs || MSB(ofs, 7)) { |
1307 | error("delta base offset overflow in pack for %s", | |
1308 | sha1_to_hex(entry->idx.sha1)); | |
1309 | goto give_up; | |
1310 | } | |
5c49c116 NP |
1311 | c = buf[used_0++]; |
1312 | ofs = (ofs << 7) + (c & 127); | |
780e6e73 | 1313 | } |
5c49c116 | 1314 | ofs = entry->in_pack_offset - ofs; |
03d66015 NP |
1315 | if (ofs <= 0 || ofs >= entry->in_pack_offset) { |
1316 | error("delta base offset out of bound for %s", | |
1317 | sha1_to_hex(entry->idx.sha1)); | |
1318 | goto give_up; | |
1319 | } | |
a7de7130 | 1320 | if (reuse_delta && !entry->preferred_base) { |
3449f8c4 NP |
1321 | struct revindex_entry *revidx; |
1322 | revidx = find_pack_revindex(p, ofs); | |
08698b1e NP |
1323 | if (!revidx) |
1324 | goto give_up; | |
3449f8c4 NP |
1325 | base_ref = nth_packed_object_sha1(p, revidx->nr); |
1326 | } | |
5c49c116 NP |
1327 | entry->in_pack_header_size = used + used_0; |
1328 | break; | |
780e6e73 | 1329 | } |
780e6e73 | 1330 | |
5c49c116 NP |
1331 | if (base_ref && (base_entry = locate_object_entry(base_ref))) { |
1332 | /* | |
1333 | * If base_ref was set above that means we wish to | |
1334 | * reuse delta data, and we even found that base | |
1335 | * in the list of objects we want to pack. Goodie! | |
1336 | * | |
1337 | * Depth value does not matter - find_deltas() will | |
1338 | * never consider reused delta as the base object to | |
1339 | * deltify other objects against, in order to avoid | |
1340 | * circular deltas. | |
3f9ac8d2 | 1341 | */ |
780e6e73 | 1342 | entry->type = entry->in_pack_type; |
5c49c116 | 1343 | entry->delta = base_entry; |
64bd76b1 | 1344 | entry->delta_size = entry->size; |
15b4d577 JH |
1345 | entry->delta_sibling = base_entry->delta_child; |
1346 | base_entry->delta_child = entry; | |
5c49c116 NP |
1347 | unuse_pack(&w_curs); |
1348 | return; | |
1349 | } | |
ab7cd7bb | 1350 | |
5c49c116 NP |
1351 | if (entry->type) { |
1352 | /* | |
1353 | * This must be a delta and we already know what the | |
1354 | * final object type is. Let's extract the actual | |
1355 | * object size from the delta header. | |
1356 | */ | |
1357 | entry->size = get_size_from_delta(p, &w_curs, | |
1358 | entry->in_pack_offset + entry->in_pack_header_size); | |
03d66015 NP |
1359 | if (entry->size == 0) |
1360 | goto give_up; | |
5c49c116 | 1361 | unuse_pack(&w_curs); |
3f9ac8d2 JH |
1362 | return; |
1363 | } | |
5c49c116 NP |
1364 | |
1365 | /* | |
1366 | * No choice but to fall back to the recursive delta walk | |
1367 | * with sha1_object_info() to find about the object type | |
1368 | * at this point... | |
1369 | */ | |
03d66015 | 1370 | give_up: |
5c49c116 | 1371 | unuse_pack(&w_curs); |
36e4d74a | 1372 | } |
3f9ac8d2 | 1373 | |
aa7e44bf | 1374 | entry->type = sha1_object_info(entry->idx.sha1, &entry->size); |
6d6f9cdd SP |
1375 | /* |
1376 | * The error condition is checked in prepare_pack(). This is | |
1377 | * to permit a missing preferred base object to be ignored | |
1378 | * as a preferred base. Doing so can result in a larger | |
1379 | * pack file, but the transfer will still take place. | |
1380 | */ | |
3f9ac8d2 JH |
1381 | } |
1382 | ||
5c49c116 NP |
1383 | static int pack_offset_sort(const void *_a, const void *_b) |
1384 | { | |
1385 | const struct object_entry *a = *(struct object_entry **)_a; | |
1386 | const struct object_entry *b = *(struct object_entry **)_b; | |
1387 | ||
1388 | /* avoid filesystem trashing with loose objects */ | |
1389 | if (!a->in_pack && !b->in_pack) | |
aa7e44bf | 1390 | return hashcmp(a->idx.sha1, b->idx.sha1); |
5c49c116 NP |
1391 | |
1392 | if (a->in_pack < b->in_pack) | |
1393 | return -1; | |
1394 | if (a->in_pack > b->in_pack) | |
1395 | return 1; | |
1396 | return a->in_pack_offset < b->in_pack_offset ? -1 : | |
1397 | (a->in_pack_offset > b->in_pack_offset); | |
1398 | } | |
1399 | ||
c323ac7d LT |
1400 | static void get_object_details(void) |
1401 | { | |
7cadf491 | 1402 | uint32_t i; |
5c49c116 NP |
1403 | struct object_entry **sorted_by_offset; |
1404 | ||
1405 | sorted_by_offset = xcalloc(nr_objects, sizeof(struct object_entry *)); | |
1406 | for (i = 0; i < nr_objects; i++) | |
1407 | sorted_by_offset[i] = objects + i; | |
1408 | qsort(sorted_by_offset, nr_objects, sizeof(*sorted_by_offset), pack_offset_sort); | |
c323ac7d | 1409 | |
15366280 JH |
1410 | for (i = 0; i < nr_objects; i++) { |
1411 | struct object_entry *entry = sorted_by_offset[i]; | |
1412 | check_object(entry); | |
754980d0 | 1413 | if (big_file_threshold < entry->size) |
15366280 JH |
1414 | entry->no_try_delta = 1; |
1415 | } | |
3449f8c4 | 1416 | |
5c49c116 | 1417 | free(sorted_by_offset); |
c323ac7d LT |
1418 | } |
1419 | ||
b904166c NP |
1420 | /* |
1421 | * We search for deltas in a list sorted by type, by filename hash, and then | |
1422 | * by size, so that we see progressively smaller and smaller files. | |
1423 | * That's because we prefer deltas to be from the bigger file | |
1424 | * to the smaller -- deletes are potentially cheaper, but perhaps | |
1425 | * more importantly, the bigger file is likely the more recent | |
1426 | * one. The deepest deltas are therefore the oldest objects which are | |
1427 | * less susceptible to be accessed often. | |
1428 | */ | |
9668cf59 | 1429 | static int type_size_sort(const void *_a, const void *_b) |
c323ac7d | 1430 | { |
9668cf59 NP |
1431 | const struct object_entry *a = *(struct object_entry **)_a; |
1432 | const struct object_entry *b = *(struct object_entry **)_b; | |
1433 | ||
c323ac7d | 1434 | if (a->type > b->type) |
27225f2e | 1435 | return -1; |
b904166c | 1436 | if (a->type < b->type) |
27225f2e | 1437 | return 1; |
b904166c | 1438 | if (a->hash > b->hash) |
7a979d99 | 1439 | return -1; |
b904166c | 1440 | if (a->hash < b->hash) |
7a979d99 | 1441 | return 1; |
b904166c | 1442 | if (a->preferred_base > b->preferred_base) |
c323ac7d | 1443 | return -1; |
b904166c NP |
1444 | if (a->preferred_base < b->preferred_base) |
1445 | return 1; | |
c323ac7d | 1446 | if (a->size > b->size) |
b904166c NP |
1447 | return -1; |
1448 | if (a->size < b->size) | |
c323ac7d | 1449 | return 1; |
b904166c | 1450 | return a < b ? -1 : (a > b); /* newest first */ |
c323ac7d LT |
1451 | } |
1452 | ||
1453 | struct unpacked { | |
1454 | struct object_entry *entry; | |
1455 | void *data; | |
f6c7081a | 1456 | struct delta_index *index; |
5a235b5e | 1457 | unsigned depth; |
c323ac7d LT |
1458 | }; |
1459 | ||
d250626c NP |
1460 | static int delta_cacheable(unsigned long src_size, unsigned long trg_size, |
1461 | unsigned long delta_size) | |
074b2eea MK |
1462 | { |
1463 | if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size) | |
1464 | return 0; | |
1465 | ||
e3dfddb3 MK |
1466 | if (delta_size < cache_max_small_delta_size) |
1467 | return 1; | |
1468 | ||
074b2eea MK |
1469 | /* cache delta, if objects are large enough compared to delta size */ |
1470 | if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10)) | |
1471 | return 1; | |
1472 | ||
1473 | return 0; | |
1474 | } | |
1475 | ||
7eb151d6 | 1476 | #ifndef NO_PTHREADS |
8ecce684 | 1477 | |
44626dc7 | 1478 | static pthread_mutex_t read_mutex; |
8ecce684 NP |
1479 | #define read_lock() pthread_mutex_lock(&read_mutex) |
1480 | #define read_unlock() pthread_mutex_unlock(&read_mutex) | |
1481 | ||
44626dc7 | 1482 | static pthread_mutex_t cache_mutex; |
3c701839 NP |
1483 | #define cache_lock() pthread_mutex_lock(&cache_mutex) |
1484 | #define cache_unlock() pthread_mutex_unlock(&cache_mutex) | |
1485 | ||
44626dc7 | 1486 | static pthread_mutex_t progress_mutex; |
8ecce684 NP |
1487 | #define progress_lock() pthread_mutex_lock(&progress_mutex) |
1488 | #define progress_unlock() pthread_mutex_unlock(&progress_mutex) | |
1489 | ||
1490 | #else | |
1491 | ||
e1ef8673 JH |
1492 | #define read_lock() (void)0 |
1493 | #define read_unlock() (void)0 | |
1494 | #define cache_lock() (void)0 | |
1495 | #define cache_unlock() (void)0 | |
1496 | #define progress_lock() (void)0 | |
1497 | #define progress_unlock() (void)0 | |
8ecce684 NP |
1498 | |
1499 | #endif | |
1500 | ||
f6c7081a | 1501 | static int try_delta(struct unpacked *trg, struct unpacked *src, |
ef0316fc | 1502 | unsigned max_depth, unsigned long *mem_usage) |
c323ac7d | 1503 | { |
f6c7081a NP |
1504 | struct object_entry *trg_entry = trg->entry; |
1505 | struct object_entry *src_entry = src->entry; | |
560b25a8 | 1506 | unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz; |
c83f032e | 1507 | unsigned ref_depth; |
21666f1a | 1508 | enum object_type type; |
c323ac7d LT |
1509 | void *delta_buf; |
1510 | ||
1511 | /* Don't bother doing diffs between different types */ | |
f6c7081a | 1512 | if (trg_entry->type != src_entry->type) |
c323ac7d LT |
1513 | return -1; |
1514 | ||
51d1e83f | 1515 | /* |
15f07e06 JK |
1516 | * We do not bother to try a delta that we discarded on an |
1517 | * earlier try, but only when reusing delta data. Note that | |
1518 | * src_entry that is marked as the preferred_base should always | |
1519 | * be considered, as even if we produce a suboptimal delta against | |
1520 | * it, we will still save the transfer cost, as we already know | |
1521 | * the other side has it and we won't send src_entry at all. | |
51d1e83f | 1522 | */ |
a7de7130 | 1523 | if (reuse_delta && trg_entry->in_pack && |
e9195b58 | 1524 | trg_entry->in_pack == src_entry->in_pack && |
15f07e06 | 1525 | !src_entry->preferred_base && |
e9195b58 JH |
1526 | trg_entry->in_pack_type != OBJ_REF_DELTA && |
1527 | trg_entry->in_pack_type != OBJ_OFS_DELTA) | |
51d1e83f LT |
1528 | return 0; |
1529 | ||
898b14ce | 1530 | /* Let's not bust the allowed depth. */ |
5a235b5e | 1531 | if (src->depth >= max_depth) |
d116a45a | 1532 | return 0; |
c323ac7d | 1533 | |
c3b06a69 | 1534 | /* Now some size filtering heuristics. */ |
560b25a8 | 1535 | trg_size = trg_entry->size; |
c83f032e NP |
1536 | if (!trg_entry->delta) { |
1537 | max_size = trg_size/2 - 20; | |
1538 | ref_depth = 1; | |
1539 | } else { | |
1540 | max_size = trg_entry->delta_size; | |
5a235b5e | 1541 | ref_depth = trg->depth; |
c83f032e | 1542 | } |
720fe22d | 1543 | max_size = (uint64_t)max_size * (max_depth - src->depth) / |
c83f032e | 1544 | (max_depth - ref_depth + 1); |
c3b06a69 NP |
1545 | if (max_size == 0) |
1546 | return 0; | |
f6c7081a | 1547 | src_size = src_entry->size; |
560b25a8 | 1548 | sizediff = src_size < trg_size ? trg_size - src_size : 0; |
27225f2e | 1549 | if (sizediff >= max_size) |
f527cb8c | 1550 | return 0; |
a1dab41a BD |
1551 | if (trg_size < src_size / 32) |
1552 | return 0; | |
f6c7081a | 1553 | |
560b25a8 NP |
1554 | /* Load data if not already done */ |
1555 | if (!trg->data) { | |
8ecce684 | 1556 | read_lock(); |
aa7e44bf | 1557 | trg->data = read_sha1_file(trg_entry->idx.sha1, &type, &sz); |
8ecce684 | 1558 | read_unlock(); |
2e3404c3 JH |
1559 | if (!trg->data) |
1560 | die("object %s cannot be read", | |
1561 | sha1_to_hex(trg_entry->idx.sha1)); | |
560b25a8 NP |
1562 | if (sz != trg_size) |
1563 | die("object %s inconsistent object length (%lu vs %lu)", | |
aa7e44bf | 1564 | sha1_to_hex(trg_entry->idx.sha1), sz, trg_size); |
ef0316fc | 1565 | *mem_usage += sz; |
560b25a8 NP |
1566 | } |
1567 | if (!src->data) { | |
8ecce684 | 1568 | read_lock(); |
aa7e44bf | 1569 | src->data = read_sha1_file(src_entry->idx.sha1, &type, &sz); |
8ecce684 | 1570 | read_unlock(); |
71064a95 NP |
1571 | if (!src->data) { |
1572 | if (src_entry->preferred_base) { | |
1573 | static int warned = 0; | |
1574 | if (!warned++) | |
1575 | warning("object %s cannot be read", | |
1576 | sha1_to_hex(src_entry->idx.sha1)); | |
1577 | /* | |
1578 | * Those objects are not included in the | |
1579 | * resulting pack. Be resilient and ignore | |
1580 | * them if they can't be read, in case the | |
1581 | * pack could be created nevertheless. | |
1582 | */ | |
1583 | return 0; | |
1584 | } | |
2e3404c3 JH |
1585 | die("object %s cannot be read", |
1586 | sha1_to_hex(src_entry->idx.sha1)); | |
71064a95 | 1587 | } |
560b25a8 NP |
1588 | if (sz != src_size) |
1589 | die("object %s inconsistent object length (%lu vs %lu)", | |
aa7e44bf | 1590 | sha1_to_hex(src_entry->idx.sha1), sz, src_size); |
ef0316fc | 1591 | *mem_usage += sz; |
560b25a8 NP |
1592 | } |
1593 | if (!src->index) { | |
1594 | src->index = create_delta_index(src->data, src_size); | |
a588d88a MK |
1595 | if (!src->index) { |
1596 | static int warned = 0; | |
1597 | if (!warned++) | |
1598 | warning("suboptimal pack - out of memory"); | |
1599 | return 0; | |
1600 | } | |
ef0316fc | 1601 | *mem_usage += sizeof_delta_index(src->index); |
560b25a8 NP |
1602 | } |
1603 | ||
1604 | delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size); | |
c323ac7d | 1605 | if (!delta_buf) |
75c42d8c | 1606 | return 0; |
f6c7081a | 1607 | |
9e2d57a0 | 1608 | if (trg_entry->delta) { |
848d732c BD |
1609 | /* Prefer only shallower same-sized deltas. */ |
1610 | if (delta_size == trg_entry->delta_size && | |
5a235b5e | 1611 | src->depth + 1 >= trg->depth) { |
848d732c BD |
1612 | free(delta_buf); |
1613 | return 0; | |
1614 | } | |
074b2eea | 1615 | } |
9e2d57a0 | 1616 | |
3c701839 NP |
1617 | /* |
1618 | * Handle memory allocation outside of the cache | |
1619 | * accounting lock. Compiler will optimize the strangeness | |
7eb151d6 | 1620 | * away when NO_PTHREADS is defined. |
3c701839 | 1621 | */ |
8e0f7003 | 1622 | free(trg_entry->delta_data); |
3c701839 | 1623 | cache_lock(); |
9e2d57a0 NP |
1624 | if (trg_entry->delta_data) { |
1625 | delta_cache_size -= trg_entry->delta_size; | |
9e2d57a0 NP |
1626 | trg_entry->delta_data = NULL; |
1627 | } | |
d250626c | 1628 | if (delta_cacheable(src_size, trg_size, delta_size)) { |
b7a28f78 | 1629 | delta_cache_size += delta_size; |
3c701839 NP |
1630 | cache_unlock(); |
1631 | trg_entry->delta_data = xrealloc(delta_buf, delta_size); | |
1632 | } else { | |
1633 | cache_unlock(); | |
074b2eea | 1634 | free(delta_buf); |
3c701839 NP |
1635 | } |
1636 | ||
b7a28f78 NP |
1637 | trg_entry->delta = src_entry; |
1638 | trg_entry->delta_size = delta_size; | |
1639 | trg->depth = src->depth + 1; | |
1640 | ||
f6c7081a | 1641 | return 1; |
c323ac7d LT |
1642 | } |
1643 | ||
898b14ce | 1644 | static unsigned int check_delta_limit(struct object_entry *me, unsigned int n) |
b2504a0d | 1645 | { |
898b14ce NP |
1646 | struct object_entry *child = me->delta_child; |
1647 | unsigned int m = n; | |
1648 | while (child) { | |
1649 | unsigned int c = check_delta_limit(child, n + 1); | |
1650 | if (m < c) | |
1651 | m = c; | |
1652 | child = child->delta_sibling; | |
1653 | } | |
1654 | return m; | |
b2504a0d NP |
1655 | } |
1656 | ||
75ad235c | 1657 | static unsigned long free_unpacked(struct unpacked *n) |
a97773ce | 1658 | { |
ef0316fc | 1659 | unsigned long freed_mem = sizeof_delta_index(n->index); |
a97773ce BD |
1660 | free_delta_index(n->index); |
1661 | n->index = NULL; | |
1662 | if (n->data) { | |
ef0316fc | 1663 | freed_mem += n->entry->size; |
a97773ce BD |
1664 | free(n->data); |
1665 | n->data = NULL; | |
a97773ce BD |
1666 | } |
1667 | n->entry = NULL; | |
7d7baa5e | 1668 | n->depth = 0; |
ef0316fc | 1669 | return freed_mem; |
a97773ce BD |
1670 | } |
1671 | ||
384b32c0 | 1672 | static void find_deltas(struct object_entry **list, unsigned *list_size, |
e334977d | 1673 | int window, int depth, unsigned *processed) |
c323ac7d | 1674 | { |
384b32c0 | 1675 | uint32_t i, idx = 0, count = 0; |
7cadf491 | 1676 | struct unpacked *array; |
ef0316fc | 1677 | unsigned long mem_usage = 0; |
c323ac7d | 1678 | |
19d4b416 | 1679 | array = xcalloc(window, sizeof(struct unpacked)); |
21fcd1bd | 1680 | |
384b32c0 | 1681 | for (;;) { |
421b488a | 1682 | struct object_entry *entry; |
c323ac7d | 1683 | struct unpacked *n = array + idx; |
ef0316fc | 1684 | int j, max_depth, best_base = -1; |
c323ac7d | 1685 | |
384b32c0 NP |
1686 | progress_lock(); |
1687 | if (!*list_size) { | |
1688 | progress_unlock(); | |
1689 | break; | |
1690 | } | |
421b488a | 1691 | entry = *list++; |
384b32c0 NP |
1692 | (*list_size)--; |
1693 | if (!entry->preferred_base) { | |
1694 | (*processed)++; | |
1695 | display_progress(progress_state, *processed); | |
1696 | } | |
1697 | progress_unlock(); | |
1698 | ||
ef0316fc | 1699 | mem_usage -= free_unpacked(n); |
c323ac7d | 1700 | n->entry = entry; |
ab7cd7bb | 1701 | |
a97773ce | 1702 | while (window_memory_limit && |
ef0316fc | 1703 | mem_usage > window_memory_limit && |
a97773ce BD |
1704 | count > 1) { |
1705 | uint32_t tail = (idx + window - count) % window; | |
75ad235c | 1706 | mem_usage -= free_unpacked(array + tail); |
a97773ce BD |
1707 | count--; |
1708 | } | |
1709 | ||
75d39853 NP |
1710 | /* We do not compute delta to *create* objects we are not |
1711 | * going to pack. | |
1712 | */ | |
1713 | if (entry->preferred_base) | |
1714 | goto next; | |
1715 | ||
898b14ce NP |
1716 | /* |
1717 | * If the current object is at pack edge, take the depth the | |
1718 | * objects that depend on the current object into account | |
1719 | * otherwise they would become too deep. | |
1720 | */ | |
1721 | max_depth = depth; | |
1722 | if (entry->delta_child) { | |
1723 | max_depth -= check_delta_limit(entry, 0); | |
1724 | if (max_depth <= 0) | |
1725 | goto next; | |
1726 | } | |
1727 | ||
78817c15 LT |
1728 | j = window; |
1729 | while (--j > 0) { | |
77639870 | 1730 | int ret; |
7cadf491 | 1731 | uint32_t other_idx = idx + j; |
c323ac7d | 1732 | struct unpacked *m; |
78817c15 LT |
1733 | if (other_idx >= window) |
1734 | other_idx -= window; | |
c323ac7d LT |
1735 | m = array + other_idx; |
1736 | if (!m->entry) | |
1737 | break; | |
ef0316fc | 1738 | ret = try_delta(n, m, max_depth, &mem_usage); |
77639870 | 1739 | if (ret < 0) |
c323ac7d | 1740 | break; |
77639870 JH |
1741 | else if (ret > 0) |
1742 | best_base = other_idx; | |
c323ac7d | 1743 | } |
898b14ce | 1744 | |
ed4a9031 NP |
1745 | /* |
1746 | * If we decided to cache the delta data, then it is best | |
1747 | * to compress it right away. First because we have to do | |
1748 | * it anyway, and doing it here while we're threaded will | |
1749 | * save a lot of time in the non threaded write phase, | |
1750 | * as well as allow for caching more deltas within | |
1751 | * the same cache size limit. | |
1752 | * ... | |
1753 | * But only if not writing to stdout, since in that case | |
1754 | * the network is most likely throttling writes anyway, | |
1755 | * and therefore it is best to go to the write phase ASAP | |
1756 | * instead, as we can afford spending more time compressing | |
1757 | * between writes at that moment. | |
1758 | */ | |
1759 | if (entry->delta_data && !pack_to_stdout) { | |
1760 | entry->z_delta_size = do_compress(&entry->delta_data, | |
1761 | entry->delta_size); | |
1762 | cache_lock(); | |
1763 | delta_cache_size -= entry->delta_size; | |
1764 | delta_cache_size += entry->z_delta_size; | |
1765 | cache_unlock(); | |
1766 | } | |
1767 | ||
70ca1a3f JH |
1768 | /* if we made n a delta, and if n is already at max |
1769 | * depth, leaving it in the window is pointless. we | |
1770 | * should evict it first. | |
70ca1a3f | 1771 | */ |
70baf5d4 | 1772 | if (entry->delta && max_depth <= n->depth) |
70ca1a3f | 1773 | continue; |
ff45715c | 1774 | |
77639870 JH |
1775 | /* |
1776 | * Move the best delta base up in the window, after the | |
1777 | * currently deltified object, to keep it longer. It will | |
1778 | * be the first base object to be attempted next. | |
1779 | */ | |
1780 | if (entry->delta) { | |
1781 | struct unpacked swap = array[best_base]; | |
1782 | int dist = (window + idx - best_base) % window; | |
1783 | int dst = best_base; | |
1784 | while (dist--) { | |
1785 | int src = (dst + 1) % window; | |
1786 | array[dst] = array[src]; | |
1787 | dst = src; | |
1788 | } | |
1789 | array[dst] = swap; | |
1790 | } | |
1791 | ||
898b14ce | 1792 | next: |
521a4f4c | 1793 | idx++; |
a97773ce BD |
1794 | if (count + 1 < window) |
1795 | count++; | |
521a4f4c LT |
1796 | if (idx >= window) |
1797 | idx = 0; | |
384b32c0 | 1798 | } |
adee7bdf | 1799 | |
f6c7081a | 1800 | for (i = 0; i < window; ++i) { |
ff45715c | 1801 | free_delta_index(array[i].index); |
adee7bdf | 1802 | free(array[i].data); |
f6c7081a | 1803 | } |
adee7bdf | 1804 | free(array); |
c323ac7d LT |
1805 | } |
1806 | ||
7eb151d6 | 1807 | #ifndef NO_PTHREADS |
8ecce684 | 1808 | |
a9a74636 NP |
1809 | static void try_to_free_from_threads(size_t size) |
1810 | { | |
1811 | read_lock(); | |
1812 | release_pack_memory(size, -1); | |
1813 | read_unlock(); | |
1814 | } | |
1815 | ||
bc9b2175 | 1816 | static try_to_free_t old_try_to_free_routine; |
851c34b0 | 1817 | |
50f22ada JS |
1818 | /* |
1819 | * The main thread waits on the condition that (at least) one of the workers | |
1820 | * has stopped working (which is indicated in the .working member of | |
1821 | * struct thread_params). | |
1822 | * When a work thread has completed its work, it sets .working to 0 and | |
1823 | * signals the main thread and waits on the condition that .data_ready | |
1824 | * becomes 1. | |
1825 | */ | |
1826 | ||
8ecce684 NP |
1827 | struct thread_params { |
1828 | pthread_t thread; | |
1829 | struct object_entry **list; | |
1830 | unsigned list_size; | |
384b32c0 | 1831 | unsigned remaining; |
8ecce684 NP |
1832 | int window; |
1833 | int depth; | |
50f22ada JS |
1834 | int working; |
1835 | int data_ready; | |
1836 | pthread_mutex_t mutex; | |
1837 | pthread_cond_t cond; | |
8ecce684 NP |
1838 | unsigned *processed; |
1839 | }; | |
1840 | ||
44626dc7 AH |
1841 | static pthread_cond_t progress_cond; |
1842 | ||
1843 | /* | |
1844 | * Mutex and conditional variable can't be statically-initialized on Windows. | |
1845 | */ | |
1846 | static void init_threaded_search(void) | |
1847 | { | |
93749194 | 1848 | init_recursive_mutex(&read_mutex); |
44626dc7 AH |
1849 | pthread_mutex_init(&cache_mutex, NULL); |
1850 | pthread_mutex_init(&progress_mutex, NULL); | |
1851 | pthread_cond_init(&progress_cond, NULL); | |
851c34b0 | 1852 | old_try_to_free_routine = set_try_to_free_routine(try_to_free_from_threads); |
44626dc7 AH |
1853 | } |
1854 | ||
1855 | static void cleanup_threaded_search(void) | |
1856 | { | |
851c34b0 | 1857 | set_try_to_free_routine(old_try_to_free_routine); |
44626dc7 AH |
1858 | pthread_cond_destroy(&progress_cond); |
1859 | pthread_mutex_destroy(&read_mutex); | |
1860 | pthread_mutex_destroy(&cache_mutex); | |
1861 | pthread_mutex_destroy(&progress_mutex); | |
1862 | } | |
c2a33679 | 1863 | |
8ecce684 NP |
1864 | static void *threaded_find_deltas(void *arg) |
1865 | { | |
c2a33679 NP |
1866 | struct thread_params *me = arg; |
1867 | ||
50f22ada | 1868 | while (me->remaining) { |
384b32c0 | 1869 | find_deltas(me->list, &me->remaining, |
c2a33679 | 1870 | me->window, me->depth, me->processed); |
50f22ada JS |
1871 | |
1872 | progress_lock(); | |
1873 | me->working = 0; | |
1874 | pthread_cond_signal(&progress_cond); | |
1875 | progress_unlock(); | |
1876 | ||
1877 | /* | |
1878 | * We must not set ->data_ready before we wait on the | |
1879 | * condition because the main thread may have set it to 1 | |
1880 | * before we get here. In order to be sure that new | |
1881 | * work is available if we see 1 in ->data_ready, it | |
1882 | * was initialized to 0 before this thread was spawned | |
1883 | * and we reset it to 0 right away. | |
1884 | */ | |
1885 | pthread_mutex_lock(&me->mutex); | |
1886 | while (!me->data_ready) | |
1887 | pthread_cond_wait(&me->cond, &me->mutex); | |
1888 | me->data_ready = 0; | |
1889 | pthread_mutex_unlock(&me->mutex); | |
c2a33679 | 1890 | } |
50f22ada JS |
1891 | /* leave ->working 1 so that this doesn't get more work assigned */ |
1892 | return NULL; | |
8ecce684 NP |
1893 | } |
1894 | ||
8ecce684 NP |
1895 | static void ll_find_deltas(struct object_entry **list, unsigned list_size, |
1896 | int window, int depth, unsigned *processed) | |
1897 | { | |
dcda3614 | 1898 | struct thread_params *p; |
384b32c0 | 1899 | int i, ret, active_threads = 0; |
c2a33679 | 1900 | |
44626dc7 AH |
1901 | init_threaded_search(); |
1902 | ||
ef055571 NP |
1903 | if (!delta_search_threads) /* --threads=0 means autodetect */ |
1904 | delta_search_threads = online_cpus(); | |
367f4a43 | 1905 | if (delta_search_threads <= 1) { |
384b32c0 | 1906 | find_deltas(list, &list_size, window, depth, processed); |
44626dc7 | 1907 | cleanup_threaded_search(); |
367f4a43 NP |
1908 | return; |
1909 | } | |
43cc2b42 | 1910 | if (progress > pack_to_stdout) |
b6c29915 | 1911 | fprintf(stderr, "Delta compression using up to %d threads.\n", |
43cc2b42 | 1912 | delta_search_threads); |
dcda3614 | 1913 | p = xcalloc(delta_search_threads, sizeof(*p)); |
367f4a43 | 1914 | |
50f22ada | 1915 | /* Partition the work amongst work threads. */ |
367f4a43 | 1916 | for (i = 0; i < delta_search_threads; i++) { |
50f22ada JS |
1917 | unsigned sub_size = list_size / (delta_search_threads - i); |
1918 | ||
bf874896 NP |
1919 | /* don't use too small segments or no deltas will be found */ |
1920 | if (sub_size < 2*window && i+1 < delta_search_threads) | |
1921 | sub_size = 0; | |
1922 | ||
8ecce684 NP |
1923 | p[i].window = window; |
1924 | p[i].depth = depth; | |
1925 | p[i].processed = processed; | |
50f22ada JS |
1926 | p[i].working = 1; |
1927 | p[i].data_ready = 0; | |
c2a33679 | 1928 | |
59921b4b | 1929 | /* try to split chunks on "path" boundaries */ |
6fc74703 NP |
1930 | while (sub_size && sub_size < list_size && |
1931 | list[sub_size]->hash && | |
384b32c0 NP |
1932 | list[sub_size]->hash == list[sub_size-1]->hash) |
1933 | sub_size++; | |
1934 | ||
50f22ada JS |
1935 | p[i].list = list; |
1936 | p[i].list_size = sub_size; | |
1937 | p[i].remaining = sub_size; | |
59921b4b | 1938 | |
384b32c0 NP |
1939 | list += sub_size; |
1940 | list_size -= sub_size; | |
1941 | } | |
1942 | ||
50f22ada JS |
1943 | /* Start work threads. */ |
1944 | for (i = 0; i < delta_search_threads; i++) { | |
1945 | if (!p[i].list_size) | |
1946 | continue; | |
68e6a4f8 JS |
1947 | pthread_mutex_init(&p[i].mutex, NULL); |
1948 | pthread_cond_init(&p[i].cond, NULL); | |
50f22ada JS |
1949 | ret = pthread_create(&p[i].thread, NULL, |
1950 | threaded_find_deltas, &p[i]); | |
1951 | if (ret) | |
1952 | die("unable to create thread: %s", strerror(ret)); | |
1953 | active_threads++; | |
1954 | } | |
1955 | ||
384b32c0 NP |
1956 | /* |
1957 | * Now let's wait for work completion. Each time a thread is done | |
1958 | * with its work, we steal half of the remaining work from the | |
1959 | * thread with the largest number of unprocessed objects and give | |
1960 | * it to that newly idle thread. This ensure good load balancing | |
1961 | * until the remaining object list segments are simply too short | |
1962 | * to be worth splitting anymore. | |
1963 | */ | |
50f22ada JS |
1964 | while (active_threads) { |
1965 | struct thread_params *target = NULL; | |
384b32c0 NP |
1966 | struct thread_params *victim = NULL; |
1967 | unsigned sub_size = 0; | |
384b32c0 NP |
1968 | |
1969 | progress_lock(); | |
50f22ada JS |
1970 | for (;;) { |
1971 | for (i = 0; !target && i < delta_search_threads; i++) | |
1972 | if (!p[i].working) | |
1973 | target = &p[i]; | |
1974 | if (target) | |
1975 | break; | |
1976 | pthread_cond_wait(&progress_cond, &progress_mutex); | |
1977 | } | |
1978 | ||
384b32c0 NP |
1979 | for (i = 0; i < delta_search_threads; i++) |
1980 | if (p[i].remaining > 2*window && | |
1981 | (!victim || victim->remaining < p[i].remaining)) | |
1982 | victim = &p[i]; | |
1983 | if (victim) { | |
1984 | sub_size = victim->remaining / 2; | |
1985 | list = victim->list + victim->list_size - sub_size; | |
1986 | while (sub_size && list[0]->hash && | |
1987 | list[0]->hash == list[-1]->hash) { | |
1988 | list++; | |
1989 | sub_size--; | |
1990 | } | |
eb9688ff NP |
1991 | if (!sub_size) { |
1992 | /* | |
1993 | * It is possible for some "paths" to have | |
1994 | * so many objects that no hash boundary | |
1995 | * might be found. Let's just steal the | |
1996 | * exact half in that case. | |
1997 | */ | |
1998 | sub_size = victim->remaining / 2; | |
1999 | list -= sub_size; | |
2000 | } | |
384b32c0 NP |
2001 | target->list = list; |
2002 | victim->list_size -= sub_size; | |
2003 | victim->remaining -= sub_size; | |
2004 | } | |
384b32c0 NP |
2005 | target->list_size = sub_size; |
2006 | target->remaining = sub_size; | |
50f22ada JS |
2007 | target->working = 1; |
2008 | progress_unlock(); | |
2009 | ||
2010 | pthread_mutex_lock(&target->mutex); | |
2011 | target->data_ready = 1; | |
2012 | pthread_cond_signal(&target->cond); | |
2013 | pthread_mutex_unlock(&target->mutex); | |
c2a33679 | 2014 | |
384b32c0 | 2015 | if (!sub_size) { |
b81d9af7 | 2016 | pthread_join(target->thread, NULL); |
50f22ada JS |
2017 | pthread_cond_destroy(&target->cond); |
2018 | pthread_mutex_destroy(&target->mutex); | |
384b32c0 | 2019 | active_threads--; |
c2a33679 | 2020 | } |
50f22ada | 2021 | } |
44626dc7 | 2022 | cleanup_threaded_search(); |
dcda3614 | 2023 | free(p); |
8ecce684 NP |
2024 | } |
2025 | ||
2026 | #else | |
384b32c0 | 2027 | #define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p) |
8ecce684 NP |
2028 | #endif |
2029 | ||
f0a24aa5 SP |
2030 | static int add_ref_tag(const char *path, const unsigned char *sha1, int flag, void *cb_data) |
2031 | { | |
2032 | unsigned char peeled[20]; | |
2033 | ||
2034 | if (!prefixcmp(path, "refs/tags/") && /* is a tag? */ | |
2035 | !peel_ref(path, peeled) && /* peelable? */ | |
f0a24aa5 SP |
2036 | locate_object_entry(peeled)) /* object packed? */ |
2037 | add_object_entry(sha1, OBJ_TAG, NULL, 0); | |
2038 | return 0; | |
2039 | } | |
2040 | ||
f3123c4a JH |
2041 | static void prepare_pack(int window, int depth) |
2042 | { | |
9668cf59 | 2043 | struct object_entry **delta_list; |
6e1c2344 RJ |
2044 | uint32_t i, nr_deltas; |
2045 | unsigned n; | |
9668cf59 | 2046 | |
3f9ac8d2 | 2047 | get_object_details(); |
9668cf59 | 2048 | |
0e8189e2 NP |
2049 | /* |
2050 | * If we're locally repacking then we need to be doubly careful | |
2051 | * from now on in order to make sure no stealth corruption gets | |
2052 | * propagated to the new pack. Clients receiving streamed packs | |
2053 | * should validate everything they get anyway so no need to incur | |
2054 | * the additional cost here in that case. | |
2055 | */ | |
2056 | if (!pack_to_stdout) | |
2057 | do_check_packed_object_crc = 1; | |
2058 | ||
75d39853 | 2059 | if (!nr_objects || !window || !depth) |
9668cf59 NP |
2060 | return; |
2061 | ||
2062 | delta_list = xmalloc(nr_objects * sizeof(*delta_list)); | |
75d39853 NP |
2063 | nr_deltas = n = 0; |
2064 | ||
2065 | for (i = 0; i < nr_objects; i++) { | |
2066 | struct object_entry *entry = objects + i; | |
2067 | ||
2068 | if (entry->delta) | |
2069 | /* This happens if we decided to reuse existing | |
a7de7130 | 2070 | * delta from a pack. "reuse_delta &&" is implied. |
75d39853 NP |
2071 | */ |
2072 | continue; | |
2073 | ||
2074 | if (entry->size < 50) | |
2075 | continue; | |
2076 | ||
2077 | if (entry->no_try_delta) | |
2078 | continue; | |
2079 | ||
6d6f9cdd | 2080 | if (!entry->preferred_base) { |
75d39853 | 2081 | nr_deltas++; |
6d6f9cdd SP |
2082 | if (entry->type < 0) |
2083 | die("unable to get type of object %s", | |
2084 | sha1_to_hex(entry->idx.sha1)); | |
eede9f42 NP |
2085 | } else { |
2086 | if (entry->type < 0) { | |
2087 | /* | |
2088 | * This object is not found, but we | |
2089 | * don't have to include it anyway. | |
2090 | */ | |
2091 | continue; | |
2092 | } | |
6d6f9cdd | 2093 | } |
75d39853 NP |
2094 | |
2095 | delta_list[n++] = entry; | |
2096 | } | |
2097 | ||
2f8b8947 | 2098 | if (nr_deltas && n > 1) { |
e334977d NP |
2099 | unsigned nr_done = 0; |
2100 | if (progress) | |
dc6a0757 NP |
2101 | progress_state = start_progress("Compressing objects", |
2102 | nr_deltas); | |
75d39853 | 2103 | qsort(delta_list, n, sizeof(*delta_list), type_size_sort); |
8ecce684 | 2104 | ll_find_deltas(delta_list, n, window+1, depth, &nr_done); |
4d4fcc54 | 2105 | stop_progress(&progress_state); |
e334977d NP |
2106 | if (nr_done != nr_deltas) |
2107 | die("inconsistency with delta count"); | |
75d39853 | 2108 | } |
9668cf59 | 2109 | free(delta_list); |
f3123c4a JH |
2110 | } |
2111 | ||
ef90d6d4 | 2112 | static int git_pack_config(const char *k, const char *v, void *cb) |
4812a93a | 2113 | { |
eeefa7c9 | 2114 | if (!strcmp(k, "pack.window")) { |
4812a93a JK |
2115 | window = git_config_int(k, v); |
2116 | return 0; | |
2117 | } | |
a97773ce BD |
2118 | if (!strcmp(k, "pack.windowmemory")) { |
2119 | window_memory_limit = git_config_ulong(k, v); | |
2120 | return 0; | |
2121 | } | |
2122 | if (!strcmp(k, "pack.depth")) { | |
842aaf93 TT |
2123 | depth = git_config_int(k, v); |
2124 | return 0; | |
2125 | } | |
960ccca6 DH |
2126 | if (!strcmp(k, "pack.compression")) { |
2127 | int level = git_config_int(k, v); | |
2128 | if (level == -1) | |
2129 | level = Z_DEFAULT_COMPRESSION; | |
2130 | else if (level < 0 || level > Z_BEST_COMPRESSION) | |
2131 | die("bad pack compression level %d", level); | |
2132 | pack_compression_level = level; | |
2133 | pack_compression_seen = 1; | |
2134 | return 0; | |
2135 | } | |
074b2eea MK |
2136 | if (!strcmp(k, "pack.deltacachesize")) { |
2137 | max_delta_cache_size = git_config_int(k, v); | |
2138 | return 0; | |
2139 | } | |
e3dfddb3 MK |
2140 | if (!strcmp(k, "pack.deltacachelimit")) { |
2141 | cache_max_small_delta_size = git_config_int(k, v); | |
2142 | return 0; | |
2143 | } | |
693b86ff NP |
2144 | if (!strcmp(k, "pack.threads")) { |
2145 | delta_search_threads = git_config_int(k, v); | |
833e3df1 | 2146 | if (delta_search_threads < 0) |
693b86ff NP |
2147 | die("invalid number of threads specified (%d)", |
2148 | delta_search_threads); | |
7eb151d6 | 2149 | #ifdef NO_PTHREADS |
833e3df1 | 2150 | if (delta_search_threads != 1) |
693b86ff NP |
2151 | warning("no threads support, ignoring %s", k); |
2152 | #endif | |
2153 | return 0; | |
2154 | } | |
4d00bda2 | 2155 | if (!strcmp(k, "pack.indexversion")) { |
ebcfb379 JH |
2156 | pack_idx_opts.version = git_config_int(k, v); |
2157 | if (pack_idx_opts.version > 2) | |
6e1c2344 | 2158 | die("bad pack.indexversion=%"PRIu32, |
ebcfb379 | 2159 | pack_idx_opts.version); |
4d00bda2 NP |
2160 | return 0; |
2161 | } | |
ef90d6d4 | 2162 | return git_default_config(k, v, cb); |
4812a93a JK |
2163 | } |
2164 | ||
b5d97e6b | 2165 | static void read_object_list_from_stdin(void) |
c323ac7d | 2166 | { |
b5d97e6b JH |
2167 | char line[40 + 1 + PATH_MAX + 2]; |
2168 | unsigned char sha1[20]; | |
b2504a0d | 2169 | |
da93d12b | 2170 | for (;;) { |
da93d12b LT |
2171 | if (!fgets(line, sizeof(line), stdin)) { |
2172 | if (feof(stdin)) | |
2173 | break; | |
2174 | if (!ferror(stdin)) | |
2175 | die("fgets returned NULL, not EOF, not error!"); | |
687dd75c | 2176 | if (errno != EINTR) |
d824cbba | 2177 | die_errno("fgets"); |
687dd75c JH |
2178 | clearerr(stdin); |
2179 | continue; | |
da93d12b | 2180 | } |
7a979d99 JH |
2181 | if (line[0] == '-') { |
2182 | if (get_sha1_hex(line+1, sha1)) | |
2183 | die("expected edge sha1, got garbage:\n %s", | |
b5d97e6b | 2184 | line); |
8d1d8f83 | 2185 | add_preferred_base(sha1); |
7a979d99 | 2186 | continue; |
21fcd1bd | 2187 | } |
c323ac7d | 2188 | if (get_sha1_hex(line, sha1)) |
ef07618f | 2189 | die("expected sha1, got garbage:\n %s", line); |
b5d97e6b | 2190 | |
bc32fed5 JH |
2191 | add_preferred_base_object(line+41); |
2192 | add_object_entry(sha1, 0, line+41, 0); | |
c323ac7d | 2193 | } |
b5d97e6b JH |
2194 | } |
2195 | ||
08cdfb13 JH |
2196 | #define OBJECT_ADDED (1u<<20) |
2197 | ||
11c211fa | 2198 | static void show_commit(struct commit *commit, void *data) |
b5d97e6b | 2199 | { |
bc32fed5 | 2200 | add_object_entry(commit->object.sha1, OBJ_COMMIT, NULL, 0); |
08cdfb13 | 2201 | commit->object.flags |= OBJECT_ADDED; |
b5d97e6b JH |
2202 | } |
2203 | ||
49473672 JH |
2204 | static void show_object(struct object *obj, |
2205 | const struct name_path *path, const char *last, | |
2206 | void *data) | |
b5d97e6b | 2207 | { |
cf2ab916 LT |
2208 | char *name = path_name(path, last); |
2209 | ||
8d2dfc49 LT |
2210 | add_preferred_base_object(name); |
2211 | add_object_entry(obj->sha1, obj->type, name, 0); | |
2212 | obj->flags |= OBJECT_ADDED; | |
2213 | ||
2214 | /* | |
2215 | * We will have generated the hash from the name, | |
2216 | * but not saved a pointer to it - we can free it | |
2217 | */ | |
2218 | free((char *)name); | |
b5d97e6b JH |
2219 | } |
2220 | ||
8d1d8f83 JH |
2221 | static void show_edge(struct commit *commit) |
2222 | { | |
2223 | add_preferred_base(commit->object.sha1); | |
2224 | } | |
2225 | ||
08cdfb13 JH |
2226 | struct in_pack_object { |
2227 | off_t offset; | |
2228 | struct object *object; | |
2229 | }; | |
2230 | ||
2231 | struct in_pack { | |
2232 | int alloc; | |
2233 | int nr; | |
2234 | struct in_pack_object *array; | |
2235 | }; | |
2236 | ||
2237 | static void mark_in_pack_object(struct object *object, struct packed_git *p, struct in_pack *in_pack) | |
2238 | { | |
2239 | in_pack->array[in_pack->nr].offset = find_pack_entry_one(object->sha1, p); | |
2240 | in_pack->array[in_pack->nr].object = object; | |
2241 | in_pack->nr++; | |
2242 | } | |
2243 | ||
2244 | /* | |
2245 | * Compare the objects in the offset order, in order to emulate the | |
f18d244a | 2246 | * "git rev-list --objects" output that produced the pack originally. |
08cdfb13 JH |
2247 | */ |
2248 | static int ofscmp(const void *a_, const void *b_) | |
2249 | { | |
2250 | struct in_pack_object *a = (struct in_pack_object *)a_; | |
2251 | struct in_pack_object *b = (struct in_pack_object *)b_; | |
2252 | ||
2253 | if (a->offset < b->offset) | |
2254 | return -1; | |
2255 | else if (a->offset > b->offset) | |
2256 | return 1; | |
2257 | else | |
2258 | return hashcmp(a->object->sha1, b->object->sha1); | |
2259 | } | |
2260 | ||
2261 | static void add_objects_in_unpacked_packs(struct rev_info *revs) | |
2262 | { | |
2263 | struct packed_git *p; | |
2264 | struct in_pack in_pack; | |
2265 | uint32_t i; | |
2266 | ||
2267 | memset(&in_pack, 0, sizeof(in_pack)); | |
2268 | ||
2269 | for (p = packed_git; p; p = p->next) { | |
2270 | const unsigned char *sha1; | |
2271 | struct object *o; | |
2272 | ||
79bc4c71 | 2273 | if (!p->pack_local || p->pack_keep) |
08cdfb13 JH |
2274 | continue; |
2275 | if (open_pack_index(p)) | |
2276 | die("cannot open pack index"); | |
2277 | ||
2278 | ALLOC_GROW(in_pack.array, | |
2279 | in_pack.nr + p->num_objects, | |
2280 | in_pack.alloc); | |
2281 | ||
2282 | for (i = 0; i < p->num_objects; i++) { | |
2283 | sha1 = nth_packed_object_sha1(p, i); | |
2284 | o = lookup_unknown_object(sha1); | |
2285 | if (!(o->flags & OBJECT_ADDED)) | |
2286 | mark_in_pack_object(o, p, &in_pack); | |
2287 | o->flags |= OBJECT_ADDED; | |
2288 | } | |
2289 | } | |
2290 | ||
2291 | if (in_pack.nr) { | |
2292 | qsort(in_pack.array, in_pack.nr, sizeof(in_pack.array[0]), | |
2293 | ofscmp); | |
2294 | for (i = 0; i < in_pack.nr; i++) { | |
2295 | struct object *o = in_pack.array[i].object; | |
2296 | add_object_entry(o->sha1, o->type, "", 0); | |
2297 | } | |
2298 | } | |
2299 | free(in_pack.array); | |
2300 | } | |
2301 | ||
094085e3 BC |
2302 | static int has_sha1_pack_kept_or_nonlocal(const unsigned char *sha1) |
2303 | { | |
2304 | static struct packed_git *last_found = (void *)1; | |
2305 | struct packed_git *p; | |
2306 | ||
2307 | p = (last_found != (void *)1) ? last_found : packed_git; | |
2308 | ||
2309 | while (p) { | |
2310 | if ((!p->pack_local || p->pack_keep) && | |
2311 | find_pack_entry_one(sha1, p)) { | |
2312 | last_found = p; | |
2313 | return 1; | |
2314 | } | |
2315 | if (p == last_found) | |
2316 | p = packed_git; | |
2317 | else | |
2318 | p = p->next; | |
2319 | if (p == last_found) | |
2320 | p = p->next; | |
2321 | } | |
2322 | return 0; | |
2323 | } | |
2324 | ||
ca11b212 NP |
2325 | static void loosen_unused_packed_objects(struct rev_info *revs) |
2326 | { | |
2327 | struct packed_git *p; | |
2328 | uint32_t i; | |
2329 | const unsigned char *sha1; | |
2330 | ||
2331 | for (p = packed_git; p; p = p->next) { | |
79bc4c71 | 2332 | if (!p->pack_local || p->pack_keep) |
ca11b212 NP |
2333 | continue; |
2334 | ||
7e52f566 JK |
2335 | if (unpack_unreachable_expiration && |
2336 | p->mtime < unpack_unreachable_expiration) | |
2337 | continue; | |
2338 | ||
ca11b212 NP |
2339 | if (open_pack_index(p)) |
2340 | die("cannot open pack index"); | |
2341 | ||
2342 | for (i = 0; i < p->num_objects; i++) { | |
2343 | sha1 = nth_packed_object_sha1(p, i); | |
094085e3 BC |
2344 | if (!locate_object_entry(sha1) && |
2345 | !has_sha1_pack_kept_or_nonlocal(sha1)) | |
ca11b212 NP |
2346 | if (force_object_loose(sha1, p->mtime)) |
2347 | die("unable to force loose object"); | |
2348 | } | |
2349 | } | |
2350 | } | |
2351 | ||
8d1d8f83 | 2352 | static void get_object_list(int ac, const char **av) |
b5d97e6b JH |
2353 | { |
2354 | struct rev_info revs; | |
2355 | char line[1000]; | |
b5d97e6b JH |
2356 | int flags = 0; |
2357 | ||
b5d97e6b JH |
2358 | init_revisions(&revs, NULL); |
2359 | save_commit_buffer = 0; | |
b5d97e6b JH |
2360 | setup_revisions(ac, av, &revs, NULL); |
2361 | ||
b5d97e6b JH |
2362 | while (fgets(line, sizeof(line), stdin) != NULL) { |
2363 | int len = strlen(line); | |
872c930d | 2364 | if (len && line[len - 1] == '\n') |
b5d97e6b JH |
2365 | line[--len] = 0; |
2366 | if (!len) | |
2367 | break; | |
2368 | if (*line == '-') { | |
2369 | if (!strcmp(line, "--not")) { | |
2370 | flags ^= UNINTERESTING; | |
2371 | continue; | |
2372 | } | |
2373 | die("not a rev '%s'", line); | |
2374 | } | |
8e676e8b | 2375 | if (handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME)) |
b5d97e6b JH |
2376 | die("bad revision '%s'", line); |
2377 | } | |
2378 | ||
3d51e1b5 MK |
2379 | if (prepare_revision_walk(&revs)) |
2380 | die("revision walk setup failed"); | |
e76a5fb4 | 2381 | mark_edges_uninteresting(&revs, show_edge); |
11c211fa | 2382 | traverse_commit_list(&revs, show_commit, show_object, NULL); |
08cdfb13 JH |
2383 | |
2384 | if (keep_unreachable) | |
2385 | add_objects_in_unpacked_packs(&revs); | |
ca11b212 NP |
2386 | if (unpack_unreachable) |
2387 | loosen_unused_packed_objects(&revs); | |
b5d97e6b JH |
2388 | } |
2389 | ||
99fb6e04 NTND |
2390 | static int option_parse_index_version(const struct option *opt, |
2391 | const char *arg, int unset) | |
2392 | { | |
2393 | char *c; | |
2394 | const char *val = arg; | |
2395 | pack_idx_opts.version = strtoul(val, &c, 10); | |
2396 | if (pack_idx_opts.version > 2) | |
2397 | die(_("unsupported index version %s"), val); | |
2398 | if (*c == ',' && c[1]) | |
2399 | pack_idx_opts.off32_limit = strtoul(c+1, &c, 0); | |
2400 | if (*c || pack_idx_opts.off32_limit & 0x80000000) | |
2401 | die(_("bad index version '%s'"), val); | |
2402 | return 0; | |
2403 | } | |
2404 | ||
7e52f566 JK |
2405 | static int option_parse_unpack_unreachable(const struct option *opt, |
2406 | const char *arg, int unset) | |
2407 | { | |
2408 | if (unset) { | |
2409 | unpack_unreachable = 0; | |
2410 | unpack_unreachable_expiration = 0; | |
2411 | } | |
2412 | else { | |
2413 | unpack_unreachable = 1; | |
2414 | if (arg) | |
2415 | unpack_unreachable_expiration = approxidate(arg); | |
2416 | } | |
2417 | return 0; | |
2418 | } | |
2419 | ||
99fb6e04 NTND |
2420 | static int option_parse_ulong(const struct option *opt, |
2421 | const char *arg, int unset) | |
2422 | { | |
2423 | if (unset) | |
2424 | die(_("option %s does not accept negative form"), | |
2425 | opt->long_name); | |
2426 | ||
2427 | if (!git_parse_ulong(arg, opt->value)) | |
2428 | die(_("unable to parse value '%s' for option %s"), | |
2429 | arg, opt->long_name); | |
2430 | return 0; | |
2431 | } | |
2432 | ||
2433 | #define OPT_ULONG(s, l, v, h) \ | |
2434 | { OPTION_CALLBACK, (s), (l), (v), "n", (h), \ | |
2435 | PARSE_OPT_NONEG, option_parse_ulong } | |
2436 | ||
b5d97e6b JH |
2437 | int cmd_pack_objects(int argc, const char **argv, const char *prefix) |
2438 | { | |
b5d97e6b | 2439 | int use_internal_rev_list = 0; |
8d1d8f83 | 2440 | int thin = 0; |
4f366275 | 2441 | int all_progress_implied = 0; |
99fb6e04 NTND |
2442 | const char *rp_av[6]; |
2443 | int rp_ac = 0; | |
2444 | int rev_list_unpacked = 0, rev_list_all = 0, rev_list_reflog = 0; | |
2445 | struct option pack_objects_options[] = { | |
2446 | OPT_SET_INT('q', "quiet", &progress, | |
4c688120 | 2447 | N_("do not show progress meter"), 0), |
99fb6e04 | 2448 | OPT_SET_INT(0, "progress", &progress, |
4c688120 | 2449 | N_("show progress meter"), 1), |
99fb6e04 | 2450 | OPT_SET_INT(0, "all-progress", &progress, |
4c688120 | 2451 | N_("show progress meter during object writing phase"), 2), |
99fb6e04 NTND |
2452 | OPT_BOOL(0, "all-progress-implied", |
2453 | &all_progress_implied, | |
4c688120 NTND |
2454 | N_("similar to --all-progress when progress meter is shown")), |
2455 | { OPTION_CALLBACK, 0, "index-version", NULL, N_("version[,offset]"), | |
2456 | N_("write the pack index file in the specified idx format version"), | |
99fb6e04 NTND |
2457 | 0, option_parse_index_version }, |
2458 | OPT_ULONG(0, "max-pack-size", &pack_size_limit, | |
4c688120 | 2459 | N_("maximum size of each output pack file")), |
99fb6e04 | 2460 | OPT_BOOL(0, "local", &local, |
4c688120 | 2461 | N_("ignore borrowed objects from alternate object store")), |
99fb6e04 | 2462 | OPT_BOOL(0, "incremental", &incremental, |
4c688120 | 2463 | N_("ignore packed objects")), |
99fb6e04 | 2464 | OPT_INTEGER(0, "window", &window, |
4c688120 | 2465 | N_("limit pack window by objects")), |
99fb6e04 | 2466 | OPT_ULONG(0, "window-memory", &window_memory_limit, |
4c688120 | 2467 | N_("limit pack window by memory in addition to object limit")), |
99fb6e04 | 2468 | OPT_INTEGER(0, "depth", &depth, |
4c688120 | 2469 | N_("maximum length of delta chain allowed in the resulting pack")), |
99fb6e04 | 2470 | OPT_BOOL(0, "reuse-delta", &reuse_delta, |
4c688120 | 2471 | N_("reuse existing deltas")), |
99fb6e04 | 2472 | OPT_BOOL(0, "reuse-object", &reuse_object, |
4c688120 | 2473 | N_("reuse existing objects")), |
99fb6e04 | 2474 | OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta, |
4c688120 | 2475 | N_("use OFS_DELTA objects")), |
99fb6e04 | 2476 | OPT_INTEGER(0, "threads", &delta_search_threads, |
4c688120 | 2477 | N_("use threads when searching for best delta matches")), |
99fb6e04 | 2478 | OPT_BOOL(0, "non-empty", &non_empty, |
4c688120 | 2479 | N_("do not create an empty pack output")), |
99fb6e04 | 2480 | OPT_BOOL(0, "revs", &use_internal_rev_list, |
4c688120 | 2481 | N_("read revision arguments from standard input")), |
99fb6e04 | 2482 | { OPTION_SET_INT, 0, "unpacked", &rev_list_unpacked, NULL, |
4c688120 | 2483 | N_("limit the objects to those that are not yet packed"), |
99fb6e04 NTND |
2484 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 }, |
2485 | { OPTION_SET_INT, 0, "all", &rev_list_all, NULL, | |
4c688120 | 2486 | N_("include objects reachable from any reference"), |
99fb6e04 NTND |
2487 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 }, |
2488 | { OPTION_SET_INT, 0, "reflog", &rev_list_reflog, NULL, | |
4c688120 | 2489 | N_("include objects referred by reflog entries"), |
99fb6e04 NTND |
2490 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 }, |
2491 | OPT_BOOL(0, "stdout", &pack_to_stdout, | |
4c688120 | 2492 | N_("output pack to stdout")), |
99fb6e04 | 2493 | OPT_BOOL(0, "include-tag", &include_tag, |
4c688120 | 2494 | N_("include tag objects that refer to objects to be packed")), |
99fb6e04 | 2495 | OPT_BOOL(0, "keep-unreachable", &keep_unreachable, |
4c688120 NTND |
2496 | N_("keep unreachable objects")), |
2497 | { OPTION_CALLBACK, 0, "unpack-unreachable", NULL, N_("time"), | |
2498 | N_("unpack unreachable objects newer than <time>"), | |
7e52f566 | 2499 | PARSE_OPT_OPTARG, option_parse_unpack_unreachable }, |
99fb6e04 | 2500 | OPT_BOOL(0, "thin", &thin, |
4c688120 | 2501 | N_("create thin packs")), |
99fb6e04 | 2502 | OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep, |
4c688120 | 2503 | N_("ignore packs that have companion .keep file")), |
99fb6e04 | 2504 | OPT_INTEGER(0, "compression", &pack_compression_level, |
4c688120 | 2505 | N_("pack compression level")), |
99fb6e04 | 2506 | OPT_SET_INT(0, "keep-true-parents", &grafts_replace_parents, |
4c688120 | 2507 | N_("do not hide commits by grafts"), 0), |
99fb6e04 NTND |
2508 | OPT_END(), |
2509 | }; | |
8d1d8f83 | 2510 | |
dae556bd CC |
2511 | read_replace_refs = 0; |
2512 | ||
ebcfb379 | 2513 | reset_pack_idx_option(&pack_idx_opts); |
ef90d6d4 | 2514 | git_config(git_pack_config, NULL); |
960ccca6 DH |
2515 | if (!pack_compression_seen && core_compression_seen) |
2516 | pack_compression_level = core_compression_level; | |
b5d97e6b JH |
2517 | |
2518 | progress = isatty(2); | |
99fb6e04 NTND |
2519 | argc = parse_options(argc, argv, prefix, pack_objects_options, |
2520 | pack_usage, 0); | |
b5d97e6b | 2521 | |
99fb6e04 NTND |
2522 | if (argc) { |
2523 | base_name = argv[0]; | |
2524 | argc--; | |
b5d97e6b | 2525 | } |
99fb6e04 NTND |
2526 | if (pack_to_stdout != !base_name || argc) |
2527 | usage_with_options(pack_usage, pack_objects_options); | |
b5d97e6b | 2528 | |
99fb6e04 NTND |
2529 | rp_av[rp_ac++] = "pack-objects"; |
2530 | if (thin) { | |
2531 | use_internal_rev_list = 1; | |
2532 | rp_av[rp_ac++] = "--objects-edge"; | |
2533 | } else | |
2534 | rp_av[rp_ac++] = "--objects"; | |
b5d97e6b | 2535 | |
99fb6e04 NTND |
2536 | if (rev_list_all) { |
2537 | use_internal_rev_list = 1; | |
2538 | rp_av[rp_ac++] = "--all"; | |
2539 | } | |
2540 | if (rev_list_reflog) { | |
2541 | use_internal_rev_list = 1; | |
2542 | rp_av[rp_ac++] = "--reflog"; | |
2543 | } | |
2544 | if (rev_list_unpacked) { | |
2545 | use_internal_rev_list = 1; | |
2546 | rp_av[rp_ac++] = "--unpacked"; | |
2547 | } | |
b5d97e6b | 2548 | |
99fb6e04 NTND |
2549 | if (!reuse_object) |
2550 | reuse_delta = 0; | |
2551 | if (pack_compression_level == -1) | |
2552 | pack_compression_level = Z_DEFAULT_COMPRESSION; | |
2553 | else if (pack_compression_level < 0 || pack_compression_level > Z_BEST_COMPRESSION) | |
2554 | die("bad pack compression level %d", pack_compression_level); | |
2555 | #ifdef NO_PTHREADS | |
2556 | if (delta_search_threads != 1) | |
2b34e486 | 2557 | warning("no threads support, ignoring --threads"); |
99fb6e04 | 2558 | #endif |
2b84b5a8 JS |
2559 | if (!pack_to_stdout && !pack_size_limit) |
2560 | pack_size_limit = pack_size_limit_cfg; | |
01c12a23 DH |
2561 | if (pack_to_stdout && pack_size_limit) |
2562 | die("--max-pack-size cannot be used to build a pack for transfer."); | |
07cf0f24 NP |
2563 | if (pack_size_limit && pack_size_limit < 1024*1024) { |
2564 | warning("minimum pack size limit is 1 MiB"); | |
2565 | pack_size_limit = 1024*1024; | |
2566 | } | |
01c12a23 | 2567 | |
8d1d8f83 JH |
2568 | if (!pack_to_stdout && thin) |
2569 | die("--thin cannot be used to build an indexable pack."); | |
b5d97e6b | 2570 | |
ca11b212 NP |
2571 | if (keep_unreachable && unpack_unreachable) |
2572 | die("--keep-unreachable and --unpack-unreachable are incompatible."); | |
2573 | ||
4f366275 NP |
2574 | if (progress && all_progress_implied) |
2575 | progress = 2; | |
2576 | ||
b5d97e6b JH |
2577 | prepare_packed_git(); |
2578 | ||
13aaf148 | 2579 | if (progress) |
dc6a0757 | 2580 | progress_state = start_progress("Counting objects", 0); |
b5d97e6b JH |
2581 | if (!use_internal_rev_list) |
2582 | read_object_list_from_stdin(); | |
8d1d8f83 JH |
2583 | else { |
2584 | rp_av[rp_ac] = NULL; | |
2585 | get_object_list(rp_ac, rp_av); | |
2586 | } | |
0ef95f72 | 2587 | cleanup_preferred_base(); |
f0a24aa5 SP |
2588 | if (include_tag && nr_result) |
2589 | for_each_ref(add_ref_tag, NULL); | |
4d4fcc54 | 2590 | stop_progress(&progress_state); |
96a02f8f | 2591 | |
f0b0af1b | 2592 | if (non_empty && !nr_result) |
1c4a2912 | 2593 | return 0; |
f7ae6a93 NP |
2594 | if (nr_result) |
2595 | prepare_pack(window, depth); | |
d01fb92f | 2596 | write_pack_file(); |
ab7cd7bb | 2597 | if (progress) |
6e1c2344 RJ |
2598 | fprintf(stderr, "Total %"PRIu32" (delta %"PRIu32")," |
2599 | " reused %"PRIu32" (delta %"PRIu32")\n", | |
67c08ce1 | 2600 | written, written_delta, reused, reused_delta); |
c323ac7d LT |
2601 | return 0; |
2602 | } |