git-p4: Unset P4DIFF environment variable when using 'p4 -du diff'
[gitweb.git] / combine-diff.c
index 044633d1643a533498d3f5aba86a35a4611f6ca7..0e19cbaacc1099fd69f7f2d9b4a17c94a327baa9 100644 (file)
@@ -92,14 +92,14 @@ struct sline {
 static char *grab_blob(const unsigned char *sha1, unsigned long *size)
 {
        char *blob;
-       char type[20];
+       enum object_type type;
        if (is_null_sha1(sha1)) {
                /* deleted blob */
                *size = 0;
                return xcalloc(1, 1);
        }
-       blob = read_sha1_file(sha1, type, size);
-       if (strcmp(type, blob_type))
+       blob = read_sha1_file(sha1, &type, size);
+       if (type != OBJ_BLOB)
                die("object '%s' is not a blob!", sha1_to_hex(sha1));
        return blob;
 }
@@ -215,8 +215,7 @@ static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
        parent_file.ptr = grab_blob(parent, &sz);
        parent_file.size = sz;
        xpp.flags = XDF_NEED_MINIMAL;
-       xecfg.ctxlen = 0;
-       xecfg.flags = 0;
+       memset(&xecfg, 0, sizeof(xecfg));
        ecb.outf = xdiff_outf;
        ecb.priv = &state;
        memset(&state, 0, sizeof(state));
@@ -227,7 +226,7 @@ static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
        state.num_parent = num_parent;
        state.n = n;
 
-       xdl_diff(&parent_file, result_file, &xpp, &xecfg, &ecb);
+       xdi_diff(&parent_file, result_file, &xpp, &xecfg, &ecb);
        free(parent_file.ptr);
 
        /* Assign line numbers for this parent.
@@ -647,15 +646,19 @@ static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
        sline->p_lno[i] = sline->p_lno[j];
 }
 
-static void dump_quoted_path(const char *prefix, const char *path,
+static void dump_quoted_path(const char *head,
+                            const char *prefix,
+                            const char *path,
                             const char *c_meta, const char *c_reset)
 {
-       printf("%s%s", c_meta, prefix);
-       if (quote_c_style(path, NULL, NULL, 0))
-               quote_c_style(path, NULL, stdout, 0);
-       else
-               printf("%s", path);
-       printf("%s\n", c_reset);
+       static struct strbuf buf = STRBUF_INIT;
+
+       strbuf_reset(&buf);
+       strbuf_addstr(&buf, c_meta);
+       strbuf_addstr(&buf, head);
+       quote_two_c_style(&buf, prefix, path, 0);
+       strbuf_addstr(&buf, c_reset);
+       puts(buf.buf);
 }
 
 static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
@@ -668,7 +671,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
        int mode_differs = 0;
        int i, show_hunks;
        int working_tree_file = is_null_sha1(elem->sha1);
-       int abbrev = opt->full_index ? 40 : DEFAULT_ABBREV;
+       int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
        mmfile_t result_file;
 
        context = opt->context;
@@ -684,7 +687,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
                        goto deleted_file;
 
                if (S_ISLNK(st.st_mode)) {
-                       size_t len = st.st_size;
+                       size_t len = xsize_t(st.st_size);
                        result_size = len;
                        result = xmalloc(len + 1);
                        if (result_size != readlink(elem->path, result, len)) {
@@ -697,14 +700,24 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
                }
                else if (0 <= (fd = open(elem->path, O_RDONLY)) &&
                         !fstat(fd, &st)) {
-                       size_t len = st.st_size;
+                       size_t len = xsize_t(st.st_size);
                        size_t sz = 0;
+                       int is_file, i;
 
                        elem->mode = canon_mode(st.st_mode);
+                       /* if symlinks don't work, assume symlink if all parents
+                        * are symlinks
+                        */
+                       is_file = has_symlinks;
+                       for (i = 0; !is_file && i < num_parent; i++)
+                               is_file = !S_ISLNK(elem->parent[i].mode);
+                       if (!is_file)
+                               elem->mode = canon_mode(S_IFLNK);
+
                        result_size = len;
                        result = xmalloc(len + 1);
                        while (sz < len) {
-                               int done = xread(fd, result+sz, len-sz);
+                               ssize_t done = xread(fd, result+sz, len-sz);
                                if (done == 0)
                                        break;
                                if (done < 0)
@@ -778,7 +791,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
 
        if (show_hunks || mode_differs || working_tree_file) {
                const char *abb;
-               int use_color = opt->color_diff;
+               int use_color = DIFF_OPT_TST(opt, COLOR_DIFF);
                const char *c_meta = diff_get_color(use_color, DIFF_METAINFO);
                const char *c_reset = diff_get_color(use_color, DIFF_RESET);
                int added = 0;
@@ -787,7 +800,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
                if (rev->loginfo && !rev->no_commit_id)
                        show_log(rev, opt->msg_sep);
                dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
-                                elem->path, c_meta, c_reset);
+                                "", elem->path, c_meta, c_reset);
                printf("%sindex ", c_meta);
                for (i = 0; i < num_parent; i++) {
                        abb = find_unique_abbrev(elem->parent[i].sha1,
@@ -823,14 +836,19 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
                        printf("%s\n", c_reset);
                }
                if (added)
-                       dump_quoted_path("--- /dev/", "null", c_meta, c_reset);
+                       dump_quoted_path("--- ", "", "/dev/null",
+                                        c_meta, c_reset);
                else
-                       dump_quoted_path("--- a/", elem->path, c_meta, c_reset);
+                       dump_quoted_path("--- ", opt->a_prefix, elem->path,
+                                        c_meta, c_reset);
                if (deleted)
-                       dump_quoted_path("+++ /dev/", "null", c_meta, c_reset);
+                       dump_quoted_path("+++ ", "", "/dev/null",
+                                        c_meta, c_reset);
                else
-                       dump_quoted_path("+++ b/", elem->path, c_meta, c_reset);
-               dump_sline(sline, cnt, num_parent, opt->color_diff);
+                       dump_quoted_path("+++ ", opt->b_prefix, elem->path,
+                                        c_meta, c_reset);
+               dump_sline(sline, cnt, num_parent,
+                          DIFF_OPT_TST(opt, COLOR_DIFF));
        }
        free(result);
 
@@ -891,16 +909,7 @@ static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct re
                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);
-       }
+       write_name_quoted(p->path, stdout, line_termination);
 }
 
 void show_combined_diff(struct combine_diff_path *p,
@@ -932,7 +941,8 @@ void diff_tree_combined(const unsigned char *sha1,
 
        diffopts = *opt;
        diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
-       diffopts.recursive = 1;
+       DIFF_OPT_SET(&diffopts, RECURSIVE);
+       DIFF_OPT_CLR(&diffopts, ALLOW_EXTERNAL);
 
        show_log_first = !!rev->loginfo && !rev->no_commit_id;
        needsep = 0;