]> git.ipfire.org Git - thirdparty/git.git/blame - t/t4124-apply-ws-rule.sh
path.c: don't call the match function without value in trie_find()
[thirdparty/git.git] / t / t4124-apply-ws-rule.sh
CommitLineData
d5a41641
JH
1#!/bin/sh
2
3604e7c5 3test_description='core.whitespace rules and git apply'
d5a41641
JH
4
5. ./test-lib.sh
6
7prepare_test_file () {
8
9 # A line that has character X is touched iff RULE is in effect:
10 # X RULE
11 # ! trailing-space
12 # @ space-before-tab
f4b05a49
JS
13 # # indent-with-non-tab (default tab width 8)
14 # = indent-with-non-tab,tabwidth=16
a347b17f 15 # % tab-in-indent
d5a41641
JH
16 sed -e "s/_/ /g" -e "s/>/ /" <<-\EOF
17 An_SP in an ordinary line>and a HT.
a347b17f
CW
18 >A HT (%).
19 _>A SP and a HT (@%).
20 _>_A SP, a HT and a SP (@%).
d5a41641
JH
21 _______Seven SP.
22 ________Eight SP (#).
a347b17f
CW
23 _______>Seven SP and a HT (@%).
24 ________>Eight SP and a HT (@#%).
25 _______>_Seven SP, a HT and a SP (@%).
26 ________>_Eight SP, a HT and a SP (@#%).
d5a41641 27 _______________Fifteen SP (#).
a347b17f 28 _______________>Fifteen SP and a HT (@#%).
f4b05a49
JS
29 ________________Sixteen SP (#=).
30 ________________>Sixteen SP and a HT (@#%=).
d5a41641
JH
31 _____a__Five SP, a non WS, two SP.
32 A line with a (!) trailing SP_
33 A line with a (!) trailing HT>
34 EOF
35}
36
37apply_patch () {
38 >target &&
39 sed -e "s|\([ab]\)/file|\1/target|" <patch |
40 git apply "$@"
41}
42
43test_fix () {
d5a41641
JH
44 # fix should not barf
45 apply_patch --whitespace=fix || return 1
46
47 # find touched lines
4fdf71be 48 $DIFF file target | sed -n -e "s/^> //p" >fixed
d5a41641 49
41ccfdd9 50 # the changed lines are all expected to change
d5a41641
JH
51 fixed_cnt=$(wc -l <fixed)
52 case "$1" in
53 '') expect_cnt=$fixed_cnt ;;
54 ?*) expect_cnt=$(grep "[$1]" <fixed | wc -l) ;;
55 esac
56 test $fixed_cnt -eq $expect_cnt || return 1
57
58 # and we are not missing anything
59 case "$1" in
60 '') expect_cnt=0 ;;
61 ?*) expect_cnt=$(grep "[$1]" <file | wc -l) ;;
62 esac
63 test $fixed_cnt -eq $expect_cnt || return 1
64
65 # Get the patch actually applied
66 git diff-files -p target >fixed-patch
67 test -s fixed-patch && return 0
68
69 # Make sure it is complaint-free
70 >target
71 git apply --whitespace=error-all <fixed-patch
72
73}
74
75test_expect_success setup '
76
77 >file &&
78 git add file &&
79 prepare_test_file >file &&
80 git diff-files -p >patch &&
81 >target &&
82 git add target
83
84'
85
86test_expect_success 'whitespace=nowarn, default rule' '
87
88 apply_patch --whitespace=nowarn &&
4fdf71be 89 test_cmp file target
d5a41641
JH
90
91'
92
93test_expect_success 'whitespace=warn, default rule' '
94
95 apply_patch --whitespace=warn &&
4fdf71be 96 test_cmp file target
d5a41641
JH
97
98'
99
100test_expect_success 'whitespace=error-all, default rule' '
101
0a5e3c50 102 test_must_fail apply_patch --whitespace=error-all &&
ec10b018 103 test_must_be_empty target
d5a41641
JH
104
105'
106
107test_expect_success 'whitespace=error-all, no rule' '
108
109 git config core.whitespace -trailing,-space-before,-indent &&
110 apply_patch --whitespace=error-all &&
4fdf71be 111 test_cmp file target
d5a41641
JH
112
113'
114
cf1b7869
JH
115test_expect_success 'whitespace=error-all, no rule (attribute)' '
116
117 git config --unset core.whitespace &&
118 echo "target -whitespace" >.gitattributes &&
119 apply_patch --whitespace=error-all &&
4fdf71be 120 test_cmp file target
cf1b7869
JH
121
122'
123
d35711ad
JS
124test_expect_success 'spaces inserted by tab-in-indent' '
125
126 git config core.whitespace -trailing,-space,-indent,tab &&
127 rm -f .gitattributes &&
128 test_fix % &&
129 sed -e "s/_/ /g" -e "s/>/ /" <<-\EOF >expect &&
130 An_SP in an ordinary line>and a HT.
131 ________A HT (%).
132 ________A SP and a HT (@%).
133 _________A SP, a HT and a SP (@%).
134 _______Seven SP.
135 ________Eight SP (#).
136 ________Seven SP and a HT (@%).
137 ________________Eight SP and a HT (@#%).
138 _________Seven SP, a HT and a SP (@%).
139 _________________Eight SP, a HT and a SP (@#%).
140 _______________Fifteen SP (#).
141 ________________Fifteen SP and a HT (@#%).
f4b05a49
JS
142 ________________Sixteen SP (#=).
143 ________________________Sixteen SP and a HT (@#%=).
d35711ad
JS
144 _____a__Five SP, a non WS, two SP.
145 A line with a (!) trailing SP_
146 A line with a (!) trailing HT>
147 EOF
148 test_cmp expect target
149
150'
151
d5a41641
JH
152for t in - ''
153do
154 case "$t" in '') tt='!' ;; *) tt= ;; esac
155 for s in - ''
156 do
157 case "$s" in '') ts='@' ;; *) ts= ;; esac
158 for i in - ''
159 do
f4b05a49 160 case "$i" in '') ti='#' ti16='=';; *) ti= ti16= ;; esac
a347b17f
CW
161 for h in - ''
162 do
163 [ -z "$h$i" ] && continue
164 case "$h" in '') th='%' ;; *) th= ;; esac
165 rule=${t}trailing,${s}space,${i}indent,${h}tab
166
167 rm -f .gitattributes
168 test_expect_success "rule=$rule" '
169 git config core.whitespace "$rule" &&
170 test_fix "$tt$ts$ti$th"
171 '
172
f4b05a49
JS
173 test_expect_success "rule=$rule,tabwidth=16" '
174 git config core.whitespace "$rule,tabwidth=16" &&
175 test_fix "$tt$ts$ti16$th"
176 '
177
a347b17f
CW
178 test_expect_success "rule=$rule (attributes)" '
179 git config --unset core.whitespace &&
180 echo "target whitespace=$rule" >.gitattributes &&
181 test_fix "$tt$ts$ti$th"
182 '
183
f4b05a49
JS
184 test_expect_success "rule=$rule,tabwidth=16 (attributes)" '
185 echo "target whitespace=$rule,tabwidth=16" >.gitattributes &&
186 test_fix "$tt$ts$ti16$th"
187 '
188
a347b17f 189 done
d5a41641
JH
190 done
191 done
192done
193
735c6744
SG
194create_patch () {
195 sed -e "s/_/ /" <<-\EOF
196 diff --git a/target b/target
197 index e69de29..8bd6648 100644
198 --- a/target
199 +++ b/target
422a82f2
JH
200 @@ -0,0 +1,3 @@
201 +An empty line follows
202 +
735c6744
SG
203 +A line with trailing whitespace and no newline_
204 \ No newline at end of file
205 EOF
206}
207
208test_expect_success 'trailing whitespace & no newline at the end of file' '
209 >target &&
422a82f2
JH
210 create_patch >patch-file &&
211 git apply --whitespace=fix patch-file &&
212 grep "newline$" target &&
213 grep "^$" target
735c6744
SG
214'
215
ef2035c5 216test_expect_success 'blank at EOF with --whitespace=fix (1)' '
33fc5211
JN
217 test_might_fail git config --unset core.whitespace &&
218 rm -f .gitattributes &&
ef2035c5
JH
219
220 { echo a; echo b; echo c; } >one &&
221 git add one &&
222 { echo a; echo b; echo c; } >expect &&
223 { cat expect; echo; } >one &&
224 git diff -- one >patch &&
225
226 git checkout one &&
227 git apply --whitespace=fix patch &&
228 test_cmp expect one
229'
230
231test_expect_success 'blank at EOF with --whitespace=fix (2)' '
232 { echo a; echo b; echo c; } >one &&
233 git add one &&
234 { echo a; echo c; } >expect &&
235 { cat expect; echo; echo; } >one &&
236 git diff -- one >patch &&
237
238 git checkout one &&
239 git apply --whitespace=fix patch &&
240 test_cmp expect one
241'
242
efa57443
JH
243test_expect_success 'blank at EOF with --whitespace=fix (3)' '
244 { echo a; echo b; echo; } >one &&
245 git add one &&
246 { echo a; echo c; echo; } >expect &&
247 { cat expect; echo; echo; } >one &&
248 git diff -- one >patch &&
249
250 git checkout one &&
251 git apply --whitespace=fix patch &&
252 test_cmp expect one
253'
254
255test_expect_success 'blank at end of hunk, not at EOF with --whitespace=fix' '
256 { echo a; echo b; echo; echo; echo; echo; echo; echo d; } >one &&
257 git add one &&
258 { echo a; echo c; echo; echo; echo; echo; echo; echo; echo d; } >expect &&
259 cp expect one &&
260 git diff -- one >patch &&
261
262 git checkout one &&
263 git apply --whitespace=fix patch &&
264 test_cmp expect one
265'
266
77b15bbd
JH
267test_expect_success 'blank at EOF with --whitespace=warn' '
268 { echo a; echo b; echo c; } >one &&
269 git add one &&
270 echo >>one &&
271 cat one >expect &&
272 git diff -- one >patch &&
273
274 git checkout one &&
275 git apply --whitespace=warn patch 2>error &&
276 test_cmp expect one &&
277 grep "new blank line at EOF" error
278'
279
280test_expect_success 'blank at EOF with --whitespace=error' '
281 { echo a; echo b; echo c; } >one &&
282 git add one &&
283 cat one >expect &&
284 echo >>one &&
285 git diff -- one >patch &&
286
287 git checkout one &&
288 test_must_fail git apply --whitespace=error patch 2>error &&
289 test_cmp expect one &&
290 grep "new blank line at EOF" error
291'
292
94ea026b
JH
293test_expect_success 'blank but not empty at EOF' '
294 { echo a; echo b; echo c; } >one &&
295 git add one &&
296 echo " " >>one &&
297 cat one >expect &&
298 git diff -- one >patch &&
299
300 git checkout one &&
301 git apply --whitespace=warn patch 2>error &&
302 test_cmp expect one &&
303 grep "new blank line at EOF" error
304'
305
c1376c12
BG
306test_expect_success 'applying beyond EOF requires one non-blank context line' '
307 { echo; echo; echo; echo; } >one &&
308 git add one &&
309 { echo b; } >>one &&
310 git diff -- one >patch &&
311
312 git checkout one &&
313 { echo a; echo; } >one &&
314 cp one expect &&
315 test_must_fail git apply --whitespace=fix patch &&
dcbaa0b3 316 test_cmp expect one &&
c1376c12 317 test_must_fail git apply --ignore-space-change --whitespace=fix patch &&
dcbaa0b3 318 test_cmp expect one
c1376c12
BG
319'
320
321test_expect_success 'tons of blanks at EOF should not apply' '
322 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do
323 echo; echo; echo; echo;
324 done >one &&
325 git add one &&
326 echo a >>one &&
327 git diff -- one >patch &&
328
329 >one &&
330 test_must_fail git apply --whitespace=fix patch &&
331 test_must_fail git apply --ignore-space-change --whitespace=fix patch
332'
333
334test_expect_success 'missing blank line at end with --whitespace=fix' '
335 echo a >one &&
336 echo >>one &&
337 git add one &&
338 echo b >>one &&
339 cp one expect &&
340 git diff -- one >patch &&
341 echo a >one &&
342 cp one saved-one &&
343 test_must_fail git apply patch &&
344 git apply --whitespace=fix patch &&
dcbaa0b3 345 test_cmp expect one &&
c1376c12
BG
346 mv saved-one one &&
347 git apply --ignore-space-change --whitespace=fix patch &&
dcbaa0b3 348 test_cmp expect one
c1376c12
BG
349'
350
351test_expect_success 'two missing blank lines at end with --whitespace=fix' '
352 { echo a; echo; echo b; echo c; } >one &&
353 cp one no-blank-lines &&
354 { echo; echo; } >>one &&
355 git add one &&
356 echo d >>one &&
357 cp one expect &&
358 echo >>one &&
359 git diff -- one >patch &&
360 cp no-blank-lines one &&
361 test_must_fail git apply patch &&
362 git apply --whitespace=fix patch &&
dcbaa0b3 363 test_cmp expect one &&
c1376c12
BG
364 mv no-blank-lines one &&
365 test_must_fail git apply patch &&
366 git apply --ignore-space-change --whitespace=fix patch &&
dcbaa0b3 367 test_cmp expect one
c1376c12
BG
368'
369
0c3ef984
BG
370test_expect_success 'missing blank line at end, insert before end, --whitespace=fix' '
371 { echo a; echo; } >one &&
372 git add one &&
373 { echo b; echo a; echo; } >one &&
374 cp one expect &&
375 git diff -- one >patch &&
376 echo a >one &&
377 test_must_fail git apply patch &&
378 git apply --whitespace=fix patch &&
dcbaa0b3 379 test_cmp expect one
0c3ef984
BG
380'
381
c1376c12
BG
382test_expect_success 'shrink file with tons of missing blanks at end of file' '
383 { echo a; echo b; echo c; } >one &&
384 cp one no-blank-lines &&
385 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do
386 echo; echo; echo; echo;
387 done >>one &&
388 git add one &&
389 echo a >one &&
390 cp one expect &&
391 git diff -- one >patch &&
392 cp no-blank-lines one &&
393 test_must_fail git apply patch &&
394 git apply --whitespace=fix patch &&
dcbaa0b3 395 test_cmp expect one &&
c1376c12
BG
396 mv no-blank-lines one &&
397 git apply --ignore-space-change --whitespace=fix patch &&
dcbaa0b3 398 test_cmp expect one
c1376c12
BG
399'
400
401test_expect_success 'missing blanks at EOF must only match blank lines' '
402 { echo a; echo b; } >one &&
403 git add one &&
404 { echo c; echo d; } >>one &&
405 git diff -- one >patch &&
406
407 echo a >one &&
2dec68cf 408 test_must_fail git apply patch &&
c1376c12
BG
409 test_must_fail git apply --whitespace=fix patch &&
410 test_must_fail git apply --ignore-space-change --whitespace=fix patch
411'
412
413sed -e's/Z//' >one <<EOF
414a
415b
416c
417 Z
418EOF
419
420test_expect_success 'missing blank line should match context line with spaces' '
421 git add one &&
422 echo d >>one &&
423 git diff -- one >patch &&
424 { echo a; echo b; echo c; } >one &&
425 cp one expect &&
426 { echo; echo d; } >>expect &&
427 git add one &&
428
429 git apply --whitespace=fix patch &&
dcbaa0b3 430 test_cmp expect one
c1376c12
BG
431'
432
433sed -e's/Z//' >one <<EOF
434a
435b
436c
437 Z
438EOF
439
440test_expect_success 'same, but with the --ignore-space-option' '
441 git add one &&
442 echo d >>one &&
443 cp one expect &&
444 git diff -- one >patch &&
445 { echo a; echo b; echo c; } >one &&
446 git add one &&
447
448 git checkout-index -f one &&
449 git apply --ignore-space-change --whitespace=fix patch &&
dcbaa0b3 450 test_cmp expect one
c1376c12
BG
451'
452
453test_expect_success 'same, but with CR-LF line endings && cr-at-eol set' '
454 git config core.whitespace cr-at-eol &&
455 printf "a\r\n" >one &&
456 printf "b\r\n" >>one &&
457 printf "c\r\n" >>one &&
458 cp one save-one &&
2dec68cf 459 printf " \r\n" >>one &&
c1376c12
BG
460 git add one &&
461 printf "d\r\n" >>one &&
462 cp one expect &&
463 git diff -- one >patch &&
464 mv save-one one &&
465
466 git apply --ignore-space-change --whitespace=fix patch &&
dcbaa0b3 467 test_cmp expect one
c1376c12
BG
468'
469
c24f3aba 470test_expect_success 'CR-LF line endings && add line && text=auto' '
c1376c12
BG
471 git config --unset core.whitespace &&
472 printf "a\r\n" >one &&
c24f3aba
TB
473 cp one save-one &&
474 git add one &&
c1376c12 475 printf "b\r\n" >>one &&
c24f3aba
TB
476 cp one expect &&
477 git diff -- one >patch &&
478 mv save-one one &&
479 echo "one text=auto" >.gitattributes &&
480 git apply patch &&
dcbaa0b3 481 test_cmp expect one
c24f3aba
TB
482'
483
484test_expect_success 'CR-LF line endings && change line && text=auto' '
485 printf "a\r\n" >one &&
c1376c12 486 cp one save-one &&
c1376c12 487 git add one &&
c24f3aba 488 printf "b\r\n" >one &&
c1376c12 489 cp one expect &&
c1376c12
BG
490 git diff -- one >patch &&
491 mv save-one one &&
c24f3aba
TB
492 echo "one text=auto" >.gitattributes &&
493 git apply patch &&
dcbaa0b3 494 test_cmp expect one
c24f3aba 495'
c1376c12 496
c24f3aba
TB
497test_expect_success 'LF in repo, CRLF in worktree && change line && text=auto' '
498 printf "a\n" >one &&
499 git add one &&
500 printf "b\r\n" >one &&
501 git diff -- one >patch &&
502 printf "a\r\n" >one &&
503 echo "one text=auto" >.gitattributes &&
504 git -c core.eol=CRLF apply patch &&
505 printf "b\r\n" >expect &&
dcbaa0b3 506 test_cmp expect one
c1376c12
BG
507'
508
250b3c6c
JH
509test_expect_success 'whitespace=fix to expand' '
510 qz_to_tab_space >preimage <<-\EOF &&
511 QQa
512 QQb
513 QQc
514 ZZZZZZZZZZZZZZZZd
515 QQe
516 QQf
517 QQg
518 EOF
519 qz_to_tab_space >patch <<-\EOF &&
520 diff --git a/preimage b/preimage
521 --- a/preimage
522 +++ b/preimage
523 @@ -1,7 +1,6 @@
524 QQa
525 QQb
526 QQc
527 -QQd
528 QQe
529 QQf
530 QQg
531 EOF
532 git -c core.whitespace=tab-in-indent apply --whitespace=fix patch
533'
534
477a08af
JH
535test_expect_success 'whitespace check skipped for excluded paths' '
536 git config core.whitespace blank-at-eol &&
537 >used &&
538 >unused &&
539 git add used unused &&
540 echo "used" >used &&
541 echo "unused " >unused &&
542 git diff-files -p used unused >patch &&
543 git apply --include=used --stat --whitespace=error <patch
544'
545
d5a41641 546test_done