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 */
77 struct lline
*next
, *prev
;
79 unsigned long parent_map
;
80 char line
[FLEX_ARRAY
];
83 /* Lines lost from current parent (before coalescing) */
85 struct lline
*lost_head
, *lost_tail
;
89 /* Lines surviving in the merge result */
91 /* Accumulated and coalesced lost lines */
97 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
98 * we did not change it).
99 * bit N is used for "interesting" lines, including context.
100 * bit (N+1) is used for "do not show deletion before this".
103 unsigned long *p_lno
;
106 static int match_string_spaces(const char *line1
, int len1
,
107 const char *line2
, int len2
,
110 if (flags
& XDF_WHITESPACE_FLAGS
) {
111 for (; len1
> 0 && XDL_ISSPACE(line1
[len1
- 1]); len1
--);
112 for (; len2
> 0 && XDL_ISSPACE(line2
[len2
- 1]); len2
--);
115 if (!(flags
& (XDF_IGNORE_WHITESPACE
| XDF_IGNORE_WHITESPACE_CHANGE
)))
116 return (len1
== len2
&& !memcmp(line1
, line2
, len1
));
118 while (len1
> 0 && len2
> 0) {
121 if (XDL_ISSPACE(line1
[len1
]) || XDL_ISSPACE(line2
[len2
])) {
122 if ((flags
& XDF_IGNORE_WHITESPACE_CHANGE
) &&
123 (!XDL_ISSPACE(line1
[len1
]) || !XDL_ISSPACE(line2
[len2
])))
126 for (; len1
> 0 && XDL_ISSPACE(line1
[len1
]); len1
--);
127 for (; len2
> 0 && XDL_ISSPACE(line2
[len2
]); len2
--);
129 if (line1
[len1
] != line2
[len2
])
133 if (flags
& XDF_IGNORE_WHITESPACE
) {
134 /* Consume remaining spaces */
135 for (; len1
> 0 && XDL_ISSPACE(line1
[len1
- 1]); len1
--);
136 for (; len2
> 0 && XDL_ISSPACE(line2
[len2
- 1]); len2
--);
139 /* We matched full line1 and line2 */
146 enum coalesce_direction
{ MATCH
, BASE
, NEW
};
148 /* Coalesce new lines into base by finding LCS */
149 static struct lline
*coalesce_lines(struct lline
*base
, int *lenbase
,
150 struct lline
*new, int lennew
,
151 unsigned long parent
, long flags
)
154 enum coalesce_direction
**directions
;
155 struct lline
*baseend
, *newend
= NULL
;
156 int i
, j
, origbaselen
= *lenbase
;
167 * Coalesce new lines into base by finding the LCS
168 * - Create the table to run dynamic programing
170 * - Then reverse read the direction structure:
171 * - If we have MATCH, assign parent to base flag, and consume
172 * both baseend and newend
173 * - Else if we have BASE, consume baseend
174 * - Else if we have NEW, insert newend lline into base and
177 lcs
= xcalloc(origbaselen
+ 1, sizeof(int*));
178 directions
= xcalloc(origbaselen
+ 1, sizeof(enum coalesce_direction
*));
179 for (i
= 0; i
< origbaselen
+ 1; i
++) {
180 lcs
[i
] = xcalloc(lennew
+ 1, sizeof(int));
181 directions
[i
] = xcalloc(lennew
+ 1, sizeof(enum coalesce_direction
));
182 directions
[i
][0] = BASE
;
184 for (j
= 1; j
< lennew
+ 1; j
++)
185 directions
[0][j
] = NEW
;
187 for (i
= 1, baseend
= base
; i
< origbaselen
+ 1; i
++) {
188 for (j
= 1, newend
= new; j
< lennew
+ 1; j
++) {
189 if (match_string_spaces(baseend
->line
, baseend
->len
,
190 newend
->line
, newend
->len
, flags
)) {
191 lcs
[i
][j
] = lcs
[i
- 1][j
- 1] + 1;
192 directions
[i
][j
] = MATCH
;
193 } else if (lcs
[i
][j
- 1] >= lcs
[i
- 1][j
]) {
194 lcs
[i
][j
] = lcs
[i
][j
- 1];
195 directions
[i
][j
] = NEW
;
197 lcs
[i
][j
] = lcs
[i
- 1][j
];
198 directions
[i
][j
] = BASE
;
201 newend
= newend
->next
;
204 baseend
= baseend
->next
;
207 for (i
= 0; i
< origbaselen
+ 1; i
++)
211 /* At this point, baseend and newend point to the end of each lists */
214 while (i
!= 0 || j
!= 0) {
215 if (directions
[i
][j
] == MATCH
) {
216 baseend
->parent_map
|= 1<<parent
;
217 baseend
= baseend
->prev
;
218 newend
= newend
->prev
;
221 } else if (directions
[i
][j
] == NEW
) {
225 /* Remove lline from new list and update newend */
227 lline
->prev
->next
= lline
->next
;
231 lline
->next
->prev
= lline
->prev
;
233 newend
= lline
->prev
;
236 /* Add lline to base list */
238 lline
->next
= baseend
->next
;
239 lline
->prev
= baseend
;
241 lline
->prev
->next
= lline
;
250 lline
->next
->prev
= lline
;
253 baseend
= baseend
->prev
;
260 struct lline
*lline
= newend
;
261 newend
= newend
->next
;
265 for (i
= 0; i
< origbaselen
+ 1; i
++)
272 static char *grab_blob(const unsigned char *sha1
, unsigned int mode
,
273 unsigned long *size
, struct userdiff_driver
*textconv
,
277 enum object_type type
;
279 if (S_ISGITLINK(mode
)) {
281 *size
= snprintf(blob
, 100,
282 "Subproject commit %s\n", sha1_to_hex(sha1
));
283 } else if (is_null_sha1(sha1
)) {
286 return xcalloc(1, 1);
287 } else if (textconv
) {
288 struct diff_filespec
*df
= alloc_filespec(path
);
289 fill_filespec(df
, sha1
, 1, mode
);
290 *size
= fill_textconv(textconv
, df
, &blob
);
293 blob
= read_sha1_file(sha1
, &type
, size
);
294 if (type
!= OBJ_BLOB
)
295 die("object '%s' is not a blob!", sha1_to_hex(sha1
));
300 static void append_lost(struct sline
*sline
, int n
, const char *line
, int len
)
303 unsigned long this_mask
= (1UL<<n
);
304 if (line
[len
-1] == '\n')
307 lline
= xmalloc(sizeof(*lline
) + len
+ 1);
310 lline
->prev
= sline
->plost
.lost_tail
;
312 lline
->prev
->next
= lline
;
314 sline
->plost
.lost_head
= lline
;
315 sline
->plost
.lost_tail
= lline
;
317 lline
->parent_map
= this_mask
;
318 memcpy(lline
->line
, line
, len
);
319 lline
->line
[len
] = 0;
322 struct combine_diff_state
{
329 struct sline
*lost_bucket
;
332 static void consume_line(void *state_
, char *line
, unsigned long len
)
334 struct combine_diff_state
*state
= state_
;
335 if (5 < len
&& !memcmp("@@ -", line
, 4)) {
336 if (parse_hunk_header(line
, len
,
337 &state
->ob
, &state
->on
,
338 &state
->nb
, &state
->nn
))
340 state
->lno
= state
->nb
;
341 if (state
->nn
== 0) {
342 /* @@ -X,Y +N,0 @@ removed Y lines
343 * that would have come *after* line N
344 * in the result. Our lost buckets hang
345 * to the line after the removed lines,
347 * Note that this is correct even when N == 0,
348 * in which case the hunk removes the first
351 state
->lost_bucket
= &state
->sline
[state
->nb
];
355 state
->lost_bucket
= &state
->sline
[state
->nb
-1];
357 if (!state
->sline
[state
->nb
-1].p_lno
)
358 state
->sline
[state
->nb
-1].p_lno
=
359 xcalloc(state
->num_parent
,
360 sizeof(unsigned long));
361 state
->sline
[state
->nb
-1].p_lno
[state
->n
] = state
->ob
;
364 if (!state
->lost_bucket
)
365 return; /* not in any hunk yet */
368 append_lost(state
->lost_bucket
, state
->n
, line
+1, len
-1);
371 state
->sline
[state
->lno
-1].flag
|= state
->nmask
;
377 static void combine_diff(const unsigned char *parent
, unsigned int mode
,
378 mmfile_t
*result_file
,
379 struct sline
*sline
, unsigned int cnt
, int n
,
380 int num_parent
, int result_deleted
,
381 struct userdiff_driver
*textconv
,
382 const char *path
, long flags
)
384 unsigned int p_lno
, lno
;
385 unsigned long nmask
= (1UL << n
);
388 mmfile_t parent_file
;
389 struct combine_diff_state state
;
393 return; /* result deleted */
395 parent_file
.ptr
= grab_blob(parent
, mode
, &sz
, textconv
, path
);
396 parent_file
.size
= sz
;
397 memset(&xpp
, 0, sizeof(xpp
));
399 memset(&xecfg
, 0, sizeof(xecfg
));
400 memset(&state
, 0, sizeof(state
));
404 state
.num_parent
= num_parent
;
407 xdi_diff_outf(&parent_file
, result_file
, consume_line
, &state
,
409 free(parent_file
.ptr
);
411 /* Assign line numbers for this parent.
413 * sline[lno].p_lno[n] records the first line number
414 * (counting from 1) for parent N if the final hunk display
415 * started by showing sline[lno] (possibly showing the lost
416 * lines attached to it first).
418 for (lno
= 0, p_lno
= 1; lno
<= cnt
; lno
++) {
420 sline
[lno
].p_lno
[n
] = p_lno
;
422 /* Coalesce new lines */
423 if (sline
[lno
].plost
.lost_head
) {
424 struct sline
*sl
= &sline
[lno
];
425 sl
->lost
= coalesce_lines(sl
->lost
, &sl
->lenlost
,
427 sl
->plost
.len
, n
, flags
);
428 sl
->plost
.lost_head
= sl
->plost
.lost_tail
= NULL
;
432 /* How many lines would this sline advance the p_lno? */
433 ll
= sline
[lno
].lost
;
435 if (ll
->parent_map
& nmask
)
436 p_lno
++; /* '-' means parent had it */
439 if (lno
< cnt
&& !(sline
[lno
].flag
& nmask
))
440 p_lno
++; /* no '+' means parent had it */
442 sline
[lno
].p_lno
[n
] = p_lno
; /* trailer */
445 static unsigned long context
= 3;
446 static char combine_marker
= '@';
448 static int interesting(struct sline
*sline
, unsigned long all_mask
)
450 /* If some parents lost lines here, or if we have added to
451 * some parent, it is interesting.
453 return ((sline
->flag
& all_mask
) || sline
->lost
);
456 static unsigned long adjust_hunk_tail(struct sline
*sline
,
457 unsigned long all_mask
,
458 unsigned long hunk_begin
,
461 /* i points at the first uninteresting line. If the last line
462 * of the hunk was interesting only because it has some
463 * deletion, then it is not all that interesting for the
464 * purpose of giving trailing context lines. This is because
465 * we output '-' line and then unmodified sline[i-1] itself in
466 * that case which gives us one extra context line.
468 if ((hunk_begin
+ 1 <= i
) && !(sline
[i
-1].flag
& all_mask
))
473 static unsigned long find_next(struct sline
*sline
,
477 int look_for_uninteresting
)
479 /* We have examined up to i-1 and are about to look at i.
480 * Find next interesting or uninteresting line. Here,
481 * "interesting" does not mean interesting(), but marked by
482 * the give_context() function below (i.e. it includes context
483 * lines that are not interesting to interesting() function
484 * that are surrounded by interesting() ones.
487 if (look_for_uninteresting
488 ? !(sline
[i
].flag
& mark
)
489 : (sline
[i
].flag
& mark
))
496 static int give_context(struct sline
*sline
, unsigned long cnt
, int num_parent
)
498 unsigned long all_mask
= (1UL<<num_parent
) - 1;
499 unsigned long mark
= (1UL<<num_parent
);
500 unsigned long no_pre_delete
= (2UL<<num_parent
);
503 /* Two groups of interesting lines may have a short gap of
504 * uninteresting lines. Connect such groups to give them a
507 * We first start from what the interesting() function says,
508 * and mark them with "mark", and paint context lines with the
509 * mark. So interesting() would still say false for such context
510 * lines but they are treated as "interesting" in the end.
512 i
= find_next(sline
, mark
, 0, cnt
, 0);
517 unsigned long j
= (context
< i
) ? (i
- context
) : 0;
520 /* Paint a few lines before the first interesting line. */
522 sline
[j
++].flag
|= mark
| no_pre_delete
;
525 /* we know up to i is to be included. where does the
526 * next uninteresting one start?
528 j
= find_next(sline
, mark
, i
, cnt
, 1);
530 break; /* the rest are all interesting */
532 /* lookahead context lines */
533 k
= find_next(sline
, mark
, j
, cnt
, 0);
534 j
= adjust_hunk_tail(sline
, all_mask
, i
, j
);
536 if (k
< j
+ context
) {
537 /* k is interesting and [j,k) are not, but
538 * paint them interesting because the gap is small.
541 sline
[j
++].flag
|= mark
;
546 /* j is the first uninteresting line and there is
547 * no overlap beyond it within context lines. Paint
548 * the trailing edge a bit.
551 k
= (j
+ context
< cnt
+1) ? j
+ context
: cnt
+1;
553 sline
[j
++].flag
|= mark
;
558 static int make_hunks(struct sline
*sline
, unsigned long cnt
,
559 int num_parent
, int dense
)
561 unsigned long all_mask
= (1UL<<num_parent
) - 1;
562 unsigned long mark
= (1UL<<num_parent
);
564 int has_interesting
= 0;
566 for (i
= 0; i
<= cnt
; i
++) {
567 if (interesting(&sline
[i
], all_mask
))
568 sline
[i
].flag
|= mark
;
570 sline
[i
].flag
&= ~mark
;
573 return give_context(sline
, cnt
, num_parent
);
575 /* Look at each hunk, and if we have changes from only one
576 * parent, or the changes are the same from all but one
577 * parent, mark that uninteresting.
581 unsigned long j
, hunk_begin
, hunk_end
;
582 unsigned long same_diff
;
583 while (i
<= cnt
&& !(sline
[i
].flag
& mark
))
586 break; /* No more interesting hunks */
588 for (j
= i
+ 1; j
<= cnt
; j
++) {
589 if (!(sline
[j
].flag
& mark
)) {
590 /* Look beyond the end to see if there
591 * is an interesting line after this
592 * hunk within context span.
594 unsigned long la
; /* lookahead */
596 la
= adjust_hunk_tail(sline
, all_mask
,
598 la
= (la
+ context
< cnt
+ 1) ?
599 (la
+ context
) : cnt
+ 1;
600 while (la
&& j
<= --la
) {
601 if (sline
[la
].flag
& mark
) {
613 /* [i..hunk_end) are interesting. Now is it really
614 * interesting? We check if there are only two versions
615 * and the result matches one of them. That is, we look
617 * (+) line, which records lines added to which parents;
618 * this line appears in the result.
619 * (-) line, which records from what parents the line
620 * was removed; this line does not appear in the result.
621 * then check the set of parents the result has difference
622 * from, from all lines. If there are lines that has
623 * different set of parents that the result has differences
624 * from, that means we have more than two versions.
626 * Even when we have only two versions, if the result does
627 * not match any of the parents, the it should be considered
628 * interesting. In such a case, we would have all '+' line.
629 * After passing the above "two versions" test, that would
630 * appear as "the same set of parents" to be "all parents".
634 for (j
= i
; j
< hunk_end
&& !has_interesting
; j
++) {
635 unsigned long this_diff
= sline
[j
].flag
& all_mask
;
636 struct lline
*ll
= sline
[j
].lost
;
638 /* This has some changes. Is it the
642 same_diff
= this_diff
;
643 else if (same_diff
!= this_diff
) {
648 while (ll
&& !has_interesting
) {
649 /* Lost this line from these parents;
650 * who are they? Are they the same?
652 this_diff
= ll
->parent_map
;
654 same_diff
= this_diff
;
655 else if (same_diff
!= this_diff
) {
662 if (!has_interesting
&& same_diff
!= all_mask
) {
663 /* This hunk is not that interesting after all */
664 for (j
= hunk_begin
; j
< hunk_end
; j
++)
665 sline
[j
].flag
&= ~mark
;
670 has_interesting
= give_context(sline
, cnt
, num_parent
);
671 return has_interesting
;
674 static void show_parent_lno(struct sline
*sline
, unsigned long l0
, unsigned long l1
, int n
, unsigned long null_context
)
676 l0
= sline
[l0
].p_lno
[n
];
677 l1
= sline
[l1
].p_lno
[n
];
678 printf(" -%lu,%lu", l0
, l1
-l0
-null_context
);
681 static int hunk_comment_line(const char *bol
)
688 return (isalpha(ch
) || ch
== '_' || ch
== '$');
691 static void show_line_to_eol(const char *line
, int len
, const char *reset
)
693 int saw_cr_at_eol
= 0;
696 saw_cr_at_eol
= (len
&& line
[len
-1] == '\r');
698 printf("%.*s%s%s\n", len
- saw_cr_at_eol
, line
,
700 saw_cr_at_eol
? "\r" : "");
703 static void dump_sline(struct sline
*sline
, const char *line_prefix
,
704 unsigned long cnt
, int num_parent
,
705 int use_color
, int result_deleted
)
707 unsigned long mark
= (1UL<<num_parent
);
708 unsigned long no_pre_delete
= (2UL<<num_parent
);
710 unsigned long lno
= 0;
711 const char *c_frag
= diff_get_color(use_color
, DIFF_FRAGINFO
);
712 const char *c_func
= diff_get_color(use_color
, DIFF_FUNCINFO
);
713 const char *c_new
= diff_get_color(use_color
, DIFF_FILE_NEW
);
714 const char *c_old
= diff_get_color(use_color
, DIFF_FILE_OLD
);
715 const char *c_plain
= diff_get_color(use_color
, DIFF_PLAIN
);
716 const char *c_reset
= diff_get_color(use_color
, DIFF_RESET
);
719 return; /* result deleted */
722 unsigned long hunk_end
;
723 unsigned long rlines
;
724 const char *hunk_comment
= NULL
;
725 unsigned long null_context
= 0;
727 while (lno
<= cnt
&& !(sline
[lno
].flag
& mark
)) {
728 if (hunk_comment_line(sline
[lno
].bol
))
729 hunk_comment
= sline
[lno
].bol
;
735 for (hunk_end
= lno
+ 1; hunk_end
<= cnt
; hunk_end
++)
736 if (!(sline
[hunk_end
].flag
& mark
))
739 rlines
= hunk_end
- lno
;
741 rlines
--; /* pointing at the last delete hunk */
745 * Even when running with --unified=0, all
746 * lines in the hunk needs to be processed in
747 * the loop below in order to show the
748 * deletion recorded in lost_head. However,
749 * we do not want to show the resulting line
750 * with all blank context markers in such a
754 for (j
= lno
; j
< hunk_end
; j
++)
755 if (!(sline
[j
].flag
& (mark
-1)))
757 rlines
-= null_context
;
760 printf("%s%s", line_prefix
, c_frag
);
761 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
762 for (i
= 0; i
< num_parent
; i
++)
763 show_parent_lno(sline
, lno
, hunk_end
, i
, null_context
);
764 printf(" +%lu,%lu ", lno
+1, rlines
);
765 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
769 for (i
= 0; i
< 40; i
++) {
770 int ch
= hunk_comment
[i
] & 0xff;
771 if (!ch
|| ch
== '\n')
777 printf("%s%s %s%s", c_reset
,
780 for (i
= 0; i
< comment_end
; i
++)
781 putchar(hunk_comment
[i
]);
784 printf("%s\n", c_reset
);
785 while (lno
< hunk_end
) {
788 unsigned long p_mask
;
789 struct sline
*sl
= &sline
[lno
++];
790 ll
= (sl
->flag
& no_pre_delete
) ? NULL
: sl
->lost
;
792 printf("%s%s", line_prefix
, c_old
);
793 for (j
= 0; j
< num_parent
; j
++) {
794 if (ll
->parent_map
& (1UL<<j
))
799 show_line_to_eol(ll
->line
, -1, c_reset
);
805 fputs(line_prefix
, stdout
);
806 if (!(sl
->flag
& (mark
-1))) {
808 * This sline was here to hang the
809 * lost lines in front of it.
813 fputs(c_plain
, stdout
);
816 fputs(c_new
, stdout
);
817 for (j
= 0; j
< num_parent
; j
++) {
818 if (p_mask
& sl
->flag
)
824 show_line_to_eol(sl
->bol
, sl
->len
, c_reset
);
829 static void reuse_combine_diff(struct sline
*sline
, unsigned long cnt
,
832 /* We have already examined parent j and we know parent i
833 * and parent j are the same, so reuse the combined result
834 * of parent j for parent i.
836 unsigned long lno
, imask
, jmask
;
840 for (lno
= 0; lno
<= cnt
; lno
++) {
841 struct lline
*ll
= sline
->lost
;
842 sline
->p_lno
[i
] = sline
->p_lno
[j
];
844 if (ll
->parent_map
& jmask
)
845 ll
->parent_map
|= imask
;
848 if (sline
->flag
& jmask
)
849 sline
->flag
|= imask
;
852 /* the overall size of the file (sline[cnt]) */
853 sline
->p_lno
[i
] = sline
->p_lno
[j
];
856 static void dump_quoted_path(const char *head
,
859 const char *line_prefix
,
860 const char *c_meta
, const char *c_reset
)
862 static struct strbuf buf
= STRBUF_INIT
;
865 strbuf_addstr(&buf
, line_prefix
);
866 strbuf_addstr(&buf
, c_meta
);
867 strbuf_addstr(&buf
, head
);
868 quote_two_c_style(&buf
, prefix
, path
, 0);
869 strbuf_addstr(&buf
, c_reset
);
873 static void show_combined_header(struct combine_diff_path
*elem
,
876 struct rev_info
*rev
,
877 const char *line_prefix
,
879 int show_file_header
)
881 struct diff_options
*opt
= &rev
->diffopt
;
882 int abbrev
= DIFF_OPT_TST(opt
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
883 const char *a_prefix
= opt
->a_prefix
? opt
->a_prefix
: "a/";
884 const char *b_prefix
= opt
->b_prefix
? opt
->b_prefix
: "b/";
885 const char *c_meta
= diff_get_color_opt(opt
, DIFF_METAINFO
);
886 const char *c_reset
= diff_get_color_opt(opt
, DIFF_RESET
);
892 if (rev
->loginfo
&& !rev
->no_commit_id
)
895 dump_quoted_path(dense
? "diff --cc " : "diff --combined ",
896 "", elem
->path
, line_prefix
, c_meta
, c_reset
);
897 printf("%s%sindex ", line_prefix
, c_meta
);
898 for (i
= 0; i
< num_parent
; i
++) {
899 abb
= find_unique_abbrev(elem
->parent
[i
].sha1
,
901 printf("%s%s", i
? "," : "", abb
);
903 abb
= find_unique_abbrev(elem
->sha1
, abbrev
);
904 printf("..%s%s\n", abb
, c_reset
);
907 deleted
= !elem
->mode
;
909 /* We say it was added if nobody had it */
911 for (i
= 0; added
&& i
< num_parent
; i
++)
912 if (elem
->parent
[i
].status
!=
916 printf("%s%snew file mode %06o",
917 line_prefix
, c_meta
, elem
->mode
);
920 printf("%s%sdeleted file ",
921 line_prefix
, c_meta
);
923 for (i
= 0; i
< num_parent
; i
++) {
924 printf("%s%06o", i
? "," : "",
925 elem
->parent
[i
].mode
);
928 printf("..%06o", elem
->mode
);
930 printf("%s\n", c_reset
);
933 if (!show_file_header
)
937 dump_quoted_path("--- ", "", "/dev/null",
938 line_prefix
, c_meta
, c_reset
);
940 dump_quoted_path("--- ", a_prefix
, elem
->path
,
941 line_prefix
, c_meta
, c_reset
);
943 dump_quoted_path("+++ ", "", "/dev/null",
944 line_prefix
, c_meta
, c_reset
);
946 dump_quoted_path("+++ ", b_prefix
, elem
->path
,
947 line_prefix
, c_meta
, c_reset
);
950 static void show_patch_diff(struct combine_diff_path
*elem
, int num_parent
,
951 int dense
, int working_tree_file
,
952 struct rev_info
*rev
)
954 struct diff_options
*opt
= &rev
->diffopt
;
955 unsigned long result_size
, cnt
, lno
;
956 int result_deleted
= 0;
958 struct sline
*sline
; /* survived lines */
959 int mode_differs
= 0;
961 mmfile_t result_file
;
962 struct userdiff_driver
*userdiff
;
963 struct userdiff_driver
*textconv
= NULL
;
965 const char *line_prefix
= diff_line_prefix(opt
);
967 context
= opt
->context
;
968 userdiff
= userdiff_find_by_path(elem
->path
);
970 userdiff
= userdiff_find_by_name("default");
971 if (DIFF_OPT_TST(opt
, ALLOW_TEXTCONV
))
972 textconv
= userdiff_get_textconv(userdiff
);
974 /* Read the result of merge first */
975 if (!working_tree_file
)
976 result
= grab_blob(elem
->sha1
, elem
->mode
, &result_size
,
977 textconv
, elem
->path
);
979 /* Used by diff-tree to read from the working tree */
983 if (lstat(elem
->path
, &st
) < 0)
986 if (S_ISLNK(st
.st_mode
)) {
987 struct strbuf buf
= STRBUF_INIT
;
989 if (strbuf_readlink(&buf
, elem
->path
, st
.st_size
) < 0) {
990 error("readlink(%s): %s", elem
->path
,
994 result_size
= buf
.len
;
995 result
= strbuf_detach(&buf
, NULL
);
996 elem
->mode
= canon_mode(st
.st_mode
);
997 } else if (S_ISDIR(st
.st_mode
)) {
998 unsigned char sha1
[20];
999 if (resolve_gitlink_ref(elem
->path
, "HEAD", sha1
) < 0)
1000 result
= grab_blob(elem
->sha1
, elem
->mode
,
1001 &result_size
, NULL
, NULL
);
1003 result
= grab_blob(sha1
, elem
->mode
,
1004 &result_size
, NULL
, NULL
);
1005 } else if (textconv
) {
1006 struct diff_filespec
*df
= alloc_filespec(elem
->path
);
1007 fill_filespec(df
, null_sha1
, 0, st
.st_mode
);
1008 result_size
= fill_textconv(textconv
, df
, &result
);
1010 } else if (0 <= (fd
= open(elem
->path
, O_RDONLY
))) {
1011 size_t len
= xsize_t(st
.st_size
);
1015 elem
->mode
= canon_mode(st
.st_mode
);
1016 /* if symlinks don't work, assume symlink if all parents
1019 is_file
= has_symlinks
;
1020 for (i
= 0; !is_file
&& i
< num_parent
; i
++)
1021 is_file
= !S_ISLNK(elem
->parent
[i
].mode
);
1023 elem
->mode
= canon_mode(S_IFLNK
);
1026 result
= xmalloc(len
+ 1);
1028 done
= read_in_full(fd
, result
, len
);
1030 die_errno("read error '%s'", elem
->path
);
1031 else if (done
< len
)
1032 die("early EOF '%s'", elem
->path
);
1036 /* If not a fake symlink, apply filters, e.g. autocrlf */
1038 struct strbuf buf
= STRBUF_INIT
;
1040 if (convert_to_git(elem
->path
, result
, len
, &buf
, safe_crlf
)) {
1042 result
= strbuf_detach(&buf
, &len
);
1052 result
= xcalloc(1, 1);
1059 for (i
= 0; i
< num_parent
; i
++) {
1060 if (elem
->parent
[i
].mode
!= elem
->mode
) {
1068 else if (userdiff
->binary
!= -1)
1069 is_binary
= userdiff
->binary
;
1071 is_binary
= buffer_is_binary(result
, result_size
);
1072 for (i
= 0; !is_binary
&& i
< num_parent
; i
++) {
1075 buf
= grab_blob(elem
->parent
[i
].sha1
,
1076 elem
->parent
[i
].mode
,
1078 if (buffer_is_binary(buf
, size
))
1084 show_combined_header(elem
, num_parent
, dense
, rev
,
1085 line_prefix
, mode_differs
, 0);
1086 printf("Binary files differ\n");
1091 for (cnt
= 0, cp
= result
; cp
< result
+ result_size
; cp
++) {
1095 if (result_size
&& result
[result_size
-1] != '\n')
1096 cnt
++; /* incomplete line */
1098 sline
= xcalloc(cnt
+2, sizeof(*sline
));
1099 sline
[0].bol
= result
;
1100 for (lno
= 0, cp
= result
; cp
< result
+ result_size
; cp
++) {
1102 sline
[lno
].len
= cp
- sline
[lno
].bol
;
1105 sline
[lno
].bol
= cp
+ 1;
1108 if (result_size
&& result
[result_size
-1] != '\n')
1109 sline
[cnt
-1].len
= result_size
- (sline
[cnt
-1].bol
- result
);
1111 result_file
.ptr
= result
;
1112 result_file
.size
= result_size
;
1114 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1115 * for deletion hunk at the end.
1117 sline
[0].p_lno
= xcalloc((cnt
+2) * num_parent
, sizeof(unsigned long));
1118 for (lno
= 0; lno
<= cnt
; lno
++)
1119 sline
[lno
+1].p_lno
= sline
[lno
].p_lno
+ num_parent
;
1121 for (i
= 0; i
< num_parent
; i
++) {
1123 for (j
= 0; j
< i
; j
++) {
1124 if (!hashcmp(elem
->parent
[i
].sha1
,
1125 elem
->parent
[j
].sha1
)) {
1126 reuse_combine_diff(sline
, cnt
, i
, j
);
1131 combine_diff(elem
->parent
[i
].sha1
,
1132 elem
->parent
[i
].mode
,
1133 &result_file
, sline
,
1134 cnt
, i
, num_parent
, result_deleted
,
1135 textconv
, elem
->path
, opt
->xdl_opts
);
1138 show_hunks
= make_hunks(sline
, cnt
, num_parent
, dense
);
1140 if (show_hunks
|| mode_differs
|| working_tree_file
) {
1141 show_combined_header(elem
, num_parent
, dense
, rev
,
1142 line_prefix
, mode_differs
, 1);
1143 dump_sline(sline
, line_prefix
, cnt
, num_parent
,
1144 opt
->use_color
, result_deleted
);
1148 for (lno
= 0; lno
< cnt
; lno
++) {
1149 if (sline
[lno
].lost
) {
1150 struct lline
*ll
= sline
[lno
].lost
;
1152 struct lline
*tmp
= ll
;
1158 free(sline
[0].p_lno
);
1162 static void show_raw_diff(struct combine_diff_path
*p
, int num_parent
, struct rev_info
*rev
)
1164 struct diff_options
*opt
= &rev
->diffopt
;
1165 int line_termination
, inter_name_termination
, i
;
1166 const char *line_prefix
= diff_line_prefix(opt
);
1168 line_termination
= opt
->line_termination
;
1169 inter_name_termination
= '\t';
1170 if (!line_termination
)
1171 inter_name_termination
= 0;
1173 if (rev
->loginfo
&& !rev
->no_commit_id
)
1177 if (opt
->output_format
& DIFF_FORMAT_RAW
) {
1178 printf("%s", line_prefix
);
1180 /* As many colons as there are parents */
1181 for (i
= 0; i
< num_parent
; i
++)
1184 /* Show the modes */
1185 for (i
= 0; i
< num_parent
; i
++)
1186 printf("%06o ", p
->parent
[i
].mode
);
1187 printf("%06o", p
->mode
);
1190 for (i
= 0; i
< num_parent
; i
++)
1191 printf(" %s", diff_unique_abbrev(p
->parent
[i
].sha1
,
1193 printf(" %s ", diff_unique_abbrev(p
->sha1
, opt
->abbrev
));
1196 if (opt
->output_format
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
)) {
1197 for (i
= 0; i
< num_parent
; i
++)
1198 putchar(p
->parent
[i
].status
);
1199 putchar(inter_name_termination
);
1202 write_name_quoted(p
->path
, stdout
, line_termination
);
1206 * The result (p->elem) is from the working tree and their
1207 * parents are typically from multiple stages during a merge
1208 * (i.e. diff-files) or the state in HEAD and in the index
1209 * (i.e. diff-index).
1211 void show_combined_diff(struct combine_diff_path
*p
,
1214 struct rev_info
*rev
)
1216 struct diff_options
*opt
= &rev
->diffopt
;
1220 if (opt
->output_format
& (DIFF_FORMAT_RAW
|
1222 DIFF_FORMAT_NAME_STATUS
))
1223 show_raw_diff(p
, num_parent
, rev
);
1224 else if (opt
->output_format
& DIFF_FORMAT_PATCH
)
1225 show_patch_diff(p
, num_parent
, dense
, 1, rev
);
1228 static void free_combined_pair(struct diff_filepair
*pair
)
1235 * A combine_diff_path expresses N parents on the LHS against 1 merge
1236 * result. Synthesize a diff_filepair that has N entries on the "one"
1237 * side and 1 entry on the "two" side.
1239 * In the future, we might want to add more data to combine_diff_path
1240 * so that we can fill fields we are ignoring (most notably, size) here,
1241 * but currently nobody uses it, so this should suffice for now.
1243 static struct diff_filepair
*combined_pair(struct combine_diff_path
*p
,
1247 struct diff_filepair
*pair
;
1248 struct diff_filespec
*pool
;
1250 pair
= xmalloc(sizeof(*pair
));
1251 pool
= xcalloc(num_parent
+ 1, sizeof(struct diff_filespec
));
1252 pair
->one
= pool
+ 1;
1255 for (i
= 0; i
< num_parent
; i
++) {
1256 pair
->one
[i
].path
= p
->path
;
1257 pair
->one
[i
].mode
= p
->parent
[i
].mode
;
1258 hashcpy(pair
->one
[i
].sha1
, p
->parent
[i
].sha1
);
1259 pair
->one
[i
].sha1_valid
= !is_null_sha1(p
->parent
[i
].sha1
);
1260 pair
->one
[i
].has_more_entries
= 1;
1262 pair
->one
[num_parent
- 1].has_more_entries
= 0;
1264 pair
->two
->path
= p
->path
;
1265 pair
->two
->mode
= p
->mode
;
1266 hashcpy(pair
->two
->sha1
, p
->sha1
);
1267 pair
->two
->sha1_valid
= !is_null_sha1(p
->sha1
);
1271 static void handle_combined_callback(struct diff_options
*opt
,
1272 struct combine_diff_path
*paths
,
1276 struct combine_diff_path
*p
;
1277 struct diff_queue_struct q
;
1280 q
.queue
= xcalloc(num_paths
, sizeof(struct diff_filepair
*));
1281 q
.alloc
= num_paths
;
1283 for (i
= 0, p
= paths
; p
; p
= p
->next
) {
1286 q
.queue
[i
++] = combined_pair(p
, num_parent
);
1288 opt
->format_callback(&q
, opt
, opt
->format_callback_data
);
1289 for (i
= 0; i
< num_paths
; i
++)
1290 free_combined_pair(q
.queue
[i
]);
1294 void diff_tree_combined(const unsigned char *sha1
,
1295 const struct sha1_array
*parents
,
1297 struct rev_info
*rev
)
1299 struct diff_options
*opt
= &rev
->diffopt
;
1300 struct diff_options diffopts
;
1301 struct combine_diff_path
*p
, *paths
= NULL
;
1302 int i
, num_paths
, needsep
, show_log_first
, num_parent
= parents
->nr
;
1305 diffopts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1306 DIFF_OPT_SET(&diffopts
, RECURSIVE
);
1307 DIFF_OPT_CLR(&diffopts
, ALLOW_EXTERNAL
);
1309 show_log_first
= !!rev
->loginfo
&& !rev
->no_commit_id
;
1311 /* find set of paths that everybody touches */
1312 for (i
= 0; i
< num_parent
; i
++) {
1313 /* show stat against the first parent even
1314 * when doing combined diff.
1316 int stat_opt
= (opt
->output_format
&
1317 (DIFF_FORMAT_NUMSTAT
|DIFF_FORMAT_DIFFSTAT
));
1318 if (i
== 0 && stat_opt
)
1319 diffopts
.output_format
= stat_opt
;
1321 diffopts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1322 diff_tree_sha1(parents
->sha1
[i
], sha1
, "", &diffopts
);
1323 diffcore_std(&diffopts
);
1324 paths
= intersect_paths(paths
, i
, num_parent
);
1326 if (show_log_first
&& i
== 0) {
1329 if (rev
->verbose_header
&& opt
->output_format
)
1330 printf("%s%c", diff_line_prefix(opt
),
1331 opt
->line_termination
);
1333 diff_flush(&diffopts
);
1336 /* find out surviving paths */
1337 for (num_paths
= 0, p
= paths
; p
; p
= p
->next
) {
1342 if (opt
->output_format
& (DIFF_FORMAT_RAW
|
1344 DIFF_FORMAT_NAME_STATUS
)) {
1345 for (p
= paths
; p
; p
= p
->next
) {
1347 show_raw_diff(p
, num_parent
, rev
);
1351 else if (opt
->output_format
&
1352 (DIFF_FORMAT_NUMSTAT
|DIFF_FORMAT_DIFFSTAT
))
1354 else if (opt
->output_format
& DIFF_FORMAT_CALLBACK
)
1355 handle_combined_callback(opt
, paths
, num_parent
, num_paths
);
1357 if (opt
->output_format
& DIFF_FORMAT_PATCH
) {
1359 printf("%s%c", diff_line_prefix(opt
),
1360 opt
->line_termination
);
1361 for (p
= paths
; p
; p
= p
->next
) {
1363 show_patch_diff(p
, num_parent
, dense
,
1369 /* Clean things up */
1371 struct combine_diff_path
*tmp
= paths
;
1372 paths
= paths
->next
;
1377 void diff_tree_combined_merge(const struct commit
*commit
, int dense
,
1378 struct rev_info
*rev
)
1380 struct commit_list
*parent
= commit
->parents
;
1381 struct sha1_array parents
= SHA1_ARRAY_INIT
;
1384 sha1_array_append(&parents
, parent
->item
->object
.sha1
);
1385 parent
= parent
->next
;
1387 diff_tree_combined(commit
->object
.sha1
, &parents
, dense
, rev
);
1388 sha1_array_clear(&parents
);