]>
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" |
c38138cd | 11 | #include "csum-file.h" |
1b0c7174 | 12 | #include "tree-walk.h" |
b5d97e6b JH |
13 | #include "diff.h" |
14 | #include "revision.h" | |
15 | #include "list-objects.h" | |
96a02f8f | 16 | #include "progress.h" |
c323ac7d | 17 | |
8ecce684 NP |
18 | #ifdef THREADED_DELTA_SEARCH |
19 | #include <pthread.h> | |
20 | #endif | |
21 | ||
231f240b | 22 | static const char pack_usage[] = "\ |
a97773ce BD |
23 | git-pack-objects [{ -q | --progress | --all-progress }] \n\ |
24 | [--max-pack-size=N] [--local] [--incremental] \n\ | |
25 | [--window=N] [--window-memory=N] [--depth=N] \n\ | |
fa736f72 NP |
26 | [--no-reuse-delta] [--no-reuse-object] [--delta-base-offset] \n\ |
27 | [--non-empty] [--revs [--unpacked | --all]*] [--reflog] \n\ | |
28 | [--stdout | base-name] [<ref-list | <object-list]"; | |
c323ac7d | 29 | |
c323ac7d | 30 | struct object_entry { |
aa7e44bf | 31 | struct pack_idx_entry idx; |
3f9ac8d2 | 32 | unsigned long size; /* uncompressed size */ |
3f9ac8d2 | 33 | struct packed_git *in_pack; /* already in pack */ |
6777a59f | 34 | off_t in_pack_offset; |
a3fbf4df | 35 | struct object_entry *delta; /* delta base object */ |
82e5a82f | 36 | struct object_entry *delta_child; /* deltified objects who bases me */ |
15b4d577 JH |
37 | struct object_entry *delta_sibling; /* other deltified objects who |
38 | * uses the same base as me | |
39 | */ | |
074b2eea | 40 | void *delta_data; /* cached delta (uncompressed) */ |
a3fbf4df | 41 | unsigned long delta_size; /* delta data size (uncompressed) */ |
5a235b5e | 42 | unsigned int hash; /* name hint hash */ |
a3fbf4df NP |
43 | enum object_type type; |
44 | enum object_type in_pack_type; /* could be delta */ | |
45 | unsigned char in_pack_header_size; | |
46 | unsigned char preferred_base; /* we do not pack this, but is available | |
a74db82e | 47 | * to be used as the base object to delta |
a3fbf4df NP |
48 | * objects against. |
49 | */ | |
a74db82e | 50 | unsigned char no_try_delta; |
c323ac7d LT |
51 | }; |
52 | ||
3f9ac8d2 | 53 | /* |
82e5a82f | 54 | * Objects we are going to pack are collected in objects array (dynamically |
3f9ac8d2 JH |
55 | * expanded). nr_objects & nr_alloc controls this array. They are stored |
56 | * in the order we see -- typically rev-list --objects order that gives us | |
57 | * nice "minimum seek" order. | |
3f9ac8d2 | 58 | */ |
e4d58311 | 59 | static struct object_entry *objects; |
d01fb92f DH |
60 | static struct object_entry **written_list; |
61 | static uint32_t nr_objects, nr_alloc, nr_result, nr_written; | |
3f9ac8d2 | 62 | |
96f1e58f | 63 | static int non_empty; |
fa736f72 | 64 | static int no_reuse_delta, no_reuse_object; |
96f1e58f DR |
65 | static int local; |
66 | static int incremental; | |
be6b1914 | 67 | static int allow_ofs_delta; |
81a216a5 NP |
68 | static const char *pack_tmp_name, *idx_tmp_name; |
69 | static char tmpname[PATH_MAX]; | |
d01fb92f | 70 | static const char *base_name; |
024701f1 | 71 | static int progress = 1; |
4812a93a | 72 | static int window = 10; |
d01fb92f | 73 | static uint32_t pack_size_limit; |
618e613a | 74 | static int depth = 50; |
df6d6101 | 75 | static int pack_to_stdout; |
8d1d8f83 | 76 | static int num_preferred_base; |
96a02f8f | 77 | static struct progress progress_state; |
960ccca6 DH |
78 | static int pack_compression_level = Z_DEFAULT_COMPRESSION; |
79 | static int pack_compression_seen; | |
c323ac7d | 80 | |
074b2eea MK |
81 | static unsigned long delta_cache_size = 0; |
82 | static unsigned long max_delta_cache_size = 0; | |
e3dfddb3 | 83 | static unsigned long cache_max_small_delta_size = 1000; |
074b2eea | 84 | |
a97773ce BD |
85 | static unsigned long window_memory_limit = 0; |
86 | ||
3f9ac8d2 JH |
87 | /* |
88 | * The object names in objects array are hashed with this hashtable, | |
e4d58311 | 89 | * to help looking up the entry by object name. |
3f9ac8d2 JH |
90 | * This hashtable is built after all the objects are seen. |
91 | */ | |
96f1e58f DR |
92 | static int *object_ix; |
93 | static int object_ix_hashsz; | |
3f9ac8d2 JH |
94 | |
95 | /* | |
96 | * Pack index for existing packs give us easy access to the offsets into | |
97 | * corresponding pack file where each object's data starts, but the entries | |
98 | * do not store the size of the compressed representation (uncompressed | |
780e6e73 NP |
99 | * size is easily available by examining the pack entry header). It is |
100 | * also rather expensive to find the sha1 for an object given its offset. | |
101 | * | |
102 | * We build a hashtable of existing packs (pack_revindex), and keep reverse | |
103 | * index here -- pack index file is sorted by object name mapping to offset; | |
104 | * this pack_revindex[].revindex array is a list of offset/index_nr pairs | |
105 | * ordered by offset, so if you know the offset of an object, next offset | |
106 | * is where its packed representation ends and the index_nr can be used to | |
107 | * get the object sha1 from the main index. | |
3f9ac8d2 | 108 | */ |
780e6e73 | 109 | struct revindex_entry { |
6777a59f | 110 | off_t offset; |
780e6e73 NP |
111 | unsigned int nr; |
112 | }; | |
3f9ac8d2 JH |
113 | struct pack_revindex { |
114 | struct packed_git *p; | |
780e6e73 NP |
115 | struct revindex_entry *revindex; |
116 | }; | |
117 | static struct pack_revindex *pack_revindex; | |
96f1e58f | 118 | static int pack_revindex_hashsz; |
3f9ac8d2 JH |
119 | |
120 | /* | |
121 | * stats | |
122 | */ | |
7cadf491 SP |
123 | static uint32_t written, written_delta; |
124 | static uint32_t reused, reused_delta; | |
3f9ac8d2 JH |
125 | |
126 | static int pack_revindex_ix(struct packed_git *p) | |
127 | { | |
2b74cffa | 128 | unsigned long ui = (unsigned long)p; |
3f9ac8d2 JH |
129 | int i; |
130 | ||
131 | ui = ui ^ (ui >> 16); /* defeat structure alignment */ | |
132 | i = (int)(ui % pack_revindex_hashsz); | |
133 | while (pack_revindex[i].p) { | |
134 | if (pack_revindex[i].p == p) | |
135 | return i; | |
136 | if (++i == pack_revindex_hashsz) | |
137 | i = 0; | |
138 | } | |
139 | return -1 - i; | |
140 | } | |
141 | ||
142 | static void prepare_pack_ix(void) | |
143 | { | |
144 | int num; | |
145 | struct packed_git *p; | |
146 | for (num = 0, p = packed_git; p; p = p->next) | |
147 | num++; | |
148 | if (!num) | |
149 | return; | |
150 | pack_revindex_hashsz = num * 11; | |
151 | pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz); | |
152 | for (p = packed_git; p; p = p->next) { | |
153 | num = pack_revindex_ix(p); | |
154 | num = - 1 - num; | |
155 | pack_revindex[num].p = p; | |
156 | } | |
157 | /* revindex elements are lazily initialized */ | |
158 | } | |
159 | ||
160 | static int cmp_offset(const void *a_, const void *b_) | |
161 | { | |
780e6e73 NP |
162 | const struct revindex_entry *a = a_; |
163 | const struct revindex_entry *b = b_; | |
164 | return (a->offset < b->offset) ? -1 : (a->offset > b->offset) ? 1 : 0; | |
3f9ac8d2 JH |
165 | } |
166 | ||
167 | /* | |
168 | * Ordered list of offsets of objects in the pack. | |
169 | */ | |
170 | static void prepare_pack_revindex(struct pack_revindex *rix) | |
171 | { | |
172 | struct packed_git *p = rix->p; | |
57059091 | 173 | int num_ent = p->num_objects; |
3f9ac8d2 | 174 | int i; |
42873078 | 175 | const char *index = p->index_data; |
3f9ac8d2 | 176 | |
780e6e73 | 177 | rix->revindex = xmalloc(sizeof(*rix->revindex) * (num_ent + 1)); |
c553ca25 NP |
178 | index += 4 * 256; |
179 | ||
180 | if (p->index_version > 1) { | |
181 | const uint32_t *off_32 = | |
182 | (uint32_t *)(index + 8 + p->num_objects * (20 + 4)); | |
183 | const uint32_t *off_64 = off_32 + p->num_objects; | |
184 | for (i = 0; i < num_ent; i++) { | |
185 | uint32_t off = ntohl(*off_32++); | |
186 | if (!(off & 0x80000000)) { | |
187 | rix->revindex[i].offset = off; | |
188 | } else { | |
189 | rix->revindex[i].offset = | |
190 | ((uint64_t)ntohl(*off_64++)) << 32; | |
191 | rix->revindex[i].offset |= | |
192 | ntohl(*off_64++); | |
193 | } | |
194 | rix->revindex[i].nr = i; | |
195 | } | |
196 | } else { | |
197 | for (i = 0; i < num_ent; i++) { | |
198 | uint32_t hl = *((uint32_t *)(index + 24 * i)); | |
199 | rix->revindex[i].offset = ntohl(hl); | |
200 | rix->revindex[i].nr = i; | |
201 | } | |
3f9ac8d2 | 202 | } |
c553ca25 | 203 | |
3f9ac8d2 JH |
204 | /* This knows the pack format -- the 20-byte trailer |
205 | * follows immediately after the last object data. | |
206 | */ | |
780e6e73 NP |
207 | rix->revindex[num_ent].offset = p->pack_size - 20; |
208 | rix->revindex[num_ent].nr = -1; | |
209 | qsort(rix->revindex, num_ent, sizeof(*rix->revindex), cmp_offset); | |
3f9ac8d2 JH |
210 | } |
211 | ||
780e6e73 | 212 | static struct revindex_entry * find_packed_object(struct packed_git *p, |
6777a59f | 213 | off_t ofs) |
3f9ac8d2 JH |
214 | { |
215 | int num; | |
216 | int lo, hi; | |
217 | struct pack_revindex *rix; | |
780e6e73 | 218 | struct revindex_entry *revindex; |
3f9ac8d2 JH |
219 | num = pack_revindex_ix(p); |
220 | if (num < 0) | |
221 | die("internal error: pack revindex uninitialized"); | |
222 | rix = &pack_revindex[num]; | |
223 | if (!rix->revindex) | |
224 | prepare_pack_revindex(rix); | |
225 | revindex = rix->revindex; | |
226 | lo = 0; | |
57059091 | 227 | hi = p->num_objects + 1; |
3f9ac8d2 JH |
228 | do { |
229 | int mi = (lo + hi) / 2; | |
780e6e73 NP |
230 | if (revindex[mi].offset == ofs) { |
231 | return revindex + mi; | |
3f9ac8d2 | 232 | } |
780e6e73 | 233 | else if (ofs < revindex[mi].offset) |
3f9ac8d2 JH |
234 | hi = mi; |
235 | else | |
236 | lo = mi + 1; | |
237 | } while (lo < hi); | |
238 | die("internal error: pack revindex corrupt"); | |
239 | } | |
240 | ||
42873078 NP |
241 | static const unsigned char *find_packed_object_name(struct packed_git *p, |
242 | off_t ofs) | |
780e6e73 NP |
243 | { |
244 | struct revindex_entry *entry = find_packed_object(p, ofs); | |
d72308e0 | 245 | return nth_packed_object_sha1(p, entry->nr); |
780e6e73 NP |
246 | } |
247 | ||
c323ac7d LT |
248 | static void *delta_against(void *buf, unsigned long size, struct object_entry *entry) |
249 | { | |
250 | unsigned long othersize, delta_size; | |
21666f1a | 251 | enum object_type type; |
aa7e44bf | 252 | void *otherbuf = read_sha1_file(entry->delta->idx.sha1, &type, &othersize); |
c323ac7d LT |
253 | void *delta_buf; |
254 | ||
255 | if (!otherbuf) | |
aa7e44bf | 256 | die("unable to read %s", sha1_to_hex(entry->delta->idx.sha1)); |
8ee378a0 | 257 | delta_buf = diff_delta(otherbuf, othersize, |
dcde55bc | 258 | buf, size, &delta_size, 0); |
75c42d8c | 259 | if (!delta_buf || delta_size != entry->delta_size) |
a6080a0a | 260 | die("delta size changed"); |
c323ac7d LT |
261 | free(buf); |
262 | free(otherbuf); | |
263 | return delta_buf; | |
264 | } | |
265 | ||
a733cb60 LT |
266 | /* |
267 | * The per-object header is a pretty dense thing, which is | |
268 | * - first byte: low four bits are "size", then three bits of "type", | |
269 | * and the high bit is "size continues". | |
270 | * - each byte afterwards: low seven bits are size continuation, | |
271 | * with the high bit being "size continues" | |
272 | */ | |
273 | static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr) | |
274 | { | |
01247d87 | 275 | int n = 1; |
a733cb60 LT |
276 | unsigned char c; |
277 | ||
eb32d236 | 278 | if (type < OBJ_COMMIT || type > OBJ_REF_DELTA) |
a733cb60 LT |
279 | die("bad type %d", type); |
280 | ||
01247d87 LT |
281 | c = (type << 4) | (size & 15); |
282 | size >>= 4; | |
283 | while (size) { | |
a733cb60 | 284 | *hdr++ = c | 0x80; |
01247d87 LT |
285 | c = size & 0x7f; |
286 | size >>= 7; | |
287 | n++; | |
a733cb60 LT |
288 | } |
289 | *hdr = c; | |
290 | return n; | |
291 | } | |
292 | ||
780e6e73 NP |
293 | /* |
294 | * we are going to reuse the existing object data as is. make | |
295 | * sure it is not corrupt. | |
296 | */ | |
079afb18 SP |
297 | static int check_pack_inflate(struct packed_git *p, |
298 | struct pack_window **w_curs, | |
6777a59f SP |
299 | off_t offset, |
300 | off_t len, | |
079afb18 SP |
301 | unsigned long expect) |
302 | { | |
303 | z_stream stream; | |
304 | unsigned char fakebuf[4096], *in; | |
305 | int st; | |
306 | ||
307 | memset(&stream, 0, sizeof(stream)); | |
308 | inflateInit(&stream); | |
309 | do { | |
310 | in = use_pack(p, w_curs, offset, &stream.avail_in); | |
311 | stream.next_in = in; | |
312 | stream.next_out = fakebuf; | |
313 | stream.avail_out = sizeof(fakebuf); | |
314 | st = inflate(&stream, Z_FINISH); | |
315 | offset += stream.next_in - in; | |
316 | } while (st == Z_OK || st == Z_BUF_ERROR); | |
317 | inflateEnd(&stream); | |
318 | return (st == Z_STREAM_END && | |
319 | stream.total_out == expect && | |
320 | stream.total_in == len) ? 0 : -1; | |
321 | } | |
322 | ||
91ecbeca NP |
323 | static int check_pack_crc(struct packed_git *p, struct pack_window **w_curs, |
324 | off_t offset, off_t len, unsigned int nr) | |
325 | { | |
326 | const uint32_t *index_crc; | |
327 | uint32_t data_crc = crc32(0, Z_NULL, 0); | |
328 | ||
329 | do { | |
330 | unsigned int avail; | |
331 | void *data = use_pack(p, w_curs, offset, &avail); | |
332 | if (avail > len) | |
333 | avail = len; | |
334 | data_crc = crc32(data_crc, data, avail); | |
335 | offset += avail; | |
336 | len -= avail; | |
337 | } while (len); | |
338 | ||
339 | index_crc = p->index_data; | |
340 | index_crc += 2 + 256 + p->num_objects * (20/4) + nr; | |
341 | ||
342 | return data_crc != ntohl(*index_crc); | |
343 | } | |
344 | ||
079afb18 SP |
345 | static void copy_pack_data(struct sha1file *f, |
346 | struct packed_git *p, | |
347 | struct pack_window **w_curs, | |
6777a59f SP |
348 | off_t offset, |
349 | off_t len) | |
079afb18 SP |
350 | { |
351 | unsigned char *in; | |
352 | unsigned int avail; | |
353 | ||
354 | while (len) { | |
355 | in = use_pack(p, w_curs, offset, &avail); | |
356 | if (avail > len) | |
6777a59f | 357 | avail = (unsigned int)len; |
079afb18 SP |
358 | sha1write(f, in, avail); |
359 | offset += avail; | |
360 | len -= avail; | |
361 | } | |
362 | } | |
363 | ||
d7dd0223 | 364 | static unsigned long write_object(struct sha1file *f, |
17b08f2c DH |
365 | struct object_entry *entry, |
366 | off_t write_offset) | |
c323ac7d LT |
367 | { |
368 | unsigned long size; | |
21666f1a | 369 | enum object_type type; |
3f9ac8d2 | 370 | void *buf; |
a733cb60 | 371 | unsigned char header[10]; |
17b08f2c | 372 | unsigned char dheader[10]; |
6777a59f SP |
373 | unsigned hdrlen; |
374 | off_t datalen; | |
a733cb60 | 375 | enum object_type obj_type; |
ab7cd7bb | 376 | int to_reuse = 0; |
17b08f2c DH |
377 | /* write limit if limited packsize and not first object */ |
378 | unsigned long limit = pack_size_limit && nr_written ? | |
379 | pack_size_limit - write_offset : 0; | |
380 | /* no if no delta */ | |
381 | int usable_delta = !entry->delta ? 0 : | |
382 | /* yes if unlimited packfile */ | |
383 | !pack_size_limit ? 1 : | |
384 | /* no if base written to previous pack */ | |
aa7e44bf | 385 | entry->delta->idx.offset == (off_t)-1 ? 0 : |
17b08f2c DH |
386 | /* otherwise double-check written to this |
387 | * pack, like we do below | |
388 | */ | |
aa7e44bf | 389 | entry->delta->idx.offset ? 1 : 0; |
c323ac7d | 390 | |
78d1e84f NP |
391 | if (!pack_to_stdout) |
392 | crc32_begin(f); | |
393 | ||
a733cb60 | 394 | obj_type = entry->type; |
fa736f72 NP |
395 | if (no_reuse_object) |
396 | to_reuse = 0; /* explicit */ | |
397 | else if (!entry->in_pack) | |
ab7cd7bb | 398 | to_reuse = 0; /* can't reuse what we don't have */ |
780e6e73 | 399 | else if (obj_type == OBJ_REF_DELTA || obj_type == OBJ_OFS_DELTA) |
17b08f2c DH |
400 | /* check_object() decided it for us ... */ |
401 | to_reuse = usable_delta; | |
402 | /* ... but pack split may override that */ | |
ab7cd7bb JH |
403 | else if (obj_type != entry->in_pack_type) |
404 | to_reuse = 0; /* pack has delta which is unusable */ | |
405 | else if (entry->delta) | |
406 | to_reuse = 0; /* we want to pack afresh */ | |
407 | else | |
408 | to_reuse = 1; /* we have it in-pack undeltified, | |
409 | * and we do not need to deltify it. | |
410 | */ | |
411 | ||
df6d6101 | 412 | if (!to_reuse) { |
17b08f2c DH |
413 | z_stream stream; |
414 | unsigned long maxsize; | |
415 | void *out; | |
5476a8ad | 416 | if (!usable_delta) { |
aa7e44bf | 417 | buf = read_sha1_file(entry->idx.sha1, &obj_type, &size); |
5476a8ad | 418 | if (!buf) |
aa7e44bf | 419 | die("unable to read %s", sha1_to_hex(entry->idx.sha1)); |
5476a8ad | 420 | } else if (entry->delta_data) { |
3f9ac8d2 | 421 | size = entry->delta_size; |
5476a8ad NP |
422 | buf = entry->delta_data; |
423 | entry->delta_data = NULL; | |
aa7e44bf | 424 | obj_type = (allow_ofs_delta && entry->delta->idx.offset) ? |
be6b1914 | 425 | OBJ_OFS_DELTA : OBJ_REF_DELTA; |
17b08f2c | 426 | } else { |
aa7e44bf | 427 | buf = read_sha1_file(entry->idx.sha1, &type, &size); |
074b2eea | 428 | if (!buf) |
aa7e44bf | 429 | die("unable to read %s", sha1_to_hex(entry->idx.sha1)); |
5476a8ad NP |
430 | buf = delta_against(buf, size, entry); |
431 | size = entry->delta_size; | |
aa7e44bf | 432 | obj_type = (allow_ofs_delta && entry->delta->idx.offset) ? |
5476a8ad | 433 | OBJ_OFS_DELTA : OBJ_REF_DELTA; |
3f9ac8d2 | 434 | } |
17b08f2c DH |
435 | /* compress the data to store and put compressed length in datalen */ |
436 | memset(&stream, 0, sizeof(stream)); | |
437 | deflateInit(&stream, pack_compression_level); | |
438 | maxsize = deflateBound(&stream, size); | |
439 | out = xmalloc(maxsize); | |
440 | /* Compress it */ | |
441 | stream.next_in = buf; | |
442 | stream.avail_in = size; | |
443 | stream.next_out = out; | |
444 | stream.avail_out = maxsize; | |
445 | while (deflate(&stream, Z_FINISH) == Z_OK) | |
446 | /* nothing */; | |
447 | deflateEnd(&stream); | |
448 | datalen = stream.total_out; | |
449 | deflateEnd(&stream); | |
3f9ac8d2 JH |
450 | /* |
451 | * The object header is a byte of 'type' followed by zero or | |
be6b1914 | 452 | * more bytes of length. |
3f9ac8d2 JH |
453 | */ |
454 | hdrlen = encode_header(obj_type, size, header); | |
3f9ac8d2 | 455 | |
be6b1914 NP |
456 | if (obj_type == OBJ_OFS_DELTA) { |
457 | /* | |
458 | * Deltas with relative base contain an additional | |
459 | * encoding of the relative offset for the delta | |
460 | * base from this object's position in the pack. | |
461 | */ | |
aa7e44bf | 462 | off_t ofs = entry->idx.offset - entry->delta->idx.offset; |
17b08f2c DH |
463 | unsigned pos = sizeof(dheader) - 1; |
464 | dheader[pos] = ofs & 127; | |
be6b1914 | 465 | while (ofs >>= 7) |
17b08f2c DH |
466 | dheader[--pos] = 128 | (--ofs & 127); |
467 | if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) { | |
468 | free(out); | |
469 | free(buf); | |
470 | return 0; | |
471 | } | |
472 | sha1write(f, header, hdrlen); | |
473 | sha1write(f, dheader + pos, sizeof(dheader) - pos); | |
474 | hdrlen += sizeof(dheader) - pos; | |
be6b1914 NP |
475 | } else if (obj_type == OBJ_REF_DELTA) { |
476 | /* | |
477 | * Deltas with a base reference contain | |
478 | * an additional 20 bytes for the base sha1. | |
479 | */ | |
17b08f2c DH |
480 | if (limit && hdrlen + 20 + datalen + 20 >= limit) { |
481 | free(out); | |
482 | free(buf); | |
483 | return 0; | |
484 | } | |
485 | sha1write(f, header, hdrlen); | |
aa7e44bf | 486 | sha1write(f, entry->delta->idx.sha1, 20); |
3f9ac8d2 | 487 | hdrlen += 20; |
17b08f2c DH |
488 | } else { |
489 | if (limit && hdrlen + datalen + 20 >= limit) { | |
490 | free(out); | |
491 | free(buf); | |
492 | return 0; | |
493 | } | |
494 | sha1write(f, header, hdrlen); | |
3f9ac8d2 | 495 | } |
17b08f2c DH |
496 | sha1write(f, out, datalen); |
497 | free(out); | |
3f9ac8d2 | 498 | free(buf); |
c323ac7d | 499 | } |
3f9ac8d2 JH |
500 | else { |
501 | struct packed_git *p = entry->in_pack; | |
03e79c88 | 502 | struct pack_window *w_curs = NULL; |
91ecbeca | 503 | struct revindex_entry *revidx; |
6777a59f | 504 | off_t offset; |
3f9ac8d2 | 505 | |
780e6e73 | 506 | if (entry->delta) { |
aa7e44bf | 507 | obj_type = (allow_ofs_delta && entry->delta->idx.offset) ? |
780e6e73 NP |
508 | OBJ_OFS_DELTA : OBJ_REF_DELTA; |
509 | reused_delta++; | |
510 | } | |
511 | hdrlen = encode_header(obj_type, entry->size, header); | |
91ecbeca NP |
512 | offset = entry->in_pack_offset; |
513 | revidx = find_packed_object(p, offset); | |
514 | datalen = revidx[1].offset - offset; | |
515 | if (!pack_to_stdout && p->index_version > 1 && | |
516 | check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) | |
aa7e44bf | 517 | die("bad packed object CRC for %s", sha1_to_hex(entry->idx.sha1)); |
91ecbeca NP |
518 | offset += entry->in_pack_header_size; |
519 | datalen -= entry->in_pack_header_size; | |
17b08f2c | 520 | if (obj_type == OBJ_OFS_DELTA) { |
aa7e44bf | 521 | off_t ofs = entry->idx.offset - entry->delta->idx.offset; |
17b08f2c DH |
522 | unsigned pos = sizeof(dheader) - 1; |
523 | dheader[pos] = ofs & 127; | |
524 | while (ofs >>= 7) | |
525 | dheader[--pos] = 128 | (--ofs & 127); | |
526 | if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) | |
527 | return 0; | |
528 | sha1write(f, header, hdrlen); | |
529 | sha1write(f, dheader + pos, sizeof(dheader) - pos); | |
530 | hdrlen += sizeof(dheader) - pos; | |
531 | } else if (obj_type == OBJ_REF_DELTA) { | |
532 | if (limit && hdrlen + 20 + datalen + 20 >= limit) | |
533 | return 0; | |
534 | sha1write(f, header, hdrlen); | |
aa7e44bf | 535 | sha1write(f, entry->delta->idx.sha1, 20); |
17b08f2c DH |
536 | hdrlen += 20; |
537 | } else { | |
538 | if (limit && hdrlen + datalen + 20 >= limit) | |
539 | return 0; | |
540 | sha1write(f, header, hdrlen); | |
541 | } | |
542 | ||
91ecbeca NP |
543 | if (!pack_to_stdout && p->index_version == 1 && |
544 | check_pack_inflate(p, &w_curs, offset, datalen, entry->size)) | |
aa7e44bf | 545 | die("corrupt packed object for %s", sha1_to_hex(entry->idx.sha1)); |
079afb18 | 546 | copy_pack_data(f, p, &w_curs, offset, datalen); |
03e79c88 | 547 | unuse_pack(&w_curs); |
3f9ac8d2 | 548 | reused++; |
a733cb60 | 549 | } |
17b08f2c | 550 | if (usable_delta) |
ab7cd7bb | 551 | written_delta++; |
3f9ac8d2 | 552 | written++; |
78d1e84f | 553 | if (!pack_to_stdout) |
aa7e44bf | 554 | entry->idx.crc32 = crc32_end(f); |
c323ac7d LT |
555 | return hdrlen + datalen; |
556 | } | |
557 | ||
6777a59f | 558 | static off_t write_one(struct sha1file *f, |
9d5ab962 | 559 | struct object_entry *e, |
6777a59f | 560 | off_t offset) |
9d5ab962 | 561 | { |
d7dd0223 NP |
562 | unsigned long size; |
563 | ||
564 | /* offset is non zero if object is written already. */ | |
aa7e44bf | 565 | if (e->idx.offset || e->preferred_base) |
9d5ab962 | 566 | return offset; |
d7dd0223 NP |
567 | |
568 | /* if we are deltified, write out base object first. */ | |
17b08f2c | 569 | if (e->delta) { |
9d5ab962 | 570 | offset = write_one(f, e->delta, offset); |
17b08f2c DH |
571 | if (!offset) |
572 | return 0; | |
573 | } | |
d7dd0223 | 574 | |
aa7e44bf | 575 | e->idx.offset = offset; |
17b08f2c DH |
576 | size = write_object(f, e, offset); |
577 | if (!size) { | |
aa7e44bf | 578 | e->idx.offset = 0; |
17b08f2c DH |
579 | return 0; |
580 | } | |
ebe27b13 | 581 | written_list[nr_written++] = e; |
d7dd0223 NP |
582 | |
583 | /* make sure off_t is sufficiently large not to wrap */ | |
584 | if (offset > offset + size) | |
585 | die("pack too large for current definition of off_t"); | |
586 | return offset + size; | |
9d5ab962 JH |
587 | } |
588 | ||
3082acfa AR |
589 | static int open_object_dir_tmp(const char *path) |
590 | { | |
591 | snprintf(tmpname, sizeof(tmpname), "%s/%s", get_object_directory(), path); | |
7647b17f | 592 | return xmkstemp(tmpname); |
3082acfa AR |
593 | } |
594 | ||
aa7e44bf | 595 | /* forward declaration for write_pack_file */ |
d01fb92f DH |
596 | static int adjust_perm(const char *path, mode_t mode); |
597 | ||
598 | static void write_pack_file(void) | |
c323ac7d | 599 | { |
ebe27b13 | 600 | uint32_t i = 0, j; |
d22b9290 | 601 | struct sha1file *f; |
ebe27b13 | 602 | off_t offset, offset_one, last_obj_offset = 0; |
a733cb60 | 603 | struct pack_header hdr; |
ebe27b13 DH |
604 | int do_progress = progress >> pack_to_stdout; |
605 | uint32_t nr_remaining = nr_result; | |
81a216a5 | 606 | |
13aaf148 NP |
607 | if (do_progress) |
608 | start_progress(&progress_state, "Writing %u objects...", "", nr_result); | |
ebe27b13 | 609 | written_list = xmalloc(nr_objects * sizeof(struct object_entry *)); |
183bdb2c | 610 | |
ebe27b13 | 611 | do { |
aa7e44bf GB |
612 | unsigned char sha1[20]; |
613 | ||
ebe27b13 DH |
614 | if (pack_to_stdout) { |
615 | f = sha1fd(1, "<stdout>"); | |
616 | } else { | |
617 | int fd = open_object_dir_tmp("tmp_pack_XXXXXX"); | |
ebe27b13 DH |
618 | pack_tmp_name = xstrdup(tmpname); |
619 | f = sha1fd(fd, pack_tmp_name); | |
620 | } | |
621 | ||
622 | hdr.hdr_signature = htonl(PACK_SIGNATURE); | |
623 | hdr.hdr_version = htonl(PACK_VERSION); | |
624 | hdr.hdr_entries = htonl(nr_remaining); | |
625 | sha1write(f, &hdr, sizeof(hdr)); | |
626 | offset = sizeof(hdr); | |
627 | nr_written = 0; | |
628 | for (; i < nr_objects; i++) { | |
629 | last_obj_offset = offset; | |
630 | offset_one = write_one(f, objects + i, offset); | |
631 | if (!offset_one) | |
632 | break; | |
633 | offset = offset_one; | |
634 | if (do_progress) | |
635 | display_progress(&progress_state, written); | |
636 | } | |
c553ca25 | 637 | |
ebe27b13 DH |
638 | /* |
639 | * Did we write the wrong # entries in the header? | |
640 | * If so, rewrite it like in fast-import | |
641 | */ | |
642 | if (pack_to_stdout || nr_written == nr_remaining) { | |
aa7e44bf | 643 | sha1close(f, sha1, 1); |
ebe27b13 | 644 | } else { |
aa7e44bf GB |
645 | sha1close(f, sha1, 0); |
646 | fixup_pack_header_footer(f->fd, sha1, pack_tmp_name, nr_written); | |
ebe27b13 DH |
647 | close(f->fd); |
648 | } | |
649 | ||
650 | if (!pack_to_stdout) { | |
d01fb92f DH |
651 | mode_t mode = umask(0); |
652 | ||
653 | umask(mode); | |
654 | mode = 0444 & ~mode; | |
655 | ||
aa7e44bf GB |
656 | idx_tmp_name = write_idx_file(NULL, |
657 | (struct pack_idx_entry **) written_list, nr_written, sha1); | |
d01fb92f | 658 | snprintf(tmpname, sizeof(tmpname), "%s-%s.pack", |
aa7e44bf | 659 | base_name, sha1_to_hex(sha1)); |
d01fb92f DH |
660 | if (adjust_perm(pack_tmp_name, mode)) |
661 | die("unable to make temporary pack file readable: %s", | |
662 | strerror(errno)); | |
663 | if (rename(pack_tmp_name, tmpname)) | |
664 | die("unable to rename temporary pack file: %s", | |
665 | strerror(errno)); | |
666 | snprintf(tmpname, sizeof(tmpname), "%s-%s.idx", | |
aa7e44bf | 667 | base_name, sha1_to_hex(sha1)); |
d01fb92f DH |
668 | if (adjust_perm(idx_tmp_name, mode)) |
669 | die("unable to make temporary index file readable: %s", | |
670 | strerror(errno)); | |
671 | if (rename(idx_tmp_name, tmpname)) | |
672 | die("unable to rename temporary index file: %s", | |
673 | strerror(errno)); | |
aa7e44bf | 674 | puts(sha1_to_hex(sha1)); |
ebe27b13 DH |
675 | } |
676 | ||
677 | /* mark written objects as written to previous pack */ | |
678 | for (j = 0; j < nr_written; j++) { | |
aa7e44bf | 679 | written_list[j]->idx.offset = (off_t)-1; |
ebe27b13 DH |
680 | } |
681 | nr_remaining -= nr_written; | |
682 | } while (nr_remaining && i < nr_objects); | |
683 | ||
684 | free(written_list); | |
183bdb2c | 685 | if (do_progress) |
96a02f8f | 686 | stop_progress(&progress_state); |
67c08ce1 | 687 | if (written != nr_result) |
7cadf491 | 688 | die("wrote %u objects while expecting %u", written, nr_result); |
01c12a23 DH |
689 | /* |
690 | * We have scanned through [0 ... i). Since we have written | |
691 | * the correct number of objects, the remaining [i ... nr_objects) | |
692 | * items must be either already written (due to out-of-order delta base) | |
693 | * or a preferred base. Count those which are neither and complain if any. | |
694 | */ | |
ebe27b13 DH |
695 | for (j = 0; i < nr_objects; i++) { |
696 | struct object_entry *e = objects + i; | |
aa7e44bf | 697 | j += !e->idx.offset && !e->preferred_base; |
d01fb92f | 698 | } |
ebe27b13 DH |
699 | if (j) |
700 | die("wrote %u objects as expected but %u unwritten", written, j); | |
c323ac7d LT |
701 | } |
702 | ||
7a979d99 JH |
703 | static int locate_object_entry_hash(const unsigned char *sha1) |
704 | { | |
705 | int i; | |
706 | unsigned int ui; | |
707 | memcpy(&ui, sha1, sizeof(unsigned int)); | |
708 | i = ui % object_ix_hashsz; | |
709 | while (0 < object_ix[i]) { | |
aa7e44bf | 710 | if (!hashcmp(sha1, objects[object_ix[i] - 1].idx.sha1)) |
7a979d99 JH |
711 | return i; |
712 | if (++i == object_ix_hashsz) | |
713 | i = 0; | |
714 | } | |
715 | return -1 - i; | |
716 | } | |
717 | ||
718 | static struct object_entry *locate_object_entry(const unsigned char *sha1) | |
719 | { | |
720 | int i; | |
721 | ||
722 | if (!object_ix_hashsz) | |
723 | return NULL; | |
724 | ||
725 | i = locate_object_entry_hash(sha1); | |
726 | if (0 <= i) | |
727 | return &objects[object_ix[i]-1]; | |
728 | return NULL; | |
729 | } | |
730 | ||
731 | static void rehash_objects(void) | |
c323ac7d | 732 | { |
7cadf491 | 733 | uint32_t i; |
7a979d99 JH |
734 | struct object_entry *oe; |
735 | ||
736 | object_ix_hashsz = nr_objects * 3; | |
737 | if (object_ix_hashsz < 1024) | |
738 | object_ix_hashsz = 1024; | |
739 | object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz); | |
5379a5c5 | 740 | memset(object_ix, 0, sizeof(int) * object_ix_hashsz); |
7a979d99 | 741 | for (i = 0, oe = objects; i < nr_objects; i++, oe++) { |
aa7e44bf | 742 | int ix = locate_object_entry_hash(oe->idx.sha1); |
7a979d99 JH |
743 | if (0 <= ix) |
744 | continue; | |
745 | ix = -1 - ix; | |
746 | object_ix[ix] = i + 1; | |
747 | } | |
748 | } | |
749 | ||
ce0bd642 | 750 | static unsigned name_hash(const char *name) |
1d6b38cc | 751 | { |
ce0bd642 LT |
752 | unsigned char c; |
753 | unsigned hash = 0; | |
754 | ||
bc32fed5 JH |
755 | if (!name) |
756 | return 0; | |
757 | ||
eeef7135 | 758 | /* |
ce0bd642 LT |
759 | * This effectively just creates a sortable number from the |
760 | * last sixteen non-whitespace characters. Last characters | |
761 | * count "most", so things that end in ".c" sort together. | |
eeef7135 | 762 | */ |
ce0bd642 LT |
763 | while ((c = *name++) != 0) { |
764 | if (isspace(c)) | |
765 | continue; | |
766 | hash = (hash >> 2) + (c << 24); | |
767 | } | |
1d6b38cc JH |
768 | return hash; |
769 | } | |
770 | ||
a74db82e JH |
771 | static void setup_delta_attr_check(struct git_attr_check *check) |
772 | { | |
773 | static struct git_attr *attr_delta; | |
774 | ||
775 | if (!attr_delta) | |
776 | attr_delta = git_attr("delta", 5); | |
777 | ||
778 | check[0].attr = attr_delta; | |
779 | } | |
780 | ||
781 | static int no_try_delta(const char *path) | |
782 | { | |
783 | struct git_attr_check check[1]; | |
784 | ||
785 | setup_delta_attr_check(check); | |
786 | if (git_checkattr(path, ARRAY_SIZE(check), check)) | |
787 | return 0; | |
788 | if (ATTR_FALSE(check->value)) | |
789 | return 1; | |
790 | return 0; | |
791 | } | |
792 | ||
5c49c116 | 793 | static int add_object_entry(const unsigned char *sha1, enum object_type type, |
bc32fed5 | 794 | const char *name, int exclude) |
c323ac7d | 795 | { |
c323ac7d | 796 | struct object_entry *entry; |
29b734e4 | 797 | struct packed_git *p, *found_pack = NULL; |
6777a59f | 798 | off_t found_offset = 0; |
29b734e4 | 799 | int ix; |
bc32fed5 | 800 | unsigned hash = name_hash(name); |
29b734e4 NP |
801 | |
802 | ix = nr_objects ? locate_object_entry_hash(sha1) : -1; | |
803 | if (ix >= 0) { | |
804 | if (exclude) { | |
805 | entry = objects + object_ix[ix] - 1; | |
81a216a5 NP |
806 | if (!entry->preferred_base) |
807 | nr_result--; | |
29b734e4 NP |
808 | entry->preferred_base = 1; |
809 | } | |
810 | return 0; | |
811 | } | |
c323ac7d | 812 | |
5c49c116 NP |
813 | for (p = packed_git; p; p = p->next) { |
814 | off_t offset = find_pack_entry_one(sha1, p); | |
815 | if (offset) { | |
816 | if (!found_pack) { | |
817 | found_offset = offset; | |
818 | found_pack = p; | |
64560374 | 819 | } |
5c49c116 NP |
820 | if (exclude) |
821 | break; | |
822 | if (incremental) | |
823 | return 0; | |
824 | if (local && !p->pack_local) | |
825 | return 0; | |
64560374 LT |
826 | } |
827 | } | |
eb019375 | 828 | |
29b734e4 NP |
829 | if (nr_objects >= nr_alloc) { |
830 | nr_alloc = (nr_alloc + 1024) * 3 / 2; | |
7cadf491 | 831 | objects = xrealloc(objects, nr_alloc * sizeof(*entry)); |
c323ac7d | 832 | } |
29b734e4 NP |
833 | |
834 | entry = objects + nr_objects++; | |
c323ac7d | 835 | memset(entry, 0, sizeof(*entry)); |
aa7e44bf | 836 | hashcpy(entry->idx.sha1, sha1); |
27225f2e | 837 | entry->hash = hash; |
5c49c116 NP |
838 | if (type) |
839 | entry->type = type; | |
29b734e4 NP |
840 | if (exclude) |
841 | entry->preferred_base = 1; | |
81a216a5 NP |
842 | else |
843 | nr_result++; | |
29b734e4 NP |
844 | if (found_pack) { |
845 | entry->in_pack = found_pack; | |
846 | entry->in_pack_offset = found_offset; | |
847 | } | |
7a979d99 JH |
848 | |
849 | if (object_ix_hashsz * 3 <= nr_objects * 4) | |
850 | rehash_objects(); | |
29b734e4 NP |
851 | else |
852 | object_ix[-1 - ix] = nr_objects; | |
7a979d99 | 853 | |
96a02f8f NP |
854 | if (progress) |
855 | display_progress(&progress_state, nr_objects); | |
29b734e4 | 856 | |
a74db82e JH |
857 | if (name && no_try_delta(name)) |
858 | entry->no_try_delta = 1; | |
859 | ||
29b734e4 | 860 | return 1; |
c323ac7d LT |
861 | } |
862 | ||
5379a5c5 JH |
863 | struct pbase_tree_cache { |
864 | unsigned char sha1[20]; | |
865 | int ref; | |
866 | int temporary; | |
867 | void *tree_data; | |
868 | unsigned long tree_size; | |
869 | }; | |
870 | ||
871 | static struct pbase_tree_cache *(pbase_tree_cache[256]); | |
872 | static int pbase_tree_cache_ix(const unsigned char *sha1) | |
873 | { | |
874 | return sha1[0] % ARRAY_SIZE(pbase_tree_cache); | |
875 | } | |
876 | static int pbase_tree_cache_ix_incr(int ix) | |
877 | { | |
878 | return (ix+1) % ARRAY_SIZE(pbase_tree_cache); | |
879 | } | |
880 | ||
881 | static struct pbase_tree { | |
882 | struct pbase_tree *next; | |
883 | /* This is a phony "cache" entry; we are not | |
884 | * going to evict it nor find it through _get() | |
885 | * mechanism -- this is for the toplevel node that | |
886 | * would almost always change with any commit. | |
887 | */ | |
888 | struct pbase_tree_cache pcache; | |
889 | } *pbase_tree; | |
890 | ||
891 | static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1) | |
892 | { | |
893 | struct pbase_tree_cache *ent, *nent; | |
894 | void *data; | |
895 | unsigned long size; | |
21666f1a | 896 | enum object_type type; |
5379a5c5 JH |
897 | int neigh; |
898 | int my_ix = pbase_tree_cache_ix(sha1); | |
899 | int available_ix = -1; | |
900 | ||
901 | /* pbase-tree-cache acts as a limited hashtable. | |
902 | * your object will be found at your index or within a few | |
903 | * slots after that slot if it is cached. | |
904 | */ | |
905 | for (neigh = 0; neigh < 8; neigh++) { | |
906 | ent = pbase_tree_cache[my_ix]; | |
a89fccd2 | 907 | if (ent && !hashcmp(ent->sha1, sha1)) { |
5379a5c5 JH |
908 | ent->ref++; |
909 | return ent; | |
910 | } | |
911 | else if (((available_ix < 0) && (!ent || !ent->ref)) || | |
912 | ((0 <= available_ix) && | |
913 | (!ent && pbase_tree_cache[available_ix]))) | |
914 | available_ix = my_ix; | |
915 | if (!ent) | |
916 | break; | |
917 | my_ix = pbase_tree_cache_ix_incr(my_ix); | |
918 | } | |
919 | ||
920 | /* Did not find one. Either we got a bogus request or | |
921 | * we need to read and perhaps cache. | |
922 | */ | |
21666f1a | 923 | data = read_sha1_file(sha1, &type, &size); |
5379a5c5 JH |
924 | if (!data) |
925 | return NULL; | |
21666f1a | 926 | if (type != OBJ_TREE) { |
5379a5c5 JH |
927 | free(data); |
928 | return NULL; | |
929 | } | |
930 | ||
931 | /* We need to either cache or return a throwaway copy */ | |
932 | ||
933 | if (available_ix < 0) | |
934 | ent = NULL; | |
935 | else { | |
936 | ent = pbase_tree_cache[available_ix]; | |
937 | my_ix = available_ix; | |
938 | } | |
939 | ||
940 | if (!ent) { | |
941 | nent = xmalloc(sizeof(*nent)); | |
942 | nent->temporary = (available_ix < 0); | |
943 | } | |
944 | else { | |
945 | /* evict and reuse */ | |
946 | free(ent->tree_data); | |
947 | nent = ent; | |
948 | } | |
e702496e | 949 | hashcpy(nent->sha1, sha1); |
5379a5c5 JH |
950 | nent->tree_data = data; |
951 | nent->tree_size = size; | |
952 | nent->ref = 1; | |
953 | if (!nent->temporary) | |
954 | pbase_tree_cache[my_ix] = nent; | |
955 | return nent; | |
956 | } | |
957 | ||
958 | static void pbase_tree_put(struct pbase_tree_cache *cache) | |
959 | { | |
960 | if (!cache->temporary) { | |
961 | cache->ref--; | |
962 | return; | |
963 | } | |
964 | free(cache->tree_data); | |
965 | free(cache); | |
966 | } | |
967 | ||
968 | static int name_cmp_len(const char *name) | |
969 | { | |
970 | int i; | |
971 | for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++) | |
972 | ; | |
973 | return i; | |
974 | } | |
975 | ||
976 | static void add_pbase_object(struct tree_desc *tree, | |
5379a5c5 | 977 | const char *name, |
ce0bd642 LT |
978 | int cmplen, |
979 | const char *fullname) | |
3f9ac8d2 | 980 | { |
4c068a98 | 981 | struct name_entry entry; |
8a5a8d6c | 982 | int cmp; |
4c068a98 LT |
983 | |
984 | while (tree_entry(tree,&entry)) { | |
1211be6b LT |
985 | if (S_ISGITLINK(entry.mode)) |
986 | continue; | |
8a5a8d6c NP |
987 | cmp = tree_entry_len(entry.path, entry.sha1) != cmplen ? 1 : |
988 | memcmp(name, entry.path, cmplen); | |
989 | if (cmp > 0) | |
7a979d99 | 990 | continue; |
8a5a8d6c NP |
991 | if (cmp < 0) |
992 | return; | |
5379a5c5 | 993 | if (name[cmplen] != '/') { |
5c49c116 NP |
994 | add_object_entry(entry.sha1, |
995 | S_ISDIR(entry.mode) ? OBJ_TREE : OBJ_BLOB, | |
bc32fed5 | 996 | fullname, 1); |
5379a5c5 JH |
997 | return; |
998 | } | |
8a5a8d6c | 999 | if (S_ISDIR(entry.mode)) { |
7a979d99 | 1000 | struct tree_desc sub; |
5379a5c5 JH |
1001 | struct pbase_tree_cache *tree; |
1002 | const char *down = name+cmplen+1; | |
1003 | int downlen = name_cmp_len(down); | |
1004 | ||
4c068a98 | 1005 | tree = pbase_tree_get(entry.sha1); |
5379a5c5 JH |
1006 | if (!tree) |
1007 | return; | |
6fda5e51 | 1008 | init_tree_desc(&sub, tree->tree_data, tree->tree_size); |
5379a5c5 | 1009 | |
ce0bd642 | 1010 | add_pbase_object(&sub, down, downlen, fullname); |
5379a5c5 JH |
1011 | pbase_tree_put(tree); |
1012 | } | |
1013 | } | |
1014 | } | |
1d6b38cc | 1015 | |
5379a5c5 JH |
1016 | static unsigned *done_pbase_paths; |
1017 | static int done_pbase_paths_num; | |
1018 | static int done_pbase_paths_alloc; | |
1019 | static int done_pbase_path_pos(unsigned hash) | |
1020 | { | |
1021 | int lo = 0; | |
1022 | int hi = done_pbase_paths_num; | |
1023 | while (lo < hi) { | |
1024 | int mi = (hi + lo) / 2; | |
1025 | if (done_pbase_paths[mi] == hash) | |
1026 | return mi; | |
1027 | if (done_pbase_paths[mi] < hash) | |
1028 | hi = mi; | |
1029 | else | |
1030 | lo = mi + 1; | |
1031 | } | |
1032 | return -lo-1; | |
1033 | } | |
1034 | ||
1035 | static int check_pbase_path(unsigned hash) | |
1036 | { | |
1037 | int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash); | |
1038 | if (0 <= pos) | |
1039 | return 1; | |
1040 | pos = -pos - 1; | |
1041 | if (done_pbase_paths_alloc <= done_pbase_paths_num) { | |
1042 | done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc); | |
1043 | done_pbase_paths = xrealloc(done_pbase_paths, | |
1044 | done_pbase_paths_alloc * | |
1045 | sizeof(unsigned)); | |
1046 | } | |
1047 | done_pbase_paths_num++; | |
1048 | if (pos < done_pbase_paths_num) | |
1049 | memmove(done_pbase_paths + pos + 1, | |
1050 | done_pbase_paths + pos, | |
1051 | (done_pbase_paths_num - pos - 1) * sizeof(unsigned)); | |
1052 | done_pbase_paths[pos] = hash; | |
1053 | return 0; | |
1054 | } | |
1055 | ||
bc32fed5 | 1056 | static void add_preferred_base_object(const char *name) |
5379a5c5 JH |
1057 | { |
1058 | struct pbase_tree *it; | |
8a5a8d6c | 1059 | int cmplen; |
bc32fed5 | 1060 | unsigned hash = name_hash(name); |
5379a5c5 | 1061 | |
8a5a8d6c | 1062 | if (!num_preferred_base || check_pbase_path(hash)) |
5379a5c5 JH |
1063 | return; |
1064 | ||
8a5a8d6c | 1065 | cmplen = name_cmp_len(name); |
5379a5c5 JH |
1066 | for (it = pbase_tree; it; it = it->next) { |
1067 | if (cmplen == 0) { | |
bc32fed5 | 1068 | add_object_entry(it->pcache.sha1, OBJ_TREE, NULL, 1); |
5379a5c5 JH |
1069 | } |
1070 | else { | |
1071 | struct tree_desc tree; | |
6fda5e51 | 1072 | init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size); |
ce0bd642 | 1073 | add_pbase_object(&tree, name, cmplen, name); |
7a979d99 | 1074 | } |
3f9ac8d2 | 1075 | } |
3f9ac8d2 JH |
1076 | } |
1077 | ||
7a979d99 | 1078 | static void add_preferred_base(unsigned char *sha1) |
3f9ac8d2 | 1079 | { |
5379a5c5 JH |
1080 | struct pbase_tree *it; |
1081 | void *data; | |
1082 | unsigned long size; | |
1083 | unsigned char tree_sha1[20]; | |
1d6b38cc | 1084 | |
8d1d8f83 JH |
1085 | if (window <= num_preferred_base++) |
1086 | return; | |
1087 | ||
5379a5c5 JH |
1088 | data = read_object_with_reference(sha1, tree_type, &size, tree_sha1); |
1089 | if (!data) | |
7a979d99 | 1090 | return; |
5379a5c5 JH |
1091 | |
1092 | for (it = pbase_tree; it; it = it->next) { | |
a89fccd2 | 1093 | if (!hashcmp(it->pcache.sha1, tree_sha1)) { |
5379a5c5 JH |
1094 | free(data); |
1095 | return; | |
1096 | } | |
1097 | } | |
1098 | ||
1099 | it = xcalloc(1, sizeof(*it)); | |
1100 | it->next = pbase_tree; | |
1101 | pbase_tree = it; | |
1102 | ||
e702496e | 1103 | hashcpy(it->pcache.sha1, tree_sha1); |
5379a5c5 JH |
1104 | it->pcache.tree_data = data; |
1105 | it->pcache.tree_size = size; | |
3f9ac8d2 JH |
1106 | } |
1107 | ||
c323ac7d LT |
1108 | static void check_object(struct object_entry *entry) |
1109 | { | |
5c49c116 | 1110 | if (entry->in_pack) { |
780e6e73 | 1111 | struct packed_git *p = entry->in_pack; |
03e79c88 | 1112 | struct pack_window *w_curs = NULL; |
5c49c116 NP |
1113 | const unsigned char *base_ref = NULL; |
1114 | struct object_entry *base_entry; | |
1115 | unsigned long used, used_0; | |
6777a59f | 1116 | unsigned int avail; |
5c49c116 NP |
1117 | off_t ofs; |
1118 | unsigned char *buf, c; | |
780e6e73 | 1119 | |
6777a59f | 1120 | buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail); |
ab7cd7bb | 1121 | |
5c49c116 | 1122 | /* |
fa736f72 NP |
1123 | * We want in_pack_type even if we do not reuse delta |
1124 | * since non-delta representations could still be reused. | |
ab7cd7bb | 1125 | */ |
6777a59f | 1126 | used = unpack_object_header_gently(buf, avail, |
5c49c116 NP |
1127 | &entry->in_pack_type, |
1128 | &entry->size); | |
ab7cd7bb | 1129 | |
5c49c116 NP |
1130 | /* |
1131 | * Determine if this is a delta and if so whether we can | |
1132 | * reuse it or not. Otherwise let's find out as cheaply as | |
1133 | * possible what the actual type and size for this object is. | |
3f9ac8d2 | 1134 | */ |
5c49c116 NP |
1135 | switch (entry->in_pack_type) { |
1136 | default: | |
1137 | /* Not a delta hence we've already got all we need. */ | |
1138 | entry->type = entry->in_pack_type; | |
1139 | entry->in_pack_header_size = used; | |
1140 | unuse_pack(&w_curs); | |
1141 | return; | |
1142 | case OBJ_REF_DELTA: | |
1143 | if (!no_reuse_delta && !entry->preferred_base) | |
1144 | base_ref = use_pack(p, &w_curs, | |
1145 | entry->in_pack_offset + used, NULL); | |
1146 | entry->in_pack_header_size = used + 20; | |
1147 | break; | |
1148 | case OBJ_OFS_DELTA: | |
1149 | buf = use_pack(p, &w_curs, | |
1150 | entry->in_pack_offset + used, NULL); | |
1151 | used_0 = 0; | |
1152 | c = buf[used_0++]; | |
1153 | ofs = c & 127; | |
1154 | while (c & 128) { | |
1155 | ofs += 1; | |
1156 | if (!ofs || MSB(ofs, 7)) | |
1157 | die("delta base offset overflow in pack for %s", | |
aa7e44bf | 1158 | sha1_to_hex(entry->idx.sha1)); |
5c49c116 NP |
1159 | c = buf[used_0++]; |
1160 | ofs = (ofs << 7) + (c & 127); | |
780e6e73 | 1161 | } |
5c49c116 NP |
1162 | if (ofs >= entry->in_pack_offset) |
1163 | die("delta base offset out of bound for %s", | |
aa7e44bf | 1164 | sha1_to_hex(entry->idx.sha1)); |
5c49c116 NP |
1165 | ofs = entry->in_pack_offset - ofs; |
1166 | if (!no_reuse_delta && !entry->preferred_base) | |
1167 | base_ref = find_packed_object_name(p, ofs); | |
1168 | entry->in_pack_header_size = used + used_0; | |
1169 | break; | |
780e6e73 | 1170 | } |
780e6e73 | 1171 | |
5c49c116 NP |
1172 | if (base_ref && (base_entry = locate_object_entry(base_ref))) { |
1173 | /* | |
1174 | * If base_ref was set above that means we wish to | |
1175 | * reuse delta data, and we even found that base | |
1176 | * in the list of objects we want to pack. Goodie! | |
1177 | * | |
1178 | * Depth value does not matter - find_deltas() will | |
1179 | * never consider reused delta as the base object to | |
1180 | * deltify other objects against, in order to avoid | |
1181 | * circular deltas. | |
3f9ac8d2 | 1182 | */ |
780e6e73 | 1183 | entry->type = entry->in_pack_type; |
5c49c116 | 1184 | entry->delta = base_entry; |
15b4d577 JH |
1185 | entry->delta_sibling = base_entry->delta_child; |
1186 | base_entry->delta_child = entry; | |
5c49c116 NP |
1187 | unuse_pack(&w_curs); |
1188 | return; | |
1189 | } | |
ab7cd7bb | 1190 | |
5c49c116 NP |
1191 | if (entry->type) { |
1192 | /* | |
1193 | * This must be a delta and we already know what the | |
1194 | * final object type is. Let's extract the actual | |
1195 | * object size from the delta header. | |
1196 | */ | |
1197 | entry->size = get_size_from_delta(p, &w_curs, | |
1198 | entry->in_pack_offset + entry->in_pack_header_size); | |
1199 | unuse_pack(&w_curs); | |
3f9ac8d2 JH |
1200 | return; |
1201 | } | |
5c49c116 NP |
1202 | |
1203 | /* | |
1204 | * No choice but to fall back to the recursive delta walk | |
1205 | * with sha1_object_info() to find about the object type | |
1206 | * at this point... | |
1207 | */ | |
1208 | unuse_pack(&w_curs); | |
36e4d74a | 1209 | } |
3f9ac8d2 | 1210 | |
aa7e44bf | 1211 | entry->type = sha1_object_info(entry->idx.sha1, &entry->size); |
21666f1a | 1212 | if (entry->type < 0) |
36e4d74a | 1213 | die("unable to get type of object %s", |
aa7e44bf | 1214 | sha1_to_hex(entry->idx.sha1)); |
3f9ac8d2 JH |
1215 | } |
1216 | ||
5c49c116 NP |
1217 | static int pack_offset_sort(const void *_a, const void *_b) |
1218 | { | |
1219 | const struct object_entry *a = *(struct object_entry **)_a; | |
1220 | const struct object_entry *b = *(struct object_entry **)_b; | |
1221 | ||
1222 | /* avoid filesystem trashing with loose objects */ | |
1223 | if (!a->in_pack && !b->in_pack) | |
aa7e44bf | 1224 | return hashcmp(a->idx.sha1, b->idx.sha1); |
5c49c116 NP |
1225 | |
1226 | if (a->in_pack < b->in_pack) | |
1227 | return -1; | |
1228 | if (a->in_pack > b->in_pack) | |
1229 | return 1; | |
1230 | return a->in_pack_offset < b->in_pack_offset ? -1 : | |
1231 | (a->in_pack_offset > b->in_pack_offset); | |
1232 | } | |
1233 | ||
c323ac7d LT |
1234 | static void get_object_details(void) |
1235 | { | |
7cadf491 | 1236 | uint32_t i; |
5c49c116 NP |
1237 | struct object_entry **sorted_by_offset; |
1238 | ||
1239 | sorted_by_offset = xcalloc(nr_objects, sizeof(struct object_entry *)); | |
1240 | for (i = 0; i < nr_objects; i++) | |
1241 | sorted_by_offset[i] = objects + i; | |
1242 | qsort(sorted_by_offset, nr_objects, sizeof(*sorted_by_offset), pack_offset_sort); | |
c323ac7d | 1243 | |
3f9ac8d2 | 1244 | prepare_pack_ix(); |
5c49c116 NP |
1245 | for (i = 0; i < nr_objects; i++) |
1246 | check_object(sorted_by_offset[i]); | |
1247 | free(sorted_by_offset); | |
c323ac7d LT |
1248 | } |
1249 | ||
9668cf59 | 1250 | static int type_size_sort(const void *_a, const void *_b) |
c323ac7d | 1251 | { |
9668cf59 NP |
1252 | const struct object_entry *a = *(struct object_entry **)_a; |
1253 | const struct object_entry *b = *(struct object_entry **)_b; | |
1254 | ||
c323ac7d LT |
1255 | if (a->type < b->type) |
1256 | return -1; | |
1257 | if (a->type > b->type) | |
1258 | return 1; | |
27225f2e LT |
1259 | if (a->hash < b->hash) |
1260 | return -1; | |
1261 | if (a->hash > b->hash) | |
1262 | return 1; | |
7a979d99 JH |
1263 | if (a->preferred_base < b->preferred_base) |
1264 | return -1; | |
1265 | if (a->preferred_base > b->preferred_base) | |
1266 | return 1; | |
c323ac7d LT |
1267 | if (a->size < b->size) |
1268 | return -1; | |
1269 | if (a->size > b->size) | |
1270 | return 1; | |
adcc7095 | 1271 | return a > b ? -1 : (a < b); /* newest last */ |
c323ac7d LT |
1272 | } |
1273 | ||
1274 | struct unpacked { | |
1275 | struct object_entry *entry; | |
1276 | void *data; | |
f6c7081a | 1277 | struct delta_index *index; |
5a235b5e | 1278 | unsigned depth; |
c323ac7d LT |
1279 | }; |
1280 | ||
d250626c NP |
1281 | static int delta_cacheable(unsigned long src_size, unsigned long trg_size, |
1282 | unsigned long delta_size) | |
074b2eea MK |
1283 | { |
1284 | if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size) | |
1285 | return 0; | |
1286 | ||
e3dfddb3 MK |
1287 | if (delta_size < cache_max_small_delta_size) |
1288 | return 1; | |
1289 | ||
074b2eea MK |
1290 | /* cache delta, if objects are large enough compared to delta size */ |
1291 | if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10)) | |
1292 | return 1; | |
1293 | ||
1294 | return 0; | |
1295 | } | |
1296 | ||
8ecce684 NP |
1297 | #ifdef THREADED_DELTA_SEARCH |
1298 | ||
1299 | static pthread_mutex_t read_mutex = PTHREAD_MUTEX_INITIALIZER; | |
1300 | #define read_lock() pthread_mutex_lock(&read_mutex) | |
1301 | #define read_unlock() pthread_mutex_unlock(&read_mutex) | |
1302 | ||
1303 | static pthread_mutex_t progress_mutex = PTHREAD_MUTEX_INITIALIZER; | |
1304 | #define progress_lock() pthread_mutex_lock(&progress_mutex) | |
1305 | #define progress_unlock() pthread_mutex_unlock(&progress_mutex) | |
1306 | ||
1307 | #else | |
1308 | ||
1309 | #define read_lock() 0 | |
1310 | #define read_unlock() 0 | |
1311 | #define progress_lock() 0 | |
1312 | #define progress_unlock() 0 | |
1313 | ||
1314 | #endif | |
1315 | ||
c323ac7d | 1316 | /* |
521a4f4c LT |
1317 | * We search for deltas _backwards_ in a list sorted by type and |
1318 | * by size, so that we see progressively smaller and smaller files. | |
1319 | * That's because we prefer deltas to be from the bigger file | |
1320 | * to the smaller - deletes are potentially cheaper, but perhaps | |
1321 | * more importantly, the bigger file is likely the more recent | |
1322 | * one. | |
c323ac7d | 1323 | */ |
f6c7081a | 1324 | static int try_delta(struct unpacked *trg, struct unpacked *src, |
ef0316fc | 1325 | unsigned max_depth, unsigned long *mem_usage) |
c323ac7d | 1326 | { |
f6c7081a NP |
1327 | struct object_entry *trg_entry = trg->entry; |
1328 | struct object_entry *src_entry = src->entry; | |
560b25a8 | 1329 | unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz; |
c83f032e | 1330 | unsigned ref_depth; |
21666f1a | 1331 | enum object_type type; |
c323ac7d LT |
1332 | void *delta_buf; |
1333 | ||
1334 | /* Don't bother doing diffs between different types */ | |
f6c7081a | 1335 | if (trg_entry->type != src_entry->type) |
c323ac7d LT |
1336 | return -1; |
1337 | ||
51d1e83f LT |
1338 | /* |
1339 | * We do not bother to try a delta that we discarded | |
8dbbd14e | 1340 | * on an earlier try, but only when reusing delta data. |
51d1e83f | 1341 | */ |
8dbbd14e | 1342 | if (!no_reuse_delta && trg_entry->in_pack && |
e9195b58 JH |
1343 | trg_entry->in_pack == src_entry->in_pack && |
1344 | trg_entry->in_pack_type != OBJ_REF_DELTA && | |
1345 | trg_entry->in_pack_type != OBJ_OFS_DELTA) | |
51d1e83f LT |
1346 | return 0; |
1347 | ||
898b14ce | 1348 | /* Let's not bust the allowed depth. */ |
5a235b5e | 1349 | if (src->depth >= max_depth) |
d116a45a | 1350 | return 0; |
c323ac7d | 1351 | |
c3b06a69 | 1352 | /* Now some size filtering heuristics. */ |
560b25a8 | 1353 | trg_size = trg_entry->size; |
c83f032e NP |
1354 | if (!trg_entry->delta) { |
1355 | max_size = trg_size/2 - 20; | |
1356 | ref_depth = 1; | |
1357 | } else { | |
1358 | max_size = trg_entry->delta_size; | |
5a235b5e | 1359 | ref_depth = trg->depth; |
c83f032e | 1360 | } |
5a235b5e | 1361 | max_size = max_size * (max_depth - src->depth) / |
c83f032e | 1362 | (max_depth - ref_depth + 1); |
c3b06a69 NP |
1363 | if (max_size == 0) |
1364 | return 0; | |
f6c7081a | 1365 | src_size = src_entry->size; |
560b25a8 | 1366 | sizediff = src_size < trg_size ? trg_size - src_size : 0; |
27225f2e | 1367 | if (sizediff >= max_size) |
f527cb8c | 1368 | return 0; |
a1dab41a BD |
1369 | if (trg_size < src_size / 32) |
1370 | return 0; | |
f6c7081a | 1371 | |
560b25a8 NP |
1372 | /* Load data if not already done */ |
1373 | if (!trg->data) { | |
8ecce684 | 1374 | read_lock(); |
aa7e44bf | 1375 | trg->data = read_sha1_file(trg_entry->idx.sha1, &type, &sz); |
8ecce684 | 1376 | read_unlock(); |
2e3404c3 JH |
1377 | if (!trg->data) |
1378 | die("object %s cannot be read", | |
1379 | sha1_to_hex(trg_entry->idx.sha1)); | |
560b25a8 NP |
1380 | if (sz != trg_size) |
1381 | die("object %s inconsistent object length (%lu vs %lu)", | |
aa7e44bf | 1382 | sha1_to_hex(trg_entry->idx.sha1), sz, trg_size); |
ef0316fc | 1383 | *mem_usage += sz; |
560b25a8 NP |
1384 | } |
1385 | if (!src->data) { | |
8ecce684 | 1386 | read_lock(); |
aa7e44bf | 1387 | src->data = read_sha1_file(src_entry->idx.sha1, &type, &sz); |
8ecce684 | 1388 | read_unlock(); |
2e3404c3 JH |
1389 | if (!src->data) |
1390 | die("object %s cannot be read", | |
1391 | sha1_to_hex(src_entry->idx.sha1)); | |
560b25a8 NP |
1392 | if (sz != src_size) |
1393 | die("object %s inconsistent object length (%lu vs %lu)", | |
aa7e44bf | 1394 | sha1_to_hex(src_entry->idx.sha1), sz, src_size); |
ef0316fc | 1395 | *mem_usage += sz; |
560b25a8 NP |
1396 | } |
1397 | if (!src->index) { | |
1398 | src->index = create_delta_index(src->data, src_size); | |
a588d88a MK |
1399 | if (!src->index) { |
1400 | static int warned = 0; | |
1401 | if (!warned++) | |
1402 | warning("suboptimal pack - out of memory"); | |
1403 | return 0; | |
1404 | } | |
ef0316fc | 1405 | *mem_usage += sizeof_delta_index(src->index); |
560b25a8 NP |
1406 | } |
1407 | ||
1408 | delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size); | |
c323ac7d | 1409 | if (!delta_buf) |
75c42d8c | 1410 | return 0; |
f6c7081a | 1411 | |
9e2d57a0 | 1412 | if (trg_entry->delta) { |
848d732c BD |
1413 | /* Prefer only shallower same-sized deltas. */ |
1414 | if (delta_size == trg_entry->delta_size && | |
5a235b5e | 1415 | src->depth + 1 >= trg->depth) { |
848d732c BD |
1416 | free(delta_buf); |
1417 | return 0; | |
1418 | } | |
074b2eea | 1419 | } |
9e2d57a0 | 1420 | |
f6c7081a NP |
1421 | trg_entry->delta = src_entry; |
1422 | trg_entry->delta_size = delta_size; | |
5a235b5e | 1423 | trg->depth = src->depth + 1; |
074b2eea | 1424 | |
9e2d57a0 NP |
1425 | if (trg_entry->delta_data) { |
1426 | delta_cache_size -= trg_entry->delta_size; | |
1427 | free(trg_entry->delta_data); | |
1428 | trg_entry->delta_data = NULL; | |
1429 | } | |
1430 | ||
d250626c | 1431 | if (delta_cacheable(src_size, trg_size, delta_size)) { |
074b2eea MK |
1432 | trg_entry->delta_data = xrealloc(delta_buf, delta_size); |
1433 | delta_cache_size += trg_entry->delta_size; | |
1434 | } else | |
1435 | free(delta_buf); | |
f6c7081a | 1436 | return 1; |
c323ac7d LT |
1437 | } |
1438 | ||
898b14ce | 1439 | static unsigned int check_delta_limit(struct object_entry *me, unsigned int n) |
b2504a0d | 1440 | { |
898b14ce NP |
1441 | struct object_entry *child = me->delta_child; |
1442 | unsigned int m = n; | |
1443 | while (child) { | |
1444 | unsigned int c = check_delta_limit(child, n + 1); | |
1445 | if (m < c) | |
1446 | m = c; | |
1447 | child = child->delta_sibling; | |
1448 | } | |
1449 | return m; | |
b2504a0d NP |
1450 | } |
1451 | ||
ef0316fc | 1452 | static unsigned long free_unpacked(struct unpacked *n) |
a97773ce | 1453 | { |
ef0316fc | 1454 | unsigned long freed_mem = sizeof_delta_index(n->index); |
a97773ce BD |
1455 | free_delta_index(n->index); |
1456 | n->index = NULL; | |
1457 | if (n->data) { | |
ef0316fc | 1458 | freed_mem += n->entry->size; |
a97773ce BD |
1459 | free(n->data); |
1460 | n->data = NULL; | |
a97773ce BD |
1461 | } |
1462 | n->entry = NULL; | |
7d7baa5e | 1463 | n->depth = 0; |
ef0316fc | 1464 | return freed_mem; |
a97773ce BD |
1465 | } |
1466 | ||
75d39853 | 1467 | static void find_deltas(struct object_entry **list, unsigned list_size, |
e334977d | 1468 | int window, int depth, unsigned *processed) |
c323ac7d | 1469 | { |
e334977d | 1470 | uint32_t i = list_size, idx = 0, count = 0; |
c323ac7d | 1471 | unsigned int array_size = window * sizeof(struct unpacked); |
7cadf491 | 1472 | struct unpacked *array; |
ef0316fc | 1473 | unsigned long mem_usage = 0; |
c323ac7d | 1474 | |
7cadf491 | 1475 | array = xmalloc(array_size); |
c323ac7d | 1476 | memset(array, 0, array_size); |
21fcd1bd | 1477 | |
7cadf491 SP |
1478 | do { |
1479 | struct object_entry *entry = list[--i]; | |
c323ac7d | 1480 | struct unpacked *n = array + idx; |
ef0316fc | 1481 | int j, max_depth, best_base = -1; |
c323ac7d | 1482 | |
ef0316fc | 1483 | mem_usage -= free_unpacked(n); |
c323ac7d | 1484 | n->entry = entry; |
ab7cd7bb | 1485 | |
a97773ce | 1486 | while (window_memory_limit && |
ef0316fc | 1487 | mem_usage > window_memory_limit && |
a97773ce BD |
1488 | count > 1) { |
1489 | uint32_t tail = (idx + window - count) % window; | |
ef0316fc | 1490 | mem_usage -= free_unpacked(array + tail); |
a97773ce BD |
1491 | count--; |
1492 | } | |
1493 | ||
75d39853 NP |
1494 | /* We do not compute delta to *create* objects we are not |
1495 | * going to pack. | |
1496 | */ | |
1497 | if (entry->preferred_base) | |
1498 | goto next; | |
1499 | ||
8ecce684 | 1500 | progress_lock(); |
e334977d | 1501 | (*processed)++; |
75d39853 | 1502 | if (progress) |
e334977d | 1503 | display_progress(&progress_state, *processed); |
8ecce684 | 1504 | progress_unlock(); |
75d39853 | 1505 | |
898b14ce NP |
1506 | /* |
1507 | * If the current object is at pack edge, take the depth the | |
1508 | * objects that depend on the current object into account | |
1509 | * otherwise they would become too deep. | |
1510 | */ | |
1511 | max_depth = depth; | |
1512 | if (entry->delta_child) { | |
1513 | max_depth -= check_delta_limit(entry, 0); | |
1514 | if (max_depth <= 0) | |
1515 | goto next; | |
1516 | } | |
1517 | ||
78817c15 LT |
1518 | j = window; |
1519 | while (--j > 0) { | |
77639870 | 1520 | int ret; |
7cadf491 | 1521 | uint32_t other_idx = idx + j; |
c323ac7d | 1522 | struct unpacked *m; |
78817c15 LT |
1523 | if (other_idx >= window) |
1524 | other_idx -= window; | |
c323ac7d LT |
1525 | m = array + other_idx; |
1526 | if (!m->entry) | |
1527 | break; | |
ef0316fc | 1528 | ret = try_delta(n, m, max_depth, &mem_usage); |
77639870 | 1529 | if (ret < 0) |
c323ac7d | 1530 | break; |
77639870 JH |
1531 | else if (ret > 0) |
1532 | best_base = other_idx; | |
c323ac7d | 1533 | } |
898b14ce | 1534 | |
70ca1a3f JH |
1535 | /* if we made n a delta, and if n is already at max |
1536 | * depth, leaving it in the window is pointless. we | |
1537 | * should evict it first. | |
70ca1a3f | 1538 | */ |
5a235b5e | 1539 | if (entry->delta && depth <= n->depth) |
70ca1a3f | 1540 | continue; |
ff45715c | 1541 | |
77639870 JH |
1542 | /* |
1543 | * Move the best delta base up in the window, after the | |
1544 | * currently deltified object, to keep it longer. It will | |
1545 | * be the first base object to be attempted next. | |
1546 | */ | |
1547 | if (entry->delta) { | |
1548 | struct unpacked swap = array[best_base]; | |
1549 | int dist = (window + idx - best_base) % window; | |
1550 | int dst = best_base; | |
1551 | while (dist--) { | |
1552 | int src = (dst + 1) % window; | |
1553 | array[dst] = array[src]; | |
1554 | dst = src; | |
1555 | } | |
1556 | array[dst] = swap; | |
1557 | } | |
1558 | ||
898b14ce | 1559 | next: |
521a4f4c | 1560 | idx++; |
a97773ce BD |
1561 | if (count + 1 < window) |
1562 | count++; | |
521a4f4c LT |
1563 | if (idx >= window) |
1564 | idx = 0; | |
7cadf491 | 1565 | } while (i > 0); |
adee7bdf | 1566 | |
f6c7081a | 1567 | for (i = 0; i < window; ++i) { |
ff45715c | 1568 | free_delta_index(array[i].index); |
adee7bdf | 1569 | free(array[i].data); |
f6c7081a | 1570 | } |
adee7bdf | 1571 | free(array); |
c323ac7d LT |
1572 | } |
1573 | ||
8ecce684 NP |
1574 | #ifdef THREADED_DELTA_SEARCH |
1575 | ||
1576 | struct thread_params { | |
1577 | pthread_t thread; | |
1578 | struct object_entry **list; | |
1579 | unsigned list_size; | |
1580 | int window; | |
1581 | int depth; | |
1582 | unsigned *processed; | |
1583 | }; | |
1584 | ||
c2a33679 NP |
1585 | static pthread_mutex_t data_request = PTHREAD_MUTEX_INITIALIZER; |
1586 | static pthread_mutex_t data_ready = PTHREAD_MUTEX_INITIALIZER; | |
1587 | static pthread_mutex_t data_provider = PTHREAD_MUTEX_INITIALIZER; | |
1588 | static struct thread_params *data_requester; | |
1589 | ||
8ecce684 NP |
1590 | static void *threaded_find_deltas(void *arg) |
1591 | { | |
c2a33679 NP |
1592 | struct thread_params *me = arg; |
1593 | ||
1594 | for (;;) { | |
1595 | pthread_mutex_lock(&data_request); | |
1596 | data_requester = me; | |
1597 | pthread_mutex_unlock(&data_provider); | |
1598 | pthread_mutex_lock(&data_ready); | |
1599 | ||
1600 | if (!me->list_size) | |
1601 | return NULL; | |
1602 | ||
1603 | find_deltas(me->list, me->list_size, | |
1604 | me->window, me->depth, me->processed); | |
1605 | } | |
8ecce684 NP |
1606 | } |
1607 | ||
c2a33679 | 1608 | #define NR_THREADS 4 |
8ecce684 NP |
1609 | |
1610 | static void ll_find_deltas(struct object_entry **list, unsigned list_size, | |
1611 | int window, int depth, unsigned *processed) | |
1612 | { | |
1613 | struct thread_params p[NR_THREADS]; | |
1614 | int i, ret; | |
c2a33679 NP |
1615 | unsigned chunk_size; |
1616 | ||
1617 | pthread_mutex_lock(&data_provider); | |
1618 | pthread_mutex_lock(&data_ready); | |
8ecce684 NP |
1619 | |
1620 | for (i = 0; i < NR_THREADS; i++) { | |
8ecce684 NP |
1621 | p[i].window = window; |
1622 | p[i].depth = depth; | |
1623 | p[i].processed = processed; | |
1624 | ret = pthread_create(&p[i].thread, NULL, | |
1625 | threaded_find_deltas, &p[i]); | |
1626 | if (ret) | |
1627 | die("unable to create thread: %s", strerror(ret)); | |
8ecce684 NP |
1628 | } |
1629 | ||
c2a33679 NP |
1630 | /* this should be auto-tuned somehow */ |
1631 | chunk_size = window * 1000; | |
1632 | ||
1633 | do { | |
1634 | unsigned sublist_size = chunk_size; | |
1635 | if (sublist_size > list_size) | |
1636 | sublist_size = list_size; | |
1637 | ||
59921b4b NP |
1638 | /* try to split chunks on "path" boundaries */ |
1639 | while (sublist_size < list_size && list[sublist_size]->hash && | |
1640 | list[sublist_size]->hash == list[sublist_size-1]->hash) | |
1641 | sublist_size++; | |
1642 | ||
c2a33679 NP |
1643 | pthread_mutex_lock(&data_provider); |
1644 | data_requester->list = list; | |
1645 | data_requester->list_size = sublist_size; | |
1646 | pthread_mutex_unlock(&data_ready); | |
1647 | ||
1648 | list += sublist_size; | |
1649 | list_size -= sublist_size; | |
1650 | if (!sublist_size) { | |
1651 | pthread_join(data_requester->thread, NULL); | |
1652 | i--; | |
1653 | } | |
1654 | pthread_mutex_unlock(&data_request); | |
1655 | } while (i); | |
8ecce684 NP |
1656 | } |
1657 | ||
1658 | #else | |
1659 | #define ll_find_deltas find_deltas | |
1660 | #endif | |
1661 | ||
f3123c4a JH |
1662 | static void prepare_pack(int window, int depth) |
1663 | { | |
9668cf59 | 1664 | struct object_entry **delta_list; |
75d39853 | 1665 | uint32_t i, n, nr_deltas; |
9668cf59 | 1666 | |
3f9ac8d2 | 1667 | get_object_details(); |
9668cf59 | 1668 | |
75d39853 | 1669 | if (!nr_objects || !window || !depth) |
9668cf59 NP |
1670 | return; |
1671 | ||
1672 | delta_list = xmalloc(nr_objects * sizeof(*delta_list)); | |
75d39853 NP |
1673 | nr_deltas = n = 0; |
1674 | ||
1675 | for (i = 0; i < nr_objects; i++) { | |
1676 | struct object_entry *entry = objects + i; | |
1677 | ||
1678 | if (entry->delta) | |
1679 | /* This happens if we decided to reuse existing | |
1680 | * delta from a pack. "!no_reuse_delta &&" is implied. | |
1681 | */ | |
1682 | continue; | |
1683 | ||
1684 | if (entry->size < 50) | |
1685 | continue; | |
1686 | ||
1687 | if (entry->no_try_delta) | |
1688 | continue; | |
1689 | ||
1690 | if (!entry->preferred_base) | |
1691 | nr_deltas++; | |
1692 | ||
1693 | delta_list[n++] = entry; | |
1694 | } | |
1695 | ||
1696 | if (nr_deltas) { | |
e334977d NP |
1697 | unsigned nr_done = 0; |
1698 | if (progress) | |
1699 | start_progress(&progress_state, | |
1700 | "Deltifying %u objects...", "", | |
1701 | nr_deltas); | |
75d39853 | 1702 | qsort(delta_list, n, sizeof(*delta_list), type_size_sort); |
8ecce684 | 1703 | ll_find_deltas(delta_list, n, window+1, depth, &nr_done); |
e334977d NP |
1704 | if (progress) |
1705 | stop_progress(&progress_state); | |
1706 | if (nr_done != nr_deltas) | |
1707 | die("inconsistency with delta count"); | |
75d39853 | 1708 | } |
9668cf59 | 1709 | free(delta_list); |
f3123c4a JH |
1710 | } |
1711 | ||
4812a93a JK |
1712 | static int git_pack_config(const char *k, const char *v) |
1713 | { | |
1714 | if(!strcmp(k, "pack.window")) { | |
1715 | window = git_config_int(k, v); | |
1716 | return 0; | |
1717 | } | |
a97773ce BD |
1718 | if (!strcmp(k, "pack.windowmemory")) { |
1719 | window_memory_limit = git_config_ulong(k, v); | |
1720 | return 0; | |
1721 | } | |
1722 | if (!strcmp(k, "pack.depth")) { | |
842aaf93 TT |
1723 | depth = git_config_int(k, v); |
1724 | return 0; | |
1725 | } | |
960ccca6 DH |
1726 | if (!strcmp(k, "pack.compression")) { |
1727 | int level = git_config_int(k, v); | |
1728 | if (level == -1) | |
1729 | level = Z_DEFAULT_COMPRESSION; | |
1730 | else if (level < 0 || level > Z_BEST_COMPRESSION) | |
1731 | die("bad pack compression level %d", level); | |
1732 | pack_compression_level = level; | |
1733 | pack_compression_seen = 1; | |
1734 | return 0; | |
1735 | } | |
074b2eea MK |
1736 | if (!strcmp(k, "pack.deltacachesize")) { |
1737 | max_delta_cache_size = git_config_int(k, v); | |
1738 | return 0; | |
1739 | } | |
e3dfddb3 MK |
1740 | if (!strcmp(k, "pack.deltacachelimit")) { |
1741 | cache_max_small_delta_size = git_config_int(k, v); | |
1742 | return 0; | |
1743 | } | |
4812a93a JK |
1744 | return git_default_config(k, v); |
1745 | } | |
1746 | ||
b5d97e6b | 1747 | static void read_object_list_from_stdin(void) |
c323ac7d | 1748 | { |
b5d97e6b JH |
1749 | char line[40 + 1 + PATH_MAX + 2]; |
1750 | unsigned char sha1[20]; | |
b2504a0d | 1751 | |
da93d12b | 1752 | for (;;) { |
da93d12b LT |
1753 | if (!fgets(line, sizeof(line), stdin)) { |
1754 | if (feof(stdin)) | |
1755 | break; | |
1756 | if (!ferror(stdin)) | |
1757 | die("fgets returned NULL, not EOF, not error!"); | |
687dd75c JH |
1758 | if (errno != EINTR) |
1759 | die("fgets: %s", strerror(errno)); | |
1760 | clearerr(stdin); | |
1761 | continue; | |
da93d12b | 1762 | } |
7a979d99 JH |
1763 | if (line[0] == '-') { |
1764 | if (get_sha1_hex(line+1, sha1)) | |
1765 | die("expected edge sha1, got garbage:\n %s", | |
b5d97e6b | 1766 | line); |
8d1d8f83 | 1767 | add_preferred_base(sha1); |
7a979d99 | 1768 | continue; |
21fcd1bd | 1769 | } |
c323ac7d | 1770 | if (get_sha1_hex(line, sha1)) |
ef07618f | 1771 | die("expected sha1, got garbage:\n %s", line); |
b5d97e6b | 1772 | |
bc32fed5 JH |
1773 | add_preferred_base_object(line+41); |
1774 | add_object_entry(sha1, 0, line+41, 0); | |
c323ac7d | 1775 | } |
b5d97e6b JH |
1776 | } |
1777 | ||
b5d97e6b JH |
1778 | static void show_commit(struct commit *commit) |
1779 | { | |
bc32fed5 | 1780 | add_object_entry(commit->object.sha1, OBJ_COMMIT, NULL, 0); |
b5d97e6b JH |
1781 | } |
1782 | ||
1783 | static void show_object(struct object_array_entry *p) | |
1784 | { | |
bc32fed5 JH |
1785 | add_preferred_base_object(p->name); |
1786 | add_object_entry(p->item->sha1, p->item->type, p->name, 0); | |
b5d97e6b JH |
1787 | } |
1788 | ||
8d1d8f83 JH |
1789 | static void show_edge(struct commit *commit) |
1790 | { | |
1791 | add_preferred_base(commit->object.sha1); | |
1792 | } | |
1793 | ||
1794 | static void get_object_list(int ac, const char **av) | |
b5d97e6b JH |
1795 | { |
1796 | struct rev_info revs; | |
1797 | char line[1000]; | |
b5d97e6b JH |
1798 | int flags = 0; |
1799 | ||
b5d97e6b JH |
1800 | init_revisions(&revs, NULL); |
1801 | save_commit_buffer = 0; | |
1802 | track_object_refs = 0; | |
1803 | setup_revisions(ac, av, &revs, NULL); | |
1804 | ||
b5d97e6b JH |
1805 | while (fgets(line, sizeof(line), stdin) != NULL) { |
1806 | int len = strlen(line); | |
1807 | if (line[len - 1] == '\n') | |
1808 | line[--len] = 0; | |
1809 | if (!len) | |
1810 | break; | |
1811 | if (*line == '-') { | |
1812 | if (!strcmp(line, "--not")) { | |
1813 | flags ^= UNINTERESTING; | |
1814 | continue; | |
1815 | } | |
1816 | die("not a rev '%s'", line); | |
1817 | } | |
1818 | if (handle_revision_arg(line, &revs, flags, 1)) | |
1819 | die("bad revision '%s'", line); | |
1820 | } | |
1821 | ||
1822 | prepare_revision_walk(&revs); | |
8d1d8f83 | 1823 | mark_edges_uninteresting(revs.commits, &revs, show_edge); |
b5d97e6b JH |
1824 | traverse_commit_list(&revs, show_commit, show_object); |
1825 | } | |
1826 | ||
b6b32ccb JH |
1827 | static int adjust_perm(const char *path, mode_t mode) |
1828 | { | |
1829 | if (chmod(path, mode)) | |
1830 | return -1; | |
1831 | return adjust_shared_perm(path); | |
1832 | } | |
1833 | ||
b5d97e6b JH |
1834 | int cmd_pack_objects(int argc, const char **argv, const char *prefix) |
1835 | { | |
b5d97e6b | 1836 | int use_internal_rev_list = 0; |
8d1d8f83 | 1837 | int thin = 0; |
7cadf491 | 1838 | uint32_t i; |
ffa84ffb RD |
1839 | const char **rp_av; |
1840 | int rp_ac_alloc = 64; | |
8d1d8f83 JH |
1841 | int rp_ac; |
1842 | ||
ffa84ffb RD |
1843 | rp_av = xcalloc(rp_ac_alloc, sizeof(*rp_av)); |
1844 | ||
8d1d8f83 JH |
1845 | rp_av[0] = "pack-objects"; |
1846 | rp_av[1] = "--objects"; /* --thin will make it --objects-edge */ | |
1847 | rp_ac = 2; | |
b5d97e6b JH |
1848 | |
1849 | git_config(git_pack_config); | |
960ccca6 DH |
1850 | if (!pack_compression_seen && core_compression_seen) |
1851 | pack_compression_level = core_compression_level; | |
b5d97e6b JH |
1852 | |
1853 | progress = isatty(2); | |
1854 | for (i = 1; i < argc; i++) { | |
1855 | const char *arg = argv[i]; | |
1856 | ||
1857 | if (*arg != '-') | |
1858 | break; | |
1859 | ||
1860 | if (!strcmp("--non-empty", arg)) { | |
1861 | non_empty = 1; | |
1862 | continue; | |
1863 | } | |
1864 | if (!strcmp("--local", arg)) { | |
1865 | local = 1; | |
1866 | continue; | |
1867 | } | |
b5d97e6b JH |
1868 | if (!strcmp("--incremental", arg)) { |
1869 | incremental = 1; | |
1870 | continue; | |
1871 | } | |
960ccca6 DH |
1872 | if (!prefixcmp(arg, "--compression=")) { |
1873 | char *end; | |
1874 | int level = strtoul(arg+14, &end, 0); | |
1875 | if (!arg[14] || *end) | |
1876 | usage(pack_usage); | |
1877 | if (level == -1) | |
1878 | level = Z_DEFAULT_COMPRESSION; | |
1879 | else if (level < 0 || level > Z_BEST_COMPRESSION) | |
1880 | die("bad pack compression level %d", level); | |
1881 | pack_compression_level = level; | |
1882 | continue; | |
1883 | } | |
6b94b1a0 DH |
1884 | if (!prefixcmp(arg, "--max-pack-size=")) { |
1885 | char *end; | |
1886 | pack_size_limit = strtoul(arg+16, &end, 0) * 1024 * 1024; | |
1887 | if (!arg[16] || *end) | |
1888 | usage(pack_usage); | |
1889 | continue; | |
1890 | } | |
599065a3 | 1891 | if (!prefixcmp(arg, "--window=")) { |
b5d97e6b JH |
1892 | char *end; |
1893 | window = strtoul(arg+9, &end, 0); | |
1894 | if (!arg[9] || *end) | |
1895 | usage(pack_usage); | |
1896 | continue; | |
1897 | } | |
a97773ce BD |
1898 | if (!prefixcmp(arg, "--window-memory=")) { |
1899 | if (!git_parse_ulong(arg+16, &window_memory_limit)) | |
1900 | usage(pack_usage); | |
1901 | continue; | |
1902 | } | |
599065a3 | 1903 | if (!prefixcmp(arg, "--depth=")) { |
b5d97e6b JH |
1904 | char *end; |
1905 | depth = strtoul(arg+8, &end, 0); | |
1906 | if (!arg[8] || *end) | |
1907 | usage(pack_usage); | |
1908 | continue; | |
1909 | } | |
1910 | if (!strcmp("--progress", arg)) { | |
1911 | progress = 1; | |
1912 | continue; | |
1913 | } | |
231f240b NP |
1914 | if (!strcmp("--all-progress", arg)) { |
1915 | progress = 2; | |
1916 | continue; | |
1917 | } | |
b5d97e6b JH |
1918 | if (!strcmp("-q", arg)) { |
1919 | progress = 0; | |
1920 | continue; | |
1921 | } | |
1922 | if (!strcmp("--no-reuse-delta", arg)) { | |
1923 | no_reuse_delta = 1; | |
1924 | continue; | |
1925 | } | |
fa736f72 NP |
1926 | if (!strcmp("--no-reuse-object", arg)) { |
1927 | no_reuse_object = no_reuse_delta = 1; | |
1928 | continue; | |
1929 | } | |
be6b1914 | 1930 | if (!strcmp("--delta-base-offset", arg)) { |
780e6e73 | 1931 | allow_ofs_delta = 1; |
be6b1914 NP |
1932 | continue; |
1933 | } | |
b5d97e6b JH |
1934 | if (!strcmp("--stdout", arg)) { |
1935 | pack_to_stdout = 1; | |
1936 | continue; | |
1937 | } | |
1938 | if (!strcmp("--revs", arg)) { | |
1939 | use_internal_rev_list = 1; | |
1940 | continue; | |
1941 | } | |
8d1d8f83 | 1942 | if (!strcmp("--unpacked", arg) || |
599065a3 | 1943 | !prefixcmp(arg, "--unpacked=") || |
63049292 | 1944 | !strcmp("--reflog", arg) || |
8d1d8f83 JH |
1945 | !strcmp("--all", arg)) { |
1946 | use_internal_rev_list = 1; | |
ffa84ffb RD |
1947 | if (rp_ac >= rp_ac_alloc - 1) { |
1948 | rp_ac_alloc = alloc_nr(rp_ac_alloc); | |
1949 | rp_av = xrealloc(rp_av, | |
1950 | rp_ac_alloc * sizeof(*rp_av)); | |
1951 | } | |
8d1d8f83 | 1952 | rp_av[rp_ac++] = arg; |
b5d97e6b JH |
1953 | continue; |
1954 | } | |
8d1d8f83 JH |
1955 | if (!strcmp("--thin", arg)) { |
1956 | use_internal_rev_list = 1; | |
1957 | thin = 1; | |
1958 | rp_av[1] = "--objects-edge"; | |
b5d97e6b | 1959 | continue; |
4ba7d711 NP |
1960 | } |
1961 | if (!prefixcmp(arg, "--index-version=")) { | |
1962 | char *c; | |
aa7e44bf GB |
1963 | pack_idx_default_version = strtoul(arg + 16, &c, 10); |
1964 | if (pack_idx_default_version > 2) | |
4ba7d711 NP |
1965 | die("bad %s", arg); |
1966 | if (*c == ',') | |
aa7e44bf GB |
1967 | pack_idx_off32_limit = strtoul(c+1, &c, 0); |
1968 | if (*c || pack_idx_off32_limit & 0x80000000) | |
4ba7d711 NP |
1969 | die("bad %s", arg); |
1970 | continue; | |
b5d97e6b JH |
1971 | } |
1972 | usage(pack_usage); | |
1973 | } | |
1974 | ||
1975 | /* Traditionally "pack-objects [options] base extra" failed; | |
1976 | * we would however want to take refs parameter that would | |
1977 | * have been given to upstream rev-list ourselves, which means | |
1978 | * we somehow want to say what the base name is. So the | |
1979 | * syntax would be: | |
1980 | * | |
1981 | * pack-objects [options] base <refs...> | |
1982 | * | |
1983 | * in other words, we would treat the first non-option as the | |
1984 | * base_name and send everything else to the internal revision | |
1985 | * walker. | |
1986 | */ | |
1987 | ||
1988 | if (!pack_to_stdout) | |
1989 | base_name = argv[i++]; | |
1990 | ||
1991 | if (pack_to_stdout != !base_name) | |
1992 | usage(pack_usage); | |
1993 | ||
01c12a23 DH |
1994 | if (pack_to_stdout && pack_size_limit) |
1995 | die("--max-pack-size cannot be used to build a pack for transfer."); | |
1996 | ||
8d1d8f83 JH |
1997 | if (!pack_to_stdout && thin) |
1998 | die("--thin cannot be used to build an indexable pack."); | |
b5d97e6b JH |
1999 | |
2000 | prepare_packed_git(); | |
2001 | ||
13aaf148 NP |
2002 | if (progress) |
2003 | start_progress(&progress_state, "Generating pack...", | |
2004 | "Counting objects: ", 0); | |
b5d97e6b JH |
2005 | if (!use_internal_rev_list) |
2006 | read_object_list_from_stdin(); | |
8d1d8f83 JH |
2007 | else { |
2008 | rp_av[rp_ac] = NULL; | |
2009 | get_object_list(rp_ac, rp_av); | |
2010 | } | |
96a02f8f NP |
2011 | if (progress) { |
2012 | stop_progress(&progress_state); | |
7cadf491 | 2013 | fprintf(stderr, "Done counting %u objects.\n", nr_objects); |
96a02f8f NP |
2014 | } |
2015 | ||
f0b0af1b | 2016 | if (non_empty && !nr_result) |
1c4a2912 | 2017 | return 0; |
7a979d99 | 2018 | if (progress && (nr_objects != nr_result)) |
7cadf491 | 2019 | fprintf(stderr, "Result has %u objects.\n", nr_result); |
f7ae6a93 NP |
2020 | if (nr_result) |
2021 | prepare_pack(window, depth); | |
d01fb92f | 2022 | write_pack_file(); |
ab7cd7bb | 2023 | if (progress) |
7cadf491 | 2024 | fprintf(stderr, "Total %u (delta %u), reused %u (delta %u)\n", |
67c08ce1 | 2025 | written, written_delta, reused, reused_delta); |
c323ac7d LT |
2026 | return 0; |
2027 | } |