bulk-checkin: replace fast-import based implementation
[gitweb.git] / builtin / revert.c
index 19b6739104a6a11bf85b6d3c5015b4481fbd6020..87df70edc33fcc71177b863e6ced870330e505d0 100644 (file)
@@ -40,7 +40,7 @@ static const char * const cherry_pick_usage[] = {
 };
 
 enum replay_action { REVERT, CHERRY_PICK };
-enum replay_subcommand { REPLAY_NONE, REPLAY_RESET };
+enum replay_subcommand { REPLAY_NONE, REPLAY_RESET, REPLAY_CONTINUE };
 
 struct replay_opts {
        enum replay_action action;
@@ -66,15 +66,6 @@ struct replay_opts {
 
 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
 
-static void fatal(const char *advice, ...)
-{
-       va_list params;
-
-       va_start(params, advice);
-       vreportf("fatal: ", advice, params);
-       va_end(params);
-}
-
 static const char *action_name(const struct replay_opts *opts)
 {
        return opts->action == REVERT ? "revert" : "cherry-pick";
@@ -117,18 +108,39 @@ static void verify_opt_compatible(const char *me, const char *base_opt, ...)
                die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
 }
 
+static void verify_opt_mutually_compatible(const char *me, ...)
+{
+       const char *opt1, *opt2 = NULL;
+       va_list ap;
+
+       va_start(ap, me);
+       while ((opt1 = va_arg(ap, const char *))) {
+               if (va_arg(ap, int))
+                       break;
+       }
+       if (opt1) {
+               while ((opt2 = va_arg(ap, const char *))) {
+                       if (va_arg(ap, int))
+                               break;
+               }
+       }
+
+       if (opt1 && opt2)
+               die(_("%s: %s cannot be used with %s"), me, opt1, opt2);
+}
+
 static void parse_args(int argc, const char **argv, struct replay_opts *opts)
 {
        const char * const * usage_str = revert_or_cherry_pick_usage(opts);
        const char *me = action_name(opts);
-       int noop;
        int reset = 0;
+       int contin = 0;
        struct option options[] = {
                OPT_BOOLEAN(0, "reset", &reset, "forget the current operation"),
+               OPT_BOOLEAN(0, "continue", &contin, "continue the current operation"),
                OPT_BOOLEAN('n', "no-commit", &opts->no_commit, "don't automatically commit"),
                OPT_BOOLEAN('e', "edit", &opts->edit, "edit the commit message"),
-               { OPTION_BOOLEAN, 'r', NULL, &noop, NULL, "no-op (backward compatibility)",
-                 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 0 },
+               OPT_NOOP_NOARG('r', NULL),
                OPT_BOOLEAN('s', "signoff", &opts->signoff, "add Signed-off-by:"),
                OPT_INTEGER('m', "mainline", &opts->mainline, "parent number"),
                OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
@@ -154,15 +166,29 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
                                        PARSE_OPT_KEEP_ARGV0 |
                                        PARSE_OPT_KEEP_UNKNOWN);
 
+       /* Check for incompatible subcommands */
+       verify_opt_mutually_compatible(me,
+                               "--reset", reset,
+                               "--continue", contin,
+                               NULL);
+
        /* Set the subcommand */
        if (reset)
                opts->subcommand = REPLAY_RESET;
+       else if (contin)
+               opts->subcommand = REPLAY_CONTINUE;
        else
                opts->subcommand = REPLAY_NONE;
 
        /* Check for incompatible command line arguments */
-       if (opts->subcommand == REPLAY_RESET) {
-               verify_opt_compatible(me, "--reset",
+       if (opts->subcommand != REPLAY_NONE) {
+               char *this_operation;
+               if (opts->subcommand == REPLAY_RESET)
+                       this_operation = "--reset";
+               else
+                       this_operation = "--continue";
+
+               verify_opt_compatible(me, this_operation,
                                "--no-commit", opts->no_commit,
                                "--signoff", opts->signoff,
                                "--mainline", opts->mainline,
@@ -276,7 +302,7 @@ static void write_cherry_pick_head(struct commit *commit)
        strbuf_release(&buf);
 }
 
-static void print_advice(void)
+static void print_advice(int show_hint)
 {
        char *msg = getenv("GIT_CHERRY_PICK_HELP");
 
@@ -291,9 +317,11 @@ static void print_advice(void)
                return;
        }
 
-       advise("after resolving the conflicts, mark the corrected paths");
-       advise("with 'git add <paths>' or 'git rm <paths>'");
-       advise("and commit the result with 'git commit'");
+       if (show_hint) {
+               advise("after resolving the conflicts, mark the corrected paths");
+               advise("with 'git add <paths>' or 'git rm <paths>'");
+               advise("and commit the result with 'git commit'");
+       }
 }
 
 static void write_message(struct strbuf *msgbuf, const char *filename)
@@ -311,33 +339,23 @@ static void write_message(struct strbuf *msgbuf, const char *filename)
 
 static struct tree *empty_tree(void)
 {
-       struct tree *tree = xcalloc(1, sizeof(struct tree));
-
-       tree->object.parsed = 1;
-       tree->object.type = OBJ_TREE;
-       pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
-       return tree;
+       return lookup_tree((const unsigned char *)EMPTY_TREE_SHA1_BIN);
 }
 
-static NORETURN void die_dirty_index(struct replay_opts *opts)
+static int error_dirty_index(struct replay_opts *opts)
 {
-       if (read_cache_unmerged()) {
-               die_resolve_conflict(action_name(opts));
-       } else {
-               if (advice_commit_before_merge) {
-                       if (opts->action == REVERT)
-                               die(_("Your local changes would be overwritten by revert.\n"
-                                         "Please, commit your changes or stash them to proceed."));
-                       else
-                               die(_("Your local changes would be overwritten by cherry-pick.\n"
-                                         "Please, commit your changes or stash them to proceed."));
-               } else {
-                       if (opts->action == REVERT)
-                               die(_("Your local changes would be overwritten by revert.\n"));
-                       else
-                               die(_("Your local changes would be overwritten by cherry-pick.\n"));
-               }
-       }
+       if (read_cache_unmerged())
+               return error_resolve_conflict(action_name(opts));
+
+       /* Different translation strings for cherry-pick and revert */
+       if (opts->action == CHERRY_PICK)
+               error(_("Your local changes would be overwritten by cherry-pick."));
+       else
+               error(_("Your local changes would be overwritten by revert."));
+
+       if (advice_commit_before_merge)
+               advise(_("Commit your changes or stash them to proceed."));
+       return -1;
 }
 
 static int fast_forward_to(const unsigned char *to, const unsigned char *from)
@@ -455,9 +473,9 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
                        die (_("Your index file is unmerged."));
        } else {
                if (get_sha1("HEAD", head))
-                       die (_("You do not have a valid HEAD"));
+                       return error(_("You do not have a valid HEAD"));
                if (index_differs_from("HEAD", 0))
-                       die_dirty_index(opts);
+                       return error_dirty_index(opts);
        }
        discard_cache();
 
@@ -470,20 +488,20 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
                struct commit_list *p;
 
                if (!opts->mainline)
-                       die(_("Commit %s is a merge but no -m option was given."),
-                           sha1_to_hex(commit->object.sha1));
+                       return error(_("Commit %s is a merge but no -m option was given."),
+                               sha1_to_hex(commit->object.sha1));
 
                for (cnt = 1, p = commit->parents;
                     cnt != opts->mainline && p;
                     cnt++)
                        p = p->next;
                if (cnt != opts->mainline || !p)
-                       die(_("Commit %s does not have parent %d"),
-                           sha1_to_hex(commit->object.sha1), opts->mainline);
+                       return error(_("Commit %s does not have parent %d"),
+                               sha1_to_hex(commit->object.sha1), opts->mainline);
                parent = p->item;
        } else if (0 < opts->mainline)
-               die(_("Mainline was specified but commit %s is not a merge."),
-                   sha1_to_hex(commit->object.sha1));
+               return error(_("Mainline was specified but commit %s is not a merge."),
+                       sha1_to_hex(commit->object.sha1));
        else
                parent = commit->parents->item;
 
@@ -493,12 +511,12 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
        if (parent && parse_commit(parent) < 0)
                /* TRANSLATORS: The first %s will be "revert" or
                   "cherry-pick", the second %s a SHA1 */
-               die(_("%s: cannot parse parent commit %s"),
-                   action_name(opts), sha1_to_hex(parent->object.sha1));
+               return error(_("%s: cannot parse parent commit %s"),
+                       action_name(opts), sha1_to_hex(parent->object.sha1));
 
        if (get_message(commit, &msg) != 0)
-               die(_("Cannot get commit message for %s"),
-                               sha1_to_hex(commit->object.sha1));
+               return error(_("Cannot get commit message for %s"),
+                       sha1_to_hex(commit->object.sha1));
 
        /*
         * "commit" is an existing commit.  We would want to apply
@@ -548,8 +566,6 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
                        strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1));
                        strbuf_addstr(&msgbuf, ")\n");
                }
-               if (!opts->no_commit)
-                       write_cherry_pick_head(commit);
        }
 
        if (!opts->strategy || !strcmp(opts->strategy, "recursive") || opts->action == REVERT) {
@@ -570,13 +586,22 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
                free_commit_list(remotes);
        }
 
+       /*
+        * If the merge was clean or if it failed due to conflict, we write
+        * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
+        * However, if the merge did not even start, then we don't want to
+        * write it at all.
+        */
+       if (opts->action == CHERRY_PICK && !opts->no_commit && (res == 0 || res == 1))
+               write_cherry_pick_head(commit);
+
        if (res) {
                error(opts->action == REVERT
                      ? _("could not revert %s... %s")
                      : _("could not apply %s... %s"),
                      find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV),
                      msg.subject);
-               print_advice();
+               print_advice(res == 1);
                rerere(opts->allow_rerere_auto);
        } else {
                if (!opts->no_commit)
@@ -641,8 +666,8 @@ static void read_and_refresh_cache(struct replay_opts *opts)
  *     assert(commit_list_count(list) == 2);
  *     return list;
  */
-struct commit_list **commit_list_append(struct commit *commit,
-                                       struct commit_list **next)
+static struct commit_list **commit_list_append(struct commit *commit,
+                                              struct commit_list **next)
 {
        struct commit_list *new = xmalloc(sizeof(struct commit_list));
        new->item = commit;
@@ -668,6 +693,137 @@ static int format_todo(struct strbuf *buf, struct commit_list *todo_list,
        return 0;
 }
 
+static struct commit *parse_insn_line(char *start, struct replay_opts *opts)
+{
+       unsigned char commit_sha1[20];
+       char sha1_abbrev[40];
+       enum replay_action action;
+       int insn_len = 0;
+       char *p, *q;
+
+       if (!prefixcmp(start, "pick ")) {
+               action = CHERRY_PICK;
+               insn_len = strlen("pick");
+               p = start + insn_len + 1;
+       } else if (!prefixcmp(start, "revert ")) {
+               action = REVERT;
+               insn_len = strlen("revert");
+               p = start + insn_len + 1;
+       } else
+               return NULL;
+
+       q = strchr(p, ' ');
+       if (!q)
+               return NULL;
+       q++;
+
+       strlcpy(sha1_abbrev, p, q - p);
+
+       /*
+        * Verify that the action matches up with the one in
+        * opts; we don't support arbitrary instructions
+        */
+       if (action != opts->action) {
+               const char *action_str;
+               action_str = action == REVERT ? "revert" : "cherry-pick";
+               error(_("Cannot %s during a %s"), action_str, action_name(opts));
+               return NULL;
+       }
+
+       if (get_sha1(sha1_abbrev, commit_sha1) < 0)
+               return NULL;
+
+       return lookup_commit_reference(commit_sha1);
+}
+
+static int parse_insn_buffer(char *buf, struct commit_list **todo_list,
+                       struct replay_opts *opts)
+{
+       struct commit_list **next = todo_list;
+       struct commit *commit;
+       char *p = buf;
+       int i;
+
+       for (i = 1; *p; i++) {
+               commit = parse_insn_line(p, opts);
+               if (!commit)
+                       return error(_("Could not parse line %d."), i);
+               next = commit_list_append(commit, next);
+               p = strchrnul(p, '\n');
+               if (*p)
+                       p++;
+       }
+       if (!*todo_list)
+               return error(_("No commits parsed."));
+       return 0;
+}
+
+static void read_populate_todo(struct commit_list **todo_list,
+                       struct replay_opts *opts)
+{
+       const char *todo_file = git_path(SEQ_TODO_FILE);
+       struct strbuf buf = STRBUF_INIT;
+       int fd, res;
+
+       fd = open(todo_file, O_RDONLY);
+       if (fd < 0)
+               die_errno(_("Could not open %s."), todo_file);
+       if (strbuf_read(&buf, fd, 0) < 0) {
+               close(fd);
+               strbuf_release(&buf);
+               die(_("Could not read %s."), todo_file);
+       }
+       close(fd);
+
+       res = parse_insn_buffer(buf.buf, todo_list, opts);
+       strbuf_release(&buf);
+       if (res)
+               die(_("Unusable instruction sheet: %s"), todo_file);
+}
+
+static int populate_opts_cb(const char *key, const char *value, void *data)
+{
+       struct replay_opts *opts = data;
+       int error_flag = 1;
+
+       if (!value)
+               error_flag = 0;
+       else if (!strcmp(key, "options.no-commit"))
+               opts->no_commit = git_config_bool_or_int(key, value, &error_flag);
+       else if (!strcmp(key, "options.edit"))
+               opts->edit = git_config_bool_or_int(key, value, &error_flag);
+       else if (!strcmp(key, "options.signoff"))
+               opts->signoff = git_config_bool_or_int(key, value, &error_flag);
+       else if (!strcmp(key, "options.record-origin"))
+               opts->record_origin = git_config_bool_or_int(key, value, &error_flag);
+       else if (!strcmp(key, "options.allow-ff"))
+               opts->allow_ff = git_config_bool_or_int(key, value, &error_flag);
+       else if (!strcmp(key, "options.mainline"))
+               opts->mainline = git_config_int(key, value);
+       else if (!strcmp(key, "options.strategy"))
+               git_config_string(&opts->strategy, key, value);
+       else if (!strcmp(key, "options.strategy-option")) {
+               ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
+               opts->xopts[opts->xopts_nr++] = xstrdup(value);
+       } else
+               return error(_("Invalid key: %s"), key);
+
+       if (!error_flag)
+               return error(_("Invalid value for %s: %s"), key, value);
+
+       return 0;
+}
+
+static void read_populate_opts(struct replay_opts **opts_ptr)
+{
+       const char *opts_file = git_path(SEQ_OPTS_FILE);
+
+       if (!file_exists(opts_file))
+               return;
+       if (git_config_from_file(populate_opts_cb, opts_file, *opts_ptr) < 0)
+               die(_("Malformed options sheet: %s"), opts_file);
+}
+
 static void walk_revs_populate_todo(struct commit_list **todo_list,
                                struct replay_opts *opts)
 {
@@ -811,6 +967,15 @@ static int pick_revisions(struct replay_opts *opts)
        if (opts->subcommand == REPLAY_RESET) {
                remove_sequencer_state(1);
                return 0;
+       } else if (opts->subcommand == REPLAY_CONTINUE) {
+               if (!file_exists(git_path(SEQ_TODO_FILE)))
+                       goto error;
+               read_populate_opts(&opts);
+               read_populate_todo(&todo_list, opts);
+
+               /* Verify that the conflict has been resolved */
+               if (!index_differs_from("HEAD", 0))
+                       todo_list = todo_list->next;
        } else {
                /*
                 * Start a new cherry-pick/ revert sequence; but
@@ -820,24 +985,28 @@ static int pick_revisions(struct replay_opts *opts)
 
                walk_revs_populate_todo(&todo_list, opts);
                if (create_seq_dir() < 0) {
-                       fatal(_("A cherry-pick or revert is in progress."));
-                       advise(_("Use --reset to forget about it"));
-                       exit(128);
+                       error(_("A cherry-pick or revert is in progress."));
+                       advise(_("Use --continue to continue the operation"));
+                       advise(_("or --reset to forget about it"));
+                       return -1;
                }
                if (get_sha1("HEAD", sha1)) {
                        if (opts->action == REVERT)
-                               die(_("Can't revert as initial commit"));
-                       die(_("Can't cherry-pick into empty head"));
+                               return error(_("Can't revert as initial commit"));
+                       return error(_("Can't cherry-pick into empty head"));
                }
                save_head(sha1_to_hex(sha1));
                save_opts(opts);
        }
        return pick_commits(todo_list, opts);
+error:
+       return error(_("No %s in progress"), action_name(opts));
 }
 
 int cmd_revert(int argc, const char **argv, const char *prefix)
 {
        struct replay_opts opts;
+       int res;
 
        memset(&opts, 0, sizeof(opts));
        if (isatty(0))
@@ -845,16 +1014,23 @@ int cmd_revert(int argc, const char **argv, const char *prefix)
        opts.action = REVERT;
        git_config(git_default_config, NULL);
        parse_args(argc, argv, &opts);
-       return pick_revisions(&opts);
+       res = pick_revisions(&opts);
+       if (res < 0)
+               die(_("revert failed"));
+       return res;
 }
 
 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
 {
        struct replay_opts opts;
+       int res;
 
        memset(&opts, 0, sizeof(opts));
        opts.action = CHERRY_PICK;
        git_config(git_default_config, NULL);
        parse_args(argc, argv, &opts);
-       return pick_revisions(&opts);
+       res = pick_revisions(&opts);
+       if (res < 0)
+               die(_("cherry-pick failed"));
+       return res;
 }