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
14 sed -e "s/_/ /g" -e "s/>/ /" <<-\EOF
15 An_SP in an ordinary line>and a HT.
18 _>_A SP, a HT and a SP (@).
21 _______>Seven SP and a HT (@).
22 ________>Eight SP and a HT (@#).
23 _______>_Seven SP, a HT and a SP (@).
24 ________>_Eight SP, a HT and a SP (@#).
25 _______________Fifteen SP (#).
26 _______________>Fifteen SP and a HT (@#).
27 ________________Sixteen SP (#).
28 ________________>Sixteen SP and a HT (@#).
29 _____a__Five SP, a non WS, two SP.
30 A line with a (!) trailing SP_
31 A line with a (!) trailing HT>
37 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
133 rule=${t}trailing,${s}space,${i}indent
136 test_expect_success "rule=$rule" '
137 git config core.whitespace "$rule" &&
141 test_expect_success "rule=$rule (attributes)" '
142 git config --unset core.whitespace &&
143 echo "target whitespace=$rule" >.gitattributes &&
152 sed -e "s/_/ /" <<-\EOF
153 diff --git a/target b/target
154 index e69de29..8bd6648 100644
158 +An empty line follows
160 +A line with trailing whitespace and no newline_
161 \ No newline at end of file
165 test_expect_success 'trailing whitespace & no newline at the end of file' '
167 create_patch >patch-file &&
168 git apply --whitespace=fix patch-file &&
169 grep "newline$" target &&
173 test_expect_success 'blank at EOF with --whitespace=fix (1)' '
174 : these can fail depending on what we did before
175 git config --unset core.whitespace
178 { echo a; echo b; echo c; } >one &&
180 { echo a; echo b; echo c; } >expect &&
181 { cat expect; echo; } >one &&
182 git diff -- one >patch &&
185 git apply --whitespace=fix patch &&
189 test_expect_success 'blank at EOF with --whitespace=fix (2)' '
190 { echo a; echo b; echo c; } >one &&
192 { echo a; echo c; } >expect &&
193 { cat expect; echo; echo; } >one &&
194 git diff -- one >patch &&
197 git apply --whitespace=fix patch &&
201 test_expect_success 'blank at EOF with --whitespace=fix (3)' '
202 { echo a; echo b; echo; } >one &&
204 { echo a; echo c; echo; } >expect &&
205 { cat expect; echo; echo; } >one &&
206 git diff -- one >patch &&
209 git apply --whitespace=fix patch &&
213 test_expect_success 'blank at end of hunk, not at EOF with --whitespace=fix' '
214 { echo a; echo b; echo; echo; echo; echo; echo; echo d; } >one &&
216 { echo a; echo c; echo; echo; echo; echo; echo; echo; echo d; } >expect &&
218 git diff -- one >patch &&
221 git apply --whitespace=fix patch &&
225 test_expect_success 'blank at EOF with --whitespace=warn' '
226 { echo a; echo b; echo c; } >one &&
230 git diff -- one >patch &&
233 git apply --whitespace=warn patch 2>error &&
234 test_cmp expect one &&
235 grep "new blank line at EOF" error
238 test_expect_success 'blank at EOF with --whitespace=error' '
239 { echo a; echo b; echo c; } >one &&
243 git diff -- one >patch &&
246 test_must_fail git apply --whitespace=error patch 2>error &&
247 test_cmp expect one &&
248 grep "new blank line at EOF" error
251 test_expect_success 'blank but not empty at EOF' '
252 { echo a; echo b; echo c; } >one &&
256 git diff -- one >patch &&
259 git apply --whitespace=warn patch 2>error &&
260 test_cmp expect one &&
261 grep "new blank line at EOF" error