Imported Upstream version 2.15.3
[platform/upstream/git.git] / fast-import.c
index c2a814e..d5e4cf0 100644 (file)
@@ -22,8 +22,8 @@ Format of STDIN stream:
     ('author' (sp name)? sp '<' email '>' sp when lf)?
     'committer' (sp name)? sp '<' email '>' sp when lf
     commit_msg
-    ('from' sp committish lf)?
-    ('merge' sp committish lf)*
+    ('from' sp commit-ish lf)?
+    ('merge' sp commit-ish lf)*
     (file_change | ls)*
     lf?;
   commit_msg ::= data;
@@ -43,18 +43,18 @@ Format of STDIN stream:
   file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
   file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
     data;
-  note_obm ::= 'N' sp (hexsha1 | idnum) sp committish lf;
-  note_inm ::= 'N' sp 'inline' sp committish lf
+  note_obm ::= 'N' sp (hexsha1 | idnum) sp commit-ish lf;
+  note_inm ::= 'N' sp 'inline' sp commit-ish lf
     data;
 
   new_tag ::= 'tag' sp tag_str lf
-    'from' sp committish lf
+    'from' sp commit-ish lf
     ('tagger' (sp name)? sp '<' email '>' sp when lf)?
     tag_msg;
   tag_msg ::= data;
 
   reset_branch ::= 'reset' sp ref_str lf
-    ('from' sp committish lf)?
+    ('from' sp commit-ish lf)?
     lf?;
 
   checkpoint ::= 'checkpoint' lf
@@ -93,7 +93,7 @@ Format of STDIN stream:
      # stream formatting is: \, " and LF.  Otherwise these values
      # are UTF8.
      #
-  committish  ::= (ref_str | hexsha1 | sha1exp_str | idnum);
+  commit-ish  ::= (ref_str | hexsha1 | sha1exp_str | idnum);
   ref_str     ::= ref;
   sha1exp_str ::= sha1exp;
   tag_str     ::= tag;
@@ -134,16 +134,17 @@ Format of STDIN stream:
   ts    ::= # time since the epoch in seconds, ascii base10 notation;
   tz    ::= # GIT style timezone;
 
-     # note: comments, ls and cat requests 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.
+     # note: comments, get-mark, ls-tree, and cat-blob requests 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 line (an lf
      # preceded it).
      #
 
+  get_mark ::= 'get-mark' sp idnum lf;
   cat_blob ::= 'cat-blob' sp (hexsha1 | idnum) lf;
   ls_tree  ::= 'ls' sp (hexsha1 | idnum) sp path_str lf;
 
@@ -153,6 +154,8 @@ Format of STDIN stream:
 
 #include "builtin.h"
 #include "cache.h"
+#include "config.h"
+#include "lockfile.h"
 #include "object.h"
 #include "blob.h"
 #include "tree.h"
@@ -162,8 +165,9 @@ Format of STDIN stream:
 #include "refs.h"
 #include "csum-file.h"
 #include "quote.h"
-#include "exec_cmd.h"
 #include "dir.h"
+#include "run-command.h"
+#include "packfile.h"
 
 #define PACK_ID_BITS 16
 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
@@ -224,7 +228,7 @@ struct tree_entry {
        struct atom_str *name;
        struct tree_entry_ms {
                uint16_t mode;
-               unsigned char sha1[20];
+               struct object_id oid;
        } versions[2];
 };
 
@@ -248,20 +252,21 @@ struct branch {
        uintmax_t last_commit;
        uintmax_t num_notes;
        unsigned active : 1;
+       unsigned delete : 1;
        unsigned pack_id : PACK_ID_BITS;
-       unsigned char sha1[20];
+       struct object_id oid;
 };
 
 struct tag {
        struct tag *next_tag;
        const char *name;
        unsigned int pack_id;
-       unsigned char sha1[20];
+       struct object_id oid;
 };
 
 struct hash_list {
        struct hash_list *next;
-       unsigned char sha1[20];
+       struct object_id oid;
 };
 
 typedef enum {
@@ -277,11 +282,10 @@ struct recent_command {
 };
 
 /* Configured limits on output */
-static unsigned long max_depth = 10;
+static unsigned long max_depth = 50;
 static off_t max_packsize;
+static int unpack_limit = 100;
 static int force_update;
-static int pack_compression_level = Z_DEFAULT_COMPRESSION;
-static int pack_compression_seen;
 
 /* Stats and misc. counters */
 static uintmax_t alloc_count;
@@ -326,6 +330,7 @@ static const char *export_marks_file;
 static const char *import_marks_file;
 static int import_marks_file_from_stream;
 static int import_marks_file_ignore_missing;
+static int import_marks_file_done;
 static int relative_marks_paths;
 
 /* Our last blob */
@@ -370,8 +375,9 @@ static volatile sig_atomic_t checkpoint_requested;
 static int cat_blob_fd = STDOUT_FILENO;
 
 static void parse_argv(void);
-static void parse_cat_blob(void);
-static void parse_ls(struct branch *b);
+static void parse_get_mark(const char *p);
+static void parse_cat_blob(const char *p);
+static void parse_ls(const char *p, struct branch *b);
 
 static void write_branch_report(FILE *rpt, struct branch *b)
 {
@@ -382,13 +388,15 @@ static void write_branch_report(FILE *rpt, struct branch *b)
                fputs(" active", rpt);
        if (b->branch_tree.tree)
                fputs(" loaded", rpt);
-       if (is_null_sha1(b->branch_tree.versions[1].sha1))
+       if (is_null_oid(&b->branch_tree.versions[1].oid))
                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, "  tip commit  : %s\n", oid_to_hex(&b->oid));
+       fprintf(rpt, "  old tree    : %s\n",
+               oid_to_hex(&b->branch_tree.versions[0].oid));
+       fprintf(rpt, "  cur tree    : %s\n",
+               oid_to_hex(&b->branch_tree.versions[1].oid));
        fprintf(rpt, "  commit clock: %" PRIuMAX "\n", b->last_commit);
 
        fputs("  last pack   : ", rpt);
@@ -403,14 +411,15 @@ static void dump_marks_helper(FILE *, uintmax_t, struct mark_set *);
 
 static void write_crash_report(const char *err)
 {
-       char *loc = git_path("fast_import_crash_%"PRIuMAX, (uintmax_t) getpid());
+       char *loc = git_pathdup("fast_import_crash_%"PRIuMAX, (uintmax_t) 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));
+               error_errno("can't write crash report %s", loc);
+               free(loc);
                return;
        }
 
@@ -419,7 +428,7 @@ static void write_crash_report(const char *err)
        fprintf(rpt, "fast-import crash report:\n");
        fprintf(rpt, "    fast-import process: %"PRIuMAX"\n", (uintmax_t) getpid());
        fprintf(rpt, "    parent process     : %"PRIuMAX"\n", (uintmax_t) getppid());
-       fprintf(rpt, "    at %s\n", show_date(time(NULL), 0, DATE_LOCAL));
+       fprintf(rpt, "    at %s\n", show_date(time(NULL), 0, DATE_MODE(ISO8601)));
        fputc('\n', rpt);
 
        fputs("fatal: ", rpt);
@@ -465,7 +474,7 @@ static void write_crash_report(const char *err)
                fputs("Annotated Tags\n", rpt);
                fputs("--------------\n", rpt);
                for (tg = first_tag; tg; tg = tg->next_tag) {
-                       fputs(sha1_to_hex(tg->sha1), rpt);
+                       fputs(oid_to_hex(&tg->oid), rpt);
                        fputc(' ', rpt);
                        fputs(tg->name, rpt);
                        fputc('\n', rpt);
@@ -484,6 +493,7 @@ static void write_crash_report(const char *err)
        fputs("-------------------\n", rpt);
        fputs("END OF CRASH REPORT\n", rpt);
        fclose(rpt);
+       free(loc);
 }
 
 static void end_packfile(void);
@@ -549,7 +559,7 @@ static void alloc_objects(unsigned int cnt)
        alloc_count += cnt;
 }
 
-static struct object_entry *new_object(unsigned char *sha1)
+static struct object_entry *new_object(struct object_id *oid)
 {
        struct object_entry *e;
 
@@ -557,38 +567,65 @@ static struct object_entry *new_object(unsigned char *sha1)
                alloc_objects(object_entry_alloc);
 
        e = blocks->next_free++;
-       hashcpy(e->idx.sha1, sha1);
+       oidcpy(&e->idx.oid, oid);
        return e;
 }
 
-static struct object_entry *find_object(unsigned char *sha1)
+static struct object_entry *find_object(struct object_id *oid)
 {
-       unsigned int h = sha1[0] << 8 | sha1[1];
+       unsigned int h = oid->hash[0] << 8 | oid->hash[1];
        struct object_entry *e;
        for (e = object_table[h]; e; e = e->next)
-               if (!hashcmp(sha1, e->idx.sha1))
+               if (!oidcmp(oid, &e->idx.oid))
                        return e;
        return NULL;
 }
 
-static struct object_entry *insert_object(unsigned char *sha1)
+static struct object_entry *insert_object(struct object_id *oid)
 {
-       unsigned int h = sha1[0] << 8 | sha1[1];
+       unsigned int h = oid->hash[0] << 8 | oid->hash[1];
        struct object_entry *e = object_table[h];
 
        while (e) {
-               if (!hashcmp(sha1, e->idx.sha1))
+               if (!oidcmp(oid, &e->idx.oid))
                        return e;
                e = e->next;
        }
 
-       e = new_object(sha1);
+       e = new_object(oid);
        e->next = object_table[h];
        e->idx.offset = 0;
        object_table[h] = e;
        return e;
 }
 
+static void invalidate_pack_id(unsigned int id)
+{
+       unsigned int h;
+       unsigned long lu;
+       struct tag *t;
+
+       for (h = 0; h < ARRAY_SIZE(object_table); h++) {
+               struct object_entry *e;
+
+               for (e = object_table[h]; e; e = e->next)
+                       if (e->pack_id == id)
+                               e->pack_id = MAX_PACK_ID;
+       }
+
+       for (lu = 0; lu < branch_table_sz; lu++) {
+               struct branch *b;
+
+               for (b = branch_table[lu]; b; b = b->table_next_branch)
+                       if (b->pack_id == id)
+                               b->pack_id = MAX_PACK_ID;
+       }
+
+       for (t = first_tag; t; t = t->next_tag)
+               if (t->pack_id == id)
+                       t->pack_id = MAX_PACK_ID;
+}
+
 static unsigned int hc_str(const char *s, size_t len)
 {
        unsigned int r = 0;
@@ -616,7 +653,7 @@ static void *pool_alloc(size_t len)
                        return xmalloc(len);
                }
                total_allocd += sizeof(struct mem_pool) + mem_pool_alloc;
-               p = xmalloc(sizeof(struct mem_pool) + mem_pool_alloc);
+               p = xmalloc(st_add(sizeof(struct mem_pool), mem_pool_alloc));
                p->next_pool = mem_pool;
                p->next_free = (char *) p->space;
                p->end = p->next_free + mem_pool_alloc;
@@ -638,8 +675,9 @@ static void *pool_calloc(size_t count, size_t size)
 
 static char *pool_strdup(const char *s)
 {
-       char *r = pool_alloc(strlen(s) + 1);
-       strcpy(r, s);
+       size_t len = strlen(s) + 1;
+       char *r = pool_alloc(len);
+       memcpy(r, s, len);
        return r;
 }
 
@@ -696,7 +734,7 @@ static struct atom_str *to_atom(const char *s, unsigned short len)
 
        c = pool_alloc(sizeof(struct atom_str) + len + 1);
        c->str_len = len;
-       strncpy(c->str_dat, s, len);
+       memcpy(c->str_dat, s, len);
        c->str_dat[len] = 0;
        c->next_atom = atom_table[hc];
        atom_table[hc] = c;
