rebase: Support preserving merges in non-interactive mode
[gitweb.git] / fast-import.c
index 32ec159915611f6a21c218969bf82a09b6720c48..ab6689a64d69bdb741f662df2038ae1daae99837 100644 (file)
@@ -372,9 +372,11 @@ static void write_branch_report(FILE *rpt, struct branch *b)
        fputc('\n', rpt);
 }
 
+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_%d", getpid());
+       char *loc = git_path("fast_import_crash_%"PRIuMAX, (uintmax_t) getpid());
        FILE *rpt = fopen(loc, "w");
        struct branch *b;
        unsigned long lu;
@@ -388,8 +390,8 @@ static void write_crash_report(const char *err)
        fprintf(stderr, "fast-import: dumping crash report to %s\n", loc);
 
        fprintf(rpt, "fast-import crash report:\n");
-       fprintf(rpt, "    fast-import process: %d\n", getpid());
-       fprintf(rpt, "    parent process     : %d\n", getppid());
+       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));
        fputc('\n', rpt);
 
@@ -430,12 +432,37 @@ static void write_crash_report(const char *err)
                        write_branch_report(rpt, b);
        }
 
+       if (first_tag) {
+               struct tag *tg;
+               fputc('\n', rpt);
+               fputs("Annotated Tags\n", rpt);
+               fputs("--------------\n", rpt);
+               for (tg = first_tag; tg; tg = tg->next_tag) {
+                       fputs(sha1_to_hex(tg->sha1), rpt);
+                       fputc(' ', rpt);
+                       fputs(tg->name, rpt);
+                       fputc('\n', rpt);
+               }
+       }
+
+       fputc('\n', rpt);
+       fputs("Marks\n", rpt);
+       fputs("-----\n", rpt);
+       if (mark_file)
+               fprintf(rpt, "  exported to %s\n", mark_file);
+       else
+               dump_marks_helper(rpt, 0, marks);
+
        fputc('\n', rpt);
        fputs("-------------------\n", rpt);
        fputs("END OF CRASH REPORT\n", rpt);
        fclose(rpt);
 }
 
+static void end_packfile(void);
+static void unkeep_all_packs(void);
+static void dump_marks(void);
+
 static NORETURN void die_nicely(const char *err, va_list params)
 {
        static int zombie;
@@ -449,6 +476,9 @@ static NORETURN void die_nicely(const char *err, va_list params)
        if (!zombie) {
                zombie = 1;
                write_crash_report(message);
+               end_packfile();
+               unkeep_all_packs();
+               dump_marks();
        }
        exit(128);
 }
@@ -786,7 +816,7 @@ static void start_packfile(void)
        int pack_fd;
 
        snprintf(tmpfile, sizeof(tmpfile),
-               "%s/tmp_pack_XXXXXX", get_object_directory());
+               "%s/pack/tmp_pack_XXXXXX", get_object_directory());
        pack_fd = xmkstemp(tmpfile);
        p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2);
        strcpy(p->pack_name, tmpfile);
@@ -848,7 +878,7 @@ static char *create_index(void)
        }
 
        snprintf(tmpfile, sizeof(tmpfile),
-               "%s/tmp_idx_XXXXXX", get_object_directory());
+               "%s/pack/tmp_idx_XXXXXX", get_object_directory());
        idx_fd = xmkstemp(tmpfile);
        f = sha1fd(idx_fd, tmpfile);
        sha1write(f, array, 256 * sizeof(int));
@@ -860,7 +890,7 @@ static char *create_index(void)
                SHA1_Update(&ctx, (*c)->sha1, 20);
        }
        sha1write(f, pack_data->sha1, sizeof(pack_data->sha1));
-       sha1close(f, NULL, 1);
+       sha1close(f, NULL, CSUM_FSYNC);
        free(idx);
        SHA1_Final(pack_data->sha1, &ctx);
        return tmpfile;
@@ -921,7 +951,8 @@ static void end_packfile(void)
 
                close_pack_windows(pack_data);
                fixup_pack_header_footer(pack_data->pack_fd, pack_data->sha1,
-                                   pack_data->pack_name, object_count);
+                                   pack_data->pack_name, object_count,
+                                   NULL, 0);
                close(pack_data->pack_fd);
                idx_name = keep_pack(create_index());
 
@@ -1486,6 +1517,8 @@ static int update_branch(struct branch *b)
        struct ref_lock *lock;
        unsigned char old_sha1[20];
 
