Skip to content

Commit 44e4155

Browse files
committed
Merge branch 'kb/maint-diff-ws-check'
* kb/maint-diff-ws-check: diff: handle lines containing only whitespace and tabs better test-lib: extend test_decode_color to handle more color codes
2 parents 016c4b8 + cfd1a98 commit 44e4155

File tree

4 files changed

+118
-47
lines changed

4 files changed

+118
-47
lines changed

t/t4015-diff-whitespace.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,4 +491,41 @@ test_expect_success 'combined diff with autocrlf conversion' '
491491
492492
'
493493

494+
# Start testing the colored format for whitespace checks
495+
496+
test_expect_success 'setup diff colors' '
497+
git config color.diff always &&
498+
git config color.diff.plain normal &&
499+
git config color.diff.meta bold &&
500+
git config color.diff.frag cyan &&
501+
git config color.diff.func normal &&
502+
git config color.diff.old red &&
503+
git config color.diff.new green &&
504+
git config color.diff.commit yellow &&
505+
git config color.diff.whitespace "normal red" &&
506+
507+
git config core.autocrlf false
508+
'
509+
cat >expected <<\EOF
510+
<BOLD>diff --git a/x b/x<RESET>
511+
<BOLD>index 9daeafb..2874b91 100644<RESET>
512+
<BOLD>--- a/x<RESET>
513+
<BOLD>+++ b/x<RESET>
514+
<CYAN>@@ -1 +1,4 @@<RESET>
515+
test<RESET>
516+
<GREEN>+<RESET><GREEN>{<RESET>
517+
<GREEN>+<RESET><BRED> <RESET>
518+
<GREEN>+<RESET><GREEN>}<RESET>
519+
EOF
520+
521+
test_expect_success 'diff that introduces a line with only tabs' '
522+
git config core.whitespace blank-at-eol &&
523+
git reset --hard &&
524+
echo "test" > x &&
525+
git commit -m "initial" x &&
526+
echo "{NTN}" | tr "NT" "\n\t" >> x &&
527+
git -c color.diff=always diff | test_decode_color >current &&
528+
test_cmp expected current
529+
'
530+
494531
test_done

t/t4034-diff-words.sh

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ aeff = aeff * ( aaa )
3535
EOF
3636

3737
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>
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>
4242
<CYAN>@@ -1,3 +1,7 @@<RESET>
4343
<RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
4444
@@ -122,10 +122,10 @@ test_expect_success '--word-diff=plain --no-color' '
122122
'
123123

124124
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>
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>
129129
<CYAN>@@ -1,3 +1,7 @@<RESET>
130130
<RED>[-h(4)-]<RESET><GREEN>{+h(4),hh[44]+}<RESET>
131131
@@ -143,10 +143,10 @@ test_expect_success '--word-diff=plain --color' '
143143
'
144144

145145
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>
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>
150150
<CYAN>@@ -1 +1 @@<RESET>
151151
<RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
152152
<CYAN>@@ -3,0 +4,4 @@<RESET> <RESET><MAGENTA>a = b + c<RESET>
@@ -163,10 +163,10 @@ test_expect_success 'word diff without context' '
163163
'
164164

165165
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>
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>
170170
<CYAN>@@ -1,3 +1,7 @@<RESET>
171171
h(4),<GREEN>hh<RESET>[44]
172172
@@ -199,10 +199,10 @@ test_expect_success 'option overrides .gitattributes' '
199199
'
200200

201201
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>
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>
206206
<CYAN>@@ -1,3 +1,7 @@<RESET>
207207
h(4)<GREEN>,hh[44]<RESET>
208208
@@ -231,10 +231,10 @@ test_expect_success 'command-line overrides config' '
231231
'
232232

233233
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>
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>
238238
<CYAN>@@ -1,3 +1,7 @@<RESET>
239239
h(4),<GREEN>{+hh+}<RESET>[44]
240240
@@ -260,10 +260,10 @@ test_expect_success 'remove diff driver regex' '
260260
'
261261

