Skip to content

Commit f922df8

Browse files
committed
Merge branch 'da/difftool'
* da/difftool: difftool: Update copyright notices to list each year separately difftool: Use eval to expand '--extcmd' expressions difftool: Add '-x' and as an alias for '--extcmd' t7800-difftool.sh: Simplify the --extcmd test git-diff.txt: Link to git-difftool difftool: Allow specifying unconfigured commands with --extcmd difftool--helper: Remove use of the GIT_MERGE_TOOL variable difftool--helper: Update copyright and remove distracting comments git-difftool: Add '--gui' for selecting a GUI tool t7800-difftool: Set a bogus tool for use by tests
2 parents 668993f + c8a5672 commit f922df8

File tree

5 files changed

+118
-27
lines changed

5 files changed

+118
-27
lines changed

Documentation/git-diff.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ $ git diff -R <2>
157157
rewrites (very expensive).
158158
<2> Output diff in reverse.
159159

160+
SEE ALSO
161+
--------
162+
linkgit:git-difftool[1]::
163+
Show changes using common diff tools
160164

161165
Author
162166
------

Documentation/git-difftool.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ git-difftool - Show changes using common diff tools
77

88
SYNOPSIS
99
--------
10-
'git difftool' [--tool=<tool>] [-y|--no-prompt|--prompt] [<'git diff' options>]
10+
'git difftool' [<options>] <commit>{0,2} [--] [<path>...]
1111

1212
DESCRIPTION
1313
-----------
@@ -58,6 +58,18 @@ is set to the name of the temporary file containing the contents
5858
of the diff post-image. `$BASE` is provided for compatibility
5959
with custom merge tool commands and has the same value as `$LOCAL`.
6060

61+
-x <command>::
62+
--extcmd=<command>::
63+
Specify a custom command for viewing diffs.
64+
'git-difftool' ignores the configured defaults and runs
65+
`$command $LOCAL $REMOTE` when this option is specified.
66+
67+
-g::
68+
--gui::
69+
When 'git-difftool' is invoked with the `-g` or `--gui` option
70+
the default diff tool will be read from the configured
71+
`diff.guitool` variable instead of `diff.tool`.
72+
6173
See linkgit:git-diff[1] for the full list of supported options.
6274

6375
CONFIG VARIABLES
@@ -68,6 +80,9 @@ difftool equivalents have not been defined.
6880
diff.tool::
6981
The default diff tool to use.
7082

83+
diff.guitool::
84+
The default diff tool to use when `--gui` is specified.
85+
7186
difftool.<tool>.path::
7287
Override the path for the given tool. This is useful in case
7388
your tool is not in the PATH.

git-difftool--helper.sh

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
# This script is typically launched by using the 'git difftool'
44
# convenience command.
55
#
6-
# Copyright (c) 2009 David Aguilar
6+
# Copyright (c) 2009, 2010 David Aguilar
77

8-
# Load common functions from git-mergetool--lib
98
TOOL_MODE=diff
109
. git-mergetool--lib
1110

@@ -20,7 +19,11 @@ should_prompt () {
2019
fi
2120
}
2221

