Merge branch 'jc/worktree-add-short-help'
[git] / t / t4061-diff-indent.sh
1 #!/bin/sh
2
3 test_description='Test diff indent heuristic.
4
5 '
6 . ./test-lib.sh
7 . "$TEST_DIRECTORY"/diff-lib.sh
8
9 # Compare two diff outputs. Ignore "index" lines, because we don't
10 # care about SHA-1s or file modes.
11 compare_diff () {
12         sed -e "/^index /d" <"$1" >.tmp-1
13         sed -e "/^index /d" <"$2" >.tmp-2
14         test_cmp .tmp-1 .tmp-2 && rm -f .tmp-1 .tmp-2
15 }
16
17 # Compare blame output using the expectation for a diff as reference.
18 # Only look for the lines coming from non-boundary commits.
19 compare_blame () {
20         sed -n -e "1,4d" -e "s/^\+//p" <"$1" >.tmp-1
21         sed -ne "s/^[^^][^)]*) *//p" <"$2" >.tmp-2
22         test_cmp .tmp-1 .tmp-2 && rm -f .tmp-1 .tmp-2
23 }
24
25 test_expect_success 'prepare' '
26         cat <<-\EOF >spaces.txt &&
27         1
28         2
29         a
30
31         b
32         3
33         4
34         EOF
35
36         cat <<-\EOF >functions.c &&
37         1
38         2
39         /* function */
40         foo() {
41             foo
42         }
43
44         3
45         4
46         EOF
47
48         git add spaces.txt functions.c &&
49         test_tick &&
50         git commit -m initial &&
51         git branch old &&
52
53         cat <<-\EOF >spaces.txt &&
54         1
55         2
56         a
57
58         b
59         a
60
61         b
62         3
63         4
64         EOF
65
66         cat <<-\EOF >functions.c &&
67         1
68         2
69         /* function */
70         bar() {
71             foo
72         }
73
74         /* function */
75         foo() {
76             foo
77         }
78
79         3
80         4
81         EOF
82
83         git add spaces.txt functions.c &&
84         test_tick &&
85         git commit -m initial &&
86         git branch new &&
87
88         tr "_" " " <<-\EOF >spaces-expect &&
89         diff --git a/spaces.txt b/spaces.txt
90         --- a/spaces.txt
91         +++ b/spaces.txt
92         @@ -3,5 +3,8 @@
93          a
94         _
95          b
96         +a
97         +
98         +b
99          3
100          4
101         EOF
102
103         tr "_" " " <<-\EOF >spaces-compacted-expect &&
104         diff --git a/spaces.txt b/spaces.txt
105         --- a/spaces.txt
106         +++ b/spaces.txt
107         @@ -2,6 +2,9 @@
108          2
109          a
110         _
111         +b
112         +a
113         +
114          b
115          3
116          4
117         EOF
118
119         tr "_" " " <<-\EOF >functions-expect &&
120         diff --git a/functions.c b/functions.c
121         --- a/functions.c
122         +++ b/functions.c
123         @@ -1,6 +1,11 @@
124          1
125          2
126          /* function */
127         +bar() {
128         +    foo
129         +}
130         +
131         +/* function */
132          foo() {
133              foo
134          }
135         EOF
136
137         tr "_" " " <<-\EOF >functions-compacted-expect
138         diff --git a/functions.c b/functions.c
139         --- a/functions.c
140         +++ b/functions.c
141         @@ -1,5 +1,10 @@
142          1
143          2
144         +/* function */
145         +bar() {
146         +    foo
147         +}
148         +
149          /* function */
150          foo() {
151              foo
152         EOF
153 '
154
155 # --- diff tests ----------------------------------------------------------
156
157 test_expect_success 'diff: ugly spaces' '
158         git diff --no-indent-heuristic old new -- spaces.txt >out &&
159         compare_diff spaces-expect out
160 '
161
162 test_expect_success 'diff: --no-indent-heuristic overrides config' '
163         git -c diff.indentHeuristic=true diff --no-indent-heuristic old new -- spaces.txt >out2 &&
164         compare_diff spaces-expect out2
165 '
166
167 test_expect_success 'diff: nice spaces with --indent-heuristic' '
168         git -c diff.indentHeuristic=false diff --indent-heuristic old new -- spaces.txt >out-compacted &&
169         compare_diff spaces-compacted-expect out-compacted
170 '
171
172 test_expect_success 'diff: nice spaces with diff.indentHeuristic=true' '
173         git -c diff.indentHeuristic=true diff old new -- spaces.txt >out-compacted2 &&
174         compare_diff spaces-compacted-expect out-compacted2
175 '
176
177 test_expect_success 'diff: --indent-heuristic with --patience' '
178         git diff --indent-heuristic --patience old new -- spaces.txt >out-compacted3 &&
179         compare_diff spaces-compacted-expect out-compacted3
180 '
181
182 test_expect_success 'diff: --indent-heuristic with --histogram' '
183         git diff --indent-heuristic --histogram old new -- spaces.txt >out-compacted4 &&
184         compare_diff spaces-compacted-expect out-compacted4
185 '
186
187 test_expect_success 'diff: ugly functions' '
188         git diff --no-indent-heuristic old new -- functions.c >out &&
189         compare_diff functions-expect out
190 '
191
192 test_expect_success 'diff: nice functions with --indent-heuristic' '
193         git diff --indent-heuristic old new -- functions.c >out-compacted &&
194         compare_diff functions-compacted-expect out-compacted
195 '
196
197 # --- blame tests ---------------------------------------------------------
198
199 test_expect_success 'blame: nice spaces with --indent-heuristic' '
200         git blame --indent-heuristic old..new -- spaces.txt >out-blame-compacted &&
201         compare_blame spaces-compacted-expect out-blame-compacted
202 '
203
204 test_expect_success 'blame: nice spaces with diff.indentHeuristic=true' '
205         git -c diff.indentHeuristic=true blame old..new -- spaces.txt >out-blame-compacted2 &&
206         compare_blame spaces-compacted-expect out-blame-compacted2
207 '
208
209 test_expect_success 'blame: ugly spaces with --no-indent-heuristic' '
210         git blame --no-indent-heuristic old..new -- spaces.txt >out-blame &&
211         compare_blame spaces-expect out-blame
212 '
213
214 test_expect_success 'blame: ugly spaces with diff.indentHeuristic=false' '
215         git -c diff.indentHeuristic=false blame old..new -- spaces.txt >out-blame2 &&
216         compare_blame spaces-expect out-blame2
217 '
218
219 test_expect_success 'blame: --no-indent-heuristic overrides config' '
220         git -c diff.indentHeuristic=true blame --no-indent-heuristic old..new -- spaces.txt >out-blame3 &&
221         git blame old..new -- spaces.txt >out-blame &&
222         compare_blame spaces-expect out-blame3
223 '
224
225 test_expect_success 'blame: --indent-heuristic overrides config' '
226         git -c diff.indentHeuristic=false blame --indent-heuristic old..new -- spaces.txt >out-blame-compacted3 &&
227         compare_blame spaces-compacted-expect out-blame-compacted2
228 '
229
230 # --- diff-tree tests -----------------------------------------------------
231
232 test_expect_success 'diff-tree: nice spaces with --indent-heuristic' '
233         git diff-tree --indent-heuristic -p old new -- spaces.txt >out-diff-tree-compacted &&
234         compare_diff spaces-compacted-expect out-diff-tree-compacted
235 '
236
237 test_expect_success 'diff-tree: nice spaces with diff.indentHeuristic=true' '
238         git -c diff.indentHeuristic=true diff-tree -p old new -- spaces.txt >out-diff-tree-compacted2 &&
239         compare_diff spaces-compacted-expect out-diff-tree-compacted2
240 '
241
242 test_expect_success 'diff-tree: ugly spaces with --no-indent-heuristic' '
243         git diff-tree --no-indent-heuristic -p old new -- spaces.txt >out-diff-tree &&
244         compare_diff spaces-expect out-diff-tree
245 '
246
247 test_expect_success 'diff-tree: ugly spaces with diff.indentHeuristic=false' '
248         git -c diff.indentHeuristic=false diff-tree -p old new -- spaces.txt >out-diff-tree2 &&
249         compare_diff spaces-expect out-diff-tree2
250 '
251
252 test_expect_success 'diff-tree: --indent-heuristic overrides config' '
253         git -c diff.indentHeuristic=false diff-tree --indent-heuristic -p old new -- spaces.txt >out-diff-tree-compacted3 &&
254         compare_diff spaces-compacted-expect out-diff-tree-compacted3
255 '
256
257 test_expect_success 'diff-tree: --no-indent-heuristic overrides config' '
258         git -c diff.indentHeuristic=true diff-tree --no-indent-heuristic -p old new -- spaces.txt >out-diff-tree3 &&
259         compare_diff spaces-expect out-diff-tree3
260 '
261
262 # --- diff-index tests ----------------------------------------------------
263
264 test_expect_success 'diff-index: nice spaces with --indent-heuristic' '
265         git checkout -B diff-index &&
266         git reset --soft HEAD~ &&
267         git diff-index --indent-heuristic -p old -- spaces.txt >out-diff-index-compacted &&
268         compare_diff spaces-compacted-expect out-diff-index-compacted &&
269         git checkout -f master
270 '
271
272 test_expect_success 'diff-index: nice spaces with diff.indentHeuristic=true' '
273         git checkout -B diff-index &&
274         git reset --soft HEAD~ &&
275         git -c diff.indentHeuristic=true diff-index -p old -- spaces.txt >out-diff-index-compacted2 &&
276         compare_diff spaces-compacted-expect out-diff-index-compacted2 &&
277         git checkout -f master
278 '
279
280 test_expect_success 'diff-index: ugly spaces with --no-indent-heuristic' '
281         git checkout -B diff-index &&
282         git reset --soft HEAD~ &&
283         git diff-index --no-indent-heuristic -p old -- spaces.txt >out-diff-index &&
284         compare_diff spaces-expect out-diff-index &&
285         git checkout -f master
286 '
287
288 test_expect_success 'diff-index: ugly spaces with diff.indentHeuristic=false' '
289         git checkout -B diff-index &&
290         git reset --soft HEAD~ &&
291         git -c diff.indentHeuristic=false diff-index -p old -- spaces.txt >out-diff-index2 &&
292         compare_diff spaces-expect out-diff-index2 &&
293         git checkout -f master
294 '
295
296 test_expect_success 'diff-index: --indent-heuristic overrides config' '
297         git checkout -B diff-index &&
298         git reset --soft HEAD~ &&
299         git -c diff.indentHeuristic=false diff-index --indent-heuristic -p old -- spaces.txt >out-diff-index-compacted3 &&
300         compare_diff spaces-compacted-expect out-diff-index-compacted3 &&
301         git checkout -f master
302 '
303
304 test_expect_success 'diff-index: --no-indent-heuristic overrides config' '
305         git checkout -B diff-index &&
306         git reset --soft HEAD~ &&
307         git -c diff.indentHeuristic=true diff-index --no-indent-heuristic -p old -- spaces.txt >out-diff-index3 &&
308         compare_diff spaces-expect out-diff-index3 &&
309         git checkout -f master
310 '
311
312 # --- diff-files tests ----------------------------------------------------
313
314 test_expect_success 'diff-files: nice spaces with --indent-heuristic' '
315         git checkout -B diff-files &&
316         git reset HEAD~ &&
317         git diff-files --indent-heuristic -p spaces.txt >out-diff-files-raw &&
318         grep -v index out-diff-files-raw >out-diff-files-compacted &&
319         compare_diff spaces-compacted-expect out-diff-files-compacted &&
320         git checkout -f master
321 '
322
323 test_expect_success 'diff-files: nice spaces with diff.indentHeuristic=true' '
324         git checkout -B diff-files &&
325         git reset HEAD~ &&
326         git -c diff.indentHeuristic=true diff-files -p spaces.txt >out-diff-files-raw2 &&
327         grep -v index out-diff-files-raw2 >out-diff-files-compacted2 &&
328         compare_diff spaces-compacted-expect out-diff-files-compacted2 &&
329         git checkout -f master
330 '
331
332 test_expect_success 'diff-files: ugly spaces with --no-indent-heuristic' '
333         git checkout -B diff-files &&
334         git reset HEAD~ &&
335         git diff-files --no-indent-heuristic -p spaces.txt >out-diff-files-raw &&
336         grep -v index out-diff-files-raw >out-diff-files &&
337         compare_diff spaces-expect out-diff-files &&
338         git checkout -f master
339 '
340
341 test_expect_success 'diff-files: ugly spaces with diff.indentHeuristic=false' '
342         git checkout -B diff-files &&
343         git reset HEAD~ &&
344         git -c diff.indentHeuristic=false diff-files -p spaces.txt >out-diff-files-raw2 &&
345         grep -v index out-diff-files-raw2 >out-diff-files &&
346         compare_diff spaces-expect out-diff-files &&
347         git checkout -f master
348 '
349
350 test_expect_success 'diff-files: --indent-heuristic overrides config' '
351         git checkout -B diff-files &&
352         git reset HEAD~ &&
353         git -c diff.indentHeuristic=false diff-files --indent-heuristic -p spaces.txt >out-diff-files-raw3 &&
354         grep -v index out-diff-files-raw3 >out-diff-files-compacted &&
355         compare_diff spaces-compacted-expect out-diff-files-compacted &&
356         git checkout -f master
357 '
358
359 test_expect_success 'diff-files: --no-indent-heuristic overrides config' '
360         git checkout -B diff-files &&
361         git reset HEAD~ &&
362         git -c diff.indentHeuristic=true diff-files --no-indent-heuristic -p spaces.txt >out-diff-files-raw4 &&
363         grep -v index out-diff-files-raw4 >out-diff-files &&
364         compare_diff spaces-expect out-diff-files &&
365         git checkout -f master
366 '
367
368 test_done