3 # Copyright (c) 2009, 2010 David Aguilar
6 test_description='git-difftool
8 Testing basic diff tool invocation
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
26 restore_test_defaults()
28 # Restores the test defaults used by several tests
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
41 test "$prompt" = "Launch 'test-tool' [Y/n]: branch"
49 stdin_doesnot_contain()
54 # Create a file on master and change it on branch
55 test_expect_success PERL 'setup' '
58 git commit -m "added file" &&
60 git checkout -b branch master &&
62 git commit -a -m "branch changed file" &&
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" &&
71 diff=$(git difftool --no-prompt branch) &&
72 test "$diff" = "master" &&
74 restore_test_defaults &&
75 diff=$(git difftool --no-prompt branch) &&
76 test "$diff" = "branch"
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)
86 test_expect_success PERL 'difftool forwards arguments to diff' '
89 echo changes>for-diff &&
91 diff=$(git difftool --cached --no-prompt -- for-diff) &&
93 git reset -- for-diff &&
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 &&
102 diff=$(git difftool --no-prompt --gui branch) &&
103 test "$diff" = "branch" &&
105 restore_test_defaults
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 &&
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" &&
118 restore_test_defaults
121 test_expect_success PERL 'difftool --gui works without configured diff.guitool' '
122 git config diff.tool test-tool &&
124 diff=$(git difftool --no-prompt --gui branch) &&
125 test "$diff" = "branch" &&
127 restore_test_defaults
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 &&
136 diff=$(git difftool --no-prompt branch) &&
137 test "$diff" = "branch" &&
139 restore_test_defaults
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 &&
148 GIT_DIFF_TOOL=test-tool &&
149 export GIT_DIFF_TOOL &&
151 diff=$(git difftool --no-prompt branch) &&
152 test "$diff" = "branch" &&
154 GIT_DIFF_TOOL=bogus-tool &&
155 export GIT_DIFF_TOOL &&
157 diff=$(git difftool --no-prompt --tool=test-tool branch) &&
158 test "$diff" = "branch" &&
160 restore_test_defaults
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 &&
169 diff=$(git difftool branch) &&
170 test "$diff" = "branch" &&
172 restore_test_defaults
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 &&
182 prompt=$(echo | git difftool branch | tail -1) &&
183 prompt_given "$prompt" &&
185 restore_test_defaults
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 &&
192 diff=$(git difftool branch) &&
193 test "$diff" = "branch" &&
195 restore_test_defaults
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 &&
203 diff=$(git difftool branch) &&
204 test "$diff" = "branch" &&
206 restore_test_defaults
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 &&
213 diff=$(git difftool -y branch) &&
214 test "$diff" = "branch" &&
216 restore_test_defaults
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 &&
223 prompt=$(echo | git difftool --prompt branch | tail -1) &&
224 prompt_given "$prompt" &&
226 restore_test_defaults
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" &&
234 restore_test_defaults &&
236 prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
237 prompt_given "$prompt" &&
239 restore_test_defaults
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" &&
249 diff=$(git difftool --no-prompt branch) &&
250 test "$diff" = "branch" &&
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 &&
256 diff=$(git difftool --no-prompt branch) &&
257 test "$diff" = "branch" &&
259 restore_test_defaults
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 &&
269 restore_test_defaults
272 test_expect_success PERL 'difftool --extcmd=cat' '
273 diff=$(git difftool --no-prompt --extcmd=cat branch) &&
274 test "$diff" = branch"$LF"master
277 test_expect_success PERL 'difftool --extcmd cat' '
278 diff=$(git difftool --no-prompt --extcmd cat branch) &&
279 test "$diff" = branch"$LF"master
282 test_expect_success PERL 'difftool -x cat' '
283 diff=$(git difftool --no-prompt -x cat branch) &&
284 test "$diff" = branch"$LF"master
287 test_expect_success PERL 'difftool --extcmd echo arg1' '
288 diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch) &&
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
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
302 # Create a second file on master and a different version on branch
303 test_expect_success PERL 'setup with 2 files different' '
306 git commit -m "added file2" &&
308 git checkout branch &&
311 git commit -a -m "branch changed file2" &&
315 test_expect_success PERL 'say no to the first file' '
316 diff=$( (echo n; echo) | git difftool -x cat branch ) &&
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
324 test_expect_success PERL 'say no to the second file' '
325 diff=$( (echo; echo n) | git difftool -x cat branch ) &&
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
333 test_expect_success PERL 'difftool --tool-help' '
334 tool_help=$(git difftool --tool-help) &&
335 echo "$tool_help" | stdin_contains tool
338 test_expect_success PERL 'setup change in subdirectory' '
339 git checkout master &&
341 echo master >sub/sub &&
343 git commit -m "added sub/sub" &&
345 echo test >>sub/sub &&
347 git commit -m "modified both"
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
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
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
368 test_expect_success PERL 'difftool --dir-diff from subdirectory' '
371 diff=$(git difftool --dir-diff --extcmd ls branch) &&
372 echo "$diff" | stdin_contains sub &&
373 echo "$diff" | stdin_contains file