combine-diff.con commit Merge branch 'nd/clear-gitenv-upon-use-of-alias' (dbda66b)
   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 "xdiff/xmacros.h"
   9#include "log-tree.h"
  10#include "refs.h"
  11#include "userdiff.h"
  12#include "sha1-array.h"
  13#include "revision.h"
  14
  15static int compare_paths(const struct combine_diff_path *one,
  16                          const struct diff_filespec *two)
  17{
  18        if (!S_ISDIR(one->mode) && !S_ISDIR(two->mode))
  19                return strcmp(one->path, two->path);
  20
  21        return base_name_compare(one->path, strlen(one->path), one->mode,
  22                                 two->path, strlen(two->path), two->mode);
  23}
  24
  25static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
  26{
  27        struct diff_queue_struct *q = &diff_queued_diff;
  28        struct combine_diff_path *p, **tail = &curr;
  29        int i, cmp;
  30
  31        if (!n) {
  32                for (i = 0; i < q->nr; i++) {
  33                        int len;
  34                        const char *path;
  35                        if (diff_unmodified_pair(q->queue[i]))
  36                                continue;
  37                        path = q->queue[i]->two->path;
  38                        len = strlen(path);
  39                        p = xmalloc(combine_diff_path_size(num_parent, len));
  40                        p->path = (char *) &(p->parent[num_parent]);
  41                        memcpy(p->path, path, len);
  42                        p->path[len] = 0;
  43                        p->next = NULL;
  44                        memset(p->parent, 0,
  45                               sizeof(p->parent[0]) * num_parent);
  46
  47                        hashcpy(p->oid.hash, q->queue[i]->two->sha1);
  48                        p->mode = q->queue[i]->two->mode;
  49                        hashcpy(p->parent[n].oid.hash, q->queue[i]->one->sha1);
  50                        p->parent[n].mode = q->queue[i]->one->mode;
  51                        p->parent[n].status = q->queue[i]->status;
  52                        *tail = p;
  53                        tail = &p->next;
  54                }
  55                return curr;
  56        }
  57
  58        /*
  59         * paths in curr (linked list) and q->queue[] (array) are
  60         * both sorted in the tree order.
  61         */
  62        i = 0;
  63        while ((p = *tail) != NULL) {
  64                cmp = ((i >= q->nr)
  65                       ? -1 : compare_paths(p, q->queue[i]->two));
  66
  67                if (cmp < 0) {
  68                        /* p->path not in q->queue[]; drop it */
  69                        *tail = p->next;
  70                        free(p);
  71                        continue;
  72                }
  73
  74                if (cmp > 0) {
  75                        /* q->queue[i] not in p->path; skip it */
  76                        i++;
  77                        continue;
  78                }
  79
  80                hashcpy(p->parent[n].oid.hash, q->queue[i]->one->sha1);
  81                p->parent[n].mode = q->queue[i]->one->mode;
  82                p->parent[n].status = q->queue[i]->status;
  83
  84                tail = &p->next;
  85                i++;
  86        }
  87        return curr;
  88}
  89
  90/* Lines lost from parent */
  91struct lline {
  92        struct lline *next, *prev;
  93        int len;
  94        unsigned long parent_map;
  95        char line[FLEX_ARRAY];
  96};
  97
  98/* Lines lost from current parent (before coalescing) */
  99struct plost {
 100        struct lline *lost_head, *lost_tail;
 101        int len;
 102};
 103
 104/* Lines surviving in the merge result */
 105struct sline {
 106        /* Accumulated and coalesced lost lines */
 107        struct lline *lost;
 108        int lenlost;
 109        struct plost plost;
 110        char *bol;
 111        int len;
 112        /* bit 0 up to (N-1) are on if the parent has this line (i.e.
 113         * we did not change it).
 114         * bit N is used for "interesting" lines, including context.
 115         * bit (N+1) is used for "do not show deletion before this".
 116         */
 117        unsigned long flag;
 118        unsigned long *p_lno;
 119};
 120
 121static int match_string_spaces(const char *line1, int len1,
 122                               const char *line2, int len2,
 123                               long flags)
 124{
 125        if (flags & XDF_WHITESPACE_FLAGS) {
 126                for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
 127                for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
 128        }
 129
 130        if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
 131                return (len1 == len2 && !memcmp(line1, line2, len1));
 132
 133        while (len1 > 0 && len2 > 0) {
 134                len1--;
 135                len2--;
 136                if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
 137                        if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
 138                            (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
 139                                return 0;
 140
 141                        for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
 142                        for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
 143                }
 144                if (line1[len1] != line2[len2])
 145                        return 0;
 146        }
 147
 148        if (flags & XDF_IGNORE_WHITESPACE) {
 149                /* Consume remaining spaces */
 150                for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
 151                for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
 152        }
 153
 154        /* We matched full line1 and line2 */
 155        if (!len1 && !len2)
 156                return 1;
 157
 158        return 0;
 159}
 160
 161enum coalesce_direction { MATCH, BASE, NEW };
 162
 163/* Coalesce new lines into base by finding LCS */
 164static struct lline *coalesce_lines(struct lline *base, int *lenbase,
 165                                    struct lline *new, int lennew,
 166                                    unsigned long parent, long flags)
 167{
 168        int **lcs;
 169        enum coalesce_direction **directions;
 170        struct lline *baseend, *newend = NULL;
 171        int i, j, origbaselen = *lenbase;
 172
 173        if (new == NULL)
 174                return base;
 175
 176        if (base == NULL) {
 177                *lenbase = lennew;
 178                return new;
 179        }
 180
 181        /*
 182         * Coalesce new lines into base by finding the LCS
 183         * - Create the table to run dynamic programming
 184         * - Compute the LCS
 185         * - Then reverse read the direction structure:
 186         *   - If we have MATCH, assign parent to base flag, and consume
 187         *   both baseend and newend
 188         *   - Else if we have BASE, consume baseend
 189         *   - Else if we have NEW, insert newend lline into base and
 190         *   consume newend
 191         */
 192        lcs = xcalloc(origbaselen + 1, sizeof(int*));
 193        directions = xcalloc(origbaselen + 1, sizeof(enum coalesce_direction*));
 194        for (i = 0; i < origbaselen + 1; i++) {
 195                lcs[i] = xcalloc(lennew + 1, sizeof(int));
 196                directions[i] = xcalloc(lennew + 1, sizeof(enum coalesce_direction));
 197                directions[i][0] = BASE;
 198        }
 199        for (j = 1; j < lennew + 1; j++)
 200                directions[0][j] = NEW;
 201
 202        for (i = 1, baseend = base; i < origbaselen + 1; i++) {
 203                for (j = 1, newend = new; j < lennew + 1; j++) {
 204                        if (match_string_spaces(baseend->line, baseend->len,
 205                                                newend->line, newend->len, flags)) {
 206                                lcs[i][j] = lcs[i - 1][j - 1] + 1;
 207                                directions[i][j] = MATCH;
 208                        } else if (lcs[i][j - 1] >= lcs[i - 1][j]) {
 209                                lcs[i][j] = lcs[i][j - 1];
 210                                directions[i][j] = NEW;
 211                        } else {
 212                                lcs[i][j] = lcs[i - 1][j];
 213                                directions[i][j] = BASE;
 214                        }
 215                        if (newend->next)
 216                                newend = newend->next;
 217                }
 218                if (baseend->next)
 219                        baseend = baseend->next;
 220        }
 221
 222        for (i = 0; i < origbaselen + 1; i++)
 223                free(lcs[i]);
 224        free(lcs);
 225
 226        /* At this point, baseend and newend point to the end of each lists */
 227        i--;
 228        j--;
 229        while (i != 0 || j != 0) {
 230                if (directions[i][j] == MATCH) {
 231                        baseend->parent_map |= 1<<parent;
 232                        baseend = baseend->prev;
 233                        newend = newend->prev;
 234                        i--;
 235                        j--;
 236                } else if (directions[i][j] == NEW) {
 237                        struct lline *lline;
 238
 239                        lline = newend;
 240                        /* Remove lline from new list and update newend */
 241                        if (lline->prev)
 242                                lline->prev->next = lline->next;
 243                        else
 244                                new = lline->next;
 245                        if (lline->next)
 246                                lline->next->prev = lline->prev;
 247
 248                        newend = lline->prev;
 249                        j--;
 250
 251                        /* Add lline to base list */
 252                        if (baseend) {
 253                                lline->next = baseend->next;
 254                                lline->prev = baseend;
 255                                if (lline->prev)
 256                                        lline->prev->next = lline;
 257                        }
 258                        else {
 259                                lline->next = base;
 260                                base = lline;
 261                        }
 262                        (*lenbase)++;
 263
 264                        if (lline->next)
 265                                lline->next->prev = lline;
 266
 267                } else {
 268                        baseend = baseend->prev;
 269                        i--;
 270                }
 271        }
 272
 273        newend = new;
 274        while (newend) {
 275                struct lline *lline = newend;
 276                newend = newend->next;
 277                free(lline);
 278        }
 279
 280        for (i = 0; i < origbaselen + 1; i++)
 281                free(directions[i]);
 282        free(directions);
 283
 284        return base;
 285}
 286
 287static char *grab_blob(const struct object_id *oid, unsigned int mode,
 288                       unsigned long *size, struct userdiff_driver *textconv,
 289                       const char *path)
 290{
 291        char *blob;
 292        enum object_type type;
 293
 294        if (S_ISGITLINK(mode)) {
 295                blob = xmalloc(100);
 296                *size = snprintf(blob, 100,
 297                                 "Subproject commit %s\n", oid_to_hex(oid));
 298        } else if (is_null_oid(oid)) {
 299                /* deleted blob */
 300                *size = 0;
 301                return xcalloc(1, 1);
 302        } else if (textconv) {
 303                struct diff_filespec *df = alloc_filespec(path);
 304                fill_filespec(df, oid->hash, 1, mode);
 305                *size = fill_textconv(textconv, df, &blob);
 306                free_filespec(df);
 307        } else {
 308                blob = read_sha1_file(oid->hash, &type, size);
 309                if (type != OBJ_BLOB)
 310                        die("object '%s' is not a blob!", oid_to_hex(oid));
 311        }
 312        return blob;
 313}
 314
 315static void append_lost(struct sline *sline, int n, const char *line, int len)
 316{
 317        struct lline *lline;
 318        unsigned long this_mask = (1UL<<n);
 319        if (line[len-1] == '\n')
 320                len--;
 321
 322        lline = xmalloc(sizeof(*lline) + len + 1);
 323        lline->len = len;
 324        lline->next = NULL;
 325        lline->prev = sline->plost.lost_tail;
 326        if (lline->prev)
 327                lline->prev->next = lline;
 328        else
 329                sline->plost.lost_head = lline;
 330        sline->plost.lost_tail = lline;
 331        sline->plost.len++;
 332        lline->parent_map = this_mask;
 333        memcpy(lline->line, line, len);
 334        lline->line[len] = 0;
 335}
 336
 337struct combine_diff_state {
 338        unsigned int lno;
 339        int ob, on, nb, nn;
 340        unsigned long nmask;
 341        int num_parent;
 342        int n;
 343        struct sline *sline;
 344        struct sline *lost_bucket;
 345};
 346
 347static void consume_line(void *state_, char *line, unsigned long len)
 348{
 349        struct combine_diff_state *state = state_;
 350        if (5 < len && !memcmp("@@ -", line, 4)) {
 351                if (parse_hunk_header(line, len,
 352                                      &state->ob, &state->on,
 353                                      &state->nb, &state->nn))
 354                        return;
 355                state->lno = state->nb;
 356                if (state->nn == 0) {
 357                        /* @@ -X,Y +N,0 @@ removed Y lines
 358                         * that would have come *after* line N
 359                         * in the result.  Our lost buckets hang
 360                         * to the line after the removed lines,
 361                         *
 362                         * Note that this is correct even when N == 0,
 363                         * in which case the hunk removes the first
 364                         * line in the file.
 365                         */
 366                        state->lost_bucket = &state->sline[state->nb];
 367                        if (!state->nb)
 368                                state->nb = 1;
 369                } else {
 370                        state->lost_bucket = &state->sline[state->nb-1];
 371                }
 372                if (!state->sline[state->nb-1].p_lno)
 373                        state->sline[state->nb-1].p_lno =
 374                                xcalloc(state->num_parent,
 375                                        sizeof(unsigned long));
 376                state->sline[state->nb-1].p_lno[state->n] = state->ob;
 377                return;
 378        }
 379        if (!state->lost_bucket)
 380                return; /* not in any hunk yet */
 381        switch (line[0]) {
 382        case '-':
 383                append_lost(state->lost_bucket, state->n, line+1, len-1);
 384                break;
 385        case '+':
 386                state->sline[state->lno-1].flag |= state->nmask;
 387                state->lno++;
 388                break;
 389        }
 390}
 391
 392static void combine_diff(const struct object_id *parent, unsigned int mode,
 393                         mmfile_t *result_file,
 394                         struct sline *sline, unsigned int cnt, int n,
 395                         int num_parent, int result_deleted,
 396                         struct userdiff_driver *textconv,
 397                         const char *path, long flags)
 398{
 399        unsigned int p_lno, lno;
 400        unsigned long nmask = (1UL << n);
 401        xpparam_t xpp;
 402        xdemitconf_t xecfg;
 403        mmfile_t parent_file;
 404        struct combine_diff_state state;
 405        unsigned long sz;
 406
 407        if (result_deleted)
 408                return; /* result deleted */
 409
 410        parent_file.ptr = grab_blob(parent, mode, &sz, textconv, path);
 411        parent_file.size = sz;
 412        memset(&xpp, 0, sizeof(xpp));
 413        xpp.flags = flags;
 414        memset(&xecfg, 0, sizeof(xecfg));
 415        memset(&state, 0, sizeof(state));
 416        state.nmask = nmask;
 417        state.sline = sline;
 418        state.lno = 1;
 419        state.num_parent = num_parent;
 420        state.n = n;
 421
 422        if (xdi_diff_outf(&parent_file, result_file, consume_line, &state,
 423                          &xpp, &xecfg))
 424                die("unable to generate combined diff for %s",
 425                    oid_to_hex(parent));
 426        free(parent_file.ptr);
 427
 428        /* Assign line numbers for this parent.
 429         *
 430         * sline[lno].p_lno[n] records the first line number
 431         * (counting from 1) for parent N if the final hunk display
 432         * started by showing sline[lno] (possibly showing the lost
 433         * lines attached to it first).
 434         */
 435        for (lno = 0,  p_lno = 1; lno <= cnt; lno++) {
 436                struct lline *ll;
 437                sline[lno].p_lno[n] = p_lno;
 438
 439                /* Coalesce new lines */
 440                if (sline[lno].plost.lost_head) {
 441                        struct sline *sl = &sline[lno];
 442                        sl->lost = coalesce_lines(sl->lost, &sl->lenlost,
 443                                                  sl->plost.lost_head,
 444                                                  sl->plost.len, n, flags);
 445                        sl->plost.lost_head = sl->plost.lost_tail = NULL;
 446                        sl->plost.len = 0;
 447                }
 448
 449                /* How many lines would this sline advance the p_lno? */
 450                ll = sline[lno].lost;
 451                while (ll) {
 452                        if (ll->parent_map & nmask)
 453                                p_lno++; /* '-' means parent had it */
 454                        ll = ll->next;
 455                }
 456                if (lno < cnt && !(sline[lno].flag & nmask))
 457                        p_lno++; /* no '+' means parent had it */
 458        }
 459        sline[lno].p_lno[n] = p_lno; /* trailer */
 460}
 461
 462static unsigned long context = 3;
 463static char combine_marker = '@';
 464
 465static int interesting(struct sline *sline, unsigned long all_mask)
 466{
 467        /* If some parents lost lines here, or if we have added to
 468         * some parent, it is interesting.
 469         */
 470        return ((sline->flag & all_mask) || sline->lost);
 471}
 472
 473static unsigned long adjust_hunk_tail(struct sline *sline,
 474                                      unsigned long all_mask,
 475                                      unsigned long hunk_begin,
 476                                      unsigned long i)
 477{
 478        /* i points at the first uninteresting line.  If the last line
 479         * of the hunk was interesting only because it has some
 480         * deletion, then it is not all that interesting for the
 481         * purpose of giving trailing context lines.  This is because
 482         * we output '-' line and then unmodified sline[i-1] itself in
 483         * that case which gives us one extra context line.
 484         */
 485        if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
 486                i--;
 487        return i;
 488}
 489
 490static unsigned long find_next(struct sline *sline,
 491                               unsigned long mark,
 492                               unsigned long i,
 493                               unsigned long cnt,
 494                               int look_for_uninteresting)
 495{
 496        /* We have examined up to i-1 and are about to look at i.
 497         * Find next interesting or uninteresting line.  Here,
 498         * "interesting" does not mean interesting(), but marked by
 499         * the give_context() function below (i.e. it includes context
 500         * lines that are not interesting to interesting() function
 501         * that are surrounded by interesting() ones.
 502         */
 503        while (i <= cnt)
 504                if (look_for_uninteresting
 505                    ? !(sline[i].flag & mark)
 506                    : (sline[i].flag & mark))
 507                        return i;
 508                else
 509                        i++;
 510        return i;
 511}
 512
 513static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
 514{
 515        unsigned long all_mask = (1UL<<num_parent) - 1;
 516        unsigned long mark = (1UL<<num_parent);
 517        unsigned long no_pre_delete = (2UL<<num_parent);
 518        unsigned long i;
 519
 520        /* Two groups of interesting lines may have a short gap of
 521         * uninteresting lines.  Connect such groups to give them a
 522         * bit of context.
 523         *
 524         * We first start from what the interesting() function says,
 525         * and mark them with "mark", and paint context lines with the
 526         * mark.  So interesting() would still say false for such context
 527         * lines but they are treated as "interesting" in the end.
 528         */
 529        i = find_next(sline, mark, 0, cnt, 0);
 530        if (cnt < i)
 531                return 0;
 532
 533        while (i <= cnt) {
 534                unsigned long j = (context < i) ? (i - context) : 0;
 535                unsigned long k;
 536
 537                /* Paint a few lines before the first interesting line. */
 538                while (j < i) {
 539                        if (!(sline[j].flag & mark))
 540                                sline[j].flag |= no_pre_delete;
 541                        sline[j++].flag |= mark;
 542                }
 543
 544        again:
 545                /* we know up to i is to be included.  where does the
 546                 * next uninteresting one start?
 547                 */
 548                j = find_next(sline, mark, i, cnt, 1);
 549                if (cnt < j)
 550                        break; /* the rest are all interesting */
 551
 552                /* lookahead context lines */
 553                k = find_next(sline, mark, j, cnt, 0);
 554                j = adjust_hunk_tail(sline, all_mask, i, j);
 555
 556                if (k < j + context) {
 557                        /* k is interesting and [j,k) are not, but
 558                         * paint them interesting because the gap is small.
 559                         */
 560                        while (j < k)
 561                                sline[j++].flag |= mark;
 562                        i = k;
 563                        goto again;
 564                }
 565
 566                /* j is the first uninteresting line and there is
 567                 * no overlap beyond it within context lines.  Paint
 568                 * the trailing edge a bit.
 569                 */
 570                i = k;
 571                k = (j + context < cnt+1) ? j + context : cnt+1;
 572                while (j < k)
 573                        sline[j++].flag |= mark;
 574        }
 575        return 1;
 576}
 577
 578static int make_hunks(struct sline *sline, unsigned long cnt,
 579                       int num_parent, int dense)
 580{
 581        unsigned long all_mask = (1UL<<num_parent) - 1;
 582        unsigned long mark = (1UL<<num_parent);
 583        unsigned long i;
 584        int has_interesting = 0;
 585
 586        for (i = 0; i <= cnt; i++) {
 587                if (interesting(&sline[i], all_mask))
 588                        sline[i].flag |= mark;
 589                else
 590                        sline[i].flag &= ~mark;
 591        }
 592        if (!dense)
 593                return give_context(sline, cnt, num_parent);
 594
 595        /* Look at each hunk, and if we have changes from only one
 596         * parent, or the changes are the same from all but one
 597         * parent, mark that uninteresting.
 598         */
 599        i = 0;
 600        while (i <= cnt) {
 601                unsigned long j, hunk_begin, hunk_end;
 602                unsigned long same_diff;
 603                while (i <= cnt && !(sline[i].flag & mark))
 604                        i++;
 605                if (cnt < i)
 606                        break; /* No more interesting hunks */
 607                hunk_begin = i;
 608                for (j = i + 1; j <= cnt; j++) {
 609                        if (!(sline[j].flag & mark)) {
 610                                /* Look beyond the end to see if there
 611                                 * is an interesting line after this
 612                                 * hunk within context span.
 613                                 */
 614                                unsigned long la; /* lookahead */
 615                                int contin = 0;
 616                                la = adjust_hunk_tail(sline, all_mask,
 617                                                     hunk_begin, j);
 618                                la = (la + context < cnt + 1) ?
 619                                        (la + context) : cnt + 1;
 620                                while (la && j <= --la) {
 621                                        if (sline[la].flag & mark) {
 622                                                contin = 1;
 623                                                break;
 624                                        }
 625                                }
 626                                if (!contin)
 627                                        break;
 628                                j = la;
 629                        }
 630                }
 631                hunk_end = j;
 632
 633                /* [i..hunk_end) are interesting.  Now is it really
 634                 * interesting?  We check if there are only two versions
 635                 * and the result matches one of them.  That is, we look
 636                 * at:
 637                 *   (+) line, which records lines added to which parents;
 638                 *       this line appears in the result.
 639                 *   (-) line, which records from what parents the line
 640                 *       was removed; this line does not appear in the result.
 641                 * then check the set of parents the result has difference
 642                 * from, from all lines.  If there are lines that has
 643                 * different set of parents that the result has differences
 644                 * from, that means we have more than two versions.
 645                 *
 646                 * Even when we have only two versions, if the result does
 647                 * not match any of the parents, the it should be considered
 648                 * interesting.  In such a case, we would have all '+' line.
 649                 * After passing the above "two versions" test, that would
 650                 * appear as "the same set of parents" to be "all parents".
 651                 */
 652                same_diff = 0;
 653                has_interesting = 0;
 654                for (j = i; j < hunk_end && !has_interesting; j++) {
 655                        unsigned long this_diff = sline[j].flag & all_mask;
 656                        struct lline *ll = sline[j].lost;
 657                        if (this_diff) {
 658                                /* This has some changes.  Is it the
 659                                 * same as others?
 660                                 */
 661                                if (!same_diff)
 662                                        same_diff = this_diff;
 663                                else if (same_diff != this_diff) {
 664                                        has_interesting = 1;
 665                                        break;
 666                                }
 667                        }
 668                        while (ll && !has_interesting) {
 669                                /* Lost this line from these parents;
 670                                 * who are they?  Are they the same?
 671                                 */
 672                                this_diff = ll->parent_map;
 673                                if (!same_diff)
 674                                        same_diff = this_diff;
 675                                else if (same_diff != this_diff) {
 676                                        has_interesting = 1;
 677                                }
 678                                ll = ll->next;
 679                        }
 680                }
 681
 682                if (!has_interesting && same_diff != all_mask) {
 683                        /* This hunk is not that interesting after all */
 684                        for (j = hunk_begin; j < hunk_end; j++)
 685                                sline[j].flag &= ~mark;
 686                }
 687                i = hunk_end;
 688        }
 689
 690        has_interesting = give_context(sline, cnt, num_parent);
 691        return has_interesting;
 692}
 693
 694static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
 695{
 696        l0 = sline[l0].p_lno[n];
 697        l1 = sline[l1].p_lno[n];
 698        printf(" -%lu,%lu", l0, l1-l0-null_context);
 699}
 700
 701static int hunk_comment_line(const char *bol)
 702{
 703        int ch;
 704
 705        if (!bol)
 706                return 0;
 707        ch = *bol & 0xff;
 708        return (isalpha(ch) || ch == '_' || ch == '$');
 709}
 710
 711static void show_line_to_eol(const char *line, int len, const char *reset)
 712{
 713        int saw_cr_at_eol = 0;
 714        if (len < 0)
 715                len = strlen(line);
 716        saw_cr_at_eol = (len && line[len-1] == '\r');
 717
 718        printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
 719               reset,
 720               saw_cr_at_eol ? "\r" : "");
 721}
 722
 723static void dump_sline(struct sline *sline, const char *line_prefix,
 724                       unsigned long cnt, int num_parent,
 725                       int use_color, int result_deleted)
 726{
 727        unsigned long mark = (1UL<<num_parent);
 728        unsigned long no_pre_delete = (2UL<<num_parent);
 729        int i;
 730        unsigned long lno = 0;
 731        const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
 732        const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
 733        const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
 734        const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
 735        const char *c_context = diff_get_color(use_color, DIFF_CONTEXT);
 736        const char *c_reset = diff_get_color(use_color, DIFF_RESET);
 737
 738        if (result_deleted)
 739                return; /* result deleted */
 740
 741        while (1) {
 742                unsigned long hunk_end;
 743                unsigned long rlines;
 744                const char *hunk_comment = NULL;
 745                unsigned long null_context = 0;
 746
 747                while (lno <= cnt && !(sline[lno].flag & mark)) {
 748                        if (hunk_comment_line(sline[lno].bol))
 749                                hunk_comment = sline[lno].bol;
 750                        lno++;
 751                }
 752                if (cnt < lno)
 753                        break;
 754                else {
 755                        for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
 756                                if (!(sline[hunk_end].flag & mark))
 757                                        break;
 758                }
 759                rlines = hunk_end - lno;
 760                if (cnt < hunk_end)
 761                        rlines--; /* pointing at the last delete hunk */
 762
 763                if (!context) {
 764                        /*
 765                         * Even when running with --unified=0, all
 766                         * lines in the hunk needs to be processed in
 767                         * the loop below in order to show the
 768                         * deletion recorded in lost_head.  However,
 769                         * we do not want to show the resulting line
 770                         * with all blank context markers in such a
 771                         * case.  Compensate.
 772                         */
 773                        unsigned long j;
 774                        for (j = lno; j < hunk_end; j++)
 775                                if (!(sline[j].flag & (mark-1)))
 776                                        null_context++;
 777                        rlines -= null_context;
 778                }
 779
 780                printf("%s%s", line_prefix, c_frag);
 781                for (i = 0; i <= num_parent; i++) putchar(combine_marker);
 782                for (i = 0; i < num_parent; i++)
 783                        show_parent_lno(sline, lno, hunk_end, i, null_context);
 784                printf(" +%lu,%lu ", lno+1, rlines);
 785                for (i = 0; i <= num_parent; i++) putchar(combine_marker);
 786
 787                if (hunk_comment) {
 788                        int comment_end = 0;
 789                        for (i = 0; i < 40; i++) {
 790                                int ch = hunk_comment[i] & 0xff;
 791                                if (!ch || ch == '\n')
 792                                        break;
 793                                if (!isspace(ch))
 794                                    comment_end = i;
 795                        }
 796                        if (comment_end)
 797                                printf("%s%s %s%s", c_reset,
 798                                                    c_context, c_reset,
 799                                                    c_func);
 800                        for (i = 0; i < comment_end; i++)
 801                                putchar(hunk_comment[i]);
 802                }
 803
 804                printf("%s\n", c_reset);
 805                while (lno < hunk_end) {
 806                        struct lline *ll;
 807                        int j;
 808                        unsigned long p_mask;
 809                        struct sline *sl = &sline[lno++];
 810                        ll = (sl->flag & no_pre_delete) ? NULL : sl->lost;
 811                        while (ll) {
 812                                printf("%s%s", line_prefix, c_old);
 813                                for (j = 0; j < num_parent; j++) {
 814                                        if (ll->parent_map & (1UL<<j))
 815                                                putchar('-');
 816                                        else
 817                                                putchar(' ');
 818                                }
 819                                show_line_to_eol(ll->line, -1, c_reset);
 820                                ll = ll->next;
 821                        }
 822                        if (cnt < lno)
 823                                break;
 824                        p_mask = 1;
 825                        fputs(line_prefix, stdout);
 826                        if (!(sl->flag & (mark-1))) {
 827                                /*
 828                                 * This sline was here to hang the
 829                                 * lost lines in front of it.
 830                                 */
 831                                if (!context)
 832                                        continue;
 833                                fputs(c_context, stdout);
 834                        }
 835                        else
 836                                fputs(c_new, stdout);
 837                        for (j = 0; j < num_parent; j++) {
 838                                if (p_mask & sl->flag)
 839                                        putchar('+');
 840                                else
 841                                        putchar(' ');
 842                                p_mask <<= 1;
 843                        }
 844                        show_line_to_eol(sl->bol, sl->len, c_reset);
 845                }
 846        }
 847}
 848
 849static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
 850                               int i, int j)
 851{
 852        /* We have already examined parent j and we know parent i
 853         * and parent j are the same, so reuse the combined result
 854         * of parent j for parent i.
 855         */
 856        unsigned long lno, imask, jmask;
 857        imask = (1UL<<i);
 858        jmask = (1UL<<j);
 859
 860        for (lno = 0; lno <= cnt; lno++) {
 861                struct lline *ll = sline->lost;
 862                sline->p_lno[i] = sline->p_lno[j];
 863                while (ll) {
 864                        if (ll->parent_map & jmask)
 865                                ll->parent_map |= imask;
 866                        ll = ll->next;
 867                }
 868                if (sline->flag & jmask)
 869                        sline->flag |= imask;
 870                sline++;
 871        }
 872        /* the overall size of the file (sline[cnt]) */
 873        sline->p_lno[i] = sline->p_lno[j];
 874}
 875
 876static void dump_quoted_path(const char *head,
 877                             const char *prefix,
 878                             const char *path,
 879                             const char *line_prefix,
 880                             const char *c_meta, const char *c_reset)
 881{
 882        static struct strbuf buf = STRBUF_INIT;
 883
 884        strbuf_reset(&buf);
 885        strbuf_addstr(&buf, line_prefix);
 886        strbuf_addstr(&buf, c_meta);
 887        strbuf_addstr(&buf, head);
 888        quote_two_c_style(&buf, prefix, path, 0);
 889        strbuf_addstr(&buf, c_reset);
 890        puts(buf.buf);
 891}
 892
 893static void show_combined_header(struct combine_diff_path *elem,
 894                                 int num_parent,
 895                                 int dense,
 896                                 struct rev_info *rev,
 897                                 const char *line_prefix,
 898                                 int mode_differs,
 899                                 int show_file_header)
 900{
 901        struct diff_options *opt = &rev->diffopt;
 902        int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? GIT_SHA1_HEXSZ : DEFAULT_ABBREV;
 903        const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
 904        const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
 905        const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
 906        const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
 907        const char *abb;
 908        int added = 0;
 909        int deleted = 0;
 910        int i;
 911
 912        if (rev->loginfo && !rev->no_commit_id)
 913                show_log(rev);
 914
 915        dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
 916                         "", elem->path, line_prefix, c_meta, c_reset);
 917        printf("%s%sindex ", line_prefix, c_meta);
 918        for (i = 0; i < num_parent; i++) {
 919                abb = find_unique_abbrev(elem->parent[i].oid.hash,
 920                                         abbrev);
 921                printf("%s%s", i ? "," : "", abb);
 922        }
 923        abb = find_unique_abbrev(elem->oid.hash, abbrev);
 924        printf("..%s%s\n", abb, c_reset);
 925
 926        if (mode_differs) {
 927                deleted = !elem->mode;
 928
 929                /* We say it was added if nobody had it */
 930                added = !deleted;
 931                for (i = 0; added && i < num_parent; i++)
 932                        if (elem->parent[i].status !=
 933                            DIFF_STATUS_ADDED)
 934                                added = 0;
 935                if (added)
 936                        printf("%s%snew file mode %06o",
 937                               line_prefix, c_meta, elem->mode);
 938                else {
 939                        if (deleted)
 940                                printf("%s%sdeleted file ",
 941                                       line_prefix, c_meta);
 942                        printf("mode ");
 943                        for (i = 0; i < num_parent; i++) {
 944                                printf("%s%06o", i ? "," : "",
 945                                       elem->parent[i].mode);
 946                        }
 947                        if (elem->mode)
 948                                printf("..%06o", elem->mode);
 949                }
 950                printf("%s\n", c_reset);
 951        }
 952
 953        if (!show_file_header)
 954                return;
 955
 956        if (added)
 957                dump_quoted_path("--- ", "", "/dev/null",
 958                                 line_prefix, c_meta, c_reset);
 959        else
 960                dump_quoted_path("--- ", a_prefix, elem->path,
 961                                 line_prefix, c_meta, c_reset);
 962        if (deleted)
 963                dump_quoted_path("+++ ", "", "/dev/null",
 964                                 line_prefix, c_meta, c_reset);
 965        else
 966                dump_quoted_path("+++ ", b_prefix, elem->path,
 967                                 line_prefix, c_meta, c_reset);
 968}
 969
 970static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
 971                            int dense, int working_tree_file,
 972                            struct rev_info *rev)
 973{
 974        struct diff_options *opt = &rev->diffopt;
 975        unsigned long result_size, cnt, lno;
 976        int result_deleted = 0;
 977        char *result, *cp;
 978        struct sline *sline; /* survived lines */
 979        int mode_differs = 0;
 980        int i, show_hunks;
 981        mmfile_t result_file;
 982        struct userdiff_driver *userdiff;
 983        struct userdiff_driver *textconv = NULL;
 984        int is_binary;
 985        const char *line_prefix = diff_line_prefix(opt);
 986
 987        context = opt->context;
 988        userdiff = userdiff_find_by_path(elem->path);
 989        if (!userdiff)
 990                userdiff = userdiff_find_by_name("default");
 991        if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV))
 992                textconv = userdiff_get_textconv(userdiff);
 993
 994        /* Read the result of merge first */
 995        if (!working_tree_file)
 996                result = grab_blob(&elem->oid, elem->mode, &result_size,
 997                                   textconv, elem->path);
 998        else {
 999                /* Used by diff-tree to read from the working tree */
1000                struct stat st;
1001                int fd = -1;
1002
1003                if (lstat(elem->path, &st) < 0)
1004                        goto deleted_file;
1005
1006                if (S_ISLNK(st.st_mode)) {
1007                        struct strbuf buf = STRBUF_INIT;
1008
1009                        if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
1010                                error("readlink(%s): %s", elem->path,
1011                                      strerror(errno));
1012                                return;
1013                        }
1014                        result_size = buf.len;
1015                        result = strbuf_detach(&buf, NULL);
1016                        elem->mode = canon_mode(st.st_mode);
1017                } else if (S_ISDIR(st.st_mode)) {
1018                        struct object_id oid;
1019                        if (resolve_gitlink_ref(elem->path, "HEAD", oid.hash) < 0)
1020                                result = grab_blob(&elem->oid, elem->mode,
1021                                                   &result_size, NULL, NULL);
1022                        else
1023                                result = grab_blob(&oid, elem->mode,
1024                                                   &result_size, NULL, NULL);
1025                } else if (textconv) {
1026                        struct diff_filespec *df = alloc_filespec(elem->path);
1027                        fill_filespec(df, null_sha1, 0, st.st_mode);
1028                        result_size = fill_textconv(textconv, df, &result);
1029                        free_filespec(df);
1030                } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
1031                        size_t len = xsize_t(st.st_size);
1032                        ssize_t done;
1033                        int is_file, i;
1034
1035                        elem->mode = canon_mode(st.st_mode);
1036                        /* if symlinks don't work, assume symlink if all parents
1037                         * are symlinks
1038                         */
1039                        is_file = has_symlinks;
1040                        for (i = 0; !is_file && i < num_parent; i++)
1041                                is_file = !S_ISLNK(elem->parent[i].mode);
1042                        if (!is_file)
1043                                elem->mode = canon_mode(S_IFLNK);
1044
1045                        result_size = len;
1046                        result = xmalloc(len + 1);
1047
1048                        done = read_in_full(fd, result, len);
1049                        if (done < 0)
1050                                die_errno("read error '%s'", elem->path);
1051                        else if (done < len)
1052                                die("early EOF '%s'", elem->path);
1053
1054                        result[len] = 0;
1055
1056                        /* If not a fake symlink, apply filters, e.g. autocrlf */
1057                        if (is_file) {
1058                                struct strbuf buf = STRBUF_INIT;
1059
1060                                if (convert_to_git(elem->path, result, len, &buf, safe_crlf)) {
1061                                        free(result);
1062                                        result = strbuf_detach(&buf, &len);
1063                                        result_size = len;
1064                                }
1065                        }
1066                }
1067                else {
1068                deleted_file:
1069                        result_deleted = 1;
1070                        result_size = 0;
1071                        elem->mode = 0;
1072                        result = xcalloc(1, 1);
1073                }
1074
1075                if (0 <= fd)
1076                        close(fd);
1077        }
1078
1079        for (i = 0; i < num_parent; i++) {
1080                if (elem->parent[i].mode != elem->mode) {
1081                        mode_differs = 1;
1082                        break;
1083                }
1084        }
1085
1086        if (textconv)
1087                is_binary = 0;
1088        else if (userdiff->binary != -1)
1089                is_binary = userdiff->binary;
1090        else {
1091                is_binary = buffer_is_binary(result, result_size);
1092                for (i = 0; !is_binary && i < num_parent; i++) {
1093                        char *buf;
1094                        unsigned long size;
1095                        buf = grab_blob(&elem->parent[i].oid,
1096                                        elem->parent[i].mode,
1097                                        &size, NULL, NULL);
1098                        if (buffer_is_binary(buf, size))
1099                                is_binary = 1;
1100                        free(buf);
1101                }
1102        }
1103        if (is_binary) {
1104                show_combined_header(elem, num_parent, dense, rev,
1105                                     line_prefix, mode_differs, 0);
1106                printf("Binary files differ\n");
1107                free(result);
1108                return;
1109        }
1110
1111        for (cnt = 0, cp = result; cp < result + result_size; cp++) {
1112                if (*cp == '\n')
1113                        cnt++;
1114        }
1115        if (result_size && result[result_size-1] != '\n')
1116                cnt++; /* incomplete line */
1117
1118        sline = xcalloc(cnt+2, sizeof(*sline));
1119        sline[0].bol = result;
1120        for (lno = 0, cp = result; cp < result + result_size; cp++) {
1121                if (*cp == '\n') {
1122                        sline[lno].len = cp - sline[lno].bol;
1123                        lno++;
1124                        if (lno < cnt)
1125                                sline[lno].bol = cp + 1;
1126                }
1127        }
1128        if (result_size && result[result_size-1] != '\n')
1129                sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
1130
1131        result_file.ptr = result;
1132        result_file.size = result_size;
1133
1134        /* Even p_lno[cnt+1] is valid -- that is for the end line number
1135         * for deletion hunk at the end.
1136         */
1137        sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
1138        for (lno = 0; lno <= cnt; lno++)
1139                sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
1140
1141        for (i = 0; i < num_parent; i++) {
1142                int j;
1143                for (j = 0; j < i; j++) {
1144                        if (!oidcmp(&elem->parent[i].oid,
1145                                     &elem->parent[j].oid)) {
1146                                reuse_combine_diff(sline, cnt, i, j);
1147                                break;
1148                        }
1149                }
1150                if (i <= j)
1151                        combine_diff(&elem->parent[i].oid,
1152                                     elem->parent[i].mode,
1153                                     &result_file, sline,
1154                                     cnt, i, num_parent, result_deleted,
1155                                     textconv, elem->path, opt->xdl_opts);
1156        }
1157
1158        show_hunks = make_hunks(sline, cnt, num_parent, dense);
1159
1160        if (show_hunks || mode_differs || working_tree_file) {
1161                show_combined_header(elem, num_parent, dense, rev,
1162                                     line_prefix, mode_differs, 1);
1163                dump_sline(sline, line_prefix, cnt, num_parent,
1164                           opt->use_color, result_deleted);
1165        }
1166        free(result);
1167
1168        for (lno = 0; lno < cnt; lno++) {
1169                if (sline[lno].lost) {
1170                        struct lline *ll = sline[lno].lost;
1171                        while (ll) {
1172                                struct lline *tmp = ll;
1173                                ll = ll->next;
1174                                free(tmp);
1175                        }
1176                }
1177        }
1178        free(sline[0].p_lno);
1179        free(sline);
1180}
1181
1182static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
1183{
1184        struct diff_options *opt = &rev->diffopt;
1185        int line_termination, inter_name_termination, i;
1186        const char *line_prefix = diff_line_prefix(opt);
1187
1188        line_termination = opt->line_termination;
1189        inter_name_termination = '\t';
1190        if (!line_termination)
1191                inter_name_termination = 0;
1192
1193        if (rev->loginfo && !rev->no_commit_id)
1194                show_log(rev);
1195
1196
1197        if (opt->output_format & DIFF_FORMAT_RAW) {
1198                printf("%s", line_prefix);
1199
1200                /* As many colons as there are parents */
1201                for (i = 0; i < num_parent; i++)
1202                        putchar(':');
1203
1204                /* Show the modes */
1205                for (i = 0; i < num_parent; i++)
1206                        printf("%06o ", p->parent[i].mode);
1207                printf("%06o", p->mode);
1208
1209                /* Show sha1's */
1210                for (i = 0; i < num_parent; i++)
1211                        printf(" %s", diff_unique_abbrev(p->parent[i].oid.hash,
1212                                                         opt->abbrev));
1213                printf(" %s ", diff_unique_abbrev(p->oid.hash, opt->abbrev));
1214        }
1215
1216        if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
1217                for (i = 0; i < num_parent; i++)
1218                        putchar(p->parent[i].status);
1219                putchar(inter_name_termination);
1220        }
1221
1222        write_name_quoted(p->path, stdout, line_termination);
1223}
1224
1225/*
1226 * The result (p->elem) is from the working tree and their
1227 * parents are typically from multiple stages during a merge
1228 * (i.e. diff-files) or the state in HEAD and in the index
1229 * (i.e. diff-index).
1230 */
1231void show_combined_diff(struct combine_diff_path *p,
1232                       int num_parent,
1233                       int dense,
1234                       struct rev_info *rev)
1235{
1236        struct diff_options *opt = &rev->diffopt;
1237
1238        if (opt->output_format & (DIFF_FORMAT_RAW |
1239                                  DIFF_FORMAT_NAME |
1240                                  DIFF_FORMAT_NAME_STATUS))
1241                show_raw_diff(p, num_parent, rev);
1242        else if (opt->output_format & DIFF_FORMAT_PATCH)
1243                show_patch_diff(p, num_parent, dense, 1, rev);
1244}
1245
1246static void free_combined_pair(struct diff_filepair *pair)
1247{
1248        free(pair->two);
1249        free(pair);
1250}
1251
1252/*
1253 * A combine_diff_path expresses N parents on the LHS against 1 merge
1254 * result. Synthesize a diff_filepair that has N entries on the "one"
1255 * side and 1 entry on the "two" side.
1256 *
1257 * In the future, we might want to add more data to combine_diff_path
1258 * so that we can fill fields we are ignoring (most notably, size) here,
1259 * but currently nobody uses it, so this should suffice for now.
1260 */
1261static struct diff_filepair *combined_pair(struct combine_diff_path *p,
1262                                           int num_parent)
1263{
1264        int i;
1265        struct diff_filepair *pair;
1266        struct diff_filespec *pool;
1267
1268        pair = xmalloc(sizeof(*pair));
1269        pool = xcalloc(num_parent + 1, sizeof(struct diff_filespec));
1270        pair->one = pool + 1;
1271        pair->two = pool;
1272
1273        for (i = 0; i < num_parent; i++) {
1274                pair->one[i].path = p->path;
1275                pair->one[i].mode = p->parent[i].mode;
1276                hashcpy(pair->one[i].sha1, p->parent[i].oid.hash);
1277                pair->one[i].sha1_valid = !is_null_oid(&p->parent[i].oid);
1278                pair->one[i].has_more_entries = 1;
1279        }
1280        pair->one[num_parent - 1].has_more_entries = 0;
1281
1282        pair->two->path = p->path;
1283        pair->two->mode = p->mode;
1284        hashcpy(pair->two->sha1, p->oid.hash);
1285        pair->two->sha1_valid = !is_null_oid(&p->oid);
1286        return pair;
1287}
1288
1289static void handle_combined_callback(struct diff_options *opt,
1290                                     struct combine_diff_path *paths,
1291                                     int num_parent,
1292                                     int num_paths)
1293{
1294        struct combine_diff_path *p;
1295        struct diff_queue_struct q;
1296        int i;
1297
1298        q.queue = xcalloc(num_paths, sizeof(struct diff_filepair *));
1299        q.alloc = num_paths;
1300        q.nr = num_paths;
1301        for (i = 0, p = paths; p; p = p->next)
1302                q.queue[i++] = combined_pair(p, num_parent);
1303        opt->format_callback(&q, opt, opt->format_callback_data);
1304        for (i = 0; i < num_paths; i++)
1305                free_combined_pair(q.queue[i]);
1306        free(q.queue);
1307}
1308
1309static const char *path_path(void *obj)
1310{
1311        struct combine_diff_path *path = (struct combine_diff_path *)obj;
1312
1313        return path->path;
1314}
1315
1316
1317/* find set of paths that every parent touches */
1318static struct combine_diff_path *find_paths_generic(const unsigned char *sha1,
1319        const struct sha1_array *parents, struct diff_options *opt)
1320{
1321        struct combine_diff_path *paths = NULL;
1322        int i, num_parent = parents->nr;
1323
1324        int output_format = opt->output_format;
1325        const char *orderfile = opt->orderfile;
1326
1327        opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1328        /* tell diff_tree to emit paths in sorted (=tree) order */
1329        opt->orderfile = NULL;
1330
1331        /* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn)  (wrt paths) */
1332        for (i = 0; i < num_parent; i++) {
1333                /*
1334                 * show stat against the first parent even when doing
1335                 * combined diff.
1336                 */
1337                int stat_opt = (output_format &
1338                                (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
1339                if (i == 0 && stat_opt)
1340                        opt->output_format = stat_opt;
1341                else
1342                        opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1343                diff_tree_sha1(parents->sha1[i], sha1, "", opt);
1344                diffcore_std(opt);
1345                paths = intersect_paths(paths, i, num_parent);
1346
1347                /* if showing diff, show it in requested order */
1348                if (opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1349                    orderfile) {
1350                        diffcore_order(orderfile);
1351                }
1352
1353                diff_flush(opt);
1354        }
1355
1356        opt->output_format = output_format;
1357        opt->orderfile = orderfile;
1358        return paths;
1359}
1360
1361
1362/*
1363 * find set of paths that everybody touches, assuming diff is run without
1364 * rename/copy detection, etc, comparing all trees simultaneously (= faster).
1365 */
1366static struct combine_diff_path *find_paths_multitree(
1367        const unsigned char *sha1, const struct sha1_array *parents,
1368        struct diff_options *opt)
1369{
1370        int i, nparent = parents->nr;
1371        const unsigned char **parents_sha1;
1372        struct combine_diff_path paths_head;
1373        struct strbuf base;
1374
1375        parents_sha1 = xmalloc(nparent * sizeof(parents_sha1[0]));
1376        for (i = 0; i < nparent; i++)
1377                parents_sha1[i] = parents->sha1[i];
1378
1379        /* fake list head, so worker can assume it is non-NULL */
1380        paths_head.next = NULL;
1381
1382        strbuf_init(&base, PATH_MAX);
1383        diff_tree_paths(&paths_head, sha1, parents_sha1, nparent, &base, opt);
1384
1385        strbuf_release(&base);
1386        free(parents_sha1);
1387        return paths_head.next;
1388}
1389
1390
1391void diff_tree_combined(const unsigned char *sha1,
1392                        const struct sha1_array *parents,
1393                        int dense,
1394                        struct rev_info *rev)
1395{
1396        struct diff_options *opt = &rev->diffopt;
1397        struct diff_options diffopts;
1398        struct combine_diff_path *p, *paths;
1399        int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
1400        int need_generic_pathscan;
1401
1402        /* nothing to do, if no parents */
1403        if (!num_parent)
1404                return;
1405
1406        show_log_first = !!rev->loginfo && !rev->no_commit_id;
1407        needsep = 0;
1408        if (show_log_first) {
1409                show_log(rev);
1410
1411                if (rev->verbose_header && opt->output_format &&
1412                    opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1413                    !commit_format_is_empty(rev->commit_format))
1414                        printf("%s%c", diff_line_prefix(opt),
1415                               opt->line_termination);
1416        }
1417
1418        diffopts = *opt;
1419        copy_pathspec(&diffopts.pathspec, &opt->pathspec);
1420        DIFF_OPT_SET(&diffopts, RECURSIVE);
1421        DIFF_OPT_CLR(&diffopts, ALLOW_EXTERNAL);
1422
1423        /* find set of paths that everybody touches
1424         *
1425         * NOTE
1426         *
1427         * Diffcore transformations are bound to diff_filespec and logic
1428         * comparing two entries - i.e. they do not apply directly to combine
1429         * diff.
1430         *
1431         * If some of such transformations is requested - we launch generic
1432         * path scanning, which works significantly slower compared to
1433         * simultaneous all-trees-in-one-go scan in find_paths_multitree().
1434         *
1435         * TODO some of the filters could be ported to work on
1436         * combine_diff_paths - i.e. all functionality that skips paths, so in
1437         * theory, we could end up having only multitree path scanning.
1438         *
1439         * NOTE please keep this semantically in sync with diffcore_std()
1440         */
1441        need_generic_pathscan = opt->skip_stat_unmatch  ||
1442                        DIFF_OPT_TST(opt, FOLLOW_RENAMES)       ||
1443                        opt->break_opt != -1    ||
1444                        opt->detect_rename      ||
1445                        opt->pickaxe            ||
1446                        opt->filter;
1447
1448
1449        if (need_generic_pathscan) {
1450                /*
1451                 * NOTE generic case also handles --stat, as it computes
1452                 * diff(sha1,parent_i) for all i to do the job, specifically
1453                 * for parent0.
1454                 */
1455                paths = find_paths_generic(sha1, parents, &diffopts);
1456        }
1457        else {
1458                int stat_opt;
1459                paths = find_paths_multitree(sha1, parents, &diffopts);
1460
1461                /*
1462                 * show stat against the first parent even
1463                 * when doing combined diff.
1464                 */
1465                stat_opt = (opt->output_format &
1466                                (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
1467                if (stat_opt) {
1468                        diffopts.output_format = stat_opt;
1469
1470                        diff_tree_sha1(parents->sha1[0], sha1, "", &diffopts);
1471                        diffcore_std(&diffopts);
1472                        if (opt->orderfile)
1473                                diffcore_order(opt->orderfile);
1474                        diff_flush(&diffopts);
1475                }
1476        }
1477
1478        /* find out number of surviving paths */
1479        for (num_paths = 0, p = paths; p; p = p->next)
1480                num_paths++;
1481
1482        /* order paths according to diffcore_order */
1483        if (opt->orderfile && num_paths) {
1484                struct obj_order *o;
1485
1486                o = xmalloc(sizeof(*o) * num_paths);
1487                for (i = 0, p = paths; p; p = p->next, i++)
1488                        o[i].obj = p;
1489                order_objects(opt->orderfile, path_path, o, num_paths);
1490                for (i = 0; i < num_paths - 1; i++) {
1491                        p = o[i].obj;
1492                        p->next = o[i+1].obj;
1493                }
1494
1495                p = o[num_paths-1].obj;
1496                p->next = NULL;
1497                paths = o[0].obj;
1498                free(o);
1499        }
1500
1501
1502        if (num_paths) {
1503                if (opt->output_format & (DIFF_FORMAT_RAW |
1504                                          DIFF_FORMAT_NAME |
1505                                          DIFF_FORMAT_NAME_STATUS)) {
1506                        for (p = paths; p; p = p->next)
1507                                show_raw_diff(p, num_parent, rev);
1508                        needsep = 1;
1509                }
1510                else if (opt->output_format &
1511                         (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
1512                        needsep = 1;
1513                else if (opt->output_format & DIFF_FORMAT_CALLBACK)
1514                        handle_combined_callback(opt, paths, num_parent, num_paths);
1515
1516                if (opt->output_format & DIFF_FORMAT_PATCH) {
1517                        if (needsep)
1518                                printf("%s%c", diff_line_prefix(opt),
1519                                       opt->line_termination);
1520                        for (p = paths; p; p = p->next)
1521                                show_patch_diff(p, num_parent, dense,
1522                                                0, rev);
1523                }
1524        }
1525
1526        /* Clean things up */
1527        while (paths) {
1528                struct combine_diff_path *tmp = paths;
1529                paths = paths->next;
1530                free(tmp);
1531        }
1532
1533        free_pathspec(&diffopts.pathspec);
1534}
1535
1536void diff_tree_combined_merge(const struct commit *commit, int dense,
1537                              struct rev_info *rev)
1538{
1539        struct commit_list *parent = get_saved_parents(rev, commit);
1540        struct sha1_array parents = SHA1_ARRAY_INIT;
1541
1542        while (parent) {
1543                sha1_array_append(&parents, parent->item->object.oid.hash);
1544                parent = parent->next;
1545        }
1546        diff_tree_combined(commit->object.oid.hash, &parents, dense, rev);
1547        sha1_array_clear(&parents);
1548}