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