Skip to content

Commit 789b3ef

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cb7fb9e commit 789b3ef

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

sequencer.c

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,11 @@ N_("you have staged changes in your working tree\n"
602602
"\n"
603603
" git rebase --continue\n");
604604

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+
605610
/*
606611
* If we are cherry-pick, and if the merge did not result in
607612
* hand-editing, we will hit this commit and inherit the original
@@ -615,16 +620,15 @@ N_("you have staged changes in your working tree\n"
615620
* author metadata.
616621
*/
617622
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)
620624
{
621625
struct child_process cmd = CHILD_PROCESS_INIT;
622626
const char *value;
623627

624628
cmd.git_cmd = 1;
625629

626630
if (is_rebase_i(opts)) {
627-
if (!edit) {
631+
if (!(flags & EDIT_MSG)) {
628632
cmd.stdout_to_stderr = 1;
629633
cmd.err = -1;
630634
}
@@ -640,24 +644,24 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,
640644
argv_array_push(&cmd.args, "commit");
641645
argv_array_push(&cmd.args, "-n");
642646

643-
if (amend)
647+
if ((flags & AMEND_MSG))
644648
argv_array_push(&cmd.args, "--amend");
645649
if (opts->gpg_sign)
646650
argv_array_pushf(&cmd.args, "-S%s", opts->gpg_sign);
647651
if (opts->signoff)
648652
argv_array_push(&cmd.args, "-s");
649653
if (defmsg)
650654
argv_array_pushl(&cmd.args, "-F", defmsg, NULL);
651-
if (cleanup_commit_message)
655+
if ((flags & CLEANUP_MSG))
652656
argv_array_push(&cmd.args, "--cleanup=strip");
653-
if (edit)
657+
if ((flags & EDIT_MSG))
654658
argv_array_push(&cmd.args, "-e");
655-
else if (!cleanup_commit_message &&
659+
else if (!(flags & CLEANUP_MSG) &&
656660
!opts->signoff && !opts->record_origin &&
657661
git_config_get_value("commit.cleanup", &value))
658662
argv_array_push(&cmd.args, "--cleanup=verbatim");
659663

660-
if (allow_empty)
664+
if ((flags & ALLOW_EMPTY))
661665
argv_array_push(&cmd.args, "--allow-empty");
662666

663667
if (opts->allow_empty_message)
@@ -926,14 +930,14 @@ static void record_in_rewritten(struct object_id *oid,
926930
static int do_pick_commit(enum todo_command command, struct commit *commit,
927931
struct replay_opts *opts, int final_fixup)
928932
{
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();
931935
unsigned char head[20];
932936
struct commit *base, *next, *parent;
933937
const char *base_label, *next_label;
934938
struct commit_message msg = { NULL, NULL, NULL, NULL };
935939
struct strbuf msgbuf = STRBUF_INIT;
936-
int res, unborn = 0, amend = 0, allow = 0;
940+
int res, unborn = 0, allow;
937941

938942
if (opts->no_commit) {
939943
/*
@@ -991,7 +995,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
991995
opts);
992996
if (res || command != TODO_REWORD)
993997
goto leave;
994-
edit = amend = 1;
998+
flags |= EDIT_MSG | AMEND_MSG;
995999
msg_file = NULL;
9961000
goto fast_forward_edit;
9971001
}
@@ -1046,15 +1050,15 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
10461050
}
10471051

10481052
if (command == TODO_REWORD)
1049-
edit = 1;
1053+
flags |= EDIT_MSG;
10501054
else if (is_fixup(command)) {
10511055
if (update_squash_messages(command, commit, opts))
10521056
return -1;
1053-
amend = 1;
1057+
flags |= AMEND_MSG;
10541058
if (!final_fixup)
10551059
msg_file = rebase_path_squash_msg();
10561060
else if (file_exists(rebase_path_fixup_msg())) {
1057-
cleanup_commit_message = 1;
1061+
flags |= CLEANUP_MSG;
10581062
msg_file = rebase_path_fixup_msg();
10591063
} else {
10601064
const char *dest = git_path("SQUASH_MSG");
@@ -1064,7 +1068,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
10641068
rebase_path_squash_msg(), dest);
10651069
unlink(git_path("MERGE_MSG"));
10661070
msg_file = dest;
1067-
edit = 1;
1071+
flags |= EDIT_MSG;
10681072
}
10691073
}
10701074

@@ -1123,11 +1127,11 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
11231127
if (allow < 0) {
11241128
res = allow;
11251129
goto leave;
1126-
}
1130+
} else if (allow)
1131+
flags |= ALLOW_EMPTY;
11271132
if (!opts->no_commit)
11281133
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);
11311135

11321136
if (!res && final_fixup) {
11331137
unlink(rebase_path_fixup_msg());
@@ -2154,7 +2158,7 @@ static int continue_single_pick(void)
21542158

21552159
static int commit_staged_changes(struct replay_opts *opts)
21562160
{
2157-
int amend = 0;
2161+
unsigned int flags = ALLOW_EMPTY | EDIT_MSG;
21582162

21592163
if (has_unstaged_changes(1))
21602164
return error(_("cannot rebase: You have unstaged changes."));
@@ -2184,10 +2188,10 @@ static int commit_staged_changes(struct replay_opts *opts)
21842188
"--continue' again."));
21852189

21862190
strbuf_release(&rev);
2187-
amend = 1;
2191+
flags |= AMEND_MSG;
21882192
}
21892193

2190-
if (run_git_commit(rebase_path_message(), opts, 1, 1, amend, 0))
2194+
if (run_git_commit(rebase_path_message(), opts, flags))
21912195
return error(_("could not commit staged changes."));
21922196
unlink(rebase_path_amend());
21932197
return 0;

0 commit comments

Comments
 (0)