sequencer: make commit options more extensible
So far every time we need to tweak the behaviour of run_git_commit() we have been adding a "int" parameter to it. As the function gains parameters and different callsites having different needs, this is becoming a maintenance burden. When a new knob needs to be added to address a specific need for a single callsite, all the other callsites need to add a "no, I do not want anything special with respect to the new knob" argument. Consolidate the existing four parameters into a flag word to make it more maintainable, as we will be adding a new one to the mix soon. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Mar 23, 2017 at 17:07 UTC
789b3effec89e6f336726de109eb8be9dc60b7a5
1 file changed
+26
-22
sequencer.c
+26
-22
@@ -602,6 +602,11 @@ N_("you have staged changes in your working tree\n"
602
"\n"
603
" git rebase --continue\n");
604
605
+#define ALLOW_EMPTY (1<<0)
606
+#define EDIT_MSG (1<<1)
607
+#define AMEND_MSG (1<<2)
608
+#define CLEANUP_MSG (1<<3)
609
+
610
/*
611
* If we are cherry-pick, and if the merge did not result in
612
* hand-editing, we will hit this commit and inherit the original
@@ -615,8 +620,7 @@ N_("you have staged changes in your working tree\n"
620
* author metadata.
621
*/
622
static int run_git_commit(const char *defmsg, struct replay_opts *opts,
618
- int allow_empty, int edit, int amend,
619
- int cleanup_commit_message)
623
+ unsigned int flags)
624
{
625
struct child_process cmd = CHILD_PROCESS_INIT;
626
const char *value;
@@ -624,7 +628,7 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,
628
cmd.git_cmd = 1;
629
630
if (is_rebase_i(opts)) {
627
- if (!edit) {
631
+ if (!(flags & EDIT_MSG)) {
632
cmd.stdout_to_stderr = 1;
633
cmd.err = -1;
634
}
@@ -640,7 +644,7 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,
644
argv_array_push(&cmd.args, "commit");
645
argv_array_push(&cmd.args, "-n");
646
643
- if (amend)
647
+ if ((flags & AMEND_MSG))
648
argv_array_push(&cmd.args, "--amend");
649
if (opts->gpg_sign)
650
argv_array_pushf(&cmd.args, "-S%s", opts->gpg_sign);
@@ -648,16 +652,16 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,
652
argv_array_push(&cmd.args, "-s");
653
if (defmsg)
654
argv_array_pushl(&cmd.args, "-F", defmsg, NULL);
651
- if (cleanup_commit_message)
655
+ if ((flags & CLEANUP_MSG))
656
argv_array_push(&cmd.args, "--cleanup=strip");
653
- if (edit)
657
+ if ((flags & EDIT_MSG))
658
argv_array_push(&cmd.args, "-e");
655
- else if (!cleanup_commit_message &&
659
+ else if (!(flags & CLEANUP_MSG) &&
660
!opts->signoff && !opts->record_origin &&
661
git_config_get_value("commit.cleanup", &value))
662
argv_array_push(&cmd.args, "--cleanup=verbatim");
663
660
- if (allow_empty)
664
+ if ((flags & ALLOW_EMPTY))
665
argv_array_push(&cmd.args, "--allow-empty");
666
667
if (opts->allow_empty_message)
@@ -926,14 +930,14 @@ static void record_in_rewritten(struct object_id *oid,
930
static int do_pick_commit(enum todo_command command, struct commit *commit,
931
struct replay_opts *opts, int final_fixup)
932
{
929
- int edit = opts->edit, cleanup_commit_message = 0;
930
- const char *msg_file = edit ? NULL : git_path_merge_msg();
933
+ unsigned int flags = opts->edit ? EDIT_MSG : 0;
934
+ const char *msg_file = opts->edit ? NULL : git_path_merge_msg();
935
unsigned char head[20];
936
struct commit *base, *next, *parent;
937
const char *base_label, *next_label;
938
struct commit_message msg = { NULL, NULL, NULL, NULL };
939
struct strbuf msgbuf = STRBUF_INIT;
936
- int res, unborn = 0, amend = 0, allow = 0;
940
+ int res, unborn = 0, allow;
941
942
if (opts->no_commit) {
943
/*
@@ -991,7 +995,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
995
opts);
996
if (res || command != TODO_REWORD)
997
goto leave;
994
- edit = amend = 1;
998
+ flags |= EDIT_MSG | AMEND_MSG;
999
msg_file = NULL;
1000
goto fast_forward_edit;
1001
}
@@ -1046,15 +1050,15 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
1050
}
1051
1052
if (command == TODO_REWORD)
1049
- edit = 1;
1053
+ flags |= EDIT_MSG;
1054
else if (is_fixup(command)) {
1055
if (update_squash_messages(command, commit, opts))
1056
return -1;
1053
- amend = 1;
1057
+ flags |= AMEND_MSG;
1058
if (!final_fixup)
1059
msg_file = rebase_path_squash_msg();
1060
else if (file_exists(rebase_path_fixup_msg())) {
1057
- cleanup_commit_message = 1;
1061
+ flags |= CLEANUP_MSG;
1062
msg_file = rebase_path_fixup_msg();
1063
} else {
1064
const char *dest = git_path("SQUASH_MSG");
@@ -1064,7 +1068,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
1068
rebase_path_squash_msg(), dest);
1069
unlink(git_path("MERGE_MSG"));
1070
msg_file = dest;
1067
- edit = 1;
1071
+ flags |= EDIT_MSG;
1072
}
1073
}
1074
@@ -1123,11 +1127,11 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
1127
if (allow < 0) {
1128
res = allow;
1129
goto leave;
1126
- }
1130
+ } else if (allow)
1131
+ flags |= ALLOW_EMPTY;
1132
if (!opts->no_commit)
1133
fast_forward_edit:
1129
- res = run_git_commit(msg_file, opts, allow, edit, amend,
1130
- cleanup_commit_message);
1134
+ res = run_git_commit(msg_file, opts, flags);
1135
1136
if (!res && final_fixup) {
1137
unlink(rebase_path_fixup_msg());
@@ -2154,7 +2158,7 @@ static int continue_single_pick(void)
2158
2159
static int commit_staged_changes(struct replay_opts *opts)
2160
{
2157
- int amend = 0;
2161
+ unsigned int flags = ALLOW_EMPTY | EDIT_MSG;
2162
2163
if (has_unstaged_changes(1))
2164
return error(_("cannot rebase: You have unstaged changes."));
@@ -2184,10 +2188,10 @@ static int commit_staged_changes(struct replay_opts *opts)
2188
"--continue' again."));
2189
2190
strbuf_release(&rev);
2187
- amend = 1;
2191
+ flags |= AMEND_MSG;
2192
}
2193
2190
- if (run_git_commit(rebase_path_message(), opts, 1, 1, amend, 0))
2194
+ if (run_git_commit(rebase_path_message(), opts, flags))
2195
return error(_("could not commit staged changes."));
2196
unlink(rebase_path_amend());
2197
return 0;