Added option to export the marks table when fast-import terminates.
[gitweb.git] / fast-import.c
index 1c74b90c84f1c100cbf593a0e7bcd1fdafe40feb..d61da3adecd3c2725a05d65728f09ce5eb5c8c4d 100644 (file)
@@ -4,7 +4,6 @@ Format of STDIN stream:
   stream ::= cmd*;
 
   cmd ::= new_blob
-        | new_branch
         | new_commit
         | new_tag
         ;
@@ -14,15 +13,12 @@ Format of STDIN stream:
     file_content;
   file_content ::= data;
 
-  new_branch ::= 'branch' sp ref_str lf
-    ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
-    lf;
-
   new_commit ::= 'commit' sp ref_str lf
-       mark?
-       ('author' sp name '<' email '>' ts tz lf)?
-       'committer' sp name '<' email '>' ts tz lf
-       commit_msg
+    mark?
+    ('author' sp name '<' email '>' ts tz lf)?
+    'committer' sp name '<' email '>' ts tz lf
+    commit_msg
+    ('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
     file_change*
     lf;
   commit_msg ::= data;
@@ -227,6 +223,7 @@ static unsigned int object_entry_alloc = 1000;
 static struct object_entry_pool *blocks;
 static struct object_entry *object_table[1 << 16];
 static struct mark_set *marks;
+static const char* mark_file;
 
 /* Our last blob */
 static struct last_object last_blob;
@@ -342,6 +339,9 @@ static void* pool_alloc(size_t len)
        }
 
        r = p->next_free;
+       /* round out to a pointer alignment */
+       if (len & (sizeof(void*) - 1))
+               len += sizeof(void*) - (len & (sizeof(void*) - 1));
        p->next_free += len;
        return r;
 }
@@ -517,6 +517,7 @@ static struct tree_entry* new_tree_entry()
 
        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));
                while (n--) {
                        *((void**)e) = e + 1;
@@ -828,7 +829,7 @@ static void load_tree(struct tree_entry *root)
        } else {
                char type[20];
                buf = read_sha1_file(root->sha1, type, &size);
-               if (!buf || !strcmp(type, tree_type))
+               if (!buf || strcmp(type, tree_type))
                        die("Can't load tree %s", sha1_to_hex(root->sha1));
        }
 
@@ -1122,6 +1123,36 @@ static void dump_tags()
        }
 }
 
+static void dump_marks_helper(FILE *f,
+       unsigned long base,
+       struct mark_set *m)
+{
+       int k;
+       if (m->shift) {
+               for (k = 0; k < 1024; k++) {
+                       if (m->data.sets[k])
+                               dump_marks_helper(f, (base + k) << m->shift,
+                                       m->data.sets[k]);
+               }
+       } else {
+               for (k = 0; k < 1024; k++) {
+                       if (m->data.marked[k])
+                               fprintf(f, "%lu,%s\n", base + k,
+                                       sha1_to_hex(m->data.marked[k]->sha1));
+               }
+       }
+}
+
+static void dump_marks()
+{
+       if (mark_file)
+       {
+               FILE *f = fopen(mark_file, "w");
+               dump_marks_helper(f, 0, marks);
+               fclose(f);
+       }
+}
+
 static void read_next_command()
 {
        read_line(&command_buf, stdin, '\n');
@@ -1296,6 +1327,69 @@ static void file_change_d(struct branch *b)
                free(p_uq);
 }
 
+static void cmd_from(struct branch *b)
+{
+       const char *from, *endp;
+       char *str_uq;
+       struct branch *s;
+
+       if (strncmp("from ", command_buf.buf, 5))
+               return;
+
+       if (b->last_commit)
+               die("Can't reinitailize branch %s", b->name);
+
+       from = strchr(command_buf.buf, ' ') + 1;
+       str_uq = unquote_c_style(from, &endp);
+       if (str_uq) {
+               if (*endp)
+                       die("Garbage after string in: %s", command_buf.buf);
+               from = str_uq;
+       }
+
+       s = lookup_branch(from);
+       if (b == s)
+               die("Can't create a branch from itself: %s", b->name);
+       else if (s) {
+               memcpy(b->sha1, s->sha1, 20);
+               memcpy(b->branch_tree.sha1, s->branch_tree.sha1, 20);
+       } else if (*from == ':') {
+               unsigned long idnum = strtoul(from + 1, NULL, 10);
+               struct object_entry *oe = find_mark(idnum);
+               unsigned long size;
+               char *buf;
+               if (oe->type != OBJ_COMMIT)
+                       die("Mark :%lu not a commit", idnum);
+               memcpy(b->sha1, oe->sha1, 20);
+               buf = unpack_entry(oe->offset, &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.sha1))
+                       die("The commit %s is corrupt", sha1_to_hex(b->sha1));
+               free(buf);
+       } else if (!get_sha1(from, b->sha1)) {
+               if (!memcmp(b->sha1, null_sha1, 20))
+                       memcpy(b->branch_tree.sha1, null_sha1, 20);
+               else {
+                       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.sha1))
+                               die("The commit %s is corrupt", sha1_to_hex(b->sha1));
+                       free(buf);
+               }
+       } else
+               die("Invalid ref name or SHA1 expression: %s", from);
+
+       read_next_command();
+}
+
 static void cmd_new_commit()
 {
        struct branch *b;
@@ -1318,7 +1412,7 @@ static void cmd_new_commit()
        }
        b = lookup_branch(sp);
        if (!b)
