combine-diff.con commit Merge branch 'jc/combine' into next (ca3874a)
   1#include "cache.h"
   2#include "commit.h"
   3#include "blob.h"
   4#include "diff.h"
   5#include "diffcore.h"
   6#include "quote.h"
   7#include "xdiff/xdiff.h"
   8
   9static int uninteresting(struct diff_filepair *p)
  10{
  11        if (diff_unmodified_pair(p))
  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                        p = xmalloc(combine_diff_path_size(num_parent, len));
  32                        p->path = (char*) &(p->parent[num_parent]);
  33                        memcpy(p->path, path, len);
  34                        p->path[len] = 0;
  35                        p->len = len;
  36                        p->next = NULL;
  37                        memset(p->parent, 0,
  38                               sizeof(p->parent[0]) * num_parent);
  39
  40                        memcpy(p->sha1, q->queue[i]->two->sha1, 20);
  41                        p->mode = q->queue[i]->two->mode;
  42                        memcpy(p->parent[n].sha1, q->queue[i]->one->sha1, 20);
  43                        p->parent[n].mode = q->queue[i]->one->mode;
  44                        p->parent[n].status = q->queue[i]->status;
  45                        *tail = p;
  46                        tail = &p->next;
  47                }
  48                return list;
  49        }
  50
  51        for (p = curr; p; p = p->next) {
  52                int found = 0;
  53                if (!p->len)
  54                        continue;
  55                for (i = 0; i < q->nr; i++) {
  56                        const char *path;
  57                        int len;
  58
  59                        if (uninteresting(q->queue[i]))
  60                                continue;
  61                        path = q->queue[i]->two->path;
  62                        len = strlen(path);
  63                        if (len == p->len && !memcmp(path, p->path, len)) {
  64                                found = 1;
  65                                memcpy(p->parent[n].sha1,
  66                                       q->queue[i]->one->sha1, 20);
  67                                p->parent[n].mode = q->queue[i]->one->mode;
  68                                p->parent[n].status = q->queue[i]->status;
  69                                break;
  70                        }
  71                }
  72                if (!found)
  73                        p->len = 0;
  74        }
  75        return curr;
  76}
  77
  78/* Lines lost from parent */
  79struct lline {
  80        struct lline *next;
  81        int len;
  82        unsigned long parent_map;
  83        char line[FLEX_ARRAY];
  84};
  85
  86/* Lines surviving in the merge result */
  87struct sline {
  88        struct lline *lost_head, **lost_tail;
  89        char *bol;
  90        int len;
  91        /* bit 0 up to (N-1) are on if the parent has this line (i.e.
  92         * we did not change it).
  93         * bit N is used for "interesting" lines, including context.
  94         */
  95        unsigned long flag;
  96        unsigned long *p_lno;
  97};
  98
  99static char *grab_blob(const unsigned char *sha1, unsigned long *size)
 100{
 101        char *blob;
 102        char type[20];
 103        if (!memcmp(sha1, null_sha1, 20)) {
 104                /* deleted blob */
 105                *size = 0;
 106                return xcalloc(1, 1);
 107        }
 108        blob = read_sha1_file(sha1, type, size);
 109        if (strcmp(type, blob_type))
 110                die("object '%s' is not a blob!", sha1_to_hex(sha1));
 111        return blob;
 112}
 113
 114static int parse_num(char **cp_p, unsigned int *num_p)
 115{
 116        char *cp = *cp_p;
 117        unsigned int num = 0;
 118        int read_some;
 119
 120        while ('0' <= *cp && *cp <= '9')
 121                num = num * 10 + *cp++ - '0';
 122        if (!(read_some = cp - *cp_p))
 123                return -1;
 124        *cp_p = cp;
 125        *num_p = num;
 126        return 0;
 127}
 128
 129static int parse_hunk_header(char *line, int len,
 130                             unsigned int *ob, unsigned int *on,
 131                             unsigned int *nb, unsigned int *nn)
 132{
 133        char *cp;
 134        cp = line + 4;
 135        if (parse_num(&cp, ob)) {
 136        bad_line:
 137                return error("malformed diff output: %s", line);
 138        }
 139        if (*cp == ',') {
 140                cp++;
 141                if (parse_num(&cp, on))
 142                        goto bad_line;
 143        }
 144        else
 145                *on = 1;
 146        if (*cp++ != ' ' || *cp++ != '+')
 147                goto bad_line;
 148        if (parse_num(&cp, nb))
 149                goto bad_line;
 150        if (*cp == ',') {
 151                cp++;
 152                if (parse_num(&cp, nn))
 153                        goto bad_line;
 154        }
 155        else
 156                *nn = 1;
 157        return -!!memcmp(cp, " @@", 3);
 158}
 159
 160static void append_lost(struct sline *sline, int n, const char *line, int len)
 161{
 162        struct lline *lline;
 163        unsigned long this_mask = (1UL<<n);
 164        if (line[len-1] == '\n')
 165                len--;
 166
 167        /* Check to see if we can squash things */
 168        if (sline->lost_head) {
 169                struct lline *last_one = NULL;
 170                /* We cannot squash it with earlier one */
 171                for (lline = sline->lost_head;
 172                     lline;
 173                     lline = lline->next)
 174                        if (lline->parent_map & this_mask)
 175                                last_one = lline;
 176                lline = last_one ? last_one->next : sline->lost_head;
 177                while (lline) {
 178                        if (lline->len == len &&
 179                            !memcmp(lline->line, line, len)) {
 180                                lline->parent_map |= this_mask;
 181                                return;
 182                        }
 183                        lline = lline->next;
 184                }
 185        }
 186
 187        lline = xmalloc(sizeof(*lline) + len + 1);
 188        lline->len = len;
 189        lline->next = NULL;
 190        lline->parent_map = this_mask;
 191        memcpy(lline->line, line, len);
 192        lline->line[len] = 0;
 193        *sline->lost_tail = lline;
 194        sline->lost_tail = &lline->next;
 195}
 196
 197struct combine_diff_state {
 198        char *remainder;
 199        unsigned long remainder_size;
 200        unsigned int lno, ob, on, nb, nn;
 201        unsigned long nmask;
 202        int num_parent;
 203        int n;
 204        struct sline *sline;
 205        struct sline *lost_bucket;
 206};
 207
 208static void consume_line(struct combine_diff_state *state,
 209                         char *line,
 210                         unsigned long len)
 211{
 212        if (5 < len && !memcmp("@@ -", line, 4)) {
 213                if (parse_hunk_header(line, len,
 214                                      &state->ob, &state->on,
 215                                      &state->nb, &state->nn))
 216                        return;
 217                state->lno = state->nb;
 218                if (!state->nb)
 219                        /* @@ -1,2 +0,0 @@ to remove the
 220                         * first two lines...
 221                         */
 222                        state->nb = 1;
 223                if (state->nn == 0)
 224                        /* @@ -X,Y +N,0 @@ removed Y lines
 225                         * that would have come *after* line N
 226                         * in the result.  Our lost buckets hang
 227                         * to the line after the removed lines,
 228                         */
 229                        state->lost_bucket = &state->sline[state->nb];
 230                else
 231                        state->lost_bucket = &state->sline[state->nb-1];
 232                if (!state->sline[state->nb-1].p_lno)
 233                        state->sline[state->nb-1].p_lno =
 234                                xcalloc(state->num_parent,
 235                                        sizeof(unsigned long));
 236                state->sline[state->nb-1].p_lno[state->n] = state->ob;
 237                return;
 238        }
 239        if (!state->lost_bucket)
 240                return; /* not in any hunk yet */
 241        switch (line[0]) {
 242        case '-':
 243                append_lost(state->lost_bucket, state->n, line+1, len-1);
 244                break;
 245        case '+':
 246                state->sline[state->lno-1].flag |= state->nmask;
 247                state->lno++;
 248                break;
 249        }
 250}
 251
 252static int combine_diff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
 253{
 254        struct combine_diff_state *priv = priv_;
 255        int i;
 256        for (i = 0; i < nbuf; i++) {
 257                if (mb[i].ptr[mb[i].size-1] != '\n') {
 258                        /* Incomplete line */
 259                        priv->remainder = realloc(priv->remainder,
 260                                                  priv->remainder_size +
 261                                                  mb[i].size);
 262                        memcpy(priv->remainder + priv->remainder_size,
 263                               mb[i].ptr, mb[i].size);
 264                        priv->remainder_size += mb[i].size;
 265                        continue;
 266                }
 267
 268                /* we have a complete line */
 269                if (!priv->remainder) {
 270                        consume_line(priv, mb[i].ptr, mb[i].size);
 271                        continue;
 272                }
 273                priv->remainder = realloc(priv->remainder,
 274                                          priv->remainder_size +
 275                                          mb[i].size);
 276                memcpy(priv->remainder + priv->remainder_size,
 277                       mb[i].ptr, mb[i].size);
 278                consume_line(priv, priv->remainder,
 279                             priv->remainder_size + mb[i].size);
 280                free(priv->remainder);
 281                priv->remainder = NULL;
 282                priv->remainder_size = 0;
 283        }
 284        return 0;
 285}
 286
 287static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
 288                         struct sline *sline, int cnt, int n, int num_parent)
 289{
 290        unsigned int p_lno, lno;
 291        unsigned long nmask = (1UL << n);
 292        xpparam_t xpp;
 293        xdemitconf_t xecfg;
 294        mmfile_t parent_file;
 295        xdemitcb_t ecb;
 296        struct combine_diff_state state;
 297        unsigned long sz;
 298
 299        if (!cnt)
 300                return; /* result deleted */
 301
 302        parent_file.ptr = grab_blob(parent, &sz);
 303        parent_file.size = sz;
 304        xpp.flags = XDF_NEED_MINIMAL;
 305        xecfg.ctxlen = 0;
 306        xecfg.flags = 0;
 307        ecb.outf = combine_diff_outf;
 308        ecb.priv = &state;
 309        memset(&state, 0, sizeof(state));
 310        state.nmask = nmask;
 311        state.sline = sline;
 312        state.lno = 1;
 313        state.num_parent = num_parent;
 314        state.n = n;
 315
 316        xdl_diff(&parent_file, result_file, &xpp, &xecfg, &ecb);
 317        if (state.remainder && state.remainder_size)
 318                consume_line(&state, state.remainder, state.remainder_size);
 319        free(state.remainder);
 320        free(parent_file.ptr);
 321
 322        /* Assign line numbers for this parent.
 323         *
 324         * sline[lno].p_lno[n] records the first line number
 325         * (counting from 1) for parent N if the final hunk display
 326         * started by showing sline[lno] (possibly showing the lost
 327         * lines attached to it first).
 328         */
 329        for (lno = 0,  p_lno = 1; lno < cnt; lno++) {
 330                struct lline *ll;
 331                sline[lno].p_lno[n] = p_lno;
 332
 333                /* How many lines would this sline advance the p_lno? */
 334                ll = sline[lno].lost_head;
 335                while (ll) {
 336                        if (ll->parent_map & nmask)
 337                                p_lno++; /* '-' means parent had it */
 338                        ll = ll->next;
 339                }
 340                if (!(sline[lno].flag & nmask))
 341                        p_lno++; /* no '+' means parent had it */
 342        }
 343        sline[lno].p_lno[n] = p_lno; /* trailer */
 344}
 345
 346static unsigned long context = 3;
 347static char combine_marker = '@';
 348
 349static int interesting(struct sline *sline, unsigned long all_mask)
 350{
 351        /* If some parents lost lines here, or if we have added to
 352         * some parent, it is interesting.
 353         */
 354        return ((sline->flag & all_mask) || sline->lost_head);
 355}
 356
 357static unsigned long adjust_hunk_tail(struct sline *sline,
 358                                      unsigned long all_mask,
 359                                      unsigned long hunk_begin,
 360                                      unsigned long i)
 361{
 362        /* i points at the first uninteresting line.  If the last line
 363         * of the hunk was interesting only because it has some
 364         * deletion, then it is not all that interesting for the
 365         * purpose of giving trailing context lines.  This is because
 366         * we output '-' line and then unmodified sline[i-1] itself in
 367         * that case which gives us one extra context line.
 368         */
 369        if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
 370                i--;
 371        return i;
 372}
 373
 374static unsigned long find_next(struct sline *sline,
 375                               unsigned long mark,
 376                               unsigned long i,
 377                               unsigned long cnt,
 378                               int uninteresting)
 379{
 380        /* We have examined up to i-1 and are about to look at i.
 381         * Find next interesting or uninteresting line.  Here,
 382         * "interesting" does not mean interesting(), but marked by
 383         * the give_context() function below (i.e. it includes context
 384         * lines that are not interesting to interesting() function
 385         * that are surrounded by interesting() ones.
 386         */
 387        while (i < cnt)
 388                if (uninteresting
 389                    ? !(sline[i].flag & mark)
 390                    : (sline[i].flag & mark))
 391                        return i;
 392                else
 393                        i++;
 394        return cnt;
 395}
 396
 397static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
 398{
 399        unsigned long all_mask = (1UL<<num_parent) - 1;
 400        unsigned long mark = (1UL<<num_parent);
 401        unsigned long i;
 402
 403        /* Two groups of interesting lines may have a short gap of
 404         * unintersting lines.  Connect such groups to give them a
 405         * bit of context.
 406         *
 407         * We first start from what the interesting() function says,
 408         * and mark them with "mark", and paint context lines with the
 409         * mark.  So interesting() would still say false for such context
 410         * lines but they are treated as "interesting" in the end.
 411         */
 412        i = find_next(sline, mark, 0, cnt, 0);
 413        if (cnt <= i)
 414                return 0;
 415
 416        while (i < cnt) {
 417                unsigned long j = (context < i) ? (i - context) : 0;
 418                unsigned long k;
 419
 420                /* Paint a few lines before the first interesting line. */
 421                while (j < i)
 422                        sline[j++].flag |= mark;
 423
 424        again:
 425                /* we know up to i is to be included.  where does the
 426                 * next uninteresting one start?
 427                 */
 428                j = find_next(sline, mark, i, cnt, 1);
 429                if (cnt <= j)
 430                        break; /* the rest are all interesting */
 431
 432                /* lookahead context lines */
 433                k = find_next(sline, mark, j, cnt, 0);
 434                j = adjust_hunk_tail(sline, all_mask, i, j);
 435
 436                if (k < j + context) {
 437                        /* k is interesting and [j,k) are not, but
 438                         * paint them interesting because the gap is small.
 439                         */
 440                        while (j < k)
 441                                sline[j++].flag |= mark;
 442                        i = k;
 443                        goto again;
 444                }
 445
 446                /* j is the first uninteresting line and there is
 447                 * no overlap beyond it within context lines.  Paint
 448                 * the trailing edge a bit.
 449                 */
 450                i = k;
 451                k = (j + context < cnt) ? j + context : cnt;
 452                while (j < k)
 453                        sline[j++].flag |= mark;
 454        }
 455        return 1;
 456}
 457
 458static int make_hunks(struct sline *sline, unsigned long cnt,
 459                       int num_parent, int dense)
 460{
 461        unsigned long all_mask = (1UL<<num_parent) - 1;
 462        unsigned long mark = (1UL<<num_parent);
 463        unsigned long i;
 464        int has_interesting = 0;
 465
 466        for (i = 0; i < cnt; i++) {
 467                if (interesting(&sline[i], all_mask))
 468                        sline[i].flag |= mark;
 469                else
 470                        sline[i].flag &= ~mark;
 471        }
 472        if (!dense)
 473                return give_context(sline, cnt, num_parent);
 474
 475        /* Look at each hunk, and if we have changes from only one
 476         * parent, or the changes are the same from all but one
 477         * parent, mark that uninteresting.
 478         */
 479        i = 0;
 480        while (i < cnt) {
 481                unsigned long j, hunk_begin, hunk_end;
 482                unsigned long same_diff;
 483                while (i < cnt && !(sline[i].flag & mark))
 484                        i++;
 485                if (cnt <= i)
 486                        break; /* No more interesting hunks */
 487                hunk_begin = i;
 488                for (j = i + 1; j < cnt; j++) {
 489                        if (!(sline[j].flag & mark)) {
 490                                /* Look beyond the end to see if there
 491                                 * is an interesting line after this
 492                                 * hunk within context span.
 493                                 */
 494                                unsigned long la; /* lookahead */
 495                                int contin = 0;
 496                                la = adjust_hunk_tail(sline, all_mask,
 497                                                     hunk_begin, j);
 498                                la = (la + context < cnt) ?
 499                                        (la + context) : cnt;
 500                                while (j <= --la) {
 501                                        if (sline[la].flag & mark) {
 502                                                contin = 1;
 503                                                break;
 504                                        }
 505                                }
 506                                if (!contin)
 507                                        break;
 508                                j = la;
 509                        }
 510                }
 511                hunk_end = j;
 512
 513                /* [i..hunk_end) are interesting.  Now is it really
 514                 * interesting?  We check if there are only two versions
 515                 * and the result matches one of them.  That is, we look
 516                 * at:
 517                 *   (+) line, which records lines added to which parents;
 518                 *       this line appears in the result.
 519                 *   (-) line, which records from what parents the line
 520                 *       was removed; this line does not appear in the result.
 521                 * then check the set of parents the result has difference
 522                 * from, from all lines.  If there are lines that has
 523                 * different set of parents that the result has differences
 524                 * from, that means we have more than two versions.
 525                 *
 526                 * Even when we have only two versions, if the result does
 527                 * not match any of the parents, the it should be considered
 528                 * interesting.  In such a case, we would have all '+' line.
 529                 * After passing the above "two versions" test, that would
 530                 * appear as "the same set of parents" to be "all parents".
 531                 */
 532                same_diff = 0;
 533                has_interesting = 0;
 534                for (j = i; j < hunk_end && !has_interesting; j++) {
 535                        unsigned long this_diff = sline[j].flag & all_mask;
 536                        struct lline *ll = sline[j].lost_head;
 537                        if (this_diff) {
 538                                /* This has some changes.  Is it the
 539                                 * same as others?
 540                                 */
 541                                if (!same_diff)
 542                                        same_diff = this_diff;
 543                                else if (same_diff != this_diff) {
 544                                        has_interesting = 1;
 545                                        break;
 546                                }
 547                        }
 548                        while (ll && !has_interesting) {
 549                                /* Lost this line from these parents;
 550                                 * who are they?  Are they the same?
 551                                 */
 552                                this_diff = ll->parent_map;
 553                                if (!same_diff)
 554                                        same_diff = this_diff;
 555                                else if (same_diff != this_diff) {
 556                                        has_interesting = 1;
 557                                }
 558                                ll = ll->next;
 559                        }
 560                }
 561
 562                if (!has_interesting && same_diff != all_mask) {
 563                        /* This hunk is not that interesting after all */
 564                        for (j = hunk_begin; j < hunk_end; j++)
 565                                sline[j].flag &= ~mark;
 566                }
 567                i = hunk_end;
 568        }
 569
 570        has_interesting = give_context(sline, cnt, num_parent);
 571        return has_interesting;
 572}
 573
 574static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, unsigned long cnt, int n)
 575{
 576        l0 = sline[l0].p_lno[n];
 577        l1 = sline[l1].p_lno[n];
 578        printf(" -%lu,%lu", l0, l1-l0);
 579}
 580
 581static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
 582{
 583        unsigned long mark = (1UL<<num_parent);
 584        int i;
 585        unsigned long lno = 0;
 586
 587        if (!cnt)
 588                return; /* result deleted */
 589
 590        while (1) {
 591                struct sline *sl = &sline[lno];
 592                int hunk_end;
 593                while (lno < cnt && !(sline[lno].flag & mark))
 594                        lno++;
 595                if (cnt <= lno)
 596                        break;
 597                for (hunk_end = lno + 1; hunk_end < cnt; hunk_end++)
 598                        if (!(sline[hunk_end].flag & mark))
 599                                break;
 600                for (i = 0; i <= num_parent; i++) putchar(combine_marker);
 601                for (i = 0; i < num_parent; i++)
 602                        show_parent_lno(sline, lno, hunk_end, cnt, i);
 603                printf(" +%lu,%lu ", lno+1, hunk_end-lno);
 604                for (i = 0; i <= num_parent; i++) putchar(combine_marker);
 605                putchar('\n');
 606                while (lno < hunk_end) {
 607                        struct lline *ll;
 608                        int j;
 609                        unsigned long p_mask;
 610                        sl = &sline[lno++];
 611                        ll = sl->lost_head;
 612                        while (ll) {
 613                                for (j = 0; j < num_parent; j++) {
 614                                        if (ll->parent_map & (1UL<<j))
 615                                                putchar('-');
 616                                        else
 617                                                putchar(' ');
 618                                }
 619                                puts(ll->line);
 620                                ll = ll->next;
 621                        }
 622                        p_mask = 1;
 623                        for (j = 0; j < num_parent; j++) {
 624                                if (p_mask & sl->flag)
 625                                        putchar('+');
 626                                else
 627                                        putchar(' ');
 628                                p_mask <<= 1;
 629                        }
 630                        printf("%.*s\n", sl->len, sl->bol);
 631                }
 632        }
 633}
 634
 635static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
 636                               int i, int j)
 637{
 638        /* We have already examined parent j and we know parent i
 639         * and parent j are the same, so reuse the combined result
 640         * of parent j for parent i.
 641         */
 642        unsigned long lno, imask, jmask;
 643        imask = (1UL<<i);
 644        jmask = (1UL<<j);
 645
 646        for (lno = 0; lno < cnt; lno++) {
 647                struct lline *ll = sline->lost_head;
 648                sline->p_lno[i] = sline->p_lno[j];
 649                while (ll) {
 650                        if (ll->parent_map & jmask)
 651                                ll->parent_map |= imask;
 652                        ll = ll->next;
 653                }
 654                if (sline->flag & jmask)
 655                        sline->flag |= imask;
 656                sline++;
 657        }
 658        /* the overall size of the file (sline[cnt]) */
 659        sline->p_lno[i] = sline->p_lno[j];
 660}
 661
 662static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
 663                           int dense, const char *header,
 664                           struct diff_options *opt)
 665{
 666        unsigned long result_size, cnt, lno;
 667        char *result, *cp, *ep;
 668        struct sline *sline; /* survived lines */
 669        int mode_differs = 0;
 670        int i, show_hunks, shown_header = 0;
 671        int working_tree_file = !memcmp(elem->sha1, null_sha1, 20);
 672        int abbrev = opt->full_index ? 40 : DEFAULT_ABBREV;
 673        mmfile_t result_file;
 674
 675        /* Read the result of merge first */
 676        if (!working_tree_file)
 677                result = grab_blob(elem->sha1, &result_size);
 678        else {
 679                /* Used by diff-tree to read from the working tree */
 680                struct stat st;
 681                int fd;
 682                if (0 <= (fd = open(elem->path, O_RDONLY)) &&
 683                    !fstat(fd, &st)) {
 684                        int len = st.st_size;
 685                        int cnt = 0;
 686
 687                        elem->mode = canon_mode(st.st_mode);
 688                        result_size = len;
 689                        result = xmalloc(len + 1);
 690                        while (cnt < len) {
 691                                int done = xread(fd, result+cnt, len-cnt);
 692                                if (done == 0)
 693                                        break;
 694                                if (done < 0)
 695                                        die("read error '%s'", elem->path);
 696                                cnt += done;
 697                        }
 698                        result[len] = 0;
 699                }
 700                else {
 701                        /* deleted file */
 702                        result_size = 0;
 703                        elem->mode = 0;
 704                        result = xmalloc(1);
 705                        result[0] = 0;
 706                }
 707                if (0 <= fd)
 708                        close(fd);
 709        }
 710
 711        for (cnt = 0, cp = result; cp - result < result_size; cp++) {
 712                if (*cp == '\n')
 713                        cnt++;
 714        }
 715        if (result_size && result[result_size-1] != '\n')
 716                cnt++; /* incomplete line */
 717
 718        sline = xcalloc(cnt+1, sizeof(*sline));
 719        ep = result;
 720        sline[0].bol = result;
 721        for (lno = 0; lno <= cnt; lno++) {
 722                sline[lno].lost_tail = &sline[lno].lost_head;
 723                sline[lno].flag = 0;
 724        }
 725        for (lno = 0, cp = result; cp - result < result_size; cp++) {
 726                if (*cp == '\n') {
 727                        sline[lno].len = cp - sline[lno].bol;
 728                        lno++;
 729                        if (lno < cnt)
 730                                sline[lno].bol = cp + 1;
 731                }
 732        }
 733        if (result_size && result[result_size-1] != '\n')
 734                sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
 735
 736        result_file.ptr = result;
 737        result_file.size = result_size;
 738
 739        sline[0].p_lno = xcalloc((cnt+1) * num_parent, sizeof(unsigned long));
 740        for (lno = 0; lno < cnt; lno++)
 741                sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
 742
 743        for (i = 0; i < num_parent; i++) {
 744                int j;
 745                for (j = 0; j < i; j++) {
 746                        if (!memcmp(elem->parent[i].sha1,
 747                                    elem->parent[j].sha1, 20)) {
 748                                reuse_combine_diff(sline, cnt, i, j);
 749                                break;
 750                        }
 751                }
 752                if (i <= j)
 753                        combine_diff(elem->parent[i].sha1, &result_file, sline,
 754                                     cnt, i, num_parent);
 755                if (elem->parent[i].mode != elem->mode)
 756                        mode_differs = 1;
 757        }
 758
 759        show_hunks = make_hunks(sline, cnt, num_parent, dense);
 760
 761        if (show_hunks || mode_differs || working_tree_file) {
 762                const char *abb;
 763
 764                if (header) {
 765                        shown_header++;
 766                        printf("%s%c", header, opt->line_termination);
 767                }
 768                printf("diff --%s ", dense ? "cc" : "combined");
 769                if (quote_c_style(elem->path, NULL, NULL, 0))
 770                        quote_c_style(elem->path, NULL, stdout, 0);
 771                else
 772                        printf("%s", elem->path);
 773                putchar('\n');
 774                printf("index ");
 775                for (i = 0; i < num_parent; i++) {
 776                        abb = find_unique_abbrev(elem->parent[i].sha1,
 777                                                 abbrev);
 778                        printf("%s%s", i ? "," : "", abb);
 779                }
 780                abb = find_unique_abbrev(elem->sha1, abbrev);
 781                printf("..%s\n", abb);
 782
 783                if (mode_differs) {
 784                        int added = !!elem->mode;
 785                        for (i = 0; added && i < num_parent; i++)
 786                                if (elem->parent[i].status !=
 787                                    DIFF_STATUS_ADDED)
 788                                        added = 0;
 789                        if (added)
 790                                printf("new file mode %06o", elem->mode);
 791                        else {
 792                                if (!elem->mode)
 793                                        printf("deleted file ");
 794                                printf("mode ");
 795                                for (i = 0; i < num_parent; i++) {
 796                                        printf("%s%06o", i ? "," : "",
 797                                               elem->parent[i].mode);
 798                                }
 799                                if (elem->mode)
 800                                        printf("..%06o", elem->mode);
 801                        }
 802                        putchar('\n');
 803                }
 804                dump_sline(sline, cnt, num_parent);
 805        }
 806        free(result);
 807
 808        for (i = 0; i < cnt; i++) {
 809                if (sline[i].lost_head) {
 810                        struct lline *ll = sline[i].lost_head;
 811                        while (ll) {
 812                                struct lline *tmp = ll;
 813                                ll = ll->next;
 814                                free(tmp);
 815                        }
 816                }
 817        }
 818        free(sline[0].p_lno);
 819        free(sline);
 820        return shown_header;
 821}
 822
 823#define COLONS "::::::::::::::::::::::::::::::::"
 824
 825static void show_raw_diff(struct combine_diff_path *p, int num_parent, const char *header, struct diff_options *opt)
 826{
 827        int i, offset, mod_type = 'A';
 828        const char *prefix;
 829        int line_termination, inter_name_termination;
 830
 831        line_termination = opt->line_termination;
 832        inter_name_termination = '\t';
 833        if (!line_termination)
 834                inter_name_termination = 0;
 835
 836        if (header)
 837                printf("%s%c", header, line_termination);
 838
 839        for (i = 0; i < num_parent; i++) {
 840                if (p->parent[i].mode)
 841                        mod_type = 'M';
 842        }
 843        if (!p->mode)
 844                mod_type = 'D';
 845
 846        if (opt->output_format == DIFF_FORMAT_RAW) {
 847                offset = strlen(COLONS) - num_parent;
 848                if (offset < 0)
 849                        offset = 0;
 850                prefix = COLONS + offset;
 851
 852                /* Show the modes */
 853                for (i = 0; i < num_parent; i++) {
 854                        printf("%s%06o", prefix, p->parent[i].mode);
 855                        prefix = " ";
 856                }
 857                printf("%s%06o", prefix, p->mode);
 858
 859                /* Show sha1's */
 860                for (i = 0; i < num_parent; i++)
 861                        printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
 862                                                         opt->abbrev));
 863                printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
 864        }
 865
 866        if (opt->output_format == DIFF_FORMAT_RAW ||
 867            opt->output_format == DIFF_FORMAT_NAME_STATUS) {
 868                for (i = 0; i < num_parent; i++)
 869                        putchar(p->parent[i].status);
 870                putchar(inter_name_termination);
 871        }
 872
 873        if (line_termination) {
 874                if (quote_c_style(p->path, NULL, NULL, 0))
 875                        quote_c_style(p->path, NULL, stdout, 0);
 876                else
 877                        printf("%s", p->path);
 878                putchar(line_termination);
 879        }
 880        else {
 881                printf("%s%c", p->path, line_termination);
 882        }
 883}
 884
 885int show_combined_diff(struct combine_diff_path *p,
 886                       int num_parent,
 887                       int dense,
 888                       const char *header,
 889                       struct diff_options *opt)
 890{
 891        if (!p->len)
 892                return 0;
 893        switch (opt->output_format) {
 894        case DIFF_FORMAT_RAW:
 895        case DIFF_FORMAT_NAME_STATUS:
 896        case DIFF_FORMAT_NAME:
 897                show_raw_diff(p, num_parent, header, opt);
 898                return 1;
 899
 900        default:
 901        case DIFF_FORMAT_PATCH:
 902                return show_patch_diff(p, num_parent, dense, header, opt);
 903        }
 904}
 905
 906const char *diff_tree_combined_merge(const unsigned char *sha1,
 907                             const char *header, int dense,
 908                             struct diff_options *opt)
 909{
 910        struct commit *commit = lookup_commit(sha1);
 911        struct diff_options diffopts;
 912        struct commit_list *parents;
 913        struct combine_diff_path *p, *paths = NULL;
 914        int num_parent, i, num_paths;
 915
 916        diffopts = *opt;
 917        diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
 918        diffopts.recursive = 1;
 919
 920        /* count parents */
 921        for (parents = commit->parents, num_parent = 0;
 922             parents;
 923             parents = parents->next, num_parent++)
 924                ; /* nothing */
 925
 926        /* find set of paths that everybody touches */
 927        for (parents = commit->parents, i = 0;
 928             parents;
 929             parents = parents->next, i++) {
 930                struct commit *parent = parents->item;
 931                diff_tree_sha1(parent->object.sha1, commit->object.sha1, "",
 932                               &diffopts);
 933                diffcore_std(&diffopts);
 934                paths = intersect_paths(paths, i, num_parent);
 935                diff_flush(&diffopts);
 936        }
 937
 938        /* find out surviving paths */
 939        for (num_paths = 0, p = paths; p; p = p->next) {
 940                if (p->len)
 941                        num_paths++;
 942        }
 943        if (num_paths) {
 944                for (p = paths; p; p = p->next) {
 945                        if (show_combined_diff(p, num_parent, dense,
 946                                               header, opt))
 947                                header = NULL;
 948                }
 949        }
 950
 951        /* Clean things up */
 952        while (paths) {
 953                struct combine_diff_path *tmp = paths;
 954                paths = paths->next;
 955                free(tmp);
 956        }
 957        return header;
 958}