Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Documentation/config/difftool.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
5 changes: 5 additions & 0 deletions Documentation/git-difftool.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`::
Expand Down
18 changes: 16 additions & 2 deletions builtin/difftool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"));

Expand Down
100 changes: 100 additions & 0 deletions t/t7800-difftool.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down Expand Up @@ -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 &&
Expand Down
Loading