7 #include "xdiff-interface.h"
8 #include "xdiff/xmacros.h"
12 #include "sha1-array.h"
15 static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
17 struct diff_queue_struct *q = &diff_queued_diff;
18 struct combine_diff_path *p;
22 struct combine_diff_path *list = NULL, **tail = &list;
23 for (i = 0; i < q->nr; i++) {
26 if (diff_unmodified_pair(q->queue[i]))
28 path = q->queue[i]->two->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);
37 sizeof(p->parent[0]) * num_parent);
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;
50 for (p = curr; p; p = p->next) {
54 for (i = 0; i < q->nr; i++) {
58 if (diff_unmodified_pair(q->queue[i]))
60 path = q->queue[i]->two->path;
62 if (len == p->len && !memcmp(path, p->path, len)) {
64 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
65 p->parent[n].mode = q->queue[i]->one->mode;
66 p->parent[n].status = q->queue[i]->status;
76 /* Lines lost from parent */
78 struct lline *next, *prev;
80 unsigned long parent_map;
81 char line[FLEX_ARRAY];
84 /* Lines lost from current parent (before coalescing) */
86 struct lline *lost_head, *lost_tail;
90 /* Lines surviving in the merge result */
92 /* Accumulated and coalesced lost lines */
98 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
99 * we did not change it).
100 * bit N is used for "interesting" lines, including context.
101 * bit (N+1) is used for "do not show deletion before this".
104 unsigned long *p_lno;
107 static int match_string_spaces(const char *line1, int len1,
108 const char *line2, int len2,
111 if (flags & XDF_WHITESPACE_FLAGS) {
112 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
113 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
116 if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
117 return (len1 == len2 && !memcmp(line1, line2, len1));
119 while (len1 > 0 && len2 > 0) {
122 if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
123 if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
124 (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
127 for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
128 for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
130 if (line1[len1] != line2[len2])
134 if (flags & XDF_IGNORE_WHITESPACE) {
135 /* Consume remaining spaces */
136 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
137 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
140 /* We matched full line1 and line2 */
147 enum coalesce_direction { MATCH, BASE, NEW };
149 /* Coalesce new lines into base by finding LCS */
150 static struct lline *coalesce_lines(struct lline *base, int *lenbase,
151 struct lline *new, int lennew,
152 unsigned long parent, long flags)
155 enum coalesce_direction **directions;
156 struct lline *baseend, *newend = NULL;
157 int i, j, origbaselen = *lenbase;
168 * Coalesce new lines into base by finding the LCS
169 * - Create the table to run dynamic programming
171 * - Then reverse read the direction structure:
172 * - If we have MATCH, assign parent to base flag, and consume
173 * both baseend and newend
174 * - Else if we have BASE, consume baseend
175 * - Else if we have NEW, insert newend lline into base and
178 lcs = xcalloc(origbaselen + 1, sizeof(int*));
179 directions = xcalloc(origbaselen + 1, sizeof(enum coalesce_direction*));
180 for (i = 0; i < origbaselen + 1; i++) {
181 lcs[i] = xcalloc(lennew + 1, sizeof(int));
182 directions[i] = xcalloc(lennew + 1, sizeof(enum coalesce_direction));
183 directions[i][0] = BASE;
185 for (j = 1; j < lennew + 1; j++)
186 directions[0][j] = NEW;
188 for (i = 1, baseend = base; i < origbaselen + 1; i++) {
189 for (j = 1, newend = new; j < lennew + 1; j++) {
190 if (match_string_spaces(baseend->line, baseend->len,
191 newend->line, newend->len, flags)) {
192 lcs[i][j] = lcs[i - 1][j - 1] + 1;
193 directions[i][j] = MATCH;
194 } else if (lcs[i][j - 1] >= lcs[i - 1][j]) {
195 lcs[i][j] = lcs[i][j - 1];
196 directions[i][j] = NEW;
198 lcs[i][j] = lcs[i - 1][j];
199 directions[i][j] = BASE;
202 newend = newend->next;
205 baseend = baseend->next;
208 for (i = 0; i < origbaselen + 1; i++)
212 /* At this point, baseend and newend point to the end of each lists */
215 while (i != 0 || j != 0) {
216 if (directions[i][j] == MATCH) {
217 baseend->parent_map |= 1<<parent;
218 baseend = baseend->prev;
219 newend = newend->prev;
222 } else if (directions[i][j] == NEW) {
226 /* Remove lline from new list and update newend */
228 lline->prev->next = lline->next;
232 lline->next->prev = lline->prev;
234 newend = lline->prev;
237 /* Add lline to base list */
239 lline->next = baseend->next;
240 lline->prev = baseend;
242 lline->prev->next = lline;
251 lline->next->prev = lline;
254 baseend = baseend->prev;
261 struct lline *lline = newend;
262 newend = newend->next;
266 for (i = 0; i < origbaselen + 1; i++)
273 static char *grab_blob(const unsigned char *sha1, unsigned int mode,
274 unsigned long *size, struct userdiff_driver *textconv,
278 enum object_type type;
280 if (S_ISGITLINK(mode)) {
282 *size = snprintf(blob, 100,
283 "Subproject commit %s\n", sha1_to_hex(sha1));
284 } else if (is_null_sha1(sha1)) {
287 return xcalloc(1, 1);
288 } else if (textconv) {
289 struct diff_filespec *df = alloc_filespec(path);
290 fill_filespec(df, sha1, 1, mode);
291 *size = fill_textconv(textconv, df, &blob);
294 blob = read_sha1_file(sha1, &type, size);
295 if (type != OBJ_BLOB)
296 die("object '%s' is not a blob!", sha1_to_hex(sha1));
301 static void append_lost(struct sline *sline, int n, const char *line, int len)
304 unsigned long this_mask = (1UL<<n);
305 if (line[len-1] == '\n')
308 lline = xmalloc(sizeof(*lline) + len + 1);
311 lline->prev = sline->plost.lost_tail;
313 lline->prev->next = lline;
315 sline->plost.lost_head = lline;
316 sline->plost.lost_tail = lline;
318 lline->parent_map = this_mask;
319 memcpy(lline->line, line, len);
320 lline->line[len] = 0;
323 struct combine_diff_state {
330 struct sline *lost_bucket;
333 static void consume_line(void *state_, char *line, unsigned long len)
335 struct combine_diff_state *state = state_;
336 if (5 < len && !memcmp("@@ -", line, 4)) {
337 if (parse_hunk_header(line, len,
338 &state->ob, &state->on,
339 &state->nb, &state->nn))
341 state->lno = state->nb;
342 if (state->nn == 0) {
343 /* @@ -X,Y +N,0 @@ removed Y lines
344 * that would have come *after* line N
345 * in the result. Our lost buckets hang
346 * to the line after the removed lines,
348 * Note that this is correct even when N == 0,
349 * in which case the hunk removes the first
352 state->lost_bucket = &state->sline[state->nb];
356 state->lost_bucket = &state->sline[state->nb-1];
358 if (!state->sline[state->nb-1].p_lno)
359 state->sline[state->nb-1].p_lno =
360 xcalloc(state->num_parent,
361 sizeof(unsigned long));
362 state->sline[state->nb-1].p_lno[state->n] = state->ob;
365 if (!state->lost_bucket)
366 return; /* not in any hunk yet */
369 append_lost(state->lost_bucket, state->n, line+1, len-1);
372 state->sline[state->lno-1].flag |= state->nmask;
378 static void combine_diff(const unsigned char *parent, unsigned int mode,
379 mmfile_t *result_file,
380 struct sline *sline, unsigned int cnt, int n,
381 int num_parent, int result_deleted,
382 struct userdiff_driver *textconv,
383 const char *path, long flags)
385 unsigned int p_lno, lno;
386 unsigned long nmask = (1UL << n);
389 mmfile_t parent_file;
390 struct combine_diff_state state;
394 return; /* result deleted */
396 parent_file.ptr = grab_blob(parent, mode, &sz, textconv, path);
397 parent_file.size = sz;
398 memset(&xpp, 0, sizeof(xpp));
400 memset(&xecfg, 0, sizeof(xecfg));
401 memset(&state, 0, sizeof(state));
405 state.num_parent = num_parent;
408 xdi_diff_outf(&parent_file, result_file, consume_line, &state,
410 free(parent_file.ptr);
412 /* Assign line numbers for this parent.
414 * sline[lno].p_lno[n] records the first line number
415 * (counting from 1) for parent N if the final hunk display
416 * started by showing sline[lno] (possibly showing the lost
417 * lines attached to it first).
419 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
421 sline[lno].p_lno[n] = p_lno;
423 /* Coalesce new lines */
424 if (sline[lno].plost.lost_head) {
425 struct sline *sl = &sline[lno];
426 sl->lost = coalesce_lines(sl->lost, &sl->lenlost,
428 sl->plost.len, n, flags);
429 sl->plost.lost_head = sl->plost.lost_tail = NULL;
433 /* How many lines would this sline advance the p_lno? */
434 ll = sline[lno].lost;
436 if (ll->parent_map & nmask)
437 p_lno++; /* '-' means parent had it */
440 if (lno < cnt && !(sline[lno].flag & nmask))
441 p_lno++; /* no '+' means parent had it */
443 sline[lno].p_lno[n] = p_lno; /* trailer */
446 static unsigned long context = 3;
447 static char combine_marker = '@';
449 static int interesting(struct sline *sline, unsigned long all_mask)
451 /* If some parents lost lines here, or if we have added to
452 * some parent, it is interesting.
454 return ((sline->flag & all_mask) || sline->lost);
457 static unsigned long adjust_hunk_tail(struct sline *sline,
458 unsigned long all_mask,
459 unsigned long hunk_begin,
462 /* i points at the first uninteresting line. If the last line
463 * of the hunk was interesting only because it has some
464 * deletion, then it is not all that interesting for the
465 * purpose of giving trailing context lines. This is because
466 * we output '-' line and then unmodified sline[i-1] itself in
467 * that case which gives us one extra context line.
469 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
474 static unsigned long find_next(struct sline *sline,
478 int look_for_uninteresting)
480 /* We have examined up to i-1 and are about to look at i.
481 * Find next interesting or uninteresting line. Here,
482 * "interesting" does not mean interesting(), but marked by
483 * the give_context() function below (i.e. it includes context
484 * lines that are not interesting to interesting() function
485 * that are surrounded by interesting() ones.
488 if (look_for_uninteresting
489 ? !(sline[i].flag & mark)
490 : (sline[i].flag & mark))
497 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
499 unsigned long all_mask = (1UL<<num_parent) - 1;
500 unsigned long mark = (1UL<<num_parent);
501 unsigned long no_pre_delete = (2UL<<num_parent);
504 /* Two groups of interesting lines may have a short gap of
505 * uninteresting lines. Connect such groups to give them a
508 * We first start from what the interesting() function says,
509 * and mark them with "mark", and paint context lines with the
510 * mark. So interesting() would still say false for such context
511 * lines but they are treated as "interesting" in the end.
513 i = find_next(sline, mark, 0, cnt, 0);
518 unsigned long j = (context < i) ? (i - context) : 0;
521 /* Paint a few lines before the first interesting line. */
523 if (!(sline[j].flag & mark))
524 sline[j].flag |= no_pre_delete;
525 sline[j++].flag |= mark;
529 /* we know up to i is to be included. where does the
530 * next uninteresting one start?
532 j = find_next(sline, mark, i, cnt, 1);
534 break; /* the rest are all interesting */
536 /* lookahead context lines */
537 k = find_next(sline, mark, j, cnt, 0);
538 j = adjust_hunk_tail(sline, all_mask, i, j);
540 if (k < j + context) {
541 /* k is interesting and [j,k) are not, but
542 * paint them interesting because the gap is small.
545 sline[j++].flag |= mark;
550 /* j is the first uninteresting line and there is
551 * no overlap beyond it within context lines. Paint
552 * the trailing edge a bit.
555 k = (j + context < cnt+1) ? j + context : cnt+1;
557 sline[j++].flag |= mark;
562 static int make_hunks(struct sline *sline, unsigned long cnt,
563 int num_parent, int dense)
565 unsigned long all_mask = (1UL<<num_parent) - 1;
566 unsigned long mark = (1UL<<num_parent);
568 int has_interesting = 0;
570 for (i = 0; i <= cnt; i++) {
571 if (interesting(&sline[i], all_mask))
572 sline[i].flag |= mark;
574 sline[i].flag &= ~mark;
577 return give_context(sline, cnt, num_parent);
579 /* Look at each hunk, and if we have changes from only one
580 * parent, or the changes are the same from all but one
581 * parent, mark that uninteresting.
585 unsigned long j, hunk_begin, hunk_end;
586 unsigned long same_diff;
587 while (i <= cnt && !(sline[i].flag & mark))
590 break; /* No more interesting hunks */
592 for (j = i + 1; j <= cnt; j++) {
593 if (!(sline[j].flag & mark)) {
594 /* Look beyond the end to see if there
595 * is an interesting line after this
596 * hunk within context span.
598 unsigned long la; /* lookahead */
600 la = adjust_hunk_tail(sline, all_mask,
602 la = (la + context < cnt + 1) ?
603 (la + context) : cnt + 1;
604 while (la && j <= --la) {
605 if (sline[la].flag & mark) {
617 /* [i..hunk_end) are interesting. Now is it really
618 * interesting? We check if there are only two versions
619 * and the result matches one of them. That is, we look
621 * (+) line, which records lines added to which parents;
622 * this line appears in the result.
623 * (-) line, which records from what parents the line
624 * was removed; this line does not appear in the result.
625 * then check the set of parents the result has difference
626 * from, from all lines. If there are lines that has
627 * different set of parents that the result has differences
628 * from, that means we have more than two versions.
630 * Even when we have only two versions, if the result does
631 * not match any of the parents, the it should be considered
632 * interesting. In such a case, we would have all '+' line.
633 * After passing the above "two versions" test, that would
634 * appear as "the same set of parents" to be "all parents".
638 for (j = i; j < hunk_end && !has_interesting; j++) {
639 unsigned long this_diff = sline[j].flag & all_mask;
640 struct lline *ll = sline[j].lost;
642 /* This has some changes. Is it the
646 same_diff = this_diff;
647 else if (same_diff != this_diff) {
652 while (ll && !has_interesting) {
653 /* Lost this line from these parents;
654 * who are they? Are they the same?
656 this_diff = ll->parent_map;
658 same_diff = this_diff;
659 else if (same_diff != this_diff) {
666 if (!has_interesting && same_diff != all_mask) {
667 /* This hunk is not that interesting after all */
668 for (j = hunk_begin; j < hunk_end; j++)
669 sline[j].flag &= ~mark;
674 has_interesting = give_context(sline, cnt, num_parent);
675 return has_interesting;
678 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
680 l0 = sline[l0].p_lno[n];
681 l1 = sline[l1].p_lno[n];
682 printf(" -%lu,%lu", l0, l1-l0-null_context);
685 static int hunk_comment_line(const char *bol)
692 return (isalpha(ch) || ch == '_' || ch == '$');
695 static void show_line_to_eol(const char *line, int len, const char *reset)
697 int saw_cr_at_eol = 0;
700 saw_cr_at_eol = (len && line[len-1] == '\r');
702 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
704 saw_cr_at_eol ? "\r" : "");
707 static void dump_sline(struct sline *sline, const char *line_prefix,
708 unsigned long cnt, int num_parent,
709 int use_color, int result_deleted)
711 unsigned long mark = (1UL<<num_parent);
712 unsigned long no_pre_delete = (2UL<<num_parent);
714 unsigned long lno = 0;
715 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
716 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
717 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
718 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
719 const char *c_plain = diff_get_color(use_color, DIFF_PLAIN);
720 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
723 return; /* result deleted */
726 unsigned long hunk_end;
727 unsigned long rlines;
728 const char *hunk_comment = NULL;
729 unsigned long null_context = 0;
731 while (lno <= cnt && !(sline[lno].flag & mark)) {
732 if (hunk_comment_line(sline[lno].bol))
733 hunk_comment = sline[lno].bol;
739 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
740 if (!(sline[hunk_end].flag & mark))
743 rlines = hunk_end - lno;
745 rlines--; /* pointing at the last delete hunk */
749 * Even when running with --unified=0, all
750 * lines in the hunk needs to be processed in
751 * the loop below in order to show the
752 * deletion recorded in lost_head. However,
753 * we do not want to show the resulting line
754 * with all blank context markers in such a
758 for (j = lno; j < hunk_end; j++)
759 if (!(sline[j].flag & (mark-1)))
761 rlines -= null_context;
764 printf("%s%s", line_prefix, c_frag);
765 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
766 for (i = 0; i < num_parent; i++)
767 show_parent_lno(sline, lno, hunk_end, i, null_context);
768 printf(" +%lu,%lu ", lno+1, rlines);
769 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
773 for (i = 0; i < 40; i++) {
774 int ch = hunk_comment[i] & 0xff;
775 if (!ch || ch == '\n')
781 printf("%s%s %s%s", c_reset,
784 for (i = 0; i < comment_end; i++)
785 putchar(hunk_comment[i]);
788 printf("%s\n", c_reset);
789 while (lno < hunk_end) {
792 unsigned long p_mask;
793 struct sline *sl = &sline[lno++];
794 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost;
796 printf("%s%s", line_prefix, c_old);
797 for (j = 0; j < num_parent; j++) {
798 if (ll->parent_map & (1UL<<j))
803 show_line_to_eol(ll->line, -1, c_reset);
809 fputs(line_prefix, stdout);
810 if (!(sl->flag & (mark-1))) {
812 * This sline was here to hang the
813 * lost lines in front of it.
817 fputs(c_plain, stdout);
820 fputs(c_new, stdout);
821 for (j = 0; j < num_parent; j++) {
822 if (p_mask & sl->flag)
828 show_line_to_eol(sl->bol, sl->len, c_reset);
833 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
836 /* We have already examined parent j and we know parent i
837 * and parent j are the same, so reuse the combined result
838 * of parent j for parent i.
840 unsigned long lno, imask, jmask;
844 for (lno = 0; lno <= cnt; lno++) {
845 struct lline *ll = sline->lost;
846 sline->p_lno[i] = sline->p_lno[j];
848 if (ll->parent_map & jmask)
849 ll->parent_map |= imask;
852 if (sline->flag & jmask)
853 sline->flag |= imask;
856 /* the overall size of the file (sline[cnt]) */
857 sline->p_lno[i] = sline->p_lno[j];
860 static void dump_quoted_path(const char *head,
863 const char *line_prefix,
864 const char *c_meta, const char *c_reset)
866 static struct strbuf buf = STRBUF_INIT;
869 strbuf_addstr(&buf, line_prefix);
870 strbuf_addstr(&buf, c_meta);
871 strbuf_addstr(&buf, head);
872 quote_two_c_style(&buf, prefix, path, 0);
873 strbuf_addstr(&buf, c_reset);
877 static void show_combined_header(struct combine_diff_path *elem,
880 struct rev_info *rev,
881 const char *line_prefix,
883 int show_file_header)
885 struct diff_options *opt = &rev->diffopt;
886 int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
887 const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
888 const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
889 const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
890 const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
896 if (rev->loginfo && !rev->no_commit_id)
899 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
900 "", elem->path, line_prefix, c_meta, c_reset);
901 printf("%s%sindex ", line_prefix, c_meta);
902 for (i = 0; i < num_parent; i++) {
903 abb = find_unique_abbrev(elem->parent[i].sha1,
905 printf("%s%s", i ? "," : "", abb);
907 abb = find_unique_abbrev(elem->sha1, abbrev);
908 printf("..%s%s\n", abb, c_reset);
911 deleted = !elem->mode;
913 /* We say it was added if nobody had it */
915 for (i = 0; added && i < num_parent; i++)
916 if (elem->parent[i].status !=
920 printf("%s%snew file mode %06o",
921 line_prefix, c_meta, elem->mode);
924 printf("%s%sdeleted file ",
925 line_prefix, c_meta);
927 for (i = 0; i < num_parent; i++) {
928 printf("%s%06o", i ? "," : "",
929 elem->parent[i].mode);
932 printf("..%06o", elem->mode);
934 printf("%s\n", c_reset);
937 if (!show_file_header)
941 dump_quoted_path("--- ", "", "/dev/null",
942 line_prefix, c_meta, c_reset);
944 dump_quoted_path("--- ", a_prefix, elem->path,
945 line_prefix, c_meta, c_reset);
947 dump_quoted_path("+++ ", "", "/dev/null",
948 line_prefix, c_meta, c_reset);
950 dump_quoted_path("+++ ", b_prefix, elem->path,
951 line_prefix, c_meta, c_reset);
954 static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
955 int dense, int working_tree_file,
956 struct rev_info *rev)
958 struct diff_options *opt = &rev->diffopt;
959 unsigned long result_size, cnt, lno;
960 int result_deleted = 0;
962 struct sline *sline; /* survived lines */
963 int mode_differs = 0;
965 mmfile_t result_file;
966 struct userdiff_driver *userdiff;
967 struct userdiff_driver *textconv = NULL;
969 const char *line_prefix = diff_line_prefix(opt);
971 context = opt->context;
972 userdiff = userdiff_find_by_path(elem->path);
974 userdiff = userdiff_find_by_name("default");
975 if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV))
976 textconv = userdiff_get_textconv(userdiff);
978 /* Read the result of merge first */
979 if (!working_tree_file)
980 result = grab_blob(elem->sha1, elem->mode, &result_size,
981 textconv, elem->path);
983 /* Used by diff-tree to read from the working tree */
987 if (lstat(elem->path, &st) < 0)
990 if (S_ISLNK(st.st_mode)) {
991 struct strbuf buf = STRBUF_INIT;
993 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
994 error("readlink(%s): %s", elem->path,
998 result_size = buf.len;
999 result = strbuf_detach(&buf, NULL);
1000 elem->mode = canon_mode(st.st_mode);
1001 } else if (S_ISDIR(st.st_mode)) {
1002 unsigned char sha1[20];
1003 if (resolve_gitlink_ref(elem->path, "HEAD", sha1) < 0)
1004 result = grab_blob(elem->sha1, elem->mode,
1005 &result_size, NULL, NULL);
1007 result = grab_blob(sha1, elem->mode,
1008 &result_size, NULL, NULL);
1009 } else if (textconv) {
1010 struct diff_filespec *df = alloc_filespec(elem->path);
1011 fill_filespec(df, null_sha1, 0, st.st_mode);
1012 result_size = fill_textconv(textconv, df, &result);
1014 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
1015 size_t len = xsize_t(st.st_size);
1019 elem->mode = canon_mode(st.st_mode);
1020 /* if symlinks don't work, assume symlink if all parents
1023 is_file = has_symlinks;
1024 for (i = 0; !is_file && i < num_parent; i++)
1025 is_file = !S_ISLNK(elem->parent[i].mode);
1027 elem->mode = canon_mode(S_IFLNK);
1030 result = xmalloc(len + 1);
1032 done = read_in_full(fd, result, len);
1034 die_errno("read error '%s'", elem->path);
1035 else if (done < len)
1036 die("early EOF '%s'", elem->path);
1040 /* If not a fake symlink, apply filters, e.g. autocrlf */
1042 struct strbuf buf = STRBUF_INIT;
1044 if (convert_to_git(elem->path, result, len, &buf, safe_crlf)) {
1046 result = strbuf_detach(&buf, &len);
1056 result = xcalloc(1, 1);
1063 for (i = 0; i < num_parent; i++) {
1064 if (elem->parent[i].mode != elem->mode) {
1072 else if (userdiff->binary != -1)
1073 is_binary = userdiff->binary;
1075 is_binary = buffer_is_binary(result, result_size);
1076 for (i = 0; !is_binary && i < num_parent; i++) {
1079 buf = grab_blob(elem->parent[i].sha1,
1080 elem->parent[i].mode,
1082 if (buffer_is_binary(buf, size))
1088 show_combined_header(elem, num_parent, dense, rev,
1089 line_prefix, mode_differs, 0);
1090 printf("Binary files differ\n");
1095 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
1099 if (result_size && result[result_size-1] != '\n')
1100 cnt++; /* incomplete line */
1102 sline = xcalloc(cnt+2, sizeof(*sline));
1103 sline[0].bol = result;
1104 for (lno = 0, cp = result; cp < result + result_size; cp++) {
1106 sline[lno].len = cp - sline[lno].bol;
1109 sline[lno].bol = cp + 1;
1112 if (result_size && result[result_size-1] != '\n')
1113 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
1115 result_file.ptr = result;
1116 result_file.size = result_size;
1118 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1119 * for deletion hunk at the end.
1121 sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
1122 for (lno = 0; lno <= cnt; lno++)
1123 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
1125 for (i = 0; i < num_parent; i++) {
1127 for (j = 0; j < i; j++) {
1128 if (!hashcmp(elem->parent[i].sha1,
1129 elem->parent[j].sha1)) {
1130 reuse_combine_diff(sline, cnt, i, j);
1135 combine_diff(elem->parent[i].sha1,
1136 elem->parent[i].mode,
1137 &result_file, sline,
1138 cnt, i, num_parent, result_deleted,
1139 textconv, elem->path, opt->xdl_opts);
1142 show_hunks = make_hunks(sline, cnt, num_parent, dense);
1144 if (show_hunks || mode_differs || working_tree_file) {
1145 show_combined_header(elem, num_parent, dense, rev,
1146 line_prefix, mode_differs, 1);
1147 dump_sline(sline, line_prefix, cnt, num_parent,
1148 opt->use_color, result_deleted);
1152 for (lno = 0; lno < cnt; lno++) {
1153 if (sline[lno].lost) {
1154 struct lline *ll = sline[lno].lost;
1156 struct lline *tmp = ll;
1162 free(sline[0].p_lno);
1166 static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
1168 struct diff_options *opt = &rev->diffopt;
1169 int line_termination, inter_name_termination, i;
1170 const char *line_prefix = diff_line_prefix(opt);
1172 line_termination = opt->line_termination;
1173 inter_name_termination = '\t';
1174 if (!line_termination)
1175 inter_name_termination = 0;
1177 if (rev->loginfo && !rev->no_commit_id)
1181 if (opt->output_format & DIFF_FORMAT_RAW) {
1182 printf("%s", line_prefix);
1184 /* As many colons as there are parents */
1185 for (i = 0; i < num_parent; i++)
1188 /* Show the modes */
1189 for (i = 0; i < num_parent; i++)
1190 printf("%06o ", p->parent[i].mode);
1191 printf("%06o", p->mode);
1194 for (i = 0; i < num_parent; i++)
1195 printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
1197 printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
1200 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
1201 for (i = 0; i < num_parent; i++)
1202 putchar(p->parent[i].status);
1203 putchar(inter_name_termination);
1206 write_name_quoted(p->path, stdout, line_termination);
1210 * The result (p->elem) is from the working tree and their
1211 * parents are typically from multiple stages during a merge
1212 * (i.e. diff-files) or the state in HEAD and in the index
1213 * (i.e. diff-index).
1215 void show_combined_diff(struct combine_diff_path *p,
1218 struct rev_info *rev)
1220 struct diff_options *opt = &rev->diffopt;
1224 if (opt->output_format & (DIFF_FORMAT_RAW |
1226 DIFF_FORMAT_NAME_STATUS))
1227 show_raw_diff(p, num_parent, rev);
1228 else if (opt->output_format & DIFF_FORMAT_PATCH)
1229 show_patch_diff(p, num_parent, dense, 1, rev);
1232 static void free_combined_pair(struct diff_filepair *pair)
1239 * A combine_diff_path expresses N parents on the LHS against 1 merge
1240 * result. Synthesize a diff_filepair that has N entries on the "one"
1241 * side and 1 entry on the "two" side.
1243 * In the future, we might want to add more data to combine_diff_path
1244 * so that we can fill fields we are ignoring (most notably, size) here,
1245 * but currently nobody uses it, so this should suffice for now.
1247 static struct diff_filepair *combined_pair(struct combine_diff_path *p,
1251 struct diff_filepair *pair;
1252 struct diff_filespec *pool;
1254 pair = xmalloc(sizeof(*pair));
1255 pool = xcalloc(num_parent + 1, sizeof(struct diff_filespec));
1256 pair->one = pool + 1;
1259 for (i = 0; i < num_parent; i++) {
1260 pair->one[i].path = p->path;
1261 pair->one[i].mode = p->parent[i].mode;
1262 hashcpy(pair->one[i].sha1, p->parent[i].sha1);
1263 pair->one[i].sha1_valid = !is_null_sha1(p->parent[i].sha1);
1264 pair->one[i].has_more_entries = 1;
1266 pair->one[num_parent - 1].has_more_entries = 0;
1268 pair->two->path = p->path;
1269 pair->two->mode = p->mode;
1270 hashcpy(pair->two->sha1, p->sha1);
1271 pair->two->sha1_valid = !is_null_sha1(p->sha1);
1275 static void handle_combined_callback(struct diff_options *opt,
1276 struct combine_diff_path *paths,
1280 struct combine_diff_path *p;
1281 struct diff_queue_struct q;
1284 q.queue = xcalloc(num_paths, sizeof(struct diff_filepair *));
1285 q.alloc = num_paths;
1287 for (i = 0, p = paths; p; p = p->next) {
1290 q.queue[i++] = combined_pair(p, num_parent);
1292 opt->format_callback(&q, opt, opt->format_callback_data);
1293 for (i = 0; i < num_paths; i++)
1294 free_combined_pair(q.queue[i]);
1298 void diff_tree_combined(const unsigned char *sha1,
1299 const struct sha1_array *parents,
1301 struct rev_info *rev)
1303 struct diff_options *opt = &rev->diffopt;
1304 struct diff_options diffopts;
1305 struct combine_diff_path *p, *paths = NULL;
1306 int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
1309 copy_pathspec(&diffopts.pathspec, &opt->pathspec);
1310 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
1311 DIFF_OPT_SET(&diffopts, RECURSIVE);
1312 DIFF_OPT_CLR(&diffopts, ALLOW_EXTERNAL);
1314 show_log_first = !!rev->loginfo && !rev->no_commit_id;
1316 /* find set of paths that everybody touches */
1317 for (i = 0; i < num_parent; i++) {
1318 /* show stat against the first parent even
1319 * when doing combined diff.
1321 int stat_opt = (opt->output_format &
1322 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
1323 if (i == 0 && stat_opt)
1324 diffopts.output_format = stat_opt;
1326 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
1327 diff_tree_sha1(parents->sha1[i], sha1, "", &diffopts);
1328 diffcore_std(&diffopts);
1329 paths = intersect_paths(paths, i, num_parent);
1331 if (show_log_first && i == 0) {
1334 if (rev->verbose_header && opt->output_format)
1335 printf("%s%c", diff_line_prefix(opt),
1336 opt->line_termination);
1338 diff_flush(&diffopts);
1341 /* find out surviving paths */
1342 for (num_paths = 0, p = paths; p; p = p->next) {
1347 if (opt->output_format & (DIFF_FORMAT_RAW |
1349 DIFF_FORMAT_NAME_STATUS)) {
1350 for (p = paths; p; p = p->next) {
1352 show_raw_diff(p, num_parent, rev);
1356 else if (opt->output_format &
1357 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
1359 else if (opt->output_format & DIFF_FORMAT_CALLBACK)
1360 handle_combined_callback(opt, paths, num_parent, num_paths);
1362 if (opt->output_format & DIFF_FORMAT_PATCH) {
1364 printf("%s%c", diff_line_prefix(opt),
1365 opt->line_termination);
1366 for (p = paths; p; p = p->next) {
1368 show_patch_diff(p, num_parent, dense,
1374 /* Clean things up */
1376 struct combine_diff_path *tmp = paths;
1377 paths = paths->next;
1381 free_pathspec(&diffopts.pathspec);
1384 void diff_tree_combined_merge(const struct commit *commit, int dense,
1385 struct rev_info *rev)
1387 struct commit_list *parent = get_saved_parents(rev, commit);
1388 struct sha1_array parents = SHA1_ARRAY_INIT;
1391 sha1_array_append(&parents, parent->item->object.sha1);
1392 parent = parent->next;
1394 diff_tree_combined(commit->object.sha1, &parents, dense, rev);
1395 sha1_array_clear(&parents);