Git 2.46-rc0
[git/gitster.git] / t / t4061-diff-indent.sh
blob2942e5d9b93899bf7bc517e95c94788dbca1d18f
1 #!/bin/sh
3 test_description='Test diff indent heuristic.
6 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
7 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9 TEST_PASSES_SANITIZE_LEAK=true
10 . ./test-lib.sh
11 . "$TEST_DIRECTORY"/lib-diff.sh
13 # Compare two diff outputs. Ignore "index" lines, because we don't
14 # care about SHA-1s or file modes.
15 compare_diff () {
16 sed -e "/^index /d" <"$1" >.tmp-1
17 sed -e "/^index /d" <"$2" >.tmp-2
18 test_cmp .tmp-1 .tmp-2 && rm -f .tmp-1 .tmp-2
21 # Compare blame output using the expectation for a diff as reference.
22 # Only look for the lines coming from non-boundary commits.
23 compare_blame () {
24 sed -n -e "1,4d" -e "s/^+//p" <"$1" >.tmp-1
25 sed -ne "s/^[^^][^)]*) *//p" <"$2" >.tmp-2
26 test_cmp .tmp-1 .tmp-2 && rm -f .tmp-1 .tmp-2
29 test_expect_success 'prepare' '
30 cat <<-\EOF >spaces.txt &&
38 EOF
40 cat <<-\EOF >functions.c &&
43 /* function */
44 foo() {
45 foo
50 EOF
52 git add spaces.txt functions.c &&
53 test_tick &&
54 git commit -m initial &&
55 git branch old &&
57 cat <<-\EOF >spaces.txt &&
68 EOF
70 cat <<-\EOF >functions.c &&
73 /* function */
74 bar() {
75 foo
78 /* function */
79 foo() {
80 foo
85 EOF
87 git add spaces.txt functions.c &&
88 test_tick &&
89 git commit -m initial &&
90 git branch new &&
92 tr "_" " " <<-\EOF >spaces-expect &&
93 diff --git a/spaces.txt b/spaces.txt
94 --- a/spaces.txt
95 +++ b/spaces.txt
96 @@ -3,5 +3,8 @@
107 tr "_" " " <<-\EOF >spaces-compacted-expect &&
108 diff --git a/spaces.txt b/spaces.txt
109 --- a/spaces.txt
110 +++ b/spaces.txt
111 @@ -2,6 +2,9 @@
123 tr "_" " " <<-\EOF >functions-expect &&
124 diff --git a/functions.c b/functions.c
125 --- a/functions.c
126 +++ b/functions.c
127 @@ -1,6 +1,11 @@
130 /* function */
131 +bar() {
132 + foo
135 +/* function */
136 foo() {
141 tr "_" " " <<-\EOF >functions-compacted-expect
142 diff --git a/functions.c b/functions.c
143 --- a/functions.c
144 +++ b/functions.c
145 @@ -1,5 +1,10 @@
148 +/* function */
149 +bar() {
150 + foo
153 /* function */
154 foo() {
159 # --- diff tests ----------------------------------------------------------
161 test_expect_success 'diff: ugly spaces' '
162 git diff --no-indent-heuristic old new -- spaces.txt >out &&
163 compare_diff spaces-expect out
166 test_expect_success 'diff: --no-indent-heuristic overrides config' '
167 git -c diff.indentHeuristic=true diff --no-indent-heuristic old new -- spaces.txt >out2 &&
168 compare_diff spaces-expect out2
171 test_expect_success 'diff: nice spaces with --indent-heuristic' '
172 git -c diff.indentHeuristic=false diff --indent-heuristic old new -- spaces.txt >out-compacted &&
173 compare_diff spaces-compacted-expect out-compacted
176 test_expect_success 'diff: nice spaces with diff.indentHeuristic=true' '
177 git -c diff.indentHeuristic=true diff old new -- spaces.txt >out-compacted2 &&
178 compare_diff spaces-compacted-expect out-compacted2
181 test_expect_success 'diff: --indent-heuristic with --patience' '
182 git diff --indent-heuristic --patience old new -- spaces.txt >out-compacted3 &&
183 compare_diff spaces-compacted-expect out-compacted3
186 test_expect_success 'diff: --indent-heuristic with --histogram' '
187 git diff --indent-heuristic --histogram old new -- spaces.txt >out-compacted4 &&
188 compare_diff spaces-compacted-expect out-compacted4
191 test_expect_success 'diff: ugly functions' '
192 git diff --no-indent-heuristic old new -- functions.c >out &&
193 compare_diff functions-expect out
196 test_expect_success 'diff: nice functions with --indent-heuristic' '
197 git diff --indent-heuristic old new -- functions.c >out-compacted &&
198 compare_diff functions-compacted-expect out-compacted
201 # --- blame tests ---------------------------------------------------------
203 test_expect_success 'blame: nice spaces with --indent-heuristic' '
204 git blame --indent-heuristic old..new -- spaces.txt >out-blame-compacted &&
205 compare_blame spaces-compacted-expect out-blame-compacted
208 test_expect_success 'blame: nice spaces with diff.indentHeuristic=true' '
209 git -c diff.indentHeuristic=true blame old..new -- spaces.txt >out-blame-compacted2 &&
210 compare_blame spaces-compacted-expect out-blame-compacted2
213 test_expect_success 'blame: ugly spaces with --no-indent-heuristic' '
214 git blame --no-indent-heuristic old..new -- spaces.txt >out-blame &&
215 compare_blame spaces-expect out-blame
218 test_expect_success 'blame: ugly spaces with diff.indentHeuristic=false' '
219 git -c diff.indentHeuristic=false blame old..new -- spaces.txt >out-blame2 &&
220 compare_blame spaces-expect out-blame2
223 test_expect_success 'blame: --no-indent-heuristic overrides config' '
224 git -c diff.indentHeuristic=true blame --no-indent-heuristic old..new -- spaces.txt >out-blame3 &&
225 git blame old..new -- spaces.txt >out-blame &&
226 compare_blame spaces-expect out-blame3
229 test_expect_success 'blame: --indent-heuristic overrides config' '
230 git -c diff.indentHeuristic=false blame --indent-heuristic old..new -- spaces.txt >out-blame-compacted3 &&
231 compare_blame spaces-compacted-expect out-blame-compacted2
234 # --- diff-tree tests -----------------------------------------------------
236 test_expect_success 'diff-tree: nice spaces with --indent-heuristic' '
237 git diff-tree --indent-heuristic -p old new -- spaces.txt >out-diff-tree-compacted &&
238 compare_diff spaces-compacted-expect out-diff-tree-compacted
241 test_expect_success 'diff-tree: nice spaces with diff.indentHeuristic=true' '
242 git -c diff.indentHeuristic=true diff-tree -p old new -- spaces.txt >out-diff-tree-compacted2 &&
243 compare_diff spaces-compacted-expect out-diff-tree-compacted2
246 test_expect_success 'diff-tree: ugly spaces with --no-indent-heuristic' '
247 git diff-tree --no-indent-heuristic -p old new -- spaces.txt >out-diff-tree &&
248 compare_diff spaces-expect out-diff-tree
251 test_expect_success 'diff-tree: ugly spaces with diff.indentHeuristic=false' '
252 git -c diff.indentHeuristic=false diff-tree -p old new -- spaces.txt >out-diff-tree2 &&
253 compare_diff spaces-expect out-diff-tree2
256 test_expect_success 'diff-tree: --indent-heuristic overrides config' '
257 git -c diff.indentHeuristic=false diff-tree --indent-heuristic -p old new -- spaces.txt >out-diff-tree-compacted3 &&
258 compare_diff spaces-compacted-expect out-diff-tree-compacted3
261 test_expect_success 'diff-tree: --no-indent-heuristic overrides config' '
262 git -c diff.indentHeuristic=true diff-tree --no-indent-heuristic -p old new -- spaces.txt >out-diff-tree3 &&
263 compare_diff spaces-expect out-diff-tree3
266 # --- diff-index tests ----------------------------------------------------
268 test_expect_success 'diff-index: nice spaces with --indent-heuristic' '
269 git checkout -B diff-index &&
270 git reset --soft HEAD~ &&
271 git diff-index --indent-heuristic -p old -- spaces.txt >out-diff-index-compacted &&
272 compare_diff spaces-compacted-expect out-diff-index-compacted &&
273 git checkout -f main
276 test_expect_success 'diff-index: nice spaces with diff.indentHeuristic=true' '
277 git checkout -B diff-index &&
278 git reset --soft HEAD~ &&
279 git -c diff.indentHeuristic=true diff-index -p old -- spaces.txt >out-diff-index-compacted2 &&
280 compare_diff spaces-compacted-expect out-diff-index-compacted2 &&
281 git checkout -f main
284 test_expect_success 'diff-index: ugly spaces with --no-indent-heuristic' '
285 git checkout -B diff-index &&
286 git reset --soft HEAD~ &&
287 git diff-index --no-indent-heuristic -p old -- spaces.txt >out-diff-index &&
288 compare_diff spaces-expect out-diff-index &&
289 git checkout -f main
292 test_expect_success 'diff-index: ugly spaces with diff.indentHeuristic=false' '
293 git checkout -B diff-index &&
294 git reset --soft HEAD~ &&
295 git -c diff.indentHeuristic=false diff-index -p old -- spaces.txt >out-diff-index2 &&
296 compare_diff spaces-expect out-diff-index2 &&
297 git checkout -f main
300 test_expect_success 'diff-index: --indent-heuristic overrides config' '
301 git checkout -B diff-index &&
302 git reset --soft HEAD~ &&
303 git -c diff.indentHeuristic=false diff-index --indent-heuristic -p old -- spaces.txt >out-diff-index-compacted3 &&
304 compare_diff spaces-compacted-expect out-diff-index-compacted3 &&
305 git checkout -f main
308 test_expect_success 'diff-index: --no-indent-heuristic overrides config' '
309 git checkout -B diff-index &&
310 git reset --soft HEAD~ &&
311 git -c diff.indentHeuristic=true diff-index --no-indent-heuristic -p old -- spaces.txt >out-diff-index3 &&
312 compare_diff spaces-expect out-diff-index3 &&
313 git checkout -f main
316 # --- diff-files tests ----------------------------------------------------
318 test_expect_success 'diff-files: nice spaces with --indent-heuristic' '
319 git checkout -B diff-files &&
320 git reset HEAD~ &&
321 git diff-files --indent-heuristic -p spaces.txt >out-diff-files-raw &&
322 grep -v index out-diff-files-raw >out-diff-files-compacted &&
323 compare_diff spaces-compacted-expect out-diff-files-compacted &&
324 git checkout -f main
327 test_expect_success 'diff-files: nice spaces with diff.indentHeuristic=true' '
328 git checkout -B diff-files &&
329 git reset HEAD~ &&
330 git -c diff.indentHeuristic=true diff-files -p spaces.txt >out-diff-files-raw2 &&
331 grep -v index out-diff-files-raw2 >out-diff-files-compacted2 &&
332 compare_diff spaces-compacted-expect out-diff-files-compacted2 &&
333 git checkout -f main
336 test_expect_success 'diff-files: ugly spaces with --no-indent-heuristic' '
337 git checkout -B diff-files &&
338 git reset HEAD~ &&
339 git diff-files --no-indent-heuristic -p spaces.txt >out-diff-files-raw &&
340 grep -v index out-diff-files-raw >out-diff-files &&
341 compare_diff spaces-expect out-diff-files &&
342 git checkout -f main
345 test_expect_success 'diff-files: ugly spaces with diff.indentHeuristic=false' '
346 git checkout -B diff-files &&
347 git reset HEAD~ &&
348 git -c diff.indentHeuristic=false diff-files -p spaces.txt >out-diff-files-raw2 &&
349 grep -v index out-diff-files-raw2 >out-diff-files &&
350 compare_diff spaces-expect out-diff-files &&
351 git checkout -f main
354 test_expect_success 'diff-files: --indent-heuristic overrides config' '
355 git checkout -B diff-files &&
356 git reset HEAD~ &&
357 git -c diff.indentHeuristic=false diff-files --indent-heuristic -p spaces.txt >out-diff-files-raw3 &&
358 grep -v index out-diff-files-raw3 >out-diff-files-compacted &&
359 compare_diff spaces-compacted-expect out-diff-files-compacted &&
360 git checkout -f main
363 test_expect_success 'diff-files: --no-indent-heuristic overrides config' '
364 git checkout -B diff-files &&
365 git reset HEAD~ &&
366 git -c diff.indentHeuristic=true diff-files --no-indent-heuristic -p spaces.txt >out-diff-files-raw4 &&
367 grep -v index out-diff-files-raw4 >out-diff-files &&
368 compare_diff spaces-expect out-diff-files &&
369 git checkout -f main
372 test_done