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
)) {
295 struct strbuf buf
= STRBUF_INIT
;
296 strbuf_addf(&buf
, "Subproject commit %s\n", oid_to_hex(oid
));
298 blob
= strbuf_detach(&buf
, NULL
);
299 } else if (is_null_oid(oid
)) {
302 return xcalloc(1, 1);
303 } else if (textconv
) {
304 struct diff_filespec
*df
= alloc_filespec(path
);
305 fill_filespec(df
, oid
, 1, mode
);
306 *size
= fill_textconv(textconv
, df
, &blob
);
309 blob
= read_sha1_file(oid
->hash
, &type
, size
);
310 if (type
!= OBJ_BLOB
)
311 die("object '%s' is not a blob!", oid_to_hex(oid
));
316 static void append_lost(struct sline
*sline
, int n
, const char *line
, int len
)
319 unsigned long this_mask
= (1UL<<n
);
320 if (line
[len
-1] == '\n')
323 FLEX_ALLOC_MEM(lline
, line
, line
, len
);
326 lline
->prev
= sline
->plost
.lost_tail
;
328 lline
->prev
->next
= lline
;
330 sline
->plost
.lost_head
= lline
;
331 sline
->plost
.lost_tail
= lline
;
333 lline
->parent_map
= this_mask
;
336 struct combine_diff_state
{
343 struct sline
*lost_bucket
;
346 static void consume_line(void *state_
, char *line
, unsigned long len
)
348 struct combine_diff_state
*state
= state_
;
349 if (5 < len
&& !memcmp("@@ -", line
, 4)) {
350 if (parse_hunk_header(line
, len
,
351 &state
->ob
, &state
->on
,
352 &state
->nb
, &state
->nn
))
354 state
->lno
= state
->nb
;
355 if (state
->nn
== 0) {
356 /* @@ -X,Y +N,0 @@ removed Y lines
357 * that would have come *after* line N
358 * in the result. Our lost buckets hang
359 * to the line after the removed lines,
361 * Note that this is correct even when N == 0,
362 * in which case the hunk removes the first
365 state
->lost_bucket
= &state
->sline
[state
->nb
];
369 state
->lost_bucket
= &state
->sline
[state
->nb
-1];
371 if (!state
->sline
[state
->nb
-1].p_lno
)
372 state
->sline
[state
->nb
-1].p_lno
=
373 xcalloc(state
->num_parent
,
374 sizeof(unsigned long));
375 state
->sline
[state
->nb
-1].p_lno
[state
->n
] = state
->ob
;
378 if (!state
->lost_bucket
)
379 return; /* not in any hunk yet */
382 append_lost(state
->lost_bucket
, state
->n
, line
+1, len
-1);
385 state
->sline
[state
->lno
-1].flag
|= state
->nmask
;
391 static void combine_diff(const struct object_id
*parent
, unsigned int mode
,
392 mmfile_t
*result_file
,
393 struct sline
*sline
, unsigned int cnt
, int n
,
394 int num_parent
, int result_deleted
,
395 struct userdiff_driver
*textconv
,
396 const char *path
, long flags
)
398 unsigned int p_lno
, lno
;
399 unsigned long nmask
= (1UL << n
);
402 mmfile_t parent_file
;
403 struct combine_diff_state state
;
407 return; /* result deleted */
409 parent_file
.ptr
= grab_blob(parent
, mode
, &sz
, textconv
, path
);
410 parent_file
.size
= sz
;
411 memset(&xpp
, 0, sizeof(xpp
));
413 memset(&xecfg
, 0, sizeof(xecfg
));
414 memset(&state
, 0, sizeof(state
));
418 state
.num_parent
= num_parent
;
421 if (xdi_diff_outf(&parent_file
, result_file
, consume_line
, &state
,
423 die("unable to generate combined diff for %s",
425 free(parent_file
.ptr
);
427 /* Assign line numbers for this parent.
429 * sline[lno].p_lno[n] records the first line number
430 * (counting from 1) for parent N if the final hunk display
431 * started by showing sline[lno] (possibly showing the lost
432 * lines attached to it first).
434 for (lno
= 0, p_lno
= 1; lno
<= cnt
; lno
++) {
436 sline
[lno
].p_lno
[n
] = p_lno
;
438 /* Coalesce new lines */
439 if (sline
[lno
].plost
.lost_head
) {
440 struct sline
*sl
= &sline
[lno
];
441 sl
->lost
= coalesce_lines(sl
->lost
, &sl
->lenlost
,
443 sl
->plost
.len
, n
, flags
);
444 sl
->plost
.lost_head
= sl
->plost
.lost_tail
= NULL
;
448 /* How many lines would this sline advance the p_lno? */
449 ll
= sline
[lno
].lost
;
451 if (ll
->parent_map
& nmask
)
452 p_lno
++; /* '-' means parent had it */
455 if (lno
< cnt
&& !(sline
[lno
].flag
& nmask
))
456 p_lno
++; /* no '+' means parent had it */
458 sline
[lno
].p_lno
[n
] = p_lno
; /* trailer */
461 static unsigned long context
= 3;
462 static char combine_marker
= '@';
464 static int interesting(struct sline
*sline
, unsigned long all_mask
)
466 /* If some parents lost lines here, or if we have added to
467 * some parent, it is interesting.
469 return ((sline
->flag
& all_mask
) || sline
->lost
);
472 static unsigned long adjust_hunk_tail(struct sline
*sline
,
473 unsigned long all_mask
,
474 unsigned long hunk_begin
,
477 /* i points at the first uninteresting line. If the last line
478 * of the hunk was interesting only because it has some
479 * deletion, then it is not all that interesting for the
480 * purpose of giving trailing context lines. This is because
481 * we output '-' line and then unmodified sline[i-1] itself in
482 * that case which gives us one extra context line.
484 if ((hunk_begin
+ 1 <= i
) && !(sline
[i
-1].flag
& all_mask
))
489 static unsigned long find_next(struct sline
*sline
,
493 int look_for_uninteresting
)
495 /* We have examined up to i-1 and are about to look at i.
496 * Find next interesting or uninteresting line. Here,
497 * "interesting" does not mean interesting(), but marked by
498 * the give_context() function below (i.e. it includes context
499 * lines that are not interesting to interesting() function
500 * that are surrounded by interesting() ones.
503 if (look_for_uninteresting
504 ? !(sline
[i
].flag
& mark
)
505 : (sline
[i
].flag
& mark
))
512 static int give_context(struct sline
*sline
, unsigned long cnt
, int num_parent
)
514 unsigned long all_mask
= (1UL<<num_parent
) - 1;
515 unsigned long mark
= (1UL<<num_parent
);
516 unsigned long no_pre_delete
= (2UL<<num_parent
);
519 /* Two groups of interesting lines may have a short gap of
520 * uninteresting lines. Connect such groups to give them a
523 * We first start from what the interesting() function says,
524 * and mark them with "mark", and paint context lines with the
525 * mark. So interesting() would still say false for such context
526 * lines but they are treated as "interesting" in the end.
528 i
= find_next(sline
, mark
, 0, cnt
, 0);
533 unsigned long j
= (context
< i
) ? (i
- context
) : 0;
536 /* Paint a few lines before the first interesting line. */
538 if (!(sline
[j
].flag
& mark
))
539 sline
[j
].flag
|= no_pre_delete
;
540 sline
[j
++].flag
|= mark
;
544 /* we know up to i is to be included. where does the
545 * next uninteresting one start?
547 j
= find_next(sline
, mark
, i
, cnt
, 1);
549 break; /* the rest are all interesting */
551 /* lookahead context lines */
552 k
= find_next(sline
, mark
, j
, cnt
, 0);
553 j
= adjust_hunk_tail(sline
, all_mask
, i
, j
);
555 if (k
< j
+ context
) {
556 /* k is interesting and [j,k) are not, but
557 * paint them interesting because the gap is small.
560 sline
[j
++].flag
|= mark
;
565 /* j is the first uninteresting line and there is
566 * no overlap beyond it within context lines. Paint
567 * the trailing edge a bit.
570 k
= (j
+ context
< cnt
+1) ? j
+ context
: cnt
+1;
572 sline
[j
++].flag
|= mark
;
577 static int make_hunks(struct sline
*sline
, unsigned long cnt
,
578 int num_parent
, int dense
)
580 unsigned long all_mask
= (1UL<<num_parent
) - 1;
581 unsigned long mark
= (1UL<<num_parent
);
583 int has_interesting
= 0;
585 for (i
= 0; i
<= cnt
; i
++) {
586 if (interesting(&sline
[i
], all_mask
))
587 sline
[i
].flag
|= mark
;
589 sline
[i
].flag
&= ~mark
;
592 return give_context(sline
, cnt
, num_parent
);
594 /* Look at each hunk, and if we have changes from only one
595 * parent, or the changes are the same from all but one
596 * parent, mark that uninteresting.
600 unsigned long j
, hunk_begin
, hunk_end
;
601 unsigned long same_diff
;
602 while (i
<= cnt
&& !(sline
[i
].flag
& mark
))
605 break; /* No more interesting hunks */
607 for (j
= i
+ 1; j
<= cnt
; j
++) {
608 if (!(sline
[j
].flag
& mark
)) {
609 /* Look beyond the end to see if there
610 * is an interesting line after this
611 * hunk within context span.
613 unsigned long la
; /* lookahead */
615 la
= adjust_hunk_tail(sline
, all_mask
,
617 la
= (la
+ context
< cnt
+ 1) ?
618 (la
+ context
) : cnt
+ 1;
619 while (la
&& j
<= --la
) {
620 if (sline
[la
].flag
& mark
) {
632 /* [i..hunk_end) are interesting. Now is it really
633 * interesting? We check if there are only two versions
634 * and the result matches one of them. That is, we look
636 * (+) line, which records lines added to which parents;
637 * this line appears in the result.
638 * (-) line, which records from what parents the line
639 * was removed; this line does not appear in the result.
640 * then check the set of parents the result has difference
641 * from, from all lines. If there are lines that has
642 * different set of parents that the result has differences
643 * from, that means we have more than two versions.
645 * Even when we have only two versions, if the result does
646 * not match any of the parents, the it should be considered
647 * interesting. In such a case, we would have all '+' line.
648 * After passing the above "two versions" test, that would
649 * appear as "the same set of parents" to be "all parents".
653 for (j
= i
; j
< hunk_end
&& !has_interesting
; j
++) {
654 unsigned long this_diff
= sline
[j
].flag
& all_mask
;
655 struct lline
*ll
= sline
[j
].lost
;
657 /* This has some changes. Is it the
661 same_diff
= this_diff
;
662 else if (same_diff
!= this_diff
) {
667 while (ll
&& !has_interesting
) {
668 /* Lost this line from these parents;
669 * who are they? Are they the same?
671 this_diff
= ll
->parent_map
;
673 same_diff
= this_diff
;
674 else if (same_diff
!= this_diff
) {
681 if (!has_interesting
&& same_diff
!= all_mask
) {
682 /* This hunk is not that interesting after all */
683 for (j
= hunk_begin
; j
< hunk_end
; j
++)
684 sline
[j
].flag
&= ~mark
;
689 has_interesting
= give_context(sline
, cnt
, num_parent
);
690 return has_interesting
;
693 static void show_parent_lno(struct sline
*sline
, unsigned long l0
, unsigned long l1
, int n
, unsigned long null_context
)
695 l0
= sline
[l0
].p_lno
[n
];
696 l1
= sline
[l1
].p_lno
[n
];
697 printf(" -%lu,%lu", l0
, l1
-l0
-null_context
);
700 static int hunk_comment_line(const char *bol
)
707 return (isalpha(ch
) || ch
== '_' || ch
== '$');
710 static void show_line_to_eol(const char *line
, int len
, const char *reset
)
712 int saw_cr_at_eol
= 0;
715 saw_cr_at_eol
= (len
&& line
[len
-1] == '\r');
717 printf("%.*s%s%s\n", len
- saw_cr_at_eol
, line
,
719 saw_cr_at_eol
? "\r" : "");
722 static void dump_sline(struct sline
*sline
, const char *line_prefix
,
723 unsigned long cnt
, int num_parent
,
724 int use_color
, int result_deleted
)
726 unsigned long mark
= (1UL<<num_parent
);
727 unsigned long no_pre_delete
= (2UL<<num_parent
);
729 unsigned long lno
= 0;
730 const char *c_frag
= diff_get_color(use_color
, DIFF_FRAGINFO
);
731 const char *c_func
= diff_get_color(use_color
, DIFF_FUNCINFO
);
732 const char *c_new
= diff_get_color(use_color
, DIFF_FILE_NEW
);
733 const char *c_old
= diff_get_color(use_color
, DIFF_FILE_OLD
);
734 const char *c_context
= diff_get_color(use_color
, DIFF_CONTEXT
);
735 const char *c_reset
= diff_get_color(use_color
, DIFF_RESET
);
738 return; /* result deleted */
741 unsigned long hunk_end
;
742 unsigned long rlines
;
743 const char *hunk_comment
= NULL
;
744 unsigned long null_context
= 0;
746 while (lno
<= cnt
&& !(sline
[lno
].flag
& mark
)) {
747 if (hunk_comment_line(sline
[lno
].bol
))
748 hunk_comment
= sline
[lno
].bol
;
754 for (hunk_end
= lno
+ 1; hunk_end
<= cnt
; hunk_end
++)
755 if (!(sline
[hunk_end
].flag
& mark
))
758 rlines
= hunk_end
- lno
;
760 rlines
--; /* pointing at the last delete hunk */
764 * Even when running with --unified=0, all
765 * lines in the hunk needs to be processed in
766 * the loop below in order to show the
767 * deletion recorded in lost_head. However,
768 * we do not want to show the resulting line
769 * with all blank context markers in such a
773 for (j
= lno
; j
< hunk_end
; j
++)
774 if (!(sline
[j
].flag
& (mark
-1)))
776 rlines
-= null_context
;
779 printf("%s%s", line_prefix
, c_frag
);
780 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
781 for (i
= 0; i
< num_parent
; i
++)
782 show_parent_lno(sline
, lno
, hunk_end
, i
, null_context
);
783 printf(" +%lu,%lu ", lno
+1, rlines
);
784 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
788 for (i
= 0; i
< 40; i
++) {
789 int ch
= hunk_comment
[i
] & 0xff;
790 if (!ch
|| ch
== '\n')
796 printf("%s%s %s%s", c_reset
,
799 for (i
= 0; i
< comment_end
; i
++)
800 putchar(hunk_comment
[i
]);
803 printf("%s\n", c_reset
);
804 while (lno
< hunk_end
) {
807 unsigned long p_mask
;
808 struct sline
*sl
= &sline
[lno
++];
809 ll
= (sl
->flag
& no_pre_delete
) ? NULL
: sl
->lost
;
811 printf("%s%s", line_prefix
, c_old
);
812 for (j
= 0; j
< num_parent
; j
++) {
813 if (ll
->parent_map
& (1UL<<j
))
818 show_line_to_eol(ll
->line
, -1, c_reset
);
824 fputs(line_prefix
, stdout
);
825 if (!(sl
->flag
& (mark
-1))) {
827 * This sline was here to hang the
828 * lost lines in front of it.
832 fputs(c_context
, stdout
);
835 fputs(c_new
, stdout
);
836 for (j
= 0; j
< num_parent
; j
++) {
837 if (p_mask
& sl
->flag
)
843 show_line_to_eol(sl
->bol
, sl
->len
, c_reset
);
848 static void reuse_combine_diff(struct sline
*sline
, unsigned long cnt
,
851 /* We have already examined parent j and we know parent i
852 * and parent j are the same, so reuse the combined result
853 * of parent j for parent i.
855 unsigned long lno
, imask
, jmask
;
859 for (lno
= 0; lno
<= cnt
; lno
++) {
860 struct lline
*ll
= sline
->lost
;
861 sline
->p_lno
[i
] = sline
->p_lno
[j
];
863 if (ll
->parent_map
& jmask
)
864 ll
->parent_map
|= imask
;
867 if (sline
->flag
& jmask
)
868 sline
->flag
|= imask
;
871 /* the overall size of the file (sline[cnt]) */
872 sline
->p_lno
[i
] = sline
->p_lno
[j
];
875 static void dump_quoted_path(const char *head
,
878 const char *line_prefix
,
879 const char *c_meta
, const char *c_reset
)
881 static struct strbuf buf
= STRBUF_INIT
;
884 strbuf_addstr(&buf
, line_prefix
);
885 strbuf_addstr(&buf
, c_meta
);
886 strbuf_addstr(&buf
, head
);
887 quote_two_c_style(&buf
, prefix
, path
, 0);
888 strbuf_addstr(&buf
, c_reset
);
892 static void show_combined_header(struct combine_diff_path
*elem
,
895 struct rev_info
*rev
,
896 const char *line_prefix
,
898 int show_file_header
)
900 struct diff_options
*opt
= &rev
->diffopt
;
901 int abbrev
= opt
->flags
.full_index
? GIT_SHA1_HEXSZ
: DEFAULT_ABBREV
;
902 const char *a_prefix
= opt
->a_prefix
? opt
->a_prefix
: "a/";
903 const char *b_prefix
= opt
->b_prefix
? opt
->b_prefix
: "b/";
904 const char *c_meta
= diff_get_color_opt(opt
, DIFF_METAINFO
);
905 const char *c_reset
= diff_get_color_opt(opt
, DIFF_RESET
);
911 if (rev
->loginfo
&& !rev
->no_commit_id
)
914 dump_quoted_path(dense
? "diff --cc " : "diff --combined ",
915 "", elem
->path
, line_prefix
, c_meta
, c_reset
);
916 printf("%s%sindex ", line_prefix
, c_meta
);
917 for (i
= 0; i
< num_parent
; i
++) {
918 abb
= find_unique_abbrev(elem
->parent
[i
].oid
.hash
,
920 printf("%s%s", i
? "," : "", abb
);
922 abb
= find_unique_abbrev(elem
->oid
.hash
, abbrev
);
923 printf("..%s%s\n", abb
, c_reset
);
926 deleted
= !elem
->mode
;
928 /* We say it was added if nobody had it */
930 for (i
= 0; added
&& i
< num_parent
; i
++)
931 if (elem
->parent
[i
].status
!=
935 printf("%s%snew file mode %06o",
936 line_prefix
, c_meta
, elem
->mode
);
939 printf("%s%sdeleted file ",
940 line_prefix
, c_meta
);
942 for (i
= 0; i
< num_parent
; i
++) {
943 printf("%s%06o", i
? "," : "",
944 elem
->parent
[i
].mode
);
947 printf("..%06o", elem
->mode
);
949 printf("%s\n", c_reset
);
952 if (!show_file_header
)
956 dump_quoted_path("--- ", "", "/dev/null",
957 line_prefix
, c_meta
, c_reset
);
959 dump_quoted_path("--- ", a_prefix
, elem
->path
,
960 line_prefix
, c_meta
, c_reset
);
962 dump_quoted_path("+++ ", "", "/dev/null",
963 line_prefix
, c_meta
, c_reset
);
965 dump_quoted_path("+++ ", b_prefix
, elem
->path
,
966 line_prefix
, c_meta
, c_reset
);
969 static void show_patch_diff(struct combine_diff_path
*elem
, int num_parent
,
970 int dense
, int working_tree_file
,
971 struct rev_info
*rev
)
973 struct diff_options
*opt
= &rev
->diffopt
;
974 unsigned long result_size
, cnt
, lno
;
975 int result_deleted
= 0;
977 struct sline
*sline
; /* survived lines */
978 int mode_differs
= 0;
980 mmfile_t result_file
;
981 struct userdiff_driver
*userdiff
;
982 struct userdiff_driver
*textconv
= NULL
;
984 const char *line_prefix
= diff_line_prefix(opt
);
986 context
= opt
->context
;
987 userdiff
= userdiff_find_by_path(elem
->path
);
989 userdiff
= userdiff_find_by_name("default");
990 if (opt
->flags
.allow_textconv
)
991 textconv
= userdiff_get_textconv(userdiff
);
993 /* Read the result of merge first */
994 if (!working_tree_file
)
995 result
= grab_blob(&elem
->oid
, elem
->mode
, &result_size
,
996 textconv
, elem
->path
);
998 /* Used by diff-tree to read from the working tree */
1002 if (lstat(elem
->path
, &st
) < 0)
1005 if (S_ISLNK(st
.st_mode
)) {
1006 struct strbuf buf
= STRBUF_INIT
;
1008 if (strbuf_readlink(&buf
, elem
->path
, st
.st_size
) < 0) {
1009 error_errno("readlink(%s)", elem
->path
);
1012 result_size
= buf
.len
;
1013 result
= strbuf_detach(&buf
, NULL
);
1014 elem
->mode
= canon_mode(st
.st_mode
);
1015 } else if (S_ISDIR(st
.st_mode
)) {
1016 struct object_id oid
;
1017 if (resolve_gitlink_ref(elem
->path
, "HEAD", &oid
) < 0)
1018 result
= grab_blob(&elem
->oid
, elem
->mode
,
1019 &result_size
, NULL
, NULL
);
1021 result
= grab_blob(&oid
, elem
->mode
,
1022 &result_size
, NULL
, NULL
);
1023 } else if (textconv
) {
1024 struct diff_filespec
*df
= alloc_filespec(elem
->path
);
1025 fill_filespec(df
, &null_oid
, 0, st
.st_mode
);
1026 result_size
= fill_textconv(textconv
, df
, &result
);
1028 } else if (0 <= (fd
= open(elem
->path
, O_RDONLY
))) {
1029 size_t len
= xsize_t(st
.st_size
);
1033 elem
->mode
= canon_mode(st
.st_mode
);
1034 /* if symlinks don't work, assume symlink if all parents
1037 is_file
= has_symlinks
;
1038 for (i
= 0; !is_file
&& i
< num_parent
; i
++)
1039 is_file
= !S_ISLNK(elem
->parent
[i
].mode
);
1041 elem
->mode
= canon_mode(S_IFLNK
);
1044 result
= xmallocz(len
);
1046 done
= read_in_full(fd
, result
, len
);
1048 die_errno("read error '%s'", elem
->path
);
1049 else if (done
< len
)
1050 die("early EOF '%s'", elem
->path
);
1052 /* If not a fake symlink, apply filters, e.g. autocrlf */
1054 struct strbuf buf
= STRBUF_INIT
;
1056 if (convert_to_git(&the_index
, elem
->path
, result
, len
, &buf
, global_conv_flags_eol
)) {
1058 result
= strbuf_detach(&buf
, &len
);
1068 result
= xcalloc(1, 1);
1075 for (i
= 0; i
< num_parent
; i
++) {
1076 if (elem
->parent
[i
].mode
!= elem
->mode
) {
1084 else if (userdiff
->binary
!= -1)
1085 is_binary
= userdiff
->binary
;
1087 is_binary
= buffer_is_binary(result
, result_size
);
1088 for (i
= 0; !is_binary
&& i
< num_parent
; i
++) {
1091 buf
= grab_blob(&elem
->parent
[i
].oid
,
1092 elem
->parent
[i
].mode
,
1094 if (buffer_is_binary(buf
, size
))
1100 show_combined_header(elem
, num_parent
, dense
, rev
,
1101 line_prefix
, mode_differs
, 0);
1102 printf("Binary files differ\n");
1107 for (cnt
= 0, cp
= result
; cp
< result
+ result_size
; cp
++) {
1111 if (result_size
&& result
[result_size
-1] != '\n')
1112 cnt
++; /* incomplete line */
1114 sline
= xcalloc(st_add(cnt
, 2), sizeof(*sline
));
1115 sline
[0].bol
= result
;
1116 for (lno
= 0, cp
= result
; cp
< result
+ result_size
; cp
++) {
1118 sline
[lno
].len
= cp
- sline
[lno
].bol
;
1121 sline
[lno
].bol
= cp
+ 1;
1124 if (result_size
&& result
[result_size
-1] != '\n')
1125 sline
[cnt
-1].len
= result_size
- (sline
[cnt
-1].bol
- result
);
1127 result_file
.ptr
= result
;
1128 result_file
.size
= result_size
;
1130 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1131 * for deletion hunk at the end.
1133 sline
[0].p_lno
= xcalloc(st_mult(st_add(cnt
, 2), num_parent
), sizeof(unsigned long));
1134 for (lno
= 0; lno
<= cnt
; lno
++)
1135 sline
[lno
+1].p_lno
= sline
[lno
].p_lno
+ num_parent
;
1137 for (i
= 0; i
< num_parent
; i
++) {
1139 for (j
= 0; j
< i
; j
++) {
1140 if (!oidcmp(&elem
->parent
[i
].oid
,
1141 &elem
->parent
[j
].oid
)) {
1142 reuse_combine_diff(sline
, cnt
, i
, j
);
1147 combine_diff(&elem
->parent
[i
].oid
,
1148 elem
->parent
[i
].mode
,
1149 &result_file
, sline
,
1150 cnt
, i
, num_parent
, result_deleted
,
1151 textconv
, elem
->path
, opt
->xdl_opts
);
1154 show_hunks
= make_hunks(sline
, cnt
, num_parent
, dense
);
1156 if (show_hunks
|| mode_differs
|| working_tree_file
) {
1157 show_combined_header(elem
, num_parent
, dense
, rev
,
1158 line_prefix
, mode_differs
, 1);
1159 dump_sline(sline
, line_prefix
, cnt
, num_parent
,
1160 opt
->use_color
, result_deleted
);
1164 for (lno
= 0; lno
< cnt
; lno
++) {
1165 if (sline
[lno
].lost
) {
1166 struct lline
*ll
= sline
[lno
].lost
;
1168 struct lline
*tmp
= ll
;
1174 free(sline
[0].p_lno
);
1178 static void show_raw_diff(struct combine_diff_path
*p
, int num_parent
, struct rev_info
*rev
)
1180 struct diff_options
*opt
= &rev
->diffopt
;
1181 int line_termination
, inter_name_termination
, i
;
1182 const char *line_prefix
= diff_line_prefix(opt
);
1184 line_termination
= opt
->line_termination
;
1185 inter_name_termination
= '\t';
1186 if (!line_termination
)
1187 inter_name_termination
= 0;
1189 if (rev
->loginfo
&& !rev
->no_commit_id
)
1193 if (opt
->output_format
& DIFF_FORMAT_RAW
) {
1194 printf("%s", line_prefix
);
1196 /* As many colons as there are parents */
1197 for (i
= 0; i
< num_parent
; i
++)
1200 /* Show the modes */
1201 for (i
= 0; i
< num_parent
; i
++)
1202 printf("%06o ", p
->parent
[i
].mode
);
1203 printf("%06o", p
->mode
);
1206 for (i
= 0; i
< num_parent
; i
++)
1207 printf(" %s", diff_aligned_abbrev(&p
->parent
[i
].oid
,
1209 printf(" %s ", diff_aligned_abbrev(&p
->oid
, opt
->abbrev
));
1212 if (opt
->output_format
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
)) {
1213 for (i
= 0; i
< num_parent
; i
++)
1214 putchar(p
->parent
[i
].status
);
1215 putchar(inter_name_termination
);
1218 write_name_quoted(p
->path
, stdout
, line_termination
);
1222 * The result (p->elem) is from the working tree and their
1223 * parents are typically from multiple stages during a merge
1224 * (i.e. diff-files) or the state in HEAD and in the index
1225 * (i.e. diff-index).
1227 void show_combined_diff(struct combine_diff_path
*p
,
1230 struct rev_info
*rev
)
1232 struct diff_options
*opt
= &rev
->diffopt
;
1234 if (opt
->output_format
& (DIFF_FORMAT_RAW
|
1236 DIFF_FORMAT_NAME_STATUS
))
1237 show_raw_diff(p
, num_parent
, rev
);
1238 else if (opt
->output_format
& DIFF_FORMAT_PATCH
)
1239 show_patch_diff(p
, num_parent
, dense
, 1, rev
);
1242 static void free_combined_pair(struct diff_filepair
*pair
)
1249 * A combine_diff_path expresses N parents on the LHS against 1 merge
1250 * result. Synthesize a diff_filepair that has N entries on the "one"
1251 * side and 1 entry on the "two" side.
1253 * In the future, we might want to add more data to combine_diff_path
1254 * so that we can fill fields we are ignoring (most notably, size) here,
1255 * but currently nobody uses it, so this should suffice for now.
1257 static struct diff_filepair
*combined_pair(struct combine_diff_path
*p
,
1261 struct diff_filepair
*pair
;
1262 struct diff_filespec
*pool
;
1264 pair
= xmalloc(sizeof(*pair
));
1265 pool
= xcalloc(st_add(num_parent
, 1), sizeof(struct diff_filespec
));
1266 pair
->one
= pool
+ 1;
1269 for (i
= 0; i
< num_parent
; i
++) {
1270 pair
->one
[i
].path
= p
->path
;
1271 pair
->one
[i
].mode
= p
->parent
[i
].mode
;
1272 oidcpy(&pair
->one
[i
].oid
, &p
->parent
[i
].oid
);
1273 pair
->one
[i
].oid_valid
= !is_null_oid(&p
->parent
[i
].oid
);
1274 pair
->one
[i
].has_more_entries
= 1;
1276 pair
->one
[num_parent
- 1].has_more_entries
= 0;
1278 pair
->two
->path
= p
->path
;
1279 pair
->two
->mode
= p
->mode
;
1280 oidcpy(&pair
->two
->oid
, &p
->oid
);
1281 pair
->two
->oid_valid
= !is_null_oid(&p
->oid
);
1285 static void handle_combined_callback(struct diff_options
*opt
,
1286 struct combine_diff_path
*paths
,
1290 struct combine_diff_path
*p
;
1291 struct diff_queue_struct q
;
1294 q
.queue
= xcalloc(num_paths
, sizeof(struct diff_filepair
*));
1295 q
.alloc
= num_paths
;
1297 for (i
= 0, p
= paths
; p
; p
= p
->next
)
1298 q
.queue
[i
++] = combined_pair(p
, num_parent
);
1299 opt
->format_callback(&q
, opt
, opt
->format_callback_data
);
1300 for (i
= 0; i
< num_paths
; i
++)
1301 free_combined_pair(q
.queue
[i
]);
1305 static const char *path_path(void *obj
)
1307 struct combine_diff_path
*path
= (struct combine_diff_path
*)obj
;
1313 /* find set of paths that every parent touches */
1314 static struct combine_diff_path
*find_paths_generic(const struct object_id
*oid
,
1315 const struct oid_array
*parents
, struct diff_options
*opt
)
1317 struct combine_diff_path
*paths
= NULL
;
1318 int i
, num_parent
= parents
->nr
;
1320 int output_format
= opt
->output_format
;
1321 const char *orderfile
= opt
->orderfile
;
1323 opt
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
1324 /* tell diff_tree to emit paths in sorted (=tree) order */
1325 opt
->orderfile
= NULL
;
1327 /* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn) (wrt paths) */
1328 for (i
= 0; i
< num_parent
; i
++) {
1330 * show stat against the first parent even when doing
1333 int stat_opt
= (output_format
&
1334 (DIFF_FORMAT_NUMSTAT
|DIFF_FORMAT_DIFFSTAT
));
1335 if (i
== 0 && stat_opt
)
1336 opt
->output_format
= stat_opt
;
1338 opt
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
1339 diff_tree_oid(&parents
->oid
[i
], oid
, "", opt
);
1341 paths
= intersect_paths(paths
, i
, num_parent
);
1343 /* if showing diff, show it in requested order */
1344 if (opt
->output_format
!= DIFF_FORMAT_NO_OUTPUT
&&
1346 diffcore_order(orderfile
);
1352 opt
->output_format
= output_format
;
1353 opt
->orderfile
= orderfile
;
1359 * find set of paths that everybody touches, assuming diff is run without
1360 * rename/copy detection, etc, comparing all trees simultaneously (= faster).
1362 static struct combine_diff_path
*find_paths_multitree(
1363 const struct object_id
*oid
, const struct oid_array
*parents
,
1364 struct diff_options
*opt
)
1366 int i
, nparent
= parents
->nr
;
1367 const struct object_id
**parents_oid
;
1368 struct combine_diff_path paths_head
;
1371 ALLOC_ARRAY(parents_oid
, nparent
);
1372 for (i
= 0; i
< nparent
; i
++)
1373 parents_oid
[i
] = &parents
->oid
[i
];
1375 /* fake list head, so worker can assume it is non-NULL */
1376 paths_head
.next
= NULL
;
1378 strbuf_init(&base
, PATH_MAX
);
1379 diff_tree_paths(&paths_head
, oid
, parents_oid
, nparent
, &base
, opt
);
1381 strbuf_release(&base
);
1383 return paths_head
.next
;
1387 void diff_tree_combined(const struct object_id
*oid
,
1388 const struct oid_array
*parents
,
1390 struct rev_info
*rev
)
1392 struct diff_options
*opt
= &rev
->diffopt
;
1393 struct diff_options diffopts
;
1394 struct combine_diff_path
*p
, *paths
;
1395 int i
, num_paths
, needsep
, show_log_first
, num_parent
= parents
->nr
;
1396 int need_generic_pathscan
;
1398 /* nothing to do, if no parents */
1402 show_log_first
= !!rev
->loginfo
&& !rev
->no_commit_id
;
1404 if (show_log_first
) {
1407 if (rev
->verbose_header
&& opt
->output_format
&&
1408 opt
->output_format
!= DIFF_FORMAT_NO_OUTPUT
&&
1409 !commit_format_is_empty(rev
->commit_format
))
1410 printf("%s%c", diff_line_prefix(opt
),
1411 opt
->line_termination
);
1415 copy_pathspec(&diffopts
.pathspec
, &opt
->pathspec
);
1416 diffopts
.flags
.recursive
= 1;
1417 diffopts
.flags
.allow_external
= 0;
1419 /* find set of paths that everybody touches
1423 * Diffcore transformations are bound to diff_filespec and logic
1424 * comparing two entries - i.e. they do not apply directly to combine
1427 * If some of such transformations is requested - we launch generic
1428 * path scanning, which works significantly slower compared to
1429 * simultaneous all-trees-in-one-go scan in find_paths_multitree().
1431 * TODO some of the filters could be ported to work on
1432 * combine_diff_paths - i.e. all functionality that skips paths, so in
1433 * theory, we could end up having only multitree path scanning.
1435 * NOTE please keep this semantically in sync with diffcore_std()
1437 need_generic_pathscan
= opt
->skip_stat_unmatch
||
1438 opt
->flags
.follow_renames
||
1439 opt
->break_opt
!= -1 ||
1440 opt
->detect_rename
||
1441 (opt
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
) ||
1445 if (need_generic_pathscan
) {
1447 * NOTE generic case also handles --stat, as it computes
1448 * diff(sha1,parent_i) for all i to do the job, specifically
1451 paths
= find_paths_generic(oid
, parents
, &diffopts
);
1455 paths
= find_paths_multitree(oid
, parents
, &diffopts
);
1458 * show stat against the first parent even
1459 * when doing combined diff.
1461 stat_opt
= (opt
->output_format
&
1462 (DIFF_FORMAT_NUMSTAT
|DIFF_FORMAT_DIFFSTAT
));
1464 diffopts
.output_format
= stat_opt
;
1466 diff_tree_oid(&parents
->oid
[0], oid
, "", &diffopts
);
1467 diffcore_std(&diffopts
);
1469 diffcore_order(opt
->orderfile
);
1470 diff_flush(&diffopts
);
1474 /* find out number of surviving paths */
1475 for (num_paths
= 0, p
= paths
; p
; p
= p
->next
)
1478 /* order paths according to diffcore_order */
1479 if (opt
->orderfile
&& num_paths
) {
1480 struct obj_order
*o
;
1482 ALLOC_ARRAY(o
, num_paths
);
1483 for (i
= 0, p
= paths
; p
; p
= p
->next
, i
++)
1485 order_objects(opt
->orderfile
, path_path
, o
, num_paths
);
1486 for (i
= 0; i
< num_paths
- 1; i
++) {
1488 p
->next
= o
[i
+1].obj
;
1491 p
= o
[num_paths
-1].obj
;
1499 if (opt
->output_format
& (DIFF_FORMAT_RAW
|
1501 DIFF_FORMAT_NAME_STATUS
)) {
1502 for (p
= paths
; p
; p
= p
->next
)
1503 show_raw_diff(p
, num_parent
, rev
);
1506 else if (opt
->output_format
&
1507 (DIFF_FORMAT_NUMSTAT
|DIFF_FORMAT_DIFFSTAT
))
1509 else if (opt
->output_format
& DIFF_FORMAT_CALLBACK
)
1510 handle_combined_callback(opt
, paths
, num_parent
, num_paths
);
1512 if (opt
->output_format
& DIFF_FORMAT_PATCH
) {
1514 printf("%s%c", diff_line_prefix(opt
),
1515 opt
->line_termination
);
1516 for (p
= paths
; p
; p
= p
->next
)
1517 show_patch_diff(p
, num_parent
, dense
,
1522 /* Clean things up */
1524 struct combine_diff_path
*tmp
= paths
;
1525 paths
= paths
->next
;
1529 clear_pathspec(&diffopts
.pathspec
);
1532 void diff_tree_combined_merge(const struct commit
*commit
, int dense
,
1533 struct rev_info
*rev
)
1535 struct commit_list
*parent
= get_saved_parents(rev
, commit
);
1536 struct oid_array parents
= OID_ARRAY_INIT
;
1539 oid_array_append(&parents
, &parent
->item
->object
.oid
);
1540 parent
= parent
->next
;
1542 diff_tree_combined(&commit
->object
.oid
, &parents
, dense
, rev
);
1543 oid_array_clear(&parents
);