-               die("Branch not declared: %s", sp);
+               b = new_branch(sp);
        if (str_uq)
                free(str_uq);
 
@@ -1335,6 +1429,8 @@ static void cmd_new_commit()
        if (!committer)
                die("Expected committer but didn't get one");
        msg = cmd_data(&msglen);
+       read_next_command();
+       cmd_from(b);
 
        /* ensure the branch is active/loaded */
        if (!b->branch_tree.tree || !max_active_branches) {
@@ -1344,7 +1440,6 @@ static void cmd_new_commit()
 
        /* file_change* */
        for (;;) {
-               read_next_command();
                if (1 == command_buf.len)
                        break;
                else if (!strncmp("M ", command_buf.buf, 2))
@@ -1353,6 +1448,7 @@ static void cmd_new_commit()
                        file_change_d(b);
                else
                        die("Unsupported file_change: %s", command_buf.buf);
+               read_next_command();
        }
 
        /* build the tree and the commit */
@@ -1382,91 +1478,6 @@ static void cmd_new_commit()
        b->last_commit = object_count_by_type[OBJ_COMMIT];
 }
 
-static void cmd_new_branch()
-{
-       struct branch *b;
-       char *str_uq;
-       const char *endp;
-       char *sp;
-
-       /* Obtain the new branch name from the rest of our command */
-       sp = strchr(command_buf.buf, ' ') + 1;
-       str_uq = unquote_c_style(sp, &endp);
-       if (str_uq) {
-               if (*endp)
-                       die("Garbage after ref in: %s", command_buf.buf);
-               sp = str_uq;
-       }
-       b = new_branch(sp);
-       if (str_uq)
-               free(str_uq);
-       read_next_command();
-
-       /* from ... */
-       if (!strncmp("from ", command_buf.buf, 5)) {
-               const char *from;
-               struct branch *s;
-
-               from = strchr(command_buf.buf, ' ') + 1;
-               str_uq = unquote_c_style(from, &endp);
-               if (str_uq) {
-                       if (*endp)
-                               die("Garbage after string in: %s", command_buf.buf);
-                       from = str_uq;
-               }
-
-               s = lookup_branch(from);
-               if (b == s)
-                       die("Can't create a branch from itself: %s", b->name);
-               else if (s) {
-                       memcpy(b->sha1, s->sha1, 20);
-                       memcpy(b->branch_tree.sha1, s->branch_tree.sha1, 20);
-               } else if (*from == ':') {
-                       unsigned long idnum = strtoul(from + 1, NULL, 10);
-                       struct object_entry *oe = find_mark(idnum);
-                       unsigned long size;
-                       char *buf;
-                       if (oe->type != OBJ_COMMIT)
-                               die("Mark :%lu not a commit", idnum);
-                       memcpy(b->sha1, oe->sha1, 20);
-                       buf = unpack_entry(oe->offset, &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.sha1))
-                               die("The commit %s is corrupt", sha1_to_hex(b->sha1));
-                       free(buf);
-               } else if (!get_sha1(from, b->sha1)) {
-                       if (!memcmp(b->sha1, null_sha1, 20))
-                               memcpy(b->branch_tree.sha1, null_sha1, 20);
-                       else {
-                               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.sha1))
-                                       die("The commit %s is corrupt", sha1_to_hex(b->sha1));
-                               free(buf);
-                       }
-               } else
-                       die("Invalid ref name or SHA1 expression: %s", from);
-
-               if (str_uq)
-                       free(str_uq);
-               read_next_command();
-       } else {
-               memcpy(b->sha1, null_sha1, 20);
-               memcpy(b->branch_tree.sha1, null_sha1, 20);
-       }
-
-       if (command_buf.eof || command_buf.len > 1)
-               die("An lf did not terminate the branch command as expected.");
-}
-
 static void cmd_new_tag()
 {
        char *str_uq;
@@ -1564,7 +1575,7 @@ static void cmd_new_tag()
 }
 
 static const char fast_import_usage[] =
-"git-fast-import [--objects=n] [--depth=n] [--active-branches=n] temp.pack";
+"git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] temp.pack";
 
 int main(int argc, const char **argv)
 {
@@ -1589,6 +1600,8 @@ int main(int argc, const char **argv)
                        max_depth = strtoul(a + 8, NULL, 0);
                else if (!strncmp(a, "--active-branches=", 18))
                        max_active_branches = strtoul(a + 18, NULL, 0);
+               else if (!strncmp(a, "--export-marks=", 15))
+                       mark_file = a + 15;
                else
                        die("unknown option %s", a);
        }
@@ -1620,8 +1633,6 @@ int main(int argc, const char **argv)
                        break;
                else if (!strcmp("blob", command_buf.buf))
                        cmd_new_blob();
-               else if (!strncmp("branch ", command_buf.buf, 7))
-                       cmd_new_branch();
                else if (!strncmp("commit ", command_buf.buf, 7))
                        cmd_new_commit();
                else if (!strncmp("tag ", command_buf.buf, 4))
@@ -1635,6 +1646,7 @@ int main(int argc, const char **argv)
        write_index(idx_name);
        dump_branches();
        dump_tags();
+       dump_marks();
 
        fprintf(stderr, "%s statistics:\n", argv[0]);
        fprintf(stderr, "---------------------------------------------------\n");