+       if (is_null_sha1(b->sha1))
+               return 0;
        if (read_ref(b->name, old_sha1))
                hashclr(old_sha1);
        lock = lock_any_ref_for_update(b->name, old_sha1, 0);
@@ -1658,7 +1691,7 @@ static void skip_optional_lf(void)
                ungetc(term_char, stdin);
 }
 
-static void cmd_mark(void)
+static void parse_mark(void)
 {
        if (!prefixcmp(command_buf.buf, "mark :")) {
                next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
@@ -1668,7 +1701,7 @@ static void cmd_mark(void)
                next_mark = 0;
 }
 
-static void cmd_data(struct strbuf *sb)
+static void parse_data(struct strbuf *sb)
 {
        strbuf_reset(sb);
 
@@ -1766,13 +1799,13 @@ static char *parse_ident(const char *buf)
        return ident;
 }
 
-static void cmd_new_blob(void)
+static void parse_new_blob(void)
 {
        static struct strbuf buf = STRBUF_INIT;
 
        read_next_command();
-       cmd_mark();
-       cmd_data(&buf);
+       parse_mark();
+       parse_data(&buf);
        store_object(OBJ_BLOB, &buf, &last_blob, NULL, next_mark);
 }
 
@@ -1836,6 +1869,7 @@ static void file_change_m(struct branch *b)
        case S_IFREG | 0644:
        case S_IFREG | 0755:
        case S_IFLNK:
+       case S_IFGITLINK:
        case 0644:
        case 0755:
                /* ok */
@@ -1868,7 +1902,20 @@ static void file_change_m(struct branch *b)
                p = uq.buf;
        }
 
-       if (inline_data) {
+       if (S_ISGITLINK(mode)) {
+               if (inline_data)
+                       die("Git links cannot be specified 'inline': %s",
+                               command_buf.buf);
+               else if (oe) {
+                       if (oe->type != OBJ_COMMIT)
+                               die("Not a commit (actually a %s): %s",
+                                       typename(oe->type), command_buf.buf);
+               }
+               /*
+                * Accept the sha1 without checking; it expected to be in
+                * another repository.
+                */
+       } else if (inline_data) {
                static struct strbuf buf = STRBUF_INIT;
 
                if (p != uq.buf) {
@@ -1876,7 +1923,7 @@ static void file_change_m(struct branch *b)
                        p = uq.buf;
                }
                read_next_command();
-               cmd_data(&buf);
+               parse_data(&buf);
                store_object(OBJ_BLOB, &buf, &last_blob, sha1, 0);
        } else if (oe) {
                if (oe->type != OBJ_BLOB)
@@ -1963,7 +2010,7 @@ 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)
+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));
@@ -1974,7 +2021,7 @@ static void cmd_from_commit(struct branch *b, char *buf, unsigned long size)
                b->branch_tree.versions[1].sha1);
 }
 
-static void cmd_from_existing(struct branch *b)
+static void parse_from_existing(struct branch *b)
 {
        if (is_null_sha1(b->sha1)) {
                hashclr(b->branch_tree.versions[0].sha1);
@@ -1985,12 +2032,12 @@ static void cmd_from_existing(struct branch *b)
 
                buf = read_object_with_reference(b->sha1,
                        commit_type, &size, b->sha1);
-               cmd_from_commit(b, buf, size);
+               parse_from_commit(b, buf, size);
                free(buf);
        }
 }
 
-static int cmd_from(struct branch *b)
+static int parse_from(struct branch *b)
 {
        const char *from;
        struct branch *s;
@@ -2021,12 +2068,12 @@ static int cmd_from(struct branch *b)
                if (oe->pack_id != MAX_PACK_ID) {
                        unsigned long size;
                        char *buf = gfi_unpack_entry(oe, &size);
-                       cmd_from_commit(b, buf, size);
+                       parse_from_commit(b, buf, size);
                        free(buf);
                } else
-                       cmd_from_existing(b);
+                       parse_from_existing(b);
        } else if (!get_sha1(from, b->sha1))
-               cmd_from_existing(b);
+               parse_from_existing(b);
        else
                die("Invalid ref name or SHA1 expression: %s", from);
 
@@ -2034,7 +2081,7 @@ static int cmd_from(struct branch *b)
        return 1;
 }
 
-static struct hash_list *cmd_merge(unsigned int *count)
+static struct hash_list *parse_merge(unsigned int *count)
 {
        struct hash_list *list = NULL, *n, *e = e;
        const char *from;
@@ -2075,7 +2122,7 @@ static struct hash_list *cmd_merge(unsigned int *count)
        return list;
 }
 
