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, **tail = &curr;
22 for (i = 0; i < q->nr; i++) {
25 if (diff_unmodified_pair(q->queue[i]))
27 path = q->queue[i]->two->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);
35 sizeof(p->parent[0]) * num_parent);
37 hashcpy(p->sha1, q->queue[i]->two->sha1);
38 p->mode = q->queue[i]->two->mode;
39 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
40 p->parent[n].mode = q->queue[i]->one->mode;
41 p->parent[n].status = q->queue[i]->status;
49 * paths in curr (linked list) and q->queue[] (array) are
50 * both sorted in the tree order.
53 while ((p = *tail) != NULL) {
55 ? -1 : strcmp(p->path, q->queue[i]->two->path));
58 /* p->path not in q->queue[]; drop it */
65 /* q->queue[i] not in p->path; skip it */
70 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
71 p->parent[n].mode = q->queue[i]->one->mode;
72 p->parent[n].status = q->queue[i]->status;
80 /* Lines lost from parent */
82 struct lline *next, *prev;
84 unsigned long parent_map;
85 char line[FLEX_ARRAY];
88 /* Lines lost from current parent (before coalescing) */
90 struct lline *lost_head, *lost_tail;
94 /* Lines surviving in the merge result */
96 /* Accumulated and coalesced lost lines */
102 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
103 * we did not change it).
104 * bit N is used for "interesting" lines, including context.
105 * bit (N+1) is used for "do not show deletion before this".
108 unsigned long *p_lno;
111 static int match_string_spaces(const char *line1, int len1,
112 const char *line2, int len2,
115 if (flags & XDF_WHITESPACE_FLAGS) {
116 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
117 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
120 if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
121 return (len1 == len2 && !memcmp(line1, line2, len1));
123 while (len1 > 0 && len2 > 0) {
126 if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
127 if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
128 (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
131 for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
132 for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
134 if (line1[len1] != line2[len2])
138 if (flags & XDF_IGNORE_WHITESPACE) {
139 /* Consume remaining spaces */
140 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
141 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
144 /* We matched full line1 and line2 */
151 enum coalesce_direction { MATCH, BASE, NEW };
153 /* Coalesce new lines into base by finding LCS */
154 static struct lline *coalesce_lines(struct lline *base, int *lenbase,
155 struct lline *new, int lennew,
156 unsigned long parent, long flags)
159 enum coalesce_direction **directions;
160 struct lline *baseend, *newend = NULL;
161 int i, j, origbaselen = *lenbase;
172 * Coalesce new lines into base by finding the LCS
173 * - Create the table to run dynamic programming
175 * - Then reverse read the direction structure:
176 * - If we have MATCH, assign parent to base flag, and consume
177 * both baseend and newend
178 * - Else if we have BASE, consume baseend
179 * - Else if we have NEW, insert newend lline into base and
182 lcs = xcalloc(origbaselen + 1, sizeof(int*));
183 directions = xcalloc(origbaselen + 1, sizeof(enum coalesce_direction*));
184 for (i = 0; i < origbaselen + 1; i++) {
185 lcs[i] = xcalloc(lennew + 1, sizeof(int));
186 directions[i] = xcalloc(lennew + 1, sizeof(enum coalesce_direction));
187 directions[i][0] = BASE;
189 for (j = 1; j < lennew + 1; j++)
190 directions[0][j] = NEW;
192 for (i = 1, baseend = base; i < origbaselen + 1; i++) {
193 for (j = 1, newend = new; j < lennew + 1; j++) {
194 if (match_string_spaces(baseend->line, baseend->len,
195 newend->line, newend->len, flags)) {
196 lcs[i][j] = lcs[i - 1][j - 1] + 1;
197 directions[i][j] = MATCH;
198 } else if (lcs[i][j - 1] >= lcs[i - 1][j]) {
199 lcs[i][j] = lcs[i][j - 1];
200 directions[i][j] = NEW;
202 lcs[i][j] = lcs[i - 1][j];
203 directions[i][j] = BASE;
206 newend = newend->next;
209 baseend = baseend->next;
212 for (i = 0; i < origbaselen + 1; i++)
216 /* At this point, baseend and newend point to the end of each lists */
219 while (i != 0 || j != 0) {
220 if (directions[i][j] == MATCH) {
221 baseend->parent_map |= 1<<parent;
222 baseend = baseend->prev;
223 newend = newend->prev;
226 } else if (directions[i][j] == NEW) {
230 /* Remove lline from new list and update newend */
232 lline->prev->next = lline->next;
236 lline->next->prev = lline->prev;
238 newend = lline->prev;
241 /* Add lline to base list */
243 lline->next = baseend->next;
244 lline->prev = baseend;
246 lline->prev->next = lline;
255 lline->next->prev = lline;
258 baseend = baseend->prev;
265 struct lline *lline = newend;
266 newend = newend->next;
270 for (i = 0; i < origbaselen + 1; i++)
277 static char *grab_blob(const unsigned char *sha1, unsigned int mode,
278 unsigned long *size, struct userdiff_driver *textconv,
282 enum object_type type;
284 if (S_ISGITLINK(mode)) {
286 *size = snprintf(blob, 100,
287 "Subproject commit %s\n", sha1_to_hex(sha1));
288 } else if (is_null_sha1(sha1)) {
291 return xcalloc(1, 1);
292 } else if (textconv) {
293 struct diff_filespec *df = alloc_filespec(path);
294 fill_filespec(df, sha1, 1, mode);
295 *size = fill_textconv(textconv, df, &blob);
298 blob = read_sha1_file(sha1, &type, size);
299 if (type != OBJ_BLOB)
300 die("object '%s' is not a blob!", sha1_to_hex(sha1));
305 static void append_lost(struct sline *sline, int n, const char *line, int len)
308 unsigned long this_mask = (1UL<<n);
309 if (line[len-1] == '\n')
312 lline = xmalloc(sizeof(*lline) + len + 1);
315 lline->prev = sline->plost.lost_tail;
317 lline->prev->next = lline;
319 sline->plost.lost_head = lline;
320 sline->plost.lost_tail = lline;
322 lline->parent_map = this_mask;
323 memcpy(lline->line, line, len);
324 lline->line[len] = 0;
327 struct combine_diff_state {
334 struct sline *lost_bucket;
337 static void consume_line(void *state_, char *line, unsigned long len)
339 struct combine_diff_state *state = state_;
340 if (5 < len && !memcmp("@@ -", line, 4)) {
341 if (parse_hunk_header(line, len,
342 &state->ob, &state->on,
343 &state->nb, &state->nn))
345 state->lno = state->nb;
346 if (state->nn == 0) {
347 /* @@ -X,Y +N,0 @@ removed Y lines
348 * that would have come *after* line N
349 * in the result. Our lost buckets hang
350 * to the line after the removed lines,
352 * Note that this is correct even when N == 0,
353 * in which case the hunk removes the first
356 state->lost_bucket = &state->sline[state->nb];
360 state->lost_bucket = &state->sline[state->nb-1];
362 if (!state->sline[state->nb-1].p_lno)
363 state->sline[state->nb-1].p_lno =
364 xcalloc(state->num_parent,
365 sizeof(unsigned long));
366 state->sline[state->nb-1].p_lno[state->n] = state->ob;
369 if (!state->lost_bucket)
370 return; /* not in any hunk yet */
373 append_lost(state->lost_bucket, state->n, line+1, len-1);
376 state->sline[state->lno-1].flag |= state->nmask;
382 static void combine_diff(const unsigned char *parent, unsigned int mode,
383 mmfile_t *result_file,
384 struct sline *sline, unsigned int cnt, int n,
385 int num_parent, int result_deleted,
386 struct userdiff_driver *textconv,
387 const char *path, long flags)
389 unsigned int p_lno, lno;
390 unsigned long nmask = (1UL << n);
393 mmfile_t parent_file;
394 struct combine_diff_state state;
398 return; /* result deleted */
400 parent_file.ptr = grab_blob(parent, mode, &sz, textconv, path);
401 parent_file.size = sz;
402 memset(&xpp, 0, sizeof(xpp));
404 memset(&xecfg, 0, sizeof(xecfg));
405 memset(&state, 0, sizeof(state));
409 state.num_parent = num_parent;
412 xdi_diff_outf(&parent_file, result_file, consume_line, &state,
414 free(parent_file.ptr);
416 /* Assign line numbers for this parent.
418 * sline[lno].p_lno[n] records the first line number
419 * (counting from 1) for parent N if the final hunk display
420 * started by showing sline[lno] (possibly showing the lost
421 * lines attached to it first).
423 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
425 sline[lno].p_lno[n] = p_lno;
427 /* Coalesce new lines */
428 if (sline[lno].plost.lost_head) {
429 struct sline *sl = &sline[lno];
430 sl->lost = coalesce_lines(sl->lost, &sl->lenlost,
432 sl->plost.len, n, flags);
433 sl->plost.lost_head = sl->plost.lost_tail = NULL;
437 /* How many lines would this sline advance the p_lno? */
438 ll = sline[lno].lost;
440 if (ll->parent_map & nmask)
441 p_lno++; /* '-' means parent had it */
444 if (lno < cnt && !(sline[lno].flag & nmask))
445 p_lno++; /* no '+' means parent had it */
447 sline[lno].p_lno[n] = p_lno; /* trailer */
450 static unsigned long context = 3;
451 static char combine_marker = '@';
453 static int interesting(struct sline *sline, unsigned long all_mask)
455 /* If some parents lost lines here, or if we have added to
456 * some parent, it is interesting.
458 return ((sline->flag & all_mask) || sline->lost);
461 static unsigned long adjust_hunk_tail(struct sline *sline,
462 unsigned long all_mask,
463 unsigned long hunk_begin,
466 /* i points at the first uninteresting line. If the last line
467 * of the hunk was interesting only because it has some
468 * deletion, then it is not all that interesting for the
469 * purpose of giving trailing context lines. This is because
470 * we output '-' line and then unmodified sline[i-1] itself in
471 * that case which gives us one extra context line.
473 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
478 static unsigned long find_next(struct sline *sline,
482 int look_for_uninteresting)
484 /* We have examined up to i-1 and are about to look at i.
485 * Find next interesting or uninteresting line. Here,
486 * "interesting" does not mean interesting(), but marked by
487 * the give_context() function below (i.e. it includes context
488 * lines that are not interesting to interesting() function
489 * that are surrounded by interesting() ones.
492 if (look_for_uninteresting
493 ? !(sline[i].flag & mark)
494 : (sline[i].flag & mark))
501 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
503 unsigned long all_mask = (1UL<<num_parent) - 1;
504 unsigned long mark = (1UL<<num_parent);
505 unsigned long no_pre_delete = (2UL<<num_parent);
508 /* Two groups of interesting lines may have a short gap of
509 * uninteresting lines. Connect such groups to give them a
512 * We first start from what the interesting() function says,
513 * and mark them with "mark", and paint context lines with the
514 * mark. So interesting() would still say false for such context
515 * lines but they are treated as "interesting" in the end.
517 i = find_next(sline, mark, 0, cnt, 0);
522 unsigned long j = (context < i) ? (i - context) : 0;
525 /* Paint a few lines before the first interesting line. */
527 if (!(sline[j].flag & mark))
528 sline[j].flag |= no_pre_delete;
529 sline[j++].flag |= mark;
533 /* we know up to i is to be included. where does the
534 * next uninteresting one start?
536 j = find_next(sline, mark, i, cnt, 1);
538 break; /* the rest are all interesting */
540 /* lookahead context lines */
541 k = find_next(sline, mark, j, cnt, 0);
542 j = adjust_hunk_tail(sline, all_mask, i, j);
544 if (k < j + context) {
545 /* k is interesting and [j,k) are not, but
546 * paint them interesting because the gap is small.
549 sline[j++].flag |= mark;
554 /* j is the first uninteresting line and there is
555 * no overlap beyond it within context lines. Paint
556 * the trailing edge a bit.
559 k = (j + context < cnt+1) ? j + context : cnt+1;
561 sline[j++].flag |= mark;
566 static int make_hunks(struct sline *sline, unsigned long cnt,
567 int num_parent, int dense)
569 unsigned long all_mask = (1UL<<num_parent) - 1;
570 unsigned long mark = (1UL<<num_parent);
572 int has_interesting = 0;
574 for (i = 0; i <= cnt; i++) {
575 if (interesting(&sline[i], all_mask))
576 sline[i].flag |= mark;
578 sline[i].flag &= ~mark;
581 return give_context(sline, cnt, num_parent);
583 /* Look at each hunk, and if we have changes from only one
584 * parent, or the changes are the same from all but one
585 * parent, mark that uninteresting.
589 unsigned long j, hunk_begin, hunk_end;
590 unsigned long same_diff;
591 while (i <= cnt && !(sline[i].flag & mark))
594 break; /* No more interesting hunks */
596 for (j = i + 1; j <= cnt; j++) {
597 if (!(sline[j].flag & mark)) {
598 /* Look beyond the end to see if there
599 * is an interesting line after this
600 * hunk within context span.
602 unsigned long la; /* lookahead */
604 la = adjust_hunk_tail(sline, all_mask,
606 la = (la + context < cnt + 1) ?
607 (la + context) : cnt + 1;
608 while (la && j <= --la) {
609 if (sline[la].flag & mark) {
621 /* [i..hunk_end) are interesting. Now is it really
622 * interesting? We check if there are only two versions
623 * and the result matches one of them. That is, we look
625 * (+) line, which records lines added to which parents;
626 * this line appears in the result.
627 * (-) line, which records from what parents the line
628 * was removed; this line does not appear in the result.
629 * then check the set of parents the result has difference
630 * from, from all lines. If there are lines that has
631 * different set of parents that the result has differences
632 * from, that means we have more than two versions.
634 * Even when we have only two versions, if the result does
635 * not match any of the parents, the it should be considered
636 * interesting. In such a case, we would have all '+' line.
637 * After passing the above "two versions" test, that would
638 * appear as "the same set of parents" to be "all parents".
642 for (j = i; j < hunk_end && !has_interesting; j++) {
643 unsigned long this_diff = sline[j].flag & all_mask;
644 struct lline *ll = sline[j].lost;
646 /* This has some changes. Is it the
650 same_diff = this_diff;
651 else if (same_diff != this_diff) {
656 while (ll && !has_interesting) {
657 /* Lost this line from these parents;
658 * who are they? Are they the same?
660 this_diff = ll->parent_map;
662 same_diff = this_diff;
663 else if (same_diff != this_diff) {
670 if (!has_interesting && same_diff != all_mask) {
671 /* This hunk is not that interesting after all */
672 for (j = hunk_begin; j < hunk_end; j++)
673 sline[j].flag &= ~mark;
678 has_interesting = give_context(sline, cnt, num_parent);
679 return has_interesting;
682 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
684 l0 = sline[l0].p_lno[n];
685 l1 = sline[l1].p_lno[n];
686 printf(" -%lu,%lu", l0, l1-l0-null_context);
689 static int hunk_comment_line(const char *bol)
696 return (isalpha(ch) || ch == '_' || ch == '$');
699 static void show_line_to_eol(const char *line, int len, const char *reset)
701 int saw_cr_at_eol = 0;
704 saw_cr_at_eol = (len && line[len-1] == '\r');
706 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
708 saw_cr_at_eol ? "\r" : "");
711 static void dump_sline(struct sline *sline, const char *line_prefix,
712 unsigned long cnt, int num_parent,
713 int use_color, int result_deleted)
715 unsigned long mark = (1UL<<num_parent);
716 unsigned long no_pre_delete = (2UL<<num_parent);
718 unsigned long lno = 0;
719 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
720 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
721 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
722 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
723 const char *c_plain = diff_get_color(use_color, DIFF_PLAIN);
724 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
727 return; /* result deleted */
730 unsigned long hunk_end;
731 unsigned long rlines;
732 const char *hunk_comment = NULL;
733 unsigned long null_context = 0;
735 while (lno <= cnt && !(sline[lno].flag & mark)) {
736 if (hunk_comment_line(sline[lno].bol))
737 hunk_comment = sline[lno].bol;
743 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
744 if (!(sline[hunk_end].flag & mark))
747 rlines = hunk_end - lno;
749 rlines--; /* pointing at the last delete hunk */
753 * Even when running with --unified=0, all
754 * lines in the hunk needs to be processed in
755 * the loop below in order to show the
756 * deletion recorded in lost_head. However,
757 * we do not want to show the resulting line
758 * with all blank context markers in such a
762 for (j = lno; j < hunk_end; j++)
763 if (!(sline[j].flag & (mark-1)))
765 rlines -= null_context;
768 printf("%s%s", line_prefix, c_frag);
769 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
770 for (i = 0; i < num_parent; i++)
771 show_parent_lno(sline, lno, hunk_end, i, null_context);
772 printf(" +%lu,%lu ", lno+1, rlines);
773 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
777 for (i = 0; i < 40; i++) {
778 int ch = hunk_comment[i] & 0xff;
779 if (!ch || ch == '\n')
785 printf("%s%s %s%s", c_reset,
788 for (i = 0; i < comment_end; i++)
789 putchar(hunk_comment[i]);
792 printf("%s\n", c_reset);
793 while (lno < hunk_end) {
796 unsigned long p_mask;
797 struct sline *sl = &sline[lno++];
798 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost;
800 printf("%s%s", line_prefix, c_old);
801 for (j = 0; j < num_parent; j++) {
802 if (ll->parent_map & (1UL<<j))
807 show_line_to_eol(ll->line, -1, c_reset);
813 fputs(line_prefix, stdout);
814 if (!(sl->flag & (mark-1))) {
816 * This sline was here to hang the
817 * lost lines in front of it.
821 fputs(c_plain, stdout);
824 fputs(c_new, stdout);
825 for (j = 0; j < num_parent; j++) {
826 if (p_mask & sl->flag)
832 show_line_to_eol(sl->bol, sl->len, c_reset);
837 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
840 /* We have already examined parent j and we know parent i
841 * and parent j are the same, so reuse the combined result
842 * of parent j for parent i.
844 unsigned long lno, imask, jmask;
848 for (lno = 0; lno <= cnt; lno++) {
849 struct lline *ll = sline->lost;
850 sline->p_lno[i] = sline->p_lno[j];
852 if (ll->parent_map & jmask)
853 ll->parent_map |= imask;
856 if (sline->flag & jmask)
857 sline->flag |= imask;
860 /* the overall size of the file (sline[cnt]) */
861 sline->p_lno[i] = sline->p_lno[j];
864 static void dump_quoted_path(const char *head,
867 const char *line_prefix,
868 const char *c_meta, const char *c_reset)
870 static struct strbuf buf = STRBUF_INIT;
873 strbuf_addstr(&buf, line_prefix);
874 strbuf_addstr(&buf, c_meta);
875 strbuf_addstr(&buf, head);
876 quote_two_c_style(&buf, prefix, path, 0);
877 strbuf_addstr(&buf, c_reset);
881 static void show_combined_header(struct combine_diff_path *elem,
884 struct rev_info *rev,
885 const char *line_prefix,
887 int show_file_header)
889 struct diff_options *opt = &rev->diffopt;
890 int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
891 const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
892 const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
893 const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
894 const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
900 if (rev->loginfo && !rev->no_commit_id)
903 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
904 "", elem->path, line_prefix, c_meta, c_reset);
905 printf("%s%sindex ", line_prefix, c_meta);
906 for (i = 0; i < num_parent; i++) {
907 abb = find_unique_abbrev(elem->parent[i].sha1,
909 printf("%s%s", i ? "," : "", abb);
911 abb = find_unique_abbrev(elem->sha1, abbrev);
912 printf("..%s%s\n", abb, c_reset);
915 deleted = !elem->mode;
917 /* We say it was added if nobody had it */
919 for (i = 0; added && i < num_parent; i++)
920 if (elem->parent[i].status !=
924 printf("%s%snew file mode %06o",
925 line_prefix, c_meta, elem->mode);
928 printf("%s%sdeleted file ",
929 line_prefix, c_meta);
931 for (i = 0; i < num_parent; i++) {
932 printf("%s%06o", i ? "," : "",
933 elem->parent[i].mode);
936 printf("..%06o", elem->mode);
938 printf("%s\n", c_reset);
941 if (!show_file_header)
945 dump_quoted_path("--- ", "", "/dev/null",
946 line_prefix, c_meta, c_reset);
948 dump_quoted_path("--- ", a_prefix, elem->path,
949 line_prefix, c_meta, c_reset);
951 dump_quoted_path("+++ ", "", "/dev/null",
952 line_prefix, c_meta, c_reset);
954 dump_quoted_path("+++ ", b_prefix, elem->path,
955 line_prefix, c_meta, c_reset);
958 static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
959 int dense, int working_tree_file,
960 struct rev_info *rev)
962 struct diff_options *opt = &rev->diffopt;
963 unsigned long result_size, cnt, lno;
964 int result_deleted = 0;
966 struct sline *sline; /* survived lines */
967 int mode_differs = 0;
969 mmfile_t result_file;
970 struct userdiff_driver *userdiff;
971 struct userdiff_driver *textconv = NULL;
973 const char *line_prefix = diff_line_prefix(opt);
975 context = opt->context;
976 userdiff = userdiff_find_by_path(elem->path);
978 userdiff = userdiff_find_by_name("default");
979 if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV))
980 textconv = userdiff_get_textconv(userdiff);
982 /* Read the result of merge first */
983 if (!working_tree_file)
984 result = grab_blob(elem->sha1, elem->mode, &result_size,
985 textconv, elem->path);
987 /* Used by diff-tree to read from the working tree */
991 if (lstat(elem->path, &st) < 0)
994 if (S_ISLNK(st.st_mode)) {
995 struct strbuf buf = STRBUF_INIT;
997 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
998 error("readlink(%s): %s", elem->path,
1002 result_size = buf.len;
1003 result = strbuf_detach(&buf, NULL);
1004 elem->mode = canon_mode(st.st_mode);
1005 } else if (S_ISDIR(st.st_mode)) {
1006 unsigned char sha1[20];
1007 if (resolve_gitlink_ref(elem->path, "HEAD", sha1) < 0)
1008 result = grab_blob(elem->sha1, elem->mode,
1009 &result_size, NULL, NULL);
1011 result = grab_blob(sha1, elem->mode,
1012 &result_size, NULL, NULL);
1013 } else if (textconv) {
1014 struct diff_filespec *df = alloc_filespec(elem->path);
1015 fill_filespec(df, null_sha1, 0, st.st_mode);
1016 result_size = fill_textconv(textconv, df, &result);
1018 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
1019 size_t len = xsize_t(st.st_size);
1023 elem->mode = canon_mode(st.st_mode);
1024 /* if symlinks don't work, assume symlink if all parents
1027 is_file = has_symlinks;
1028 for (i = 0; !is_file && i < num_parent; i++)
1029 is_file = !S_ISLNK(elem->parent[i].mode);
1031 elem->mode = canon_mode(S_IFLNK);
1034 result = xmalloc(len + 1);
1036 done = read_in_full(fd, result, len);
1038 die_errno("read error '%s'", elem->path);
1039 else if (done < len)
1040 die("early EOF '%s'", elem->path);
1044 /* If not a fake symlink, apply filters, e.g. autocrlf */
1046 struct strbuf buf = STRBUF_INIT;
1048 if (convert_to_git(elem->path, result, len, &buf, safe_crlf)) {
1050 result = strbuf_detach(&buf, &len);
1060 result = xcalloc(1, 1);
1067 for (i = 0; i < num_parent; i++) {
1068 if (elem->parent[i].mode != elem->mode) {
1076 else if (userdiff->binary != -1)
1077 is_binary = userdiff->binary;
1079 is_binary = buffer_is_binary(result, result_size);
1080 for (i = 0; !is_binary && i < num_parent; i++) {
1083 buf = grab_blob(elem->parent[i].sha1,
1084 elem->parent[i].mode,
1086 if (buffer_is_binary(buf, size))
1092 show_combined_header(elem, num_parent, dense, rev,
1093 line_prefix, mode_differs, 0);
1094 printf("Binary files differ\n");
1099 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
1103 if (result_size && result[result_size-1] != '\n')
1104 cnt++; /* incomplete line */
1106 sline = xcalloc(cnt+2, sizeof(*sline));
1107 sline[0].bol = result;
1108 for (lno = 0, cp = result; cp < result + result_size; cp++) {
1110 sline[lno].len = cp - sline[lno].bol;
1113 sline[lno].bol = cp + 1;
1116 if (result_size && result[result_size-1] != '\n')
1117 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
1119 result_file.ptr = result;
1120 result_file.size = result_size;
1122 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1123 * for deletion hunk at the end.
1125 sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
1126 for (lno = 0; lno <= cnt; lno++)
1127 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
1129 for (i = 0; i < num_parent; i++) {
1131 for (j = 0; j < i; j++) {
1132 if (!hashcmp(elem->parent[i].sha1,
1133 elem->parent[j].sha1)) {
1134 reuse_combine_diff(sline, cnt, i, j);
1139 combine_diff(elem->parent[i].sha1,
1140 elem->parent[i].mode,
1141 &result_file, sline,
1142 cnt, i, num_parent, result_deleted,
1143 textconv, elem->path, opt->xdl_opts);
1146 show_hunks = make_hunks(sline, cnt, num_parent, dense);
1148 if (show_hunks || mode_differs || working_tree_file) {
1149 show_combined_header(elem, num_parent, dense, rev,
1150 line_prefix, mode_differs, 1);
1151 dump_sline(sline, line_prefix, cnt, num_parent,
1152 opt->use_color, result_deleted);
1156 for (lno = 0; lno < cnt; lno++) {
1157 if (sline[lno].lost) {
1158 struct lline *ll = sline[lno].lost;
1160 struct lline *tmp = ll;
1166 free(sline[0].p_lno);
1170 static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
1172 struct diff_options *opt = &rev->diffopt;
1173 int line_termination, inter_name_termination, i;
1174 const char *line_prefix = diff_line_prefix(opt);
1176 line_termination = opt->line_termination;
1177 inter_name_termination = '\t';
1178 if (!line_termination)
1179 inter_name_termination = 0;
1181 if (rev->loginfo && !rev->no_commit_id)
1185 if (opt->output_format & DIFF_FORMAT_RAW) {
1186 printf("%s", line_prefix);
1188 /* As many colons as there are parents */
1189 for (i = 0; i < num_parent; i++)
1192 /* Show the modes */
1193 for (i = 0; i < num_parent; i++)
1194 printf("%06o ", p->parent[i].mode);
1195 printf("%06o", p->mode);
1198 for (i = 0; i < num_parent; i++)
1199 printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
1201 printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
1204 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
1205 for (i = 0; i < num_parent; i++)
1206 putchar(p->parent[i].status);
1207 putchar(inter_name_termination);
1210 write_name_quoted(p->path, stdout, line_termination);
1214 * The result (p->elem) is from the working tree and their
1215 * parents are typically from multiple stages during a merge
1216 * (i.e. diff-files) or the state in HEAD and in the index
1217 * (i.e. diff-index).
1219 void show_combined_diff(struct combine_diff_path *p,
1222 struct rev_info *rev)
1224 struct diff_options *opt = &rev->diffopt;
1226 if (opt->output_format & (DIFF_FORMAT_RAW |
1228 DIFF_FORMAT_NAME_STATUS))
1229 show_raw_diff(p, num_parent, rev);
1230 else if (opt->output_format & DIFF_FORMAT_PATCH)
1231 show_patch_diff(p, num_parent, dense, 1, rev);
1234 static void free_combined_pair(struct diff_filepair *pair)
1241 * A combine_diff_path expresses N parents on the LHS against 1 merge
1242 * result. Synthesize a diff_filepair that has N entries on the "one"
1243 * side and 1 entry on the "two" side.
1245 * In the future, we might want to add more data to combine_diff_path
1246 * so that we can fill fields we are ignoring (most notably, size) here,
1247 * but currently nobody uses it, so this should suffice for now.
1249 static struct diff_filepair *combined_pair(struct combine_diff_path *p,
1253 struct diff_filepair *pair;
1254 struct diff_filespec *pool;
1256 pair = xmalloc(sizeof(*pair));
1257 pool = xcalloc(num_parent + 1, sizeof(struct diff_filespec));
1258 pair->one = pool + 1;
1261 for (i = 0; i < num_parent; i++) {
1262 pair->one[i].path = p->path;
1263 pair->one[i].mode = p->parent[i].mode;
1264 hashcpy(pair->one[i].sha1, p->parent[i].sha1);
1265 pair->one[i].sha1_valid = !is_null_sha1(p->parent[i].sha1);
1266 pair->one[i].has_more_entries = 1;
1268 pair->one[num_parent - 1].has_more_entries = 0;
1270 pair->two->path = p->path;
1271 pair->two->mode = p->mode;
1272 hashcpy(pair->two->sha1, p->sha1);
1273 pair->two->sha1_valid = !is_null_sha1(p->sha1);
1277 static void handle_combined_callback(struct diff_options *opt,
1278 struct combine_diff_path *paths,
1282 struct combine_diff_path *p;
1283 struct diff_queue_struct q;
1286 q.queue = xcalloc(num_paths, sizeof(struct diff_filepair *));
1287 q.alloc = num_paths;
1289 for (i = 0, p = paths; p; p = p->next)
1290 q.queue[i++] = combined_pair(p, num_parent);
1291 opt->format_callback(&q, opt, opt->format_callback_data);
1292 for (i = 0; i < num_paths; i++)
1293 free_combined_pair(q.queue[i]);
1297 static const char *path_path(void *obj)
1299 struct combine_diff_path *path = (struct combine_diff_path *)obj;
1304 void diff_tree_combined(const unsigned char *sha1,
1305 const struct sha1_array *parents,
1307 struct rev_info *rev)
1309 struct diff_options *opt = &rev->diffopt;
1310 struct diff_options diffopts;
1311 struct combine_diff_path *p, *paths = NULL;
1312 int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
1315 copy_pathspec(&diffopts.pathspec, &opt->pathspec);
1316 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
1317 DIFF_OPT_SET(&diffopts, RECURSIVE);
1318 DIFF_OPT_CLR(&diffopts, ALLOW_EXTERNAL);
1319 /* tell diff_tree to emit paths in sorted (=tree) order */
1320 diffopts.orderfile = NULL;
1322 show_log_first = !!rev->loginfo && !rev->no_commit_id;
1324 /* find set of paths that everybody touches */
1325 for (i = 0; i < num_parent; i++) {
1326 /* show stat against the first parent even
1327 * when doing combined diff.
1329 int stat_opt = (opt->output_format &
1330 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
1331 if (i == 0 && stat_opt)
1332 diffopts.output_format = stat_opt;
1334 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
1335 diff_tree_sha1(parents->sha1[i], sha1, "", &diffopts);
1336 diffcore_std(&diffopts);
1337 paths = intersect_paths(paths, i, num_parent);
1339 if (show_log_first && i == 0) {
1342 if (rev->verbose_header && opt->output_format)
1343 printf("%s%c", diff_line_prefix(opt),
1344 opt->line_termination);
1347 /* if showing diff, show it in requested order */
1348 if (diffopts.output_format != DIFF_FORMAT_NO_OUTPUT &&
1350 diffcore_order(opt->orderfile);
1353 diff_flush(&diffopts);
1356 /* find out number of surviving paths */
1357 for (num_paths = 0, p = paths; p; p = p->next)
1360 /* order paths according to diffcore_order */
1361 if (opt->orderfile && num_paths) {
1362 struct obj_order *o;
1364 o = xmalloc(sizeof(*o) * num_paths);
1365 for (i = 0, p = paths; p; p = p->next, i++)
1367 order_objects(opt->orderfile, path_path, o, num_paths);
1368 for (i = 0; i < num_paths - 1; i++) {
1370 p->next = o[i+1].obj;
1373 p = o[num_paths-1].obj;
1381 if (opt->output_format & (DIFF_FORMAT_RAW |
1383 DIFF_FORMAT_NAME_STATUS)) {
1384 for (p = paths; p; p = p->next)
1385 show_raw_diff(p, num_parent, rev);
1388 else if (opt->output_format &
1389 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
1391 else if (opt->output_format & DIFF_FORMAT_CALLBACK)
1392 handle_combined_callback(opt, paths, num_parent, num_paths);
1394 if (opt->output_format & DIFF_FORMAT_PATCH) {
1396 printf("%s%c", diff_line_prefix(opt),
1397 opt->line_termination);
1398 for (p = paths; p; p = p->next)
1399 show_patch_diff(p, num_parent, dense,
1404 /* Clean things up */
1406 struct combine_diff_path *tmp = paths;
1407 paths = paths->next;
1411 free_pathspec(&diffopts.pathspec);
1414 void diff_tree_combined_merge(const struct commit *commit, int dense,
1415 struct rev_info *rev)
1417 struct commit_list *parent = get_saved_parents(rev, commit);
1418 struct sha1_array parents = SHA1_ARRAY_INIT;
1421 sha1_array_append(&parents, parent->item->object.sha1);
1422 parent = parent->next;
1424 diff_tree_combined(commit->object.sha1, &parents, dense, rev);
1425 sha1_array_clear(&parents);