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>
39 sed -e "s|\([ab]\)/file|\1/target|" <patch |
45 apply_patch --whitespace=fix || return 1
48 $DIFF file target | sed -n -e "s/^> //p" >fixed
50 # the changed lines are all expected to change
51 fixed_cnt=$(wc -l <fixed)
53 '') expect_cnt=$fixed_cnt ;;
54 ?*) expect_cnt=$(grep "[$1]" <fixed | wc -l) ;;
56 test $fixed_cnt -eq $expect_cnt || return 1
58 # and we are not missing anything
61 ?*) expect_cnt=$(grep "[$1]" <file | wc -l) ;;
63 test $fixed_cnt -eq $expect_cnt || return 1
65 # Get the patch actually applied
66 git diff-files -p target >fixed-patch
67 test -s fixed-patch && return 0
69 # Make sure it is complaint-free
71 git apply --whitespace=error-all <fixed-patch
75 test_expect_success setup '
79 prepare_test_file >file &&
80 git diff-files -p >patch &&
86 test_expect_success 'whitespace=nowarn, default rule' '
88 apply_patch --whitespace=nowarn &&
93 test_expect_success 'whitespace=warn, default rule' '
95 apply_patch --whitespace=warn &&
100 test_expect_success 'whitespace=error-all, default rule' '
102 test_must_fail apply_patch --whitespace=error-all &&
107 test_expect_success 'whitespace=error-all, no rule' '
109 git config core.whitespace -trailing,-space-before,-indent &&
110 apply_patch --whitespace=error-all &&
115 test_expect_success 'whitespace=error-all, no rule (attribute)' '
117 git config --unset core.whitespace &&
118 echo "target -whitespace" >.gitattributes &&
119 apply_patch --whitespace=error-all &&
124 test_expect_success 'spaces inserted by tab-in-indent' '
126 git config core.whitespace -trailing,-space,-indent,tab &&
127 rm -f .gitattributes &&
129 sed -e "s/_/ /g" -e "s/>/ /" <<-\EOF >expect &&
130 An_SP in an ordinary line>and a HT.
132 ________A SP and a HT (@%).
133 _________A SP, a HT and a 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 (@#%).
142 ________________Sixteen SP (#=).
143 ________________________Sixteen SP and a HT (@#%=).
144 _____a__Five SP, a non WS, two SP.
145 A line with a (!) trailing SP_
146 A line with a (!) trailing HT>
148 test_cmp expect target
154 case "$t" in '') tt='!' ;; *) tt= ;; esac
157 case "$s" in '') ts='@' ;; *) ts= ;; esac
160 case "$i" in '') ti='#' ti16='=';; *) ti= ti16= ;; esac
163 [ -z "$h$i" ] && continue
164 case "$h" in '') th='%' ;; *) th= ;; esac
165 rule=${t}trailing,${s}space,${i}indent,${h}tab
168 test_expect_success "rule=$rule" '
169 git config core.whitespace "$rule" &&
170 test_fix "$tt$ts$ti$th"
173 test_expect_success "rule=$rule,tabwidth=16" '
174 git config core.whitespace "$rule,tabwidth=16" &&
175 test_fix "$tt$ts$ti16$th"
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"
184 test_expect_success "rule=$rule,tabwidth=16 (attributes)" '
185 echo "target whitespace=$rule,tabwidth=16" >.gitattributes &&
186 test_fix "$tt$ts$ti16$th"
195 sed -e "s/_/ /" <<-\EOF
196 diff --git a/target b/target
197 index e69de29..8bd6648 100644
201 +An empty line follows
203 +A line with trailing whitespace and no newline_
204 \ No newline at end of file
208 test_expect_success 'trailing whitespace & no newline at the end of file' '
210 create_patch >patch-file &&
211 git apply --whitespace=fix patch-file &&
212 grep "newline$" target &&
216 test_expect_success 'blank at EOF with --whitespace=fix (1)' '
217 test_might_fail git config --unset core.whitespace &&
218 rm -f .gitattributes &&
220 { echo a; echo b; echo c; } >one &&
222 { echo a; echo b; echo c; } >expect &&
223 { cat expect; echo; } >one &&
224 git diff -- one >patch &&
227 git apply --whitespace=fix patch &&
231 test_expect_success 'blank at EOF with --whitespace=fix (2)' '
232 { echo a; echo b; echo c; } >one &&
234 { echo a; echo c; } >expect &&
235 { cat expect; echo; echo; } >one &&
236 git diff -- one >patch &&
239 git apply --whitespace=fix patch &&
243 test_expect_success 'blank at EOF with --whitespace=fix (3)' '
244 { echo a; echo b; echo; } >one &&
246 { echo a; echo c; echo; } >expect &&
247 { cat expect; echo; echo; } >one &&
248 git diff -- one >patch &&
251 git apply --whitespace=fix patch &&
255 test_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 &&
258 { echo a; echo c; echo; echo; echo; echo; echo; echo; echo d; } >expect &&
260 git diff -- one >patch &&
263 git apply --whitespace=fix patch &&
267 test_expect_success 'blank at EOF with --whitespace=warn' '
268 { echo a; echo b; echo c; } >one &&
272 git diff -- one >patch &&
275 git apply --whitespace=warn patch 2>error &&
276 test_cmp expect one &&
277 grep "new blank line at EOF" error
280 test_expect_success 'blank at EOF with --whitespace=error' '
281 { echo a; echo b; echo c; } >one &&
285 git diff -- one >patch &&
288 test_must_fail git apply --whitespace=error patch 2>error &&
289 test_cmp expect one &&
290 grep "new blank line at EOF" error
293 test_expect_success 'blank but not empty at EOF' '
294 { echo a; echo b; echo c; } >one &&
298 git diff -- one >patch &&
301 git apply --whitespace=warn patch 2>error &&
302 test_cmp expect one &&
303 grep "new blank line at EOF" error
306 test_expect_success 'applying beyond EOF requires one non-blank context line' '
307 { echo; echo; echo; echo; } >one &&
310 git diff -- one >patch &&
313 { echo a; echo; } >one &&
315 test_must_fail git apply --whitespace=fix patch &&
316 test_cmp one expect &&
317 test_must_fail git apply --ignore-space-change --whitespace=fix patch &&
321 test_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;
327 git diff -- one >patch &&
330 test_must_fail git apply --whitespace=fix patch &&
331 test_must_fail git apply --ignore-space-change --whitespace=fix patch
334 test_expect_success 'missing blank line at end with --whitespace=fix' '
340 git diff -- one >patch &&
343 test_must_fail git apply patch &&
344 git apply --whitespace=fix patch &&
345 test_cmp one expect &&
347 git apply --ignore-space-change --whitespace=fix patch &&
351 test_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 &&
359 git diff -- one >patch &&
360 cp no-blank-lines one &&
361 test_must_fail git apply patch &&
362 git apply --whitespace=fix patch &&
363 test_cmp one expect &&
364 mv no-blank-lines one &&
365 test_must_fail git apply patch &&
366 git apply --ignore-space-change --whitespace=fix patch &&
370 test_expect_success 'missing blank line at end, insert before end, --whitespace=fix' '
371 { echo a; echo; } >one &&
373 { echo b; echo a; echo; } >one &&
375 git diff -- one >patch &&
377 test_must_fail git apply patch &&
378 git apply --whitespace=fix patch &&
382 test_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;
391 git diff -- one >patch &&
392 cp no-blank-lines one &&
393 test_must_fail git apply patch &&
394 git apply --whitespace=fix patch &&
395 test_cmp one expect &&
396 mv no-blank-lines one &&
397 git apply --ignore-space-change --whitespace=fix patch &&
401 test_expect_success 'missing blanks at EOF must only match blank lines' '
402 { echo a; echo b; } >one &&
404 { echo c; echo d; } >>one &&
405 git diff -- one >patch &&
408 test_must_fail git apply patch &&
409 test_must_fail git apply --whitespace=fix patch &&
410 test_must_fail git apply --ignore-space-change --whitespace=fix patch
413 sed -e's/Z//' >one <<EOF
420 test_expect_success 'missing blank line should match context line with spaces' '
423 git diff -- one >patch &&
424 { echo a; echo b; echo c; } >one &&
426 { echo; echo d; } >>expect &&
429 git apply --whitespace=fix patch &&
433 sed -e's/Z//' >one <<EOF
440 test_expect_success 'same, but with the --ignore-space-option' '
444 git diff -- one >patch &&
445 { echo a; echo b; echo c; } >one &&
448 git checkout-index -f one &&
449 git apply --ignore-space-change --whitespace=fix patch &&
453 test_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 &&
459 printf " \r\n" >>one &&
461 printf "d\r\n" >>one &&
463 git diff -- one >patch &&
466 git apply --ignore-space-change --whitespace=fix patch &&
470 test_expect_success 'same, but with CR-LF line endings && cr-at-eol unset' '
471 git config --unset core.whitespace &&
472 printf "a\r\n" >one &&
473 printf "b\r\n" >>one &&
474 printf "c\r\n" >>one &&
476 printf " \r\n" >>one &&
479 printf "d\r\n" >>one &&
480 git diff -- one >patch &&
484 git apply --ignore-space-change --whitespace=fix patch &&
488 test_expect_success 'whitespace=fix to expand' '
489 qz_to_tab_space >preimage <<-\EOF &&
498 qz_to_tab_space >patch <<-\EOF &&
499 diff --git a/preimage b/preimage
511 git -c core.whitespace=tab-in-indent apply --whitespace=fix patch
514 test_expect_success 'whitespace check skipped for excluded paths' '
515 git config core.whitespace blank-at-eol &&
518 git add used unused &&
520 echo "unused " >unused &&
521 git diff-files -p used unused >patch &&
522 git apply --include=used --stat --whitespace=error <patch