1#!/bin/sh
2
3test_description='word diff colors'
4
5. ./test-lib.sh
6
7test_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
15decrypt_color () {
16 sed \
17 -e 's/.\[1m/<WHITE>/g' \
18 -e 's/.\[31m/<RED>/g' \
19 -e 's/.\[32m/<GREEN>/g' \
20 -e 's/.\[35m/<MAGENTA>/g' \
21 -e 's/.\[36m/<BROWN>/g' \
22 -e 's/.\[m/<RESET>/g'
23}
24
25word_diff () {
26 test_must_fail git diff --no-index "$@" pre post > output &&
27 decrypt_color < output > output.decrypted &&
28 test_cmp expect output.decrypted
29}
30
31cat > pre <<\EOF
32h(4)
33
34a = b + c
35EOF
36
37cat > post <<\EOF
38h(4),hh[44]
39
40a = b + c
41
42aa = a
43
44aeff = aeff * ( aaa )
45EOF
46
47cat > expect <<\EOF
48<WHITE>diff --git a/pre b/post<RESET>
49<WHITE>index 330b04f..5ed8eff 100644<RESET>
50<WHITE>--- a/pre<RESET>
51<WHITE>+++ b/post<RESET>
52<BROWN>@@ -1,3 +1,7 @@<RESET>
53<RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
54
55a = b + c<RESET>
56
57<GREEN>aa = a<RESET>
58
59<GREEN>aeff = aeff * ( aaa )<RESET>
60EOF
61
62test_expect_success 'word diff with runs of whitespace' '
63
64 word_diff --color-words
65
66'
67
68cat > expect <<\EOF
69<WHITE>diff --git a/pre b/post<RESET>
70<WHITE>index 330b04f..5ed8eff 100644<RESET>
71<WHITE>--- a/pre<RESET>
72<WHITE>+++ b/post<RESET>
73<BROWN>@@ -1 +1 @@<RESET>
74<RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
75<BROWN>@@ -3,0 +4,4 @@<RESET> <RESET><MAGENTA>a = b + c<RESET>
76
77<GREEN>aa = a<RESET>
78
79<GREEN>aeff = aeff * ( aaa )<RESET>
80EOF
81
82test_expect_success 'word diff without context' '
83
84 word_diff --color-words --unified=0
85
86'
87
88cat > expect <<\EOF
89<WHITE>diff --git a/pre b/post<RESET>
90<WHITE>index 330b04f..5ed8eff 100644<RESET>
91<WHITE>--- a/pre<RESET>
92<WHITE>+++ b/post<RESET>
93<BROWN>@@ -1,3 +1,7 @@<RESET>
94h(4),<GREEN>hh<RESET>[44]
95
96a = b + c<RESET>
97
98<GREEN>aa = a<RESET>
99
100<GREEN>aeff = aeff * ( aaa<RESET> )
101EOF
102cp expect expect.letter-runs-are-words
103
104test_expect_success 'word diff with a regular expression' '
105
106 word_diff --color-words="[a-z]+"
107
108'
109
110test_expect_success 'set a diff driver' '
111 git config diff.testdriver.wordRegex "[^[:space:]]" &&
112 cat <<EOF > .gitattributes
113pre diff=testdriver
114post diff=testdriver
115EOF
116'
117
118test_expect_success 'option overrides .gitattributes' '
119
120 word_diff --color-words="[a-z]+"
121
122'
123
124cat > 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<BROWN>@@ -1,3 +1,7 @@<RESET>
130h(4)<GREEN>,hh[44]<RESET>
131
132a = b + c<RESET>
133
134<GREEN>aa = a<RESET>
135
136<GREEN>aeff = aeff * ( aaa )<RESET>
137EOF
138cp expect expect.non-whitespace-is-word
139
140test_expect_success 'use regex supplied by driver' '
141
142 word_diff --color-words
143
144'
145
146test_expect_success 'set diff.wordRegex option' '
147 git config diff.wordRegex "[[:alnum:]]+"
148'
149
150cp expect.letter-runs-are-words expect
151
152test_expect_success 'command-line overrides config' '
153 word_diff --color-words="[a-z]+"
154'
155
156cp expect.non-whitespace-is-word expect
157
158test_expect_success '.gitattributes override config' '
159 word_diff --color-words
160'
161
162test_expect_success 'remove diff driver regex' '
163 git config --unset diff.testdriver.wordRegex
164'
165
166cat > expect <<\EOF
167<WHITE>diff --git a/pre b/post<RESET>
168<WHITE>index 330b04f..5ed8eff 100644<RESET>
169<WHITE>--- a/pre<RESET>
170<WHITE>+++ b/post<RESET>
171<BROWN>@@ -1,3 +1,7 @@<RESET>
172h(4),<GREEN>hh[44<RESET>]
173
174a = b + c<RESET>
175
176<GREEN>aa = a<RESET>
177
178<GREEN>aeff = aeff * ( aaa<RESET> )
179EOF
180
181test_expect_success 'use configured regex' '
182 word_diff --color-words
183'
184
185echo 'aaa (aaa)' > pre
186echo 'aaa (aaa) aaa' > post
187
188cat > expect <<\EOF
189<WHITE>diff --git a/pre b/post<RESET>
190<WHITE>index c29453b..be22f37 100644<RESET>
191<WHITE>--- a/pre<RESET>
192<WHITE>+++ b/post<RESET>
193<BROWN>@@ -1 +1 @@<RESET>
194aaa (aaa) <GREEN>aaa<RESET>
195EOF
196
197test_expect_success 'test parsing words for newline' '
198
199 word_diff --color-words="a+"
200
201
202'
203
204echo '(:' > pre
205echo '(' > post
206
207cat > expect <<\EOF
208<WHITE>diff --git a/pre b/post<RESET>
209<WHITE>index 289cb9d..2d06f37 100644<RESET>
210<WHITE>--- a/pre<RESET>
211<WHITE>+++ b/post<RESET>
212<BROWN>@@ -1 +1 @@<RESET>
213(<RED>:<RESET>
214EOF
215
216test_expect_success 'test when words are only removed at the end' '
217
218 word_diff --color-words=.
219
220'
221
222test_done