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