@@ -668,6 +668,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
668668 const char * hook_arg2 = NULL ;
669669 int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE );
670670 int old_display_comment_prefix ;
671+ int merge_contains_scissors = 0 ;
671672
672673 /* This checks and barfs if author is badly specified */
673674 determine_author_info (author_ident );
@@ -728,6 +729,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
728729 strbuf_addbuf (& sb , & message );
729730 hook_arg1 = "message" ;
730731 } else if (!stat (git_path_merge_msg (the_repository ), & statbuf )) {
732+ size_t merge_msg_start ;
733+
731734 /*
732735 * prepend SQUASH_MSG here if it exists and a
733736 * "merge --squash" was originally performed
@@ -738,8 +741,16 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
738741 hook_arg1 = "squash" ;
739742 } else
740743 hook_arg1 = "merge" ;
744+
745+ merge_msg_start = sb .len ;
741746 if (strbuf_read_file (& sb , git_path_merge_msg (the_repository ), 0 ) < 0 )
742747 die_errno (_ ("could not read MERGE_MSG" ));
748+
749+ if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
750+ wt_status_locate_end (sb .buf + merge_msg_start ,
751+ sb .len - merge_msg_start ) <
752+ sb .len - merge_msg_start )
753+ merge_contains_scissors = 1 ;
743754 } else if (!stat (git_path_squash_msg (the_repository ), & statbuf )) {
744755 if (strbuf_read_file (& sb , git_path_squash_msg (the_repository ), 0 ) < 0 )
745756 die_errno (_ ("could not read SQUASH_MSG" ));
@@ -807,7 +818,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
807818 struct ident_split ci , ai ;
808819
809820 if (whence != FROM_COMMIT ) {
810- if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS )
821+ if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
822+ !merge_contains_scissors )
811823 wt_status_add_cut_line (s -> fp );
812824 status_printf_ln (s , GIT_COLOR_NORMAL ,
813825 whence == FROM_MERGE
@@ -832,10 +844,10 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
832844 _ ("Please enter the commit message for your changes."
833845 " Lines starting\nwith '%c' will be ignored, and an empty"
834846 " message aborts the commit.\n" ), comment_line_char );
835- else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
836- whence == FROM_COMMIT )
837- wt_status_add_cut_line (s -> fp );
838- else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
847+ else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS ) {
848+ if ( whence == FROM_COMMIT && ! merge_contains_scissors )
849+ wt_status_add_cut_line (s -> fp );
850+ } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
839851 status_printf (s , GIT_COLOR_NORMAL ,
840852 _ ("Please enter the commit message for your changes."
841853 " Lines starting\n"
@@ -1172,24 +1184,7 @@ static int parse_and_validate_options(int argc, const char *argv[],
11721184 die (_ ("Only one of --include/--only/--all/--interactive/--patch can be used." ));
11731185 if (argc == 0 && (also || (only && !amend && !allow_empty )))
11741186 die (_ ("No paths with --include/--only does not make sense." ));
1175- if (!cleanup_arg || !strcmp (cleanup_arg , "default" ))
1176- cleanup_mode = use_editor ? COMMIT_MSG_CLEANUP_ALL :
1177- COMMIT_MSG_CLEANUP_SPACE ;
1178- else if (!strcmp (cleanup_arg , "verbatim" ))
1179- cleanup_mode = COMMIT_MSG_CLEANUP_NONE ;
1180- else if (!strcmp (cleanup_arg , "whitespace" ))
1181- cleanup_mode = COMMIT_MSG_CLEANUP_SPACE ;
1182- else if (!strcmp (cleanup_arg , "strip" ))
1183- cleanup_mode = COMMIT_MSG_CLEANUP_ALL ;
1184- else if (!strcmp (cleanup_arg , "scissors" ))
1185- cleanup_mode = use_editor ? COMMIT_MSG_CLEANUP_SCISSORS :
1186- COMMIT_MSG_CLEANUP_SPACE ;
1187- /*
1188- * Please update _git_commit() in git-completion.bash when you
1189- * add new options.
1190- */
1191- else
1192- die (_ ("Invalid cleanup mode %s" ), cleanup_arg );
1187+ cleanup_mode = get_cleanup_mode (cleanup_arg , use_editor );
11931188
11941189 handle_untracked_files_arg (s );
11951190
@@ -1491,7 +1486,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
14911486 OPT_BOOL ('s' , "signoff" , & signoff , N_ ("add Signed-off-by:" )),
14921487 OPT_FILENAME ('t' , "template" , & template_file , N_ ("use specified template file" )),
14931488 OPT_BOOL ('e' , "edit" , & edit_flag , N_ ("force edit of commit" )),
1494- OPT_STRING ( 0 , "cleanup" , & cleanup_arg , N_ ( "default" ), N_ ( "how to strip spaces and #comments from message" ) ),
1489+ OPT_CLEANUP ( & cleanup_arg ),
14951490 OPT_BOOL (0 , "status" , & include_status , N_ ("include status in commit message template" )),
14961491 { OPTION_STRING , 'S' , "gpg-sign" , & sign_commit , N_ ("key-id" ),
14971492 N_ ("GPG sign commit" ), PARSE_OPT_OPTARG , NULL , (intptr_t ) "" },
@@ -1627,11 +1622,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
16271622 die (_ ("could not read commit message: %s" ), strerror (saved_errno ));
16281623 }
16291624
1630- if (verbose || /* Truncate the message just before the diff, if any. */
1631- cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS )
1632- strbuf_setlen (& sb , wt_status_locate_end (sb .buf , sb .len ));
1633- if (cleanup_mode != COMMIT_MSG_CLEANUP_NONE )
1634- strbuf_stripspace (& sb , cleanup_mode == COMMIT_MSG_CLEANUP_ALL );
1625+ cleanup_message (& sb , cleanup_mode , verbose );
16351626
16361627 if (message_is_empty (& sb , cleanup_mode ) && !allow_empty_message ) {
16371628 rollback_index_files ();
0 commit comments