[PATCH] fix and testcase for git-commit-tree option
[git.git] / diff.c
diff --git a/diff.c b/diff.c
index d908ef3..f745cdd 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"
 static const char *diff_opts = "-pu";
 static unsigned char null_sha1[20] = { 0, };
 
-static int detect_rename;
 static int reverse_diff;
-static int diff_raw_output = -1;
-static const char **pathspec;
-static int speccnt;
-static int minimum_score;
 
 static const char *external_diff(void)
 {
@@ -165,20 +159,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) {
+               spec->mode = DIFF_FILE_CANON_MODE(mode);
+               memcpy(spec->sha1, sha1, 20);
+               spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
+       }
 }
 
 /*
@@ -233,7 +230,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;
@@ -290,7 +287,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,
@@ -318,7 +315,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.
@@ -387,32 +384,14 @@ 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 \
  *               infile2 infile2-sha1 infile2-mode [ rename-to ]
  *
  */
-static void run_external_diff(const char *name,
+static void run_external_diff(const char *pgm,
+                             const char *name,
                              const char *other,
                              struct diff_filespec *one,
                              struct diff_filespec *two,
@@ -423,9 +402,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);
@@ -443,7 +419,6 @@ static void run_external_diff(const char *name,
        if (pid < 0)
                die("unable to fork");
        if (!pid) {
-               const char *pgm = external_diff();
                if (pgm) {
                        if (one && two) {
                                const char *exec_arg[10];
@@ -460,7 +435,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
@@ -493,122 +468,120 @@ static void run_external_diff(const char *name,
        remove_tempfile();
 }
 
-int diff_scoreopt_parse(const char *opt)
+static void run_diff(const char *name,
+                    const char *other,
+                    struct diff_filespec *one,
+                    struct diff_filespec *two,
+                    const char *xfrm_msg)
 {
-       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;
+       const char *pgm = external_diff();
+       if (!pgm &&
+           DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
+           (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
+               /* a filepair that changes between file and symlink
+                * needs to be split into deletion and creation.
+                */
+               struct diff_filespec *null = alloc_filespec(two->path);
+               run_external_diff(NULL, name, other, one, null, xfrm_msg);
+               free(null);
+               null = alloc_filespec(one->path);
+               run_external_diff(NULL, name, other, null, two, xfrm_msg);
+               free(null);
+       }
+       else
+               run_external_diff(pgm, name, other, one, two, xfrm_msg);
 }
 
-void diff_setup(int detect_rename_, int minimum_score_, int reverse_diff_,
-               int diff_raw_output_,
-               const char **pathspec_, int speccnt_)
+void diff_setup(int reverse_diff_)
 {
-       detect_rename = detect_rename_;
        reverse_diff = reverse_diff_;
-       pathspec = pathspec_;
-       diff_raw_output = diff_raw_output_;
-       speccnt = speccnt_;
-       minimum_score = minimum_score_ ? : DEFAULT_MINIMUM_SCORE;
 }
 
-static struct diff_queue_struct queued_diff;
+struct diff_queue_struct diff_queued_diff;
 
-struct diff_file_pair *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_file_pair *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;
+       diff_q(queue, dp);
+       return dp;
 }
 
-static void diff_flush_raw(struct diff_file_pair *p)
+static void diff_flush_raw(struct diff_filepair *p,
+                          int line_termination,
+                          int inter_name_termination)
 {
-       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);
-               return;
+       int two_paths;
+       char status[10];
+
+       if (line_termination) {
+               const char *err = "path %s cannot be expressed without -z";
+               if (strchr(p->one->path, line_termination) ||
+                   strchr(p->one->path, inter_name_termination))
+                       die(err, p->one->path);
+               if (strchr(p->two->path, line_termination) ||
+                   strchr(p->two->path, inter_name_termination))
+                       die(err, p->two->path);
        }
 
-       if (p->one->file_valid) {
-               it = p->one;
-               addremove = '-';
-       } else {
-               it = p->two;
-               addremove = '+';
+       switch (p->status) {
+       case 'C': case 'R':
+               two_paths = 1;
+               sprintf(status, "%c%03d", p->status,
+                       (int)(0.5 + p->score * 100.0/MAX_SCORE));
+               break;
+       default:
+               two_paths = 0;
+               status[0] = p->status;
+               status[1] = 0;
+               break;
        }
-
-       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);
+       printf(":%06o %06o %s ",
+              p->one->mode, p->two->mode, sha1_to_hex(p->one->sha1));
+       printf("%s %s%c%s",
+              sha1_to_hex(p->two->sha1),
+              status,
+              inter_name_termination,
+              p->one->path);
+       if (two_paths)
+               printf("%c%s", inter_name_termination, p->two->path);
+       putchar(line_termination);
 }
 
-static void diff_flush_patch(struct diff_file_pair *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);
-}
-
-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_file_pairs any way they want,
+        * 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) ||
+       /* deletion, addition, mode or type change
+        * and rename are all interesting.
+        */
+       if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
+           DIFF_PAIR_MODE_CHANGED(p) ||
            strcmp(one->path, two->path))
                return 0;
 
@@ -623,31 +596,231 @@ static int identical(struct diff_filespec *one, struct diff_filespec *two)
        return 0;
 }
 
