builtin-am: implement --resolved/--continue
authorPaul Tan <pyokagan@gmail.com>
Tue, 4 Aug 2015 13:51:32 +0000 (21:51 +0800)
committerJunio C Hamano <gitster@pobox.com>
Wed, 5 Aug 2015 05:02:11 +0000 (22:02 -0700)
Since 0c15cc9 (git-am: --resolved., 2005-11-16), git-am supported
resuming from a failed patch application. The user will manually apply
the patch, and the run git am --resolved which will then commit the
resulting index. Re-implement this feature by introducing am_resolve().

Since it makes no sense for the user to run am --resolved when there is
no session in progress, we error out in this case.

Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/am.c
index 537ad62501971012768d0f91da23adfd36c19603..fd267213aa40e9e71ef2ad49191798e26ad2acc6 100644 (file)
@@ -759,6 +759,21 @@ static void do_commit(const struct am_state *state)
        strbuf_release(&sb);
 }
 
+/**
+ * Validates the am_state for resuming -- the "msg" and authorship fields must
+ * be filled up.
+ */
+static void validate_resume_state(const struct am_state *state)
+{
+       if (!state->msg)
+               die(_("cannot resume: %s does not exist."),
+                       am_path(state, "final-commit"));
+
+       if (!state->author_name || !state->author_email || !state->author_date)
+               die(_("cannot resume: %s does not exist."),
+                       am_path(state, "author-script"));
+}
+
 /**
  * Applies all queued mail.
  */
@@ -813,6 +828,36 @@ static void am_run(struct am_state *state)
        run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
 }
 
+/**
+ * Resume the current am session after patch application failure. The user did
+ * all the hard work, and we do not have to do any patch application. Just
+ * trust and commit what the user has in the index and working tree.
+ */
+static void am_resolve(struct am_state *state)
+{
+       validate_resume_state(state);
+
+       printf_ln(_("Applying: %.*s"), linelen(state->msg), state->msg);
+
+       if (!index_has_changes(NULL)) {
+               printf_ln(_("No changes - did you forget to use 'git add'?\n"
+                       "If there is nothing left to stage, chances are that something else\n"
+                       "already introduced the same changes; you might want to skip this patch."));
+               exit(128);
+       }
+
+       if (unmerged_cache()) {
+               printf_ln(_("You still have unmerged paths in your index.\n"
+                       "Did you forget to use 'git add'?"));
+               exit(128);
+       }
+
+       do_commit(state);
+
+       am_next(state);
+       am_run(state);
+}
+
 /**
  * parse_options() callback that validates and sets opt->value to the
  * PATCH_FORMAT_* enum value corresponding to `arg`.
@@ -828,13 +873,20 @@ static int parse_opt_patchformat(const struct option *opt, const char *arg, int
        return 0;
 }
 
+enum resume_mode {
+       RESUME_FALSE = 0,
+       RESUME_RESOLVED
+};
+
 int cmd_am(int argc, const char **argv, const char *prefix)
 {
        struct am_state state;
        int patch_format = PATCH_FORMAT_UNKNOWN;
+       enum resume_mode resume = RESUME_FALSE;
 
        const char * const usage[] = {
                N_("git am [options] [(<mbox>|<Maildir>)...]"),
+               N_("git am [options] --continue"),
                NULL
        };
 
@@ -842,6 +894,12 @@ int cmd_am(int argc, const char **argv, const char *prefix)
                OPT_CALLBACK(0, "patch-format", &patch_format, N_("format"),
                        N_("format the patch(es) are in"),
                        parse_opt_patchformat),
+               OPT_CMDMODE(0, "continue", &resume,
+                       N_("continue applying patches after resolving a conflict"),
+                       RESUME_RESOLVED),
+               OPT_CMDMODE('r', "resolved", &resume,
+                       N_("synonyms for --continue"),
+                       RESUME_RESOLVED),
                OPT_END()
        };
 
@@ -875,6 +933,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
                struct argv_array paths = ARGV_ARRAY_INIT;
                int i;
 
+               if (resume)
+                       die(_("Resolve operation not in progress, we are not resuming."));
+
                for (i = 0; i < argc; i++) {
                        if (is_absolute_path(argv[i]) || !prefix)
                                argv_array_push(&paths, argv[i]);
@@ -887,7 +948,16 @@ int cmd_am(int argc, const char **argv, const char *prefix)
                argv_array_clear(&paths);
        }
 
-       am_run(&state);
+       switch (resume) {
+       case RESUME_FALSE:
+               am_run(&state);
+               break;
+       case RESUME_RESOLVED:
+               am_resolve(&state);
+               break;
+       default:
+               die("BUG: invalid resume value");
+       }
 
        am_state_release(&state);