apply: get rid of --index-info in favor of --build-fake-ancestor
[gitweb.git] / fast-import.c
index 0f485c7828ce80e4f42f80d16fcb5b59fd3421d8..078079d404d8245d73ea8ef36d764f2bb311d0a9 100644 (file)
@@ -8,59 +8,71 @@ Format of STDIN stream:
         | new_tag
         | reset_branch
         | checkpoint
+        | progress
         ;
 
   new_blob ::= 'blob' lf
-       mark?
+    mark?
     file_content;
   file_content ::= data;
 
   new_commit ::= 'commit' sp ref_str lf
     mark?
-    ('author' sp name '<' email '>' ts tz lf)?
-    'committer' sp name '<' email '>' ts tz lf
+    ('author' sp name '<' email '>' when lf)?
+    'committer' sp name '<' email '>' when lf
     commit_msg
     ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
     ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)*
     file_change*
-    lf;
+    lf?;
   commit_msg ::= data;
 
-  file_change ::= file_del | file_obm | file_inm;
+  file_change ::= file_clr
+    | file_del
+    | file_rnm
+    | file_cpy
+    | file_obm
+    | file_inm;
+  file_clr ::= 'deleteall' lf;
   file_del ::= 'D' sp path_str lf;
+  file_rnm ::= 'R' sp path_str sp path_str lf;
+  file_cpy ::= 'C' sp path_str sp path_str lf;
   file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
   file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
     data;
 
   new_tag ::= 'tag' sp tag_str lf
     'from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf
-       'tagger' sp name '<' email '>' ts tz lf
+    'tagger' sp name '<' email '>' when lf
     tag_msg;
   tag_msg ::= data;
 
   reset_branch ::= 'reset' sp ref_str lf
     ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
-    lf;
+    lf?;
 
   checkpoint ::= 'checkpoint' lf
-    lf;
+    lf?;
+
+  progress ::= 'progress' sp not_lf* lf
+    lf?;
 
      # note: the first idnum in a stream should be 1 and subsequent
      # idnums should not have gaps between values as this will cause
      # the stream parser to reserve space for the gapped values.  An
-        # idnum can be updated in the future to a new object by issuing
+     # idnum can be updated in the future to a new object by issuing
      # a new mark directive with the old idnum.
-        #
+     #
   mark ::= 'mark' sp idnum lf;
   data ::= (delimited_data | exact_data)
-    lf;
+    lf?;
 
     # note: delim may be any string but must not contain lf.
     # data_line may contain any data but must not be exactly
     # delim.
   delimited_data ::= 'data' sp '<<' delim lf
     (data_line lf)*
-       delim lf;
+    delim lf;
 
      # note: declen indicates the length of binary_data in bytes.
      # declen does not include the lf preceeding the binary data.
@@ -70,10 +82,10 @@ Format of STDIN stream:
 
      # note: quoted strings are C-style quoting supporting \c for
      # common escapes of 'c' (e..g \n, \t, \\, \") or \nnn where nnn
-        # is the signed byte value in octal.  Note that the only
+     # is the signed byte value in octal.  Note that the only
      # characters which must actually be escaped to protect the
      # stream formatting is: \, " and LF.  Otherwise these values
-        # are UTF8.
+     # are UTF8.
      #
   ref_str     ::= ref;
   sha1exp_str ::= sha1exp;
@@ -81,20 +93,24 @@ Format of STDIN stream:
   path_str    ::= path    | '"' quoted(path)    '"' ;
   mode        ::= '100644' | '644'
                 | '100755' | '755'
-                | '140000'
+                | '120000'
                 ;
 
   declen ::= # unsigned 32 bit value, ascii base10 notation;
   bigint ::= # unsigned integer value, ascii base10 notation;
   binary_data ::= # file content, not interpreted;
 
+  when         ::= raw_when | rfc2822_when;
+  raw_when     ::= ts sp tz;
+  rfc2822_when ::= # Valid RFC 2822 date and time;
+
   sp ::= # ASCII space character;
   lf ::= # ASCII newline (LF) character;
 
      # note: a colon (':') must precede the numerical value assigned to
-        # an idnum.  This is to distinguish it from a ref or tag name as
+     # an idnum.  This is to distinguish it from a ref or tag name as
      # GIT does not permit ':' in ref or tag strings.
-        #
+     #
   idnum   ::= ':' bigint;
   path    ::= # GIT style file path, e.g. "a/b/c";
   ref     ::= # GIT ref name, e.g. "refs/heads/MOZ_GECKO_EXPERIMENT";
@@ -103,13 +119,24 @@ Format of STDIN stream:
   hexsha1 ::= # SHA1 in hexadecimal format;
 
      # note: name and email are UTF8 strings, however name must not
-        # contain '<' or lf and email must not contain any of the
+     # contain '<' or lf and email must not contain any of the
      # following: '<', '>', lf.
-        #
+     #
   name  ::= # valid GIT author/committer name;
   email ::= # valid GIT author/committer email;
   ts    ::= # time since the epoch in seconds, ascii base10 notation;
   tz    ::= # GIT style timezone;
+
+     # note: comments may appear anywhere in the input, except
+     # within a data command.  Any form of the data command
+     # always escapes the related input from comment processing.
+     #
+     # In case it is not clear, the '#' that starts the comment
+     # must be the first character on that the line (an lf have
+     # preceeded it).
+     #
+  comment ::= '#' not_lf* lf;
+  not_lf  ::= # Any byte that is not ASCII newline (LF);
 */
 
 #include "builtin.h"
@@ -117,6 +144,7 @@ Format of STDIN stream:
 #include "object.h"
 #include "blob.h"
 #include "tree.h"
+#include "commit.h"
 #include "delta.h"
 #include "pack.h"
 #include "refs.h"
@@ -210,7 +238,8 @@ struct branch
        const char *name;
        struct tree_entry branch_tree;
        uintmax_t last_commit;
-       unsigned int pack_id;
+       unsigned active : 1;
+       unsigned pack_id : PACK_ID_BITS;
        unsigned char sha1[20];
 };
 
@@ -234,9 +263,23 @@ struct hash_list
        unsigned char sha1[20];
 };
 
+typedef enum {
+       WHENSPEC_RAW = 1,
+       WHENSPEC_RFC2822,
+       WHENSPEC_NOW,
+} whenspec_type;
+
+struct recent_command
+{
+       struct recent_command *prev;
+       struct recent_command *next;
+       char *buf;
+};
+
 /* Configured limits on output */
 static unsigned long max_depth = 10;
-static unsigned long max_packsize = (1LL << 32) - 1;
+static off_t max_packsize = (1LL << 32) - 1;
+static int force_update;
 
 /* Stats and misc. counters */
 static uintmax_t alloc_count;
@@ -247,6 +290,8 @@ static uintmax_t delta_count_by_type[1 << TYPE_BITS];
 static unsigned long object_count;
 static unsigned long branch_count;
 static unsigned long branch_load_count;
+static int failure;
+static FILE *pack_edges;
 
 /* Memory pools */
 static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
@@ -294,10 +339,122 @@ static struct tag *first_tag;
 static struct tag *last_tag;
 
 /* Input stream parsing */
+static whenspec_type whenspec = WHENSPEC_RAW;
 static struct strbuf command_buf;
+static int unread_command_buf;
+static struct recent_command cmd_hist = {&cmd_hist, &cmd_hist, NULL};
+static struct recent_command *cmd_tail = &cmd_hist;
+static struct recent_command *rc_free;
+static unsigned int cmd_save = 100;
 static uintmax_t next_mark;
 static struct dbuf new_data;
 
