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