]>
Commit | Line | Data |
---|---|---|
5d4a6003 | 1 | #include "builtin.h" |
c323ac7d LT |
2 | #include "cache.h" |
3 | #include "object.h" | |
8e440259 PE |
4 | #include "blob.h" |
5 | #include "commit.h" | |
6 | #include "tag.h" | |
7 | #include "tree.h" | |
c323ac7d | 8 | #include "delta.h" |
a733cb60 | 9 | #include "pack.h" |
c38138cd | 10 | #include "csum-file.h" |
1b0c7174 | 11 | #include "tree-walk.h" |
21fcd1bd | 12 | #include <sys/time.h> |
b2504a0d | 13 | #include <signal.h> |
c323ac7d | 14 | |
ab7cd7bb | 15 | static const char pack_usage[] = "git-pack-objects [-q] [--no-reuse-delta] [--non-empty] [--local] [--incremental] [--window=N] [--depth=N] {--stdout | base-name} < object-list"; |
c323ac7d | 16 | |
c323ac7d LT |
17 | struct object_entry { |
18 | unsigned char sha1[20]; | |
3f9ac8d2 | 19 | unsigned long size; /* uncompressed size */ |
15b4d577 JH |
20 | unsigned long offset; /* offset into the final pack file; |
21 | * nonzero if already written. | |
22 | */ | |
3f9ac8d2 | 23 | unsigned int depth; /* delta depth */ |
15b4d577 | 24 | unsigned int delta_limit; /* base adjustment for in-pack delta */ |
3f9ac8d2 | 25 | unsigned int hash; /* name hint hash */ |
a733cb60 | 26 | enum object_type type; |
ab7cd7bb | 27 | enum object_type in_pack_type; /* could be delta */ |
3f9ac8d2 JH |
28 | unsigned long delta_size; /* delta data size (uncompressed) */ |
29 | struct object_entry *delta; /* delta base object */ | |
30 | struct packed_git *in_pack; /* already in pack */ | |
3f9ac8d2 | 31 | unsigned int in_pack_offset; |
82e5a82f | 32 | struct object_entry *delta_child; /* deltified objects who bases me */ |
15b4d577 JH |
33 | struct object_entry *delta_sibling; /* other deltified objects who |
34 | * uses the same base as me | |
35 | */ | |
7a979d99 JH |
36 | int preferred_base; /* we do not pack this, but is encouraged to |
37 | * be used as the base objectto delta huge | |
38 | * objects against. | |
39 | */ | |
c323ac7d LT |
40 | }; |
41 | ||
3f9ac8d2 | 42 | /* |
82e5a82f | 43 | * Objects we are going to pack are collected in objects array (dynamically |
3f9ac8d2 JH |
44 | * expanded). nr_objects & nr_alloc controls this array. They are stored |
45 | * in the order we see -- typically rev-list --objects order that gives us | |
46 | * nice "minimum seek" order. | |
47 | * | |
48 | * sorted-by-sha ans sorted-by-type are arrays of pointers that point at | |
49 | * elements in the objects array. The former is used to build the pack | |
50 | * index (lists object names in the ascending order to help offset lookup), | |
51 | * and the latter is used to group similar things together by try_delta() | |
52 | * heuristics. | |
53 | */ | |
54 | ||
5f3de58f | 55 | static unsigned char object_list_sha1[20]; |
96f1e58f DR |
56 | static int non_empty; |
57 | static int no_reuse_delta; | |
58 | static int local; | |
59 | static int incremental; | |
c323ac7d | 60 | static struct object_entry **sorted_by_sha, **sorted_by_type; |
96f1e58f DR |
61 | static struct object_entry *objects; |
62 | static int nr_objects, nr_alloc, nr_result; | |
c323ac7d | 63 | static const char *base_name; |
e1808845 | 64 | static unsigned char pack_file_sha1[20]; |
024701f1 | 65 | static int progress = 1; |
96f1e58f | 66 | static volatile sig_atomic_t progress_update; |
4812a93a | 67 | static int window = 10; |
df6d6101 | 68 | static int pack_to_stdout; |
c323ac7d | 69 | |
3f9ac8d2 JH |
70 | /* |
71 | * The object names in objects array are hashed with this hashtable, | |
72 | * to help looking up the entry by object name. Binary search from | |
73 | * sorted_by_sha is also possible but this was easier to code and faster. | |
74 | * This hashtable is built after all the objects are seen. | |
75 | */ | |
96f1e58f DR |
76 | static int *object_ix; |
77 | static int object_ix_hashsz; | |
3f9ac8d2 JH |
78 | |
79 | /* | |
80 | * Pack index for existing packs give us easy access to the offsets into | |
81 | * corresponding pack file where each object's data starts, but the entries | |
82 | * do not store the size of the compressed representation (uncompressed | |
83 | * size is easily available by examining the pack entry header). We build | |
84 | * a hashtable of existing packs (pack_revindex), and keep reverse index | |
85 | * here -- pack index file is sorted by object name mapping to offset; this | |
86 | * pack_revindex[].revindex array is an ordered list of offsets, so if you | |
87 | * know the offset of an object, next offset is where its packed | |
88 | * representation ends. | |
89 | */ | |
90 | struct pack_revindex { | |
91 | struct packed_git *p; | |
92 | unsigned long *revindex; | |
93 | } *pack_revindex = NULL; | |
96f1e58f | 94 | static int pack_revindex_hashsz; |
3f9ac8d2 JH |
95 | |
96 | /* | |
97 | * stats | |
98 | */ | |
96f1e58f DR |
99 | static int written; |
100 | static int written_delta; | |
101 | static int reused; | |
102 | static int reused_delta; | |
3f9ac8d2 JH |
103 | |
104 | static int pack_revindex_ix(struct packed_git *p) | |
105 | { | |
2b74cffa | 106 | unsigned long ui = (unsigned long)p; |
3f9ac8d2 JH |
107 | int i; |
108 | ||
109 | ui = ui ^ (ui >> 16); /* defeat structure alignment */ | |
110 | i = (int)(ui % pack_revindex_hashsz); | |
111 | while (pack_revindex[i].p) { | |
112 | if (pack_revindex[i].p == p) | |
113 | return i; | |
114 | if (++i == pack_revindex_hashsz) | |
115 | i = 0; | |
116 | } | |
117 | return -1 - i; | |
118 | } | |
119 | ||
120 | static void prepare_pack_ix(void) | |
121 | { | |
122 | int num; | |
123 | struct packed_git *p; | |
124 | for (num = 0, p = packed_git; p; p = p->next) | |
125 | num++; | |
126 | if (!num) | |
127 | return; | |
128 | pack_revindex_hashsz = num * 11; | |
129 | pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz); | |
130 | for (p = packed_git; p; p = p->next) { | |
131 | num = pack_revindex_ix(p); | |
132 | num = - 1 - num; | |
133 | pack_revindex[num].p = p; | |
134 | } | |
135 | /* revindex elements are lazily initialized */ | |
136 | } | |
137 | ||
138 | static int cmp_offset(const void *a_, const void *b_) | |
139 | { | |
140 | unsigned long a = *(unsigned long *) a_; | |
141 | unsigned long b = *(unsigned long *) b_; | |
142 | if (a < b) | |
143 | return -1; | |
144 | else if (a == b) | |
145 | return 0; | |
146 | else | |
147 | return 1; | |
148 | } | |
149 | ||
150 | /* | |
151 | * Ordered list of offsets of objects in the pack. | |
152 | */ | |
153 | static void prepare_pack_revindex(struct pack_revindex *rix) | |
154 | { | |
155 | struct packed_git *p = rix->p; | |
156 | int num_ent = num_packed_objects(p); | |
157 | int i; | |
158 | void *index = p->index_base + 256; | |
159 | ||
160 | rix->revindex = xmalloc(sizeof(unsigned long) * (num_ent + 1)); | |
161 | for (i = 0; i < num_ent; i++) { | |
1d7f171c | 162 | unsigned int hl = *((unsigned int *)((char *) index + 24*i)); |
3f9ac8d2 JH |
163 | rix->revindex[i] = ntohl(hl); |
164 | } | |
165 | /* This knows the pack format -- the 20-byte trailer | |
166 | * follows immediately after the last object data. | |
167 | */ | |
168 | rix->revindex[num_ent] = p->pack_size - 20; | |
169 | qsort(rix->revindex, num_ent, sizeof(unsigned long), cmp_offset); | |
170 | } | |
171 | ||
172 | static unsigned long find_packed_object_size(struct packed_git *p, | |
173 | unsigned long ofs) | |
174 | { | |
175 | int num; | |
176 | int lo, hi; | |
177 | struct pack_revindex *rix; | |
178 | unsigned long *revindex; | |
179 | num = pack_revindex_ix(p); | |
180 | if (num < 0) | |
181 | die("internal error: pack revindex uninitialized"); | |
182 | rix = &pack_revindex[num]; | |
183 | if (!rix->revindex) | |
184 | prepare_pack_revindex(rix); | |
185 | revindex = rix->revindex; | |
186 | lo = 0; | |
187 | hi = num_packed_objects(p) + 1; | |
188 | do { | |
189 | int mi = (lo + hi) / 2; | |
190 | if (revindex[mi] == ofs) { | |
191 | return revindex[mi+1] - ofs; | |
192 | } | |
193 | else if (ofs < revindex[mi]) | |
194 | hi = mi; | |
195 | else | |
196 | lo = mi + 1; | |
197 | } while (lo < hi); | |
198 | die("internal error: pack revindex corrupt"); | |
199 | } | |
200 | ||
c323ac7d LT |
201 | static void *delta_against(void *buf, unsigned long size, struct object_entry *entry) |
202 | { | |
203 | unsigned long othersize, delta_size; | |
204 | char type[10]; | |
205 | void *otherbuf = read_sha1_file(entry->delta->sha1, type, &othersize); | |
206 | void *delta_buf; | |
207 | ||
208 | if (!otherbuf) | |
209 | die("unable to read %s", sha1_to_hex(entry->delta->sha1)); | |
8ee378a0 | 210 | delta_buf = diff_delta(otherbuf, othersize, |
dcde55bc | 211 | buf, size, &delta_size, 0); |
75c42d8c | 212 | if (!delta_buf || delta_size != entry->delta_size) |
c323ac7d LT |
213 | die("delta size changed"); |
214 | free(buf); | |
215 | free(otherbuf); | |
216 | return delta_buf; | |
217 | } | |
218 | ||
a733cb60 LT |
219 | /* |
220 | * The per-object header is a pretty dense thing, which is | |
221 | * - first byte: low four bits are "size", then three bits of "type", | |
222 | * and the high bit is "size continues". | |
223 | * - each byte afterwards: low seven bits are size continuation, | |
224 | * with the high bit being "size continues" | |
225 | */ | |
226 | static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr) | |
227 | { | |
01247d87 | 228 | int n = 1; |
a733cb60 LT |
229 | unsigned char c; |
230 | ||
231 | if (type < OBJ_COMMIT || type > OBJ_DELTA) | |
232 | die("bad type %d", type); | |
233 | ||
01247d87 LT |
234 | c = (type << 4) | (size & 15); |
235 | size >>= 4; | |
236 | while (size) { | |
a733cb60 | 237 | *hdr++ = c | 0x80; |
01247d87 LT |
238 | c = size & 0x7f; |
239 | size >>= 7; | |
240 | n++; | |
a733cb60 LT |
241 | } |
242 | *hdr = c; | |
243 | return n; | |
244 | } | |
245 | ||
72518e9c | 246 | static int check_inflate(unsigned char *data, unsigned long len, unsigned long expect) |
df6d6101 | 247 | { |
72518e9c JH |
248 | z_stream stream; |
249 | unsigned char fakebuf[4096]; | |
250 | int st; | |
251 | ||
252 | memset(&stream, 0, sizeof(stream)); | |
253 | stream.next_in = data; | |
254 | stream.avail_in = len; | |
255 | stream.next_out = fakebuf; | |
256 | stream.avail_out = sizeof(fakebuf); | |
257 | inflateInit(&stream); | |
258 | ||
259 | while (1) { | |
260 | st = inflate(&stream, Z_FINISH); | |
261 | if (st == Z_STREAM_END || st == Z_OK) { | |
262 | st = (stream.total_out == expect && | |
263 | stream.total_in == len) ? 0 : -1; | |
264 | break; | |
265 | } | |
266 | if (st != Z_BUF_ERROR) { | |
267 | st = -1; | |
268 | break; | |
269 | } | |
270 | stream.next_out = fakebuf; | |
271 | stream.avail_out = sizeof(fakebuf); | |
272 | } | |
273 | inflateEnd(&stream); | |
274 | return st; | |
df6d6101 JH |
275 | } |
276 | ||
277 | /* | |
278 | * we are going to reuse the existing pack entry data. make | |
279 | * sure it is not corrupt. | |
280 | */ | |
72518e9c | 281 | static int revalidate_pack_entry(struct object_entry *entry, unsigned char *data, unsigned long len) |
df6d6101 | 282 | { |
72518e9c JH |
283 | enum object_type type; |
284 | unsigned long size, used; | |
df6d6101 JH |
285 | |
286 | if (pack_to_stdout) | |
287 | return 0; | |
288 | ||
72518e9c JH |
289 | /* the caller has already called use_packed_git() for us, |
290 | * so it is safe to access the pack data from mmapped location. | |
291 | * make sure the entry inflates correctly. | |
292 | */ | |
293 | used = unpack_object_header_gently(data, len, &type, &size); | |
294 | if (!used) | |
295 | return -1; | |
296 | if (type == OBJ_DELTA) | |
297 | used += 20; /* skip base object name */ | |
298 | data += used; | |
299 | len -= used; | |
300 | return check_inflate(data, len, entry->size); | |
df6d6101 JH |
301 | } |
302 | ||
303 | static int revalidate_loose_object(struct object_entry *entry, | |
304 | unsigned char *map, | |
305 | unsigned long mapsize) | |
306 | { | |
307 | /* we already know this is a loose object with new type header. */ | |
72518e9c JH |
308 | enum object_type type; |
309 | unsigned long size, used; | |
df6d6101 JH |
310 | |
311 | if (pack_to_stdout) | |
312 | return 0; | |
313 | ||
72518e9c JH |
314 | used = unpack_object_header_gently(map, mapsize, &type, &size); |
315 | if (!used) | |
316 | return -1; | |
317 | map += used; | |
318 | mapsize -= used; | |
319 | return check_inflate(map, mapsize, size); | |
df6d6101 JH |
320 | } |
321 | ||
7a979d99 JH |
322 | static unsigned long write_object(struct sha1file *f, |
323 | struct object_entry *entry) | |
c323ac7d LT |
324 | { |
325 | unsigned long size; | |
326 | char type[10]; | |
3f9ac8d2 | 327 | void *buf; |
a733cb60 | 328 | unsigned char header[10]; |
c323ac7d | 329 | unsigned hdrlen, datalen; |
a733cb60 | 330 | enum object_type obj_type; |
ab7cd7bb | 331 | int to_reuse = 0; |
c323ac7d | 332 | |
7a979d99 JH |
333 | if (entry->preferred_base) |
334 | return 0; | |
c323ac7d | 335 | |
a733cb60 | 336 | obj_type = entry->type; |
ab7cd7bb JH |
337 | if (! entry->in_pack) |
338 | to_reuse = 0; /* can't reuse what we don't have */ | |
339 | else if (obj_type == OBJ_DELTA) | |
340 | to_reuse = 1; /* check_object() decided it for us */ | |
341 | else if (obj_type != entry->in_pack_type) | |
342 | to_reuse = 0; /* pack has delta which is unusable */ | |
343 | else if (entry->delta) | |
344 | to_reuse = 0; /* we want to pack afresh */ | |
345 | else | |
346 | to_reuse = 1; /* we have it in-pack undeltified, | |
347 | * and we do not need to deltify it. | |
348 | */ | |
349 | ||
ceec1361 JH |
350 | if (!entry->in_pack && !entry->delta) { |
351 | unsigned char *map; | |
352 | unsigned long mapsize; | |
353 | map = map_sha1_file(entry->sha1, &mapsize); | |
354 | if (map && !legacy_loose_object(map)) { | |
355 | /* We can copy straight into the pack file */ | |
df6d6101 JH |
356 | if (revalidate_loose_object(entry, map, mapsize)) |
357 | die("corrupt loose object %s", | |
358 | sha1_to_hex(entry->sha1)); | |
ceec1361 JH |
359 | sha1write(f, map, mapsize); |
360 | munmap(map, mapsize); | |
361 | written++; | |
362 | reused++; | |
363 | return mapsize; | |
364 | } | |
365 | if (map) | |
366 | munmap(map, mapsize); | |
367 | } | |
368 | ||
df6d6101 | 369 | if (!to_reuse) { |
3f9ac8d2 JH |
370 | buf = read_sha1_file(entry->sha1, type, &size); |
371 | if (!buf) | |
372 | die("unable to read %s", sha1_to_hex(entry->sha1)); | |
373 | if (size != entry->size) | |
374 | die("object %s size inconsistency (%lu vs %lu)", | |
375 | sha1_to_hex(entry->sha1), size, entry->size); | |
376 | if (entry->delta) { | |
377 | buf = delta_against(buf, size, entry); | |
378 | size = entry->delta_size; | |
379 | obj_type = OBJ_DELTA; | |
380 | } | |
381 | /* | |
382 | * The object header is a byte of 'type' followed by zero or | |
383 | * more bytes of length. For deltas, the 20 bytes of delta | |
384 | * sha1 follows that. | |
385 | */ | |
386 | hdrlen = encode_header(obj_type, size, header); | |
387 | sha1write(f, header, hdrlen); | |
388 | ||
389 | if (entry->delta) { | |
390 | sha1write(f, entry->delta, 20); | |
391 | hdrlen += 20; | |
392 | } | |
393 | datalen = sha1write_compressed(f, buf, size); | |
394 | free(buf); | |
c323ac7d | 395 | } |
3f9ac8d2 JH |
396 | else { |
397 | struct packed_git *p = entry->in_pack; | |
398 | use_packed_git(p); | |
399 | ||
400 | datalen = find_packed_object_size(p, entry->in_pack_offset); | |
1d7f171c | 401 | buf = (char *) p->pack_base + entry->in_pack_offset; |
df6d6101 | 402 | |
72518e9c | 403 | if (revalidate_pack_entry(entry, buf, datalen)) |
df6d6101 | 404 | die("corrupt delta in pack %s", sha1_to_hex(entry->sha1)); |
3f9ac8d2 JH |
405 | sha1write(f, buf, datalen); |
406 | unuse_packed_git(p); | |
407 | hdrlen = 0; /* not really */ | |
ab7cd7bb JH |
408 | if (obj_type == OBJ_DELTA) |
409 | reused_delta++; | |
3f9ac8d2 | 410 | reused++; |
a733cb60 | 411 | } |
ab7cd7bb JH |
412 | if (obj_type == OBJ_DELTA) |
413 | written_delta++; | |
3f9ac8d2 | 414 | written++; |
c323ac7d LT |
415 | return hdrlen + datalen; |
416 | } | |
417 | ||
9d5ab962 JH |
418 | static unsigned long write_one(struct sha1file *f, |
419 | struct object_entry *e, | |
420 | unsigned long offset) | |
421 | { | |
422 | if (e->offset) | |
423 | /* offset starts from header size and cannot be zero | |
424 | * if it is written already. | |
425 | */ | |
426 | return offset; | |
427 | e->offset = offset; | |
428 | offset += write_object(f, e); | |
82f9d58a | 429 | /* if we are deltified, write out its base object. */ |
9d5ab962 JH |
430 | if (e->delta) |
431 | offset = write_one(f, e->delta, offset); | |
432 | return offset; | |
433 | } | |
434 | ||
c323ac7d LT |
435 | static void write_pack_file(void) |
436 | { | |
437 | int i; | |
d22b9290 | 438 | struct sha1file *f; |
a733cb60 | 439 | unsigned long offset; |
a733cb60 | 440 | struct pack_header hdr; |
183bdb2c JH |
441 | unsigned last_percent = 999; |
442 | int do_progress = 0; | |
c323ac7d | 443 | |
d22b9290 LT |
444 | if (!base_name) |
445 | f = sha1fd(1, "<stdout>"); | |
183bdb2c JH |
446 | else { |
447 | f = sha1create("%s-%s.%s", base_name, | |
448 | sha1_to_hex(object_list_sha1), "pack"); | |
449 | do_progress = progress; | |
450 | } | |
451 | if (do_progress) | |
f0b0af1b | 452 | fprintf(stderr, "Writing %d objects.\n", nr_result); |
183bdb2c | 453 | |
a733cb60 | 454 | hdr.hdr_signature = htonl(PACK_SIGNATURE); |
01247d87 | 455 | hdr.hdr_version = htonl(PACK_VERSION); |
7a979d99 | 456 | hdr.hdr_entries = htonl(nr_result); |
a733cb60 LT |
457 | sha1write(f, &hdr, sizeof(hdr)); |
458 | offset = sizeof(hdr); | |
f0b0af1b JH |
459 | if (!nr_result) |
460 | goto done; | |
5e8dc750 | 461 | for (i = 0; i < nr_objects; i++) { |
9d5ab962 | 462 | offset = write_one(f, objects + i, offset); |
183bdb2c | 463 | if (do_progress) { |
f0b0af1b | 464 | unsigned percent = written * 100 / nr_result; |
183bdb2c JH |
465 | if (progress_update || percent != last_percent) { |
466 | fprintf(stderr, "%4u%% (%u/%u) done\r", | |
f0b0af1b | 467 | percent, written, nr_result); |
183bdb2c JH |
468 | progress_update = 0; |
469 | last_percent = percent; | |
470 | } | |
5e8dc750 NP |
471 | } |
472 | } | |
183bdb2c JH |
473 | if (do_progress) |
474 | fputc('\n', stderr); | |
f0b0af1b | 475 | done: |
e1808845 | 476 | sha1close(f, pack_file_sha1, 1); |
c323ac7d LT |
477 | } |
478 | ||
479 | static void write_index_file(void) | |
480 | { | |
481 | int i; | |
7a979d99 JH |
482 | struct sha1file *f = sha1create("%s-%s.%s", base_name, |
483 | sha1_to_hex(object_list_sha1), "idx"); | |
c323ac7d | 484 | struct object_entry **list = sorted_by_sha; |
7a979d99 | 485 | struct object_entry **last = list + nr_result; |
c323ac7d LT |
486 | unsigned int array[256]; |
487 | ||
488 | /* | |
489 | * Write the first-level table (the list is sorted, | |
490 | * but we use a 256-entry lookup to be able to avoid | |
e1808845 | 491 | * having to do eight extra binary search iterations). |
c323ac7d LT |
492 | */ |
493 | for (i = 0; i < 256; i++) { | |
494 | struct object_entry **next = list; | |
495 | while (next < last) { | |
496 | struct object_entry *entry = *next; | |
497 | if (entry->sha1[0] != i) | |
498 | break; | |
499 | next++; | |
500 | } | |
501 | array[i] = htonl(next - sorted_by_sha); | |
502 | list = next; | |
503 | } | |
c38138cd | 504 | sha1write(f, array, 256 * sizeof(int)); |
c323ac7d LT |
505 | |
506 | /* | |
507 | * Write the actual SHA1 entries.. | |
508 | */ | |
509 | list = sorted_by_sha; | |
7a979d99 | 510 | for (i = 0; i < nr_result; i++) { |
c323ac7d LT |
511 | struct object_entry *entry = *list++; |
512 | unsigned int offset = htonl(entry->offset); | |
c38138cd LT |
513 | sha1write(f, &offset, 4); |
514 | sha1write(f, entry->sha1, 20); | |
c323ac7d | 515 | } |
e1808845 LT |
516 | sha1write(f, pack_file_sha1, 20); |
517 | sha1close(f, NULL, 1); | |
c323ac7d LT |
518 | } |
519 | ||
7a979d99 JH |
520 | static int locate_object_entry_hash(const unsigned char *sha1) |
521 | { | |
522 | int i; | |
523 | unsigned int ui; | |
524 | memcpy(&ui, sha1, sizeof(unsigned int)); | |
525 | i = ui % object_ix_hashsz; | |
526 | while (0 < object_ix[i]) { | |
a89fccd2 | 527 | if (!hashcmp(sha1, objects[object_ix[i] - 1].sha1)) |
7a979d99 JH |
528 | return i; |
529 | if (++i == object_ix_hashsz) | |
530 | i = 0; | |
531 | } | |
532 | return -1 - i; | |
533 | } | |
534 | ||
535 | static struct object_entry *locate_object_entry(const unsigned char *sha1) | |
536 | { | |
537 | int i; | |
538 | ||
539 | if (!object_ix_hashsz) | |
540 | return NULL; | |
541 | ||
542 | i = locate_object_entry_hash(sha1); | |
543 | if (0 <= i) | |
544 | return &objects[object_ix[i]-1]; | |
545 | return NULL; | |
546 | } | |
547 | ||
548 | static void rehash_objects(void) | |
c323ac7d | 549 | { |
7a979d99 JH |
550 | int i; |
551 | struct object_entry *oe; | |
552 | ||
553 | object_ix_hashsz = nr_objects * 3; | |
554 | if (object_ix_hashsz < 1024) | |
555 | object_ix_hashsz = 1024; | |
556 | object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz); | |
5379a5c5 | 557 | memset(object_ix, 0, sizeof(int) * object_ix_hashsz); |
7a979d99 JH |
558 | for (i = 0, oe = objects; i < nr_objects; i++, oe++) { |
559 | int ix = locate_object_entry_hash(oe->sha1); | |
560 | if (0 <= ix) | |
561 | continue; | |
562 | ix = -1 - ix; | |
563 | object_ix[ix] = i + 1; | |
564 | } | |
565 | } | |
566 | ||
ce0bd642 | 567 | static unsigned name_hash(const char *name) |
1d6b38cc | 568 | { |
ce0bd642 LT |
569 | unsigned char c; |
570 | unsigned hash = 0; | |
571 | ||
eeef7135 | 572 | /* |
ce0bd642 LT |
573 | * This effectively just creates a sortable number from the |
574 | * last sixteen non-whitespace characters. Last characters | |
575 | * count "most", so things that end in ".c" sort together. | |
eeef7135 | 576 | */ |
ce0bd642 LT |
577 | while ((c = *name++) != 0) { |
578 | if (isspace(c)) | |
579 | continue; | |
580 | hash = (hash >> 2) + (c << 24); | |
581 | } | |
1d6b38cc JH |
582 | return hash; |
583 | } | |
584 | ||
585 | static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclude) | |
c323ac7d LT |
586 | { |
587 | unsigned int idx = nr_objects; | |
588 | struct object_entry *entry; | |
3f9ac8d2 | 589 | struct packed_git *p; |
ab7cd7bb JH |
590 | unsigned int found_offset = 0; |
591 | struct packed_git *found_pack = NULL; | |
b925410d | 592 | int ix, status = 0; |
c323ac7d | 593 | |
7a979d99 | 594 | if (!exclude) { |
64560374 LT |
595 | for (p = packed_git; p; p = p->next) { |
596 | struct pack_entry e; | |
64560374 LT |
597 | if (find_pack_entry_one(sha1, &e, p)) { |
598 | if (incremental) | |
599 | return 0; | |
600 | if (local && !p->pack_local) | |
601 | return 0; | |
7a979d99 JH |
602 | if (!found_pack) { |
603 | found_offset = e.offset; | |
604 | found_pack = e.p; | |
605 | } | |
64560374 LT |
606 | } |
607 | } | |
608 | } | |
7a979d99 JH |
609 | if ((entry = locate_object_entry(sha1)) != NULL) |
610 | goto already_added; | |
eb019375 | 611 | |
c323ac7d LT |
612 | if (idx >= nr_alloc) { |
613 | unsigned int needed = (idx + 1024) * 3 / 2; | |
614 | objects = xrealloc(objects, needed * sizeof(*entry)); | |
615 | nr_alloc = needed; | |
616 | } | |
617 | entry = objects + idx; | |
7a979d99 | 618 | nr_objects = idx + 1; |
c323ac7d | 619 | memset(entry, 0, sizeof(*entry)); |
e702496e | 620 | hashcpy(entry->sha1, sha1); |
27225f2e | 621 | entry->hash = hash; |
7a979d99 JH |
622 | |
623 | if (object_ix_hashsz * 3 <= nr_objects * 4) | |
624 | rehash_objects(); | |
625 | else { | |
626 | ix = locate_object_entry_hash(entry->sha1); | |
627 | if (0 <= ix) | |
628 | die("internal error in object hashing."); | |
629 | object_ix[-1 - ix] = idx + 1; | |
3f9ac8d2 | 630 | } |
b925410d | 631 | status = 1; |
7a979d99 JH |
632 | |
633 | already_added: | |
f0b0af1b JH |
634 | if (progress_update) { |
635 | fprintf(stderr, "Counting objects...%d\r", nr_objects); | |
636 | progress_update = 0; | |
637 | } | |
7a979d99 JH |
638 | if (exclude) |
639 | entry->preferred_base = 1; | |
640 | else { | |
641 | if (found_pack) { | |
642 | entry->in_pack = found_pack; | |
643 | entry->in_pack_offset = found_offset; | |
644 | } | |
a49dd05f | 645 | } |
b925410d | 646 | return status; |
c323ac7d LT |
647 | } |
648 | ||
5379a5c5 JH |
649 | struct pbase_tree_cache { |
650 | unsigned char sha1[20]; | |
651 | int ref; | |
652 | int temporary; | |
653 | void *tree_data; | |
654 | unsigned long tree_size; | |
655 | }; | |
656 | ||
657 | static struct pbase_tree_cache *(pbase_tree_cache[256]); | |
658 | static int pbase_tree_cache_ix(const unsigned char *sha1) | |
659 | { | |
660 | return sha1[0] % ARRAY_SIZE(pbase_tree_cache); | |
661 | } | |
662 | static int pbase_tree_cache_ix_incr(int ix) | |
663 | { | |
664 | return (ix+1) % ARRAY_SIZE(pbase_tree_cache); | |
665 | } | |
666 | ||
667 | static struct pbase_tree { | |
668 | struct pbase_tree *next; | |
669 | /* This is a phony "cache" entry; we are not | |
670 | * going to evict it nor find it through _get() | |
671 | * mechanism -- this is for the toplevel node that | |
672 | * would almost always change with any commit. | |
673 | */ | |
674 | struct pbase_tree_cache pcache; | |
675 | } *pbase_tree; | |
676 | ||
677 | static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1) | |
678 | { | |
679 | struct pbase_tree_cache *ent, *nent; | |
680 | void *data; | |
681 | unsigned long size; | |
682 | char type[20]; | |
683 | int neigh; | |
684 | int my_ix = pbase_tree_cache_ix(sha1); | |
685 | int available_ix = -1; | |
686 | ||
687 | /* pbase-tree-cache acts as a limited hashtable. | |
688 | * your object will be found at your index or within a few | |
689 | * slots after that slot if it is cached. | |
690 | */ | |
691 | for (neigh = 0; neigh < 8; neigh++) { | |
692 | ent = pbase_tree_cache[my_ix]; | |
a89fccd2 | 693 | if (ent && !hashcmp(ent->sha1, sha1)) { |
5379a5c5 JH |
694 | ent->ref++; |
695 | return ent; | |
696 | } | |
697 | else if (((available_ix < 0) && (!ent || !ent->ref)) || | |
698 | ((0 <= available_ix) && | |
699 | (!ent && pbase_tree_cache[available_ix]))) | |
700 | available_ix = my_ix; | |
701 | if (!ent) | |
702 | break; | |
703 | my_ix = pbase_tree_cache_ix_incr(my_ix); | |
704 | } | |
705 | ||
706 | /* Did not find one. Either we got a bogus request or | |
707 | * we need to read and perhaps cache. | |
708 | */ | |
709 | data = read_sha1_file(sha1, type, &size); | |
710 | if (!data) | |
711 | return NULL; | |
712 | if (strcmp(type, tree_type)) { | |
713 | free(data); | |
714 | return NULL; | |
715 | } | |
716 | ||
717 | /* We need to either cache or return a throwaway copy */ | |
718 | ||
719 | if (available_ix < 0) | |
720 | ent = NULL; | |
721 | else { | |
722 | ent = pbase_tree_cache[available_ix]; | |
723 | my_ix = available_ix; | |
724 | } | |
725 | ||
726 | if (!ent) { | |
727 | nent = xmalloc(sizeof(*nent)); | |
728 | nent->temporary = (available_ix < 0); | |
729 | } | |
730 | else { | |
731 | /* evict and reuse */ | |
732 | free(ent->tree_data); | |
733 | nent = ent; | |
734 | } | |
e702496e | 735 | hashcpy(nent->sha1, sha1); |
5379a5c5 JH |
736 | nent->tree_data = data; |
737 | nent->tree_size = size; | |
738 | nent->ref = 1; | |
739 | if (!nent->temporary) | |
740 | pbase_tree_cache[my_ix] = nent; | |
741 | return nent; | |
742 | } | |
743 | ||
744 | static void pbase_tree_put(struct pbase_tree_cache *cache) | |
745 | { | |
746 | if (!cache->temporary) { | |
747 | cache->ref--; | |
748 | return; | |
749 | } | |
750 | free(cache->tree_data); | |
751 | free(cache); | |
752 | } | |
753 | ||
754 | static int name_cmp_len(const char *name) | |
755 | { | |
756 | int i; | |
757 | for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++) | |
758 | ; | |
759 | return i; | |
760 | } | |
761 | ||
762 | static void add_pbase_object(struct tree_desc *tree, | |
5379a5c5 | 763 | const char *name, |
ce0bd642 LT |
764 | int cmplen, |
765 | const char *fullname) | |
3f9ac8d2 | 766 | { |
4c068a98 LT |
767 | struct name_entry entry; |
768 | ||
769 | while (tree_entry(tree,&entry)) { | |
7a979d99 JH |
770 | unsigned long size; |
771 | char type[20]; | |
772 | ||
4c068a98 LT |
773 | if (entry.pathlen != cmplen || |
774 | memcmp(entry.path, name, cmplen) || | |
775 | !has_sha1_file(entry.sha1) || | |
776 | sha1_object_info(entry.sha1, type, &size)) | |
7a979d99 | 777 | continue; |
5379a5c5 | 778 | if (name[cmplen] != '/') { |
ce0bd642 | 779 | unsigned hash = name_hash(fullname); |
4c068a98 | 780 | add_object_entry(entry.sha1, hash, 1); |
5379a5c5 JH |
781 | return; |
782 | } | |
8e440259 | 783 | if (!strcmp(type, tree_type)) { |
7a979d99 | 784 | struct tree_desc sub; |
5379a5c5 JH |
785 | struct pbase_tree_cache *tree; |
786 | const char *down = name+cmplen+1; | |
787 | int downlen = name_cmp_len(down); | |
788 | ||
4c068a98 | 789 | tree = pbase_tree_get(entry.sha1); |
5379a5c5 JH |
790 | if (!tree) |
791 | return; | |
792 | sub.buf = tree->tree_data; | |
793 | sub.size = tree->tree_size; | |
794 | ||
ce0bd642 | 795 | add_pbase_object(&sub, down, downlen, fullname); |
5379a5c5 JH |
796 | pbase_tree_put(tree); |
797 | } | |
798 | } | |
799 | } | |
1d6b38cc | 800 | |
5379a5c5 JH |
801 | static unsigned *done_pbase_paths; |
802 | static int done_pbase_paths_num; | |
803 | static int done_pbase_paths_alloc; | |
804 | static int done_pbase_path_pos(unsigned hash) | |
805 | { | |
806 | int lo = 0; | |
807 | int hi = done_pbase_paths_num; | |
808 | while (lo < hi) { | |
809 | int mi = (hi + lo) / 2; | |
810 | if (done_pbase_paths[mi] == hash) | |
811 | return mi; | |
812 | if (done_pbase_paths[mi] < hash) | |
813 | hi = mi; | |
814 | else | |
815 | lo = mi + 1; | |
816 | } | |
817 | return -lo-1; | |
818 | } | |
819 | ||
820 | static int check_pbase_path(unsigned hash) | |
821 | { | |
822 | int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash); | |
823 | if (0 <= pos) | |
824 | return 1; | |
825 | pos = -pos - 1; | |
826 | if (done_pbase_paths_alloc <= done_pbase_paths_num) { | |
827 | done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc); | |
828 | done_pbase_paths = xrealloc(done_pbase_paths, | |
829 | done_pbase_paths_alloc * | |
830 | sizeof(unsigned)); | |
831 | } | |
832 | done_pbase_paths_num++; | |
833 | if (pos < done_pbase_paths_num) | |
834 | memmove(done_pbase_paths + pos + 1, | |
835 | done_pbase_paths + pos, | |
836 | (done_pbase_paths_num - pos - 1) * sizeof(unsigned)); | |
837 | done_pbase_paths[pos] = hash; | |
838 | return 0; | |
839 | } | |
840 | ||
841 | static void add_preferred_base_object(char *name, unsigned hash) | |
842 | { | |
843 | struct pbase_tree *it; | |
844 | int cmplen = name_cmp_len(name); | |
845 | ||
846 | if (check_pbase_path(hash)) | |
847 | return; | |
848 | ||
849 | for (it = pbase_tree; it; it = it->next) { | |
850 | if (cmplen == 0) { | |
ce0bd642 | 851 | hash = name_hash(""); |
5379a5c5 JH |
852 | add_object_entry(it->pcache.sha1, hash, 1); |
853 | } | |
854 | else { | |
855 | struct tree_desc tree; | |
856 | tree.buf = it->pcache.tree_data; | |
857 | tree.size = it->pcache.tree_size; | |
ce0bd642 | 858 | add_pbase_object(&tree, name, cmplen, name); |
7a979d99 | 859 | } |
3f9ac8d2 | 860 | } |
3f9ac8d2 JH |
861 | } |
862 | ||
7a979d99 | 863 | static void add_preferred_base(unsigned char *sha1) |
3f9ac8d2 | 864 | { |
5379a5c5 JH |
865 | struct pbase_tree *it; |
866 | void *data; | |
867 | unsigned long size; | |
868 | unsigned char tree_sha1[20]; | |
1d6b38cc | 869 | |
5379a5c5 JH |
870 | data = read_object_with_reference(sha1, tree_type, &size, tree_sha1); |
871 | if (!data) | |
7a979d99 | 872 | return; |
5379a5c5 JH |
873 | |
874 | for (it = pbase_tree; it; it = it->next) { | |
a89fccd2 | 875 | if (!hashcmp(it->pcache.sha1, tree_sha1)) { |
5379a5c5 JH |
876 | free(data); |
877 | return; | |
878 | } | |
879 | } | |
880 | ||
881 | it = xcalloc(1, sizeof(*it)); | |
882 | it->next = pbase_tree; | |
883 | pbase_tree = it; | |
884 | ||
e702496e | 885 | hashcpy(it->pcache.sha1, tree_sha1); |
5379a5c5 JH |
886 | it->pcache.tree_data = data; |
887 | it->pcache.tree_size = size; | |
3f9ac8d2 JH |
888 | } |
889 | ||
c323ac7d LT |
890 | static void check_object(struct object_entry *entry) |
891 | { | |
36e4d74a JH |
892 | char type[20]; |
893 | ||
7a979d99 | 894 | if (entry->in_pack && !entry->preferred_base) { |
ab7cd7bb JH |
895 | unsigned char base[20]; |
896 | unsigned long size; | |
897 | struct object_entry *base_entry; | |
898 | ||
899 | /* We want in_pack_type even if we do not reuse delta. | |
900 | * There is no point not reusing non-delta representations. | |
901 | */ | |
902 | check_reuse_pack_delta(entry->in_pack, | |
903 | entry->in_pack_offset, | |
904 | base, &size, | |
905 | &entry->in_pack_type); | |
906 | ||
3f9ac8d2 JH |
907 | /* Check if it is delta, and the base is also an object |
908 | * we are going to pack. If so we will reuse the existing | |
909 | * delta. | |
910 | */ | |
ab7cd7bb JH |
911 | if (!no_reuse_delta && |
912 | entry->in_pack_type == OBJ_DELTA && | |
7a979d99 JH |
913 | (base_entry = locate_object_entry(base)) && |
914 | (!base_entry->preferred_base)) { | |
ab7cd7bb JH |
915 | |
916 | /* Depth value does not matter - find_deltas() | |
917 | * will never consider reused delta as the | |
918 | * base object to deltify other objects | |
919 | * against, in order to avoid circular deltas. | |
3f9ac8d2 | 920 | */ |
ab7cd7bb JH |
921 | |
922 | /* uncompressed size of the delta data */ | |
3f9ac8d2 JH |
923 | entry->size = entry->delta_size = size; |
924 | entry->delta = base_entry; | |
925 | entry->type = OBJ_DELTA; | |
ab7cd7bb | 926 | |
15b4d577 JH |
927 | entry->delta_sibling = base_entry->delta_child; |
928 | base_entry->delta_child = entry; | |
ab7cd7bb | 929 | |
3f9ac8d2 JH |
930 | return; |
931 | } | |
932 | /* Otherwise we would do the usual */ | |
36e4d74a | 933 | } |
3f9ac8d2 JH |
934 | |
935 | if (sha1_object_info(entry->sha1, type, &entry->size)) | |
36e4d74a JH |
936 | die("unable to get type of object %s", |
937 | sha1_to_hex(entry->sha1)); | |
3f9ac8d2 | 938 | |
8e440259 | 939 | if (!strcmp(type, commit_type)) { |
3f9ac8d2 | 940 | entry->type = OBJ_COMMIT; |
8e440259 | 941 | } else if (!strcmp(type, tree_type)) { |
3f9ac8d2 | 942 | entry->type = OBJ_TREE; |
8e440259 | 943 | } else if (!strcmp(type, blob_type)) { |
3f9ac8d2 | 944 | entry->type = OBJ_BLOB; |
8e440259 | 945 | } else if (!strcmp(type, tag_type)) { |
3f9ac8d2 JH |
946 | entry->type = OBJ_TAG; |
947 | } else | |
948 | die("unable to pack object %s of type %s", | |
949 | sha1_to_hex(entry->sha1), type); | |
950 | } | |
951 | ||
15b4d577 JH |
952 | static unsigned int check_delta_limit(struct object_entry *me, unsigned int n) |
953 | { | |
954 | struct object_entry *child = me->delta_child; | |
955 | unsigned int m = n; | |
956 | while (child) { | |
957 | unsigned int c = check_delta_limit(child, n + 1); | |
958 | if (m < c) | |
959 | m = c; | |
960 | child = child->delta_sibling; | |
961 | } | |
962 | return m; | |
963 | } | |
964 | ||
c323ac7d LT |
965 | static void get_object_details(void) |
966 | { | |
967 | int i; | |
15b4d577 | 968 | struct object_entry *entry; |
c323ac7d | 969 | |
3f9ac8d2 | 970 | prepare_pack_ix(); |
15b4d577 JH |
971 | for (i = 0, entry = objects; i < nr_objects; i++, entry++) |
972 | check_object(entry); | |
b76f6b62 JH |
973 | |
974 | if (nr_objects == nr_result) { | |
975 | /* | |
976 | * Depth of objects that depend on the entry -- this | |
977 | * is subtracted from depth-max to break too deep | |
978 | * delta chain because of delta data reusing. | |
979 | * However, we loosen this restriction when we know we | |
980 | * are creating a thin pack -- it will have to be | |
981 | * expanded on the other end anyway, so do not | |
982 | * artificially cut the delta chain and let it go as | |
983 | * deep as it wants. | |
984 | */ | |
985 | for (i = 0, entry = objects; i < nr_objects; i++, entry++) | |
986 | if (!entry->delta && entry->delta_child) | |
987 | entry->delta_limit = | |
988 | check_delta_limit(entry, 1); | |
989 | } | |
c323ac7d LT |
990 | } |
991 | ||
992 | typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *); | |
993 | ||
994 | static entry_sort_t current_sort; | |
995 | ||
996 | static int sort_comparator(const void *_a, const void *_b) | |
997 | { | |
998 | struct object_entry *a = *(struct object_entry **)_a; | |
999 | struct object_entry *b = *(struct object_entry **)_b; | |
1000 | return current_sort(a,b); | |
1001 | } | |
1002 | ||
1003 | static struct object_entry **create_sorted_list(entry_sort_t sort) | |
1004 | { | |
1005 | struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *)); | |
1006 | int i; | |
1007 | ||
1008 | for (i = 0; i < nr_objects; i++) | |
1009 | list[i] = objects + i; | |
1010 | current_sort = sort; | |
1011 | qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator); | |
1012 | return list; | |
1013 | } | |
1014 | ||
1015 | static int sha1_sort(const struct object_entry *a, const struct object_entry *b) | |
1016 | { | |
a89fccd2 | 1017 | return hashcmp(a->sha1, b->sha1); |
c323ac7d LT |
1018 | } |
1019 | ||
962554c6 | 1020 | static struct object_entry **create_final_object_list(void) |
7a979d99 JH |
1021 | { |
1022 | struct object_entry **list; | |
1023 | int i, j; | |
1024 | ||
1025 | for (i = nr_result = 0; i < nr_objects; i++) | |
1026 | if (!objects[i].preferred_base) | |
1027 | nr_result++; | |
1028 | list = xmalloc(nr_result * sizeof(struct object_entry *)); | |
1029 | for (i = j = 0; i < nr_objects; i++) { | |
1030 | if (!objects[i].preferred_base) | |
1031 | list[j++] = objects + i; | |
1032 | } | |
1033 | current_sort = sha1_sort; | |
1034 | qsort(list, nr_result, sizeof(struct object_entry *), sort_comparator); | |
1035 | return list; | |
1036 | } | |
1037 | ||
c323ac7d LT |
1038 | static int type_size_sort(const struct object_entry *a, const struct object_entry *b) |
1039 | { | |
1040 | if (a->type < b->type) | |
1041 | return -1; | |
1042 | if (a->type > b->type) | |
1043 | return 1; | |
27225f2e LT |
1044 | if (a->hash < b->hash) |
1045 | return -1; | |
1046 | if (a->hash > b->hash) | |
1047 | return 1; | |
7a979d99 JH |
1048 | if (a->preferred_base < b->preferred_base) |
1049 | return -1; | |
1050 | if (a->preferred_base > b->preferred_base) | |
1051 | return 1; | |
c323ac7d LT |
1052 | if (a->size < b->size) |
1053 | return -1; | |
1054 | if (a->size > b->size) | |
1055 | return 1; | |
1056 | return a < b ? -1 : (a > b); | |
1057 | } | |
1058 | ||
1059 | struct unpacked { | |
1060 | struct object_entry *entry; | |
1061 | void *data; | |
f6c7081a | 1062 | struct delta_index *index; |
c323ac7d LT |
1063 | }; |
1064 | ||
1065 | /* | |
521a4f4c LT |
1066 | * We search for deltas _backwards_ in a list sorted by type and |
1067 | * by size, so that we see progressively smaller and smaller files. | |
1068 | * That's because we prefer deltas to be from the bigger file | |
1069 | * to the smaller - deletes are potentially cheaper, but perhaps | |
1070 | * more importantly, the bigger file is likely the more recent | |
1071 | * one. | |
c323ac7d | 1072 | */ |
f6c7081a | 1073 | static int try_delta(struct unpacked *trg, struct unpacked *src, |
560b25a8 | 1074 | unsigned max_depth) |
c323ac7d | 1075 | { |
f6c7081a NP |
1076 | struct object_entry *trg_entry = trg->entry; |
1077 | struct object_entry *src_entry = src->entry; | |
560b25a8 NP |
1078 | unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz; |
1079 | char type[10]; | |
c323ac7d LT |
1080 | void *delta_buf; |
1081 | ||
1082 | /* Don't bother doing diffs between different types */ | |
f6c7081a | 1083 | if (trg_entry->type != src_entry->type) |
c323ac7d LT |
1084 | return -1; |
1085 | ||
7a979d99 JH |
1086 | /* We do not compute delta to *create* objects we are not |
1087 | * going to pack. | |
1088 | */ | |
f6c7081a | 1089 | if (trg_entry->preferred_base) |
75c42d8c | 1090 | return -1; |
7a979d99 | 1091 | |
51d1e83f LT |
1092 | /* |
1093 | * We do not bother to try a delta that we discarded | |
8dbbd14e | 1094 | * on an earlier try, but only when reusing delta data. |
51d1e83f | 1095 | */ |
8dbbd14e NP |
1096 | if (!no_reuse_delta && trg_entry->in_pack && |
1097 | trg_entry->in_pack == src_entry->in_pack) | |
51d1e83f LT |
1098 | return 0; |
1099 | ||
f6c7081a NP |
1100 | /* |
1101 | * If the current object is at pack edge, take the depth the | |
7a979d99 JH |
1102 | * objects that depend on the current object into account -- |
1103 | * otherwise they would become too deep. | |
ab7cd7bb | 1104 | */ |
f6c7081a NP |
1105 | if (trg_entry->delta_child) { |
1106 | if (max_depth <= trg_entry->delta_limit) | |
15b4d577 | 1107 | return 0; |
f6c7081a | 1108 | max_depth -= trg_entry->delta_limit; |
15b4d577 | 1109 | } |
f6c7081a | 1110 | if (src_entry->depth >= max_depth) |
d116a45a | 1111 | return 0; |
c323ac7d | 1112 | |
c3b06a69 | 1113 | /* Now some size filtering heuristics. */ |
560b25a8 NP |
1114 | trg_size = trg_entry->size; |
1115 | max_size = trg_size/2 - 20; | |
c3b06a69 NP |
1116 | max_size = max_size * (max_depth - src_entry->depth) / max_depth; |
1117 | if (max_size == 0) | |
1118 | return 0; | |
4e8da195 | 1119 | if (trg_entry->delta && trg_entry->delta_size <= max_size) |
f6c7081a NP |
1120 | max_size = trg_entry->delta_size-1; |
1121 | src_size = src_entry->size; | |
560b25a8 | 1122 | sizediff = src_size < trg_size ? trg_size - src_size : 0; |
27225f2e | 1123 | if (sizediff >= max_size) |
f527cb8c | 1124 | return 0; |
f6c7081a | 1125 | |
560b25a8 NP |
1126 | /* Load data if not already done */ |
1127 | if (!trg->data) { | |
1128 | trg->data = read_sha1_file(trg_entry->sha1, type, &sz); | |
1129 | if (sz != trg_size) | |
1130 | die("object %s inconsistent object length (%lu vs %lu)", | |
1131 | sha1_to_hex(trg_entry->sha1), sz, trg_size); | |
1132 | } | |
1133 | if (!src->data) { | |
1134 | src->data = read_sha1_file(src_entry->sha1, type, &sz); | |
1135 | if (sz != src_size) | |
1136 | die("object %s inconsistent object length (%lu vs %lu)", | |
1137 | sha1_to_hex(src_entry->sha1), sz, src_size); | |
1138 | } | |
1139 | if (!src->index) { | |
1140 | src->index = create_delta_index(src->data, src_size); | |
1141 | if (!src->index) | |
1142 | die("out of memory"); | |
1143 | } | |
1144 | ||
1145 | delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size); | |
c323ac7d | 1146 | if (!delta_buf) |
75c42d8c | 1147 | return 0; |
f6c7081a NP |
1148 | |
1149 | trg_entry->delta = src_entry; | |
1150 | trg_entry->delta_size = delta_size; | |
1151 | trg_entry->depth = src_entry->depth + 1; | |
c323ac7d | 1152 | free(delta_buf); |
f6c7081a | 1153 | return 1; |
c323ac7d LT |
1154 | } |
1155 | ||
b2504a0d NP |
1156 | static void progress_interval(int signum) |
1157 | { | |
b2504a0d NP |
1158 | progress_update = 1; |
1159 | } | |
1160 | ||
d116a45a | 1161 | static void find_deltas(struct object_entry **list, int window, int depth) |
c323ac7d | 1162 | { |
521a4f4c | 1163 | int i, idx; |
c323ac7d LT |
1164 | unsigned int array_size = window * sizeof(struct unpacked); |
1165 | struct unpacked *array = xmalloc(array_size); | |
183bdb2c JH |
1166 | unsigned processed = 0; |
1167 | unsigned last_percent = 999; | |
c323ac7d LT |
1168 | |
1169 | memset(array, 0, array_size); | |
521a4f4c LT |
1170 | i = nr_objects; |
1171 | idx = 0; | |
183bdb2c | 1172 | if (progress) |
f0b0af1b | 1173 | fprintf(stderr, "Deltifying %d objects.\n", nr_result); |
21fcd1bd | 1174 | |
521a4f4c | 1175 | while (--i >= 0) { |
c323ac7d LT |
1176 | struct object_entry *entry = list[i]; |
1177 | struct unpacked *n = array + idx; | |
c323ac7d LT |
1178 | int j; |
1179 | ||
f0b0af1b JH |
1180 | if (!entry->preferred_base) |
1181 | processed++; | |
1182 | ||
183bdb2c | 1183 | if (progress) { |
f0b0af1b | 1184 | unsigned percent = processed * 100 / nr_result; |
183bdb2c JH |
1185 | if (percent != last_percent || progress_update) { |
1186 | fprintf(stderr, "%4u%% (%u/%u) done\r", | |
f0b0af1b | 1187 | percent, processed, nr_result); |
183bdb2c JH |
1188 | progress_update = 0; |
1189 | last_percent = percent; | |
1190 | } | |
21fcd1bd | 1191 | } |
3f9ac8d2 JH |
1192 | |
1193 | if (entry->delta) | |
1194 | /* This happens if we decided to reuse existing | |
ab7cd7bb | 1195 | * delta from a pack. "!no_reuse_delta &&" is implied. |
3f9ac8d2 JH |
1196 | */ |
1197 | continue; | |
1198 | ||
9a8b6a0a JH |
1199 | if (entry->size < 50) |
1200 | continue; | |
ff45715c NP |
1201 | free_delta_index(n->index); |
1202 | n->index = NULL; | |
c323ac7d | 1203 | free(n->data); |
560b25a8 | 1204 | n->data = NULL; |
c323ac7d | 1205 | n->entry = entry; |
ab7cd7bb | 1206 | |
78817c15 LT |
1207 | j = window; |
1208 | while (--j > 0) { | |
1209 | unsigned int other_idx = idx + j; | |
c323ac7d | 1210 | struct unpacked *m; |
78817c15 LT |
1211 | if (other_idx >= window) |
1212 | other_idx -= window; | |
c323ac7d LT |
1213 | m = array + other_idx; |
1214 | if (!m->entry) | |
1215 | break; | |
560b25a8 | 1216 | if (try_delta(n, m, depth) < 0) |
c323ac7d LT |
1217 | break; |
1218 | } | |
70ca1a3f JH |
1219 | /* if we made n a delta, and if n is already at max |
1220 | * depth, leaving it in the window is pointless. we | |
1221 | * should evict it first. | |
70ca1a3f JH |
1222 | */ |
1223 | if (entry->delta && depth <= entry->depth) | |
1224 | continue; | |
ff45715c | 1225 | |
521a4f4c LT |
1226 | idx++; |
1227 | if (idx >= window) | |
1228 | idx = 0; | |
c323ac7d | 1229 | } |
adee7bdf | 1230 | |
b2504a0d NP |
1231 | if (progress) |
1232 | fputc('\n', stderr); | |
1233 | ||
f6c7081a | 1234 | for (i = 0; i < window; ++i) { |
ff45715c | 1235 | free_delta_index(array[i].index); |
adee7bdf | 1236 | free(array[i].data); |
f6c7081a | 1237 | } |
adee7bdf | 1238 | free(array); |
c323ac7d LT |
1239 | } |
1240 | ||
f3123c4a JH |
1241 | static void prepare_pack(int window, int depth) |
1242 | { | |
3f9ac8d2 | 1243 | get_object_details(); |
f3123c4a JH |
1244 | sorted_by_type = create_sorted_list(type_size_sort); |
1245 | if (window && depth) | |
1246 | find_deltas(sorted_by_type, window+1, depth); | |
f3123c4a JH |
1247 | } |
1248 | ||
df6d6101 | 1249 | static int reuse_cached_pack(unsigned char *sha1) |
f3123c4a JH |
1250 | { |
1251 | static const char cache[] = "pack-cache/pack-%s.%s"; | |
1252 | char *cached_pack, *cached_idx; | |
1253 | int ifd, ofd, ifd_ix = -1; | |
1254 | ||
1255 | cached_pack = git_path(cache, sha1_to_hex(sha1), "pack"); | |
1256 | ifd = open(cached_pack, O_RDONLY); | |
1257 | if (ifd < 0) | |
1258 | return 0; | |
1259 | ||
1260 | if (!pack_to_stdout) { | |
1261 | cached_idx = git_path(cache, sha1_to_hex(sha1), "idx"); | |
1262 | ifd_ix = open(cached_idx, O_RDONLY); | |
1263 | if (ifd_ix < 0) { | |
1264 | close(ifd); | |
1265 | return 0; | |
1266 | } | |
1267 | } | |
1268 | ||
ab7cd7bb JH |
1269 | if (progress) |
1270 | fprintf(stderr, "Reusing %d objects pack %s\n", nr_objects, | |
1271 | sha1_to_hex(sha1)); | |
f3123c4a JH |
1272 | |
1273 | if (pack_to_stdout) { | |
1274 | if (copy_fd(ifd, 1)) | |
1275 | exit(1); | |
1276 | close(ifd); | |
1277 | } | |
1278 | else { | |
1279 | char name[PATH_MAX]; | |
1280 | snprintf(name, sizeof(name), | |
1281 | "%s-%s.%s", base_name, sha1_to_hex(sha1), "pack"); | |
1282 | ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666); | |
1283 | if (ofd < 0) | |
1284 | die("unable to open %s (%s)", name, strerror(errno)); | |
1285 | if (copy_fd(ifd, ofd)) | |
1286 | exit(1); | |
1287 | close(ifd); | |
1288 | ||
1289 | snprintf(name, sizeof(name), | |
1290 | "%s-%s.%s", base_name, sha1_to_hex(sha1), "idx"); | |
1291 | ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666); | |
1292 | if (ofd < 0) | |
1293 | die("unable to open %s (%s)", name, strerror(errno)); | |
1294 | if (copy_fd(ifd_ix, ofd)) | |
1295 | exit(1); | |
1296 | close(ifd_ix); | |
1297 | puts(sha1_to_hex(sha1)); | |
1298 | } | |
1299 | ||
1300 | return 1; | |
1301 | } | |
1302 | ||
fb7a6531 LT |
1303 | static void setup_progress_signal(void) |
1304 | { | |
1305 | struct sigaction sa; | |
1306 | struct itimerval v; | |
1307 | ||
1308 | memset(&sa, 0, sizeof(sa)); | |
1309 | sa.sa_handler = progress_interval; | |
1310 | sigemptyset(&sa.sa_mask); | |
1311 | sa.sa_flags = SA_RESTART; | |
1312 | sigaction(SIGALRM, &sa, NULL); | |
1313 | ||
1314 | v.it_interval.tv_sec = 1; | |
1315 | v.it_interval.tv_usec = 0; | |
1316 | v.it_value = v.it_interval; | |
1317 | setitimer(ITIMER_REAL, &v, NULL); | |
1318 | } | |
1319 | ||
4812a93a JK |
1320 | static int git_pack_config(const char *k, const char *v) |
1321 | { | |
1322 | if(!strcmp(k, "pack.window")) { | |
1323 | window = git_config_int(k, v); | |
1324 | return 0; | |
1325 | } | |
1326 | return git_default_config(k, v); | |
1327 | } | |
1328 | ||
5d4a6003 | 1329 | int cmd_pack_objects(int argc, const char **argv, const char *prefix) |
c323ac7d | 1330 | { |
5f3de58f | 1331 | SHA_CTX ctx; |
0dec30b9 | 1332 | char line[40 + 1 + PATH_MAX + 2]; |
df6d6101 | 1333 | int depth = 10; |
84c8d8ae | 1334 | struct object_entry **list; |
5379a5c5 | 1335 | int num_preferred_base = 0; |
c323ac7d LT |
1336 | int i; |
1337 | ||
4812a93a | 1338 | git_config(git_pack_config); |
53228a5f | 1339 | |
86118bcb | 1340 | progress = isatty(2); |
c323ac7d LT |
1341 | for (i = 1; i < argc; i++) { |
1342 | const char *arg = argv[i]; | |
1343 | ||
1344 | if (*arg == '-') { | |
1c4a2912 LT |
1345 | if (!strcmp("--non-empty", arg)) { |
1346 | non_empty = 1; | |
1347 | continue; | |
1348 | } | |
64560374 LT |
1349 | if (!strcmp("--local", arg)) { |
1350 | local = 1; | |
1351 | continue; | |
1352 | } | |
363b7817 JH |
1353 | if (!strcmp("--progress", arg)) { |
1354 | progress = 1; | |
1355 | continue; | |
1356 | } | |
eb019375 LT |
1357 | if (!strcmp("--incremental", arg)) { |
1358 | incremental = 1; | |
1359 | continue; | |
1360 | } | |
c323ac7d LT |
1361 | if (!strncmp("--window=", arg, 9)) { |
1362 | char *end; | |
f846bbff | 1363 | window = strtoul(arg+9, &end, 0); |
c323ac7d LT |
1364 | if (!arg[9] || *end) |
1365 | usage(pack_usage); | |
1366 | continue; | |
1367 | } | |
d116a45a LT |
1368 | if (!strncmp("--depth=", arg, 8)) { |
1369 | char *end; | |
1370 | depth = strtoul(arg+8, &end, 0); | |
1371 | if (!arg[8] || *end) | |
1372 | usage(pack_usage); | |
1373 | continue; | |
1374 | } | |
86118bcb JH |
1375 | if (!strcmp("--progress", arg)) { |
1376 | progress = 1; | |
1377 | continue; | |
1378 | } | |
024701f1 JH |
1379 | if (!strcmp("-q", arg)) { |
1380 | progress = 0; | |
1381 | continue; | |
1382 | } | |
ab7cd7bb JH |
1383 | if (!strcmp("--no-reuse-delta", arg)) { |
1384 | no_reuse_delta = 1; | |
1385 | continue; | |
1386 | } | |
d22b9290 LT |
1387 | if (!strcmp("--stdout", arg)) { |
1388 | pack_to_stdout = 1; | |
1389 | continue; | |
1390 | } | |
c323ac7d LT |
1391 | usage(pack_usage); |
1392 | } | |
1393 | if (base_name) | |
1394 | usage(pack_usage); | |
1395 | base_name = arg; | |
1396 | } | |
1397 | ||
d22b9290 | 1398 | if (pack_to_stdout != !base_name) |
c323ac7d LT |
1399 | usage(pack_usage); |
1400 | ||
64560374 | 1401 | prepare_packed_git(); |
b2504a0d | 1402 | |
21fcd1bd JH |
1403 | if (progress) { |
1404 | fprintf(stderr, "Generating pack...\n"); | |
fb7a6531 | 1405 | setup_progress_signal(); |
21fcd1bd | 1406 | } |
b2504a0d | 1407 | |
da93d12b | 1408 | for (;;) { |
c323ac7d | 1409 | unsigned char sha1[20]; |
5379a5c5 | 1410 | unsigned hash; |
27225f2e | 1411 | |
da93d12b LT |
1412 | if (!fgets(line, sizeof(line), stdin)) { |
1413 | if (feof(stdin)) | |
1414 | break; | |
1415 | if (!ferror(stdin)) | |
1416 | die("fgets returned NULL, not EOF, not error!"); | |
687dd75c JH |
1417 | if (errno != EINTR) |
1418 | die("fgets: %s", strerror(errno)); | |
1419 | clearerr(stdin); | |
1420 | continue; | |
da93d12b LT |
1421 | } |
1422 | ||
7a979d99 JH |
1423 | if (line[0] == '-') { |
1424 | if (get_sha1_hex(line+1, sha1)) | |
1425 | die("expected edge sha1, got garbage:\n %s", | |
1426 | line+1); | |
5379a5c5 JH |
1427 | if (num_preferred_base++ < window) |
1428 | add_preferred_base(sha1); | |
7a979d99 | 1429 | continue; |
21fcd1bd | 1430 | } |
c323ac7d | 1431 | if (get_sha1_hex(line, sha1)) |
ef07618f | 1432 | die("expected sha1, got garbage:\n %s", line); |
ce0bd642 | 1433 | hash = name_hash(line+41); |
5379a5c5 JH |
1434 | add_preferred_base_object(line+41, hash); |
1435 | add_object_entry(sha1, hash, 0); | |
c323ac7d | 1436 | } |
21fcd1bd JH |
1437 | if (progress) |
1438 | fprintf(stderr, "Done counting %d objects.\n", nr_objects); | |
f0b0af1b JH |
1439 | sorted_by_sha = create_final_object_list(); |
1440 | if (non_empty && !nr_result) | |
1c4a2912 | 1441 | return 0; |
c323ac7d | 1442 | |
84c8d8ae JH |
1443 | SHA1_Init(&ctx); |
1444 | list = sorted_by_sha; | |
7a979d99 | 1445 | for (i = 0; i < nr_result; i++) { |
84c8d8ae JH |
1446 | struct object_entry *entry = *list++; |
1447 | SHA1_Update(&ctx, entry->sha1, 20); | |
1448 | } | |
1449 | SHA1_Final(object_list_sha1, &ctx); | |
7a979d99 JH |
1450 | if (progress && (nr_objects != nr_result)) |
1451 | fprintf(stderr, "Result has %d objects.\n", nr_result); | |
84c8d8ae | 1452 | |
df6d6101 | 1453 | if (reuse_cached_pack(object_list_sha1)) |
f3123c4a JH |
1454 | ; |
1455 | else { | |
f0b0af1b JH |
1456 | if (nr_result) |
1457 | prepare_pack(window, depth); | |
5e8dc750 NP |
1458 | if (progress && pack_to_stdout) { |
1459 | /* the other end usually displays progress itself */ | |
1460 | struct itimerval v = {{0,},}; | |
1461 | setitimer(ITIMER_REAL, &v, NULL); | |
1462 | signal(SIGALRM, SIG_IGN ); | |
1463 | progress_update = 0; | |
1464 | } | |
1465 | write_pack_file(); | |
f3123c4a JH |
1466 | if (!pack_to_stdout) { |
1467 | write_index_file(); | |
1468 | puts(sha1_to_hex(object_list_sha1)); | |
1469 | } | |
5f3de58f | 1470 | } |
ab7cd7bb JH |
1471 | if (progress) |
1472 | fprintf(stderr, "Total %d, written %d (delta %d), reused %d (delta %d)\n", | |
7a979d99 | 1473 | nr_result, written, written_delta, reused, reused_delta); |
c323ac7d LT |
1474 | return 0; |
1475 | } |