git-svn: t/t91??-*: optimize the tests a bit
[gitweb.git] / diff.c
diff --git a/diff.c b/diff.c
index 3fd7a5220d283ef0e575cfb0d436c029977c632e..f14288bb8a100c43c6709f658b9a9ca44832fd7f 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -1,9 +1,6 @@
 /*
  * Copyright (C) 2005 Junio C Hamano
  */
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <signal.h>
 #include "cache.h"
 #include "quote.h"
 #include "diff.h"
@@ -12,6 +9,12 @@
 #include "xdiff-interface.h"
 #include "color.h"
 
+#ifdef NO_FAST_WORKING_DIRECTORY
+#define FAST_WORKING_DIRECTORY 0
+#else
+#define FAST_WORKING_DIRECTORY 1
+#endif
+
 static int use_size_cache;
 
 static int diff_detect_rename_default;
@@ -20,12 +23,13 @@ static int diff_use_color_default;
 
 static char diff_colors[][COLOR_MAXLEN] = {
        "\033[m",       /* reset */
-       "",             /* normal */
-       "\033[1m",      /* bold */
-       "\033[36m",     /* cyan */
-       "\033[31m",     /* red */
-       "\033[32m",     /* green */
-       "\033[33m"      /* yellow */
+       "",             /* PLAIN (normal) */
+       "\033[1m",      /* METAINFO (bold) */
+       "\033[36m",     /* FRAGINFO (cyan) */
+       "\033[31m",     /* OLD (red) */
+       "\033[32m",     /* NEW (green) */
+       "\033[33m",     /* COMMIT (yellow) */
+       "\033[41m",     /* WHITESPACE (red background) */
 };
 
 static int parse_diff_color_slot(const char *var, int ofs)
@@ -42,6 +46,8 @@ static int parse_diff_color_slot(const char *var, int ofs)
                return DIFF_FILE_NEW;
        if (!strcasecmp(var+ofs, "commit"))
                return DIFF_COMMIT;
+       if (!strcasecmp(var+ofs, "whitespace"))
+               return DIFF_WHITESPACE;
        die("bad config variable '%s'", var);
 }
 
@@ -57,7 +63,7 @@ int git_diff_ui_config(const char *var, const char *value)
                diff_rename_limit_default = git_config_int(var, value);
                return 0;
        }
-       if (!strcmp(var, "diff.color")) {
+       if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
                diff_use_color_default = git_config_colorbool(var, value);
                return 0;
        }
@@ -71,7 +77,7 @@ int git_diff_ui_config(const char *var, const char *value)
                        diff_detect_rename_default = DIFF_DETECT_RENAME;
                return 0;
        }
-       if (!strncmp(var, "diff.color.", 11)) {
+       if (!strncmp(var, "diff.color.", 11) || !strncmp(var, "color.diff.", 11)) {
                int slot = parse_diff_color_slot(var, 11);
                color_parse(value, var, diff_colors[slot]);
                return 0;
@@ -205,7 +211,7 @@ static void emit_rewrite_diff(const char *name_a,
        diff_populate_filespec(two, 0);
        lc_a = count_lines(one->data, one->size);
        lc_b = count_lines(two->data, two->size);
-       printf("--- %s\n+++ %s\n@@ -", name_a, name_b);
+       printf("--- a/%s\n+++ b/%s\n@@ -", name_a, name_b);
        print_line_count(lc_a);
        printf(" +");
        print_line_count(lc_b);
@@ -383,9 +389,89 @@ const char *diff_get_color(int diff_use_color, enum color_diff ix)
        return "";
 }
 
+static void emit_line(const char *set, const char *reset, const char *line, int len)
+{
+       if (len > 0 && line[len-1] == '\n')
+               len--;
+       fputs(set, stdout);
+       fwrite(line, len, 1, stdout);
+       puts(reset);
+}
+
+static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len)
+{
+       int col0 = ecbdata->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.
+        */
+       for (i = col0; i < len; i++) {
+               if (line[i] == '\t') {
+                       last_tab_in_indent = i;
+                       if (0 <= last_space_in_indent)
+                               need_highlight_leading_space = 1;
+               }
+               else if (line[i] == ' ')
+                       last_space_in_indent = i;
+               else
+                       break;
+       }
+       fputs(set, stdout);
+       fwrite(line, col0, 1, stdout);
+       fputs(reset, stdout);
+       if (((i == len) || line[i] == '\n') && i != col0) {
+               /* The whole line was indent */
+               emit_line(ws, reset, line + col0, len - col0);
+               return;
+       }
+       i = col0;
+       if (need_highlight_leading_space) {
+               while (i < last_tab_in_indent) {
+                       if (line[i] == ' ') {
+                               fputs(ws, stdout);
+                               putchar(' ');
+                               fputs(reset, stdout);
+                       }
+                       else
+                               putchar(line[i]);
+                       i++;
+               }
+       }
+       tail = len - 1;
+       if (line[tail] == '\n' && i < tail)
+               tail--;
+       while (i < tail) {
+               if (!isspace(line[tail]))
+                       break;
+               tail--;
+       }
+       if ((i < tail && line[tail + 1] != '\n')) {
+               /* This has whitespace between tail+1..len */
+               fputs(set, stdout);
+               fwrite(line + i, tail - i + 1, 1, stdout);
+               fputs(reset, stdout);
+               emit_line(ws, reset, line + tail + 1, len - tail - 1);
+       }
+       else
+               emit_line(set, reset, line + i, len - i);
+}
+
 static void fn_out_consume(void *priv, char *line, unsigned long len)
 {
        int i;
+       int color;
        struct emit_callback *ecbdata = priv;
        const char *set = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
        const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
@@ -403,45 +489,52 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
                ;
        if (2 <= i && i < len && line[i] == ' ') {
                ecbdata->nparents = i - 1;
-               set = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO);
+               emit_line(diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO),
+                         reset, line, len);
+               return;
        }
