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