difftool: print list of valid tools with '--tool-help'
[git] / t / t7800-difftool.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2009, 2010 David Aguilar
4 #
5
6 test_description='git-difftool
7
8 Testing basic diff tool invocation
9 '
10
11 . ./test-lib.sh
12
13 remove_config_vars()
14 {
15         # Unset all config variables used by git-difftool
16         git config --unset diff.tool
17         git config --unset diff.guitool
18         git config --unset difftool.test-tool.cmd
19         git config --unset difftool.prompt
20         git config --unset merge.tool
21         git config --unset mergetool.test-tool.cmd
22         git config --unset mergetool.prompt
23         return 0
24 }
25
26 restore_test_defaults()
27 {
28         # Restores the test defaults used by several tests
29         remove_config_vars
30         unset GIT_DIFF_TOOL
31         unset GIT_DIFFTOOL_PROMPT
32         unset GIT_DIFFTOOL_NO_PROMPT
33         git config diff.tool test-tool &&
34         git config difftool.test-tool.cmd 'cat $LOCAL'
35         git config difftool.bogus-tool.cmd false
36 }
37
38 prompt_given()
39 {
40         prompt="$1"
41         test "$prompt" = "Launch 'test-tool' [Y/n]: branch"
42 }
43
44 stdin_contains()
45 {
46         grep >/dev/null "$1"
47 }
48
49 stdin_doesnot_contain()
50 {
51         ! stdin_contains "$1"
52 }
53
54 # Create a file on master and change it on branch
55 test_expect_success PERL 'setup' '
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
67 test_expect_success PERL 'custom commands' '
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
80 test_expect_success PERL 'difftool ignores bad --tool values' '
81         diff=$(git difftool --no-prompt --tool=bad-tool branch)
82         test "$?" = 1 &&
83         test "$diff" = ""
84 '
85
86 test_expect_success PERL 'difftool honors --gui' '
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
97 test_expect_success PERL 'difftool --gui last setting wins' '
98         git config diff.guitool bogus-tool &&
99         git difftool --no-prompt --gui --no-gui &&
100
101         git config merge.tool bogus-tool &&
102         git config diff.tool bogus-tool &&
103         git config diff.guitool test-tool &&
104         diff=$(git difftool --no-prompt --no-gui --gui branch) &&
105         test "$diff" = "branch" &&
106
107         restore_test_defaults
108 '
109
110 test_expect_success PERL 'difftool --gui works without configured diff.guitool' '
111         git config diff.tool test-tool &&
112
113         diff=$(git difftool --no-prompt --gui branch) &&
114         test "$diff" = "branch" &&
115
116         restore_test_defaults
117 '
118
119 # Specify the diff tool using $GIT_DIFF_TOOL
120 test_expect_success PERL 'GIT_DIFF_TOOL variable' '
121         test_might_fail git config --unset diff.tool &&
122         GIT_DIFF_TOOL=test-tool &&
123         export GIT_DIFF_TOOL &&
124
125         diff=$(git difftool --no-prompt branch) &&
126         test "$diff" = "branch" &&
127
128         restore_test_defaults
129 '
130
131 # Test the $GIT_*_TOOL variables and ensure
132 # that $GIT_DIFF_TOOL always wins unless --tool is specified
133 test_expect_success PERL 'GIT_DIFF_TOOL overrides' '
134         git config diff.tool bogus-tool &&
135         git config merge.tool bogus-tool &&
136
137         GIT_DIFF_TOOL=test-tool &&
138         export GIT_DIFF_TOOL &&
139
140         diff=$(git difftool --no-prompt branch) &&
141         test "$diff" = "branch" &&
142
143         GIT_DIFF_TOOL=bogus-tool &&
144         export GIT_DIFF_TOOL &&
145
146         diff=$(git difftool --no-prompt --tool=test-tool branch) &&
147         test "$diff" = "branch" &&
148
149         restore_test_defaults
150 '
151
152 # Test that we don't have to pass --no-prompt to difftool
153 # when $GIT_DIFFTOOL_NO_PROMPT is true
154 test_expect_success PERL 'GIT_DIFFTOOL_NO_PROMPT variable' '
155         GIT_DIFFTOOL_NO_PROMPT=true &&
156         export GIT_DIFFTOOL_NO_PROMPT &&
157
158         diff=$(git difftool branch) &&
159         test "$diff" = "branch" &&
160
161         restore_test_defaults
162 '
163
164 # git-difftool supports the difftool.prompt variable.
165 # Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
166 test_expect_success PERL 'GIT_DIFFTOOL_PROMPT variable' '
167         git config difftool.prompt false &&
168         GIT_DIFFTOOL_PROMPT=true &&
169         export GIT_DIFFTOOL_PROMPT &&
170
171         prompt=$(echo | git difftool branch | tail -1) &&
172         prompt_given "$prompt" &&
173
174         restore_test_defaults
175 '
176
177 # Test that we don't have to pass --no-prompt when difftool.prompt is false
178 test_expect_success PERL 'difftool.prompt config variable is false' '
179         git config difftool.prompt false &&
180
181         diff=$(git difftool branch) &&
182         test "$diff" = "branch" &&
183
184         restore_test_defaults
185 '
186
187 # Test that we don't have to pass --no-prompt when mergetool.prompt is false
188 test_expect_success PERL 'difftool merge.prompt = false' '
189         test_might_fail git config --unset difftool.prompt &&
190         git config mergetool.prompt false &&
191
192         diff=$(git difftool branch) &&
193         test "$diff" = "branch" &&
194
195         restore_test_defaults
196 '
197
198 # Test that the -y flag can override difftool.prompt = true
199 test_expect_success PERL 'difftool.prompt can overridden with -y' '
200         git config difftool.prompt true &&
201
202         diff=$(git difftool -y branch) &&
203         test "$diff" = "branch" &&
204
205         restore_test_defaults
206 '
207
208 # Test that the --prompt flag can override difftool.prompt = false
209 test_expect_success PERL 'difftool.prompt can overridden with --prompt' '
210         git config difftool.prompt false &&
211
212         prompt=$(echo | git difftool --prompt branch | tail -1) &&
213         prompt_given "$prompt" &&
214
215         restore_test_defaults
216 '
217
218 # Test that the last flag passed on the command-line wins
219 test_expect_success PERL 'difftool last flag wins' '
220         diff=$(git difftool --prompt --no-prompt branch) &&
221         test "$diff" = "branch" &&
222
223         restore_test_defaults &&
224
225         prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
226         prompt_given "$prompt" &&
227
228         restore_test_defaults
229 '
230
231 # git-difftool falls back to git-mergetool config variables
232 # so test that behavior here
233 test_expect_success PERL 'difftool + mergetool config variables' '
234         remove_config_vars &&
235         git config merge.tool test-tool &&
236         git config mergetool.test-tool.cmd "cat \$LOCAL" &&
237
238         diff=$(git difftool --no-prompt branch) &&
239         test "$diff" = "branch" &&
240
241         # set merge.tool to something bogus, diff.tool to test-tool
242         git config merge.tool bogus-tool &&
243         git config diff.tool test-tool &&
244
245         diff=$(git difftool --no-prompt branch) &&
246         test "$diff" = "branch" &&
247
248         restore_test_defaults
249 '
250
251 test_expect_success PERL 'difftool.<tool>.path' '
252         git config difftool.tkdiff.path echo &&
253         diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
254         git config --unset difftool.tkdiff.path &&
255         lines=$(echo "$diff" | grep file | wc -l) &&
256         test "$lines" -eq 1 &&
257
258         restore_test_defaults
259 '
260
261 test_expect_success PERL 'difftool --extcmd=cat' '
262         diff=$(git difftool --no-prompt --extcmd=cat branch) &&
263         test "$diff" = branch"$LF"master
264 '
265
266 test_expect_success PERL 'difftool --extcmd cat' '
267         diff=$(git difftool --no-prompt --extcmd cat branch) &&
268         test "$diff" = branch"$LF"master
269 '
270
271 test_expect_success PERL 'difftool -x cat' '
272         diff=$(git difftool --no-prompt -x cat branch) &&
273         test "$diff" = branch"$LF"master
274 '
275
276 test_expect_success PERL 'difftool --extcmd echo arg1' '
277         diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch) &&
278         test "$diff" = file
279 '
280
281 test_expect_success PERL 'difftool --extcmd cat arg1' '
282         diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch) &&
283         test "$diff" = master
284 '
285
286 test_expect_success PERL 'difftool --extcmd cat arg2' '
287         diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch) &&
288         test "$diff" = branch
289 '
290
291 # Create a second file on master and a different version on branch
292 test_expect_success PERL 'setup with 2 files different' '
293         echo m2 >file2 &&
294         git add file2 &&
295         git commit -m "added file2" &&
296
297         git checkout branch &&
298         echo br2 >file2 &&
299         git add file2 &&
300         git commit -a -m "branch changed file2" &&
301         git checkout master
302 '
303
304 test_expect_success PERL 'say no to the first file' '
305         diff=$( (echo n; echo) | git difftool -x cat branch ) &&
306
307         echo "$diff" | stdin_contains m2 &&
308         echo "$diff" | stdin_contains br2 &&
309         echo "$diff" | stdin_doesnot_contain master &&
310         echo "$diff" | stdin_doesnot_contain branch
311 '
312
313 test_expect_success PERL 'say no to the second file' '
314         diff=$( (echo; echo n) | git difftool -x cat branch ) &&
315
316         echo "$diff" | stdin_contains master &&
317         echo "$diff" | stdin_contains branch &&
318         echo "$diff" | stdin_doesnot_contain m2 &&
319         echo "$diff" | stdin_doesnot_contain br2
320 '
321
322 test_expect_success PERL 'difftool --tool-help' '
323         tool_help=$(git difftool --tool-help) &&
324         echo "$tool_help" | stdin_contains tool
325 '
326
327 test_expect_success PERL 'setup change in subdirectory' '
328         git checkout master &&
329         mkdir sub &&
330         echo master >sub/sub &&
331         git add sub/sub &&
332         git commit -m "added sub/sub" &&
333         echo test >>file &&
334         echo test >>sub/sub &&
335         git add . &&
336         git commit -m "modified both"
337 '
338
339 test_expect_success PERL 'difftool -d' '
340         diff=$(git difftool -d --extcmd ls branch) &&
341         echo "$diff" | stdin_contains sub &&
342         echo "$diff" | stdin_contains file
343 '
344
345 test_expect_success PERL 'difftool --dir-diff' '
346         diff=$(git difftool --dir-diff --extcmd ls branch) &&
347         echo "$diff" | stdin_contains sub &&
348         echo "$diff" | stdin_contains file
349 '
350
351 test_expect_success PERL 'difftool --dir-diff ignores --prompt' '
352         diff=$(git difftool --dir-diff --prompt --extcmd ls branch) &&
353         echo "$diff" | stdin_contains sub &&
354         echo "$diff" | stdin_contains file
355 '
356
357 test_expect_success PERL 'difftool --dir-diff from subdirectory' '
358         (
359                 cd sub &&
360                 diff=$(git difftool --dir-diff --extcmd ls branch) &&
361                 echo "$diff" | stdin_contains sub &&
362                 echo "$diff" | stdin_contains file
363         )
364 '
365
366 test_done