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
15 sed -e "s/_/ /g" -e "s/>/ /" <<-\EOF
16 An_SP in an ordinary line>and a HT.
19 _>_A SP, a HT and a SP (@%).
22 _______>Seven SP and a HT (@%).
23 ________>Eight SP and a HT (@#%).
24 _______>_Seven SP, a HT and a SP (@%).
25 ________>_Eight SP, a HT and a SP (@#%).
26 _______________Fifteen SP (#).
27 _______________>Fifteen SP and a HT (@#%).
28 ________________Sixteen SP (#).
29 ________________>Sixteen SP and a HT (@#%).
30 _____a__Five SP, a non WS, two SP.
31 A line with a (!) trailing SP_
32 A line with a (!) trailing HT>
38 sed -e "s|\([ab]\)/file|\1/target|" <patch |
44 apply_patch --whitespace=fix || return 1
47 diff file target | sed -n -e "s/^> //p" >fixed
49 # the changed lines are all expeced to change
50 fixed_cnt=$(wc -l <fixed)
52 '') expect_cnt=$fixed_cnt ;;
53 ?*) expect_cnt=$(grep "[$1]" <fixed | wc -l) ;;
55 test $fixed_cnt -eq $expect_cnt || return 1
57 # and we are not missing anything
60 ?*) expect_cnt=$(grep "[$1]" <file | wc -l) ;;
62 test $fixed_cnt -eq $expect_cnt || return 1
64 # Get the patch actually applied
65 git diff-files -p target >fixed-patch
66 test -s fixed-patch && return 0
68 # Make sure it is complaint-free
70 git apply --whitespace=error-all <fixed-patch
74 test_expect_success setup '
78 prepare_test_file >file &&
79 git diff-files -p >patch &&
85 test_expect_success 'whitespace=nowarn, default rule' '
87 apply_patch --whitespace=nowarn &&
92 test_expect_success 'whitespace=warn, default rule' '
94 apply_patch --whitespace=warn &&
99 test_expect_success 'whitespace=error-all, default rule' '
101 apply_patch --whitespace=error-all && return 1
102 test -s target && return 1
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
='#' ;; *) ti
= ;; 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 (attributes)" '
174 git config --unset core.whitespace &&
175 echo "target whitespace=$rule" >.gitattributes &&
176 test_fix "$tt$ts$ti$th"
185 sed -e "s/_/ /" <<-\EOF
186 diff --git a/target b/target
187 index e69de29..8bd6648 100644
191 +An empty line follows
193 +A line with trailing whitespace and no newline_
194 \ No newline at end of file
198 test_expect_success 'trailing whitespace & no newline at the end of file' '
200 create_patch >patch-file &&
201 git apply --whitespace=fix patch-file &&
202 grep "newline$" target &&
206 test_expect_success 'blank at EOF with --whitespace=fix (1)' '
207 : these can fail depending on what we did before
208 git config --unset core.whitespace
211 { echo a; echo b; echo c; } >one &&
213 { echo a; echo b; echo c; } >expect &&
214 { cat expect; echo; } >one &&
215 git diff -- one >patch &&
218 git apply --whitespace=fix patch &&
222 test_expect_success 'blank at EOF with --whitespace=fix (2)' '
223 { echo a; echo b; echo c; } >one &&
225 { echo a; echo c; } >expect &&
226 { cat expect; echo; echo; } >one &&
227 git diff -- one >patch &&
230 git apply --whitespace=fix patch &&
234 test_expect_success 'blank at EOF with --whitespace=fix (3)' '
235 { echo a; echo b; echo; } >one &&
237 { echo a; echo c; echo; } >expect &&
238 { cat expect; echo; echo; } >one &&
239 git diff -- one >patch &&
242 git apply --whitespace=fix patch &&
246 test_expect_success 'blank at end of hunk, not at EOF with --whitespace=fix' '
247 { echo a; echo b; echo; echo; echo; echo; echo; echo d; } >one &&
249 { echo a; echo c; echo; echo; echo; echo; echo; echo; echo d; } >expect &&
251 git diff -- one >patch &&
254 git apply --whitespace=fix patch &&
258 test_expect_success 'blank at EOF with --whitespace=warn' '
259 { echo a; echo b; echo c; } >one &&
263 git diff -- one >patch &&
266 git apply --whitespace=warn patch 2>error &&
267 test_cmp expect one &&
268 grep "new blank line at EOF" error
271 test_expect_success 'blank at EOF with --whitespace=error' '
272 { echo a; echo b; echo c; } >one &&
276 git diff -- one >patch &&
279 test_must_fail git apply --whitespace=error patch 2>error &&
280 test_cmp expect one &&
281 grep "new blank line at EOF" error
284 test_expect_success 'blank but not empty at EOF' '
285 { echo a; echo b; echo c; } >one &&
289 git diff -- one >patch &&
292 git apply --whitespace=warn patch 2>error &&
293 test_cmp expect one &&
294 grep "new blank line at EOF" error
297 test_expect_success 'applying beyond EOF requires one non-blank context line' '
298 { echo; echo; echo; echo; } >one &&
301 git diff -- one >patch &&
304 { echo a; echo; } >one &&
306 test_must_fail git apply --whitespace=fix patch &&
307 test_cmp one expect &&
308 test_must_fail git apply --ignore-space-change --whitespace=fix patch &&
312 test_expect_success 'tons of blanks at EOF should not apply' '
313 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do
314 echo; echo; echo; echo;
318 git diff -- one >patch &&
321 test_must_fail git apply --whitespace=fix patch &&
322 test_must_fail git apply --ignore-space-change --whitespace=fix patch
325 test_expect_success 'missing blank line at end with --whitespace=fix' '
331 git diff -- one >patch &&
334 test_must_fail git apply patch &&
335 git apply --whitespace=fix patch &&
336 test_cmp one expect &&
338 git apply --ignore-space-change --whitespace=fix patch &&
342 test_expect_success 'two missing blank lines at end with --whitespace=fix' '
343 { echo a; echo; echo b; echo c; } >one &&
344 cp one no-blank-lines &&
345 { echo; echo; } >>one &&
350 git diff -- one >patch &&
351 cp no-blank-lines one &&
352 test_must_fail git apply patch &&
353 git apply --whitespace=fix patch &&
354 test_cmp one expect &&
355 mv no-blank-lines one &&
356 test_must_fail git apply patch &&
357 git apply --ignore-space-change --whitespace=fix patch &&
361 test_expect_success 'shrink file with tons of missing blanks at end of file' '
362 { echo a; echo b; echo c; } >one &&
363 cp one no-blank-lines &&
364 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do
365 echo; echo; echo; echo;
370 git diff -- one >patch &&
371 cp no-blank-lines one &&
372 test_must_fail git apply patch &&
373 git apply --whitespace=fix patch &&
374 test_cmp one expect &&
375 mv no-blank-lines one &&
376 git apply --ignore-space-change --whitespace=fix patch &&
380 test_expect_success 'missing blanks at EOF must only match blank lines' '
381 { echo a; echo b; } >one &&
383 { echo c; echo d; } >>one &&
384 git diff -- one >patch &&
387 test_must_fail git apply patch
388 test_must_fail git apply --whitespace=fix patch &&
389 test_must_fail git apply --ignore-space-change --whitespace=fix patch
392 sed -e's/Z//' >one <<EOF
399 test_expect_success 'missing blank line should match context line with spaces' '
402 git diff -- one >patch &&
403 { echo a; echo b; echo c; } >one &&
405 { echo; echo d; } >>expect &&
408 git apply --whitespace=fix patch &&
412 sed -e's/Z//' >one <<EOF
419 test_expect_success 'same, but with the --ignore-space-option' '
423 git diff -- one >patch &&
424 { echo a; echo b; echo c; } >one &&
427 git checkout-index -f one &&
428 git apply --ignore-space-change --whitespace=fix patch &&
432 test_expect_success 'same, but with CR-LF line endings && cr-at-eol set' '
433 git config core.whitespace cr-at-eol &&
434 printf "a\r\n" >one &&
435 printf "b\r\n" >>one &&
436 printf "c\r\n" >>one &&
440 printf "d\r\n" >>one &&
442 git diff -- one >patch &&
445 git apply --ignore-space-change --whitespace=fix patch &&
449 test_expect_success 'same, but with CR-LF line endings && cr-at-eol unset' '
450 git config --unset core.whitespace &&
451 printf "a\r\n" >one &&
452 printf "b\r\n" >>one &&
453 printf "c\r\n" >>one &&
458 printf "d\r\n" >>one &&
459 git diff -- one >patch &&
463 git apply --ignore-space-change --whitespace=fix patch &&