3 test_description
='core.whitespace rules and git apply'
9 # A line that has character X is touched iff RULE is in effect:
13 # # indent-with-non-tab (default tab width 8)
14 # = indent-with-non-tab,tabwidth=16
16 sed -e "s/_/ /g" -e "s/>/ /" <<-\EOF
17 An_SP in an ordinary line>and a HT.
20 _>_A SP, a HT and a SP (@%).
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 (@#%).
27 _______________Fifteen SP (#).
28 _______________>Fifteen SP and a HT (@#%).
29 ________________Sixteen SP (#=).
30 ________________>Sixteen SP and a HT (@#%=).
31 _____a__Five SP, a non WS, two SP.
32 A line with a (!) trailing SP_
33 A line with a (!) trailing HT>
41 cmd_prefix=test_must_fail &&
45 sed -e "s|\([ab]\)/file|\1/target|" <patch |
46 $cmd_prefix git apply "$@"
51 apply_patch --whitespace=fix || return 1
54 $DIFF file target | sed -n -e "s/^> //p" >fixed
55 # busybox's diff(1) doesn't output normal format
58 $DIFF -u file target |
59 grep -v '^+++ target' |
60 sed -ne "/^+/s/+//p" >fixed
63 # the changed lines are all expected to change
64 fixed_cnt=$(wc -l <fixed)
66 '') expect_cnt=$fixed_cnt ;;
67 ?*) expect_cnt=$(grep "[$1]" <fixed | wc -l) ;;
69 test $fixed_cnt -eq $expect_cnt || return 1
71 # and we are not missing anything
74 ?*) expect_cnt=$(grep "[$1]" <file | wc -l) ;;
76 test $fixed_cnt -eq $expect_cnt || return 1
78 # Get the patch actually applied
79 git diff-files -p target >fixed-patch
80 test -s fixed-patch && return 0
82 # Make sure it is complaint-free
84 git apply --whitespace=error-all <fixed-patch
88 test_expect_success setup '
92 prepare_test_file >file &&
93 git diff-files -p >patch &&
99 test_expect_success 'whitespace=nowarn, default rule' '
101 apply_patch --whitespace=nowarn &&
106 test_expect_success 'whitespace=warn, default rule' '
108 apply_patch --whitespace=warn &&
113 test_expect_success 'whitespace=error-all, default rule' '
115 apply_patch ! --whitespace=error-all &&
116 test_must_be_empty target
120 test_expect_success 'whitespace=error-all, no rule' '
122 git config core.whitespace -trailing,-space-before,-indent &&
123 apply_patch --whitespace=error-all &&
128 test_expect_success 'whitespace=error-all, no rule (attribute)' '
130 git config --unset core.whitespace &&
131 echo "target -whitespace" >.gitattributes &&
132 apply_patch --whitespace=error-all &&
137 test_expect_success 'spaces inserted by tab-in-indent' '
139 git config core.whitespace -trailing,-space,-indent,tab &&
140 rm -f .gitattributes &&
142 sed -e "s/_/ /g" -e "s/>/ /" <<-\
EOF >expect &&
143 An_SP in an ordinary line>and a HT.
145 ________A SP and a HT (@%).
146 _________A SP, a HT and a SP (@%).
148 ________Eight SP (#).
149 ________Seven SP and a HT (@%).
150 ________________Eight SP and a HT (@#%).
151 _________Seven SP, a HT and a SP (@%).
152 _________________Eight SP, a HT and a SP (@#%).
153 _______________Fifteen SP (#).
154 ________________Fifteen SP and a HT (@#%).
155 ________________Sixteen SP (#=).
156 ________________________Sixteen SP and a HT (@#%=).
157 _____a__Five SP, a non WS, two SP.
158 A line with a (!) trailing SP_
159 A line with a (!) trailing HT>
161 test_cmp expect target
167 case "$t" in '') tt
='!' ;; *) tt
= ;; esac
170 case "$s" in '') ts
='@' ;; *) ts
= ;; esac
173 case "$i" in '') ti
='#' ti16
='=';; *) ti
= ti16
= ;; esac
176 [ -z "$h$i" ] && continue
177 case "$h" in '') th
='%' ;; *) th
= ;; esac
178 rule
=${t}trailing,${s}space,${i}indent,${h}tab
181 test_expect_success
"rule=$rule" '
182 git config core.whitespace "$rule" &&
183 test_fix "$tt$ts$ti$th"
186 test_expect_success
"rule=$rule,tabwidth=16" '
187 git config core.whitespace "$rule,tabwidth=16" &&
188 test_fix "$tt$ts$ti16$th"
191 test_expect_success
"rule=$rule (attributes)" '
192 git config --unset core.whitespace &&
193 echo "target whitespace=$rule" >.gitattributes &&
194 test_fix "$tt$ts$ti$th"
197 test_expect_success
"rule=$rule,tabwidth=16 (attributes)" '
198 echo "target whitespace=$rule,tabwidth=16" >.gitattributes &&
199 test_fix "$tt$ts$ti16$th"
208 sed -e "s/_/ /" <<-\EOF
209 diff --git a/target b/target
210 index e69de29..8bd6648 100644
214 +An empty line follows
216 +A line with trailing whitespace and no newline_
217 \ No newline at end of file
221 test_expect_success 'trailing whitespace & no newline at the end of file' '
223 create_patch >patch-file &&
224 git apply --whitespace=fix patch-file &&
225 grep "newline$" target &&
229 test_expect_success 'blank at EOF with --whitespace=fix (1)' '
230 test_might_fail git config --unset core.whitespace &&
231 rm -f .gitattributes &&
233 { echo a; echo b; echo c; } >one &&
235 { echo a; echo b; echo c; } >expect &&
236 { cat expect; echo; } >one &&
237 git diff -- one >patch &&
240 git apply --whitespace=fix patch &&
244 test_expect_success 'blank at EOF with --whitespace=fix (2)' '
245 { echo a; echo b; echo c; } >one &&
247 { echo a; echo c; } >expect &&
248 { cat expect; echo; echo; } >one &&
249 git diff -- one >patch &&
252 git apply --whitespace=fix patch &&
256 test_expect_success 'blank at EOF with --whitespace=fix (3)' '
257 { echo a; echo b; echo; } >one &&
259 { echo a; echo c; echo; } >expect &&
260 { cat expect; echo; echo; } >one &&
261 git diff -- one >patch &&
264 git apply --whitespace=fix patch &&
268 test_expect_success 'blank at end of hunk, not at EOF with --whitespace=fix' '
269 { echo a; echo b; echo; echo; echo; echo; echo; echo d; } >one &&
271 { echo a; echo c; echo; echo; echo; echo; echo; echo; echo d; } >expect &&
273 git diff -- one >patch &&
276 git apply --whitespace=fix patch &&
280 test_expect_success 'blank at EOF with --whitespace=warn' '
281 { echo a; echo b; echo c; } >one &&
285 git diff -- one >patch &&
288 git apply --whitespace=warn patch 2>error &&
289 test_cmp expect one &&
290 grep "new blank line at EOF" error
293 test_expect_success 'blank at EOF with --whitespace=error' '
294 { echo a; echo b; echo c; } >one &&
298 git diff -- one >patch &&
301 test_must_fail git apply --whitespace=error patch 2>error &&
302 test_cmp expect one &&
303 grep "new blank line at EOF" error
306 test_expect_success 'blank but not empty at EOF' '
307 { echo a; echo b; echo c; } >one &&
311 git diff -- one >patch &&
314 git apply --whitespace=warn patch 2>error &&
315 test_cmp expect one &&
316 grep "new blank line at EOF" error
319 test_expect_success 'applying beyond EOF requires one non-blank context line' '
320 { echo; echo; echo; echo; } >one &&
323 git diff -- one >patch &&
326 { echo a; echo; } >one &&
328 test_must_fail git apply --whitespace=fix patch &&
329 test_cmp expect one &&
330 test_must_fail git apply --ignore-space-change --whitespace=fix patch &&
334 test_expect_success 'tons of blanks at EOF should not apply' '
335 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do
336 echo; echo; echo; echo;
340 git diff -- one >patch &&
343 test_must_fail git apply --whitespace=fix patch &&
344 test_must_fail git apply --ignore-space-change --whitespace=fix patch
347 test_expect_success 'missing blank line at end with --whitespace=fix' '
353 git diff -- one >patch &&
356 test_must_fail git apply patch &&
357 git apply --whitespace=fix patch &&
358 test_cmp expect one &&
360 git apply --ignore-space-change --whitespace=fix patch &&
364 test_expect_success 'two missing blank lines at end with --whitespace=fix' '
365 { echo a; echo; echo b; echo c; } >one &&
366 cp one no-blank-lines &&
367 { echo; echo; } >>one &&
372 git diff -- one >patch &&
373 cp no-blank-lines one &&
374 test_must_fail git apply patch &&
375 git apply --whitespace=fix patch &&
376 test_cmp expect one &&
377 mv no-blank-lines one &&
378 test_must_fail git apply patch &&
379 git apply --ignore-space-change --whitespace=fix patch &&
383 test_expect_success 'missing blank line at end, insert before end, --whitespace=fix' '
384 { echo a; echo; } >one &&
386 { echo b; echo a; echo; } >one &&
388 git diff -- one >patch &&
390 test_must_fail git apply patch &&
391 git apply --whitespace=fix patch &&
395 test_expect_success 'shrink file with tons of missing blanks at end of file' '
396 { echo a; echo b; echo c; } >one &&
397 cp one no-blank-lines &&
398 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do
399 echo; echo; echo; echo;
404 git diff -- one >patch &&
405 cp no-blank-lines one &&
406 test_must_fail git apply patch &&
407 git apply --whitespace=fix patch &&
408 test_cmp expect one &&
409 mv no-blank-lines one &&
410 git apply --ignore-space-change --whitespace=fix patch &&
414 test_expect_success 'missing blanks at EOF must only match blank lines' '
415 { echo a; echo b; } >one &&
417 { echo c; echo d; } >>one &&
418 git diff -- one >patch &&
421 test_must_fail git apply patch &&
422 test_must_fail git apply --whitespace=fix patch &&
423 test_must_fail git apply --ignore-space-change --whitespace=fix patch
426 sed -e's/Z//' >one <<EOF
433 test_expect_success 'missing blank line should match context line with spaces' '
436 git diff -- one >patch &&
437 { echo a; echo b; echo c; } >one &&
439 { echo; echo d; } >>expect &&
442 git apply --whitespace=fix patch &&
446 sed -e's/Z//' >one <<EOF
453 test_expect_success 'same, but with the --ignore-space-option' '
457 git diff -- one >patch &&
458 { echo a; echo b; echo c; } >one &&
461 git checkout-index -f one &&
462 git apply --ignore-space-change --whitespace=fix patch &&
466 test_expect_success 'same, but with CR-LF line endings && cr-at-eol set' '
467 git config core.whitespace cr-at-eol &&
468 printf "a\r\n" >one &&
469 printf "b\r\n" >>one &&
470 printf "c\r\n" >>one &&
472 printf " \r\n" >>one &&
474 printf "d\r\n" >>one &&
476 git diff -- one >patch &&
479 git apply --ignore-space-change --whitespace=fix patch &&
483 test_expect_success 'CR-LF line endings && add line && text=auto' '
484 git config --unset core.whitespace &&
485 printf "a\r\n" >one &&
488 printf "b\r\n" >>one &&
490 git diff -- one >patch &&
492 echo "one text=auto" >.gitattributes &&
497 test_expect_success 'CR-LF line endings && change line && text=auto' '
498 printf "a\r\n" >one &&
501 printf "b\r\n" >one &&
503 git diff -- one >patch &&
505 echo "one text=auto" >.gitattributes &&
510 test_expect_success 'LF in repo, CRLF in worktree && change line && text=auto' '
513 printf "b\r\n" >one &&
514 git diff -- one >patch &&
515 printf "a\r\n" >one &&
516 echo "one text=auto" >.gitattributes &&
517 git -c core.eol=CRLF apply patch &&
518 printf "b\r\n" >expect &&
522 test_expect_success 'whitespace=fix to expand' '
523 qz_to_tab_space >preimage <<-\
EOF &&
532 qz_to_tab_space >patch <<-\EOF &&
533 diff --git a/preimage b/preimage
545 git -c core.whitespace=tab-in-indent apply --whitespace=fix patch
548 test_expect_success 'whitespace check skipped for excluded paths' '
549 git config core.whitespace blank-at-eol &&
552 git add used unused &&
554 echo "unused " >unused &&
555 git diff-files -p used unused >patch &&
556 git apply --include=used --stat --whitespace=error <patch