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