combine-diff: do not lose hunks with only deletion at end.
[git.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
9 static int uninteresting(struct diff_filepair *p)
10 {
11         if (diff_unmodified_pair(p))
12                 return 1;
13         return 0;
14 }
15
16 static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
17 {
18         struct diff_queue_struct *q = &diff_queued_diff;
19         struct combine_diff_path *p;
20         int i;
21
22         if (!n) {
23                 struct combine_diff_path *list = NULL, **tail = &list;
24                 for (i = 0; i < q->nr; i++) {
25                         int len;
26                         const char *path;
27                         if (uninteresting(q->queue[i]))
28                                 continue;
29                         path = q->queue[i]->two->path;
30                         len = strlen(path);
31                         p = xmalloc(combine_diff_path_size(num_parent, len));
32                         p->path = (char*) &(p->parent[num_parent]);
33                         memcpy(p->path, path, len);
34                         p->path[len] = 0;
35                         p->len = len;
36                         p->next = NULL;
37                         memset(p->parent, 0,
38                                sizeof(p->parent[0]) * num_parent);
39
40                         memcpy(p->sha1, q->queue[i]->two->sha1, 20);
41                         p->mode = q->queue[i]->two->mode;
42                         memcpy(p->parent[n].sha1, q->queue[i]->one->sha1, 20);
43                         p->parent[n].mode = q->queue[i]->one->mode;
44                         p->parent[n].status = q->queue[i]->status;
45                         *tail = p;
46                         tail = &p->next;
47                 }
48                 return list;
49         }
50
51         for (p = curr; p; p = p->next) {
52                 int found = 0;
53                 if (!p->len)
54                         continue;
55                 for (i = 0; i < q->nr; i++) {
56                         const char *path;
57                         int len;
58
59                         if (uninteresting(q->queue[i]))
60                                 continue;
61                         path = q->queue[i]->two->path;
62                         len = strlen(path);
63                         if (len == p->len && !memcmp(path, p->path, len)) {
64                                 found = 1;
65                                 memcpy(p->parent[n].sha1,
66                                        q->queue[i]->one->sha1, 20);
67                                 p->parent[n].mode = q->queue[i]->one->mode;
68                                 p->parent[n].status = q->queue[i]->status;
69                                 break;
70                         }
71                 }
72                 if (!found)
73                         p->len = 0;
74         }
75         return curr;
76 }
77
78 /* Lines lost from parent */
79 struct lline {
80         struct lline *next;
81         int len;
82         unsigned long parent_map;
83         char line[FLEX_ARRAY];
84 };
85
86 /* Lines surviving in the merge result */
87 struct sline {
88         struct lline *lost_head, **lost_tail;
89         char *bol;
90         int len;
91         /* bit 0 up to (N-1) are on if the parent has this line (i.e.
92          * we did not change it).
93          * bit N is used for "interesting" lines, including context.
94          */
95         unsigned long flag;
96         unsigned long *p_lno;
97 };
98
99 static char *grab_blob(const unsigned char *sha1, unsigned long *size)
100 {
101         char *blob;
102         char type[20];
103         if (!memcmp(sha1, null_sha1, 20)) {
104                 /* deleted blob */
105                 *size = 0;
106                 return xcalloc(1, 1);
107         }
108         blob = read_sha1_file(sha1, type, size);
109         if (strcmp(type, blob_type))
110                 die("object '%s' is not a blob!", sha1_to_hex(sha1));
111         return blob;
112 }
113
114 static void append_lost(struct sline *sline, int n, const char *line, int len)
115 {
116         struct lline *lline;
117         unsigned long this_mask = (1UL<<n);
118         if (line[len-1] == '\n')
119                 len--;
120
121         /* Check to see if we can squash things */
122         if (sline->lost_head) {
123                 struct lline *last_one = NULL;
124                 /* We cannot squash it with earlier one */
125                 for (lline = sline->lost_head;
126                      lline;
127                      lline = lline->next)
128                         if (lline->parent_map & this_mask)
129                                 last_one = lline;
130                 lline = last_one ? last_one->next : sline->lost_head;
131                 while (lline) {
132                         if (lline->len == len &&
133                             !memcmp(lline->line, line, len)) {
134                                 lline->parent_map |= this_mask;
135                                 return;
136                         }
137                         lline = lline->next;
138                 }
139         }
140
141         lline = xmalloc(sizeof(*lline) + len + 1);
142         lline->len = len;
143         lline->next = NULL;
144         lline->parent_map = this_mask;
145         memcpy(lline->line, line, len);
146         lline->line[len] = 0;
147         *sline->lost_tail = lline;
148         sline->lost_tail = &lline->next;
149 }
150
151 struct combine_diff_state {
152         struct xdiff_emit_state xm;
153
154         unsigned int lno;
155         int ob, on, nb, nn;
156         unsigned long nmask;
157         int num_parent;
158         int n;
159         struct sline *sline;
160         struct sline *lost_bucket;
161 };
162
163 static void consume_line(void *state_, char *line, unsigned long len)
164 {
165         struct combine_diff_state *state = state_;
166         if (5 < len && !memcmp("@@ -", line, 4)) {
167                 if (parse_hunk_header(line, len,
168                                       &state->ob, &state->on,
169                                       &state->nb, &state->nn))
170                         return;
171                 state->lno = state->nb;
172                 if (!state->nb)
173                         /* @@ -1,2 +0,0 @@ to remove the
174                          * first two lines...
175                          */
176                         state->nb = 1;
177                 if (state->nn == 0)
178                         /* @@ -X,Y +N,0 @@ removed Y lines
179                          * that would have come *after* line N
180                          * in the result.  Our lost buckets hang
181                          * to the line after the removed lines,
182                          */
183                         state->lost_bucket = &state->sline[state->nb];
184                 else
185                         state->lost_bucket = &state->sline[state->nb-1];
186                 if (!state->sline[state->nb-1].p_lno)
187                         state->sline[state->nb-1].p_lno =
188                                 xcalloc(state->num_parent,
189                                         sizeof(unsigned long));
190                 state->sline[state->nb-1].p_lno[state->n] = state->ob;
191                 return;
192         }
193         if (!state->lost_bucket)
194                 return; /* not in any hunk yet */
195         switch (line[0]) {
196         case '-':
197                 append_lost(state->lost_bucket, state->n, line+1, len-1);
198                 break;
199         case '+':
200                 state->sline[state->lno-1].flag |= state->nmask;
201                 state->lno++;
202                 break;
203         }
204 }
205
206 static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
207                          struct sline *sline, int cnt, int n, int num_parent)
208 {
209         unsigned int p_lno, lno;
210         unsigned long nmask = (1UL << n);
211         xpparam_t xpp;
212         xdemitconf_t xecfg;
213         mmfile_t parent_file;
214         xdemitcb_t ecb;
215         struct combine_diff_state state;
216         unsigned long sz;
217
218         if (!cnt)
219                 return; /* result deleted */
220
221         parent_file.ptr = grab_blob(parent, &sz);
222         parent_file.size = sz;
223         xpp.flags = XDF_NEED_MINIMAL;
224         xecfg.ctxlen = 0;
225         xecfg.flags = 0;
226         ecb.outf = xdiff_outf;
227         ecb.priv = &state;
228         memset(&state, 0, sizeof(state));
229         state.xm.consume = consume_line;
230         state.nmask = nmask;
231         state.sline = sline;
232         state.lno = 1;
233         state.num_parent = num_parent;
234         state.n = n;
235
236         xdl_diff(&parent_file, result_file, &xpp, &xecfg, &ecb);
237         free(parent_file.ptr);
238
239         /* Assign line numbers for this parent.
240          *
241          * sline[lno].p_lno[n] records the first line number
242          * (counting from 1) for parent N if the final hunk display
243          * started by showing sline[lno] (possibly showing the lost
244          * lines attached to it first).
245          */
246         for (lno = 0,  p_lno = 1; lno <= cnt; lno++) {
247                 struct lline *ll;
248                 sline[lno].p_lno[n] = p_lno;
249
250                 /* How many lines would this sline advance the p_lno? */
251                 ll = sline[lno].lost_head;
252                 while (ll) {
253                         if (ll->parent_map & nmask)
254                                 p_lno++; /* '-' means parent had it */
255                         ll = ll->next;
256                 }
257                 if (lno < cnt && !(sline[lno].flag & nmask))
258                         p_lno++; /* no '+' means parent had it */
259         }
260         sline[lno].p_lno[n] = p_lno; /* trailer */
261 }
262
263 static unsigned long context = 3;
264 static char combine_marker = '@';
265
266 static int interesting(struct sline *sline, unsigned long all_mask)
267 {
268         /* If some parents lost lines here, or if we have added to
269          * some parent, it is interesting.
270          */
271         return ((sline->flag & all_mask) || sline->lost_head);
272 }
273
274 static unsigned long adjust_hunk_tail(struct sline *sline,
275                                       unsigned long all_mask,
276                                       unsigned long hunk_begin,
277                                       unsigned long i)
278 {
279         /* i points at the first uninteresting line.  If the last line
280          * of the hunk was interesting only because it has some
281          * deletion, then it is not all that interesting for the
282          * purpose of giving trailing context lines.  This is because
283          * we output '-' line and then unmodified sline[i-1] itself in
284          * that case which gives us one extra context line.
285          */
286         if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
287                 i--;
288         return i;
289 }
290
291 static unsigned long find_next(struct sline *sline,
292                                unsigned long mark,
293                                unsigned long i,
294                                unsigned long cnt,
295                                int uninteresting)
296 {
297         /* We have examined up to i-1 and are about to look at i.
298          * Find next interesting or uninteresting line.  Here,
299          * "interesting" does not mean interesting(), but marked by
300          * the give_context() function below (i.e. it includes context
301          * lines that are not interesting to interesting() function
302          * that are surrounded by interesting() ones.
303          */
304         while (i < cnt)
305                 if (uninteresting
306                     ? !(sline[i].flag & mark)
307                     : (sline[i].flag & mark))
308                         return i;
309                 else
310                         i++;
311         return cnt;
312 }
313
314 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
315 {
316         unsigned long all_mask = (1UL<<num_parent) - 1;
317         unsigned long mark = (1UL<<num_parent);
318         unsigned long i;
319
320         /* Two groups of interesting lines may have a short gap of
321          * unintersting lines.  Connect such groups to give them a
322          * bit of context.
323          *
324          * We first start from what the interesting() function says,
325          * and mark them with "mark", and paint context lines with the
326          * mark.  So interesting() would still say false for such context
327          * lines but they are treated as "interesting" in the end.
328          */
329         i = find_next(sline, mark, 0, cnt, 0);
330         if (cnt <= i)
331                 return 0;
332
333         while (i < cnt) {
334                 unsigned long j = (context < i) ? (i - context) : 0;
335                 unsigned long k;
336
337                 /* Paint a few lines before the first interesting line. */
338                 while (j < i)
339                         sline[j++].flag |= mark;
340
341         again:
342                 /* we know up to i is to be included.  where does the
343                  * next uninteresting one start?
344                  */
345                 j = find_next(sline, mark, i, cnt, 1);
346                 if (cnt <= j)
347                         break; /* the rest are all interesting */
348
349                 /* lookahead context lines */
350                 k = find_next(sline, mark, j, cnt, 0);
351                 j = adjust_hunk_tail(sline, all_mask, i, j);
352
353                 if (k < j + context) {
354                         /* k is interesting and [j,k) are not, but
355                          * paint them interesting because the gap is small.
356                          */
357                         while (j < k)
358                                 sline[j++].flag |= mark;
359                         i = k;
360                         goto again;
361                 }
362
363                 /* j is the first uninteresting line and there is
364                  * no overlap beyond it within context lines.  Paint
365                  * the trailing edge a bit.
366                  */
367                 i = k;
368                 k = (j + context < cnt) ? j + context : cnt;
369                 while (j < k)
370                         sline[j++].flag |= mark;
371         }
372         return 1;
373 }
374
375 static int make_hunks(struct sline *sline, unsigned long cnt,
376                        int num_parent, int dense)
377 {
378         unsigned long all_mask = (1UL<<num_parent) - 1;
379         unsigned long mark = (1UL<<num_parent);
380         unsigned long i;
381         int has_interesting = 0;
382
383         for (i = 0; i < cnt; i++) {
384                 if (interesting(&sline[i], all_mask))
385                         sline[i].flag |= mark;
386                 else
387                         sline[i].flag &= ~mark;
388         }
389         if (!dense)
390                 return give_context(sline, cnt, num_parent);
391
392         /* Look at each hunk, and if we have changes from only one
393          * parent, or the changes are the same from all but one
394          * parent, mark that uninteresting.
395          */
396         i = 0;
397         while (i < cnt) {
398                 unsigned long j, hunk_begin, hunk_end;
399                 unsigned long same_diff;
400                 while (i < cnt && !(sline[i].flag & mark))
401                         i++;
402                 if (cnt <= i)
403                         break; /* No more interesting hunks */
404                 hunk_begin = i;
405                 for (j = i + 1; j < cnt; j++) {
406                         if (!(sline[j].flag & mark)) {
407                                 /* Look beyond the end to see if there
408                                  * is an interesting line after this
409                                  * hunk within context span.
410                                  */
411                                 unsigned long la; /* lookahead */
412                                 int contin = 0;
413                                 la = adjust_hunk_tail(sline, all_mask,
414                                                      hunk_begin, j);
415                                 la = (la + context < cnt) ?
416                                         (la + context) : cnt;
417                                 while (j <= --la) {
418                                         if (sline[la].flag & mark) {
419                                                 contin = 1;
420                                                 break;
421                                         }
422                                 }
423                                 if (!contin)
424                                         break;
425                                 j = la;
426                         }
427                 }
428                 hunk_end = j;
429
430                 /* [i..hunk_end) are interesting.  Now is it really
431                  * interesting?  We check if there are only two versions
432                  * and the result matches one of them.  That is, we look
433                  * at:
434                  *   (+) line, which records lines added to which parents;
435                  *       this line appears in the result.
436                  *   (-) line, which records from what parents the line
437                  *       was removed; this line does not appear in the result.
438                  * then check the set of parents the result has difference
439                  * from, from all lines.  If there are lines that has
440                  * different set of parents that the result has differences
441                  * from, that means we have more than two versions.
442                  *
443                  * Even when we have only two versions, if the result does
444                  * not match any of the parents, the it should be considered
445                  * interesting.  In such a case, we would have all '+' line.
446                  * After passing the above "two versions" test, that would
447                  * appear as "the same set of parents" to be "all parents".
448                  */
449                 same_diff = 0;
450                 has_interesting = 0;
451                 for (j = i; j < hunk_end && !has_interesting; j++) {
452                         unsigned long this_diff = sline[j].flag & all_mask;
453                         struct lline *ll = sline[j].lost_head;
454                         if (this_diff) {
455                                 /* This has some changes.  Is it the
456                                  * same as others?
457                                  */
458                                 if (!same_diff)
459                                         same_diff = this_diff;
460                                 else if (same_diff != this_diff) {
461                                         has_interesting = 1;
462                                         break;
463                                 }
464                         }
465                         while (ll && !has_interesting) {
466                                 /* Lost this line from these parents;
467                                  * who are they?  Are they the same?
468                                  */
469                                 this_diff = ll->parent_map;
470                                 if (!same_diff)
471                                         same_diff = this_diff;
472                                 else if (same_diff != this_diff) {
473                                         has_interesting = 1;
474                                 }
475                                 ll = ll->next;
476                         }
477                 }
478
479                 if (!has_interesting && same_diff != all_mask) {
480                         /* This hunk is not that interesting after all */
481                         for (j = hunk_begin; j < hunk_end; j++)
482                                 sline[j].flag &= ~mark;
483                 }
484                 i = hunk_end;
485         }
486
487         has_interesting = give_context(sline, cnt, num_parent);
488         return has_interesting;
489 }
490
491 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, unsigned long cnt, int n)
492 {
493         l0 = sline[l0].p_lno[n];
494         l1 = sline[l1].p_lno[n];
495         printf(" -%lu,%lu", l0, l1-l0);
496 }
497
498 static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
499 {
500         unsigned long mark = (1UL<<num_parent);
501         int i;
502         unsigned long lno = 0;
503
504         if (!cnt)
505                 return; /* result deleted */
506
507         while (1) {
508                 struct sline *sl = &sline[lno];
509                 int hunk_end;
510                 while (lno < cnt && !(sline[lno].flag & mark))
511                         lno++;
512                 if (cnt < lno)
513                         break;
514                 else if (cnt == lno) {
515                         /* See if there is anything interesting */
516                         struct lline *ll;
517                         for (ll = sline[lno].lost_head; ll; ll = ll->next)
518                                 if (ll->parent_map)
519                                         break;
520                         if (!ll)
521                                 break;
522                         hunk_end = cnt + 1;
523                 }
524                 else {
525                         for (hunk_end = lno + 1; hunk_end < cnt; hunk_end++)
526                                 if (!(sline[hunk_end].flag & mark))
527                                         break;
528                 }
529                 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
530                 for (i = 0; i < num_parent; i++)
531                         show_parent_lno(sline, lno, hunk_end, cnt, i);
532                 printf(" +%lu,%lu ", lno+1,
533                        (cnt == lno) ? 0 : hunk_end - lno);
534                 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
535                 putchar('\n');
536                 while (lno < hunk_end) {
537                         struct lline *ll;
538                         int j;
539                         unsigned long p_mask;
540                         sl = &sline[lno++];
541                         ll = sl->lost_head;
542                         while (ll) {
543                                 for (j = 0; j < num_parent; j++) {
544                                         if (ll->parent_map & (1UL<<j))
545                                                 putchar('-');
546                                         else
547                                                 putchar(' ');
548                                 }
549                                 puts(ll->line);
550                                 ll = ll->next;
551                         }
552                         if (cnt <= lno)
553                                 break;
554                         p_mask = 1;
555                         for (j = 0; j < num_parent; j++) {
556                                 if (p_mask & sl->flag)
557                                         putchar('+');
558                                 else
559                                         putchar(' ');
560                                 p_mask <<= 1;
561                         }
562                         printf("%.*s\n", sl->len, sl->bol);
563                 }
564         }
565 }
566
567 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
568                                int i, int j)
569 {
570         /* We have already examined parent j and we know parent i
571          * and parent j are the same, so reuse the combined result
572          * of parent j for parent i.
573          */
574         unsigned long lno, imask, jmask;
575         imask = (1UL<<i);
576         jmask = (1UL<<j);
577
578         for (lno = 0; lno <= cnt; lno++) {
579                 struct lline *ll = sline->lost_head;
580                 sline->p_lno[i] = sline->p_lno[j];
581                 while (ll) {
582                         if (ll->parent_map & jmask)
583                                 ll->parent_map |= imask;
584                         ll = ll->next;
585                 }
586                 if (sline->flag & jmask)
587                         sline->flag |= imask;
588                 sline++;
589         }
590         /* the overall size of the file (sline[cnt]) */
591         sline->p_lno[i] = sline->p_lno[j];
592 }
593
594 static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
595                            int dense, const char *header,
596                            struct diff_options *opt)
597 {
598         unsigned long result_size, cnt, lno;
599         char *result, *cp, *ep;
600         struct sline *sline; /* survived lines */
601         int mode_differs = 0;
602         int i, show_hunks, shown_header = 0;
603         int working_tree_file = !memcmp(elem->sha1, null_sha1, 20);
604         int abbrev = opt->full_index ? 40 : DEFAULT_ABBREV;
605         mmfile_t result_file;
606
607         /* Read the result of merge first */
608         if (!working_tree_file)
609                 result = grab_blob(elem->sha1, &result_size);
610         else {
611                 /* Used by diff-tree to read from the working tree */
612                 struct stat st;
613                 int fd;
614                 if (0 <= (fd = open(elem->path, O_RDONLY)) &&
615                     !fstat(fd, &st)) {
616                         int len = st.st_size;
617                         int cnt = 0;
618
619                         elem->mode = canon_mode(st.st_mode);
620                         result_size = len;
621                         result = xmalloc(len + 1);
622                         while (cnt < len) {
623                                 int done = xread(fd, result+cnt, len-cnt);
624                                 if (done == 0)
625                                         break;
626                                 if (done < 0)
627                                         die("read error '%s'", elem->path);
628                                 cnt += done;
629                         }
630                         result[len] = 0;
631                 }
632                 else {
633                         /* deleted file */
634                         result_size = 0;
635                         elem->mode = 0;
636                         result = xmalloc(1);
637                         result[0] = 0;
638                 }
639                 if (0 <= fd)
640                         close(fd);
641         }
642
643         for (cnt = 0, cp = result; cp - result < result_size; cp++) {
644                 if (*cp == '\n')
645                         cnt++;
646         }
647         if (result_size && result[result_size-1] != '\n')
648                 cnt++; /* incomplete line */
649
650         sline = xcalloc(cnt+2, sizeof(*sline));
651         ep = result;
652         sline[0].bol = result;
653         for (lno = 0; lno <= cnt + 1; lno++) {
654                 sline[lno].lost_tail = &sline[lno].lost_head;
655                 sline[lno].flag = 0;
656         }
657         for (lno = 0, cp = result; cp - result < result_size; cp++) {
658                 if (*cp == '\n') {
659                         sline[lno].len = cp - sline[lno].bol;
660                         lno++;
661                         if (lno < cnt)
662                                 sline[lno].bol = cp + 1;
663                 }
664         }
665         if (result_size && result[result_size-1] != '\n')
666                 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
667
668         result_file.ptr = result;
669         result_file.size = result_size;
670
671         /* Even p_lno[cnt+1] is valid -- that is for the end line number
672          * for deletion hunk at the end.
673          */
674         sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
675         for (lno = 0; lno <= cnt; lno++)
676                 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
677
678         for (i = 0; i < num_parent; i++) {
679                 int j;
680                 for (j = 0; j < i; j++) {
681                         if (!memcmp(elem->parent[i].sha1,
682                                     elem->parent[j].sha1, 20)) {
683                                 reuse_combine_diff(sline, cnt, i, j);
684                                 break;
685                         }
686                 }
687                 if (i <= j)
688                         combine_diff(elem->parent[i].sha1, &result_file, sline,
689                                      cnt, i, num_parent);
690                 if (elem->parent[i].mode != elem->mode)
691                         mode_differs = 1;
692         }
693
694         show_hunks = make_hunks(sline, cnt, num_parent, dense);
695
696         if (show_hunks || mode_differs || working_tree_file) {
697                 const char *abb;
698
699                 if (header) {
700                         shown_header++;
701                         printf("%s%c", header, opt->line_termination);
702                 }
703                 printf("diff --%s ", dense ? "cc" : "combined");
704                 if (quote_c_style(elem->path, NULL, NULL, 0))
705                         quote_c_style(elem->path, NULL, stdout, 0);
706                 else
707                         printf("%s", elem->path);
708                 putchar('\n');
709                 printf("index ");
710                 for (i = 0; i < num_parent; i++) {
711                         abb = find_unique_abbrev(elem->parent[i].sha1,
712                                                  abbrev);
713                         printf("%s%s", i ? "," : "", abb);
714                 }
715                 abb = find_unique_abbrev(elem->sha1, abbrev);
716                 printf("..%s\n", abb);
717
718                 if (mode_differs) {
719                         int added = !!elem->mode;
720                         for (i = 0; added && i < num_parent; i++)
721                                 if (elem->parent[i].status !=
722                                     DIFF_STATUS_ADDED)
723                                         added = 0;
724                         if (added)
725                                 printf("new file mode %06o", elem->mode);
726                         else {
727                                 if (!elem->mode)
728                                         printf("deleted file ");
729                                 printf("mode ");
730                                 for (i = 0; i < num_parent; i++) {
731                                         printf("%s%06o", i ? "," : "",
732                                                elem->parent[i].mode);
733                                 }
734                                 if (elem->mode)
735                                         printf("..%06o", elem->mode);
736                         }
737                         putchar('\n');
738                 }
739                 dump_sline(sline, cnt, num_parent);
740         }
741         free(result);
742
743         for (i = 0; i < cnt; i++) {
744                 if (sline[i].lost_head) {
745                         struct lline *ll = sline[i].lost_head;
746                         while (ll) {
747                                 struct lline *tmp = ll;
748                                 ll = ll->next;
749                                 free(tmp);
750                         }
751                 }
752         }
753         free(sline[0].p_lno);
754         free(sline);
755         return shown_header;
756 }
757
758 #define COLONS "::::::::::::::::::::::::::::::::"
759
760 static void show_raw_diff(struct combine_diff_path *p, int num_parent, const char *header, struct diff_options *opt)
761 {
762         int i, offset, mod_type = 'A';
763         const char *prefix;
764         int line_termination, inter_name_termination;
765
766         line_termination = opt->line_termination;
767         inter_name_termination = '\t';
768         if (!line_termination)
769                 inter_name_termination = 0;
770
771         if (header)
772                 printf("%s%c", header, line_termination);
773
774         for (i = 0; i < num_parent; i++) {
775                 if (p->parent[i].mode)
776                         mod_type = 'M';
777         }
778         if (!p->mode)
779                 mod_type = 'D';
780
781         if (opt->output_format == DIFF_FORMAT_RAW) {
782                 offset = strlen(COLONS) - num_parent;
783                 if (offset < 0)
784                         offset = 0;
785                 prefix = COLONS + offset;
786
787                 /* Show the modes */
788                 for (i = 0; i < num_parent; i++) {
789                         printf("%s%06o", prefix, p->parent[i].mode);
790                         prefix = " ";
791                 }
792                 printf("%s%06o", prefix, p->mode);
793
794                 /* Show sha1's */
795                 for (i = 0; i < num_parent; i++)
796                         printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
797                                                          opt->abbrev));
798                 printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
799         }
800
801         if (opt->output_format == DIFF_FORMAT_RAW ||
802             opt->output_format == DIFF_FORMAT_NAME_STATUS) {
803                 for (i = 0; i < num_parent; i++)
804                         putchar(p->parent[i].status);
805                 putchar(inter_name_termination);
806         }
807
808         if (line_termination) {
809                 if (quote_c_style(p->path, NULL, NULL, 0))
810                         quote_c_style(p->path, NULL, stdout, 0);
811                 else
812                         printf("%s", p->path);
813                 putchar(line_termination);
814         }
815         else {
816                 printf("%s%c", p->path, line_termination);
817         }
818 }
819
820 int show_combined_diff(struct combine_diff_path *p,
821                        int num_parent,
822                        int dense,
823                        const char *header,
824                        struct diff_options *opt)
825 {
826         if (!p->len)
827                 return 0;
828         switch (opt->output_format) {
829         case DIFF_FORMAT_RAW:
830         case DIFF_FORMAT_NAME_STATUS:
831         case DIFF_FORMAT_NAME:
832                 show_raw_diff(p, num_parent, header, opt);
833                 return 1;
834
835         default:
836         case DIFF_FORMAT_PATCH:
837                 return show_patch_diff(p, num_parent, dense, header, opt);
838         }
839 }
840
841 const char *diff_tree_combined_merge(const unsigned char *sha1,
842                              const char *header, int dense,
843                              struct diff_options *opt)
844 {
845         struct commit *commit = lookup_commit(sha1);
846         struct diff_options diffopts;
847         struct commit_list *parents;
848         struct combine_diff_path *p, *paths = NULL;
849         int num_parent, i, num_paths;
850
851         diffopts = *opt;
852         diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
853         diffopts.recursive = 1;
854
855         /* count parents */
856         for (parents = commit->parents, num_parent = 0;
857              parents;
858              parents = parents->next, num_parent++)
859                 ; /* nothing */
860
861         /* find set of paths that everybody touches */
862         for (parents = commit->parents, i = 0;
863              parents;
864              parents = parents->next, i++) {
865                 struct commit *parent = parents->item;
866                 diff_tree_sha1(parent->object.sha1, commit->object.sha1, "",
867                                &diffopts);
868                 diffcore_std(&diffopts);
869                 paths = intersect_paths(paths, i, num_parent);
870                 diff_flush(&diffopts);
871         }
872
873         /* find out surviving paths */
874         for (num_paths = 0, p = paths; p; p = p->next) {
875                 if (p->len)
876                         num_paths++;
877         }
878         if (num_paths) {
879                 for (p = paths; p; p = p->next) {
880                         if (show_combined_diff(p, num_parent, dense,
881                                                header, opt))
882                                 header = NULL;
883                 }
884         }
885
886         /* Clean things up */
887         while (paths) {
888                 struct combine_diff_path *tmp = paths;
889                 paths = paths->next;
890                 free(tmp);
891         }
892         return header;
893 }