]>
Commit | Line | Data |
---|---|---|
c323ac7d LT |
1 | #include "cache.h" |
2 | #include "object.h" | |
3 | #include "delta.h" | |
a733cb60 | 4 | #include "pack.h" |
c38138cd | 5 | #include "csum-file.h" |
1b0c7174 | 6 | #include "tree-walk.h" |
21fcd1bd | 7 | #include <sys/time.h> |
b2504a0d | 8 | #include <signal.h> |
c323ac7d | 9 | |
ab7cd7bb | 10 | 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 | 11 | |
c323ac7d LT |
12 | struct object_entry { |
13 | unsigned char sha1[20]; | |
3f9ac8d2 | 14 | unsigned long size; /* uncompressed size */ |
15b4d577 JH |
15 | unsigned long offset; /* offset into the final pack file; |
16 | * nonzero if already written. | |
17 | */ | |
3f9ac8d2 | 18 | unsigned int depth; /* delta depth */ |
15b4d577 | 19 | unsigned int delta_limit; /* base adjustment for in-pack delta */ |
3f9ac8d2 | 20 | unsigned int hash; /* name hint hash */ |
a733cb60 | 21 | enum object_type type; |
ab7cd7bb | 22 | enum object_type in_pack_type; /* could be delta */ |
3f9ac8d2 JH |
23 | unsigned long delta_size; /* delta data size (uncompressed) */ |
24 | struct object_entry *delta; /* delta base object */ | |
25 | struct packed_git *in_pack; /* already in pack */ | |
3f9ac8d2 | 26 | unsigned int in_pack_offset; |
15b4d577 JH |
27 | struct object_entry *delta_child; /* delitified objects who bases me */ |
28 | struct object_entry *delta_sibling; /* other deltified objects who | |
29 | * uses the same base as me | |
30 | */ | |
7a979d99 JH |
31 | int preferred_base; /* we do not pack this, but is encouraged to |
32 | * be used as the base objectto delta huge | |
33 | * objects against. | |
34 | */ | |
c323ac7d LT |
35 | }; |
36 | ||
3f9ac8d2 JH |
37 | /* |
38 | * Objects we are going to pack are colected in objects array (dynamically | |
39 | * expanded). nr_objects & nr_alloc controls this array. They are stored | |
40 | * in the order we see -- typically rev-list --objects order that gives us | |
41 | * nice "minimum seek" order. | |
42 | * | |
43 | * sorted-by-sha ans sorted-by-type are arrays of pointers that point at | |
44 | * elements in the objects array. The former is used to build the pack | |
45 | * index (lists object names in the ascending order to help offset lookup), | |
46 | * and the latter is used to group similar things together by try_delta() | |
47 | * heuristics. | |
48 | */ | |
49 | ||
5f3de58f | 50 | static unsigned char object_list_sha1[20]; |
1c4a2912 | 51 | static int non_empty = 0; |
ab7cd7bb | 52 | static int no_reuse_delta = 0; |
64560374 | 53 | static int local = 0; |
eb019375 | 54 | static int incremental = 0; |
c323ac7d LT |
55 | static struct object_entry **sorted_by_sha, **sorted_by_type; |
56 | static struct object_entry *objects = NULL; | |
7a979d99 | 57 | static int nr_objects = 0, nr_alloc = 0, nr_result = 0; |
c323ac7d | 58 | static const char *base_name; |
e1808845 | 59 | static unsigned char pack_file_sha1[20]; |
024701f1 | 60 | static int progress = 1; |
5e8dc750 | 61 | static volatile int progress_update = 0; |
c323ac7d | 62 | |
3f9ac8d2 JH |
63 | /* |
64 | * The object names in objects array are hashed with this hashtable, | |
65 | * to help looking up the entry by object name. Binary search from | |
66 | * sorted_by_sha is also possible but this was easier to code and faster. | |
67 | * This hashtable is built after all the objects are seen. | |
68 | */ | |
69 | static int *object_ix = NULL; | |
70 | static int object_ix_hashsz = 0; | |
71 | ||
72 | /* | |
73 | * Pack index for existing packs give us easy access to the offsets into | |
74 | * corresponding pack file where each object's data starts, but the entries | |
75 | * do not store the size of the compressed representation (uncompressed | |
76 | * size is easily available by examining the pack entry header). We build | |
77 | * a hashtable of existing packs (pack_revindex), and keep reverse index | |
78 | * here -- pack index file is sorted by object name mapping to offset; this | |
79 | * pack_revindex[].revindex array is an ordered list of offsets, so if you | |
80 | * know the offset of an object, next offset is where its packed | |
81 | * representation ends. | |
82 | */ | |
83 | struct pack_revindex { | |
84 | struct packed_git *p; | |
85 | unsigned long *revindex; | |
86 | } *pack_revindex = NULL; | |
87 | static int pack_revindex_hashsz = 0; | |
88 | ||
89 | /* | |
90 | * stats | |
91 | */ | |
92 | static int written = 0; | |
ab7cd7bb | 93 | static int written_delta = 0; |
3f9ac8d2 | 94 | static int reused = 0; |
ab7cd7bb | 95 | static int reused_delta = 0; |
3f9ac8d2 JH |
96 | |
97 | static int pack_revindex_ix(struct packed_git *p) | |
98 | { | |
2b74cffa | 99 | unsigned long ui = (unsigned long)p; |
3f9ac8d2 JH |
100 | int i; |
101 | ||
102 | ui = ui ^ (ui >> 16); /* defeat structure alignment */ | |
103 | i = (int)(ui % pack_revindex_hashsz); | |
104 | while (pack_revindex[i].p) { | |
105 | if (pack_revindex[i].p == p) | |
106 | return i; | |
107 | if (++i == pack_revindex_hashsz) | |
108 | i = 0; | |
109 | } | |
110 | return -1 - i; | |
111 | } | |
112 | ||
113 | static void prepare_pack_ix(void) | |
114 | { | |
115 | int num; | |
116 | struct packed_git *p; | |
117 | for (num = 0, p = packed_git; p; p = p->next) | |
118 | num++; | |
119 | if (!num) | |
120 | return; | |
121 | pack_revindex_hashsz = num * 11; | |
122 | pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz); | |
123 | for (p = packed_git; p; p = p->next) { | |
124 | num = pack_revindex_ix(p); | |
125 | num = - 1 - num; | |
126 | pack_revindex[num].p = p; | |
127 | } | |
128 | /* revindex elements are lazily initialized */ | |
129 | } | |
130 | ||
131 | static int cmp_offset(const void *a_, const void *b_) | |
132 | { | |
133 | unsigned long a = *(unsigned long *) a_; | |
134 | unsigned long b = *(unsigned long *) b_; | |
135 | if (a < b) | |
136 | return -1; | |
137 | else if (a == b) | |
138 | return 0; | |
139 | else | |
140 | return 1; | |
141 | } | |
142 | ||
143 | /* | |
144 | * Ordered list of offsets of objects in the pack. | |
145 | */ | |
146 | static void prepare_pack_revindex(struct pack_revindex *rix) | |
147 | { | |
148 | struct packed_git *p = rix->p; | |
149 | int num_ent = num_packed_objects(p); | |
150 | int i; | |
151 | void *index = p->index_base + 256; | |
152 | ||
153 | rix->revindex = xmalloc(sizeof(unsigned long) * (num_ent + 1)); | |
154 | for (i = 0; i < num_ent; i++) { | |
155 | long hl = *((long *)(index + 24 * i)); | |
156 | rix->revindex[i] = ntohl(hl); | |
157 | } | |
158 | /* This knows the pack format -- the 20-byte trailer | |
159 | * follows immediately after the last object data. | |
160 | */ | |
161 | rix->revindex[num_ent] = p->pack_size - 20; | |
162 | qsort(rix->revindex, num_ent, sizeof(unsigned long), cmp_offset); | |
163 | } | |
164 | ||
165 | static unsigned long find_packed_object_size(struct packed_git *p, | |
166 | unsigned long ofs) | |
167 | { | |
168 | int num; | |
169 | int lo, hi; | |
170 | struct pack_revindex *rix; | |
171 | unsigned long *revindex; | |
172 | num = pack_revindex_ix(p); | |
173 | if (num < 0) | |
174 | die("internal error: pack revindex uninitialized"); | |
175 | rix = &pack_revindex[num]; | |
176 | if (!rix->revindex) | |
177 | prepare_pack_revindex(rix); | |
178 | revindex = rix->revindex; | |
179 | lo = 0; | |
180 | hi = num_packed_objects(p) + 1; | |
181 | do { | |
182 | int mi = (lo + hi) / 2; | |
183 | if (revindex[mi] == ofs) { | |
184 | return revindex[mi+1] - ofs; | |
185 | } | |
186 | else if (ofs < revindex[mi]) | |
187 | hi = mi; | |
188 | else | |
189 | lo = mi + 1; | |
190 | } while (lo < hi); | |
191 | die("internal error: pack revindex corrupt"); | |
192 | } | |
193 | ||
c323ac7d LT |
194 | static void *delta_against(void *buf, unsigned long size, struct object_entry *entry) |
195 | { | |
196 | unsigned long othersize, delta_size; | |
197 | char type[10]; | |
198 | void *otherbuf = read_sha1_file(entry->delta->sha1, type, &othersize); | |
199 | void *delta_buf; | |
200 | ||
201 | if (!otherbuf) | |
202 | die("unable to read %s", sha1_to_hex(entry->delta->sha1)); | |
8ee378a0 | 203 | delta_buf = diff_delta(otherbuf, othersize, |
dcde55bc | 204 | buf, size, &delta_size, 0); |
75c42d8c | 205 | if (!delta_buf || delta_size != entry->delta_size) |
c323ac7d LT |
206 | die("delta size changed"); |
207 | free(buf); | |
208 | free(otherbuf); | |
209 | return delta_buf; | |
210 | } | |
211 | ||
a733cb60 LT |
212 | /* |
213 | * The per-object header is a pretty dense thing, which is | |
214 | * - first byte: low four bits are "size", then three bits of "type", | |
215 | * and the high bit is "size continues". | |
216 | * - each byte afterwards: low seven bits are size continuation, | |
217 | * with the high bit being "size continues" | |
218 | */ | |
219 | static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr) | |
220 | { | |
01247d87 | 221 | int n = 1; |
a733cb60 LT |
222 | unsigned char c; |
223 | ||
224 | if (type < OBJ_COMMIT || type > OBJ_DELTA) | |
225 | die("bad type %d", type); | |
226 | ||
01247d87 LT |
227 | c = (type << 4) | (size & 15); |
228 | size >>= 4; | |
229 | while (size) { | |
a733cb60 | 230 | *hdr++ = c | 0x80; |
01247d87 LT |
231 | c = size & 0x7f; |
232 | size >>= 7; | |
233 | n++; | |
a733cb60 LT |
234 | } |
235 | *hdr = c; | |
236 | return n; | |
237 | } | |
238 | ||
7a979d99 JH |
239 | static unsigned long write_object(struct sha1file *f, |
240 | struct object_entry *entry) | |
c323ac7d LT |
241 | { |
242 | unsigned long size; | |
243 | char type[10]; | |
3f9ac8d2 | 244 | void *buf; |
a733cb60 | 245 | unsigned char header[10]; |
c323ac7d | 246 | unsigned hdrlen, datalen; |
a733cb60 | 247 | enum object_type obj_type; |
ab7cd7bb | 248 | int to_reuse = 0; |
c323ac7d | 249 | |
7a979d99 JH |
250 | if (entry->preferred_base) |
251 | return 0; | |
c323ac7d | 252 | |
a733cb60 | 253 | obj_type = entry->type; |
ab7cd7bb JH |
254 | if (! entry->in_pack) |
255 | to_reuse = 0; /* can't reuse what we don't have */ | |
256 | else if (obj_type == OBJ_DELTA) | |
257 | to_reuse = 1; /* check_object() decided it for us */ | |
258 | else if (obj_type != entry->in_pack_type) | |
259 | to_reuse = 0; /* pack has delta which is unusable */ | |
260 | else if (entry->delta) | |
261 | to_reuse = 0; /* we want to pack afresh */ | |
262 | else | |
263 | to_reuse = 1; /* we have it in-pack undeltified, | |
264 | * and we do not need to deltify it. | |
265 | */ | |
266 | ||
267 | if (! to_reuse) { | |
3f9ac8d2 JH |
268 | buf = read_sha1_file(entry->sha1, type, &size); |
269 | if (!buf) | |
270 | die("unable to read %s", sha1_to_hex(entry->sha1)); | |
271 | if (size != entry->size) | |
272 | die("object %s size inconsistency (%lu vs %lu)", | |
273 | sha1_to_hex(entry->sha1), size, entry->size); | |
274 | if (entry->delta) { | |
275 | buf = delta_against(buf, size, entry); | |
276 | size = entry->delta_size; | |
277 | obj_type = OBJ_DELTA; | |
278 | } | |
279 | /* | |
280 | * The object header is a byte of 'type' followed by zero or | |
281 | * more bytes of length. For deltas, the 20 bytes of delta | |
282 | * sha1 follows that. | |
283 | */ | |
284 | hdrlen = encode_header(obj_type, size, header); | |
285 | sha1write(f, header, hdrlen); | |
286 | ||
287 | if (entry->delta) { | |
288 | sha1write(f, entry->delta, 20); | |
289 | hdrlen += 20; | |
290 | } | |
291 | datalen = sha1write_compressed(f, buf, size); | |
292 | free(buf); | |
c323ac7d | 293 | } |
3f9ac8d2 JH |
294 | else { |
295 | struct packed_git *p = entry->in_pack; | |
296 | use_packed_git(p); | |
297 | ||
298 | datalen = find_packed_object_size(p, entry->in_pack_offset); | |
299 | buf = p->pack_base + entry->in_pack_offset; | |
300 | sha1write(f, buf, datalen); | |
301 | unuse_packed_git(p); | |
302 | hdrlen = 0; /* not really */ | |
ab7cd7bb JH |
303 | if (obj_type == OBJ_DELTA) |
304 | reused_delta++; | |
3f9ac8d2 | 305 | reused++; |
a733cb60 | 306 | } |
ab7cd7bb JH |
307 | if (obj_type == OBJ_DELTA) |
308 | written_delta++; | |
3f9ac8d2 | 309 | written++; |
c323ac7d LT |
310 | return hdrlen + datalen; |
311 | } | |
312 | ||
9d5ab962 JH |
313 | static unsigned long write_one(struct sha1file *f, |
314 | struct object_entry *e, | |
315 | unsigned long offset) | |
316 | { | |
317 | if (e->offset) | |
318 | /* offset starts from header size and cannot be zero | |
319 | * if it is written already. | |
320 | */ | |
321 | return offset; | |
322 | e->offset = offset; | |
323 | offset += write_object(f, e); | |
82f9d58a | 324 | /* if we are deltified, write out its base object. */ |
9d5ab962 JH |
325 | if (e->delta) |
326 | offset = write_one(f, e->delta, offset); | |
327 | return offset; | |
328 | } | |
329 | ||
c323ac7d LT |
330 | static void write_pack_file(void) |
331 | { | |
332 | int i; | |
d22b9290 | 333 | struct sha1file *f; |
a733cb60 | 334 | unsigned long offset; |
a733cb60 | 335 | struct pack_header hdr; |
183bdb2c JH |
336 | unsigned last_percent = 999; |
337 | int do_progress = 0; | |
c323ac7d | 338 | |
d22b9290 LT |
339 | if (!base_name) |
340 | f = sha1fd(1, "<stdout>"); | |
183bdb2c JH |
341 | else { |
342 | f = sha1create("%s-%s.%s", base_name, | |
343 | sha1_to_hex(object_list_sha1), "pack"); | |
344 | do_progress = progress; | |
345 | } | |
346 | if (do_progress) | |
f0b0af1b | 347 | fprintf(stderr, "Writing %d objects.\n", nr_result); |
183bdb2c | 348 | |
a733cb60 | 349 | hdr.hdr_signature = htonl(PACK_SIGNATURE); |
01247d87 | 350 | hdr.hdr_version = htonl(PACK_VERSION); |
7a979d99 | 351 | hdr.hdr_entries = htonl(nr_result); |
a733cb60 LT |
352 | sha1write(f, &hdr, sizeof(hdr)); |
353 | offset = sizeof(hdr); | |
f0b0af1b JH |
354 | if (!nr_result) |
355 | goto done; | |
5e8dc750 | 356 | for (i = 0; i < nr_objects; i++) { |
9d5ab962 | 357 | offset = write_one(f, objects + i, offset); |
183bdb2c | 358 | if (do_progress) { |
f0b0af1b | 359 | unsigned percent = written * 100 / nr_result; |
183bdb2c JH |
360 | if (progress_update || percent != last_percent) { |
361 | fprintf(stderr, "%4u%% (%u/%u) done\r", | |
f0b0af1b | 362 | percent, written, nr_result); |
183bdb2c JH |
363 | progress_update = 0; |
364 | last_percent = percent; | |
365 | } | |
5e8dc750 NP |
366 | } |
367 | } | |
183bdb2c JH |
368 | if (do_progress) |
369 | fputc('\n', stderr); | |
f0b0af1b | 370 | done: |
e1808845 | 371 | sha1close(f, pack_file_sha1, 1); |
c323ac7d LT |
372 | } |
373 | ||
374 | static void write_index_file(void) | |
375 | { | |
376 | int i; | |
7a979d99 JH |
377 | struct sha1file *f = sha1create("%s-%s.%s", base_name, |
378 | sha1_to_hex(object_list_sha1), "idx"); | |
c323ac7d | 379 | struct object_entry **list = sorted_by_sha; |
7a979d99 | 380 | struct object_entry **last = list + nr_result; |
c323ac7d LT |
381 | unsigned int array[256]; |
382 | ||
383 | /* | |
384 | * Write the first-level table (the list is sorted, | |
385 | * but we use a 256-entry lookup to be able to avoid | |
e1808845 | 386 | * having to do eight extra binary search iterations). |
c323ac7d LT |
387 | */ |
388 | for (i = 0; i < 256; i++) { | |
389 | struct object_entry **next = list; | |
390 | while (next < last) { | |
391 | struct object_entry *entry = *next; | |
392 | if (entry->sha1[0] != i) | |
393 | break; | |
394 | next++; | |
395 | } | |
396 | array[i] = htonl(next - sorted_by_sha); | |
397 | list = next; | |
398 | } | |
c38138cd | 399 | sha1write(f, array, 256 * sizeof(int)); |
c323ac7d LT |
400 | |
401 | /* | |
402 | * Write the actual SHA1 entries.. | |
403 | */ | |
404 | list = sorted_by_sha; | |
7a979d99 | 405 | for (i = 0; i < nr_result; i++) { |
c323ac7d LT |
406 | struct object_entry *entry = *list++; |
407 | unsigned int offset = htonl(entry->offset); | |
c38138cd LT |
408 | sha1write(f, &offset, 4); |
409 | sha1write(f, entry->sha1, 20); | |
c323ac7d | 410 | } |
e1808845 LT |
411 | sha1write(f, pack_file_sha1, 20); |
412 | sha1close(f, NULL, 1); | |
c323ac7d LT |
413 | } |
414 | ||
7a979d99 JH |
415 | static int locate_object_entry_hash(const unsigned char *sha1) |
416 | { | |
417 | int i; | |
418 | unsigned int ui; | |
419 | memcpy(&ui, sha1, sizeof(unsigned int)); | |
420 | i = ui % object_ix_hashsz; | |
421 | while (0 < object_ix[i]) { | |
422 | if (!memcmp(sha1, objects[object_ix[i]-1].sha1, 20)) | |
423 | return i; | |
424 | if (++i == object_ix_hashsz) | |
425 | i = 0; | |
426 | } | |
427 | return -1 - i; | |
428 | } | |
429 | ||
430 | static struct object_entry *locate_object_entry(const unsigned char *sha1) | |
431 | { | |
432 | int i; | |
433 | ||
434 | if (!object_ix_hashsz) | |
435 | return NULL; | |
436 | ||
437 | i = locate_object_entry_hash(sha1); | |
438 | if (0 <= i) | |
439 | return &objects[object_ix[i]-1]; | |
440 | return NULL; | |
441 | } | |
442 | ||
443 | static void rehash_objects(void) | |
c323ac7d | 444 | { |
7a979d99 JH |
445 | int i; |
446 | struct object_entry *oe; | |
447 | ||
448 | object_ix_hashsz = nr_objects * 3; | |
449 | if (object_ix_hashsz < 1024) | |
450 | object_ix_hashsz = 1024; | |
451 | object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz); | |
452 | object_ix = memset(object_ix, 0, sizeof(int) * object_ix_hashsz); | |
453 | for (i = 0, oe = objects; i < nr_objects; i++, oe++) { | |
454 | int ix = locate_object_entry_hash(oe->sha1); | |
455 | if (0 <= ix) | |
456 | continue; | |
457 | ix = -1 - ix; | |
458 | object_ix[ix] = i + 1; | |
459 | } | |
460 | } | |
461 | ||
1d6b38cc JH |
462 | struct name_path { |
463 | struct name_path *up; | |
464 | const char *elem; | |
465 | int len; | |
466 | }; | |
467 | ||
eeef7135 JH |
468 | #define DIRBITS 12 |
469 | ||
1d6b38cc JH |
470 | static unsigned name_hash(struct name_path *path, const char *name) |
471 | { | |
472 | struct name_path *p = path; | |
473 | const char *n = name + strlen(name); | |
eeef7135 | 474 | unsigned hash = 0, name_hash = 0, name_done = 0; |
1d6b38cc JH |
475 | |
476 | if (n != name && n[-1] == '\n') | |
477 | n--; | |
478 | while (name <= --n) { | |
479 | unsigned char c = *n; | |
eeef7135 JH |
480 | if (c == '/' && !name_done) { |
481 | name_hash = hash; | |
482 | name_done = 1; | |
483 | hash = 0; | |
484 | } | |
1d6b38cc JH |
485 | hash = hash * 11 + c; |
486 | } | |
eeef7135 JH |
487 | if (!name_done) { |
488 | name_hash = hash; | |
489 | hash = 0; | |
490 | } | |
1d6b38cc JH |
491 | for (p = path; p; p = p->up) { |
492 | hash = hash * 11 + '/'; | |
493 | n = p->elem + p->len; | |
494 | while (p->elem <= --n) { | |
495 | unsigned char c = *n; | |
496 | hash = hash * 11 + c; | |
497 | } | |
498 | } | |
eeef7135 JH |
499 | /* |
500 | * Make sure "Makefile" and "t/Makefile" are hashed separately | |
501 | * but close enough. | |
502 | */ | |
503 | hash = (name_hash<<DIRBITS) | (hash & ((1U<<DIRBITS )-1)); | |
504 | ||
505 | if (0) { /* debug */ | |
506 | n = name + strlen(name); | |
507 | if (n != name && n[-1] == '\n') | |
508 | n--; | |
509 | while (name <= --n) | |
510 | fputc(*n, stderr); | |
511 | for (p = path; p; p = p->up) { | |
512 | fputc('/', stderr); | |
513 | n = p->elem + p->len; | |
514 | while (p->elem <= --n) | |
515 | fputc(*n, stderr); | |
516 | } | |
517 | fprintf(stderr, "\t%08x\n", hash); | |
518 | } | |
1d6b38cc JH |
519 | return hash; |
520 | } | |
521 | ||
522 | static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclude) | |
c323ac7d LT |
523 | { |
524 | unsigned int idx = nr_objects; | |
525 | struct object_entry *entry; | |
3f9ac8d2 | 526 | struct packed_git *p; |
ab7cd7bb JH |
527 | unsigned int found_offset = 0; |
528 | struct packed_git *found_pack = NULL; | |
b925410d | 529 | int ix, status = 0; |
c323ac7d | 530 | |
7a979d99 | 531 | if (!exclude) { |
64560374 LT |
532 | for (p = packed_git; p; p = p->next) { |
533 | struct pack_entry e; | |
64560374 LT |
534 | if (find_pack_entry_one(sha1, &e, p)) { |
535 | if (incremental) | |
536 | return 0; | |
537 | if (local && !p->pack_local) | |
538 | return 0; | |
7a979d99 JH |
539 | if (!found_pack) { |
540 | found_offset = e.offset; | |
541 | found_pack = e.p; | |
542 | } | |
64560374 LT |
543 | } |
544 | } | |
545 | } | |
7a979d99 JH |
546 | if ((entry = locate_object_entry(sha1)) != NULL) |
547 | goto already_added; | |
eb019375 | 548 | |
c323ac7d LT |
549 | if (idx >= nr_alloc) { |
550 | unsigned int needed = (idx + 1024) * 3 / 2; | |
551 | objects = xrealloc(objects, needed * sizeof(*entry)); | |
552 | nr_alloc = needed; | |
553 | } | |
554 | entry = objects + idx; | |
7a979d99 | 555 | nr_objects = idx + 1; |
c323ac7d LT |
556 | memset(entry, 0, sizeof(*entry)); |
557 | memcpy(entry->sha1, sha1, 20); | |
27225f2e | 558 | entry->hash = hash; |
7a979d99 JH |
559 | |
560 | if (object_ix_hashsz * 3 <= nr_objects * 4) | |
561 | rehash_objects(); | |
562 | else { | |
563 | ix = locate_object_entry_hash(entry->sha1); | |
564 | if (0 <= ix) | |
565 | die("internal error in object hashing."); | |
566 | object_ix[-1 - ix] = idx + 1; | |
3f9ac8d2 | 567 | } |
b925410d | 568 | status = 1; |
7a979d99 JH |
569 | |
570 | already_added: | |
f0b0af1b JH |
571 | if (progress_update) { |
572 | fprintf(stderr, "Counting objects...%d\r", nr_objects); | |
573 | progress_update = 0; | |
574 | } | |
7a979d99 JH |
575 | if (exclude) |
576 | entry->preferred_base = 1; | |
577 | else { | |
578 | if (found_pack) { | |
579 | entry->in_pack = found_pack; | |
580 | entry->in_pack_offset = found_offset; | |
581 | } | |
a49dd05f | 582 | } |
b925410d | 583 | return status; |
c323ac7d LT |
584 | } |
585 | ||
1d6b38cc | 586 | static void add_pbase_tree(struct tree_desc *tree, struct name_path *up) |
3f9ac8d2 | 587 | { |
7a979d99 JH |
588 | while (tree->size) { |
589 | const unsigned char *sha1; | |
590 | const char *name; | |
1d6b38cc | 591 | unsigned mode, hash; |
7a979d99 JH |
592 | unsigned long size; |
593 | char type[20]; | |
594 | ||
595 | sha1 = tree_entry_extract(tree, &name, &mode); | |
596 | update_tree_entry(tree); | |
597 | if (!has_sha1_file(sha1)) | |
598 | continue; | |
599 | if (sha1_object_info(sha1, type, &size)) | |
600 | continue; | |
b925410d | 601 | |
1d6b38cc JH |
602 | hash = name_hash(up, name); |
603 | if (!add_object_entry(sha1, hash, 1)) | |
b925410d JH |
604 | continue; |
605 | ||
7a979d99 JH |
606 | if (!strcmp(type, "tree")) { |
607 | struct tree_desc sub; | |
608 | void *elem; | |
1d6b38cc JH |
609 | struct name_path me; |
610 | ||
7a979d99 JH |
611 | elem = read_sha1_file(sha1, type, &sub.size); |
612 | sub.buf = elem; | |
613 | if (sub.buf) { | |
1d6b38cc JH |
614 | me.up = up; |
615 | me.elem = name; | |
616 | me.len = strlen(name); | |
617 | add_pbase_tree(&sub, &me); | |
7a979d99 JH |
618 | free(elem); |
619 | } | |
620 | } | |
3f9ac8d2 | 621 | } |
3f9ac8d2 JH |
622 | } |
623 | ||
7a979d99 | 624 | static void add_preferred_base(unsigned char *sha1) |
3f9ac8d2 | 625 | { |
7a979d99 JH |
626 | struct tree_desc tree; |
627 | void *elem; | |
1d6b38cc | 628 | |
7a979d99 JH |
629 | elem = read_object_with_reference(sha1, "tree", &tree.size, NULL); |
630 | tree.buf = elem; | |
631 | if (!tree.buf) | |
632 | return; | |
1d6b38cc JH |
633 | if (add_object_entry(sha1, name_hash(NULL, ""), 1)) |
634 | add_pbase_tree(&tree, NULL); | |
7a979d99 | 635 | free(elem); |
3f9ac8d2 JH |
636 | } |
637 | ||
c323ac7d LT |
638 | static void check_object(struct object_entry *entry) |
639 | { | |
36e4d74a JH |
640 | char type[20]; |
641 | ||
7a979d99 | 642 | if (entry->in_pack && !entry->preferred_base) { |
ab7cd7bb JH |
643 | unsigned char base[20]; |
644 | unsigned long size; | |
645 | struct object_entry *base_entry; | |
646 | ||
647 | /* We want in_pack_type even if we do not reuse delta. | |
648 | * There is no point not reusing non-delta representations. | |
649 | */ | |
650 | check_reuse_pack_delta(entry->in_pack, | |
651 | entry->in_pack_offset, | |
652 | base, &size, | |
653 | &entry->in_pack_type); | |
654 | ||
3f9ac8d2 JH |
655 | /* Check if it is delta, and the base is also an object |
656 | * we are going to pack. If so we will reuse the existing | |
657 | * delta. | |
658 | */ | |
ab7cd7bb JH |
659 | if (!no_reuse_delta && |
660 | entry->in_pack_type == OBJ_DELTA && | |
7a979d99 JH |
661 | (base_entry = locate_object_entry(base)) && |
662 | (!base_entry->preferred_base)) { | |
ab7cd7bb JH |
663 | |
664 | /* Depth value does not matter - find_deltas() | |
665 | * will never consider reused delta as the | |
666 | * base object to deltify other objects | |
667 | * against, in order to avoid circular deltas. | |
3f9ac8d2 | 668 | */ |
ab7cd7bb JH |
669 | |
670 | /* uncompressed size of the delta data */ | |
3f9ac8d2 JH |
671 | entry->size = entry->delta_size = size; |
672 | entry->delta = base_entry; | |
673 | entry->type = OBJ_DELTA; | |
ab7cd7bb | 674 | |
15b4d577 JH |
675 | entry->delta_sibling = base_entry->delta_child; |
676 | base_entry->delta_child = entry; | |
ab7cd7bb | 677 | |
3f9ac8d2 JH |
678 | return; |
679 | } | |
680 | /* Otherwise we would do the usual */ | |
36e4d74a | 681 | } |
3f9ac8d2 JH |
682 | |
683 | if (sha1_object_info(entry->sha1, type, &entry->size)) | |
36e4d74a JH |
684 | die("unable to get type of object %s", |
685 | sha1_to_hex(entry->sha1)); | |
3f9ac8d2 JH |
686 | |
687 | if (!strcmp(type, "commit")) { | |
688 | entry->type = OBJ_COMMIT; | |
689 | } else if (!strcmp(type, "tree")) { | |
690 | entry->type = OBJ_TREE; | |
691 | } else if (!strcmp(type, "blob")) { | |
692 | entry->type = OBJ_BLOB; | |
693 | } else if (!strcmp(type, "tag")) { | |
694 | entry->type = OBJ_TAG; | |
695 | } else | |
696 | die("unable to pack object %s of type %s", | |
697 | sha1_to_hex(entry->sha1), type); | |
698 | } | |
699 | ||
15b4d577 JH |
700 | static unsigned int check_delta_limit(struct object_entry *me, unsigned int n) |
701 | { | |
702 | struct object_entry *child = me->delta_child; | |
703 | unsigned int m = n; | |
704 | while (child) { | |
705 | unsigned int c = check_delta_limit(child, n + 1); | |
706 | if (m < c) | |
707 | m = c; | |
708 | child = child->delta_sibling; | |
709 | } | |
710 | return m; | |
711 | } | |
712 | ||
c323ac7d LT |
713 | static void get_object_details(void) |
714 | { | |
715 | int i; | |
15b4d577 | 716 | struct object_entry *entry; |
c323ac7d | 717 | |
3f9ac8d2 | 718 | prepare_pack_ix(); |
15b4d577 JH |
719 | for (i = 0, entry = objects; i < nr_objects; i++, entry++) |
720 | check_object(entry); | |
b76f6b62 JH |
721 | |
722 | if (nr_objects == nr_result) { | |
723 | /* | |
724 | * Depth of objects that depend on the entry -- this | |
725 | * is subtracted from depth-max to break too deep | |
726 | * delta chain because of delta data reusing. | |
727 | * However, we loosen this restriction when we know we | |
728 | * are creating a thin pack -- it will have to be | |
729 | * expanded on the other end anyway, so do not | |
730 | * artificially cut the delta chain and let it go as | |
731 | * deep as it wants. | |
732 | */ | |
733 | for (i = 0, entry = objects; i < nr_objects; i++, entry++) | |
734 | if (!entry->delta && entry->delta_child) | |
735 | entry->delta_limit = | |
736 | check_delta_limit(entry, 1); | |
737 | } | |
c323ac7d LT |
738 | } |
739 | ||
740 | typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *); | |
741 | ||
742 | static entry_sort_t current_sort; | |
743 | ||
744 | static int sort_comparator(const void *_a, const void *_b) | |
745 | { | |
746 | struct object_entry *a = *(struct object_entry **)_a; | |
747 | struct object_entry *b = *(struct object_entry **)_b; | |
748 | return current_sort(a,b); | |
749 | } | |
750 | ||
751 | static struct object_entry **create_sorted_list(entry_sort_t sort) | |
752 | { | |
753 | struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *)); | |
754 | int i; | |
755 | ||
756 | for (i = 0; i < nr_objects; i++) | |
757 | list[i] = objects + i; | |
758 | current_sort = sort; | |
759 | qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator); | |
760 | return list; | |
761 | } | |
762 | ||
763 | static int sha1_sort(const struct object_entry *a, const struct object_entry *b) | |
764 | { | |
765 | return memcmp(a->sha1, b->sha1, 20); | |
766 | } | |
767 | ||
962554c6 | 768 | static struct object_entry **create_final_object_list(void) |
7a979d99 JH |
769 | { |
770 | struct object_entry **list; | |
771 | int i, j; | |
772 | ||
773 | for (i = nr_result = 0; i < nr_objects; i++) | |
774 | if (!objects[i].preferred_base) | |
775 | nr_result++; | |
776 | list = xmalloc(nr_result * sizeof(struct object_entry *)); | |
777 | for (i = j = 0; i < nr_objects; i++) { | |
778 | if (!objects[i].preferred_base) | |
779 | list[j++] = objects + i; | |
780 | } | |
781 | current_sort = sha1_sort; | |
782 | qsort(list, nr_result, sizeof(struct object_entry *), sort_comparator); | |
783 | return list; | |
784 | } | |
785 | ||
c323ac7d LT |
786 | static int type_size_sort(const struct object_entry *a, const struct object_entry *b) |
787 | { | |
788 | if (a->type < b->type) | |
789 | return -1; | |
790 | if (a->type > b->type) | |
791 | return 1; | |
27225f2e LT |
792 | if (a->hash < b->hash) |
793 | return -1; | |
794 | if (a->hash > b->hash) | |
795 | return 1; | |
7a979d99 JH |
796 | if (a->preferred_base < b->preferred_base) |
797 | return -1; | |
798 | if (a->preferred_base > b->preferred_base) | |
799 | return 1; | |
c323ac7d LT |
800 | if (a->size < b->size) |
801 | return -1; | |
802 | if (a->size > b->size) | |
803 | return 1; | |
804 | return a < b ? -1 : (a > b); | |
805 | } | |
806 | ||
807 | struct unpacked { | |
808 | struct object_entry *entry; | |
809 | void *data; | |
810 | }; | |
811 | ||
812 | /* | |
521a4f4c LT |
813 | * We search for deltas _backwards_ in a list sorted by type and |
814 | * by size, so that we see progressively smaller and smaller files. | |
815 | * That's because we prefer deltas to be from the bigger file | |
816 | * to the smaller - deletes are potentially cheaper, but perhaps | |
817 | * more importantly, the bigger file is likely the more recent | |
818 | * one. | |
c323ac7d | 819 | */ |
d116a45a | 820 | static int try_delta(struct unpacked *cur, struct unpacked *old, unsigned max_depth) |
c323ac7d LT |
821 | { |
822 | struct object_entry *cur_entry = cur->entry; | |
823 | struct object_entry *old_entry = old->entry; | |
521a4f4c | 824 | unsigned long size, oldsize, delta_size, sizediff; |
75c42d8c | 825 | long max_size; |
c323ac7d LT |
826 | void *delta_buf; |
827 | ||
828 | /* Don't bother doing diffs between different types */ | |
829 | if (cur_entry->type != old_entry->type) | |
830 | return -1; | |
831 | ||
7a979d99 JH |
832 | /* We do not compute delta to *create* objects we are not |
833 | * going to pack. | |
834 | */ | |
835 | if (cur_entry->preferred_base) | |
75c42d8c | 836 | return -1; |
7a979d99 JH |
837 | |
838 | /* If the current object is at pack edge, take the depth the | |
839 | * objects that depend on the current object into account -- | |
840 | * otherwise they would become too deep. | |
ab7cd7bb | 841 | */ |
15b4d577 JH |
842 | if (cur_entry->delta_child) { |
843 | if (max_depth <= cur_entry->delta_limit) | |
844 | return 0; | |
845 | max_depth -= cur_entry->delta_limit; | |
846 | } | |
ab7cd7bb | 847 | |
c323ac7d LT |
848 | size = cur_entry->size; |
849 | oldsize = old_entry->size; | |
521a4f4c | 850 | sizediff = oldsize > size ? oldsize - size : size - oldsize; |
f0b0af1b JH |
851 | |
852 | if (size < 50) | |
c323ac7d | 853 | return -1; |
d116a45a LT |
854 | if (old_entry->depth >= max_depth) |
855 | return 0; | |
c323ac7d LT |
856 | |
857 | /* | |
858 | * NOTE! | |
859 | * | |
860 | * We always delta from the bigger to the smaller, since that's | |
861 | * more space-efficient (deletes don't have to say _what_ they | |
862 | * delete). | |
863 | */ | |
75c42d8c | 864 | max_size = size / 2 - 20; |
70ca1a3f JH |
865 | if (cur_entry->delta) |
866 | max_size = cur_entry->delta_size-1; | |
27225f2e LT |
867 | if (sizediff >= max_size) |
868 | return -1; | |
8ee378a0 JH |
869 | delta_buf = diff_delta(old->data, oldsize, |
870 | cur->data, size, &delta_size, max_size); | |
c323ac7d | 871 | if (!delta_buf) |
75c42d8c LT |
872 | return 0; |
873 | cur_entry->delta = old_entry; | |
874 | cur_entry->delta_size = delta_size; | |
d116a45a | 875 | cur_entry->depth = old_entry->depth + 1; |
c323ac7d | 876 | free(delta_buf); |
eb41ab11 | 877 | return 0; |
c323ac7d LT |
878 | } |
879 | ||
b2504a0d NP |
880 | static void progress_interval(int signum) |
881 | { | |
882 | signal(SIGALRM, progress_interval); | |
883 | progress_update = 1; | |
884 | } | |
885 | ||
d116a45a | 886 | static void find_deltas(struct object_entry **list, int window, int depth) |
c323ac7d | 887 | { |
521a4f4c | 888 | int i, idx; |
c323ac7d LT |
889 | unsigned int array_size = window * sizeof(struct unpacked); |
890 | struct unpacked *array = xmalloc(array_size); | |
183bdb2c JH |
891 | unsigned processed = 0; |
892 | unsigned last_percent = 999; | |
c323ac7d LT |
893 | |
894 | memset(array, 0, array_size); | |
521a4f4c LT |
895 | i = nr_objects; |
896 | idx = 0; | |
183bdb2c | 897 | if (progress) |
f0b0af1b | 898 | fprintf(stderr, "Deltifying %d objects.\n", nr_result); |
21fcd1bd | 899 | |
521a4f4c | 900 | while (--i >= 0) { |
c323ac7d LT |
901 | struct object_entry *entry = list[i]; |
902 | struct unpacked *n = array + idx; | |
903 | unsigned long size; | |
904 | char type[10]; | |
905 | int j; | |
906 | ||
f0b0af1b JH |
907 | if (!entry->preferred_base) |
908 | processed++; | |
909 | ||
183bdb2c | 910 | if (progress) { |
f0b0af1b | 911 | unsigned percent = processed * 100 / nr_result; |
183bdb2c JH |
912 | if (percent != last_percent || progress_update) { |
913 | fprintf(stderr, "%4u%% (%u/%u) done\r", | |
f0b0af1b | 914 | percent, processed, nr_result); |
183bdb2c JH |
915 | progress_update = 0; |
916 | last_percent = percent; | |
917 | } | |
21fcd1bd | 918 | } |
3f9ac8d2 JH |
919 | |
920 | if (entry->delta) | |
921 | /* This happens if we decided to reuse existing | |
ab7cd7bb | 922 | * delta from a pack. "!no_reuse_delta &&" is implied. |
3f9ac8d2 JH |
923 | */ |
924 | continue; | |
925 | ||
c323ac7d LT |
926 | free(n->data); |
927 | n->entry = entry; | |
928 | n->data = read_sha1_file(entry->sha1, type, &size); | |
929 | if (size != entry->size) | |
930 | die("object %s inconsistent object length (%lu vs %lu)", sha1_to_hex(entry->sha1), size, entry->size); | |
ab7cd7bb | 931 | |
78817c15 LT |
932 | j = window; |
933 | while (--j > 0) { | |
934 | unsigned int other_idx = idx + j; | |
c323ac7d | 935 | struct unpacked *m; |
78817c15 LT |
936 | if (other_idx >= window) |
937 | other_idx -= window; | |
c323ac7d LT |
938 | m = array + other_idx; |
939 | if (!m->entry) | |
940 | break; | |
d116a45a | 941 | if (try_delta(n, m, depth) < 0) |
c323ac7d LT |
942 | break; |
943 | } | |
70ca1a3f JH |
944 | #if 0 |
945 | /* if we made n a delta, and if n is already at max | |
946 | * depth, leaving it in the window is pointless. we | |
947 | * should evict it first. | |
948 | * ... in theory only; somehow this makes things worse. | |
949 | */ | |
950 | if (entry->delta && depth <= entry->depth) | |
951 | continue; | |
952 | #endif | |
521a4f4c LT |
953 | idx++; |
954 | if (idx >= window) | |
955 | idx = 0; | |
c323ac7d | 956 | } |
adee7bdf | 957 | |
b2504a0d NP |
958 | if (progress) |
959 | fputc('\n', stderr); | |
960 | ||
adee7bdf SV |
961 | for (i = 0; i < window; ++i) |
962 | free(array[i].data); | |
963 | free(array); | |
c323ac7d LT |
964 | } |
965 | ||
f3123c4a JH |
966 | static void prepare_pack(int window, int depth) |
967 | { | |
3f9ac8d2 | 968 | get_object_details(); |
f3123c4a JH |
969 | sorted_by_type = create_sorted_list(type_size_sort); |
970 | if (window && depth) | |
971 | find_deltas(sorted_by_type, window+1, depth); | |
f3123c4a JH |
972 | } |
973 | ||
974 | static int reuse_cached_pack(unsigned char *sha1, int pack_to_stdout) | |
975 | { | |
976 | static const char cache[] = "pack-cache/pack-%s.%s"; | |
977 | char *cached_pack, *cached_idx; | |
978 | int ifd, ofd, ifd_ix = -1; | |
979 | ||
980 | cached_pack = git_path(cache, sha1_to_hex(sha1), "pack"); | |
981 | ifd = open(cached_pack, O_RDONLY); | |
982 | if (ifd < 0) | |
983 | return 0; | |
984 | ||
985 | if (!pack_to_stdout) { | |
986 | cached_idx = git_path(cache, sha1_to_hex(sha1), "idx"); | |
987 | ifd_ix = open(cached_idx, O_RDONLY); | |
988 | if (ifd_ix < 0) { | |
989 | close(ifd); | |
990 | return 0; | |
991 | } | |
992 | } | |
993 | ||
ab7cd7bb JH |
994 | if (progress) |
995 | fprintf(stderr, "Reusing %d objects pack %s\n", nr_objects, | |
996 | sha1_to_hex(sha1)); | |
f3123c4a JH |
997 | |
998 | if (pack_to_stdout) { | |
999 | if (copy_fd(ifd, 1)) | |
1000 | exit(1); | |
1001 | close(ifd); | |
1002 | } | |
1003 | else { | |
1004 | char name[PATH_MAX]; | |
1005 | snprintf(name, sizeof(name), | |
1006 | "%s-%s.%s", base_name, sha1_to_hex(sha1), "pack"); | |
1007 | ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666); | |
1008 | if (ofd < 0) | |
1009 | die("unable to open %s (%s)", name, strerror(errno)); | |
1010 | if (copy_fd(ifd, ofd)) | |
1011 | exit(1); | |
1012 | close(ifd); | |
1013 | ||
1014 | snprintf(name, sizeof(name), | |
1015 | "%s-%s.%s", base_name, sha1_to_hex(sha1), "idx"); | |
1016 | ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666); | |
1017 | if (ofd < 0) | |
1018 | die("unable to open %s (%s)", name, strerror(errno)); | |
1019 | if (copy_fd(ifd_ix, ofd)) | |
1020 | exit(1); | |
1021 | close(ifd_ix); | |
1022 | puts(sha1_to_hex(sha1)); | |
1023 | } | |
1024 | ||
1025 | return 1; | |
1026 | } | |
1027 | ||
c323ac7d LT |
1028 | int main(int argc, char **argv) |
1029 | { | |
5f3de58f | 1030 | SHA_CTX ctx; |
27225f2e | 1031 | char line[PATH_MAX + 20]; |
d22b9290 | 1032 | int window = 10, depth = 10, pack_to_stdout = 0; |
84c8d8ae | 1033 | struct object_entry **list; |
c323ac7d LT |
1034 | int i; |
1035 | ||
53228a5f JH |
1036 | setup_git_directory(); |
1037 | ||
c323ac7d LT |
1038 | for (i = 1; i < argc; i++) { |
1039 | const char *arg = argv[i]; | |
1040 | ||
1041 | if (*arg == '-') { | |
1c4a2912 LT |
1042 | if (!strcmp("--non-empty", arg)) { |
1043 | non_empty = 1; | |
1044 | continue; | |
1045 | } | |
64560374 LT |
1046 | if (!strcmp("--local", arg)) { |
1047 | local = 1; | |
1048 | continue; | |
1049 | } | |
eb019375 LT |
1050 | if (!strcmp("--incremental", arg)) { |
1051 | incremental = 1; | |
1052 | continue; | |
1053 | } | |
c323ac7d LT |
1054 | if (!strncmp("--window=", arg, 9)) { |
1055 | char *end; | |
f846bbff | 1056 | window = strtoul(arg+9, &end, 0); |
c323ac7d LT |
1057 | if (!arg[9] || *end) |
1058 | usage(pack_usage); | |
1059 | continue; | |
1060 | } | |
d116a45a LT |
1061 | if (!strncmp("--depth=", arg, 8)) { |
1062 | char *end; | |
1063 | depth = strtoul(arg+8, &end, 0); | |
1064 | if (!arg[8] || *end) | |
1065 | usage(pack_usage); | |
1066 | continue; | |
1067 | } | |
024701f1 JH |
1068 | if (!strcmp("-q", arg)) { |
1069 | progress = 0; | |
1070 | continue; | |
1071 | } | |
ab7cd7bb JH |
1072 | if (!strcmp("--no-reuse-delta", arg)) { |
1073 | no_reuse_delta = 1; | |
1074 | continue; | |
1075 | } | |
d22b9290 LT |
1076 | if (!strcmp("--stdout", arg)) { |
1077 | pack_to_stdout = 1; | |
1078 | continue; | |
1079 | } | |
c323ac7d LT |
1080 | usage(pack_usage); |
1081 | } | |
1082 | if (base_name) | |
1083 | usage(pack_usage); | |
1084 | base_name = arg; | |
1085 | } | |
1086 | ||
d22b9290 | 1087 | if (pack_to_stdout != !base_name) |
c323ac7d LT |
1088 | usage(pack_usage); |
1089 | ||
64560374 | 1090 | prepare_packed_git(); |
b2504a0d | 1091 | |
21fcd1bd | 1092 | if (progress) { |
b2504a0d NP |
1093 | struct itimerval v; |
1094 | v.it_interval.tv_sec = 1; | |
1095 | v.it_interval.tv_usec = 0; | |
1096 | v.it_value = v.it_interval; | |
1097 | signal(SIGALRM, progress_interval); | |
1098 | setitimer(ITIMER_REAL, &v, NULL); | |
21fcd1bd | 1099 | fprintf(stderr, "Generating pack...\n"); |
21fcd1bd | 1100 | } |
b2504a0d | 1101 | |
c323ac7d LT |
1102 | while (fgets(line, sizeof(line), stdin) != NULL) { |
1103 | unsigned char sha1[20]; | |
27225f2e | 1104 | |
7a979d99 JH |
1105 | if (line[0] == '-') { |
1106 | if (get_sha1_hex(line+1, sha1)) | |
1107 | die("expected edge sha1, got garbage:\n %s", | |
1108 | line+1); | |
1109 | add_preferred_base(sha1); | |
1110 | continue; | |
21fcd1bd | 1111 | } |
c323ac7d | 1112 | if (get_sha1_hex(line, sha1)) |
ef07618f | 1113 | die("expected sha1, got garbage:\n %s", line); |
1d6b38cc | 1114 | add_object_entry(sha1, name_hash(NULL, line+41), 0); |
c323ac7d | 1115 | } |
21fcd1bd JH |
1116 | if (progress) |
1117 | fprintf(stderr, "Done counting %d objects.\n", nr_objects); | |
f0b0af1b JH |
1118 | sorted_by_sha = create_final_object_list(); |
1119 | if (non_empty && !nr_result) | |
1c4a2912 | 1120 | return 0; |
c323ac7d | 1121 | |
84c8d8ae JH |
1122 | SHA1_Init(&ctx); |
1123 | list = sorted_by_sha; | |
7a979d99 | 1124 | for (i = 0; i < nr_result; i++) { |
84c8d8ae JH |
1125 | struct object_entry *entry = *list++; |
1126 | SHA1_Update(&ctx, entry->sha1, 20); | |
1127 | } | |
1128 | SHA1_Final(object_list_sha1, &ctx); | |
7a979d99 JH |
1129 | if (progress && (nr_objects != nr_result)) |
1130 | fprintf(stderr, "Result has %d objects.\n", nr_result); | |
84c8d8ae | 1131 | |
f3123c4a JH |
1132 | if (reuse_cached_pack(object_list_sha1, pack_to_stdout)) |
1133 | ; | |
1134 | else { | |
f0b0af1b JH |
1135 | if (nr_result) |
1136 | prepare_pack(window, depth); | |
5e8dc750 NP |
1137 | if (progress && pack_to_stdout) { |
1138 | /* the other end usually displays progress itself */ | |
1139 | struct itimerval v = {{0,},}; | |
1140 | setitimer(ITIMER_REAL, &v, NULL); | |
1141 | signal(SIGALRM, SIG_IGN ); | |
1142 | progress_update = 0; | |
1143 | } | |
1144 | write_pack_file(); | |
f3123c4a JH |
1145 | if (!pack_to_stdout) { |
1146 | write_index_file(); | |
1147 | puts(sha1_to_hex(object_list_sha1)); | |
1148 | } | |
5f3de58f | 1149 | } |
ab7cd7bb JH |
1150 | if (progress) |
1151 | fprintf(stderr, "Total %d, written %d (delta %d), reused %d (delta %d)\n", | |
7a979d99 | 1152 | nr_result, written, written_delta, reused, reused_delta); |
c323ac7d LT |
1153 | return 0; |
1154 | } |