]>
Commit | Line | Data |
---|---|---|
f92f2038 DA |
1 | #!/bin/sh |
2 | # | |
9e5a86f2 | 3 | # Copyright (c) 2009, 2010, 2012, 2013 David Aguilar |
f92f2038 DA |
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 | |
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 | ||
26 | restore_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 |
38 | prompt_given() |
39 | { | |
40 | prompt="$1" | |
ba959de1 SC |
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" | |
a904392e DA |
52 | } |
53 | ||
f92f2038 | 54 | # Create a file on master and change it on branch |
2c4f3026 | 55 | test_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 | 67 | test_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 | ||
a427ef7a DA |
79 | # Ensures that a custom difftool.<tool>.cmd overrides built-ins |
80 | test_expect_success PERL 'custom commands override built-ins' ' | |
81 | restore_test_defaults && | |
82 | git config difftool.defaults.cmd "cat \$REMOTE" && | |
83 | ||
84 | diff=$(git difftool --tool defaults --no-prompt branch) && | |
85 | test "$diff" = "master" && | |
86 | ||
87 | git config --unset difftool.defaults.cmd | |
88 | ' | |
89 | ||
f92f2038 | 90 | # Ensures that git-difftool ignores bogus --tool values |
2c4f3026 | 91 | test_expect_success PERL 'difftool ignores bad --tool values' ' |
23218bbd | 92 | diff=$(git difftool --no-prompt --tool=bad-tool branch) |
f92f2038 DA |
93 | test "$?" = 1 && |
94 | test "$diff" = "" | |
95 | ' | |
96 | ||
d50b2c73 DA |
97 | test_expect_success PERL 'difftool forwards arguments to diff' ' |
98 | >for-diff && | |
99 | git add for-diff && | |
100 | echo changes>for-diff && | |
101 | git add for-diff && | |
102 | diff=$(git difftool --cached --no-prompt -- for-diff) && | |
103 | test "$diff" = "" && | |
104 | git reset -- for-diff && | |
105 | rm for-diff | |
106 | ' | |
107 | ||
2c4f3026 | 108 | test_expect_success PERL 'difftool honors --gui' ' |
4cefa495 DA |
109 | git config merge.tool bogus-tool && |
110 | git config diff.tool bogus-tool && | |
111 | git config diff.guitool test-tool && | |
112 | ||
113 | diff=$(git difftool --no-prompt --gui branch) && | |
114 | test "$diff" = "branch" && | |
115 | ||
116 | restore_test_defaults | |
117 | ' | |
118 | ||
85089604 TH |
119 | test_expect_success PERL 'difftool --gui last setting wins' ' |
120 | git config diff.guitool bogus-tool && | |
121 | git difftool --no-prompt --gui --no-gui && | |
122 | ||
123 | git config merge.tool bogus-tool && | |
124 | git config diff.tool bogus-tool && | |
125 | git config diff.guitool test-tool && | |
126 | diff=$(git difftool --no-prompt --no-gui --gui branch) && | |
127 | test "$diff" = "branch" && | |
128 | ||
129 | restore_test_defaults | |
130 | ' | |
131 | ||
2c4f3026 | 132 | test_expect_success PERL 'difftool --gui works without configured diff.guitool' ' |
42accaec DA |
133 | git config diff.tool test-tool && |
134 | ||
135 | diff=$(git difftool --no-prompt --gui branch) && | |
136 | test "$diff" = "branch" && | |
137 | ||
138 | restore_test_defaults | |
139 | ' | |
140 | ||
f92f2038 | 141 | # Specify the diff tool using $GIT_DIFF_TOOL |
2c4f3026 | 142 | test_expect_success PERL 'GIT_DIFF_TOOL variable' ' |
bc0f35ca | 143 | test_might_fail git config --unset diff.tool && |
f92f2038 DA |
144 | GIT_DIFF_TOOL=test-tool && |
145 | export GIT_DIFF_TOOL && | |
146 | ||
147 | diff=$(git difftool --no-prompt branch) && | |
148 | test "$diff" = "branch" && | |
149 | ||
150 | restore_test_defaults | |
151 | ' | |
152 | ||
153 | # Test the $GIT_*_TOOL variables and ensure | |
154 | # that $GIT_DIFF_TOOL always wins unless --tool is specified | |
2c4f3026 | 155 | test_expect_success PERL 'GIT_DIFF_TOOL overrides' ' |
f92f2038 DA |
156 | git config diff.tool bogus-tool && |
157 | git config merge.tool bogus-tool && | |
158 | ||
f92f2038 | 159 | GIT_DIFF_TOOL=test-tool && |
f92f2038 DA |
160 | export GIT_DIFF_TOOL && |
161 | ||
162 | diff=$(git difftool --no-prompt branch) && | |
163 | test "$diff" = "branch" && | |
164 | ||
165 | GIT_DIFF_TOOL=bogus-tool && | |
166 | export GIT_DIFF_TOOL && | |
167 | ||
168 | diff=$(git difftool --no-prompt --tool=test-tool branch) && | |
169 | test "$diff" = "branch" && | |
170 | ||
171 | restore_test_defaults | |
172 | ' | |
173 | ||
174 | # Test that we don't have to pass --no-prompt to difftool | |
175 | # when $GIT_DIFFTOOL_NO_PROMPT is true | |
2c4f3026 | 176 | test_expect_success PERL 'GIT_DIFFTOOL_NO_PROMPT variable' ' |
f92f2038 DA |
177 | GIT_DIFFTOOL_NO_PROMPT=true && |
178 | export GIT_DIFFTOOL_NO_PROMPT && | |
179 | ||
180 | diff=$(git difftool branch) && | |
181 | test "$diff" = "branch" && | |
182 | ||
183 | restore_test_defaults | |
184 | ' | |
185 | ||
a904392e DA |
186 | # git-difftool supports the difftool.prompt variable. |
187 | # Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false | |
2c4f3026 | 188 | test_expect_success PERL 'GIT_DIFFTOOL_PROMPT variable' ' |
a904392e DA |
189 | git config difftool.prompt false && |
190 | GIT_DIFFTOOL_PROMPT=true && | |
191 | export GIT_DIFFTOOL_PROMPT && | |
192 | ||
3319df6f | 193 | prompt=$(echo | git difftool branch | tail -1) && |
a904392e DA |
194 | prompt_given "$prompt" && |
195 | ||
196 | restore_test_defaults | |
197 | ' | |
198 | ||
199 | # Test that we don't have to pass --no-prompt when difftool.prompt is false | |
2c4f3026 | 200 | test_expect_success PERL 'difftool.prompt config variable is false' ' |
a904392e DA |
201 | git config difftool.prompt false && |
202 | ||
203 | diff=$(git difftool branch) && | |
204 | test "$diff" = "branch" && | |
205 | ||
206 | restore_test_defaults | |
207 | ' | |
208 | ||
a88183f1 | 209 | # Test that we don't have to pass --no-prompt when mergetool.prompt is false |
2c4f3026 | 210 | test_expect_success PERL 'difftool merge.prompt = false' ' |
bc0f35ca | 211 | test_might_fail git config --unset difftool.prompt && |
a88183f1 DA |
212 | git config mergetool.prompt false && |
213 | ||
214 | diff=$(git difftool branch) && | |
215 | test "$diff" = "branch" && | |
216 | ||
217 | restore_test_defaults | |
218 | ' | |
219 | ||
a904392e | 220 | # Test that the -y flag can override difftool.prompt = true |
2c4f3026 | 221 | test_expect_success PERL 'difftool.prompt can overridden with -y' ' |
a904392e DA |
222 | git config difftool.prompt true && |
223 | ||
224 | diff=$(git difftool -y branch) && | |
225 | test "$diff" = "branch" && | |
226 | ||
227 | restore_test_defaults | |
228 | ' | |
229 | ||
230 | # Test that the --prompt flag can override difftool.prompt = false | |
2c4f3026 | 231 | test_expect_success PERL 'difftool.prompt can overridden with --prompt' ' |
a904392e DA |
232 | git config difftool.prompt false && |
233 | ||
234 | prompt=$(echo | git difftool --prompt branch | tail -1) && | |
235 | prompt_given "$prompt" && | |
236 | ||
237 | restore_test_defaults | |
238 | ' | |
239 | ||
240 | # Test that the last flag passed on the command-line wins | |
2c4f3026 | 241 | test_expect_success PERL 'difftool last flag wins' ' |
a904392e DA |
242 | diff=$(git difftool --prompt --no-prompt branch) && |
243 | test "$diff" = "branch" && | |
244 | ||
245 | restore_test_defaults && | |
246 | ||
247 | prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) && | |
248 | prompt_given "$prompt" && | |
249 | ||
250 | restore_test_defaults | |
251 | ' | |
252 | ||
f92f2038 DA |
253 | # git-difftool falls back to git-mergetool config variables |
254 | # so test that behavior here | |
2c4f3026 | 255 | test_expect_success PERL 'difftool + mergetool config variables' ' |
bc0f35ca | 256 | remove_config_vars && |
f92f2038 DA |
257 | git config merge.tool test-tool && |
258 | git config mergetool.test-tool.cmd "cat \$LOCAL" && | |
259 | ||
260 | diff=$(git difftool --no-prompt branch) && | |
261 | test "$diff" = "branch" && | |
262 | ||
263 | # set merge.tool to something bogus, diff.tool to test-tool | |
264 | git config merge.tool bogus-tool && | |
265 | git config diff.tool test-tool && | |
266 | ||
267 | diff=$(git difftool --no-prompt branch) && | |
268 | test "$diff" = "branch" && | |
269 | ||
270 | restore_test_defaults | |
271 | ' | |
272 | ||
2c4f3026 | 273 | test_expect_success PERL 'difftool.<tool>.path' ' |
f92f2038 DA |
274 | git config difftool.tkdiff.path echo && |
275 | diff=$(git difftool --tool=tkdiff --no-prompt branch) && | |
276 | git config --unset difftool.tkdiff.path && | |
277 | lines=$(echo "$diff" | grep file | wc -l) && | |
1c6f5b52 DA |
278 | test "$lines" -eq 1 && |
279 | ||
280 | restore_test_defaults | |
281 | ' | |
282 | ||
2c4f3026 | 283 | test_expect_success PERL 'difftool --extcmd=cat' ' |
1c6f5b52 | 284 | diff=$(git difftool --no-prompt --extcmd=cat branch) && |
a9e11220 | 285 | test "$diff" = branch"$LF"master |
f47f1e2c | 286 | ' |
1c6f5b52 | 287 | |
2c4f3026 | 288 | test_expect_success PERL 'difftool --extcmd cat' ' |
f47f1e2c DA |
289 | diff=$(git difftool --no-prompt --extcmd cat branch) && |
290 | test "$diff" = branch"$LF"master | |
291 | ' | |
1c6f5b52 | 292 | |
2c4f3026 | 293 | test_expect_success PERL 'difftool -x cat' ' |
f47f1e2c DA |
294 | diff=$(git difftool --no-prompt -x cat branch) && |
295 | test "$diff" = branch"$LF"master | |
9f3d54d1 DA |
296 | ' |
297 | ||
2c4f3026 | 298 | test_expect_success PERL 'difftool --extcmd echo arg1' ' |
bc0f35ca | 299 | diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch) && |
9f3d54d1 DA |
300 | test "$diff" = file |
301 | ' | |
1c6f5b52 | 302 | |
2c4f3026 | 303 | test_expect_success PERL 'difftool --extcmd cat arg1' ' |
bc0f35ca | 304 | diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch) && |
9f3d54d1 DA |
305 | test "$diff" = master |
306 | ' | |
1c6f5b52 | 307 | |
2c4f3026 | 308 | test_expect_success PERL 'difftool --extcmd cat arg2' ' |
bc0f35ca | 309 | diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch) && |
9f3d54d1 | 310 | test "$diff" = branch |
f92f2038 DA |
311 | ' |
312 | ||
ba959de1 SC |
313 | # Create a second file on master and a different version on branch |
314 | test_expect_success PERL 'setup with 2 files different' ' | |
315 | echo m2 >file2 && | |
316 | git add file2 && | |
317 | git commit -m "added file2" && | |
318 | ||
319 | git checkout branch && | |
320 | echo br2 >file2 && | |
321 | git add file2 && | |
322 | git commit -a -m "branch changed file2" && | |
323 | git checkout master | |
324 | ' | |
325 | ||
326 | test_expect_success PERL 'say no to the first file' ' | |
15a31e78 | 327 | diff=$( (echo n; echo) | git difftool -x cat branch ) && |
ba959de1 SC |
328 | |
329 | echo "$diff" | stdin_contains m2 && | |
330 | echo "$diff" | stdin_contains br2 && | |
331 | echo "$diff" | stdin_doesnot_contain master && | |
332 | echo "$diff" | stdin_doesnot_contain branch | |
333 | ' | |
334 | ||
335 | test_expect_success PERL 'say no to the second file' ' | |
15a31e78 | 336 | diff=$( (echo; echo n) | git difftool -x cat branch ) && |
ba959de1 SC |
337 | |
338 | echo "$diff" | stdin_contains master && | |
339 | echo "$diff" | stdin_contains branch && | |
340 | echo "$diff" | stdin_doesnot_contain m2 && | |
341 | echo "$diff" | stdin_doesnot_contain br2 | |
342 | ' | |
343 | ||
bf73fc21 TH |
344 | test_expect_success PERL 'difftool --tool-help' ' |
345 | tool_help=$(git difftool --tool-help) && | |
346 | echo "$tool_help" | stdin_contains tool | |
347 | ' | |
348 | ||
7e0abcec TH |
349 | test_expect_success PERL 'setup change in subdirectory' ' |
350 | git checkout master && | |
351 | mkdir sub && | |
352 | echo master >sub/sub && | |
353 | git add sub/sub && | |
354 | git commit -m "added sub/sub" && | |
355 | echo test >>file && | |
356 | echo test >>sub/sub && | |
357 | git add . && | |
358 | git commit -m "modified both" | |
359 | ' | |
360 | ||
361 | test_expect_success PERL 'difftool -d' ' | |
362 | diff=$(git difftool -d --extcmd ls branch) && | |
363 | echo "$diff" | stdin_contains sub && | |
364 | echo "$diff" | stdin_contains file | |
365 | ' | |
366 | ||
367 | test_expect_success PERL 'difftool --dir-diff' ' | |
368 | diff=$(git difftool --dir-diff --extcmd ls branch) && | |
369 | echo "$diff" | stdin_contains sub && | |
370 | echo "$diff" | stdin_contains file | |
371 | ' | |
372 | ||
373 | test_expect_success PERL 'difftool --dir-diff ignores --prompt' ' | |
374 | diff=$(git difftool --dir-diff --prompt --extcmd ls branch) && | |
375 | echo "$diff" | stdin_contains sub && | |
376 | echo "$diff" | stdin_contains file | |
377 | ' | |
378 | ||
379 | test_expect_success PERL 'difftool --dir-diff from subdirectory' ' | |
380 | ( | |
381 | cd sub && | |
382 | diff=$(git difftool --dir-diff --extcmd ls branch) && | |
383 | echo "$diff" | stdin_contains sub && | |
384 | echo "$diff" | stdin_contains file | |
385 | ) | |
386 | ' | |
387 | ||
f92f2038 | 388 | test_done |