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