diff.c: second war on whitespace.
This adds DIFF_WHITESPACE color class (default = reverse red) to colored diff output to let you catch common whitespace errors. - trailing whitespaces at the end of line - a space followed by a tab in the indent Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Sep 22, 2006 at 22:48 UTC
448c3ef144b2f528bb6dc50153ac13cc2abbc3c4
2 files changed
+132
-41
diff.c
+131
-41
@@ -20,12 +20,13 @@ static int diff_use_color_default;
20
21
static char diff_colors[][COLOR_MAXLEN] = {
22
"\033[m", /* reset */
23
- "", /* normal */
24
- "\033[1m", /* bold */
25
- "\033[36m", /* cyan */
26
- "\033[31m", /* red */
27
- "\033[32m", /* green */
28
- "\033[33m" /* yellow */
23
+ "", /* PLAIN (normal) */
24
+ "\033[1m", /* METAINFO (bold) */
25
+ "\033[36m", /* FRAGINFO (cyan) */
26
+ "\033[31m", /* OLD (red) */
27
+ "\033[32m", /* NEW (green) */
28
+ "\033[33m", /* COMMIT (yellow) */
29
+ "\033[41m", /* WHITESPACE (red background) */
30
};
31
32
static int parse_diff_color_slot(const char *var, int ofs)
@@ -42,6 +43,8 @@ static int parse_diff_color_slot(const char *var, int ofs)
43
return DIFF_FILE_NEW;
44
if (!strcasecmp(var+ofs, "commit"))
45
return DIFF_COMMIT;
46
+ if (!strcasecmp(var+ofs, "whitespace"))
47
+ return DIFF_WHITESPACE;
48
die("bad config variable '%s'", var);
49
}
50
@@ -383,9 +386,89 @@ const char *diff_get_color(int diff_use_color, enum color_diff ix)
386
return "";
387
}
388
389
+static void emit_line(const char *set, const char *reset, const char *line, int len)
390
+{
391
+ if (len > 0 && line[len-1] == '\n')
392
+ len--;
393
+ fputs(set, stdout);
394
+ fwrite(line, len, 1, stdout);
395
+ puts(reset);
396
+}
397
+
398
+static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len)
399
+{
400
+ int col0 = ecbdata->nparents;
401
+ int last_tab_in_indent = -1;
402
+ int last_space_in_indent = -1;
403
+ int i;
404
+ int tail = len;
405
+ int need_highlight_leading_space = 0;
406
+ const char *ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE);
407
+ const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_NEW);
408
+
409
+ if (!*ws) {
410
+ emit_line(set, reset, line, len);
411
+ return;
412
+ }
413
+
414
+ /* The line is a newly added line. Does it have funny leading
415
+ * whitespaces? In indent, SP should never precede a TAB.
416
+ */
417
+ for (i = col0; i < len; i++) {
418
+ if (line[i] == '\t') {
419
+ last_tab_in_indent = i;
420
+ if (0 <= last_space_in_indent)
421
+ need_highlight_leading_space = 1;
422
+ }
423
+ else if (line[i] == ' ')
424
+ last_space_in_indent = i;
425
+ else
426
+ break;
427
+ }
428
+ fputs(set, stdout);
429
+ fwrite(line, col0, 1, stdout);
430
+ fputs(reset, stdout);
431
+ if (((i == len) || line[i] == '\n') && i != col0) {
432
+ /* The whole line was indent */
433
+ emit_line(ws, reset, line + col0, len - col0);
434
+ return;
435
+ }
436
+ i = col0;
437
+ if (need_highlight_leading_space) {
438
+ while (i < last_tab_in_indent) {
439
+ if (line[i] == ' ') {
440
+ fputs(ws, stdout);
441
+ putchar(' ');
442
+ fputs(reset, stdout);
443
+ }
444
+ else
445
+ putchar(line[i]);
446
+ i++;
447
+ }
448
+ }
449
+ tail = len - 1;
450
+ if (line[tail] == '\n' && i < tail)
451
+ tail--;
452
+ while (i < tail) {
453
+ if (!isspace(line[tail]))
454
+ break;
455
+ tail--;
456
+ }
457
+ if ((i < tail && line[tail + 1] != '\n')) {
458
+ /* This has whitespace between tail+1..len */
459
+ fputs(set, stdout);
460
+ fwrite(line + i, tail - i + 1, 1, stdout);
461
+ fputs(reset, stdout);
462
+ emit_line(ws, reset, line + tail + 1, len - tail - 1);
463
+ }
464
+ else
465
+ emit_line(set, reset, line + i, len - i);
466
+}
467
+
468
static void fn_out_consume(void *priv, char *line, unsigned long len)
469
{
470
int i;
471
+ int color;
472
struct emit_callback *ecbdata = priv;
473
const char *set = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
474
const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
@@ -403,45 +486,52 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
486
;
487
if (2 <= i && i < len && line[i] == ' ') {
488
ecbdata->nparents = i - 1;
406
- set = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO);
489
+ emit_line(diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO),
490
+ reset, line, len);
491
+ return;
492
}
408
- else if (len < ecbdata->nparents)
493
+
494
+ if (len < ecbdata->nparents) {
495
set = reset;
410
- else {
411
- int nparents = ecbdata->nparents;
412
- int color = DIFF_PLAIN;
413
- if (ecbdata->diff_words && nparents != 1)
414
- /* fall back to normal diff */
415
- free_diff_words_data(ecbdata);
416
- if (ecbdata->diff_words) {
417
- if (line[0] == '-') {
418
- diff_words_append(line, len,
419
- &ecbdata->diff_words->minus);
420
- return;
421
- } else if (line[0] == '+') {
422
- diff_words_append(line, len,
423
- &ecbdata->diff_words->plus);
424
- return;
425
- }
426
- if (ecbdata->diff_words->minus.text.size ||
427
- ecbdata->diff_words->plus.text.size)
428
- diff_words_show(ecbdata->diff_words);
429
- line++;
430
- len--;
431
- } else
432
- for (i = 0; i < nparents && len; i++) {
433
- if (line[i] == '-')
434
- color = DIFF_FILE_OLD;
435
- else if (line[i] == '+')
436
- color = DIFF_FILE_NEW;
437
- }
438
- set = diff_get_color(ecbdata->color_diff, color);
496
+ emit_line(reset, reset, line, len);
497
+ return;
498
}
440
- if (len > 0 && line[len-1] == '\n')
499
+
500
+ color = DIFF_PLAIN;
501
+ if (ecbdata->diff_words && ecbdata->nparents != 1)
502
+ /* fall back to normal diff */
503
+ free_diff_words_data(ecbdata);
504
+ if (ecbdata->diff_words) {
505
+ if (line[0] == '-') {
506
+ diff_words_append(line, len,
507
+ &ecbdata->diff_words->minus);
508
+ return;
509
+ } else if (line[0] == '+') {
510
+ diff_words_append(line, len,
511
+ &ecbdata->diff_words->plus);
512
+ return;
513
+ }
514
+ if (ecbdata->diff_words->minus.text.size ||
515
+ ecbdata->diff_words->plus.text.size)
516
+ diff_words_show(ecbdata->diff_words);
517
+ line++;
518
len--;
442
- fputs (set, stdout);
443
- fwrite (line, len, 1, stdout);
444
- puts (reset);
519
+ emit_line(set, reset, line, len);
520
+ return;
521
+ }
522
+ for (i = 0; i < ecbdata->nparents && len; i++) {
523
+ if (line[i] == '-')
524
+ color = DIFF_FILE_OLD;
525
+ else if (line[i] == '+')
526
+ color = DIFF_FILE_NEW;
527
+ }
528
+
529
+ if (color != DIFF_FILE_NEW) {
530
+ emit_line(diff_get_color(ecbdata->color_diff, color),
531
+ reset, line, len);
532
+ return;
533
+ }
534
+ emit_add_line(reset, ecbdata, line, len);
535
}
536
537
static char *pprint_rename(const char *a, const char *b)
diff.h
+1
@@ -86,6 +86,7 @@ enum color_diff {
86
DIFF_FILE_OLD = 4,
87
DIFF_FILE_NEW = 5,
88
DIFF_COMMIT = 6,
89
+ DIFF_WHITESPACE = 7,
90
};
91
const char *diff_get_color(int diff_use_color, enum color_diff ix);
92