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