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(const struct object_id *oid, unsigned int mode,
289 unsigned long *size, struct userdiff_driver *textconv,
293 enum object_type type;
295 if (S_ISGITLINK(mode)) {
296 struct strbuf buf = STRBUF_INIT;
297 strbuf_addf(&buf, "Subproject commit %s\n", oid_to_hex(oid));
299 blob = strbuf_detach(&buf, NULL);
300 } else if (is_null_oid(oid)) {
303 return xcalloc(1, 1);
304 } else if (textconv) {
305 struct diff_filespec *df = alloc_filespec(path);
306 fill_filespec(df, oid, 1, mode);
307 *size = fill_textconv(textconv, df, &blob);
310 blob = read_object_file(oid, &type, size);
311 if (type != OBJ_BLOB)
312 die("object '%s' is not a blob!", oid_to_hex(oid));
317 static void append_lost(struct sline *sline, int n, const char *line, int len)
320 unsigned long this_mask = (1UL<<n);
321 if (line[len-1] == '\n')
324 FLEX_ALLOC_MEM(lline, line, line, len);
327 lline->prev = sline->plost.lost_tail;
329 lline->prev->next = lline;
331 sline->plost.lost_head = lline;
332 sline->plost.lost_tail = lline;
334 lline->parent_map = this_mask;
337 struct combine_diff_state {
344 struct sline *lost_bucket;
347 static void consume_line(void *state_, char *line, unsigned long len)
349 struct combine_diff_state *state = state_;
350 if (5 < len && !memcmp("@@ -", line, 4)) {
351 if (parse_hunk_header(line, len,
352 &state->ob, &state->on,
353 &state->nb, &state->nn))
355 state->lno = state->nb;
356 if (state->nn == 0) {
357 /* @@ -X,Y +N,0 @@ removed Y lines
358 * that would have come *after* line N
359 * in the result. Our lost buckets hang
360 * to the line after the removed lines,
362 * Note that this is correct even when N == 0,
363 * in which case the hunk removes the first
366 state->lost_bucket = &state->sline[state->nb];
370 state->lost_bucket = &state->sline[state->nb-1];
372 if (!state->sline[state->nb-1].p_lno)
373 state->sline[state->nb-1].p_lno =
374 xcalloc(state->num_parent,
375 sizeof(unsigned long));
376 state->sline[state->nb-1].p_lno[state->n] = state->ob;
379 if (!state->lost_bucket)
380 return; /* not in any hunk yet */
383 append_lost(state->lost_bucket, state->n, line+1, len-1);
386 state->sline[state->lno-1].flag |= state->nmask;
392 static void combine_diff(const struct object_id *parent, unsigned int mode,
393 mmfile_t *result_file,
394 struct sline *sline, unsigned int cnt, int n,
395 int num_parent, int result_deleted,
396 struct userdiff_driver *textconv,
397 const char *path, long flags)
399 unsigned int p_lno, lno;
400 unsigned long nmask = (1UL << n);
403 mmfile_t parent_file;
404 struct combine_diff_state state;
408 return; /* result deleted */
410 parent_file.ptr = grab_blob(parent, mode, &sz, textconv, path);
411 parent_file.size = sz;
412 memset(&xpp, 0, sizeof(xpp));
414 memset(&xecfg, 0, sizeof(xecfg));
415 memset(&state, 0, sizeof(state));
419 state.num_parent = num_parent;
422 if (xdi_diff_outf(&parent_file, result_file, consume_line, &state,
424 die("unable to generate combined diff for %s",
426 free(parent_file.ptr);
428 /* Assign line numbers for this parent.
430 * sline[lno].p_lno[n] records the first line number
431 * (counting from 1) for parent N if the final hunk display
432 * started by showing sline[lno] (possibly showing the lost
433 * lines attached to it first).
435 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
437 sline[lno].p_lno[n] = p_lno;
439 /* Coalesce new lines */
440 if (sline[lno].plost.lost_head) {
441 struct sline *sl = &sline[lno];
442 sl->lost = coalesce_lines(sl->lost, &sl->lenlost,
444 sl->plost.len, n, flags);
445 sl->plost.lost_head = sl->plost.lost_tail = NULL;
449 /* How many lines would this sline advance the p_lno? */
450 ll = sline[lno].lost;
452 if (ll->parent_map & nmask)
453 p_lno++; /* '-' means parent had it */
456 if (lno < cnt && !(sline[lno].flag & nmask))
457 p_lno++; /* no '+' means parent had it */
459 sline[lno].p_lno[n] = p_lno; /* trailer */
462 static unsigned long context = 3;
463 static char combine_marker = '@';
465 static int interesting(struct sline *sline, unsigned long all_mask)
467 /* If some parents lost lines here, or if we have added to
468 * some parent, it is interesting.
470 return ((sline->flag & all_mask) || sline->lost);
473 static unsigned long adjust_hunk_tail(struct sline *sline,
474 unsigned long all_mask,
475 unsigned long hunk_begin,
478 /* i points at the first uninteresting line. If the last line
479 * of the hunk was interesting only because it has some
480 * deletion, then it is not all that interesting for the
481 * purpose of giving trailing context lines. This is because
482 * we output '-' line and then unmodified sline[i-1] itself in
483 * that case which gives us one extra context line.
485 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
490 static unsigned long find_next(struct sline *sline,
494 int look_for_uninteresting)
496 /* We have examined up to i-1 and are about to look at i.
497 * Find next interesting or uninteresting line. Here,
498 * "interesting" does not mean interesting(), but marked by
499 * the give_context() function below (i.e. it includes context
500 * lines that are not interesting to interesting() function
501 * that are surrounded by interesting() ones.
504 if (look_for_uninteresting
505 ? !(sline[i].flag & mark)
506 : (sline[i].flag & mark))
513 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
515 unsigned long all_mask = (1UL<<num_parent) - 1;
516 unsigned long mark = (1UL<<num_parent);
517 unsigned long no_pre_delete = (2UL<<num_parent);
520 /* Two groups of interesting lines may have a short gap of
521 * uninteresting lines. Connect such groups to give them a
524 * We first start from what the interesting() function says,
525 * and mark them with "mark", and paint context lines with the
526 * mark. So interesting() would still say false for such context
527 * lines but they are treated as "interesting" in the end.
529 i = find_next(sline, mark, 0, cnt, 0);
534 unsigned long j = (context < i) ? (i - context) : 0;
537 /* Paint a few lines before the first interesting line. */
539 if (!(sline[j].flag & mark))
540 sline[j].flag |= no_pre_delete;
541 sline[j++].flag |= mark;
545 /* we know up to i is to be included. where does the
546 * next uninteresting one start?
548 j = find_next(sline, mark, i, cnt, 1);
550 break; /* the rest are all interesting */
552 /* lookahead context lines */
553 k = find_next(sline, mark, j, cnt, 0);
554 j = adjust_hunk_tail(sline, all_mask, i, j);
556 if (k < j + context) {
557 /* k is interesting and [j,k) are not, but
558 * paint them interesting because the gap is small.
561 sline[j++].flag |= mark;
566 /* j is the first uninteresting line and there is
567 * no overlap beyond it within context lines. Paint
568 * the trailing edge a bit.
571 k = (j + context < cnt+1) ? j + context : cnt+1;
573 sline[j++].flag |= mark;
578 static int make_hunks(struct sline *sline, unsigned long cnt,
579 int num_parent, int dense)
581 unsigned long all_mask = (1UL<<num_parent) - 1;
582 unsigned long mark = (1UL<<num_parent);
584 int has_interesting = 0;
586 for (i = 0; i <= cnt; i++) {
587 if (interesting(&sline[i], all_mask))
588 sline[i].flag |= mark;
590 sline[i].flag &= ~mark;
593 return give_context(sline, cnt, num_parent);
595 /* Look at each hunk, and if we have changes from only one
596 * parent, or the changes are the same from all but one
597 * parent, mark that uninteresting.
601 unsigned long j, hunk_begin, hunk_end;
602 unsigned long same_diff;
603 while (i <= cnt && !(sline[i].flag & mark))
606 break; /* No more interesting hunks */
608 for (j = i + 1; j <= cnt; j++) {
609 if (!(sline[j].flag & mark)) {
610 /* Look beyond the end to see if there
611 * is an interesting line after this
612 * hunk within context span.
614 unsigned long la; /* lookahead */
616 la = adjust_hunk_tail(sline, all_mask,
618 la = (la + context < cnt + 1) ?
619 (la + context) : cnt + 1;
620 while (la && j <= --la) {
621 if (sline[la].flag & mark) {
633 /* [i..hunk_end) are interesting. Now is it really
634 * interesting? We check if there are only two versions
635 * and the result matches one of them. That is, we look
637 * (+) line, which records lines added to which parents;
638 * this line appears in the result.
639 * (-) line, which records from what parents the line
640 * was removed; this line does not appear in the result.
641 * then check the set of parents the result has difference
642 * from, from all lines. If there are lines that has
643 * different set of parents that the result has differences
644 * from, that means we have more than two versions.
646 * Even when we have only two versions, if the result does
647 * not match any of the parents, the it should be considered
648 * interesting. In such a case, we would have all '+' line.
649 * After passing the above "two versions" test, that would
650 * appear as "the same set of parents" to be "all parents".
654 for (j = i; j < hunk_end && !has_interesting; j++) {
655 unsigned long this_diff = sline[j].flag & all_mask;
656 struct lline *ll = sline[j].lost;
658 /* This has some changes. Is it the
662 same_diff = this_diff;
663 else if (same_diff != this_diff) {
668 while (ll && !has_interesting) {
669 /* Lost this line from these parents;
670 * who are they? Are they the same?
672 this_diff = ll->parent_map;
674 same_diff = this_diff;
675 else if (same_diff != this_diff) {
682 if (!has_interesting && same_diff != all_mask) {
683 /* This hunk is not that interesting after all */
684 for (j = hunk_begin; j < hunk_end; j++)
685 sline[j].flag &= ~mark;
690 has_interesting = give_context(sline, cnt, num_parent);
691 return has_interesting;
694 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
696 l0 = sline[l0].p_lno[n];
697 l1 = sline[l1].p_lno[n];
698 printf(" -%lu,%lu", l0, l1-l0-null_context);
701 static int hunk_comment_line(const char *bol)
708 return (isalpha(ch) || ch == '_' || ch == '$');
711 static void show_line_to_eol(const char *line, int len, const char *reset)
713 int saw_cr_at_eol = 0;
716 saw_cr_at_eol = (len && line[len-1] == '\r');
718 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
720 saw_cr_at_eol ? "\r" : "");
723 static void dump_sline(struct sline *sline, const char *line_prefix,
724 unsigned long cnt, int num_parent,
725 int use_color, int result_deleted)
727 unsigned long mark = (1UL<<num_parent);
728 unsigned long no_pre_delete = (2UL<<num_parent);
730 unsigned long lno = 0;
731 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
732 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
733 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
734 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
735 const char *c_context = diff_get_color(use_color, DIFF_CONTEXT);
736 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
739 return; /* result deleted */
742 unsigned long hunk_end;
743 unsigned long rlines;
744 const char *hunk_comment = NULL;
745 unsigned long null_context = 0;
747 while (lno <= cnt && !(sline[lno].flag & mark)) {
748 if (hunk_comment_line(sline[lno].bol))
749 hunk_comment = sline[lno].bol;
755 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
756 if (!(sline[hunk_end].flag & mark))
759 rlines = hunk_end - lno;
761 rlines--; /* pointing at the last delete hunk */
765 * Even when running with --unified=0, all
766 * lines in the hunk needs to be processed in
767 * the loop below in order to show the
768 * deletion recorded in lost_head. However,
769 * we do not want to show the resulting line
770 * with all blank context markers in such a
774 for (j = lno; j < hunk_end; j++)
775 if (!(sline[j].flag & (mark-1)))
777 rlines -= null_context;
780 printf("%s%s", line_prefix, c_frag);
781 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
782 for (i = 0; i < num_parent; i++)
783 show_parent_lno(sline, lno, hunk_end, i, null_context);
784 printf(" +%lu,%lu ", lno+1, rlines);
785 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
789 for (i = 0; i < 40; i++) {
790 int ch = hunk_comment[i] & 0xff;
791 if (!ch || ch == '\n')
797 printf("%s%s %s%s", c_reset,
800 for (i = 0; i < comment_end; i++)
801 putchar(hunk_comment[i]);
804 printf("%s\n", c_reset);
805 while (lno < hunk_end) {
808 unsigned long p_mask;
809 struct sline *sl = &sline[lno++];
810 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost;
812 printf("%s%s", line_prefix, c_old);
813 for (j = 0; j < num_parent; j++) {
814 if (ll->parent_map & (1UL<<j))
819 show_line_to_eol(ll->line, -1, c_reset);
825 fputs(line_prefix, stdout);
826 if (!(sl->flag & (mark-1))) {
828 * This sline was here to hang the
829 * lost lines in front of it.
833 fputs(c_context, stdout);
836 fputs(c_new, stdout);
837 for (j = 0; j < num_parent; j++) {
838 if (p_mask & sl->flag)
844 show_line_to_eol(sl->bol, sl->len, c_reset);
849 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
852 /* We have already examined parent j and we know parent i
853 * and parent j are the same, so reuse the combined result
854 * of parent j for parent i.
856 unsigned long lno, imask, jmask;
860 for (lno = 0; lno <= cnt; lno++) {
861 struct lline *ll = sline->lost;
862 sline->p_lno[i] = sline->p_lno[j];
864 if (ll->parent_map & jmask)
865 ll->parent_map |= imask;
868 if (sline->flag & jmask)
869 sline->flag |= imask;
872 /* the overall size of the file (sline[cnt]) */
873 sline->p_lno[i] = sline->p_lno[j];
876 static void dump_quoted_path(const char *head,
879 const char *line_prefix,
880 const char *c_meta, const char *c_reset)
882 static struct strbuf buf = STRBUF_INIT;
885 strbuf_addstr(&buf, line_prefix);
886 strbuf_addstr(&buf, c_meta);
887 strbuf_addstr(&buf, head);
888 quote_two_c_style(&buf, prefix, path, 0);
889 strbuf_addstr(&buf, c_reset);
893 static void show_combined_header(struct combine_diff_path *elem,
896 struct rev_info *rev,
897 const char *line_prefix,
899 int show_file_header)
901 struct diff_options *opt = &rev->diffopt;
902 int abbrev = opt->flags.full_index ? GIT_SHA1_HEXSZ : DEFAULT_ABBREV;
903 const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
904 const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
905 const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
906 const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
912 if (rev->loginfo && !rev->no_commit_id)
915 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
916 "", elem->path, line_prefix, c_meta, c_reset);
917 printf("%s%sindex ", line_prefix, c_meta);
918 for (i = 0; i < num_parent; i++) {
919 abb = find_unique_abbrev(&elem->parent[i].oid,
921 printf("%s%s", i ? "," : "", abb);
923 abb = find_unique_abbrev(&elem->oid, abbrev);
924 printf("..%s%s\n", abb, c_reset);
927 deleted = !elem->mode;
929 /* We say it was added if nobody had it */
931 for (i = 0; added && i < num_parent; i++)
932 if (elem->parent[i].status !=
936 printf("%s%snew file mode %06o",
937 line_prefix, c_meta, elem->mode);
940 printf("%s%sdeleted file ",
941 line_prefix, c_meta);
943 for (i = 0; i < num_parent; i++) {
944 printf("%s%06o", i ? "," : "",
945 elem->parent[i].mode);
948 printf("..%06o", elem->mode);
950 printf("%s\n", c_reset);
953 if (!show_file_header)
957 dump_quoted_path("--- ", "", "/dev/null",
958 line_prefix, c_meta, c_reset);
960 dump_quoted_path("--- ", a_prefix, elem->path,
961 line_prefix, c_meta, c_reset);
963 dump_quoted_path("+++ ", "", "/dev/null",
964 line_prefix, c_meta, c_reset);
966 dump_quoted_path("+++ ", b_prefix, elem->path,
967 line_prefix, c_meta, c_reset);
970 static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
971 int dense, int working_tree_file,
972 struct rev_info *rev)
974 struct diff_options *opt = &rev->diffopt;
975 unsigned long result_size, cnt, lno;
976 int result_deleted = 0;
978 struct sline *sline; /* survived lines */
979 int mode_differs = 0;
981 mmfile_t result_file;
982 struct userdiff_driver *userdiff;
983 struct userdiff_driver *textconv = NULL;
985 const char *line_prefix = diff_line_prefix(opt);
987 context = opt->context;
988 userdiff = userdiff_find_by_path(elem->path);
990 userdiff = userdiff_find_by_name("default");
991 if (opt->flags.allow_textconv)
992 textconv = userdiff_get_textconv(userdiff);
994 /* Read the result of merge first */
995 if (!working_tree_file)
996 result = grab_blob(&elem->oid, elem->mode, &result_size,
997 textconv, elem->path);
999 /* Used by diff-tree to read from the working tree */
1003 if (lstat(elem->path, &st) < 0)
1006 if (S_ISLNK(st.st_mode)) {
1007 struct strbuf buf = STRBUF_INIT;
1009 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
1010 error_errno("readlink(%s)", elem->path);
1013 result_size = buf.len;
1014 result = strbuf_detach(&buf, NULL);
1015 elem->mode = canon_mode(st.st_mode);
1016 } else if (S_ISDIR(st.st_mode)) {
1017 struct object_id oid;
1018 if (resolve_gitlink_ref(elem->path, "HEAD", &oid) < 0)
1019 result = grab_blob(&elem->oid, elem->mode,
1020 &result_size, NULL, NULL);
1022 result = grab_blob(&oid, elem->mode,
1023 &result_size, NULL, NULL);
1024 } else if (textconv) {
1025 struct diff_filespec *df = alloc_filespec(elem->path);
1026 fill_filespec(df, &null_oid, 0, st.st_mode);
1027 result_size = fill_textconv(textconv, df, &result);
1029 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
1030 size_t len = xsize_t(st.st_size);
1034 elem->mode = canon_mode(st.st_mode);
1035 /* if symlinks don't work, assume symlink if all parents
1038 is_file = has_symlinks;
1039 for (i = 0; !is_file && i < num_parent; i++)
1040 is_file = !S_ISLNK(elem->parent[i].mode);
1042 elem->mode = canon_mode(S_IFLNK);
1045 result = xmallocz(len);
1047 done = read_in_full(fd, result, len);
1049 die_errno("read error '%s'", elem->path);
1050 else if (done < len)
1051 die("early EOF '%s'", elem->path);
1053 /* If not a fake symlink, apply filters, e.g. autocrlf */
1055 struct strbuf buf = STRBUF_INIT;
1057 if (convert_to_git(&the_index, elem->path, result, len, &buf, global_conv_flags_eol)) {
1059 result = strbuf_detach(&buf, &len);
1069 result = xcalloc(1, 1);
1076 for (i = 0; i < num_parent; i++) {
1077 if (elem->parent[i].mode != elem->mode) {
1085 else if (userdiff->binary != -1)
1086 is_binary = userdiff->binary;
1088 is_binary = buffer_is_binary(result, result_size);
1089 for (i = 0; !is_binary && i < num_parent; i++) {
1092 buf = grab_blob(&elem->parent[i].oid,
1093 elem->parent[i].mode,
1095 if (buffer_is_binary(buf, size))
1101 show_combined_header(elem, num_parent, dense, rev,
1102 line_prefix, mode_differs, 0);
1103 printf("Binary files differ\n");
1108 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
1112 if (result_size && result[result_size-1] != '\n')
1113 cnt++; /* incomplete line */
1115 sline = xcalloc(st_add(cnt, 2), sizeof(*sline));
1116 sline[0].bol = result;
1117 for (lno = 0, cp = result; cp < result + result_size; cp++) {
1119 sline[lno].len = cp - sline[lno].bol;
1122 sline[lno].bol = cp + 1;
1125 if (result_size && result[result_size-1] != '\n')
1126 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
1128 result_file.ptr = result;
1129 result_file.size = result_size;
1131 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1132 * for deletion hunk at the end.
1134 sline[0].p_lno = xcalloc(st_mult(st_add(cnt, 2), num_parent), sizeof(unsigned long));
1135 for (lno = 0; lno <= cnt; lno++)
1136 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
1138 for (i = 0; i < num_parent; i++) {
1140 for (j = 0; j < i; j++) {
1141 if (!oidcmp(&elem->parent[i].oid,
1142 &elem->parent[j].oid)) {
1143 reuse_combine_diff(sline, cnt, i, j);
1148 combine_diff(&elem->parent[i].oid,
1149 elem->parent[i].mode,
1150 &result_file, sline,
1151 cnt, i, num_parent, result_deleted,
1152 textconv, elem->path, opt->xdl_opts);
1155 show_hunks = make_hunks(sline, cnt, num_parent, dense);
1157 if (show_hunks || mode_differs || working_tree_file) {
1158 show_combined_header(elem, num_parent, dense, rev,
1159 line_prefix, mode_differs, 1);
1160 dump_sline(sline, line_prefix, cnt, num_parent,
1161 opt->use_color, result_deleted);
1165 for (lno = 0; lno < cnt; lno++) {
1166 if (sline[lno].lost) {
1167 struct lline *ll = sline[lno].lost;
1169 struct lline *tmp = ll;
1175 free(sline[0].p_lno);
1179 static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
1181 struct diff_options *opt = &rev->diffopt;
1182 int line_termination, inter_name_termination, i;
1183 const char *line_prefix = diff_line_prefix(opt);
1185 line_termination = opt->line_termination;
1186 inter_name_termination = '\t';
1187 if (!line_termination)
1188 inter_name_termination = 0;
1190 if (rev->loginfo && !rev->no_commit_id)
1194 if (opt->output_format & DIFF_FORMAT_RAW) {
1195 printf("%s", line_prefix);
1197 /* As many colons as there are parents */
1198 for (i = 0; i < num_parent; i++)
1201 /* Show the modes */
1202 for (i = 0; i < num_parent; i++)
1203 printf("%06o ", p->parent[i].mode);
1204 printf("%06o", p->mode);
1207 for (i = 0; i < num_parent; i++)
1208 printf(" %s", diff_aligned_abbrev(&p->parent[i].oid,
1210 printf(" %s ", diff_aligned_abbrev(&p->oid, opt->abbrev));
1213 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
1214 for (i = 0; i < num_parent; i++)
1215 putchar(p->parent[i].status);
1216 putchar(inter_name_termination);
1219 write_name_quoted(p->path, stdout, line_termination);
1223 * The result (p->elem) is from the working tree and their
1224 * parents are typically from multiple stages during a merge
1225 * (i.e. diff-files) or the state in HEAD and in the index
1226 * (i.e. diff-index).
1228 void show_combined_diff(struct combine_diff_path *p,
1231 struct rev_info *rev)
1233 struct diff_options *opt = &rev->diffopt;
1235 if (opt->output_format & (DIFF_FORMAT_RAW |
1237 DIFF_FORMAT_NAME_STATUS))
1238 show_raw_diff(p, num_parent, rev);
1239 else if (opt->output_format & DIFF_FORMAT_PATCH)
1240 show_patch_diff(p, num_parent, dense, 1, rev);
1243 static void free_combined_pair(struct diff_filepair *pair)
1250 * A combine_diff_path expresses N parents on the LHS against 1 merge
1251 * result. Synthesize a diff_filepair that has N entries on the "one"
1252 * side and 1 entry on the "two" side.
1254 * In the future, we might want to add more data to combine_diff_path
1255 * so that we can fill fields we are ignoring (most notably, size) here,
1256 * but currently nobody uses it, so this should suffice for now.
1258 static struct diff_filepair *combined_pair(struct combine_diff_path *p,
1262 struct diff_filepair *pair;
1263 struct diff_filespec *pool;
1265 pair = xmalloc(sizeof(*pair));
1266 pool = xcalloc(st_add(num_parent, 1), sizeof(struct diff_filespec));
1267 pair->one = pool + 1;
1270 for (i = 0; i < num_parent; i++) {
1271 pair->one[i].path = p->path;
1272 pair->one[i].mode = p->parent[i].mode;
1273 oidcpy(&pair->one[i].oid, &p->parent[i].oid);
1274 pair->one[i].oid_valid = !is_null_oid(&p->parent[i].oid);
1275 pair->one[i].has_more_entries = 1;
1277 pair->one[num_parent - 1].has_more_entries = 0;
1279 pair->two->path = p->path;
1280 pair->two->mode = p->mode;
1281 oidcpy(&pair->two->oid, &p->oid);
1282 pair->two->oid_valid = !is_null_oid(&p->oid);
1286 static void handle_combined_callback(struct diff_options *opt,
1287 struct combine_diff_path *paths,
1291 struct combine_diff_path *p;
1292 struct diff_queue_struct q;
1295 q.queue = xcalloc(num_paths, sizeof(struct diff_filepair *));
1296 q.alloc = num_paths;
1298 for (i = 0, p = paths; p; p = p->next)
1299 q.queue[i++] = combined_pair(p, num_parent);
1300 opt->format_callback(&q, opt, opt->format_callback_data);
1301 for (i = 0; i < num_paths; i++)
1302 free_combined_pair(q.queue[i]);
1306 static const char *path_path(void *obj)
1308 struct combine_diff_path *path = (struct combine_diff_path *)obj;
1314 /* find set of paths that every parent touches */
1315 static struct combine_diff_path *find_paths_generic(const struct object_id *oid,
1316 const struct oid_array *parents, struct diff_options *opt)
1318 struct combine_diff_path *paths = NULL;
1319 int i, num_parent = parents->nr;
1321 int output_format = opt->output_format;
1322 const char *orderfile = opt->orderfile;
1324 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1325 /* tell diff_tree to emit paths in sorted (=tree) order */
1326 opt->orderfile = NULL;
1328 /* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn) (wrt paths) */
1329 for (i = 0; i < num_parent; i++) {
1331 * show stat against the first parent even when doing
1334 int stat_opt = (output_format &
1335 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
1336 if (i == 0 && stat_opt)
1337 opt->output_format = stat_opt;
1339 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1340 diff_tree_oid(&parents->oid[i], oid, "", opt);
1342 paths = intersect_paths(paths, i, num_parent);
1344 /* if showing diff, show it in requested order */
1345 if (opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1347 diffcore_order(orderfile);
1353 opt->output_format = output_format;
1354 opt->orderfile = orderfile;
1360 * find set of paths that everybody touches, assuming diff is run without
1361 * rename/copy detection, etc, comparing all trees simultaneously (= faster).
1363 static struct combine_diff_path *find_paths_multitree(
1364 const struct object_id *oid, const struct oid_array *parents,
1365 struct diff_options *opt)
1367 int i, nparent = parents->nr;
1368 const struct object_id **parents_oid;
1369 struct combine_diff_path paths_head;
1372 ALLOC_ARRAY(parents_oid, nparent);
1373 for (i = 0; i < nparent; i++)
1374 parents_oid[i] = &parents->oid[i];
1376 /* fake list head, so worker can assume it is non-NULL */
1377 paths_head.next = NULL;
1379 strbuf_init(&base, PATH_MAX);
1380 diff_tree_paths(&paths_head, oid, parents_oid, nparent, &base, opt);
1382 strbuf_release(&base);
1384 return paths_head.next;
1388 void diff_tree_combined(const struct object_id *oid,
1389 const struct oid_array *parents,
1391 struct rev_info *rev)
1393 struct diff_options *opt = &rev->diffopt;
1394 struct diff_options diffopts;
1395 struct combine_diff_path *p, *paths;
1396 int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
1397 int need_generic_pathscan;
1399 /* nothing to do, if no parents */
1403 show_log_first = !!rev->loginfo && !rev->no_commit_id;
1405 if (show_log_first) {
1408 if (rev->verbose_header && opt->output_format &&
1409 opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1410 !commit_format_is_empty(rev->commit_format))
1411 printf("%s%c", diff_line_prefix(opt),
1412 opt->line_termination);
1416 copy_pathspec(&diffopts.pathspec, &opt->pathspec);
1417 diffopts.flags.recursive = 1;
1418 diffopts.flags.allow_external = 0;
1420 /* find set of paths that everybody touches
1424 * Diffcore transformations are bound to diff_filespec and logic
1425 * comparing two entries - i.e. they do not apply directly to combine
1428 * If some of such transformations is requested - we launch generic
1429 * path scanning, which works significantly slower compared to
1430 * simultaneous all-trees-in-one-go scan in find_paths_multitree().
1432 * TODO some of the filters could be ported to work on
1433 * combine_diff_paths - i.e. all functionality that skips paths, so in
1434 * theory, we could end up having only multitree path scanning.
1436 * NOTE please keep this semantically in sync with diffcore_std()
1438 need_generic_pathscan = opt->skip_stat_unmatch ||
1439 opt->flags.follow_renames ||
1440 opt->break_opt != -1 ||
1441 opt->detect_rename ||
1442 (opt->pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
1446 if (need_generic_pathscan) {
1448 * NOTE generic case also handles --stat, as it computes
1449 * diff(sha1,parent_i) for all i to do the job, specifically
1452 paths = find_paths_generic(oid, parents, &diffopts);
1456 paths = find_paths_multitree(oid, parents, &diffopts);
1459 * show stat against the first parent even
1460 * when doing combined diff.
1462 stat_opt = (opt->output_format &
1463 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
1465 diffopts.output_format = stat_opt;
1467 diff_tree_oid(&parents->oid[0], oid, "", &diffopts);
1468 diffcore_std(&diffopts);
1470 diffcore_order(opt->orderfile);
1471 diff_flush(&diffopts);
1475 /* find out number of surviving paths */
1476 for (num_paths = 0, p = paths; p; p = p->next)
1479 /* order paths according to diffcore_order */
1480 if (opt->orderfile && num_paths) {
1481 struct obj_order *o;
1483 ALLOC_ARRAY(o, num_paths);
1484 for (i = 0, p = paths; p; p = p->next, i++)
1486 order_objects(opt->orderfile, path_path, o, num_paths);
1487 for (i = 0; i < num_paths - 1; i++) {
1489 p->next = o[i+1].obj;
1492 p = o[num_paths-1].obj;
1500 if (opt->output_format & (DIFF_FORMAT_RAW |
1502 DIFF_FORMAT_NAME_STATUS)) {
1503 for (p = paths; p; p = p->next)
1504 show_raw_diff(p, num_parent, rev);
1507 else if (opt->output_format &
1508 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
1510 else if (opt->output_format & DIFF_FORMAT_CALLBACK)
1511 handle_combined_callback(opt, paths, num_parent, num_paths);
1513 if (opt->output_format & DIFF_FORMAT_PATCH) {
1515 printf("%s%c", diff_line_prefix(opt),
1516 opt->line_termination);
1517 for (p = paths; p; p = p->next)
1518 show_patch_diff(p, num_parent, dense,
1523 /* Clean things up */
1525 struct combine_diff_path *tmp = paths;
1526 paths = paths->next;
1530 clear_pathspec(&diffopts.pathspec);
1533 void diff_tree_combined_merge(const struct commit *commit, int dense,
1534 struct rev_info *rev)
1536 struct commit_list *parent = get_saved_parents(rev, commit);
1537 struct oid_array parents = OID_ARRAY_INIT;
1540 oid_array_append(&parents, &parent->item->object.oid);
1541 parent = parent->next;
1543 diff_tree_combined(&commit->object.oid, &parents, dense, rev);
1544 oid_array_clear(&parents);