+static void write_branch_report(FILE *rpt, struct branch *b)
+{
+       fprintf(rpt, "%s:\n", b->name);
+
+       fprintf(rpt, "  status      :");
+       if (b->active)
+               fputs(" active", rpt);
+       if (b->branch_tree.tree)
+               fputs(" loaded", rpt);
+       if (is_null_sha1(b->branch_tree.versions[1].sha1))
+               fputs(" dirty", rpt);
+       fputc('\n', rpt);
+
+       fprintf(rpt, "  tip commit  : %s\n", sha1_to_hex(b->sha1));
+       fprintf(rpt, "  old tree    : %s\n", sha1_to_hex(b->branch_tree.versions[0].sha1));
+       fprintf(rpt, "  cur tree    : %s\n", sha1_to_hex(b->branch_tree.versions[1].sha1));
+       fprintf(rpt, "  commit clock: %" PRIuMAX "\n", b->last_commit);
+
+       fputs("  last pack   : ", rpt);
+       if (b->pack_id < MAX_PACK_ID)
+               fprintf(rpt, "%u", b->pack_id);
+       fputc('\n', rpt);
+
+       fputc('\n', rpt);
+}
+
+static void write_crash_report(const char *err)
+{
+       char *loc = git_path("fast_import_crash_%d", getpid());
+       FILE *rpt = fopen(loc, "w");
+       struct branch *b;
+       unsigned long lu;
+       struct recent_command *rc;
+
+       if (!rpt) {
+               error("can't write crash report %s: %s", loc, strerror(errno));
+               return;
+       }
+
+       fprintf(stderr, "fast-import: dumping crash report to %s\n", loc);
+
+       fprintf(rpt, "fast-import crash report:\n");
+       fprintf(rpt, "    fast-import process: %d\n", getpid());
+       fprintf(rpt, "    parent process     : %d\n", getppid());
+       fprintf(rpt, "    at %s\n", show_date(time(NULL), 0, DATE_LOCAL));
+       fputc('\n', rpt);
+
+       fputs("fatal: ", rpt);
+       fputs(err, rpt);
+       fputc('\n', rpt);
+
+       fputc('\n', rpt);
+       fputs("Most Recent Commands Before Crash\n", rpt);
+       fputs("---------------------------------\n", rpt);
+       for (rc = cmd_hist.next; rc != &cmd_hist; rc = rc->next) {
+               if (rc->next == &cmd_hist)
+                       fputs("* ", rpt);
+               else
+                       fputs("  ", rpt);
+               fputs(rc->buf, rpt);
+               fputc('\n', rpt);
+       }
+
+       fputc('\n', rpt);
+       fputs("Active Branch LRU\n", rpt);
+       fputs("-----------------\n", rpt);
+       fprintf(rpt, "    active_branches = %lu cur, %lu max\n",
+               cur_active_branches,
+               max_active_branches);
+       fputc('\n', rpt);
+       fputs("  pos  clock name\n", rpt);
+       fputs("  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n", rpt);
+       for (b = active_branches, lu = 0; b; b = b->active_next_branch)
+               fprintf(rpt, "  %2lu) %6" PRIuMAX" %s\n",
+                       ++lu, b->last_commit, b->name);
+
+       fputc('\n', rpt);
+       fputs("Inactive Branches\n", rpt);
+       fputs("-----------------\n", rpt);
+       for (lu = 0; lu < branch_table_sz; lu++) {
+               for (b = branch_table[lu]; b; b = b->table_next_branch)
+                       write_branch_report(rpt, b);
+       }
+
+       fputc('\n', rpt);
+       fputs("-------------------\n", rpt);
+       fputs("END OF CRASH REPORT\n", rpt);
+       fclose(rpt);
+}
+
+static NORETURN void die_nicely(const char *err, va_list params)
+{
+       static int zombie;
+       char message[2 * PATH_MAX];
+
+       vsnprintf(message, sizeof(message), err, params);
+       fputs("fatal: ", stderr);
+       fputs(message, stderr);
+       fputc('\n', stderr);
+
+       if (!zombie) {
+               zombie = 1;
+               write_crash_report(message);
+       }
+       exit(128);
+}
 
 static void alloc_objects(unsigned int cnt)
 {
@@ -312,7 +469,7 @@ static void alloc_objects(unsigned int cnt)
        alloc_count += cnt;
 }
 
-static struct object_entrynew_object(unsigned char *sha1)
+static struct object_entry *new_object(unsigned char *sha1)
 {
        struct object_entry *e;
 
@@ -324,7 +481,7 @@ static struct object_entry* new_object(unsigned char *sha1)
        return e;
 }
 
-static struct object_entryfind_object(unsigned char *sha1)
+static struct object_entry *find_object(unsigned char *sha1)
 {
        unsigned int h = sha1[0] << 8 | sha1[1];
        struct object_entry *e;
@@ -334,7 +491,7 @@ static struct object_entry* find_object(unsigned char *sha1)
        return NULL;
 }
 
-static struct object_entryinsert_object(unsigned char *sha1)
+static struct object_entry *insert_object(unsigned char *sha1)
 {
        unsigned int h = sha1[0] << 8 | sha1[1];
        struct object_entry *e = object_table[h];
@@ -365,7 +522,7 @@ static unsigned int hc_str(const char *s, size_t len)
        return r;
 }
 
-static voidpool_alloc(size_t len)
+static void *pool_alloc(size_t len)
 {
        struct mem_pool *p;
        void *r;
@@ -395,7 +552,7 @@ static void* pool_alloc(size_t len)
        return r;
 }
 
-static voidpool_calloc(size_t count, size_t size)
+static void *pool_calloc(size_t count, size_t size)
 {
        size_t len = count * size;
        void *r = pool_alloc(len);
@@ -403,7 +560,7 @@ static void* pool_calloc(size_t count, size_t size)
        return r;
 }
 
-static charpool_strdup(const char *s)
+static char *pool_strdup(const char *s)
 {
        char *r = pool_alloc(strlen(s) + 1);
        strcpy(r, s);
@@ -444,7 +601,7 @@ static void insert_mark(uintmax_t idnum, struct object_entry *oe)
        s->data.marked[idnum] = oe;
 }
 
-static struct object_entryfind_mark(uintmax_t idnum)
+static struct object_entry *find_mark(uintmax_t idnum)
 {
        uintmax_t orig_idnum = idnum;
        struct mark_set *s = marks;
@@ -459,11 +616,11 @@ static struct object_entry* find_mark(uintmax_t idnum)
                        oe = s->data.marked[idnum];
        }
        if (!oe)
-               die("mark :%ju not declared", orig_idnum);
+               die("mark :%" PRIuMAX " not declared", orig_idnum);
        return oe;
 }
 
-static struct atom_strto_atom(const char *s, unsigned short len)
+static struct atom_str *to_atom(const char *s, unsigned short len)
 {
        unsigned int hc = hc_str(s, len) % atom_table_sz;
        struct atom_str *c;
@@ -482,7 +639,7 @@ static struct atom_str* to_atom(const char *s, unsigned short len)
        return c;
 }
 
-static struct branchlookup_branch(const char *name)
+static struct branch *lookup_branch(const char *name)
 {
        unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
        struct branch *b;
@@ -493,21 +650,26 @@ static struct branch* lookup_branch(const char *name)
        return NULL;
 }
 
-static struct branchnew_branch(const char *name)
+static struct branch *new_branch(const char *name)
 {
        unsigned int hc = hc_str(name, strlen(name)) % branch_table_sz;
        struct branch* b = lookup_branch(name);
 
        if (b)
                die("Invalid attempt to create duplicate branch: %s", name);
-       if (check_ref_format(name))
+       switch (check_ref_format(name)) {
+       case  0: break; /* its valid */
+       case -2: break; /* valid, but too few '/', allow anyway */
+       default:
                die("Branch name doesn't conform to GIT standards: %s", name);
+       }
 
        b = pool_calloc(1, sizeof(struct branch));
        b->name = pool_strdup(name);
        b->table_next_branch = branch_table[hc];
        b->branch_tree.versions[0].mode = S_IFDIR;
        b->branch_tree.versions[1].mode = S_IFDIR;
+       b->active = 0;
        b->pack_id = MAX_PACK_ID;
        branch_table[hc] = b;
        branch_count++;
@@ -520,7 +682,7 @@ static unsigned int hc_entries(unsigned int cnt)
        return cnt < avail_tree_table_sz ? cnt : avail_tree_table_sz - 1;
 }
 
-static struct tree_contentnew_tree_content(unsigned int cnt)
+static struct tree_content *new_tree_content(unsigned int cnt)
 {
        struct avail_tree_content *f, *l = NULL;
        struct tree_content *t;
@@ -564,7 +726,7 @@ static void release_tree_content_recursive(struct tree_content *t)
        release_tree_content(t);
 }
 
