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