Merge branch 'jc/maint-format-patch-encoding' into maint
[gitweb.git] / fast-import.c
index 5085fbf116e57a59f488afa3f0055c9427efb3c9..5e83296bf49f9b41b019185539cbd4ec1bf94513 100644 (file)
@@ -154,13 +154,16 @@ Format of STDIN stream:
 
 #define PACK_ID_BITS 16
 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
+#define DEPTH_BITS 13
+#define MAX_DEPTH ((1<<DEPTH_BITS)-1)
 
 struct object_entry
 {
        struct object_entry *next;
        uint32_t offset;
-       unsigned type : TYPE_BITS;
-       unsigned pack_id : PACK_ID_BITS;
+       uint32_t type : TYPE_BITS,
+               pack_id : PACK_ID_BITS,
+               depth : DEPTH_BITS;
        unsigned char sha1[20];
 };
 
@@ -375,7 +378,7 @@ static void write_branch_report(FILE *rpt, struct branch *b)
        fputc('\n', rpt);
 }
 
-static void write_crash_report(const char *err, va_list params)
+static void write_crash_report(const char *err)
 {
        char *loc = git_path("fast_import_crash_%d", getpid());
        FILE *rpt = fopen(loc, "w");
@@ -397,7 +400,7 @@ static void write_crash_report(const char *err, va_list params)
        fputc('\n', rpt);
 
        fputs("fatal: ", rpt);
-       vfprintf(rpt, err, params);
+       fputs(err, rpt);
        fputc('\n', rpt);
 
        fputc('\n', rpt);
@@ -442,16 +445,17 @@ static void write_crash_report(const char *err, va_list params)
 static NORETURN void die_nicely(const char *err, va_list params)
 {
        static int zombie;
+       char message[2 * PATH_MAX];
 
+       vsnprintf(message, sizeof(message), err, params);
        fputs("fatal: ", stderr);
-       vfprintf(stderr, err, params);
+       fputs(message, stderr);
        fputc('\n', stderr);
 
        if (!zombie) {
                zombie = 1;
-               write_crash_report(err, params);
+               write_crash_report(message);
        }
-
        exit(128);
 }
 
@@ -1104,7 +1108,7 @@ static int store_object(
                unsigned pos = sizeof(hdr) - 1;
 
                delta_count_by_type[type]++;
-               last->depth++;
+               e->depth = last->depth + 1;
 
                hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr);
                write_or_die(pack_data->pack_fd, hdr, hdrlen);
@@ -1116,8 +1120,7 @@ static int store_object(
                write_or_die(pack_data->pack_fd, hdr + pos, sizeof(hdr) - pos);
                pack_size += sizeof(hdr) - pos;
        } else {
-               if (last)
-                       last->depth = 0;
+               e->depth = 0;
                hdrlen = encode_header(type, datlen, hdr);
                write_or_die(pack_data->pack_fd, hdr, hdrlen);
                pack_size += hdrlen;
@@ -1133,6 +1136,7 @@ static int store_object(
                        free(last->data);
                last->data = dat;
                last->offset = e->offset;
+               last->depth = e->depth;
                last->len = datlen;
        }
        return 0;
@@ -1180,7 +1184,7 @@ static void load_tree(struct tree_entry *root)
        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;
+               t->delta_depth = myoe->depth;
                buf = gfi_unpack_entry(myoe, &size);
        } else {
                enum object_type type;
@@ -1618,7 +1622,7 @@ static void read_next_command(void)
        } while (command_buf.buf[0] == '#');
 }
 
-static void skip_optional_lf()
+static void skip_optional_lf(void)
 {
        int term_char = fgetc(stdin);
        if (term_char != '\n' && term_char != EOF)
@@ -1858,7 +1862,7 @@ 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, typename(oe->type));
+                               typename(oe->type), command_buf.buf);
        } else {
                enum object_type type = sha1_object_info(sha1, NULL);
                if (type < 0)
@@ -2346,8 +2350,11 @@ int main(int argc, const char **argv)
                }
                else if (!prefixcmp(a, "--max-pack-size="))
                        max_packsize = strtoumax(a + 16, NULL, 0) * 1024 * 1024;
-               else if (!prefixcmp(a, "--depth="))
+               else if (!prefixcmp(a, "--depth=")) {
                        max_depth = strtoul(a + 8, NULL, 0);
+                       if (max_depth > MAX_DEPTH)
+                               die("--depth cannot exceed %u", MAX_DEPTH);
+               }
                else if (!prefixcmp(a, "--active-branches="))
                        max_active_branches = strtoul(a + 18, NULL, 0);
                else if (!prefixcmp(a, "--import-marks="))