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