2 #include "object-store.h"
8 #include "xdiff-interface.h"
9 #include "xdiff/xmacros.h"
13 #include "sha1-array.h"
16 static int compare_paths(const struct combine_diff_path *one,
17 const struct diff_filespec *two)
19 if (!S_ISDIR(one->mode) && !S_ISDIR(two->mode))
20 return strcmp(one->path, two->path);
22 return base_name_compare(one->path, strlen(one->path), one->mode,
23 two->path, strlen(two->path), two->mode);
26 static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
28 struct diff_queue_struct *q = &diff_queued_diff;
29 struct combine_diff_path *p, **tail = &curr;
33 for (i = 0; i < q->nr; i++) {
36 if (diff_unmodified_pair(q->queue[i]))
38 path = q->queue[i]->two->path;
40 p = xmalloc(combine_diff_path_size(num_parent, len));
41 p->path = (char *) &(p->parent[num_parent]);
42 memcpy(p->path, path, len);
46 sizeof(p->parent[0]) * num_parent);
48 oidcpy(&p->oid, &q->queue[i]->two->oid);
49 p->mode = q->queue[i]->two->mode;
50 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
51 p->parent[n].mode = q->queue[i]->one->mode;
52 p->parent[n].status = q->queue[i]->status;
60 * paths in curr (linked list) and q->queue[] (array) are
61 * both sorted in the tree order.
64 while ((p = *tail) != NULL) {
66 ? -1 : compare_paths(p, q->queue[i]->two));
69 /* p->path not in q->queue[]; drop it */
76 /* q->queue[i] not in p->path; skip it */
81 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
82 p->parent[n].mode = q->queue[i]->one->mode;
83 p->parent[n].status = q->queue[i]->status;
91 /* Lines lost from parent */
93 struct lline *next, *prev;
95 unsigned long parent_map;
96 char line[FLEX_ARRAY];
99 /* Lines lost from current parent (before coalescing) */
101 struct lline *lost_head, *lost_tail;
105 /* Lines surviving in the merge result */
107 /* Accumulated and coalesced lost lines */
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".
119 unsigned long *p_lno;
122 static int match_string_spaces(const char *line1, int len1,
123 const char *line2, int len2,
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--);
131 if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
132 return (len1 == len2 && !memcmp(line1, line2, len1));
134 while (len1 > 0 && len2 > 0) {
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])))
142 for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
143 for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
145 if (line1[len1] != line2[len2])
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--);
155 /* We matched full line1 and line2 */
162 enum coalesce_direction { MATCH, BASE, NEW };
164 /* Coalesce new lines into base by finding LCS */
165 static struct lline *coalesce_lines(struct lline *base, int *lenbase,
166 struct lline *newline, int lennew,
167 unsigned long parent, long flags)
170 enum coalesce_direction **directions;
171 struct lline *baseend, *newend = NULL;
172 int i, j, origbaselen = *lenbase;
183 * Coalesce new lines into base by finding the LCS
184 * - Create the table to run dynamic programming
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
193 lcs = xcalloc(st_add(origbaselen, 1), sizeof(int*));
194 directions = xcalloc(st_add(origbaselen, 1), sizeof(enum coalesce_direction*));
195 for (i = 0; i < origbaselen + 1; i++) {
196 lcs[i] = xcalloc(st_add(lennew, 1), sizeof(int));
197 directions[i] = xcalloc(st_add(lennew, 1), sizeof(enum coalesce_direction));
198 directions[i][0] = BASE;
200 for (j = 1; j < lennew + 1; j++)
201 directions[0][j] = NEW;
203 for (i = 1, baseend = base; i < origbaselen + 1; i++) {
204 for (j = 1, newend = newline; 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;
213 lcs[i][j] = lcs[i - 1][j];
214 directions[i][j] = BASE;
217 newend = newend->next;
220 baseend = baseend->next;
223 for (i = 0; i < origbaselen + 1; i++)
227 /* At this point, baseend and newend point to the end of each lists */
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;
237 } else if (directions[i][j] == NEW) {
241 /* Remove lline from new list and update newend */
243 lline->prev->next = lline->next;
245 newline = lline->next;
247 lline->next->prev = lline->prev;
249 newend = lline->prev;
252 /* Add lline to base list */
254 lline->next = baseend->next;
255 lline->prev = baseend;
257 lline->prev->next = lline;
266 lline->next->prev = lline;
269 baseend = baseend->prev;
276 struct lline *lline = newend;
277 newend = newend->next;
281 for (i = 0; i < origbaselen + 1; i++)
288 static char *grab_blob(struct repository *r,
289 const struct object_id *oid, unsigned int mode,
290 unsigned long *size, struct userdiff_driver *textconv,
294 enum object_type type;
296 if (S_ISGITLINK(mode)) {
297 struct strbuf buf = STRBUF_INIT;
298 strbuf_addf(&buf, "Subproject commit %s\n", oid_to_hex(oid));
300 blob = strbuf_detach(&buf, NULL);
301 } else if (is_null_oid(oid)) {
304 return xcalloc(1, 1);
305 } else if (textconv) {
306 struct diff_filespec *df = alloc_filespec(path);
307 fill_filespec(df, oid, 1, mode);
308 *size = fill_textconv(r, textconv, df, &blob);
311 blob = read_object_file(oid, &type, size);
312 if (type != OBJ_BLOB)
313 die("object '%s' is not a blob!", oid_to_hex(oid));
318 static void append_lost(struct sline *sline, int n, const char *line, int len)
321 unsigned long this_mask = (1UL<<n);
322 if (line[len-1] == '\n')
325 FLEX_ALLOC_MEM(lline, line, line, len);
328 lline->prev = sline->plost.lost_tail;
330 lline->prev->next = lline;
332 sline->plost.lost_head = lline;
333 sline->plost.lost_tail = lline;
335 lline->parent_map = this_mask;
338 struct combine_diff_state {
345 struct sline *lost_bucket;
348 static void consume_hunk(void *state_,
351 const char *funcline, long funclen)
353 struct combine_diff_state *state = state_;
359 state->lno = state->nb;
360 if (state->nn == 0) {
361 /* @@ -X,Y +N,0 @@ removed Y lines
362 * that would have come *after* line N
363 * in the result. Our lost buckets hang
364 * to the line after the removed lines,
366 * Note that this is correct even when N == 0,
367 * in which case the hunk removes the first
370 state->lost_bucket = &state->sline[state->nb];
374 state->lost_bucket = &state->sline[state->nb-1];
376 if (!state->sline[state->nb-1].p_lno)
377 state->sline[state->nb-1].p_lno =
378 xcalloc(state->num_parent, sizeof(unsigned long));
379 state->sline[state->nb-1].p_lno[state->n] = state->ob;
382 static void consume_line(void *state_, char *line, unsigned long len)
384 struct combine_diff_state *state = state_;
385 if (!state->lost_bucket)
386 return; /* not in any hunk yet */
389 append_lost(state->lost_bucket, state->n, line+1, len-1);
392 state->sline[state->lno-1].flag |= state->nmask;
398 static void combine_diff(struct repository *r,
399 const struct object_id *parent, unsigned int mode,
400 mmfile_t *result_file,
401 struct sline *sline, unsigned int cnt, int n,
402 int num_parent, int result_deleted,
403 struct userdiff_driver *textconv,
404 const char *path, long flags)
406 unsigned int p_lno, lno;
407 unsigned long nmask = (1UL << n);
410 mmfile_t parent_file;
411 struct combine_diff_state state;
415 return; /* result deleted */
417 parent_file.ptr = grab_blob(r, parent, mode, &sz, textconv, path);
418 parent_file.size = sz;
419 memset(&xpp, 0, sizeof(xpp));
421 memset(&xecfg, 0, sizeof(xecfg));
422 memset(&state, 0, sizeof(state));
426 state.num_parent = num_parent;
429 if (xdi_diff_outf(&parent_file, result_file, consume_hunk,
430 consume_line, &state, &xpp, &xecfg))
431 die("unable to generate combined diff for %s",
433 free(parent_file.ptr);
435 /* Assign line numbers for this parent.
437 * sline[lno].p_lno[n] records the first line number
438 * (counting from 1) for parent N if the final hunk display
439 * started by showing sline[lno] (possibly showing the lost
440 * lines attached to it first).
442 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
444 sline[lno].p_lno[n] = p_lno;
446 /* Coalesce new lines */
447 if (sline[lno].plost.lost_head) {
448 struct sline *sl = &sline[lno];
449 sl->lost = coalesce_lines(sl->lost, &sl->lenlost,
451 sl->plost.len, n, flags);
452 sl->plost.lost_head = sl->plost.lost_tail = NULL;
456 /* How many lines would this sline advance the p_lno? */
457 ll = sline[lno].lost;
459 if (ll->parent_map & nmask)
460 p_lno++; /* '-' means parent had it */
463 if (lno < cnt && !(sline[lno].flag & nmask))
464 p_lno++; /* no '+' means parent had it */
466 sline[lno].p_lno[n] = p_lno; /* trailer */
469 static unsigned long context = 3;
470 static char combine_marker = '@';
472 static int interesting(struct sline *sline, unsigned long all_mask)
474 /* If some parents lost lines here, or if we have added to
475 * some parent, it is interesting.
477 return ((sline->flag & all_mask) || sline->lost);
480 static unsigned long adjust_hunk_tail(struct sline *sline,
481 unsigned long all_mask,
482 unsigned long hunk_begin,
485 /* i points at the first uninteresting line. If the last line
486 * of the hunk was interesting only because it has some
487 * deletion, then it is not all that interesting for the
488 * purpose of giving trailing context lines. This is because
489 * we output '-' line and then unmodified sline[i-1] itself in
490 * that case which gives us one extra context line.
492 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
497 static unsigned long find_next(struct sline *sline,
501 int look_for_uninteresting)
503 /* We have examined up to i-1 and are about to look at i.
504 * Find next interesting or uninteresting line. Here,
505 * "interesting" does not mean interesting(), but marked by
506 * the give_context() function below (i.e. it includes context
507 * lines that are not interesting to interesting() function
508 * that are surrounded by interesting() ones.
511 if (look_for_uninteresting
512 ? !(sline[i].flag & mark)
513 : (sline[i].flag & mark))
520 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
522 unsigned long all_mask = (1UL<<num_parent) - 1;
523 unsigned long mark = (1UL<<num_parent);
524 unsigned long no_pre_delete = (2UL<<num_parent);
527 /* Two groups of interesting lines may have a short gap of
528 * uninteresting lines. Connect such groups to give them a
531 * We first start from what the interesting() function says,
532 * and mark them with "mark", and paint context lines with the
533 * mark. So interesting() would still say false for such context
534 * lines but they are treated as "interesting" in the end.
536 i = find_next(sline, mark, 0, cnt, 0);
541 unsigned long j = (context < i) ? (i - context) : 0;
544 /* Paint a few lines before the first interesting line. */
546 if (!(sline[j].flag & mark))
547 sline[j].flag |= no_pre_delete;
548 sline[j++].flag |= mark;
552 /* we know up to i is to be included. where does the
553 * next uninteresting one start?
555 j = find_next(sline, mark, i, cnt, 1);
557 break; /* the rest are all interesting */
559 /* lookahead context lines */
560 k = find_next(sline, mark, j, cnt, 0);
561 j = adjust_hunk_tail(sline, all_mask, i, j);
563 if (k < j + context) {
564 /* k is interesting and [j,k) are not, but
565 * paint them interesting because the gap is small.
568 sline[j++].flag |= mark;
573 /* j is the first uninteresting line and there is
574 * no overlap beyond it within context lines. Paint
575 * the trailing edge a bit.
578 k = (j + context < cnt+1) ? j + context : cnt+1;
580 sline[j++].flag |= mark;
585 static int make_hunks(struct sline *sline, unsigned long cnt,
586 int num_parent, int dense)
588 unsigned long all_mask = (1UL<<num_parent) - 1;
589 unsigned long mark = (1UL<<num_parent);
591 int has_interesting = 0;
593 for (i = 0; i <= cnt; i++) {
594 if (interesting(&sline[i], all_mask))
595 sline[i].flag |= mark;
597 sline[i].flag &= ~mark;
600 return give_context(sline, cnt, num_parent);
602 /* Look at each hunk, and if we have changes from only one
603 * parent, or the changes are the same from all but one
604 * parent, mark that uninteresting.
608 unsigned long j, hunk_begin, hunk_end;
609 unsigned long same_diff;
610 while (i <= cnt && !(sline[i].flag & mark))
613 break; /* No more interesting hunks */
615 for (j = i + 1; j <= cnt; j++) {
616 if (!(sline[j].flag & mark)) {
617 /* Look beyond the end to see if there
618 * is an interesting line after this
619 * hunk within context span.
621 unsigned long la; /* lookahead */
623 la = adjust_hunk_tail(sline, all_mask,
625 la = (la + context < cnt + 1) ?
626 (la + context) : cnt + 1;
627 while (la && j <= --la) {
628 if (sline[la].flag & mark) {
640 /* [i..hunk_end) are interesting. Now is it really
641 * interesting? We check if there are only two versions
642 * and the result matches one of them. That is, we look
644 * (+) line, which records lines added to which parents;
645 * this line appears in the result.
646 * (-) line, which records from what parents the line
647 * was removed; this line does not appear in the result.
648 * then check the set of parents the result has difference
649 * from, from all lines. If there are lines that has
650 * different set of parents that the result has differences
651 * from, that means we have more than two versions.
653 * Even when we have only two versions, if the result does
654 * not match any of the parents, the it should be considered
655 * interesting. In such a case, we would have all '+' line.
656 * After passing the above "two versions" test, that would
657 * appear as "the same set of parents" to be "all parents".
661 for (j = i; j < hunk_end && !has_interesting; j++) {
662 unsigned long this_diff = sline[j].flag & all_mask;
663 struct lline *ll = sline[j].lost;
665 /* This has some changes. Is it the
669 same_diff = this_diff;
670 else if (same_diff != this_diff) {
675 while (ll && !has_interesting) {
676 /* Lost this line from these parents;
677 * who are they? Are they the same?
679 this_diff = ll->parent_map;
681 same_diff = this_diff;
682 else if (same_diff != this_diff) {
689 if (!has_interesting && same_diff != all_mask) {
690 /* This hunk is not that interesting after all */
691 for (j = hunk_begin; j < hunk_end; j++)
692 sline[j].flag &= ~mark;
697 has_interesting = give_context(sline, cnt, num_parent);
698 return has_interesting;
701 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
703 l0 = sline[l0].p_lno[n];
704 l1 = sline[l1].p_lno[n];
705 printf(" -%lu,%lu", l0, l1-l0-null_context);
708 static int hunk_comment_line(const char *bol)
715 return (isalpha(ch) || ch == '_' || ch == '$');
718 static void show_line_to_eol(const char *line, int len, const char *reset)
720 int saw_cr_at_eol = 0;
723 saw_cr_at_eol = (len && line[len-1] == '\r');
725 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
727 saw_cr_at_eol ? "\r" : "");
730 static void dump_sline(struct sline *sline, const char *line_prefix,
731 unsigned long cnt, int num_parent,
732 int use_color, int result_deleted)
734 unsigned long mark = (1UL<<num_parent);
735 unsigned long no_pre_delete = (2UL<<num_parent);
737 unsigned long lno = 0;
738 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
739 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
740 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
741 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
742 const char *c_context = diff_get_color(use_color, DIFF_CONTEXT);
743 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
746 return; /* result deleted */
749 unsigned long hunk_end;
750 unsigned long rlines;
751 const char *hunk_comment = NULL;
752 unsigned long null_context = 0;
754 while (lno <= cnt && !(sline[lno].flag & mark)) {
755 if (hunk_comment_line(sline[lno].bol))
756 hunk_comment = sline[lno].bol;
762 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
763 if (!(sline[hunk_end].flag & mark))
766 rlines = hunk_end - lno;
768 rlines--; /* pointing at the last delete hunk */
772 * Even when running with --unified=0, all
773 * lines in the hunk needs to be processed in
774 * the loop below in order to show the
775 * deletion recorded in lost_head. However,
776 * we do not want to show the resulting line
777 * with all blank context markers in such a
781 for (j = lno; j < hunk_end; j++)
782 if (!(sline[j].flag & (mark-1)))
784 rlines -= null_context;
787 printf("%s%s", line_prefix, c_frag);
788 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
789 for (i = 0; i < num_parent; i++)
790 show_parent_lno(sline, lno, hunk_end, i, null_context);
791 printf(" +%lu,%lu ", lno+1, rlines);
792 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
796 for (i = 0; i < 40; i++) {
797 int ch = hunk_comment[i] & 0xff;
798 if (!ch || ch == '\n')
804 printf("%s%s %s%s", c_reset,
807 for (i = 0; i < comment_end; i++)
808 putchar(hunk_comment[i]);
811 printf("%s\n", c_reset);
812 while (lno < hunk_end) {
815 unsigned long p_mask;
816 struct sline *sl = &sline[lno++];
817 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost;
819 printf("%s%s", line_prefix, c_old);
820 for (j = 0; j < num_parent; j++) {
821 if (ll->parent_map & (1UL<<j))
826 show_line_to_eol(ll->line, -1, c_reset);
832 fputs(line_prefix, stdout);
833 if (!(sl->flag & (mark-1))) {
835 * This sline was here to hang the
836 * lost lines in front of it.
840 fputs(c_context, stdout);
843 fputs(c_new, stdout);
844 for (j = 0; j < num_parent; j++) {
845 if (p_mask & sl->flag)
851 show_line_to_eol(sl->bol, sl->len, c_reset);
856 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
859 /* We have already examined parent j and we know parent i
860 * and parent j are the same, so reuse the combined result
861 * of parent j for parent i.
863 unsigned long lno, imask, jmask;
867 for (lno = 0; lno <= cnt; lno++) {
868 struct lline *ll = sline->lost;
869 sline->p_lno[i] = sline->p_lno[j];
871 if (ll->parent_map & jmask)
872 ll->parent_map |= imask;
875 if (sline->flag & jmask)
876 sline->flag |= imask;
879 /* the overall size of the file (sline[cnt]) */
880 sline->p_lno[i] = sline->p_lno[j];
883 static void dump_quoted_path(const char *head,
886 const char *line_prefix,
887 const char *c_meta, const char *c_reset)
889 static struct strbuf buf = STRBUF_INIT;
892 strbuf_addstr(&buf, line_prefix);
893 strbuf_addstr(&buf, c_meta);
894 strbuf_addstr(&buf, head);
895 quote_two_c_style(&buf, prefix, path, 0);
896 strbuf_addstr(&buf, c_reset);
900 static void show_combined_header(struct combine_diff_path *elem,
903 struct rev_info *rev,
904 const char *line_prefix,
906 int show_file_header)
908 struct diff_options *opt = &rev->diffopt;
909 int abbrev = opt->flags.full_index ? GIT_SHA1_HEXSZ : DEFAULT_ABBREV;
910 const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
911 const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
912 const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
913 const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
919 if (rev->loginfo && !rev->no_commit_id)
922 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
923 "", elem->path, line_prefix, c_meta, c_reset);
924 printf("%s%sindex ", line_prefix, c_meta);
925 for (i = 0; i < num_parent; i++) {
926 abb = find_unique_abbrev(&elem->parent[i].oid,
928 printf("%s%s", i ? "," : "", abb);
930 abb = find_unique_abbrev(&elem->oid, abbrev);
931 printf("..%s%s\n", abb, c_reset);
934 deleted = !elem->mode;
936 /* We say it was added if nobody had it */
938 for (i = 0; added && i < num_parent; i++)
939 if (elem->parent[i].status !=
943 printf("%s%snew file mode %06o",
944 line_prefix, c_meta, elem->mode);
947 printf("%s%sdeleted file ",
948 line_prefix, c_meta);
950 for (i = 0; i < num_parent; i++) {
951 printf("%s%06o", i ? "," : "",
952 elem->parent[i].mode);
955 printf("..%06o", elem->mode);
957 printf("%s\n", c_reset);
960 if (!show_file_header)
964 dump_quoted_path("--- ", "", "/dev/null",
965 line_prefix, c_meta, c_reset);
967 dump_quoted_path("--- ", a_prefix, elem->path,
968 line_prefix, c_meta, c_reset);
970 dump_quoted_path("+++ ", "", "/dev/null",
971 line_prefix, c_meta, c_reset);
973 dump_quoted_path("+++ ", b_prefix, elem->path,
974 line_prefix, c_meta, c_reset);
977 static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
978 int dense, int working_tree_file,
979 struct rev_info *rev)
981 struct diff_options *opt = &rev->diffopt;
982 unsigned long result_size, cnt, lno;
983 int result_deleted = 0;
985 struct sline *sline; /* survived lines */
986 int mode_differs = 0;
988 mmfile_t result_file;
989 struct userdiff_driver *userdiff;
990 struct userdiff_driver *textconv = NULL;
992 const char *line_prefix = diff_line_prefix(opt);
994 context = opt->context;
995 userdiff = userdiff_find_by_path(opt->repo->index, elem->path);
997 userdiff = userdiff_find_by_name("default");
998 if (opt->flags.allow_textconv)
999 textconv = userdiff_get_textconv(userdiff);
1001 /* Read the result of merge first */
1002 if (!working_tree_file)
1003 result = grab_blob(opt->repo, &elem->oid, elem->mode, &result_size,
1004 textconv, elem->path);
1006 /* Used by diff-tree to read from the working tree */
1010 if (lstat(elem->path, &st) < 0)
1013 if (S_ISLNK(st.st_mode)) {
1014 struct strbuf buf = STRBUF_INIT;
1016 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
1017 error_errno("readlink(%s)", elem->path);
1020 result_size = buf.len;
1021 result = strbuf_detach(&buf, NULL);
1022 elem->mode = canon_mode(st.st_mode);
1023 } else if (S_ISDIR(st.st_mode)) {
1024 struct object_id oid;
1025 if (resolve_gitlink_ref(elem->path, "HEAD", &oid) < 0)
1026 result = grab_blob(opt->repo, &elem->oid,
1027 elem->mode, &result_size,
1030 result = grab_blob(opt->repo, &oid, elem->mode,
1031 &result_size, NULL, NULL);
1032 } else if (textconv) {
1033 struct diff_filespec *df = alloc_filespec(elem->path);
1034 fill_filespec(df, &null_oid, 0, st.st_mode);
1035 result_size = fill_textconv(opt->repo, textconv, df, &result);
1037 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
1038 size_t len = xsize_t(st.st_size);
1042 elem->mode = canon_mode(st.st_mode);
1043 /* if symlinks don't work, assume symlink if all parents
1046 is_file = has_symlinks;
1047 for (i = 0; !is_file && i < num_parent; i++)
1048 is_file = !S_ISLNK(elem->parent[i].mode);
1050 elem->mode = canon_mode(S_IFLNK);
1053 result = xmallocz(len);
1055 done = read_in_full(fd, result, len);
1057 die_errno("read error '%s'", elem->path);
1058 else if (done < len)
1059 die("early EOF '%s'", elem->path);
1061 /* If not a fake symlink, apply filters, e.g. autocrlf */
1063 struct strbuf buf = STRBUF_INIT;
1065 if (convert_to_git(rev->diffopt.repo->index,
1066 elem->path, result, len, &buf, global_conv_flags_eol)) {
1068 result = strbuf_detach(&buf, &len);
1078 result = xcalloc(1, 1);
1085 for (i = 0; i < num_parent; i++) {
1086 if (elem->parent[i].mode != elem->mode) {
1094 else if (userdiff->binary != -1)
1095 is_binary = userdiff->binary;
1097 is_binary = buffer_is_binary(result, result_size);
1098 for (i = 0; !is_binary && i < num_parent; i++) {
1101 buf = grab_blob(opt->repo,
1102 &elem->parent[i].oid,
1103 elem->parent[i].mode,
1105 if (buffer_is_binary(buf, size))
1111 show_combined_header(elem, num_parent, dense, rev,
1112 line_prefix, mode_differs, 0);
1113 printf("Binary files differ\n");
1118 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
1122 if (result_size && result[result_size-1] != '\n')
1123 cnt++; /* incomplete line */
1125 sline = xcalloc(st_add(cnt, 2), sizeof(*sline));
1126 sline[0].bol = result;
1127 for (lno = 0, cp = result; cp < result + result_size; cp++) {
1129 sline[lno].len = cp - sline[lno].bol;
1132 sline[lno].bol = cp + 1;
1135 if (result_size && result[result_size-1] != '\n')
1136 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
1138 result_file.ptr = result;
1139 result_file.size = result_size;
1141 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1142 * for deletion hunk at the end.
1144 sline[0].p_lno = xcalloc(st_mult(st_add(cnt, 2), num_parent), sizeof(unsigned long));
1145 for (lno = 0; lno <= cnt; lno++)
1146 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
1148 for (i = 0; i < num_parent; i++) {
1150 for (j = 0; j < i; j++) {
1151 if (oideq(&elem->parent[i].oid,
1152 &elem->parent[j].oid)) {
1153 reuse_combine_diff(sline, cnt, i, j);
1158 combine_diff(opt->repo,
1159 &elem->parent[i].oid,
1160 elem->parent[i].mode,
1161 &result_file, sline,
1162 cnt, i, num_parent, result_deleted,
1163 textconv, elem->path, opt->xdl_opts);
1166 show_hunks = make_hunks(sline, cnt, num_parent, dense);
1168 if (show_hunks || mode_differs || working_tree_file) {
1169 show_combined_header(elem, num_parent, dense, rev,
1170 line_prefix, mode_differs, 1);
1171 dump_sline(sline, line_prefix, cnt, num_parent,
1172 opt->use_color, result_deleted);
1176 for (lno = 0; lno < cnt; lno++) {
1177 if (sline[lno].lost) {
1178 struct lline *ll = sline[lno].lost;
1180 struct lline *tmp = ll;
1186 free(sline[0].p_lno);
1190 static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
1192 struct diff_options *opt = &rev->diffopt;
1193 int line_termination, inter_name_termination, i;
1194 const char *line_prefix = diff_line_prefix(opt);
1196 line_termination = opt->line_termination;
1197 inter_name_termination = '\t';
1198 if (!line_termination)
1199 inter_name_termination = 0;
1201 if (rev->loginfo && !rev->no_commit_id)
1205 if (opt->output_format & DIFF_FORMAT_RAW) {
1206 printf("%s", line_prefix);
1208 /* As many colons as there are parents */
1209 for (i = 0; i < num_parent; i++)
1212 /* Show the modes */
1213 for (i = 0; i < num_parent; i++)
1214 printf("%06o ", p->parent[i].mode);
1215 printf("%06o", p->mode);
1218 for (i = 0; i < num_parent; i++)
1219 printf(" %s", diff_aligned_abbrev(&p->parent[i].oid,
1221 printf(" %s ", diff_aligned_abbrev(&p->oid, opt->abbrev));
1224 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
1225 for (i = 0; i < num_parent; i++)
1226 putchar(p->parent[i].status);
1227 putchar(inter_name_termination);
1230 write_name_quoted(p->path, stdout, line_termination);
1234 * The result (p->elem) is from the working tree and their
1235 * parents are typically from multiple stages during a merge
1236 * (i.e. diff-files) or the state in HEAD and in the index
1237 * (i.e. diff-index).
1239 void show_combined_diff(struct combine_diff_path *p,
1242 struct rev_info *rev)
1244 struct diff_options *opt = &rev->diffopt;
1246 if (opt->output_format & (DIFF_FORMAT_RAW |
1248 DIFF_FORMAT_NAME_STATUS))
1249 show_raw_diff(p, num_parent, rev);
1250 else if (opt->output_format & DIFF_FORMAT_PATCH)
1251 show_patch_diff(p, num_parent, dense, 1, rev);
1254 static void free_combined_pair(struct diff_filepair *pair)
1261 * A combine_diff_path expresses N parents on the LHS against 1 merge
1262 * result. Synthesize a diff_filepair that has N entries on the "one"
1263 * side and 1 entry on the "two" side.
1265 * In the future, we might want to add more data to combine_diff_path
1266 * so that we can fill fields we are ignoring (most notably, size) here,
1267 * but currently nobody uses it, so this should suffice for now.
1269 static struct diff_filepair *combined_pair(struct combine_diff_path *p,
1273 struct diff_filepair *pair;
1274 struct diff_filespec *pool;
1276 pair = xmalloc(sizeof(*pair));
1277 pool = xcalloc(st_add(num_parent, 1), sizeof(struct diff_filespec));
1278 pair->one = pool + 1;
1281 for (i = 0; i < num_parent; i++) {
1282 pair->one[i].path = p->path;
1283 pair->one[i].mode = p->parent[i].mode;
1284 oidcpy(&pair->one[i].oid, &p->parent[i].oid);
1285 pair->one[i].oid_valid = !is_null_oid(&p->parent[i].oid);
1286 pair->one[i].has_more_entries = 1;
1288 pair->one[num_parent - 1].has_more_entries = 0;
1290 pair->two->path = p->path;
1291 pair->two->mode = p->mode;
1292 oidcpy(&pair->two->oid, &p->oid);
1293 pair->two->oid_valid = !is_null_oid(&p->oid);
1297 static void handle_combined_callback(struct diff_options *opt,
1298 struct combine_diff_path *paths,
1302 struct combine_diff_path *p;
1303 struct diff_queue_struct q;
1306 q.queue = xcalloc(num_paths, sizeof(struct diff_filepair *));
1307 q.alloc = num_paths;
1309 for (i = 0, p = paths; p; p = p->next)
1310 q.queue[i++] = combined_pair(p, num_parent);
1311 opt->format_callback(&q, opt, opt->format_callback_data);
1312 for (i = 0; i < num_paths; i++)
1313 free_combined_pair(q.queue[i]);
1317 static const char *path_path(void *obj)
1319 struct combine_diff_path *path = (struct combine_diff_path *)obj;
1325 /* find set of paths that every parent touches */
1326 static struct combine_diff_path *find_paths_generic(const struct object_id *oid,
1327 const struct oid_array *parents, struct diff_options *opt)
1329 struct combine_diff_path *paths = NULL;
1330 int i, num_parent = parents->nr;
1332 int output_format = opt->output_format;
1333 const char *orderfile = opt->orderfile;
1335 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1336 /* tell diff_tree to emit paths in sorted (=tree) order */
1337 opt->orderfile = NULL;
1339 /* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn) (wrt paths) */
1340 for (i = 0; i < num_parent; i++) {
1342 * show stat against the first parent even when doing
1345 int stat_opt = (output_format &
1346 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
1347 if (i == 0 && stat_opt)
1348 opt->output_format = stat_opt;
1350 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1351 diff_tree_oid(&parents->oid[i], oid, "", opt);
1353 paths = intersect_paths(paths, i, num_parent);
1355 /* if showing diff, show it in requested order */
1356 if (opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1358 diffcore_order(orderfile);
1364 opt->output_format = output_format;
1365 opt->orderfile = orderfile;
1371 * find set of paths that everybody touches, assuming diff is run without
1372 * rename/copy detection, etc, comparing all trees simultaneously (= faster).
1374 static struct combine_diff_path *find_paths_multitree(
1375 const struct object_id *oid, const struct oid_array *parents,
1376 struct diff_options *opt)
1378 int i, nparent = parents->nr;
1379 const struct object_id **parents_oid;
1380 struct combine_diff_path paths_head;
1383 ALLOC_ARRAY(parents_oid, nparent);
1384 for (i = 0; i < nparent; i++)
1385 parents_oid[i] = &parents->oid[i];
1387 /* fake list head, so worker can assume it is non-NULL */
1388 paths_head.next = NULL;
1390 strbuf_init(&base, PATH_MAX);
1391 diff_tree_paths(&paths_head, oid, parents_oid, nparent, &base, opt);
1393 strbuf_release(&base);
1395 return paths_head.next;
1399 void diff_tree_combined(const struct object_id *oid,
1400 const struct oid_array *parents,
1402 struct rev_info *rev)
1404 struct diff_options *opt = &rev->diffopt;
1405 struct diff_options diffopts;
1406 struct combine_diff_path *p, *paths;
1407 int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
1408 int need_generic_pathscan;
1410 /* nothing to do, if no parents */
1414 show_log_first = !!rev->loginfo && !rev->no_commit_id;
1416 if (show_log_first) {
1419 if (rev->verbose_header && opt->output_format &&
1420 opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1421 !commit_format_is_empty(rev->commit_format))
1422 printf("%s%c", diff_line_prefix(opt),
1423 opt->line_termination);
1427 copy_pathspec(&diffopts.pathspec, &opt->pathspec);
1428 diffopts.flags.recursive = 1;
1429 diffopts.flags.allow_external = 0;
1431 /* find set of paths that everybody touches
1435 * Diffcore transformations are bound to diff_filespec and logic
1436 * comparing two entries - i.e. they do not apply directly to combine
1439 * If some of such transformations is requested - we launch generic
1440 * path scanning, which works significantly slower compared to
1441 * simultaneous all-trees-in-one-go scan in find_paths_multitree().
1443 * TODO some of the filters could be ported to work on
1444 * combine_diff_paths - i.e. all functionality that skips paths, so in
1445 * theory, we could end up having only multitree path scanning.
1447 * NOTE please keep this semantically in sync with diffcore_std()
1449 need_generic_pathscan = opt->skip_stat_unmatch ||
1450 opt->flags.follow_renames ||
1451 opt->break_opt != -1 ||
1452 opt->detect_rename ||
1453 (opt->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
1457 if (need_generic_pathscan) {
1459 * NOTE generic case also handles --stat, as it computes
1460 * diff(sha1,parent_i) for all i to do the job, specifically
1463 paths = find_paths_generic(oid, parents, &diffopts);
1467 paths = find_paths_multitree(oid, parents, &diffopts);
1470 * show stat against the first parent even
1471 * when doing combined diff.
1473 stat_opt = (opt->output_format &
1474 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
1476 diffopts.output_format = stat_opt;
1478 diff_tree_oid(&parents->oid[0], oid, "", &diffopts);
1479 diffcore_std(&diffopts);
1481 diffcore_order(opt->orderfile);
1482 diff_flush(&diffopts);
1486 /* find out number of surviving paths */
1487 for (num_paths = 0, p = paths; p; p = p->next)
1490 /* order paths according to diffcore_order */
1491 if (opt->orderfile && num_paths) {
1492 struct obj_order *o;
1494 ALLOC_ARRAY(o, num_paths);
1495 for (i = 0, p = paths; p; p = p->next, i++)
1497 order_objects(opt->orderfile, path_path, o, num_paths);
1498 for (i = 0; i < num_paths - 1; i++) {
1500 p->next = o[i+1].obj;
1503 p = o[num_paths-1].obj;
1511 if (opt->output_format & (DIFF_FORMAT_RAW |
1513 DIFF_FORMAT_NAME_STATUS)) {
1514 for (p = paths; p; p = p->next)
1515 show_raw_diff(p, num_parent, rev);
1518 else if (opt->output_format &
1519 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
1521 else if (opt->output_format & DIFF_FORMAT_CALLBACK)
1522 handle_combined_callback(opt, paths, num_parent, num_paths);
1524 if (opt->output_format & DIFF_FORMAT_PATCH) {
1526 printf("%s%c", diff_line_prefix(opt),
1527 opt->line_termination);
1528 for (p = paths; p; p = p->next)
1529 show_patch_diff(p, num_parent, dense,
1534 /* Clean things up */
1536 struct combine_diff_path *tmp = paths;
1537 paths = paths->next;
1541 clear_pathspec(&diffopts.pathspec);
1544 void diff_tree_combined_merge(const struct commit *commit, int dense,
1545 struct rev_info *rev)
1547 struct commit_list *parent = get_saved_parents(rev, commit);
1548 struct oid_array parents = OID_ARRAY_INIT;
1551 oid_array_append(&parents, &parent->item->object.oid);
1552 parent = parent->next;
1554 diff_tree_combined(&commit->object.oid, &parents, dense, rev);
1555 oid_array_clear(&parents);