Merge branch 'js/diff-color-check'
authorJunio C Hamano <junkio@cox.net>
Tue, 20 Feb 2007 02:30:59 +0000 (18:30 -0800)
committerJunio C Hamano <junkio@cox.net>
Tue, 20 Feb 2007 02:30:59 +0000 (18:30 -0800)
* js/diff-color-check:
diff --check: use colour

1  2 
diff.c
diff --combined diff.c
index 12c8b2b876fc09454204cac3d26a0e98413bfbe2,d5caf6d603d177f7e1a8c05179fabb4071e919ba..07589c3b1e87a8099413df55651c7a241d9e61a9
--- 1/diff.c
--- 2/diff.c
+++ b/diff.c
@@@ -207,18 -207,11 +207,18 @@@ static void emit_rewrite_diff(const cha
                              struct diff_filespec *two)
  {
        int lc_a, lc_b;
 +      const char *name_a_tab, *name_b_tab;
 +
 +      name_a_tab = strchr(name_a, ' ') ? "\t" : "";
 +      name_b_tab = strchr(name_b, ' ') ? "\t" : "";
 +
        diff_populate_filespec(one, 0);
        diff_populate_filespec(two, 0);
        lc_a = count_lines(one->data, one->size);
        lc_b = count_lines(two->data, two->size);
 -      printf("--- a/%s\n+++ b/%s\n@@ -", name_a, name_b);
 +      printf("--- a/%s%s\n+++ b/%s%s\n@@ -",
 +             name_a, name_a_tab,
 +             name_b, name_b_tab);
        print_line_count(lc_a);
        printf(" +");
        print_line_count(lc_b);
@@@ -405,22 -398,16 +405,16 @@@ static void emit_line(const char *set, 
        puts(reset);
  }
  
- static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len)
+ static void emit_line_with_ws(int nparents,
+               const char *set, const char *reset, const char *ws,
+               const char *line, int len)
  {
-       int col0 = ecbdata->nparents;
+       int col0 = nparents;
        int last_tab_in_indent = -1;
        int last_space_in_indent = -1;
        int i;
        int tail = len;
        int need_highlight_leading_space = 0;
-       const char *ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE);
-       const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_NEW);
-       if (!*ws) {
-               emit_line(set, reset, line, len);
-               return;
-       }
        /* The line is a newly added line.  Does it have funny leading
         * whitespaces?  In indent, SP should never precede a TAB.
         */
                emit_line(set, reset, line + i, len - i);
  }
  
+ static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len)
+ {
+       const char *ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE);
+       const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_NEW);
+       if (!*ws)
+               emit_line(set, reset, line, len);
+       else
+               emit_line_with_ws(ecbdata->nparents, set, reset, ws,
+                               line, len);
+ }
  static void fn_out_consume(void *priv, char *line, unsigned long len)
  {
        int i;
        const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
  
        if (ecbdata->label_path[0]) {
 -              printf("%s--- %s%s\n", set, ecbdata->label_path[0], reset);
 -              printf("%s+++ %s%s\n", set, ecbdata->label_path[1], reset);
 +              const char *name_a_tab, *name_b_tab;
 +
 +              name_a_tab = strchr(ecbdata->label_path[0], ' ') ? "\t" : "";
 +              name_b_tab = strchr(ecbdata->label_path[1], ' ') ? "\t" : "";
 +
 +              printf("%s--- %s%s%s\n",
 +                     set, ecbdata->label_path[0], reset, name_a_tab);
 +              printf("%s+++ %s%s%s\n",
 +                     set, ecbdata->label_path[1], reset, name_b_tab);
                ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
        }
  
@@@ -884,30 -876,44 +890,44 @@@ static void show_numstat(struct diffsta
  struct checkdiff_t {
        struct xdiff_emit_state xm;
        const char *filename;
-       int lineno;
+       int lineno, color_diff;
  };
  
  static void checkdiff_consume(void *priv, char *line, unsigned long len)
  {
        struct checkdiff_t *data = priv;
+       const char *ws = diff_get_color(data->color_diff, DIFF_WHITESPACE);
+       const char *reset = diff_get_color(data->color_diff, DIFF_RESET);
+       const char *set = diff_get_color(data->color_diff, DIFF_FILE_NEW);
  
        if (line[0] == '+') {
-               int i, spaces = 0;
+               int i, spaces = 0, space_before_tab = 0, white_space_at_end = 0;
  
                /* check space before tab */
                for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++)
                        if (line[i] == ' ')
                                spaces++;
                if (line[i - 1] == '\t' && spaces)
-                       printf("%s:%d: space before tab:%.*s\n",
-                               data->filename, data->lineno, (int)len, line);
+                       space_before_tab = 1;
  
                /* check white space at line end */
                if (line[len - 1] == '\n')
                        len--;
                if (isspace(line[len - 1]))
-                       printf("%s:%d: white space at end: %.*s\n",
-                               data->filename, data->lineno, (int)len, line);
+                       white_space_at_end = 1;
+               if (space_before_tab || white_space_at_end) {
+                       printf("%s:%d: %s", data->filename, data->lineno, ws);
+                       if (space_before_tab) {
+                               printf("space before tab");
+                               if (white_space_at_end)
+                                       putchar(',');
+                       }
+                       if (white_space_at_end)
+                               printf("white space at end");
+                       printf(":%s ", reset);
+                       emit_line_with_ws(1, set, reset, ws, line, len);
+               }
  
                data->lineno++;
        } else if (line[0] == ' ')
@@@ -1165,7 -1171,7 +1185,7 @@@ static void builtin_diffstat(const cha
  
  static void builtin_checkdiff(const char *name_a, const char *name_b,
                             struct diff_filespec *one,
-                            struct diff_filespec *two)
+                            struct diff_filespec *two, struct diff_options *o)
  {
        mmfile_t mf1, mf2;
        struct checkdiff_t data;
        data.xm.consume = checkdiff_consume;
        data.filename = name_b ? name_b : name_a;
        data.lineno = 0;
+       data.color_diff = o->color_diff;
  
        if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
                die("unable to read files to diff");
@@@ -1787,7 -1794,7 +1808,7 @@@ static void run_checkdiff(struct diff_f
        diff_fill_sha1_info(p->one);
        diff_fill_sha1_info(p->two);
  
-       builtin_checkdiff(name, other, p->one, p->two);
+       builtin_checkdiff(name, other, p->one, p->two, o);
  }
  
  void diff_setup(struct diff_options *options)
@@@ -2038,8 -2045,6 +2059,8 @@@ int diff_opt_parse(struct diff_options 
                options->xdl_opts |= XDF_IGNORE_WHITESPACE;
        else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
                options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
 +      else if (!strcmp(arg, "--ignore-space-at-eol"))
 +              options->xdl_opts |= XDF_IGNORE_WHITESPACE_AT_EOL;
        else if (!strcmp(arg, "--color-words"))
                options->color_diff = options->color_diff_words = 1;
        else if (!strcmp(arg, "--no-renames"))