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
56 # the changed lines are all expected to change
57 fixed_cnt=$(wc -l <fixed)
59 '') expect_cnt=$fixed_cnt ;;
60 ?*) expect_cnt=$(grep "[$1]" <fixed | wc -l) ;;
62 test $fixed_cnt -eq $expect_cnt || return 1
64 # and we are not missing anything
67 ?*) expect_cnt=$(grep "[$1]" <file | wc -l) ;;
69 test $fixed_cnt -eq $expect_cnt || return 1
71 # Get the patch actually applied
72 git diff-files -p target >fixed-patch
73 test -s fixed-patch && return 0
75 # Make sure it is complaint-free
77 git apply --whitespace=error-all <fixed-patch
81 test_expect_success setup '
85 prepare_test_file >file &&
86 git diff-files -p >patch &&
92 test_expect_success 'whitespace=nowarn, default rule' '
94 apply_patch --whitespace=nowarn &&
99 test_expect_success 'whitespace=warn, default rule' '
101 apply_patch --whitespace=warn &&
106 test_expect_success 'whitespace=error-all, default rule' '
108 apply_patch ! --whitespace=error-all &&
109 test_must_be_empty target
113 test_expect_success 'whitespace=error-all, no rule' '
115 git config core.whitespace -trailing,-space-before,-indent &&
116 apply_patch --whitespace=error-all &&
121 test_expect_success 'whitespace=error-all, no rule (attribute)' '
123 git config --unset core.whitespace &&
124 echo "target -whitespace" >.gitattributes &&
125 apply_patch --whitespace=error-all &&
130 test_expect_success 'spaces inserted by tab-in-indent' '
132 git config core.whitespace -trailing,-space,-indent,tab &&
133 rm -f .gitattributes &&
135 sed -e "s/_/ /g" -e "s/>/ /" <<-\
EOF >expect &&
136 An_SP in an ordinary line>and a HT.
138 ________A SP and a HT (@%).
139 _________A SP, a HT and a SP (@%).
141 ________Eight SP (#).
142 ________Seven SP and a HT (@%).
143 ________________Eight SP and a HT (@#%).
144 _________Seven SP, a HT and a SP (@%).
145 _________________Eight SP, a HT and a SP (@#%).
146 _______________Fifteen SP (#).
147 ________________Fifteen SP and a HT (@#%).
148 ________________Sixteen SP (#=).
149 ________________________Sixteen SP and a HT (@#%=).
150 _____a__Five SP, a non WS, two SP.
151 A line with a (!) trailing SP_
152 A line with a (!) trailing HT>
154 test_cmp expect target
160 case "$t" in '') tt
='!' ;; *) tt
= ;; esac
163 case "$s" in '') ts
='@' ;; *) ts
= ;; esac
166 case "$i" in '') ti
='#' ti16
='=';; *) ti
= ti16
= ;; esac
169 [ -z "$h$i" ] && continue
170 case "$h" in '') th
='%' ;; *) th
= ;; esac
171 rule
=${t}trailing,${s}space,${i}indent,${h}tab
174 test_expect_success
"rule=$rule" '
175 git config core.whitespace "$rule" &&
176 test_fix "$tt$ts$ti$th"
179 test_expect_success
"rule=$rule,tabwidth=16" '
180 git config core.whitespace "$rule,tabwidth=16" &&
181 test_fix "$tt$ts$ti16$th"
184 test_expect_success
"rule=$rule (attributes)" '
185 git config --unset core.whitespace &&
186 echo "target whitespace=$rule" >.gitattributes &&
187 test_fix "$tt$ts$ti$th"
190 test_expect_success
"rule=$rule,tabwidth=16 (attributes)" '
191 echo "target whitespace=$rule,tabwidth=16" >.gitattributes &&
192 test_fix "$tt$ts$ti16$th"
201 sed -e "s/_/ /" <<-\EOF
202 diff --git a/target b/target
203 index e69de29..8bd6648 100644
207 +An empty line follows
209 +A line with trailing whitespace and no newline_
210 \ No newline at end of file
214 test_expect_success 'trailing whitespace & no newline at the end of file' '
216 create_patch >patch-file &&
217 git apply --whitespace=fix patch-file &&
218 grep "newline$" target &&
222 test_expect_success 'blank at EOF with --whitespace=fix (1)' '
223 test_might_fail git config --unset core.whitespace &&
224 rm -f .gitattributes &&
226 { echo a; echo b; echo c; } >one &&
228 { echo a; echo b; echo c; } >expect &&
229 { cat expect; echo; } >one &&
230 git diff -- one >patch &&
233 git apply --whitespace=fix patch &&
237 test_expect_success 'blank at EOF with --whitespace=fix (2)' '
238 { echo a; echo b; echo c; } >one &&
240 { echo a; echo c; } >expect &&
241 { cat expect; echo; echo; } >one &&
242 git diff -- one >patch &&
245 git apply --whitespace=fix patch &&
249 test_expect_success 'blank at EOF with --whitespace=fix (3)' '
250 { echo a; echo b; echo; } >one &&
252 { echo a; echo c; echo; } >expect &&
253 { cat expect; echo; echo; } >one &&
254 git diff -- one >patch &&
257 git apply --whitespace=fix patch &&
261 test_expect_success 'blank at end of hunk, not at EOF with --whitespace=fix' '
262 { echo a; echo b; echo; echo; echo; echo; echo; echo d; } >one &&
264 { echo a; echo c; echo; echo; echo; echo; echo; echo; echo d; } >expect &&
266 git diff -- one >patch &&
269 git apply --whitespace=fix patch &&
273 test_expect_success 'blank at EOF with --whitespace=warn' '
274 { echo a; echo b; echo c; } >one &&
278 git diff -- one >patch &&
281 git apply --whitespace=warn patch 2>error &&
282 test_cmp expect one &&
283 grep "new blank line at EOF" error
286 test_expect_success 'blank at EOF with --whitespace=error' '
287 { echo a; echo b; echo c; } >one &&
291 git diff -- one >patch &&
294 test_must_fail git apply --whitespace=error patch 2>error &&
295 test_cmp expect one &&
296 grep "new blank line at EOF" error
299 test_expect_success 'blank but not empty at EOF' '
300 { echo a; echo b; echo c; } >one &&
304 git diff -- one >patch &&
307 git apply --whitespace=warn patch 2>error &&
308 test_cmp expect one &&
309 grep "new blank line at EOF" error
312 test_expect_success 'applying beyond EOF requires one non-blank context line' '
313 { echo; echo; echo; echo; } >one &&
316 git diff -- one >patch &&
319 { echo a; echo; } >one &&
321 test_must_fail git apply --whitespace=fix patch &&
322 test_cmp expect one &&
323 test_must_fail git apply --ignore-space-change --whitespace=fix patch &&
327 test_expect_success 'tons of blanks at EOF should not apply' '
328 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do
329 echo; echo; echo; echo;
333 git diff -- one >patch &&
336 test_must_fail git apply --whitespace=fix patch &&
337 test_must_fail git apply --ignore-space-change --whitespace=fix patch
340 test_expect_success 'missing blank line at end with --whitespace=fix' '
346 git diff -- one >patch &&
349 test_must_fail git apply patch &&
350 git apply --whitespace=fix patch &&
351 test_cmp expect one &&
353 git apply --ignore-space-change --whitespace=fix patch &&
357 test_expect_success 'two missing blank lines at end with --whitespace=fix' '
358 { echo a; echo; echo b; echo c; } >one &&
359 cp one no-blank-lines &&
360 { echo; echo; } >>one &&
365 git diff -- one >patch &&
366 cp no-blank-lines one &&
367 test_must_fail git apply patch &&
368 git apply --whitespace=fix patch &&
369 test_cmp expect one &&
370 mv no-blank-lines one &&
371 test_must_fail git apply patch &&
372 git apply --ignore-space-change --whitespace=fix patch &&
376 test_expect_success 'missing blank line at end, insert before end, --whitespace=fix' '
377 { echo a; echo; } >one &&
379 { echo b; echo a; echo; } >one &&
381 git diff -- one >patch &&
383 test_must_fail git apply patch &&
384 git apply --whitespace=fix patch &&
388 test_expect_success 'shrink file with tons of missing blanks at end of file' '
389 { echo a; echo b; echo c; } >one &&
390 cp one no-blank-lines &&
391 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do
392 echo; echo; echo; echo;
397 git diff -- one >patch &&
398 cp no-blank-lines one &&
399 test_must_fail git apply patch &&
400 git apply --whitespace=fix patch &&
401 test_cmp expect one &&
402 mv no-blank-lines one &&
403 git apply --ignore-space-change --whitespace=fix patch &&
407 test_expect_success 'missing blanks at EOF must only match blank lines' '
408 { echo a; echo b; } >one &&
410 { echo c; echo d; } >>one &&
411 git diff -- one >patch &&
414 test_must_fail git apply patch &&
415 test_must_fail git apply --whitespace=fix patch &&
416 test_must_fail git apply --ignore-space-change --whitespace=fix patch
419 sed -e's/Z//' >one <<EOF
426 test_expect_success 'missing blank line should match context line with spaces' '
429 git diff -- one >patch &&
430 { echo a; echo b; echo c; } >one &&
432 { echo; echo d; } >>expect &&
435 git apply --whitespace=fix patch &&
439 sed -e's/Z//' >one <<EOF
446 test_expect_success 'same, but with the --ignore-space-option' '
450 git diff -- one >patch &&
451 { echo a; echo b; echo c; } >one &&
454 git checkout-index -f one &&
455 git apply --ignore-space-change --whitespace=fix patch &&
459 test_expect_success 'same, but with CR-LF line endings && cr-at-eol set' '
460 git config core.whitespace cr-at-eol &&
461 printf "a\r\n" >one &&
462 printf "b\r\n" >>one &&
463 printf "c\r\n" >>one &&
465 printf " \r\n" >>one &&
467 printf "d\r\n" >>one &&
469 git diff -- one >patch &&
472 git apply --ignore-space-change --whitespace=fix patch &&
476 test_expect_success 'CR-LF line endings && add line && text=auto' '
477 git config --unset core.whitespace &&
478 printf "a\r\n" >one &&
481 printf "b\r\n" >>one &&
483 git diff -- one >patch &&
485 echo "one text=auto" >.gitattributes &&
490 test_expect_success 'CR-LF line endings && change line && text=auto' '
491 printf "a\r\n" >one &&
494 printf "b\r\n" >one &&
496 git diff -- one >patch &&
498 echo "one text=auto" >.gitattributes &&
503 test_expect_success 'LF in repo, CRLF in worktree && change line && text=auto' '
506 printf "b\r\n" >one &&
507 git diff -- one >patch &&
508 printf "a\r\n" >one &&
509 echo "one text=auto" >.gitattributes &&
510 git -c core.eol=CRLF apply patch &&
511 printf "b\r\n" >expect &&
515 test_expect_success 'whitespace=fix to expand' '
516 qz_to_tab_space >preimage <<-\
EOF &&
525 qz_to_tab_space >patch <<-\EOF &&
526 diff --git a/preimage b/preimage
538 git -c core.whitespace=tab-in-indent apply --whitespace=fix patch
541 test_expect_success 'whitespace check skipped for excluded paths' '
542 git config core.whitespace blank-at-eol &&
545 git add used unused &&
547 echo "unused " >unused &&
548 git diff-files -p used unused >patch &&
549 git apply --include=used --stat --whitespace=error <patch