commit: extract cleanup_mode functions to sequencer

Signed-off-by: Denton Liu <liu.denton@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Denton Liu committed Apr 17, 2019 at 11:23 UTC f29cd8620d85543dbef572f522c71991b3736387
3 files changed +37 -23
builtin/commit.c
+2 -23
@@ -1172,24 +1172,7 @@ static int parse_and_validate_options(int argc, const char *argv[],
1172 die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
1173 if (argc == 0 && (also || (only && !amend && !allow_empty)))
1174 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);
1175 + cleanup_mode = get_cleanup_mode(cleanup_arg, use_editor);
1176
1177 handle_untracked_files_arg(s);
1178
@@ -1626,11 +1609,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1609 die(_("could not read commit message: %s"), strerror(saved_errno));
1610 }
1611
1629 - if (verbose || /* Truncate the message just before the diff, if any. */
1630 - cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
1631 - strbuf_setlen(&sb, wt_status_locate_end(sb.buf, sb.len));
1632 - if (cleanup_mode != COMMIT_MSG_CLEANUP_NONE)
1633 - strbuf_stripspace(&sb, cleanup_mode == COMMIT_MSG_CLEANUP_ALL);
1612 + cleanup_message(&sb, cleanup_mode, verbose);
1613
1614 if (message_is_empty(&sb, cleanup_mode) && !allow_empty_message) {
1615 rollback_index_files();
sequencer.c
+29
@@ -516,6 +516,25 @@ static int fast_forward_to(struct repository *r,
516 return 0;
517 }
518
519 +enum commit_msg_cleanup_mode get_cleanup_mode(const char *cleanup_arg,
520 + int use_editor)
521 +{
522 + if (!cleanup_arg || !strcmp(cleanup_arg, "default"))
523 + return use_editor ? COMMIT_MSG_CLEANUP_ALL :
524 + COMMIT_MSG_CLEANUP_SPACE;
525 + else if (!strcmp(cleanup_arg, "verbatim"))
526 + return COMMIT_MSG_CLEANUP_NONE;
527 + else if (!strcmp(cleanup_arg, "whitespace"))
528 + return COMMIT_MSG_CLEANUP_SPACE;
529 + else if (!strcmp(cleanup_arg, "strip"))
530 + return COMMIT_MSG_CLEANUP_ALL;
531 + else if (!strcmp(cleanup_arg, "scissors"))
532 + return use_editor ? COMMIT_MSG_CLEANUP_SCISSORS :
533 + COMMIT_MSG_CLEANUP_SPACE;
534 + else
535 + die(_("Invalid cleanup mode %s"), cleanup_arg);
536 +}
537 +
538 void append_conflicts_hint(struct index_state *istate,
539 struct strbuf *msgbuf)
540 {
@@ -1018,6 +1037,16 @@ static int rest_is_empty(const struct strbuf *sb, int start)
1037 return 1;
1038 }
1039
1040 +void cleanup_message(struct strbuf *msgbuf,
1041 + enum commit_msg_cleanup_mode cleanup_mode, int verbose)
1042 +{
1043 + if (verbose || /* Truncate the message just before the diff, if any. */
1044 + cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
1045 + strbuf_setlen(msgbuf, wt_status_locate_end(msgbuf->buf, msgbuf->len));
1046 + if (cleanup_mode != COMMIT_MSG_CLEANUP_NONE)
1047 + strbuf_stripspace(msgbuf, cleanup_mode == COMMIT_MSG_CLEANUP_ALL);
1048 +}
1049 +
1050 /*
1051 * Find out if the message in the strbuf contains only whitespace and
1052 * Signed-off-by lines.
sequencer.h
+6
@@ -117,6 +117,12 @@ int rearrange_squash(struct repository *r);
117 void append_signoff(struct strbuf *msgbuf, size_t ignore_footer, unsigned flag);
118
119 void append_conflicts_hint(struct index_state *istate, struct strbuf *msgbuf);
120 +enum commit_msg_cleanup_mode get_cleanup_mode(const char *cleanup_arg,
121 + int use_editor);
122 +
123 +void cleanup_message(struct strbuf *msgbuf,
124 + enum commit_msg_cleanup_mode cleanup_mode, int verbose);
125 +
126 int message_is_empty(const struct strbuf *sb,
127 enum commit_msg_cleanup_mode cleanup_mode);
128 int template_untouched(const struct strbuf *sb, const char *template_file,