]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7800-difftool.sh
Merge branch 'ab/perl-i18n'
[thirdparty/git.git] / t / t7800-difftool.sh
CommitLineData
f92f2038
DA
1#!/bin/sh
2#
c8a5672e 3# Copyright (c) 2009, 2010 David Aguilar
f92f2038
DA
4#
5
6test_description='git-difftool
7
8Testing basic diff tool invocation
9'
10
11. ./test-lib.sh
12
13remove_config_vars()
14{
15 # Unset all config variables used by git-difftool
16 git config --unset diff.tool
4cefa495 17 git config --unset diff.guitool
f92f2038 18 git config --unset difftool.test-tool.cmd
a904392e 19 git config --unset difftool.prompt
f92f2038
DA
20 git config --unset merge.tool
21 git config --unset mergetool.test-tool.cmd
a88183f1 22 git config --unset mergetool.prompt
f92f2038
DA
23 return 0
24}
25
26restore_test_defaults()
27{
28 # Restores the test defaults used by several tests
29 remove_config_vars
30 unset GIT_DIFF_TOOL
a904392e 31 unset GIT_DIFFTOOL_PROMPT
f92f2038
DA
32 unset GIT_DIFFTOOL_NO_PROMPT
33 git config diff.tool test-tool &&
34 git config difftool.test-tool.cmd 'cat $LOCAL'
23218bbd 35 git config difftool.bogus-tool.cmd false
f92f2038
DA
36}
37
a904392e
DA
38prompt_given()
39{
40 prompt="$1"
ba959de1
SC
41 test "$prompt" = "Launch 'test-tool' [Y/n]: branch"
42}
43
44stdin_contains()
45{
46 grep >/dev/null "$1"
47}
48
49stdin_doesnot_contain()
50{
51 ! stdin_contains "$1"
a904392e
DA
52}
53
f92f2038 54# Create a file on master and change it on branch
2c4f3026 55test_expect_success PERL 'setup' '
f92f2038
DA
56 echo master >file &&
57 git add file &&
58 git commit -m "added file" &&
59
60 git checkout -b branch master &&
61 echo branch >file &&
62 git commit -a -m "branch changed file" &&
63 git checkout master
64'
65
66# Configure a custom difftool.<tool>.cmd and use it
2c4f3026 67test_expect_success PERL 'custom commands' '
f92f2038
DA
68 restore_test_defaults &&
69 git config difftool.test-tool.cmd "cat \$REMOTE" &&
70
71 diff=$(git difftool --no-prompt branch) &&
72 test "$diff" = "master" &&
73
74 restore_test_defaults &&
75 diff=$(git difftool --no-prompt branch) &&
76 test "$diff" = "branch"
77'
78
79# Ensures that git-difftool ignores bogus --tool values
2c4f3026 80test_expect_success PERL 'difftool ignores bad --tool values' '
23218bbd 81 diff=$(git difftool --no-prompt --tool=bad-tool branch)
f92f2038
DA
82 test "$?" = 1 &&
83 test "$diff" = ""
84'
85
2c4f3026 86test_expect_success PERL 'difftool honors --gui' '
4cefa495
DA
87 git config merge.tool bogus-tool &&
88 git config diff.tool bogus-tool &&
89 git config diff.guitool test-tool &&
90
91 diff=$(git difftool --no-prompt --gui branch) &&
92 test "$diff" = "branch" &&
93
94 restore_test_defaults
95'
96
2c4f3026 97test_expect_success PERL 'difftool --gui works without configured diff.guitool' '
42accaec
DA
98 git config diff.tool test-tool &&
99
100 diff=$(git difftool --no-prompt --gui branch) &&
101 test "$diff" = "branch" &&
102
103 restore_test_defaults
104'
105
f92f2038 106# Specify the diff tool using $GIT_DIFF_TOOL
2c4f3026 107test_expect_success PERL 'GIT_DIFF_TOOL variable' '
bc0f35ca 108 test_might_fail git config --unset diff.tool &&
f92f2038
DA
109 GIT_DIFF_TOOL=test-tool &&
110 export GIT_DIFF_TOOL &&
111
112 diff=$(git difftool --no-prompt branch) &&
113 test "$diff" = "branch" &&
114
115 restore_test_defaults
116'
117
118# Test the $GIT_*_TOOL variables and ensure
119# that $GIT_DIFF_TOOL always wins unless --tool is specified
2c4f3026 120test_expect_success PERL 'GIT_DIFF_TOOL overrides' '
f92f2038
DA
121 git config diff.tool bogus-tool &&
122 git config merge.tool bogus-tool &&
123
f92f2038 124 GIT_DIFF_TOOL=test-tool &&
f92f2038
DA
125 export GIT_DIFF_TOOL &&
126
127 diff=$(git difftool --no-prompt branch) &&
128 test "$diff" = "branch" &&
129
130 GIT_DIFF_TOOL=bogus-tool &&
131 export GIT_DIFF_TOOL &&
132
133 diff=$(git difftool --no-prompt --tool=test-tool branch) &&
134 test "$diff" = "branch" &&
135
136 restore_test_defaults
137'
138
139# Test that we don't have to pass --no-prompt to difftool
140# when $GIT_DIFFTOOL_NO_PROMPT is true
2c4f3026 141test_expect_success PERL 'GIT_DIFFTOOL_NO_PROMPT variable' '
f92f2038
DA
142 GIT_DIFFTOOL_NO_PROMPT=true &&
143 export GIT_DIFFTOOL_NO_PROMPT &&
144
145 diff=$(git difftool branch) &&
146 test "$diff" = "branch" &&
147
148 restore_test_defaults
149'
150
a904392e
DA
151# git-difftool supports the difftool.prompt variable.
152# Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
2c4f3026 153test_expect_success PERL 'GIT_DIFFTOOL_PROMPT variable' '
a904392e
DA
154 git config difftool.prompt false &&
155 GIT_DIFFTOOL_PROMPT=true &&
156 export GIT_DIFFTOOL_PROMPT &&
157
3319df6f 158 prompt=$(echo | git difftool branch | tail -1) &&
a904392e
DA
159 prompt_given "$prompt" &&
160
161 restore_test_defaults
162'
163
164# Test that we don't have to pass --no-prompt when difftool.prompt is false
2c4f3026 165test_expect_success PERL 'difftool.prompt config variable is false' '
a904392e
DA
166 git config difftool.prompt false &&
167
168 diff=$(git difftool branch) &&
169 test "$diff" = "branch" &&
170
171 restore_test_defaults
172'
173
a88183f1 174# Test that we don't have to pass --no-prompt when mergetool.prompt is false
2c4f3026 175test_expect_success PERL 'difftool merge.prompt = false' '
bc0f35ca 176 test_might_fail git config --unset difftool.prompt &&
a88183f1
DA
177 git config mergetool.prompt false &&
178
179 diff=$(git difftool branch) &&
180 test "$diff" = "branch" &&
181
182 restore_test_defaults
183'
184
a904392e 185# Test that the -y flag can override difftool.prompt = true
2c4f3026 186test_expect_success PERL 'difftool.prompt can overridden with -y' '
a904392e
DA
187 git config difftool.prompt true &&
188
189 diff=$(git difftool -y branch) &&
190 test "$diff" = "branch" &&
191
192 restore_test_defaults
193'
194
195# Test that the --prompt flag can override difftool.prompt = false
2c4f3026 196test_expect_success PERL 'difftool.prompt can overridden with --prompt' '
a904392e
DA
197 git config difftool.prompt false &&
198
199 prompt=$(echo | git difftool --prompt branch | tail -1) &&
200 prompt_given "$prompt" &&
201
202 restore_test_defaults
203'
204
205# Test that the last flag passed on the command-line wins
2c4f3026 206test_expect_success PERL 'difftool last flag wins' '
a904392e
DA
207 diff=$(git difftool --prompt --no-prompt branch) &&
208 test "$diff" = "branch" &&
209
210 restore_test_defaults &&
211
212 prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
213 prompt_given "$prompt" &&
214
215 restore_test_defaults
216'
217
f92f2038
DA
218# git-difftool falls back to git-mergetool config variables
219# so test that behavior here
2c4f3026 220test_expect_success PERL 'difftool + mergetool config variables' '
bc0f35ca 221 remove_config_vars &&
f92f2038
DA
222 git config merge.tool test-tool &&
223 git config mergetool.test-tool.cmd "cat \$LOCAL" &&
224
225 diff=$(git difftool --no-prompt branch) &&
226 test "$diff" = "branch" &&
227
228 # set merge.tool to something bogus, diff.tool to test-tool
229 git config merge.tool bogus-tool &&
230 git config diff.tool test-tool &&
231
232 diff=$(git difftool --no-prompt branch) &&
233 test "$diff" = "branch" &&
234
235 restore_test_defaults
236'
237
2c4f3026 238test_expect_success PERL 'difftool.<tool>.path' '
f92f2038
DA
239 git config difftool.tkdiff.path echo &&
240 diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
241 git config --unset difftool.tkdiff.path &&
242 lines=$(echo "$diff" | grep file | wc -l) &&
1c6f5b52
DA
243 test "$lines" -eq 1 &&
244
245 restore_test_defaults
246'
247
2c4f3026 248test_expect_success PERL 'difftool --extcmd=cat' '
1c6f5b52 249 diff=$(git difftool --no-prompt --extcmd=cat branch) &&
a9e11220 250 test "$diff" = branch"$LF"master
f47f1e2c 251'
1c6f5b52 252
2c4f3026 253test_expect_success PERL 'difftool --extcmd cat' '
f47f1e2c
DA
254 diff=$(git difftool --no-prompt --extcmd cat branch) &&
255 test "$diff" = branch"$LF"master
256'
1c6f5b52 257
2c4f3026 258test_expect_success PERL 'difftool -x cat' '
f47f1e2c
DA
259 diff=$(git difftool --no-prompt -x cat branch) &&
260 test "$diff" = branch"$LF"master
9f3d54d1
DA
261'
262
2c4f3026 263test_expect_success PERL 'difftool --extcmd echo arg1' '
bc0f35ca 264 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch) &&
9f3d54d1
DA
265 test "$diff" = file
266'
1c6f5b52 267
2c4f3026 268test_expect_success PERL 'difftool --extcmd cat arg1' '
bc0f35ca 269 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch) &&
9f3d54d1
DA
270 test "$diff" = master
271'
1c6f5b52 272
2c4f3026 273test_expect_success PERL 'difftool --extcmd cat arg2' '
bc0f35ca 274 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch) &&
9f3d54d1 275 test "$diff" = branch
f92f2038
DA
276'
277
ba959de1
SC
278# Create a second file on master and a different version on branch
279test_expect_success PERL 'setup with 2 files different' '
280 echo m2 >file2 &&
281 git add file2 &&
282 git commit -m "added file2" &&
283
284 git checkout branch &&
285 echo br2 >file2 &&
286 git add file2 &&
287 git commit -a -m "branch changed file2" &&
288 git checkout master
289'
290
291test_expect_success PERL 'say no to the first file' '
15a31e78 292 diff=$( (echo n; echo) | git difftool -x cat branch ) &&
ba959de1
SC
293
294 echo "$diff" | stdin_contains m2 &&
295 echo "$diff" | stdin_contains br2 &&
296 echo "$diff" | stdin_doesnot_contain master &&
297 echo "$diff" | stdin_doesnot_contain branch
298'
299
300test_expect_success PERL 'say no to the second file' '
15a31e78 301 diff=$( (echo; echo n) | git difftool -x cat branch ) &&
ba959de1
SC
302
303 echo "$diff" | stdin_contains master &&
304 echo "$diff" | stdin_contains branch &&
305 echo "$diff" | stdin_doesnot_contain m2 &&
306 echo "$diff" | stdin_doesnot_contain br2
307'
308
f92f2038 309test_done