diff --check: use colour
Reuse the colour handling of the regular diff. Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
Johannes Schindelin committed
Feb 18, 2007 at 17:27 UTC
c5a8c3ecd7fcec68b05b38640d4eac4e1cc7f50f
1 file changed
+39
-18
diff.c
+39
-18
@@ -398,22 +398,16 @@ static void emit_line(const char *set, const char *reset, const char *line, int
398
puts(reset);
399
}
400
401
-static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len)
401
+static void emit_line_with_ws(int nparents,
402
+ const char *set, const char *reset, const char *ws,
403
+ const char *line, int len)
404
{
403
- int col0 = ecbdata->nparents;
405
+ int col0 = nparents;
406
int last_tab_in_indent = -1;
407
int last_space_in_indent = -1;
408
int i;
409
int tail = len;
410
int need_highlight_leading_space = 0;
409
- const char *ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE);
410
- const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_NEW);
411
-
412
- if (!*ws) {
413
- emit_line(set, reset, line, len);
414
- return;
415
- }
416
-
411
/* The line is a newly added line. Does it have funny leading
412
* whitespaces? In indent, SP should never precede a TAB.
413
*/
@@ -468,6 +462,18 @@ static void emit_add_line(const char *reset, struct emit_callback *ecbdata, cons
462
emit_line(set, reset, line + i, len - i);
463
}
464
465
+static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len)
466
+{
467
+ const char *ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE);
468
+ const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_NEW);
469
+
470
+ if (!*ws)
471
+ emit_line(set, reset, line, len);
472
+ else
473
+ emit_line_with_ws(ecbdata->nparents, set, reset, ws,
474
+ line, len);
475
+}
476
+
477
static void fn_out_consume(void *priv, char *line, unsigned long len)
478
{
479
int i;
@@ -870,30 +876,44 @@ static void show_numstat(struct diffstat_t* data, struct diff_options *options)
876
struct checkdiff_t {
877
struct xdiff_emit_state xm;
878
const char *filename;
873
- int lineno;
879
+ int lineno, color_diff;
880
};
881
882
static void checkdiff_consume(void *priv, char *line, unsigned long len)
883
{
884
struct checkdiff_t *data = priv;
885
+ const char *ws = diff_get_color(data->color_diff, DIFF_WHITESPACE);
886
+ const char *reset = diff_get_color(data->color_diff, DIFF_RESET);
887
+ const char *set = diff_get_color(data->color_diff, DIFF_FILE_NEW);
888
889
if (line[0] == '+') {
881
- int i, spaces = 0;
890
+ int i, spaces = 0, space_before_tab = 0, white_space_at_end = 0;
891
892
/* check space before tab */
893
for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++)
894
if (line[i] == ' ')
895
spaces++;
896
if (line[i - 1] == '\t' && spaces)
888
- printf("%s:%d: space before tab:%.*s\n",
889
- data->filename, data->lineno, (int)len, line);
897
+ space_before_tab = 1;
898
899
/* check white space at line end */
900
if (line[len - 1] == '\n')
901
len--;
902
if (isspace(line[len - 1]))
895
- printf("%s:%d: white space at end: %.*s\n",
896
- data->filename, data->lineno, (int)len, line);
903
+ white_space_at_end = 1;
904
+
905
+ if (space_before_tab || white_space_at_end) {
906
+ printf("%s:%d: %s", data->filename, data->lineno, ws);
907
+ if (space_before_tab) {
908
+ printf("space before tab");
909
+ if (white_space_at_end)
910
+ putchar(',');
911
+ }
912
+ if (white_space_at_end)
913
+ printf("white space at end");
914
+ printf(":%s ", reset);
915
+ emit_line_with_ws(1, set, reset, ws, line, len);
916
+ }
917
918
data->lineno++;
919
} else if (line[0] == ' ')
@@ -1151,7 +1171,7 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
1171
1172
static void builtin_checkdiff(const char *name_a, const char *name_b,
1173
struct diff_filespec *one,
1154
- struct diff_filespec *two)
1174
+ struct diff_filespec *two, struct diff_options *o)
1175
{
1176
mmfile_t mf1, mf2;
1177
struct checkdiff_t data;
@@ -1163,6 +1183,7 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
1183
data.xm.consume = checkdiff_consume;
1184
data.filename = name_b ? name_b : name_a;
1185
data.lineno = 0;
1186
+ data.color_diff = o->color_diff;
1187
1188
if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1189
die("unable to read files to diff");
@@ -1773,7 +1794,7 @@ static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
1794
diff_fill_sha1_info(p->one);
1795
diff_fill_sha1_info(p->two);
1796
1776
- builtin_checkdiff(name, other, p->one, p->two);
1797
+ builtin_checkdiff(name, other, p->one, p->two, o);
1798
}
1799
1800
void diff_setup(struct diff_options *options)