Merge branch 'maint'
[gitweb.git] / combine-diff.c
index 6d783054d58bf31c7a1d9d8e38e678198ea6dd7e..7693884fdb7e43d0aecd77ad1c71531181d7ceee 100644 (file)
@@ -1,5 +1,6 @@
 #include "cache.h"
 #include "commit.h"
+#include "blob.h"
 #include "diff.h"
 #include "diffcore.h"
 #include "quote.h"
@@ -39,6 +40,7 @@ static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr,
                        p->mode = q->queue[i]->two->mode;
                        memcpy(p->parent[n].sha1, q->queue[i]->one->sha1, 20);
                        p->parent[n].mode = q->queue[i]->one->mode;
+                       p->parent[n].status = q->queue[i]->status;
                        *tail = p;
                        tail = &p->next;
                }
@@ -62,6 +64,7 @@ static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr,
                                memcpy(p->parent[n].sha1,
                                       q->queue[i]->one->sha1, 20);
                                p->parent[n].mode = q->queue[i]->one->mode;
+                               p->parent[n].status = q->queue[i]->status;
                                break;
                        }
                }
@@ -102,7 +105,7 @@ static char *grab_blob(const unsigned char *sha1, unsigned long *size)
                return xcalloc(1, 1);
        }
        blob = read_sha1_file(sha1, type, size);
-       if (strcmp(type, "blob"))
+       if (strcmp(type, blob_type))
                die("object '%s' is not a blob!", sha1_to_hex(sha1));
        return blob;
 }
@@ -618,8 +621,9 @@ static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
        sline->p_lno[i] = sline->p_lno[j];
 }
 