-static struct tree_contentgrow_tree_content(
+static struct tree_content *grow_tree_content(
        struct tree_content *t,
        int amt)
 {
@@ -576,7 +738,7 @@ static struct tree_content* grow_tree_content(
        return r;
 }
 
-static struct tree_entrynew_tree_entry(void)
+static struct tree_entry *new_tree_entry(void)
 {
        struct tree_entry *e;
 
@@ -604,6 +766,31 @@ static void release_tree_entry(struct tree_entry *e)
        avail_tree_entry = e;
 }
 
+static struct tree_content *dup_tree_content(struct tree_content *s)
+{
+       struct tree_content *d;
+       struct tree_entry *a, *b;
+       unsigned int i;
+
+       if (!s)
+               return NULL;
+       d = new_tree_content(s->entry_count);
+       for (i = 0; i < s->entry_count; i++) {
+               a = s->entries[i];
+               b = new_tree_entry();
+               memcpy(b, a, sizeof(*a));
+               if (a->tree && is_null_sha1(b->versions[1].sha1))
+                       b->tree = dup_tree_content(a->tree);
+               else
+                       b->tree = NULL;
+               d->entries[i] = b;
+       }
+       d->entry_count = s->entry_count;
+       d->delta_depth = s->delta_depth;
+
+       return d;
+}
+
 static void start_packfile(void)
 {
        static char tmpfile[PATH_MAX];
@@ -612,10 +799,8 @@ static void start_packfile(void)
        int pack_fd;
 
        snprintf(tmpfile, sizeof(tmpfile),
-               "%s/pack_XXXXXX", get_object_directory());
-       pack_fd = mkstemp(tmpfile);
-       if (pack_fd < 0)
-               die("Can't create %s: %s", tmpfile, strerror(errno));
+               "%s/tmp_pack_XXXXXX", get_object_directory());
+       pack_fd = xmkstemp(tmpfile);
        p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2);
        strcpy(p->pack_name, tmpfile);
        p->pack_fd = pack_fd;
@@ -633,42 +818,6 @@ static void start_packfile(void)
        all_packs[pack_id] = p;
 }
 
-static void fixup_header_footer(void)
-{
-       static const int buf_sz = 128 * 1024;
-       int pack_fd = pack_data->pack_fd;
-       SHA_CTX c;
-       struct pack_header hdr;
-       char *buf;
-
-       if (lseek(pack_fd, 0, SEEK_SET) != 0)
-               die("Failed seeking to start: %s", strerror(errno));
-       if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
-               die("Unable to reread header of %s", pack_data->pack_name);
-       if (lseek(pack_fd, 0, SEEK_SET) != 0)
-               die("Failed seeking to start: %s", strerror(errno));
-       hdr.hdr_entries = htonl(object_count);
-       write_or_die(pack_fd, &hdr, sizeof(hdr));
-
-       SHA1_Init(&c);
-       SHA1_Update(&c, &hdr, sizeof(hdr));
-
-       buf = xmalloc(buf_sz);
-       for (;;) {
-               size_t n = xread(pack_fd, buf, buf_sz);
-               if (!n)
-                       break;
-               if (n < 0)
-                       die("Failed to checksum %s", pack_data->pack_name);
-               SHA1_Update(&c, buf, n);
-       }
-       free(buf);
-
-       SHA1_Final(pack_data->sha1, &c);
-       write_or_die(pack_fd, pack_data->sha1, sizeof(pack_data->sha1));
-       close(pack_fd);
-}
-
 static int oecmp (const void *a_, const void *b_)
 {
        struct object_entry *a = *((struct object_entry**)a_);
@@ -676,7 +825,7 @@ static int oecmp (const void *a_, const void *b_)
        return hashcmp(a->sha1, b->sha1);
 }
 
-static charcreate_index(void)
+static char *create_index(void)
 {
        static char tmpfile[PATH_MAX];
        SHA_CTX ctx;
@@ -712,10 +861,8 @@ static char* create_index(void)
        }
 
        snprintf(tmpfile, sizeof(tmpfile),
-               "%s/index_XXXXXX", get_object_directory());
-       idx_fd = mkstemp(tmpfile);
-       if (idx_fd < 0)
-               die("Can't create %s: %s", tmpfile, strerror(errno));
+               "%s/tmp_idx_XXXXXX", get_object_directory());
+       idx_fd = xmkstemp(tmpfile);
        f = sha1fd(idx_fd, tmpfile);
        sha1write(f, array, 256 * sizeof(int));
        SHA1_Init(&ctx);
@@ -732,10 +879,10 @@ static char* create_index(void)
        return tmpfile;
 }
 
