]> git.ipfire.org Git - thirdparty/git.git/blob - t/t4034-diff-words.sh
t6004: add pathspec globbing test for log family
[thirdparty/git.git] / t / t4034-diff-words.sh
1 #!/bin/sh
2
3 test_description='word diff colors'
4
5 . ./test-lib.sh
6
7 test_expect_success setup '
8
9 git config diff.color.old red &&
10 git config diff.color.new green &&
11 git config diff.color.func magenta
12
13 '
14
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
19 }
20
21 cat > pre <<\EOF
22 h(4)
23
24 a = b + c
25 EOF
26
27 cat > post <<\EOF
28 h(4),hh[44]
29
30 a = b + c
31
32 aa = a
33
34 aeff = aeff * ( aaa )
35 EOF
36
37 cat > expect <<\EOF
38 <BOLD>diff --git a/pre b/post<RESET>
39 <BOLD>index 330b04f..5ed8eff 100644<RESET>
40 <BOLD>--- a/pre<RESET>
41 <BOLD>+++ b/post<RESET>
42 <CYAN>@@ -1,3 +1,7 @@<RESET>
43 <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
44
45 a = b + c<RESET>
46
47 <GREEN>aa = a<RESET>
48
49 <GREEN>aeff = aeff * ( aaa )<RESET>
50 EOF
51
52 test_expect_success 'word diff with runs of whitespace' '
53
54 word_diff --color-words
55
56 '
57
58 test_expect_success '--word-diff=color' '
59
60 word_diff --word-diff=color
61
62 '
63
64 test_expect_success '--color --word-diff=color' '
65
66 word_diff --color --word-diff=color
67
68 '
69
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]
78 ~
79 # significant space
80 ~
81 a = b + c
82 ~
83 ~
84 +aa = a
85 ~
86 ~
87 +aeff = aeff * ( aaa )
88 ~
89 EOF
90
91 test_expect_success '--word-diff=porcelain' '
92
93 word_diff --word-diff=porcelain
94
95 '
96
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]+}
104
105 a = b + c
106
107 {+aa = a+}
108
109 {+aeff = aeff * ( aaa )+}
110 EOF
111
112 test_expect_success '--word-diff=plain' '
113
114 word_diff --word-diff=plain
115
116 '
117
118 test_expect_success '--word-diff=plain --no-color' '
119
120 word_diff --word-diff=plain --no-color
121
122 '
123
124 cat > expect <<EOF
125 <BOLD>diff --git a/pre b/post<RESET>
126 <BOLD>index 330b04f..5ed8eff 100644<RESET>
127 <BOLD>--- a/pre<RESET>
128 <BOLD>+++ b/post<RESET>
129 <CYAN>@@ -1,3 +1,7 @@<RESET>
130 <RED>[-h(4)-]<RESET><GREEN>{+h(4),hh[44]+}<RESET>
131
132 a = b + c<RESET>
133
134 <GREEN>{+aa = a+}<RESET>
135
136 <GREEN>{+aeff = aeff * ( aaa )+}<RESET>
137 EOF
138
139 test_expect_success '--word-diff=plain --color' '
140
141 word_diff --word-diff=plain --color
142
143 '
144
145 cat > expect <<\EOF
146 <BOLD>diff --git a/pre b/post<RESET>
147 <BOLD>index 330b04f..5ed8eff 100644<RESET>
148 <BOLD>--- a/pre<RESET>
149 <BOLD>+++ 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>
153
154 <GREEN>aa = a<RESET>
155
156 <GREEN>aeff = aeff * ( aaa )<RESET>
157 EOF
158
159 test_expect_success 'word diff without context' '
160
161 word_diff --color-words --unified=0
162
163 '
164
165 cat > expect <<\EOF
166 <BOLD>diff --git a/pre b/post<RESET>
167 <BOLD>index 330b04f..5ed8eff 100644<RESET>
168 <BOLD>--- a/pre<RESET>
169 <BOLD>+++ b/post<RESET>
170 <CYAN>@@ -1,3 +1,7 @@<RESET>
171 h(4),<GREEN>hh<RESET>[44]
172
173 a = b + c<RESET>
174
175 <GREEN>aa = a<RESET>
176
177 <GREEN>aeff = aeff * ( aaa<RESET> )
178 EOF
179 cp expect expect.letter-runs-are-words
180
181 test_expect_success 'word diff with a regular expression' '
182
183 word_diff --color-words="[a-z]+"
184
185 '
186
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
192 EOF
193 '
194
195 test_expect_success 'option overrides .gitattributes' '
196
197 word_diff --color-words="[a-z]+"
198
199 '
200
201 cat > expect <<\EOF
202 <BOLD>diff --git a/pre b/post<RESET>
203 <BOLD>index 330b04f..5ed8eff 100644<RESET>
204 <BOLD>--- a/pre<RESET>
205 <BOLD>+++ b/post<RESET>
206 <CYAN>@@ -1,3 +1,7 @@<RESET>
207 h(4)<GREEN>,hh[44]<RESET>
208
209 a = b + c<RESET>
210
211 <GREEN>aa = a<RESET>
212
213 <GREEN>aeff = aeff * ( aaa )<RESET>
214 EOF
215 cp expect expect.non-whitespace-is-word
216
217 test_expect_success 'use regex supplied by driver' '
218
219 word_diff --color-words
220
221 '
222
223 test_expect_success 'set diff.wordRegex option' '
224 git config diff.wordRegex "[[:alnum:]]+"
225 '
226
227 cp expect.letter-runs-are-words expect
228
229 test_expect_success 'command-line overrides config' '
230 word_diff --color-words="[a-z]+"
231 '
232
233 cat > expect <<\EOF
234 <BOLD>diff --git a/pre b/post<RESET>
235 <BOLD>index 330b04f..5ed8eff 100644<RESET>
236 <BOLD>--- a/pre<RESET>
237 <BOLD>+++ b/post<RESET>
238 <CYAN>@@ -1,3 +1,7 @@<RESET>
239 h(4),<GREEN>{+hh+}<RESET>[44]
240
241 a = b + c<RESET>
242
243 <GREEN>{+aa = a+}<RESET>
244
245 <GREEN>{+aeff = aeff * ( aaa+}<RESET> )
246 EOF
247
248 test_expect_success 'command-line overrides config: --word-diff-regex' '
249 word_diff --color --word-diff-regex="[a-z]+"
250 '
251
252 cp expect.non-whitespace-is-word expect
253
254 test_expect_success '.gitattributes override config' '
255 word_diff --color-words
256 '
257
258 test_expect_success 'remove diff driver regex' '
259 git config --unset diff.testdriver.wordRegex
260 '
261
262 cat > expect <<\EOF
263 <BOLD>diff --git a/pre b/post<RESET>
264 <BOLD>index 330b04f..5ed8eff 100644<RESET>
265 <BOLD>--- a/pre<RESET>
266 <BOLD>+++ b/post<RESET>
267 <CYAN>@@ -1,3 +1,7 @@<RESET>
268 h(4),<GREEN>hh[44<RESET>]
269
270 a = b + c<RESET>
271
272 <GREEN>aa = a<RESET>
273
274 <GREEN>aeff = aeff * ( aaa<RESET> )
275 EOF
276
277 test_expect_success 'use configured regex' '
278 word_diff --color-words
279 '
280
281 echo 'aaa (aaa)' > pre
282 echo 'aaa (aaa) aaa' > post
283
284 cat > expect <<\EOF
285 <BOLD>diff --git a/pre b/post<RESET>
286 <BOLD>index c29453b..be22f37 100644<RESET>
287 <BOLD>--- a/pre<RESET>
288 <BOLD>+++ b/post<RESET>
289 <CYAN>@@ -1 +1 @@<RESET>
290 aaa (aaa) <GREEN>aaa<RESET>
291 EOF
292
293 test_expect_success 'test parsing words for newline' '
294
295 word_diff --color-words="a+"
296
297
298 '
299
300 echo '(:' > pre
301 echo '(' > post
302
303 cat > expect <<\EOF
304 <BOLD>diff --git a/pre b/post<RESET>
305 <BOLD>index 289cb9d..2d06f37 100644<RESET>
306 <BOLD>--- a/pre<RESET>
307 <BOLD>+++ b/post<RESET>
308 <CYAN>@@ -1 +1 @@<RESET>
309 (<RED>:<RESET>
310 EOF
311
312 test_expect_success 'test when words are only removed at the end' '
313
314 word_diff --color-words=.
315
316 '
317
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 @@
324 -(:
325 +(
326 EOF
327
328 test_expect_success '--word-diff=none' '
329
330 word_diff --word-diff=plain --word-diff=none
331
332 '
333
334 test_done