23-
# Sets up shell variables and runs a merge tool
22+
# Indicates that --extcmd=... was specified
23+
use_ext_cmd () {
24+
test -n "$GIT_DIFFTOOL_EXTCMD"
25+
}
26+
2427
launch_merge_tool () {
2528
# Merged is the filename as it appears in the work tree
2629
# Local is the contents of a/filename
@@ -35,20 +38,28 @@ launch_merge_tool () {
3538
# the user with the real $MERGED name before launching $merge_tool.
3639
if should_prompt; then
3740
printf "\nViewing: '$MERGED'\n"
38-
printf "Hit return to launch '%s': " "$merge_tool"
41+
if use_ext_cmd; then
42+
printf "Hit return to launch '%s': " \
43+
"$GIT_DIFFTOOL_EXTCMD"
44+
else
45+
printf "Hit return to launch '%s': " "$merge_tool"
46+
fi
3947
read ans
4048
fi
4149

42-
# Run the appropriate merge tool command
43-
run_merge_tool "$merge_tool"
50+
if use_ext_cmd; then
51+
eval $GIT_DIFFTOOL_EXTCMD '"$LOCAL"' '"$REMOTE"'
52+
else
53+
run_merge_tool "$merge_tool"
54+
fi
4455
}
4556

46-
# Allow GIT_DIFF_TOOL and GIT_MERGE_TOOL to provide default values
47-
test -n "$GIT_MERGE_TOOL" && merge_tool="$GIT_MERGE_TOOL"
48-
test -n "$GIT_DIFF_TOOL" && merge_tool="$GIT_DIFF_TOOL"
49-
50-
if test -z "$merge_tool"; then
51-
merge_tool="$(get_merge_tool)" || exit
57+
if ! use_ext_cmd; then
58+
if test -n "$GIT_DIFF_TOOL"; then
59+
merge_tool="$GIT_DIFF_TOOL"
60+
else
61+
merge_tool="$(get_merge_tool)" || exit
62+
fi
5263
fi
5364

5465
# Launch the merge tool on each path provided by 'git diff'

git-difftool.perl

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env perl
2-
# Copyright (c) 2009 David Aguilar
2+
# Copyright (c) 2009, 2010 David Aguilar
33
#
44
# This is a wrapper around the GIT_EXTERNAL_DIFF-compatible
55
# git-difftool--helper script.
@@ -15,13 +15,17 @@
1515
use Cwd qw(abs_path);
1616
use File::Basename qw(dirname);
1717

18+
require Git;
19+
1820
my $DIR = abs_path(dirname($0));
1921

2022

2123
sub usage
2224
{
2325
print << 'USAGE';
24-
usage: git difftool [--tool=<tool>] [-y|--no-prompt] ["git diff" options]
26+
usage: git difftool [-t|--tool=<tool>] [-x|--extcmd=<cmd>]
27+
[-y|--no-prompt] [-g|--gui]
28+
['git diff' options]
2529
USAGE
2630
exit 1;
2731
}
@@ -63,6 +67,24 @@ sub generate_command
6367
$ENV{GIT_DIFF_TOOL} = substr($arg, 7);
6468
next;
6569
}
70+
if ($arg eq '-x' || $arg eq '--extcmd') {
71+
usage() if $#ARGV <= $idx;
72+
$ENV{GIT_DIFFTOOL_EXTCMD} = $ARGV[$idx + 1];
73+
$skip_next = 1;
74+
next;
75+
}
76+
if ($arg =~ /^--extcmd=/) {
77+
$ENV{GIT_DIFFTOOL_EXTCMD} = substr($arg, 9);
78+
next;
79+
}
80+
if ($arg eq '-g' || $arg eq '--gui') {
81+
my $tool = Git::command_oneline('config',
82+
'diff.guitool');
83+
if (length($tool)) {
84+
$ENV{GIT_DIFF_TOOL} = $tool;
85+
}
86+
next;
87+
}
6688
if ($arg eq '-y' || $arg eq '--no-prompt') {
6789
$ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
6890
delete $ENV{GIT_DIFFTOOL_PROMPT};

t/t7800-difftool.sh

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22
#
3-
# Copyright (c) 2009 David Aguilar
3+
# Copyright (c) 2009, 2010 David Aguilar
44
#
55

66
test_description='git-difftool
@@ -15,10 +15,14 @@ if ! test_have_prereq PERL; then
1515
test_done
1616
fi
1717

18+
LF='
19+
'
20+
1821
remove_config_vars()
1922
{
2023
# Unset all config variables used by git-difftool
2124
git config --unset diff.tool
25+
git config --unset diff.guitool
2226
git config --unset difftool.test-tool.cmd
2327
git config --unset difftool.prompt
2428
git config --unset merge.tool
@@ -31,11 +35,11 @@ restore_test_defaults()
3135
# Restores the test defaults used by several tests
3236
remove_config_vars
3337
unset GIT_DIFF_TOOL
34-
unset GIT_MERGE_TOOL
3538
unset GIT_DIFFTOOL_PROMPT
3639
unset GIT_DIFFTOOL_NO_PROMPT
3740
git config diff.tool test-tool &&
3841
git config difftool.test-tool.cmd 'cat $LOCAL'
42+
git config difftool.bogus-tool.cmd false
3943
}
4044

4145
prompt_given()
@@ -71,11 +75,22 @@ test_expect_success 'custom commands' '
7175

7276
# Ensures that git-difftool ignores bogus --tool values
7377
test_expect_success 'difftool ignores bad --tool values' '
74-
diff=$(git difftool --no-prompt --tool=bogus-tool branch)
78+
diff=$(git difftool --no-prompt --tool=bad-tool branch)
7579
test "$?" = 1 &&
7680
test "$diff" = ""
7781
'
7882

83+
test_expect_success 'difftool honors --gui' '
84+
git config merge.tool bogus-tool &&
85+
git config diff.tool bogus-tool &&
86+
git config diff.guitool test-tool &&
87+
88+
diff=$(git difftool --no-prompt --gui branch) &&
89+
test "$diff" = "branch" &&
90+
91+
restore_test_defaults
92+
'
93+
7994
# Specify the diff tool using $GIT_DIFF_TOOL
8095
test_expect_success 'GIT_DIFF_TOOL variable' '
8196
git config --unset diff.tool
@@ -94,15 +109,7 @@ test_expect_success 'GIT_DIFF_TOOL overrides' '
94109
git config diff.tool bogus-tool &&
95110
git config merge.tool bogus-tool &&
96111
97-
GIT_MERGE_TOOL=test-tool &&
98-
export GIT_MERGE_TOOL &&
99-
diff=$(git difftool --no-prompt branch) &&
100-
test "$diff" = "branch" &&
101-
unset GIT_MERGE_TOOL &&
102-
103-
GIT_MERGE_TOOL=bogus-tool &&
104112
GIT_DIFF_TOOL=test-tool &&
105-
export GIT_MERGE_TOOL &&
106113
export GIT_DIFF_TOOL &&
107114
108115
diff=$(git difftool --no-prompt branch) &&
@@ -210,7 +217,39 @@ test_expect_success 'difftool.<tool>.path' '
210217
diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
211218
git config --unset difftool.tkdiff.path &&
212219
lines=$(echo "$diff" | grep file | wc -l) &&
213-
test "$lines" -eq 1
220+
test "$lines" -eq 1 &&
221+
222+
restore_test_defaults
223+
'
224+
225+
test_expect_success 'difftool --extcmd=cat' '
226+
diff=$(git difftool --no-prompt --extcmd=cat branch) &&
227+
test "$diff" = branch"$LF"master
228+
'
229+
230+
test_expect_success 'difftool --extcmd cat' '
231+
diff=$(git difftool --no-prompt --extcmd cat branch) &&
232+
test "$diff" = branch"$LF"master
233+
'
234+
235+
test_expect_success 'difftool -x cat' '
236+
diff=$(git difftool --no-prompt -x cat branch) &&
237+
test "$diff" = branch"$LF"master
238+
'
239+
240+
test_expect_success 'difftool --extcmd echo arg1' '
241+
diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch)
242+
test "$diff" = file
243+
'
244+
245+
test_expect_success 'difftool --extcmd cat arg1' '
246+
diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch)
247+
test "$diff" = master
248+
'
249+
250+
test_expect_success 'difftool --extcmd cat arg2' '
251+
diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch)
252+
test "$diff" = branch
214253
'
215254

216255
test_done

0 commit comments

Comments
 (0)