builtin-am: pass git-apply's options to git-apply

git-am.sh recognizes some of git-apply's options, and would pass them to git-apply: * --whitespace, since 8c31cb8 (git-am: --whitespace=x option., 2006-02-28) * -C, since 67dad68 (add -C[NUM] to git-am, 2007-02-08) * -p, since 2092a1f (Teach git-am to pass -p option down to git-apply, 2007-02-11) * --directory, since b47dfe9 (git-am: add --directory=<dir> option, 2009-01-11) * --reject, since b80da42 (git-am: implement --reject option passed to git-apply, 2009-01-23) * --ignore-space-change, --ignore-whitespace, since 86c91f9 (git apply: option to ignore whitespace differences, 2009-08-04) * --exclude, since 77e9e49 (am: pass exclude down to apply, 2011-08-03) * --include, since 58725ef (am: support --include option, 2012-03-28) * --reject, since b80da42 (git-am: implement --reject option passed to git-apply, 2009-01-23) Re-implement support for these options 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:51 UTC 257e8cecc13b323c7b5c3fb249955b3ee8f4af3d
1 file changed +45
builtin/am.c
+45
@@ -106,6 +106,7 @@ struct am_state {
106 int keep; /* enum keep_type */
107 int message_id;
108 int scissors; /* enum scissors_type */
109 + struct argv_array git_apply_opts;
110 const char *resolvemsg;
111 int rebasing;
112 };
@@ -128,6 +129,8 @@ static void am_state_init(struct am_state *state, const char *dir)
129 git_config_get_bool("am.messageid", &state->message_id);
130
131 state->scissors = SCISSORS_UNSET;
132 +
133 + argv_array_init(&state->git_apply_opts);
134 }
135
136 /**
@@ -140,6 +143,7 @@ static void am_state_release(struct am_state *state)
143 free(state->author_email);
144 free(state->author_date);
145 free(state->msg);
146 + argv_array_clear(&state->git_apply_opts);
147 }
148
149 /**
@@ -411,6 +415,11 @@ static void am_load(struct am_state *state)
415 else
416 state->scissors = SCISSORS_UNSET;
417
418 + read_state_file(&sb, state, "apply-opt", 1);
419 + argv_array_clear(&state->git_apply_opts);
420 + if (sq_dequote_to_argv_array(sb.buf, &state->git_apply_opts) < 0)
421 + die(_("could not parse %s"), am_path(state, "apply-opt"));
422 +
423 state->rebasing = !!file_exists(am_path(state, "rebasing"));
424
425 strbuf_release(&sb);
@@ -585,6 +594,7 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
594 {
595 unsigned char curr_head[GIT_SHA1_RAWSZ];
596 const char *str;
597 + struct strbuf sb = STRBUF_INIT;
598
599 if (!patch_format)
600 patch_format = detect_patch_format(paths);
@@ -647,6 +657,9 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
657
658 write_file(am_path(state, "scissors"), 1, "%s", str);
659
660 + sq_quote_argv(&sb, state->git_apply_opts.argv, 0);
661 + write_file(am_path(state, "apply-opt"), 1, "%s", sb.buf);
662 +
663 if (state->rebasing)
664 write_file(am_path(state, "rebasing"), 1, "%s", "");
665 else
@@ -671,6 +684,8 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
684 write_file(am_path(state, "next"), 1, "%d", state->cur);
685
686 write_file(am_path(state, "last"), 1, "%d", state->last);
687 +
688 + strbuf_release(&sb);
689 }
690
691 /**
@@ -1058,6 +1073,8 @@ static int run_apply(const struct am_state *state, const char *index_file)
1073
1074 argv_array_push(&cp.args, "apply");
1075
1076 + argv_array_pushv(&cp.args, state->git_apply_opts.argv);
1077 +
1078 if (index_file)
1079 argv_array_push(&cp.args, "--cached");
1080 else
@@ -1084,6 +1101,7 @@ static int build_fake_ancestor(const struct am_state *state, const char *index_f
1101
1102 cp.git_cmd = 1;
1103 argv_array_push(&cp.args, "apply");
1104 + argv_array_pushv(&cp.args, state->git_apply_opts.argv);
1105 argv_array_pushf(&cp.args, "--build-fake-ancestor=%s", index_file);
1106 argv_array_push(&cp.args, am_path(state, "patch"));
1107
@@ -1599,9 +1617,36 @@ int cmd_am(int argc, const char **argv, const char *prefix)
1617 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 0},
1618 OPT_BOOL('c', "scissors", &state.scissors,
1619 N_("strip everything before a scissors line")),
1620 + OPT_PASSTHRU_ARGV(0, "whitespace", &state.git_apply_opts, N_("action"),
1621 + N_("pass it through git-apply"),
1622 + 0),
1623 + OPT_PASSTHRU_ARGV(0, "ignore-space-change", &state.git_apply_opts, NULL,
1624 + N_("pass it through git-apply"),
1625 + PARSE_OPT_NOARG),
1626 + OPT_PASSTHRU_ARGV(0, "ignore-whitespace", &state.git_apply_opts, NULL,
1627 + N_("pass it through git-apply"),
1628 + PARSE_OPT_NOARG),
1629 + OPT_PASSTHRU_ARGV(0, "directory", &state.git_apply_opts, N_("root"),
1630 + N_("pass it through git-apply"),
1631 + 0),
1632 + OPT_PASSTHRU_ARGV(0, "exclude", &state.git_apply_opts, N_("path"),
1633 + N_("pass it through git-apply"),
1634 + 0),
1635 + OPT_PASSTHRU_ARGV(0, "include", &state.git_apply_opts, N_("path"),
1636 + N_("pass it through git-apply"),
1637 + 0),
1638 + OPT_PASSTHRU_ARGV('C', NULL, &state.git_apply_opts, N_("n"),
1639 + N_("pass it through git-apply"),
1640 + 0),
1641 + OPT_PASSTHRU_ARGV('p', NULL, &state.git_apply_opts, N_("num"),
1642 + N_("pass it through git-apply"),
1643 + 0),
1644 OPT_CALLBACK(0, "patch-format", &patch_format, N_("format"),
1645 N_("format the patch(es) are in"),
1646 parse_opt_patchformat),
1647 + OPT_PASSTHRU_ARGV(0, "reject", &state.git_apply_opts, NULL,
1648 + N_("pass it through git-apply"),
1649 + PARSE_OPT_NOARG),
1650 OPT_STRING(0, "resolvemsg", &state.resolvemsg, NULL,
1651 N_("override error message when patch failure occurs")),
1652 OPT_CMDMODE(0, "continue", &resume,