7 #include "xdiff-interface.h"
8 #include "xdiff/xmacros.h"
12 #include "sha1-array.h"
14 static struct combine_diff_path
*intersect_paths(struct combine_diff_path
*curr
, int n
, int num_parent
)
16 struct diff_queue_struct
*q
= &diff_queued_diff
;
17 struct combine_diff_path
*p
;
21 struct combine_diff_path
*list
= NULL
, **tail
= &list
;
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
);
36 sizeof(p
->parent
[0]) * num_parent
);
38 hashcpy(p
->sha1
, q
->queue
[i
]->two
->sha1
);
39 p
->mode
= q
->queue
[i
]->two
->mode
;
40 hashcpy(p
->parent
[n
].sha1
, q
->queue
[i
]->one
->sha1
);
41 p
->parent
[n
].mode
= q
->queue
[i
]->one
->mode
;
42 p
->parent
[n
].status
= q
->queue
[i
]->status
;
49 for (p
= curr
; p
; p
= p
->next
) {
53 for (i
= 0; i
< q
->nr
; i
++) {
57 if (diff_unmodified_pair(q
->queue
[i
]))
59 path
= q
->queue
[i
]->two
->path
;
61 if (len
== p
->len
&& !memcmp(path
, p
->path
, len
)) {
63 hashcpy(p
->parent
[n
].sha1
, q
->queue
[i
]->one
->sha1
);
64 p
->parent
[n
].mode
= q
->queue
[i
]->one
->mode
;
65 p
->parent
[n
].status
= q
->queue
[i
]->status
;
75 /* Lines lost from parent */
79 unsigned long parent_map
;
80 char line
[FLEX_ARRAY
];
83 /* Lines surviving in the merge result */
85 struct lline
*lost_head
, **lost_tail
;
86 struct lline
*next_lost
;
89 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
90 * we did not change it).
91 * bit N is used for "interesting" lines, including context.
92 * bit (N+1) is used for "do not show deletion before this".
98 static char *grab_blob(const unsigned char *sha1
, unsigned int mode
,
99 unsigned long *size
, struct userdiff_driver
*textconv
,
103 enum object_type type
;
105 if (S_ISGITLINK(mode
)) {
107 *size
= snprintf(blob
, 100,
108 "Subproject commit %s\n", sha1_to_hex(sha1
));
109 } else if (is_null_sha1(sha1
)) {
112 return xcalloc(1, 1);
113 } else if (textconv
) {
114 struct diff_filespec
*df
= alloc_filespec(path
);
115 fill_filespec(df
, sha1
, 1, mode
);
116 *size
= fill_textconv(textconv
, df
, &blob
);
119 blob
= read_sha1_file(sha1
, &type
, size
);
120 if (type
!= OBJ_BLOB
)
121 die("object '%s' is not a blob!", sha1_to_hex(sha1
));
126 static int match_string_spaces(const char *line1
, int len1
,
127 const char *line2
, int len2
,
130 if (flags
& XDF_WHITESPACE_FLAGS
) {
131 for (; len1
> 0 && XDL_ISSPACE(line1
[len1
- 1]); len1
--);
132 for (; len2
> 0 && XDL_ISSPACE(line2
[len2
- 1]); len2
--);
135 if (!(flags
& (XDF_IGNORE_WHITESPACE
| XDF_IGNORE_WHITESPACE_CHANGE
)))
136 return (len1
== len2
&& !memcmp(line1
, line2
, len1
));
138 while (len1
> 0 && len2
> 0) {
141 if (XDL_ISSPACE(line1
[len1
]) || XDL_ISSPACE(line2
[len2
])) {
142 if ((flags
& XDF_IGNORE_WHITESPACE_CHANGE
) &&
143 (!XDL_ISSPACE(line1
[len1
]) || !XDL_ISSPACE(line2
[len2
])))
146 for (; len1
> 0 && XDL_ISSPACE(line1
[len1
]); len1
--);
147 for (; len2
> 0 && XDL_ISSPACE(line2
[len2
]); len2
--);
149 if (line1
[len1
] != line2
[len2
])
153 if (flags
& XDF_IGNORE_WHITESPACE
) {
154 /* Consume remaining spaces */
155 for (; len1
> 0 && XDL_ISSPACE(line1
[len1
- 1]); len1
--);
156 for (; len2
> 0 && XDL_ISSPACE(line2
[len2
- 1]); len2
--);
159 /* We matched full line1 and line2 */
166 static void append_lost(struct sline
*sline
, int n
, const char *line
, int len
, long flags
)
169 unsigned long this_mask
= (1UL<<n
);
170 if (line
[len
-1] == '\n')
173 /* Check to see if we can squash things */
174 if (sline
->lost_head
) {
175 lline
= sline
->next_lost
;
177 if (match_string_spaces(lline
->line
, lline
->len
,
179 lline
->parent_map
|= this_mask
;
180 sline
->next_lost
= lline
->next
;
187 lline
= xmalloc(sizeof(*lline
) + len
+ 1);
190 lline
->parent_map
= this_mask
;
191 memcpy(lline
->line
, line
, len
);
192 lline
->line
[len
] = 0;
193 *sline
->lost_tail
= lline
;
194 sline
->lost_tail
= &lline
->next
;
195 sline
->next_lost
= NULL
;
198 struct combine_diff_state
{
205 struct sline
*lost_bucket
;
209 static void consume_line(void *state_
, char *line
, unsigned long len
)
211 struct combine_diff_state
*state
= state_
;
212 if (5 < len
&& !memcmp("@@ -", line
, 4)) {
213 if (parse_hunk_header(line
, len
,
214 &state
->ob
, &state
->on
,
215 &state
->nb
, &state
->nn
))
217 state
->lno
= state
->nb
;
218 if (state
->nn
== 0) {
219 /* @@ -X,Y +N,0 @@ removed Y lines
220 * that would have come *after* line N
221 * in the result. Our lost buckets hang
222 * to the line after the removed lines,
224 * Note that this is correct even when N == 0,
225 * in which case the hunk removes the first
228 state
->lost_bucket
= &state
->sline
[state
->nb
];
232 state
->lost_bucket
= &state
->sline
[state
->nb
-1];
234 if (!state
->sline
[state
->nb
-1].p_lno
)
235 state
->sline
[state
->nb
-1].p_lno
=
236 xcalloc(state
->num_parent
,
237 sizeof(unsigned long));
238 state
->sline
[state
->nb
-1].p_lno
[state
->n
] = state
->ob
;
239 state
->lost_bucket
->next_lost
= state
->lost_bucket
->lost_head
;
242 if (!state
->lost_bucket
)
243 return; /* not in any hunk yet */
246 append_lost(state
->lost_bucket
, state
->n
, line
+1, len
-1, state
->flags
);
249 state
->sline
[state
->lno
-1].flag
|= state
->nmask
;
255 static void combine_diff(const unsigned char *parent
, unsigned int mode
,
256 mmfile_t
*result_file
,
257 struct sline
*sline
, unsigned int cnt
, int n
,
258 int num_parent
, int result_deleted
,
259 struct userdiff_driver
*textconv
,
260 const char *path
, long flags
)
262 unsigned int p_lno
, lno
;
263 unsigned long nmask
= (1UL << n
);
266 mmfile_t parent_file
;
267 struct combine_diff_state state
;
271 return; /* result deleted */
273 parent_file
.ptr
= grab_blob(parent
, mode
, &sz
, textconv
, path
);
274 parent_file
.size
= sz
;
275 memset(&xpp
, 0, sizeof(xpp
));
277 memset(&xecfg
, 0, sizeof(xecfg
));
278 memset(&state
, 0, sizeof(state
));
283 state
.num_parent
= num_parent
;
286 xdi_diff_outf(&parent_file
, result_file
, consume_line
, &state
,
288 free(parent_file
.ptr
);
290 /* Assign line numbers for this parent.
292 * sline[lno].p_lno[n] records the first line number
293 * (counting from 1) for parent N if the final hunk display
294 * started by showing sline[lno] (possibly showing the lost
295 * lines attached to it first).
297 for (lno
= 0, p_lno
= 1; lno
<= cnt
; lno
++) {
299 sline
[lno
].p_lno
[n
] = p_lno
;
301 /* How many lines would this sline advance the p_lno? */
302 ll
= sline
[lno
].lost_head
;
304 if (ll
->parent_map
& nmask
)
305 p_lno
++; /* '-' means parent had it */
308 if (lno
< cnt
&& !(sline
[lno
].flag
& nmask
))
309 p_lno
++; /* no '+' means parent had it */
311 sline
[lno
].p_lno
[n
] = p_lno
; /* trailer */
314 static unsigned long context
= 3;
315 static char combine_marker
= '@';
317 static int interesting(struct sline
*sline
, unsigned long all_mask
)
319 /* If some parents lost lines here, or if we have added to
320 * some parent, it is interesting.
322 return ((sline
->flag
& all_mask
) || sline
->lost_head
);
325 static unsigned long adjust_hunk_tail(struct sline
*sline
,
326 unsigned long all_mask
,
327 unsigned long hunk_begin
,
330 /* i points at the first uninteresting line. If the last line
331 * of the hunk was interesting only because it has some
332 * deletion, then it is not all that interesting for the
333 * purpose of giving trailing context lines. This is because
334 * we output '-' line and then unmodified sline[i-1] itself in
335 * that case which gives us one extra context line.
337 if ((hunk_begin
+ 1 <= i
) && !(sline
[i
-1].flag
& all_mask
))
342 static unsigned long find_next(struct sline
*sline
,
346 int look_for_uninteresting
)
348 /* We have examined up to i-1 and are about to look at i.
349 * Find next interesting or uninteresting line. Here,
350 * "interesting" does not mean interesting(), but marked by
351 * the give_context() function below (i.e. it includes context
352 * lines that are not interesting to interesting() function
353 * that are surrounded by interesting() ones.
356 if (look_for_uninteresting
357 ? !(sline
[i
].flag
& mark
)
358 : (sline
[i
].flag
& mark
))
365 static int give_context(struct sline
*sline
, unsigned long cnt
, int num_parent
)
367 unsigned long all_mask
= (1UL<<num_parent
) - 1;
368 unsigned long mark
= (1UL<<num_parent
);
369 unsigned long no_pre_delete
= (2UL<<num_parent
);
372 /* Two groups of interesting lines may have a short gap of
373 * uninteresting lines. Connect such groups to give them a
376 * We first start from what the interesting() function says,
377 * and mark them with "mark", and paint context lines with the
378 * mark. So interesting() would still say false for such context
379 * lines but they are treated as "interesting" in the end.
381 i
= find_next(sline
, mark
, 0, cnt
, 0);
386 unsigned long j
= (context
< i
) ? (i
- context
) : 0;
389 /* Paint a few lines before the first interesting line. */
391 sline
[j
++].flag
|= mark
| no_pre_delete
;
394 /* we know up to i is to be included. where does the
395 * next uninteresting one start?
397 j
= find_next(sline
, mark
, i
, cnt
, 1);
399 break; /* the rest are all interesting */
401 /* lookahead context lines */
402 k
= find_next(sline
, mark
, j
, cnt
, 0);
403 j
= adjust_hunk_tail(sline
, all_mask
, i
, j
);
405 if (k
< j
+ context
) {
406 /* k is interesting and [j,k) are not, but
407 * paint them interesting because the gap is small.
410 sline
[j
++].flag
|= mark
;
415 /* j is the first uninteresting line and there is
416 * no overlap beyond it within context lines. Paint
417 * the trailing edge a bit.
420 k
= (j
+ context
< cnt
+1) ? j
+ context
: cnt
+1;
422 sline
[j
++].flag
|= mark
;
427 static int make_hunks(struct sline
*sline
, unsigned long cnt
,
428 int num_parent
, int dense
)
430 unsigned long all_mask
= (1UL<<num_parent
) - 1;
431 unsigned long mark
= (1UL<<num_parent
);
433 int has_interesting
= 0;
435 for (i
= 0; i
<= cnt
; i
++) {
436 if (interesting(&sline
[i
], all_mask
))
437 sline
[i
].flag
|= mark
;
439 sline
[i
].flag
&= ~mark
;
442 return give_context(sline
, cnt
, num_parent
);
444 /* Look at each hunk, and if we have changes from only one
445 * parent, or the changes are the same from all but one
446 * parent, mark that uninteresting.
450 unsigned long j
, hunk_begin
, hunk_end
;
451 unsigned long same_diff
;
452 while (i
<= cnt
&& !(sline
[i
].flag
& mark
))
455 break; /* No more interesting hunks */
457 for (j
= i
+ 1; j
<= cnt
; j
++) {
458 if (!(sline
[j
].flag
& mark
)) {
459 /* Look beyond the end to see if there
460 * is an interesting line after this
461 * hunk within context span.
463 unsigned long la
; /* lookahead */
465 la
= adjust_hunk_tail(sline
, all_mask
,
467 la
= (la
+ context
< cnt
+ 1) ?
468 (la
+ context
) : cnt
+ 1;
469 while (la
&& j
<= --la
) {
470 if (sline
[la
].flag
& mark
) {
482 /* [i..hunk_end) are interesting. Now is it really
483 * interesting? We check if there are only two versions
484 * and the result matches one of them. That is, we look
486 * (+) line, which records lines added to which parents;
487 * this line appears in the result.
488 * (-) line, which records from what parents the line
489 * was removed; this line does not appear in the result.
490 * then check the set of parents the result has difference
491 * from, from all lines. If there are lines that has
492 * different set of parents that the result has differences
493 * from, that means we have more than two versions.
495 * Even when we have only two versions, if the result does
496 * not match any of the parents, the it should be considered
497 * interesting. In such a case, we would have all '+' line.
498 * After passing the above "two versions" test, that would
499 * appear as "the same set of parents" to be "all parents".
503 for (j
= i
; j
< hunk_end
&& !has_interesting
; j
++) {
504 unsigned long this_diff
= sline
[j
].flag
& all_mask
;
505 struct lline
*ll
= sline
[j
].lost_head
;
507 /* This has some changes. Is it the
511 same_diff
= this_diff
;
512 else if (same_diff
!= this_diff
) {
517 while (ll
&& !has_interesting
) {
518 /* Lost this line from these parents;
519 * who are they? Are they the same?
521 this_diff
= ll
->parent_map
;
523 same_diff
= this_diff
;
524 else if (same_diff
!= this_diff
) {
531 if (!has_interesting
&& same_diff
!= all_mask
) {
532 /* This hunk is not that interesting after all */
533 for (j
= hunk_begin
; j
< hunk_end
; j
++)
534 sline
[j
].flag
&= ~mark
;
539 has_interesting
= give_context(sline
, cnt
, num_parent
);
540 return has_interesting
;
543 static void show_parent_lno(struct sline
*sline
, unsigned long l0
, unsigned long l1
, int n
, unsigned long null_context
)
545 l0
= sline
[l0
].p_lno
[n
];
546 l1
= sline
[l1
].p_lno
[n
];
547 printf(" -%lu,%lu", l0
, l1
-l0
-null_context
);
550 static int hunk_comment_line(const char *bol
)
557 return (isalpha(ch
) || ch
== '_' || ch
== '$');
560 static void show_line_to_eol(const char *line
, int len
, const char *reset
)
562 int saw_cr_at_eol
= 0;
565 saw_cr_at_eol
= (len
&& line
[len
-1] == '\r');
567 printf("%.*s%s%s\n", len
- saw_cr_at_eol
, line
,
569 saw_cr_at_eol
? "\r" : "");
572 static void dump_sline(struct sline
*sline
, const char *line_prefix
,
573 unsigned long cnt
, int num_parent
,
574 int use_color
, int result_deleted
)
576 unsigned long mark
= (1UL<<num_parent
);
577 unsigned long no_pre_delete
= (2UL<<num_parent
);
579 unsigned long lno
= 0;
580 const char *c_frag
= diff_get_color(use_color
, DIFF_FRAGINFO
);
581 const char *c_func
= diff_get_color(use_color
, DIFF_FUNCINFO
);
582 const char *c_new
= diff_get_color(use_color
, DIFF_FILE_NEW
);
583 const char *c_old
= diff_get_color(use_color
, DIFF_FILE_OLD
);
584 const char *c_plain
= diff_get_color(use_color
, DIFF_PLAIN
);
585 const char *c_reset
= diff_get_color(use_color
, DIFF_RESET
);
588 return; /* result deleted */
591 unsigned long hunk_end
;
592 unsigned long rlines
;
593 const char *hunk_comment
= NULL
;
594 unsigned long null_context
= 0;
596 while (lno
<= cnt
&& !(sline
[lno
].flag
& mark
)) {
597 if (hunk_comment_line(sline
[lno
].bol
))
598 hunk_comment
= sline
[lno
].bol
;
604 for (hunk_end
= lno
+ 1; hunk_end
<= cnt
; hunk_end
++)
605 if (!(sline
[hunk_end
].flag
& mark
))
608 rlines
= hunk_end
- lno
;
610 rlines
--; /* pointing at the last delete hunk */
614 * Even when running with --unified=0, all
615 * lines in the hunk needs to be processed in
616 * the loop below in order to show the
617 * deletion recorded in lost_head. However,
618 * we do not want to show the resulting line
619 * with all blank context markers in such a
623 for (j
= lno
; j
< hunk_end
; j
++)
624 if (!(sline
[j
].flag
& (mark
-1)))
626 rlines
-= null_context
;
629 printf("%s%s", line_prefix
, c_frag
);
630 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
631 for (i
= 0; i
< num_parent
; i
++)
632 show_parent_lno(sline
, lno
, hunk_end
, i
, null_context
);
633 printf(" +%lu,%lu ", lno
+1, rlines
);
634 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
638 for (i
= 0; i
< 40; i
++) {
639 int ch
= hunk_comment
[i
] & 0xff;
640 if (!ch
|| ch
== '\n')
646 printf("%s%s %s%s", c_reset
,
649 for (i
= 0; i
< comment_end
; i
++)
650 putchar(hunk_comment
[i
]);
653 printf("%s\n", c_reset
);
654 while (lno
< hunk_end
) {
657 unsigned long p_mask
;
658 struct sline
*sl
= &sline
[lno
++];
659 ll
= (sl
->flag
& no_pre_delete
) ? NULL
: sl
->lost_head
;
661 printf("%s%s", line_prefix
, c_old
);
662 for (j
= 0; j
< num_parent
; j
++) {
663 if (ll
->parent_map
& (1UL<<j
))
668 show_line_to_eol(ll
->line
, -1, c_reset
);
674 fputs(line_prefix
, stdout
);
675 if (!(sl
->flag
& (mark
-1))) {
677 * This sline was here to hang the
678 * lost lines in front of it.
682 fputs(c_plain
, stdout
);
685 fputs(c_new
, stdout
);
686 for (j
= 0; j
< num_parent
; j
++) {
687 if (p_mask
& sl
->flag
)
693 show_line_to_eol(sl
->bol
, sl
->len
, c_reset
);
698 static void reuse_combine_diff(struct sline
*sline
, unsigned long cnt
,
701 /* We have already examined parent j and we know parent i
702 * and parent j are the same, so reuse the combined result
703 * of parent j for parent i.
705 unsigned long lno
, imask
, jmask
;
709 for (lno
= 0; lno
<= cnt
; lno
++) {
710 struct lline
*ll
= sline
->lost_head
;
711 sline
->p_lno
[i
] = sline
->p_lno
[j
];
713 if (ll
->parent_map
& jmask
)
714 ll
->parent_map
|= imask
;
717 if (sline
->flag
& jmask
)
718 sline
->flag
|= imask
;
721 /* the overall size of the file (sline[cnt]) */
722 sline
->p_lno
[i
] = sline
->p_lno
[j
];
725 static void dump_quoted_path(const char *head
,
728 const char *line_prefix
,
729 const char *c_meta
, const char *c_reset
)
731 static struct strbuf buf
= STRBUF_INIT
;
734 strbuf_addstr(&buf
, line_prefix
);
735 strbuf_addstr(&buf
, c_meta
);
736 strbuf_addstr(&buf
, head
);
737 quote_two_c_style(&buf
, prefix
, path
, 0);
738 strbuf_addstr(&buf
, c_reset
);
742 static void show_combined_header(struct combine_diff_path
*elem
,
745 struct rev_info
*rev
,
746 const char *line_prefix
,
748 int show_file_header
)
750 struct diff_options
*opt
= &rev
->diffopt
;
751 int abbrev
= DIFF_OPT_TST(opt
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
752 const char *a_prefix
= opt
->a_prefix
? opt
->a_prefix
: "a/";
753 const char *b_prefix
= opt
->b_prefix
? opt
->b_prefix
: "b/";
754 const char *c_meta
= diff_get_color_opt(opt
, DIFF_METAINFO
);
755 const char *c_reset
= diff_get_color_opt(opt
, DIFF_RESET
);
761 if (rev
->loginfo
&& !rev
->no_commit_id
)
764 dump_quoted_path(dense
? "diff --cc " : "diff --combined ",
765 "", elem
->path
, line_prefix
, c_meta
, c_reset
);
766 printf("%s%sindex ", line_prefix
, c_meta
);
767 for (i
= 0; i
< num_parent
; i
++) {
768 abb
= find_unique_abbrev(elem
->parent
[i
].sha1
,
770 printf("%s%s", i
? "," : "", abb
);
772 abb
= find_unique_abbrev(elem
->sha1
, abbrev
);
773 printf("..%s%s\n", abb
, c_reset
);
776 deleted
= !elem
->mode
;
778 /* We say it was added if nobody had it */
780 for (i
= 0; added
&& i
< num_parent
; i
++)
781 if (elem
->parent
[i
].status
!=
785 printf("%s%snew file mode %06o",
786 line_prefix
, c_meta
, elem
->mode
);
789 printf("%s%sdeleted file ",
790 line_prefix
, c_meta
);
792 for (i
= 0; i
< num_parent
; i
++) {
793 printf("%s%06o", i
? "," : "",
794 elem
->parent
[i
].mode
);
797 printf("..%06o", elem
->mode
);
799 printf("%s\n", c_reset
);
802 if (!show_file_header
)
806 dump_quoted_path("--- ", "", "/dev/null",
807 line_prefix
, c_meta
, c_reset
);
809 dump_quoted_path("--- ", a_prefix
, elem
->path
,
810 line_prefix
, c_meta
, c_reset
);
812 dump_quoted_path("+++ ", "", "/dev/null",
813 line_prefix
, c_meta
, c_reset
);
815 dump_quoted_path("+++ ", b_prefix
, elem
->path
,
816 line_prefix
, c_meta
, c_reset
);
819 static void show_patch_diff(struct combine_diff_path
*elem
, int num_parent
,
820 int dense
, int working_tree_file
,
821 struct rev_info
*rev
)
823 struct diff_options
*opt
= &rev
->diffopt
;
824 unsigned long result_size
, cnt
, lno
;
825 int result_deleted
= 0;
827 struct sline
*sline
; /* survived lines */
828 int mode_differs
= 0;
830 mmfile_t result_file
;
831 struct userdiff_driver
*userdiff
;
832 struct userdiff_driver
*textconv
= NULL
;
834 const char *line_prefix
= diff_line_prefix(opt
);
836 context
= opt
->context
;
837 userdiff
= userdiff_find_by_path(elem
->path
);
839 userdiff
= userdiff_find_by_name("default");
840 if (DIFF_OPT_TST(opt
, ALLOW_TEXTCONV
))
841 textconv
= userdiff_get_textconv(userdiff
);
843 /* Read the result of merge first */
844 if (!working_tree_file
)
845 result
= grab_blob(elem
->sha1
, elem
->mode
, &result_size
,
846 textconv
, elem
->path
);
848 /* Used by diff-tree to read from the working tree */
852 if (lstat(elem
->path
, &st
) < 0)
855 if (S_ISLNK(st
.st_mode
)) {
856 struct strbuf buf
= STRBUF_INIT
;
858 if (strbuf_readlink(&buf
, elem
->path
, st
.st_size
) < 0) {
859 error("readlink(%s): %s", elem
->path
,
863 result_size
= buf
.len
;
864 result
= strbuf_detach(&buf
, NULL
);
865 elem
->mode
= canon_mode(st
.st_mode
);
866 } else if (S_ISDIR(st
.st_mode
)) {
867 unsigned char sha1
[20];
868 if (resolve_gitlink_ref(elem
->path
, "HEAD", sha1
) < 0)
869 result
= grab_blob(elem
->sha1
, elem
->mode
,
870 &result_size
, NULL
, NULL
);
872 result
= grab_blob(sha1
, elem
->mode
,
873 &result_size
, NULL
, NULL
);
874 } else if (textconv
) {
875 struct diff_filespec
*df
= alloc_filespec(elem
->path
);
876 fill_filespec(df
, null_sha1
, 0, st
.st_mode
);
877 result_size
= fill_textconv(textconv
, df
, &result
);
879 } else if (0 <= (fd
= open(elem
->path
, O_RDONLY
))) {
880 size_t len
= xsize_t(st
.st_size
);
884 elem
->mode
= canon_mode(st
.st_mode
);
885 /* if symlinks don't work, assume symlink if all parents
888 is_file
= has_symlinks
;
889 for (i
= 0; !is_file
&& i
< num_parent
; i
++)
890 is_file
= !S_ISLNK(elem
->parent
[i
].mode
);
892 elem
->mode
= canon_mode(S_IFLNK
);
895 result
= xmalloc(len
+ 1);
897 done
= read_in_full(fd
, result
, len
);
899 die_errno("read error '%s'", elem
->path
);
901 die("early EOF '%s'", elem
->path
);
905 /* If not a fake symlink, apply filters, e.g. autocrlf */
907 struct strbuf buf
= STRBUF_INIT
;
909 if (convert_to_git(elem
->path
, result
, len
, &buf
, safe_crlf
)) {
911 result
= strbuf_detach(&buf
, &len
);
921 result
= xcalloc(1, 1);
928 for (i
= 0; i
< num_parent
; i
++) {
929 if (elem
->parent
[i
].mode
!= elem
->mode
) {
937 else if (userdiff
->binary
!= -1)
938 is_binary
= userdiff
->binary
;
940 is_binary
= buffer_is_binary(result
, result_size
);
941 for (i
= 0; !is_binary
&& i
< num_parent
; i
++) {
944 buf
= grab_blob(elem
->parent
[i
].sha1
,
945 elem
->parent
[i
].mode
,
947 if (buffer_is_binary(buf
, size
))
953 show_combined_header(elem
, num_parent
, dense
, rev
,
954 line_prefix
, mode_differs
, 0);
955 printf("Binary files differ\n");
960 for (cnt
= 0, cp
= result
; cp
< result
+ result_size
; cp
++) {
964 if (result_size
&& result
[result_size
-1] != '\n')
965 cnt
++; /* incomplete line */
967 sline
= xcalloc(cnt
+2, sizeof(*sline
));
968 sline
[0].bol
= result
;
969 for (lno
= 0; lno
<= cnt
+ 1; lno
++) {
970 sline
[lno
].lost_tail
= &sline
[lno
].lost_head
;
973 for (lno
= 0, cp
= result
; cp
< result
+ result_size
; cp
++) {
975 sline
[lno
].len
= cp
- sline
[lno
].bol
;
978 sline
[lno
].bol
= cp
+ 1;
981 if (result_size
&& result
[result_size
-1] != '\n')
982 sline
[cnt
-1].len
= result_size
- (sline
[cnt
-1].bol
- result
);
984 result_file
.ptr
= result
;
985 result_file
.size
= result_size
;
987 /* Even p_lno[cnt+1] is valid -- that is for the end line number
988 * for deletion hunk at the end.
990 sline
[0].p_lno
= xcalloc((cnt
+2) * num_parent
, sizeof(unsigned long));
991 for (lno
= 0; lno
<= cnt
; lno
++)
992 sline
[lno
+1].p_lno
= sline
[lno
].p_lno
+ num_parent
;
994 for (i
= 0; i
< num_parent
; i
++) {
996 for (j
= 0; j
< i
; j
++) {
997 if (!hashcmp(elem
->parent
[i
].sha1
,
998 elem
->parent
[j
].sha1
)) {
999 reuse_combine_diff(sline
, cnt
, i
, j
);
1004 combine_diff(elem
->parent
[i
].sha1
,
1005 elem
->parent
[i
].mode
,
1006 &result_file
, sline
,
1007 cnt
, i
, num_parent
, result_deleted
,
1008 textconv
, elem
->path
, opt
->xdl_opts
);
1011 show_hunks
= make_hunks(sline
, cnt
, num_parent
, dense
);
1013 if (show_hunks
|| mode_differs
|| working_tree_file
) {
1014 show_combined_header(elem
, num_parent
, dense
, rev
,
1015 line_prefix
, mode_differs
, 1);
1016 dump_sline(sline
, line_prefix
, cnt
, num_parent
,
1017 opt
->use_color
, result_deleted
);
1021 for (lno
= 0; lno
< cnt
; lno
++) {
1022 if (sline
[lno
].lost_head
) {
1023 struct lline
*ll
= sline
[lno
].lost_head
;
1025 struct lline
*tmp
= ll
;
1031 free(sline
[0].p_lno
);
1035 static void show_raw_diff(struct combine_diff_path
*p
, int num_parent
, struct rev_info
*rev
)
1037 struct diff_options
*opt
= &rev
->diffopt
;
1038 int line_termination
, inter_name_termination
, i
;
1039 const char *line_prefix
= diff_line_prefix(opt
);
1041 line_termination
= opt
->line_termination
;
1042 inter_name_termination
= '\t';
1043 if (!line_termination
)
1044 inter_name_termination
= 0;
1046 if (rev
->loginfo
&& !rev
->no_commit_id
)
1050 if (opt
->output_format
& DIFF_FORMAT_RAW
) {
1051 printf("%s", line_prefix
);
1053 /* As many colons as there are parents */
1054 for (i
= 0; i
< num_parent
; i
++)
1057 /* Show the modes */
1058 for (i
= 0; i
< num_parent
; i
++)
1059 printf("%06o ", p
->parent
[i
].mode
);
1060 printf("%06o", p
->mode
);
1063 for (i
= 0; i
< num_parent
; i
++)
1064 printf(" %s", diff_unique_abbrev(p
->parent
[i
].sha1
,
1066 printf(" %s ", diff_unique_abbrev(p
->sha1
, opt
->abbrev
));
1069 if (opt
->output_format
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
)) {
1070 for (i
= 0; i
< num_parent
; i
++)
1071 putchar(p
->parent
[i
].status
);
1072 putchar(inter_name_termination
);
1075 write_name_quoted(p
->path
, stdout
, line_termination
);
1079 * The result (p->elem) is from the working tree and their
1080 * parents are typically from multiple stages during a merge
1081 * (i.e. diff-files) or the state in HEAD and in the index
1082 * (i.e. diff-index).
1084 void show_combined_diff(struct combine_diff_path
*p
,
1087 struct rev_info
*rev
)
1089 struct diff_options
*opt
= &rev
->diffopt
;
1093 if (opt
->output_format
& (DIFF_FORMAT_RAW
|
1095 DIFF_FORMAT_NAME_STATUS
))
1096 show_raw_diff(p
, num_parent
, rev
);
1097 else if (opt
->output_format
& DIFF_FORMAT_PATCH
)
1098 show_patch_diff(p
, num_parent
, dense
, 1, rev
);
1101 static void free_combined_pair(struct diff_filepair
*pair
)
1108 * A combine_diff_path expresses N parents on the LHS against 1 merge
1109 * result. Synthesize a diff_filepair that has N entries on the "one"
1110 * side and 1 entry on the "two" side.
1112 * In the future, we might want to add more data to combine_diff_path
1113 * so that we can fill fields we are ignoring (most notably, size) here,
1114 * but currently nobody uses it, so this should suffice for now.
1116 static struct diff_filepair
*combined_pair(struct combine_diff_path
*p
,
1120 struct diff_filepair
*pair
;
1121 struct diff_filespec
*pool
;
1123 pair
= xmalloc(sizeof(*pair
));
1124 pool
= xcalloc(num_parent
+ 1, sizeof(struct diff_filespec
));
1125 pair
->one
= pool
+ 1;
1128 for (i
= 0; i
< num_parent
; i
++) {
1129 pair
->one
[i
].path
= p
->path
;
1130 pair
->one
[i
].mode
= p
->parent
[i
].mode
;
1131 hashcpy(pair
->one
[i
].sha1
, p
->parent
[i
].sha1
);
1132 pair
->one
[i
].sha1_valid
= !is_null_sha1(p
->parent
[i
].sha1
);
1133 pair
->one
[i
].has_more_entries
= 1;
1135 pair
->one
[num_parent
- 1].has_more_entries
= 0;
1137 pair
->two
->path
= p
->path
;
1138 pair
->two
->mode
= p
->mode
;
1139 hashcpy(pair
->two
->sha1
, p
->sha1
);
1140 pair
->two
->sha1_valid
= !is_null_sha1(p
->sha1
);
1144 static void handle_combined_callback(struct diff_options
*opt
,
1145 struct combine_diff_path
*paths
,
1149 struct combine_diff_path
*p
;
1150 struct diff_queue_struct q
;
1153 q
.queue
= xcalloc(num_paths
, sizeof(struct diff_filepair
*));
1154 q
.alloc
= num_paths
;
1156 for (i
= 0, p
= paths
; p
; p
= p
->next
) {
1159 q
.queue
[i
++] = combined_pair(p
, num_parent
);
1161 opt
->format_callback(&q
, opt
, opt
->format_callback_data
);
1162 for (i
= 0; i
< num_paths
; i
++)
1163 free_combined_pair(q
.queue
[i
]);
1167 void diff_tree_combined(const unsigned char *sha1
,
1168 const struct sha1_array
*parents
,
1170 struct rev_info
*rev
)
1172 struct diff_options
*opt
= &rev
->diffopt
;
1173 struct diff_options diffopts
;
1174 struct combine_diff_path
*p
, *paths
= NULL
;
1175 int i
, num_paths
, needsep
, show_log_first
, num_parent
= parents
->nr
;
1178 diffopts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1179 DIFF_OPT_SET(&diffopts
, RECURSIVE
);
1180 DIFF_OPT_CLR(&diffopts
, ALLOW_EXTERNAL
);
1182 show_log_first
= !!rev
->loginfo
&& !rev
->no_commit_id
;
1184 /* find set of paths that everybody touches */
1185 for (i
= 0; i
< num_parent
; i
++) {
1186 /* show stat against the first parent even
1187 * when doing combined diff.
1189 int stat_opt
= (opt
->output_format
&
1190 (DIFF_FORMAT_NUMSTAT
|DIFF_FORMAT_DIFFSTAT
));
1191 if (i
== 0 && stat_opt
)
1192 diffopts
.output_format
= stat_opt
;
1194 diffopts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1195 diff_tree_sha1(parents
->sha1
[i
], sha1
, "", &diffopts
);
1196 diffcore_std(&diffopts
);
1197 paths
= intersect_paths(paths
, i
, num_parent
);
1199 if (show_log_first
&& i
== 0) {
1202 if (rev
->verbose_header
&& opt
->output_format
)
1203 printf("%s%c", diff_line_prefix(opt
),
1204 opt
->line_termination
);
1206 diff_flush(&diffopts
);
1209 /* find out surviving paths */
1210 for (num_paths
= 0, p
= paths
; p
; p
= p
->next
) {
1215 if (opt
->output_format
& (DIFF_FORMAT_RAW
|
1217 DIFF_FORMAT_NAME_STATUS
)) {
1218 for (p
= paths
; p
; p
= p
->next
) {
1220 show_raw_diff(p
, num_parent
, rev
);
1224 else if (opt
->output_format
&
1225 (DIFF_FORMAT_NUMSTAT
|DIFF_FORMAT_DIFFSTAT
))
1227 else if (opt
->output_format
& DIFF_FORMAT_CALLBACK
)
1228 handle_combined_callback(opt
, paths
, num_parent
, num_paths
);
1230 if (opt
->output_format
& DIFF_FORMAT_PATCH
) {
1232 printf("%s%c", diff_line_prefix(opt
),
1233 opt
->line_termination
);
1234 for (p
= paths
; p
; p
= p
->next
) {
1236 show_patch_diff(p
, num_parent
, dense
,
1242 /* Clean things up */
1244 struct combine_diff_path
*tmp
= paths
;
1245 paths
= paths
->next
;
1250 void diff_tree_combined_merge(const struct commit
*commit
, int dense
,
1251 struct rev_info
*rev
)
1253 struct commit_list
*parent
= commit
->parents
;
1254 struct sha1_array parents
= SHA1_ARRAY_INIT
;
1257 sha1_array_append(&parents
, parent
->item
->object
.sha1
);
1258 parent
= parent
->next
;
1260 diff_tree_combined(commit
->object
.sha1
, &parents
, dense
, rev
);
1261 sha1_array_clear(&parents
);