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