Code

Use strbuf API in apply, blame, commit-tree and diff
[git.git] / diff.c
diff --git a/diff.c b/diff.c
index a5fc56bdad5d96b2a4e0e1140206b8367a257867..c054b234b877952501dcf05b2ea312ae77441042 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -9,6 +9,7 @@
 #include "xdiff-interface.h"
 #include "color.h"
 #include "attr.h"
+#include "strbuf.h"
 
 #ifdef NO_FAST_WORKING_DIRECTORY
 #define FAST_WORKING_DIRECTORY 0
@@ -19,6 +20,7 @@
 static int diff_detect_rename_default;
 static int diff_rename_limit_default = -1;
 static int diff_use_color_default;
+int diff_auto_refresh_index = 1;
 
 static char diff_colors[][COLOR_MAXLEN] = {
        "\033[m",       /* reset */
@@ -166,6 +168,10 @@ int git_diff_ui_config(const char *var, const char *value)
                        diff_detect_rename_default = DIFF_DETECT_RENAME;
                return 0;
        }
+       if (!strcmp(var, "diff.autorefreshindex")) {
+               diff_auto_refresh_index = git_config_bool(var, value);
+               return 0;
+       }
        if (!prefixcmp(var, "diff.")) {
                const char *ep = strrchr(var, '.');
 
@@ -1540,26 +1546,16 @@ static int reuse_worktree_file(const char *name, const unsigned char *sha1, int
 
 static int populate_from_stdin(struct diff_filespec *s)
 {
-#define INCREMENT 1024
-       char *buf;
-       unsigned long size;
-       ssize_t got;
-
-       size = 0;
-       buf = NULL;
-       while (1) {
-               buf = xrealloc(buf, size + INCREMENT);
-               got = xread(0, buf + size, INCREMENT);
-               if (!got)
-                       break; /* EOF */
-               if (got < 0)
-                       return error("error while reading from stdin %s",
+       struct strbuf buf;
+
+       strbuf_init(&buf);
+       if (strbuf_read(&buf, 0) < 0)
+               return error("error while reading from stdin %s",
                                     strerror(errno));
-               size += got;
-       }
+
        s->should_munmap = 0;
-       s->data = buf;
-       s->size = size;
+       s->size = buf.len;
+       s->data = strbuf_detach(&buf);
        s->should_free = 1;
        return 0;
 }
@@ -2919,10 +2915,6 @@ static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
                                fill_mmfile(&mf2, p->two) < 0)
                        return error("unable to read files to diff");
 
-               /* Maybe hash p->two? into the patch id? */
-               if (diff_filespec_is_binary(p->two))
-                       continue;
-
                len1 = remove_space(p->one->path, strlen(p->one->path));
                len2 = remove_space(p->two->path, strlen(p->two->path));
                if (p->one->mode == 0)
@@ -3143,11 +3135,64 @@ static void diffcore_apply_filter(const char *filter)
        *q = outq;
 }
 
+static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
+{
+       int i;
+       struct diff_queue_struct *q = &diff_queued_diff;
+       struct diff_queue_struct outq;
+       outq.queue = NULL;
+       outq.nr = outq.alloc = 0;
+
+       for (i = 0; i < q->nr; i++) {
+               struct diff_filepair *p = q->queue[i];
+
+               /*
+                * 1. Entries that come from stat info dirtyness
+                *    always have both sides (iow, not create/delete),
+                *    one side of the object name is unknown, with
+                *    the same mode and size.  Keep the ones that
+                *    do not match these criteria.  They have real
+                *    differences.
+                *
+                * 2. At this point, the file is known to be modified,
+                *    with the same mode and size, and the object
+                *    name of one side is unknown.  Need to inspect
+                *    the identical contents.
+                */
+               if (!DIFF_FILE_VALID(p->one) || /* (1) */
+                   !DIFF_FILE_VALID(p->two) ||
+                   (p->one->sha1_valid && p->two->sha1_valid) ||
+                   (p->one->mode != p->two->mode) ||
+                   diff_populate_filespec(p->one, 1) ||
+                   diff_populate_filespec(p->two, 1) ||
+                   (p->one->size != p->two->size) ||
+
+                   diff_populate_filespec(p->one, 0) || /* (2) */
+                   diff_populate_filespec(p->two, 0) ||
+                   memcmp(p->one->data, p->two->data, p->one->size))
+                       diff_q(&outq, p);
+               else {
+                       /*
+                        * The caller can subtract 1 from skip_stat_unmatch
+                        * to determine how many paths were dirty only
+                        * due to stat info mismatch.
+                        */
+                       if (!diffopt->no_index)
+                               diffopt->skip_stat_unmatch++;
+                       diff_free_filepair(p);
+               }
+       }
+       free(q->queue);
+       *q = outq;
+}
+
 void diffcore_std(struct diff_options *options)
 {
        if (options->quiet)
                return;
 
+       if (options->skip_stat_unmatch && !options->find_copies_harder)
+               diffcore_skip_stat_unmatch(options);
        if (options->break_opt != -1)
                diffcore_break(options->break_opt);
        if (options->detect_rename)