Merge branch 'gp/maint-cvsserver' into maint
[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 decrypt_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
25 word_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
31 cat > pre <<\EOF
32 h(4)
33
34 a = b + c
35 EOF
36
37 cat > post <<\EOF
38 h(4),hh[44]
39
40 a = b + c
41
42 aa = a
43
44 aeff = aeff * ( aaa )
45 EOF
46
47 cat > 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
55 a = b + c<RESET>
56
57 <GREEN>aa = a<RESET>
58
59 <GREEN>aeff = aeff * ( aaa )<RESET>
60 EOF
61
62 test_expect_success 'word diff with runs of whitespace' '
63
64         word_diff --color-words
65
66 '
67
68 cat > 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>
80 EOF
81
82 test_expect_success 'word diff without context' '
83
84         word_diff --color-words --unified=0
85
86 '
87
88 cat > 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>
94 h(4),<GREEN>hh<RESET>[44]
95
96 a = b + c<RESET>
97
98 <GREEN>aa = a<RESET>
99
100 <GREEN>aeff = aeff * ( aaa<RESET> )
101 EOF
102 cp expect expect.letter-runs-are-words
103
104 test_expect_success 'word diff with a regular expression' '
105
106         word_diff --color-words="[a-z]+"
107
108 '
109
110 test_expect_success 'set a diff driver' '
111         git config diff.testdriver.wordRegex "[^[:space:]]" &&
112         cat <<EOF > .gitattributes
113 pre diff=testdriver
114 post diff=testdriver
115 EOF
116 '
117
118 test_expect_success 'option overrides .gitattributes' '
119
120         word_diff --color-words="[a-z]+"
121
122 '
123
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 <BROWN>@@ -1,3 +1,7 @@<RESET>
130 h(4)<GREEN>,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 cp expect expect.non-whitespace-is-word
139
140 test_expect_success 'use regex supplied by driver' '
141
142         word_diff --color-words
143
144 '
145
146 test_expect_success 'set diff.wordRegex option' '
147         git config diff.wordRegex "[[:alnum:]]+"
148 '
149
150 cp expect.letter-runs-are-words expect
151
152 test_expect_success 'command-line overrides config' '
153         word_diff --color-words="[a-z]+"
154 '
155
156 cp expect.non-whitespace-is-word expect
157
158 test_expect_success '.gitattributes override config' '
159         word_diff --color-words
160 '
161
162 test_expect_success 'remove diff driver regex' '
163         git config --unset diff.testdriver.wordRegex
164 '
165
166 cat > 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>
172 h(4),<GREEN>hh[44<RESET>]
173
174 a = b + c<RESET>
175
176 <GREEN>aa = a<RESET>
177
178 <GREEN>aeff = aeff * ( aaa<RESET> )
179 EOF
180
181 test_expect_success 'use configured regex' '
182         word_diff --color-words
183 '
184
185 echo 'aaa (aaa)' > pre
186 echo 'aaa (aaa) aaa' > post
187
188 cat > 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>
194 aaa (aaa) <GREEN>aaa<RESET>
195 EOF
196
197 test_expect_success 'test parsing words for newline' '
198
199         word_diff --color-words="a+"
200
201
202 '
203
204 echo '(:' > pre
205 echo '(' > post
206
207 cat > 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>
214 EOF
215
216 test_expect_success 'test when words are only removed at the end' '
217
218         word_diff --color-words=.
219
220 '
221
222 test_done