Use the final_oid_fn to finalize hashing of object IDs
[alt-git.git] / t / t4061-diff-indent.sh
blob7750b87ca16ab39aa965caa4fe71b462d899adc7
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-lib.sh
10 . "$TEST_DIRECTORY"/lib-diff.sh
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
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
28 test_expect_success 'prepare' '
29 cat <<-\EOF >spaces.txt &&
37 EOF
39 cat <<-\EOF >functions.c &&
42 /* function */
43 foo() {
44 foo
49 EOF
51 git add spaces.txt functions.c &&
52 test_tick &&
53 git commit -m initial &&
54 git branch old &&
56 cat <<-\EOF >spaces.txt &&
67 EOF
69 cat <<-\EOF >functions.c &&
72 /* function */
73 bar() {
74 foo
77 /* function */
78 foo() {
79 foo
84 EOF
86 git add spaces.txt functions.c &&
87 test_tick &&
88 git commit -m initial &&
89 git branch new &&
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 @@
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 @@
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 @@
129 /* function */
130 +bar() {
131 + foo
134 +/* function */
135 foo() {
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 @@
147 +/* function */
148 +bar() {
149 + foo
152 /* function */
153 foo() {
158 # --- diff tests ----------------------------------------------------------
160 test_expect_success 'diff: ugly spaces' '
161 git diff --no-indent-heuristic old new -- spaces.txt >out &&
162 compare_diff spaces-expect out
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
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
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
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
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
190 test_expect_success 'diff: ugly functions' '
191 git diff --no-indent-heuristic old new -- functions.c >out &&
192 compare_diff functions-expect out
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
200 # --- blame tests ---------------------------------------------------------
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
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
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
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
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
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
233 # --- diff-tree tests -----------------------------------------------------
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
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
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
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
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
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
265 # --- diff-index tests ----------------------------------------------------
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
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
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
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
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
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
315 # --- diff-files tests ----------------------------------------------------
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
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
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
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
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
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
371 test_done