]> git.ipfire.org Git - thirdparty/git.git/blobdiff - combine-diff.c
combine-diff: a few more finishing touches.
[thirdparty/git.git] / combine-diff.c
index 3daa8cb13eaebf0c7eb66bbc80a63436a00bb342..01a8437f5c837f30eb463d19b98774d56f182283 100644 (file)
@@ -7,13 +7,6 @@
 #include "xdiff-interface.h"
 #include "log-tree.h"
 
-static int uninteresting(struct diff_filepair *p)
-{
-       if (diff_unmodified_pair(p))
-               return 1;
-       return 0;
-}
-
 static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
 {
        struct diff_queue_struct *q = &diff_queued_diff;
@@ -25,7 +18,7 @@ static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr,
                for (i = 0; i < q->nr; i++) {
                        int len;
                        const char *path;
-                       if (uninteresting(q->queue[i]))
+                       if (diff_unmodified_pair(q->queue[i]))
                                continue;
                        path = q->queue[i]->two->path;
                        len = strlen(path);
@@ -38,9 +31,9 @@ static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr,
                        memset(p->parent, 0,
                               sizeof(p->parent[0]) * num_parent);
 
-                       memcpy(p->sha1, q->queue[i]->two->sha1, 20);
+                       hashcpy(p->sha1, q->queue[i]->two->sha1);
                        p->mode = q->queue[i]->two->mode;
-                       memcpy(p->parent[n].sha1, q->queue[i]->one->sha1, 20);
+                       hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
                        p->parent[n].mode = q->queue[i]->one->mode;
                        p->parent[n].status = q->queue[i]->status;
                        *tail = p;
@@ -57,14 +50,13 @@ static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr,
                        const char *path;
                        int len;
 
-                       if (uninteresting(q->queue[i]))
+                       if (diff_unmodified_pair(q->queue[i]))
                                continue;
                        path = q->queue[i]->two->path;
                        len = strlen(path);
                        if (len == p->len && !memcmp(path, p->path, len)) {
                                found = 1;
-                               memcpy(p->parent[n].sha1,
-                                      q->queue[i]->one->sha1, 20);
+                               hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
                                p->parent[n].mode = q->queue[i]->one->mode;
                                p->parent[n].status = q->queue[i]->status;
                                break;
@@ -101,7 +93,7 @@ static char *grab_blob(const unsigned char *sha1, unsigned long *size)
 {
        char *blob;
        char type[20];
-       if (!memcmp(sha1, null_sha1, 20)) {
+       if (is_null_sha1(sha1)) {
                /* deleted blob */
                *size = 0;
                return xcalloc(1, 1);
@@ -205,7 +197,8 @@ static void consume_line(void *state_, char *line, unsigned long len)
 }
 
 static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
-                        struct sline *sline, int cnt, int n, int num_parent)
+                        struct sline *sline, unsigned int cnt, int n,
+                        int num_parent)
 {
        unsigned int p_lno, lno;
        unsigned long nmask = (1UL << n);
@@ -293,7 +286,7 @@ static unsigned long find_next(struct sline *sline,
                               unsigned long mark,
                               unsigned long i,
                               unsigned long cnt,
-                              int uninteresting)
+                              int look_for_uninteresting)
 {
        /* We have examined up to i-1 and are about to look at i.
         * Find next interesting or uninteresting line.  Here,
@@ -303,7 +296,7 @@ static unsigned long find_next(struct sline *sline,
         * that are surrounded by interesting() ones.
         */
        while (i <= cnt)
-               if (uninteresting
+               if (look_for_uninteresting
                    ? !(sline[i].flag & mark)
                    : (sline[i].flag & mark))
                        return i;
@@ -319,7 +312,7 @@ static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
        unsigned long i;
 
        /* Two groups of interesting lines may have a short gap of
-        * unintersting lines.  Connect such groups to give them a
+        * uninteresting lines.  Connect such groups to give them a
         * bit of context.
         *
         * We first start from what the interesting() function says,
@@ -489,18 +482,30 @@ static int make_hunks(struct sline *sline, unsigned long cnt,
        return has_interesting;
 }
 
-static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, unsigned long cnt, int n)
+static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n)
 {
        l0 = sline[l0].p_lno[n];
        l1 = sline[l1].p_lno[n];
        printf(" -%lu,%lu", l0, l1-l0);
 }
 
-static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
+static int hunk_comment_line(const char *bol)
+{
+       int ch = *bol & 0xff;
+       return (isalpha(ch) || ch == '_' || ch == '$');
+}
+
+static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
+                      int use_color)
 {
        unsigned long mark = (1UL<<num_parent);
        int i;
        unsigned long lno = 0;
+       const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
+       const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
+       const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
+       const char *c_plain = diff_get_color(use_color, DIFF_PLAIN);
+       const char *c_reset = diff_get_color(use_color, DIFF_RESET);
 
        if (!cnt)
                return; /* result deleted */
@@ -509,8 +514,13 @@ static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
                struct sline *sl = &sline[lno];
                unsigned long hunk_end;
                unsigned long rlines;
-               while (lno <= cnt && !(sline[lno].flag & mark))
+               const char *hunk_comment = NULL;
+
+               while (lno <= cnt && !(sline[lno].flag & mark)) {
+                       if (hunk_comment_line(sline[lno].bol))
+                               hunk_comment = sline[lno].bol;
                        lno++;
+               }
                if (cnt < lno)
                        break;
                else {
@@ -521,12 +531,29 @@ static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
                rlines = hunk_end - lno;
                if (cnt < hunk_end)
                        rlines--; /* pointing at the last delete hunk */
+               fputs(c_frag, stdout);
                for (i = 0; i <= num_parent; i++) putchar(combine_marker);
                for (i = 0; i < num_parent; i++)
-                       show_parent_lno(sline, lno, hunk_end, cnt, i);
+                       show_parent_lno(sline, lno, hunk_end, i);
                printf(" +%lu,%lu ", lno+1, rlines);
                for (i = 0; i <= num_parent; i++) putchar(combine_marker);
-               putchar('\n');
+
+               if (hunk_comment) {
+                       int comment_end = 0;
+                       for (i = 0; i < 40; i++) {
+                               int ch = hunk_comment[i] & 0xff;
+                               if (!ch || ch == '\n')
+                                       break;
+                               if (!isspace(ch))
+                                   comment_end = i;
+                       }
+                       if (comment_end)
+                               putchar(' ');
+                       for (i = 0; i < comment_end; i++)
+                               putchar(hunk_comment[i]);
+               }
+
+               printf("%s\n", c_reset);
                while (lno < hunk_end) {
                        struct lline *ll;
                        int j;
@@ -534,18 +561,23 @@ static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
                        sl = &sline[lno++];
                        ll = sl->lost_head;
                        while (ll) {
+                               fputs(c_old, stdout);
                                for (j = 0; j < num_parent; j++) {
                                        if (ll->parent_map & (1UL<<j))
                                                putchar('-');
                                        else
                                                putchar(' ');
                                }
-                               puts(ll->line);
+                               printf("%s%s\n", ll->line, c_reset);
                                ll = ll->next;
                        }
                        if (cnt < lno)
                                break;
                        p_mask = 1;
+                       if (!(sl->flag & (mark-1)))
+                               fputs(c_plain, stdout);
+                       else
+                               fputs(c_new, stdout);
                        for (j = 0; j < num_parent; j++) {
                                if (p_mask & sl->flag)
                                        putchar('+');
@@ -553,7 +585,7 @@ static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
                                        putchar(' ');
                                p_mask <<= 1;
                        }
-                       printf("%.*s\n", sl->len, sl->bol);
+                       printf("%.*s%s\n", sl->len, sl->bol, c_reset);
                }
        }
 }
@@ -585,26 +617,27 @@ 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 *prefix, const char *path,
+                            const char *c_meta, const char *c_reset)
 {
-       fputs(prefix, stdout);
+       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);
-       putchar('\n');
+       printf("%s\n", c_reset);
 }
 
-static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
-                          int dense, struct rev_info *rev)
+static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
+                           int dense, struct rev_info *rev)
 {
        struct diff_options *opt = &rev->diffopt;
        unsigned long result_size, cnt, lno;
        char *result, *cp;
        struct sline *sline; /* survived lines */
        int mode_differs = 0;
-       int i, show_hunks, shown_header = 0;
-       int working_tree_file = !memcmp(elem->sha1, null_sha1, 20);
+       int i, show_hunks;
+       int working_tree_file = is_null_sha1(elem->sha1);
        int abbrev = opt->full_index ? 40 : DEFAULT_ABBREV;
        mmfile_t result_file;
 
@@ -619,18 +652,18 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
                if (0 <= (fd = open(elem->path, O_RDONLY)) &&
                    !fstat(fd, &st)) {
                        int len = st.st_size;
-                       int cnt = 0;
+                       int sz = 0;
 
                        elem->mode = canon_mode(st.st_mode);
                        result_size = len;
                        result = xmalloc(len + 1);
-                       while (cnt < len) {
-                               int done = xread(fd, result+cnt, len-cnt);
+                       while (sz < len) {
+                               int done = xread(fd, result+sz, len-sz);
                                if (done == 0)
                                        break;
                                if (done < 0)
                                        die("read error '%s'", elem->path);
-                               cnt += done;
+                               sz += done;
                        }
                        result[len] = 0;
                }
@@ -638,14 +671,13 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
                        /* deleted file */
                        result_size = 0;
                        elem->mode = 0;
-                       result = xmalloc(1);
-                       result[0] = 0;
+                       result = xcalloc(1, 1);
                }
                if (0 <= fd)
                        close(fd);
        }
 
