fa7af5e58c61ce1e0b6494b356e93c0d3da413e4
   1#include "cache.h"
   2#include "commit.h"
   3#include "diff.h"
   4#include "diffcore.h"
   5#include "quote.h"
   6
   7static int uninteresting(struct diff_filepair *p)
   8{
   9        if (diff_unmodified_pair(p))
  10                return 1;
  11        if (!S_ISREG(p->one->mode) || !S_ISREG(p->two->mode))
  12                return 1;
  13        return 0;
  14}
  15
  16static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
  17{
  18        struct diff_queue_struct *q = &diff_queued_diff;
  19        struct combine_diff_path *p;
  20        int i;
  21
  22        if (!n) {
  23                struct combine_diff_path *list = NULL, **tail = &list;
  24                for (i = 0; i < q->nr; i++) {
  25                        int len;
  26                        const char *path;
  27                        if (uninteresting(q->queue[i]))
  28                                continue;
  29                        path = q->queue[i]->two->path;
  30                        len = strlen(path);
  31
  32                        p = xmalloc(sizeof(*p) + len + 1 + num_parent * 20);
  33                        p->path = (char*) &(p->parent_sha1[num_parent][0]);
  34                        memcpy(p->path, path, len);
  35                        p->path[len] = 0;
  36                        p->len = len;
  37                        p->next = NULL;
  38                        memcpy(p->sha1, q->queue[i]->two->sha1, 20);
  39                        memcpy(p->parent_sha1[n], q->queue[i]->one->sha1, 20);
  40                        *tail = p;
  41                        tail = &p->next;
  42                }
  43                return list;
  44        }
  45
  46        for (p = curr; p; p = p->next) {
  47                int found = 0;
  48                if (!p->len)
  49                        continue;
  50                for (i = 0; i < q->nr; i++) {
  51                        const char *path;
  52                        int len;
  53
  54                        if (uninteresting(q->queue[i]))
  55                                continue;
  56                        path = q->queue[i]->two->path;
  57                        len = strlen(path);
  58                        if (len == p->len && !memcmp(path, p->path, len)) {
  59                                found = 1;
  60                                memcpy(p->parent_sha1[n],
  61                                       q->queue[i]->one->sha1, 20);
  62                                break;
  63                        }
  64                }
  65                if (!found)
  66                        p->len = 0;
  67        }
  68        return curr;
  69}
  70
  71/* Lines lost from parent */
  72struct lline {
  73        struct lline *next;
  74        int len;
  75        unsigned long parent_map;
  76        char line[FLEX_ARRAY];
  77};
  78
  79/* Lines surviving in the merge result */
  80struct sline {
  81        struct lline *lost_head, **lost_tail;
  82        char *bol;
  83        int len;
  84        /* bit 0 up to (N-1) are on if the parent does _not_
  85         * have this line (i.e. we changed it).
  86         * bit N is used for "interesting" lines, including context.
  87         */
  88        unsigned long flag;
  89        unsigned long *p_lno;
  90};
  91
  92static char *grab_blob(const unsigned char *sha1, unsigned long *size)
  93{
  94        char *blob;
  95        char type[20];
  96        if (!memcmp(sha1, null_sha1, 20)) {
  97                /* deleted blob */
  98                *size = 0;
  99                return xcalloc(1, 1);
 100        }
 101        blob = read_sha1_file(sha1, type, size);
 102        if (strcmp(type, "blob"))
 103                die("object '%s' is not a blob!", sha1_to_hex(sha1));
 104        return blob;
 105}
 106
 107#define TMPPATHLEN 50
 108#define MAXLINELEN 10240
 109
 110static void write_to_temp_file(char *tmpfile, void *blob, unsigned long size)
 111{
 112        int fd = git_mkstemp(tmpfile, TMPPATHLEN, ".diff_XXXXXX");
 113        if (fd < 0)
 114                die("unable to create temp-file");
 115        if (write(fd, blob, size) != size)
 116                die("unable to write temp-file");
 117        close(fd);
 118}
 119
 120static void write_temp_blob(char *tmpfile, const unsigned char *sha1)
 121{
 122        unsigned long size;
 123        void *blob;
 124        blob = grab_blob(sha1, &size);
 125        write_to_temp_file(tmpfile, blob, size);
 126        free(blob);
 127}
 128
 129static int parse_num(char **cp_p, unsigned int *num_p)
 130{
 131        char *cp = *cp_p;
 132        unsigned int num = 0;
 133        int read_some;
 134
 135        while ('0' <= *cp && *cp <= '9')
 136                num = num * 10 + *cp++ - '0';
 137        if (!(read_some = cp - *cp_p))
 138                return -1;
 139        *cp_p = cp;
 140        *num_p = num;
 141        return 0;
 142}
 143
 144static int parse_hunk_header(char *line, int len,
 145                             unsigned int *ob, unsigned int *on,
 146                             unsigned int *nb, unsigned int *nn)
 147{
 148        char *cp;
 149        cp = line + 4;
 150        if (parse_num(&cp, ob)) {
 151        bad_line:
 152                return error("malformed diff output: %s", line);
 153        }
 154        if (*cp == ',') {
 155                cp++;
 156                if (parse_num(&cp, on))
 157                        goto bad_line;
 158        }
 159        else
 160                *on = 1;
 161        if (*cp++ != ' ' || *cp++ != '+')
 162                goto bad_line;
 163        if (parse_num(&cp, nb))
 164                goto bad_line;
 165        if (*cp == ',') {
 166                cp++;
 167                if (parse_num(&cp, nn))
 168                        goto bad_line;
 169        }
 170        else
 171                *nn = 1;
 172        return -!!memcmp(cp, " @@", 3);
 173}
 174
 175static void append_lost(struct sline *sline, int n, const char *line)
 176{
 177        struct lline *lline;
 178        int len = strlen(line);
 179        unsigned long this_mask = (1UL<<n);
 180        if (line[len-1] == '\n')
 181                len--;
 182
 183        /* Check to see if we can squash things */
 184        if (sline->lost_head) {
 185                struct lline *last_one = NULL;
 186                /* We cannot squash it with earlier one */
 187                for (lline = sline->lost_head;
 188                     lline;
 189                     lline = lline->next)
 190                        if (lline->parent_map & this_mask)
 191                                last_one = lline;
 192                lline = last_one ? last_one->next : sline->lost_head;
 193                while (lline) {
 194                        if (lline->len == len &&
 195                            !memcmp(lline->line, line, len)) {
 196                                lline->parent_map |= this_mask;
 197                                return;
 198                        }
 199                        lline = lline->next;
 200                }
 201        }
 202
 203        lline = xmalloc(sizeof(*lline) + len + 1);
 204        lline->len = len;
 205        lline->next = NULL;
 206        lline->parent_map = this_mask;
 207        memcpy(lline->line, line, len);
 208        lline->line[len] = 0;
 209        *sline->lost_tail = lline;
 210        sline->lost_tail = &lline->next;
 211}
 212
 213static void combine_diff(const unsigned char *parent, const char *ourtmp,
 214                         struct sline *sline, int cnt, int n, int num_parent)
 215{
 216        FILE *in;
 217        char parent_tmp[TMPPATHLEN];
 218        char cmd[TMPPATHLEN * 2 + 1024];
 219        char line[MAXLINELEN];
 220        unsigned int lno, ob, on, nb, nn, p_lno;
 221        unsigned long nmask = (1UL << n);
 222        unsigned long pmask = ~nmask;
 223        struct sline *lost_bucket = NULL;
 224
 225        write_temp_blob(parent_tmp, parent);
 226        sprintf(cmd, "diff --unified=0 -La/x -Lb/x '%s' '%s'",
 227                parent_tmp, ourtmp);
 228        in = popen(cmd, "r");
 229        if (!in)
 230                die("cannot spawn %s", cmd);
 231
 232        lno = 1;
 233        while (fgets(line, sizeof(line), in) != NULL) {
 234                int len = strlen(line);
 235                if (5 < len && !memcmp("@@ -", line, 4)) {
 236                        if (parse_hunk_header(line, len,
 237                                              &ob, &on, &nb, &nn))
 238                                break;
 239                        lno = nb;
 240                        if (!nb)
 241                                /* @@ -1,2 +0,0 @@ to remove the
 242                                 * first two lines...
 243                                 */
 244                                nb = 1;
 245                        if (nn == 0)
 246                                /* @@ -X,Y +N,0 @@ removed Y lines
 247                                 * that would have come *after* line N
 248                                 * in the result.  Our lost buckets hang
 249                                 * to the line after the removed lines,
 250                                 */
 251                                lost_bucket = &sline[nb];
 252                        else
 253                                lost_bucket = &sline[nb-1];
 254                        if (!sline[nb-1].p_lno)
 255                                sline[nb-1].p_lno =
 256                                        xcalloc(num_parent,
 257                                                sizeof(unsigned long));
 258                        sline[nb-1].p_lno[n] = ob;
 259                        continue;
 260                }
 261                if (!lost_bucket)
 262                        continue; /* not in any hunk yet */
 263                switch (line[0]) {
 264                case '-':
 265                        append_lost(lost_bucket, n, line+1);
 266                        break;
 267                case '+':
 268                        sline[lno-1].flag &= pmask;
 269                        lno++;
 270                        break;
 271                }
 272        }
 273        fclose(in);
 274        unlink(parent_tmp);
 275
 276        /* Assign line numbers for this parent.
 277         *
 278         * sline[lno].p_lno[n] records the first line number
 279         * (counting from 1) for parent N if the final hunk display
 280         * started by showing sline[lno] (possibly showing the lost
 281         * lines attached to it first).
 282         */
 283        for (lno = 0,  p_lno = 1; lno < cnt; lno++) {
 284                struct lline *ll;
 285                sline[lno].p_lno[n] = p_lno;
 286
 287                /* How many lines would this sline advance the p_lno? */
 288                ll = sline[lno].lost_head;
 289                while (ll) {
 290                        if (ll->parent_map & nmask)
 291                                p_lno++; /* '-' means parent had it */
 292                        ll = ll->next;
 293                }
 294                if (sline[lno].flag & nmask)
 295                        p_lno++; /* no '+' means parent had it */
 296        }
 297        sline[lno].p_lno[n] = p_lno; /* trailer */
 298}
 299
 300static unsigned long context = 3;
 301static char combine_marker = '@';
 302
 303static int interesting(struct sline *sline, unsigned long all_mask)
 304{
 305        return ((sline->flag & all_mask) != all_mask || sline->lost_head);
 306}
 307
 308static unsigned long adjust_hunk_tail(struct sline *sline,
 309                                      unsigned long all_mask,
 310                                      unsigned long hunk_begin,
 311                                      unsigned long i)
 312{
 313        /* i points at the first uninteresting line.
 314         * If the last line of the hunk was interesting
 315         * only because it has some deletion, then
 316         * it is not all that interesting for the
 317         * purpose of giving trailing context lines.
 318         */
 319        if ((hunk_begin + 1 <= i) &&
 320            ((sline[i-1].flag & all_mask) == all_mask))
 321                i--;
 322        return i;
 323}
 324
 325static unsigned long next_interesting(struct sline *sline,
 326                                      unsigned long mark,
 327                                      unsigned long i,
 328                                      unsigned long cnt,
 329                                      int uninteresting)
 330{
 331        while (i < cnt)
 332                if (uninteresting ?
 333                    !(sline[i].flag & mark) :
 334                    (sline[i].flag & mark))
 335                        return i;
 336                else
 337                        i++;
 338        return cnt;
 339}
 340
 341static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
 342{
 343        unsigned long all_mask = (1UL<<num_parent) - 1;
 344        unsigned long mark = (1UL<<num_parent);
 345        unsigned long i;
 346
 347        i = next_interesting(sline, mark, 0, cnt, 0);
 348        if (cnt <= i)
 349                return 0;
 350
 351        while (i < cnt) {
 352                unsigned long j = (context < i) ? (i - context) : 0;
 353                unsigned long k;
 354                while (j < i)
 355                        sline[j++].flag |= mark;
 356
 357        again:
 358                j = next_interesting(sline, mark, i, cnt, 1);
 359                if (cnt <= j)
 360                        break; /* the rest are all interesting */
 361
 362                /* lookahead context lines */
 363                k = next_interesting(sline, mark, j, cnt, 0);
 364                j = adjust_hunk_tail(sline, all_mask, i, j);
 365
 366                if (k < j + context) {
 367                        /* k is interesting and [j,k) are not, but
 368                         * paint them interesting because the gap is small.
 369                         */
 370                        while (j < k)
 371                                sline[j++].flag |= mark;
 372                        i = k;
 373                        goto again;
 374                }
 375
 376                /* j is the first uninteresting line and there is
 377                 * no overlap beyond it within context lines.
 378                 */
 379                i = k;
 380                k = (j + context < cnt) ? j + context : cnt;
 381                while (j < k)
 382                        sline[j++].flag |= mark;
 383        }
 384        return 1;
 385}
 386
 387static int make_hunks(struct sline *sline, unsigned long cnt,
 388                       int num_parent, int dense)
 389{
 390        unsigned long all_mask = (1UL<<num_parent) - 1;
 391        unsigned long mark = (1UL<<num_parent);
 392        unsigned long i;
 393        int has_interesting = 0;
 394
 395        for (i = 0; i < cnt; i++) {
 396                if (interesting(&sline[i], all_mask))
 397                        sline[i].flag |= mark;
 398                else
 399                        sline[i].flag &= ~mark;
 400        }
 401        if (!dense)
 402                return give_context(sline, cnt, num_parent);
 403
 404        /* Look at each hunk, and if we have changes from only one
 405         * parent, or the changes are the same from all but one
 406         * parent, mark that uninteresting.
 407         */
 408        i = 0;
 409        while (i < cnt) {
 410                unsigned long j, hunk_begin, hunk_end;
 411                unsigned long same_diff;
 412                while (i < cnt && !(sline[i].flag & mark))
 413                        i++;
 414                if (cnt <= i)
 415                        break; /* No more interesting hunks */
 416                hunk_begin = i;
 417                for (j = i + 1; j < cnt; j++) {
 418                        if (!(sline[j].flag & mark)) {
 419                                /* Look beyond the end to see if there
 420                                 * is an interesting line after this
 421                                 * hunk within context span.
 422                                 */
 423                                unsigned long la; /* lookahead */
 424                                int contin = 0;
 425                                la = adjust_hunk_tail(sline, all_mask,
 426                                                     hunk_begin, j);
 427                                la = (la + context < cnt) ?
 428                                        (la + context) : cnt;
 429                                while (j <= --la) {
 430                                        if (sline[la].flag & mark) {
 431                                                contin = 1;
 432                                                break;
 433                                        }
 434                                }
 435                                if (!contin)
 436                                        break;
 437                                j = la;
 438                        }
 439                }
 440                hunk_end = j;
 441
 442                /* [i..hunk_end) are interesting.  Now is it really
 443                 * interesting?  We check if there are only two versions
 444                 * and the result matches one of them.  That is, we look
 445                 * at:
 446                 *   (+) line, which records lines added to which parents;
 447                 *       this line appears in the result.
 448                 *   (-) line, which records from what parents the line
 449                 *       was removed; this line does not appear in the result.
 450                 * then check the set of parents the result has difference
 451                 * from, from all lines.  If there are lines that has
 452                 * different set of parents that the result has differences
 453                 * from, that means we have more than two versions.
 454                 *
 455                 * Even when we have only two versions, if the result does
 456                 * not match any of the parents, the it should be considered
 457                 * interesting.  In such a case, we would have all '+' line.
 458                 * After passing the above "two versions" test, that would
 459                 * appear as "the same set of parents" to be "all parents".
 460                 */
 461                same_diff = 0;
 462                has_interesting = 0;
 463                for (j = i; j < hunk_end && !has_interesting; j++) {
 464                        unsigned long this_diff = ~sline[j].flag & all_mask;
 465                        struct lline *ll = sline[j].lost_head;
 466                        if (this_diff) {
 467                                /* This has some changes.  Is it the
 468                                 * same as others?
 469                                 */
 470                                if (!same_diff)
 471                                        same_diff = this_diff;
 472                                else if (same_diff != this_diff) {
 473                                        has_interesting = 1;
 474                                        break;
 475                                }
 476                        }
 477                        while (ll && !has_interesting) {
 478                                /* Lost this line from these parents;
 479                                 * who are they?  Are they the same?
 480                                 */
 481                                this_diff = ll->parent_map;
 482                                if (!same_diff)
 483                                        same_diff = this_diff;
 484                                else if (same_diff != this_diff) {
 485                                        has_interesting = 1;
 486                                }
 487                                ll = ll->next;
 488                        }
 489                }
 490
 491                if (!has_interesting && same_diff != all_mask) {
 492                        /* This hunk is not that interesting after all */
 493                        for (j = hunk_begin; j < hunk_end; j++)
 494                                sline[j].flag &= ~mark;
 495                }
 496                i = hunk_end;
 497        }
 498
 499        has_interesting = give_context(sline, cnt, num_parent);
 500        return has_interesting;
 501}
 502
 503static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, unsigned long cnt, int n)
 504{
 505        l0 = sline[l0].p_lno[n];
 506        l1 = sline[l1].p_lno[n];
 507        printf("-%lu,%lu ", l0, l1-l0);
 508}
 509
 510static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
 511{
 512        unsigned long mark = (1UL<<num_parent);
 513        int i;
 514        unsigned long lno = 0;
 515
 516        while (1) {
 517                struct sline *sl = &sline[lno];
 518                int hunk_end;
 519                while (lno < cnt && !(sline[lno].flag & mark))
 520                        lno++;
 521                if (cnt <= lno)
 522                        break;
 523                for (hunk_end = lno + 1; hunk_end < cnt; hunk_end++)
 524                        if (!(sline[hunk_end].flag & mark))
 525                                break;
 526                for (i = 0; i <= num_parent; i++) putchar(combine_marker);
 527                printf(" +%lu,%lu ", lno+1, hunk_end-lno);
 528                for (i = 0; i < num_parent; i++)
 529                        show_parent_lno(sline, lno, hunk_end, cnt, i);
 530                for (i = 0; i <= num_parent; i++) putchar(combine_marker);
 531                putchar('\n');
 532                while (lno < hunk_end) {
 533                        struct lline *ll;
 534                        int j;
 535                        sl = &sline[lno++];
 536                        ll = sl->lost_head;
 537                        while (ll) {
 538                                for (j = 0; j < num_parent; j++) {
 539                                        if (ll->parent_map & (1UL<<j))
 540                                                putchar('-');
 541                                        else
 542                                                putchar(' ');
 543                                }
 544                                puts(ll->line);
 545                                ll = ll->next;
 546                        }
 547                        for (j = 0; j < num_parent; j++) {
 548                                if ((1UL<<j) & sl->flag)
 549                                        putchar(' ');
 550                                else
 551                                        putchar('+');
 552                        }
 553                        printf("%.*s\n", sl->len, sl->bol);
 554                }
 555        }
 556}
 557
 558static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
 559                               int i, int j)
 560{
 561        /* We have already examined parent j and we know parent i
 562         * and parent j are the same, so reuse the combined result
 563         * of parent j for parent i.
 564         */
 565        unsigned long lno, imask, jmask;
 566        imask = (1UL<<i);
 567        jmask = (1UL<<j);
 568
 569        for (lno = 0; lno < cnt; lno++) {
 570                struct lline *ll = sline->lost_head;
 571                sline->p_lno[i] = sline->p_lno[j];
 572                while (ll) {
 573                        if (ll->parent_map & jmask)
 574                                ll->parent_map |= imask;
 575                        ll = ll->next;
 576                }
 577                if (!(sline->flag & jmask))
 578                        sline->flag &= ~imask;
 579                sline++;
 580        }
 581}
 582
 583int show_combined_diff(struct combine_diff_path *elem, int num_parent,
 584                       int dense, const char *header, int show_empty)
 585{
 586        unsigned long size, cnt, lno;
 587        char *result, *cp, *ep;
 588        struct sline *sline; /* survived lines */
 589        int i, show_hunks, shown_header = 0;
 590        char ourtmp_buf[TMPPATHLEN];
 591        char *ourtmp = ourtmp_buf;
 592
 593        /* Read the result of merge first */
 594        if (memcmp(elem->sha1, null_sha1, 20)) {
 595                result = grab_blob(elem->sha1, &size);
 596                write_to_temp_file(ourtmp, result, size);
 597        }
 598        else {
 599                struct stat st;
 600                int fd;
 601                ourtmp = elem->path;
 602                if (0 <= (fd = open(ourtmp, O_RDONLY)) &&
 603                    !fstat(fd, &st)) {
 604                        int len = st.st_size;
 605                        int cnt = 0;
 606
 607                        size = len;
 608                        result = xmalloc(len + 1);
 609                        while (cnt < len) {
 610                                int done = xread(fd, result+cnt, len-cnt);
 611                                if (done == 0)
 612                                        break;
 613                                if (done < 0)
 614                                        die("read error '%s'", ourtmp);
 615                                cnt += done;
 616                        }
 617                        result[len] = 0;
 618                }
 619                else {
 620                        /* deleted file */
 621                        size = 0;
 622                        result = xmalloc(1);
 623                        result[0] = 0;
 624                        ourtmp = "/dev/null";
 625                }
 626                if (0 <= fd)
 627                        close(fd);
 628        }
 629
 630        for (cnt = 0, cp = result; cp - result < size; cp++) {
 631                if (*cp == '\n')
 632                        cnt++;
 633        }
 634        if (result[size-1] != '\n')
 635                cnt++; /* incomplete line */
 636
 637        sline = xcalloc(cnt+1, sizeof(*sline));
 638        ep = result;
 639        sline[0].bol = result;
 640        for (lno = 0, cp = result; cp - result < size; cp++) {
 641                if (*cp == '\n') {
 642                        sline[lno].lost_tail = &sline[lno].lost_head;
 643                        sline[lno].len = cp - sline[lno].bol;
 644                        sline[lno].flag = (1UL<<num_parent) - 1;
 645                        lno++;
 646                        if (lno < cnt)
 647                                sline[lno].bol = cp + 1;
 648                }
 649        }
 650        if (result[size-1] != '\n') {
 651                sline[cnt-1].lost_tail = &sline[cnt-1].lost_head;
 652                sline[cnt-1].len = size - (sline[cnt-1].bol - result);
 653                sline[cnt-1].flag = (1UL<<num_parent) - 1;
 654        }
 655
 656        sline[0].p_lno = xcalloc((cnt+1) * num_parent, sizeof(unsigned long));
 657        for (lno = 0; lno < cnt; lno++)
 658                sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
 659
 660        for (i = 0; i < num_parent; i++) {
 661                int j;
 662                for (j = 0; j < i; j++) {
 663                        if (!memcmp(elem->parent_sha1[i],
 664                                    elem->parent_sha1[j], 20)) {
 665                                reuse_combine_diff(sline, cnt, i, j);
 666                                break;
 667                        }
 668                }
 669                if (i <= j)
 670                        combine_diff(elem->parent_sha1[i], ourtmp, sline,
 671                                     cnt, i, num_parent);
 672        }
 673
 674        show_hunks = make_hunks(sline, cnt, num_parent, dense);
 675
 676        if (header && (show_hunks || show_empty)) {
 677                shown_header++;
 678                puts(header);
 679        }
 680        if (show_hunks) {
 681                printf("diff --%s ", dense ? "cc" : "combined");
 682                if (quote_c_style(elem->path, NULL, NULL, 0))
 683                        quote_c_style(elem->path, NULL, stdout, 0);
 684                else
 685                        printf("%s", elem->path);
 686                putchar('\n');
 687                printf("index ");
 688                for (i = 0; i < num_parent; i++) {
 689                        printf("%s%s",
 690                               i ? ".." : "",
 691                               find_unique_abbrev(elem->parent_sha1[i],
 692                                                  DEFAULT_ABBREV));
 693                }
 694                printf("->%s\n",
 695                       find_unique_abbrev(elem->sha1, DEFAULT_ABBREV));
 696                dump_sline(sline, cnt, num_parent);
 697        }
 698        if (ourtmp == ourtmp_buf)
 699                unlink(ourtmp);
 700        free(result);
 701
 702        for (i = 0; i < cnt; i++) {
 703                if (sline[i].lost_head) {
 704                        struct lline *ll = sline[i].lost_head;
 705                        while (ll) {
 706                                struct lline *tmp = ll;
 707                                ll = ll->next;
 708                                free(tmp);
 709                        }
 710                }
 711        }
 712        free(sline);
 713        return shown_header;
 714}
 715
 716int diff_tree_combined_merge(const unsigned char *sha1,
 717                             const char *header,
 718                             int show_empty_merge, int dense)
 719{
 720        struct commit *commit = lookup_commit(sha1);
 721        struct diff_options diffopts;
 722        struct commit_list *parents;
 723        struct combine_diff_path *p, *paths = NULL;
 724        int num_parent, i, num_paths;
 725
 726        diff_setup(&diffopts);
 727        diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
 728        diffopts.recursive = 1;
 729
 730        /* count parents */
 731        for (parents = commit->parents, num_parent = 0;
 732             parents;
 733             parents = parents->next, num_parent++)
 734                ; /* nothing */
 735
 736        /* find set of paths that everybody touches */
 737        for (parents = commit->parents, i = 0;
 738             parents;
 739             parents = parents->next, i++) {
 740                struct commit *parent = parents->item;
 741                diff_tree_sha1(parent->object.sha1, commit->object.sha1, "",
 742                               &diffopts);
 743                paths = intersect_paths(paths, i, num_parent);
 744                diff_flush(&diffopts);
 745        }
 746
 747        /* find out surviving paths */
 748        for (num_paths = 0, p = paths; p; p = p->next) {
 749                if (p->len)
 750                        num_paths++;
 751        }
 752        if (num_paths || show_empty_merge) {
 753                for (p = paths; p; p = p->next) {
 754                        if (!p->len)
 755                                continue;
 756                        if (show_combined_diff(p, num_parent, dense, header,
 757                                               show_empty_merge))
 758                                header = NULL;
 759                }
 760        }
 761
 762        /* Clean things up */
 763        while (paths) {
 764                struct combine_diff_path *tmp = paths;
 765                paths = paths->next;
 766                free(tmp);
 767        }
 768        return 0;
 769}