Skip to content

Commit 2863584

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

File tree

3 files changed

+31
-53
lines changed

3 files changed

+31
-53
lines changed

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);
@@ -115,25 +115,15 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
115115
if (opts->keep_redundant_commits)
116116
opts->allow_empty = 1;
117117

118-
/* Set the subcommand */
119-
if (cmd == 'q')
120-
opts->subcommand = REPLAY_REMOVE_STATE;
121-
else if (cmd == 'c')
122-
opts->subcommand = REPLAY_CONTINUE;
123-
else if (cmd == 'a')
124-
opts->subcommand = REPLAY_ROLLBACK;
125-
else
126-
opts->subcommand = REPLAY_NONE;
127-
128118
/* Check for incompatible command line arguments */
129-
if (opts->subcommand != REPLAY_NONE) {
119+
if (cmd) {
130120
char *this_operation;
131-
if (opts->subcommand == REPLAY_REMOVE_STATE)
121+
if (cmd == 'q')
132122
this_operation = "--quit";
133-
else if (opts->subcommand == REPLAY_CONTINUE)
123+
else if (cmd == 'c')
134124
this_operation = "--continue";
135125
else {
136-
assert(opts->subcommand == REPLAY_ROLLBACK);
126+
assert(cmd == 'a');
137127
this_operation = "--abort";
138128
}
139129

@@ -156,7 +146,7 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
156146
"--edit", opts->edit,
157147
NULL);
158148

159-
if (opts->subcommand != REPLAY_NONE) {
149+
if (cmd) {
160150
opts->revs = NULL;
161151
} else {
162152
struct setup_revision_opt s_r_opt;
@@ -178,6 +168,14 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
178168
/* These option values will be free()d */
179169
opts->gpg_sign = xstrdup_or_null(opts->gpg_sign);
180170
opts->strategy = xstrdup_or_null(opts->strategy);
171+
172+
if (cmd == 'q')
173+
return sequencer_remove_state(opts);
174+
if (cmd == 'c')
175+
return sequencer_continue(opts);
176+
if (cmd == 'a')
177+
return sequencer_rollback(opts);
178+
return sequencer_pick_revisions(opts);
181179
}
182180

183181
int cmd_revert(int argc, const char **argv, const char *prefix)
@@ -189,8 +187,7 @@ int cmd_revert(int argc, const char **argv, const char *prefix)
189187
opts.edit = 1;
190188
opts.action = REPLAY_REVERT;
191189
git_config(git_default_config, NULL);
192-
parse_args(argc, argv, &opts);
193-
res = sequencer_pick_revisions(&opts);
190+
res = run_sequencer(argc, argv, &opts);
194191
if (res < 0)
195192
die(_("revert failed"));
196193
return res;
@@ -203,8 +200,7 @@ int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
203200

204201
opts.action = REPLAY_PICK;
205202
git_config(git_default_config, NULL);
206-
parse_args(argc, argv, &opts);
207-
res = sequencer_pick_revisions(&opts);
203+
res = run_sequencer(argc, argv, &opts);
208204
if (res < 0)
209205
die(_("cherry-pick failed"));
210206
return res;

sequencer.c

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
119119
return 1;
120120
}
121121

122-
static void remove_sequencer_state(const struct replay_opts *opts)
122+
int sequencer_remove_state(struct replay_opts *opts)
123123
{
124124
struct strbuf dir = STRBUF_INIT;
125125
int i;
@@ -133,6 +133,8 @@ static void remove_sequencer_state(const struct replay_opts *opts)
133133
strbuf_addf(&dir, "%s", get_dir(opts));
134134
remove_dir_recursively(&dir, 0);
135135
strbuf_release(&dir);
136+
137+
return 0;
136138
}
137139

138140
static const char *action_name(const struct replay_opts *opts)
@@ -975,7 +977,7 @@ static int rollback_single_pick(void)
975977
return reset_for_rollback(head_sha1);
976978
}
977979

