Merge branch 'maint'
[gitweb.git] / combine-diff.c
index 9aa099b061add3eb9a7adb763f0195e2c534f1ac..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;
 }
@@ -619,7 +622,8 @@ static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
 }
 
 static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
-                          int dense, const char *header)
+                          int dense, const char *header,
+                          struct diff_options *opt)
 {
        unsigned long size, cnt, lno;
        char *result, *cp, *ep;
@@ -628,9 +632,11 @@ static int show_patch_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 @@ static int show_patch_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 @@ static int show_patch_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 @@ static int show_patch_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 @@ static int show_patch_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,7 +800,7 @@ static void show_raw_diff(struct combine_diff_path *p, int num_parent, const cha
                inter_name_termination = 0;
 
        if (header)
-               puts(header);
+               printf("%s%c", header, line_termination);
 
        for (i = 0; i < num_parent; i++) {
                if (p->parent[i].mode)
@@ -820,8 +830,11 @@ static void show_raw_diff(struct combine_diff_path *p, int num_parent, const cha
        }
 
        if (opt->output_format == DIFF_FORMAT_RAW ||
-           opt->output_format == DIFF_FORMAT_NAME_STATUS)
-               printf("%c%c", mod_type, inter_name_termination);
+           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))
@@ -852,7 +865,7 @@ int show_combined_diff(struct combine_diff_path *p,
 
        default:
        case DIFF_FORMAT_PATCH:
-               return show_patch_diff(p, num_parent, dense, header);
+               return show_patch_diff(p, num_parent, dense, header, opt);
        }
 }