Document new --date=<format>
[gitweb.git] / fast-import.c
index 5d040fdb001c05daab19d4b6e239512cbdb07aba..a1cb13f09b1d661c6fff75d51378e3e63d211f20 100644 (file)
@@ -26,9 +26,10 @@ Format of STDIN stream:
     lf;
   commit_msg ::= data;
 
-  file_change ::= file_clr | file_del | file_obm | file_inm;
+  file_change ::= file_clr | file_del | file_rnm | file_obm | file_inm;
   file_clr ::= 'deleteall' lf;
   file_del ::= 'D' sp path_str lf;
+  file_rnm ::= 'R' sp path_str sp path_str lf;
   file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
   file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
     data;
@@ -133,10 +134,6 @@ Format of STDIN stream:
 #define PACK_ID_BITS 16
 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
 
-#ifndef PRIuMAX
-#define PRIuMAX "llu"
-#endif
-
 struct object_entry
 {
        struct object_entry *next;
@@ -220,7 +217,8 @@ struct branch
        const char *name;
        struct tree_entry branch_tree;
        uintmax_t last_commit;
-       unsigned int pack_id;
+       unsigned active : 1;
+       unsigned pack_id : PACK_ID_BITS;
        unsigned char sha1[20];
 };
 
@@ -252,7 +250,7 @@ typedef enum {
 
 /* Configured limits on output */
 static unsigned long max_depth = 10;
-static unsigned long max_packsize = (1LL << 32) - 1;
+static off_t max_packsize = (1LL << 32) - 1;
 static int force_update;
 
 /* Stats and misc. counters */
@@ -528,6 +526,7 @@ static struct branch *new_branch(const char *name)
        b->table_next_branch = branch_table[hc];
        b->branch_tree.versions[0].mode = S_IFDIR;
        b->branch_tree.versions[1].mode = S_IFDIR;
+       b->active = 0;
        b->pack_id = MAX_PACK_ID;
        branch_table[hc] = b;
        branch_count++;
@@ -632,7 +631,7 @@ static void start_packfile(void)
        int pack_fd;
 
        snprintf(tmpfile, sizeof(tmpfile),
-               "%s/pack_XXXXXX", get_object_directory());
+               "%s/tmp_pack_XXXXXX", get_object_directory());
        pack_fd = mkstemp(tmpfile);
        if (pack_fd < 0)
                die("Can't create %s: %s", tmpfile, strerror(errno));
@@ -653,42 +652,6 @@ static void start_packfile(void)
        all_packs[pack_id] = p;
 }
 
-static void fixup_header_footer(void)
-{
-       static const int buf_sz = 128 * 1024;
-       int pack_fd = pack_data->pack_fd;
-       SHA_CTX c;
-       struct pack_header hdr;
-       char *buf;
-
-       if (lseek(pack_fd, 0, SEEK_SET) != 0)
-               die("Failed seeking to start: %s", strerror(errno));
-       if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
-               die("Unable to reread header of %s", pack_data->pack_name);
-       if (lseek(pack_fd, 0, SEEK_SET) != 0)
-               die("Failed seeking to start: %s", strerror(errno));
-       hdr.hdr_entries = htonl(object_count);
-       write_or_die(pack_fd, &hdr, sizeof(hdr));
-
-       SHA1_Init(&c);
-       SHA1_Update(&c, &hdr, sizeof(hdr));
-
-       buf = xmalloc(buf_sz);
-       for (;;) {
-               size_t n = xread(pack_fd, buf, buf_sz);
-               if (!n)
-                       break;
-               if (n < 0)
-                       die("Failed to checksum %s", pack_data->pack_name);
-               SHA1_Update(&c, buf, n);
-       }
-       free(buf);
-
-       SHA1_Final(pack_data->sha1, &c);
-       write_or_die(pack_fd, pack_data->sha1, sizeof(pack_data->sha1));
-       close(pack_fd);
-}
-
 static int oecmp (const void *a_, const void *b_)
 {
        struct object_entry *a = *((struct object_entry**)a_);
@@ -732,7 +695,7 @@ static char *create_index(void)
        }
 
        snprintf(tmpfile, sizeof(tmpfile),
-               "%s/index_XXXXXX", get_object_directory());
+               "%s/tmp_idx_XXXXXX", get_object_directory());
        idx_fd = mkstemp(tmpfile);
        if (idx_fd < 0)
                die("Can't create %s: %s", tmpfile, strerror(errno));
