Skip to content

Commit a699367

Browse files
jnavilagitster
authored andcommitted
i18n: factorize more 'incompatible options' messages
Find more incompatible options to factorize. When more than two options are mutually exclusive, print the ones which are actually on the command line. Signed-off-by: Jean-Noël Avila <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5d01301 commit a699367

File tree

8 files changed

+79
-32
lines changed

8 files changed

+79
-32
lines changed

builtin/commit.c

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,16 +1242,14 @@ static int parse_and_validate_options(int argc, const char *argv[],
12421242
struct commit *current_head,
12431243
struct wt_status *s)
12441244
{
1245-
int f = 0;
1246-
12471245
argc = parse_options(argc, argv, prefix, options, usage, 0);
12481246
finalize_deferred_config(s);
12491247

12501248
if (force_author && !strchr(force_author, '>'))
12511249
force_author = find_author_by_nickname(force_author);
12521250

12531251
if (force_author && renew_authorship)
1254-
die(_("Using both --reset-author and --author does not make sense"));
1252+
die(_("options '%s' and '%s' cannot be used together"), "--reset-author", "--author");
12551253

12561254
if (logfile || have_option_m || use_message)
12571255
use_editor = 0;
@@ -1268,20 +1266,16 @@ static int parse_and_validate_options(int argc, const char *argv[],
12681266
die(_("You are in the middle of a rebase -- cannot amend."));
12691267
}
12701268
if (fixup_message && squash_message)
1271-
die(_("Options --squash and --fixup cannot be used together"));
1272-
if (use_message)
1273-
f++;
1274-
if (edit_message)
1275-
f++;
1276-
if (fixup_message)
1277-
f++;
1278-
if (logfile)
1279-
f++;
1280-
if (f > 1)
1281-
die(_("Only one of -c/-C/-F/--fixup can be used."));
1282-
if (have_option_m && (edit_message || use_message || logfile))
1283-
die((_("Option -m cannot be combined with -c/-C/-F.")));
1284-
if (f || have_option_m)
1269+
die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup");
1270+
die_for_incompatible_opt4(!!use_message, "-C",
1271+
!!edit_message, "-c",
1272+
!!logfile, "-F",
1273+
!!fixup_message, "--fixup");
1274+
die_for_incompatible_opt4(have_option_m, "-m",
1275+
!!edit_message, "-c",
1276+
!!use_message, "-C",
1277+
!!logfile, "-F");
1278+
if (use_message || edit_message || logfile ||fixup_message || have_option_m)
12851279
template_file = NULL;
12861280
if (edit_message)
12871281
use_message = edit_message;
@@ -1306,9 +1300,10 @@ static int parse_and_validate_options(int argc, const char *argv[],
13061300
if (patch_interactive)
13071301
interactive = 1;
13081302

1309-
if (also + only + all + interactive > 1)
1310-
die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
1311-
1303+
die_for_incompatible_opt4(also, "-i/--include",
1304+
only, "-o/--only",
1305+
all, "-a/--all",
1306+
interactive, "--interactive/-p/--patch");
13121307
if (fixup_message) {
13131308
/*
13141309
* We limit --fixup's suboptions to only alpha characters.

builtin/difftool.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,8 +732,9 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
732732
} else if (dir_diff)
733733
die(_("options '%s' and '%s' cannot be used together"), "--dir-diff", "--no-index");
734734

735-
if (use_gui_tool + !!difftool_cmd + !!extcmd > 1)
736-
die(_("options '%s', '%s', and '%s' cannot be used together"), "--gui", "--tool", "--extcmd");
735+
die_for_incompatible_opt3(use_gui_tool, "--gui",
736+
!!difftool_cmd, "--tool",
737+
!!extcmd, "--extcmd");
737738

738739
if (use_gui_tool)
739740
setenv("GIT_MERGETOOL_GUI", "true", 1);

builtin/grep.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,11 +1167,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
11671167
if (!show_in_pager && !opt.status_only)
11681168
setup_pager();
11691169

1170-
if (!use_index && (untracked || cached))
1171-
die(_("--cached or --untracked cannot be used with --no-index"));
1172-
1173-
if (untracked && cached)
1174-
die(_("--untracked cannot be used with --cached"));
1170+
die_for_incompatible_opt3(!use_index, "--no-index",
1171+
untracked, "--untracked",
1172+
cached, "--cached");
11751173

11761174
if (!use_index || untracked) {
11771175
int use_exclude = (opt_exclude < 0) ? use_index : !!opt_exclude;

builtin/log.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,8 +1978,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
19781978
if (rev.show_notes)
19791979
load_display_notes(&rev.notes_opt);
19801980

1981-
if (use_stdout + rev.diffopt.close_file + !!output_directory > 1)
1982-
die(_("options '%s', '%s', and '%s' cannot be used together"), "--stdout", "--output", "--output-directory");
1981+
die_for_incompatible_opt3(use_stdout, "--stdout",
1982+
rev.diffopt.close_file, "--output",
1983+
!!output_directory, "--output-directory");
19831984

19841985
if (use_stdout) {
19851986
setup_pager();

builtin/merge-base.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,14 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)
159159
if (argc < 2)
160160
usage_with_options(merge_base_usage, options);
161161
if (show_all)
162-
die("--is-ancestor cannot be used with --all");
162+
die(_("options '%s' and '%s' cannot be used together"),
163+
"--is-ancestor", "--all");
163164
return handle_is_ancestor(argc, argv);
164165
}
165166

166167
if (cmdmode == 'r' && show_all)
167-
die("--independent cannot be used with --all");
168+
die(_("options '%s' and '%s' cannot be used together"),
169+
"--independent", "--all");
168170

169171
if (cmdmode == 'o')
170172
return handle_octopus(argc, argv, show_all);

parse-options.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,3 +1079,37 @@ void NORETURN usage_msg_opt(const char *msg,
10791079
die_message("%s\n", msg); /* The extra \n is intentional */
10801080
usage_with_options(usagestr, options);
10811081
}
1082+
1083+
void die_for_incompatible_opt4(int opt1, const char *opt1_name,
1084+
int opt2, const char *opt2_name,
1085+
int opt3, const char *opt3_name,
1086+
int opt4, const char *opt4_name)
1087+
{
1088+
int count = 0;
1089+
const char *options[4];
1090+
1091+
if (opt1)
1092+
options[count++] = opt1_name;
1093+
if (opt2)
1094+
options[count++] = opt2_name;
1095+
if (opt3)
1096+
options[count++] = opt3_name;
1097+
if (opt4)
1098+
options[count++] = opt4_name;
1099+
switch (count) {
1100+
case 4:
1101+
die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
1102+
opt1_name, opt2_name, opt3_name, opt4_name);
1103+
break;
1104+
case 3:
1105+
die(_("options '%s', '%s', and '%s' cannot be used together"),
1106+
options[0], options[1], options[2]);
1107+
break;
1108+
case 2:
1109+
die(_("options '%s' and '%s' cannot be used together"),
1110+
options[0], options[1]);
1111+
break;
1112+
default:
1113+
break;
1114+
}
1115+
}

parse-options.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,22 @@ NORETURN void usage_msg_opt(const char *msg,
225225
const char * const *usagestr,
226226
const struct option *options);
227227

228+
void die_for_incompatible_opt4(int opt1, const char *opt1_name,
229+
int opt2, const char *opt2_name,
230+
int opt3, const char *opt3_name,
231+
int opt4, const char *opt4_name);
232+
233+
234+
static inline void die_for_incompatible_opt3(int opt1, const char *opt1_name,
235+
int opt2, const char *opt2_name,
236+
int opt3, const char *opt3_name)
237+
{
238+
die_for_incompatible_opt4(opt1, opt1_name,
239+
opt2, opt2_name,
240+
opt3, opt3_name,
241+
0, "");
242+
}
243+
228244
/*
229245
* Use these assertions for callbacks that expect to be called with NONEG and
230246
* NOARG respectively, and do not otherwise handle the "unset" and "arg"

t/t7500-commit-template-squash-signoff.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ test_expect_success '--fixup=reword: give error with pathsec' '
442442
'
443443

444444
test_expect_success '--fixup=reword: -F give error message' '
445-
echo "fatal: Only one of -c/-C/-F/--fixup can be used." >expect &&
445+
echo "fatal: options '\''-F'\'' and '\''--fixup'\'' cannot be used together" >expect &&
446446
test_must_fail git commit --fixup=reword:HEAD~ -F msg 2>actual &&
447447
test_cmp expect actual
448448
'

0 commit comments

Comments
 (0)