Skip to content

Commit f29cd86

Browse files
Denton-Lgitster
authored andcommitted
commit: extract cleanup_mode functions to sequencer
Signed-off-by: Denton Liu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 94ca361 commit f29cd86

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed

builtin/commit.c

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,24 +1172,7 @@ static int parse_and_validate_options(int argc, const char *argv[],
11721172
die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
11731173
if (argc == 0 && (also || (only && !amend && !allow_empty)))
11741174
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);
11931176

11941177
handle_untracked_files_arg(s);
11951178

@@ -1626,11 +1609,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
16261609
die(_("could not read commit message: %s"), strerror(saved_errno));
16271610
}
16281611

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);
16341613

16351614
if (message_is_empty(&sb, cleanup_mode) && !allow_empty_message) {
16361615
rollback_index_files();

sequencer.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,25 @@ static int fast_forward_to(struct repository *r,
516516
return 0;
517517
}
518518

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+
519538
void append_conflicts_hint(struct index_state *istate,
520539
struct strbuf *msgbuf)
521540
{
@@ -1018,6 +1037,16 @@ static int rest_is_empty(const struct strbuf *sb, int start)
10181037
return 1;
10191038
}
10201039

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+
10211050
/*
10221051
* Find out if the message in the strbuf contains only whitespace and
10231052
* Signed-off-by lines.

sequencer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ int rearrange_squash(struct repository *r);
117117
void append_signoff(struct strbuf *msgbuf, size_t ignore_footer, unsigned flag);
118118

119119
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+
120126
int message_is_empty(const struct strbuf *sb,
121127
enum commit_msg_cleanup_mode cleanup_mode);
122128
int template_untouched(const struct strbuf *sb, const char *template_file,

0 commit comments

Comments
 (0)