@@ -807,7 +845,8 @@ static struct tree_entry *new_tree_entry(void)
        if (!avail_tree_entry) {
                unsigned int n = tree_entry_alloc;
                total_allocd += n * sizeof(struct tree_entry);
-               avail_tree_entry = e = xmalloc(n * sizeof(struct tree_entry));
+               ALLOC_ARRAY(e, n);
+               avail_tree_entry = e;
                while (n-- > 1) {
                        *((void**)e) = e + 1;
                        e++;
@@ -841,7 +880,7 @@ static struct tree_content *dup_tree_content(struct tree_content *s)
                a = s->entries[i];
                b = new_tree_entry();
                memcpy(b, a, sizeof(*a));
-               if (a->tree && is_null_sha1(b->versions[1].sha1))
+               if (a->tree && is_null_oid(&b->versions[1].oid))
                        b->tree = dup_tree_content(a->tree);
                else
                        b->tree = NULL;
@@ -855,15 +894,15 @@ static struct tree_content *dup_tree_content(struct tree_content *s)
 
 static void start_packfile(void)
 {
-       static char tmp_file[PATH_MAX];
+       struct strbuf tmp_file = STRBUF_INIT;
        struct packed_git *p;
        struct pack_header hdr;
        int pack_fd;
 
-       pack_fd = odb_mkstemp(tmp_file, sizeof(tmp_file),
-                             "pack/tmp_pack_XXXXXX");
-       p = xcalloc(1, sizeof(*p) + strlen(tmp_file) + 2);
-       strcpy(p->pack_name, tmp_file);
+       pack_fd = odb_mkstemp(&tmp_file, "pack/tmp_pack_XXXXXX");
+       FLEX_ALLOC_STR(p, pack_name, tmp_file.buf);
+       strbuf_release(&tmp_file);
+
        p->pack_fd = pack_fd;
        p->do_not_close = 1;
        pack_file = sha1fd(pack_fd, p->pack_name);
@@ -877,7 +916,7 @@ static void start_packfile(void)
        pack_size = sizeof(hdr);
        object_count = 0;
 
-       all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1));
+       REALLOC_ARRAY(all_packs, pack_id + 1);
        all_packs[pack_id] = p;
 }
 
@@ -889,7 +928,7 @@ static const char *create_index(void)
        struct object_entry_pool *o;
 
        /* Build the table of object IDs. */
-       idx = xmalloc(object_count * sizeof(*idx));
+       ALLOC_ARRAY(idx, object_count);
        c = idx;
        for (o = blocks; o; o = o->next_pool)
                for (e = o->next_free; e-- != o->entries;)
@@ -906,60 +945,89 @@ static const char *create_index(void)
 
 static char *keep_pack(const char *curr_index_name)
 {
-       static char name[PATH_MAX];
        static const char *keep_msg = "fast-import";
+       struct strbuf name = STRBUF_INIT;
        int keep_fd;
 
-       keep_fd = odb_pack_keep(name, sizeof(name), pack_data->sha1);
+       odb_pack_name(&name, pack_data->sha1, "keep");
+       keep_fd = odb_pack_keep(name.buf);
        if (keep_fd < 0)
                die_errno("cannot create keep file");
        write_or_die(keep_fd, keep_msg, strlen(keep_msg));
        if (close(keep_fd))
                die_errno("failed to write keep file");
 
-       snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
-                get_object_directory(), sha1_to_hex(pack_data->sha1));
-       if (move_temp_to_file(pack_data->pack_name, name))
+       odb_pack_name(&name, pack_data->sha1, "pack");
+       if (finalize_object_file(pack_data->pack_name, name.buf))
                die("cannot store pack file");
 
-       snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
-                get_object_directory(), sha1_to_hex(pack_data->sha1));
-       if (move_temp_to_file(curr_index_name, name))
+       odb_pack_name(&name, pack_data->sha1, "idx");
+       if (finalize_object_file(curr_index_name, name.buf))
                die("cannot store index file");
        free((void *)curr_index_name);
-       return name;
+       return strbuf_detach(&name, NULL);
 }
 
 static void unkeep_all_packs(void)
 {
-       static char name[PATH_MAX];
+       struct strbuf name = STRBUF_INIT;
        int k;
 
        for (k = 0; k < pack_id; k++) {
                struct packed_git *p = all_packs[k];
-               snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
-                        get_object_directory(), sha1_to_hex(p->sha1));
-               unlink_or_warn(name);
+               odb_pack_name(&name, p->sha1, "keep");
+               unlink_or_warn(name.buf);
        }
+       strbuf_release(&name);
+}
+
+static int loosen_small_pack(const struct packed_git *p)
+{
+       struct child_process unpack = CHILD_PROCESS_INIT;
+
+       if (lseek(p->pack_fd, 0, SEEK_SET) < 0)
+               die_errno("Failed seeking to start of '%s'", p->pack_name);
+
+       unpack.in = p->pack_fd;
+       unpack.git_cmd = 1;
+       unpack.stdout_to_stderr = 1;
+       argv_array_push(&unpack.args, "unpack-objects");
+       if (!show_stats)
+               argv_array_push(&unpack.args, "-q");
+
+       return run_command(&unpack);
 }
 
 static void end_packfile(void)
 {
-       struct packed_git *old_p = pack_data, *new_p;
+       static int running;
+
+       if (running || !pack_data)
+               return;
 
+       running = 1;
        clear_delta_base_cache();
        if (object_count) {
-               unsigned char cur_pack_sha1[20];
+               struct packed_git *new_p;
+               struct object_id cur_pack_oid;
                char *idx_name;
                int i;
                struct branch *b;
                struct tag *t;
 
                close_pack_windows(pack_data);
-               sha1close(pack_file, cur_pack_sha1, 0);
+               sha1close(pack_file, cur_pack_oid.hash, 0);
                fixup_pack_header_footer(pack_data->pack_fd, pack_data->sha1,
                                    pack_data->pack_name, object_count,
-                                   cur_pack_sha1, pack_size);
+                                   cur_pack_oid.hash, pack_size);
+
+               if (object_count <= unpack_limit) {
+                       if (!loosen_small_pack(pack_data)) {
+                               invalidate_pack_id(pack_id);
+                               goto discard_pack;
+                       }
+               }
+
                close(pack_data->pack_fd);
                idx_name = keep_pack(create_index());
 
@@ -969,6 +1037,7 @@ static void end_packfile(void)
                        die("core git rejected index %s", idx_name);
                all_packs[pack_id] = new_p;
                install_packed_git(new_p);
+               free(idx_name);
 
                /* Print the boundary */
                if (pack_edges) {
@@ -976,12 +1045,14 @@ static void end_packfile(void)
                        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));
+                                               fprintf(pack_edges, " %s",
+                                                       oid_to_hex(&b->oid));
                                }
                        }
                        for (t = first_tag; t; t = t->next_tag) {
                                if (t->pack_id == pack_id)
-                                       fprintf(pack_edges, " %s", sha1_to_hex(t->sha1));
+                                       fprintf(pack_edges, " %s",
+                                               oid_to_hex(&t->oid));
                        }
                        fputc('\n', pack_edges);
                        fflush(pack_edges);
@@ -990,10 +1061,12 @@ static void end_packfile(void)
                pack_id++;
        }
        else {
-               close(old_p->pack_fd);
-               unlink_or_warn(old_p->pack_name);
+discard_pack:
+               close(pack_data->pack_fd);
+               unlink_or_warn(pack_data->pack_name);
        }
-       free(old_p);
+       FREE_AND_NULL(pack_data);
+       running = 0;
 
        /* We can't carry a delta across packfiles. */
        strbuf_release(&last_blob.data);
