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>
maint
Jean-Noël Avila 2022-01-31 22:07:46 +00:00 committed by Junio C Hamano
parent 5d01301f2b
commit a699367bb8
8 changed files with 79 additions and 32 deletions

View File

@ -1242,8 +1242,6 @@ static int parse_and_validate_options(int argc, const char *argv[],
struct commit *current_head, struct commit *current_head,
struct wt_status *s) struct wt_status *s)
{ {
int f = 0;

argc = parse_options(argc, argv, prefix, options, usage, 0); argc = parse_options(argc, argv, prefix, options, usage, 0);
finalize_deferred_config(s); finalize_deferred_config(s);


@ -1251,7 +1249,7 @@ static int parse_and_validate_options(int argc, const char *argv[],
force_author = find_author_by_nickname(force_author); force_author = find_author_by_nickname(force_author);


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


if (logfile || have_option_m || use_message) if (logfile || have_option_m || use_message)
use_editor = 0; use_editor = 0;
@ -1268,20 +1266,16 @@ static int parse_and_validate_options(int argc, const char *argv[],
die(_("You are in the middle of a rebase -- cannot amend.")); die(_("You are in the middle of a rebase -- cannot amend."));
} }
if (fixup_message && squash_message) if (fixup_message && squash_message)
die(_("Options --squash and --fixup cannot be used together")); die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup");
if (use_message) die_for_incompatible_opt4(!!use_message, "-C",
f++; !!edit_message, "-c",
if (edit_message) !!logfile, "-F",
f++; !!fixup_message, "--fixup");
if (fixup_message) die_for_incompatible_opt4(have_option_m, "-m",
f++; !!edit_message, "-c",
if (logfile) !!use_message, "-C",
f++; !!logfile, "-F");
if (f > 1) if (use_message || edit_message || logfile ||fixup_message || have_option_m)
die(_("Only one of -c/-C/-F/--fixup can be used."));
if (have_option_m && (edit_message || use_message || logfile))
die((_("Option -m cannot be combined with -c/-C/-F.")));
if (f || have_option_m)
template_file = NULL; template_file = NULL;
if (edit_message) if (edit_message)
use_message = edit_message; use_message = edit_message;
@ -1306,9 +1300,10 @@ static int parse_and_validate_options(int argc, const char *argv[],
if (patch_interactive) if (patch_interactive)
interactive = 1; interactive = 1;


if (also + only + all + interactive > 1) die_for_incompatible_opt4(also, "-i/--include",
die(_("Only one of --include/--only/--all/--interactive/--patch can be used.")); only, "-o/--only",

all, "-a/--all",
interactive, "--interactive/-p/--patch");
if (fixup_message) { if (fixup_message) {
/* /*
* We limit --fixup's suboptions to only alpha characters. * We limit --fixup's suboptions to only alpha characters.

View File

@ -732,8 +732,9 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
} else if (dir_diff) } else if (dir_diff)
die(_("options '%s' and '%s' cannot be used together"), "--dir-diff", "--no-index"); die(_("options '%s' and '%s' cannot be used together"), "--dir-diff", "--no-index");


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


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

View File

@ -1167,11 +1167,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
if (!show_in_pager && !opt.status_only) if (!show_in_pager && !opt.status_only)
setup_pager(); setup_pager();


if (!use_index && (untracked || cached)) die_for_incompatible_opt3(!use_index, "--no-index",
die(_("--cached or --untracked cannot be used with --no-index")); untracked, "--untracked",

cached, "--cached");
if (untracked && cached)
die(_("--untracked cannot be used with --cached"));


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

View File

@ -1978,8 +1978,9 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
if (rev.show_notes) if (rev.show_notes)
load_display_notes(&rev.notes_opt); load_display_notes(&rev.notes_opt);


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


if (use_stdout) { if (use_stdout) {
setup_pager(); setup_pager();

View File

@ -159,12 +159,14 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)
if (argc < 2) if (argc < 2)
usage_with_options(merge_base_usage, options); usage_with_options(merge_base_usage, options);
if (show_all) if (show_all)
die("--is-ancestor cannot be used with --all"); die(_("options '%s' and '%s' cannot be used together"),
"--is-ancestor", "--all");
return handle_is_ancestor(argc, argv); return handle_is_ancestor(argc, argv);
} }


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


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

View File

@ -1079,3 +1079,37 @@ void NORETURN usage_msg_opt(const char *msg,
die_message("%s\n", msg); /* The extra \n is intentional */ die_message("%s\n", msg); /* The extra \n is intentional */
usage_with_options(usagestr, options); usage_with_options(usagestr, options);
} }

void die_for_incompatible_opt4(int opt1, const char *opt1_name,
int opt2, const char *opt2_name,
int opt3, const char *opt3_name,
int opt4, const char *opt4_name)
{
int count = 0;
const char *options[4];

if (opt1)
options[count++] = opt1_name;
if (opt2)
options[count++] = opt2_name;
if (opt3)
options[count++] = opt3_name;
if (opt4)
options[count++] = opt4_name;
switch (count) {
case 4:
die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
opt1_name, opt2_name, opt3_name, opt4_name);
break;
case 3:
die(_("options '%s', '%s', and '%s' cannot be used together"),
options[0], options[1], options[2]);
break;
case 2:
die(_("options '%s' and '%s' cannot be used together"),
options[0], options[1]);
break;
default:
break;
}
}

View File

@ -225,6 +225,22 @@ NORETURN void usage_msg_opt(const char *msg,
const char * const *usagestr, const char * const *usagestr,
const struct option *options); const struct option *options);


void die_for_incompatible_opt4(int opt1, const char *opt1_name,
int opt2, const char *opt2_name,
int opt3, const char *opt3_name,
int opt4, const char *opt4_name);


static inline void die_for_incompatible_opt3(int opt1, const char *opt1_name,
int opt2, const char *opt2_name,
int opt3, const char *opt3_name)
{
die_for_incompatible_opt4(opt1, opt1_name,
opt2, opt2_name,
opt3, opt3_name,
0, "");
}

/* /*
* Use these assertions for callbacks that expect to be called with NONEG and * Use these assertions for callbacks that expect to be called with NONEG and
* NOARG respectively, and do not otherwise handle the "unset" and "arg" * NOARG respectively, and do not otherwise handle the "unset" and "arg"

View File

@ -442,7 +442,7 @@ test_expect_success '--fixup=reword: give error with pathsec' '
' '


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