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 apply_patch --whitespace=error-all && return 1
103 test -s target && return 1
108 test_expect_success 'whitespace=error-all, no rule' '
110 git config core.whitespace -trailing,-space-before,-indent &&
111 apply_patch --whitespace=error-all &&
116 test_expect_success 'whitespace=error-all, no rule (attribute)' '
118 git config --unset core.whitespace &&
119 echo "target -whitespace" >.gitattributes &&
120 apply_patch --whitespace=error-all &&
125 test_expect_success 'spaces inserted by tab-in-indent' '
127 git config core.whitespace -trailing,-space,-indent,tab &&
128 rm -f .gitattributes &&
130 sed -e "s/_/ /g" -e "s/>/ /" <<-\
EOF >expect &&
131 An_SP in an ordinary line>and a HT.
133 ________A SP and a HT (@%).
134 _________A SP, a HT and a SP (@%).
136 ________Eight SP (#).
137 ________Seven SP and a HT (@%).
138 ________________Eight SP and a HT (@#%).
139 _________Seven SP, a HT and a SP (@%).
140 _________________Eight SP, a HT and a SP (@#%).
141 _______________Fifteen SP (#).
142 ________________Fifteen SP and a HT (@#%).
143 ________________Sixteen SP (#=).
144 ________________________Sixteen SP and a HT (@#%=).
145 _____a__Five SP, a non WS, two SP.
146 A line with a (!) trailing SP_
147 A line with a (!) trailing HT>
149 test_cmp expect target
155 case "$t" in '') tt
='!' ;; *) tt
= ;; esac
158 case "$s" in '') ts
='@' ;; *) ts
= ;; esac
161 case "$i" in '') ti
='#' ti16
='=';; *) ti
= ti16
= ;; esac
164 [ -z "$h$i" ] && continue
165 case "$h" in '') th
='%' ;; *) th
= ;; esac
166 rule
=${t}trailing,${s}space,${i}indent,${h}tab
169 test_expect_success
"rule=$rule" '
170 git config core.whitespace "$rule" &&
171 test_fix "$tt$ts$ti$th"
174 test_expect_success
"rule=$rule,tabwidth=16" '
175 git config core.whitespace "$rule,tabwidth=16" &&
176 test_fix "$tt$ts$ti16$th"
179 test_expect_success
"rule=$rule (attributes)" '
180 git config --unset core.whitespace &&
181 echo "target whitespace=$rule" >.gitattributes &&
182 test_fix "$tt$ts$ti$th"
185 test_expect_success
"rule=$rule,tabwidth=16 (attributes)" '
186 echo "target whitespace=$rule,tabwidth=16" >.gitattributes &&
187 test_fix "$tt$ts$ti16$th"
196 sed -e "s/_/ /" <<-\EOF
197 diff --git a/target b/target
198 index e69de29..8bd6648 100644
202 +An empty line follows
204 +A line with trailing whitespace and no newline_
205 \ No newline at end of file
209 test_expect_success 'trailing whitespace & no newline at the end of file' '
211 create_patch >patch-file &&
212 git apply --whitespace=fix patch-file &&
213 grep "newline$" target &&
217 test_expect_success 'blank at EOF with --whitespace=fix (1)' '
218 test_might_fail git config --unset core.whitespace &&
219 rm -f .gitattributes &&
221 { echo a; echo b; echo c; } >one &&
223 { echo a; echo b; echo c; } >expect &&
224 { cat expect; echo; } >one &&
225 git diff -- one >patch &&
228 git apply --whitespace=fix patch &&
232 test_expect_success 'blank at EOF with --whitespace=fix (2)' '
233 { echo a; echo b; echo c; } >one &&
235 { echo a; echo c; } >expect &&
236 { cat expect; echo; echo; } >one &&
237 git diff -- one >patch &&
240 git apply --whitespace=fix patch &&
244 test_expect_success 'blank at EOF with --whitespace=fix (3)' '
245 { echo a; echo b; echo; } >one &&
247 { echo a; echo c; echo; } >expect &&
248 { cat expect; echo; echo; } >one &&
249 git diff -- one >patch &&
252 git apply --whitespace=fix patch &&
256 test_expect_success 'blank at end of hunk, not at EOF with --whitespace=fix' '
257 { echo a; echo b; echo; echo; echo; echo; echo; echo d; } >one &&
259 { echo a; echo c; echo; echo; echo; echo; echo; echo; echo d; } >expect &&
261 git diff -- one >patch &&
264 git apply --whitespace=fix patch &&
268 test_expect_success 'blank at EOF with --whitespace=warn' '
269 { echo a; echo b; echo c; } >one &&
273 git diff -- one >patch &&
276 git apply --whitespace=warn patch 2>error &&
277 test_cmp expect one &&
278 grep "new blank line at EOF" error
281 test_expect_success 'blank at EOF with --whitespace=error' '
282 { echo a; echo b; echo c; } >one &&
286 git diff -- one >patch &&
289 test_must_fail git apply --whitespace=error patch 2>error &&
290 test_cmp expect one &&
291 grep "new blank line at EOF" error
294 test_expect_success 'blank but not empty at EOF' '
295 { echo a; echo b; echo c; } >one &&
299 git diff -- one >patch &&
302 git apply --whitespace=warn patch 2>error &&
303 test_cmp expect one &&
304 grep "new blank line at EOF" error
307 test_expect_success 'applying beyond EOF requires one non-blank context line' '
308 { echo; echo; echo; echo; } >one &&
311 git diff -- one >patch &&
314 { echo a; echo; } >one &&
316 test_must_fail git apply --whitespace=fix patch &&
317 test_cmp one expect &&
318 test_must_fail git apply --ignore-space-change --whitespace=fix patch &&
322 test_expect_success 'tons of blanks at EOF should not apply' '
323 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do
324 echo; echo; echo; echo;
328 git diff -- one >patch &&
331 test_must_fail git apply --whitespace=fix patch &&
332 test_must_fail git apply --ignore-space-change --whitespace=fix patch
335 test_expect_success 'missing blank line at end with --whitespace=fix' '
341 git diff -- one >patch &&
344 test_must_fail git apply patch &&
345 git apply --whitespace=fix patch &&
346 test_cmp one expect &&
348 git apply --ignore-space-change --whitespace=fix patch &&
352 test_expect_success 'two missing blank lines at end with --whitespace=fix' '
353 { echo a; echo; echo b; echo c; } >one &&
354 cp one no-blank-lines &&
355 { echo; echo; } >>one &&
360 git diff -- one >patch &&
361 cp no-blank-lines one &&
362 test_must_fail git apply patch &&
363 git apply --whitespace=fix patch &&
364 test_cmp one expect &&
365 mv no-blank-lines one &&
366 test_must_fail git apply patch &&
367 git apply --ignore-space-change --whitespace=fix patch &&
371 test_expect_success 'missing blank line at end, insert before end, --whitespace=fix' '
372 { echo a; echo; } >one &&
374 { echo b; echo a; echo; } >one &&
376 git diff -- one >patch &&
378 test_must_fail git apply patch &&
379 git apply --whitespace=fix patch &&
383 test_expect_success 'shrink file with tons of missing blanks at end of file' '
384 { echo a; echo b; echo c; } >one &&
385 cp one no-blank-lines &&
386 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do
387 echo; echo; echo; echo;
392 git diff -- one >patch &&
393 cp no-blank-lines one &&
394 test_must_fail git apply patch &&
395 git apply --whitespace=fix patch &&
396 test_cmp one expect &&
397 mv no-blank-lines one &&
398 git apply --ignore-space-change --whitespace=fix patch &&
402 test_expect_success 'missing blanks at EOF must only match blank lines' '
403 { echo a; echo b; } >one &&
405 { echo c; echo d; } >>one &&
406 git diff -- one >patch &&
409 test_must_fail git apply patch &&
410 test_must_fail git apply --whitespace=fix patch &&
411 test_must_fail git apply --ignore-space-change --whitespace=fix patch
414 sed -e's/Z//' >one <<EOF
421 test_expect_success 'missing blank line should match context line with spaces' '
424 git diff -- one >patch &&
425 { echo a; echo b; echo c; } >one &&
427 { echo; echo d; } >>expect &&
430 git apply --whitespace=fix patch &&
434 sed -e's/Z//' >one <<EOF
441 test_expect_success 'same, but with the --ignore-space-option' '
445 git diff -- one >patch &&
446 { echo a; echo b; echo c; } >one &&
449 git checkout-index -f one &&
450 git apply --ignore-space-change --whitespace=fix patch &&
454 test_expect_success 'same, but with CR-LF line endings && cr-at-eol set' '
455 git config core.whitespace cr-at-eol &&
456 printf "a\r\n" >one &&
457 printf "b\r\n" >>one &&
458 printf "c\r\n" >>one &&
460 printf " \r\n" >>one &&
462 printf "d\r\n" >>one &&
464 git diff -- one >patch &&
467 git apply --ignore-space-change --whitespace=fix patch &&
471 test_expect_success 'same, but with CR-LF line endings && cr-at-eol unset' '
472 git config --unset core.whitespace &&
473 printf "a\r\n" >one &&
474 printf "b\r\n" >>one &&
475 printf "c\r\n" >>one &&
477 printf " \r\n" >>one &&
480 printf "d\r\n" >>one &&
481 git diff -- one >patch &&
485 git apply --ignore-space-change --whitespace=fix patch &&
489 test_expect_success 'whitespace=fix to expand' '
490 qz_to_tab_space >preimage <<-\
EOF &&
499 qz_to_tab_space >patch <<-\EOF &&
500 diff --git a/preimage b/preimage
512 git -c core.whitespace=tab-in-indent apply --whitespace=fix patch