diff --cached: give more sensible error message when HEAD is yet to be created.
[gitweb.git] / fast-import.c
index ee4777fdafcbedc24997155581e55c339b40981e..5d040fdb001c05daab19d4b6e239512cbdb07aba 100644 (file)
@@ -26,7 +26,8 @@ Format of STDIN stream:
     lf;
   commit_msg ::= data;
 
-  file_change ::= file_del | file_obm | file_inm;
+  file_change ::= file_clr | file_del | file_obm | file_inm;
+  file_clr ::= 'deleteall' lf;
   file_del ::= 'D' 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
@@ -81,7 +82,7 @@ Format of STDIN stream:
   path_str    ::= path    | '"' quoted(path)    '"' ;
   mode        ::= '100644' | '644'
                 | '100755' | '755'
-                | '140000'
+                | '120000'
                 ;
 
   declen ::= # unsigned 32 bit value, ascii base10 notation;
@@ -121,6 +122,7 @@ Format of STDIN stream:
 #include "object.h"
 #include "blob.h"
 #include "tree.h"
+#include "commit.h"
 #include "delta.h"
 #include "pack.h"
 #include "refs.h"
@@ -131,6 +133,10 @@ 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;
@@ -247,6 +253,7 @@ typedef enum {
 /* Configured limits on output */
 static unsigned long max_depth = 10;
 static unsigned long max_packsize = (1LL << 32) - 1;
+static int force_update;
 
 /* Stats and misc. counters */
 static uintmax_t alloc_count;
@@ -257,6 +264,8 @@ static uintmax_t delta_count_by_type[1 << TYPE_BITS];
 static unsigned long object_count;
 static unsigned long branch_count;
 static unsigned long branch_load_count;
+static int failure;
+static FILE *pack_edges;
 
 /* Memory pools */
 static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
@@ -470,7 +479,7 @@ static struct object_entry *find_mark(uintmax_t idnum)
                        oe = s->data.marked[idnum];
        }
        if (!oe)
-               die("mark :%ju not declared", orig_idnum);
+               die("mark :%" PRIuMAX " not declared", orig_idnum);
        return oe;
 }
 
@@ -807,18 +816,21 @@ static void end_packfile(void)
                install_packed_git(new_p);
 
                /* Print the boundary */
-               fprintf(stdout, "%s:", new_p->pack_name);
-               for (i = 0; i < branch_table_sz; i++) {
-                       for (b = branch_table[i]; b; b = b->table_next_branch) {
-                               if (b->pack_id == pack_id)
-                                       fprintf(stdout, " %s", sha1_to_hex(b->sha1));
+               if (pack_edges) {
+                       fprintf(pack_edges, "%s:", new_p->pack_name);
+                       for (i = 0; i < branch_table_sz; i++) {
+                               for (b = branch_table[i]; b; b = b->table_next_branch) {
+                                       if (b->pack_id == pack_id)
+                                               fprintf(pack_edges, " %s", sha1_to_hex(b->sha1));
+                               }
                        }
+                       for (t = first_tag; t; t = t->next_tag) {
+                               if (t->pack_id == pack_id)
+                                       fprintf(pack_edges, " %s", sha1_to_hex(t->sha1));
+                       }
+                       fputc('\n', pack_edges);
+                       fflush(pack_edges);
                }
-               for (t = first_tag; t; t = t->next_tag) {
-                       if (t->pack_id == pack_id)
-                               fprintf(stdout, " %s", sha1_to_hex(t->sha1));
-               }
-               fputc('\n', stdout);
 
                pack_id++;
        }
@@ -834,7 +846,7 @@ static void end_packfile(void)
        last_blob.depth = 0;
 }
 
