]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7800-difftool.sh
remote-bzr: fixes for older versions of bzr
[thirdparty/git.git] / t / t7800-difftool.sh
CommitLineData
f92f2038
DA
1#!/bin/sh
2#
9e5a86f2 3# Copyright (c) 2009, 2010, 2012, 2013 David Aguilar
f92f2038
DA
4#
5
6test_description='git-difftool
7
8Testing basic diff tool invocation
9'
10
11. ./test-lib.sh
12
e42360c4 13difftool_test_setup ()
f92f2038 14{
e42360c4
DA
15 test_config diff.tool test-tool &&
16 test_config difftool.test-tool.cmd 'cat "$LOCAL"' &&
17 test_config difftool.bogus-tool.cmd false
f92f2038
DA
18}
19
e42360c4 20prompt_given ()
a904392e
DA
21{
22 prompt="$1"
ba959de1
SC
23 test "$prompt" = "Launch 'test-tool' [Y/n]: branch"
24}
25
f92f2038 26# Create a file on master and change it on branch
2c4f3026 27test_expect_success PERL 'setup' '
f92f2038
DA
28 echo master >file &&
29 git add file &&
30 git commit -m "added file" &&
31
32 git checkout -b branch master &&
33 echo branch >file &&
34 git commit -a -m "branch changed file" &&
35 git checkout master
36'
37
38# Configure a custom difftool.<tool>.cmd and use it
2c4f3026 39test_expect_success PERL 'custom commands' '
e42360c4
DA
40 difftool_test_setup &&
41 test_config difftool.test-tool.cmd "cat \"\$REMOTE\"" &&
42 echo master >expect &&
43 git difftool --no-prompt branch >actual &&
44 test_cmp expect actual &&
f92f2038 45
e42360c4
DA
46 test_config difftool.test-tool.cmd "cat \"\$LOCAL\"" &&
47 echo branch >expect &&
48 git difftool --no-prompt branch >actual &&
49 test_cmp expect actual
f92f2038
DA
50'
51
e42360c4 52test_expect_success PERL 'custom tool commands override built-ins' '
f469e840 53 test_config difftool.vimdiff.cmd "cat \"\$REMOTE\"" &&
e42360c4 54 echo master >expect &&
f469e840 55 git difftool --tool vimdiff --no-prompt branch >actual &&
e42360c4 56 test_cmp expect actual
a427ef7a
DA
57'
58
2c4f3026 59test_expect_success PERL 'difftool ignores bad --tool values' '
e42360c4
DA
60 : >expect &&
61 test_expect_code 1 \
62 git difftool --no-prompt --tool=bad-tool branch >actual &&
63 test_cmp expect actual
f92f2038
DA
64'
65
d50b2c73 66test_expect_success PERL 'difftool forwards arguments to diff' '
e42360c4 67 difftool_test_setup &&
d50b2c73
DA
68 >for-diff &&
69 git add for-diff &&
70 echo changes>for-diff &&
71 git add for-diff &&
e42360c4
DA
72 : >expect &&
73 git difftool --cached --no-prompt -- for-diff >actual &&
74 test_cmp expect actual &&
d50b2c73
DA
75 git reset -- for-diff &&
76 rm for-diff
77'
78
2c4f3026 79test_expect_success PERL 'difftool honors --gui' '
e42360c4
DA
80 difftool_test_setup &&
81 test_config merge.tool bogus-tool &&
82 test_config diff.tool bogus-tool &&
83 test_config diff.guitool test-tool &&
4cefa495 84
e42360c4
DA
85 echo branch >expect &&
86 git difftool --no-prompt --gui branch >actual &&
87 test_cmp expect actual
4cefa495
DA
88'
89
85089604 90test_expect_success PERL 'difftool --gui last setting wins' '
e42360c4
DA
91 difftool_test_setup &&
92 : >expect &&
93 git difftool --no-prompt --gui --no-gui >actual &&
94 test_cmp expect actual &&
85089604 95
e42360c4
DA
96 test_config merge.tool bogus-tool &&
97 test_config diff.tool bogus-tool &&
98 test_config diff.guitool test-tool &&
99 echo branch >expect &&
100 git difftool --no-prompt --no-gui --gui branch >actual &&
101 test_cmp expect actual
85089604
TH
102'
103
2c4f3026 104test_expect_success PERL 'difftool --gui works without configured diff.guitool' '
e42360c4
DA
105 difftool_test_setup &&
106 echo branch >expect &&
107 git difftool --no-prompt --gui branch >actual &&
108 test_cmp expect actual
42accaec
DA
109'
110
f92f2038 111# Specify the diff tool using $GIT_DIFF_TOOL
2c4f3026 112test_expect_success PERL 'GIT_DIFF_TOOL variable' '
e42360c4
DA
113 difftool_test_setup &&
114 git config --unset diff.tool &&
115 echo branch >expect &&
116 GIT_DIFF_TOOL=test-tool git difftool --no-prompt branch >actual &&
117 test_cmp expect actual
f92f2038
DA
118'
119
120# Test the $GIT_*_TOOL variables and ensure
121# that $GIT_DIFF_TOOL always wins unless --tool is specified
2c4f3026 122test_expect_success PERL 'GIT_DIFF_TOOL overrides' '
e42360c4
DA
123 difftool_test_setup &&
124 test_config diff.tool bogus-tool &&
125 test_config merge.tool bogus-tool &&
f92f2038 126
e42360c4
DA
127 echo branch >expect &&
128 GIT_DIFF_TOOL=test-tool git difftool --no-prompt branch >actual &&
129 test_cmp expect actual &&
f92f2038 130
e42360c4
DA
131 test_config diff.tool bogus-tool &&
132 test_config merge.tool bogus-tool &&
133 GIT_DIFF_TOOL=bogus-tool \
134 git difftool --no-prompt --tool=test-tool branch >actual &&
135 test_cmp expect actual
f92f2038
DA
136'
137
138# Test that we don't have to pass --no-prompt to difftool
139# when $GIT_DIFFTOOL_NO_PROMPT is true
2c4f3026 140test_expect_success PERL 'GIT_DIFFTOOL_NO_PROMPT variable' '
e42360c4
DA
141 difftool_test_setup &&
142 echo branch >expect &&
143 GIT_DIFFTOOL_NO_PROMPT=true git difftool branch >actual &&
144 test_cmp expect actual
f92f2038
DA
145'
146
a904392e
DA
147# git-difftool supports the difftool.prompt variable.
148# Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
2c4f3026 149test_expect_success PERL 'GIT_DIFFTOOL_PROMPT variable' '
e42360c4
DA
150 difftool_test_setup &&
151 test_config difftool.prompt false &&
152 echo >input &&
153 GIT_DIFFTOOL_PROMPT=true git difftool branch <input >output &&
154 prompt=$(tail -1 <output) &&
155 prompt_given "$prompt"
a904392e
DA
156'
157
158# Test that we don't have to pass --no-prompt when difftool.prompt is false
2c4f3026 159test_expect_success PERL 'difftool.prompt config variable is false' '
e42360c4
DA
160 difftool_test_setup &&
161 test_config difftool.prompt false &&
162 echo branch >expect &&
163 git difftool branch >actual &&
164 test_cmp expect actual
a904392e
DA
165'
166
a88183f1 167# Test that we don't have to pass --no-prompt when mergetool.prompt is false
2c4f3026 168test_expect_success PERL 'difftool merge.prompt = false' '
e42360c4 169 difftool_test_setup &&
bc0f35ca 170 test_might_fail git config --unset difftool.prompt &&
e42360c4
DA
171 test_config mergetool.prompt false &&
172 echo branch >expect &&
173 git difftool branch >actual &&
174 test_cmp expect actual
a88183f1
DA
175'
176
a904392e 177# Test that the -y flag can override difftool.prompt = true
2c4f3026 178test_expect_success PERL 'difftool.prompt can overridden with -y' '
e42360c4
DA
179 difftool_test_setup &&
180 test_config difftool.prompt true &&
181 echo branch >expect &&
182 git difftool -y branch >actual &&
183 test_cmp expect actual
a904392e
DA
184'
185
186# Test that the --prompt flag can override difftool.prompt = false
2c4f3026 187test_expect_success PERL 'difftool.prompt can overridden with --prompt' '
e42360c4
DA
188 difftool_test_setup &&
189 test_config difftool.prompt false &&
190 echo >input &&
191 git difftool --prompt branch <input >output &&
192 prompt=$(tail -1 <output) &&
193 prompt_given "$prompt"
a904392e
DA
194'
195
196# Test that the last flag passed on the command-line wins
2c4f3026 197test_expect_success PERL 'difftool last flag wins' '
e42360c4
DA
198 difftool_test_setup &&
199 echo branch >expect &&
200 git difftool --prompt --no-prompt branch >actual &&
201 test_cmp expect actual &&
202 echo >input &&
203 git difftool --no-prompt --prompt branch <input >output &&
204 prompt=$(tail -1 <output) &&
205 prompt_given "$prompt"
a904392e
DA
206'
207
f92f2038
DA
208# git-difftool falls back to git-mergetool config variables
209# so test that behavior here
2c4f3026 210test_expect_success PERL 'difftool + mergetool config variables' '
e42360c4
DA
211 test_config merge.tool test-tool &&
212 test_config mergetool.test-tool.cmd "cat \$LOCAL" &&
213 echo branch >expect &&
214 git difftool --no-prompt branch >actual &&
215 test_cmp expect actual &&
f92f2038
DA
216
217 # set merge.tool to something bogus, diff.tool to test-tool
e42360c4
DA
218 test_config merge.tool bogus-tool &&
219 test_config diff.tool test-tool &&
220 git difftool --no-prompt branch >actual &&
221 test_cmp expect actual
f92f2038
DA
222'
223
2c4f3026 224test_expect_success PERL 'difftool.<tool>.path' '
e42360c4
DA
225 test_config difftool.tkdiff.path echo &&
226 git difftool --tool=tkdiff --no-prompt branch >output &&
227 lines=$(grep file output | wc -l) &&
228 test "$lines" -eq 1
1c6f5b52
DA
229'
230
2c4f3026 231test_expect_success PERL 'difftool --extcmd=cat' '
e42360c4
DA
232 echo branch >expect &&
233 echo master >>expect &&
234 git difftool --no-prompt --extcmd=cat branch >actual &&
235 test_cmp expect actual
f47f1e2c 236'
1c6f5b52 237
2c4f3026 238test_expect_success PERL 'difftool --extcmd cat' '
e42360c4
DA
239 echo branch >expect &&
240 echo master >>expect &&
241 git difftool --no-prompt --extcmd=cat branch >actual &&
242 test_cmp expect actual
f47f1e2c 243'
1c6f5b52 244
2c4f3026 245test_expect_success PERL 'difftool -x cat' '
e42360c4
DA
246 echo branch >expect &&
247 echo master >>expect &&
248 git difftool --no-prompt -x cat branch >actual &&
249 test_cmp expect actual
9f3d54d1
DA
250'
251
2c4f3026 252test_expect_success PERL 'difftool --extcmd echo arg1' '
e42360c4
DA
253 echo file >expect &&
254 git difftool --no-prompt \
255 --extcmd sh\ -c\ \"echo\ \$1\" branch >actual &&
256 test_cmp expect actual
9f3d54d1 257'
1c6f5b52 258
2c4f3026 259test_expect_success PERL 'difftool --extcmd cat arg1' '
e42360c4
DA
260 echo master >expect &&
261 git difftool --no-prompt \
262 --extcmd sh\ -c\ \"cat\ \$1\" branch >actual &&
263 test_cmp expect actual
9f3d54d1 264'
1c6f5b52 265
2c4f3026 266test_expect_success PERL 'difftool --extcmd cat arg2' '
e42360c4
DA
267 echo branch >expect &&
268 git difftool --no-prompt \
269 --extcmd sh\ -c\ \"cat\ \$2\" branch >actual &&
270 test_cmp expect actual
f92f2038
DA
271'
272
ba959de1
SC
273# Create a second file on master and a different version on branch
274test_expect_success PERL 'setup with 2 files different' '
275 echo m2 >file2 &&
276 git add file2 &&
277 git commit -m "added file2" &&
278
279 git checkout branch &&
280 echo br2 >file2 &&
281 git add file2 &&
282 git commit -a -m "branch changed file2" &&
283 git checkout master
284'
285
286test_expect_success PERL 'say no to the first file' '
e42360c4
DA
287 (echo n && echo) >input &&
288 git difftool -x cat branch <input >output &&
472353a5
JK
289 grep m2 output &&
290 grep br2 output &&
291 ! grep master output &&
292 ! grep branch output
ba959de1
SC
293'
294
295test_expect_success PERL 'say no to the second file' '
e42360c4
DA
296 (echo && echo n) >input &&
297 git difftool -x cat branch <input >output &&
472353a5
JK
298 grep master output &&
299 grep branch output &&
300 ! grep m2 output &&
301 ! grep br2 output
ba959de1
SC
302'
303
bf73fc21 304test_expect_success PERL 'difftool --tool-help' '
e42360c4 305 git difftool --tool-help >output &&
472353a5 306 grep tool output
bf73fc21
TH
307'
308
7e0abcec
TH
309test_expect_success PERL 'setup change in subdirectory' '
310 git checkout master &&
311 mkdir sub &&
312 echo master >sub/sub &&
313 git add sub/sub &&
314 git commit -m "added sub/sub" &&
315 echo test >>file &&
316 echo test >>sub/sub &&
3caf5a93 317 git add file sub/sub &&
7e0abcec
TH
318 git commit -m "modified both"
319'
320
e01afdb7
JK
321run_dir_diff_test () {
322 test_expect_success PERL "$1 --no-symlinks" "
323 symlinks=--no-symlinks &&
324 $2
325 "
326 test_expect_success PERL,SYMLINKS "$1 --symlinks" "
327 symlinks=--symlinks &&
328 $2
329 "
330}
331
332run_dir_diff_test 'difftool -d' '
333 git difftool -d $symlinks --extcmd ls branch >output &&
472353a5
JK
334 grep sub output &&
335 grep file output
7e0abcec
TH
336'
337
e01afdb7
JK
338run_dir_diff_test 'difftool --dir-diff' '
339 git difftool --dir-diff $symlinks --extcmd ls branch >output &&
472353a5
JK
340 grep sub output &&
341 grep file output
7e0abcec
TH
342'
343
e01afdb7
JK
344run_dir_diff_test 'difftool --dir-diff ignores --prompt' '
345 git difftool --dir-diff $symlinks --prompt --extcmd ls branch >output &&
472353a5
JK
346 grep sub output &&
347 grep file output
bf341b90
JK
348'
349
e01afdb7 350run_dir_diff_test 'difftool --dir-diff from subdirectory' '
bf341b90
JK
351 (
352 cd sub &&
e01afdb7 353 git difftool --dir-diff $symlinks --extcmd ls branch >output &&
472353a5
JK
354 grep sub output &&
355 grep file output
bf341b90
JK
356 )
357'
358
02c56314
JK
359write_script .git/CHECK_SYMLINKS <<\EOF
360for f in file file2 sub/sub
361do
362 echo "$f"
363 readlink "$2/$f"
364done >actual
365EOF
366
367test_expect_success PERL,SYMLINKS 'difftool --dir-diff --symlink without unstaged changes' '
368 cat >expect <<-EOF &&
369 file
370 $(pwd)/file
371 file2
372 $(pwd)/file2
373 sub/sub
374 $(pwd)/sub/sub
375 EOF
376 git difftool --dir-diff --symlink \
377 --extcmd "./.git/CHECK_SYMLINKS" branch HEAD &&
378 test_cmp actual expect
379'
380
67aa147a
JK
381write_script modify-file <<\EOF
382echo "new content" >file
383EOF
384
385test_expect_success PERL 'difftool --no-symlinks does not overwrite working tree file ' '
386 echo "orig content" >file &&
387 git difftool --dir-diff --no-symlinks --extcmd "$(pwd)/modify-file" branch &&
388 echo "new content" >expect &&
389 test_cmp expect file
390'
391
392write_script modify-both-files <<\EOF
393echo "wt content" >file &&
394echo "tmp content" >"$2/file" &&
395echo "$2" >tmpdir
396EOF
397
398test_expect_success PERL 'difftool --no-symlinks detects conflict ' '
399 (
400 TMPDIR=$TRASH_DIRECTORY &&
401 export TMPDIR &&
402 echo "orig content" >file &&
403 test_must_fail git difftool --dir-diff --no-symlinks --extcmd "$(pwd)/modify-both-files" branch &&
404 echo "wt content" >expect &&
405 test_cmp expect file &&
406 echo "tmp content" >expect &&
407 test_cmp expect "$(cat tmpdir)/file"
408 )
409'
410
f92f2038 411test_done