diff --git a/Documentation/config/difftool.adoc b/Documentation/config/difftool.adoc index 1b8d48381357aa..31ca494538ea00 100644 --- a/Documentation/config/difftool.adoc +++ b/Documentation/config/difftool.adoc @@ -37,6 +37,14 @@ See the `--trust-exit-code` option in linkgit:git-difftool[1] for more details. `difftool.prompt`:: Prompt before each invocation of the diff tool. +`difftool.dirDiff`:: + Set `true` to perform a directory diff by default (equivalent to + specifying the `--dir-diff` argument). The default is `false`. + The `-d`/`--dir-diff` and `--no-dir-diff` arguments override + this setting. It is ignored when `--no-index` is given. ++ +See the `--dir-diff` option in linkgit:git-difftool[1] for more details. + `difftool.guiDefault`:: Set `true` to use the `diff.guitool` by default (equivalent to specifying the `--gui` argument), or `auto` to select `diff.guitool` or `diff.tool` diff --git a/Documentation/git-difftool.adoc b/Documentation/git-difftool.adoc index dd7cacf95e35df..f4e80e184865c9 100644 --- a/Documentation/git-difftool.adoc +++ b/Documentation/git-difftool.adoc @@ -21,9 +21,14 @@ OPTIONS ------- `-d`:: `--dir-diff`:: +`--no-dir-diff`:: Copy the modified files to a temporary location and perform a directory diff on them. This mode never prompts before launching the diff tool. ++ +`--no-dir-diff` compares the files one at a time, overriding +`difftool.dirDiff`. The default is taken from `difftool.dirDiff`, +or is `--no-dir-diff` if it is unset. `-y`:: `--no-prompt`:: diff --git a/builtin/difftool.c b/builtin/difftool.c index bc7b2ea44396bb..4a979e7458585c 100644 --- a/builtin/difftool.c +++ b/builtin/difftool.c @@ -44,6 +44,7 @@ struct difftool_options { int has_symlinks; int symlinks; int trust_exit_code; + int dir_diff; }; static int difftool_config(const char *var, const char *value, @@ -54,6 +55,10 @@ static int difftool_config(const char *var, const char *value, dt_options->trust_exit_code = git_config_bool(var, value); return 0; } + if (!strcmp(var, "difftool.dirdiff")) { + dt_options->dir_diff = git_config_bool(var, value); + return 0; + } if (!strcmp(var, "core.symlinks")) { dt_options->has_symlinks = git_config_bool(var, value); return 0; @@ -719,12 +724,13 @@ int cmd_difftool(int argc, const char *prefix, struct repository *repo) { - int use_gui_tool = -1, dir_diff = 0, prompt = -1, tool_help = 0, no_index = 0; + int use_gui_tool = -1, dir_diff = -1, prompt = -1, tool_help = 0, no_index = 0; static char *difftool_cmd = NULL, *extcmd = NULL; struct difftool_options dt_options = { .has_symlinks = 1, .symlinks = 1, - .trust_exit_code = 0 + .trust_exit_code = 0, + .dir_diff = 0 }; struct option builtin_difftool_options[] = { OPT_BOOL('g', "gui", &use_gui_tool, @@ -763,6 +769,14 @@ int cmd_difftool(int argc, if (tool_help) return print_tool_help(); + /* + * An explicit -d/--dir-diff or --no-dir-diff wins over + * difftool.dirDiff. The configured default does not apply to + * --no-index, which cannot be combined with directory diff mode. + */ + if (dir_diff < 0) + dir_diff = no_index ? 0 : dt_options.dir_diff; + if (!no_index && !startup_info->have_repository) die(_("difftool requires worktree or --no-index")); diff --git a/t/t7800-difftool.sh b/t/t7800-difftool.sh index 0079aa9da440cf..d510593c862d72 100755 --- a/t/t7800-difftool.sh +++ b/t/t7800-difftool.sh @@ -513,6 +513,90 @@ run_dir_diff_test 'difftool --dir-diff' ' grep "^file$" output ' +test_expect_success 'setup difftool.dirDiff tests' ' + write_script .git/report-mode.sh <<-\EOF && + if test -d "$1" + then + echo dir + else + echo file + fi + EOF + echo dir >expect-dir && + echo file >expect-file +' + +# Runs difftool with the given arguments, and checks that it ran in the +# expected mode: "dir" for a directory diff, "file" for one per file. +difftool_mode_is () { + mode=$1 && + shift && + git difftool --no-prompt --extcmd .git/report-mode.sh "$@" branch >output && + sort -u output >actual && + test_cmp expect-$mode actual +} + +test_expect_success 'difftool defaults to file mode without difftool.dirDiff' ' + difftool_mode_is file +' + +for scope in local global +do + if test $scope = global + then + config=test_config_global + else + config=test_config + fi + + test_expect_success "$scope difftool.dirDiff = true selects dir-diff mode" ' + $config difftool.dirDiff true && + difftool_mode_is dir + ' + + test_expect_success "$scope difftool.dirDiff = false selects file mode" ' + $config difftool.dirDiff false && + difftool_mode_is file + ' + + test_expect_success "$scope difftool.dirDiff = true is overridden by --no-dir-diff" ' + $config difftool.dirDiff true && + difftool_mode_is file --no-dir-diff + ' + + test_expect_success "$scope difftool.dirDiff = false is overridden by -d" ' + $config difftool.dirDiff false && + difftool_mode_is dir -d + ' + + test_expect_success "$scope difftool.dirDiff = false is overridden by --dir-diff" ' + $config difftool.dirDiff false && + difftool_mode_is dir --dir-diff + ' +done + +test_expect_success 'local difftool.dirDiff overrides global' ' + test_config_global difftool.dirDiff true && + test_config difftool.dirDiff false && + difftool_mode_is file && + test_config_global difftool.dirDiff false && + test_config difftool.dirDiff true && + difftool_mode_is dir +' + +test_expect_success 'difftool.dirDiff from the command line' ' + git -c difftool.dirDiff=true difftool --no-prompt \ + --extcmd .git/report-mode.sh branch >output && + sort -u output >actual && + test_cmp expect-dir actual +' + +test_expect_success 'last of -d and --no-dir-diff wins over difftool.dirDiff' ' + test_config difftool.dirDiff true && + difftool_mode_is dir --no-dir-diff -d && + difftool_mode_is file -d --no-dir-diff +' + run_dir_diff_test 'difftool --dir-diff avoids repeated slashes in TMPDIR' ' TMPDIR="${TMPDIR:-/tmp}////" \ git difftool --dir-diff $symlinks --extcmd echo branch >output && @@ -889,6 +973,22 @@ test_expect_success 'outside worktree' ' test_cmp expect actual ' +test_expect_success 'difftool.dirDiff does not apply to --no-index' ' + echo 1 >1 && + echo 2 >2 && + test_expect_code 1 nongit git \ + -c diff.tool=echo -c difftool.echo.cmd="echo \$LOCAL \$REMOTE" \ + -c difftool.dirDiff=true \ + difftool --no-prompt --no-index ../1 ../2 >actual && + echo "../1 ../2" >expect && + test_cmp expect actual +' + +test_expect_success 'difftool --dir-diff --no-index still fails' ' + test_must_fail git difftool --dir-diff --no-index 1 2 2>err && + test_grep "cannot be used together" err +' + test_expect_success 'difftool --gui, --tool and --extcmd are mutually exclusive' ' difftool_test_setup && test_must_fail git difftool --gui --tool=test-tool &&