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