-static void cmd_new_commit(void)
+static void parse_new_commit(void)
 {
        static struct strbuf msg = STRBUF_INIT;
        struct branch *b;
@@ -2092,7 +2139,7 @@ static void cmd_new_commit(void)
                b = new_branch(sp);
 
        read_next_command();
-       cmd_mark();
+       parse_mark();
        if (!prefixcmp(command_buf.buf, "author ")) {
                author = parse_ident(command_buf.buf + 7);
                read_next_command();
@@ -2103,10 +2150,10 @@ static void cmd_new_commit(void)
        }
        if (!committer)
                die("Expected committer but didn't get one");
-       cmd_data(&msg);
+       parse_data(&msg);
        read_next_command();
-       cmd_from(b);
-       merge_list = cmd_merge(&merge_count);
+       parse_from(b);
+       merge_list = parse_merge(&merge_count);
 
        /* ensure the branch is active/loaded */
        if (!b->branch_tree.tree || !max_active_branches) {
@@ -2164,7 +2211,7 @@ static void cmd_new_commit(void)
        b->last_commit = object_count_by_type[OBJ_COMMIT];
 }
 
-static void cmd_new_tag(void)
+static void parse_new_tag(void)
 {
        static struct strbuf msg = STRBUF_INIT;
        char *sp;
@@ -2221,7 +2268,7 @@ static void cmd_new_tag(void)
 
        /* tag payload/message */
        read_next_command();
-       cmd_data(&msg);
+       parse_data(&msg);
 
        /* build the tag object */
        strbuf_reset(&new_data);
@@ -2241,7 +2288,7 @@ static void cmd_new_tag(void)
                t->pack_id = pack_id;
 }
 
-static void cmd_reset_branch(void)
+static void parse_reset_branch(void)
 {
        struct branch *b;
        char *sp;
@@ -2261,12 +2308,12 @@ static void cmd_reset_branch(void)
        else
                b = new_branch(sp);
        read_next_command();
-       cmd_from(b);
+       parse_from(b);
        if (command_buf.len > 0)
                unread_command_buf = 1;
 }
 
-static void cmd_checkpoint(void)
+static void parse_checkpoint(void)
 {
        if (object_count) {
                cycle_packfile();
@@ -2277,7 +2324,7 @@ static void cmd_checkpoint(void)
        skip_optional_lf();
 }
 
-static void cmd_progress(void)
+static void parse_progress(void)
 {
        fwrite(command_buf.buf, 1, command_buf.len, stdout);
        fputc('\n', stdout);
@@ -2320,7 +2367,7 @@ static void import_marks(const char *input_file)
        fclose(f);
 }
 
-static int git_pack_config(const char *k, const char *v)
+static int git_pack_config(const char *k, const char *v, void *cb)
 {
        if (!strcmp(k, "pack.depth")) {
                max_depth = git_config_int(k, v);
@@ -2338,17 +2385,18 @@ static int git_pack_config(const char *k, const char *v)
                pack_compression_seen = 1;
                return 0;
        }
-       return git_default_config(k, v);
+       return git_default_config(k, v, cb);
 }
 
 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]";
+"git fast-import [--date-format=f] [--max-pack-size=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file]";
 
 int main(int argc, const char **argv)
 {
        unsigned int i, show_stats = 1;
 
-       git_config(git_pack_config);
+       setup_git_directory();
+       git_config(git_pack_config, NULL);
        if (!pack_compression_seen && core_compression_seen)
                pack_compression_level = core_compression_level;
 
@@ -2416,17 +2464,17 @@ int main(int argc, const char **argv)
        set_die_routine(die_nicely);
        while (read_next_command() != EOF) {
                if (!strcmp("blob", command_buf.buf))
-                       cmd_new_blob();
+                       parse_new_blob();
                else if (!prefixcmp(command_buf.buf, "commit "))
-                       cmd_new_commit();
+                       parse_new_commit();
                else if (!prefixcmp(command_buf.buf, "tag "))
-                       cmd_new_tag();
+                       parse_new_tag();
                else if (!prefixcmp(command_buf.buf, "reset "))
-                       cmd_reset_branch();
+                       parse_reset_branch();
                else if (!strcmp("checkpoint", command_buf.buf))
-                       cmd_checkpoint();
+                       parse_checkpoint();
                else if (!prefixcmp(command_buf.buf, "progress "))
-                       cmd_progress();
+                       parse_progress();
                else
                        die("Unsupported command: %s", command_buf.buf);
        }