builtin-am: support --keep-cr, am.keepcr

Since ad2c928 (git-am: Add command line parameter `--keep-cr` passing it to git-mailsplit, 2010-02-27), git-am.sh supported the --keep-cr option and would pass it to git-mailsplit. Since e80d4cb (git-am: Add am.keepcr and --no-keep-cr to override it, 2010-02-27), git-am.sh supported the am.keepcr config setting, which controls whether --keep-cr is on by default. 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:51 UTC 5d123a4017813540ad67656608c5d85e84cc8e5d
1 file changed +23 -6
builtin/am.c
+23 -6
@@ -502,7 +502,7 @@ done:
502 * Splits out individual email patches from `paths`, where each path is either
503 * a mbox file or a Maildir. Returns 0 on success, -1 on failure.
504 */
505 -static int split_mail_mbox(struct am_state *state, const char **paths)
505 +static int split_mail_mbox(struct am_state *state, const char **paths, int keep_cr)
506 {
507 struct child_process cp = CHILD_PROCESS_INIT;
508 struct strbuf last = STRBUF_INIT;
@@ -512,6 +512,8 @@ static int split_mail_mbox(struct am_state *state, const char **paths)
512 argv_array_pushf(&cp.args, "-d%d", state->prec);
513 argv_array_pushf(&cp.args, "-o%s", state->dir);
514 argv_array_push(&cp.args, "-b");
515 + if (keep_cr)
516 + argv_array_push(&cp.args, "--keep-cr");
517 argv_array_push(&cp.args, "--");
518 argv_array_pushv(&cp.args, paths);
519
@@ -536,14 +538,22 @@ static int split_mail_mbox(struct am_state *state, const char **paths)
538 * state->cur will be set to the index of the first mail, and state->last will
539 * be set to the index of the last mail.
540 *
541 + * Set keep_cr to 0 to convert all lines ending with \r\n to end with \n, 1
542 + * to disable this behavior, -1 to use the default configured setting.
543 + *
544 * Returns 0 on success, -1 on failure.
545 */
546 static int split_mail(struct am_state *state, enum patch_format patch_format,
542 - const char **paths)
547 + const char **paths, int keep_cr)
548 {
549 + if (keep_cr < 0) {
550 + keep_cr = 0;
551 + git_config_get_bool("am.keepcr", &keep_cr);
552 + }
553 +
554 switch (patch_format) {
555 case PATCH_FORMAT_MBOX:
546 - return split_mail_mbox(state, paths);
556 + return split_mail_mbox(state, paths, keep_cr);
557 default:
558 die("BUG: invalid patch_format");
559 }
@@ -554,7 +564,7 @@ static int split_mail(struct am_state *state, enum patch_format patch_format,
564 * Setup a new am session for applying patches
565 */
566 static void am_setup(struct am_state *state, enum patch_format patch_format,
557 - const char **paths)
567 + const char **paths, int keep_cr)
568 {
569 unsigned char curr_head[GIT_SHA1_RAWSZ];
570 const char *str;
@@ -570,7 +580,7 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
580 if (mkdir(state->dir, 0777) < 0 && errno != EEXIST)
581 die_errno(_("failed to create directory '%s'"), state->dir);
582
573 - if (split_mail(state, patch_format, paths) < 0) {
583 + if (split_mail(state, patch_format, paths, keep_cr) < 0) {
584 am_destroy(state);
585 die(_("Failed to split patches."));
586 }
@@ -1511,6 +1521,7 @@ enum resume_mode {
1521 int cmd_am(int argc, const char **argv, const char *prefix)
1522 {
1523 struct am_state state;
1524 + int keep_cr = -1;
1525 int patch_format = PATCH_FORMAT_UNKNOWN;
1526 enum resume_mode resume = RESUME_FALSE;
1527
@@ -1534,6 +1545,12 @@ int cmd_am(int argc, const char **argv, const char *prefix)
1545 N_("pass -b flag to git-mailinfo"), KEEP_NON_PATCH),
1546 OPT_BOOL('m', "message-id", &state.message_id,
1547 N_("pass -m flag to git-mailinfo")),
1548 + { OPTION_SET_INT, 0, "keep-cr", &keep_cr, NULL,
1549 + N_("pass --keep-cr flag to git-mailsplit for mbox format"),
1550 + PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
1551 + { OPTION_SET_INT, 0, "no-keep-cr", &keep_cr, NULL,
1552 + N_("do not pass --keep-cr flag to git-mailsplit independent of am.keepcr"),
1553 + PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 0},
1554 OPT_CALLBACK(0, "patch-format", &patch_format, N_("format"),
1555 N_("format the patch(es) are in"),
1556 parse_opt_patchformat),
@@ -1631,7 +1648,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
1648 argv_array_push(&paths, mkpath("%s/%s", prefix, argv[i]));
1649 }
1650
1634 - am_setup(&state, patch_format, paths.argv);
1651 + am_setup(&state, patch_format, paths.argv, keep_cr);
1652
1653 argv_array_clear(&paths);
1654 }