blob: 9c3e997b9d6be69467a78f9ff8b1ff746be8e9b6 [file] [log] [blame]
David Aguilarf92f2032009-04-07 16:30:53 -07001#!/bin/sh
2#
David Aguilarc8a56722010-01-15 19:10:03 -08003# Copyright (c) 2009, 2010 David Aguilar
David Aguilarf92f2032009-04-07 16:30:53 -07004#
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
David Aguilar4cefa492009-12-22 21:27:14 -080017 git config --unset diff.guitool
David Aguilarf92f2032009-04-07 16:30:53 -070018 git config --unset difftool.test-tool.cmd
David Aguilara9043922009-04-07 01:21:22 -070019 git config --unset difftool.prompt
David Aguilarf92f2032009-04-07 16:30:53 -070020 git config --unset merge.tool
21 git config --unset mergetool.test-tool.cmd
David Aguilara88183f2010-01-22 22:03:36 -080022 git config --unset mergetool.prompt
David Aguilarf92f2032009-04-07 16:30:53 -070023 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
David Aguilara9043922009-04-07 01:21:22 -070031 unset GIT_DIFFTOOL_PROMPT
David Aguilarf92f2032009-04-07 16:30:53 -070032 unset GIT_DIFFTOOL_NO_PROMPT
33 git config diff.tool test-tool &&
34 git config difftool.test-tool.cmd 'cat $LOCAL'
David Aguilar23218bb2009-12-22 21:27:13 -080035 git config difftool.bogus-tool.cmd false
David Aguilarf92f2032009-04-07 16:30:53 -070036}
37
David Aguilara9043922009-04-07 01:21:22 -070038prompt_given()
39{
40 prompt="$1"
Sitaram Chamartyba959de2011-10-08 18:40:15 +053041 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"
David Aguilara9043922009-04-07 01:21:22 -070052}
53
David Aguilarf92f2032009-04-07 16:30:53 -070054# Create a file on master and change it on branch
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +000055test_expect_success PERL 'setup' '
David Aguilarf92f2032009-04-07 16:30:53 -070056 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
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +000067test_expect_success PERL 'custom commands' '
David Aguilarf92f2032009-04-07 16:30:53 -070068 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
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +000080test_expect_success PERL 'difftool ignores bad --tool values' '
David Aguilar23218bb2009-12-22 21:27:13 -080081 diff=$(git difftool --no-prompt --tool=bad-tool branch)
David Aguilarf92f2032009-04-07 16:30:53 -070082 test "$?" = 1 &&
83 test "$diff" = ""
84'
85
David Aguilard50b2c72012-03-16 20:54:37 -070086test_expect_success PERL 'difftool forwards arguments to diff' '
87 >for-diff &&
88 git add for-diff &&
89 echo changes>for-diff &&
90 git add for-diff &&
91 diff=$(git difftool --cached --no-prompt -- for-diff) &&
92 test "$diff" = "" &&
93 git reset -- for-diff &&
94 rm for-diff
95'
96
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +000097test_expect_success PERL 'difftool honors --gui' '
David Aguilar4cefa492009-12-22 21:27:14 -080098 git config merge.tool bogus-tool &&
99 git config diff.tool bogus-tool &&
100 git config diff.guitool test-tool &&
101
102 diff=$(git difftool --no-prompt --gui branch) &&
103 test "$diff" = "branch" &&
104
105 restore_test_defaults
106'
107
Tim Henigan85089602012-03-22 15:52:17 -0400108test_expect_success PERL 'difftool --gui last setting wins' '
109 git config diff.guitool bogus-tool &&
110 git difftool --no-prompt --gui --no-gui &&
111
112 git config merge.tool bogus-tool &&
113 git config diff.tool bogus-tool &&
114 git config diff.guitool test-tool &&
115 diff=$(git difftool --no-prompt --no-gui --gui branch) &&
116 test "$diff" = "branch" &&
117
118 restore_test_defaults
119'
120
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +0000121test_expect_success PERL 'difftool --gui works without configured diff.guitool' '
David Aguilar42accae2010-03-27 14:58:09 -0700122 git config diff.tool test-tool &&
123
124 diff=$(git difftool --no-prompt --gui branch) &&
125 test "$diff" = "branch" &&
126
127 restore_test_defaults
128'
129
David Aguilarf92f2032009-04-07 16:30:53 -0700130# Specify the diff tool using $GIT_DIFF_TOOL
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +0000131test_expect_success PERL 'GIT_DIFF_TOOL variable' '
Elijah Newrenbc0f35c2010-10-03 14:00:12 -0600132 test_might_fail git config --unset diff.tool &&
David Aguilarf92f2032009-04-07 16:30:53 -0700133 GIT_DIFF_TOOL=test-tool &&
134 export GIT_DIFF_TOOL &&
135
136 diff=$(git difftool --no-prompt branch) &&
137 test "$diff" = "branch" &&
138
139 restore_test_defaults
140'
141
142# Test the $GIT_*_TOOL variables and ensure
143# that $GIT_DIFF_TOOL always wins unless --tool is specified
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +0000144test_expect_success PERL 'GIT_DIFF_TOOL overrides' '
David Aguilarf92f2032009-04-07 16:30:53 -0700145 git config diff.tool bogus-tool &&
146 git config merge.tool bogus-tool &&
147
David Aguilarf92f2032009-04-07 16:30:53 -0700148 GIT_DIFF_TOOL=test-tool &&
David Aguilarf92f2032009-04-07 16:30:53 -0700149 export GIT_DIFF_TOOL &&
150
151 diff=$(git difftool --no-prompt branch) &&
152 test "$diff" = "branch" &&
153
154 GIT_DIFF_TOOL=bogus-tool &&
155 export GIT_DIFF_TOOL &&
156
157 diff=$(git difftool --no-prompt --tool=test-tool branch) &&
158 test "$diff" = "branch" &&
159
160 restore_test_defaults
161'
162
163# Test that we don't have to pass --no-prompt to difftool
164# when $GIT_DIFFTOOL_NO_PROMPT is true
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +0000165test_expect_success PERL 'GIT_DIFFTOOL_NO_PROMPT variable' '
David Aguilarf92f2032009-04-07 16:30:53 -0700166 GIT_DIFFTOOL_NO_PROMPT=true &&
167 export GIT_DIFFTOOL_NO_PROMPT &&
168
169 diff=$(git difftool branch) &&
170 test "$diff" = "branch" &&
171
172 restore_test_defaults
173'
174
David Aguilara9043922009-04-07 01:21:22 -0700175# git-difftool supports the difftool.prompt variable.
176# Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +0000177test_expect_success PERL 'GIT_DIFFTOOL_PROMPT variable' '
David Aguilara9043922009-04-07 01:21:22 -0700178 git config difftool.prompt false &&
179 GIT_DIFFTOOL_PROMPT=true &&
180 export GIT_DIFFTOOL_PROMPT &&
181
Markus Heidelberg3319df62009-10-25 01:39:19 +0200182 prompt=$(echo | git difftool branch | tail -1) &&
David Aguilara9043922009-04-07 01:21:22 -0700183 prompt_given "$prompt" &&
184
185 restore_test_defaults
186'
187
188# Test that we don't have to pass --no-prompt when difftool.prompt is false
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +0000189test_expect_success PERL 'difftool.prompt config variable is false' '
David Aguilara9043922009-04-07 01:21:22 -0700190 git config difftool.prompt false &&
191
192 diff=$(git difftool branch) &&
193 test "$diff" = "branch" &&
194
195 restore_test_defaults
196'
197
David Aguilara88183f2010-01-22 22:03:36 -0800198# Test that we don't have to pass --no-prompt when mergetool.prompt is false
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +0000199test_expect_success PERL 'difftool merge.prompt = false' '
Elijah Newrenbc0f35c2010-10-03 14:00:12 -0600200 test_might_fail git config --unset difftool.prompt &&
David Aguilara88183f2010-01-22 22:03:36 -0800201 git config mergetool.prompt false &&
202
203 diff=$(git difftool branch) &&
204 test "$diff" = "branch" &&
205
206 restore_test_defaults
207'
208
David Aguilara9043922009-04-07 01:21:22 -0700209# Test that the -y flag can override difftool.prompt = true
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +0000210test_expect_success PERL 'difftool.prompt can overridden with -y' '
David Aguilara9043922009-04-07 01:21:22 -0700211 git config difftool.prompt true &&
212
213 diff=$(git difftool -y branch) &&
214 test "$diff" = "branch" &&
215
216 restore_test_defaults
217'
218
219# Test that the --prompt flag can override difftool.prompt = false
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +0000220test_expect_success PERL 'difftool.prompt can overridden with --prompt' '
David Aguilara9043922009-04-07 01:21:22 -0700221 git config difftool.prompt false &&
222
223 prompt=$(echo | git difftool --prompt branch | tail -1) &&
224 prompt_given "$prompt" &&
225
226 restore_test_defaults
227'
228
229# Test that the last flag passed on the command-line wins
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +0000230test_expect_success PERL 'difftool last flag wins' '
David Aguilara9043922009-04-07 01:21:22 -0700231 diff=$(git difftool --prompt --no-prompt branch) &&
232 test "$diff" = "branch" &&
233
234 restore_test_defaults &&
235
236 prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
237 prompt_given "$prompt" &&
238
239 restore_test_defaults
240'
241
David Aguilarf92f2032009-04-07 16:30:53 -0700242# git-difftool falls back to git-mergetool config variables
243# so test that behavior here
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +0000244test_expect_success PERL 'difftool + mergetool config variables' '
Elijah Newrenbc0f35c2010-10-03 14:00:12 -0600245 remove_config_vars &&
David Aguilarf92f2032009-04-07 16:30:53 -0700246 git config merge.tool test-tool &&
247 git config mergetool.test-tool.cmd "cat \$LOCAL" &&
248
249 diff=$(git difftool --no-prompt branch) &&
250 test "$diff" = "branch" &&
251
252 # set merge.tool to something bogus, diff.tool to test-tool
253 git config merge.tool bogus-tool &&
254 git config diff.tool test-tool &&
255
256 diff=$(git difftool --no-prompt branch) &&
257 test "$diff" = "branch" &&
258
259 restore_test_defaults
260'
261
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +0000262test_expect_success PERL 'difftool.<tool>.path' '
David Aguilarf92f2032009-04-07 16:30:53 -0700263 git config difftool.tkdiff.path echo &&
264 diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
265 git config --unset difftool.tkdiff.path &&
266 lines=$(echo "$diff" | grep file | wc -l) &&
David Aguilar1c6f5b52010-01-09 20:02:42 -0800267 test "$lines" -eq 1 &&
268
269 restore_test_defaults
270'
271
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +0000272test_expect_success PERL 'difftool --extcmd=cat' '
David Aguilar1c6f5b52010-01-09 20:02:42 -0800273 diff=$(git difftool --no-prompt --extcmd=cat branch) &&
David Aguilara9e11222010-01-15 14:03:42 -0800274 test "$diff" = branch"$LF"master
David Aguilarf47f1e22010-01-15 14:03:43 -0800275'
David Aguilar1c6f5b52010-01-09 20:02:42 -0800276
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +0000277test_expect_success PERL 'difftool --extcmd cat' '
David Aguilarf47f1e22010-01-15 14:03:43 -0800278 diff=$(git difftool --no-prompt --extcmd cat branch) &&
279 test "$diff" = branch"$LF"master
280'
David Aguilar1c6f5b52010-01-09 20:02:42 -0800281
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +0000282test_expect_success PERL 'difftool -x cat' '
David Aguilarf47f1e22010-01-15 14:03:43 -0800283 diff=$(git difftool --no-prompt -x cat branch) &&
284 test "$diff" = branch"$LF"master
David Aguilar9f3d54d2010-01-15 14:03:44 -0800285'
David Aguilar1c6f5b52010-01-09 20:02:42 -0800286
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +0000287test_expect_success PERL 'difftool --extcmd echo arg1' '
Elijah Newrenbc0f35c2010-10-03 14:00:12 -0600288 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch) &&
David Aguilar9f3d54d2010-01-15 14:03:44 -0800289 test "$diff" = file
290'
David Aguilar1c6f5b52010-01-09 20:02:42 -0800291
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +0000292test_expect_success PERL 'difftool --extcmd cat arg1' '
Elijah Newrenbc0f35c2010-10-03 14:00:12 -0600293 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch) &&
David Aguilar9f3d54d2010-01-15 14:03:44 -0800294 test "$diff" = master
295'
296
Ævar Arnfjörð Bjarmason2c4f3022010-07-28 10:34:58 +0000297test_expect_success PERL 'difftool --extcmd cat arg2' '
Elijah Newrenbc0f35c2010-10-03 14:00:12 -0600298 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch) &&
David Aguilar9f3d54d2010-01-15 14:03:44 -0800299 test "$diff" = branch
David Aguilarf92f2032009-04-07 16:30:53 -0700300'
301
Sitaram Chamartyba959de2011-10-08 18:40:15 +0530302# Create a second file on master and a different version on branch
303test_expect_success PERL 'setup with 2 files different' '
304 echo m2 >file2 &&
305 git add file2 &&
306 git commit -m "added file2" &&
307
308 git checkout branch &&
309 echo br2 >file2 &&
310 git add file2 &&
311 git commit -a -m "branch changed file2" &&
312 git checkout master
313'
314
315test_expect_success PERL 'say no to the first file' '
Junio C Hamano15a31e72011-10-14 11:07:26 -0700316 diff=$( (echo n; echo) | git difftool -x cat branch ) &&
Sitaram Chamartyba959de2011-10-08 18:40:15 +0530317
318 echo "$diff" | stdin_contains m2 &&
319 echo "$diff" | stdin_contains br2 &&
320 echo "$diff" | stdin_doesnot_contain master &&
321 echo "$diff" | stdin_doesnot_contain branch
322'
323
324test_expect_success PERL 'say no to the second file' '
Junio C Hamano15a31e72011-10-14 11:07:26 -0700325 diff=$( (echo; echo n) | git difftool -x cat branch ) &&
Sitaram Chamartyba959de2011-10-08 18:40:15 +0530326
327 echo "$diff" | stdin_contains master &&
328 echo "$diff" | stdin_contains branch &&
329 echo "$diff" | stdin_doesnot_contain m2 &&
330 echo "$diff" | stdin_doesnot_contain br2
331'
332
Tim Heniganbf73fc22012-03-29 09:39:18 -0400333test_expect_success PERL 'difftool --tool-help' '
334 tool_help=$(git difftool --tool-help) &&
335 echo "$tool_help" | stdin_contains tool
336'
337
Tim Henigan7e0abce2012-04-23 14:23:41 -0400338test_expect_success PERL 'setup change in subdirectory' '
339 git checkout master &&
340 mkdir sub &&
341 echo master >sub/sub &&
342 git add sub/sub &&
343 git commit -m "added sub/sub" &&
344 echo test >>file &&
345 echo test >>sub/sub &&
346 git add . &&
347 git commit -m "modified both"
348'
349
350test_expect_success PERL 'difftool -d' '
351 diff=$(git difftool -d --extcmd ls branch) &&
352 echo "$diff" | stdin_contains sub &&
353 echo "$diff" | stdin_contains file
354'
355
356test_expect_success PERL 'difftool --dir-diff' '
357 diff=$(git difftool --dir-diff --extcmd ls branch) &&
358 echo "$diff" | stdin_contains sub &&
359 echo "$diff" | stdin_contains file
360'
361
362test_expect_success PERL 'difftool --dir-diff ignores --prompt' '
363 diff=$(git difftool --dir-diff --prompt --extcmd ls branch) &&
364 echo "$diff" | stdin_contains sub &&
365 echo "$diff" | stdin_contains file
366'
367
368test_expect_success PERL 'difftool --dir-diff from subdirectory' '
369 (
370 cd sub &&
371 diff=$(git difftool --dir-diff --extcmd ls branch) &&
372 echo "$diff" | stdin_contains sub &&
373 echo "$diff" | stdin_contains file
374 )
375'
376
David Aguilarf92f2032009-04-07 16:30:53 -0700377test_done