-int show_combined_diff(struct combine_diff_path *elem, int num_parent,
-                      int dense, const char *header)
+static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
+                          int dense, const char *header,
+                          struct diff_options *opt)
 {
        unsigned long size, cnt, lno;
        char *result, *cp, *ep;
@@ -628,9 +632,11 @@ int show_combined_diff(struct combine_diff_path *elem, int num_parent,
        int i, show_hunks, shown_header = 0;
        char ourtmp_buf[TMPPATHLEN];
        char *ourtmp = ourtmp_buf;
+       int working_tree_file = !memcmp(elem->sha1, null_sha1, 20);
+       int abbrev = opt->full_index ? 40 : DEFAULT_ABBREV;
 
        /* Read the result of merge first */
-       if (memcmp(elem->sha1, null_sha1, 20)) {
+       if (!working_tree_file) {
                result = grab_blob(elem->sha1, &size);
                write_to_temp_file(ourtmp, result, size);
        }
@@ -644,6 +650,7 @@ int show_combined_diff(struct combine_diff_path *elem, int num_parent,
                        int len = st.st_size;
                        int cnt = 0;
 
+                       elem->mode = canon_mode(st.st_mode);
                        size = len;
                        result = xmalloc(len + 1);
                        while (cnt < len) {
@@ -659,6 +666,7 @@ int show_combined_diff(struct combine_diff_path *elem, int num_parent,
                else {
                        /* deleted file */
                        size = 0;
+                       elem->mode = 0;
                        result = xmalloc(1);
                        result[0] = 0;
                        ourtmp = "/dev/null";
@@ -714,15 +722,12 @@ int show_combined_diff(struct combine_diff_path *elem, int num_parent,
 
        show_hunks = make_hunks(sline, cnt, num_parent, dense);
 
-       if (show_hunks || mode_differs) {
+       if (show_hunks || mode_differs || working_tree_file) {
                const char *abb;
-               char null_abb[DEFAULT_ABBREV + 1];
 
-               memset(null_abb, '0', DEFAULT_ABBREV);
-               null_abb[DEFAULT_ABBREV] = 0;
                if (header) {
                        shown_header++;
-                       puts(header);
+                       printf("%s%c", header, opt->line_termination);
                }
                printf("diff --%s ", dense ? "cc" : "combined");
                if (quote_c_style(elem->path, NULL, NULL, 0))
@@ -732,28 +737,33 @@ int show_combined_diff(struct combine_diff_path *elem, int num_parent,
                putchar('\n');
                printf("index ");
                for (i = 0; i < num_parent; i++) {
-                       if (elem->parent[i].mode != elem->mode)
-                               mode_differs = 1;
-                       if (memcmp(elem->parent[i].sha1, null_sha1, 20))
-                               abb = find_unique_abbrev(elem->parent[i].sha1,
-                                                        DEFAULT_ABBREV);
-                       else
-                               abb = null_abb;
+                       abb = find_unique_abbrev(elem->parent[i].sha1,
+                                                abbrev);
                        printf("%s%s", i ? "," : "", abb);
                }
-               if (memcmp(elem->sha1, null_sha1, 20))
-                       abb = find_unique_abbrev(elem->sha1, DEFAULT_ABBREV);
-               else
-                       abb = null_abb;
+               abb = find_unique_abbrev(elem->sha1, abbrev);
                printf("..%s\n", abb);
 
                if (mode_differs) {
-                       printf("mode ");
-                       for (i = 0; i < num_parent; i++) {
-                               printf("%s%06o", i ? "," : "",
-                                      elem->parent[i].mode);
+                       int added = !!elem->mode;
+                       for (i = 0; added && i < num_parent; i++)
+                               if (elem->parent[i].status !=
+                                   DIFF_STATUS_ADDED)
+                                       added = 0;
+                       if (added)
+                               printf("new file mode %06o", elem->mode);
+                       else {
+                               if (!elem->mode)
+                                       printf("deleted file ");
+                               printf("mode ");
+                               for (i = 0; i < num_parent; i++) {
+                                       printf("%s%06o", i ? "," : "",
+                                              elem->parent[i].mode);
+                               }
+                               if (elem->mode)
+                                       printf("..%06o", elem->mode);
                        }
-                       printf("..%06o\n", elem->mode);
+                       putchar('\n');
                }
                dump_sline(sline, cnt, num_parent);
        }
@@ -790,33 +800,73 @@ static void show_raw_diff(struct combine_diff_path *p, int num_parent, const cha
                inter_name_termination = 0;
 
        if (header)
-               puts(header);
-       offset = strlen(COLONS) - num_parent;
-       if (offset < 0)
-               offset = 0;
-       prefix = COLONS + offset;
+               printf("%s%c", header, line_termination);
 
-       /* Show the modes */
        for (i = 0; i < num_parent; i++) {
-               int mode = p->parent[i].mode;
-               if (mode)
+               if (p->parent[i].mode)
                        mod_type = 'M';
-               printf("%s%06o", prefix, mode);
-               prefix = " ";
        }
-       printf("%s%06o", prefix, p->mode);
        if (!p->mode)
                mod_type = 'D';
 
-       /* Show sha1's */
-       for (i = 0; i < num_parent; i++) {
-               printf("%s%s", prefix, diff_unique_abbrev(p->parent[i].sha1, opt->abbrev));
-               prefix = " ";
+       if (opt->output_format == DIFF_FORMAT_RAW) {
+               offset = strlen(COLONS) - num_parent;
+               if (offset < 0)
+                       offset = 0;
+               prefix = COLONS + offset;
+
+               /* Show the modes */
+               for (i = 0; i < num_parent; i++) {
+                       printf("%s%06o", prefix, p->parent[i].mode);
+                       prefix = " ";
+               }
+               printf("%s%06o", prefix, p->mode);
+
+               /* Show sha1's */
+               for (i = 0; i < num_parent; i++)
+                       printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
+                                                        opt->abbrev));
+               printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
        }
-       printf("%s%s", prefix, diff_unique_abbrev(p->sha1, opt->abbrev));
 
-       /* Modification type, terminations, filename */
-       printf(" %c%c%s%c", mod_type, inter_name_termination, p->path, line_termination);
+       if (opt->output_format == DIFF_FORMAT_RAW ||
+           opt->output_format == DIFF_FORMAT_NAME_STATUS) {
+               for (i = 0; i < num_parent; i++)
+                       putchar(p->parent[i].status);
+               putchar(inter_name_termination);
+       }
+
+       if (line_termination) {
+               if (quote_c_style(p->path, NULL, NULL, 0))
+                       quote_c_style(p->path, NULL, stdout, 0);
+               else
+                       printf("%s", p->path);
+               putchar(line_termination);
+       }
+       else {
+               printf("%s%c", p->path, line_termination);
+       }
+}
+
+int show_combined_diff(struct combine_diff_path *p,
+                      int num_parent,
+                      int dense,
+                      const char *header,
+                      struct diff_options *opt)
+{
+       if (!p->len)
+               return 0;
+       switch (opt->output_format) {
+       case DIFF_FORMAT_RAW:
+       case DIFF_FORMAT_NAME_STATUS:
+       case DIFF_FORMAT_NAME:
+               show_raw_diff(p, num_parent, header, opt);
+               return 1;
+
+       default:
+       case DIFF_FORMAT_PATCH:
+               return show_patch_diff(p, num_parent, dense, header, opt);
+       }
 }
 
 const char *diff_tree_combined_merge(const unsigned char *sha1,
@@ -858,14 +908,8 @@ const char *diff_tree_combined_merge(const unsigned char *sha1,
        }
        if (num_paths) {
                for (p = paths; p; p = p->next) {
-                       if (!p->len)
-                               continue;
-                       if (opt->output_format == DIFF_FORMAT_RAW) {
-                               show_raw_diff(p, num_parent, header, opt);
-                               header = NULL;
-                               continue;
-                       }
-                       if (show_combined_diff(p, num_parent, dense, header))
+                       if (show_combined_diff(p, num_parent, dense,
+                                              header, opt))
                                header = NULL;
                }
        }