978-
static int sequencer_rollback(struct replay_opts *opts)
980+
int sequencer_rollback(struct replay_opts *opts)
979981
{
980982
FILE *f;
981983
unsigned char sha1[20];
@@ -1010,9 +1012,8 @@ static int sequencer_rollback(struct replay_opts *opts)
10101012
}
10111013
if (reset_for_rollback(sha1))
10121014
goto fail;
1013-
remove_sequencer_state(opts);
10141015
strbuf_release(&buf);
1015-
return 0;
1016+
return sequencer_remove_state(opts);
10161017
fail:
10171018
strbuf_release(&buf);
10181019
return -1;
@@ -1097,8 +1098,7 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
10971098
* Sequence of picks finished successfully; cleanup by
10981099
* removing the .git/sequencer directory
10991100
*/
1100-
remove_sequencer_state(opts);
1101-
return 0;
1101+
return sequencer_remove_state(opts);
11021102
}
11031103

11041104
static int continue_single_pick(void)
@@ -1111,11 +1111,14 @@ static int continue_single_pick(void)
11111111
return run_command_v_opt(argv, RUN_GIT_CMD);
11121112
}
11131113

1114-
static int sequencer_continue(struct replay_opts *opts)
1114+
int sequencer_continue(struct replay_opts *opts)
11151115
{
11161116
struct todo_list todo_list = TODO_LIST_INIT;
11171117
int res;
11181118

1119+
if (read_and_refresh_cache(opts))
1120+
return -1;
1121+
11191122
if (!file_exists(get_todo_path(opts)))
11201123
return continue_single_pick();
11211124
if (read_populate_opts(opts))
@@ -1154,26 +1157,10 @@ int sequencer_pick_revisions(struct replay_opts *opts)
11541157
unsigned char sha1[20];
11551158
int i, res;
11561159

1157-
if (opts->subcommand == REPLAY_NONE)
1158-
assert(opts->revs);
1159-
1160+
assert(opts->revs);
11601161
if (read_and_refresh_cache(opts))
11611162
return -1;
11621163

1163-
/*
1164-
* Decide what to do depending on the arguments; a fresh
1165-
* cherry-pick should be handled differently from an existing
1166-
* one that is being continued
1167-
*/
1168-
if (opts->subcommand == REPLAY_REMOVE_STATE) {
1169-
remove_sequencer_state(opts);
1170-
return 0;
1171-
}
1172-
if (opts->subcommand == REPLAY_ROLLBACK)
1173-
return sequencer_rollback(opts);
1174-
if (opts->subcommand == REPLAY_CONTINUE)
1175-
return sequencer_continue(opts);
1176-
11771164
for (i = 0; i < opts->revs->pending.nr; i++) {
11781165
unsigned char sha1[20];
11791166
const char *name = opts->revs->pending.objects[i].name;

sequencer.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,8 @@ enum replay_action {
1010
REPLAY_PICK
1111
};
1212

13-
enum replay_subcommand {
14-
REPLAY_NONE,
15-
REPLAY_REMOVE_STATE,
16-
REPLAY_CONTINUE,
17-
REPLAY_ROLLBACK
18-
};
19-
2013
struct replay_opts {
2114
enum replay_action action;
22-
enum replay_subcommand subcommand;
2315

2416
/* Boolean options */
2517
int edit;
@@ -44,9 +36,12 @@ struct replay_opts {
4436
/* Only used by REPLAY_NONE */
4537
struct rev_info *revs;
4638
};
47-
#define REPLAY_OPTS_INIT { -1, -1 }
39+
#define REPLAY_OPTS_INIT { -1 }
4840

4941
int sequencer_pick_revisions(struct replay_opts *opts);
42+
int sequencer_continue(struct replay_opts *opts);
43+
int sequencer_rollback(struct replay_opts *opts);
44+
int sequencer_remove_state(struct replay_opts *opts);
5045

5146
extern const char sign_off_header[];
5247

0 commit comments

Comments
 (0)