@@ -1011,33 +1084,33 @@ static int store_object(
        enum object_type type,
        struct strbuf *dat,
        struct last_object *last,
-       unsigned char *sha1out,
+       struct object_id *oidout,
        uintmax_t mark)
 {
        void *out, *delta;
        struct object_entry *e;
        unsigned char hdr[96];
-       unsigned char sha1[20];
+       struct object_id oid;
        unsigned long hdrlen, deltalen;
        git_SHA_CTX c;
        git_zstream s;
 
-       hdrlen = sprintf((char *)hdr,"%s %lu", typename(type),
-               (unsigned long)dat->len) + 1;
+       hdrlen = xsnprintf((char *)hdr, sizeof(hdr), "%s %lu",
+                          typename(type), (unsigned long)dat->len) + 1;
        git_SHA1_Init(&c);
        git_SHA1_Update(&c, hdr, hdrlen);
        git_SHA1_Update(&c, dat->buf, dat->len);
-       git_SHA1_Final(sha1, &c);
-       if (sha1out)
-               hashcpy(sha1out, sha1);
+       git_SHA1_Final(oid.hash, &c);
+       if (oidout)
+               oidcpy(oidout, &oid);
 
-       e = insert_object(sha1);
+       e = insert_object(&oid);
        if (mark)
                insert_mark(mark, e);
        if (e->idx.offset) {
                duplicate_count_by_type[type]++;
                return 1;
-       } else if (find_sha1_pack(sha1, packed_git)) {
+       } else if (find_sha1_pack(oid.hash, packed_git)) {
                e->type = type;
                e->pack_id = MAX_PACK_ID;
                e->idx.offset = 1; /* just not zero! */
@@ -1053,7 +1126,6 @@ static int store_object(
        } else
                delta = NULL;
 
-       memset(&s, 0, sizeof(s));
        git_deflate_init(&s, pack_compression_level);
        if (delta) {
                s.next_in = delta;
@@ -1078,10 +1150,8 @@ static int store_object(
 
                /* We cannot carry a delta into the new pack. */
                if (delta) {
-                       free(delta);
-                       delta = NULL;
+                       FREE_AND_NULL(delta);
 
-                       memset(&s, 0, sizeof(s));
                        git_deflate_init(&s, pack_compression_level);
                        s.next_in = (void *)dat->buf;
                        s.avail_in = dat->len;
@@ -1108,7 +1178,8 @@ static int store_object(
                delta_count_by_type[type]++;
                e->depth = last->depth + 1;
 
-               hdrlen = encode_in_pack_object_header(OBJ_OFS_DELTA, deltalen, hdr);
+               hdrlen = encode_in_pack_object_header(hdr, sizeof(hdr),
+                                                     OBJ_OFS_DELTA, deltalen);
                sha1write(pack_file, hdr, hdrlen);
                pack_size += hdrlen;
 
@@ -1119,7 +1190,8 @@ static int store_object(
                pack_size += sizeof(hdr) - pos;
        } else {
                e->depth = 0;
-               hdrlen = encode_in_pack_object_header(type, dat->len, hdr);
+               hdrlen = encode_in_pack_object_header(hdr, sizeof(hdr),
+                                                     type, dat->len);
                sha1write(pack_file, hdr, hdrlen);
                pack_size += hdrlen;
        }
@@ -1150,13 +1222,13 @@ static void truncate_pack(struct sha1file_checkpoint *checkpoint)
        pack_size = checkpoint->offset;
 }
 
-static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
+static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark)
 {
        size_t in_sz = 64 * 1024, out_sz = 64 * 1024;
        unsigned char *in_buf = xmalloc(in_sz);
        unsigned char *out_buf = xmalloc(out_sz);
        struct object_entry *e;
-       unsigned char sha1[20];
+       struct object_id oid;
        unsigned long hdrlen;
        off_t offset;
        git_SHA_CTX c;
@@ -1172,21 +1244,16 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
        sha1file_checkpoint(pack_file, &checkpoint);
        offset = checkpoint.offset;
 
-       hdrlen = snprintf((char *)out_buf, out_sz, "blob %" PRIuMAX, len) + 1;
-       if (out_sz <= hdrlen)
-               die("impossibly large object header");
+       hdrlen = xsnprintf((char *)out_buf, out_sz, "blob %" PRIuMAX, len) + 1;
 
        git_SHA1_Init(&c);
        git_SHA1_Update(&c, out_buf, hdrlen);
 
        crc32_begin(pack_file);
 
-       memset(&s, 0, sizeof(s));
        git_deflate_init(&s, pack_compression_level);
 
-       hdrlen = encode_in_pack_object_header(OBJ_BLOB, len, out_buf);
-       if (out_sz <= hdrlen)
-               die("impossibly large object header");
+       hdrlen = encode_in_pack_object_header(out_buf, out_sz, OBJ_BLOB, len);
 
        s.next_out = out_buf + hdrlen;
        s.avail_out = out_sz - hdrlen;
@@ -1224,12 +1291,12 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
                }
        }
        git_deflate_end(&s);
-       git_SHA1_Final(sha1, &c);
+       git_SHA1_Final(oid.hash, &c);
 
-       if (sha1out)
-               hashcpy(sha1out, sha1);
+       if (oidout)
+               oidcpy(oidout, &oid);
 
-       e = insert_object(sha1);
+       e = insert_object(&oid);
 
        if (mark)
                insert_mark(mark, e);
@@ -1238,7 +1305,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
                duplicate_count_by_type[OBJ_BLOB]++;
                truncate_pack(&checkpoint);
 
-       } else if (find_sha1_pack(sha1, packed_git)) {
+       } else if (find_sha1_pack(oid.hash, packed_git)) {
                e->type = OBJ_BLOB;
                e->pack_id = MAX_PACK_ID;
                e->idx.offset = 1; /* just not zero! */
@@ -1322,7 +1389,7 @@ static const char *get_mode(const char *str, uint16_t *modep)
 
 static void load_tree(struct tree_entry *root)
 {
-       unsigned char *sha1 = root->versions[1].sha1;
+       struct object_id *oid = &root->versions[1].oid;
        struct object_entry *myoe;
        struct tree_content *t;
        unsigned long size;
@@ -1330,22 +1397,22 @@ static void load_tree(struct tree_entry *root)
        const char *c;
 
        root->tree = t = new_tree_content(8);
-       if (is_null_sha1(sha1))
+       if (is_null_oid(oid))
                return;
 
-       myoe = find_object(sha1);
+       myoe = find_object(oid);
        if (myoe && myoe->pack_id != MAX_PACK_ID) {
                if (myoe->type != OBJ_TREE)
-                       die("Not a tree: %s", sha1_to_hex(sha1));
+                       die("Not a tree: %s", oid_to_hex(oid));
                t->delta_depth = myoe->depth;
                buf = gfi_unpack_entry(myoe, &size);
                if (!buf)
-                       die("Can't load tree %s", sha1_to_hex(sha1));
+                       die("Can't load tree %s", oid_to_hex(oid));
        } else {
                enum object_type type;
-               buf = read_sha1_file(sha1, &type, &size);
+               buf = read_sha1_file(oid->hash, &type, &size);
                if (!buf || type != OBJ_TREE)
-                       die("Can't load tree %s", sha1_to_hex(sha1));
+                       die("Can't load tree %s", oid_to_hex(oid));
        }
 
        c = buf;
@@ -1359,13 +1426,13 @@ static void load_tree(struct tree_entry *root)
                e->tree = NULL;
                c = get_mode(c, &e->versions[1].mode);
                if (!c)
-                       die("Corrupt mode in %s", sha1_to_hex(sha1));
+                       die("Corrupt mode in %s", oid_to_hex(oid));
                e->versions[0].mode = e->versions[1].mode;
                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);
-               c += 20;
+               hashcpy(e->versions[0].oid.hash, (unsigned char *)c);
+               hashcpy(e->versions[1].oid.hash, (unsigned char *)c);
+               c += GIT_SHA1_RAWSZ;
        }
        free(buf);
 }
@@ -1394,9 +1461,9 @@ static void mktree(struct tree_content *t, int v, struct strbuf *b)
        unsigned int i;
 
        if (!v)
-               qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
+               QSORT(t->entries, t->entry_count, tecmp0);
        else
-               qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
+               QSORT(t->entries, t->entry_count, tecmp1);
 
        for (i = 0; i < t->entry_count; i++) {
                if (t->entries[i]->versions[v].mode)
@@ -1412,27 +1479,31 @@ static void mktree(struct tree_content *t, int v, struct strbuf *b)
                strbuf_addf(b, "%o %s%c",
                        (unsigned int)(e->versions[v].mode & ~NO_DELTA),
                        e->name->str_dat, '\0');
-               strbuf_add(b, e->versions[v].sha1, 20);
+               strbuf_add(b, e->versions[v].oid.hash, GIT_SHA1_RAWSZ);
        }
 }
 
 static void store_tree(struct tree_entry *root)
 {
-       struct tree_content *t = root->tree;
+       struct tree_content *t;
        unsigned int i, j, del;
        struct last_object lo = { STRBUF_INIT, 0, 0, /* no_swap */ 1 };
        struct object_entry *le = NULL;
 
-       if (!is_null_sha1(root->versions[1].sha1))
+       if (!is_null_oid(&root->versions[1].oid))
                return;
 
+       if (!root->tree)
+               load_tree(root);
+       t = root->tree;
+
        for (i = 0; i < t->entry_count; i++) {
                if (t->entries[i]->tree)
                        store_tree(t->entries[i]);
        }
 
        if (!(root->versions[0].mode & NO_DELTA))
-               le = find_object(root->versions[0].sha1);
+               le = find_object(&root->versions[0].oid);
        if (S_ISDIR(root->versions[0].mode) && le && le->pack_id == pack_id) {
                mktree(t, 0, &old_tree);
                lo.data = old_tree;
@@ -1441,14 +1512,14 @@ static void store_tree(struct tree_entry *root)
        }
 
        mktree(t, 1, &new_tree);
-       store_object(OBJ_TREE, &new_tree, &lo, root->versions[1].sha1, 0);
+       store_object(OBJ_TREE, &new_tree, &lo, &root->versions[1].oid, 0);
 
        t->delta_depth = lo.depth;
        for (i = 0, j = 0, del = 0; i < t->entry_count; i++) {
                struct tree_entry *e = t->entries[i];
                if (e->versions[1].mode) {
                        e->versions[0].mode = e->versions[1].mode;
-                       hashcpy(e->versions[0].sha1, e->versions[1].sha1);
+                       oidcpy(&e->versions[0].oid, &e->versions[1].oid);
                        t->entries[j++] = e;
                } else {
                        release_tree_entry(e);
@@ -1460,14 +1531,14 @@ static void store_tree(struct tree_entry *root)
 
 static void tree_content_replace(
        struct tree_entry *root,
-       const unsigned char *sha1,
+       const struct object_id *oid,
        const uint16_t mode,
        struct tree_content *newtree)
 {
        if (!S_ISDIR(mode))
                die("Root cannot be a non-directory");
-       hashclr(root->versions[0].sha1);
-       hashcpy(root->versions[1].sha1, sha1);
+       oidclr(&root->versions[0].oid);
+       oidcpy(&root->versions[1].oid, oid);
        if (root->tree)
                release_tree_content_recursive(root->tree);
        root->tree = newtree;
@@ -1476,7 +1547,7 @@ static void tree_content_replace(
 static int tree_content_set(
        struct tree_entry *root,
        const char *p,
-       const unsigned char *sha1,
+       const struct object_id *oid,
        const uint16_t mode,
        struct tree_content *subtree)
 {
@@ -1485,14 +1556,11 @@ static int tree_content_set(
        unsigned int i, n;
        struct tree_entry *e;
 
-       slash1 = strchr(p, '/');
-       if (slash1)
-               n = slash1 - p;
-       else
-               n = strlen(p);
+       slash1 = strchrnul(p, '/');
+       n = slash1 - p;
        if (!n)
                die("Empty path component found in input");
-       if (!slash1 && !S_ISDIR(mode) && subtree)
+       if (!*slash1 && !S_ISDIR(mode) && subtree)
                die("Non-directories cannot have subtrees");
 
        if (!root->tree)
@@ -1500,14 +1568,14 @@ static int tree_content_set(
        t = root->tree;
        for (i = 0; i < t->entry_count; i++) {
                e = t->entries[i];
-               if (e->name->str_len == n && !strncmp_icase(p, e->name->str_dat, n)) {
-                       if (!slash1) {
+               if (e->name->str_len == n && !fspathncmp(p, e->name->str_dat, n)) {
+                       if (!*slash1) {
                                if (!S_ISDIR(mode)
                                                && e->versions[1].mode == mode
-                                               && !hashcmp(e->versions[1].sha1, sha1))
+                                               && !oidcmp(&e->versions[1].oid, oid))
                                        return 0;
                                e->versions[1].mode = mode;
-                               hashcpy(e->versions[1].sha1, sha1);
+                               oidcpy(&e->versions[1].oid, oid);
                                if (e->tree)
                                        release_tree_content_recursive(e->tree);
                                e->tree = subtree;
@@ -1528,7 +1596,7 @@ static int tree_content_set(
                                if (S_ISDIR(e->versions[0].mode))
                                        e->versions[0].mode |= NO_DELTA;
 
-                               hashclr(root->versions[1].sha1);
+                               oidclr(&root->versions[1].oid);
                                return 1;
                        }
                        if (!S_ISDIR(e->versions[1].mode)) {
@@ -1537,8 +1605,8 @@ static int tree_content_set(
                        }
                        if (!e->tree)
                                load_tree(e);
-                       if (tree_content_set(e, slash1 + 1, sha1, mode, subtree)) {
-                               hashclr(root->versions[1].sha1);
+                       if (tree_content_set(e, slash1 + 1, oid, mode, subtree)) {
+                               oidclr(&root->versions[1].oid);
                                return 1;
                        }
                        return 0;
@@ -1550,44 +1618,48 @@ static int tree_content_set(
        e = new_tree_entry();
        e->name = to_atom(p, n);
        e->versions[0].mode = 0;
-       hashclr(e->versions[0].sha1);
+       oidclr(&e->versions[0].oid);
        t->entries[t->entry_count++] = e;
-       if (slash1) {
+       if (*slash1) {
                e->tree = new_tree_content(8);
                e->versions[1].mode = S_IFDIR;
-               tree_content_set(e, slash1 + 1, sha1, mode, subtree);
+               tree_content_set(e, slash1 + 1, oid, mode, subtree);
        } else {
                e->tree = subtree;
                e->versions[1].mode = mode;
-               hashcpy(e->versions[1].sha1, sha1);
+               oidcpy(&e->versions[1].oid, oid);
        }
-       hashclr(root->versions[1].sha1);
+       oidclr(&root->versions[1].oid);
        return 1;
 }
 
 static int tree_content_remove(
        struct tree_entry *root,
        const char *p,
-       struct tree_entry *backup_leaf)
+       struct tree_entry *backup_leaf,
+       int allow_root)
 {
        struct tree_content *t;
        const char *slash1;
        unsigned int i, n;
        struct tree_entry *e;
 
-       slash1 = strchr(p, '/');
-       if (slash1)
-               n = slash1 - p;
-       else
-               n = strlen(p);
+       slash1 = strchrnul(p, '/');
+       n = slash1 - p;
 
        if (!root->tree)
                load_tree(root);
+
+       if (!*p && allow_root) {
+               e = root;
+               goto del_entry;
+       }
+
        t = root->tree;
        for (i = 0; i < t->entry_count; i++) {
                e = t->entries[i];
-               if (e->name->str_len == n && !strncmp_icase(p, e->name->str_dat, n)) {
-                       if (slash1 && !S_ISDIR(e->versions[1].mode))
+               if (e->name->str_len == n && !fspathncmp(p, e->name->str_dat, n)) {
+                       if (*slash1 && !S_ISDIR(e->versions[1].mode))
                                /*
                                 * If p names a file in some subdirectory, and a
                                 * file or symlink matching the name of the
@@ -1595,14 +1667,14 @@ static int tree_content_remove(
                                 * exist and need not be deleted.
                                 */
                                return 1;
-                       if (!slash1 || !S_ISDIR(e->versions[1].mode))
+                       if (!*slash1 || !S_ISDIR(e->versions[1].mode))
                                goto del_entry;
                        if (!e->tree)
                                load_tree(e);
-                       if (tree_content_remove(e, slash1 + 1, backup_leaf)) {
+                       if (tree_content_remove(e, slash1 + 1, backup_leaf, 0)) {
                                for (n = 0; n < e->tree->entry_count; n++) {
                                        if (e->tree->entries[n]->versions[1].mode) {
-                                               hashclr(root->versions[1].sha1);
+                                               oidclr(&root->versions[1].oid);
                                                return 1;
                                        }
                                }
@@ -1621,86 +1693,101 @@ del_entry:
                release_tree_content_recursive(e->tree);
        e->tree = NULL;
        e->versions[1].mode = 0;
-       hashclr(e->versions[1].sha1);
-       hashclr(root->versions[1].sha1);
+       oidclr(&e->versions[1].oid);
+       oidclr(&root->versions[1].oid);
        return 1;
 }
 
 static int tree_content_get(
        struct tree_entry *root,
        const char *p,
-       struct tree_entry *leaf)
+       struct tree_entry *leaf,
+       int allow_root)
 {
        struct tree_content *t;
        const char *slash1;
        unsigned int i, n;
        struct tree_entry *e;
 
-       slash1 = strchr(p, '/');
-       if (slash1)
-               n = slash1 - p;
-       else
-               n = strlen(p);
-       if (!n)
+       slash1 = strchrnul(p, '/');
+       n = slash1 - p;
+       if (!n && !allow_root)
                die("Empty path component found in input");
 
        if (!root->tree)
                load_tree(root);
+
+       if (!n) {
+               e = root;
+               goto found_entry;
+       }
+
        t = root->tree;
        for (i = 0; i < t->entry_count; i++) {
                e = t->entries[i];
-               if (e->name->str_len == n && !strncmp_icase(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 (e->name->str_len == n && !fspathncmp(p, e->name->str_dat, n)) {
+                       if (!*slash1)
+                               goto found_entry;
                        if (!S_ISDIR(e->versions[1].mode))
                                return 0;
                        if (!e->tree)
                                load_tree(e);
-                       return tree_content_get(e, slash1 + 1, leaf);
+                       return tree_content_get(e, slash1 + 1, leaf, 0);
                }
        }
        return 0;
+
+found_entry:
+       memcpy(leaf, e, sizeof(*leaf));
+       if (e->tree && is_null_oid(&e->versions[1].oid))
+               leaf->tree = dup_tree_content(e->tree);
+       else
+               leaf->tree = NULL;
+       return 1;
 }
 
 static int update_branch(struct branch *b)
 {
        static const char *msg = "fast-import";
-       struct ref_lock *lock;
-       unsigned char old_sha1[20];
+       struct ref_transaction *transaction;
+       struct object_id old_oid;
+       struct strbuf err = STRBUF_INIT;
 
-       if (is_null_sha1(b->sha1))
+       if (is_null_oid(&b->oid)) {
+               if (b->delete)
+                       delete_ref(NULL, b->name, NULL, 0);
                return 0;
-       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)) {
+       }
+       if (read_ref(b->name, old_oid.hash))
+               oidclr(&old_oid);
+       if (!force_update && !is_null_oid(&old_oid)) {
                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);
+               old_cmit = lookup_commit_reference_gently(&old_oid, 0);
+               new_cmit = lookup_commit_reference_gently(&b->oid, 0);
+               if (!old_cmit || !new_cmit)
                        return error("Branch %s is missing commits.", b->name);
-               }
 
                if (!in_merge_bases(old_cmit, new_cmit)) {
-                       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));
+                               b->name, oid_to_hex(&b->oid),
+                               oid_to_hex(&old_oid));
                        return -1;
                }
        }
-       if (write_ref_sha1(lock, b->sha1, msg) < 0)
-               return error("Unable to update %s", b->name);
+       transaction = ref_transaction_begin(&err);
+       if (!transaction ||
+           ref_transaction_update(transaction, b->name, b->oid.hash, old_oid.hash,
+                                  0, msg, &err) ||
+           ref_transaction_commit(transaction, &err)) {
+               ref_transaction_free(transaction);
+               error("%s", err.buf);
+               strbuf_release(&err);
+               return -1;
+       }
+       ref_transaction_free(transaction);
+       strbuf_release(&err);
        return 0;
 }
 
@@ -1719,15 +1806,32 @@ static void dump_tags(void)
 {
        static const char *msg = "fast-import";
        struct tag *t;
-       struct ref_lock *lock;
-       char ref_name[PATH_MAX];
+       struct strbuf ref_name = STRBUF_INIT;
+       struct strbuf err = STRBUF_INIT;
+       struct ref_transaction *transaction;
 
+       transaction = ref_transaction_begin(&err);
+       if (!transaction) {
+               failure |= error("%s", err.buf);
+               goto cleanup;
+       }
        for (t = first_tag; t; t = t->next_tag) {
-               sprintf(ref_name, "tags/%s", t->name);
-               lock = lock_ref_sha1(ref_name, NULL);
-               if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
-                       failure |= error("Unable to update %s", ref_name);
+               strbuf_reset(&ref_name);
+               strbuf_addf(&ref_name, "refs/tags/%s", t->name);
+
+               if (ref_transaction_update(transaction, ref_name.buf,
+                                          t->oid.hash, NULL, 0, msg, &err)) {
+                       failure |= error("%s", err.buf);
+                       goto cleanup;
+               }
        }
+       if (ref_transaction_commit(transaction, &err))
+               failure |= error("%s", err.buf);
+
+ cleanup:
+       ref_transaction_free(transaction);
+       strbuf_release(&ref_name);
+       strbuf_release(&err);
 }
 
 static void dump_marks_helper(FILE *f,
@@ -1745,7 +1849,7 @@ static void dump_marks_helper(FILE *f,
                for (k = 0; k < 1024; k++) {
                        if (m->data.marked[k])
                                fprintf(f, ":%" PRIuMAX " %s\n", base + k,
-                                       sha1_to_hex(m->data.marked[k]->idx.sha1));
+                                       oid_to_hex(&m->data.marked[k]->idx.oid));
                }
        }
 }
@@ -1753,20 +1857,18 @@ static void dump_marks_helper(FILE *f,
 static void dump_marks(void)
 {
        static struct lock_file mark_lock;
-       int mark_fd;
        FILE *f;
 
-       if (!export_marks_file)
+       if (!export_marks_file || (import_marks_file && !import_marks_file_done))
                return;
 
-       mark_fd = hold_lock_file_for_update(&mark_lock, export_marks_file, 0);
-       if (mark_fd < 0) {
-               failure |= error("Unable to write marks file %s: %s",
-                       export_marks_file, strerror(errno));
+       if (hold_lock_file_for_update(&mark_lock, export_marks_file, 0) < 0) {
+               failure |= error_errno("Unable to write marks file %s",
+                                      export_marks_file);
                return;
        }
 
-       f = fdopen(mark_fd, "w");
+       f = fdopen_lock_file(&mark_lock, "w");
        if (!f) {
                int saved_errno = errno;
                rollback_lock_file(&mark_lock);
@@ -1775,27 +1877,10 @@ static void dump_marks(void)
                return;
        }
 
-       /*
-        * Since the lock file was fdopen()'ed, it should not be close()'ed.
-        * Assign -1 to the lock file descriptor so that commit_lock_file()
-        * won't try to close() it.
-        */
-       mark_lock.fd = -1;
-
        dump_marks_helper(f, 0, marks);
-       if (ferror(f) || fclose(f)) {
-               int saved_errno = errno;
-               rollback_lock_file(&mark_lock);
-               failure |= error("Unable to write marks file %s: %s",
-                       export_marks_file, strerror(saved_errno));
-               return;
-       }
-
        if (commit_lock_file(&mark_lock)) {
-               int saved_errno = errno;
-               rollback_lock_file(&mark_lock);
-               failure |= error("Unable to commit marks file %s: %s",
-                       export_marks_file, strerror(saved_errno));
+               failure |= error_errno("Unable to write file %s",
+                                      export_marks_file);
                return;
        }
 }
@@ -1807,13 +1892,13 @@ static void read_marks(void)
        if (f)
                ;
        else if (import_marks_file_ignore_missing && errno == ENOENT)
-               return; /* Marks file does not exist */
+               goto done; /* Marks file does not exist */
        else
                die_errno("cannot read '%s'", import_marks_file);
        while (fgets(line, sizeof(line), f)) {
                uintmax_t mark;
                char *end;
-               unsigned char sha1[20];
+               struct object_id oid;
                struct object_entry *e;
 
                end = strchr(line, '\n');
@@ -1822,14 +1907,14 @@ static void read_marks(void)
                *end = 0;
                mark = strtoumax(line + 1, &end, 10);
                if (!mark || end == line + 1
-                       || *end != ' ' || get_sha1(end + 1, sha1))
+                       || *end != ' ' || get_oid_hex(end + 1, &oid))
                        die("corrupt mark line: %s", line);
-               e = find_object(sha1);
+               e = find_object(&oid);
                if (!e) {
-                       enum object_type type = sha1_object_info(sha1, NULL);
+                       enum object_type type = sha1_object_info(oid.hash, NULL);
                        if (type < 0)
-                               die("object not found: %s", sha1_to_hex(sha1));
-                       e = insert_object(sha1);
+                               die("object not found: %s", oid_to_hex(&oid));
+                       e = insert_object(&oid);
                        e->type = type;
                        e->pack_id = MAX_PACK_ID;
                        e->idx.offset = 1; /* just not zero! */
@@ -1837,6 +1922,8 @@ static void read_marks(void)
                insert_mark(mark, e);
        }
        fclose(f);
+done:
+       import_marks_file_done = 1;
 }
 
 
@@ -1850,19 +1937,21 @@ static int read_next_command(void)
        }
 
        for (;;) {
+               const char *p;
+
                if (unread_command_buf) {
                        unread_command_buf = 0;
                } else {
                        struct recent_command *rc;
 
                        strbuf_detach(&command_buf, NULL);
-                       stdin_eof = strbuf_getline(&command_buf, stdin, '\n');
+                       stdin_eof = strbuf_getline_lf(&command_buf, stdin);
                        if (stdin_eof)
                                return EOF;
 
                        if (!seen_data_command
-                               && prefixcmp(command_buf.buf, "feature ")
-                               && prefixcmp(command_buf.buf, "option ")) {
+                               && !starts_with(command_buf.buf, "feature ")
+                               && !starts_with(command_buf.buf, "option ")) {
                                parse_argv();
                        }
 
@@ -1882,8 +1971,12 @@ static int read_next_command(void)
                        rc->prev->next = rc;
                        cmd_tail = rc;
                }
-               if (!prefixcmp(command_buf.buf, "cat-blob ")) {
-                       parse_cat_blob();
+               if (skip_prefix(command_buf.buf, "get-mark ", &p)) {
+                       parse_get_mark(p);
+                       continue;
+               }
+               if (skip_prefix(command_buf.buf, "cat-blob ", &p)) {
+                       parse_cat_blob(p);
                        continue;
                }
                if (command_buf.buf[0] == '#')
@@ -1901,8 +1994,9 @@ static void skip_optional_lf(void)
 
 static void parse_mark(void)
 {
-       if (!prefixcmp(command_buf.buf, "mark :")) {
-               next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
+       const char *v;
+       if (skip_prefix(command_buf.buf, "mark :", &v)) {
+               next_mark = strtoumax(v, NULL, 10);
                read_next_command();
        }
        else
@@ -1911,18 +2005,19 @@ static void parse_mark(void)
 
 static int parse_data(struct strbuf *sb, uintmax_t limit, uintmax_t *len_res)
 {
+       const char *data;
        strbuf_reset(sb);
 
-       if (prefixcmp(command_buf.buf, "data "))
+       if (!skip_prefix(command_buf.buf, "data ", &data))
                die("Expected 'data n' command, found: %s", command_buf.buf);
 
-       if (!prefixcmp(command_buf.buf + 5, "<<")) {
-               char *term = xstrdup(command_buf.buf + 5 + 2);
-               size_t term_len = command_buf.len - 5 - 2;
+       if (skip_prefix(data, "<<", &data)) {
+               char *term = xstrdup(data);
+               size_t term_len = command_buf.len - (data - command_buf.buf);
 
                strbuf_detach(&command_buf, NULL);
                for (;;) {
-                       if (strbuf_getline(&command_buf, stdin, '\n') == EOF)
+                       if (strbuf_getline_lf(&command_buf, stdin) == EOF)
                                die("EOF in data (terminator '%s' not found)", term);
                        if (term_len == command_buf.len
                                && !strcmp(term, command_buf.buf))
@@ -1933,7 +2028,7 @@ static int parse_data(struct strbuf *sb, uintmax_t limit, uintmax_t *len_res)
                free(term);
        }
        else {
-               uintmax_t len = strtoumax(command_buf.buf + 5, NULL, 10);
+               uintmax_t len = strtoumax(data, NULL, 10);
                size_t n = 0, length = (size_t)len;
 
                if (limit && limit < len) {
@@ -1956,7 +2051,7 @@ static int parse_data(struct strbuf *sb, uintmax_t limit, uintmax_t *len_res)
        return 1;
 }
 
-static int validate_raw_date(const char *src, char *result, int maxlen)
+static int validate_raw_date(const char *src, struct strbuf *result)
 {
        const char *orig_src = src;
        char *endp;
@@ -1974,11 +2069,10 @@ static int validate_raw_date(const char *src, char *result, int maxlen)
                return -1;
 
        num = strtoul(src + 1, &endp, 10);
-       if (errno || endp == src + 1 || *endp || (endp - orig_src) >= maxlen ||
-           1400 < num)
+       if (errno || endp == src + 1 || *endp || 1400 < num)
                return -1;
 
-       strcpy(result, orig_src);
+       strbuf_addstr(result, orig_src);
        return 0;
 }
 
@@ -1986,7 +2080,7 @@ static char *parse_ident(const char *buf)
 {
        const char *ltgt;
        size_t name_len;
-       char *ident;
+       struct strbuf ident = STRBUF_INIT;
 
        /* ensure there is a space delimiter even if there is no name */
        if (*buf == '<')
@@ -2005,45 +2099,44 @@ static char *parse_ident(const char *buf)
                die("Missing space after > in ident string: %s", buf);
        ltgt++;
        name_len = ltgt - buf;
-       ident = xmalloc(name_len + 24);
-       strncpy(ident, buf, name_len);
+       strbuf_add(&ident, buf, name_len);
 
        switch (whenspec) {
        case WHENSPEC_RAW:
-               if (validate_raw_date(ltgt, ident + name_len, 24) < 0)
+               if (validate_raw_date(ltgt, &ident) < 0)
                        die("Invalid raw date \"%s\" in ident: %s", ltgt, buf);
                break;
        case WHENSPEC_RFC2822:
-               if (parse_date(ltgt, ident + name_len, 24) < 0)
+               if (parse_date(ltgt, &ident) < 0)
                        die("Invalid rfc2822 date \"%s\" in ident: %s", ltgt, buf);
                break;
        case WHENSPEC_NOW:
                if (strcmp("now", ltgt))
                        die("Date in ident must be 'now': %s", buf);
-               datestamp(ident + name_len, 24);
+               datestamp(&ident);
                break;
        }
 
-       return ident;
+       return strbuf_detach(&ident, NULL);
 }
 
 static void parse_and_store_blob(
        struct last_object *last,
-       unsigned char *sha1out,
+       struct object_id *oidout,
        uintmax_t mark)
 {
        static struct strbuf buf = STRBUF_INIT;
        uintmax_t len;
 
        if (parse_data(&buf, big_file_threshold, &len))
-               store_object(OBJ_BLOB, &buf, last, sha1out, mark);
+               store_object(OBJ_BLOB, &buf, last, oidout, mark);
        else {
                if (last) {
                        strbuf_release(&last->data);
                        last->offset = 0;
                        last->depth = 0;
                }
-               stream_blob(len, sha1out, mark);
+               stream_blob(len, oidout, mark);
                skip_optional_lf();
        }
 }
@@ -2119,26 +2212,30 @@ static void construct_path_with_fanout(const char *hex_sha1,
                path[i++] = '/';
                fanout--;
        }
-       memcpy(path + i, hex_sha1 + j, 40 - j);
-       path[i + 40 - j] = '\0';
+       memcpy(path + i, hex_sha1 + j, GIT_SHA1_HEXSZ - j);
+       path[i + GIT_SHA1_HEXSZ - j] = '\0';
 }
 
 static uintmax_t do_change_note_fanout(
                struct tree_entry *orig_root, struct tree_entry *root,
-               char *hex_sha1, unsigned int hex_sha1_len,
+               char *hex_oid, unsigned int hex_oid_len,
                char *fullpath, unsigned int fullpath_len,
                unsigned char fanout)
 {
-       struct tree_content *t = root->tree;
+       struct tree_content *t;
        struct tree_entry *e, leaf;
-       unsigned int i, tmp_hex_sha1_len, tmp_fullpath_len;
+       unsigned int i, tmp_hex_oid_len, tmp_fullpath_len;
        uintmax_t num_notes = 0;
-       unsigned char sha1[20];
+       struct object_id oid;
        char realpath[60];
 
+       if (!root->tree)
+               load_tree(root);
+       t = root->tree;
+
        for (i = 0; t && i < t->entry_count; i++) {
                e = t->entries[i];
-               tmp_hex_sha1_len = hex_sha1_len + e->name->str_len;
+               tmp_hex_oid_len = hex_oid_len + e->name->str_len;
                tmp_fullpath_len = fullpath_len;
 
                /*
@@ -2150,12 +2247,12 @@ static uintmax_t do_change_note_fanout(
                 * of 2 chars.
                 */
                if (!e->versions[1].mode ||
-                   tmp_hex_sha1_len > 40 ||
+                   tmp_hex_oid_len > GIT_SHA1_HEXSZ ||
                    e->name->str_len % 2)
                        continue;
 
                /* This _may_ be a note entry, or a subdir containing notes */
-               memcpy(hex_sha1 + hex_sha1_len, e->name->str_dat,
+               memcpy(hex_oid + hex_oid_len, e->name->str_dat,
                       e->name->str_len);
                if (tmp_fullpath_len)
                        fullpath[tmp_fullpath_len++] = '/';
@@ -2164,14 +2261,14 @@ static uintmax_t do_change_note_fanout(
                tmp_fullpath_len += e->name->str_len;
                fullpath[tmp_fullpath_len] = '\0';
 
-               if (tmp_hex_sha1_len == 40 && !get_sha1_hex(hex_sha1, sha1)) {
+               if (tmp_hex_oid_len == GIT_SHA1_HEXSZ && !get_oid_hex(hex_oid, &oid)) {
                        /* This is a note entry */
                        if (fanout == 0xff) {
                                /* Counting mode, no rename */
                                num_notes++;
                                continue;
                        }
-                       construct_path_with_fanout(hex_sha1, fanout, realpath);
+                       construct_path_with_fanout(hex_oid, fanout, realpath);
                        if (!strcmp(fullpath, realpath)) {
                                /* Note entry is in correct location */
                                num_notes++;
@@ -2179,18 +2276,16 @@ static uintmax_t do_change_note_fanout(
                        }
 
                        /* Rename fullpath to realpath */
-                       if (!tree_content_remove(orig_root, fullpath, &leaf))
+                       if (!tree_content_remove(orig_root, fullpath, &leaf, 0))
                                die("Failed to remove path %s", fullpath);
                        tree_content_set(orig_root, realpath,
-                               leaf.versions[1].sha1,
+                               &leaf.versions[1].oid,
                                leaf.versions[1].mode,
                                leaf.tree);
                } else if (S_ISDIR(e->versions[1].mode)) {
                        /* This is a subdir that may contain note entries */
-                       if (!e->tree)
-                               load_tree(e);
                        num_notes += do_change_note_fanout(orig_root, e,
-                               hex_sha1, tmp_hex_sha1_len,
+                               hex_oid, tmp_hex_oid_len,
                                fullpath, tmp_fullpath_len, fanout);
                }
 
@@ -2203,8 +2298,14 @@ static uintmax_t do_change_note_fanout(
 static uintmax_t change_note_fanout(struct tree_entry *root,
                unsigned char fanout)
 {
-       char hex_sha1[40], path[60];
-       return do_change_note_fanout(root, root, hex_sha1, 0, path, 0, fanout);
+       /*
+        * The size of path is due to one slash between every two hex digits,
+        * plus the terminating NUL.  Note that there is no slash at the end, so
+        * the number of slashes is one less than half the number of hex
+        * characters.
+        */
+       char hex_oid[GIT_MAX_HEXSZ], path[GIT_MAX_HEXSZ + (GIT_MAX_HEXSZ / 2) - 1 + 1];
+       return do_change_note_fanout(root, root, hex_oid, 0, path, 0, fanout);
 }
 
 /*
@@ -2254,19 +2355,18 @@ static uintmax_t parse_mark_ref_space(const char **p)
        char *end;
 
        mark = parse_mark_ref(*p, &end);
-       if (*end != ' ')
+       if (*end++ != ' ')
                die("Missing space after mark: %s", command_buf.buf);
        *p = end;
        return mark;
 }
 
-static void file_change_m(struct branch *b)
+static void file_change_m(const char *p, struct branch *b)
 {
-       const char *p = command_buf.buf + 2;
        static struct strbuf uq = STRBUF_INIT;
        const char *endp;
-       struct object_entry *oe = oe;
-       unsigned char sha1[20];
+       struct object_entry *oe;
+       struct object_id oid;
        uint16_t mode, inline_data = 0;
 
        p = get_mode(p, &mode);
@@ -2289,20 +2389,17 @@ static void file_change_m(struct branch *b)
 
        if (*p == ':') {
                oe = find_mark(parse_mark_ref_space(&p));
-               hashcpy(sha1, oe->idx.sha1);
-       } else if (!prefixcmp(p, "inline ")) {
+               oidcpy(&oid, &oe->idx.oid);
+       } else if (skip_prefix(p, "inline ", &p)) {
                inline_data = 1;
-               p += strlen("inline");  /* advance to space */
+               oe = NULL; /* not used with inline_data, but makes gcc happy */
        } else {
-               if (get_sha1_hex(p, sha1))
+               if (parse_oid_hex(p, &oid, &p))
                        die("Invalid dataref: %s", command_buf.buf);
-               oe = find_object(sha1);
-               p += 40;
-               if (*p != ' ')
+               oe = find_object(&oid);
+               if (*p++ != ' ')
                        die("Missing space after SHA1: %s", command_buf.buf);
        }
-       assert(*p == ' ');
-       p++;  /* skip space */
 
        strbuf_reset(&uq);
        if (!unquote_c_style(&uq, p, &endp)) {
@@ -2312,8 +2409,8 @@ static void file_change_m(struct branch *b)
        }
 
        /* Git does not track empty, non-toplevel directories. */
-       if (S_ISDIR(mode) && !memcmp(sha1, EMPTY_TREE_SHA1_BIN, 20) && *p) {
-               tree_content_remove(&b->branch_tree, p, NULL);
+       if (S_ISDIR(mode) && is_empty_tree_oid(&oid) && *p) {
+               tree_content_remove(&b->branch_tree, p, NULL, 0);
                return;
        }
 
@@ -2339,12 +2436,12 @@ static void file_change_m(struct branch *b)
                        p = uq.buf;
                }
                read_next_command();
-               parse_and_store_blob(&last_blob, sha1, 0);
+               parse_and_store_blob(&last_blob, &oid, 0);
        } else {
                enum object_type expected = S_ISDIR(mode) ?
                                                OBJ_TREE: OBJ_BLOB;
                enum object_type type = oe ? oe->type :
-                                       sha1_object_info(sha1, NULL);
+                                       sha1_object_info(oid.hash, NULL);
                if (type < 0)
                        die("%s not found: %s",
                                        S_ISDIR(mode) ?  "Tree" : "Blob",
@@ -2356,15 +2453,14 @@ static void file_change_m(struct branch *b)
        }
 
        if (!*p) {
-               tree_content_replace(&b->branch_tree, sha1, mode, NULL);
+               tree_content_replace(&b->branch_tree, &oid, mode, NULL);
                return;
        }
-       tree_content_set(&b->branch_tree, p, sha1, mode, NULL);
+       tree_content_set(&b->branch_tree, p, &oid, mode, NULL);
 }
 
-static void file_change_d(struct branch *b)
+static void file_change_d(const char *p, struct branch *b)
 {
-       const char *p = command_buf.buf + 2;
        static struct strbuf uq = STRBUF_INIT;
        const char *endp;
 
@@ -2374,18 +2470,17 @@ static void file_change_d(struct branch *b)
                        die("Garbage after path in: %s", command_buf.buf);
                p = uq.buf;
        }
-       tree_content_remove(&b->branch_tree, p, NULL);
+       tree_content_remove(&b->branch_tree, p, NULL, 1);
 }
 
-static void file_change_cr(struct branch *b, int rename)
+static void file_change_cr(const char *s, struct branch *b, int rename)
 {
-       const char *s, *d;
+       const char *d;
        static struct strbuf s_uq = STRBUF_INIT;
        static struct strbuf d_uq = STRBUF_INIT;
        const char *endp;
        struct tree_entry leaf;
 
-       s = command_buf.buf + 2;
        strbuf_reset(&s_uq);
        if (!unquote_c_style(&s_uq, s, &endp)) {
                if (*endp != ' ')
@@ -2412,31 +2507,30 @@ static void file_change_cr(struct branch *b, int rename)
 
        memset(&leaf, 0, sizeof(leaf));
        if (rename)
-               tree_content_remove(&b->branch_tree, s, &leaf);
+               tree_content_remove(&b->branch_tree, s, &leaf, 1);
        else
-               tree_content_get(&b->branch_tree, s, &leaf);
+               tree_content_get(&b->branch_tree, s, &leaf, 1);
        if (!leaf.versions[1].mode)
                die("Path %s not in branch", s);
        if (!*d) {      /* C "path/to/subdir" "" */
                tree_content_replace(&b->branch_tree,
-                       leaf.versions[1].sha1,
+                       &leaf.versions[1].oid,
                        leaf.versions[1].mode,
                        leaf.tree);
                return;
        }
        tree_content_set(&b->branch_tree, d,
-               leaf.versions[1].sha1,
+               &leaf.versions[1].oid,
                leaf.versions[1].mode,
                leaf.tree);
 }
 
-static void note_change_n(struct branch *b, unsigned char *old_fanout)
+static void note_change_n(const char *p, struct branch *b, unsigned char *old_fanout)
 {
-       const char *p = command_buf.buf + 2;
        static struct strbuf uq = STRBUF_INIT;
-       struct object_entry *oe = oe;
+       struct object_entry *oe;
        struct branch *s;
-       unsigned char sha1[20], commit_sha1[20];
+       struct object_id oid, commit_oid;
        char path[60];
        uint16_t inline_data = 0;
        unsigned char new_fanout;
@@ -2461,37 +2555,34 @@ static void note_change_n(struct branch *b, unsigned char *old_fanout)
        /* <dataref> or 'inline' */
        if (*p == ':') {
                oe = find_mark(parse_mark_ref_space(&p));
-               hashcpy(sha1, oe->idx.sha1);
-       } else if (!prefixcmp(p, "inline ")) {
+               oidcpy(&oid, &oe->idx.oid);
+       } else if (skip_prefix(p, "inline ", &p)) {
                inline_data = 1;
-               p += strlen("inline");  /* advance to space */
+               oe = NULL; /* not used with inline_data, but makes gcc happy */
        } else {
-               if (get_sha1_hex(p, sha1))
+               if (parse_oid_hex(p, &oid, &p))
                        die("Invalid dataref: %s", command_buf.buf);
-               oe = find_object(sha1);
-               p += 40;
-               if (*p != ' ')
+               oe = find_object(&oid);
+               if (*p++ != ' ')
                        die("Missing space after SHA1: %s", command_buf.buf);
        }
-       assert(*p == ' ');
-       p++;  /* skip space */
 
-       /* <committish> */
+       /* <commit-ish> */
        s = lookup_branch(p);
        if (s) {
-               if (is_null_sha1(s->sha1))
+               if (is_null_oid(&s->oid))
                        die("Can't add a note on empty branch.");
-               hashcpy(commit_sha1, s->sha1);
+               oidcpy(&commit_oid, &s->oid);
        } else if (*p == ':') {
                uintmax_t commit_mark = parse_mark_ref_eol(p);
                struct object_entry *commit_oe = find_mark(commit_mark);
                if (commit_oe->type != OBJ_COMMIT)
                        die("Mark :%" PRIuMAX " not a commit", commit_mark);
-               hashcpy(commit_sha1, commit_oe->idx.sha1);
-       } else if (!get_sha1(p, commit_sha1)) {
+               oidcpy(&commit_oid, &commit_oe->idx.oid);
+       } else if (!get_oid(p, &commit_oid)) {
                unsigned long size;
-               char *buf = read_object_with_reference(commit_sha1,
-                       commit_type, &size, commit_sha1);
+               char *buf = read_object_with_reference(commit_oid.hash,
+                       commit_type, &size, commit_oid.hash);
                if (!buf || size < 46)
                        die("Not a valid commit: %s", p);
                free(buf);
@@ -2504,13 +2595,13 @@ static void note_change_n(struct branch *b, unsigned char *old_fanout)
                        p = uq.buf;
                }
                read_next_command();
-               parse_and_store_blob(&last_blob, sha1, 0);
+               parse_and_store_blob(&last_blob, &oid, 0);
        } else if (oe) {
                if (oe->type != OBJ_BLOB)
                        die("Not a blob (actually a %s): %s",
                                typename(oe->type), command_buf.buf);
-       } else if (!is_null_sha1(sha1)) {
-               enum object_type type = sha1_object_info(sha1, NULL);
+       } else if (!is_null_oid(&oid)) {
+               enum object_type type = sha1_object_info(oid.hash, NULL);
                if (type < 0)
                        die("Blob not found: %s", command_buf.buf);
                if (type != OBJ_BLOB)
@@ -2518,50 +2609,51 @@ static void note_change_n(struct branch *b, unsigned char *old_fanout)
                            typename(type), command_buf.buf);
        }
 
-       construct_path_with_fanout(sha1_to_hex(commit_sha1), *old_fanout, path);
-       if (tree_content_remove(&b->branch_tree, path, NULL))
+       construct_path_with_fanout(oid_to_hex(&commit_oid), *old_fanout, path);
+       if (tree_content_remove(&b->branch_tree, path, NULL, 0))
                b->num_notes--;
 
-       if (is_null_sha1(sha1))
+       if (is_null_oid(&oid))
                return; /* nothing to insert */
 
        b->num_notes++;
        new_fanout = convert_num_notes_to_fanout(b->num_notes);
-       construct_path_with_fanout(sha1_to_hex(commit_sha1), new_fanout, path);
-       tree_content_set(&b->branch_tree, path, sha1, S_IFREG | 0644, NULL);
+       construct_path_with_fanout(oid_to_hex(&commit_oid), new_fanout, path);
+       tree_content_set(&b->branch_tree, path, &oid, S_IFREG | 0644, NULL);
 }
 
 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);
+       oidclr(&b->branch_tree.versions[0].oid);
+       oidclr(&b->branch_tree.versions[1].oid);
        load_tree(&b->branch_tree);
        b->num_notes = 0;
 }
 
 static void parse_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 (!buf || size < GIT_SHA1_HEXSZ + 6)
+               die("Not a valid commit: %s", oid_to_hex(&b->oid));
        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);
+               || get_oid_hex(buf + 5, &b->branch_tree.versions[1].oid))
+               die("The commit %s is corrupt", oid_to_hex(&b->oid));
+       oidcpy(&b->branch_tree.versions[0].oid,
+              &b->branch_tree.versions[1].oid);
 }
 
 static void parse_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);
+       if (is_null_oid(&b->oid)) {
+               oidclr(&b->branch_tree.versions[0].oid);
+               oidclr(&b->branch_tree.versions[1].oid);
        } else {
                unsigned long size;
                char *buf;
 
-               buf = read_object_with_reference(b->sha1,
-                       commit_type, &size, b->sha1);
+               buf = read_object_with_reference(b->oid.hash,
+                                                commit_type, &size,
+                                                b->oid.hash);
                parse_from_commit(b, buf, size);
                free(buf);
        }
@@ -2571,69 +2663,75 @@ static int parse_from(struct branch *b)
 {
        const char *from;
        struct branch *s;
+       struct object_id oid;
 
-       if (prefixcmp(command_buf.buf, "from "))
+       if (!skip_prefix(command_buf.buf, "from ", &from))
                return 0;
 
-       if (b->branch_tree.tree) {
-               release_tree_content_recursive(b->branch_tree.tree);
-               b->branch_tree.tree = NULL;
-       }
+       oidcpy(&oid, &b->branch_tree.versions[1].oid);
 
-       from = strchr(command_buf.buf, ' ') + 1;
        s = lookup_branch(from);
        if (b == s)
                die("Can't create a branch from itself: %s", b->name);
        else if (s) {
-               unsigned char *t = s->branch_tree.versions[1].sha1;
-               hashcpy(b->sha1, s->sha1);
-               hashcpy(b->branch_tree.versions[0].sha1, t);
-               hashcpy(b->branch_tree.versions[1].sha1, t);
+               struct object_id *t = &s->branch_tree.versions[1].oid;
+               oidcpy(&b->oid, &s->oid);
+               oidcpy(&b->branch_tree.versions[0].oid, t);
+               oidcpy(&b->branch_tree.versions[1].oid, t);
        } else if (*from == ':') {
                uintmax_t idnum = parse_mark_ref_eol(from);
                struct object_entry *oe = find_mark(idnum);
                if (oe->type != OBJ_COMMIT)
                        die("Mark :%" PRIuMAX " not a commit", idnum);
-               hashcpy(b->sha1, oe->idx.sha1);
-               if (oe->pack_id != MAX_PACK_ID) {
-                       unsigned long size;
-                       char *buf = gfi_unpack_entry(oe, &size);
-                       parse_from_commit(b, buf, size);
-                       free(buf);
-               } else
-                       parse_from_existing(b);
-       } else if (!get_sha1(from, b->sha1))
+               if (oidcmp(&b->oid, &oe->idx.oid)) {
+                       oidcpy(&b->oid, &oe->idx.oid);
+                       if (oe->pack_id != MAX_PACK_ID) {
+                               unsigned long size;
+                               char *buf = gfi_unpack_entry(oe, &size);
+                               parse_from_commit(b, buf, size);
+                               free(buf);
+                       } else
+                               parse_from_existing(b);
+               }
+       } else if (!get_oid(from, &b->oid)) {
                parse_from_existing(b);
+               if (is_null_oid(&b->oid))
+                       b->delete = 1;
+       }
        else
                die("Invalid ref name or SHA1 expression: %s", from);
 
+       if (b->branch_tree.tree && oidcmp(&oid, &b->branch_tree.versions[1].oid)) {
+               release_tree_content_recursive(b->branch_tree.tree);
+               b->branch_tree.tree = NULL;
+       }
+
        read_next_command();
        return 1;
 }
 
 static struct hash_list *parse_merge(unsigned int *count)
 {
-       struct hash_list *list = NULL, *n, *e = e;
+       struct hash_list *list = NULL, **tail = &list, *n;
        const char *from;
        struct branch *s;
 
        *count = 0;
-       while (!prefixcmp(command_buf.buf, "merge ")) {
-               from = strchr(command_buf.buf, ' ') + 1;
+       while (skip_prefix(command_buf.buf, "merge ", &from)) {
                n = xmalloc(sizeof(*n));
                s = lookup_branch(from);
                if (s)
-                       hashcpy(n->sha1, s->sha1);
+                       oidcpy(&n->oid, &s->oid);
                else if (*from == ':') {
                        uintmax_t idnum = parse_mark_ref_eol(from);
                        struct object_entry *oe = find_mark(idnum);
                        if (oe->type != OBJ_COMMIT)
                                die("Mark :%" PRIuMAX " not a commit", idnum);
-                       hashcpy(n->sha1, oe->idx.sha1);
-               } else if (!get_sha1(from, n->sha1)) {
+                       oidcpy(&n->oid, &oe->idx.oid);
+               } else if (!get_oid(from, &n->oid)) {
                        unsigned long size;
-                       char *buf = read_object_with_reference(n->sha1,
-                               commit_type, &size, n->sha1);
+                       char *buf = read_object_with_reference(n->oid.hash,
+                               commit_type, &size, n->oid.hash);
                        if (!buf || size < 46)
                                die("Not a valid commit: %s", from);
                        free(buf);
@@ -2641,42 +2739,38 @@ static struct hash_list *parse_merge(unsigned int *count)
                        die("Invalid ref name or SHA1 expression: %s", from);
 
                n->next = NULL;
-               if (list)
-                       e->next = n;
-               else
-                       list = n;
-               e = n;
+               *tail = n;
+               tail = &n->next;
+
                (*count)++;
                read_next_command();
        }
        return list;
 }
 
-static void parse_new_commit(void)
+static void parse_new_commit(const char *arg)
 {
        static struct strbuf msg = STRBUF_INIT;
        struct branch *b;
-       char *sp;
        char *author = NULL;
        char *committer = NULL;
        struct hash_list *merge_list = NULL;
        unsigned int merge_count;
        unsigned char prev_fanout, new_fanout;
+       const char *v;
 
-       /* Obtain the branch name from the rest of our command */
-       sp = strchr(command_buf.buf, ' ') + 1;
-       b = lookup_branch(sp);
+       b = lookup_branch(arg);
        if (!b)
-               b = new_branch(sp);
+               b = new_branch(arg);
 
        read_next_command();
        parse_mark();
-       if (!prefixcmp(command_buf.buf, "author ")) {
-               author = parse_ident(command_buf.buf + 7);
+       if (skip_prefix(command_buf.buf, "author ", &v)) {
+               author = parse_ident(v);
                read_next_command();
        }
-       if (!prefixcmp(command_buf.buf, "committer ")) {
-               committer = parse_ident(command_buf.buf + 10);
+       if (skip_prefix(command_buf.buf, "committer ", &v)) {
+               committer = parse_ident(v);
                read_next_command();
        }
        if (!committer)
@@ -2696,20 +2790,20 @@ static void parse_new_commit(void)
 
        /* file_change* */
        while (command_buf.len > 0) {
-               if (!prefixcmp(command_buf.buf, "M "))
-                       file_change_m(b);
-               else if (!prefixcmp(command_buf.buf, "D "))
-                       file_change_d(b);
-               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 (!prefixcmp(command_buf.buf, "N "))
-                       note_change_n(b, &prev_fanout);
+               if (skip_prefix(command_buf.buf, "M ", &v))
+                       file_change_m(v, b);
+               else if (skip_prefix(command_buf.buf, "D ", &v))
+                       file_change_d(v, b);
+               else if (skip_prefix(command_buf.buf, "R ", &v))
+                       file_change_cr(v, b, 1);
+               else if (skip_prefix(command_buf.buf, "C ", &v))
+                       file_change_cr(v, b, 0);
+               else if (skip_prefix(command_buf.buf, "N ", &v))
+                       note_change_n(v, b, &prev_fanout);
                else if (!strcmp("deleteall", command_buf.buf))
                        file_change_deleteall(b);
-               else if (!prefixcmp(command_buf.buf, "ls "))
-                       parse_ls(b);
+               else if (skip_prefix(command_buf.buf, "ls ", &v))
+                       parse_ls(v, b);
                else {
                        unread_command_buf = 1;
                        break;
@@ -2724,17 +2818,19 @@ static void parse_new_commit(void)
 
        /* build the tree and the commit */
        store_tree(&b->branch_tree);
-       hashcpy(b->branch_tree.versions[0].sha1,
-               b->branch_tree.versions[1].sha1);
+       oidcpy(&b->branch_tree.versions[0].oid,
+              &b->branch_tree.versions[1].oid);
 
        strbuf_reset(&new_data);
        strbuf_addf(&new_data, "tree %s\n",
-               sha1_to_hex(b->branch_tree.versions[1].sha1));
-       if (!is_null_sha1(b->sha1))
-               strbuf_addf(&new_data, "parent %s\n", sha1_to_hex(b->sha1));
+               oid_to_hex(&b->branch_tree.versions[1].oid));
+       if (!is_null_oid(&b->oid))
+               strbuf_addf(&new_data, "parent %s\n",
+                           oid_to_hex(&b->oid));
        while (merge_list) {
                struct hash_list *next = merge_list->next;
-               strbuf_addf(&new_data, "parent %s\n", sha1_to_hex(merge_list->sha1));
+               strbuf_addf(&new_data, "parent %s\n",
+                           oid_to_hex(&merge_list->oid));
                free(merge_list);
                merge_list = next;
        }
@@ -2747,28 +2843,26 @@ static void parse_new_commit(void)
        free(author);
        free(committer);
 
-       if (!store_object(OBJ_COMMIT, &new_data, NULL, b->sha1, next_mark))
+       if (!store_object(OBJ_COMMIT, &new_data, NULL, &b->oid, next_mark))
                b->pack_id = pack_id;
        b->last_commit = object_count_by_type[OBJ_COMMIT];
 }
 
-static void parse_new_tag(void)
+static void parse_new_tag(const char *arg)
 {
        static struct strbuf msg = STRBUF_INIT;
-       char *sp;
        const char *from;
        char *tagger;
        struct branch *s;
        struct tag *t;
        uintmax_t from_mark = 0;
-       unsigned char sha1[20];
+       struct object_id oid;
        enum object_type type;
+       const char *v;
 
-       /* Obtain the new tag name from the rest of our command */
-       sp = strchr(command_buf.buf, ' ') + 1;
        t = pool_alloc(sizeof(struct tag));
        memset(t, 0, sizeof(struct tag));
-       t->name = pool_strdup(sp);
+       t->name = pool_strdup(arg);
        if (last_tag)
                last_tag->next_tag = t;
        else
@@ -2777,25 +2871,24 @@ static void parse_new_tag(void)
        read_next_command();
 
        /* from ... */
-       if (prefixcmp(command_buf.buf, "from "))
+       if (!skip_prefix(command_buf.buf, "from ", &from))
                die("Expected from command, got %s", command_buf.buf);
-       from = strchr(command_buf.buf, ' ') + 1;
        s = lookup_branch(from);
        if (s) {
-               if (is_null_sha1(s->sha1))
+               if (is_null_oid(&s->oid))
                        die("Can't tag an empty branch.");
-               hashcpy(sha1, s->sha1);
+               oidcpy(&oid, &s->oid);
                type = OBJ_COMMIT;
        } else if (*from == ':') {
                struct object_entry *oe;
                from_mark = parse_mark_ref_eol(from);
                oe = find_mark(from_mark);
                type = oe->type;
-               hashcpy(sha1, oe->idx.sha1);
-       } else if (!get_sha1(from, sha1)) {
-               struct object_entry *oe = find_object(sha1);
+               oidcpy(&oid, &oe->idx.oid);
+       } else if (!get_oid(from, &oid)) {
+               struct object_entry *oe = find_object(&oid);
                if (!oe) {
-                       type = sha1_object_info(sha1, NULL);
+                       type = sha1_object_info(oid.hash, NULL);
                        if (type < 0)
                                die("Not a valid object: %s", from);
                } else
@@ -2805,8 +2898,8 @@ static void parse_new_tag(void)
        read_next_command();
 
        /* tagger ... */
-       if (!prefixcmp(command_buf.buf, "tagger ")) {
-               tagger = parse_ident(command_buf.buf + 7);
+       if (skip_prefix(command_buf.buf, "tagger ", &v)) {
+               tagger = parse_ident(v);
                read_next_command();
        } else
                tagger = NULL;
@@ -2821,7 +2914,7 @@ static void parse_new_tag(void)
                    "object %s\n"
                    "type %s\n"
                    "tag %s\n",
-                   sha1_to_hex(sha1), typename(type), t->name);
+                   oid_to_hex(&oid), typename(type), t->name);
        if (tagger)
                strbuf_addf(&new_data,
                            "tagger %s\n", tagger);
@@ -2829,31 +2922,28 @@ static void parse_new_tag(void)
        strbuf_addbuf(&new_data, &msg);
        free(tagger);
 
-       if (store_object(OBJ_TAG, &new_data, NULL, t->sha1, 0))
+       if (store_object(OBJ_TAG, &new_data, NULL, &t->oid, 0))
                t->pack_id = MAX_PACK_ID;
        else
                t->pack_id = pack_id;
 }
 
-static void parse_reset_branch(void)
+static void parse_reset_branch(const char *arg)
 {
        struct branch *b;
-       char *sp;
 
-       /* Obtain the branch name from the rest of our command */
-       sp = strchr(command_buf.buf, ' ') + 1;
-       b = lookup_branch(sp);
+       b = lookup_branch(arg);
        if (b) {
-               hashclr(b->sha1);
-               hashclr(b->branch_tree.versions[0].sha1);
-               hashclr(b->branch_tree.versions[1].sha1);
+               oidclr(&b->oid);
+               oidclr(&b->branch_tree.versions[0].oid);
+               oidclr(&b->branch_tree.versions[1].oid);
                if (b->branch_tree.tree) {
                        release_tree_content_recursive(b->branch_tree.tree);
                        b->branch_tree.tree = NULL;
                }
        }
        else
-               b = new_branch(sp);
+               b = new_branch(arg);
        read_next_command();
        parse_from(b);
        if (command_buf.len > 0)
@@ -2862,11 +2952,11 @@ static void parse_reset_branch(void)
 
 static void cat_blob_write(const char *buf, unsigned long size)
 {
-       if (write_in_full(cat_blob_fd, buf, size) != size)
+       if (write_in_full(cat_blob_fd, buf, size) < 0)
                die_errno("Write to frontend failed");
 }
 
-static void cat_blob(struct object_entry *oe, unsigned char sha1[20])
+static void cat_blob(struct object_entry *oe, struct object_id *oid)
 {
        struct strbuf line = STRBUF_INIT;
        unsigned long size;
@@ -2874,7 +2964,7 @@ static void cat_blob(struct object_entry *oe, unsigned char sha1[20])
        char *buf;
 
        if (!oe || oe->pack_id == MAX_PACK_ID) {
-               buf = read_sha1_file(sha1, &type, &size);
+               buf = read_sha1_file(oid->hash, &type, &size);
        } else {
                type = oe->type;
                buf = gfi_unpack_entry(oe, &size);
@@ -2885,19 +2975,19 @@ static void cat_blob(struct object_entry *oe, unsigned char sha1[20])
         */
        if (type <= 0) {
                strbuf_reset(&line);
-               strbuf_addf(&line, "%s missing\n", sha1_to_hex(sha1));
+               strbuf_addf(&line, "%s missing\n", oid_to_hex(oid));
                cat_blob_write(line.buf, line.len);
                strbuf_release(&line);
                free(buf);
                return;
        }
        if (!buf)
-               die("Can't read object %s", sha1_to_hex(sha1));
+               die("Can't read object %s", oid_to_hex(oid));
        if (type != OBJ_BLOB)
                die("Object %s is a %s but a blob was expected.",
-                   sha1_to_hex(sha1), typename(type));
+                   oid_to_hex(oid), typename(type));
        strbuf_reset(&line);
-       strbuf_addf(&line, "%s %s %lu\n", sha1_to_hex(sha1),
+       strbuf_addf(&line, "%s %s %lu\n", oid_to_hex(oid),
                                                typename(type), size);
        cat_blob_write(line.buf, line.len);
        strbuf_release(&line);
@@ -2911,41 +3001,56 @@ static void cat_blob(struct object_entry *oe, unsigned char sha1[20])
                free(buf);
 }
 
-static void parse_cat_blob(void)
+static void parse_get_mark(const char *p)
 {
-       const char *p;
        struct object_entry *oe = oe;
-       unsigned char sha1[20];
+       char output[GIT_MAX_HEXSZ + 2];
+
+       /* get-mark SP <object> LF */
+       if (*p != ':')
+               die("Not a mark: %s", p);
+
+       oe = find_mark(parse_mark_ref_eol(p));
+       if (!oe)
+               die("Unknown mark: %s", command_buf.buf);
+
+       xsnprintf(output, sizeof(output), "%s\n", oid_to_hex(&oe->idx.oid));
+       cat_blob_write(output, GIT_SHA1_HEXSZ + 1);
+}
+
+static void parse_cat_blob(const char *p)
+{
+       struct object_entry *oe = oe;
+       struct object_id oid;
 
        /* cat-blob SP <object> LF */
-       p = command_buf.buf + strlen("cat-blob ");
        if (*p == ':') {
                oe = find_mark(parse_mark_ref_eol(p));
                if (!oe)
                        die("Unknown mark: %s", command_buf.buf);
-               hashcpy(sha1, oe->idx.sha1);
+               oidcpy(&oid, &oe->idx.oid);
        } else {
-               if (get_sha1_hex(p, sha1))
+               if (parse_oid_hex(p, &oid, &p))
                        die("Invalid dataref: %s", command_buf.buf);
-               if (p[40])
+               if (*p)
                        die("Garbage after SHA1: %s", command_buf.buf);
-               oe = find_object(sha1);
+               oe = find_object(&oid);
        }
 
-       cat_blob(oe, sha1);
+       cat_blob(oe, &oid);
 }
 
 static struct object_entry *dereference(struct object_entry *oe,
-                                       unsigned char sha1[20])
+                                       struct object_id *oid)
 {
        unsigned long size;
        char *buf = NULL;
        if (!oe) {
-               enum object_type type = sha1_object_info(sha1, NULL);
+               enum object_type type = sha1_object_info(oid->hash, NULL);
                if (type < 0)
-                       die("object not found: %s", sha1_to_hex(sha1));
+                       die("object not found: %s", oid_to_hex(oid));
                /* cache it! */
-               oe = insert_object(sha1);
+               oe = insert_object(oid);
                oe->type = type;
                oe->pack_id = MAX_PACK_ID;
                oe->idx.offset = 1;
@@ -2957,54 +3062,55 @@ static struct object_entry *dereference(struct object_entry *oe,
        case OBJ_TAG:
                break;
        default:
-               die("Not a treeish: %s", command_buf.buf);
+               die("Not a tree-ish: %s", command_buf.buf);
        }
 
        if (oe->pack_id != MAX_PACK_ID) {       /* in a pack being written */
                buf = gfi_unpack_entry(oe, &size);
        } else {
                enum object_type unused;
-               buf = read_sha1_file(sha1, &unused, &size);
+               buf = read_sha1_file(oid->hash, &unused, &size);
        }
        if (!buf)
-               die("Can't load object %s", sha1_to_hex(sha1));
+               die("Can't load object %s", oid_to_hex(oid));
 
        /* Peel one layer. */
        switch (oe->type) {
        case OBJ_TAG:
-               if (size < 40 + strlen("object ") ||
-                   get_sha1_hex(buf + strlen("object "), sha1))
+               if (size < GIT_SHA1_HEXSZ + strlen("object ") ||
+                   get_oid_hex(buf + strlen("object "), oid))
                        die("Invalid SHA1 in tag: %s", command_buf.buf);
                break;
        case OBJ_COMMIT:
-               if (size < 40 + strlen("tree ") ||
-                   get_sha1_hex(buf + strlen("tree "), sha1))
+               if (size < GIT_SHA1_HEXSZ + strlen("tree ") ||
+                   get_oid_hex(buf + strlen("tree "), oid))
                        die("Invalid SHA1 in commit: %s", command_buf.buf);
        }
 
        free(buf);
-       return find_object(sha1);
+       return find_object(oid);
 }
 
 static struct object_entry *parse_treeish_dataref(const char **p)
 {
-       unsigned char sha1[20];
+       struct object_id oid;
        struct object_entry *e;
 
        if (**p == ':') {       /* <mark> */
                e = find_mark(parse_mark_ref_space(p));
                if (!e)
                        die("Unknown mark: %s", command_buf.buf);
-               hashcpy(sha1, e->idx.sha1);
+               oidcpy(&oid, &e->idx.oid);
        } else {        /* <sha1> */
-               if (get_sha1_hex(*p, sha1))
+               if (parse_oid_hex(*p, &oid, p))
                        die("Invalid dataref: %s", command_buf.buf);
-               e = find_object(sha1);
-               *p += 40;
+               e = find_object(&oid);
+               if (*(*p)++ != ' ')
+                       die("Missing space after tree-ish: %s", command_buf.buf);
        }
 
        while (!e || e->type != OBJ_TREE)
-               e = dereference(e, sha1);
+               e = dereference(e, &oid);
        return e;
 }
 
@@ -3035,14 +3141,12 @@ static void print_ls(int mode, const unsigned char *sha1, const char *path)
        cat_blob_write(line.buf, line.len);
 }
 
-static void parse_ls(struct branch *b)
+static void parse_ls(const char *p, struct branch *b)
 {
-       const char *p;
        struct tree_entry *root = NULL;
        struct tree_entry leaf = {NULL};
 
-       /* ls SP (<treeish> SP)? <path> */
-       p = command_buf.buf + strlen("ls ");
+       /* ls SP (<tree-ish> SP)? <path> */
        if (*p == '"') {
                if (!b)
                        die("Not in a commit: %s", command_buf.buf);
@@ -3050,10 +3154,10 @@ static void parse_ls(struct branch *b)
        } else {
                struct object_entry *e = parse_treeish_dataref(&p);
                root = new_tree_entry();
-               hashcpy(root->versions[1].sha1, e->idx.sha1);
+               oidcpy(&root->versions[1].oid, &e->idx.oid);
+               if (!is_null_oid(&root->versions[1].oid))
+                       root->versions[1].mode = S_IFDIR;
                load_tree(root);
-               if (*p++ != ' ')
-                       die("Missing space after tree-ish: %s", command_buf.buf);
        }
        if (*p == '"') {
                static struct strbuf uq = STRBUF_INIT;
@@ -3065,7 +3169,7 @@ static void parse_ls(struct branch *b)
                        die("Garbage after path in: %s", command_buf.buf);
                p = uq.buf;
        }
-       tree_content_get(root, p, &leaf);
+       tree_content_get(root, p, &leaf, 1);
        /*
         * A directory in preparation would have a sha1 of zero
         * until it is saved.  Save, for simplicity.
@@ -3073,7 +3177,7 @@ static void parse_ls(struct branch *b)
        if (S_ISDIR(leaf.versions[1].mode))
                store_tree(&leaf);
 
-       print_ls(leaf.versions[1].mode, leaf.versions[1].sha1, p);
+       print_ls(leaf.versions[1].mode, leaf.versions[1].oid.hash, p);
        if (leaf.tree)
                release_tree_content_recursive(leaf.tree);
        if (!b || root != &b->branch_tree)
@@ -3085,10 +3189,10 @@ static void checkpoint(void)
        checkpoint_requested = 0;
        if (object_count) {
                cycle_packfile();
-               dump_branches();
-               dump_tags();
-               dump_marks();
        }
+       dump_branches();
+       dump_tags();
+       dump_marks();
 }
 
 static void parse_checkpoint(void)
@@ -3107,12 +3211,9 @@ static void parse_progress(void)
 
 static char* make_fast_import_path(const char *path)
 {
-       struct strbuf abs_path = STRBUF_INIT;
-
        if (!relative_marks_paths || is_absolute_path(path))
                return xstrdup(path);
-       strbuf_addf(&abs_path, "%s/info/fast-import/%s", get_git_dir(), path);
-       return strbuf_detach(&abs_path, NULL);
+       return git_pathdup("info/fast-import/%s", path);
 }
 
 static void option_import_marks(const char *marks,
@@ -3184,16 +3285,14 @@ static void option_export_pack_edges(const char *edges)
 {
        if (pack_edges)
                fclose(pack_edges);
-       pack_edges = fopen(edges, "a");
-       if (!pack_edges)
-               die_errno("Cannot open '%s'", edges);
+       pack_edges = xfopen(edges, "a");
 }
 
 static int parse_one_option(const char *option)
 {
-       if (!prefixcmp(option, "max-pack-size=")) {
+       if (skip_prefix(option, "max-pack-size=", &option)) {
                unsigned long v;
-               if (!git_parse_ulong(option + 14, &v))
+               if (!git_parse_ulong(option, &v))
                        return 0;
                if (v < 8192) {
                        warning("max-pack-size is now in bytes, assuming --max-pack-size=%lum", v);
@@ -3203,20 +3302,20 @@ static int parse_one_option(const char *option)
                        v = 1024 * 1024;
                }
                max_packsize = v;
-       } else if (!prefixcmp(option, "big-file-threshold=")) {
+       } else if (skip_prefix(option, "big-file-threshold=", &option)) {
                unsigned long v;
-               if (!git_parse_ulong(option + 19, &v))
+               if (!git_parse_ulong(option, &v))
                        return 0;
                big_file_threshold = v;
-       } else if (!prefixcmp(option, "depth=")) {
-               option_depth(option + 6);
-       } else if (!prefixcmp(option, "active-branches=")) {
-               option_active_branches(option + 16);
-       } else if (!prefixcmp(option, "export-pack-edges=")) {
-               option_export_pack_edges(option + 18);
-       } else if (!prefixcmp(option, "quiet")) {
+       } else if (skip_prefix(option, "depth=", &option)) {
+               option_depth(option);
+       } else if (skip_prefix(option, "active-branches=", &option)) {
+               option_active_branches(option);
+       } else if (skip_prefix(option, "export-pack-edges=", &option)) {
+               option_export_pack_edges(option);
+       } else if (starts_with(option, "quiet")) {
                show_stats = 0;
-       } else if (!prefixcmp(option, "stats")) {
+       } else if (starts_with(option, "stats")) {
                show_stats = 1;
        } else {
                return 0;
@@ -3227,15 +3326,18 @@ static int parse_one_option(const char *option)
 
 static int parse_one_feature(const char *feature, int from_stream)
 {
-       if (!prefixcmp(feature, "date-format=")) {
-               option_date_format(feature + 12);
-       } else if (!prefixcmp(feature, "import-marks=")) {
-               option_import_marks(feature + 13, from_stream, 0);
-       } else if (!prefixcmp(feature, "import-marks-if-exists=")) {
-               option_import_marks(feature + strlen("import-marks-if-exists="),
-                                       from_stream, 1);
-       } else if (!prefixcmp(feature, "export-marks=")) {
-               option_export_marks(feature + 13);
+       const char *arg;
+
+       if (skip_prefix(feature, "date-format=", &arg)) {
+               option_date_format(arg);
+       } else if (skip_prefix(feature, "import-marks=", &arg)) {
+               option_import_marks(arg, from_stream, 0);
+       } else if (skip_prefix(feature, "import-marks-if-exists=", &arg)) {
+               option_import_marks(arg, from_stream, 1);
+       } else if (skip_prefix(feature, "export-marks=", &arg)) {
+               option_export_marks(arg);
+       } else if (!strcmp(feature, "get-mark")) {
+               ; /* Don't die - this feature is supported */
        } else if (!strcmp(feature, "cat-blob")) {
                ; /* Don't die - this feature is supported */
        } else if (!strcmp(feature, "relative-marks")) {
@@ -3255,10 +3357,8 @@ static int parse_one_feature(const char *feature, int from_stream)
        return 1;
 }
 
-static void parse_feature(void)
+static void parse_feature(const char *feature)
 {
-       char *feature = command_buf.buf + 8;
-
        if (seen_data_command)
                die("Got feature command '%s' after data command", feature);
 
@@ -3268,10 +3368,8 @@ static void parse_feature(void)
        die("This version of fast-import does not support feature %s.", feature);
 }
 
-static void parse_option(void)
+static void parse_option(const char *option)
 {
-       char *option = command_buf.buf + 11;
-
        if (seen_data_command)
                die("Got option command '%s' after data command", option);
 
@@ -3281,36 +3379,31 @@ static void parse_option(void)
        die("This version of fast-import does not support option: %s", option);
 }
 
-static int git_pack_config(const char *k, const char *v, void *cb)
+static void git_pack_config(void)
 {
-       if (!strcmp(k, "pack.depth")) {
-               max_depth = git_config_int(k, v);
+       int indexversion_value;
+       int limit;
+       unsigned long packsizelimit_value;
+
+       if (!git_config_get_ulong("pack.depth", &max_depth)) {
                if (max_depth > MAX_DEPTH)
                        max_depth = MAX_DEPTH;
-               return 0;
-       }
-       if (!strcmp(k, "pack.compression")) {
-               int level = git_config_int(k, v);
-               if (level == -1)
-                       level = Z_DEFAULT_COMPRESSION;
-               else if (level < 0 || level > Z_BEST_COMPRESSION)
-                       die("bad pack compression level %d", level);
-               pack_compression_level = level;
-               pack_compression_seen = 1;
-               return 0;
        }
-       if (!strcmp(k, "pack.indexversion")) {
-               pack_idx_opts.version = git_config_int(k, v);
+       if (!git_config_get_int("pack.indexversion", &indexversion_value)) {
+               pack_idx_opts.version = indexversion_value;
                if (pack_idx_opts.version > 2)
-                       die("bad pack.indexversion=%"PRIu32,
-                           pack_idx_opts.version);
-               return 0;
-       }
-       if (!strcmp(k, "pack.packsizelimit")) {
-               max_packsize = git_config_ulong(k, v);
-               return 0;
+                       git_die_config("pack.indexversion",
+                                       "bad pack.indexversion=%"PRIu32, pack_idx_opts.version);
        }
-       return git_default_config(k, v, cb);
+       if (!git_config_get_ulong("pack.packsizelimit", &packsizelimit_value))
+               max_packsize = packsizelimit_value;
+
+       if (!git_config_get_int("fastimport.unpacklimit", &limit))
+               unpack_limit = limit;
+       else if (!git_config_get_int("transfer.unpacklimit", &limit))
+               unpack_limit = limit;
+
+       git_config(git_default_config, NULL);
 }
 
 static const char fast_import_usage[] =
@@ -3326,18 +3419,21 @@ static void parse_argv(void)
                if (*a != '-' || !strcmp(a, "--"))
                        break;
 
-               if (parse_one_option(a + 2))
+               if (!skip_prefix(a, "--", &a))
+                       die("unknown option %s", a);
+
+               if (parse_one_option(a))
                        continue;
 
-               if (parse_one_feature(a + 2, 0))
+               if (parse_one_feature(a, 0))
                        continue;
 
-               if (!prefixcmp(a + 2, "cat-blob-fd=")) {
-                       option_cat_blob_fd(a + 2 + strlen("cat-blob-fd="));
+               if (skip_prefix(a, "cat-blob-fd=", &a)) {
+                       option_cat_blob_fd(a);
                        continue;
                }
 
-               die("unknown option %s", a);
+               die("unknown option --%s", a);
        }
        if (i != global_argc)
                usage(fast_import_usage);
@@ -3347,22 +3443,16 @@ static void parse_argv(void)
                read_marks();
 }
 
-int main(int argc, const char **argv)
+int cmd_main(int argc, const char **argv)
 {
        unsigned int i;
 
-       git_extract_argv0_path(argv[0]);
-
-       git_setup_gettext();
-
        if (argc == 2 && !strcmp(argv[1], "-h"))
                usage(fast_import_usage);
 
        setup_git_directory();
        reset_pack_idx_option(&pack_idx_opts);
-       git_config(git_pack_config, NULL);
-       if (!pack_compression_seen && core_compression_seen)
-               pack_compression_level = core_compression_level;
+       git_pack_config();
 
        alloc_objects(object_entry_alloc);
        strbuf_init(&command_buf, 0);
@@ -3384,27 +3474,28 @@ int main(int argc, const char **argv)
        set_die_routine(die_nicely);
        set_checkpoint_signal();
        while (read_next_command() != EOF) {
+               const char *v;
                if (!strcmp("blob", command_buf.buf))
                        parse_new_blob();
-               else if (!prefixcmp(command_buf.buf, "ls "))
-                       parse_ls(NULL);
-               else if (!prefixcmp(command_buf.buf, "commit "))
-                       parse_new_commit();
-               else if (!prefixcmp(command_buf.buf, "tag "))
-                       parse_new_tag();
-               else if (!prefixcmp(command_buf.buf, "reset "))
-                       parse_reset_branch();
+               else if (skip_prefix(command_buf.buf, "ls ", &v))
+                       parse_ls(v, NULL);
+               else if (skip_prefix(command_buf.buf, "commit ", &v))
+                       parse_new_commit(v);
+               else if (skip_prefix(command_buf.buf, "tag ", &v))
+                       parse_new_tag(v);
+               else if (skip_prefix(command_buf.buf, "reset ", &v))
+                       parse_reset_branch(v);
                else if (!strcmp("checkpoint", command_buf.buf))
                        parse_checkpoint();
                else if (!strcmp("done", command_buf.buf))
                        break;
-               else if (!prefixcmp(command_buf.buf, "progress "))
+               else if (starts_with(command_buf.buf, "progress "))
                        parse_progress();
-               else if (!prefixcmp(command_buf.buf, "feature "))
-                       parse_feature();
-               else if (!prefixcmp(command_buf.buf, "option git "))
-                       parse_option();
-               else if (!prefixcmp(command_buf.buf, "option "))
+               else if (skip_prefix(command_buf.buf, "feature ", &v))
+                       parse_feature(v);
+               else if (skip_prefix(command_buf.buf, "option git ", &v))
+                       parse_option(v);
+               else if (starts_with(command_buf.buf, "option "))
                        /* ignore non-git options*/;
                else
                        die("Unsupported command: %s", command_buf.buf);