diff mbox series

[4/5] difftool: make --gui, --tool and --extcmd exclusive

Message ID c019926b32016369e9f497d8e227107fdf192440.1555880168.git.liu.denton@gmail.com (mailing list archive)
State New, archived
Headers show
Series difftool and mergetool improvements | expand

Commit Message

Denton Liu April 22, 2019, 5:07 a.m. UTC
In git-difftool, these options specify which tool to ultimately run. As
a result, they are logically conflicting. Explicitly disallow these
options from being used together.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 builtin/difftool.c  | 11 ++++++++++-
 t/t7800-difftool.sh |  8 ++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

Comments

Eric Sunshine April 22, 2019, 7:03 a.m. UTC | #1
On Mon, Apr 22, 2019 at 1:07 AM Denton Liu <liu.denton@gmail.com> wrote:
> In git-difftool, these options specify which tool to ultimately run. As
> a result, they are logically conflicting. Explicitly disallow these
> options from being used together.
>
> Signed-off-by: Denton Liu <liu.denton@gmail.com>
> ---
> diff --git a/builtin/difftool.c b/builtin/difftool.c
> @@ -731,6 +731,15 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
> +       if (use_gui_tool)
> +               count++;
> +       if (difftool_cmd)
> +               count++;
> +       if (extcmd)
> +               count++;
> +       if (count > 1)
> +               die(_("--gui, --tool and --extcmd are exclusive"));

The more typical way to check this condition in this codebase is:

    if (use_gui_tool + !!difftool_cmd + !!extcmd > 1)
        die(...);

Also, I think you might want: s/exclusive/mutually &/
diff mbox series

Patch

diff --git a/builtin/difftool.c b/builtin/difftool.c
index a3ea60ea71..5ad39c9172 100644
--- a/builtin/difftool.c
+++ b/builtin/difftool.c
@@ -690,7 +690,7 @@  static int run_file_diff(int prompt, const char *prefix,
 int cmd_difftool(int argc, const char **argv, const char *prefix)
 {
 	int use_gui_tool = 0, dir_diff = 0, prompt = -1, symlinks = 0,
-	    tool_help = 0;
+	    tool_help = 0, count = 0;
 	static char *difftool_cmd = NULL, *extcmd = NULL;
 	struct option builtin_difftool_options[] = {
 		OPT_BOOL('g', "gui", &use_gui_tool,
@@ -731,6 +731,15 @@  int cmd_difftool(int argc, const char **argv, const char *prefix)
 	setenv(GIT_DIR_ENVIRONMENT, absolute_path(get_git_dir()), 1);
 	setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(get_git_work_tree()), 1);
 
+	if (use_gui_tool)
+		count++;
+	if (difftool_cmd)
+		count++;
+	if (extcmd)
+		count++;
+	if (count > 1)
+		die(_("--gui, --tool and --extcmd are exclusive"));
+
 	if (use_gui_tool && diff_gui_tool && *diff_gui_tool)
 		setenv("GIT_DIFF_TOOL", diff_gui_tool, 1);
 	else if (difftool_cmd) {
diff --git a/t/t7800-difftool.sh b/t/t7800-difftool.sh
index bb9a7f4ff9..107f31213d 100755
--- a/t/t7800-difftool.sh
+++ b/t/t7800-difftool.sh
@@ -705,4 +705,12 @@  test_expect_success SYMLINKS 'difftool --dir-diff handles modified symlinks' '
 	test_cmp expect actual
 '
 
+test_expect_success 'difftool --gui, --tool and --extcmd are exclusive' '
+	difftool_test_setup &&
+	test_must_fail git difftool --gui --tool=test-tool &&
+	test_must_fail git difftool --gui --extcmd=cat &&
+	test_must_fail git difftool --tool=test-tool --extcmd=cat &&
+	test_must_fail git difftool --gui --tool=test-tool --extcmd=cat
+'
+
 test_done