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 <jn.avila@free.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jean-Noël Avila committed
Jan 31, 2022 at 22:07 UTC
a699367bb8749a338aefb092c5d7ac75c69d61e1
8 files changed
+79
-32
builtin/commit.c
+15
-20
@@ -1242,8 +1242,6 @@ static int parse_and_validate_options(int argc, const char *argv[],
1242
struct commit *current_head,
1243
struct wt_status *s)
1244
{
1245
- int f = 0;
1246
-
1245
argc = parse_options(argc, argv, prefix, options, usage, 0);
1246
finalize_deferred_config(s);
1247
@@ -1251,7 +1249,7 @@ static int parse_and_validate_options(int argc, const char *argv[],
1249
force_author = find_author_by_nickname(force_author);
1250
1251
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");
1253
1254
if (logfile || have_option_m || use_message)
1255
use_editor = 0;
@@ -1268,20 +1266,16 @@ static int parse_and_validate_options(int argc, const char *argv[],
1266
die(_("You are in the middle of a rebase -- cannot amend."));
1267
}
1268
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)
1279
template_file = NULL;
1280
if (edit_message)
1281
use_message = edit_message;
@@ -1306,9 +1300,10 @@ static int parse_and_validate_options(int argc, const char *argv[],
1300
if (patch_interactive)
1301
interactive = 1;
1302
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");
1307
if (fixup_message) {
1308
/*
1309
* We limit --fixup's suboptions to only alpha characters.
builtin/difftool.c
+3
-2
@@ -732,8 +732,9 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
732
} else if (dir_diff)
733
die(_("options '%s' and '%s' cannot be used together"), "--dir-diff", "--no-index");
734
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");
738
739
if (use_gui_tool)
740
setenv("GIT_MERGETOOL_GUI", "true", 1);
builtin/grep.c
+3
-5
@@ -1167,11 +1167,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
1167
if (!show_in_pager && !opt.status_only)
1168
setup_pager();
1169
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");
1173
1174
if (!use_index || untracked) {
1175
int use_exclude = (opt_exclude < 0) ? use_index : !!opt_exclude;
builtin/log.c
+3
-2
@@ -1978,8 +1978,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1978
if (rev.show_notes)
1979
load_display_notes(&rev.notes_opt);
1980
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");
1984
1985
if (use_stdout) {
1986
setup_pager();
builtin/merge-base.c
+4
-2
@@ -159,12 +159,14 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)
159
if (argc < 2)
160
usage_with_options(merge_base_usage, options);
161
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");
164
return handle_is_ancestor(argc, argv);
165
}
166
167
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");
170
171
if (cmdmode == 'o')
172
return handle_octopus(argc, argv, show_all);
parse-options.c
+34
@@ -1079,3 +1079,37 @@ void NORETURN usage_msg_opt(const char *msg,
1079
die_message("%s\n", msg); /* The extra \n is intentional */
1080
usage_with_options(usagestr, options);
1081
}
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
+16
@@ -225,6 +225,22 @@ NORETURN void usage_msg_opt(const char *msg,
225
const char * const *usagestr,
226
const struct option *options);
227
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
+
244
/*
245
* Use these assertions for callbacks that expect to be called with NONEG and
246
* NOARG respectively, and do not otherwise handle the "unset" and "arg"
t/t7500-commit-template-squash-signoff.sh
+1
-1
@@ -442,7 +442,7 @@ test_expect_success '--fixup=reword: give error with pathsec' '
442
'
443
444
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 &&
446
test_must_fail git commit --fixup=reword:HEAD~ -F msg 2>actual &&
447
test_cmp expect actual
448
'