fast-import.con commit git-svn: gracefully handle --follow-parent failures (c7eba71)
   1/*
   2Format of STDIN stream:
   3
   4  stream ::= cmd*;
   5
   6  cmd ::= new_blob
   7        | new_commit
   8        | new_tag
   9        | reset_branch
  10        | checkpoint
  11        ;
  12
  13  new_blob ::= 'blob' lf
  14        mark?
  15    file_content;
  16  file_content ::= data;
  17
  18  new_commit ::= 'commit' sp ref_str lf
  19    mark?
  20    ('author' sp name '<' email '>' when lf)?
  21    'committer' sp name '<' email '>' when lf
  22    commit_msg
  23    ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
  24    ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)*
  25    file_change*
  26    lf;
  27  commit_msg ::= data;
  28
  29  file_change ::= file_clr | file_del | file_obm | file_inm;
  30  file_clr ::= 'deleteall' lf;
  31  file_del ::= 'D' sp path_str lf;
  32  file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
  33  file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
  34    data;
  35
  36  new_tag ::= 'tag' sp tag_str lf
  37    'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
  38        'tagger' sp name '<' email '>' when lf
  39    tag_msg;
  40  tag_msg ::= data;
  41
  42  reset_branch ::= 'reset' sp ref_str lf
  43    ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
  44    lf;
  45
  46  checkpoint ::= 'checkpoint' lf
  47    lf;
  48
  49     # note: the first idnum in a stream should be 1 and subsequent
  50     # idnums should not have gaps between values as this will cause
  51     # the stream parser to reserve space for the gapped values.  An
  52         # idnum can be updated in the future to a new object by issuing
  53     # a new mark directive with the old idnum.
  54         #
  55  mark ::= 'mark' sp idnum lf;
  56  data ::= (delimited_data | exact_data)
  57    lf;
  58
  59    # note: delim may be any string but must not contain lf.
  60    # data_line may contain any data but must not be exactly
  61    # delim.
  62  delimited_data ::= 'data' sp '<<' delim lf
  63    (data_line lf)*
  64        delim lf;
  65
  66     # note: declen indicates the length of binary_data in bytes.
  67     # declen does not include the lf preceeding the binary data.
  68     #
  69  exact_data ::= 'data' sp declen lf
  70    binary_data;
  71
  72     # note: quoted strings are C-style quoting supporting \c for
  73     # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
  74         # is the signed byte value in octal.  Note that the only
  75     # characters which must actually be escaped to protect the
  76     # stream formatting is: \, " and LF.  Otherwise these values
  77         # are UTF8.
  78     #
  79  ref_str     ::= ref;
  80  sha1exp_str ::= sha1exp;
  81  tag_str     ::= tag;
  82  path_str    ::= path    | '"' quoted(path)    '"' ;
  83  mode        ::= '100644' | '644'
  84                | '100755' | '755'
  85                | '120000'
  86                ;
  87
  88  declen ::= # unsigned 32 bit value, ascii base10 notation;
  89  bigint ::= # unsigned integer value, ascii base10 notation;
  90  binary_data ::= # file content, not interpreted;
  91
  92  when         ::= raw_when | rfc2822_when;
  93  raw_when     ::= ts sp tz;
  94  rfc2822_when ::= # Valid RFC 2822 date and time;
  95
  96  sp ::= # ASCII space character;
  97  lf ::= # ASCII newline (LF) character;
  98
  99     # note: a colon (':') must precede the numerical value assigned to
 100         # an idnum.  This is to distinguish it from a ref or tag name as
 101     # GIT does not permit ':' in ref or tag strings.
 102         #
 103  idnum   ::= ':' bigint;
 104  path    ::= # GIT style file path, e.g. "a/b/c";
 105  ref     ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
 106  tag     ::= # GIT tag name, e.g. "FIREFOX_1_5";
 107  sha1exp ::= # Any valid GIT SHA1 expression;
 108  hexsha1 ::= # SHA1 in hexadecimal format;
 109
 110     # note: name and email are UTF8 strings, however name must not
 111         # contain '<' or lf and email must not contain any of the
 112     # following: '<', '>', lf.
 113         #
 114  name  ::= # valid GIT author/committer name;
 115  email ::= # valid GIT author/committer email;
 116  ts    ::= # time since the epoch in seconds, ascii base10 notation;
 117  tz    ::= # GIT style timezone;
 118*/
 119
 120#include "builtin.h"
 121#include "cache.h"
 122#include "object.h"
 123#include "blob.h"
 124#include "tree.h"
 125#include "commit.h"
 126#include "delta.h"
 127#include "pack.h"
 128#include "refs.h"
 129#include "csum-file.h"
 130#include "strbuf.h"
 131#include "quote.h"
 132
 133#define PACK_ID_BITS 16
 134#define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
 135
 136#ifndef PRIuMAX
 137#define PRIuMAX "llu"
 138#endif
 139
 140struct object_entry
 141{
 142        struct object_entry *next;
 143        uint32_t offset;
 144        unsigned type : TYPE_BITS;
 145        unsigned pack_id : PACK_ID_BITS;
 146        unsigned char sha1[20];
 147};
 148
 149struct object_entry_pool
 150{
 151        struct object_entry_pool *next_pool;
 152        struct object_entry *next_free;
 153        struct object_entry *end;
 154        struct object_entry entries[FLEX_ARRAY]; /* more */
 155};
 156
 157struct mark_set
 158{
 159        union {
 160                struct object_entry *marked[1024];
 161                struct mark_set *sets[1024];
 162        } data;
 163        unsigned int shift;
 164};
 165
 166struct last_object
 167{
 168        void *data;
 169        unsigned long len;
 170        uint32_t offset;
 171        unsigned int depth;
 172        unsigned no_free:1;
 173};
 174
 175struct mem_pool
 176{
 177        struct mem_pool *next_pool;
 178        char *next_free;
 179        char *end;
 180        char space[FLEX_ARRAY]; /* more */
 181};
 182
 183struct atom_str
 184{
 185        struct atom_str *next_atom;
 186        unsigned short str_len;
 187        char str_dat[FLEX_ARRAY]; /* more */
 188};
 189
 190struct tree_content;
 191struct tree_entry
 192{
 193        struct tree_content *tree;
 194        struct atom_str* name;
 195        struct tree_entry_ms
 196        {
 197                uint16_t mode;
 198                unsigned char sha1[20];
 199        } versions[2];
 200};
 201
 202struct tree_content
 203{
 204        unsigned int entry_capacity; /* must match avail_tree_content */
 205        unsigned int entry_count;
 206        unsigned int delta_depth;
 207        struct tree_entry *entries[FLEX_ARRAY]; /* more */
 208};
 209
 210struct avail_tree_content
 211{
 212        unsigned int entry_capacity; /* must match tree_content */
 213        struct avail_tree_content *next_avail;
 214};
 215
 216struct branch
 217{
 218        struct branch *table_next_branch;
 219        struct branch *active_next_branch;
 220        const char *name;
 221        struct tree_entry branch_tree;
 222        uintmax_t last_commit;
 223        unsigned int pack_id;
 224        unsigned char sha1[20];
 225};
 226
 227struct tag
 228{
 229        struct tag *next_tag;
 230        const char *name;
 231        unsigned int pack_id;
 232        unsigned char sha1[20];
 233};
 234
 235struct dbuf
 236{
 237        void *buffer;
 238        size_t capacity;
 239};
 240
 241struct hash_list
 242{
 243        struct hash_list *next;
 244        unsigned char sha1[20];
 245};
 246
 247typedef enum {
 248        WHENSPEC_RAW = 1,
 249        WHENSPEC_RFC2822,
 250        WHENSPEC_NOW,
 251} whenspec_type;
 252
 253/* Configured limits on output */
 254static unsigned long max_depth = 10;
 255static unsigned long max_packsize = (1LL << 32) - 1;
 256static int force_update;
 257
 258/* Stats and misc. counters */
 259static uintmax_t alloc_count;
 260static uintmax_t marks_set_count;
 261static uintmax_t object_count_by_type[1 << TYPE_BITS];
 262static uintmax_t duplicate_count_by_type[1 << TYPE_BITS];
 263static uintmax_t delta_count_by_type[1 << TYPE_BITS];
 264static unsigned long object_count;
 265static unsigned long branch_count;
 266static unsigned long branch_load_count;
 267static int failure;
 268static FILE *pack_edges;
 269
 270/* Memory pools */
 271static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
 272static size_t total_allocd;
 273static struct mem_pool *mem_pool;
 274
 275/* Atom management */
 276static unsigned int atom_table_sz = 4451;
 277static unsigned int atom_cnt;
 278static struct atom_str **atom_table;
 279
 280/* The .pack file being generated */
 281static unsigned int pack_id;
 282static struct packed_git *pack_data;
 283static struct packed_git **all_packs;
 284static unsigned long pack_size;
 285
 286/* Table of objects we've written. */
 287static unsigned int object_entry_alloc = 5000;
 288static struct object_entry_pool *blocks;
 289static struct object_entry *object_table[1 << 16];
 290static struct mark_set *marks;
 291static const char* mark_file;
 292
 293/* Our last blob */
 294static struct last_object last_blob;
 295
 296/* Tree management */
 297static unsigned int tree_entry_alloc = 1000;
 298static void *avail_tree_entry;
 299static unsigned int avail_tree_table_sz = 100;
 300static struct avail_tree_content **avail_tree_table;
 301static struct dbuf old_tree;
 302static struct dbuf new_tree;
 303
 304/* Branch data */
 305static unsigned long max_active_branches = 5;
 306static unsigned long cur_active_branches;
 307static unsigned long branch_table_sz = 1039;
 308static struct branch **branch_table;
 309static struct branch *active_branches;
 310
 311/* Tag data */
 312static struct tag *first_tag;
 313static struct tag *last_tag;
 314
 315/* Input stream parsing */
 316static whenspec_type whenspec = WHENSPEC_RAW;
 317static struct strbuf command_buf;
 318static uintmax_t next_mark;
 319static struct dbuf new_data;
 320
 321
 322static void alloc_objects(unsigned int cnt)
 323{
 324        struct object_entry_pool *b;
 325
 326        b = xmalloc(sizeof(struct object_entry_pool)
 327                + cnt * sizeof(struct object_entry));
 328        b->next_pool = blocks;
 329        b->next_free = b->entries;
 330        b->end = b->entries + cnt;
 331        blocks = b;
 332        alloc_count += cnt;
 333}
 334
 335static struct object_entry *new_object(unsigned char *sha1)
 336{
 337        struct object_entry *e;
 338
 339        if (blocks->next_free == blocks->end)
 340                alloc_objects(object_entry_alloc);
 341
 342        e = blocks->next_free++;
 343        hashcpy(e->sha1, sha1);
 344        return e;
 345}
 346
 347static struct object_entry *find_object(unsigned char *sha1)
 348{
 349        unsigned int h = sha1[0] << 8 | sha1[1];
 350        struct object_entry *e;
 351        for (e = object_table[h]; e; e = e->next)
 352                if (!hashcmp(sha1, e->sha1))
 353                        return e;
 354        return NULL;
 355}
 356
 357static struct object_entry *insert_object(unsigned char *sha1)
 358{
 359        unsigned int h = sha1[0] << 8 | sha1[1];
 360        struct object_entry *e = object_table[h];
 361        struct object_entry *p = NULL;
 362
 363        while (e) {
 364                if (!hashcmp(sha1, e->sha1))
 365                        return e;
 366                p = e;
 367                e = e->next;
 368        }
 369
 370        e = new_object(sha1);
 371        e->next = NULL;
 372        e->offset = 0;
 373        if (p)
 374                p->next = e;
 375        else
 376                object_table[h] = e;
 377        return e;
 378}
 379
 380static unsigned int hc_str(const char *s, size_t len)
 381{
 382        unsigned int r = 0;
 383        while (len-- > 0)
 384                r = r * 31 + *s++;
 385        return r;
 386}
 387
 388static void *pool_alloc(size_t len)
 389{
 390        struct mem_pool *p;
 391        void *r;
 392
 393        for (p = mem_pool; p; p = p->next_pool)
 394                if ((p->end - p->next_free >= len))
 395                        break;
 396
 397        if (!p) {
 398                if (len >= (mem_pool_alloc/2)) {
 399                        total_allocd += len;
 400                        return xmalloc(len);
 401                }
 402                total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
 403                p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
 404                p->next_pool = mem_pool;
 405                p->next_free = p->space;
 406                p->end = p->next_free + mem_pool_alloc;
 407                mem_pool = p;
 408        }
 409
 410        r = p->next_free;
 411        /* round out to a pointer alignment */
 412        if (len & (sizeof(void*) - 1))
 413                len += sizeof(void*) - (len & (sizeof(void*) - 1));
 414        p->next_free += len;
 415        return r;
 416}
 417
 418static void *pool_calloc(size_t count, size_t size)
 419{
 420        size_t len = count * size;
 421        void *r = pool_alloc(len);
 422        memset(r, 0, len);
 423        return r;
 424}
 425
 426static char *pool_strdup(const char *s)
 427{
 428        char *r = pool_alloc(strlen(s) + 1);
 429        strcpy(r, s);
 430        return r;
 431}
 432
 433static void size_dbuf(struct dbuf *b, size_t maxlen)
 434{
 435        if (b->buffer) {
 436                if (b->capacity >= maxlen)
 437                        return;
 438                free(b->buffer);
 439        }
 440        b->capacity = ((maxlen / 1024) + 1) * 1024;
 441        b->buffer = xmalloc(b->capacity);
 442}
 443
 444static void insert_mark(uintmax_t idnum, struct object_entry *oe)
 445{
 446        struct mark_set *s = marks;
 447        while ((idnum >> s->shift) >= 1024) {
 448                s = pool_calloc(1, sizeof(struct mark_set));
 449                s->shift = marks->shift + 10;
 450                s->data.sets[0] = marks;
 451                marks = s;
 452        }
 453        while (s->shift) {
 454                uintmax_t i = idnum >> s->shift;
 455                idnum -= i << s->shift;
 456                if (!s->data.sets[i]) {
 457                        s->data.sets[i] = pool_calloc(1, sizeof(struct mark_set));
 458                        s->data.sets[i]->shift = s->shift - 10;
 459                }
 460                s = s->data.sets[i];
 461        }
 462        if (!s->data.marked[idnum])
 463                marks_set_count++;
 464        s->data.marked[idnum] = oe;
 465}
 466
 467static struct object_entry *find_mark(uintmax_t idnum)
 468{
 469        uintmax_t orig_idnum = idnum;
 470        struct mark_set *s = marks;
 471        struct object_entry *oe = NULL;
 472        if ((idnum >> s->shift) < 1024) {
 473                while (s && s->shift) {
 474                        uintmax_t i = idnum >> s->shift;
 475                        idnum -= i << s->shift;
 476                        s = s->data.sets[i];
 477                }
 478                if (s)
 479                        oe = s->data.marked[idnum];
 480        }
 481        if (!oe)
 482                die("mark :%" PRIuMAX " not declared", orig_idnum);
 483        return oe;
 484}
 485
 486static struct atom_str *to_atom(const char *s, unsigned short len)
 487{
 488        unsigned int hc = hc_str(s, len) % atom_table_sz;
 489        struct atom_str *c;
 490
 491        for (c = atom_table[hc]; c; c = c->next_atom)
 492                if (c->str_len == len && !strncmp(s, c->str_dat, len))
 493                        return c;
 494
 495        c = pool_alloc(sizeof(struct atom_str) + len + 1);
 496        c->str_len = len;
 497        strncpy(c->str_dat, s, len);
 498        c->str_dat[len] = 0;
 499        c->next_atom = atom_table[hc];
 500        atom_table[hc] = c;
 501        atom_cnt++;
 502        return c;
 503}
 504
 505static struct branch *lookup_branch(const char *name)
 506{
 507        unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
 508        struct branch *b;
 509
 510        for (b = branch_table[hc]; b; b = b->table_next_branch)
 511                if (!strcmp(name, b->name))
 512                        return b;
 513        return NULL;
 514}
 515
 516static struct branch *new_branch(const char *name)
 517{
 518        unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
 519        struct branch* b = lookup_branch(name);
 520
 521        if (b)
 522                die("Invalid attempt to create duplicate branch: %s", name);
 523        if (check_ref_format(name))
 524                die("Branch name doesn't conform to GIT standards: %s", name);
 525
 526        b = pool_calloc(1, sizeof(struct branch));
 527        b->name = pool_strdup(name);
 528        b->table_next_branch = branch_table[hc];
 529        b->branch_tree.versions[0].mode = S_IFDIR;
 530        b->branch_tree.versions[1].mode = S_IFDIR;
 531        b->pack_id = MAX_PACK_ID;
 532        branch_table[hc] = b;
 533        branch_count++;
 534        return b;
 535}
 536
 537static unsigned int hc_entries(unsigned int cnt)
 538{
 539        cnt = cnt & 7 ? (cnt / 8) + 1 : cnt / 8;
 540        return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
 541}
 542
 543static struct tree_content *new_tree_content(unsigned int cnt)
 544{
 545        struct avail_tree_content *f, *l = NULL;
 546        struct tree_content *t;
 547        unsigned int hc = hc_entries(cnt);
 548
 549        for (f = avail_tree_table[hc]; f; l = f, f = f->next_avail)
 550                if (f->entry_capacity >= cnt)
 551                        break;
 552
 553        if (f) {
 554                if (l)
 555                        l->next_avail = f->next_avail;
 556                else
 557                        avail_tree_table[hc] = f->next_avail;
 558        } else {
 559                cnt = cnt & 7 ? ((cnt / 8) + 1) * 8 : cnt;
 560                f = pool_alloc(sizeof(*t) + sizeof(t->entries[0]) * cnt);
 561                f->entry_capacity = cnt;
 562        }
 563
 564        t = (struct tree_content*)f;
 565        t->entry_count = 0;
 566        t->delta_depth = 0;
 567        return t;
 568}
 569
 570static void release_tree_entry(struct tree_entry *e);
 571static void release_tree_content(struct tree_content *t)
 572{
 573        struct avail_tree_content *f = (struct avail_tree_content*)t;
 574        unsigned int hc = hc_entries(f->entry_capacity);
 575        f->next_avail = avail_tree_table[hc];
 576        avail_tree_table[hc] = f;
 577}
 578
 579static void release_tree_content_recursive(struct tree_content *t)
 580{
 581        unsigned int i;
 582        for (i = 0; i < t->entry_count; i++)
 583                release_tree_entry(t->entries[i]);
 584        release_tree_content(t);
 585}
 586
 587static struct tree_content *grow_tree_content(
 588        struct tree_content *t,
 589        int amt)
 590{
 591        struct tree_content *r = new_tree_content(t->entry_count + amt);
 592        r->entry_count = t->entry_count;
 593        r->delta_depth = t->delta_depth;
 594        memcpy(r->entries,t->entries,t->entry_count*sizeof(t->entries[0]));
 595        release_tree_content(t);
 596        return r;
 597}
 598
 599static struct tree_entry *new_tree_entry(void)
 600{
 601        struct tree_entry *e;
 602
 603        if (!avail_tree_entry) {
 604                unsigned int n = tree_entry_alloc;
 605                total_allocd += n * sizeof(struct tree_entry);
 606                avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
 607                while (n-- > 1) {
 608                        *((void**)e) = e + 1;
 609                        e++;
 610                }
 611                *((void**)e) = NULL;
 612        }
 613
 614        e = avail_tree_entry;
 615        avail_tree_entry = *((void**)e);
 616        return e;
 617}
 618
 619static void release_tree_entry(struct tree_entry *e)
 620{
 621        if (e->tree)
 622                release_tree_content_recursive(e->tree);
 623        *((void**)e) = avail_tree_entry;
 624        avail_tree_entry = e;
 625}
 626
 627static void start_packfile(void)
 628{
 629        static char tmpfile[PATH_MAX];
 630        struct packed_git *p;
 631        struct pack_header hdr;
 632        int pack_fd;
 633
 634        snprintf(tmpfile, sizeof(tmpfile),
 635                "%s/pack_XXXXXX", get_object_directory());
 636        pack_fd = mkstemp(tmpfile);
 637        if (pack_fd < 0)
 638                die("Can't create %s: %s", tmpfile, strerror(errno));
 639        p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2);
 640        strcpy(p->pack_name, tmpfile);
 641        p->pack_fd = pack_fd;
 642
 643        hdr.hdr_signature = htonl(PACK_SIGNATURE);
 644        hdr.hdr_version = htonl(2);
 645        hdr.hdr_entries = 0;
 646        write_or_die(p->pack_fd, &hdr, sizeof(hdr));
 647
 648        pack_data = p;
 649        pack_size = sizeof(hdr);
 650        object_count = 0;
 651
 652        all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1));
 653        all_packs[pack_id] = p;
 654}
 655
 656static void fixup_header_footer(void)
 657{
 658        static const int buf_sz = 128 * 1024;
 659        int pack_fd = pack_data->pack_fd;
 660        SHA_CTX c;
 661        struct pack_header hdr;
 662        char *buf;
 663
 664        if (lseek(pack_fd, 0, SEEK_SET) != 0)
 665                die("Failed seeking to start: %s", strerror(errno));
 666        if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
 667                die("Unable to reread header of %s", pack_data->pack_name);
 668        if (lseek(pack_fd, 0, SEEK_SET) != 0)
 669                die("Failed seeking to start: %s", strerror(errno));
 670        hdr.hdr_entries = htonl(object_count);
 671        write_or_die(pack_fd, &hdr, sizeof(hdr));
 672
 673        SHA1_Init(&c);
 674        SHA1_Update(&c, &hdr, sizeof(hdr));
 675
 676        buf = xmalloc(buf_sz);
 677        for (;;) {
 678                size_t n = xread(pack_fd, buf, buf_sz);
 679                if (!n)
 680                        break;
 681                if (n < 0)
 682                        die("Failed to checksum %s", pack_data->pack_name);
 683                SHA1_Update(&c, buf, n);
 684        }
 685        free(buf);
 686
 687        SHA1_Final(pack_data->sha1, &c);
 688        write_or_die(pack_fd, pack_data->sha1, sizeof(pack_data->sha1));
 689        close(pack_fd);
 690}
 691
 692static int oecmp (const void *a_, const void *b_)
 693{
 694        struct object_entry *a = *((struct object_entry**)a_);
 695        struct object_entry *b = *((struct object_entry**)b_);
 696        return hashcmp(a->sha1, b->sha1);
 697}
 698
 699static char *create_index(void)
 700{
 701        static char tmpfile[PATH_MAX];
 702        SHA_CTX ctx;
 703        struct sha1file *f;
 704        struct object_entry **idx, **c, **last, *e;
 705        struct object_entry_pool *o;
 706        uint32_t array[256];
 707        int i, idx_fd;
 708
 709        /* Build the sorted table of object IDs. */
 710        idx = xmalloc(object_count * sizeof(struct object_entry*));
 711        c = idx;
 712        for (o = blocks; o; o = o->next_pool)
 713                for (e = o->next_free; e-- != o->entries;)
 714                        if (pack_id == e->pack_id)
 715                                *c++ = e;
 716        last = idx + object_count;
 717        if (c != last)
 718                die("internal consistency error creating the index");
 719        qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
 720
 721        /* Generate the fan-out array. */
 722        c = idx;
 723        for (i = 0; i < 256; i++) {
 724                struct object_entry **next = c;;
 725                while (next < last) {
 726                        if ((*next)->sha1[0] != i)
 727                                break;
 728                        next++;
 729                }
 730                array[i] = htonl(next - idx);
 731                c = next;
 732        }
 733
 734        snprintf(tmpfile, sizeof(tmpfile),
 735                "%s/index_XXXXXX", get_object_directory());
 736        idx_fd = mkstemp(tmpfile);
 737        if (idx_fd < 0)
 738                die("Can't create %s: %s", tmpfile, strerror(errno));
 739        f = sha1fd(idx_fd, tmpfile);
 740        sha1write(f, array, 256 * sizeof(int));
 741        SHA1_Init(&ctx);
 742        for (c = idx; c != last; c++) {
 743                uint32_t offset = htonl((*c)->offset);
 744                sha1write(f, &offset, 4);
 745                sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
 746                SHA1_Update(&ctx, (*c)->sha1, 20);
 747        }
 748        sha1write(f, pack_data->sha1, sizeof(pack_data->sha1));
 749        sha1close(f, NULL, 1);
 750        free(idx);
 751        SHA1_Final(pack_data->sha1, &ctx);
 752        return tmpfile;
 753}
 754
 755static char *keep_pack(char *curr_index_name)
 756{
 757        static char name[PATH_MAX];
 758        static char *keep_msg = "fast-import";
 759        int keep_fd;
 760
 761        chmod(pack_data->pack_name, 0444);
 762        chmod(curr_index_name, 0444);
 763
 764        snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
 765                 get_object_directory(), sha1_to_hex(pack_data->sha1));
 766        keep_fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
 767        if (keep_fd < 0)
 768                die("cannot create keep file");
 769        write(keep_fd, keep_msg, strlen(keep_msg));
 770        close(keep_fd);
 771
 772        snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
 773                 get_object_directory(), sha1_to_hex(pack_data->sha1));
 774        if (move_temp_to_file(pack_data->pack_name, name))
 775                die("cannot store pack file");
 776
 777        snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
 778                 get_object_directory(), sha1_to_hex(pack_data->sha1));
 779        if (move_temp_to_file(curr_index_name, name))
 780                die("cannot store index file");
 781        return name;
 782}
 783
 784static void unkeep_all_packs(void)
 785{
 786        static char name[PATH_MAX];
 787        int k;
 788
 789        for (k = 0; k < pack_id; k++) {
 790                struct packed_git *p = all_packs[k];
 791                snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
 792                         get_object_directory(), sha1_to_hex(p->sha1));
 793                unlink(name);
 794        }
 795}
 796
 797static void end_packfile(void)
 798{
 799        struct packed_git *old_p = pack_data, *new_p;
 800
 801        if (object_count) {
 802                char *idx_name;
 803                int i;
 804                struct branch *b;
 805                struct tag *t;
 806
 807                fixup_header_footer();
 808                idx_name = keep_pack(create_index());
 809
 810                /* Register the packfile with core git's machinary. */
 811                new_p = add_packed_git(idx_name, strlen(idx_name), 1);
 812                if (!new_p)
 813                        die("core git rejected index %s", idx_name);
 814                new_p->windows = old_p->windows;
 815                all_packs[pack_id] = new_p;
 816                install_packed_git(new_p);
 817
 818                /* Print the boundary */
 819                if (pack_edges) {
 820                        fprintf(pack_edges, "%s:", new_p->pack_name);
 821                        for (i = 0; i < branch_table_sz; i++) {
 822                                for (b = branch_table[i]; b; b = b->table_next_branch) {
 823                                        if (b->pack_id == pack_id)
 824                                                fprintf(pack_edges, " %s", sha1_to_hex(b->sha1));
 825                                }
 826                        }
 827                        for (t = first_tag; t; t = t->next_tag) {
 828                                if (t->pack_id == pack_id)
 829                                        fprintf(pack_edges, " %s", sha1_to_hex(t->sha1));
 830                        }
 831                        fputc('\n', pack_edges);
 832                        fflush(pack_edges);
 833                }
 834
 835                pack_id++;
 836        }
 837        else
 838                unlink(old_p->pack_name);
 839        free(old_p);
 840
 841        /* We can't carry a delta across packfiles. */
 842        free(last_blob.data);
 843        last_blob.data = NULL;
 844        last_blob.len = 0;
 845        last_blob.offset = 0;
 846        last_blob.depth = 0;
 847}
 848
 849static void cycle_packfile(void)
 850{
 851        end_packfile();
 852        start_packfile();
 853}
 854
 855static size_t encode_header(
 856        enum object_type type,
 857        size_t size,
 858        unsigned char *hdr)
 859{
 860        int n = 1;
 861        unsigned char c;
 862
 863        if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
 864                die("bad type %d", type);
 865
 866        c = (type << 4) | (size & 15);
 867        size >>= 4;
 868        while (size) {
 869                *hdr++ = c | 0x80;
 870                c = size & 0x7f;
 871                size >>= 7;
 872                n++;
 873        }
 874        *hdr = c;
 875        return n;
 876}
 877
 878static int store_object(
 879        enum object_type type,
 880        void *dat,
 881        size_t datlen,
 882        struct last_object *last,
 883        unsigned char *sha1out,
 884        uintmax_t mark)
 885{
 886        void *out, *delta;
 887        struct object_entry *e;
 888        unsigned char hdr[96];
 889        unsigned char sha1[20];
 890        unsigned long hdrlen, deltalen;
 891        SHA_CTX c;
 892        z_stream s;
 893
 894        hdrlen = sprintf((char*)hdr,"%s %lu", type_names[type],
 895                (unsigned long)datlen) + 1;
 896        SHA1_Init(&c);
 897        SHA1_Update(&c, hdr, hdrlen);
 898        SHA1_Update(&c, dat, datlen);
 899        SHA1_Final(sha1, &c);
 900        if (sha1out)
 901                hashcpy(sha1out, sha1);
 902
 903        e = insert_object(sha1);
 904        if (mark)
 905                insert_mark(mark, e);
 906        if (e->offset) {
 907                duplicate_count_by_type[type]++;
 908                return 1;
 909        }
 910
 911        if (last && last->data && last->depth < max_depth) {
 912                delta = diff_delta(last->data, last->len,
 913                        dat, datlen,
 914                        &deltalen, 0);
 915                if (delta && deltalen >= datlen) {
 916                        free(delta);
 917                        delta = NULL;
 918                }
 919        } else
 920                delta = NULL;
 921
 922        memset(&s, 0, sizeof(s));
 923        deflateInit(&s, zlib_compression_level);
 924        if (delta) {
 925                s.next_in = delta;
 926                s.avail_in = deltalen;
 927        } else {
 928                s.next_in = dat;
 929                s.avail_in = datlen;
 930        }
 931        s.avail_out = deflateBound(&s, s.avail_in);
 932        s.next_out = out = xmalloc(s.avail_out);
 933        while (deflate(&s, Z_FINISH) == Z_OK)
 934                /* nothing */;
 935        deflateEnd(&s);
 936
 937        /* Determine if we should auto-checkpoint. */
 938        if ((pack_size + 60 + s.total_out) > max_packsize
 939                || (pack_size + 60 + s.total_out) < pack_size) {
 940
 941                /* This new object needs to *not* have the current pack_id. */
 942                e->pack_id = pack_id + 1;
 943                cycle_packfile();
 944
 945                /* We cannot carry a delta into the new pack. */
 946                if (delta) {
 947                        free(delta);
 948                        delta = NULL;
 949
 950                        memset(&s, 0, sizeof(s));
 951                        deflateInit(&s, zlib_compression_level);
 952                        s.next_in = dat;
 953                        s.avail_in = datlen;
 954                        s.avail_out = deflateBound(&s, s.avail_in);
 955                        s.next_out = out = xrealloc(out, s.avail_out);
 956                        while (deflate(&s, Z_FINISH) == Z_OK)
 957                                /* nothing */;
 958                        deflateEnd(&s);
 959                }
 960        }
 961
 962        e->type = type;
 963        e->pack_id = pack_id;
 964        e->offset = pack_size;
 965        object_count++;
 966        object_count_by_type[type]++;
 967
 968        if (delta) {
 969                unsigned long ofs = e->offset - last->offset;
 970                unsigned pos = sizeof(hdr) - 1;
 971
 972                delta_count_by_type[type]++;
 973                last->depth++;
 974
 975                hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr);
 976                write_or_die(pack_data->pack_fd, hdr, hdrlen);
 977                pack_size += hdrlen;
 978
 979                hdr[pos] = ofs & 127;
 980                while (ofs >>= 7)
 981                        hdr[--pos] = 128 | (--ofs & 127);
 982                write_or_die(pack_data->pack_fd, hdr + pos, sizeof(hdr) - pos);
 983                pack_size += sizeof(hdr) - pos;
 984        } else {
 985                if (last)
 986                        last->depth = 0;
 987                hdrlen = encode_header(type, datlen, hdr);
 988                write_or_die(pack_data->pack_fd, hdr, hdrlen);
 989                pack_size += hdrlen;
 990        }
 991
 992        write_or_die(pack_data->pack_fd, out, s.total_out);
 993        pack_size += s.total_out;
 994
 995        free(out);
 996        free(delta);
 997        if (last) {
 998                if (!last->no_free)
 999                        free(last->data);
1000                last->data = dat;
1001                last->offset = e->offset;
1002                last->len = datlen;
1003        }
1004        return 0;
1005}
1006
1007static void *gfi_unpack_entry(
1008        struct object_entry *oe,
1009        unsigned long *sizep)
1010{
1011        static char type[20];
1012        struct packed_git *p = all_packs[oe->pack_id];
1013        if (p == pack_data)
1014                p->pack_size = pack_size + 20;
1015        return unpack_entry(p, oe->offset, type, sizep);
1016}
1017
1018static const char *get_mode(const char *str, uint16_t *modep)
1019{
1020        unsigned char c;
1021        uint16_t mode = 0;
1022
1023        while ((c = *str++) != ' ') {
1024                if (c < '0' || c > '7')
1025                        return NULL;
1026                mode = (mode << 3) + (c - '0');
1027        }
1028        *modep = mode;
1029        return str;
1030}
1031
1032static void load_tree(struct tree_entry *root)
1033{
1034        unsigned char* sha1 = root->versions[1].sha1;
1035        struct object_entry *myoe;
1036        struct tree_content *t;
1037        unsigned long size;
1038        char *buf;
1039        const char *c;
1040
1041        root->tree = t = new_tree_content(8);
1042        if (is_null_sha1(sha1))
1043                return;
1044
1045        myoe = find_object(sha1);
1046        if (myoe) {
1047                if (myoe->type != OBJ_TREE)
1048                        die("Not a tree: %s", sha1_to_hex(sha1));
1049                t->delta_depth = 0;
1050                buf = gfi_unpack_entry(myoe, &size);
1051        } else {
1052                char type[20];
1053                buf = read_sha1_file(sha1, type, &size);
1054                if (!buf || strcmp(type, tree_type))
1055                        die("Can't load tree %s", sha1_to_hex(sha1));
1056        }
1057
1058        c = buf;
1059        while (c != (buf + size)) {
1060                struct tree_entry *e = new_tree_entry();
1061
1062                if (t->entry_count == t->entry_capacity)
1063                        root->tree = t = grow_tree_content(t, 8);
1064                t->entries[t->entry_count++] = e;
1065
1066                e->tree = NULL;
1067                c = get_mode(c, &e->versions[1].mode);
1068                if (!c)
1069                        die("Corrupt mode in %s", sha1_to_hex(sha1));
1070                e->versions[0].mode = e->versions[1].mode;
1071                e->name = to_atom(c, (unsigned short)strlen(c));
1072                c += e->name->str_len + 1;
1073                hashcpy(e->versions[0].sha1, (unsigned char*)c);
1074                hashcpy(e->versions[1].sha1, (unsigned char*)c);
1075                c += 20;
1076        }
1077        free(buf);
1078}
1079
1080static int tecmp0 (const void *_a, const void *_b)
1081{
1082        struct tree_entry *a = *((struct tree_entry**)_a);
1083        struct tree_entry *b = *((struct tree_entry**)_b);
1084        return base_name_compare(
1085                a->name->str_dat, a->name->str_len, a->versions[0].mode,
1086                b->name->str_dat, b->name->str_len, b->versions[0].mode);
1087}
1088
1089static int tecmp1 (const void *_a, const void *_b)
1090{
1091        struct tree_entry *a = *((struct tree_entry**)_a);
1092        struct tree_entry *b = *((struct tree_entry**)_b);
1093        return base_name_compare(
1094                a->name->str_dat, a->name->str_len, a->versions[1].mode,
1095                b->name->str_dat, b->name->str_len, b->versions[1].mode);
1096}
1097
1098static void mktree(struct tree_content *t,
1099        int v,
1100        unsigned long *szp,
1101        struct dbuf *b)
1102{
1103        size_t maxlen = 0;
1104        unsigned int i;
1105        char *c;
1106
1107        if (!v)
1108                qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
1109        else
1110                qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
1111
1112        for (i = 0; i < t->entry_count; i++) {
1113                if (t->entries[i]->versions[v].mode)
1114                        maxlen += t->entries[i]->name->str_len + 34;
1115        }
1116
1117        size_dbuf(b, maxlen);
1118        c = b->buffer;
1119        for (i = 0; i < t->entry_count; i++) {
1120                struct tree_entry *e = t->entries[i];
1121                if (!e->versions[v].mode)
1122                        continue;
1123                c += sprintf(c, "%o", (unsigned int)e->versions[v].mode);
1124                *c++ = ' ';
1125                strcpy(c, e->name->str_dat);
1126                c += e->name->str_len + 1;
1127                hashcpy((unsigned char*)c, e->versions[v].sha1);
1128                c += 20;
1129        }
1130        *szp = c - (char*)b->buffer;
1131}
1132
1133static void store_tree(struct tree_entry *root)
1134{
1135        struct tree_content *t = root->tree;
1136        unsigned int i, j, del;
1137        unsigned long new_len;
1138        struct last_object lo;
1139        struct object_entry *le;
1140
1141        if (!is_null_sha1(root->versions[1].sha1))
1142                return;
1143
1144        for (i = 0; i < t->entry_count; i++) {
1145                if (t->entries[i]->tree)
1146                        store_tree(t->entries[i]);
1147        }
1148
1149        le = find_object(root->versions[0].sha1);
1150        if (!S_ISDIR(root->versions[0].mode)
1151                || !le
1152                || le->pack_id != pack_id) {
1153                lo.data = NULL;
1154                lo.depth = 0;
1155        } else {
1156                mktree(t, 0, &lo.len, &old_tree);
1157                lo.data = old_tree.buffer;
1158                lo.offset = le->offset;
1159                lo.depth = t->delta_depth;
1160                lo.no_free = 1;
1161        }
1162
1163        mktree(t, 1, &new_len, &new_tree);
1164        store_object(OBJ_TREE, new_tree.buffer, new_len,
1165                &lo, root->versions[1].sha1, 0);
1166
1167        t->delta_depth = lo.depth;
1168        for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
1169                struct tree_entry *e = t->entries[i];
1170                if (e->versions[1].mode) {
1171                        e->versions[0].mode = e->versions[1].mode;
1172                        hashcpy(e->versions[0].sha1, e->versions[1].sha1);
1173                        t->entries[j++] = e;
1174                } else {
1175                        release_tree_entry(e);
1176                        del++;
1177                }
1178        }
1179        t->entry_count -= del;
1180}
1181
1182static int tree_content_set(
1183        struct tree_entry *root,
1184        const char *p,
1185        const unsigned char *sha1,
1186        const uint16_t mode)
1187{
1188        struct tree_content *t = root->tree;
1189        const char *slash1;
1190        unsigned int i, n;
1191        struct tree_entry *e;
1192
1193        slash1 = strchr(p, '/');
1194        if (slash1)
1195                n = slash1 - p;
1196        else
1197                n = strlen(p);
1198
1199        for (i = 0; i < t->entry_count; i++) {
1200                e = t->entries[i];
1201                if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1202                        if (!slash1) {
1203                                if (e->versions[1].mode == mode
1204                                                && !hashcmp(e->versions[1].sha1, sha1))
1205                                        return 0;
1206                                e->versions[1].mode = mode;
1207                                hashcpy(e->versions[1].sha1, sha1);
1208                                if (e->tree) {
1209                                        release_tree_content_recursive(e->tree);
1210                                        e->tree = NULL;
1211                                }
1212                                hashclr(root->versions[1].sha1);
1213                                return 1;
1214                        }
1215                        if (!S_ISDIR(e->versions[1].mode)) {
1216                                e->tree = new_tree_content(8);
1217                                e->versions[1].mode = S_IFDIR;
1218                        }
1219                        if (!e->tree)
1220                                load_tree(e);
1221                        if (tree_content_set(e, slash1 + 1, sha1, mode)) {
1222                                hashclr(root->versions[1].sha1);
1223                                return 1;
1224                        }
1225                        return 0;
1226                }
1227        }
1228
1229        if (t->entry_count == t->entry_capacity)
1230                root->tree = t = grow_tree_content(t, 8);
1231        e = new_tree_entry();
1232        e->name = to_atom(p, (unsigned short)n);
1233        e->versions[0].mode = 0;
1234        hashclr(e->versions[0].sha1);
1235        t->entries[t->entry_count++] = e;
1236        if (slash1) {
1237                e->tree = new_tree_content(8);
1238                e->versions[1].mode = S_IFDIR;
1239                tree_content_set(e, slash1 + 1, sha1, mode);
1240        } else {
1241                e->tree = NULL;
1242                e->versions[1].mode = mode;
1243                hashcpy(e->versions[1].sha1, sha1);
1244        }
1245        hashclr(root->versions[1].sha1);
1246        return 1;
1247}
1248
1249static int tree_content_remove(struct tree_entry *root, const char *p)
1250{
1251        struct tree_content *t = root->tree;
1252        const char *slash1;
1253        unsigned int i, n;
1254        struct tree_entry *e;
1255
1256        slash1 = strchr(p, '/');
1257        if (slash1)
1258                n = slash1 - p;
1259        else
1260                n = strlen(p);
1261
1262        for (i = 0; i < t->entry_count; i++) {
1263                e = t->entries[i];
1264                if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1265                        if (!slash1 || !S_ISDIR(e->versions[1].mode))
1266                                goto del_entry;
1267                        if (!e->tree)
1268                                load_tree(e);
1269                        if (tree_content_remove(e, slash1 + 1)) {
1270                                for (n = 0; n < e->tree->entry_count; n++) {
1271                                        if (e->tree->entries[n]->versions[1].mode) {
1272                                                hashclr(root->versions[1].sha1);
1273                                                return 1;
1274                                        }
1275                                }
1276                                goto del_entry;
1277                        }
1278                        return 0;
1279                }
1280        }
1281        return 0;
1282
1283del_entry:
1284        if (e->tree) {
1285                release_tree_content_recursive(e->tree);
1286                e->tree = NULL;
1287        }
1288        e->versions[1].mode = 0;
1289        hashclr(e->versions[1].sha1);
1290        hashclr(root->versions[1].sha1);
1291        return 1;
1292}
1293
1294static int update_branch(struct branch *b)
1295{
1296        static const char *msg = "fast-import";
1297        struct ref_lock *lock;
1298        unsigned char old_sha1[20];
1299
1300        if (read_ref(b->name, old_sha1))
1301                hashclr(old_sha1);
1302        lock = lock_any_ref_for_update(b->name, old_sha1);
1303        if (!lock)
1304                return error("Unable to lock %s", b->name);
1305        if (!force_update && !is_null_sha1(old_sha1)) {
1306                struct commit *old_cmit, *new_cmit;
1307
1308                old_cmit = lookup_commit_reference_gently(old_sha1, 0);
1309                new_cmit = lookup_commit_reference_gently(b->sha1, 0);
1310                if (!old_cmit || !new_cmit) {
1311                        unlock_ref(lock);
1312                        return error("Branch %s is missing commits.", b->name);
1313                }
1314
1315                if (!in_merge_bases(old_cmit, &new_cmit, 1)) {
1316                        unlock_ref(lock);
1317                        warn("Not updating %s"
1318                                " (new tip %s does not contain %s)",
1319                                b->name, sha1_to_hex(b->sha1), sha1_to_hex(old_sha1));
1320                        return -1;
1321                }
1322        }
1323        if (write_ref_sha1(lock, b->sha1, msg) < 0)
1324                return error("Unable to update %s", b->name);
1325        return 0;
1326}
1327
1328static void dump_branches(void)
1329{
1330        unsigned int i;
1331        struct branch *b;
1332
1333        for (i = 0; i < branch_table_sz; i++) {
1334                for (b = branch_table[i]; b; b = b->table_next_branch)
1335                        failure |= update_branch(b);
1336        }
1337}
1338
1339static void dump_tags(void)
1340{
1341        static const char *msg = "fast-import";
1342        struct tag *t;
1343        struct ref_lock *lock;
1344        char ref_name[PATH_MAX];
1345
1346        for (t = first_tag; t; t = t->next_tag) {
1347                sprintf(ref_name, "tags/%s", t->name);
1348                lock = lock_ref_sha1(ref_name, NULL);
1349                if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
1350                        failure |= error("Unable to update %s", ref_name);
1351        }
1352}
1353
1354static void dump_marks_helper(FILE *f,
1355        uintmax_t base,
1356        struct mark_set *m)
1357{
1358        uintmax_t k;
1359        if (m->shift) {
1360                for (k = 0; k < 1024; k++) {
1361                        if (m->data.sets[k])
1362                                dump_marks_helper(f, (base + k) << m->shift,
1363                                        m->data.sets[k]);
1364                }
1365        } else {
1366                for (k = 0; k < 1024; k++) {
1367                        if (m->data.marked[k])
1368                                fprintf(f, ":%" PRIuMAX " %s\n", base + k,
1369                                        sha1_to_hex(m->data.marked[k]->sha1));
1370                }
1371        }
1372}
1373
1374static void dump_marks(void)
1375{
1376        if (mark_file)
1377        {
1378                FILE *f = fopen(mark_file, "w");
1379                if (f) {
1380                        dump_marks_helper(f, 0, marks);
1381                        fclose(f);
1382                } else
1383                        failure |= error("Unable to write marks file %s: %s",
1384                                mark_file, strerror(errno));
1385        }
1386}
1387
1388static void read_next_command(void)
1389{
1390        read_line(&command_buf, stdin, '\n');
1391}
1392
1393static void cmd_mark(void)
1394{
1395        if (!prefixcmp(command_buf.buf, "mark :")) {
1396                next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
1397                read_next_command();
1398        }
1399        else
1400                next_mark = 0;
1401}
1402
1403static void *cmd_data (size_t *size)
1404{
1405        size_t length;
1406        char *buffer;
1407
1408        if (prefixcmp(command_buf.buf, "data "))
1409                die("Expected 'data n' command, found: %s", command_buf.buf);
1410
1411        if (!prefixcmp(command_buf.buf + 5, "<<")) {
1412                char *term = xstrdup(command_buf.buf + 5 + 2);
1413                size_t sz = 8192, term_len = command_buf.len - 5 - 2;
1414                length = 0;
1415                buffer = xmalloc(sz);
1416                for (;;) {
1417                        read_next_command();
1418                        if (command_buf.eof)
1419                                die("EOF in data (terminator '%s' not found)", term);
1420                        if (term_len == command_buf.len
1421                                && !strcmp(term, command_buf.buf))
1422                                break;
1423                        if (sz < (length + command_buf.len)) {
1424                                sz = sz * 3 / 2 + 16;
1425                                if (sz < (length + command_buf.len))
1426                                        sz = length + command_buf.len;
1427                                buffer = xrealloc(buffer, sz);
1428                        }
1429                        memcpy(buffer + length,
1430                                command_buf.buf,
1431                                command_buf.len - 1);
1432                        length += command_buf.len - 1;
1433                        buffer[length++] = '\n';
1434                }
1435                free(term);
1436        }
1437        else {
1438                size_t n = 0;
1439                length = strtoul(command_buf.buf + 5, NULL, 10);
1440                buffer = xmalloc(length);
1441                while (n < length) {
1442                        size_t s = fread(buffer + n, 1, length - n, stdin);
1443                        if (!s && feof(stdin))
1444                                die("EOF in data (%lu bytes remaining)",
1445                                        (unsigned long)(length - n));
1446                        n += s;
1447                }
1448        }
1449
1450        if (fgetc(stdin) != '\n')
1451                die("An lf did not trail the binary data as expected.");
1452
1453        *size = length;
1454        return buffer;
1455}
1456
1457static int validate_raw_date(const char *src, char *result, int maxlen)
1458{
1459        const char *orig_src = src;
1460        char *endp, sign;
1461
1462        strtoul(src, &endp, 10);
1463        if (endp == src || *endp != ' ')
1464                return -1;
1465
1466        src = endp + 1;
1467        if (*src != '-' && *src != '+')
1468                return -1;
1469        sign = *src;
1470
1471        strtoul(src + 1, &endp, 10);
1472        if (endp == src || *endp || (endp - orig_src) >= maxlen)
1473                return -1;
1474
1475        strcpy(result, orig_src);
1476        return 0;
1477}
1478
1479static char *parse_ident(const char *buf)
1480{
1481        const char *gt;
1482        size_t name_len;
1483        char *ident;
1484
1485        gt = strrchr(buf, '>');
1486        if (!gt)
1487                die("Missing > in ident string: %s", buf);
1488        gt++;
1489        if (*gt != ' ')
1490                die("Missing space after > in ident string: %s", buf);
1491        gt++;
1492        name_len = gt - buf;
1493        ident = xmalloc(name_len + 24);
1494        strncpy(ident, buf, name_len);
1495
1496        switch (whenspec) {
1497        case WHENSPEC_RAW:
1498                if (validate_raw_date(gt, ident + name_len, 24) < 0)
1499                        die("Invalid raw date \"%s\" in ident: %s", gt, buf);
1500                break;
1501        case WHENSPEC_RFC2822:
1502                if (parse_date(gt, ident + name_len, 24) < 0)
1503                        die("Invalid rfc2822 date \"%s\" in ident: %s", gt, buf);
1504                break;
1505        case WHENSPEC_NOW:
1506                if (strcmp("now", gt))
1507                        die("Date in ident must be 'now': %s", buf);
1508                datestamp(ident + name_len, 24);
1509                break;
1510        }
1511
1512        return ident;
1513}
1514
1515static void cmd_new_blob(void)
1516{
1517        size_t l;
1518        void *d;
1519
1520        read_next_command();
1521        cmd_mark();
1522        d = cmd_data(&l);
1523
1524        if (store_object(OBJ_BLOB, d, l, &last_blob, NULL, next_mark))
1525                free(d);
1526}
1527
1528static void unload_one_branch(void)
1529{
1530        while (cur_active_branches
1531                && cur_active_branches >= max_active_branches) {
1532                unsigned long min_commit = ULONG_MAX;
1533                struct branch *e, *l = NULL, *p = NULL;
1534
1535                for (e = active_branches; e; e = e->active_next_branch) {
1536                        if (e->last_commit < min_commit) {
1537                                p = l;
1538                                min_commit = e->last_commit;
1539                        }
1540                        l = e;
1541                }
1542
1543                if (p) {
1544                        e = p->active_next_branch;
1545                        p->active_next_branch = e->active_next_branch;
1546                } else {
1547                        e = active_branches;
1548                        active_branches = e->active_next_branch;
1549                }
1550                e->active_next_branch = NULL;
1551                if (e->branch_tree.tree) {
1552                        release_tree_content_recursive(e->branch_tree.tree);
1553                        e->branch_tree.tree = NULL;
1554                }
1555                cur_active_branches--;
1556        }
1557}
1558
1559static void load_branch(struct branch *b)
1560{
1561        load_tree(&b->branch_tree);
1562        b->active_next_branch = active_branches;
1563        active_branches = b;
1564        cur_active_branches++;
1565        branch_load_count++;
1566}
1567
1568static void file_change_m(struct branch *b)
1569{
1570        const char *p = command_buf.buf + 2;
1571        char *p_uq;
1572        const char *endp;
1573        struct object_entry *oe = oe;
1574        unsigned char sha1[20];
1575        uint16_t mode, inline_data = 0;
1576        char type[20];
1577
1578        p = get_mode(p, &mode);
1579        if (!p)
1580                die("Corrupt mode: %s", command_buf.buf);
1581        switch (mode) {
1582        case S_IFREG | 0644:
1583        case S_IFREG | 0755:
1584        case S_IFLNK:
1585        case 0644:
1586        case 0755:
1587                /* ok */
1588                break;
1589        default:
1590                die("Corrupt mode: %s", command_buf.buf);
1591        }
1592
1593        if (*p == ':') {
1594                char *x;
1595                oe = find_mark(strtoumax(p + 1, &x, 10));
1596                hashcpy(sha1, oe->sha1);
1597                p = x;
1598        } else if (!prefixcmp(p, "inline")) {
1599                inline_data = 1;
1600                p += 6;
1601        } else {
1602                if (get_sha1_hex(p, sha1))
1603                        die("Invalid SHA1: %s", command_buf.buf);
1604                oe = find_object(sha1);
1605                p += 40;
1606        }
1607        if (*p++ != ' ')
1608                die("Missing space after SHA1: %s", command_buf.buf);
1609
1610        p_uq = unquote_c_style(p, &endp);
1611        if (p_uq) {
1612                if (*endp)
1613                        die("Garbage after path in: %s", command_buf.buf);
1614                p = p_uq;
1615        }
1616
1617        if (inline_data) {
1618                size_t l;
1619                void *d;
1620                if (!p_uq)
1621                        p = p_uq = xstrdup(p);
1622                read_next_command();
1623                d = cmd_data(&l);
1624                if (store_object(OBJ_BLOB, d, l, &last_blob, sha1, 0))
1625                        free(d);
1626        } else if (oe) {
1627                if (oe->type != OBJ_BLOB)
1628                        die("Not a blob (actually a %s): %s",
1629                                command_buf.buf, type_names[oe->type]);
1630        } else {
1631                if (sha1_object_info(sha1, type, NULL))
1632                        die("Blob not found: %s", command_buf.buf);
1633                if (strcmp(blob_type, type))
1634                        die("Not a blob (actually a %s): %s",
1635                                command_buf.buf, type);
1636        }
1637
1638        tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
1639        free(p_uq);
1640}
1641
1642static void file_change_d(struct branch *b)
1643{
1644        const char *p = command_buf.buf + 2;
1645        char *p_uq;
1646        const char *endp;
1647
1648        p_uq = unquote_c_style(p, &endp);
1649        if (p_uq) {
1650                if (*endp)
1651                        die("Garbage after path in: %s", command_buf.buf);
1652                p = p_uq;
1653        }
1654        tree_content_remove(&b->branch_tree, p);
1655        free(p_uq);
1656}
1657
1658static void file_change_deleteall(struct branch *b)
1659{
1660        release_tree_content_recursive(b->branch_tree.tree);
1661        hashclr(b->branch_tree.versions[0].sha1);
1662        hashclr(b->branch_tree.versions[1].sha1);
1663        load_tree(&b->branch_tree);
1664}
1665
1666static void cmd_from(struct branch *b)
1667{
1668        const char *from;
1669        struct branch *s;
1670
1671        if (prefixcmp(command_buf.buf, "from "))
1672                return;
1673
1674        if (b->branch_tree.tree) {
1675                release_tree_content_recursive(b->branch_tree.tree);
1676                b->branch_tree.tree = NULL;
1677        }
1678
1679        from = strchr(command_buf.buf, ' ') + 1;
1680        s = lookup_branch(from);
1681        if (b == s)
1682                die("Can't create a branch from itself: %s", b->name);
1683        else if (s) {
1684                unsigned char *t = s->branch_tree.versions[1].sha1;
1685                hashcpy(b->sha1, s->sha1);
1686                hashcpy(b->branch_tree.versions[0].sha1, t);
1687                hashcpy(b->branch_tree.versions[1].sha1, t);
1688        } else if (*from == ':') {
1689                uintmax_t idnum = strtoumax(from + 1, NULL, 10);
1690                struct object_entry *oe = find_mark(idnum);
1691                unsigned long size;
1692                char *buf;
1693                if (oe->type != OBJ_COMMIT)
1694                        die("Mark :%" PRIuMAX " not a commit", idnum);
1695                hashcpy(b->sha1, oe->sha1);
1696                buf = gfi_unpack_entry(oe, &size);
1697                if (!buf || size < 46)
1698                        die("Not a valid commit: %s", from);
1699                if (memcmp("tree ", buf, 5)
1700                        || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1701                        die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1702                free(buf);
1703                hashcpy(b->branch_tree.versions[0].sha1,
1704                        b->branch_tree.versions[1].sha1);
1705        } else if (!get_sha1(from, b->sha1)) {
1706                if (is_null_sha1(b->sha1)) {
1707                        hashclr(b->branch_tree.versions[0].sha1);
1708                        hashclr(b->branch_tree.versions[1].sha1);
1709                } else {
1710                        unsigned long size;
1711                        char *buf;
1712
1713                        buf = read_object_with_reference(b->sha1,
1714                                type_names[OBJ_COMMIT], &size, b->sha1);
1715                        if (!buf || size < 46)
1716                                die("Not a valid commit: %s", from);
1717                        if (memcmp("tree ", buf, 5)
1718                                || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
1719                                die("The commit %s is corrupt", sha1_to_hex(b->sha1));
1720                        free(buf);
1721                        hashcpy(b->branch_tree.versions[0].sha1,
1722                                b->branch_tree.versions[1].sha1);
1723                }
1724        } else
1725                die("Invalid ref name or SHA1 expression: %s", from);
1726
1727        read_next_command();
1728}
1729
1730static struct hash_list *cmd_merge(unsigned int *count)
1731{
1732        struct hash_list *list = NULL, *n, *e = e;
1733        const char *from;
1734        struct branch *s;
1735
1736        *count = 0;
1737        while (!prefixcmp(command_buf.buf, "merge ")) {
1738                from = strchr(command_buf.buf, ' ') + 1;
1739                n = xmalloc(sizeof(*n));
1740                s = lookup_branch(from);
1741                if (s)
1742                        hashcpy(n->sha1, s->sha1);
1743                else if (*from == ':') {
1744                        uintmax_t idnum = strtoumax(from + 1, NULL, 10);
1745                        struct object_entry *oe = find_mark(idnum);
1746                        if (oe->type != OBJ_COMMIT)
1747                                die("Mark :%" PRIuMAX " not a commit", idnum);
1748                        hashcpy(n->sha1, oe->sha1);
1749                } else if (get_sha1(from, n->sha1))
1750                        die("Invalid ref name or SHA1 expression: %s", from);
1751
1752                n->next = NULL;
1753                if (list)
1754                        e->next = n;
1755                else
1756                        list = n;
1757                e = n;
1758                (*count)++;
1759                read_next_command();
1760        }
1761        return list;
1762}
1763
1764static void cmd_new_commit(void)
1765{
1766        struct branch *b;
1767        void *msg;
1768        size_t msglen;
1769        char *sp;
1770        char *author = NULL;
1771        char *committer = NULL;
1772        struct hash_list *merge_list = NULL;
1773        unsigned int merge_count;
1774
1775        /* Obtain the branch name from the rest of our command */
1776        sp = strchr(command_buf.buf, ' ') + 1;
1777        b = lookup_branch(sp);
1778        if (!b)
1779                b = new_branch(sp);
1780
1781        read_next_command();
1782        cmd_mark();
1783        if (!prefixcmp(command_buf.buf, "author ")) {
1784                author = parse_ident(command_buf.buf + 7);
1785                read_next_command();
1786        }
1787        if (!prefixcmp(command_buf.buf, "committer ")) {
1788                committer = parse_ident(command_buf.buf + 10);
1789                read_next_command();
1790        }
1791        if (!committer)
1792                die("Expected committer but didn't get one");
1793        msg = cmd_data(&msglen);
1794        read_next_command();
1795        cmd_from(b);
1796        merge_list = cmd_merge(&merge_count);
1797
1798        /* ensure the branch is active/loaded */
1799        if (!b->branch_tree.tree || !max_active_branches) {
1800                unload_one_branch();
1801                load_branch(b);
1802        }
1803
1804        /* file_change* */
1805        for (;;) {
1806                if (1 == command_buf.len)
1807                        break;
1808                else if (!prefixcmp(command_buf.buf, "M "))
1809                        file_change_m(b);
1810                else if (!prefixcmp(command_buf.buf, "D "))
1811                        file_change_d(b);
1812                else if (!strcmp("deleteall", command_buf.buf))
1813                        file_change_deleteall(b);
1814                else
1815                        die("Unsupported file_change: %s", command_buf.buf);
1816                read_next_command();
1817        }
1818
1819        /* build the tree and the commit */
1820        store_tree(&b->branch_tree);
1821        hashcpy(b->branch_tree.versions[0].sha1,
1822                b->branch_tree.versions[1].sha1);
1823        size_dbuf(&new_data, 114 + msglen
1824                + merge_count * 49
1825                + (author
1826                        ? strlen(author) + strlen(committer)
1827                        : 2 * strlen(committer)));
1828        sp = new_data.buffer;
1829        sp += sprintf(sp, "tree %s\n",
1830                sha1_to_hex(b->branch_tree.versions[1].sha1));
1831        if (!is_null_sha1(b->sha1))
1832                sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
1833        while (merge_list) {
1834                struct hash_list *next = merge_list->next;
1835                sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1));
1836                free(merge_list);
1837                merge_list = next;
1838        }
1839        sp += sprintf(sp, "author %s\n", author ? author : committer);
1840        sp += sprintf(sp, "committer %s\n", committer);
1841        *sp++ = '\n';
1842        memcpy(sp, msg, msglen);
1843        sp += msglen;
1844        free(author);
1845        free(committer);
1846        free(msg);
1847
1848        if (!store_object(OBJ_COMMIT,
1849                new_data.buffer, sp - (char*)new_data.buffer,
1850                NULL, b->sha1, next_mark))
1851                b->pack_id = pack_id;
1852        b->last_commit = object_count_by_type[OBJ_COMMIT];
1853}
1854
1855static void cmd_new_tag(void)
1856{
1857        char *sp;
1858        const char *from;
1859        char *tagger;
1860        struct branch *s;
1861        void *msg;
1862        size_t msglen;
1863        struct tag *t;
1864        uintmax_t from_mark = 0;
1865        unsigned char sha1[20];
1866
1867        /* Obtain the new tag name from the rest of our command */
1868        sp = strchr(command_buf.buf, ' ') + 1;
1869        t = pool_alloc(sizeof(struct tag));
1870        t->next_tag = NULL;
1871        t->name = pool_strdup(sp);
1872        if (last_tag)
1873                last_tag->next_tag = t;
1874        else
1875                first_tag = t;
1876        last_tag = t;
1877        read_next_command();
1878
1879        /* from ... */
1880        if (prefixcmp(command_buf.buf, "from "))
1881                die("Expected from command, got %s", command_buf.buf);
1882        from = strchr(command_buf.buf, ' ') + 1;
1883        s = lookup_branch(from);
1884        if (s) {
1885                hashcpy(sha1, s->sha1);
1886        } else if (*from == ':') {
1887                struct object_entry *oe;
1888                from_mark = strtoumax(from + 1, NULL, 10);
1889                oe = find_mark(from_mark);
1890                if (oe->type != OBJ_COMMIT)
1891                        die("Mark :%" PRIuMAX " not a commit", from_mark);
1892                hashcpy(sha1, oe->sha1);
1893        } else if (!get_sha1(from, sha1)) {
1894                unsigned long size;
1895                char *buf;
1896
1897                buf = read_object_with_reference(sha1,
1898                        type_names[OBJ_COMMIT], &size, sha1);
1899                if (!buf || size < 46)
1900                        die("Not a valid commit: %s", from);
1901                free(buf);
1902        } else
1903                die("Invalid ref name or SHA1 expression: %s", from);
1904        read_next_command();
1905
1906        /* tagger ... */
1907        if (prefixcmp(command_buf.buf, "tagger "))
1908                die("Expected tagger command, got %s", command_buf.buf);
1909        tagger = parse_ident(command_buf.buf + 7);
1910
1911        /* tag payload/message */
1912        read_next_command();
1913        msg = cmd_data(&msglen);
1914
1915        /* build the tag object */
1916        size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
1917        sp = new_data.buffer;
1918        sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
1919        sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
1920        sp += sprintf(sp, "tag %s\n", t->name);
1921        sp += sprintf(sp, "tagger %s\n", tagger);
1922        *sp++ = '\n';
1923        memcpy(sp, msg, msglen);
1924        sp += msglen;
1925        free(tagger);
1926        free(msg);
1927
1928        if (store_object(OBJ_TAG, new_data.buffer,
1929                sp - (char*)new_data.buffer,
1930                NULL, t->sha1, 0))
1931                t->pack_id = MAX_PACK_ID;
1932        else
1933                t->pack_id = pack_id;
1934}
1935
1936static void cmd_reset_branch(void)
1937{
1938        struct branch *b;
1939        char *sp;
1940
1941        /* Obtain the branch name from the rest of our command */
1942        sp = strchr(command_buf.buf, ' ') + 1;
1943        b = lookup_branch(sp);
1944        if (b) {
1945                hashclr(b->sha1);
1946                hashclr(b->branch_tree.versions[0].sha1);
1947                hashclr(b->branch_tree.versions[1].sha1);
1948                if (b->branch_tree.tree) {
1949                        release_tree_content_recursive(b->branch_tree.tree);
1950                        b->branch_tree.tree = NULL;
1951                }
1952        }
1953        else
1954                b = new_branch(sp);
1955        read_next_command();
1956        cmd_from(b);
1957}
1958
1959static void cmd_checkpoint(void)
1960{
1961        if (object_count) {
1962                cycle_packfile();
1963                dump_branches();
1964                dump_tags();
1965                dump_marks();
1966        }
1967        read_next_command();
1968}
1969
1970static const char fast_import_usage[] =
1971"git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
1972
1973int main(int argc, const char **argv)
1974{
1975        int i, show_stats = 1;
1976
1977        git_config(git_default_config);
1978
1979        for (i = 1; i < argc; i++) {
1980                const char *a = argv[i];
1981
1982                if (*a != '-' || !strcmp(a, "--"))
1983                        break;
1984                else if (!prefixcmp(a, "--date-format=")) {
1985                        const char *fmt = a + 14;
1986                        if (!strcmp(fmt, "raw"))
1987                                whenspec = WHENSPEC_RAW;
1988                        else if (!strcmp(fmt, "rfc2822"))
1989                                whenspec = WHENSPEC_RFC2822;
1990                        else if (!strcmp(fmt, "now"))
1991                                whenspec = WHENSPEC_NOW;
1992                        else
1993                                die("unknown --date-format argument %s", fmt);
1994                }
1995                else if (!prefixcmp(a, "--max-pack-size="))
1996                        max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
1997                else if (!prefixcmp(a, "--depth="))
1998                        max_depth = strtoul(a + 8, NULL, 0);
1999                else if (!prefixcmp(a, "--active-branches="))
2000                        max_active_branches = strtoul(a + 18, NULL, 0);
2001                else if (!prefixcmp(a, "--export-marks="))
2002                        mark_file = a + 15;
2003                else if (!prefixcmp(a, "--export-pack-edges=")) {
2004                        if (pack_edges)
2005                                fclose(pack_edges);
2006                        pack_edges = fopen(a + 20, "a");
2007                        if (!pack_edges)
2008                                die("Cannot open %s: %s", a + 20, strerror(errno));
2009                } else if (!strcmp(a, "--force"))
2010                        force_update = 1;
2011                else if (!strcmp(a, "--quiet"))
2012                        show_stats = 0;
2013                else if (!strcmp(a, "--stats"))
2014                        show_stats = 1;
2015                else
2016                        die("unknown option %s", a);
2017        }
2018        if (i != argc)
2019                usage(fast_import_usage);
2020
2021        alloc_objects(object_entry_alloc);
2022        strbuf_init(&command_buf);
2023
2024        atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
2025        branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
2026        avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
2027        marks = pool_calloc(1, sizeof(struct mark_set));
2028
2029        start_packfile();
2030        for (;;) {
2031                read_next_command();
2032                if (command_buf.eof)
2033                        break;
2034                else if (!strcmp("blob", command_buf.buf))
2035                        cmd_new_blob();
2036                else if (!prefixcmp(command_buf.buf, "commit "))
2037                        cmd_new_commit();
2038                else if (!prefixcmp(command_buf.buf, "tag "))
2039                        cmd_new_tag();
2040                else if (!prefixcmp(command_buf.buf, "reset "))
2041                        cmd_reset_branch();
2042                else if (!strcmp("checkpoint", command_buf.buf))
2043                        cmd_checkpoint();
2044                else
2045                        die("Unsupported command: %s", command_buf.buf);
2046        }
2047        end_packfile();
2048
2049        dump_branches();
2050        dump_tags();
2051        unkeep_all_packs();
2052        dump_marks();
2053
2054        if (pack_edges)
2055                fclose(pack_edges);
2056
2057        if (show_stats) {
2058                uintmax_t total_count = 0, duplicate_count = 0;
2059                for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
2060                        total_count += object_count_by_type[i];
2061                for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
2062                        duplicate_count += duplicate_count_by_type[i];
2063
2064                fprintf(stderr, "%s statistics:\n", argv[0]);
2065                fprintf(stderr, "---------------------------------------------------------------------\n");
2066                fprintf(stderr, "Alloc'd objects: %10" PRIuMAX "\n", alloc_count);
2067                fprintf(stderr, "Total objects:   %10" PRIuMAX " (%10" PRIuMAX " duplicates                  )\n", total_count, duplicate_count);
2068                fprintf(stderr, "      blobs  :   %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB]);
2069                fprintf(stderr, "      trees  :   %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE]);
2070                fprintf(stderr, "      commits:   %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT]);
2071                fprintf(stderr, "      tags   :   %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG]);
2072                fprintf(stderr, "Total branches:  %10lu (%10lu loads     )\n", branch_count, branch_load_count);
2073                fprintf(stderr, "      marks:     %10" PRIuMAX " (%10" PRIuMAX " unique    )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
2074                fprintf(stderr, "      atoms:     %10u\n", atom_cnt);
2075                fprintf(stderr, "Memory total:    %10" PRIuMAX " KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
2076                fprintf(stderr, "       pools:    %10lu KiB\n", (unsigned long)(total_allocd/1024));
2077                fprintf(stderr, "     objects:    %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
2078                fprintf(stderr, "---------------------------------------------------------------------\n");
2079                pack_report();
2080                fprintf(stderr, "---------------------------------------------------------------------\n");
2081                fprintf(stderr, "\n");
2082        }
2083
2084        return failure ? 1 : 0;
2085}