262262
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>
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>
267267
<CYAN>@@ -1,3 +1,7 @@<RESET>
268268
h(4),<GREEN>hh[44<RESET>]
269269
@@ -282,10 +282,10 @@ echo 'aaa (aaa)' > pre
282282
echo 'aaa (aaa) aaa' > post
283283

284284
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>
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>
289289
<CYAN>@@ -1 +1 @@<RESET>
290290
aaa (aaa) <GREEN>aaa<RESET>
291291
EOF
@@ -301,10 +301,10 @@ echo '(:' > pre
301301
echo '(' > post
302302

303303
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>
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>
308308
<CYAN>@@ -1 +1 @@<RESET>
309309
(<RED>:<RESET>
310310
EOF

t/test-lib.sh

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,47 @@ test_set_editor () {
238238
}
239239

240240
test_decode_color () {
241-
sed -e 's/.\[1m/<WHITE>/g' \
242-
-e 's/.\[31m/<RED>/g' \
243-
-e 's/.\[32m/<GREEN>/g' \
244-
-e 's/.\[33m/<YELLOW>/g' \
245-
-e 's/.\[34m/<BLUE>/g' \
246-
-e 's/.\[35m/<MAGENTA>/g' \
247-
-e 's/.\[36m/<CYAN>/g' \
248-
-e 's/.\[m/<RESET>/g'
241+
awk '
242+
function name(n) {
243+
if (n == 0) return "RESET";
244+
if (n == 1) return "BOLD";
245+
if (n == 30) return "BLACK";
246+
if (n == 31) return "RED";
247+
if (n == 32) return "GREEN";
248+
if (n == 33) return "YELLOW";
249+
if (n == 34) return "BLUE";
250+
if (n == 35) return "MAGENTA";
251+
if (n == 36) return "CYAN";
252+
if (n == 37) return "WHITE";
253+
if (n == 40) return "BLACK";
254+
if (n == 41) return "BRED";
255+
if (n == 42) return "BGREEN";
256+
if (n == 43) return "BYELLOW";
257+
if (n == 44) return "BBLUE";
258+
if (n == 45) return "BMAGENTA";
259+
if (n == 46) return "BCYAN";
260+
if (n == 47) return "BWHITE";
261+
}
262+
{
263+
while (match($0, /\x1b\[[0-9;]*m/) != 0) {
264+
printf "%s<", substr($0, 1, RSTART-1);
265+
codes = substr($0, RSTART+2, RLENGTH-3);
266+
if (length(codes) == 0)
267+
printf "%s", name(0)
268+
else {
269+
n = split(codes, ary, ";");
270+
sep = "";
271+
for (i = 1; i <= n; i++) {
272+
printf "%s%s", sep, name(ary[i]);
273+
sep = ";"
274+
}
275+
}
276+
printf ">";
277+
$0 = substr($0, RSTART + RLENGTH, length($0) - RSTART - RLENGTH + 1);
278+
}
279+
print
280+
}
281+
'
249282
}
250283

251284
nul_to_q () {

ws.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,11 @@ static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
174174
}
175175
}
176176

177+
if (trailing_whitespace == -1)
178+
trailing_whitespace = len;
179+
177180
/* Check indentation */
178-
for (i = 0; i < len; i++) {
181+
for (i = 0; i < trailing_whitespace; i++) {
179182
if (line[i] == ' ')
180183
continue;
181184
if (line[i] != '\t')
@@ -218,8 +221,6 @@ static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
218221
* Now the rest of the line starts at "written".
219222
* The non-highlighted part ends at "trailing_whitespace".
220223
*/
221-
if (trailing_whitespace == -1)
222-
trailing_whitespace = len;
223224

224225
/* Emit non-highlighted (middle) segment. */
225226
if (trailing_whitespace - written > 0) {

0 commit comments

Comments
 (0)