Skip to content

Commit 73533e2

Browse files
committed
sequencer: get rid of the subcommand field
The subcommands are used exactly once, at the very beginning of sequencer_pick_revisions(), to determine what to do. This is an unnecessary level of indirection: we can simply call the correct function to begin with. So let's do that. While at it, ensure that the subcommands return an error code so that they do not have to die() all over the place (bad practice for library functions...). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent dcf2532 commit 73533e2

3 files changed

Lines changed: 34 additions & 54 deletions

File tree

builtin/revert.c

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static void verify_opt_compatible(const char *me, const char *base_opt, ...)
7171
die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
7272
}
7373

74-
static void parse_args(int argc, const char **argv, struct replay_opts *opts)
74+
static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
7575
{
7676
const char * const * usage_str = revert_or_cherry_pick_usage(opts);
7777
const char *me = action_name(opts);
@@ -121,25 +121,15 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
121121
if (opts->keep_redundant_commits)
122122
opts->allow_empty = 1;
123123

124-
/* Set the subcommand */
125-
if (cmd == 'q')
126-
opts->subcommand = REPLAY_REMOVE_STATE;
127-
else if (cmd == 'c')
128-
opts->subcommand = REPLAY_CONTINUE;
129-
else if (cmd == 'a')
130-
opts->subcommand = REPLAY_ROLLBACK;
131-
else
132-
opts->subcommand = REPLAY_NONE;
133-
134124
/* Check for incompatible command line arguments */
135-
if (opts->subcommand != REPLAY_NONE) {
125+
if (cmd) {
136126
char *this_operation;
137-
if (opts->subcommand == REPLAY_REMOVE_STATE)
127+
if (cmd == 'q')
138128
this_operation = "--quit";
139-
else if (opts->subcommand == REPLAY_CONTINUE)
129+
else if (cmd == 'c')
140130
this_operation = "--continue";
141131
else {
142-
assert(opts->subcommand == REPLAY_ROLLBACK);
132+
assert(cmd == 'a');
143133
this_operation = "--abort";
144134
}
145135

@@ -162,7 +152,7 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
162152
"--edit", opts->edit,
163153
NULL);
164154

165-
if (opts->subcommand != REPLAY_NONE) {
155+
if (cmd) {
166156
opts->revs = NULL;
167157
} else {
168158
struct setup_revision_opt s_r_opt;
@@ -180,6 +170,14 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
180170

181171
if (argc > 1)
182172
usage_with_options(usage_str, options);
173+
174+
if (cmd == 'q')
175+
return sequencer_remove_state(opts);
176+
if (cmd == 'c')
177+
return sequencer_continue(opts);
178+
if (cmd == 'a')
179+
return sequencer_rollback(opts);
180+
return sequencer_pick_revisions(opts);
183181
}
184182

185183
int cmd_revert(int argc, const char **argv, const char *prefix)
@@ -191,8 +189,7 @@ int cmd_revert(int argc, const char **argv, const char *prefix)
191189
opts.edit = 1;
192190
opts.action = REPLAY_REVERT;
193191
git_config(git_default_config, NULL);
194-
parse_args(argc, argv, &opts);
195-
res = sequencer_pick_revisions(&opts);
192+
res = run_sequencer(argc, argv, &opts);
196193
if (res < 0)
197194
die(_("revert failed"));
198195
return res;
@@ -205,8 +202,7 @@ int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
205202

206203
opts.action = REPLAY_PICK;
207204
git_config(git_default_config, NULL);
208-
parse_args(argc, argv, &opts);
209-
res = sequencer_pick_revisions(&opts);
205+
res = run_sequencer(argc, argv, &opts);
210206
if (res < 0)
211207
die(_("cherry-pick failed"));
212208
return res;

sequencer.c

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,15 @@ static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
134134
return 1;
135135
}
136136

137-
static void remove_sequencer_state(const struct replay_opts *opts)
137+
int sequencer_remove_state(struct replay_opts *opts)
138138
{
139139
struct strbuf dir = STRBUF_INIT;
140140

141141
strbuf_addf(&dir, "%s", get_dir(opts));
142142
remove_dir_recursively(&dir, 0);
143143
strbuf_release(&dir);
144+
145+
return 0;
144146
}
145147

146148
static const char *action_name(const struct replay_opts *opts)
@@ -941,12 +943,15 @@ static int rollback_single_pick(void)
941943
return reset_for_rollback(head_sha1);
942944
}
943945

944-
static int sequencer_rollback(struct replay_opts *opts)
946+
int sequencer_rollback(struct replay_opts *opts)
945947
{
946948
FILE *f;
947949
unsigned char sha1[20];
948950
struct strbuf buf = STRBUF_INIT;
949951

952+
if (read_and_refresh_cache(opts))
953+
return -1;
954+
950955
f = fopen(git_path_head_file(), "r");
951956
if (!f && errno == ENOENT) {
952957
/*
@@ -973,9 +978,8 @@ static int sequencer_rollback(struct replay_opts *opts)
973978
}
974979
if (reset_for_rollback(sha1))
975980
goto fail;
976-
remove_sequencer_state(opts);
977981
strbuf_release(&buf);
978-
return 0;
982+
return sequencer_remove_state(opts);
979983
fail:
980984
strbuf_release(&buf);
981985
return -1;
@@ -1062,8 +1066,7 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
10621066
* Sequence of picks finished successfully; cleanup by
10631067
* removing the .git/sequencer directory
10641068
*/
1065-
remove_sequencer_state(opts);
1066-
return 0;
1069+
return sequencer_remove_state(opts);
10671070
}
10681071

10691072
static int continue_single_pick(void)
@@ -1076,11 +1079,14 @@ static int continue_single_pick(void)
10761079
return run_command_v_opt(argv, RUN_GIT_CMD);
10771080
}
10781081

1079-
static int sequencer_continue(struct replay_opts *opts)
1082+
int sequencer_continue(struct replay_opts *opts)
10801083
{
10811084
struct todo_list todo_list = TODO_LIST_INIT;
10821085
int res;
10831086

1087+
if (read_and_refresh_cache(opts))
1088+
return -1;
1089+
10841090
if (!file_exists(get_todo_path(opts)))
10851091
return continue_single_pick();
10861092
if (read_populate_opts(opts) ||
@@ -1116,26 +1122,10 @@ int sequencer_pick_revisions(struct replay_opts *opts)
11161122
unsigned char sha1[20];
11171123
int i, res;
11181124

1119-
if (opts->subcommand == REPLAY_NONE)
1120-
assert(opts->revs);
1121-
1125+
assert(opts->revs);
11221126
if (read_and_refresh_cache(opts))
11231127
return -1;
11241128

1125-
/*
1126-
* Decide what to do depending on the arguments; a fresh
1127-
* cherry-pick should be handled differently from an existing
1128-
* one that is being continued
1129-
*/
1130-
if (opts->subcommand == REPLAY_REMOVE_STATE) {
1131-
remove_sequencer_state(opts);
1132-
return 0;
1133-
}
1134-
if (opts->subcommand == REPLAY_ROLLBACK)
1135-
return sequencer_rollback(opts);
1136-
if (opts->subcommand == REPLAY_CONTINUE)
1137-
return sequencer_continue(opts);
1138-
11391129
for (i = 0; i < opts->revs->pending.nr; i++) {
11401130
unsigned char sha1[20];
11411131
const char *name = opts->revs->pending.objects[i].name;

sequencer.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,8 @@ enum replay_action {
1111
REPLAY_INTERACTIVE_REBASE
1212
};
1313

14-
enum replay_subcommand {
15-
REPLAY_NONE,
16-
REPLAY_REMOVE_STATE,
17-
REPLAY_CONTINUE,
18-
REPLAY_ROLLBACK,
19-
REPLAY_SKIP
20-
};
21-
2214
struct replay_opts {
2315
enum replay_action action;
24-
enum replay_subcommand subcommand;
2516

2617
/* Boolean options */
2718
int edit;
@@ -46,9 +37,12 @@ struct replay_opts {
4637
/* Only used by REPLAY_NONE */
4738
struct rev_info *revs;
4839
};
49-
#define REPLAY_OPTS_INIT { -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0, 0, NULL }
40+
#define REPLAY_OPTS_INIT { -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0, 0, NULL }
5041

5142
int sequencer_pick_revisions(struct replay_opts *opts);
43+
int sequencer_continue(struct replay_opts *opts);
44+
int sequencer_rollback(struct replay_opts *opts);
45+
int sequencer_remove_state(struct replay_opts *opts);
5246

5347
extern const char sign_off_header[];
5448

0 commit comments

Comments
 (0)