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 &&
126 case "$t" in '') tt='!' ;; *) tt= ;; esac
129 case "$s" in '') ts='@' ;; *) ts= ;; esac
132 case "$i" in '') ti='#' ;; *) ti= ;; esac
135 [ -z "$h$i" ] && continue
136 case "$h" in '') th='%' ;; *) th= ;; esac
137 rule=${t}trailing,${s}space,${i}indent,${h}tab
140 test_expect_success "rule=$rule" '
141 git config core.whitespace "$rule" &&
142 test_fix "$tt$ts$ti$th"
145 test_expect_success "rule=$rule (attributes)" '
146 git config --unset core.whitespace &&
147 echo "target whitespace=$rule" >.gitattributes &&
148 test_fix "$tt$ts$ti$th"
157 sed -e "s/_/ /" <<-\EOF
158 diff --git a/target b/target
159 index e69de29..8bd6648 100644
163 +An empty line follows
165 +A line with trailing whitespace and no newline_
166 \ No newline at end of file
170 test_expect_success 'trailing whitespace & no newline at the end of file' '
172 create_patch >patch-file &&
173 git apply --whitespace=fix patch-file &&
174 grep "newline$" target &&
178 test_expect_success 'blank at EOF with --whitespace=fix (1)' '
179 : these can fail depending on what we did before
180 git config --unset core.whitespace
183 { echo a; echo b; echo c; } >one &&
185 { echo a; echo b; echo c; } >expect &&
186 { cat expect; echo; } >one &&
187 git diff -- one >patch &&
190 git apply --whitespace=fix patch &&
194 test_expect_success 'blank at EOF with --whitespace=fix (2)' '
195 { echo a; echo b; echo c; } >one &&
197 { echo a; echo c; } >expect &&
198 { cat expect; echo; echo; } >one &&
199 git diff -- one >patch &&
202 git apply --whitespace=fix patch &&
206 test_expect_success 'blank at EOF with --whitespace=fix (3)' '
207 { echo a; echo b; echo; } >one &&
209 { echo a; echo c; echo; } >expect &&
210 { cat expect; echo; echo; } >one &&
211 git diff -- one >patch &&
214 git apply --whitespace=fix patch &&
218 test_expect_success 'blank at end of hunk, not at EOF with --whitespace=fix' '
219 { echo a; echo b; echo; echo; echo; echo; echo; echo d; } >one &&
221 { echo a; echo c; echo; echo; echo; echo; echo; echo; echo d; } >expect &&
223 git diff -- one >patch &&
226 git apply --whitespace=fix patch &&
230 test_expect_success 'blank at EOF with --whitespace=warn' '
231 { echo a; echo b; echo c; } >one &&
235 git diff -- one >patch &&
238 git apply --whitespace=warn patch 2>error &&
239 test_cmp expect one &&
240 grep "new blank line at EOF" error
243 test_expect_success 'blank at EOF with --whitespace=error' '
244 { echo a; echo b; echo c; } >one &&
248 git diff -- one >patch &&
251 test_must_fail git apply --whitespace=error patch 2>error &&
252 test_cmp expect one &&
253 grep "new blank line at EOF" error
256 test_expect_success 'blank but not empty at EOF' '
257 { echo a; echo b; echo c; } >one &&
261 git diff -- one >patch &&
264 git apply --whitespace=warn patch 2>error &&
265 test_cmp expect one &&
266 grep "new blank line at EOF" error
269 test_expect_success 'applying beyond EOF requires one non-blank context line' '
270 { echo; echo; echo; echo; } >one &&
273 git diff -- one >patch &&
276 { echo a; echo; } >one &&
278 test_must_fail git apply --whitespace=fix patch &&
279 test_cmp one expect &&
280 test_must_fail git apply --ignore-space-change --whitespace=fix patch &&
284 test_expect_success 'tons of blanks at EOF should not apply' '
285 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do
286 echo; echo; echo; echo;
290 git diff -- one >patch &&
293 test_must_fail git apply --whitespace=fix patch &&
294 test_must_fail git apply --ignore-space-change --whitespace=fix patch
297 test_expect_success 'missing blank line at end with --whitespace=fix' '
303 git diff -- one >patch &&
306 test_must_fail git apply patch &&
307 git apply --whitespace=fix patch &&
308 test_cmp one expect &&
310 git apply --ignore-space-change --whitespace=fix patch &&
314 test_expect_success 'two missing blank lines at end with --whitespace=fix' '
315 { echo a; echo; echo b; echo c; } >one &&
316 cp one no-blank-lines &&
317 { echo; echo; } >>one &&
322 git diff -- one >patch &&
323 cp no-blank-lines one &&
324 test_must_fail git apply patch &&
325 git apply --whitespace=fix patch &&
326 test_cmp one expect &&
327 mv no-blank-lines one &&
328 test_must_fail git apply patch &&
329 git apply --ignore-space-change --whitespace=fix patch &&
333 test_expect_success 'missing blank line at end, insert before end, --whitespace=fix' '
334 { echo a; echo; } >one &&
336 { echo b; echo a; echo; } >one &&
338 git diff -- one >patch &&
340 test_must_fail git apply patch &&
341 git apply --whitespace=fix patch &&
345 test_expect_success 'shrink file with tons of missing blanks at end of file' '
346 { echo a; echo b; echo c; } >one &&
347 cp one no-blank-lines &&
348 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do
349 echo; echo; echo; echo;
354 git diff -- one >patch &&
355 cp no-blank-lines one &&
356 test_must_fail git apply patch &&
357 git apply --whitespace=fix patch &&
358 test_cmp one expect &&
359 mv no-blank-lines one &&
360 git apply --ignore-space-change --whitespace=fix patch &&
364 test_expect_success 'missing blanks at EOF must only match blank lines' '
365 { echo a; echo b; } >one &&
367 { echo c; echo d; } >>one &&
368 git diff -- one >patch &&
371 test_must_fail git apply patch
372 test_must_fail git apply --whitespace=fix patch &&
373 test_must_fail git apply --ignore-space-change --whitespace=fix patch
376 sed -e's/Z//' >one <<EOF
383 test_expect_success 'missing blank line should match context line with spaces' '
386 git diff -- one >patch &&
387 { echo a; echo b; echo c; } >one &&
389 { echo; echo d; } >>expect &&
392 git apply --whitespace=fix patch &&
396 sed -e's/Z//' >one <<EOF
403 test_expect_success 'same, but with the --ignore-space-option' '
407 git diff -- one >patch &&
408 { echo a; echo b; echo c; } >one &&
411 git checkout-index -f one &&
412 git apply --ignore-space-change --whitespace=fix patch &&
416 test_expect_success 'same, but with CR-LF line endings && cr-at-eol set' '
417 git config core.whitespace cr-at-eol &&
418 printf "a\r\n" >one &&
419 printf "b\r\n" >>one &&
420 printf "c\r\n" >>one &&
424 printf "d\r\n" >>one &&
426 git diff -- one >patch &&
429 git apply --ignore-space-change --whitespace=fix patch &&
433 test_expect_success 'same, but with CR-LF line endings && cr-at-eol unset' '
434 git config --unset core.whitespace &&
435 printf "a\r\n" >one &&
436 printf "b\r\n" >>one &&
437 printf "c\r\n" >>one &&
442 printf "d\r\n" >>one &&
443 git diff -- one >patch &&
447 git apply --ignore-space-change --whitespace=fix patch &&