index-pack.con commit Finally implement "git log --follow" (750f7b6)
   1#include "cache.h"
   2#include "delta.h"
   3#include "pack.h"
   4#include "csum-file.h"
   5#include "blob.h"
   6#include "commit.h"
   7#include "tag.h"
   8#include "tree.h"
   9#include "progress.h"
  10
  11static const char index_pack_usage[] =
  12"git-index-pack [-v] [-o <index-file>] [{ ---keep | --keep=<msg> }] { <pack-file> | --stdin [--fix-thin] [<pack-file>] }";
  13
  14struct object_entry
  15{
  16        struct pack_idx_entry idx;
  17        unsigned long size;
  18        unsigned int hdr_size;
  19        enum object_type type;
  20        enum object_type real_type;
  21};
  22
  23union delta_base {
  24        unsigned char sha1[20];
  25        off_t offset;
  26};
  27
  28/*
  29 * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want
  30 * to memcmp() only the first 20 bytes.
  31 */
  32#define UNION_BASE_SZ   20
  33
  34struct delta_entry
  35{
  36        union delta_base base;
  37        int obj_no;
  38};
  39
  40static struct object_entry *objects;
  41static struct delta_entry *deltas;
  42static int nr_objects;
  43static int nr_deltas;
  44static int nr_resolved_deltas;
  45
  46static int from_stdin;
  47static int verbose;
  48
  49static struct progress progress;
  50
  51/* We always read in 4kB chunks. */
  52static unsigned char input_buffer[4096];
  53static unsigned int input_offset, input_len;
  54static off_t consumed_bytes;
  55static SHA_CTX input_ctx;
  56static uint32_t input_crc32;
  57static int input_fd, output_fd, pack_fd;
  58
  59/* Discard current buffer used content. */
  60static void flush(void)
  61{
  62        if (input_offset) {
  63                if (output_fd >= 0)
  64                        write_or_die(output_fd, input_buffer, input_offset);
  65                SHA1_Update(&input_ctx, input_buffer, input_offset);
  66                memmove(input_buffer, input_buffer + input_offset, input_len);
  67                input_offset = 0;
  68        }
  69}
  70
  71/*
  72 * Make sure at least "min" bytes are available in the buffer, and
  73 * return the pointer to the buffer.
  74 */
  75static void *fill(int min)
  76{
  77        if (min <= input_len)
  78                return input_buffer + input_offset;
  79        if (min > sizeof(input_buffer))
  80                die("cannot fill %d bytes", min);
  81        flush();
  82        do {
  83                ssize_t ret = xread(input_fd, input_buffer + input_len,
  84                                sizeof(input_buffer) - input_len);
  85                if (ret <= 0) {
  86                        if (!ret)
  87                                die("early EOF");
  88                        die("read error on input: %s", strerror(errno));
  89                }
  90                input_len += ret;
  91        } while (input_len < min);
  92        return input_buffer;
  93}
  94
  95static void use(int bytes)
  96{
  97        if (bytes > input_len)
  98                die("used more bytes than were available");
  99        input_crc32 = crc32(input_crc32, input_buffer + input_offset, bytes);
 100        input_len -= bytes;
 101        input_offset += bytes;
 102
 103        /* make sure off_t is sufficiently large not to wrap */
 104        if (consumed_bytes > consumed_bytes + bytes)
 105                die("pack too large for current definition of off_t");
 106        consumed_bytes += bytes;
 107}
 108
 109static const char *open_pack_file(const char *pack_name)
 110{
 111        if (from_stdin) {
 112                input_fd = 0;
 113                if (!pack_name) {
 114                        static char tmpfile[PATH_MAX];
 115                        snprintf(tmpfile, sizeof(tmpfile),
 116                                 "%s/tmp_pack_XXXXXX", get_object_directory());
 117                        output_fd = mkstemp(tmpfile);
 118                        pack_name = xstrdup(tmpfile);
 119                } else
 120                        output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
 121                if (output_fd < 0)
 122                        die("unable to create %s: %s\n", pack_name, strerror(errno));
 123                pack_fd = output_fd;
 124        } else {
 125                input_fd = open(pack_name, O_RDONLY);
 126                if (input_fd < 0)
 127                        die("cannot open packfile '%s': %s",
 128                            pack_name, strerror(errno));
 129                output_fd = -1;
 130                pack_fd = input_fd;
 131        }
 132        SHA1_Init(&input_ctx);
 133        return pack_name;
 134}
 135
 136static void parse_pack_header(void)
 137{
 138        struct pack_header *hdr = fill(sizeof(struct pack_header));
 139
 140        /* Header consistency check */
 141        if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
 142                die("pack signature mismatch");
 143        if (!pack_version_ok(hdr->hdr_version))
 144                die("pack version %d unsupported", ntohl(hdr->hdr_version));
 145
 146        nr_objects = ntohl(hdr->hdr_entries);
 147        use(sizeof(struct pack_header));
 148}
 149
 150static void bad_object(unsigned long offset, const char *format,
 151                       ...) NORETURN __attribute__((format (printf, 2, 3)));
 152
 153static void bad_object(unsigned long offset, const char *format, ...)
 154{
 155        va_list params;
 156        char buf[1024];
 157
 158        va_start(params, format);
 159        vsnprintf(buf, sizeof(buf), format, params);
 160        va_end(params);
 161        die("pack has bad object at offset %lu: %s", offset, buf);
 162}
 163
 164static void *unpack_entry_data(unsigned long offset, unsigned long size)
 165{
 166        z_stream stream;
 167        void *buf = xmalloc(size);
 168
 169        memset(&stream, 0, sizeof(stream));
 170        stream.next_out = buf;
 171        stream.avail_out = size;
 172        stream.next_in = fill(1);
 173        stream.avail_in = input_len;
 174        inflateInit(&stream);
 175
 176        for (;;) {
 177                int ret = inflate(&stream, 0);
 178                use(input_len - stream.avail_in);
 179                if (stream.total_out == size && ret == Z_STREAM_END)
 180                        break;
 181                if (ret != Z_OK)
 182                        bad_object(offset, "inflate returned %d", ret);
 183                stream.next_in = fill(1);
 184                stream.avail_in = input_len;
 185        }
 186        inflateEnd(&stream);
 187        return buf;
 188}
 189
 190static void *unpack_raw_entry(struct object_entry *obj, union delta_base *delta_base)
 191{
 192        unsigned char *p, c;
 193        unsigned long size;
 194        off_t base_offset;
 195        unsigned shift;
 196        void *data;
 197
 198        obj->idx.offset = consumed_bytes;
 199        input_crc32 = crc32(0, Z_NULL, 0);
 200
 201        p = fill(1);
 202        c = *p;
 203        use(1);
 204        obj->type = (c >> 4) & 7;
 205        size = (c & 15);
 206        shift = 4;
 207        while (c & 0x80) {
 208                p = fill(1);
 209                c = *p;
 210                use(1);
 211                size += (c & 0x7fUL) << shift;
 212                shift += 7;
 213        }
 214        obj->size = size;
 215
 216        switch (obj->type) {
 217        case OBJ_REF_DELTA:
 218                hashcpy(delta_base->sha1, fill(20));
 219                use(20);
 220                break;
 221        case OBJ_OFS_DELTA:
 222                memset(delta_base, 0, sizeof(*delta_base));
 223                p = fill(1);
 224                c = *p;
 225                use(1);
 226                base_offset = c & 127;
 227                while (c & 128) {
 228                        base_offset += 1;
 229                        if (!base_offset || MSB(base_offset, 7))
 230                                bad_object(obj->idx.offset, "offset value overflow for delta base object");
 231                        p = fill(1);
 232                        c = *p;
 233                        use(1);
 234                        base_offset = (base_offset << 7) + (c & 127);
 235                }
 236                delta_base->offset = obj->idx.offset - base_offset;
 237                if (delta_base->offset >= obj->idx.offset)
 238                        bad_object(obj->idx.offset, "delta base offset is out of bound");
 239                break;
 240        case OBJ_COMMIT:
 241        case OBJ_TREE:
 242        case OBJ_BLOB:
 243        case OBJ_TAG:
 244                break;
 245        default:
 246                bad_object(obj->idx.offset, "unknown object type %d", obj->type);
 247        }
 248        obj->hdr_size = consumed_bytes - obj->idx.offset;
 249
 250        data = unpack_entry_data(obj->idx.offset, obj->size);
 251        obj->idx.crc32 = input_crc32;
 252        return data;
 253}
 254
 255static void *get_data_from_pack(struct object_entry *obj)
 256{
 257        unsigned long from = obj[0].idx.offset + obj[0].hdr_size;
 258        unsigned long len = obj[1].idx.offset - from;
 259        unsigned long rdy = 0;
 260        unsigned char *src, *data;
 261        z_stream stream;
 262        int st;
 263
 264        src = xmalloc(len);
 265        data = src;
 266        do {
 267                ssize_t n = pread(pack_fd, data + rdy, len - rdy, from + rdy);
 268                if (n <= 0)
 269                        die("cannot pread pack file: %s", strerror(errno));
 270                rdy += n;
 271        } while (rdy < len);
 272        data = xmalloc(obj->size);
 273        memset(&stream, 0, sizeof(stream));
 274        stream.next_out = data;
 275        stream.avail_out = obj->size;
 276        stream.next_in = src;
 277        stream.avail_in = len;
 278        inflateInit(&stream);
 279        while ((st = inflate(&stream, Z_FINISH)) == Z_OK);
 280        inflateEnd(&stream);
 281        if (st != Z_STREAM_END || stream.total_out != obj->size)
 282                die("serious inflate inconsistency");
 283        free(src);
 284        return data;
 285}
 286
 287static int find_delta(const union delta_base *base)
 288{
 289        int first = 0, last = nr_deltas;
 290
 291        while (first < last) {
 292                int next = (first + last) / 2;
 293                struct delta_entry *delta = &deltas[next];
 294                int cmp;
 295
 296                cmp = memcmp(base, &delta->base, UNION_BASE_SZ);
 297                if (!cmp)
 298                        return next;
 299                if (cmp < 0) {
 300                        last = next;
 301                        continue;
 302                }
 303                first = next+1;
 304        }
 305        return -first-1;
 306}
 307
 308static int find_delta_children(const union delta_base *base,
 309                               int *first_index, int *last_index)
 310{
 311        int first = find_delta(base);
 312        int last = first;
 313        int end = nr_deltas - 1;
 314
 315        if (first < 0)
 316                return -1;
 317        while (first > 0 && !memcmp(&deltas[first - 1].base, base, UNION_BASE_SZ))
 318                --first;
 319        while (last < end && !memcmp(&deltas[last + 1].base, base, UNION_BASE_SZ))
 320                ++last;
 321        *first_index = first;
 322        *last_index = last;
 323        return 0;
 324}
 325
 326static void sha1_object(const void *data, unsigned long size,
 327                        enum object_type type, unsigned char *sha1)
 328{
 329        hash_sha1_file(data, size, typename(type), sha1);
 330        if (has_sha1_file(sha1)) {
 331                void *has_data;
 332                enum object_type has_type;
 333                unsigned long has_size;
 334                has_data = read_sha1_file(sha1, &has_type, &has_size);
 335                if (!has_data)
 336                        die("cannot read existing object %s", sha1_to_hex(sha1));
 337                if (size != has_size || type != has_type ||
 338                    memcmp(data, has_data, size) != 0)
 339                        die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1));
 340                free(has_data);
 341        }
 342}
 343
 344static void resolve_delta(struct object_entry *delta_obj, void *base_data,
 345                          unsigned long base_size, enum object_type type)
 346{
 347        void *delta_data;
 348        unsigned long delta_size;
 349        void *result;
 350        unsigned long result_size;
 351        union delta_base delta_base;
 352        int j, first, last;
 353
 354        delta_obj->real_type = type;
 355        delta_data = get_data_from_pack(delta_obj);
 356        delta_size = delta_obj->size;
 357        result = patch_delta(base_data, base_size, delta_data, delta_size,
 358                             &result_size);
 359        free(delta_data);
 360        if (!result)
 361                bad_object(delta_obj->idx.offset, "failed to apply delta");
 362        sha1_object(result, result_size, type, delta_obj->idx.sha1);
 363        nr_resolved_deltas++;
 364
 365        hashcpy(delta_base.sha1, delta_obj->idx.sha1);
 366        if (!find_delta_children(&delta_base, &first, &last)) {
 367                for (j = first; j <= last; j++) {
 368                        struct object_entry *child = objects + deltas[j].obj_no;
 369                        if (child->real_type == OBJ_REF_DELTA)
 370                                resolve_delta(child, result, result_size, type);
 371                }
 372        }
 373
 374        memset(&delta_base, 0, sizeof(delta_base));
 375        delta_base.offset = delta_obj->idx.offset;
 376        if (!find_delta_children(&delta_base, &first, &last)) {
 377                for (j = first; j <= last; j++) {
 378                        struct object_entry *child = objects + deltas[j].obj_no;
 379                        if (child->real_type == OBJ_OFS_DELTA)
 380                                resolve_delta(child, result, result_size, type);
 381                }
 382        }
 383
 384        free(result);
 385}
 386
 387static int compare_delta_entry(const void *a, const void *b)
 388{
 389        const struct delta_entry *delta_a = a;
 390        const struct delta_entry *delta_b = b;
 391        return memcmp(&delta_a->base, &delta_b->base, UNION_BASE_SZ);
 392}
 393
 394/* Parse all objects and return the pack content SHA1 hash */
 395static void parse_pack_objects(unsigned char *sha1)
 396{
 397        int i;
 398        struct delta_entry *delta = deltas;
 399        void *data;
 400        struct stat st;
 401
 402        /*
 403         * First pass:
 404         * - find locations of all objects;
 405         * - calculate SHA1 of all non-delta objects;
 406         * - remember base (SHA1 or offset) for all deltas.
 407         */
 408        if (verbose)
 409                start_progress(&progress, "Indexing %u objects...", "", nr_objects);
 410        for (i = 0; i < nr_objects; i++) {
 411                struct object_entry *obj = &objects[i];
 412                data = unpack_raw_entry(obj, &delta->base);
 413                obj->real_type = obj->type;
 414                if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) {
 415                        nr_deltas++;
 416                        delta->obj_no = i;
 417                        delta++;
 418                } else
 419                        sha1_object(data, obj->size, obj->type, obj->idx.sha1);
 420                free(data);
 421                if (verbose)
 422                        display_progress(&progress, i+1);
 423        }
 424        objects[i].idx.offset = consumed_bytes;
 425        if (verbose)
 426                stop_progress(&progress);
 427
 428        /* Check pack integrity */
 429        flush();
 430        SHA1_Final(sha1, &input_ctx);
 431        if (hashcmp(fill(20), sha1))
 432                die("pack is corrupted (SHA1 mismatch)");
 433        use(20);
 434
 435        /* If input_fd is a file, we should have reached its end now. */
 436        if (fstat(input_fd, &st))
 437                die("cannot fstat packfile: %s", strerror(errno));
 438        if (S_ISREG(st.st_mode) &&
 439                        lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size)
 440                die("pack has junk at the end");
 441
 442        if (!nr_deltas)
 443                return;
 444
 445        /* Sort deltas by base SHA1/offset for fast searching */
 446        qsort(deltas, nr_deltas, sizeof(struct delta_entry),
 447              compare_delta_entry);
 448
 449        /*
 450         * Second pass:
 451         * - for all non-delta objects, look if it is used as a base for
 452         *   deltas;
 453         * - if used as a base, uncompress the object and apply all deltas,
 454         *   recursively checking if the resulting object is used as a base
 455         *   for some more deltas.
 456         */
 457        if (verbose)
 458                start_progress(&progress, "Resolving %u deltas...", "", nr_deltas);
 459        for (i = 0; i < nr_objects; i++) {
 460                struct object_entry *obj = &objects[i];
 461                union delta_base base;
 462                int j, ref, ref_first, ref_last, ofs, ofs_first, ofs_last;
 463
 464                if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA)
 465                        continue;
 466                hashcpy(base.sha1, obj->idx.sha1);
 467                ref = !find_delta_children(&base, &ref_first, &ref_last);
 468                memset(&base, 0, sizeof(base));
 469                base.offset = obj->idx.offset;
 470                ofs = !find_delta_children(&base, &ofs_first, &ofs_last);
 471                if (!ref && !ofs)
 472                        continue;
 473                data = get_data_from_pack(obj);
 474                if (ref)
 475                        for (j = ref_first; j <= ref_last; j++) {
 476                                struct object_entry *child = objects + deltas[j].obj_no;
 477                                if (child->real_type == OBJ_REF_DELTA)
 478                                        resolve_delta(child, data,
 479                                                      obj->size, obj->type);
 480                        }
 481                if (ofs)
 482                        for (j = ofs_first; j <= ofs_last; j++) {
 483                                struct object_entry *child = objects + deltas[j].obj_no;
 484                                if (child->real_type == OBJ_OFS_DELTA)
 485                                        resolve_delta(child, data,
 486                                                      obj->size, obj->type);
 487                        }
 488                free(data);
 489                if (verbose)
 490                        display_progress(&progress, nr_resolved_deltas);
 491        }
 492}
 493
 494static int write_compressed(int fd, void *in, unsigned int size, uint32_t *obj_crc)
 495{
 496        z_stream stream;
 497        unsigned long maxsize;
 498        void *out;
 499
 500        memset(&stream, 0, sizeof(stream));
 501        deflateInit(&stream, zlib_compression_level);
 502        maxsize = deflateBound(&stream, size);
 503        out = xmalloc(maxsize);
 504
 505        /* Compress it */
 506        stream.next_in = in;
 507        stream.avail_in = size;
 508        stream.next_out = out;
 509        stream.avail_out = maxsize;
 510        while (deflate(&stream, Z_FINISH) == Z_OK);
 511        deflateEnd(&stream);
 512
 513        size = stream.total_out;
 514        write_or_die(fd, out, size);
 515        *obj_crc = crc32(*obj_crc, out, size);
 516        free(out);
 517        return size;
 518}
 519
 520static void append_obj_to_pack(const unsigned char *sha1, void *buf,
 521                               unsigned long size, enum object_type type)
 522{
 523        struct object_entry *obj = &objects[nr_objects++];
 524        unsigned char header[10];
 525        unsigned long s = size;
 526        int n = 0;
 527        unsigned char c = (type << 4) | (s & 15);
 528        s >>= 4;
 529        while (s) {
 530                header[n++] = c | 0x80;
 531                c = s & 0x7f;
 532                s >>= 7;
 533        }
 534        header[n++] = c;
 535        write_or_die(output_fd, header, n);
 536        obj[0].idx.crc32 = crc32(0, Z_NULL, 0);
 537        obj[0].idx.crc32 = crc32(obj[0].idx.crc32, header, n);
 538        obj[1].idx.offset = obj[0].idx.offset + n;
 539        obj[1].idx.offset += write_compressed(output_fd, buf, size, &obj[0].idx.crc32);
 540        hashcpy(obj->idx.sha1, sha1);
 541}
 542
 543static int delta_pos_compare(const void *_a, const void *_b)
 544{
 545        struct delta_entry *a = *(struct delta_entry **)_a;
 546        struct delta_entry *b = *(struct delta_entry **)_b;
 547        return a->obj_no - b->obj_no;
 548}
 549
 550static void fix_unresolved_deltas(int nr_unresolved)
 551{
 552        struct delta_entry **sorted_by_pos;
 553        int i, n = 0;
 554
 555        /*
 556         * Since many unresolved deltas may well be themselves base objects
 557         * for more unresolved deltas, we really want to include the
 558         * smallest number of base objects that would cover as much delta
 559         * as possible by picking the
 560         * trunc deltas first, allowing for other deltas to resolve without
 561         * additional base objects.  Since most base objects are to be found
 562         * before deltas depending on them, a good heuristic is to start
 563         * resolving deltas in the same order as their position in the pack.
 564         */
 565        sorted_by_pos = xmalloc(nr_unresolved * sizeof(*sorted_by_pos));
 566        for (i = 0; i < nr_deltas; i++) {
 567                if (objects[deltas[i].obj_no].real_type != OBJ_REF_DELTA)
 568                        continue;
 569                sorted_by_pos[n++] = &deltas[i];
 570        }
 571        qsort(sorted_by_pos, n, sizeof(*sorted_by_pos), delta_pos_compare);
 572
 573        for (i = 0; i < n; i++) {
 574                struct delta_entry *d = sorted_by_pos[i];
 575                void *data;
 576                unsigned long size;
 577                enum object_type type;
 578                int j, first, last;
 579
 580                if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
 581                        continue;
 582                data = read_sha1_file(d->base.sha1, &type, &size);
 583                if (!data)
 584                        continue;
 585
 586                find_delta_children(&d->base, &first, &last);
 587                for (j = first; j <= last; j++) {
 588                        struct object_entry *child = objects + deltas[j].obj_no;
 589                        if (child->real_type == OBJ_REF_DELTA)
 590                                resolve_delta(child, data, size, type);
 591                }
 592
 593                if (check_sha1_signature(d->base.sha1, data, size, typename(type)))
 594                        die("local object %s is corrupt", sha1_to_hex(d->base.sha1));
 595                append_obj_to_pack(d->base.sha1, data, size, type);
 596                free(data);
 597                if (verbose)
 598                        display_progress(&progress, nr_resolved_deltas);
 599        }
 600        free(sorted_by_pos);
 601}
 602
 603static void final(const char *final_pack_name, const char *curr_pack_name,
 604                  const char *final_index_name, const char *curr_index_name,
 605                  const char *keep_name, const char *keep_msg,
 606                  unsigned char *sha1)
 607{
 608        const char *report = "pack";
 609        char name[PATH_MAX];
 610        int err;
 611
 612        if (!from_stdin) {
 613                close(input_fd);
 614        } else {
 615                err = close(output_fd);
 616                if (err)
 617                        die("error while closing pack file: %s", strerror(errno));
 618                chmod(curr_pack_name, 0444);
 619        }
 620
 621        if (keep_msg) {
 622                int keep_fd, keep_msg_len = strlen(keep_msg);
 623                if (!keep_name) {
 624                        snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
 625                                 get_object_directory(), sha1_to_hex(sha1));
 626                        keep_name = name;
 627                }
 628                keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600);
 629                if (keep_fd < 0) {
 630                        if (errno != EEXIST)
 631                                die("cannot write keep file");
 632                } else {
 633                        if (keep_msg_len > 0) {
 634                                write_or_die(keep_fd, keep_msg, keep_msg_len);
 635                                write_or_die(keep_fd, "\n", 1);
 636                        }
 637                        close(keep_fd);
 638                        report = "keep";
 639                }
 640        }
 641
 642        if (final_pack_name != curr_pack_name) {
 643                if (!final_pack_name) {
 644                        snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
 645                                 get_object_directory(), sha1_to_hex(sha1));
 646                        final_pack_name = name;
 647                }
 648                if (move_temp_to_file(curr_pack_name, final_pack_name))
 649                        die("cannot store pack file");
 650        }
 651
 652        chmod(curr_index_name, 0444);
 653        if (final_index_name != curr_index_name) {
 654                if (!final_index_name) {
 655                        snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
 656                                 get_object_directory(), sha1_to_hex(sha1));
 657                        final_index_name = name;
 658                }
 659                if (move_temp_to_file(curr_index_name, final_index_name))
 660                        die("cannot store index file");
 661        }
 662
 663        if (!from_stdin) {
 664                printf("%s\n", sha1_to_hex(sha1));
 665        } else {
 666                char buf[48];
 667                int len = snprintf(buf, sizeof(buf), "%s\t%s\n",
 668                                   report, sha1_to_hex(sha1));
 669                write_or_die(1, buf, len);
 670
 671                /*
 672                 * Let's just mimic git-unpack-objects here and write
 673                 * the last part of the input buffer to stdout.
 674                 */
 675                while (input_len) {
 676                        err = xwrite(1, input_buffer + input_offset, input_len);
 677                        if (err <= 0)
 678                                break;
 679                        input_len -= err;
 680                        input_offset += err;
 681                }
 682        }
 683}
 684
 685int main(int argc, char **argv)
 686{
 687        int i, fix_thin_pack = 0;
 688        const char *curr_pack, *pack_name = NULL;
 689        const char *curr_index, *index_name = NULL;
 690        const char *keep_name = NULL, *keep_msg = NULL;
 691        char *index_name_buf = NULL, *keep_name_buf = NULL;
 692        struct pack_idx_entry **idx_objects;
 693        unsigned char sha1[20];
 694
 695        for (i = 1; i < argc; i++) {
 696                const char *arg = argv[i];
 697
 698                if (*arg == '-') {
 699                        if (!strcmp(arg, "--stdin")) {
 700                                from_stdin = 1;
 701                        } else if (!strcmp(arg, "--fix-thin")) {
 702                                fix_thin_pack = 1;
 703                        } else if (!strcmp(arg, "--keep")) {
 704                                keep_msg = "";
 705                        } else if (!prefixcmp(arg, "--keep=")) {
 706                                keep_msg = arg + 7;
 707                        } else if (!prefixcmp(arg, "--pack_header=")) {
 708                                struct pack_header *hdr;
 709                                char *c;
 710
 711                                hdr = (struct pack_header *)input_buffer;
 712                                hdr->hdr_signature = htonl(PACK_SIGNATURE);
 713                                hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
 714                                if (*c != ',')
 715                                        die("bad %s", arg);
 716                                hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
 717                                if (*c)
 718                                        die("bad %s", arg);
 719                                input_len = sizeof(*hdr);
 720                        } else if (!strcmp(arg, "-v")) {
 721                                verbose = 1;
 722                        } else if (!strcmp(arg, "-o")) {
 723                                if (index_name || (i+1) >= argc)
 724                                        usage(index_pack_usage);
 725                                index_name = argv[++i];
 726                        } else if (!prefixcmp(arg, "--index-version=")) {
 727                                char *c;
 728                                pack_idx_default_version = strtoul(arg + 16, &c, 10);
 729                                if (pack_idx_default_version > 2)
 730                                        die("bad %s", arg);
 731                                if (*c == ',')
 732                                        pack_idx_off32_limit = strtoul(c+1, &c, 0);
 733                                if (*c || pack_idx_off32_limit & 0x80000000)
 734                                        die("bad %s", arg);
 735                        } else
 736                                usage(index_pack_usage);
 737                        continue;
 738                }
 739
 740                if (pack_name)
 741                        usage(index_pack_usage);
 742                pack_name = arg;
 743        }
 744
 745        if (!pack_name && !from_stdin)
 746                usage(index_pack_usage);
 747        if (fix_thin_pack && !from_stdin)
 748                die("--fix-thin cannot be used without --stdin");
 749        if (!index_name && pack_name) {
 750                int len = strlen(pack_name);
 751                if (!has_extension(pack_name, ".pack"))
 752                        die("packfile name '%s' does not end with '.pack'",
 753                            pack_name);
 754                index_name_buf = xmalloc(len);
 755                memcpy(index_name_buf, pack_name, len - 5);
 756                strcpy(index_name_buf + len - 5, ".idx");
 757                index_name = index_name_buf;
 758        }
 759        if (keep_msg && !keep_name && pack_name) {
 760                int len = strlen(pack_name);
 761                if (!has_extension(pack_name, ".pack"))
 762                        die("packfile name '%s' does not end with '.pack'",
 763                            pack_name);
 764                keep_name_buf = xmalloc(len);
 765                memcpy(keep_name_buf, pack_name, len - 5);
 766                strcpy(keep_name_buf + len - 5, ".keep");
 767                keep_name = keep_name_buf;
 768        }
 769
 770        curr_pack = open_pack_file(pack_name);
 771        parse_pack_header();
 772        objects = xmalloc((nr_objects + 1) * sizeof(struct object_entry));
 773        deltas = xmalloc(nr_objects * sizeof(struct delta_entry));
 774        parse_pack_objects(sha1);
 775        if (nr_deltas == nr_resolved_deltas) {
 776                if (verbose)
 777                        stop_progress(&progress);
 778                /* Flush remaining pack final 20-byte SHA1. */
 779                flush();
 780        } else {
 781                if (fix_thin_pack) {
 782                        int nr_unresolved = nr_deltas - nr_resolved_deltas;
 783                        int nr_objects_initial = nr_objects;
 784                        if (nr_unresolved <= 0)
 785                                die("confusion beyond insanity");
 786                        objects = xrealloc(objects,
 787                                           (nr_objects + nr_unresolved + 1)
 788                                           * sizeof(*objects));
 789                        fix_unresolved_deltas(nr_unresolved);
 790                        if (verbose) {
 791                                stop_progress(&progress);
 792                                fprintf(stderr, "%d objects were added to complete this thin pack.\n",
 793                                        nr_objects - nr_objects_initial);
 794                        }
 795                        fixup_pack_header_footer(output_fd, sha1,
 796                                curr_pack, nr_objects);
 797                }
 798                if (nr_deltas != nr_resolved_deltas)
 799                        die("pack has %d unresolved deltas",
 800                            nr_deltas - nr_resolved_deltas);
 801        }
 802        free(deltas);
 803
 804        idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *));
 805        for (i = 0; i < nr_objects; i++)
 806                idx_objects[i] = &objects[i].idx;
 807        curr_index = write_idx_file(index_name, idx_objects, nr_objects, sha1);
 808        free(idx_objects);
 809
 810        final(pack_name, curr_pack,
 811                index_name, curr_index,
 812                keep_name, keep_msg,
 813                sha1);
 814        free(objects);
 815        free(index_name_buf);
 816        free(keep_name_buf);
 817
 818        return 0;
 819}