@@ -755,7 +718,7 @@ static char *create_index(void)
 static char *keep_pack(char *curr_index_name)
 {
        static char name[PATH_MAX];
-       static char *keep_msg = "fast-import";
+       static const char *keep_msg = "fast-import";
        int keep_fd;
 
        chmod(pack_data->pack_name, 0444);
@@ -804,7 +767,9 @@ static void end_packfile(void)
                struct branch *b;
                struct tag *t;
 
-               fixup_header_footer();
+               fixup_pack_header_footer(pack_data->pack_fd, pack_data->sha1,
+                                   pack_data->pack_name, object_count);
+               close(pack_data->pack_fd);
                idx_name = keep_pack(create_index());
 
                /* Register the packfile with core git's machinary. */
@@ -891,7 +856,7 @@ static int store_object(
        SHA_CTX c;
        z_stream s;
 
-       hdrlen = sprintf((char*)hdr,"%s %lu", type_names[type],
+       hdrlen = sprintf((char*)hdr,"%s %lu", typename(type),
                (unsigned long)datlen) + 1;
        SHA1_Init(&c);
        SHA1_Update(&c, hdr, hdrlen);
@@ -906,6 +871,12 @@ static int store_object(
        if (e->offset) {
                duplicate_count_by_type[type]++;
                return 1;
+       } else if (find_sha1_pack(sha1, packed_git)) {
+               e->type = type;
+               e->pack_id = MAX_PACK_ID;
+               e->offset = 1; /* just not zero! */
+               duplicate_count_by_type[type]++;
+               return 1;
        }
 
        if (last && last->data && last->depth < max_depth) {
@@ -1008,11 +979,11 @@ static void *gfi_unpack_entry(
        struct object_entry *oe,
        unsigned long *sizep)
 {
-       static char type[20];
+       enum object_type type;
        struct packed_git *p = all_packs[oe->pack_id];
        if (p == pack_data)
                p->pack_size = pack_size + 20;
-       return unpack_entry(p, oe->offset, type, sizep);
+       return unpack_entry(p, oe->offset, &type, sizep);
 }
 
 static const char *get_mode(const char *str, uint16_t *modep)
@@ -1043,15 +1014,15 @@ static void load_tree(struct tree_entry *root)
                return;
 
        myoe = find_object(sha1);
-       if (myoe) {
+       if (myoe && myoe->pack_id != MAX_PACK_ID) {
                if (myoe->type != OBJ_TREE)
                        die("Not a tree: %s", sha1_to_hex(sha1));
                t->delta_depth = 0;
                buf = gfi_unpack_entry(myoe, &size);
        } else {
-               char type[20];
-               buf = read_sha1_file(sha1, type, &size);
-               if (!buf || strcmp(type, tree_type))
+               enum object_type type;
+               buf = read_sha1_file(sha1, &type, &size);
+               if (!buf || type != OBJ_TREE)
                        die("Can't load tree %s", sha1_to_hex(sha1));
        }
 
@@ -1060,7 +1031,7 @@ static void load_tree(struct tree_entry *root)
                struct tree_entry *e = new_tree_entry();
 
                if (t->entry_count == t->entry_capacity)
-                       root->tree = t = grow_tree_content(t, 8);
+                       root->tree = t = grow_tree_content(t, t->entry_count);
                t->entries[t->entry_count++] = e;
 
                e->tree = NULL;
@@ -1068,7 +1039,7 @@ static void load_tree(struct tree_entry *root)
                if (!c)
                        die("Corrupt mode in %s", sha1_to_hex(sha1));
                e->versions[0].mode = e->versions[1].mode;
-               e->name = to_atom(c, (unsigned short)strlen(c));
+               e->name = to_atom(c, strlen(c));
                c += e->name->str_len + 1;
                hashcpy(e->versions[0].sha1, (unsigned char*)c);
                hashcpy(e->versions[1].sha1, (unsigned char*)c);
@@ -1152,6 +1123,7 @@ static void store_tree(struct tree_entry *root)
                || le->pack_id != pack_id) {
                lo.data = NULL;
                lo.depth = 0;
+               lo.no_free = 0;
        } else {
                mktree(t, 0, &lo.len, &old_tree);
                lo.data = old_tree.buffer;
@@ -1183,7 +1155,8 @@ static int tree_content_set(
        struct tree_entry *root,
        const char *p,
        const unsigned char *sha1,
-       const uint16_t mode)
+       const uint16_t mode,
+       struct tree_content *subtree)
 {
        struct tree_content *t = root->tree;
        const char *slash1;
@@ -1195,20 +1168,24 @@ static int tree_content_set(
                n = slash1 - p;
        else
                n = strlen(p);
+       if (!n)
+               die("Empty path component found in input");
+       if (!slash1 && !S_ISDIR(mode) && subtree)
+               die("Non-directories cannot have subtrees");
 
        for (i = 0; i < t->entry_count; i++) {
                e = t->entries[i];
                if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
                        if (!slash1) {
-                               if (e->versions[1].mode == mode
+                               if (!S_ISDIR(mode)
+                                               && e->versions[1].mode == mode
                                                && !hashcmp(e->versions[1].sha1, sha1))
                                        return 0;
                                e->versions[1].mode = mode;
                                hashcpy(e->versions[1].sha1, sha1);
-                               if (e->tree) {
+                               if (e->tree)
                                        release_tree_content_recursive(e->tree);
-                                       e->tree = NULL;
-                               }
+                               e->tree = subtree;
                                hashclr(root->versions[1].sha1);
                                return 1;
                        }
@@ -1218,7 +1195,7 @@ static int tree_content_set(
                        }
                        if (!e->tree)
                                load_tree(e);
-                       if (tree_content_set(e, slash1 + 1, sha1, mode)) {
+                       if (tree_content_set(e, slash1 + 1, sha1, mode, subtree)) {
                                hashclr(root->versions[1].sha1);
                                return 1;
                        }
@@ -1227,18 +1204,18 @@ static int tree_content_set(
        }
 
        if (t->entry_count == t->entry_capacity)
-               root->tree = t = grow_tree_content(t, 8);
+               root->tree = t = grow_tree_content(t, t->entry_count);
        e = new_tree_entry();
-       e->name = to_atom(p, (unsigned short)n);
+       e->name = to_atom(p, n);
        e->versions[0].mode = 0;
        hashclr(e->versions[0].sha1);
        t->entries[t->entry_count++] = e;
        if (slash1) {
                e->tree = new_tree_content(8);
                e->versions[1].mode = S_IFDIR;
-               tree_content_set(e, slash1 + 1, sha1, mode);
+               tree_content_set(e, slash1 + 1, sha1, mode, subtree);
        } else {
-               e->tree = NULL;
+               e->tree = subtree;
                e->versions[1].mode = mode;
                hashcpy(e->versions[1].sha1, sha1);
        }
@@ -1246,7 +1223,10 @@ static int tree_content_set(
        return 1;
 }
 
-static int tree_content_remove(struct tree_entry *root, const char *p)
+static int tree_content_remove(
+       struct tree_entry *root,
+       const char *p,
+       struct tree_entry *backup_leaf)
 {
        struct tree_content *t = root->tree;
        const char *slash1;
@@ -1266,13 +1246,14 @@ static int tree_content_remove(struct tree_entry *root, const char *p)
                                goto del_entry;
                        if (!e->tree)
                                load_tree(e);
-                       if (tree_content_remove(e, slash1 + 1)) {
+                       if (tree_content_remove(e, slash1 + 1, backup_leaf)) {
                                for (n = 0; n < e->tree->entry_count; n++) {
                                        if (e->tree->entries[n]->versions[1].mode) {
                                                hashclr(root->versions[1].sha1);
                                                return 1;
                                        }
                                }
+                               backup_leaf = NULL;
                                goto del_entry;
                        }
                        return 0;
@@ -1281,10 +1262,11 @@ static int tree_content_remove(struct tree_entry *root, const char *p)
        return 0;
 
 del_entry:
-       if (e->tree) {
+       if (backup_leaf)
+               memcpy(backup_leaf, e, sizeof(*backup_leaf));
+       else if (e->tree)
                release_tree_content_recursive(e->tree);
-               e->tree = NULL;
-       }
+       e->tree = NULL;
        e->versions[1].mode = 0;
        hashclr(e->versions[1].sha1);
        hashclr(root->versions[1].sha1);
@@ -1299,7 +1281,7 @@ static int update_branch(struct branch *b)
 
        if (read_ref(b->name, old_sha1))
                hashclr(old_sha1);
-       lock = lock_any_ref_for_update(b->name, 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)) {
@@ -1314,7 +1296,7 @@ static int update_branch(struct branch *b)
 
                if (!in_merge_bases(old_cmit, &new_cmit, 1)) {
                        unlock_ref(lock);
-                       warn("Not updating %s"
+                       warning("Not updating %s"
                                " (new tip %s does not contain %s)",
                                b->name, sha1_to_hex(b->sha1), sha1_to_hex(old_sha1));
                        return -1;
@@ -1373,16 +1355,33 @@ static void dump_marks_helper(FILE *f,
 
 static void dump_marks(void)
 {
-       if (mark_file)
-       {
-               FILE *f = fopen(mark_file, "w");
-               if (f) {
-                       dump_marks_helper(f, 0, marks);
-                       fclose(f);
-               } else
-                       failure |= error("Unable to write marks file %s: %s",
-                               mark_file, strerror(errno));
+       static struct lock_file mark_lock;
+       int mark_fd;
+       FILE *f;
+
+       if (!mark_file)
+               return;
+
+       mark_fd = hold_lock_file_for_update(&mark_lock, mark_file, 0);
+       if (mark_fd < 0) {
+               failure |= error("Unable to write marks file %s: %s",
+                       mark_file, strerror(errno));
+               return;
        }
+
+       f = fdopen(mark_fd, "w");
+       if (!f) {
+               rollback_lock_file(&mark_lock);
+               failure |= error("Unable to write marks file %s: %s",
+                       mark_file, strerror(errno));
+               return;
+       }
+
+       dump_marks_helper(f, 0, marks);
+       fclose(f);
+       if (commit_lock_file(&mark_lock))
+               failure |= error("Unable to write marks file %s: %s",
+                       mark_file, strerror(errno));
 }
 
 static void read_next_command(void)
@@ -1529,7 +1528,7 @@ static void unload_one_branch(void)
 {
        while (cur_active_branches
                && cur_active_branches >= max_active_branches) {
-               unsigned long min_commit = ULONG_MAX;
+               uintmax_t min_commit = ULONG_MAX;
                struct branch *e, *l = NULL, *p = NULL;
 
                for (e = active_branches; e; e = e->active_next_branch) {
@@ -1547,6 +1546,7 @@ static void unload_one_branch(void)
                        e = active_branches;
                        active_branches = e->active_next_branch;
                }
+               e->active = 0;
                e->active_next_branch = NULL;
                if (e->branch_tree.tree) {
                        release_tree_content_recursive(e->branch_tree.tree);
@@ -1559,10 +1559,13 @@ static void unload_one_branch(void)
 static void load_branch(struct branch *b)
 {
        load_tree(&b->branch_tree);
-       b->active_next_branch = active_branches;
-       active_branches = b;
-       cur_active_branches++;
-       branch_load_count++;
+       if (!b->active) {
+               b->active = 1;
+               b->active_next_branch = active_branches;
+               active_branches = b;
+               cur_active_branches++;
+               branch_load_count++;
+       }
 }
 
 static void file_change_m(struct branch *b)
@@ -1573,7 +1576,6 @@ static void file_change_m(struct branch *b)
        struct object_entry *oe = oe;
        unsigned char sha1[20];
        uint16_t mode, inline_data = 0;
-       char type[20];
 
        p = get_mode(p, &mode);
        if (!p)
@@ -1626,16 +1628,17 @@ static void file_change_m(struct branch *b)
        } else if (oe) {
                if (oe->type != OBJ_BLOB)
                        die("Not a blob (actually a %s): %s",
-                               command_buf.buf, type_names[oe->type]);
+                               command_buf.buf, typename(oe->type));
        } else {
-               if (sha1_object_info(sha1, type, NULL))
+               enum object_type type = sha1_object_info(sha1, NULL);
+               if (type < 0)
                        die("Blob not found: %s", command_buf.buf);
-               if (strcmp(blob_type, type))
+               if (type != OBJ_BLOB)
                        die("Not a blob (actually a %s): %s",
-                               command_buf.buf, type);
+                           typename(type), command_buf.buf);
        }
 
-       tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
+       tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode, NULL);
        free(p_uq);
 }
 
@@ -1651,10 +1654,58 @@ static void file_change_d(struct branch *b)
                        die("Garbage after path in: %s", command_buf.buf);
                p = p_uq;
        }
-       tree_content_remove(&b->branch_tree, p);
+       tree_content_remove(&b->branch_tree, p, NULL);
        free(p_uq);
 }
 
+static void file_change_r(struct branch *b)
+{
+       const char *s, *d;
+       char *s_uq, *d_uq;
+       const char *endp;
+       struct tree_entry leaf;
+
+       s = command_buf.buf + 2;
+       s_uq = unquote_c_style(s, &endp);
+       if (s_uq) {
+               if (*endp != ' ')
+                       die("Missing space after source: %s", command_buf.buf);
+       }
+       else {
+               endp = strchr(s, ' ');
+               if (!endp)
+                       die("Missing space after source: %s", command_buf.buf);
+               s_uq = xmalloc(endp - s + 1);
+               memcpy(s_uq, s, endp - s);
+               s_uq[endp - s] = 0;
+       }
+       s = s_uq;
+
+       endp++;
+       if (!*endp)
+               die("Missing dest: %s", command_buf.buf);
+
+       d = endp;
+       d_uq = unquote_c_style(d, &endp);
+       if (d_uq) {
+               if (*endp)
+                       die("Garbage after dest in: %s", command_buf.buf);
+               d = d_uq;
+       }
+
+       memset(&leaf, 0, sizeof(leaf));
+       tree_content_remove(&b->branch_tree, s, &leaf);
+       if (!leaf.versions[1].mode)
+               die("Path %s not in branch", s);
+       tree_content_set(&b->branch_tree, d,
+               leaf.versions[1].sha1,
+               leaf.versions[1].mode,
+               leaf.tree);
+
+       free(s_uq);
+       free(d_uq);
+}
+
 static void file_change_deleteall(struct branch *b)
 {
        release_tree_content_recursive(b->branch_tree.tree);
@@ -1663,6 +1714,33 @@ static void file_change_deleteall(struct branch *b)
        load_tree(&b->branch_tree);
 }
 
+static void cmd_from_commit(struct branch *b, char *buf, unsigned long size)
+{
+       if (!buf || size < 46)
+               die("Not a valid commit: %s", sha1_to_hex(b->sha1));
+       if (memcmp("tree ", buf, 5)
+               || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
+               die("The commit %s is corrupt", sha1_to_hex(b->sha1));
+       hashcpy(b->branch_tree.versions[0].sha1,
+               b->branch_tree.versions[1].sha1);
+}
+
+static void cmd_from_existing(struct branch *b)
+{
+       if (is_null_sha1(b->sha1)) {
+               hashclr(b->branch_tree.versions[0].sha1);
+               hashclr(b->branch_tree.versions[1].sha1);
+       } else {
+               unsigned long size;
+               char *buf;
+
+               buf = read_object_with_reference(b->sha1,
+                       commit_type, &size, b->sha1);
+               cmd_from_commit(b, buf, size);
+               free(buf);
+       }
+}
+
 static void cmd_from(struct branch *b)
 {
        const char *from;
@@ -1688,40 +1766,19 @@ static void cmd_from(struct branch *b)
        } else if (*from == ':') {
                uintmax_t idnum = strtoumax(from + 1, NULL, 10);
                struct object_entry *oe = find_mark(idnum);
-               unsigned long size;
-               char *buf;
                if (oe->type != OBJ_COMMIT)
                        die("Mark :%" PRIuMAX " not a commit", idnum);
                hashcpy(b->sha1, oe->sha1);
-               buf = gfi_unpack_entry(oe, &size);
-               if (!buf || size < 46)
-                       die("Not a valid commit: %s", from);
-               if (memcmp("tree ", buf, 5)
-                       || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
-                       die("The commit %s is corrupt", sha1_to_hex(b->sha1));
-               free(buf);
-               hashcpy(b->branch_tree.versions[0].sha1,
-                       b->branch_tree.versions[1].sha1);
-       } else if (!get_sha1(from, b->sha1)) {
-               if (is_null_sha1(b->sha1)) {
-                       hashclr(b->branch_tree.versions[0].sha1);
-                       hashclr(b->branch_tree.versions[1].sha1);
-               } else {
+               if (oe->pack_id != MAX_PACK_ID) {
                        unsigned long size;
-                       char *buf;
-
-                       buf = read_object_with_reference(b->sha1,
-                               type_names[OBJ_COMMIT], &size, b->sha1);
-                       if (!buf || size < 46)
-                               die("Not a valid commit: %s", from);
-                       if (memcmp("tree ", buf, 5)
-                               || get_sha1_hex(buf + 5, b->branch_tree.versions[1].sha1))
-                               die("The commit %s is corrupt", sha1_to_hex(b->sha1));
+                       char *buf = gfi_unpack_entry(oe, &size);
+                       cmd_from_commit(b, buf, size);
                        free(buf);
-                       hashcpy(b->branch_tree.versions[0].sha1,
-                               b->branch_tree.versions[1].sha1);
-               }
-       } else
+               } else
+                       cmd_from_existing(b);
+       } else if (!get_sha1(from, b->sha1))
+               cmd_from_existing(b);
+       else
                die("Invalid ref name or SHA1 expression: %s", from);
 
        read_next_command();
@@ -1746,7 +1803,14 @@ static struct hash_list *cmd_merge(unsigned int *count)
                        if (oe->type != OBJ_COMMIT)
                                die("Mark :%" PRIuMAX " not a commit", idnum);
                        hashcpy(n->sha1, oe->sha1);
-               } else if (get_sha1(from, n->sha1))
+               } else if (!get_sha1(from, n->sha1)) {
+                       unsigned long size;
+                       char *buf = read_object_with_reference(n->sha1,
+                               commit_type, &size, n->sha1);
+                       if (!buf || size < 46)
+                               die("Not a valid commit: %s", from);
+                       free(buf);
+               } else
                        die("Invalid ref name or SHA1 expression: %s", from);
 
                n->next = NULL;
@@ -1809,6 +1873,8 @@ static void cmd_new_commit(void)
                        file_change_m(b);
                else if (!prefixcmp(command_buf.buf, "D "))
                        file_change_d(b);
+               else if (!prefixcmp(command_buf.buf, "R "))
+                       file_change_r(b);
                else if (!strcmp("deleteall", command_buf.buf))
                        file_change_deleteall(b);
                else
@@ -1895,7 +1961,7 @@ static void cmd_new_tag(void)
                char *buf;
 
                buf = read_object_with_reference(sha1,
-                       type_names[OBJ_COMMIT], &size, sha1);
+                       commit_type, &size, sha1);
                if (!buf || size < 46)
                        die("Not a valid commit: %s", from);
                free(buf);
@@ -1916,7 +1982,7 @@ static void cmd_new_tag(void)
        size_dbuf(&new_data, 67+strlen(t->name)+strlen(tagger)+msglen);
        sp = new_data.buffer;
        sp += sprintf(sp, "object %s\n", sha1_to_hex(sha1));
-       sp += sprintf(sp, "type %s\n", type_names[OBJ_COMMIT]);
+       sp += sprintf(sp, "type %s\n", commit_type);
        sp += sprintf(sp, "tag %s\n", t->name);
        sp += sprintf(sp, "tagger %s\n", tagger);
        *sp++ = '\n';
@@ -1967,6 +2033,41 @@ static void cmd_checkpoint(void)
        read_next_command();
 }
 
+static void import_marks(const char *input_file)
+{
+       char line[512];
+       FILE *f = fopen(input_file, "r");
+       if (!f)
+               die("cannot read %s: %s", input_file, strerror(errno));
+       while (fgets(line, sizeof(line), f)) {
+               uintmax_t mark;
+               char *end;
+               unsigned char sha1[20];
+               struct object_entry *e;
+
+               end = strchr(line, '\n');
+               if (line[0] != ':' || !end)
+                       die("corrupt mark line: %s", line);
+               *end = 0;
+               mark = strtoumax(line + 1, &end, 10);
+               if (!mark || end == line + 1
+                       || *end != ' ' || get_sha1(end + 1, sha1))
+                       die("corrupt mark line: %s", line);
+               e = find_object(sha1);
+               if (!e) {
+                       enum object_type type = sha1_object_info(sha1, NULL);
+                       if (type < 0)
+                               die("object not found: %s", sha1_to_hex(sha1));
+                       e = insert_object(sha1);
+                       e->type = type;
+                       e->pack_id = MAX_PACK_ID;
+                       e->offset = 1; /* just not zero! */
+               }
+               insert_mark(mark, e);
+       }
+       fclose(f);
+}
+
 static const char fast_import_usage[] =
 "git-fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
 
@@ -1975,6 +2076,12 @@ int main(int argc, const char **argv)
        int i, show_stats = 1;
 
        git_config(git_default_config);
+       alloc_objects(object_entry_alloc);
+       strbuf_init(&command_buf);
+       atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
+       branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
+       avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
+       marks = pool_calloc(1, sizeof(struct mark_set));
 
        for (i = 1; i < argc; i++) {
                const char *a = argv[i];
@@ -1998,6 +2105,8 @@ int main(int argc, const char **argv)
                        max_depth = strtoul(a + 8, NULL, 0);
                else if (!prefixcmp(a, "--active-branches="))
                        max_active_branches = strtoul(a + 18, NULL, 0);
+               else if (!prefixcmp(a, "--import-marks="))
+                       import_marks(a + 15);
                else if (!prefixcmp(a, "--export-marks="))
                        mark_file = a + 15;
                else if (!prefixcmp(a, "--export-pack-edges=")) {
@@ -2018,14 +2127,7 @@ int main(int argc, const char **argv)
        if (i != argc)
                usage(fast_import_usage);
 
-       alloc_objects(object_entry_alloc);
-       strbuf_init(&command_buf);
-
-       atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
-       branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
-       avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
-       marks = pool_calloc(1, sizeof(struct mark_set));
-
+       prepare_packed_git();
        start_packfile();
        for (;;) {
                read_next_command();