-static void checkpoint(void)
+static void cycle_packfile(void)
 {
        end_packfile();
        start_packfile();
@@ -879,7 +891,8 @@ static int store_object(
        SHA_CTX c;
        z_stream s;
 
-       hdrlen = sprintf((char*)hdr,"%s %lu",type_names[type],datlen) + 1;
+       hdrlen = sprintf((char*)hdr,"%s %lu", type_names[type],
+               (unsigned long)datlen) + 1;
        SHA1_Init(&c);
        SHA1_Update(&c, hdr, hdrlen);
        SHA1_Update(&c, dat, datlen);
@@ -927,7 +940,7 @@ static int store_object(
 
                /* This new object needs to *not* have the current pack_id. */
                e->pack_id = pack_id + 1;
-               checkpoint();
+               cycle_packfile();
 
                /* We cannot carry a delta into the new pack. */
                if (delta) {
@@ -1278,19 +1291,48 @@ static int tree_content_remove(struct tree_entry *root, const char *p)
        return 1;
 }
 
-static void dump_branches(void)
+static int update_branch(struct branch *b)
 {
        static const char *msg = "fast-import";
+       struct ref_lock *lock;
+       unsigned char old_sha1[20];
+
+       if (read_ref(b->name, old_sha1))
+               hashclr(old_sha1);
+       lock = lock_any_ref_for_update(b->name, old_sha1);
+       if (!lock)
+               return error("Unable to lock %s", b->name);
+       if (!force_update && !is_null_sha1(old_sha1)) {
+               struct commit *old_cmit, *new_cmit;
+
+               old_cmit = lookup_commit_reference_gently(old_sha1, 0);
+               new_cmit = lookup_commit_reference_gently(b->sha1, 0);
+               if (!old_cmit || !new_cmit) {
+                       unlock_ref(lock);
+                       return error("Branch %s is missing commits.", b->name);
+               }
+
+               if (!in_merge_bases(old_cmit, &new_cmit, 1)) {
+                       unlock_ref(lock);
+                       warn("Not updating %s"
+                               " (new tip %s does not contain %s)",
+                               b->name, sha1_to_hex(b->sha1), sha1_to_hex(old_sha1));
+                       return -1;
+               }
+       }
+       if (write_ref_sha1(lock, b->sha1, msg) < 0)
+               return error("Unable to update %s", b->name);
+       return 0;
+}
+
+static void dump_branches(void)
+{
        unsigned int i;
        struct branch *b;
-       struct ref_lock *lock;
 
        for (i = 0; i < branch_table_sz; i++) {
-               for (b = branch_table[i]; b; b = b->table_next_branch) {
-                       lock = lock_any_ref_for_update(b->name, NULL);
-                       if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0)
-                               die("Can't write %s", b->name);
-               }
+               for (b = branch_table[i]; b; b = b->table_next_branch)
+                       failure |= update_branch(b);
        }
 }
 
@@ -1299,13 +1341,13 @@ static void dump_tags(void)
        static const char *msg = "fast-import";
        struct tag *t;
        struct ref_lock *lock;
-       char path[PATH_MAX];
+       char ref_name[PATH_MAX];
 
        for (t = first_tag; t; t = t->next_tag) {
-               sprintf(path, "refs/tags/%s", t->name);
-               lock = lock_any_ref_for_update(path, NULL);
+               sprintf(ref_name, "tags/%s", t->name);
+               lock = lock_ref_sha1(ref_name, NULL);
                if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
-                       die("Can't write %s", path);
+                       failure |= error("Unable to update %s", ref_name);
        }
 }
 
@@ -1323,7 +1365,7 @@ static void dump_marks_helper(FILE *f,
        } else {
                for (k = 0; k < 1024; k++) {
                        if (m->data.marked[k])
-                               fprintf(f, ":%ju %s\n", base + k,
+                               fprintf(f, ":%" PRIuMAX " %s\n", base + k,
                                        sha1_to_hex(m->data.marked[k]->sha1));
                }
        }
@@ -1334,8 +1376,12 @@ static void dump_marks(void)
        if (mark_file)
        {
                FILE *f = fopen(mark_file, "w");
-               dump_marks_helper(f, 0, marks);
-               fclose(f);
+               if (f) {
+                       dump_marks_helper(f, 0, marks);
+                       fclose(f);
+               } else
+                       failure |= error("Unable to write marks file %s: %s",
+                               mark_file, strerror(errno));
        }
 }
 
@@ -1346,7 +1392,7 @@ static void read_next_command(void)
 
 static void cmd_mark(void)
 {
-       if (!strncmp("mark :", command_buf.buf, 6)) {
+       if (!prefixcmp(command_buf.buf, "mark :")) {
                next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
                read_next_command();
        }
@@ -1359,10 +1405,10 @@ static void *cmd_data (size_t *size)
        size_t length;
        char *buffer;
 
-       if (strncmp("data ", command_buf.buf, 5))
+       if (prefixcmp(command_buf.buf, "data "))
                die("Expected 'data n' command, found: %s", command_buf.buf);
 
-       if (!strncmp("<<", command_buf.buf + 5, 2)) {
+       if (!prefixcmp(command_buf.buf + 5, "<<")) {
                char *term = xstrdup(command_buf.buf + 5 + 2);
                size_t sz = 8192, term_len = command_buf.len - 5 - 2;
                length = 0;
@@ -1395,7 +1441,8 @@ static void *cmd_data (size_t *size)
                while (n < length) {
                        size_t s = fread(buffer + n, 1, length - n, stdin);
                        if (!s && feof(stdin))
-                               die("EOF in data (%lu bytes remaining)", length - n);
+                               die("EOF in data (%lu bytes remaining)",
+                                       (unsigned long)(length - n));
                        n += s;
                }
        }
@@ -1548,7 +1595,7 @@ static void file_change_m(struct branch *b)
                oe = find_mark(strtoumax(p + 1, &x, 10));
                hashcpy(sha1, oe->sha1);
                p = x;
-       } else if (!strncmp("inline", p, 6)) {
+       } else if (!prefixcmp(p, "inline")) {
                inline_data = 1;
                p += 6;
        } else {
@@ -1608,16 +1655,26 @@ static void file_change_d(struct branch *b)
        free(p_uq);
 }
 
+static void file_change_deleteall(struct branch *b)
+{
+       release_tree_content_recursive(b->branch_tree.tree);
+       hashclr(b->branch_tree.versions[0].sha1);
+       hashclr(b->branch_tree.versions[1].sha1);
+       load_tree(&b->branch_tree);
+}
+
 static void cmd_from(struct branch *b)
 {
        const char *from;
        struct branch *s;
 
-       if (strncmp("from ", command_buf.buf, 5))
+       if (prefixcmp(command_buf.buf, "from "))
                return;
 
-       if (b->last_commit)
-               die("Can't reinitailize branch %s", b->name);
+       if (b->branch_tree.tree) {
+               release_tree_content_recursive(b->branch_tree.tree);
+               b->branch_tree.tree = NULL;
+       }
 
        from = strchr(command_buf.buf, ' ') + 1;
        s = lookup_branch(from);
@@ -1634,7 +1691,7 @@ static void cmd_from(struct branch *b)
                unsigned long size;
                char *buf;
                if (oe->type != OBJ_COMMIT)
-                       die("Mark :%ju not a commit", idnum);
+                       die("Mark :%" PRIuMAX " not a commit", idnum);
                hashcpy(b->sha1, oe->sha1);
                buf = gfi_unpack_entry(oe, &size);
                if (!buf || size < 46)
@@ -1677,7 +1734,7 @@ static struct hash_list *cmd_merge(unsigned int *count)
        struct branch *s;
 
        *count = 0;
-       while (!strncmp("merge ", command_buf.buf, 6)) {
+       while (!prefixcmp(command_buf.buf, "merge ")) {
                from = strchr(command_buf.buf, ' ') + 1;
                n = xmalloc(sizeof(*n));
                s = lookup_branch(from);
@@ -1687,7 +1744,7 @@ static struct hash_list *cmd_merge(unsigned int *count)
                        uintmax_t idnum = strtoumax(from + 1, NULL, 10);
                        struct object_entry *oe = find_mark(idnum);
                        if (oe->type != OBJ_COMMIT)
-                               die("Mark :%ju not a commit", idnum);
+                               die("Mark :%" PRIuMAX " not a commit", idnum);
                        hashcpy(n->sha1, oe->sha1);
                } else if (get_sha1(from, n->sha1))
                        die("Invalid ref name or SHA1 expression: %s", from);
@@ -1723,11 +1780,11 @@ static void cmd_new_commit(void)
 
        read_next_command();
        cmd_mark();
-       if (!strncmp("author ", command_buf.buf, 7)) {
+       if (!prefixcmp(command_buf.buf, "author ")) {
                author = parse_ident(command_buf.buf + 7);
                read_next_command();
        }
-       if (!strncmp("committer ", command_buf.buf, 10)) {
+       if (!prefixcmp(command_buf.buf, "committer ")) {
                committer = parse_ident(command_buf.buf + 10);
                read_next_command();
        }
@@ -1748,10 +1805,12 @@ static void cmd_new_commit(void)
        for (;;) {
                if (1 == command_buf.len)
                        break;
-               else if (!strncmp("M ", command_buf.buf, 2))
+               else if (!prefixcmp(command_buf.buf, "M "))
                        file_change_m(b);
-               else if (!strncmp("D ", command_buf.buf, 2))
+               else if (!prefixcmp(command_buf.buf, "D "))
                        file_change_d(b);
+               else if (!strcmp("deleteall", command_buf.buf))
+                       file_change_deleteall(b);
                else
                        die("Unsupported file_change: %s", command_buf.buf);
                read_next_command();
@@ -1818,7 +1877,7 @@ static void cmd_new_tag(void)
        read_next_command();
 
        /* from ... */
-       if (strncmp("from ", command_buf.buf, 5))
+       if (prefixcmp(command_buf.buf, "from "))
                die("Expected from command, got %s", command_buf.buf);
        from = strchr(command_buf.buf, ' ') + 1;
        s = lookup_branch(from);
@@ -1829,7 +1888,7 @@ static void cmd_new_tag(void)
                from_mark = strtoumax(from + 1, NULL, 10);
                oe = find_mark(from_mark);
                if (oe->type != OBJ_COMMIT)
-                       die("Mark :%ju not a commit", from_mark);
+                       die("Mark :%" PRIuMAX " not a commit", from_mark);
                hashcpy(sha1, oe->sha1);
        } else if (!get_sha1(from, sha1)) {
                unsigned long size;
@@ -1845,7 +1904,7 @@ static void cmd_new_tag(void)
        read_next_command();
 
        /* tagger ... */
-       if (strncmp("tagger ", command_buf.buf, 7))
+       if (prefixcmp(command_buf.buf, "tagger "))
                die("Expected tagger command, got %s", command_buf.buf);
        tagger = parse_ident(command_buf.buf + 7);
 
@@ -1883,7 +1942,9 @@ static void cmd_reset_branch(void)
        sp = strchr(command_buf.buf, ' ') + 1;
        b = lookup_branch(sp);
        if (b) {
-               b->last_commit = 0;
+               hashclr(b->sha1);
+               hashclr(b->branch_tree.versions[0].sha1);
+               hashclr(b->branch_tree.versions[1].sha1);
                if (b->branch_tree.tree) {
                        release_tree_content_recursive(b->branch_tree.tree);
                        b->branch_tree.tree = NULL;
@@ -1897,8 +1958,12 @@ static void cmd_reset_branch(void)
 
 static void cmd_checkpoint(void)
 {
-       if (object_count)
-               checkpoint();
+       if (object_count) {
+               cycle_packfile();
+               dump_branches();
+               dump_tags();
+               dump_marks();
+       }
        read_next_command();
 }
 
@@ -1907,8 +1972,7 @@ static const char fast_import_usage[] =
 
 int main(int argc, const char **argv)
 {
-       int i;
-       uintmax_t total_count, duplicate_count;
+       int i, show_stats = 1;
 
        git_config(git_default_config);
 
@@ -1917,7 +1981,7 @@ int main(int argc, const char **argv)
 
                if (*a != '-' || !strcmp(a, "--"))
                        break;
-               else if (!strncmp(a, "--date-format=", 14)) {
+               else if (!prefixcmp(a, "--date-format=")) {
                        const char *fmt = a + 14;
                        if (!strcmp(fmt, "raw"))
                                whenspec = WHENSPEC_RAW;
@@ -1928,14 +1992,26 @@ int main(int argc, const char **argv)
                        else
                                die("unknown --date-format argument %s", fmt);
                }
-               else if (!strncmp(a, "--max-pack-size=", 16))
+               else if (!prefixcmp(a, "--max-pack-size="))
                        max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
-               else if (!strncmp(a, "--depth=", 8))
+               else if (!prefixcmp(a, "--depth="))
                        max_depth = strtoul(a + 8, NULL, 0);
-               else if (!strncmp(a, "--active-branches=", 18))
+               else if (!prefixcmp(a, "--active-branches="))
                        max_active_branches = strtoul(a + 18, NULL, 0);
-               else if (!strncmp(a, "--export-marks=", 15))
+               else if (!prefixcmp(a, "--export-marks="))
                        mark_file = a + 15;
+               else if (!prefixcmp(a, "--export-pack-edges=")) {
+                       if (pack_edges)
+                               fclose(pack_edges);
+                       pack_edges = fopen(a + 20, "a");
+                       if (!pack_edges)
+                               die("Cannot open %s: %s", a + 20, strerror(errno));
+               } else if (!strcmp(a, "--force"))
+                       force_update = 1;
+               else if (!strcmp(a, "--quiet"))
+                       show_stats = 0;
+               else if (!strcmp(a, "--stats"))
+                       show_stats = 1;
                else
                        die("unknown option %s", a);
        }
@@ -1957,11 +2033,11 @@ int main(int argc, const char **argv)
                        break;
                else if (!strcmp("blob", command_buf.buf))
                        cmd_new_blob();
-               else if (!strncmp("commit ", command_buf.buf, 7))
+               else if (!prefixcmp(command_buf.buf, "commit "))
                        cmd_new_commit();
-               else if (!strncmp("tag ", command_buf.buf, 4))
+               else if (!prefixcmp(command_buf.buf, "tag "))
                        cmd_new_tag();
-               else if (!strncmp("reset ", command_buf.buf, 6))
+               else if (!prefixcmp(command_buf.buf, "reset "))
                        cmd_reset_branch();
                else if (!strcmp("checkpoint", command_buf.buf))
                        cmd_checkpoint();
@@ -1975,31 +2051,35 @@ int main(int argc, const char **argv)
        unkeep_all_packs();
        dump_marks();
 
-       total_count = 0;
-       for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
-               total_count += object_count_by_type[i];
-       duplicate_count = 0;
-       for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
-               duplicate_count += duplicate_count_by_type[i];
-
-       fprintf(stderr, "%s statistics:\n", argv[0]);
-       fprintf(stderr, "---------------------------------------------------------------------\n");
-       fprintf(stderr, "Alloc'd objects: %10ju\n", alloc_count);
-       fprintf(stderr, "Total objects:   %10ju (%10ju duplicates                  )\n", total_count, duplicate_count);
-       fprintf(stderr, "      blobs  :   %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB]);
-       fprintf(stderr, "      trees  :   %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE]);
-       fprintf(stderr, "      commits:   %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT]);
-       fprintf(stderr, "      tags   :   %10ju (%10ju duplicates %10ju deltas)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG]);
-       fprintf(stderr, "Total branches:  %10lu (%10lu loads     )\n", branch_count, branch_load_count);
-       fprintf(stderr, "      marks:     %10ju (%10ju unique    )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
-       fprintf(stderr, "      atoms:     %10u\n", atom_cnt);
-       fprintf(stderr, "Memory total:    %10ju KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
-       fprintf(stderr, "       pools:    %10lu KiB\n", total_allocd/1024);
-       fprintf(stderr, "     objects:    %10ju KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
-       fprintf(stderr, "---------------------------------------------------------------------\n");
-       pack_report();
-       fprintf(stderr, "---------------------------------------------------------------------\n");
-       fprintf(stderr, "\n");
+       if (pack_edges)
+               fclose(pack_edges);
+
+       if (show_stats) {
+               uintmax_t total_count = 0, duplicate_count = 0;
+               for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
+                       total_count += object_count_by_type[i];
+               for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
+                       duplicate_count += duplicate_count_by_type[i];
+
+               fprintf(stderr, "%s statistics:\n", argv[0]);
+               fprintf(stderr, "---------------------------------------------------------------------\n");
+               fprintf(stderr, "Alloc'd objects: %10" PRIuMAX "\n", alloc_count);
+               fprintf(stderr, "Total objects:   %10" PRIuMAX " (%10" PRIuMAX " duplicates                  )\n", total_count, duplicate_count);
+               fprintf(stderr, "      blobs  :   %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB], delta_count_by_type[OBJ_BLOB]);
+               fprintf(stderr, "      trees  :   %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE], delta_count_by_type[OBJ_TREE]);
+               fprintf(stderr, "      commits:   %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT], delta_count_by_type[OBJ_COMMIT]);
+               fprintf(stderr, "      tags   :   %10" PRIuMAX " (%10" PRIuMAX " duplicates %10" PRIuMAX " deltas)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG], delta_count_by_type[OBJ_TAG]);
+               fprintf(stderr, "Total branches:  %10lu (%10lu loads     )\n", branch_count, branch_load_count);
+               fprintf(stderr, "      marks:     %10" PRIuMAX " (%10" PRIuMAX " unique    )\n", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);
+               fprintf(stderr, "      atoms:     %10u\n", atom_cnt);
+               fprintf(stderr, "Memory total:    %10" PRIuMAX " KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
+               fprintf(stderr, "       pools:    %10lu KiB\n", (unsigned long)(total_allocd/1024));
+               fprintf(stderr, "     objects:    %10" PRIuMAX " KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
+               fprintf(stderr, "---------------------------------------------------------------------\n");
+               pack_report();
+               fprintf(stderr, "---------------------------------------------------------------------\n");
+               fprintf(stderr, "\n");
+       }
 
-       return 0;
+       return failure ? 1 : 0;
 }