Junio C Hamano | 49e703a | 2007-11-02 17:46:55 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='diff whitespace error detection' |
| 4 | |
Ævar Arnfjörð Bjarmason | 7ff2478 | 2021-10-12 15:56:45 +0200 | [diff] [blame] | 5 | TEST_PASSES_SANITIZE_LEAK=true |
Junio C Hamano | 49e703a | 2007-11-02 17:46:55 -0700 | [diff] [blame] | 6 | . ./test-lib.sh |
| 7 | |
| 8 | test_expect_success setup ' |
| 9 | |
| 10 | git config diff.color.whitespace "blue reverse" && |
| 11 | >F && |
| 12 | git add F && |
| 13 | echo " Eight SP indent" >>F && |
| 14 | echo " HT and SP indent" >>F && |
| 15 | echo "With trailing SP " >>F && |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 16 | echo "Carriage ReturnQ" | tr Q "\015" >>F && |
Junio C Hamano | 04c6e9e | 2008-08-11 22:15:28 -0700 | [diff] [blame] | 17 | echo "No problem" >>F && |
| 18 | echo >>F |
Junio C Hamano | 49e703a | 2007-11-02 17:46:55 -0700 | [diff] [blame] | 19 | |
| 20 | ' |
| 21 | |
| 22 | blue_grep='7;34m' ;# ESC [ 7 ; 3 4 m |
| 23 | |
Junio C Hamano | eca4460 | 2009-12-26 13:53:17 -0800 | [diff] [blame] | 24 | printf "\033[%s" "$blue_grep" >check-grep |
| 25 | if (grep "$blue_grep" <check-grep | grep "$blue_grep") >/dev/null 2>&1 |
| 26 | then |
| 27 | grep_a=grep |
| 28 | elif (grep -a "$blue_grep" <check-grep | grep -a "$blue_grep") >/dev/null 2>&1 |
| 29 | then |
| 30 | grep_a='grep -a' |
| 31 | else |
| 32 | grep_a=grep ;# expected to fail... |
| 33 | fi |
| 34 | rm -f check-grep |
| 35 | |
| 36 | prepare_output () { |
| 37 | git diff --color >output |
| 38 | $grep_a "$blue_grep" output >error |
| 39 | $grep_a -v "$blue_grep" output >normal |
Elijah Newren | 0564b3e | 2010-10-03 14:00:06 -0600 | [diff] [blame] | 40 | return 0 |
Junio C Hamano | eca4460 | 2009-12-26 13:53:17 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Junio C Hamano | 49e703a | 2007-11-02 17:46:55 -0700 | [diff] [blame] | 43 | test_expect_success default ' |
| 44 | |
Elijah Newren | 0564b3e | 2010-10-03 14:00:06 -0600 | [diff] [blame] | 45 | prepare_output && |
Junio C Hamano | 49e703a | 2007-11-02 17:46:55 -0700 | [diff] [blame] | 46 | |
| 47 | grep Eight normal >/dev/null && |
| 48 | grep HT error >/dev/null && |
| 49 | grep With error >/dev/null && |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 50 | grep Return error >/dev/null && |
Junio C Hamano | 49e703a | 2007-11-02 17:46:55 -0700 | [diff] [blame] | 51 | grep No normal >/dev/null |
| 52 | |
| 53 | ' |
| 54 | |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 55 | test_expect_success 'default (attribute)' ' |
| 56 | |
| 57 | test_might_fail git config --unset core.whitespace && |
| 58 | echo "F whitespace" >.gitattributes && |
| 59 | prepare_output && |
| 60 | |
| 61 | grep Eight error >/dev/null && |
| 62 | grep HT error >/dev/null && |
| 63 | grep With error >/dev/null && |
| 64 | grep Return error >/dev/null && |
| 65 | grep No normal >/dev/null |
| 66 | |
| 67 | ' |
| 68 | |
| 69 | test_expect_success 'default, tabwidth=10 (attribute)' ' |
| 70 | |
| 71 | git config core.whitespace "tabwidth=10" && |
| 72 | echo "F whitespace" >.gitattributes && |
| 73 | prepare_output && |
| 74 | |
| 75 | grep Eight normal >/dev/null && |
| 76 | grep HT error >/dev/null && |
| 77 | grep With error >/dev/null && |
| 78 | grep Return error >/dev/null && |
| 79 | grep No normal >/dev/null |
| 80 | |
| 81 | ' |
| 82 | |
| 83 | test_expect_success 'no check (attribute)' ' |
| 84 | |
| 85 | test_might_fail git config --unset core.whitespace && |
| 86 | echo "F -whitespace" >.gitattributes && |
| 87 | prepare_output && |
| 88 | |
| 89 | grep Eight normal >/dev/null && |
| 90 | grep HT normal >/dev/null && |
| 91 | grep With normal >/dev/null && |
| 92 | grep Return normal >/dev/null && |
| 93 | grep No normal >/dev/null |
| 94 | |
| 95 | ' |
| 96 | |
| 97 | test_expect_success 'no check, tabwidth=10 (attribute), must be irrelevant' ' |
| 98 | |
| 99 | git config core.whitespace "tabwidth=10" && |
| 100 | echo "F -whitespace" >.gitattributes && |
| 101 | prepare_output && |
| 102 | |
| 103 | grep Eight normal >/dev/null && |
| 104 | grep HT normal >/dev/null && |
| 105 | grep With normal >/dev/null && |
| 106 | grep Return normal >/dev/null && |
| 107 | grep No normal >/dev/null |
| 108 | |
| 109 | ' |
| 110 | |
Junio C Hamano | 49e703a | 2007-11-02 17:46:55 -0700 | [diff] [blame] | 111 | test_expect_success 'without -trail' ' |
| 112 | |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 113 | rm -f .gitattributes && |
Elijah Newren | 0564b3e | 2010-10-03 14:00:06 -0600 | [diff] [blame] | 114 | git config core.whitespace -trail && |
| 115 | prepare_output && |
Junio C Hamano | 49e703a | 2007-11-02 17:46:55 -0700 | [diff] [blame] | 116 | |
| 117 | grep Eight normal >/dev/null && |
| 118 | grep HT error >/dev/null && |
| 119 | grep With normal >/dev/null && |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 120 | grep Return normal >/dev/null && |
Junio C Hamano | 49e703a | 2007-11-02 17:46:55 -0700 | [diff] [blame] | 121 | grep No normal >/dev/null |
| 122 | |
| 123 | ' |
| 124 | |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 125 | test_expect_success 'without -trail (attribute)' ' |
| 126 | |
Elijah Newren | 0564b3e | 2010-10-03 14:00:06 -0600 | [diff] [blame] | 127 | test_might_fail git config --unset core.whitespace && |
| 128 | echo "F whitespace=-trail" >.gitattributes && |
| 129 | prepare_output && |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 130 | |
| 131 | grep Eight normal >/dev/null && |
| 132 | grep HT error >/dev/null && |
| 133 | grep With normal >/dev/null && |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 134 | grep Return normal >/dev/null && |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 135 | grep No normal >/dev/null |
| 136 | |
| 137 | ' |
| 138 | |
Junio C Hamano | 49e703a | 2007-11-02 17:46:55 -0700 | [diff] [blame] | 139 | test_expect_success 'without -space' ' |
| 140 | |
Elijah Newren | 0564b3e | 2010-10-03 14:00:06 -0600 | [diff] [blame] | 141 | rm -f .gitattributes && |
| 142 | git config core.whitespace -space && |
| 143 | prepare_output && |
Junio C Hamano | 49e703a | 2007-11-02 17:46:55 -0700 | [diff] [blame] | 144 | |
| 145 | grep Eight normal >/dev/null && |
| 146 | grep HT normal >/dev/null && |
| 147 | grep With error >/dev/null && |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 148 | grep Return error >/dev/null && |
Junio C Hamano | 49e703a | 2007-11-02 17:46:55 -0700 | [diff] [blame] | 149 | grep No normal >/dev/null |
| 150 | |
| 151 | ' |
| 152 | |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 153 | test_expect_success 'without -space (attribute)' ' |
| 154 | |
Elijah Newren | 0564b3e | 2010-10-03 14:00:06 -0600 | [diff] [blame] | 155 | test_might_fail git config --unset core.whitespace && |
| 156 | echo "F whitespace=-space" >.gitattributes && |
| 157 | prepare_output && |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 158 | |
| 159 | grep Eight normal >/dev/null && |
| 160 | grep HT normal >/dev/null && |
| 161 | grep With error >/dev/null && |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 162 | grep Return error >/dev/null && |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 163 | grep No normal >/dev/null |
| 164 | |
| 165 | ' |
| 166 | |
Junio C Hamano | 49e703a | 2007-11-02 17:46:55 -0700 | [diff] [blame] | 167 | test_expect_success 'with indent-non-tab only' ' |
| 168 | |
Elijah Newren | 0564b3e | 2010-10-03 14:00:06 -0600 | [diff] [blame] | 169 | rm -f .gitattributes && |
| 170 | git config core.whitespace indent,-trailing,-space && |
| 171 | prepare_output && |
Junio C Hamano | 49e703a | 2007-11-02 17:46:55 -0700 | [diff] [blame] | 172 | |
| 173 | grep Eight error >/dev/null && |
| 174 | grep HT normal >/dev/null && |
| 175 | grep With normal >/dev/null && |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 176 | grep Return normal >/dev/null && |
Junio C Hamano | 49e703a | 2007-11-02 17:46:55 -0700 | [diff] [blame] | 177 | grep No normal >/dev/null |
| 178 | |
| 179 | ' |
| 180 | |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 181 | test_expect_success 'with indent-non-tab only (attribute)' ' |
| 182 | |
Elijah Newren | 0564b3e | 2010-10-03 14:00:06 -0600 | [diff] [blame] | 183 | test_might_fail git config --unset core.whitespace && |
| 184 | echo "F whitespace=indent,-trailing,-space" >.gitattributes && |
| 185 | prepare_output && |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 186 | |
| 187 | grep Eight error >/dev/null && |
| 188 | grep HT normal >/dev/null && |
| 189 | grep With normal >/dev/null && |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 190 | grep Return normal >/dev/null && |
| 191 | grep No normal >/dev/null |
| 192 | |
| 193 | ' |
| 194 | |
Johannes Sixt | f4b05a4 | 2010-11-30 09:29:11 +0100 | [diff] [blame] | 195 | test_expect_success 'with indent-non-tab only, tabwidth=10' ' |
| 196 | |
| 197 | rm -f .gitattributes && |
| 198 | git config core.whitespace indent,tabwidth=10,-trailing,-space && |
| 199 | prepare_output && |
| 200 | |
| 201 | grep Eight normal >/dev/null && |
| 202 | grep HT normal >/dev/null && |
| 203 | grep With normal >/dev/null && |
| 204 | grep Return normal >/dev/null && |
| 205 | grep No normal >/dev/null |
| 206 | |
| 207 | ' |
| 208 | |
| 209 | test_expect_success 'with indent-non-tab only, tabwidth=10 (attribute)' ' |
| 210 | |
| 211 | test_might_fail git config --unset core.whitespace && |
| 212 | echo "F whitespace=indent,-trailing,-space,tabwidth=10" >.gitattributes && |
| 213 | prepare_output && |
| 214 | |
| 215 | grep Eight normal >/dev/null && |
| 216 | grep HT normal >/dev/null && |
| 217 | grep With normal >/dev/null && |
| 218 | grep Return normal >/dev/null && |
| 219 | grep No normal >/dev/null |
| 220 | |
| 221 | ' |
| 222 | |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 223 | test_expect_success 'with cr-at-eol' ' |
| 224 | |
Elijah Newren | 0564b3e | 2010-10-03 14:00:06 -0600 | [diff] [blame] | 225 | rm -f .gitattributes && |
| 226 | git config core.whitespace cr-at-eol && |
| 227 | prepare_output && |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 228 | |
| 229 | grep Eight normal >/dev/null && |
| 230 | grep HT error >/dev/null && |
| 231 | grep With error >/dev/null && |
| 232 | grep Return normal >/dev/null && |
| 233 | grep No normal >/dev/null |
| 234 | |
| 235 | ' |
| 236 | |
| 237 | test_expect_success 'with cr-at-eol (attribute)' ' |
| 238 | |
Elijah Newren | 0564b3e | 2010-10-03 14:00:06 -0600 | [diff] [blame] | 239 | test_might_fail git config --unset core.whitespace && |
| 240 | echo "F whitespace=trailing,cr-at-eol" >.gitattributes && |
| 241 | prepare_output && |
Junio C Hamano | b2979ff | 2008-01-15 00:59:05 -0800 | [diff] [blame] | 242 | |
| 243 | grep Eight normal >/dev/null && |
| 244 | grep HT error >/dev/null && |
| 245 | grep With error >/dev/null && |
| 246 | grep Return normal >/dev/null && |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 247 | grep No normal >/dev/null |
| 248 | |
| 249 | ' |
| 250 | |
Junio C Hamano | 04c6e9e | 2008-08-11 22:15:28 -0700 | [diff] [blame] | 251 | test_expect_success 'trailing empty lines (1)' ' |
| 252 | |
| 253 | rm -f .gitattributes && |
| 254 | test_must_fail git diff --check >output && |
Junio C Hamano | 5b5061e | 2009-09-03 22:30:27 -0700 | [diff] [blame] | 255 | grep "new blank line at" output && |
Junio C Hamano | 04c6e9e | 2008-08-11 22:15:28 -0700 | [diff] [blame] | 256 | grep "trailing whitespace" output |
| 257 | |
| 258 | ' |
| 259 | |
| 260 | test_expect_success 'trailing empty lines (2)' ' |
| 261 | |
| 262 | echo "F -whitespace" >.gitattributes && |
| 263 | git diff --check >output && |
SZEDER Gábor | ec10b01 | 2018-08-19 23:57:22 +0200 | [diff] [blame] | 264 | test_must_be_empty output |
Junio C Hamano | 04c6e9e | 2008-08-11 22:15:28 -0700 | [diff] [blame] | 265 | |
| 266 | ' |
| 267 | |
Christoph Mallon | 8837d33 | 2010-10-10 19:24:06 +0200 | [diff] [blame] | 268 | test_expect_success 'checkdiff shows correct line number for trailing blank lines' ' |
| 269 | |
| 270 | printf "a\nb\n" > G && |
| 271 | git add G && |
| 272 | printf "x\nx\nx\na\nb\nc\n\n" > G && |
| 273 | [ "$(git diff --check -- G)" = "G:7: new blank line at EOF." ] |
| 274 | |
| 275 | ' |
| 276 | |
Junio C Hamano | 3928097 | 2008-08-27 19:48:01 -0700 | [diff] [blame] | 277 | test_expect_success 'do not color trailing cr in context' ' |
Elijah Newren | 0564b3e | 2010-10-03 14:00:06 -0600 | [diff] [blame] | 278 | test_might_fail git config --unset core.whitespace && |
Junio C Hamano | 3928097 | 2008-08-27 19:48:01 -0700 | [diff] [blame] | 279 | rm -f .gitattributes && |
| 280 | echo AAAQ | tr Q "\015" >G && |
| 281 | git add G && |
Elijah Newren | 0564b3e | 2010-10-03 14:00:06 -0600 | [diff] [blame] | 282 | echo BBBQ | tr Q "\015" >>G && |
Junio C Hamano | 3928097 | 2008-08-27 19:48:01 -0700 | [diff] [blame] | 283 | git diff --color G | tr "\015" Q >output && |
| 284 | grep "BBB.*${blue_grep}Q" output && |
| 285 | grep "AAA.*\[mQ" output |
| 286 | |
| 287 | ' |
| 288 | |
Junio C Hamano | 690ed84 | 2009-09-04 00:41:15 -0700 | [diff] [blame] | 289 | test_expect_success 'color new trailing blank lines' ' |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 290 | test_write_lines a b "" "" >x && |
Junio C Hamano | 690ed84 | 2009-09-04 00:41:15 -0700 | [diff] [blame] | 291 | git add x && |
Eric Sunshine | 0849541 | 2021-12-09 00:11:05 -0500 | [diff] [blame] | 292 | test_write_lines a "" "" "" c "" "" "" "" >x && |
Junio C Hamano | 690ed84 | 2009-09-04 00:41:15 -0700 | [diff] [blame] | 293 | git diff --color x >output && |
Junio C Hamano | eca4460 | 2009-12-26 13:53:17 -0800 | [diff] [blame] | 294 | cnt=$($grep_a "${blue_grep}" output | wc -l) && |
Junio C Hamano | 690ed84 | 2009-09-04 00:41:15 -0700 | [diff] [blame] | 295 | test $cnt = 2 |
| 296 | ' |
| 297 | |
Junio C Hamano | 49e703a | 2007-11-02 17:46:55 -0700 | [diff] [blame] | 298 | test_done |