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