-       for (cnt = 0, cp = result; cp - result < result_size; cp++) {
+       for (cnt = 0, cp = result; cp < result + result_size; cp++) {
                if (*cp == '\n')
                        cnt++;
        }
@@ -658,7 +690,7 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
                sline[lno].lost_tail = &sline[lno].lost_head;
                sline[lno].flag = 0;
        }
-       for (lno = 0, cp = result; cp - result < result_size; cp++) {
+       for (lno = 0, cp = result; cp < result + result_size; cp++) {
                if (*cp == '\n') {
                        sline[lno].len = cp - sline[lno].bol;
                        lno++;
@@ -682,8 +714,8 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
        for (i = 0; i < num_parent; i++) {
                int j;
                for (j = 0; j < i; j++) {
-                       if (!memcmp(elem->parent[i].sha1,
-                                   elem->parent[j].sha1, 20)) {
+                       if (!hashcmp(elem->parent[i].sha1,
+                                    elem->parent[j].sha1)) {
                                reuse_combine_diff(sline, cnt, i, j);
                                break;
                        }
@@ -699,30 +731,40 @@ static int 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;
+               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;
+               int deleted = 0;
 
                if (rev->loginfo)
-                       show_log(rev, rev->loginfo, "\n");
-               dump_quoted_path(dense ? "diff --cc " : "diff --combined ", elem->path);
-               printf("index ");
+                       show_log(rev, opt->msg_sep);
+               dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
+                                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,
                                                 abbrev);
                        printf("%s%s", i ? "," : "", abb);
                }
                abb = find_unique_abbrev(elem->sha1, abbrev);
-               printf("..%s\n", abb);
+               printf("..%s%s\n", abb, c_reset);
 
                if (mode_differs) {
-                       int added = !!elem->mode;
+                       deleted = !elem->mode;
+
+                       /* We say it was added if nobody had it */
+                       added = !deleted;
                        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);
+                               printf("%snew file mode %06o",
+                                      c_meta, elem->mode);
                        else {
-                               if (!elem->mode)
-                                       printf("deleted file ");
+                               if (deleted)
+                                       printf("%sdeleted file ", c_meta);
                                printf("mode ");
                                for (i = 0; i < num_parent; i++) {
                                        printf("%s%06o", i ? "," : "",
@@ -731,17 +773,23 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
                                if (elem->mode)
                                        printf("..%06o", elem->mode);
                        }
-                       putchar('\n');
+                       printf("%s\n", c_reset);
                }
-               dump_quoted_path("--- a/", elem->path);
-               dump_quoted_path("+++ b/", elem->path);
-               dump_sline(sline, cnt, num_parent);
+               if (added)
+                       dump_quoted_path("--- /dev/", "null", c_meta, c_reset);
+               else
+                       dump_quoted_path("--- a/", elem->path, c_meta, c_reset);
+               if (deleted)
+                       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);
        }
        free(result);
 
-       for (i = 0; i < cnt; i++) {
-               if (sline[i].lost_head) {
-                       struct lline *ll = sline[i].lost_head;
+       for (lno = 0; lno < cnt; lno++) {
+               if (sline[lno].lost_head) {
+                       struct lline *ll = sline[lno].lost_head;
                        while (ll) {
                                struct lline *tmp = ll;
                                ll = ll->next;
@@ -751,7 +799,6 @@ static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
        }
        free(sline[0].p_lno);
        free(sline);
-       return shown_header;
 }
 
 #define COLONS "::::::::::::::::::::::::::::::::"
@@ -769,7 +816,7 @@ static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct re
                inter_name_termination = 0;
 
        if (rev->loginfo)
-               show_log(rev, rev->loginfo, "\n");
+               show_log(rev, opt->msg_sep);
 
        if (opt->output_format & DIFF_FORMAT_RAW) {
                offset = strlen(COLONS) - num_parent;
@@ -819,11 +866,10 @@ void show_combined_diff(struct combine_diff_path *p,
                return;
        if (opt->output_format & (DIFF_FORMAT_RAW |
                                  DIFF_FORMAT_NAME |
-                                 DIFF_FORMAT_NAME_STATUS)) {
+                                 DIFF_FORMAT_NAME_STATUS))
                show_raw_diff(p, num_parent, rev);
-       } else if (opt->output_format & DIFF_FORMAT_PATCH) {
+       else if (opt->output_format & DIFF_FORMAT_PATCH)
                show_patch_diff(p, num_parent, dense, rev);
-       }
 }
 
 void diff_tree_combined(const unsigned char *sha1,
@@ -835,30 +881,33 @@ void diff_tree_combined(const unsigned char *sha1,
        struct diff_options *opt = &rev->diffopt;
        struct diff_options diffopts;
        struct combine_diff_path *p, *paths = NULL;
-       int i, num_paths;
+       int i, num_paths, needsep, show_log_first;
 
        diffopts = *opt;
-       diffopts.output_format &= ~(DIFF_FORMAT_RAW | DIFF_FORMAT_DIFFSTAT);
+       diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
        diffopts.recursive = 1;
 
+       show_log_first = !!rev->loginfo;
+       needsep = 0;
        /* find set of paths that everybody touches */
        for (i = 0; i < num_parent; i++) {
                /* show stat against the first parent even
                 * when doing combined diff.
                 */
                if (i == 0 && opt->output_format & DIFF_FORMAT_DIFFSTAT)
-                       diffopts.output_format |= DIFF_FORMAT_DIFFSTAT;
+                       diffopts.output_format = DIFF_FORMAT_DIFFSTAT;
                else
-                       diffopts.output_format |= DIFF_FORMAT_NO_OUTPUT;
+                       diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
                diff_tree_sha1(parent[i], sha1, "", &diffopts);
                diffcore_std(&diffopts);
                paths = intersect_paths(paths, i, num_parent);
 
-               if (opt->output_format & DIFF_FORMAT_DIFFSTAT && rev->loginfo)
-                       show_log(rev, rev->loginfo, "---\n");
+               if (show_log_first && i == 0) {
+                       show_log(rev, opt->msg_sep);
+                       if (rev->verbose_header && opt->output_format)
+                               putchar(opt->line_termination);
+               }
                diff_flush(&diffopts);
-               if (opt->output_format & DIFF_FORMAT_DIFFSTAT)
-                       putchar('\n');
        }
 
        /* find out surviving paths */
@@ -874,9 +923,13 @@ void diff_tree_combined(const unsigned char *sha1,
                                if (p->len)
                                        show_raw_diff(p, num_parent, rev);
                        }
-                       putchar(opt->line_termination);
+                       needsep = 1;
                }
+               else if (opt->output_format & DIFF_FORMAT_DIFFSTAT)
+                       needsep = 1;
                if (opt->output_format & DIFF_FORMAT_PATCH) {
+                       if (needsep)
+                               putchar(opt->line_termination);
                        for (p = paths; p; p = p->next) {
                                if (p->len)
                                        show_patch_diff(p, num_parent, dense,
@@ -911,6 +964,7 @@ void diff_tree_combined_merge(const unsigned char *sha1,
        for (parents = commit->parents, num_parent = 0;
             parents;
             parents = parents->next, num_parent++)
-               memcpy(parent + num_parent, parents->item->object.sha1, 20);
+               hashcpy((unsigned char*)(parent + num_parent),
+                       parents->item->object.sha1);
        diff_tree_combined(sha1, parent, num_parent, dense, rev);
 }