Skip to content

Commit a904392

Browse files
davvidgitster
authored andcommitted
difftool: add support for a difftool.prompt config variable
difftool now supports difftool.prompt so that users do not have to pass --no-prompt or hit enter each time a diff tool is launched. The --prompt flag overrides the configuration variable. Signed-off-by: David Aguilar <davvid@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent f92f203 commit a904392

File tree

5 files changed

+96
-6
lines changed

5 files changed

+96
-6
lines changed

Documentation/config.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,9 @@ difftool.<tool>.cmd::
685685
is set to the name of the temporary file containing the contents
686686
of the diff post-image.
687687

688+
difftool.prompt::
689+
Prompt before each invocation of the diff tool.
690+
688691
diff.wordRegex::
689692
A POSIX Extended Regular Expression used to determine what is a "word"
690693
when performing word-by-word difference calculations. Character

Documentation/git-difftool.txt

Lines changed: 9 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] [<'git diff' options>]
10+
'git difftool' [--tool=<tool>] [-y|--no-prompt|--prompt] [<'git diff' options>]
1111

1212
DESCRIPTION
1313
-----------
@@ -21,6 +21,11 @@ OPTIONS
2121
--no-prompt::
2222
Do not prompt before launching a diff tool.
2323

24+
--prompt::
25+
Prompt before each invocation of the diff tool.
26+
This is the default behaviour; the option is provided to
27+
override any configuration settings.
28+
2429
-t <tool>::
2530
--tool=<tool>::
2631
Use the diff tool specified by <tool>.
@@ -72,6 +77,9 @@ difftool.<tool>.cmd::
7277
+
7378
See the `--tool=<tool>` option above for more details.
7479

80+
difftool.prompt::
81+
Prompt before each invocation of the diff tool.
82+
7583
SEE ALSO
7684
--------
7785
linkgit:git-diff[1]::

git-difftool--helper.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55
#
66
# Copyright (c) 2009 David Aguilar
77

8-
# Set GIT_DIFFTOOL_NO_PROMPT to bypass the per-file prompt.
8+
# difftool.prompt controls the default prompt/no-prompt behavior
9+
# and is overridden with $GIT_DIFFTOOL*_PROMPT.
910
should_prompt () {
10-
test -z "$GIT_DIFFTOOL_NO_PROMPT"
11+
prompt=$(git config --bool difftool.prompt || echo true)
12+
if test "$prompt" = true; then
13+
test -z "$GIT_DIFFTOOL_NO_PROMPT"
14+
else
15+
test -n "$GIT_DIFFTOOL_PROMPT"
16+
fi
1117
}
1218

1319
# This function prepares temporary files and launches the appropriate

git-difftool.perl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
# Copyright (c) 2009 David Aguilar
33
#
44
# This is a wrapper around the GIT_EXTERNAL_DIFF-compatible
5-
# git-difftool--helper script. This script exports
6-
# GIT_EXTERNAL_DIFF and GIT_PAGER for use by git, and
7-
# GIT_DIFFTOOL_NO_PROMPT and GIT_DIFF_TOOL for use by git-difftool--helper.
5+
# git-difftool--helper script.
6+
#
7+
# This script exports GIT_EXTERNAL_DIFF and GIT_PAGER for use by git.
8+
# GIT_DIFFTOOL_NO_PROMPT, GIT_DIFFTOOL_PROMPT, and GIT_DIFF_TOOL
9+
# are exported for use by git-difftool--helper.
10+
#
811
# Any arguments that are unknown to this script are forwarded to 'git diff'.
912

1013
use strict;
@@ -62,6 +65,12 @@ sub generate_command
6265
}
6366
if ($arg eq '-y' || $arg eq '--no-prompt') {
6467
$ENV{GIT_DIFFTOOL_NO_PROMPT} = 'true';
68+
delete $ENV{GIT_DIFFTOOL_PROMPT};
69+
next;
70+
}
71+
if ($arg eq '--prompt') {
72+
$ENV{GIT_DIFFTOOL_PROMPT} = 'true';
73+
delete $ENV{GIT_DIFFTOOL_NO_PROMPT};
6574
next;
6675
}
6776
if ($arg eq '-h' || $arg eq '--help') {

t/t7800-difftool.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ remove_config_vars()
1515
# Unset all config variables used by git-difftool
1616
git config --unset diff.tool
1717
git config --unset difftool.test-tool.cmd
18+
git config --unset difftool.prompt
1819
git config --unset merge.tool
1920
git config --unset mergetool.test-tool.cmd
2021
return 0
@@ -26,11 +27,18 @@ restore_test_defaults()
2627
remove_config_vars
2728
unset GIT_DIFF_TOOL
2829
unset GIT_MERGE_TOOL
30+
unset GIT_DIFFTOOL_PROMPT
2931
unset GIT_DIFFTOOL_NO_PROMPT
3032
git config diff.tool test-tool &&
3133
git config difftool.test-tool.cmd 'cat $LOCAL'
3234
}
3335

36+
prompt_given()
37+
{
38+
prompt="$1"
39+
test "$prompt" = "Hit return to launch 'test-tool': branch"
40+
}
41+
3442
# Create a file on master and change it on branch
3543
test_expect_success 'setup' '
3644
echo master >file &&
@@ -116,6 +124,62 @@ test_expect_success 'GIT_DIFFTOOL_NO_PROMPT variable' '
116124
restore_test_defaults
117125
'
118126

127+
# git-difftool supports the difftool.prompt variable.
128+
# Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
129+
test_expect_success 'GIT_DIFFTOOL_PROMPT variable' '
130+
git config difftool.prompt false &&
131+
GIT_DIFFTOOL_PROMPT=true &&
132+
export GIT_DIFFTOOL_PROMPT &&
133+
134+
prompt=$(echo | git difftool --prompt branch | tail -1) &&
135+
prompt_given "$prompt" &&
136+
137+
restore_test_defaults
138+
'
139+
140+
# Test that we don't have to pass --no-prompt when difftool.prompt is false
141+
test_expect_success 'difftool.prompt config variable is false' '
142+
git config difftool.prompt false &&
143+
144+
diff=$(git difftool branch) &&
145+
test "$diff" = "branch" &&
146+
147+
restore_test_defaults
148+
'
149+
150+
# Test that the -y flag can override difftool.prompt = true
151+
test_expect_success 'difftool.prompt can overridden with -y' '
152+
git config difftool.prompt true &&
153+
154+
diff=$(git difftool -y branch) &&
155+
test "$diff" = "branch" &&
156+
157+
restore_test_defaults
158+
'
159+
160+
# Test that the --prompt flag can override difftool.prompt = false
161+
test_expect_success 'difftool.prompt can overridden with --prompt' '
162+
git config difftool.prompt false &&
163+
164+
prompt=$(echo | git difftool --prompt branch | tail -1) &&
165+
prompt_given "$prompt" &&
166+
167+
restore_test_defaults
168+
'
169+
170+
# Test that the last flag passed on the command-line wins
171+
test_expect_success 'difftool last flag wins' '
172+
diff=$(git difftool --prompt --no-prompt branch) &&
173+
test "$diff" = "branch" &&
174+
175+
restore_test_defaults &&
176+
177+
prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
178+
prompt_given "$prompt" &&
179+
180+
restore_test_defaults
181+
'
182+
119183
# git-difftool falls back to git-mergetool config variables
120184
# so test that behavior here
121185
test_expect_success 'difftool + mergetool config variables' '

0 commit comments

Comments
 (0)