-       else if (len < ecbdata->nparents)
+
+       if (len < ecbdata->nparents) {
                set = reset;
-       else {
-               int nparents = ecbdata->nparents;
-               int color = DIFF_PLAIN;
-               if (ecbdata->diff_words && nparents != 1)
-                       /* fall back to normal diff */
-                       free_diff_words_data(ecbdata);
-               if (ecbdata->diff_words) {
-                       if (line[0] == '-') {
-                               diff_words_append(line, len,
-                                               &ecbdata->diff_words->minus);
-                               return;
-                       } else if (line[0] == '+') {
-                               diff_words_append(line, len,
-                                               &ecbdata->diff_words->plus);
-                               return;
-                       }
-                       if (ecbdata->diff_words->minus.text.size ||
-                                       ecbdata->diff_words->plus.text.size)
-                               diff_words_show(ecbdata->diff_words);
-                       line++;
-                       len--;
-               } else
-                       for (i = 0; i < nparents && len; i++) {
-                               if (line[i] == '-')
-                                       color = DIFF_FILE_OLD;
-                               else if (line[i] == '+')
-                                       color = DIFF_FILE_NEW;
-                       }
-               set = diff_get_color(ecbdata->color_diff, color);
+               emit_line(reset, reset, line, len);
+               return;
        }
-       if (len > 0 && line[len-1] == '\n')
+
+       color = DIFF_PLAIN;
+       if (ecbdata->diff_words && ecbdata->nparents != 1)
+               /* fall back to normal diff */
+               free_diff_words_data(ecbdata);
+       if (ecbdata->diff_words) {
+               if (line[0] == '-') {
+                       diff_words_append(line, len,
+                                         &ecbdata->diff_words->minus);
+                       return;
+               } else if (line[0] == '+') {
+                       diff_words_append(line, len,
+                                         &ecbdata->diff_words->plus);
+                       return;
+               }
+               if (ecbdata->diff_words->minus.text.size ||
+                   ecbdata->diff_words->plus.text.size)
+                       diff_words_show(ecbdata->diff_words);
+               line++;
                len--;
-       fputs (set, stdout);
-       fwrite (line, len, 1, stdout);
-       puts (reset);
+               emit_line(set, reset, line, len);
+               return;
+       }
+       for (i = 0; i < ecbdata->nparents && len; i++) {
+               if (line[i] == '-')
+                       color = DIFF_FILE_OLD;
+               else if (line[i] == '+')
+                       color = DIFF_FILE_NEW;
+       }
+
+       if (color != DIFF_FILE_NEW) {
+               emit_line(diff_get_color(ecbdata->color_diff, color),
+                         reset, line, len);
+               return;
+       }
+       emit_add_line(reset, ecbdata, line, len);
 }
 
 static char *pprint_rename(const char *a, const char *b)
