Skip to content

Commit 5f11bec

Browse files
committed
Merge branch 'rs/parse-options-cmdmode'
parse-options improvements for OPT_CMDMODE options. * rs/parse-options-cmdmode: am: simplify --show-current-patch handling parse-options: make CMDMODE errors more precise
2 parents 2d2cd0a + e5cf20e commit 5f11bec

File tree

5 files changed

+183
-125
lines changed

5 files changed

+183
-125
lines changed

builtin/am.c

Lines changed: 44 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,16 @@ enum signoff_type {
9292
SIGNOFF_EXPLICIT /* --signoff was set on the command-line */
9393
};
9494

95-
enum show_patch_type {
96-
SHOW_PATCH_RAW = 0,
97-
SHOW_PATCH_DIFF = 1,
95+
enum resume_type {
96+
RESUME_FALSE = 0,
97+
RESUME_APPLY,
98+
RESUME_RESOLVED,
99+
RESUME_SKIP,
100+
RESUME_ABORT,
101+
RESUME_QUIT,
102+
RESUME_SHOW_PATCH_RAW,
103+
RESUME_SHOW_PATCH_DIFF,
104+
RESUME_ALLOW_EMPTY,
98105
};
99106

100107
enum empty_action {
@@ -2191,7 +2198,7 @@ static void am_abort(struct am_state *state)
21912198
am_destroy(state);
21922199
}
21932200

2194-
static int show_patch(struct am_state *state, enum show_patch_type sub_mode)
2201+
static int show_patch(struct am_state *state, enum resume_type resume_mode)
21952202
{
21962203
struct strbuf sb = STRBUF_INIT;
21972204
const char *patch_path;
@@ -2206,11 +2213,11 @@ static int show_patch(struct am_state *state, enum show_patch_type sub_mode)
22062213
return run_command(&cmd);
22072214
}
22082215

2209-
switch (sub_mode) {
2210-
case SHOW_PATCH_RAW:
2216+
switch (resume_mode) {
2217+
case RESUME_SHOW_PATCH_RAW:
22112218
patch_path = am_path(state, msgnum(state));
22122219
break;
2213-
case SHOW_PATCH_DIFF:
2220+
case RESUME_SHOW_PATCH_DIFF:
22142221
patch_path = am_path(state, "patch");
22152222
break;
22162223
default:
@@ -2257,57 +2264,25 @@ static int parse_opt_patchformat(const struct option *opt, const char *arg, int
22572264
return 0;
22582265
}
22592266

2260-
enum resume_type {
2261-
RESUME_FALSE = 0,
2262-
RESUME_APPLY,
2263-
RESUME_RESOLVED,
2264-
RESUME_SKIP,
2265-
RESUME_ABORT,
2266-
RESUME_QUIT,
2267-
RESUME_SHOW_PATCH,
2268-
RESUME_ALLOW_EMPTY,
2269-
};
2270-
2271-
struct resume_mode {
2272-
enum resume_type mode;
2273-
enum show_patch_type sub_mode;
2274-
};
2275-
22762267
static int parse_opt_show_current_patch(const struct option *opt, const char *arg, int unset)
22772268
{
22782269
int *opt_value = opt->value;
2279-
struct resume_mode *resume = container_of(opt_value, struct resume_mode, mode);
22802270

2271+
BUG_ON_OPT_NEG(unset);
2272+
2273+
if (!arg)
2274+
*opt_value = opt->defval;
2275+
else if (!strcmp(arg, "raw"))
2276+
*opt_value = RESUME_SHOW_PATCH_RAW;
2277+
else if (!strcmp(arg, "diff"))
2278+
*opt_value = RESUME_SHOW_PATCH_DIFF;
22812279
/*
22822280
* Please update $__git_showcurrentpatch in git-completion.bash
22832281
* when you add new options
22842282
*/
2285-
const char *valid_modes[] = {
2286-
[SHOW_PATCH_DIFF] = "diff",
2287-
[SHOW_PATCH_RAW] = "raw"
2288-
};
2289-
int new_value = SHOW_PATCH_RAW;
2290-
2291-
BUG_ON_OPT_NEG(unset);
2292-
2293-
if (arg) {
2294-
for (new_value = 0; new_value < ARRAY_SIZE(valid_modes); new_value++) {
2295-
if (!strcmp(arg, valid_modes[new_value]))
2296-
break;
2297-
}
2298-
if (new_value >= ARRAY_SIZE(valid_modes))
2299-
return error(_("invalid value for '%s': '%s'"),
2300-
"--show-current-patch", arg);
2301-
}
2302-
2303-
if (resume->mode == RESUME_SHOW_PATCH && new_value != resume->sub_mode)
2304-
return error(_("options '%s=%s' and '%s=%s' "
2305-
"cannot be used together"),
2306-
"--show-current-patch", arg,
2307-
"--show-current-patch", valid_modes[resume->sub_mode]);
2308-
2309-
resume->mode = RESUME_SHOW_PATCH;
2310-
resume->sub_mode = new_value;
2283+
else
2284+
return error(_("invalid value for '%s': '%s'"),
2285+
"--show-current-patch", arg);
23112286
return 0;
23122287
}
23132288

@@ -2317,7 +2292,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
23172292
int binary = -1;
23182293
int keep_cr = -1;
23192294
int patch_format = PATCH_FORMAT_UNKNOWN;
2320-
struct resume_mode resume = { .mode = RESUME_FALSE };
2295+
enum resume_type resume_mode = RESUME_FALSE;
23212296
int in_progress;
23222297
int ret = 0;
23232298

@@ -2388,27 +2363,27 @@ int cmd_am(int argc, const char **argv, const char *prefix)
23882363
PARSE_OPT_NOARG),
23892364
OPT_STRING(0, "resolvemsg", &state.resolvemsg, NULL,
23902365
N_("override error message when patch failure occurs")),
2391-
OPT_CMDMODE(0, "continue", &resume.mode,
2366+
OPT_CMDMODE(0, "continue", &resume_mode,
23922367
N_("continue applying patches after resolving a conflict"),
23932368
RESUME_RESOLVED),
2394-
OPT_CMDMODE('r', "resolved", &resume.mode,
2369+
OPT_CMDMODE('r', "resolved", &resume_mode,
23952370
N_("synonyms for --continue"),
23962371
RESUME_RESOLVED),
2397-
OPT_CMDMODE(0, "skip", &resume.mode,
2372+
OPT_CMDMODE(0, "skip", &resume_mode,
23982373
N_("skip the current patch"),
23992374
RESUME_SKIP),
2400-
OPT_CMDMODE(0, "abort", &resume.mode,
2375+
OPT_CMDMODE(0, "abort", &resume_mode,
24012376
N_("restore the original branch and abort the patching operation"),
24022377
RESUME_ABORT),
2403-
OPT_CMDMODE(0, "quit", &resume.mode,
2378+
OPT_CMDMODE(0, "quit", &resume_mode,
24042379
N_("abort the patching operation but keep HEAD where it is"),
24052380
RESUME_QUIT),
2406-
{ OPTION_CALLBACK, 0, "show-current-patch", &resume.mode,
2381+
{ OPTION_CALLBACK, 0, "show-current-patch", &resume_mode,
24072382
"(diff|raw)",
24082383
N_("show the patch being applied"),
24092384
PARSE_OPT_CMDMODE | PARSE_OPT_OPTARG | PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
2410-
parse_opt_show_current_patch, RESUME_SHOW_PATCH },
2411-
OPT_CMDMODE(0, "allow-empty", &resume.mode,
2385+
parse_opt_show_current_patch, RESUME_SHOW_PATCH_RAW },
2386+
OPT_CMDMODE(0, "allow-empty", &resume_mode,
24122387
N_("record the empty patch as an empty commit"),
24132388
RESUME_ALLOW_EMPTY),
24142389
OPT_BOOL(0, "committer-date-is-author-date",
@@ -2463,12 +2438,12 @@ int cmd_am(int argc, const char **argv, const char *prefix)
24632438
* intend to feed us a patch but wanted to continue
24642439
* unattended.
24652440
*/
2466-
if (argc || (resume.mode == RESUME_FALSE && !isatty(0)))
2441+
if (argc || (resume_mode == RESUME_FALSE && !isatty(0)))
24672442
die(_("previous rebase directory %s still exists but mbox given."),
24682443
state.dir);
24692444

2470-
if (resume.mode == RESUME_FALSE)
2471-
resume.mode = RESUME_APPLY;
2445+
if (resume_mode == RESUME_FALSE)
2446+
resume_mode = RESUME_APPLY;
24722447

24732448
if (state.signoff == SIGNOFF_EXPLICIT)
24742449
am_append_signoff(&state);
@@ -2482,7 +2457,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
24822457
* stray directories.
24832458
*/
24842459
if (file_exists(state.dir) && !state.rebasing) {
2485-
if (resume.mode == RESUME_ABORT || resume.mode == RESUME_QUIT) {
2460+
if (resume_mode == RESUME_ABORT || resume_mode == RESUME_QUIT) {
24862461
am_destroy(&state);
24872462
am_state_release(&state);
24882463
return 0;
@@ -2493,7 +2468,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
24932468
state.dir);
24942469
}
24952470

2496-
if (resume.mode)
2471+
if (resume_mode)
24972472
die(_("Resolve operation not in progress, we are not resuming."));
24982473

24992474
for (i = 0; i < argc; i++) {
@@ -2511,7 +2486,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
25112486
strvec_clear(&paths);
25122487
}
25132488

2514-
switch (resume.mode) {
2489+
switch (resume_mode) {
25152490
case RESUME_FALSE:
25162491
am_run(&state, 0);
25172492
break;
@@ -2520,7 +2495,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
25202495
break;
25212496
case RESUME_RESOLVED:
25222497
case RESUME_ALLOW_EMPTY:
2523-
am_resolve(&state, resume.mode == RESUME_ALLOW_EMPTY ? 1 : 0);
2498+
am_resolve(&state, resume_mode == RESUME_ALLOW_EMPTY ? 1 : 0);
25242499
break;
25252500
case RESUME_SKIP:
25262501
am_skip(&state);
@@ -2532,8 +2507,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
25322507
am_rerere_clear();
25332508
am_destroy(&state);
25342509
break;
2535-
case RESUME_SHOW_PATCH:
2536-
ret = show_patch(&state, resume.sub_mode);
2510+
case RESUME_SHOW_PATCH_RAW:
2511+
case RESUME_SHOW_PATCH_DIFF:
2512+
ret = show_patch(&state, resume_mode);
25372513
break;
25382514
default:
25392515
BUG("invalid resume value");

0 commit comments

Comments
 (0)