[PATCH] Rename/copy detection fix.
[git.git] / diff.c
diff --git a/diff.c b/diff.c
index 85505fa..ababcaa 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -4,7 +4,6 @@
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <signal.h>
-#include <limits.h>
 #include "cache.h"
 #include "diff.h"
 #include "diffcore.h"
@@ -13,9 +12,9 @@ static const char *diff_opts = "-pu";
 static unsigned char null_sha1[20] = { 0, };
 
 static int reverse_diff;
-static int diff_raw_output = -1;
-static const char **pathspec;
-static int speccnt;
+static int generate_patch;
+static int line_termination = '\n';
+static int inter_name_termination = '\t';
 
 static const char *external_diff(void)
 {
@@ -163,20 +162,23 @@ struct diff_filespec *alloc_filespec(const char *path)
        struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
        spec->path = (char *)(spec + 1);
        strcpy(spec->path, path);
-       spec->should_free = spec->should_munmap = spec->file_valid = 0;
+       spec->should_free = spec->should_munmap = 0;
        spec->xfrm_flags = 0;
        spec->size = 0;
-       spec->data = 0;
+       spec->data = NULL;
+       spec->mode = 0;
+       memset(spec->sha1, 0, 20);
        return spec;
 }
 
 void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
                   unsigned short mode)
 {
-       spec->mode = mode;
-       memcpy(spec->sha1, sha1, 20);
-       spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
-       spec->file_valid = 1;
+       if (mode) { /* just playing defensive */
+               spec->mode = mode;
+               memcpy(spec->sha1, sha1, 20);
+               spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
+       }
 }
 
 /*
@@ -231,7 +233,7 @@ static int work_tree_matches(const char *name, const unsigned char *sha1)
 int diff_populate_filespec(struct diff_filespec *s)
 {
        int err = 0;
-       if (!s->file_valid)
+       if (!DIFF_FILE_VALID(s))
                die("internal error: asking to populate invalid file.");
        if (S_ISDIR(s->mode))
                return -1;
@@ -288,7 +290,7 @@ void diff_free_filespec_data(struct diff_filespec *s)
        else if (s->should_munmap)
                munmap(s->data, s->size);
        s->should_free = s->should_munmap = 0;
-       s->data = 0;
+       s->data = NULL;
 }
 
 static void prep_temp_blob(struct diff_tempfile *temp,
@@ -316,7 +318,7 @@ static void prepare_temp_file(const char *name,
                              struct diff_tempfile *temp,
                              struct diff_filespec *one)
 {
-       if (!one->file_valid) {
+       if (!DIFF_FILE_VALID(one)) {
        not_a_valid_file:
                /* A '-' entry produces this for file-2, and
                 * a '+' entry produces this for file-1.
@@ -385,25 +387,6 @@ static void remove_tempfile_on_signal(int signo)
        remove_tempfile();
 }
 
-static int matches_pathspec(const char *name)
-{
-       int i;
-       int namelen;
-
-       if (speccnt == 0)
-               return 1;
-
-       namelen = strlen(name);
-       for (i = 0; i < speccnt; i++) {
-               int speclen = strlen(pathspec[i]);
-               if (! strncmp(pathspec[i], name, speclen) &&
-                   speclen <= namelen &&
-                   (name[speclen] == 0 || name[speclen] == '/'))
-                       return 1;
-       }
-       return 0;
-}
-
 /* An external diff command takes:
  *
  * diff-cmd name infile1 infile1-sha1 infile1-mode \
@@ -421,9 +404,6 @@ static void run_external_diff(const char *name,
        int status;
        static int atexit_asked = 0;
 
-       if (!matches_pathspec(name) && (!other || !matches_pathspec(other)))
-               return;
-
        if (one && two) {
                prepare_temp_file(name, &temp[0], one);
                prepare_temp_file(other ? : name, &temp[1], two);
@@ -458,7 +438,7 @@ static void run_external_diff(const char *name,
                                        *arg++ = other;
                                        *arg++ = xfrm_msg;
                                }
-                               *arg = 0;
+                               *arg = NULL;
                                execvp(pgm, (char *const*) exec_arg);
                        }
                        else
@@ -491,116 +471,69 @@ static void run_external_diff(const char *name,
        remove_tempfile();
 }
 
-int diff_scoreopt_parse(const char *opt)
-{
-       int diglen, num, scale, i;
-       if (opt[0] != '-' || (opt[1] != 'M' && opt[1] != 'C'))
-               return -1; /* that is not a -M nor -C option */
-       diglen = strspn(opt+2, "0123456789");
-       if (diglen == 0 || strlen(opt+2) != diglen)
-               return 0; /* use default */
-       sscanf(opt+2, "%d", &num);
-       for (i = 0, scale = 1; i < diglen; i++)
-               scale *= 10;
-
-       /* user says num divided by scale and we say internally that
-        * is MAX_SCORE * num / scale.
-        */
-       return MAX_SCORE * num / scale;
-}
-
-void diff_setup(int reverse_diff_, int diff_raw_output_)
+void diff_setup(int reverse_diff_)
 {
        reverse_diff = reverse_diff_;
-       diff_raw_output = diff_raw_output_;
 }
 
 struct diff_queue_struct diff_queued_diff;
 
-struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
-                                 struct diff_filespec *one,
-                                 struct diff_filespec *two)
+void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
 {
-       struct diff_filepair *dp = xmalloc(sizeof(*dp));
-       dp->one = one;
-       dp->two = two;
-       dp->xfrm_msg = 0;
-       dp->orig_order = queue->nr;
-       dp->xfrm_work = 0;
        if (queue->alloc <= queue->nr) {
                queue->alloc = alloc_nr(queue->alloc);
                queue->queue = xrealloc(queue->queue,
-                                      sizeof(dp) * queue->alloc);
+                                       sizeof(dp) * queue->alloc);
        }
        queue->queue[queue->nr++] = dp;
-       return dp;
 }
 
-static const char *git_object_type(unsigned mode)
+struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
+                                struct diff_filespec *one,
+                                struct diff_filespec *two)
 {
-       return S_ISDIR(mode) ? "tree" : "blob";
+       struct diff_filepair *dp = xmalloc(sizeof(*dp));
+       dp->one = one;
+       dp->two = two;
+       dp->score = 0;
+       dp->orig_order = queue->nr;
+       dp->rename_rank = 0;
+       diff_q(queue, dp);
+       return dp;
 }
 
 static void diff_flush_raw(struct diff_filepair *p)
 {
-       struct diff_filespec *it;
-       int addremove;
-
-       /* raw output does not have a way to express rename nor copy */
-       if (strcmp(p->one->path, p->two->path))
-               return;
-
-       if (p->one->file_valid && p->two->file_valid) {
-               char hex[41];
-               strcpy(hex, sha1_to_hex(p->one->sha1));
-               printf("*%06o->%06o %s %s->%s %s%c",
-                      p->one->mode, p->two->mode,
-                      git_object_type(p->one->mode),
-                      hex, sha1_to_hex(p->two->sha1),
-                      p->one->path, diff_raw_output);
+       if (DIFF_PAIR_UNMERGED(p)) {
+               printf("U %s%c", p->one->path, line_termination);
                return;
        }
-
-       if (p->one->file_valid) {
-               it = p->one;
-               addremove = '-';
-       } else {
-               it = p->two;
-               addremove = '+';
-       }
-
-       printf("%c%06o %s %s %s%c",
-              addremove,
-              it->mode, git_object_type(it->mode),
-              sha1_to_hex(it->sha1), it->path, diff_raw_output);
-}
-
-static void diff_flush_patch(struct diff_filepair *p)
-{
-       const char *name, *other;
-
-       name = p->one->path;
-       other = (strcmp(name, p->two->path) ? p->two->path : NULL);
-       if ((p->one->file_valid && S_ISDIR(p->one->mode)) ||
-           (p->two->file_valid && S_ISDIR(p->two->mode)))
-               return; /* no tree diffs in patch format */ 
-
-       run_external_diff(name, other, p->one, p->two, p->xfrm_msg);
+       printf(":%06o %06o %s ",
+              p->one->mode, p->two->mode, sha1_to_hex(p->one->sha1));
+       printf("%s%c%s%c%s%c",
+              sha1_to_hex(p->two->sha1), inter_name_termination,
+              p->one->path, inter_name_termination,
+              p->two->path, line_termination);
 }
 
-static int identical(struct diff_filespec *one, struct diff_filespec *two)
+int diff_unmodified_pair(struct diff_filepair *p)
 {
        /* This function is written stricter than necessary to support
         * the currently implemented transformers, but the idea is to
         * let transformers to produce diff_filepairs any way they want,
         * and filter and clean them up here before producing the output.
         */
+       struct diff_filespec *one, *two;
+
+       if (DIFF_PAIR_UNMERGED(p))
+               return 0; /* unmerged is interesting */
 
-       if (!one->file_valid && !two->file_valid)
-               return 1; /* not interesting */
+       one = p->one;
+       two = p->two;
 
        /* deletion, addition, mode change and renames are all interesting. */
-       if ((one->file_valid != two->file_valid) || (one->mode != two->mode) ||
+       if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
+           (one->mode != two->mode) ||
            strcmp(one->path, two->path))
                return 0;
 
@@ -615,45 +548,162 @@ static int identical(struct diff_filespec *one, struct diff_filespec *two)
        return 0;
 }
 
-static void diff_flush_one(struct diff_filepair *p)
+static void diff_flush_patch(struct diff_filepair *p, const char *msg)
 {
-       if (identical(p->one, p->two))
+       const char *name, *other;
+
+       /* diffcore_prune() keeps "stay" entries for diff-raw
+        * copy/rename detection, but when we are generating
+        * patches we do not need them.
+        */
+       if (diff_unmodified_pair(p))
                return;
-       if (0 <= diff_raw_output)
-               diff_flush_raw(p);
+
+       name = p->one->path;
+       other = (strcmp(name, p->two->path) ? p->two->path : NULL);
+       if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
+           (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
+               return; /* no tree diffs in patch format */ 
+
+       if (DIFF_PAIR_UNMERGED(p))
+               run_external_diff(name, NULL, NULL, NULL, NULL);
        else
-               diff_flush_patch(p);
+               run_external_diff(name, other, p->one, p->two, msg);
 }
 
-int diff_queue_is_empty(void)
+int diff_needs_to_stay(struct diff_queue_struct *q, int i,
+                      struct diff_filespec *it)
 {
+       /* If it will be used in later entry (either stay or used
+        * as the source of rename/copy), we need to copy, not rename.
+        */
+       while (i < q->nr) {
+               struct diff_filepair *p = q->queue[i++];
+               if (!DIFF_FILE_VALID(p->two))
+                       continue; /* removed is fine */
+               if (strcmp(p->one->path, it->path))
+                       continue; /* not relevant */
+
+               /* p has its src set to *it and it is not a delete;
+                * it will be used for in-place change, rename/copy,
+                * or just stays there.  We cannot rename it out.
+                */
+               return 1;
+       }
+       return 0;
+}
+
+static int diff_used_as_source(struct diff_queue_struct *q, int lim,
+                              struct diff_filespec *it)
+{
+       int i;
+       for (i = 0; i < lim; i++) {
+               struct diff_filepair *p = q->queue[i++];
+               if (!strcmp(p->one->path, it->path))
+                       return 1;
+       }
+       return 0;
+}
+
+void diffcore_prune(void)
+{
+       /*
+        * Although rename/copy detection wants to have "no-change"
+        * entries fed into them, the downstream do not need to see
+        * them, unless we had rename/copy for the same path earlier.
+        * This function removes such entries.
+        *
+        * The applications that use rename/copy should:
+        *
+        * (1) feed change and "no-change" entries via diff_queue().
+        * (2) call diffcore_rename, and any other future diffcore_xxx
+        *     that would benefit by still having "no-change" entries.
+        * (3) call diffcore_prune
+        * (4) call other diffcore_xxx that do not need to see
+        *     "no-change" entries.
+        * (5) call diff_flush().
+        */
        struct diff_queue_struct *q = &diff_queued_diff;
+       struct diff_queue_struct outq;
        int i;
 
+       outq.queue = NULL;
+       outq.nr = outq.alloc = 0;
+
        for (i = 0; i < q->nr; i++) {
                struct diff_filepair *p = q->queue[i];
-               if (!identical(p->one, p->two))
-                       return 0;
+               if (!diff_unmodified_pair(p) ||
+                   diff_used_as_source(q, i, p->one))
+                       diff_q(&outq, p);
+               else
+                       free(p);
        }
-       return 1;
+       free(q->queue);
+       *q = outq;
+       return;
 }
 
-void diff_flush(const char **pathspec_, int speccnt_)
+static void diff_flush_one(struct diff_filepair *p, const char *msg)
+{
+       if (generate_patch)
+               diff_flush_patch(p, msg);
+       else
+               diff_flush_raw(p);
+}
+
+int diff_queue_is_empty(void)
 {
        struct diff_queue_struct *q = &diff_queued_diff;
-       int i;
+       return q->nr == 0;
+}
 
-       pathspec = pathspec_;
-       speccnt = speccnt_;
+void diff_flush(int diff_output_style)
+{
+       struct diff_queue_struct *q = &diff_queued_diff;
+       int i;
 
-       for (i = 0; i < q->nr; i++)
-               diff_flush_one(q->queue[i]);
+       generate_patch = 0;
+       switch (diff_output_style) {
+       case DIFF_FORMAT_HUMAN:
+               line_termination = '\n';
+               inter_name_termination = '\t';
+               break;
+       case DIFF_FORMAT_MACHINE:
+               line_termination = inter_name_termination = 0;
+               break;
+       case DIFF_FORMAT_PATCH:
+               generate_patch = 1;
+               break;
+       }
+       for (i = 0; i < q->nr; i++) {
+               char msg_[PATH_MAX*2+200], *msg = NULL;
+               struct diff_filepair *p = q->queue[i];
+               if (strcmp(p->one->path, p->two->path)) {
+                       /* This is rename or copy.  Which one is it? */
+                       if (diff_needs_to_stay(q, i+1, p->one)) {
+                               sprintf(msg_,
+                                       "similarity index %d%%\n"
+                                       "copy from %s\n"
+                                       "copy to %s\n",
+                                       (int)(0.5 + p->score * 100/MAX_SCORE),
+                                       p->one->path, p->two->path);
+                       }
+                       else
+                               sprintf(msg_,
+                                       "similarity index %d%%\n"
+                                       "rename old %s\n"
+                                       "rename new %s\n",
+                                       (int)(0.5 + p->score * 100/MAX_SCORE),
+                                       p->one->path, p->two->path);
+                       msg = msg_;
+               }
+               diff_flush_one(p, msg);
+       }
 
        for (i = 0; i < q->nr; i++) {
                struct diff_filepair *p = q->queue[i];
                diff_free_filespec_data(p->one);
                diff_free_filespec_data(p->two);
-               free(p->xfrm_msg);
                free(p);
        }
        free(q->queue);
@@ -675,10 +725,10 @@ void diff_addremove(int addremove, unsigned mode,
         * entries to the diff-core.  They will be prefixed
         * with something like '=' or '*' (I haven't decided
         * which but should not make any difference).
-        * Feeding the same new and old to diff_change() should
-        * also have the same effect.  diff_flush() should
-        * filter the identical ones out at the final output
-        * stage.
+        * Feeding the same new and old to diff_change() 
+        * also has the same effect.  diffcore_prune() should
+        * be used to filter uninteresting ones out before the
+        * final output happens.
         */
        if (reverse_diff)
                addremove = (addremove == '+' ? '-' :
@@ -697,10 +747,35 @@ void diff_addremove(int addremove, unsigned mode,
        diff_queue(&diff_queued_diff, one, two);
 }
 
+void diff_guif(unsigned old_mode,
+              unsigned new_mode,
+              const unsigned char *old_sha1,
+              const unsigned char *new_sha1,
+              const char *old_path,
+              const char *new_path)
+{
+       struct diff_filespec *one, *two;
+
+       if (reverse_diff) {
+               unsigned tmp;
+               const unsigned char *tmp_c;
+               tmp = old_mode; old_mode = new_mode; new_mode = tmp;
+               tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
+       }
+       one = alloc_filespec(old_path);
+       two = alloc_filespec(new_path);
+       if (old_mode)
+               fill_filespec(one, old_sha1, old_mode);
+       if (new_mode)
+               fill_filespec(two, new_sha1, new_mode);
+       diff_queue(&diff_queued_diff, one, two);
+}
+
 void diff_change(unsigned old_mode, unsigned new_mode,
                 const unsigned char *old_sha1,
                 const unsigned char *new_sha1,
-                const char *base, const char *path) {
+                const char *base, const char *path) 
+{
        char concatpath[PATH_MAX];
        struct diff_filespec *one, *two;
 
@@ -722,9 +797,8 @@ void diff_change(unsigned old_mode, unsigned new_mode,
 
 void diff_unmerge(const char *path)
 {
-       if (0 <= diff_raw_output) {
-               printf("U %s%c", path, diff_raw_output);
-               return;
-       }
-       run_external_diff(path, NULL, NULL, NULL, NULL);
+       struct diff_filespec *one, *two;
+       one = alloc_filespec(path);
+       two = alloc_filespec(path);
+       diff_queue(&diff_queued_diff, one, two);
 }