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