-static void diff_flush_one(struct diff_file_pair *p)
+static void diff_flush_patch(struct diff_filepair *p)
 {
-       if (identical(p->one, p->two))
+       const char *name, *other;
+       char msg_[PATH_MAX*2+200], *msg;
+
+       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 */ 
+
+       switch (p->status) {
+       case 'C':
+               sprintf(msg_,
+                       "similarity index %d%%\n"
+                       "copy from %s\n"
+                       "copy to %s\n",
+                       (int)(0.5 + p->score * 100.0/MAX_SCORE),
+                       p->one->path, p->two->path);
+               msg = msg_;
+               break;
+       case 'R':
+               sprintf(msg_,
+                       "similarity index %d%%\n"
+                       "rename old %s\n"
+                       "rename new %s\n",
+                       (int)(0.5 + p->score * 100.0/MAX_SCORE),
+                       p->one->path, p->two->path);
+               msg = msg_;
+               break;
+       default:
+               msg = NULL;
+       }
+
+       if (DIFF_PAIR_UNMERGED(p))
+               run_diff(name, NULL, NULL, NULL, NULL);
        else
-               diff_flush_patch(p);
+               run_diff(name, other, p->one, p->two, msg);
 }
 
-void diff_flush(void)
+int diff_needs_to_stay(struct diff_queue_struct *q, int i,
+                      struct diff_filespec *it)
 {
-       struct diff_queue_struct *q = &queued_diff;
-       int i;
+       /* 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;
+}
 
-       if (detect_rename)
-               diff_detect_rename(q, detect_rename, minimum_score);
+int diff_queue_is_empty(void)
+{
+       struct diff_queue_struct *q = &diff_queued_diff;
+       int i;
        for (i = 0; i < q->nr; i++)
-               diff_flush_one(q->queue[i]);
+               if (!diff_unmodified_pair(q->queue[i]))
+                       return 0;
+       return 1;
+}
+
+#if DIFF_DEBUG
+void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
+{
+       fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
+               x, one ? : "",
+               s->path,
+               DIFF_FILE_VALID(s) ? "valid" : "invalid",
+               s->mode,
+               s->sha1_valid ? sha1_to_hex(s->sha1) : "");
+       fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
+               x, one ? : "",
+               s->size, s->xfrm_flags);
+}
 
+void diff_debug_filepair(const struct diff_filepair *p, int i)
+{
+       diff_debug_filespec(p->one, i, "one");
+       diff_debug_filespec(p->two, i, "two");
+       fprintf(stderr, "score %d, status %c\n",
+               p->score, p->status ? : '?');
+}
+
+void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
+{
+       int i;
+       if (msg)
+               fprintf(stderr, "%s\n", msg);
+       fprintf(stderr, "q->nr = %d\n", q->nr);
        for (i = 0; i < q->nr; i++) {
-               struct diff_file_pair *p = q->queue[i];
+               struct diff_filepair *p = q->queue[i];
+               diff_debug_filepair(p, i);
+       }
+}
+#endif
+
+static void diff_resolve_rename_copy(void)
+{
+       int i, j;
+       struct diff_filepair *p, *pp;
+       struct diff_queue_struct *q = &diff_queued_diff;
+
+       /* This should not depend on the ordering of things. */
+
+       diff_debug_queue("resolve-rename-copy", q);
+
+       for (i = 0; i < q->nr; i++) {
+               p = q->queue[i];
+               p->status = 0; /* undecided */
+               if (DIFF_PAIR_UNMERGED(p))
+                       p->status = 'U';
+               else if (!DIFF_FILE_VALID((p)->one))
+                       p->status = 'N';
+               else if (!DIFF_FILE_VALID((p)->two)) {
+                       /* Deletion record should be omitted if there
+                        * are rename/copy entries using this one as
+                        * the source.  Then we can say one of them
+                        * is a rename and the rest are copies.
+                        */
+                       p->status = 'D';
+                       for (j = 0; j < q->nr; j++) {
+                               pp = q->queue[j];
+                               if (!strcmp(pp->one->path, p->one->path) &&
+                                   strcmp(pp->one->path, pp->two->path)) {
+                                       p->status = 'X';
+                                       break;
+                               }
+                       }
+               }
+               else if (DIFF_PAIR_TYPE_CHANGED(p))
+                       p->status = 'T';
+
+               /* from this point on, we are dealing with a pair
+                * whose both sides are valid and of the same type, i.e.
+                * either in-place edit or rename/copy edit.
+                */
+               else if (strcmp(p->one->path, p->two->path)) {
+                       /* See if there is somebody else anywhere that
+                        * will keep the path (either modified or
+                        * unmodified).  If so, we have to be a copy,
+                        * not a rename.  In addition, if there is
+                        * some other rename or copy that comes later
+                        * than us that uses the same source, we
+                        * have to be a copy, not a rename.
+                        */
+                       for (j = 0; j < q->nr; j++) {
+                               pp = q->queue[j];
+                               if (strcmp(pp->one->path, p->one->path))
+                                       continue;
+                               if (!strcmp(pp->one->path, pp->two->path)) {
+                                       if (DIFF_FILE_VALID(pp->two)) {
+                                               /* non-delete */
+                                               p->status = 'C';
+                                               break;
+                                       }
+                                       continue;
+                               }
+                               /* pp is a rename/copy ... */
+                               if (i < j) {
+                                       /* ... and comes later than us */
+                                       p->status = 'C';
+                                       break;
+                               }
+                       }
+                       if (!p->status)
+                               p->status = 'R';
+               }
+               else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
+                        p->one->mode != p->two->mode)
+                       p->status = 'M';
+               else
+                       /* this is a "no-change" entry */
+                       p->status = 'X';
+       }
+       diff_debug_queue("resolve-rename-copy done", q);
+}
+
+void diff_flush(int diff_output_style, int resolve_rename_copy)
+{
+       struct diff_queue_struct *q = &diff_queued_diff;
+       int i;
+       int line_termination = '\n';
+       int inter_name_termination = '\t';
+
+       if (diff_output_style == DIFF_FORMAT_MACHINE)
+               line_termination = inter_name_termination = 0;
+       if (resolve_rename_copy)
+               diff_resolve_rename_copy();
+
+       for (i = 0; i < q->nr; i++) {
+               struct diff_filepair *p = q->queue[i];
+               if ((diff_output_style == DIFF_FORMAT_NO_OUTPUT) ||
+                   (p->status == 'X'))
+                       continue;
+               if (p->status == 0)
+                       die("internal error in diff-resolve-rename-copy");
+               switch (diff_output_style) {
+               case DIFF_FORMAT_PATCH:
+                       diff_flush_patch(p);
+                       break;
+               case DIFF_FORMAT_HUMAN:
+               case DIFF_FORMAT_MACHINE:
+                       diff_flush_raw(p, line_termination,
+                                      inter_name_termination);
+                       break;
+               }
+       }
+       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);
@@ -669,10 +842,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.
+        * Before the final output happens, they are pruned after
+        * merged into rename/copy pairs as appropriate.
         */
        if (reverse_diff)
                addremove = (addremove == '+' ? '-' :
@@ -688,13 +861,37 @@ void diff_addremove(int addremove, unsigned mode,
        if (addremove != '-')
                fill_filespec(two, sha1, mode);
 
-       diff_queue(&queued_diff, one, two);
+       diff_queue(&diff_queued_diff, one, two);
+}
+
+void diff_helper_input(unsigned old_mode,
+                      unsigned new_mode,
+                      const unsigned char *old_sha1,
+                      const unsigned char *new_sha1,
+                      const char *old_path,
+                      int status,
+                      int score,
+                      const char *new_path)
+{
+       struct diff_filespec *one, *two;
+       struct diff_filepair *dp;
+
+       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);
+       dp = diff_queue(&diff_queued_diff, one, two);
+       dp->score = score;
+       dp->status = status;
 }
 
 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;
 
@@ -711,14 +908,13 @@ void diff_change(unsigned old_mode, unsigned new_mode,
        fill_filespec(one, old_sha1, old_mode);
        fill_filespec(two, new_sha1, new_mode);
 
-       diff_queue(&queued_diff, one, two);
+       diff_queue(&diff_queued_diff, one, two);
 }
 
 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);
 }