Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/t/t4034-diff-words.sh # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / t / t4034-diff-words.sh` 416 lines ยท 9475 bytes Raw Blame History 1 #!/bin/sh 2 3 test_description='word diff colors' 4 5 . ./test-lib.sh 6 . "$TEST_DIRECTORY"/lib-diff.sh 7 8 cat >pre.simple <<-\EOF 9 h(4) 10 11 a = b + c 12 EOF 13 cat >post.simple <<-\EOF 14 h(4),hh[44] 15 16 a = b + c 17 18 aa = a 19 20 aeff = aeff * ( aaa ) 21 EOF 22 pre=$(git rev-parse --short $(git hash-object pre.simple)) 23 post=$(git rev-parse --short $(git hash-object post.simple)) 24 cat >expect.letter-runs-are-words <<-EOF 25 diff --git a/pre b/post 26 index $pre..$post 100644 27 --- a/pre 28 +++ b/post 29 @@ -1,3 +1,7 @@ 30 h(4),hh[44] 31 32 a = b + c 33 34 aa = a 35 36 aeff = aeff * ( aaa ) 37 EOF 38 cat >expect.non-whitespace-is-word <<-EOF 39 diff --git a/pre b/post 40 index $pre..$post 100644 41 --- a/pre 42 +++ b/post 43 @@ -1,3 +1,7 @@ 44 h(4),hh[44] 45 46 a = b + c 47 48 aa = a 49 50 aeff = aeff * ( aaa ) 51 EOF 52 53 word_diff () { 54 pre=$(git rev-parse --short $(git hash-object pre)) && 55 post=$(git rev-parse --short $(git hash-object post)) && 56 test_must_fail git diff --no-index "$@" pre post >output && 57 test_decode_color output.decrypted && 58 sed -e "2s/index [^ ]*/index $pre..$post/" expect >expected 59 test_cmp expected output.decrypted 60 } 61 62 test_language_driver () { 63 lang=$1 64 test_expect_success "diff driver '$lang'" ' 65 cp "$TEST_DIRECTORY/t4034/'"$lang"'/pre" \ 66 "$TEST_DIRECTORY/t4034/'"$lang"'/post" \ 67 "$TEST_DIRECTORY/t4034/'"$lang"'/expect" . && 68 echo "* diff='"$lang"'" >.gitattributes && 69 word_diff --color-words 70 ' 71 test_expect_success "diff driver '$lang' in Islandic" ' 72 test_env LANG=is_IS.UTF-8 LANGUAGE=is LC_ALL="$is_IS_locale" \ 73 word_diff --color-words 74 ' 75 } 76 77 test_expect_success setup ' 78 git config diff.color.old red && 79 git config diff.color.new green && 80 git config diff.color.func magenta 81 ' 82 83 test_expect_success 'set up pre and post with runs of whitespace' ' 84 cp pre.simple pre && 85 cp post.simple post 86 ' 87 88 test_expect_success 'word diff with runs of whitespace' ' 89 cat >expect <<-EOF && 90 diff --git a/pre b/post 91 index $pre..$post 100644 92 --- a/pre 93 +++ b/post 94 @@ -1,3 +1,7 @@ 95 h(4)h(4),hh[44] 96 97 a = b + c 98 99 aa = a 100 101 aeff = aeff * ( aaa ) 102 EOF 103 word_diff --color-words && 104 word_diff --word-diff=color && 105 word_diff --color --word-diff=color 106 ' 107 108 test_expect_success '--word-diff=porcelain' ' 109 sed "s/#.*$//" >expect <<-EOF && 110 diff --git a/pre b/post 111 index $pre..$post 100644 112 --- a/pre 113 +++ b/post 114 @@ -1,3 +1,7 @@ 115 -h(4) 116 +h(4),hh[44] 117 ~ 118 # significant space 119 ~ 120 a = b + c 121 ~ 122 ~ 123 +aa = a 124 ~ 125 ~ 126 +aeff = aeff * ( aaa ) 127 ~ 128 EOF 129 word_diff --word-diff=porcelain 130 ' 131 132 test_expect_success '--word-diff=plain' ' 133 cat >expect <<-EOF && 134 diff --git a/pre b/post 135 index $pre..$post 100644 136 --- a/pre 137 +++ b/post 138 @@ -1,3 +1,7 @@ 139 [-h(4)-]{+h(4),hh[44]+} 140 141 a = b + c 142 143 {+aa = a+} 144 145 {+aeff = aeff * ( aaa )+} 146 EOF 147 word_diff --word-diff=plain && 148 word_diff --word-diff=plain --no-color 149 ' 150 151 test_expect_success '--word-diff=plain --color' ' 152 cat >expect <<-EOF && 153 diff --git a/pre b/post 154 index $pre..$post 100644 155 --- a/pre 156 +++ b/post 157 @@ -1,3 +1,7 @@ 158 [-h(4)-]{+h(4),hh[44]+} 159 160 a = b + c 161 162 {+aa = a+} 163 164 {+aeff = aeff * ( aaa )+} 165 EOF 166 word_diff --word-diff=plain --color 167 ' 168 169 test_expect_success 'word diff without context' ' 170 cat >expect <<-EOF && 171 diff --git a/pre b/post 172 index $pre..$post 100644 173 --- a/pre 174 +++ b/post 175 @@ -1 +1 @@ 176 h(4)h(4),hh[44] 177 @@ -3,0 +4,4 @@ a = b + c 178 179 aa = a 180 181 aeff = aeff * ( aaa ) 182 EOF 183 word_diff --color-words --unified=0 184 ' 185 186 test_expect_success 'word diff with a regular expression' ' 187 cp expect.letter-runs-are-words expect && 188 word_diff --color-words="[a-z]+" 189 ' 190 191 test_expect_success 'word diff with zero length matches' ' 192 cp expect.letter-runs-are-words expect && 193 word_diff --color-words="[a-z${LF}]*" 194 ' 195 196 test_expect_success 'set up a diff driver' ' 197 git config diff.testdriver.wordRegex "[^[:space:]]" && 198 cat <<-\EOF >.gitattributes 199 pre diff=testdriver 200 post diff=testdriver 201 EOF 202 ' 203 204 test_expect_success 'option overrides .gitattributes' ' 205 cp expect.letter-runs-are-words expect && 206 word_diff --color-words="[a-z]+" 207 ' 208 209 test_expect_success 'use regex supplied by driver' ' 210 cp expect.non-whitespace-is-word expect && 211 word_diff --color-words 212 ' 213 214 test_expect_success 'set up diff.wordRegex option' ' 215 git config diff.wordRegex "[[:alnum:]]+" 216 ' 217 218 test_expect_success 'command-line overrides config' ' 219 cp expect.letter-runs-are-words expect && 220 word_diff --color-words="[a-z]+" 221 ' 222 223 test_expect_success 'command-line overrides config: --word-diff-regex' ' 224 cat >expect <<-EOF && 225 diff --git a/pre b/post 226 index $pre..$post 100644 227 --- a/pre 228 +++ b/post 229 @@ -1,3 +1,7 @@ 230 h(4),{+hh+}[44] 231 232 a = b + c 233 234 {+aa = a+} 235 236 {+aeff = aeff * ( aaa+} ) 237 EOF 238 word_diff --color --word-diff-regex="[a-z]+" 239 ' 240 241 test_expect_success '.gitattributes override config' ' 242 cp expect.non-whitespace-is-word expect && 243 word_diff --color-words 244 ' 245 246 test_expect_success 'setup: remove diff driver regex' ' 247 test_unconfig diff.testdriver.wordRegex 248 ' 249 250 test_expect_success 'use configured regex' ' 251 cat >expect <<-EOF && 252 diff --git a/pre b/post 253 index $pre..$post 100644 254 --- a/pre 255 +++ b/post 256 @@ -1,3 +1,7 @@ 257 h(4),hh[44] 258 259 a = b + c 260 261 aa = a 262 263 aeff = aeff * ( aaa ) 264 EOF 265 word_diff --color-words 266 ' 267 268 test_expect_success 'test parsing words for newline' ' 269 echo "aaa (aaa)" >pre && 270 echo "aaa (aaa) aaa" >post && 271 pre=$(git rev-parse --short $(git hash-object pre)) && 272 post=$(git rev-parse --short $(git hash-object post)) && 273 cat >expect <<-EOF && 274 diff --git a/pre b/post 275 index $pre..$post 100644 276 --- a/pre 277 +++ b/post 278 @@ -1 +1 @@ 279 aaa (aaa) aaa 280 EOF 281 word_diff --color-words="a+" 282 ' 283 284 test_expect_success 'test when words are only removed at the end' ' 285 echo "(:" >pre && 286 echo "(" >post && 287 pre=$(git rev-parse --short $(git hash-object pre)) && 288 post=$(git rev-parse --short $(git hash-object post)) && 289 cat >expect <<-EOF && 290 diff --git a/pre b/post 291 index $pre..$post 100644 292 --- a/pre 293 +++ b/post 294 @@ -1 +1 @@ 295 (: 296 EOF 297 word_diff --color-words=. 298 ' 299 300 test_expect_success '--word-diff=none' ' 301 echo "(:" >pre && 302 echo "(" >post && 303 pre=$(git rev-parse --short $(git hash-object pre)) && 304 post=$(git rev-parse --short $(git hash-object post)) && 305 cat >expect <<-EOF && 306 diff --git a/pre b/post 307 index $pre..$post 100644 308 --- a/pre 309 +++ b/post 310 @@ -1 +1 @@ 311 -(: 312 +( 313 EOF 314 word_diff --word-diff=plain --word-diff=none 315 ' 316 317 test_expect_success 'unset default driver' ' 318 test_unconfig diff.wordregex 319 ' 320 321 test_language_driver ada 322 test_language_driver bibtex 323 test_language_driver bash 324 test_language_driver cpp 325 test_language_driver csharp 326 test_language_driver css 327 test_language_driver dts 328 test_language_driver fortran 329 test_language_driver html 330 test_language_driver java 331 test_language_driver kotlin 332 test_language_driver matlab 333 test_language_driver objc 334 test_language_driver pascal 335 test_language_driver perl 336 test_language_driver php 337 test_language_driver python 338 test_language_driver ruby 339 test_language_driver scheme 340 test_language_driver tex 341 342 test_expect_success 'word-diff with diff.sbe' ' 343 cat >pre <<-\EOF && 344 a 345 346 b 347 EOF 348 cat >post <<-\EOF && 349 a 350 351 c 352 EOF 353 pre=$(git rev-parse --short $(git hash-object pre)) && 354 post=$(git rev-parse --short $(git hash-object post)) && 355 cat >expect <<-EOF && 356 diff --git a/pre b/post 357 index $pre..$post 100644 358 --- a/pre 359 +++ b/post 360 @@ -1,3 +1,3 @@ 361 a 362 363 [-b-]{+c+} 364 EOF 365 test_config diff.suppress-blank-empty true && 366 word_diff --word-diff=plain 367 ' 368 369 test_expect_success 'word-diff with no newline at EOF' ' 370 printf "%s" "a a a a a" >pre && 371 printf "%s" "a a ab a a" >post && 372 pre=$(git rev-parse --short $(git hash-object pre)) && 373 post=$(git rev-parse --short $(git hash-object post)) && 374 cat >expect <<-EOF && 375 diff --git a/pre b/post 376 index $pre..$post 100644 377 --- a/pre 378 +++ b/post 379 @@ -1 +1 @@ 380 a a [-a-]{+ab+} a a 381 EOF 382 word_diff --word-diff=plain 383 ' 384 385 test_expect_success 'setup history with two files' ' 386 echo "a b; c" >a.tex && 387 echo "a b; c" >z.txt && 388 git add a.tex z.txt && 389 git commit -minitial && 390 391 # modify both 392 echo "a bx; c" >a.tex && 393 echo "a bx; c" >z.txt && 394 git commit -mmodified -a 395 ' 396 397 test_expect_success 'wordRegex for the first file does not apply to the second' ' 398 echo "*.tex diff=tex" >.gitattributes && 399 test_config diff.tex.wordRegex "[a-z]+|." && 400 cat >expect <<-\EOF && 401 diff --git a/a.tex b/a.tex 402 --- a/a.tex 403 +++ b/a.tex 404 @@ -1 +1 @@ 405 a [-b-]{+bx+}; c 406 diff --git a/z.txt b/z.txt 407 --- a/z.txt 408 +++ b/z.txt 409 @@ -1 +1 @@ 410 a [-b;-]{+bx;+} c 411 EOF 412 git diff --word-diff HEAD~ >actual && 413 compare_diff_patch expect actual 414 ' 415 416 test_done