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