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