-static charkeep_pack(char *curr_index_name)
+static char *keep_pack(char *curr_index_name)
 {
        static char name[PATH_MAX];
-       static char *keep_msg = "fast-import";
+       static const char *keep_msg = "fast-import";
        int keep_fd;
 
        chmod(pack_data->pack_name, 0444);
@@ -784,7 +931,9 @@ static void end_packfile(void)
                struct branch *b;
                struct tag *t;
 
-               fixup_header_footer();
+               fixup_pack_header_footer(pack_data->pack_fd, pack_data->sha1,
+                                   pack_data->pack_name, object_count);
+               close(pack_data->pack_fd);
                idx_name = keep_pack(create_index());
 
                /* Register the packfile with core git's machinary. */
@@ -796,18 +945,21 @@ static void end_packfile(void)
                install_packed_git(new_p);
 
                /* Print the boundary */
-               fprintf(stdout, "%s:", new_p->pack_name);
-               for (i = 0; i < branch_table_sz; i++) {
-                       for (b = branch_table[i]; b; b = b->table_next_branch) {
-                               if (b->pack_id == pack_id)
-                                       fprintf(stdout, " %s", sha1_to_hex(b->sha1));
+               if (pack_edges) {
+                       fprintf(pack_edges, "%s:", new_p->pack_name);
+                       for (i = 0; i < branch_table_sz; i++) {
+                               for (b = branch_table[i]; b; b = b->table_next_branch) {
+                                       if (b->pack_id == pack_id)
+                                               fprintf(pack_edges, " %s", sha1_to_hex(b->sha1));
+                               }
                        }
+                       for (t = first_tag; t; t = t->next_tag) {
+                               if (t->pack_id == pack_id)
+                                       fprintf(pack_edges, " %s", sha1_to_hex(t->sha1));
+                       }
+                       fputc('\n', pack_edges);
+                       fflush(pack_edges);
                }
-               for (t = first_tag; t; t = t->next_tag) {
-                       if (t->pack_id == pack_id)
-                               fprintf(stdout, " %s", sha1_to_hex(t->sha1));
-               }
-               fputc('\n', stdout);
 
                pack_id++;
        }
@@ -823,7 +975,7 @@ static void end_packfile(void)
        last_blob.depth = 0;
 }
 
-static void checkpoint(void)
+static void cycle_packfile(void)
 {
        end_packfile();
        start_packfile();
@@ -868,7 +1020,8 @@ static int store_object(
        SHA_CTX c;
        z_stream s;
 
-       hdrlen = sprintf((char*)hdr,"%s %lu",type_names[type],datlen) + 1;
+       hdrlen = sprintf((char*)hdr,"%s %lu", typename(type),
+               (unsigned long)datlen) + 1;
        SHA1_Init(&c);
        SHA1_Update(&c, hdr, hdrlen);
        SHA1_Update(&c, dat, datlen);
@@ -882,6 +1035,12 @@ static int store_object(
        if (e->offset) {
                duplicate_count_by_type[type]++;
                return 1;
+       } else if (find_sha1_pack(sha1, packed_git)) {
+               e->type = type;
+               e->pack_id = MAX_PACK_ID;
+               e->offset = 1; /* just not zero! */
+               duplicate_count_by_type[type]++;
+               return 1;
        }
 
        if (last && last->data && last->depth < max_depth) {
@@ -916,7 +1075,7 @@ static int store_object(
 
                /* This new object needs to *not* have the current pack_id. */
                e->pack_id = pack_id + 1;
-               checkpoint();
+               cycle_packfile();
 
                /* We cannot carry a delta into the new pack. */
                if (delta) {
@@ -969,10 +1128,9 @@ static int store_object(
        pack_size += s.total_out;
 
        free(out);
-       if (delta)
-               free(delta);
+       free(delta);
        if (last) {
-               if (last->data && !last->no_free)
+               if (!last->no_free)
                        free(last->data);
                last->data = dat;
                last->offset = e->offset;
@@ -985,11 +1143,11 @@ static void *gfi_unpack_entry(
        struct object_entry *oe,
        unsigned long *sizep)
 {
-       static char type[20];
+       enum object_type type;
        struct packed_git *p = all_packs[oe->pack_id];
        if (p == pack_data)
                p->pack_size = pack_size + 20;
-       return unpack_entry(p, oe->offset, type, sizep);
+       return unpack_entry(p, oe->offset, &type, sizep);
 }
 
 static const char *get_mode(const char *str, uint16_t *modep)
@@ -1020,15 +1178,15 @@ static void load_tree(struct tree_entry *root)
                return;
 
        myoe = find_object(sha1);
-       if (myoe) {
+       if (myoe && myoe->pack_id != MAX_PACK_ID) {
                if (myoe->type != OBJ_TREE)
                        die("Not a tree: %s", sha1_to_hex(sha1));
                t->delta_depth = 0;
                buf = gfi_unpack_entry(myoe, &size);
        } else {
-               char type[20];
-               buf = read_sha1_file(sha1, type, &size);
-               if (!buf || strcmp(type, tree_type))
+               enum object_type type;
+               buf = read_sha1_file(sha1, &type, &size);
+               if (!buf || type != OBJ_TREE)
                        die("Can't load tree %s", sha1_to_hex(sha1));
        }
 
@@ -1037,7 +1195,7 @@ static void load_tree(struct tree_entry *root)
                struct tree_entry *e = new_tree_entry();
 
                if (t->entry_count == t->entry_capacity)
-                       root->tree = t = grow_tree_content(t, 8);
+                       root->tree = t = grow_tree_content(t, t->entry_count);
                t->entries[t->entry_count++] = e;
 
                e->tree = NULL;
@@ -1045,7 +1203,7 @@ static void load_tree(struct tree_entry *root)
                if (!c)
                        die("Corrupt mode in %s", sha1_to_hex(sha1));
                e->versions[0].mode = e->versions[1].mode;
-               e->name = to_atom(c, (unsigned short)strlen(c));
+               e->name = to_atom(c, strlen(c));
                c += e->name->str_len + 1;
                hashcpy(e->versions[0].sha1, (unsigned char*)c);
                hashcpy(e->versions[1].sha1, (unsigned char*)c);
@@ -1129,6 +1287,7 @@ static void store_tree(struct tree_entry *root)
                || le->pack_id != pack_id) {
                lo.data = NULL;
                lo.depth = 0;
+               lo.no_free = 0;
        } else {
                mktree(t, 0, &lo.len, &old_tree);
                lo.data = old_tree.buffer;
@@ -1160,7 +1319,8 @@ static int tree_content_set(
        struct tree_entry *root,
        const char *p,
        const unsigned char *sha1,
-       const uint16_t mode)
+       const uint16_t mode,
+       struct tree_content *subtree)
 {
        struct tree_content *t = root->tree;
        const char *slash1;
@@ -1172,20 +1332,24 @@ static int tree_content_set(
                n = slash1 - p;
        else
                n = strlen(p);
+       if (!n)
+               die("Empty path component found in input");
+       if (!slash1 && !S_ISDIR(mode) && subtree)
+               die("Non-directories cannot have subtrees");
 
        for (i = 0; i < t->entry_count; i++) {
                e = t->entries[i];
                if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
                        if (!slash1) {
-                               if (e->versions[1].mode == mode
+                               if (!S_ISDIR(mode)
+                                               && e->versions[1].mode == mode
                                                && !hashcmp(e->versions[1].sha1, sha1))
                                        return 0;
                                e->versions[1].mode = mode;
                                hashcpy(e->versions[1].sha1, sha1);
-                               if (e->tree) {
+                               if (e->tree)
                                        release_tree_content_recursive(e->tree);
-                                       e->tree = NULL;
-                               }
+                               e->tree = subtree;
                                hashclr(root->versions[1].sha1);
                                return 1;
                        }
@@ -1195,7 +1359,7 @@ static int tree_content_set(
                        }
                        if (!e->tree)
                                load_tree(e);
-                       if (tree_content_set(e, slash1 + 1, sha1, mode)) {
+                       if (tree_content_set(e, slash1 + 1, sha1, mode, subtree)) {
                                hashclr(root->versions[1].sha1);
                                return 1;
                        }
@@ -1204,18 +1368,18 @@ static int tree_content_set(
        }
 
        if (t->entry_count == t->entry_capacity)
-               root->tree = t = grow_tree_content(t, 8);
+               root->tree = t = grow_tree_content(t, t->entry_count);
        e = new_tree_entry();
-       e->name = to_atom(p, (unsigned short)n);
+       e->name = to_atom(p, n);
        e->versions[0].mode = 0;
        hashclr(e->versions[0].sha1);
        t->entries[t->entry_count++] = e;
        if (slash1) {
                e->tree = new_tree_content(8);
                e->versions[1].mode = S_IFDIR;
-               tree_content_set(e, slash1 + 1, sha1, mode);
+               tree_content_set(e, slash1 + 1, sha1, mode, subtree);
        } else {
-               e->tree = NULL;
+               e->tree = subtree;
                e->versions[1].mode = mode;
                hashcpy(e->versions[1].sha1, sha1);
        }
@@ -1223,7 +1387,10 @@ static int tree_content_set(
        return 1;
 }
 
-static int tree_content_remove(struct tree_entry *root, const char *p)
+static int tree_content_remove(
+       struct tree_entry *root,
+       const char *p,
+       struct tree_entry *backup_leaf)
 {
        struct tree_content *t = root->tree;
        const char *slash1;
@@ -1243,13 +1410,14 @@ static int tree_content_remove(struct tree_entry *root, const char *p)
                                goto del_entry;
                        if (!e->tree)
                                load_tree(e);
-                       if (tree_content_remove(e, slash1 + 1)) {
+                       if (tree_content_remove(e, slash1 + 1, backup_leaf)) {
                                for (n = 0; n < e->tree->entry_count; n++) {
                                        if (e->tree->entries[n]->versions[1].mode) {
                                                hashclr(root->versions[1].sha1);
                                                return 1;
                                        }
                                }
+                               backup_leaf = NULL;
                                goto del_entry;
                        }
                        return 0;
@@ -1258,29 +1426,96 @@ static int tree_content_remove(struct tree_entry *root, const char *p)
        return 0;
 
 del_entry:
-       if (e->tree) {
+       if (backup_leaf)
+               memcpy(backup_leaf, e, sizeof(*backup_leaf));
+       else if (e->tree)
                release_tree_content_recursive(e->tree);
-               e->tree = NULL;
-       }
+       e->tree = NULL;
        e->versions[1].mode = 0;
        hashclr(e->versions[1].sha1);
        hashclr(root->versions[1].sha1);
        return 1;
 }
 
-static void dump_branches(void)
+static int tree_content_get(
+       struct tree_entry *root,
+       const char *p,
+       struct tree_entry *leaf)
+{
+       struct tree_content *t = root->tree;
+       const char *slash1;
+       unsigned int i, n;
+       struct tree_entry *e;
+
+       slash1 = strchr(p, '/');
+       if (slash1)
+               n = slash1 - p;
+       else
+               n = strlen(p);
+
+       for (i = 0; i < t->entry_count; i++) {
+               e = t->entries[i];
+               if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
+                       if (!slash1) {
+                               memcpy(leaf, e, sizeof(*leaf));
+                               if (e->tree && is_null_sha1(e->versions[1].sha1))
+                                       leaf->tree = dup_tree_content(e->tree);
+                               else
+                                       leaf->tree = NULL;
+                               return 1;
+                       }
+                       if (!S_ISDIR(e->versions[1].mode))
+                               return 0;
+                       if (!e->tree)
+                               load_tree(e);
+                       return tree_content_get(e, slash1 + 1, leaf);
+               }
+       }
+       return 0;
+}
+
+static int update_branch(struct branch *b)
 {
        static const char *msg = "fast-import";
+       struct ref_lock *lock;
+       unsigned char old_sha1[20];
+
+       if (read_ref(b->name, old_sha1))
+               hashclr(old_sha1);
+       lock = lock_any_ref_for_update(b->name, old_sha1, 0);
+       if (!lock)
+               return error("Unable to lock %s", b->name);
+       if (!force_update && !is_null_sha1(old_sha1)) {
+               struct commit *old_cmit, *new_cmit;
+
+               old_cmit = lookup_commit_reference_gently(old_sha1, 0);
+               new_cmit = lookup_commit_reference_gently(b->sha1, 0);
+               if (!old_cmit || !new_cmit) {
+                       unlock_ref(lock);
+                       return error("Branch %s is missing commits.", b->name);
+               }
+
+               if (!in_merge_bases(old_cmit, &new_cmit, 1)) {
+                       unlock_ref(lock);
+                       warning("Not updating %s"
+                               " (new tip %s does not contain %s)",
+                               b->name, sha1_to_hex(b->sha1), sha1_to_hex(old_sha1));
+                       return -1;
+               }
+       }
+       if (write_ref_sha1(lock, b->sha1, msg) < 0)
+               return error("Unable to update %s", b->name);
+       return 0;
+}
+
+static void dump_branches(void)
+{
        unsigned int i;
        struct branch *b;
-       struct ref_lock *lock;
 
        for (i = 0; i < branch_table_sz; i++) {
-               for (b = branch_table[i]; b; b = b->table_next_branch) {
-                       lock = lock_any_ref_for_update(b->name, NULL);
-                       if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0)
-                               die("Can't write %s", b->name);
-               }
+               for (b = branch_table[i]; b; b = b->table_next_branch)
+                       failure |= update_branch(b);
        }
 }
 
@@ -1289,13 +1524,13 @@ static void dump_tags(void)
        static const char *msg = "fast-import";
        struct tag *t;
        struct ref_lock *lock;
-       char path[PATH_MAX];
+       char ref_name[PATH_MAX];
 
        for (t = first_tag; t; t = t->next_tag) {
-               sprintf(path, "refs/tags/%s", t->name);
-               lock = lock_any_ref_for_update(path, NULL);
+               sprintf(ref_name, "tags/%s", t->name);
+               lock = lock_ref_sha1(ref_name, NULL);
                if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
-                       die("Can't write %s", path);
+                       failure |= error("Unable to update %s", ref_name);
        }
 }
 
@@ -1313,7 +1548,7 @@ static void dump_marks_helper(FILE *f,
        } else {
                for (k = 0; k < 1024; k++) {
                        if (m->data.marked[k])
-                               fprintf(f, ":%ju %s\n", base + k,
+                               fprintf(f, ":%" PRIuMAX " %s\n", base + k,
                                        sha1_to_hex(m->data.marked[k]->sha1));
                }
        }
@@ -1321,22 +1556,79 @@ static void dump_marks_helper(FILE *f,
 
 static void dump_marks(void)
 {
-       if (mark_file)
-       {
-               FILE *f = fopen(mark_file, "w");
-               dump_marks_helper(f, 0, marks);
-               fclose(f);
+       static struct lock_file mark_lock;
+       int mark_fd;
+       FILE *f;
+
+       if (!mark_file)
+               return;
+
+       mark_fd = hold_lock_file_for_update(&mark_lock, mark_file, 0);
+       if (mark_fd < 0) {
+               failure |= error("Unable to write marks file %s: %s",
+                       mark_file, strerror(errno));
+               return;
+       }
+
+       f = fdopen(mark_fd, "w");
+       if (!f) {
+               rollback_lock_file(&mark_lock);
+               failure |= error("Unable to write marks file %s: %s",
+                       mark_file, strerror(errno));
+               return;
        }
+
+       dump_marks_helper(f, 0, marks);
+       fclose(f);
+       if (commit_lock_file(&mark_lock))
+               failure |= error("Unable to write marks file %s: %s",
+                       mark_file, strerror(errno));
 }
 
 static void read_next_command(void)
 {
-       read_line(&command_buf, stdin, '\n');
+       do {
+               if (unread_command_buf) {
+                       unread_command_buf = 0;
+                       if (command_buf.eof)
+                               return;
+               } else {
+                       struct recent_command *rc;
+
+                       command_buf.buf = NULL;
+                       read_line(&command_buf, stdin, '\n');
+                       if (command_buf.eof)
+                               return;
+
+                       rc = rc_free;
+                       if (rc)
+                               rc_free = rc->next;
+                       else {
+                               rc = cmd_hist.next;
+                               cmd_hist.next = rc->next;
+                               cmd_hist.next->prev = &cmd_hist;
+                               free(rc->buf);
+                       }
+
+                       rc->buf = command_buf.buf;
+                       rc->prev = cmd_tail;
+                       rc->next = cmd_hist.prev;
+                       rc->prev->next = rc;
+                       cmd_tail = rc;
+               }
+       } while (command_buf.buf[0] == '#');
+}
+
+static void skip_optional_lf(void)
+{
+       int term_char = fgetc(stdin);
+       if (term_char != '\n' && term_char != EOF)
+               ungetc(term_char, stdin);
 }
 
 static void cmd_mark(void)
 {
-       if (!strncmp("mark :", command_buf.buf, 6)) {
+       if (!prefixcmp(command_buf.buf, "mark :")) {
                next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
                read_next_command();
        }
@@ -1344,32 +1636,28 @@ static void cmd_mark(void)
                next_mark = 0;
 }
 
-static voidcmd_data (size_t *size)
+static void *cmd_data (size_t *size)
 {
        size_t length;
        char *buffer;
 
-       if (strncmp("data ", command_buf.buf, 5))
+       if (prefixcmp(command_buf.buf, "data "))
                die("Expected 'data n' command, found: %s", command_buf.buf);
 
-       if (!strncmp("<<", command_buf.buf + 5, 2)) {
+       if (!prefixcmp(command_buf.buf + 5, "<<")) {
                char *term = xstrdup(command_buf.buf + 5 + 2);
                size_t sz = 8192, term_len = command_buf.len - 5 - 2;
                length = 0;
                buffer = xmalloc(sz);
+               command_buf.buf = NULL;
                for (;;) {
-                       read_next_command();
+                       read_line(&command_buf, stdin, '\n');
                        if (command_buf.eof)
                                die("EOF in data (terminator '%s' not found)", term);
                        if (term_len == command_buf.len
                                && !strcmp(term, command_buf.buf))
                                break;
-                       if (sz < (length + command_buf.len)) {
-                               sz = sz * 3 / 2 + 16;
-                               if (sz < (length + command_buf.len))
-                                       sz = length + command_buf.len;
-                               buffer = xrealloc(buffer, sz);
-                       }
+                       ALLOC_GROW(buffer, length + command_buf.len, sz);
                        memcpy(buffer + length,
                                command_buf.buf,
                                command_buf.len - 1);
@@ -1385,18 +1673,75 @@ static void* cmd_data (size_t *size)
                while (n < length) {
                        size_t s = fread(buffer + n, 1, length - n, stdin);
                        if (!s && feof(stdin))
-                               die("EOF in data (%lu bytes remaining)", length - n);
+                               die("EOF in data (%lu bytes remaining)",
+                                       (unsigned long)(length - n));
                        n += s;
                }
        }
 
-       if (fgetc(stdin) != '\n')
-               die("An lf did not trail the binary data as expected.");
-
+       skip_optional_lf();
        *size = length;
        return buffer;
 }
 
+static int validate_raw_date(const char *src, char *result, int maxlen)
+{
+       const char *orig_src = src;
+       char *endp, sign;
+
+       strtoul(src, &endp, 10);
+       if (endp == src || *endp != ' ')
+               return -1;
+
+       src = endp + 1;
+       if (*src != '-' && *src != '+')
+               return -1;
+       sign = *src;
+
+       strtoul(src + 1, &endp, 10);
+       if (endp == src || *endp || (endp - orig_src) >= maxlen)
+               return -1;
+
+       strcpy(result, orig_src);
+       return 0;
+}
+
+static char *parse_ident(const char *buf)
+{
+       const char *gt;
+       size_t name_len;
+       char *ident;
+
+       gt = strrchr(buf, '>');
+       if (!gt)
+               die("Missing > in ident string: %s", buf);
+       gt++;
+       if (*gt != ' ')
+               die("Missing space after > in ident string: %s", buf);
+       gt++;
+       name_len = gt - buf;
+       ident = xmalloc(name_len + 24);
+       strncpy(ident, buf, name_len);
+
+       switch (whenspec) {
+       case WHENSPEC_RAW:
+               if (validate_raw_date(gt, ident + name_len, 24) < 0)
+                       die("Invalid raw date \"%s\" in ident: %s", gt, buf);
+               break;
+       case WHENSPEC_RFC2822:
+               if (parse_date(gt, ident + name_len, 24) < 0)
+                       die("Invalid rfc2822 date \"%s\" in ident: %s", gt, buf);
+               break;
+       case WHENSPEC_NOW:
+               if (strcmp("now", gt))
+                       die("Date in ident must be 'now': %s", buf);
+               datestamp(ident + name_len, 24);
+               break;
+       }
+
+       return ident;
+}
+
 static void cmd_new_blob(void)
 {
        size_t l;
@@ -1414,7 +1759,7 @@ static void unload_one_branch(void)
 {
        while (cur_active_branches
                && cur_active_branches >= max_active_branches) {
-               unsigned long min_commit = ULONG_MAX;
+               uintmax_t min_commit = ULONG_MAX;
                struct branch *e, *l = NULL, *p = NULL;
 
                for (e = active_branches; e; e = e->active_next_branch) {
@@ -1432,6 +1777,7 @@ static void unload_one_branch(void)
                        e = active_branches;
                        active_branches = e->active_next_branch;
                }
+               e->active = 0;
                e->active_next_branch = NULL;
                if (e->branch_tree.tree) {
                        release_tree_content_recursive(e->branch_tree.tree);
@@ -1444,10 +1790,13 @@ static void unload_one_branch(void)
 static void load_branch(struct branch *b)
 {
        load_tree(&b->branch_tree);
-       b->active_next_branch = active_branches;
-       active_branches = b;
-       cur_active_branches++;
-       branch_load_count++;
+       if (!b->active) {
+               b->active = 1;
+               b->active_next_branch = active_branches;
+               active_branches = b;
+               cur_active_branches++;
+               branch_load_count++;
+       }
 }
 
 static void file_change_m(struct branch *b)
@@ -1458,7 +1807,6 @@ static void file_change_m(struct branch *b)
        struct object_entry *oe = oe;
        unsigned char sha1[20];
        uint16_t mode, inline_data = 0;
-       char type[20];
 
        p = get_mode(p, &mode);
        if (!p)
@@ -1480,7 +1828,7 @@ static void file_change_m(struct branch *b)
                oe = find_mark(strtoumax(p + 1, &x, 10));
                hashcpy(sha1, oe->sha1);
                p = x;
-       } else if (!strncmp("inline", p, 6)) {
+       } else if (!prefixcmp(p, "inline")) {
                inline_data = 1;
                p += 6;
        } else {
@@ -1511,19 +1859,18 @@ static void file_change_m(struct branch *b)
        } else if (oe) {
                if (oe->type != OBJ_BLOB)
                        die("Not a blob (actually a %s): %s",
-                               command_buf.buf, type_names[oe->type]);
+                               command_buf.buf, typename(oe->type));
        } else {
-               if (sha1_object_info(sha1, type, NULL))
+               enum object_type type = sha1_object_info(sha1, NULL);
+               if (type < 0)
                        die("Blob not found: %s", command_buf.buf);
-               if (strcmp(blob_type, type))
+               if (type != OBJ_BLOB)
                        die("Not a blob (actually a %s): %s",
-                               command_buf.buf, type);
+                           typename(type), command_buf.buf);
        }
 
-       tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
-
-       if (p_uq)
-               free(p_uq);
+       tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode, NULL);
+       free(p_uq);
 }
 
 static void file_change_d(struct branch *b)
@@ -1538,21 +1885,108 @@ static void file_change_d(struct branch *b)
                        die("Garbage after path in: %s", command_buf.buf);
                p = p_uq;
        }
-       tree_content_remove(&b->branch_tree, p);
-       if (p_uq)
-               free(p_uq);
+       tree_content_remove(&b->branch_tree, p, NULL);
+       free(p_uq);
+}
+
+static void file_change_cr(struct branch *b, int rename)
+{
+       const char *s, *d;
+       char *s_uq, *d_uq;
+       const char *endp;
+       struct tree_entry leaf;
+
+       s = command_buf.buf + 2;
+       s_uq = unquote_c_style(s, &endp);
+       if (s_uq) {
+               if (*endp != ' ')
+                       die("Missing space after source: %s", command_buf.buf);
+       }
+       else {
+               endp = strchr(s, ' ');
+               if (!endp)
+                       die("Missing space after source: %s", command_buf.buf);
+               s_uq = xmalloc(endp - s + 1);
+               memcpy(s_uq, s, endp - s);
+               s_uq[endp - s] = 0;
+       }
+       s = s_uq;
+
+       endp++;
+       if (!*endp)
+               die("Missing dest: %s", command_buf.buf);
+
+       d = endp;
+       d_uq = unquote_c_style(d, &endp);
+       if (d_uq) {
+               if (*endp)
+                       die("Garbage after dest in: %s", command_buf.buf);
+               d = d_uq;
+       }
+
+       memset(&leaf, 0, sizeof(leaf));
+       if (rename)
+               tree_content_remove(&b->branch_tree, s, &leaf);
+       else
+               tree_content_get(&b->branch_tree, s, &leaf);
+       if (!leaf.versions[1].mode)
+               die("Path %s not in branch", s);
+       tree_content_set(&b->branch_tree, d,
+               leaf.versions[1].sha1,
+               leaf.versions[1].mode,
+               leaf.tree);
+
+       free(s_uq);
+       free(d_uq);
+}
+
+static void file_change_deleteall(struct branch *b)
+{
+       release_tree_content_recursive(b->branch_tree.tree);
+       hashclr(b->branch_tree.versions[0].sha1);
+       hashclr(b->branch_tree.versions[1].sha1);
+       load_tree(&b->branch_tree);
+}
+
+static void cmd_from_commit(struct branch *b, char *buf, unsigned long size)
+{
+       if (!buf || size < 46)
+               die("Not a valid commit: %s", sha1_to_hex(b->sha1));
+       if (memcmp("tree ", buf, 5)
+               || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
+               die("The commit %s is corrupt", sha1_to_hex(b->sha1));
+       hashcpy(b->branch_tree.versions[0].sha1,
+               b->branch_tree.versions[1].sha1);
+}
+
+static void cmd_from_existing(struct branch *b)
+{
+       if (is_null_sha1(b->sha1)) {
+               hashclr(b->branch_tree.versions[0].sha1);
+               hashclr(b->branch_tree.versions[1].sha1);
+       } else {
+               unsigned long size;
+               char *buf;
+
+               buf = read_object_with_reference(b->sha1,
+                       commit_type, &size, b->sha1);
+               cmd_from_commit(b, buf, size);
+               free(buf);
+       }
 }
 
-static void cmd_from(struct branch *b)
+static int cmd_from(struct branch *b)
 {
        const char *from;
        struct branch *s;
 
-       if (strncmp("from ", command_buf.buf, 5))
-               return;
+       if (prefixcmp(command_buf.buf, "from "))
+               return 0;
 
-       if (b->last_commit)
-               die("Can't reinitailize branch %s", b->name);
+       if (b->branch_tree.tree) {
+               release_tree_content_recursive(b->branch_tree.tree);
+               b->branch_tree.tree = NULL;
+       }
 
        from = strchr(command_buf.buf, ' ') + 1;
        s = lookup_branch(from);
@@ -1566,53 +2000,33 @@ static void cmd_from(struct branch *b)
        } else if (*from == ':') {
                uintmax_t idnum = strtoumax(from + 1, NULL, 10);
                struct object_entry *oe = find_mark(idnum);
-               unsigned long size;
-               char *buf;
                if (oe->type != OBJ_COMMIT)
-                       die("Mark :%ju not a commit", idnum);
+                       die("Mark :%" PRIuMAX " not a commit", idnum);
                hashcpy(b->sha1, oe->sha1);
-               buf = gfi_unpack_entry(oe, &size);
-               if (!buf || size < 46)
-                       die("Not a valid commit: %s", from);
-               if (memcmp("tree ", buf, 5)
-                       || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
-                       die("The commit %s is corrupt", sha1_to_hex(b->sha1));
-               free(buf);
-               hashcpy(b->branch_tree.versions[0].sha1,
-                       b->branch_tree.versions[1].sha1);
-       } else if (!get_sha1(from, b->sha1)) {
-               if (is_null_sha1(b->sha1)) {
-                       hashclr(b->branch_tree.versions[0].sha1);
-                       hashclr(b->branch_tree.versions[1].sha1);
-               } else {
+               if (oe->pack_id != MAX_PACK_ID) {
                        unsigned long size;
-                       char *buf;
-
-                       buf = read_object_with_reference(b->sha1,
-                               type_names[OBJ_COMMIT], &size, b->sha1);
-                       if (!buf || size < 46)
-                               die("Not a valid commit: %s", from);
-                       if (memcmp("tree ", buf, 5)
-                               || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
-                               die("The commit %s is corrupt", sha1_to_hex(b->sha1));
+                       char *buf = gfi_unpack_entry(oe, &size);
+                       cmd_from_commit(b, buf, size);
                        free(buf);
-                       hashcpy(b->branch_tree.versions[0].sha1,
-                               b->branch_tree.versions[1].sha1);
-               }
-       } else
+               } else
+                       cmd_from_existing(b);
+       } else if (!get_sha1(from, b->sha1))
+               cmd_from_existing(b);
+       else
                die("Invalid ref name or SHA1 expression: %s", from);
 
        read_next_command();
+       return 1;
 }
 
-static struct hash_listcmd_merge(unsigned int *count)
+static struct hash_list *cmd_merge(unsigned int *count)
 {
        struct hash_list *list = NULL, *n, *e = e;
        const char *from;
        struct branch *s;
 
        *count = 0;
-       while (!strncmp("merge ", command_buf.buf, 6)) {
+       while (!prefixcmp(command_buf.buf, "merge ")) {
                from = strchr(command_buf.buf, ' ') + 1;
                n = xmalloc(sizeof(*n));
                s = lookup_branch(from);
@@ -1622,9 +2036,16 @@ static struct hash_list* cmd_merge(unsigned int *count)
                        uintmax_t idnum = strtoumax(from + 1, NULL, 10);
                        struct object_entry *oe = find_mark(idnum);
                        if (oe->type != OBJ_COMMIT)
-                               die("Mark :%ju not a commit", idnum);
+                               die("Mark :%" PRIuMAX " not a commit", idnum);
                        hashcpy(n->sha1, oe->sha1);
-               } else if (get_sha1(from, n->sha1))
+               } else if (!get_sha1(from, n->sha1)) {
+                       unsigned long size;
+                       char *buf = read_object_with_reference(n->sha1,
+                               commit_type, &size, n->sha1);
+                       if (!buf || size < 46)
+                               die("Not a valid commit: %s", from);
+                       free(buf);
+               } else
                        die("Invalid ref name or SHA1 expression: %s", from);
 
                n->next = NULL;
@@ -1658,12 +2079,12 @@ static void cmd_new_commit(void)
 
        read_next_command();
        cmd_mark();
-       if (!strncmp("author ", command_buf.buf, 7)) {
-               author = strdup(command_buf.buf);
+       if (!prefixcmp(command_buf.buf, "author ")) {
+               author = parse_ident(command_buf.buf + 7);
                read_next_command();
        }
-       if (!strncmp("committer ", command_buf.buf, 10)) {
-               committer = strdup(command_buf.buf);
+       if (!prefixcmp(command_buf.buf, "committer ")) {
+               committer = parse_ident(command_buf.buf + 10);
                read_next_command();
        }
        if (!committer)
@@ -1680,15 +2101,21 @@ static void cmd_new_commit(void)
        }
 
        /* file_change* */
-       for (;;) {
-               if (1 == command_buf.len)
-                       break;
-               else if (!strncmp("M ", command_buf.buf, 2))
+       while (!command_buf.eof && command_buf.len > 1) {
+               if (!prefixcmp(command_buf.buf, "M "))
                        file_change_m(b);
-               else if (!strncmp("D ", command_buf.buf, 2))
+               else if (!prefixcmp(command_buf.buf, "D "))
                        file_change_d(b);
-               else
-                       die("Unsupported file_change: %s", command_buf.buf);
+               else if (!prefixcmp(command_buf.buf, "R "))
+                       file_change_cr(b, 1);
+               else if (!prefixcmp(command_buf.buf, "C "))
+                       file_change_cr(b, 0);
+               else if (!strcmp("deleteall", command_buf.buf))
+                       file_change_deleteall(b);
+               else {
+                       unread_command_buf = 1;
+                       break;
+               }
                read_next_command();
        }
 
@@ -1696,7 +2123,7 @@ static void cmd_new_commit(void)
        store_tree(&b->branch_tree);
        hashcpy(b->branch_tree.versions[0].sha1,
                b->branch_tree.versions[1].sha1);
-       size_dbuf(&new_data, 97 + msglen
+       size_dbuf(&new_data, 114 + msglen
                + merge_count * 49
                + (author
                        ? strlen(author) + strlen(committer)
@@ -1712,15 +2139,12 @@ static void cmd_new_commit(void)
                free(merge_list);
                merge_list = next;
        }
-       if (author)
-               sp += sprintf(sp, "%s\n", author);
-       else
-               sp += sprintf(sp, "author %s\n", committer + 10);
-       sp += sprintf(sp, "%s\n\n", committer);
+       sp += sprintf(sp, "author %s\n", author ? author : committer);
+       sp += sprintf(sp, "committer %s\n", committer);
+       *sp++ = '\n';
        memcpy(sp, msg, msglen);
        sp += msglen;
-       if (author)
-               free(author);
+       free(author);
        free(committer);
        free(msg);
 
@@ -1756,7 +2180,7 @@ static void cmd_new_tag(void)
        read_next_command();
 
        /* from ... */
-       if (strncmp("from ", command_buf.buf, 5))
+       if (prefixcmp(command_buf.buf, "from "))
                die("Expected from command, got %s", command_buf.buf);
        from = strchr(command_buf.buf, ' ') + 1;
        s = lookup_branch(from);
@@ -1767,14 +2191,14 @@ static void cmd_new_tag(void)
                from_mark = strtoumax(from + 1, NULL, 10);
                oe = find_mark(from_mark);
                if (oe->type != OBJ_COMMIT)
-                       die("Mark :%ju not a commit", from_mark);
+                       die("Mark :%" PRIuMAX " not a commit", from_mark);
                hashcpy(sha1, oe->sha1);
        } else if (!get_sha1(from, sha1)) {
                unsigned long size;
                char *buf;
 
                buf = read_object_with_reference(sha1,
-                       type_names[OBJ_COMMIT], &size, sha1);
+                       commit_type, &size, sha1);
                if (!buf || size < 46)
                        die("Not a valid commit: %s", from);
                free(buf);
@@ -1783,9 +2207,9 @@ static void cmd_new_tag(void)
        read_next_command();
 
        /* tagger ... */
-       if (strncmp("tagger ", command_buf.buf, 7))
+       if (prefixcmp(command_buf.buf, "tagger "))
                die("Expected tagger command, got %s", command_buf.buf);
-       tagger = strdup(command_buf.buf);
+       tagger = parse_ident(command_buf.buf + 7);
 
        /* tag payload/message */
        read_next_command();
@@ -1795,9 +2219,10 @@ static void cmd_new_tag(void)
        size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
        sp = new_data.buffer;
        sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
-       sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
+       sp += sprintf(sp, "type %s\n", commit_type);
        sp += sprintf(sp, "tag %s\n", t->name);
-       sp += sprintf(sp, "%s\n\n", tagger);
+       sp += sprintf(sp, "tagger %s\n", tagger);
+       *sp++ = '\n';
        memcpy(sp, msg, msglen);
        sp += msglen;
        free(tagger);
@@ -1820,7 +2245,9 @@ static void cmd_reset_branch(void)
        sp = strchr(command_buf.buf, ' ') + 1;
        b = lookup_branch(sp);
        if (b) {
-               b->last_commit = 0;
+               hashclr(b->sha1);
+               hashclr(b->branch_tree.versions[0].sha1);
+               hashclr(b->branch_tree.versions[1].sha1);
                if (b->branch_tree.tree) {
                        release_tree_content_recursive(b->branch_tree.tree);
                        b->branch_tree.tree = NULL;
@@ -1829,68 +2256,147 @@ static void cmd_reset_branch(void)
        else
                b = new_branch(sp);
        read_next_command();
-       cmd_from(b);
+       if (!cmd_from(b) && command_buf.len > 1)
+               unread_command_buf = 1;
 }
 
 static void cmd_checkpoint(void)
 {
-       if (object_count)
-               checkpoint();
-       read_next_command();
+       if (object_count) {
+               cycle_packfile();
+               dump_branches();
+               dump_tags();
+               dump_marks();
+       }
+       skip_optional_lf();
+}
+
+static void cmd_progress(void)
+{
+       fwrite(command_buf.buf, 1, command_buf.len - 1, stdout);
+       fputc('\n', stdout);
+       fflush(stdout);
+       skip_optional_lf();
+}
+
+static void import_marks(const char *input_file)
+{
+       char line[512];
+       FILE *f = fopen(input_file, "r");
+       if (!f)
+               die("cannot read %s: %s", input_file, strerror(errno));
+       while (fgets(line, sizeof(line), f)) {
+               uintmax_t mark;
+               char *end;
+               unsigned char sha1[20];
+               struct object_entry *e;
+
+               end = strchr(line, '\n');
+               if (line[0] != ':' || !end)
+                       die("corrupt mark line: %s", line);
+               *end = 0;
+               mark = strtoumax(line + 1, &end, 10);
+               if (!mark || end == line + 1
+                       || *end != ' ' || get_sha1(end + 1, sha1))
+                       die("corrupt mark line: %s", line);
+               e = find_object(sha1);
+               if (!e) {
+                       enum object_type type = sha1_object_info(sha1, NULL);
+                       if (type < 0)
+                               die("object not found: %s", sha1_to_hex(sha1));
+                       e = insert_object(sha1);
+                       e->type = type;
+                       e->pack_id = MAX_PACK_ID;
+                       e->offset = 1; /* just not zero! */
+               }
+               insert_mark(mark, e);
+       }
+       fclose(f);
 }
 
 static const char fast_import_usage[] =
-"git-fast-import [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log]";
+"git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
 
 int main(int argc, const char **argv)
 {
-       int i;
-       uintmax_t total_count, duplicate_count;
+       unsigned int i, show_stats = 1;
 
        git_config(git_default_config);
+       alloc_objects(object_entry_alloc);
+       strbuf_init(&command_buf);
+       atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
+       branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
+       avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
+       marks = pool_calloc(1, sizeof(struct mark_set));
 
        for (i = 1; i < argc; i++) {
                const char *a = argv[i];
 
                if (*a != '-' || !strcmp(a, "--"))
                        break;
-               else if (!strncmp(a, "--max-pack-size=", 16))
+               else if (!prefixcmp(a, "--date-format=")) {
+                       const char *fmt = a + 14;
+                       if (!strcmp(fmt, "raw"))
+                               whenspec = WHENSPEC_RAW;
+                       else if (!strcmp(fmt, "rfc2822"))
+                               whenspec = WHENSPEC_RFC2822;
+                       else if (!strcmp(fmt, "now"))
+                               whenspec = WHENSPEC_NOW;
+                       else
+                               die("unknown --date-format argument %s", fmt);
+               }
+               else if (!prefixcmp(a, "--max-pack-size="))
                        max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
-               else if (!strncmp(a, "--depth=", 8))
+               else if (!prefixcmp(a, "--depth="))
                        max_depth = strtoul(a + 8, NULL, 0);
-               else if (!strncmp(a, "--active-branches=", 18))
+               else if (!prefixcmp(a, "--active-branches="))
                        max_active_branches = strtoul(a + 18, NULL, 0);
-               else if (!strncmp(a, "--export-marks=", 15))
+               else if (!prefixcmp(a, "--import-marks="))
+                       import_marks(a + 15);
+               else if (!prefixcmp(a, "--export-marks="))
                        mark_file = a + 15;
+               else if (!prefixcmp(a, "--export-pack-edges=")) {
+                       if (pack_edges)
+                               fclose(pack_edges);
+                       pack_edges = fopen(a + 20, "a");
+                       if (!pack_edges)
+                               die("Cannot open %s: %s", a + 20, strerror(errno));
+               } else if (!strcmp(a, "--force"))
+                       force_update = 1;
+               else if (!strcmp(a, "--quiet"))
+                       show_stats = 0;
+               else if (!strcmp(a, "--stats"))
+                       show_stats = 1;
                else
                        die("unknown option %s", a);
        }
        if (i != argc)
                usage(fast_import_usage);
 
-       alloc_objects(object_entry_alloc);
-       strbuf_init(&command_buf);
-
-       atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
-       branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
-       avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
-       marks = pool_calloc(1, sizeof(struct mark_set));
+       rc_free = pool_alloc(cmd_save * sizeof(*rc_free));
+       for (i = 0; i < (cmd_save - 1); i++)
+               rc_free[i].next = &rc_free[i + 1];
+       rc_free[cmd_save - 1].next = NULL;
 
+       prepare_packed_git();
        start_packfile();
+       set_die_routine(die_nicely);
        for (;;) {
                read_next_command();
                if (command_buf.eof)
                        break;
                else if (!strcmp("blob", command_buf.buf))
                        cmd_new_blob();
-               else if (!strncmp("commit ", command_buf.buf, 7))
+               else if (!prefixcmp(command_buf.buf, "commit "))
                        cmd_new_commit();
-               else if (!strncmp("tag ", command_buf.buf, 4))
+               else if (!prefixcmp(command_buf.buf, "tag "))
                        cmd_new_tag();
-               else if (!strncmp("reset ", command_buf.buf, 6))
+               else if (!prefixcmp(command_buf.buf, "reset "))
                        cmd_reset_branch();
                else if (!strcmp("checkpoint", command_buf.buf))
                        cmd_checkpoint();
+               else if (!prefixcmp(command_buf.buf, "progress "))
+                       cmd_progress();
                else
                        die("Unsupported command: %s", command_buf.buf);
        }
@@ -1901,31 +2407,35 @@ int main(int argc, const char **argv)
        unkeep_all_packs();
        dump_marks();
 
-       total_count = 0;
-       for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
-               total_count += object_count_by_type[i];
-       duplicate_count = 0;
-       for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
-               duplicate_count += duplicate_count_by_type[i];
-
-       fprintf(stderr, "%s statistics:\n", argv[0]);
-       fprintf(stderr, "---------------------------------------------------------------------\n");
-       fprintf(stderr, "Alloc'd objects: %10ju\n", alloc_count);
-       fprintf(stderr, "Total objects:   %10ju (%10ju duplicates                  )\n", total_count, duplicate_count);
-       fprintf(stderr, "      blobs  :   %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB]);
-       fprintf(stderr, "      trees  :   %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE]);
-       fprintf(stderr, "      commits:   %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT]);
-       fprintf(stderr, "      tags   :   %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG]);
-       fprintf(stderr, "Total branches:  %10lu (%10lu loads     )\n", branch_count, branch_load_count);
-       fprintf(stderr, "      marks:     %10ju (%10ju unique    )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
-       fprintf(stderr, "      atoms:     %10u\n", atom_cnt);
-       fprintf(stderr, "Memory total:    %10ju KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
-       fprintf(stderr, "       pools:    %10lu KiB\n", total_allocd/1024);
-       fprintf(stderr, "     objects:    %10ju KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
-       fprintf(stderr, "---------------------------------------------------------------------\n");
-       pack_report();
-       fprintf(stderr, "---------------------------------------------------------------------\n");
-       fprintf(stderr, "\n");
+       if (pack_edges)
+               fclose(pack_edges);
+
+       if (show_stats) {
+               uintmax_t total_count = 0, duplicate_count = 0;
+               for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
+                       total_count += object_count_by_type[i];
+               for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
+                       duplicate_count += duplicate_count_by_type[i];
+
+               fprintf(stderr, "%s statistics:\n", argv[0]);
+               fprintf(stderr, "---------------------------------------------------------------------\n");
+               fprintf(stderr, "Alloc'd objects: %10" PRIuMAX "\n", alloc_count);
+               fprintf(stderr, "Total objects:   %10" PRIuMAX " (%10" PRIuMAX " duplicates                  )\n", total_count, duplicate_count);
+               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]);
+               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]);
+               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]);
+               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]);
+               fprintf(stderr, "Total branches:  %10lu (%10lu loads     )\n", branch_count, branch_load_count);
+               fprintf(stderr, "      marks:     %10" PRIuMAX " (%10" PRIuMAX " unique    )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
+               fprintf(stderr, "      atoms:     %10u\n", atom_cnt);
+               fprintf(stderr, "Memory total:    %10" PRIuMAX " KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
+               fprintf(stderr, "       pools:    %10lu KiB\n", (unsigned long)(total_allocd/1024));
+               fprintf(stderr, "     objects:    %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
+               fprintf(stderr, "---------------------------------------------------------------------\n");
+               pack_report();
+               fprintf(stderr, "---------------------------------------------------------------------\n");
+               fprintf(stderr, "\n");
+       }
 
-       return 0;
+       return failure ? 1 : 0;
 }