@@ -550,9 +643,12 @@ const char mime_boundary_leader[] = "------------";
 static int scale_linear(int it, int width, int max_change)
 {
        /*
-        * round(width * it / max_change);
+        * make sure that at least one '-' is printed if there were deletions,
+        * and likewise for '+'.
         */
-       return (it * width * 2 + max_change) / (max_change * 2);
+       if (max_change < 2)
+               return it;
+       return ((it - 1) * (width - 1) + max_change - 1) / (max_change - 1);
 }
 
 static void show_name(const char *prefix, const char *name, int len,
@@ -684,9 +780,9 @@ static void show_stats(struct diffstat_t* data, struct diff_options *options)
                dels += del;
 
                if (width <= max_change) {
-                       total = scale_linear(total, width, max_change);
                        add = scale_linear(add, width, max_change);
-                       del = total - add;
+                       del = scale_linear(del, width, max_change);
+                       total = add + del;
                }
                show_name(prefix, name, len, reset, set);
                printf("%5d ", added + deleted);
@@ -702,6 +798,55 @@ static void show_stats(struct diffstat_t* data, struct diff_options *options)
               set, total_files, adds, dels, reset);
 }
 
+static void show_shortstats(struct diffstat_t* data)
+{
+       int i, adds = 0, dels = 0, total_files = data->nr;
+
+       if (data->nr == 0)
+               return;
+
+       for (i = 0; i < data->nr; i++) {
+               if (!data->files[i]->is_binary &&
+                   !data->files[i]->is_unmerged) {
+                       int added = data->files[i]->added;
+                       int deleted= data->files[i]->deleted;
+                       if (!data->files[i]->is_renamed &&
+                           (added + deleted == 0)) {
+                               total_files--;
+                       } else {
+                               adds += added;
+                               dels += deleted;
+                       }
+               }
+               free(data->files[i]->name);
+               free(data->files[i]);
+       }
+       free(data->files);
+
+       printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
+              total_files, adds, dels);
+}
+
+static void show_numstat(struct diffstat_t* data, struct diff_options *options)
+{
+       int i;
+
+       for (i = 0; i < data->nr; i++) {
+               struct diffstat_file *file = data->files[i];
+
+               if (file->is_binary)
+                       printf("-\t-\t");
+               else
+                       printf("%d\t%d\t", file->added, file->deleted);
+               if (options->line_termination &&
+                   quote_c_style(file->name, NULL, NULL, 0))
+                       quote_c_style(file->name, NULL, stdout, 0);
+               else
+                       fputs(file->name, stdout);
+               putchar(options->line_termination);
+       }
+}
+
 struct checkdiff_t {
        struct xdiff_emit_state xm;
        const char *filename;
@@ -715,8 +860,6 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
        if (line[0] == '+') {
                int i, spaces = 0;
 
-               data->lineno++;
-
                /* check space before tab */
                for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++)
                        if (line[i] == ' ')
@@ -731,6 +874,8 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
                if (isspace(line[len - 1]))
                        printf("%s:%d: white space at end: %.*s\n",
                                data->filename, data->lineno, (int)len, line);
+
+               data->lineno++;
        } else if (line[0] == ' ')
                data->lineno++;
        else if (line[0] == '@') {
@@ -1045,7 +1190,7 @@ void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
  * the work tree has that object contents, return true, so that
  * prepare_temp_file() does not have to inflate and extract.
  */
-static int work_tree_matches(const char *name, const unsigned char *sha1)
+static int reuse_worktree_file(const char *name, const unsigned char *sha1, int want_file)
 {
        struct cache_entry *ce;
        struct stat st;
@@ -1066,6 +1211,18 @@ static int work_tree_matches(const char *name, const unsigned char *sha1)
        if (!active_cache)
                return 0;
 
+       /* We want to avoid the working directory if our caller
+        * doesn't need the data in a normal file, this system
+        * is rather slow with its stat/open/mmap/close syscalls,
+        * and the object is contained in a pack file.  The pack
+        * is probably already open and will be faster to obtain
+        * the data through than the working directory.  Loose
+        * objects however would tend to be slower as they need
+        * to be individually opened and inflated.
+        */
+       if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(sha1, NULL))
+               return 0;
+
        len = strlen(name);
        pos = cache_name_pos(name, len);
        if (pos < 0)
@@ -1152,7 +1309,7 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
        if (s->data)
                return err;
        if (!s->sha1_valid ||
-           work_tree_matches(s->path, s->sha1)) {
+           reuse_worktree_file(s->path, s->sha1, 0)) {
                struct stat st;
                int fd;
                if (lstat(s->path, &st) < 0) {
@@ -1259,7 +1416,7 @@ static void prepare_temp_file(const char *name,
        }
 
        if (!one->sha1_valid ||
-           work_tree_matches(name, one->sha1)) {
+           reuse_worktree_file(name, one->sha1, 1)) {
                struct stat st;
                if (lstat(name, &st) < 0) {
                        if (errno == ENOENT)
@@ -1638,7 +1795,9 @@ int diff_setup_done(struct diff_options *options)
                                      DIFF_FORMAT_CHECKDIFF |
                                      DIFF_FORMAT_NO_OUTPUT))
                options->output_format &= ~(DIFF_FORMAT_RAW |
+                                           DIFF_FORMAT_NUMSTAT |
                                            DIFF_FORMAT_DIFFSTAT |
+                                           DIFF_FORMAT_SHORTSTAT |
                                            DIFF_FORMAT_SUMMARY |
                                            DIFF_FORMAT_PATCH);
 
@@ -1647,7 +1806,10 @@ int diff_setup_done(struct diff_options *options)
         * recursive bits for other formats here.
         */
        if (options->output_format & (DIFF_FORMAT_PATCH |
+                                     DIFF_FORMAT_NUMSTAT |
                                      DIFF_FORMAT_DIFFSTAT |
+                                     DIFF_FORMAT_SHORTSTAT |
+                                     DIFF_FORMAT_SUMMARY |
                                      DIFF_FORMAT_CHECKDIFF))
                options->recursive = 1;
        /*
@@ -1735,15 +1897,38 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
        else if (!strcmp(arg, "--patch-with-raw")) {
                options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
        }
-       else if (!strcmp(arg, "--stat"))
-               options->output_format |= DIFF_FORMAT_DIFFSTAT;
-       else if (!strncmp(arg, "--stat-width=", 13)) {
-               options->stat_width = strtoul(arg + 13, NULL, 10);
-               options->output_format |= DIFF_FORMAT_DIFFSTAT;
+       else if (!strcmp(arg, "--numstat")) {
+               options->output_format |= DIFF_FORMAT_NUMSTAT;
        }
-       else if (!strncmp(arg, "--stat-name-width=", 18)) {
-               options->stat_name_width = strtoul(arg + 18, NULL, 10);
+       else if (!strcmp(arg, "--shortstat")) {
+               options->output_format |= DIFF_FORMAT_SHORTSTAT;
+       }
+       else if (!strncmp(arg, "--stat", 6)) {
+               char *end;
+               int width = options->stat_width;
+               int name_width = options->stat_name_width;
+               arg += 6;
+               end = (char *)arg;
+
+               switch (*arg) {
+               case '-':
+                       if (!strncmp(arg, "-width=", 7))
+                               width = strtoul(arg + 7, &end, 10);
+                       else if (!strncmp(arg, "-name-width=", 12))
+                               name_width = strtoul(arg + 12, &end, 10);
+                       break;
+               case '=':
+                       width = strtoul(arg+1, &end, 10);
+                       if (*end == ',')
+                               name_width = strtoul(end+1, &end, 10);
+               }
+
+               /* Important! This checks all the error cases! */
+               if (*end)
+                       return 0;
                options->output_format |= DIFF_FORMAT_DIFFSTAT;
+               options->stat_name_width = name_width;
+               options->stat_width = width;
        }
        else if (!strcmp(arg, "--check"))
                options->output_format |= DIFF_FORMAT_CHECKDIFF;
@@ -2492,7 +2677,7 @@ void diff_flush(struct diff_options *options)
                separator++;
        }
 
-       if (output_format & DIFF_FORMAT_DIFFSTAT) {
+       if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT)) {
                struct diffstat_t diffstat;
 
                memset(&diffstat, 0, sizeof(struct diffstat_t));
@@ -2502,7 +2687,12 @@ void diff_flush(struct diff_options *options)
                        if (check_pair_status(p))
                                diff_flush_stat(p, options, &diffstat);
                }
-               show_stats(&diffstat, options);
+               if (output_format & DIFF_FORMAT_NUMSTAT)
+                       show_numstat(&diffstat, options);
+               if (output_format & DIFF_FORMAT_DIFFSTAT)
+                       show_stats(&diffstat, options);
+               else if (output_format & DIFF_FORMAT_SHORTSTAT)
+                       show_shortstats(&diffstat);
                separator++;
        }