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