Skip to content

Commit 1c6f5b5

Browse files
davvidgitster
authored andcommitted
difftool: Allow specifying unconfigured commands with --extcmd
git-difftool requires difftool.<tool>.cmd configuration even when tools use the standard "$diffcmd $from $to" form. This teaches git-difftool to run these tools in lieu of configuration by allowing the command to be specified on the command line. Reference: http://article.gmane.org/gmane.comp.version-control.git/133377 Signed-off-by: David Aguilar <davvid@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 61ed71d commit 1c6f5b5

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

Documentation/git-difftool.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ 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+
--extcmd=<command>::
62+
Specify a custom command for viewing diffs.
63+
'git-difftool' ignores the configured defaults and runs
64+
`$command $LOCAL $REMOTE` when this option is specified.
65+
6166
-g::
6267
--gui::
6368
When 'git-difftool' is invoked with the `-g` or `--gui` option

git-difftool--helper.sh

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ should_prompt () {
1919
fi
2020
}
2121

22+
# Indicates that --extcmd=... was specified
23+
use_ext_cmd () {
24+
test -n "$GIT_DIFFTOOL_EXTCMD"
25+
}
26+
2227
launch_merge_tool () {
2328
# Merged is the filename as it appears in the work tree
2429
# Local is the contents of a/filename
@@ -33,18 +38,29 @@ launch_merge_tool () {
3338
# the user with the real $MERGED name before launching $merge_tool.
3439
if should_prompt; then
3540
printf "\nViewing: '$MERGED'\n"
36-
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
3747
read ans
3848
fi
3949

40-
run_merge_tool "$merge_tool"
50+
if use_ext_cmd; then
51+
$GIT_DIFFTOOL_EXTCMD "$LOCAL" "$REMOTE"
52+
else
53+
run_merge_tool "$merge_tool"
54+
fi
55+
4156
}
4257

43-
# GIT_DIFF_TOOL indicates that --tool=... was specified
44-
if test -n "$GIT_DIFF_TOOL"; then
45-
merge_tool="$GIT_DIFF_TOOL"
46-
else
47-
merge_tool="$(get_merge_tool)" || exit
58+
if ! use_ext_cmd; then
59+
if test -n "$GIT_DIFF_TOOL"; then
60+
merge_tool="$GIT_DIFF_TOOL"
61+
else
62+
merge_tool="$(get_merge_tool)" || exit
63+
fi
4864
fi
4965

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

git-difftool.perl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ sub generate_command
6262
$skip_next = 1;
6363
next;
6464
}
65+
if ($arg =~ /^--extcmd=/) {
66+
$ENV{GIT_DIFFTOOL_EXTCMD} = substr($arg, 9);
67+
next;
68+
}
6569
if ($arg =~ /^--tool=/) {
6670
$ENV{GIT_DIFF_TOOL} = substr($arg, 7);
6771
next;

t/t7800-difftool.sh

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,24 @@ test_expect_success 'difftool.<tool>.path' '
214214
diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
215215
git config --unset difftool.tkdiff.path &&
216216
lines=$(echo "$diff" | grep file | wc -l) &&
217-
test "$lines" -eq 1
217+
test "$lines" -eq 1 &&
218+
219+
restore_test_defaults
220+
'
221+
222+
test_expect_success 'difftool --extcmd=...' '
223+
diff=$(git difftool --no-prompt --extcmd=cat branch) &&
224+
225+
lines=$(echo "$diff" | wc -l) &&
226+
test "$lines" -eq 2 &&
227+
228+
lines=$(echo "$diff" | grep master | wc -l) &&
229+
test "$lines" -eq 1 &&
230+
231+
lines=$(echo "$diff" | grep branch | wc -l) &&
232+
test "$lines" -eq 1 &&
233+
234+
restore_test_defaults
218235
'
219236

220237
test_done

0 commit comments

Comments
 (0)