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