diff-tree --cc: squelch header generation on empty patch.
[git.git] / combine-diff.c
index 062ed8a..3b219a0 100644 (file)
@@ -29,7 +29,7 @@ static struct path_list *intersect_paths(struct path_list *curr,
        int i;
 
        if (!n) {
-               struct path_list *list = NULL, *tail = NULL;
+               struct path_list *list = NULL, **tail = &list;
                for (i = 0; i < q->nr; i++) {
                        int len;
                        const char *path;
@@ -46,12 +46,8 @@ static struct path_list *intersect_paths(struct path_list *curr,
                        p->next = NULL;
                        memcpy(p->sha1, q->queue[i]->two->sha1, 20);
                        memcpy(p->parent_sha1[n], q->queue[i]->one->sha1, 20);
-                       if (!tail)
-                               list = tail = p;
-                       else {
-                               tail->next = p;
-                               p = tail;
-                       }
+                       *tail = p;
+                       tail = &p->next;
                }
                return list;
        }
@@ -212,10 +208,7 @@ static void append_lost(struct sline *sline, int n, const char *line)
        lline->parent_map = this_mask;
        memcpy(lline->line, line, len);
        lline->line[len] = 0;
-       if (sline->lost_head)
-               *(sline->lost_tail) = lline;
-       else
-               sline->lost_head = lline;
+       *sline->lost_tail = lline;
        sline->lost_tail = &lline->next;
 }
 
@@ -278,29 +271,65 @@ static int interesting(struct sline *sline, unsigned long all_mask)
        return ((sline->flag & all_mask) != all_mask || sline->lost_head);
 }
 
-static unsigned long line_diff_parents(struct sline *sline, unsigned long all_mask)
+static unsigned long line_common_diff(struct sline *sline, unsigned long all_mask)
 {
        /*
-        * Look at the line and see from which parents we have difference.
-        * Lower bits of sline->flag records if the parent had this line,
-        * so XOR with all_mask gives us on-bits for parents we have
-        * differences with.
+        * Look at the line and see from which parents we have the
+        * same difference.
+        */
+
+       /* Lower bits of sline->flag records if the parent had this
+        * line, so XOR with all_mask gives us on-bits for parents we
+        * have differences with.
         */
-       unsigned long parents = (sline->flag ^ all_mask);
+       unsigned long common_adds = (sline->flag ^ all_mask) & all_mask;
+       unsigned long common_removes = all_mask;
+
+       /* If all the parents have this line, that also counts as
+        * having the same difference.
+        */
+       if (!common_adds)
+               common_adds = all_mask;
+
        if (sline->lost_head) {
+               /* Lost head list records the lines removed from
+                * the parents, and parent_map records from which
+                * parent the line was removed.
+                */
                struct lline *ll;
-               for (ll = sline->lost_head; ll; ll = ll->next)
-                       parents |= ll->parent_map;
+               for (ll = sline->lost_head; ll; ll = ll->next) {
+                       common_removes &= ll->parent_map;
+               }
        }
-       return parents & all_mask;
+       return common_adds & common_removes;
 }
 
-static void make_hunks(struct sline *sline, unsigned long cnt,
+static unsigned long line_all_diff(struct sline *sline, unsigned long all_mask)
+{
+       /*
+        * Look at the line and see from which parents we have some difference.
+        */
+       unsigned long different = (sline->flag ^ all_mask) & all_mask;
+       if (sline->lost_head) {
+               /* Lost head list records the lines removed from
+                * the parents, and parent_map records from which
+                * parent the line was removed.
+                */
+               struct lline *ll;
+               for (ll = sline->lost_head; ll; ll = ll->next) {
+                       different |= ll->parent_map;
+               }
+       }
+       return different;
+}
+
+static int make_hunks(struct sline *sline, unsigned long cnt,
                       int num_parent, int dense)
 {
        unsigned long all_mask = (1UL<<num_parent) - 1;
        unsigned long mark = (1UL<<num_parent);
        unsigned long i;
+       int has_interesting = 0;
 
        i = 0;
        while (i < cnt) {
@@ -316,20 +345,23 @@ static void make_hunks(struct sline *sline, unsigned long cnt,
                        j = (i + context < cnt) ? i + context : cnt;
                        while (i < j)
                                sline[i++].flag |= mark;
+                       has_interesting = 1;
                        continue;
                }
                i++;
        }
        if (!dense)
-               return;
+               return has_interesting;
 
-       /* Look at each hunk, and if it contains changes from only
-        * one parent, mark that uninteresting.
+       /* Look at each hunk, and if we have changes from only one
+        * parent, or the changes are the same from all but one
+        * parent, mark that uninteresting.
         */
+       has_interesting = 0;
        i = 0;
        while (i < cnt) {
-               int j, hunk_end, diffs;
-               unsigned long parents;
+               int j, hunk_end, same, diff;
+               unsigned long same_diff, all_diff;
                while (i < cnt && !(sline[i].flag & mark))
                        i++;
                if (cnt <= i)
@@ -337,28 +369,32 @@ static void make_hunks(struct sline *sline, unsigned long cnt,
                for (hunk_end = i + 1; hunk_end < cnt; hunk_end++)
                        if (!(sline[hunk_end].flag & mark))
                                break;
-               /* [i..hunk_end) are interesting.  Now is it from
-                * only one parent?
-                * If lost lines are only from one parent and
-                * remaining lines existed in parents other than
-                * that parent, then the hunk is not that interesting.
+               /* [i..hunk_end) are interesting.  Now does it have
+                * the same change with all but one parent?
                 */
-               parents = 0;
-               diffs = 0;
-               for (j = i; j < hunk_end; j++)
-                       parents |= line_diff_parents(sline + j, all_mask);
-               /* Now, how many bits from [0..num_parent) are on? */
+               same_diff = all_mask;
+               all_diff = 0;
+               for (j = i; j < hunk_end; j++) {
+                       same_diff &= line_common_diff(sline + j, all_mask);
+                       all_diff |= line_all_diff(sline + j, all_mask);
+               }
+               diff = same = 0;
                for (j = 0; j < num_parent; j++) {
-                       if (parents & (1UL<<j))
-                               diffs++;
+                       if (same_diff & (1UL<<j))
+                               same++;
+                       if (all_diff & (1UL<<j))
+                               diff++;
                }
-               if (diffs < 2) {
+               if ((num_parent - 1 <= same) || (diff == 1)) {
                        /* This hunk is not that interesting after all */
                        for (j = i; j < hunk_end; j++)
                                sline[j].flag &= ~mark;
                }
+               else
+                       has_interesting = 1;
                i = hunk_end;
        }
+       return has_interesting;
 }
 
 static void dump_sline(struct sline *sline, int cnt, int num_parent)
@@ -393,7 +429,6 @@ static void dump_sline(struct sline *sline, int cnt, int num_parent)
                                        else
                                                putchar(' ');
                                }
-                               putchar(' ');
                                puts(ll->line);
                                ll = ll->next;
                        }
@@ -403,18 +438,18 @@ static void dump_sline(struct sline *sline, int cnt, int num_parent)
                                else
                                        putchar('+');
                        }
