builtin-am: implement -i/--interactive

Since d1c5f2a (Add git-am, applymbox replacement., 2005-10-07), git-am.sh supported the --interactive mode. After parsing the patch mail and extracting the patch, commit message and authorship info, an interactive session will begin that allows the user to choose between: * applying the patch * applying the patch and all subsequent patches (by disabling interactive mode in subsequent patches) * skipping the patch * editing the commit message Since f89ad67 (Add [v]iew patch in git-am interactive., 2005-10-25), git-am.sh --interactive also supported viewing the patch to be applied. When --resolved-ing in --interactive mode, we need to take care to update the patch with the contents of the index, such that the correct patch will be displayed when the patch is viewed in interactive mode. Re-implement the above in builtin/am.c 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:52 UTC 7ff26832535a83bd8d75beefbf822d965e066961
1 file changed +104 -1
builtin/am.c
+104 -1
@@ -25,6 +25,7 @@
25 #include "log-tree.h"
26 #include "notes-utils.h"
27 #include "rerere.h"
28 +#include "prompt.h"
29
30 /**
31 * Returns 1 if the file is empty or does not exist, 0 otherwise.
@@ -119,6 +120,7 @@ struct am_state {
120 int prec;
121
122 /* various operating modes and command line options */
123 + int interactive;
124 int threeway;
125 int quiet;
126 int signoff;
@@ -1171,7 +1173,7 @@ static void NORETURN die_user_resolve(const struct am_state *state)
1173 if (state->resolvemsg) {
1174 printf_ln("%s", state->resolvemsg);
1175 } else {
1174 - const char *cmdline = "git am";
1176 + const char *cmdline = state->interactive ? "git am -i" : "git am";
1177
1178 printf_ln(_("When you have resolved this problem, run \"%s --continue\"."), cmdline);
1179 printf_ln(_("If you prefer to skip this patch, run \"%s --skip\" instead."), cmdline);
@@ -1403,6 +1405,36 @@ static void write_commit_patch(const struct am_state *state, struct commit *comm
1405 log_tree_commit(&rev_info, commit);
1406 }
1407
1408 +/**
1409 + * Writes the diff of the index against HEAD as a patch to the state
1410 + * directory's "patch" file.
1411 + */
1412 +static void write_index_patch(const struct am_state *state)
1413 +{
1414 + struct tree *tree;
1415 + unsigned char head[GIT_SHA1_RAWSZ];
1416 + struct rev_info rev_info;
1417 + FILE *fp;
1418 +
1419 + if (!get_sha1_tree("HEAD", head))
1420 + tree = lookup_tree(head);
1421 + else
1422 + tree = lookup_tree(EMPTY_TREE_SHA1_BIN);
1423 +
1424 + fp = xfopen(am_path(state, "patch"), "w");
1425 + init_revisions(&rev_info, NULL);
1426 + rev_info.diff = 1;
1427 + rev_info.disable_stdin = 1;
1428 + rev_info.no_commit_id = 1;
1429 + rev_info.diffopt.output_format = DIFF_FORMAT_PATCH;
1430 + rev_info.diffopt.use_color = 0;
1431 + rev_info.diffopt.file = fp;
1432 + rev_info.diffopt.close_file = 1;
1433 + add_pending_object(&rev_info, &tree->object, "");
1434 + diff_setup_done(&rev_info.diffopt);
1435 + run_diff_index(&rev_info, 1);
1436 +}
1437 +
1438 /**
1439 * Like parse_mail(), but parses the mail by looking up its commit ID
1440 * directly. This is used in --rebasing mode to bypass git-mailinfo's munging
@@ -1654,6 +1686,65 @@ static void validate_resume_state(const struct am_state *state)
1686 am_path(state, "author-script"));
1687 }
1688
1689 +/**
1690 + * Interactively prompt the user on whether the current patch should be
1691 + * applied.
1692 + *
1693 + * Returns 0 if the user chooses to apply the patch, 1 if the user chooses to
1694 + * skip it.
1695 + */
1696 +static int do_interactive(struct am_state *state)
1697 +{
1698 + assert(state->msg);
1699 +
1700 + if (!isatty(0))
1701 + die(_("cannot be interactive without stdin connected to a terminal."));
1702 +
1703 + for (;;) {
1704 + const char *reply;
1705 +
1706 + puts(_("Commit Body is:"));
1707 + puts("--------------------------");
1708 + printf("%s", state->msg);
1709 + puts("--------------------------");
1710 +
1711 + /*
1712 + * TRANSLATORS: Make sure to include [y], [n], [e], [v] and [a]
1713 + * in your translation. The program will only accept English
1714 + * input at this point.
1715 + */
1716 + reply = git_prompt(_("Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all: "), PROMPT_ECHO);
1717 +
1718 + if (!reply) {
1719 + continue;
1720 + } else if (*reply == 'y' || *reply == 'Y') {
1721 + return 0;
1722 + } else if (*reply == 'a' || *reply == 'A') {
1723 + state->interactive = 0;
1724 + return 0;
1725 + } else if (*reply == 'n' || *reply == 'N') {
1726 + return 1;
1727 + } else if (*reply == 'e' || *reply == 'E') {
1728 + struct strbuf msg = STRBUF_INIT;
1729 +
1730 + if (!launch_editor(am_path(state, "final-commit"), &msg, NULL)) {
1731 + free(state->msg);
1732 + state->msg = strbuf_detach(&msg, &state->msg_len);
1733 + }
1734 + strbuf_release(&msg);
1735 + } else if (*reply == 'v' || *reply == 'V') {
1736 + const char *pager = git_pager(1);
1737 + struct child_process cp = CHILD_PROCESS_INIT;
1738 +
1739 + if (!pager)
1740 + pager = "cat";
1741 + argv_array_push(&cp.args, pager);
1742 + argv_array_push(&cp.args, am_path(state, "patch"));
1743 + run_command(&cp);
1744 + }
1745 + }
1746 +}
1747 +
1748 /**
1749 * Applies all queued mail.
1750 *
@@ -1702,6 +1793,9 @@ static void am_run(struct am_state *state, int resume)
1793 write_commit_msg(state);
1794 }
1795
1796 + if (state->interactive && do_interactive(state))
1797 + goto next;
1798 +
1799 if (run_applypatch_msg_hook(state))
1800 exit(1);
1801
@@ -1787,10 +1881,17 @@ static void am_resolve(struct am_state *state)
1881 die_user_resolve(state);
1882 }
1883
1884 + if (state->interactive) {
1885 + write_index_patch(state);
1886 + if (do_interactive(state))
1887 + goto next;
1888 + }
1889 +
1890 rerere(0);
1891
1892 do_commit(state);
1893
1894 +next:
1895 am_next(state);
1896 am_run(state, 0);
1897 }
@@ -2036,6 +2137,8 @@ int cmd_am(int argc, const char **argv, const char *prefix)
2137 };
2138
2139 struct option options[] = {
2140 + OPT_BOOL('i', "interactive", &state.interactive,
2141 + N_("run interactively")),
2142 OPT_BOOL('3', "3way", &state.threeway,
2143 N_("allow fall back on 3way merging if needed")),
2144 OPT__QUIET(&state.quiet, N_("be quiet")),