builtin-am: implement --resolved/--continue

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>

Paul Tan committed Aug 4, 2015 at 21:51 UTC 240bfd2de9a7aec31240300ba1d7e89c59dbafe9
1 file changed +71 -1
builtin/am.c
+71 -1
@@ -759,6 +759,21 @@ static void do_commit(const struct am_state *state)
759 strbuf_release(&sb);
760 }
761
762 +/**
763 + * Validates the am_state for resuming -- the "msg" and authorship fields must
764 + * be filled up.
765 + */
766 +static void validate_resume_state(const struct am_state *state)
767 +{
768 + if (!state->msg)
769 + die(_("cannot resume: %s does not exist."),
770 + am_path(state, "final-commit"));
771 +
772 + if (!state->author_name || !state->author_email || !state->author_date)
773 + die(_("cannot resume: %s does not exist."),
774 + am_path(state, "author-script"));
775 +}
776 +
777 /**
778 * Applies all queued mail.
779 */
@@ -813,6 +828,36 @@ next:
828 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
829 }
830
831 +/**
832 + * Resume the current am session after patch application failure. The user did
833 + * all the hard work, and we do not have to do any patch application. Just
834 + * trust and commit what the user has in the index and working tree.
835 + */
836 +static void am_resolve(struct am_state *state)
837 +{
838 + validate_resume_state(state);
839 +
840 + printf_ln(_("Applying: %.*s"), linelen(state->msg), state->msg);
841 +
842 + if (!index_has_changes(NULL)) {
843 + printf_ln(_("No changes - did you forget to use 'git add'?\n"
844 + "If there is nothing left to stage, chances are that something else\n"
845 + "already introduced the same changes; you might want to skip this patch."));
846 + exit(128);
847 + }
848 +
849 + if (unmerged_cache()) {
850 + printf_ln(_("You still have unmerged paths in your index.\n"
851 + "Did you forget to use 'git add'?"));
852 + exit(128);
853 + }
854 +
855 + do_commit(state);
856 +
857 + am_next(state);
858 + am_run(state);
859 +}
860 +
861 /**
862 * parse_options() callback that validates and sets opt->value to the
863 * PATCH_FORMAT_* enum value corresponding to `arg`.
@@ -828,13 +873,20 @@ static int parse_opt_patchformat(const struct option *opt, const char *arg, int
873 return 0;
874 }
875
876 +enum resume_mode {
877 + RESUME_FALSE = 0,
878 + RESUME_RESOLVED
879 +};
880 +
881 int cmd_am(int argc, const char **argv, const char *prefix)
882 {
883 struct am_state state;
884 int patch_format = PATCH_FORMAT_UNKNOWN;
885 + enum resume_mode resume = RESUME_FALSE;
886
887 const char * const usage[] = {
888 N_("git am [options] [(<mbox>|<Maildir>)...]"),
889 + N_("git am [options] --continue"),
890 NULL
891 };
892
@@ -842,6 +894,12 @@ int cmd_am(int argc, const char **argv, const char *prefix)
894 OPT_CALLBACK(0, "patch-format", &patch_format, N_("format"),
895 N_("format the patch(es) are in"),
896 parse_opt_patchformat),
897 + OPT_CMDMODE(0, "continue", &resume,
898 + N_("continue applying patches after resolving a conflict"),
899 + RESUME_RESOLVED),
900 + OPT_CMDMODE('r', "resolved", &resume,
901 + N_("synonyms for --continue"),
902 + RESUME_RESOLVED),
903 OPT_END()
904 };
905
@@ -875,6 +933,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
933 struct argv_array paths = ARGV_ARRAY_INIT;
934 int i;
935
936 + if (resume)
937 + die(_("Resolve operation not in progress, we are not resuming."));
938 +
939 for (i = 0; i < argc; i++) {
940 if (is_absolute_path(argv[i]) || !prefix)
941 argv_array_push(&paths, argv[i]);
@@ -887,7 +948,16 @@ int cmd_am(int argc, const char **argv, const char *prefix)
948 argv_array_clear(&paths);
949 }
950
890 - am_run(&state);
951 + switch (resume) {
952 + case RESUME_FALSE:
953 + am_run(&state);
954 + break;
955 + case RESUME_RESOLVED:
956 + am_resolve(&state);
957 + break;
958 + default:
959 + die("BUG: invalid resume value");
960 + }
961
962 am_state_release(&state);
963