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