Junio C Hamano | d5a4164 | 2007-11-23 20:14:20 -0800 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Nanako Shiraishi | 3604e7c | 2008-09-03 17:59:29 +0900 | [diff] [blame] | 3 | test_description='core.whitespace rules and git apply' |
Junio C Hamano | d5a4164 | 2007-11-23 20:14:20 -0800 | [diff] [blame] | 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
| 7 | prepare_test_file () { |
| 8 | |
| 9 | # A line that has character X is touched iff RULE is in effect: |
| 10 | # X RULE |
| 11 | # ! trailing-space |
| 12 | # @ space-before-tab |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 13 | # # indent-with-non-tab (default tab width 8) |
| 14 | # = indent-with-non-tab,tabwidth=16 |
Chris Webb | a347b17 | 2010-04-03 00:37:37 +0100 | [diff] [blame] | 15 | # % tab-in-indent |
Junio C Hamano | d5a4164 | 2007-11-23 20:14:20 -0800 | [diff] [blame] | 16 | sed -e "s/_/ /g" -e "s/>/ /" <<-\EOF |
| 17 | An_SP in an ordinary line>and a HT. |
Chris Webb | a347b17 | 2010-04-03 00:37:37 +0100 | [diff] [blame] | 18 | >A HT (%). |
| 19 | _>A SP and a HT (@%). |
| 20 | _>_A SP, a HT and a SP (@%). |
Junio C Hamano | d5a4164 | 2007-11-23 20:14:20 -0800 | [diff] [blame] | 21 | _______Seven SP. |
| 22 | ________Eight SP (#). |
Chris Webb | a347b17 | 2010-04-03 00:37:37 +0100 | [diff] [blame] | 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 (@#%). |
Junio C Hamano | d5a4164 | 2007-11-23 20:14:20 -0800 | [diff] [blame] | 27 | _______________Fifteen SP (#). |
Chris Webb | a347b17 | 2010-04-03 00:37:37 +0100 | [diff] [blame] | 28 | _______________>Fifteen SP and a HT (@#%). |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 29 | ________________Sixteen SP (#=). |
| 30 | ________________>Sixteen SP and a HT (@#%=). |
Junio C Hamano | d5a4164 | 2007-11-23 20:14:20 -0800 | [diff] [blame] | 31 | _____a__Five SP, a non WS, two SP. |
| 32 | A line with a (!) trailing SP_ |
| 33 | A line with a (!) trailing HT> |
| 34 | EOF |
| 35 | } |
| 36 | |
| 37 | apply_patch () { |
Denton Liu | 37a63fa | 2020-01-06 23:53:13 -0500 | [diff] [blame] | 38 | cmd_prefix= && |
| 39 | if test "x$1" = 'x!' |
| 40 | then |
| 41 | cmd_prefix=test_must_fail && |
| 42 | shift |
| 43 | fi && |
Junio C Hamano | d5a4164 | 2007-11-23 20:14:20 -0800 | [diff] [blame] | 44 | >target && |
| 45 | sed -e "s|\([ab]\)/file|\1/target|" <patch | |
Denton Liu | 37a63fa | 2020-01-06 23:53:13 -0500 | [diff] [blame] | 46 | $cmd_prefix git apply "$@" |
Junio C Hamano | d5a4164 | 2007-11-23 20:14:20 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | test_fix () { |
Junio C Hamano | d5a4164 | 2007-11-23 20:14:20 -0800 | [diff] [blame] | 50 | # fix should not barf |
| 51 | apply_patch --whitespace=fix || return 1 |
| 52 | |
| 53 | # find touched lines |
Gary V. Vaughan | 4fdf71b | 2010-05-14 09:31:37 +0000 | [diff] [blame] | 54 | $DIFF file target | sed -n -e "s/^> //p" >fixed |
Đoàn Trần Công Danh | f73533a | 2020-03-26 11:37:37 +0700 | [diff] [blame] | 55 | # busybox's diff(1) doesn't output normal format |
| 56 | if ! test -s fixed |
| 57 | then |
| 58 | $DIFF -u file target | |
| 59 | grep -v '^+++ target' | |
| 60 | sed -ne "/^+/s/+//p" >fixed |
| 61 | fi |
Junio C Hamano | d5a4164 | 2007-11-23 20:14:20 -0800 | [diff] [blame] | 62 | |
Stefano Lattarini | 41ccfdd | 2013-04-12 00:36:10 +0200 | [diff] [blame] | 63 | # the changed lines are all expected to change |
Junio C Hamano | d5a4164 | 2007-11-23 20:14:20 -0800 | [diff] [blame] | 64 | fixed_cnt=$(wc -l <fixed) |
| 65 | case "$1" in |
| 66 | '') expect_cnt=$fixed_cnt ;; |
| 67 | ?*) expect_cnt=$(grep "[$1]" <fixed | wc -l) ;; |
| 68 | esac |
| 69 | test $fixed_cnt -eq $expect_cnt || return 1 |
| 70 | |
| 71 | # and we are not missing anything |
| 72 | case "$1" in |
| 73 | '') expect_cnt=0 ;; |
| 74 | ?*) expect_cnt=$(grep "[$1]" <file | wc -l) ;; |
| 75 | esac |
| 76 | test $fixed_cnt -eq $expect_cnt || return 1 |
| 77 | |
| 78 | # Get the patch actually applied |
| 79 | git diff-files -p target >fixed-patch |
| 80 | test -s fixed-patch && return 0 |
| 81 | |
| 82 | # Make sure it is complaint-free |
| 83 | >target |
| 84 | git apply --whitespace=error-all <fixed-patch |
| 85 | |
| 86 | } |
| 87 | |
| 88 | test_expect_success setup ' |
| 89 | |
| 90 | >file && |
| 91 | git add file && |
| 92 | prepare_test_file >file && |
| 93 | git diff-files -p >patch && |
| 94 | >target && |
| 95 | git add target |
| 96 | |
| 97 | ' |
| 98 | |
| 99 | test_expect_success 'whitespace=nowarn, default rule' ' |
| 100 | |
| 101 | apply_patch --whitespace=nowarn && |
Gary V. Vaughan | 4fdf71b | 2010-05-14 09:31:37 +0000 | [diff] [blame] | 102 | test_cmp file target |
Junio C Hamano | d5a4164 | 2007-11-23 20:14:20 -0800 | [diff] [blame] | 103 | |
| 104 | ' |
| 105 | |
| 106 | test_expect_success 'whitespace=warn, default rule' ' |
| 107 | |
| 108 | apply_patch --whitespace=warn && |
Gary V. Vaughan | 4fdf71b | 2010-05-14 09:31:37 +0000 | [diff] [blame] | 109 | test_cmp file target |
Junio C Hamano | d5a4164 | 2007-11-23 20:14:20 -0800 | [diff] [blame] | 110 | |
| 111 | ' |
| 112 | |
| 113 | test_expect_success 'whitespace=error-all, default rule' ' |
| 114 | |
Denton Liu | 37a63fa | 2020-01-06 23:53:13 -0500 | [diff] [blame] | 115 | apply_patch ! --whitespace=error-all && |
SZEDER Gábor | ec10b01 | 2018-08-19 23:57:22 +0200 | [diff] [blame] | 116 | test_must_be_empty target |
Junio C Hamano | d5a4164 | 2007-11-23 20:14:20 -0800 | [diff] [blame] | 117 | |
| 118 | ' |
| 119 | |
| 120 | test_expect_success 'whitespace=error-all, no rule' ' |
| 121 | |
| 122 | git config core.whitespace -trailing,-space-before,-indent && |
| 123 | apply_patch --whitespace=error-all && |
Gary V. Vaughan | 4fdf71b | 2010-05-14 09:31:37 +0000 | [diff] [blame] | 124 | test_cmp file target |
Junio C Hamano | d5a4164 | 2007-11-23 20:14:20 -0800 | [diff] [blame] | 125 | |
| 126 | ' |
| 127 | |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 128 | test_expect_success 'whitespace=error-all, no rule (attribute)' ' |
| 129 | |
| 130 | git config --unset core.whitespace && |
| 131 | echo "target -whitespace" >.gitattributes && |
| 132 | apply_patch --whitespace=error-all && |
Gary V. Vaughan | 4fdf71b | 2010-05-14 09:31:37 +0000 | [diff] [blame] | 133 | test_cmp file target |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 134 | |
| 135 | ' |
| 136 | |
Johannes Sixt | d35711a | 2010-11-30 09:22:04 +0100 | [diff] [blame] | 137 | test_expect_success 'spaces inserted by tab-in-indent' ' |
| 138 | |
| 139 | git config core.whitespace -trailing,-space,-indent,tab && |
| 140 | rm -f .gitattributes && |
| 141 | test_fix % && |
| 142 | sed -e "s/_/ /g" -e "s/>/ /" <<-\EOF >expect && |
| 143 | An_SP in an ordinary line>and a HT. |
| 144 | ________A HT (%). |
| 145 | ________A SP and a HT (@%). |
| 146 | _________A SP, a HT and a SP (@%). |
| 147 | _______Seven SP. |
| 148 | ________Eight SP (#). |
| 149 | ________Seven SP and a HT (@%). |
| 150 | ________________Eight SP and a HT (@#%). |
| 151 | _________Seven SP, a HT and a SP (@%). |
| 152 | _________________Eight SP, a HT and a SP (@#%). |
| 153 | _______________Fifteen SP (#). |
| 154 | ________________Fifteen SP and a HT (@#%). |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 155 | ________________Sixteen SP (#=). |
| 156 | ________________________Sixteen SP and a HT (@#%=). |
Johannes Sixt | d35711a | 2010-11-30 09:22:04 +0100 | [diff] [blame] | 157 | _____a__Five SP, a non WS, two SP. |
| 158 | A line with a (!) trailing SP_ |
| 159 | A line with a (!) trailing HT> |
| 160 | EOF |
| 161 | test_cmp expect target |
| 162 | |
| 163 | ' |
| 164 | |
Junio C Hamano | d5a4164 | 2007-11-23 20:14:20 -0800 | [diff] [blame] | 165 | for t in - '' |
| 166 | do |
| 167 | case "$t" in '') tt='!' ;; *) tt= ;; esac |
| 168 | for s in - '' |
| 169 | do |
| 170 | case "$s" in '') ts='@' ;; *) ts= ;; esac |
| 171 | for i in - '' |
| 172 | do |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 173 | case "$i" in '') ti='#' ti16='=';; *) ti= ti16= ;; esac |
Chris Webb | a347b17 | 2010-04-03 00:37:37 +0100 | [diff] [blame] | 174 | for h in - '' |
| 175 | do |
| 176 | [ -z "$h$i" ] && continue |
| 177 | case "$h" in '') th='%' ;; *) th= ;; esac |
| 178 | rule=${t}trailing,${s}space,${i}indent,${h}tab |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 179 | |
Chris Webb | a347b17 | 2010-04-03 00:37:37 +0100 | [diff] [blame] | 180 | rm -f .gitattributes |
| 181 | test_expect_success "rule=$rule" ' |
| 182 | git config core.whitespace "$rule" && |
| 183 | test_fix "$tt$ts$ti$th" |
| 184 | ' |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 185 | |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 186 | test_expect_success "rule=$rule,tabwidth=16" ' |
| 187 | git config core.whitespace "$rule,tabwidth=16" && |
| 188 | test_fix "$tt$ts$ti16$th" |
| 189 | ' |
| 190 | |
Chris Webb | a347b17 | 2010-04-03 00:37:37 +0100 | [diff] [blame] | 191 | test_expect_success "rule=$rule (attributes)" ' |
| 192 | git config --unset core.whitespace && |
| 193 | echo "target whitespace=$rule" >.gitattributes && |
| 194 | test_fix "$tt$ts$ti$th" |
| 195 | ' |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 196 | |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 197 | test_expect_success "rule=$rule,tabwidth=16 (attributes)" ' |
| 198 | echo "target whitespace=$rule,tabwidth=16" >.gitattributes && |
| 199 | test_fix "$tt$ts$ti16$th" |
| 200 | ' |
| 201 | |
Chris Webb | a347b17 | 2010-04-03 00:37:37 +0100 | [diff] [blame] | 202 | done |
Junio C Hamano | d5a4164 | 2007-11-23 20:14:20 -0800 | [diff] [blame] | 203 | done |
| 204 | done |
| 205 | done |
| 206 | |
SZEDER Gábor | 735c674 | 2009-07-22 19:24:38 -0500 | [diff] [blame] | 207 | create_patch () { |
| 208 | sed -e "s/_/ /" <<-\EOF |
| 209 | diff --git a/target b/target |
| 210 | index e69de29..8bd6648 100644 |
| 211 | --- a/target |
| 212 | +++ b/target |
Junio C Hamano | 422a82f | 2009-07-25 01:29:20 -0700 | [diff] [blame] | 213 | @@ -0,0 +1,3 @@ |
| 214 | +An empty line follows |
| 215 | + |
SZEDER Gábor | 735c674 | 2009-07-22 19:24:38 -0500 | [diff] [blame] | 216 | +A line with trailing whitespace and no newline_ |
| 217 | \ No newline at end of file |
| 218 | EOF |
| 219 | } |
| 220 | |
| 221 | test_expect_success 'trailing whitespace & no newline at the end of file' ' |
| 222 | >target && |
Junio C Hamano | 422a82f | 2009-07-25 01:29:20 -0700 | [diff] [blame] | 223 | create_patch >patch-file && |
| 224 | git apply --whitespace=fix patch-file && |
| 225 | grep "newline$" target && |
| 226 | grep "^$" target |
SZEDER Gábor | 735c674 | 2009-07-22 19:24:38 -0500 | [diff] [blame] | 227 | ' |
| 228 | |
Junio C Hamano | ef2035c | 2009-09-04 01:41:47 -0700 | [diff] [blame] | 229 | test_expect_success 'blank at EOF with --whitespace=fix (1)' ' |
Jonathan Nieder | 33fc521 | 2010-10-31 02:41:08 -0500 | [diff] [blame] | 230 | test_might_fail git config --unset core.whitespace && |
| 231 | rm -f .gitattributes && |
Junio C Hamano | ef2035c | 2009-09-04 01:41:47 -0700 | [diff] [blame] | 232 | |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 233 | test_write_lines a b c >one && |
Junio C Hamano | ef2035c | 2009-09-04 01:41:47 -0700 | [diff] [blame] | 234 | git add one && |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 235 | test_write_lines a b c >expect && |
Eric Sunshine | 7abcbcb | 2021-12-09 00:11:08 -0500 | [diff] [blame] | 236 | { cat expect && echo; } >one && |
Junio C Hamano | ef2035c | 2009-09-04 01:41:47 -0700 | [diff] [blame] | 237 | git diff -- one >patch && |
| 238 | |
| 239 | git checkout one && |
| 240 | git apply --whitespace=fix patch && |
| 241 | test_cmp expect one |
| 242 | ' |
| 243 | |
| 244 | test_expect_success 'blank at EOF with --whitespace=fix (2)' ' |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 245 | test_write_lines a b c >one && |
Junio C Hamano | ef2035c | 2009-09-04 01:41:47 -0700 | [diff] [blame] | 246 | git add one && |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 247 | test_write_lines a b >expect && |
| 248 | { cat expect && test_write_lines "" ""; } >one && |
Junio C Hamano | ef2035c | 2009-09-04 01:41:47 -0700 | [diff] [blame] | 249 | git diff -- one >patch && |
| 250 | |
| 251 | git checkout one && |
| 252 | git apply --whitespace=fix patch && |
| 253 | test_cmp expect one |
| 254 | ' |
| 255 | |
Junio C Hamano | efa5744 | 2009-09-03 14:08:20 -0700 | [diff] [blame] | 256 | test_expect_success 'blank at EOF with --whitespace=fix (3)' ' |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 257 | test_write_lines a b "" >one && |
Junio C Hamano | efa5744 | 2009-09-03 14:08:20 -0700 | [diff] [blame] | 258 | git add one && |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 259 | test_write_lines a c "" >expect && |
| 260 | { cat expect && test_write_lines "" ""; } >one && |
Junio C Hamano | efa5744 | 2009-09-03 14:08:20 -0700 | [diff] [blame] | 261 | git diff -- one >patch && |
| 262 | |
| 263 | git checkout one && |
| 264 | git apply --whitespace=fix patch && |
| 265 | test_cmp expect one |
| 266 | ' |
| 267 | |
| 268 | test_expect_success 'blank at end of hunk, not at EOF with --whitespace=fix' ' |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 269 | test_write_lines a b "" "" "" "" "" d >one && |
Junio C Hamano | efa5744 | 2009-09-03 14:08:20 -0700 | [diff] [blame] | 270 | git add one && |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 271 | test_write_lines a b "" "" "" "" "" "" d >expect && |
Junio C Hamano | efa5744 | 2009-09-03 14:08:20 -0700 | [diff] [blame] | 272 | cp expect one && |
| 273 | git diff -- one >patch && |
| 274 | |
| 275 | git checkout one && |
| 276 | git apply --whitespace=fix patch && |
| 277 | test_cmp expect one |
| 278 | ' |
| 279 | |
Junio C Hamano | 77b15bb | 2009-09-03 16:02:32 -0700 | [diff] [blame] | 280 | test_expect_success 'blank at EOF with --whitespace=warn' ' |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 281 | test_write_lines a b c >one && |
Junio C Hamano | 77b15bb | 2009-09-03 16:02:32 -0700 | [diff] [blame] | 282 | git add one && |
| 283 | echo >>one && |
| 284 | cat one >expect && |
| 285 | git diff -- one >patch && |
| 286 | |
| 287 | git checkout one && |
| 288 | git apply --whitespace=warn patch 2>error && |
| 289 | test_cmp expect one && |
| 290 | grep "new blank line at EOF" error |
| 291 | ' |
| 292 | |
| 293 | test_expect_success 'blank at EOF with --whitespace=error' ' |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 294 | test_write_lines a b c >one && |
Junio C Hamano | 77b15bb | 2009-09-03 16:02:32 -0700 | [diff] [blame] | 295 | git add one && |
| 296 | cat one >expect && |
| 297 | echo >>one && |
| 298 | git diff -- one >patch && |
| 299 | |
| 300 | git checkout one && |
| 301 | test_must_fail git apply --whitespace=error patch 2>error && |
| 302 | test_cmp expect one && |
| 303 | grep "new blank line at EOF" error |
| 304 | ' |
| 305 | |
Junio C Hamano | 94ea026 | 2009-09-04 02:25:57 -0700 | [diff] [blame] | 306 | test_expect_success 'blank but not empty at EOF' ' |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 307 | test_write_lines a b c >one && |
Junio C Hamano | 94ea026 | 2009-09-04 02:25:57 -0700 | [diff] [blame] | 308 | git add one && |
| 309 | echo " " >>one && |
| 310 | cat one >expect && |
| 311 | git diff -- one >patch && |
| 312 | |
| 313 | git checkout one && |
| 314 | git apply --whitespace=warn patch 2>error && |
| 315 | test_cmp expect one && |
| 316 | grep "new blank line at EOF" error |
| 317 | ' |
| 318 | |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 319 | test_expect_success 'applying beyond EOF requires one non-blank context line' ' |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 320 | test_write_lines "" "" "" "" >one && |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 321 | git add one && |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 322 | echo b >>one && |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 323 | git diff -- one >patch && |
| 324 | |
| 325 | git checkout one && |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 326 | test_write_lines a "" >one && |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 327 | cp one expect && |
| 328 | test_must_fail git apply --whitespace=fix patch && |
Matthew DeVore | dcbaa0b | 2018-10-05 14:54:04 -0700 | [diff] [blame] | 329 | test_cmp expect one && |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 330 | test_must_fail git apply --ignore-space-change --whitespace=fix patch && |
Matthew DeVore | dcbaa0b | 2018-10-05 14:54:04 -0700 | [diff] [blame] | 331 | test_cmp expect one |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 332 | ' |
| 333 | |
| 334 | test_expect_success 'tons of blanks at EOF should not apply' ' |
| 335 | for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do |
Eric Sunshine | cbe1d9d | 2021-12-09 00:11:13 -0500 | [diff] [blame] | 336 | test_write_lines "" "" "" "" || return 1 |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 337 | done >one && |
| 338 | git add one && |
| 339 | echo a >>one && |
| 340 | git diff -- one >patch && |
| 341 | |
| 342 | >one && |
| 343 | test_must_fail git apply --whitespace=fix patch && |
| 344 | test_must_fail git apply --ignore-space-change --whitespace=fix patch |
| 345 | ' |
| 346 | |
| 347 | test_expect_success 'missing blank line at end with --whitespace=fix' ' |
| 348 | echo a >one && |
| 349 | echo >>one && |
| 350 | git add one && |
| 351 | echo b >>one && |
| 352 | cp one expect && |
| 353 | git diff -- one >patch && |
| 354 | echo a >one && |
| 355 | cp one saved-one && |
| 356 | test_must_fail git apply patch && |
| 357 | git apply --whitespace=fix patch && |
Matthew DeVore | dcbaa0b | 2018-10-05 14:54:04 -0700 | [diff] [blame] | 358 | test_cmp expect one && |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 359 | mv saved-one one && |
| 360 | git apply --ignore-space-change --whitespace=fix patch && |
Matthew DeVore | dcbaa0b | 2018-10-05 14:54:04 -0700 | [diff] [blame] | 361 | test_cmp expect one |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 362 | ' |
| 363 | |
| 364 | test_expect_success 'two missing blank lines at end with --whitespace=fix' ' |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 365 | test_write_lines a "" b c >one && |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 366 | cp one no-blank-lines && |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 367 | test_write_lines "" "" >>one && |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 368 | git add one && |
| 369 | echo d >>one && |
| 370 | cp one expect && |
| 371 | echo >>one && |
| 372 | git diff -- one >patch && |
| 373 | cp no-blank-lines one && |
| 374 | test_must_fail git apply patch && |
| 375 | git apply --whitespace=fix patch && |
Matthew DeVore | dcbaa0b | 2018-10-05 14:54:04 -0700 | [diff] [blame] | 376 | test_cmp expect one && |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 377 | mv no-blank-lines one && |
| 378 | test_must_fail git apply patch && |
| 379 | git apply --ignore-space-change --whitespace=fix patch && |
Matthew DeVore | dcbaa0b | 2018-10-05 14:54:04 -0700 | [diff] [blame] | 380 | test_cmp expect one |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 381 | ' |
| 382 | |
Björn Gustavsson | 0c3ef98 | 2010-04-08 06:14:31 +0200 | [diff] [blame] | 383 | test_expect_success 'missing blank line at end, insert before end, --whitespace=fix' ' |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 384 | test_write_lines a "" >one && |
Björn Gustavsson | 0c3ef98 | 2010-04-08 06:14:31 +0200 | [diff] [blame] | 385 | git add one && |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 386 | test_write_lines b a "" >one && |
Björn Gustavsson | 0c3ef98 | 2010-04-08 06:14:31 +0200 | [diff] [blame] | 387 | cp one expect && |
| 388 | git diff -- one >patch && |
| 389 | echo a >one && |
| 390 | test_must_fail git apply patch && |
| 391 | git apply --whitespace=fix patch && |
Matthew DeVore | dcbaa0b | 2018-10-05 14:54:04 -0700 | [diff] [blame] | 392 | test_cmp expect one |
Björn Gustavsson | 0c3ef98 | 2010-04-08 06:14:31 +0200 | [diff] [blame] | 393 | ' |
| 394 | |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 395 | test_expect_success 'shrink file with tons of missing blanks at end of file' ' |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 396 | test_write_lines a b c >one && |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 397 | cp one no-blank-lines && |
| 398 | for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do |
Eric Sunshine | cbe1d9d | 2021-12-09 00:11:13 -0500 | [diff] [blame] | 399 | test_write_lines "" "" "" "" || return 1 |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 400 | done >>one && |
| 401 | git add one && |
| 402 | echo a >one && |
| 403 | cp one expect && |
| 404 | git diff -- one >patch && |
| 405 | cp no-blank-lines one && |
| 406 | test_must_fail git apply patch && |
| 407 | git apply --whitespace=fix patch && |
Matthew DeVore | dcbaa0b | 2018-10-05 14:54:04 -0700 | [diff] [blame] | 408 | test_cmp expect one && |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 409 | mv no-blank-lines one && |
| 410 | git apply --ignore-space-change --whitespace=fix patch && |
Matthew DeVore | dcbaa0b | 2018-10-05 14:54:04 -0700 | [diff] [blame] | 411 | test_cmp expect one |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 412 | ' |
| 413 | |
| 414 | test_expect_success 'missing blanks at EOF must only match blank lines' ' |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 415 | test_write_lines a b >one && |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 416 | git add one && |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 417 | test_write_lines c d >>one && |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 418 | git diff -- one >patch && |
| 419 | |
| 420 | echo a >one && |
Jonathan Nieder | 2dec68c | 2010-10-31 02:30:58 -0500 | [diff] [blame] | 421 | test_must_fail git apply patch && |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 422 | test_must_fail git apply --whitespace=fix patch && |
| 423 | test_must_fail git apply --ignore-space-change --whitespace=fix patch |
| 424 | ' |
| 425 | |
| 426 | sed -e's/Z//' >one <<EOF |
| 427 | a |
| 428 | b |
| 429 | c |
| 430 | Z |
| 431 | EOF |
| 432 | |
| 433 | test_expect_success 'missing blank line should match context line with spaces' ' |
| 434 | git add one && |
| 435 | echo d >>one && |
| 436 | git diff -- one >patch && |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 437 | test_write_lines a b c >one && |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 438 | cp one expect && |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 439 | test_write_lines "" d >>expect && |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 440 | git add one && |
| 441 | |
| 442 | git apply --whitespace=fix patch && |
Matthew DeVore | dcbaa0b | 2018-10-05 14:54:04 -0700 | [diff] [blame] | 443 | test_cmp expect one |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 444 | ' |
| 445 | |
| 446 | sed -e's/Z//' >one <<EOF |
| 447 | a |
| 448 | b |
| 449 | c |
| 450 | Z |
| 451 | EOF |
| 452 | |
| 453 | test_expect_success 'same, but with the --ignore-space-option' ' |
| 454 | git add one && |
| 455 | echo d >>one && |
| 456 | cp one expect && |
| 457 | git diff -- one >patch && |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 458 | test_write_lines a b c >one && |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 459 | git add one && |
| 460 | |
| 461 | git checkout-index -f one && |
| 462 | git apply --ignore-space-change --whitespace=fix patch && |
Matthew DeVore | dcbaa0b | 2018-10-05 14:54:04 -0700 | [diff] [blame] | 463 | test_cmp expect one |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 464 | ' |
| 465 | |
| 466 | test_expect_success 'same, but with CR-LF line endings && cr-at-eol set' ' |
| 467 | git config core.whitespace cr-at-eol && |
| 468 | printf "a\r\n" >one && |
| 469 | printf "b\r\n" >>one && |
| 470 | printf "c\r\n" >>one && |
| 471 | cp one save-one && |
Jonathan Nieder | 2dec68c | 2010-10-31 02:30:58 -0500 | [diff] [blame] | 472 | printf " \r\n" >>one && |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 473 | git add one && |
| 474 | printf "d\r\n" >>one && |
| 475 | cp one expect && |
| 476 | git diff -- one >patch && |
| 477 | mv save-one one && |
| 478 | |
| 479 | git apply --ignore-space-change --whitespace=fix patch && |
Matthew DeVore | dcbaa0b | 2018-10-05 14:54:04 -0700 | [diff] [blame] | 480 | test_cmp expect one |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 481 | ' |
| 482 | |
Torsten Bögershausen | c24f3ab | 2017-08-19 13:28:01 +0200 | [diff] [blame] | 483 | test_expect_success 'CR-LF line endings && add line && text=auto' ' |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 484 | git config --unset core.whitespace && |
| 485 | printf "a\r\n" >one && |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 486 | cp one save-one && |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 487 | git add one && |
Torsten Bögershausen | c24f3ab | 2017-08-19 13:28:01 +0200 | [diff] [blame] | 488 | printf "b\r\n" >>one && |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 489 | cp one expect && |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 490 | git diff -- one >patch && |
| 491 | mv save-one one && |
Torsten Bögershausen | c24f3ab | 2017-08-19 13:28:01 +0200 | [diff] [blame] | 492 | echo "one text=auto" >.gitattributes && |
| 493 | git apply patch && |
Matthew DeVore | dcbaa0b | 2018-10-05 14:54:04 -0700 | [diff] [blame] | 494 | test_cmp expect one |
Torsten Bögershausen | c24f3ab | 2017-08-19 13:28:01 +0200 | [diff] [blame] | 495 | ' |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 496 | |
Torsten Bögershausen | c24f3ab | 2017-08-19 13:28:01 +0200 | [diff] [blame] | 497 | test_expect_success 'CR-LF line endings && change line && text=auto' ' |
| 498 | printf "a\r\n" >one && |
| 499 | cp one save-one && |
| 500 | git add one && |
| 501 | printf "b\r\n" >one && |
| 502 | cp one expect && |
| 503 | git diff -- one >patch && |
| 504 | mv save-one one && |
| 505 | echo "one text=auto" >.gitattributes && |
| 506 | git apply patch && |
Matthew DeVore | dcbaa0b | 2018-10-05 14:54:04 -0700 | [diff] [blame] | 507 | test_cmp expect one |
Torsten Bögershausen | c24f3ab | 2017-08-19 13:28:01 +0200 | [diff] [blame] | 508 | ' |
| 509 | |
| 510 | test_expect_success 'LF in repo, CRLF in worktree && change line && text=auto' ' |
| 511 | printf "a\n" >one && |
| 512 | git add one && |
| 513 | printf "b\r\n" >one && |
| 514 | git diff -- one >patch && |
| 515 | printf "a\r\n" >one && |
| 516 | echo "one text=auto" >.gitattributes && |
| 517 | git -c core.eol=CRLF apply patch && |
| 518 | printf "b\r\n" >expect && |
Matthew DeVore | dcbaa0b | 2018-10-05 14:54:04 -0700 | [diff] [blame] | 519 | test_cmp expect one |
Björn Gustavsson | c1376c1 | 2010-03-06 15:31:04 +0100 | [diff] [blame] | 520 | ' |
| 521 | |
Junio C Hamano | 250b3c6 | 2013-03-22 11:10:03 -0700 | [diff] [blame] | 522 | test_expect_success 'whitespace=fix to expand' ' |
| 523 | qz_to_tab_space >preimage <<-\EOF && |
| 524 | QQa |
| 525 | QQb |
| 526 | QQc |
| 527 | ZZZZZZZZZZZZZZZZd |
| 528 | QQe |
| 529 | QQf |
| 530 | QQg |
| 531 | EOF |
| 532 | qz_to_tab_space >patch <<-\EOF && |
| 533 | diff --git a/preimage b/preimage |
| 534 | --- a/preimage |
| 535 | +++ b/preimage |
| 536 | @@ -1,7 +1,6 @@ |
| 537 | QQa |
| 538 | QQb |
| 539 | QQc |
| 540 | -QQd |
| 541 | QQe |
| 542 | QQf |
| 543 | QQg |
| 544 | EOF |
| 545 | git -c core.whitespace=tab-in-indent apply --whitespace=fix patch |
| 546 | ' |
| 547 | |
Junio C Hamano | 477a08a | 2014-08-06 13:09:05 -0700 | [diff] [blame] | 548 | test_expect_success 'whitespace check skipped for excluded paths' ' |
| 549 | git config core.whitespace blank-at-eol && |
| 550 | >used && |
| 551 | >unused && |
| 552 | git add used unused && |
| 553 | echo "used" >used && |
| 554 | echo "unused " >unused && |
| 555 | git diff-files -p used unused >patch && |
| 556 | git apply --include=used --stat --whitespace=error <patch |
| 557 | ' |
| 558 | |
Junio C Hamano | d5a4164 | 2007-11-23 20:14:20 -0800 | [diff] [blame] | 559 | test_done |