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