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