grep: prepare for new header field filter
[git.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 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
97 test_expect_success PERL 'difftool honors --gui' '
98         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
108 test_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
121 test_expect_success PERL 'difftool --gui works without configured diff.guitool' '
122         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
130 # Specify the diff tool using $GIT_DIFF_TOOL
131 test_expect_success PERL 'GIT_DIFF_TOOL variable' '
132         test_might_fail git config --unset diff.tool &&
133         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
144 test_expect_success PERL 'GIT_DIFF_TOOL overrides' '
145         git config diff.tool bogus-tool &&
146         git config merge.tool bogus-tool &&
147
148         GIT_DIFF_TOOL=test-tool &&
149         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
165 test_expect_success PERL 'GIT_DIFFTOOL_NO_PROMPT variable' '
166         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
175 # git-difftool supports the difftool.prompt variable.
176 # Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
177 test_expect_success PERL 'GIT_DIFFTOOL_PROMPT variable' '
178         git config difftool.prompt false &&
179         GIT_DIFFTOOL_PROMPT=true &&
180         export GIT_DIFFTOOL_PROMPT &&
181
182         prompt=$(echo | git difftool branch | tail -1) &&
183         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
189 test_expect_success PERL 'difftool.prompt config variable is false' '
190         git config difftool.prompt false &&
191
192         diff=$(git difftool branch) &&
193         test "$diff" = "branch" &&
194
195         restore_test_defaults
196 '
197
198 # Test that we don't have to pass --no-prompt when mergetool.prompt is false
199 test_expect_success PERL 'difftool merge.prompt = false' '
200         test_might_fail git config --unset difftool.prompt &&
201         git config mergetool.prompt false &&
202
203         diff=$(git difftool branch) &&
204         test "$diff" = "branch" &&
205
206         restore_test_defaults
207 '
208
209 # Test that the -y flag can override difftool.prompt = true
210 test_expect_success PERL 'difftool.prompt can overridden with -y' '
211         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
220 test_expect_success PERL 'difftool.prompt can overridden with --prompt' '
221         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
230 test_expect_success PERL 'difftool last flag wins' '
231         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
242 # git-difftool falls back to git-mergetool config variables
243 # so test that behavior here
244 test_expect_success PERL 'difftool + mergetool config variables' '
245         remove_config_vars &&
246         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
262 test_expect_success PERL 'difftool.<tool>.path' '
263         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) &&
267         test "$lines" -eq 1 &&
268
269         restore_test_defaults
270 '
271
272 test_expect_success PERL 'difftool --extcmd=cat' '
273         diff=$(git difftool --no-prompt --extcmd=cat branch) &&
274         test "$diff" = branch"$LF"master
275 '
276
277 test_expect_success PERL 'difftool --extcmd cat' '
278         diff=$(git difftool --no-prompt --extcmd cat branch) &&
279         test "$diff" = branch"$LF"master
280 '
281
282 test_expect_success PERL 'difftool -x cat' '
283         diff=$(git difftool --no-prompt -x cat branch) &&
284         test "$diff" = branch"$LF"master
285 '
286
287 test_expect_success PERL 'difftool --extcmd echo arg1' '
288         diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch) &&
289         test "$diff" = file
290 '
291
292 test_expect_success PERL 'difftool --extcmd cat arg1' '
293         diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch) &&
294         test "$diff" = master
295 '
296
297 test_expect_success PERL 'difftool --extcmd cat arg2' '
298         diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch) &&
299         test "$diff" = branch
300 '
301
302 # Create a second file on master and a different version on branch
303 test_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
315 test_expect_success PERL 'say no to the first file' '
316         diff=$( (echo n; echo) | git difftool -x cat branch ) &&
317
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
324 test_expect_success PERL 'say no to the second file' '
325         diff=$( (echo; echo n) | git difftool -x cat branch ) &&
326
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
333 test_expect_success PERL 'difftool --tool-help' '
334         tool_help=$(git difftool --tool-help) &&
335         echo "$tool_help" | stdin_contains tool
336 '
337
338 test_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
350 test_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
356 test_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
362 test_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
368 test_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
377 test_done