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