@@ -502,7 +502,7 @@ static int detect_patch_format(const char **paths)
502502 * Splits out individual email patches from `paths`, where each path is either
503503 * a mbox file or a Maildir. Returns 0 on success, -1 on failure.
504504 */
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 )
506506{
507507 struct child_process cp = CHILD_PROCESS_INIT ;
508508 struct strbuf last = STRBUF_INIT ;
@@ -512,6 +512,8 @@ static int split_mail_mbox(struct am_state *state, const char **paths)
512512 argv_array_pushf (& cp .args , "-d%d" , state -> prec );
513513 argv_array_pushf (& cp .args , "-o%s" , state -> dir );
514514 argv_array_push (& cp .args , "-b" );
515+ if (keep_cr )
516+ argv_array_push (& cp .args , "--keep-cr" );
515517 argv_array_push (& cp .args , "--" );
516518 argv_array_pushv (& cp .args , paths );
517519
@@ -536,14 +538,22 @@ static int split_mail_mbox(struct am_state *state, const char **paths)
536538 * state->cur will be set to the index of the first mail, and state->last will
537539 * be set to the index of the last mail.
538540 *
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+ *
539544 * Returns 0 on success, -1 on failure.
540545 */
541546static int split_mail (struct am_state * state , enum patch_format patch_format ,
542- const char * * paths )
547+ const char * * paths , int keep_cr )
543548{
549+ if (keep_cr < 0 ) {
550+ keep_cr = 0 ;
551+ git_config_get_bool ("am.keepcr" , & keep_cr );
552+ }
553+
544554 switch (patch_format ) {
545555 case PATCH_FORMAT_MBOX :
546- return split_mail_mbox (state , paths );
556+ return split_mail_mbox (state , paths , keep_cr );
547557 default :
548558 die ("BUG: invalid patch_format" );
549559 }
@@ -554,7 +564,7 @@ static int split_mail(struct am_state *state, enum patch_format patch_format,
554564 * Setup a new am session for applying patches
555565 */
556566static void am_setup (struct am_state * state , enum patch_format patch_format ,
557- const char * * paths )
567+ const char * * paths , int keep_cr )
558568{
559569 unsigned char curr_head [GIT_SHA1_RAWSZ ];
560570 const char * str ;
@@ -570,7 +580,7 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
570580 if (mkdir (state -> dir , 0777 ) < 0 && errno != EEXIST )
571581 die_errno (_ ("failed to create directory '%s'" ), state -> dir );
572582
573- if (split_mail (state , patch_format , paths ) < 0 ) {
583+ if (split_mail (state , patch_format , paths , keep_cr ) < 0 ) {
574584 am_destroy (state );
575585 die (_ ("Failed to split patches." ));
576586 }
@@ -1511,6 +1521,7 @@ enum resume_mode {
15111521int cmd_am (int argc , const char * * argv , const char * prefix )
15121522{
15131523 struct am_state state ;
1524+ int keep_cr = -1 ;
15141525 int patch_format = PATCH_FORMAT_UNKNOWN ;
15151526 enum resume_mode resume = RESUME_FALSE ;
15161527
@@ -1534,6 +1545,12 @@ int cmd_am(int argc, const char **argv, const char *prefix)
15341545 N_ ("pass -b flag to git-mailinfo" ), KEEP_NON_PATCH ),
15351546 OPT_BOOL ('m' , "message-id" , & state .message_id ,
15361547 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 },
15371554 OPT_CALLBACK (0 , "patch-format" , & patch_format , N_ ("format" ),
15381555 N_ ("format the patch(es) are in" ),
15391556 parse_opt_patchformat ),
@@ -1631,7 +1648,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
16311648 argv_array_push (& paths , mkpath ("%s/%s" , prefix , argv [i ]));
16321649 }
16331650
1634- am_setup (& state , patch_format , paths .argv );
1651+ am_setup (& state , patch_format , paths .argv , keep_cr );
16351652
16361653 argv_array_clear (& paths );
16371654 }
0 commit comments