Merge branch 'tr/rfc-reset-doc' into maint
[git/jnareb-git/bp-gitweb.git] / t / t4034-diff-words.sh
blob6f7548c3a144ecada6642eccee6b0f687baafb2d
1 #!/bin/sh
3 test_description='word diff colors'
5 . ./test-lib.sh
7 test_expect_success setup '
9 git config diff.color.old red
10 git config diff.color.new green
11 git config diff.color.func magenta
15 word_diff () {
16 test_must_fail git diff --no-index "$@" pre post > output &&
17 test_decode_color <output >output.decrypted &&
18 test_cmp expect output.decrypted
21 cat > pre <<\EOF
22 h(4)
24 a = b + c
25 EOF
27 cat > post <<\EOF
28 h(4),hh[44]
30 a = b + c
32 aa = a
34 aeff = aeff * ( aaa )
35 EOF
37 cat > expect <<\EOF
38 <WHITE>diff --git a/pre b/post<RESET>
39 <WHITE>index 330b04f..5ed8eff 100644<RESET>
40 <WHITE>--- a/pre<RESET>
41 <WHITE>+++ b/post<RESET>
42 <CYAN>@@ -1,3 +1,7 @@<RESET>
43 <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
45 a = b + c<RESET>
47 <GREEN>aa = a<RESET>
49 <GREEN>aeff = aeff * ( aaa )<RESET>
50 EOF
52 test_expect_success 'word diff with runs of whitespace' '
54 word_diff --color-words
58 test_expect_success '--word-diff=color' '
60 word_diff --word-diff=color
64 test_expect_success '--color --word-diff=color' '
66 word_diff --color --word-diff=color
70 sed 's/#.*$//' > expect <<EOF
71 diff --git a/pre b/post
72 index 330b04f..5ed8eff 100644
73 --- a/pre
74 +++ b/post
75 @@ -1,3 +1,7 @@
76 -h(4)
77 +h(4),hh[44]
79 # significant space
81 a = b + c
84 +aa = a
87 +aeff = aeff * ( aaa )
89 EOF
91 test_expect_success '--word-diff=porcelain' '
93 word_diff --word-diff=porcelain
97 cat > expect <<EOF
98 diff --git a/pre b/post
99 index 330b04f..5ed8eff 100644
100 --- a/pre
101 +++ b/post
102 @@ -1,3 +1,7 @@
103 [-h(4)-]{+h(4),hh[44]+}
105 a = b + c
107 {+aa = a+}
109 {+aeff = aeff * ( aaa )+}
112 test_expect_success '--word-diff=plain' '
114 word_diff --word-diff=plain
118 test_expect_success '--word-diff=plain --no-color' '
120 word_diff --word-diff=plain --no-color
124 cat > expect <<EOF
125 <WHITE>diff --git a/pre b/post<RESET>
126 <WHITE>index 330b04f..5ed8eff 100644<RESET>
127 <WHITE>--- a/pre<RESET>
128 <WHITE>+++ b/post<RESET>
129 <CYAN>@@ -1,3 +1,7 @@<RESET>
130 <RED>[-h(4)-]<RESET><GREEN>{+h(4),hh[44]+}<RESET>
132 a = b + c<RESET>
134 <GREEN>{+aa = a+}<RESET>
136 <GREEN>{+aeff = aeff * ( aaa )+}<RESET>
139 test_expect_success '--word-diff=plain --color' '
141 word_diff --word-diff=plain --color
145 cat > expect <<\EOF
146 <WHITE>diff --git a/pre b/post<RESET>
147 <WHITE>index 330b04f..5ed8eff 100644<RESET>
148 <WHITE>--- a/pre<RESET>
149 <WHITE>+++ b/post<RESET>
150 <CYAN>@@ -1 +1 @@<RESET>
151 <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
152 <CYAN>@@ -3,0 +4,4 @@<RESET> <RESET><MAGENTA>a = b + c<RESET>
154 <GREEN>aa = a<RESET>
156 <GREEN>aeff = aeff * ( aaa )<RESET>
159 test_expect_success 'word diff without context' '
161 word_diff --color-words --unified=0
165 cat > expect <<\EOF
166 <WHITE>diff --git a/pre b/post<RESET>
167 <WHITE>index 330b04f..5ed8eff 100644<RESET>
168 <WHITE>--- a/pre<RESET>
169 <WHITE>+++ b/post<RESET>
170 <CYAN>@@ -1,3 +1,7 @@<RESET>
171 h(4),<GREEN>hh<RESET>[44]
173 a = b + c<RESET>
175 <GREEN>aa = a<RESET>
177 <GREEN>aeff = aeff * ( aaa<RESET> )
179 cp expect expect.letter-runs-are-words
181 test_expect_success 'word diff with a regular expression' '
183 word_diff --color-words="[a-z]+"
187 test_expect_success 'set a diff driver' '
188 git config diff.testdriver.wordRegex "[^[:space:]]" &&
189 cat <<EOF > .gitattributes
190 pre diff=testdriver
191 post diff=testdriver
195 test_expect_success 'option overrides .gitattributes' '
197 word_diff --color-words="[a-z]+"
201 cat > expect <<\EOF
202 <WHITE>diff --git a/pre b/post<RESET>
203 <WHITE>index 330b04f..5ed8eff 100644<RESET>
204 <WHITE>--- a/pre<RESET>
205 <WHITE>+++ b/post<RESET>
206 <CYAN>@@ -1,3 +1,7 @@<RESET>
207 h(4)<GREEN>,hh[44]<RESET>
209 a = b + c<RESET>
211 <GREEN>aa = a<RESET>
213 <GREEN>aeff = aeff * ( aaa )<RESET>
215 cp expect expect.non-whitespace-is-word
217 test_expect_success 'use regex supplied by driver' '
219 word_diff --color-words
223 test_expect_success 'set diff.wordRegex option' '
224 git config diff.wordRegex "[[:alnum:]]+"
227 cp expect.letter-runs-are-words expect
229 test_expect_success 'command-line overrides config' '
230 word_diff --color-words="[a-z]+"
233 cat > expect <<\EOF
234 <WHITE>diff --git a/pre b/post<RESET>
235 <WHITE>index 330b04f..5ed8eff 100644<RESET>
236 <WHITE>--- a/pre<RESET>
237 <WHITE>+++ b/post<RESET>
238 <CYAN>@@ -1,3 +1,7 @@<RESET>
239 h(4),<GREEN>{+hh+}<RESET>[44]
241 a = b + c<RESET>
243 <GREEN>{+aa = a+}<RESET>
245 <GREEN>{+aeff = aeff * ( aaa+}<RESET> )
248 test_expect_success 'command-line overrides config: --word-diff-regex' '
249 word_diff --color --word-diff-regex="[a-z]+"
252 cp expect.non-whitespace-is-word expect
254 test_expect_success '.gitattributes override config' '
255 word_diff --color-words
258 test_expect_success 'remove diff driver regex' '
259 git config --unset diff.testdriver.wordRegex
262 cat > expect <<\EOF
263 <WHITE>diff --git a/pre b/post<RESET>
264 <WHITE>index 330b04f..5ed8eff 100644<RESET>
265 <WHITE>--- a/pre<RESET>
266 <WHITE>+++ b/post<RESET>
267 <CYAN>@@ -1,3 +1,7 @@<RESET>
268 h(4),<GREEN>hh[44<RESET>]
270 a = b + c<RESET>
272 <GREEN>aa = a<RESET>
274 <GREEN>aeff = aeff * ( aaa<RESET> )
277 test_expect_success 'use configured regex' '
278 word_diff --color-words
281 echo 'aaa (aaa)' > pre
282 echo 'aaa (aaa) aaa' > post
284 cat > expect <<\EOF
285 <WHITE>diff --git a/pre b/post<RESET>
286 <WHITE>index c29453b..be22f37 100644<RESET>
287 <WHITE>--- a/pre<RESET>
288 <WHITE>+++ b/post<RESET>
289 <CYAN>@@ -1 +1 @@<RESET>
290 aaa (aaa) <GREEN>aaa<RESET>
293 test_expect_success 'test parsing words for newline' '
295 word_diff --color-words="a+"
300 echo '(:' > pre
301 echo '(' > post
303 cat > expect <<\EOF
304 <WHITE>diff --git a/pre b/post<RESET>
305 <WHITE>index 289cb9d..2d06f37 100644<RESET>
306 <WHITE>--- a/pre<RESET>
307 <WHITE>+++ b/post<RESET>
308 <CYAN>@@ -1 +1 @@<RESET>
309 (<RED>:<RESET>
312 test_expect_success 'test when words are only removed at the end' '
314 word_diff --color-words=.
318 cat > expect <<\EOF
319 diff --git a/pre b/post
320 index 289cb9d..2d06f37 100644
321 --- a/pre
322 +++ b/post
323 @@ -1 +1 @@
328 test_expect_success '--word-diff=none' '
330 word_diff --word-diff=plain --word-diff=none
334 test_done