status: preload index to optimize lstat(2) calls
[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 if ! test_have_prereq PERL; then
14         say 'skipping difftool tests, perl not available'
15         test_done
16 fi
17
18 LF='
19 '
20
21 remove_config_vars()
22 {
23         # Unset all config variables used by git-difftool
24         git config --unset diff.tool
25         git config --unset diff.guitool
26         git config --unset difftool.test-tool.cmd
27         git config --unset difftool.prompt
28         git config --unset merge.tool
29         git config --unset mergetool.test-tool.cmd
30         git config --unset mergetool.prompt
31         return 0
32 }
33
34 restore_test_defaults()
35 {
36         # Restores the test defaults used by several tests
37         remove_config_vars
38         unset GIT_DIFF_TOOL
39         unset GIT_DIFFTOOL_PROMPT
40         unset GIT_DIFFTOOL_NO_PROMPT
41         git config diff.tool test-tool &&
42         git config difftool.test-tool.cmd 'cat $LOCAL'
43         git config difftool.bogus-tool.cmd false
44 }
45
46 prompt_given()
47 {
48         prompt="$1"
49         test "$prompt" = "Hit return to launch 'test-tool': branch"
50 }
51
52 # Create a file on master and change it on branch
53 test_expect_success 'setup' '
54         echo master >file &&
55         git add file &&
56         git commit -m "added file" &&
57
58         git checkout -b branch master &&
59         echo branch >file &&
60         git commit -a -m "branch changed file" &&
61         git checkout master
62 '
63
64 # Configure a custom difftool.<tool>.cmd and use it
65 test_expect_success 'custom commands' '
66         restore_test_defaults &&
67         git config difftool.test-tool.cmd "cat \$REMOTE" &&
68
69         diff=$(git difftool --no-prompt branch) &&
70         test "$diff" = "master" &&
71
72         restore_test_defaults &&
73         diff=$(git difftool --no-prompt branch) &&
74         test "$diff" = "branch"
75 '
76
77 # Ensures that git-difftool ignores bogus --tool values
78 test_expect_success 'difftool ignores bad --tool values' '
79         diff=$(git difftool --no-prompt --tool=bad-tool branch)
80         test "$?" = 1 &&
81         test "$diff" = ""
82 '
83
84 test_expect_success 'difftool honors --gui' '
85         git config merge.tool bogus-tool &&
86         git config diff.tool bogus-tool &&
87         git config diff.guitool test-tool &&
88
89         diff=$(git difftool --no-prompt --gui branch) &&
90         test "$diff" = "branch" &&
91
92         restore_test_defaults
93 '
94
95 # Specify the diff tool using $GIT_DIFF_TOOL
96 test_expect_success 'GIT_DIFF_TOOL variable' '
97         git config --unset diff.tool
98         GIT_DIFF_TOOL=test-tool &&
99         export GIT_DIFF_TOOL &&
100
101         diff=$(git difftool --no-prompt branch) &&
102         test "$diff" = "branch" &&
103
104         restore_test_defaults
105 '
106
107 # Test the $GIT_*_TOOL variables and ensure
108 # that $GIT_DIFF_TOOL always wins unless --tool is specified
109 test_expect_success 'GIT_DIFF_TOOL overrides' '
110         git config diff.tool bogus-tool &&
111         git config merge.tool bogus-tool &&
112
113         GIT_DIFF_TOOL=test-tool &&
114         export GIT_DIFF_TOOL &&
115
116         diff=$(git difftool --no-prompt branch) &&
117         test "$diff" = "branch" &&
118
119         GIT_DIFF_TOOL=bogus-tool &&
120         export GIT_DIFF_TOOL &&
121
122         diff=$(git difftool --no-prompt --tool=test-tool branch) &&
123         test "$diff" = "branch" &&
124
125         restore_test_defaults
126 '
127
128 # Test that we don't have to pass --no-prompt to difftool
129 # when $GIT_DIFFTOOL_NO_PROMPT is true
130 test_expect_success 'GIT_DIFFTOOL_NO_PROMPT variable' '
131         GIT_DIFFTOOL_NO_PROMPT=true &&
132         export GIT_DIFFTOOL_NO_PROMPT &&
133
134         diff=$(git difftool branch) &&
135         test "$diff" = "branch" &&
136
137         restore_test_defaults
138 '
139
140 # git-difftool supports the difftool.prompt variable.
141 # Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
142 test_expect_success 'GIT_DIFFTOOL_PROMPT variable' '
143         git config difftool.prompt false &&
144         GIT_DIFFTOOL_PROMPT=true &&
145         export GIT_DIFFTOOL_PROMPT &&
146
147         prompt=$(echo | git difftool branch | tail -1) &&
148         prompt_given "$prompt" &&
149
150         restore_test_defaults
151 '
152
153 # Test that we don't have to pass --no-prompt when difftool.prompt is false
154 test_expect_success 'difftool.prompt config variable is false' '
155         git config difftool.prompt false &&
156
157         diff=$(git difftool branch) &&
158         test "$diff" = "branch" &&
159
160         restore_test_defaults
161 '
162
163 # Test that we don't have to pass --no-prompt when mergetool.prompt is false
164 test_expect_success 'difftool merge.prompt = false' '
165         git config --unset difftool.prompt
166         git config mergetool.prompt false &&
167
168         diff=$(git difftool branch) &&
169         test "$diff" = "branch" &&
170
171         restore_test_defaults
172 '
173
174 # Test that the -y flag can override difftool.prompt = true
175 test_expect_success 'difftool.prompt can overridden with -y' '
176         git config difftool.prompt true &&
177
178         diff=$(git difftool -y branch) &&
179         test "$diff" = "branch" &&
180
181         restore_test_defaults
182 '
183
184 # Test that the --prompt flag can override difftool.prompt = false
185 test_expect_success 'difftool.prompt can overridden with --prompt' '
186         git config difftool.prompt false &&
187
188         prompt=$(echo | git difftool --prompt branch | tail -1) &&
189         prompt_given "$prompt" &&
190
191         restore_test_defaults
192 '
193
194 # Test that the last flag passed on the command-line wins
195 test_expect_success 'difftool last flag wins' '
196         diff=$(git difftool --prompt --no-prompt branch) &&
197         test "$diff" = "branch" &&
198
199         restore_test_defaults &&
200
201         prompt=$(echo | git difftool --no-prompt --prompt branch | tail -1) &&
202         prompt_given "$prompt" &&
203
204         restore_test_defaults
205 '
206
207 # git-difftool falls back to git-mergetool config variables
208 # so test that behavior here
209 test_expect_success 'difftool + mergetool config variables' '
210         remove_config_vars
211         git config merge.tool test-tool &&
212         git config mergetool.test-tool.cmd "cat \$LOCAL" &&
213
214         diff=$(git difftool --no-prompt branch) &&
215         test "$diff" = "branch" &&
216
217         # set merge.tool to something bogus, diff.tool to test-tool
218         git config merge.tool bogus-tool &&
219         git config diff.tool test-tool &&
220
221         diff=$(git difftool --no-prompt branch) &&
222         test "$diff" = "branch" &&
223
224         restore_test_defaults
225 '
226
227 test_expect_success 'difftool.<tool>.path' '
228         git config difftool.tkdiff.path echo &&
229         diff=$(git difftool --tool=tkdiff --no-prompt branch) &&
230         git config --unset difftool.tkdiff.path &&
231         lines=$(echo "$diff" | grep file | wc -l) &&
232         test "$lines" -eq 1 &&
233
234         restore_test_defaults
235 '
236
237 test_expect_success 'difftool --extcmd=cat' '
238         diff=$(git difftool --no-prompt --extcmd=cat branch) &&
239         test "$diff" = branch"$LF"master
240 '
241
242 test_expect_success 'difftool --extcmd cat' '
243         diff=$(git difftool --no-prompt --extcmd cat branch) &&
244         test "$diff" = branch"$LF"master
245 '
246
247 test_expect_success 'difftool -x cat' '
248         diff=$(git difftool --no-prompt -x cat branch) &&
249         test "$diff" = branch"$LF"master
250 '
251
252 test_expect_success 'difftool --extcmd echo arg1' '
253         diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"echo\ \$1\" branch)
254         test "$diff" = file
255 '
256
257 test_expect_success 'difftool --extcmd cat arg1' '
258         diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$1\" branch)
259         test "$diff" = master
260 '
261
262 test_expect_success 'difftool --extcmd cat arg2' '
263         diff=$(git difftool --no-prompt --extcmd sh\ -c\ \"cat\ \$2\" branch)
264         test "$diff" = branch
265 '
266
267 test_done