builtin-am: implement -k/--keep, --keep-non-patch

Since d1c5f2a (Add git-am, applymbox replacement., 2005-10-07), git-am.sh supported the -k/--keep option to pass the -k option to git-mailsplit. Since f7e5ea1 (am: learn passing -b to mailinfo, 2012-01-16), git-am.sh supported the --keep-non-patch option to pass the -b option to git-mailsplit. Re-implement these two 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 4f1b69617577309d361a006fc77d71a5cff7ec06
1 file changed +50
builtin/am.c
+50
@@ -68,6 +68,12 @@ enum patch_format {
68 PATCH_FORMAT_MBOX
69 };
70
71 +enum keep_type {
72 + KEEP_FALSE = 0,
73 + KEEP_TRUE, /* pass -k flag to git-mailinfo */
74 + KEEP_NON_PATCH /* pass -b flag to git-mailinfo */
75 +};
76 +
77 struct am_state {
78 /* state directory path */
79 char *dir;
@@ -91,6 +97,7 @@ struct am_state {
97 int quiet;
98 int signoff;
99 int utf8;
100 + int keep; /* enum keep_type */
101 const char *resolvemsg;
102 int rebasing;
103 };
@@ -373,6 +380,14 @@ static void am_load(struct am_state *state)
380 read_state_file(&sb, state, "utf8", 1);
381 state->utf8 = !strcmp(sb.buf, "t");
382
383 + read_state_file(&sb, state, "keep", 1);
384 + if (!strcmp(sb.buf, "t"))
385 + state->keep = KEEP_TRUE;
386 + else if (!strcmp(sb.buf, "b"))
387 + state->keep = KEEP_NON_PATCH;
388 + else
389 + state->keep = KEEP_FALSE;
390 +
391 state->rebasing = !!file_exists(am_path(state, "rebasing"));
392
393 strbuf_release(&sb);
@@ -536,6 +551,7 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
551 const char **paths)
552 {
553 unsigned char curr_head[GIT_SHA1_RAWSZ];
554 + const char *str;
555
556 if (!patch_format)
557 patch_format = detect_patch_format(paths);
@@ -564,6 +580,22 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
580
581 write_file(am_path(state, "utf8"), 1, state->utf8 ? "t" : "f");
582
583 + switch (state->keep) {
584 + case KEEP_FALSE:
585 + str = "f";
586 + break;
587 + case KEEP_TRUE:
588 + str = "t";
589 + break;
590 + case KEEP_NON_PATCH:
591 + str = "b";
592 + break;
593 + default:
594 + die("BUG: invalid value for state->keep");
595 + }
596 +
597 + write_file(am_path(state, "keep"), 1, "%s", str);
598 +
599 if (state->rebasing)
600 write_file(am_path(state, "rebasing"), 1, "%s", "");
601 else
@@ -731,6 +763,20 @@ static int parse_mail(struct am_state *state, const char *mail)
763
764 argv_array_push(&cp.args, "mailinfo");
765 argv_array_push(&cp.args, state->utf8 ? "-u" : "-n");
766 +
767 + switch (state->keep) {
768 + case KEEP_FALSE:
769 + break;
770 + case KEEP_TRUE:
771 + argv_array_push(&cp.args, "-k");
772 + break;
773 + case KEEP_NON_PATCH:
774 + argv_array_push(&cp.args, "-b");
775 + break;
776 + default:
777 + die("BUG: invalid value for state->keep");
778 + }
779 +
780 argv_array_push(&cp.args, am_path(state, "msg"));
781 argv_array_push(&cp.args, am_path(state, "patch"));
782
@@ -1471,6 +1517,10 @@ int cmd_am(int argc, const char **argv, const char *prefix)
1517 N_("add a Signed-off-by line to the commit message")),
1518 OPT_BOOL('u', "utf8", &state.utf8,
1519 N_("recode into utf8 (default)")),
1520 + OPT_SET_INT('k', "keep", &state.keep,
1521 + N_("pass -k flag to git-mailinfo"), KEEP_TRUE),
1522 + OPT_SET_INT(0, "keep-non-patch", &state.keep,
1523 + N_("pass -b flag to git-mailinfo"), KEEP_NON_PATCH),
1524 OPT_CALLBACK(0, "patch-format", &patch_format, N_("format"),
1525 N_("format the patch(es) are in"),
1526 parse_opt_patchformat),