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 <pyoka...@gmail.com>
---
 builtin/am.c | 30 ++++++++++++++++++++++++------
 1 file changed, 24 insertions(+), 6 deletions(-)

diff --git a/builtin/am.c b/builtin/am.c
index 4cec380..1991f36 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -469,7 +469,8 @@ done:
  * Splits out individual patches from `paths`, where each path is either a mbox
  * file or a Maildir. Return 0 on success, -1 on failure.
  */
-static int split_patches_mbox(struct am_state *state, struct string_list 
*paths)
+static int split_patches_mbox(struct am_state *state, struct string_list 
*paths,
+               int keep_cr)
 {
        struct child_process cp = CHILD_PROCESS_INIT;
        struct string_list_item *item;
@@ -480,6 +481,8 @@ static int split_patches_mbox(struct am_state *state, 
struct string_list *paths)
        argv_array_pushf(&cp.args, "-d%d", state->prec);
        argv_array_pushf(&cp.args, "-o%s", state->dir.buf);
        argv_array_push(&cp.args, "-b");
+       if (keep_cr)
+               argv_array_push(&cp.args, "--keep-cr");
        argv_array_push(&cp.args, "--");
 
        for_each_string_list_item(item, paths)
@@ -501,14 +504,22 @@ static int split_patches_mbox(struct am_state *state, 
struct string_list *paths)
  * set to the index of the first patch, and state->last will be set to the
  * index of the last patch.
  *
+ * Set keep_cr to 0 to convert all lines ending with \r\n to end with \n, 1
+ * to disable this behavior, -1 to use the default configured setting.
+ *
  * Returns 0 on success, -1 on failure.
  */
 static int split_patches(struct am_state *state, enum patch_format 
patch_format,
-               struct string_list *paths)
+               struct string_list *paths, int keep_cr)
 {
+       if (keep_cr < 0) {
+               keep_cr = 0;
+               git_config_get_bool("am.keepcr", &keep_cr);
+       }
+
        switch (patch_format) {
        case PATCH_FORMAT_MBOX:
-               return split_patches_mbox(state, paths);
+               return split_patches_mbox(state, paths, keep_cr);
        default:
                die("BUG: invalid patch_format");
        }
@@ -519,7 +530,7 @@ static int split_patches(struct am_state *state, enum 
patch_format patch_format,
  * Setup a new am session for applying patches
  */
 static void am_setup(struct am_state *state, enum patch_format patch_format,
-               struct string_list *paths)
+               struct string_list *paths, int keep_cr)
 {
        unsigned char curr_head[GIT_SHA1_RAWSZ];
        const char *str;
@@ -535,7 +546,7 @@ static void am_setup(struct am_state *state, enum 
patch_format patch_format,
        if (mkdir(state->dir.buf, 0777) < 0 && errno != EEXIST)
                die_errno(_("failed to create directory '%s'"), state->dir.buf);
 
-       if (split_patches(state, patch_format, paths) < 0) {
+       if (split_patches(state, patch_format, paths, keep_cr) < 0) {
                am_destroy(state);
                die(_("Failed to split patches."));
        }
@@ -1371,6 +1382,7 @@ enum resume_mode {
 };
 
 static struct am_state state;
+static int opt_keep_cr = -1;
 static int opt_patch_format;
 static enum resume_mode opt_resume;
 
@@ -1392,6 +1404,12 @@ static struct option am_options[] = {
                N_("pass -b flag to git-mailinfo"), KEEP_NON_PATCH),
        OPT_BOOL('m', "message-id", &state.message_id,
                N_("pass -m flag to git-mailinfo")),
+       { OPTION_SET_INT, 0, "keep-cr", &opt_keep_cr, NULL,
+         N_("pass --keep-cr flag to git-mailsplit for mbox format"),
+         PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
+       { OPTION_SET_INT, 0, "no-keep-cr", &opt_keep_cr, NULL,
+         N_("do not pass --keep-cr flag to git-mailsplit independent of 
am.keepcr"),
+         PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 0},
        OPT_CALLBACK(0, "patch-format", &opt_patch_format, N_("format"),
                N_("format the patch(es) are in"), parse_opt_patchformat),
        OPT_STRING(0, "resolvemsg", &state.resolvemsg, NULL,
@@ -1486,7 +1504,7 @@ int cmd_am(int argc, const char **argv, const char 
*prefix)
                                string_list_append(&paths, mkpath("%s/%s", 
prefix, argv[i]));
                }
 
-               am_setup(&state, opt_patch_format, &paths);
+               am_setup(&state, opt_patch_format, &paths, opt_keep_cr);
 
                string_list_clear(&paths, 0);
        }
-- 
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to