7 #include "xdiff-interface.h"
8 #include "xdiff/xmacros.h"
12 #include "sha1-array.h"
15 static int compare_paths(const struct combine_diff_path
*one
,
16 const struct diff_filespec
*two
)
18 if (!S_ISDIR(one
->mode
) && !S_ISDIR(two
->mode
))
19 return strcmp(one
->path
, two
->path
);
21 return base_name_compare(one
->path
, strlen(one
->path
), one
->mode
,
22 two
->path
, strlen(two
->path
), two
->mode
);
25 static struct combine_diff_path
*intersect_paths(struct combine_diff_path
*curr
, int n
, int num_parent
)
27 struct diff_queue_struct
*q
= &diff_queued_diff
;
28 struct combine_diff_path
*p
, **tail
= &curr
;
32 for (i
= 0; i
< q
->nr
; i
++) {
35 if (diff_unmodified_pair(q
->queue
[i
]))
37 path
= q
->queue
[i
]->two
->path
;
39 p
= xmalloc(combine_diff_path_size(num_parent
, len
));
40 p
->path
= (char *) &(p
->parent
[num_parent
]);
41 memcpy(p
->path
, path
, len
);
45 sizeof(p
->parent
[0]) * num_parent
);
47 oidcpy(&p
->oid
, &q
->queue
[i
]->two
->oid
);
48 p
->mode
= q
->queue
[i
]->two
->mode
;
49 oidcpy(&p
->parent
[n
].oid
, &q
->queue
[i
]->one
->oid
);
50 p
->parent
[n
].mode
= q
->queue
[i
]->one
->mode
;
51 p
->parent
[n
].status
= q
->queue
[i
]->status
;
59 * paths in curr (linked list) and q->queue[] (array) are
60 * both sorted in the tree order.
63 while ((p
= *tail
) != NULL
) {
65 ? -1 : compare_paths(p
, q
->queue
[i
]->two
));
68 /* p->path not in q->queue[]; drop it */
75 /* q->queue[i] not in p->path; skip it */
80 oidcpy(&p
->parent
[n
].oid
, &q
->queue
[i
]->one
->oid
);
81 p
->parent
[n
].mode
= q
->queue
[i
]->one
->mode
;
82 p
->parent
[n
].status
= q
->queue
[i
]->status
;
90 /* Lines lost from parent */
92 struct lline
*next
, *prev
;
94 unsigned long parent_map
;
95 char line
[FLEX_ARRAY
];
98 /* Lines lost from current parent (before coalescing) */
100 struct lline
*lost_head
, *lost_tail
;
104 /* Lines surviving in the merge result */
106 /* Accumulated and coalesced lost lines */
112 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
113 * we did not change it).
114 * bit N is used for "interesting" lines, including context.
115 * bit (N+1) is used for "do not show deletion before this".
118 unsigned long *p_lno
;
121 static int match_string_spaces(const char *line1
, int len1
,
122 const char *line2
, int len2
,
125 if (flags
& XDF_WHITESPACE_FLAGS
) {
126 for (; len1
> 0 && XDL_ISSPACE(line1
[len1
- 1]); len1
--);
127 for (; len2
> 0 && XDL_ISSPACE(line2
[len2
- 1]); len2
--);
130 if (!(flags
& (XDF_IGNORE_WHITESPACE
| XDF_IGNORE_WHITESPACE_CHANGE
)))
131 return (len1
== len2
&& !memcmp(line1
, line2
, len1
));
133 while (len1
> 0 && len2
> 0) {
136 if (XDL_ISSPACE(line1
[len1
]) || XDL_ISSPACE(line2
[len2
])) {
137 if ((flags
& XDF_IGNORE_WHITESPACE_CHANGE
) &&
138 (!XDL_ISSPACE(line1
[len1
]) || !XDL_ISSPACE(line2
[len2
])))
141 for (; len1
> 0 && XDL_ISSPACE(line1
[len1
]); len1
--);
142 for (; len2
> 0 && XDL_ISSPACE(line2
[len2
]); len2
--);
144 if (line1
[len1
] != line2
[len2
])
148 if (flags
& XDF_IGNORE_WHITESPACE
) {
149 /* Consume remaining spaces */
150 for (; len1
> 0 && XDL_ISSPACE(line1
[len1
- 1]); len1
--);
151 for (; len2
> 0 && XDL_ISSPACE(line2
[len2
- 1]); len2
--);
154 /* We matched full line1 and line2 */
161 enum coalesce_direction
{ MATCH
, BASE
, NEW
};
163 /* Coalesce new lines into base by finding LCS */
164 static struct lline
*coalesce_lines(struct lline
*base
, int *lenbase
,
165 struct lline
*new, int lennew
,
166 unsigned long parent
, long flags
)
169 enum coalesce_direction
**directions
;
170 struct lline
*baseend
, *newend
= NULL
;
171 int i
, j
, origbaselen
= *lenbase
;
182 * Coalesce new lines into base by finding the LCS
183 * - Create the table to run dynamic programming
185 * - Then reverse read the direction structure:
186 * - If we have MATCH, assign parent to base flag, and consume
187 * both baseend and newend
188 * - Else if we have BASE, consume baseend
189 * - Else if we have NEW, insert newend lline into base and
192 lcs
= xcalloc(st_add(origbaselen
, 1), sizeof(int*));
193 directions
= xcalloc(st_add(origbaselen
, 1), sizeof(enum coalesce_direction
*));
194 for (i
= 0; i
< origbaselen
+ 1; i
++) {
195 lcs
[i
] = xcalloc(st_add(lennew
, 1), sizeof(int));
196 directions
[i
] = xcalloc(st_add(lennew
, 1), sizeof(enum coalesce_direction
));
197 directions
[i
][0] = BASE
;
199 for (j
= 1; j
< lennew
+ 1; j
++)
200 directions
[0][j
] = NEW
;
202 for (i
= 1, baseend
= base
; i
< origbaselen
+ 1; i
++) {
203 for (j
= 1, newend
= new; j
< lennew
+ 1; j
++) {
204 if (match_string_spaces(baseend
->line
, baseend
->len
,
205 newend
->line
, newend
->len
, flags
)) {
206 lcs
[i
][j
] = lcs
[i
- 1][j
- 1] + 1;
207 directions
[i
][j
] = MATCH
;
208 } else if (lcs
[i
][j
- 1] >= lcs
[i
- 1][j
]) {
209 lcs
[i
][j
] = lcs
[i
][j
- 1];
210 directions
[i
][j
] = NEW
;
212 lcs
[i
][j
] = lcs
[i
- 1][j
];
213 directions
[i
][j
] = BASE
;
216 newend
= newend
->next
;
219 baseend
= baseend
->next
;
222 for (i
= 0; i
< origbaselen
+ 1; i
++)
226 /* At this point, baseend and newend point to the end of each lists */
229 while (i
!= 0 || j
!= 0) {
230 if (directions
[i
][j
] == MATCH
) {
231 baseend
->parent_map
|= 1<<parent
;
232 baseend
= baseend
->prev
;
233 newend
= newend
->prev
;
236 } else if (directions
[i
][j
] == NEW
) {
240 /* Remove lline from new list and update newend */
242 lline
->prev
->next
= lline
->next
;
246 lline
->next
->prev
= lline
->prev
;
248 newend
= lline
->prev
;
251 /* Add lline to base list */
253 lline
->next
= baseend
->next
;
254 lline
->prev
= baseend
;
256 lline
->prev
->next
= lline
;
265 lline
->next
->prev
= lline
;
268 baseend
= baseend
->prev
;
275 struct lline
*lline
= newend
;
276 newend
= newend
->next
;
280 for (i
= 0; i
< origbaselen
+ 1; i
++)
287 static char *grab_blob(const struct object_id
*oid
, unsigned int mode
,
288 unsigned long *size
, struct userdiff_driver
*textconv
,
292 enum object_type type
;
294 if (S_ISGITLINK(mode
)) {
296 *size
= snprintf(blob
, 100,
297 "Subproject commit %s\n", oid_to_hex(oid
));
298 } else if (is_null_oid(oid
)) {
301 return xcalloc(1, 1);
302 } else if (textconv
) {
303 struct diff_filespec
*df
= alloc_filespec(path
);
304 fill_filespec(df
, oid
->hash
, 1, mode
);
305 *size
= fill_textconv(textconv
, df
, &blob
);
308 blob
= read_sha1_file(oid
->hash
, &type
, size
);
309 if (type
!= OBJ_BLOB
)
310 die("object '%s' is not a blob!", oid_to_hex(oid
));
315 static void append_lost(struct sline
*sline
, int n
, const char *line
, int len
)
318 unsigned long this_mask
= (1UL<<n
);
319 if (line
[len
-1] == '\n')
322 FLEX_ALLOC_MEM(lline
, line
, line
, len
);
325 lline
->prev
= sline
->plost
.lost_tail
;
327 lline
->prev
->next
= lline
;
329 sline
->plost
.lost_head
= lline
;
330 sline
->plost
.lost_tail
= lline
;
332 lline
->parent_map
= this_mask
;
335 struct combine_diff_state
{
342 struct sline
*lost_bucket
;
345 static void consume_line(void *state_
, char *line
, unsigned long len
)
347 struct combine_diff_state
*state
= state_
;
348 if (5 < len
&& !memcmp("@@ -", line
, 4)) {
349 if (parse_hunk_header(line
, len
,
350 &state
->ob
, &state
->on
,
351 &state
->nb
, &state
->nn
))
353 state
->lno
= state
->nb
;
354 if (state
->nn
== 0) {
355 /* @@ -X,Y +N,0 @@ removed Y lines
356 * that would have come *after* line N
357 * in the result. Our lost buckets hang
358 * to the line after the removed lines,
360 * Note that this is correct even when N == 0,
361 * in which case the hunk removes the first
364 state
->lost_bucket
= &state
->sline
[state
->nb
];
368 state
->lost_bucket
= &state
->sline
[state
->nb
-1];
370 if (!state
->sline
[state
->nb
-1].p_lno
)
371 state
->sline
[state
->nb
-1].p_lno
=
372 xcalloc(state
->num_parent
,
373 sizeof(unsigned long));
374 state
->sline
[state
->nb
-1].p_lno
[state
->n
] = state
->ob
;
377 if (!state
->lost_bucket
)
378 return; /* not in any hunk yet */
381 append_lost(state
->lost_bucket
, state
->n
, line
+1, len
-1);
384 state
->sline
[state
->lno
-1].flag
|= state
->nmask
;
390 static void combine_diff(const struct object_id
*parent
, unsigned int mode
,
391 mmfile_t
*result_file
,
392 struct sline
*sline
, unsigned int cnt
, int n
,
393 int num_parent
, int result_deleted
,
394 struct userdiff_driver
*textconv
,
395 const char *path
, long flags
)
397 unsigned int p_lno
, lno
;
398 unsigned long nmask
= (1UL << n
);
401 mmfile_t parent_file
;
402 struct combine_diff_state state
;
406 return; /* result deleted */
408 parent_file
.ptr
= grab_blob(parent
, mode
, &sz
, textconv
, path
);
409 parent_file
.size
= sz
;
410 memset(&xpp
, 0, sizeof(xpp
));
412 memset(&xecfg
, 0, sizeof(xecfg
));
413 memset(&state
, 0, sizeof(state
));
417 state
.num_parent
= num_parent
;
420 if (xdi_diff_outf(&parent_file
, result_file
, consume_line
, &state
,
422 die("unable to generate combined diff for %s",
424 free(parent_file
.ptr
);
426 /* Assign line numbers for this parent.
428 * sline[lno].p_lno[n] records the first line number
429 * (counting from 1) for parent N if the final hunk display
430 * started by showing sline[lno] (possibly showing the lost
431 * lines attached to it first).
433 for (lno
= 0, p_lno
= 1; lno
<= cnt
; lno
++) {
435 sline
[lno
].p_lno
[n
] = p_lno
;
437 /* Coalesce new lines */
438 if (sline
[lno
].plost
.lost_head
) {
439 struct sline
*sl
= &sline
[lno
];
440 sl
->lost
= coalesce_lines(sl
->lost
, &sl
->lenlost
,
442 sl
->plost
.len
, n
, flags
);
443 sl
->plost
.lost_head
= sl
->plost
.lost_tail
= NULL
;
447 /* How many lines would this sline advance the p_lno? */
448 ll
= sline
[lno
].lost
;
450 if (ll
->parent_map
& nmask
)
451 p_lno
++; /* '-' means parent had it */
454 if (lno
< cnt
&& !(sline
[lno
].flag
& nmask
))
455 p_lno
++; /* no '+' means parent had it */
457 sline
[lno
].p_lno
[n
] = p_lno
; /* trailer */
460 static unsigned long context
= 3;
461 static char combine_marker
= '@';
463 static int interesting(struct sline
*sline
, unsigned long all_mask
)
465 /* If some parents lost lines here, or if we have added to
466 * some parent, it is interesting.
468 return ((sline
->flag
& all_mask
) || sline
->lost
);
471 static unsigned long adjust_hunk_tail(struct sline
*sline
,
472 unsigned long all_mask
,
473 unsigned long hunk_begin
,
476 /* i points at the first uninteresting line. If the last line
477 * of the hunk was interesting only because it has some
478 * deletion, then it is not all that interesting for the
479 * purpose of giving trailing context lines. This is because
480 * we output '-' line and then unmodified sline[i-1] itself in
481 * that case which gives us one extra context line.
483 if ((hunk_begin
+ 1 <= i
) && !(sline
[i
-1].flag
& all_mask
))
488 static unsigned long find_next(struct sline
*sline
,
492 int look_for_uninteresting
)
494 /* We have examined up to i-1 and are about to look at i.
495 * Find next interesting or uninteresting line. Here,
496 * "interesting" does not mean interesting(), but marked by
497 * the give_context() function below (i.e. it includes context
498 * lines that are not interesting to interesting() function
499 * that are surrounded by interesting() ones.
502 if (look_for_uninteresting
503 ? !(sline
[i
].flag
& mark
)
504 : (sline
[i
].flag
& mark
))
511 static int give_context(struct sline
*sline
, unsigned long cnt
, int num_parent
)
513 unsigned long all_mask
= (1UL<<num_parent
) - 1;
514 unsigned long mark
= (1UL<<num_parent
);
515 unsigned long no_pre_delete
= (2UL<<num_parent
);
518 /* Two groups of interesting lines may have a short gap of
519 * uninteresting lines. Connect such groups to give them a
522 * We first start from what the interesting() function says,
523 * and mark them with "mark", and paint context lines with the
524 * mark. So interesting() would still say false for such context
525 * lines but they are treated as "interesting" in the end.
527 i
= find_next(sline
, mark
, 0, cnt
, 0);
532 unsigned long j
= (context
< i
) ? (i
- context
) : 0;
535 /* Paint a few lines before the first interesting line. */
537 if (!(sline
[j
].flag
& mark
))
538 sline
[j
].flag
|= no_pre_delete
;
539 sline
[j
++].flag
|= mark
;
543 /* we know up to i is to be included. where does the
544 * next uninteresting one start?
546 j
= find_next(sline
, mark
, i
, cnt
, 1);
548 break; /* the rest are all interesting */
550 /* lookahead context lines */
551 k
= find_next(sline
, mark
, j
, cnt
, 0);
552 j
= adjust_hunk_tail(sline
, all_mask
, i
, j
);
554 if (k
< j
+ context
) {
555 /* k is interesting and [j,k) are not, but
556 * paint them interesting because the gap is small.
559 sline
[j
++].flag
|= mark
;
564 /* j is the first uninteresting line and there is
565 * no overlap beyond it within context lines. Paint
566 * the trailing edge a bit.
569 k
= (j
+ context
< cnt
+1) ? j
+ context
: cnt
+1;
571 sline
[j
++].flag
|= mark
;
576 static int make_hunks(struct sline
*sline
, unsigned long cnt
,
577 int num_parent
, int dense
)
579 unsigned long all_mask
= (1UL<<num_parent
) - 1;
580 unsigned long mark
= (1UL<<num_parent
);
582 int has_interesting
= 0;
584 for (i
= 0; i
<= cnt
; i
++) {
585 if (interesting(&sline
[i
], all_mask
))
586 sline
[i
].flag
|= mark
;
588 sline
[i
].flag
&= ~mark
;
591 return give_context(sline
, cnt
, num_parent
);
593 /* Look at each hunk, and if we have changes from only one
594 * parent, or the changes are the same from all but one
595 * parent, mark that uninteresting.
599 unsigned long j
, hunk_begin
, hunk_end
;
600 unsigned long same_diff
;
601 while (i
<= cnt
&& !(sline
[i
].flag
& mark
))
604 break; /* No more interesting hunks */
606 for (j
= i
+ 1; j
<= cnt
; j
++) {
607 if (!(sline
[j
].flag
& mark
)) {
608 /* Look beyond the end to see if there
609 * is an interesting line after this
610 * hunk within context span.
612 unsigned long la
; /* lookahead */
614 la
= adjust_hunk_tail(sline
, all_mask
,
616 la
= (la
+ context
< cnt
+ 1) ?
617 (la
+ context
) : cnt
+ 1;
618 while (la
&& j
<= --la
) {
619 if (sline
[la
].flag
& mark
) {
631 /* [i..hunk_end) are interesting. Now is it really
632 * interesting? We check if there are only two versions
633 * and the result matches one of them. That is, we look
635 * (+) line, which records lines added to which parents;
636 * this line appears in the result.
637 * (-) line, which records from what parents the line
638 * was removed; this line does not appear in the result.
639 * then check the set of parents the result has difference
640 * from, from all lines. If there are lines that has
641 * different set of parents that the result has differences
642 * from, that means we have more than two versions.
644 * Even when we have only two versions, if the result does
645 * not match any of the parents, the it should be considered
646 * interesting. In such a case, we would have all '+' line.
647 * After passing the above "two versions" test, that would
648 * appear as "the same set of parents" to be "all parents".
652 for (j
= i
; j
< hunk_end
&& !has_interesting
; j
++) {
653 unsigned long this_diff
= sline
[j
].flag
& all_mask
;
654 struct lline
*ll
= sline
[j
].lost
;
656 /* This has some changes. Is it the
660 same_diff
= this_diff
;
661 else if (same_diff
!= this_diff
) {
666 while (ll
&& !has_interesting
) {
667 /* Lost this line from these parents;
668 * who are they? Are they the same?
670 this_diff
= ll
->parent_map
;
672 same_diff
= this_diff
;
673 else if (same_diff
!= this_diff
) {
680 if (!has_interesting
&& same_diff
!= all_mask
) {
681 /* This hunk is not that interesting after all */
682 for (j
= hunk_begin
; j
< hunk_end
; j
++)
683 sline
[j
].flag
&= ~mark
;
688 has_interesting
= give_context(sline
, cnt
, num_parent
);
689 return has_interesting
;
692 static void show_parent_lno(struct sline
*sline
, unsigned long l0
, unsigned long l1
, int n
, unsigned long null_context
)
694 l0
= sline
[l0
].p_lno
[n
];
695 l1
= sline
[l1
].p_lno
[n
];
696 printf(" -%lu,%lu", l0
, l1
-l0
-null_context
);
699 static int hunk_comment_line(const char *bol
)
706 return (isalpha(ch
) || ch
== '_' || ch
== '$');
709 static void show_line_to_eol(const char *line
, int len
, const char *reset
)
711 int saw_cr_at_eol
= 0;
714 saw_cr_at_eol
= (len
&& line
[len
-1] == '\r');
716 printf("%.*s%s%s\n", len
- saw_cr_at_eol
, line
,
718 saw_cr_at_eol
? "\r" : "");
721 static void dump_sline(struct sline
*sline
, const char *line_prefix
,
722 unsigned long cnt
, int num_parent
,
723 int use_color
, int result_deleted
)
725 unsigned long mark
= (1UL<<num_parent
);
726 unsigned long no_pre_delete
= (2UL<<num_parent
);
728 unsigned long lno
= 0;
729 const char *c_frag
= diff_get_color(use_color
, DIFF_FRAGINFO
);
730 const char *c_func
= diff_get_color(use_color
, DIFF_FUNCINFO
);
731 const char *c_new
= diff_get_color(use_color
, DIFF_FILE_NEW
);
732 const char *c_old
= diff_get_color(use_color
, DIFF_FILE_OLD
);
733 const char *c_context
= diff_get_color(use_color
, DIFF_CONTEXT
);
734 const char *c_reset
= diff_get_color(use_color
, DIFF_RESET
);
737 return; /* result deleted */
740 unsigned long hunk_end
;
741 unsigned long rlines
;
742 const char *hunk_comment
= NULL
;
743 unsigned long null_context
= 0;
745 while (lno
<= cnt
&& !(sline
[lno
].flag
& mark
)) {
746 if (hunk_comment_line(sline
[lno
].bol
))
747 hunk_comment
= sline
[lno
].bol
;
753 for (hunk_end
= lno
+ 1; hunk_end
<= cnt
; hunk_end
++)
754 if (!(sline
[hunk_end
].flag
& mark
))
757 rlines
= hunk_end
- lno
;
759 rlines
--; /* pointing at the last delete hunk */
763 * Even when running with --unified=0, all
764 * lines in the hunk needs to be processed in
765 * the loop below in order to show the
766 * deletion recorded in lost_head. However,
767 * we do not want to show the resulting line
768 * with all blank context markers in such a
772 for (j
= lno
; j
< hunk_end
; j
++)
773 if (!(sline
[j
].flag
& (mark
-1)))
775 rlines
-= null_context
;
778 printf("%s%s", line_prefix
, c_frag
);
779 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
780 for (i
= 0; i
< num_parent
; i
++)
781 show_parent_lno(sline
, lno
, hunk_end
, i
, null_context
);
782 printf(" +%lu,%lu ", lno
+1, rlines
);
783 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
787 for (i
= 0; i
< 40; i
++) {
788 int ch
= hunk_comment
[i
] & 0xff;
789 if (!ch
|| ch
== '\n')
795 printf("%s%s %s%s", c_reset
,
798 for (i
= 0; i
< comment_end
; i
++)
799 putchar(hunk_comment
[i
]);
802 printf("%s\n", c_reset
);
803 while (lno
< hunk_end
) {
806 unsigned long p_mask
;
807 struct sline
*sl
= &sline
[lno
++];
808 ll
= (sl
->flag
& no_pre_delete
) ? NULL
: sl
->lost
;
810 printf("%s%s", line_prefix
, c_old
);
811 for (j
= 0; j
< num_parent
; j
++) {
812 if (ll
->parent_map
& (1UL<<j
))
817 show_line_to_eol(ll
->line
, -1, c_reset
);
823 fputs(line_prefix
, stdout
);
824 if (!(sl
->flag
& (mark
-1))) {
826 * This sline was here to hang the
827 * lost lines in front of it.
831 fputs(c_context
, stdout
);
834 fputs(c_new
, stdout
);
835 for (j
= 0; j
< num_parent
; j
++) {
836 if (p_mask
& sl
->flag
)
842 show_line_to_eol(sl
->bol
, sl
->len
, c_reset
);
847 static void reuse_combine_diff(struct sline
*sline
, unsigned long cnt
,
850 /* We have already examined parent j and we know parent i
851 * and parent j are the same, so reuse the combined result
852 * of parent j for parent i.
854 unsigned long lno
, imask
, jmask
;
858 for (lno
= 0; lno
<= cnt
; lno
++) {
859 struct lline
*ll
= sline
->lost
;
860 sline
->p_lno
[i
] = sline
->p_lno
[j
];
862 if (ll
->parent_map
& jmask
)
863 ll
->parent_map
|= imask
;
866 if (sline
->flag
& jmask
)
867 sline
->flag
|= imask
;
870 /* the overall size of the file (sline[cnt]) */
871 sline
->p_lno
[i
] = sline
->p_lno
[j
];
874 static void dump_quoted_path(const char *head
,
877 const char *line_prefix
,
878 const char *c_meta
, const char *c_reset
)
880 static struct strbuf buf
= STRBUF_INIT
;
883 strbuf_addstr(&buf
, line_prefix
);
884 strbuf_addstr(&buf
, c_meta
);
885 strbuf_addstr(&buf
, head
);
886 quote_two_c_style(&buf
, prefix
, path
, 0);
887 strbuf_addstr(&buf
, c_reset
);
891 static void show_combined_header(struct combine_diff_path
*elem
,
894 struct rev_info
*rev
,
895 const char *line_prefix
,
897 int show_file_header
)
899 struct diff_options
*opt
= &rev
->diffopt
;
900 int abbrev
= DIFF_OPT_TST(opt
, FULL_INDEX
) ? GIT_SHA1_HEXSZ
: DEFAULT_ABBREV
;
901 const char *a_prefix
= opt
->a_prefix
? opt
->a_prefix
: "a/";
902 const char *b_prefix
= opt
->b_prefix
? opt
->b_prefix
: "b/";
903 const char *c_meta
= diff_get_color_opt(opt
, DIFF_METAINFO
);
904 const char *c_reset
= diff_get_color_opt(opt
, DIFF_RESET
);
910 if (rev
->loginfo
&& !rev
->no_commit_id
)
913 dump_quoted_path(dense
? "diff --cc " : "diff --combined ",
914 "", elem
->path
, line_prefix
, c_meta
, c_reset
);
915 printf("%s%sindex ", line_prefix
, c_meta
);
916 for (i
= 0; i
< num_parent
; i
++) {
917 abb
= find_unique_abbrev(elem
->parent
[i
].oid
.hash
,
919 printf("%s%s", i
? "," : "", abb
);
921 abb
= find_unique_abbrev(elem
->oid
.hash
, abbrev
);
922 printf("..%s%s\n", abb
, c_reset
);
925 deleted
= !elem
->mode
;
927 /* We say it was added if nobody had it */
929 for (i
= 0; added
&& i
< num_parent
; i
++)
930 if (elem
->parent
[i
].status
!=
934 printf("%s%snew file mode %06o",
935 line_prefix
, c_meta
, elem
->mode
);
938 printf("%s%sdeleted file ",
939 line_prefix
, c_meta
);
941 for (i
= 0; i
< num_parent
; i
++) {
942 printf("%s%06o", i
? "," : "",
943 elem
->parent
[i
].mode
);
946 printf("..%06o", elem
->mode
);
948 printf("%s\n", c_reset
);
951 if (!show_file_header
)
955 dump_quoted_path("--- ", "", "/dev/null",
956 line_prefix
, c_meta
, c_reset
);
958 dump_quoted_path("--- ", a_prefix
, elem
->path
,
959 line_prefix
, c_meta
, c_reset
);
961 dump_quoted_path("+++ ", "", "/dev/null",
962 line_prefix
, c_meta
, c_reset
);
964 dump_quoted_path("+++ ", b_prefix
, elem
->path
,
965 line_prefix
, c_meta
, c_reset
);
968 static void show_patch_diff(struct combine_diff_path
*elem
, int num_parent
,
969 int dense
, int working_tree_file
,
970 struct rev_info
*rev
)
972 struct diff_options
*opt
= &rev
->diffopt
;
973 unsigned long result_size
, cnt
, lno
;
974 int result_deleted
= 0;
976 struct sline
*sline
; /* survived lines */
977 int mode_differs
= 0;
979 mmfile_t result_file
;
980 struct userdiff_driver
*userdiff
;
981 struct userdiff_driver
*textconv
= NULL
;
983 const char *line_prefix
= diff_line_prefix(opt
);
985 context
= opt
->context
;
986 userdiff
= userdiff_find_by_path(elem
->path
);
988 userdiff
= userdiff_find_by_name("default");
989 if (DIFF_OPT_TST(opt
, ALLOW_TEXTCONV
))
990 textconv
= userdiff_get_textconv(userdiff
);
992 /* Read the result of merge first */
993 if (!working_tree_file
)
994 result
= grab_blob(&elem
->oid
, elem
->mode
, &result_size
,
995 textconv
, elem
->path
);
997 /* Used by diff-tree to read from the working tree */
1001 if (lstat(elem
->path
, &st
) < 0)
1004 if (S_ISLNK(st
.st_mode
)) {
1005 struct strbuf buf
= STRBUF_INIT
;
1007 if (strbuf_readlink(&buf
, elem
->path
, st
.st_size
) < 0) {
1008 error_errno("readlink(%s)", elem
->path
);
1011 result_size
= buf
.len
;
1012 result
= strbuf_detach(&buf
, NULL
);
1013 elem
->mode
= canon_mode(st
.st_mode
);
1014 } else if (S_ISDIR(st
.st_mode
)) {
1015 struct object_id oid
;
1016 if (resolve_gitlink_ref(elem
->path
, "HEAD", oid
.hash
) < 0)
1017 result
= grab_blob(&elem
->oid
, elem
->mode
,
1018 &result_size
, NULL
, NULL
);
1020 result
= grab_blob(&oid
, elem
->mode
,
1021 &result_size
, NULL
, NULL
);
1022 } else if (textconv
) {
1023 struct diff_filespec
*df
= alloc_filespec(elem
->path
);
1024 fill_filespec(df
, null_sha1
, 0, st
.st_mode
);
1025 result_size
= fill_textconv(textconv
, df
, &result
);
1027 } else if (0 <= (fd
= open(elem
->path
, O_RDONLY
))) {
1028 size_t len
= xsize_t(st
.st_size
);
1032 elem
->mode
= canon_mode(st
.st_mode
);
1033 /* if symlinks don't work, assume symlink if all parents
1036 is_file
= has_symlinks
;
1037 for (i
= 0; !is_file
&& i
< num_parent
; i
++)
1038 is_file
= !S_ISLNK(elem
->parent
[i
].mode
);
1040 elem
->mode
= canon_mode(S_IFLNK
);
1043 result
= xmallocz(len
);
1045 done
= read_in_full(fd
, result
, len
);
1047 die_errno("read error '%s'", elem
->path
);
1048 else if (done
< len
)
1049 die("early EOF '%s'", elem
->path
);
1051 /* If not a fake symlink, apply filters, e.g. autocrlf */
1053 struct strbuf buf
= STRBUF_INIT
;
1055 if (convert_to_git(elem
->path
, result
, len
, &buf
, safe_crlf
)) {
1057 result
= strbuf_detach(&buf
, &len
);
1067 result
= xcalloc(1, 1);
1074 for (i
= 0; i
< num_parent
; i
++) {
1075 if (elem
->parent
[i
].mode
!= elem
->mode
) {
1083 else if (userdiff
->binary
!= -1)
1084 is_binary
= userdiff
->binary
;
1086 is_binary
= buffer_is_binary(result
, result_size
);
1087 for (i
= 0; !is_binary
&& i
< num_parent
; i
++) {
1090 buf
= grab_blob(&elem
->parent
[i
].oid
,
1091 elem
->parent
[i
].mode
,
1093 if (buffer_is_binary(buf
, size
))
1099 show_combined_header(elem
, num_parent
, dense
, rev
,
1100 line_prefix
, mode_differs
, 0);
1101 printf("Binary files differ\n");
1106 for (cnt
= 0, cp
= result
; cp
< result
+ result_size
; cp
++) {
1110 if (result_size
&& result
[result_size
-1] != '\n')
1111 cnt
++; /* incomplete line */
1113 sline
= xcalloc(st_add(cnt
, 2), sizeof(*sline
));
1114 sline
[0].bol
= result
;
1115 for (lno
= 0, cp
= result
; cp
< result
+ result_size
; cp
++) {
1117 sline
[lno
].len
= cp
- sline
[lno
].bol
;
1120 sline
[lno
].bol
= cp
+ 1;
1123 if (result_size
&& result
[result_size
-1] != '\n')
1124 sline
[cnt
-1].len
= result_size
- (sline
[cnt
-1].bol
- result
);
1126 result_file
.ptr
= result
;
1127 result_file
.size
= result_size
;
1129 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1130 * for deletion hunk at the end.
1132 sline
[0].p_lno
= xcalloc(st_mult(st_add(cnt
, 2), num_parent
), sizeof(unsigned long));
1133 for (lno
= 0; lno
<= cnt
; lno
++)
1134 sline
[lno
+1].p_lno
= sline
[lno
].p_lno
+ num_parent
;
1136 for (i
= 0; i
< num_parent
; i
++) {
1138 for (j
= 0; j
< i
; j
++) {
1139 if (!oidcmp(&elem
->parent
[i
].oid
,
1140 &elem
->parent
[j
].oid
)) {
1141 reuse_combine_diff(sline
, cnt
, i
, j
);
1146 combine_diff(&elem
->parent
[i
].oid
,
1147 elem
->parent
[i
].mode
,
1148 &result_file
, sline
,
1149 cnt
, i
, num_parent
, result_deleted
,
1150 textconv
, elem
->path
, opt
->xdl_opts
);
1153 show_hunks
= make_hunks(sline
, cnt
, num_parent
, dense
);
1155 if (show_hunks
|| mode_differs
|| working_tree_file
) {
1156 show_combined_header(elem
, num_parent
, dense
, rev
,
1157 line_prefix
, mode_differs
, 1);
1158 dump_sline(sline
, line_prefix
, cnt
, num_parent
,
1159 opt
->use_color
, result_deleted
);
1163 for (lno
= 0; lno
< cnt
; lno
++) {
1164 if (sline
[lno
].lost
) {
1165 struct lline
*ll
= sline
[lno
].lost
;
1167 struct lline
*tmp
= ll
;
1173 free(sline
[0].p_lno
);
1177 static void show_raw_diff(struct combine_diff_path
*p
, int num_parent
, struct rev_info
*rev
)
1179 struct diff_options
*opt
= &rev
->diffopt
;
1180 int line_termination
, inter_name_termination
, i
;
1181 const char *line_prefix
= diff_line_prefix(opt
);
1183 line_termination
= opt
->line_termination
;
1184 inter_name_termination
= '\t';
1185 if (!line_termination
)
1186 inter_name_termination
= 0;
1188 if (rev
->loginfo
&& !rev
->no_commit_id
)
1192 if (opt
->output_format
& DIFF_FORMAT_RAW
) {
1193 printf("%s", line_prefix
);
1195 /* As many colons as there are parents */
1196 for (i
= 0; i
< num_parent
; i
++)
1199 /* Show the modes */
1200 for (i
= 0; i
< num_parent
; i
++)
1201 printf("%06o ", p
->parent
[i
].mode
);
1202 printf("%06o", p
->mode
);
1205 for (i
= 0; i
< num_parent
; i
++)
1206 printf(" %s", diff_unique_abbrev(p
->parent
[i
].oid
.hash
,
1208 printf(" %s ", diff_unique_abbrev(p
->oid
.hash
, opt
->abbrev
));
1211 if (opt
->output_format
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
)) {
1212 for (i
= 0; i
< num_parent
; i
++)
1213 putchar(p
->parent
[i
].status
);
1214 putchar(inter_name_termination
);
1217 write_name_quoted(p
->path
, stdout
, line_termination
);
1221 * The result (p->elem) is from the working tree and their
1222 * parents are typically from multiple stages during a merge
1223 * (i.e. diff-files) or the state in HEAD and in the index
1224 * (i.e. diff-index).
1226 void show_combined_diff(struct combine_diff_path
*p
,
1229 struct rev_info
*rev
)
1231 struct diff_options
*opt
= &rev
->diffopt
;
1233 if (opt
->output_format
& (DIFF_FORMAT_RAW
|
1235 DIFF_FORMAT_NAME_STATUS
))
1236 show_raw_diff(p
, num_parent
, rev
);
1237 else if (opt
->output_format
& DIFF_FORMAT_PATCH
)
1238 show_patch_diff(p
, num_parent
, dense
, 1, rev
);
1241 static void free_combined_pair(struct diff_filepair
*pair
)
1248 * A combine_diff_path expresses N parents on the LHS against 1 merge
1249 * result. Synthesize a diff_filepair that has N entries on the "one"
1250 * side and 1 entry on the "two" side.
1252 * In the future, we might want to add more data to combine_diff_path
1253 * so that we can fill fields we are ignoring (most notably, size) here,
1254 * but currently nobody uses it, so this should suffice for now.
1256 static struct diff_filepair
*combined_pair(struct combine_diff_path
*p
,
1260 struct diff_filepair
*pair
;
1261 struct diff_filespec
*pool
;
1263 pair
= xmalloc(sizeof(*pair
));
1264 pool
= xcalloc(st_add(num_parent
, 1), sizeof(struct diff_filespec
));
1265 pair
->one
= pool
+ 1;
1268 for (i
= 0; i
< num_parent
; i
++) {
1269 pair
->one
[i
].path
= p
->path
;
1270 pair
->one
[i
].mode
= p
->parent
[i
].mode
;
1271 oidcpy(&pair
->one
[i
].oid
, &p
->parent
[i
].oid
);
1272 pair
->one
[i
].oid_valid
= !is_null_oid(&p
->parent
[i
].oid
);
1273 pair
->one
[i
].has_more_entries
= 1;
1275 pair
->one
[num_parent
- 1].has_more_entries
= 0;
1277 pair
->two
->path
= p
->path
;
1278 pair
->two
->mode
= p
->mode
;
1279 oidcpy(&pair
->two
->oid
, &p
->oid
);
1280 pair
->two
->oid_valid
= !is_null_oid(&p
->oid
);
1284 static void handle_combined_callback(struct diff_options
*opt
,
1285 struct combine_diff_path
*paths
,
1289 struct combine_diff_path
*p
;
1290 struct diff_queue_struct q
;
1293 q
.queue
= xcalloc(num_paths
, sizeof(struct diff_filepair
*));
1294 q
.alloc
= num_paths
;
1296 for (i
= 0, p
= paths
; p
; p
= p
->next
)
1297 q
.queue
[i
++] = combined_pair(p
, num_parent
);
1298 opt
->format_callback(&q
, opt
, opt
->format_callback_data
);
1299 for (i
= 0; i
< num_paths
; i
++)
1300 free_combined_pair(q
.queue
[i
]);
1304 static const char *path_path(void *obj
)
1306 struct combine_diff_path
*path
= (struct combine_diff_path
*)obj
;
1312 /* find set of paths that every parent touches */
1313 static struct combine_diff_path
*find_paths_generic(const unsigned char *sha1
,
1314 const struct sha1_array
*parents
, struct diff_options
*opt
)
1316 struct combine_diff_path
*paths
= NULL
;
1317 int i
, num_parent
= parents
->nr
;
1319 int output_format
= opt
->output_format
;
1320 const char *orderfile
= opt
->orderfile
;
1322 opt
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
1323 /* tell diff_tree to emit paths in sorted (=tree) order */
1324 opt
->orderfile
= NULL
;
1326 /* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn) (wrt paths) */
1327 for (i
= 0; i
< num_parent
; i
++) {
1329 * show stat against the first parent even when doing
1332 int stat_opt
= (output_format
&
1333 (DIFF_FORMAT_NUMSTAT
|DIFF_FORMAT_DIFFSTAT
));
1334 if (i
== 0 && stat_opt
)
1335 opt
->output_format
= stat_opt
;
1337 opt
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
1338 diff_tree_sha1(parents
->sha1
[i
], sha1
, "", opt
);
1340 paths
= intersect_paths(paths
, i
, num_parent
);
1342 /* if showing diff, show it in requested order */
1343 if (opt
->output_format
!= DIFF_FORMAT_NO_OUTPUT
&&
1345 diffcore_order(orderfile
);
1351 opt
->output_format
= output_format
;
1352 opt
->orderfile
= orderfile
;
1358 * find set of paths that everybody touches, assuming diff is run without
1359 * rename/copy detection, etc, comparing all trees simultaneously (= faster).
1361 static struct combine_diff_path
*find_paths_multitree(
1362 const unsigned char *sha1
, const struct sha1_array
*parents
,
1363 struct diff_options
*opt
)
1365 int i
, nparent
= parents
->nr
;
1366 const unsigned char **parents_sha1
;
1367 struct combine_diff_path paths_head
;
1370 ALLOC_ARRAY(parents_sha1
, nparent
);
1371 for (i
= 0; i
< nparent
; i
++)
1372 parents_sha1
[i
] = parents
->sha1
[i
];
1374 /* fake list head, so worker can assume it is non-NULL */
1375 paths_head
.next
= NULL
;
1377 strbuf_init(&base
, PATH_MAX
);
1378 diff_tree_paths(&paths_head
, sha1
, parents_sha1
, nparent
, &base
, opt
);
1380 strbuf_release(&base
);
1382 return paths_head
.next
;
1386 void diff_tree_combined(const unsigned char *sha1
,
1387 const struct sha1_array
*parents
,
1389 struct rev_info
*rev
)
1391 struct diff_options
*opt
= &rev
->diffopt
;
1392 struct diff_options diffopts
;
1393 struct combine_diff_path
*p
, *paths
;
1394 int i
, num_paths
, needsep
, show_log_first
, num_parent
= parents
->nr
;
1395 int need_generic_pathscan
;
1397 /* nothing to do, if no parents */
1401 show_log_first
= !!rev
->loginfo
&& !rev
->no_commit_id
;
1403 if (show_log_first
) {
1406 if (rev
->verbose_header
&& opt
->output_format
&&
1407 opt
->output_format
!= DIFF_FORMAT_NO_OUTPUT
&&
1408 !commit_format_is_empty(rev
->commit_format
))
1409 printf("%s%c", diff_line_prefix(opt
),
1410 opt
->line_termination
);
1414 copy_pathspec(&diffopts
.pathspec
, &opt
->pathspec
);
1415 DIFF_OPT_SET(&diffopts
, RECURSIVE
);
1416 DIFF_OPT_CLR(&diffopts
, ALLOW_EXTERNAL
);
1418 /* find set of paths that everybody touches
1422 * Diffcore transformations are bound to diff_filespec and logic
1423 * comparing two entries - i.e. they do not apply directly to combine
1426 * If some of such transformations is requested - we launch generic
1427 * path scanning, which works significantly slower compared to
1428 * simultaneous all-trees-in-one-go scan in find_paths_multitree().
1430 * TODO some of the filters could be ported to work on
1431 * combine_diff_paths - i.e. all functionality that skips paths, so in
1432 * theory, we could end up having only multitree path scanning.
1434 * NOTE please keep this semantically in sync with diffcore_std()
1436 need_generic_pathscan
= opt
->skip_stat_unmatch
||
1437 DIFF_OPT_TST(opt
, FOLLOW_RENAMES
) ||
1438 opt
->break_opt
!= -1 ||
1439 opt
->detect_rename
||
1444 if (need_generic_pathscan
) {
1446 * NOTE generic case also handles --stat, as it computes
1447 * diff(sha1,parent_i) for all i to do the job, specifically
1450 paths
= find_paths_generic(sha1
, parents
, &diffopts
);
1454 paths
= find_paths_multitree(sha1
, parents
, &diffopts
);
1457 * show stat against the first parent even
1458 * when doing combined diff.
1460 stat_opt
= (opt
->output_format
&
1461 (DIFF_FORMAT_NUMSTAT
|DIFF_FORMAT_DIFFSTAT
));
1463 diffopts
.output_format
= stat_opt
;
1465 diff_tree_sha1(parents
->sha1
[0], sha1
, "", &diffopts
);
1466 diffcore_std(&diffopts
);
1468 diffcore_order(opt
->orderfile
);
1469 diff_flush(&diffopts
);
1473 /* find out number of surviving paths */
1474 for (num_paths
= 0, p
= paths
; p
; p
= p
->next
)
1477 /* order paths according to diffcore_order */
1478 if (opt
->orderfile
&& num_paths
) {
1479 struct obj_order
*o
;
1481 ALLOC_ARRAY(o
, num_paths
);
1482 for (i
= 0, p
= paths
; p
; p
= p
->next
, i
++)
1484 order_objects(opt
->orderfile
, path_path
, o
, num_paths
);
1485 for (i
= 0; i
< num_paths
- 1; i
++) {
1487 p
->next
= o
[i
+1].obj
;
1490 p
= o
[num_paths
-1].obj
;
1498 if (opt
->output_format
& (DIFF_FORMAT_RAW
|
1500 DIFF_FORMAT_NAME_STATUS
)) {
1501 for (p
= paths
; p
; p
= p
->next
)
1502 show_raw_diff(p
, num_parent
, rev
);
1505 else if (opt
->output_format
&
1506 (DIFF_FORMAT_NUMSTAT
|DIFF_FORMAT_DIFFSTAT
))
1508 else if (opt
->output_format
& DIFF_FORMAT_CALLBACK
)
1509 handle_combined_callback(opt
, paths
, num_parent
, num_paths
);
1511 if (opt
->output_format
& DIFF_FORMAT_PATCH
) {
1513 printf("%s%c", diff_line_prefix(opt
),
1514 opt
->line_termination
);
1515 for (p
= paths
; p
; p
= p
->next
)
1516 show_patch_diff(p
, num_parent
, dense
,
1521 /* Clean things up */
1523 struct combine_diff_path
*tmp
= paths
;
1524 paths
= paths
->next
;
1528 clear_pathspec(&diffopts
.pathspec
);
1531 void diff_tree_combined_merge(const struct commit
*commit
, int dense
,
1532 struct rev_info
*rev
)
1534 struct commit_list
*parent
= get_saved_parents(rev
, commit
);
1535 struct sha1_array parents
= SHA1_ARRAY_INIT
;
1538 sha1_array_append(&parents
, parent
->item
->object
.oid
.hash
);
1539 parent
= parent
->next
;
1541 diff_tree_combined(commit
->object
.oid
.hash
, &parents
, dense
, rev
);
1542 sha1_array_clear(&parents
);