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