-                       printf(" %.*s\n", sl->len, sl->bol);
+                       printf("%.*s\n", sl->len, sl->bol);
                }
        }
 }
 
-static void show_combined_diff(struct path_list *elem, int num_parent,
-                              int dense)
+static int show_combined_diff(struct path_list *elem, int num_parent,
+                             int dense, const char *header, int show_empty)
 {
        unsigned long size, cnt, lno;
        char *result, *cp, *ep;
        struct sline *sline; /* survived lines */
-       int i;
+       int i, show_hunks, shown_header = 0;
        char ourtmp[TMPPATHLEN];
 
        /* Read the result of merge first */
@@ -433,6 +468,7 @@ static void show_combined_diff(struct path_list *elem, int num_parent,
        sline[0].bol = result;
        for (lno = 0, cp = result; cp - result < size; cp++) {
                if (*cp == '\n') {
+                       sline[lno].lost_tail = &sline[lno].lost_head;
                        sline[lno].len = cp - sline[lno].bol;
                        sline[lno].flag = (1UL<<num_parent) - 1;
                        lno++;
@@ -441,6 +477,7 @@ static void show_combined_diff(struct path_list *elem, int num_parent,
                }
        }
        if (result[size-1] != '\n') {
+               sline[cnt-1].lost_tail = &sline[cnt-1].lost_head;
                sline[cnt-1].len = size - (sline[cnt-1].bol - result);
                sline[cnt-1].flag = (1UL<<num_parent) - 1;
        }
@@ -448,9 +485,21 @@ static void show_combined_diff(struct path_list *elem, int num_parent,
        for (i = 0; i < num_parent; i++)
                combine_diff(elem->parent_sha1[i], ourtmp, sline, cnt, i);
 
-       make_hunks(sline, cnt, num_parent, dense);
+       show_hunks = make_hunks(sline, cnt, num_parent, dense);
 
-       dump_sline(sline, cnt, num_parent);
+       if (header && (show_hunks || show_empty)) {
+               shown_header++;
+               puts(header);
+       }
+       if (show_hunks) {
+               printf("diff --%s ", dense ? "cc" : "combined");
+               if (quote_c_style(elem->path, NULL, NULL, 0))
+                       quote_c_style(elem->path, NULL, stdout, 0);
+               else
+                       printf("%s", elem->path);
+               putchar('\n');
+               dump_sline(sline, cnt, num_parent);
+       }
        unlink(ourtmp);
        free(result);
 
@@ -465,6 +514,7 @@ static void show_combined_diff(struct path_list *elem, int num_parent,
                }
        }
        free(sline);
+       return shown_header;
 }
 
 int diff_tree_combined_merge(const unsigned char *sha1,
@@ -504,17 +554,12 @@ int diff_tree_combined_merge(const unsigned char *sha1,
                        num_paths++;
        }
        if (num_paths || show_empty_merge) {
-               puts(header);
                for (p = paths; p; p = p->next) {
                        if (!p->len)
                                continue;
-                       printf("diff --combined ");
-                       if (quote_c_style(p->path, NULL, NULL, 0))
-                               quote_c_style(p->path, NULL, stdout, 0);
-                       else
-                               printf("%s", p->path);
-                       putchar('\n');
-                       show_combined_diff(p, num_parent, dense);
+                       if (show_combined_diff(p, num_parent, dense, header,
+                                              show_empty_merge))
+                               header = NULL;
                }
        }