2 #include "object-store.h"
9 #include "xdiff-interface.h"
10 #include "xdiff/xmacros.h"
14 #include "oid-array.h"
17 static int compare_paths(const struct combine_diff_path
*one
,
18 const struct diff_filespec
*two
)
20 if (!S_ISDIR(one
->mode
) && !S_ISDIR(two
->mode
))
21 return strcmp(one
->path
, two
->path
);
23 return base_name_compare(one
->path
, strlen(one
->path
), one
->mode
,
24 two
->path
, strlen(two
->path
), two
->mode
);
27 static int filename_changed(char status
)
29 return status
== 'R' || status
== 'C';
32 static struct combine_diff_path
*intersect_paths(
33 struct combine_diff_path
*curr
,
36 int combined_all_paths
)
38 struct diff_queue_struct
*q
= &diff_queued_diff
;
39 struct combine_diff_path
*p
, **tail
= &curr
;
43 for (i
= 0; i
< q
->nr
; i
++) {
46 if (diff_unmodified_pair(q
->queue
[i
]))
48 path
= q
->queue
[i
]->two
->path
;
50 p
= xmalloc(combine_diff_path_size(num_parent
, len
));
51 p
->path
= (char *) &(p
->parent
[num_parent
]);
52 memcpy(p
->path
, path
, len
);
56 sizeof(p
->parent
[0]) * num_parent
);
58 oidcpy(&p
->oid
, &q
->queue
[i
]->two
->oid
);
59 p
->mode
= q
->queue
[i
]->two
->mode
;
60 oidcpy(&p
->parent
[n
].oid
, &q
->queue
[i
]->one
->oid
);
61 p
->parent
[n
].mode
= q
->queue
[i
]->one
->mode
;
62 p
->parent
[n
].status
= q
->queue
[i
]->status
;
64 if (combined_all_paths
&&
65 filename_changed(p
->parent
[n
].status
)) {
66 strbuf_init(&p
->parent
[n
].path
, 0);
67 strbuf_addstr(&p
->parent
[n
].path
,
68 q
->queue
[i
]->one
->path
);
77 * paths in curr (linked list) and q->queue[] (array) are
78 * both sorted in the tree order.
81 while ((p
= *tail
) != NULL
) {
83 ? -1 : compare_paths(p
, q
->queue
[i
]->two
));
86 /* p->path not in q->queue[]; drop it */
88 for (j
= 0; j
< num_parent
; j
++)
89 if (combined_all_paths
&&
90 filename_changed(p
->parent
[j
].status
))
91 strbuf_release(&p
->parent
[j
].path
);
97 /* q->queue[i] not in p->path; skip it */
102 oidcpy(&p
->parent
[n
].oid
, &q
->queue
[i
]->one
->oid
);
103 p
->parent
[n
].mode
= q
->queue
[i
]->one
->mode
;
104 p
->parent
[n
].status
= q
->queue
[i
]->status
;
105 if (combined_all_paths
&&
106 filename_changed(p
->parent
[n
].status
))
107 strbuf_addstr(&p
->parent
[n
].path
,
108 q
->queue
[i
]->one
->path
);
116 /* Lines lost from parent */
118 struct lline
*next
, *prev
;
120 unsigned long parent_map
;
121 char line
[FLEX_ARRAY
];
124 /* Lines lost from current parent (before coalescing) */
126 struct lline
*lost_head
, *lost_tail
;
130 /* Lines surviving in the merge result */
132 /* Accumulated and coalesced lost lines */
138 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
139 * we did not change it).
140 * bit N is used for "interesting" lines, including context.
141 * bit (N+1) is used for "do not show deletion before this".
144 unsigned long *p_lno
;
147 static int match_string_spaces(const char *line1
, int len1
,
148 const char *line2
, int len2
,
151 if (flags
& XDF_WHITESPACE_FLAGS
) {
152 for (; len1
> 0 && XDL_ISSPACE(line1
[len1
- 1]); len1
--);
153 for (; len2
> 0 && XDL_ISSPACE(line2
[len2
- 1]); len2
--);
156 if (!(flags
& (XDF_IGNORE_WHITESPACE
| XDF_IGNORE_WHITESPACE_CHANGE
)))
157 return (len1
== len2
&& !memcmp(line1
, line2
, len1
));
159 while (len1
> 0 && len2
> 0) {
162 if (XDL_ISSPACE(line1
[len1
]) || XDL_ISSPACE(line2
[len2
])) {
163 if ((flags
& XDF_IGNORE_WHITESPACE_CHANGE
) &&
164 (!XDL_ISSPACE(line1
[len1
]) || !XDL_ISSPACE(line2
[len2
])))
167 for (; len1
> 0 && XDL_ISSPACE(line1
[len1
]); len1
--);
168 for (; len2
> 0 && XDL_ISSPACE(line2
[len2
]); len2
--);
170 if (line1
[len1
] != line2
[len2
])
174 if (flags
& XDF_IGNORE_WHITESPACE
) {
175 /* Consume remaining spaces */
176 for (; len1
> 0 && XDL_ISSPACE(line1
[len1
- 1]); len1
--);
177 for (; len2
> 0 && XDL_ISSPACE(line2
[len2
- 1]); len2
--);
180 /* We matched full line1 and line2 */
187 enum coalesce_direction
{ MATCH
, BASE
, NEW
};
189 /* Coalesce new lines into base by finding LCS */
190 static struct lline
*coalesce_lines(struct lline
*base
, int *lenbase
,
191 struct lline
*newline
, int lennew
,
192 unsigned long parent
, long flags
)
195 enum coalesce_direction
**directions
;
196 struct lline
*baseend
, *newend
= NULL
;
197 int i
, j
, origbaselen
= *lenbase
;
208 * Coalesce new lines into base by finding the LCS
209 * - Create the table to run dynamic programming
211 * - Then reverse read the direction structure:
212 * - If we have MATCH, assign parent to base flag, and consume
213 * both baseend and newend
214 * - Else if we have BASE, consume baseend
215 * - Else if we have NEW, insert newend lline into base and
218 CALLOC_ARRAY(lcs
, st_add(origbaselen
, 1));
219 CALLOC_ARRAY(directions
, st_add(origbaselen
, 1));
220 for (i
= 0; i
< origbaselen
+ 1; i
++) {
221 CALLOC_ARRAY(lcs
[i
], st_add(lennew
, 1));
222 CALLOC_ARRAY(directions
[i
], st_add(lennew
, 1));
223 directions
[i
][0] = BASE
;
225 for (j
= 1; j
< lennew
+ 1; j
++)
226 directions
[0][j
] = NEW
;
228 for (i
= 1, baseend
= base
; i
< origbaselen
+ 1; i
++) {
229 for (j
= 1, newend
= newline
; j
< lennew
+ 1; j
++) {
230 if (match_string_spaces(baseend
->line
, baseend
->len
,
231 newend
->line
, newend
->len
, flags
)) {
232 lcs
[i
][j
] = lcs
[i
- 1][j
- 1] + 1;
233 directions
[i
][j
] = MATCH
;
234 } else if (lcs
[i
][j
- 1] >= lcs
[i
- 1][j
]) {
235 lcs
[i
][j
] = lcs
[i
][j
- 1];
236 directions
[i
][j
] = NEW
;
238 lcs
[i
][j
] = lcs
[i
- 1][j
];
239 directions
[i
][j
] = BASE
;
242 newend
= newend
->next
;
245 baseend
= baseend
->next
;
248 for (i
= 0; i
< origbaselen
+ 1; i
++)
252 /* At this point, baseend and newend point to the end of each lists */
255 while (i
!= 0 || j
!= 0) {
256 if (directions
[i
][j
] == MATCH
) {
257 baseend
->parent_map
|= 1<<parent
;
258 baseend
= baseend
->prev
;
259 newend
= newend
->prev
;
262 } else if (directions
[i
][j
] == NEW
) {
266 /* Remove lline from new list and update newend */
268 lline
->prev
->next
= lline
->next
;
270 newline
= lline
->next
;
272 lline
->next
->prev
= lline
->prev
;
274 newend
= lline
->prev
;
277 /* Add lline to base list */
279 lline
->next
= baseend
->next
;
280 lline
->prev
= baseend
;
282 lline
->prev
->next
= lline
;
291 lline
->next
->prev
= lline
;
294 baseend
= baseend
->prev
;
301 struct lline
*lline
= newend
;
302 newend
= newend
->next
;
306 for (i
= 0; i
< origbaselen
+ 1; i
++)
313 static char *grab_blob(struct repository
*r
,
314 const struct object_id
*oid
, unsigned int mode
,
315 unsigned long *size
, struct userdiff_driver
*textconv
,
319 enum object_type type
;
321 if (S_ISGITLINK(mode
)) {
322 struct strbuf buf
= STRBUF_INIT
;
323 strbuf_addf(&buf
, "Subproject commit %s\n", oid_to_hex(oid
));
325 blob
= strbuf_detach(&buf
, NULL
);
326 } else if (is_null_oid(oid
)) {
329 return xcalloc(1, 1);
330 } else if (textconv
) {
331 struct diff_filespec
*df
= alloc_filespec(path
);
332 fill_filespec(df
, oid
, 1, mode
);
333 *size
= fill_textconv(r
, textconv
, df
, &blob
);
336 blob
= read_object_file(oid
, &type
, size
);
337 if (type
!= OBJ_BLOB
)
338 die("object '%s' is not a blob!", oid_to_hex(oid
));
343 static void append_lost(struct sline
*sline
, int n
, const char *line
, int len
)
346 unsigned long this_mask
= (1UL<<n
);
347 if (line
[len
-1] == '\n')
350 FLEX_ALLOC_MEM(lline
, line
, line
, len
);
353 lline
->prev
= sline
->plost
.lost_tail
;
355 lline
->prev
->next
= lline
;
357 sline
->plost
.lost_head
= lline
;
358 sline
->plost
.lost_tail
= lline
;
360 lline
->parent_map
= this_mask
;
363 struct combine_diff_state
{
370 struct sline
*lost_bucket
;
373 static void consume_hunk(void *state_
,
376 const char *func UNUSED
, long funclen UNUSED
)
378 struct combine_diff_state
*state
= state_
;
384 state
->lno
= state
->nb
;
385 if (state
->nn
== 0) {
386 /* @@ -X,Y +N,0 @@ removed Y lines
387 * that would have come *after* line N
388 * in the result. Our lost buckets hang
389 * to the line after the removed lines,
391 * Note that this is correct even when N == 0,
392 * in which case the hunk removes the first
395 state
->lost_bucket
= &state
->sline
[state
->nb
];
399 state
->lost_bucket
= &state
->sline
[state
->nb
-1];
401 if (!state
->sline
[state
->nb
-1].p_lno
)
402 CALLOC_ARRAY(state
->sline
[state
->nb
- 1].p_lno
,
404 state
->sline
[state
->nb
-1].p_lno
[state
->n
] = state
->ob
;
407 static int consume_line(void *state_
, char *line
, unsigned long len
)
409 struct combine_diff_state
*state
= state_
;
410 if (!state
->lost_bucket
)
411 return 0; /* not in any hunk yet */
414 append_lost(state
->lost_bucket
, state
->n
, line
+1, len
-1);
417 state
->sline
[state
->lno
-1].flag
|= state
->nmask
;
424 static void combine_diff(struct repository
*r
,
425 const struct object_id
*parent
, unsigned int mode
,
426 mmfile_t
*result_file
,
427 struct sline
*sline
, unsigned int cnt
, int n
,
428 int num_parent
, int result_deleted
,
429 struct userdiff_driver
*textconv
,
430 const char *path
, long flags
)
432 unsigned int p_lno
, lno
;
433 unsigned long nmask
= (1UL << n
);
436 mmfile_t parent_file
;
437 struct combine_diff_state state
;
441 return; /* result deleted */
443 parent_file
.ptr
= grab_blob(r
, parent
, mode
, &sz
, textconv
, path
);
444 parent_file
.size
= sz
;
445 memset(&xpp
, 0, sizeof(xpp
));
447 memset(&xecfg
, 0, sizeof(xecfg
));
448 memset(&state
, 0, sizeof(state
));
452 state
.num_parent
= num_parent
;
455 if (xdi_diff_outf(&parent_file
, result_file
, consume_hunk
,
456 consume_line
, &state
, &xpp
, &xecfg
))
457 die("unable to generate combined diff for %s",
459 free(parent_file
.ptr
);
461 /* Assign line numbers for this parent.
463 * sline[lno].p_lno[n] records the first line number
464 * (counting from 1) for parent N if the final hunk display
465 * started by showing sline[lno] (possibly showing the lost
466 * lines attached to it first).
468 for (lno
= 0, p_lno
= 1; lno
<= cnt
; lno
++) {
470 sline
[lno
].p_lno
[n
] = p_lno
;
472 /* Coalesce new lines */
473 if (sline
[lno
].plost
.lost_head
) {
474 struct sline
*sl
= &sline
[lno
];
475 sl
->lost
= coalesce_lines(sl
->lost
, &sl
->lenlost
,
477 sl
->plost
.len
, n
, flags
);
478 sl
->plost
.lost_head
= sl
->plost
.lost_tail
= NULL
;
482 /* How many lines would this sline advance the p_lno? */
483 ll
= sline
[lno
].lost
;
485 if (ll
->parent_map
& nmask
)
486 p_lno
++; /* '-' means parent had it */
489 if (lno
< cnt
&& !(sline
[lno
].flag
& nmask
))
490 p_lno
++; /* no '+' means parent had it */
492 sline
[lno
].p_lno
[n
] = p_lno
; /* trailer */
495 static unsigned long context
= 3;
496 static char combine_marker
= '@';
498 static int interesting(struct sline
*sline
, unsigned long all_mask
)
500 /* If some parents lost lines here, or if we have added to
501 * some parent, it is interesting.
503 return ((sline
->flag
& all_mask
) || sline
->lost
);
506 static unsigned long adjust_hunk_tail(struct sline
*sline
,
507 unsigned long all_mask
,
508 unsigned long hunk_begin
,
511 /* i points at the first uninteresting line. If the last line
512 * of the hunk was interesting only because it has some
513 * deletion, then it is not all that interesting for the
514 * purpose of giving trailing context lines. This is because
515 * we output '-' line and then unmodified sline[i-1] itself in
516 * that case which gives us one extra context line.
518 if ((hunk_begin
+ 1 <= i
) && !(sline
[i
-1].flag
& all_mask
))
523 static unsigned long find_next(struct sline
*sline
,
527 int look_for_uninteresting
)
529 /* We have examined up to i-1 and are about to look at i.
530 * Find next interesting or uninteresting line. Here,
531 * "interesting" does not mean interesting(), but marked by
532 * the give_context() function below (i.e. it includes context
533 * lines that are not interesting to interesting() function
534 * that are surrounded by interesting() ones.
537 if (look_for_uninteresting
538 ? !(sline
[i
].flag
& mark
)
539 : (sline
[i
].flag
& mark
))
546 static int give_context(struct sline
*sline
, unsigned long cnt
, int num_parent
)
548 unsigned long all_mask
= (1UL<<num_parent
) - 1;
549 unsigned long mark
= (1UL<<num_parent
);
550 unsigned long no_pre_delete
= (2UL<<num_parent
);
553 /* Two groups of interesting lines may have a short gap of
554 * uninteresting lines. Connect such groups to give them a
557 * We first start from what the interesting() function says,
558 * and mark them with "mark", and paint context lines with the
559 * mark. So interesting() would still say false for such context
560 * lines but they are treated as "interesting" in the end.
562 i
= find_next(sline
, mark
, 0, cnt
, 0);
567 unsigned long j
= (context
< i
) ? (i
- context
) : 0;
570 /* Paint a few lines before the first interesting line. */
572 if (!(sline
[j
].flag
& mark
))
573 sline
[j
].flag
|= no_pre_delete
;
574 sline
[j
++].flag
|= mark
;
578 /* we know up to i is to be included. where does the
579 * next uninteresting one start?
581 j
= find_next(sline
, mark
, i
, cnt
, 1);
583 break; /* the rest are all interesting */
585 /* lookahead context lines */
586 k
= find_next(sline
, mark
, j
, cnt
, 0);
587 j
= adjust_hunk_tail(sline
, all_mask
, i
, j
);
589 if (k
< j
+ context
) {
590 /* k is interesting and [j,k) are not, but
591 * paint them interesting because the gap is small.
594 sline
[j
++].flag
|= mark
;
599 /* j is the first uninteresting line and there is
600 * no overlap beyond it within context lines. Paint
601 * the trailing edge a bit.
604 k
= (j
+ context
< cnt
+1) ? j
+ context
: cnt
+1;
606 sline
[j
++].flag
|= mark
;
611 static int make_hunks(struct sline
*sline
, unsigned long cnt
,
612 int num_parent
, int dense
)
614 unsigned long all_mask
= (1UL<<num_parent
) - 1;
615 unsigned long mark
= (1UL<<num_parent
);
617 int has_interesting
= 0;
619 for (i
= 0; i
<= cnt
; i
++) {
620 if (interesting(&sline
[i
], all_mask
))
621 sline
[i
].flag
|= mark
;
623 sline
[i
].flag
&= ~mark
;
626 return give_context(sline
, cnt
, num_parent
);
628 /* Look at each hunk, and if we have changes from only one
629 * parent, or the changes are the same from all but one
630 * parent, mark that uninteresting.
634 unsigned long j
, hunk_begin
, hunk_end
;
635 unsigned long same_diff
;
636 while (i
<= cnt
&& !(sline
[i
].flag
& mark
))
639 break; /* No more interesting hunks */
641 for (j
= i
+ 1; j
<= cnt
; j
++) {
642 if (!(sline
[j
].flag
& mark
)) {
643 /* Look beyond the end to see if there
644 * is an interesting line after this
645 * hunk within context span.
647 unsigned long la
; /* lookahead */
649 la
= adjust_hunk_tail(sline
, all_mask
,
651 la
= (la
+ context
< cnt
+ 1) ?
652 (la
+ context
) : cnt
+ 1;
653 while (la
&& j
<= --la
) {
654 if (sline
[la
].flag
& mark
) {
666 /* [i..hunk_end) are interesting. Now is it really
667 * interesting? We check if there are only two versions
668 * and the result matches one of them. That is, we look
670 * (+) line, which records lines added to which parents;
671 * this line appears in the result.
672 * (-) line, which records from what parents the line
673 * was removed; this line does not appear in the result.
674 * then check the set of parents the result has difference
675 * from, from all lines. If there are lines that has
676 * different set of parents that the result has differences
677 * from, that means we have more than two versions.
679 * Even when we have only two versions, if the result does
680 * not match any of the parents, the it should be considered
681 * interesting. In such a case, we would have all '+' line.
682 * After passing the above "two versions" test, that would
683 * appear as "the same set of parents" to be "all parents".
687 for (j
= i
; j
< hunk_end
&& !has_interesting
; j
++) {
688 unsigned long this_diff
= sline
[j
].flag
& all_mask
;
689 struct lline
*ll
= sline
[j
].lost
;
691 /* This has some changes. Is it the
695 same_diff
= this_diff
;
696 else if (same_diff
!= this_diff
) {
701 while (ll
&& !has_interesting
) {
702 /* Lost this line from these parents;
703 * who are they? Are they the same?
705 this_diff
= ll
->parent_map
;
707 same_diff
= this_diff
;
708 else if (same_diff
!= this_diff
) {
715 if (!has_interesting
&& same_diff
!= all_mask
) {
716 /* This hunk is not that interesting after all */
717 for (j
= hunk_begin
; j
< hunk_end
; j
++)
718 sline
[j
].flag
&= ~mark
;
723 has_interesting
= give_context(sline
, cnt
, num_parent
);
724 return has_interesting
;
727 static void show_parent_lno(struct sline
*sline
, unsigned long l0
, unsigned long l1
, int n
, unsigned long null_context
)
729 l0
= sline
[l0
].p_lno
[n
];
730 l1
= sline
[l1
].p_lno
[n
];
731 printf(" -%lu,%lu", l0
, l1
-l0
-null_context
);
734 static int hunk_comment_line(const char *bol
)
741 return (isalpha(ch
) || ch
== '_' || ch
== '$');
744 static void show_line_to_eol(const char *line
, int len
, const char *reset
)
746 int saw_cr_at_eol
= 0;
749 saw_cr_at_eol
= (len
&& line
[len
-1] == '\r');
751 printf("%.*s%s%s\n", len
- saw_cr_at_eol
, line
,
753 saw_cr_at_eol
? "\r" : "");
756 static void dump_sline(struct sline
*sline
, const char *line_prefix
,
757 unsigned long cnt
, int num_parent
,
758 int use_color
, int result_deleted
)
760 unsigned long mark
= (1UL<<num_parent
);
761 unsigned long no_pre_delete
= (2UL<<num_parent
);
763 unsigned long lno
= 0;
764 const char *c_frag
= diff_get_color(use_color
, DIFF_FRAGINFO
);
765 const char *c_func
= diff_get_color(use_color
, DIFF_FUNCINFO
);
766 const char *c_new
= diff_get_color(use_color
, DIFF_FILE_NEW
);
767 const char *c_old
= diff_get_color(use_color
, DIFF_FILE_OLD
);
768 const char *c_context
= diff_get_color(use_color
, DIFF_CONTEXT
);
769 const char *c_reset
= diff_get_color(use_color
, DIFF_RESET
);
772 return; /* result deleted */
775 unsigned long hunk_end
;
776 unsigned long rlines
;
777 const char *hunk_comment
= NULL
;
778 unsigned long null_context
= 0;
780 while (lno
<= cnt
&& !(sline
[lno
].flag
& mark
)) {
781 if (hunk_comment_line(sline
[lno
].bol
))
782 hunk_comment
= sline
[lno
].bol
;
788 for (hunk_end
= lno
+ 1; hunk_end
<= cnt
; hunk_end
++)
789 if (!(sline
[hunk_end
].flag
& mark
))
792 rlines
= hunk_end
- lno
;
794 rlines
--; /* pointing at the last delete hunk */
798 * Even when running with --unified=0, all
799 * lines in the hunk needs to be processed in
800 * the loop below in order to show the
801 * deletion recorded in lost_head. However,
802 * we do not want to show the resulting line
803 * with all blank context markers in such a
807 for (j
= lno
; j
< hunk_end
; j
++)
808 if (!(sline
[j
].flag
& (mark
-1)))
810 rlines
-= null_context
;
813 printf("%s%s", line_prefix
, c_frag
);
814 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
815 for (i
= 0; i
< num_parent
; i
++)
816 show_parent_lno(sline
, lno
, hunk_end
, i
, null_context
);
817 printf(" +%lu,%lu ", lno
+1, rlines
);
818 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
822 for (i
= 0; i
< 40; i
++) {
823 int ch
= hunk_comment
[i
] & 0xff;
824 if (!ch
|| ch
== '\n')
830 printf("%s%s %s%s", c_reset
,
833 for (i
= 0; i
< comment_end
; i
++)
834 putchar(hunk_comment
[i
]);
837 printf("%s\n", c_reset
);
838 while (lno
< hunk_end
) {
841 unsigned long p_mask
;
842 struct sline
*sl
= &sline
[lno
++];
843 ll
= (sl
->flag
& no_pre_delete
) ? NULL
: sl
->lost
;
845 printf("%s%s", line_prefix
, c_old
);
846 for (j
= 0; j
< num_parent
; j
++) {
847 if (ll
->parent_map
& (1UL<<j
))
852 show_line_to_eol(ll
->line
, -1, c_reset
);
858 fputs(line_prefix
, stdout
);
859 if (!(sl
->flag
& (mark
-1))) {
861 * This sline was here to hang the
862 * lost lines in front of it.
866 fputs(c_context
, stdout
);
869 fputs(c_new
, stdout
);
870 for (j
= 0; j
< num_parent
; j
++) {
871 if (p_mask
& sl
->flag
)
877 show_line_to_eol(sl
->bol
, sl
->len
, c_reset
);
882 static void reuse_combine_diff(struct sline
*sline
, unsigned long cnt
,
885 /* We have already examined parent j and we know parent i
886 * and parent j are the same, so reuse the combined result
887 * of parent j for parent i.
889 unsigned long lno
, imask
, jmask
;
893 for (lno
= 0; lno
<= cnt
; lno
++) {
894 struct lline
*ll
= sline
->lost
;
895 sline
->p_lno
[i
] = sline
->p_lno
[j
];
897 if (ll
->parent_map
& jmask
)
898 ll
->parent_map
|= imask
;
901 if (sline
->flag
& jmask
)
902 sline
->flag
|= imask
;
905 /* the overall size of the file (sline[cnt]) */
906 sline
->p_lno
[i
] = sline
->p_lno
[j
];
909 static void dump_quoted_path(const char *head
,
912 const char *line_prefix
,
913 const char *c_meta
, const char *c_reset
)
915 static struct strbuf buf
= STRBUF_INIT
;
918 strbuf_addstr(&buf
, line_prefix
);
919 strbuf_addstr(&buf
, c_meta
);
920 strbuf_addstr(&buf
, head
);
921 quote_two_c_style(&buf
, prefix
, path
, 0);
922 strbuf_addstr(&buf
, c_reset
);
926 static void show_combined_header(struct combine_diff_path
*elem
,
928 struct rev_info
*rev
,
929 const char *line_prefix
,
931 int show_file_header
)
933 struct diff_options
*opt
= &rev
->diffopt
;
934 int abbrev
= opt
->flags
.full_index
? the_hash_algo
->hexsz
: DEFAULT_ABBREV
;
935 const char *a_prefix
= opt
->a_prefix
? opt
->a_prefix
: "a/";
936 const char *b_prefix
= opt
->b_prefix
? opt
->b_prefix
: "b/";
937 const char *c_meta
= diff_get_color_opt(opt
, DIFF_METAINFO
);
938 const char *c_reset
= diff_get_color_opt(opt
, DIFF_RESET
);
943 int dense
= rev
->dense_combined_merges
;
945 if (rev
->loginfo
&& !rev
->no_commit_id
)
948 dump_quoted_path(dense
? "diff --cc " : "diff --combined ",
949 "", elem
->path
, line_prefix
, c_meta
, c_reset
);
950 printf("%s%sindex ", line_prefix
, c_meta
);
951 for (i
= 0; i
< num_parent
; i
++) {
952 abb
= find_unique_abbrev(&elem
->parent
[i
].oid
,
954 printf("%s%s", i
? "," : "", abb
);
956 abb
= find_unique_abbrev(&elem
->oid
, abbrev
);
957 printf("..%s%s\n", abb
, c_reset
);
960 deleted
= !elem
->mode
;
962 /* We say it was added if nobody had it */
964 for (i
= 0; added
&& i
< num_parent
; i
++)
965 if (elem
->parent
[i
].status
!=
969 printf("%s%snew file mode %06o",
970 line_prefix
, c_meta
, elem
->mode
);
973 printf("%s%sdeleted file ",
974 line_prefix
, c_meta
);
976 for (i
= 0; i
< num_parent
; i
++) {
977 printf("%s%06o", i
? "," : "",
978 elem
->parent
[i
].mode
);
981 printf("..%06o", elem
->mode
);
983 printf("%s\n", c_reset
);
986 if (!show_file_header
)
989 if (rev
->combined_all_paths
) {
990 for (i
= 0; i
< num_parent
; i
++) {
991 char *path
= filename_changed(elem
->parent
[i
].status
)
992 ? elem
->parent
[i
].path
.buf
: elem
->path
;
993 if (elem
->parent
[i
].status
== DIFF_STATUS_ADDED
)
994 dump_quoted_path("--- ", "", "/dev/null",
995 line_prefix
, c_meta
, c_reset
);
997 dump_quoted_path("--- ", a_prefix
, path
,
998 line_prefix
, c_meta
, c_reset
);
1002 dump_quoted_path("--- ", "", "/dev/null",
1003 line_prefix
, c_meta
, c_reset
);
1005 dump_quoted_path("--- ", a_prefix
, elem
->path
,
1006 line_prefix
, c_meta
, c_reset
);
1009 dump_quoted_path("+++ ", "", "/dev/null",
1010 line_prefix
, c_meta
, c_reset
);
1012 dump_quoted_path("+++ ", b_prefix
, elem
->path
,
1013 line_prefix
, c_meta
, c_reset
);
1016 static void show_patch_diff(struct combine_diff_path
*elem
, int num_parent
,
1017 int working_tree_file
,
1018 struct rev_info
*rev
)
1020 struct diff_options
*opt
= &rev
->diffopt
;
1021 unsigned long result_size
, cnt
, lno
;
1022 int result_deleted
= 0;
1024 struct sline
*sline
; /* survived lines */
1025 int mode_differs
= 0;
1027 mmfile_t result_file
;
1028 struct userdiff_driver
*userdiff
;
1029 struct userdiff_driver
*textconv
= NULL
;
1031 const char *line_prefix
= diff_line_prefix(opt
);
1033 context
= opt
->context
;
1034 userdiff
= userdiff_find_by_path(opt
->repo
->index
, elem
->path
);
1036 userdiff
= userdiff_find_by_name("default");
1037 if (opt
->flags
.allow_textconv
)
1038 textconv
= userdiff_get_textconv(opt
->repo
, userdiff
);
1040 /* Read the result of merge first */
1041 if (!working_tree_file
)
1042 result
= grab_blob(opt
->repo
, &elem
->oid
, elem
->mode
, &result_size
,
1043 textconv
, elem
->path
);
1045 /* Used by diff-tree to read from the working tree */
1049 if (lstat(elem
->path
, &st
) < 0)
1052 if (S_ISLNK(st
.st_mode
)) {
1053 struct strbuf buf
= STRBUF_INIT
;
1055 if (strbuf_readlink(&buf
, elem
->path
, st
.st_size
) < 0) {
1056 error_errno("readlink(%s)", elem
->path
);
1059 result_size
= buf
.len
;
1060 result
= strbuf_detach(&buf
, NULL
);
1061 elem
->mode
= canon_mode(st
.st_mode
);
1062 } else if (S_ISDIR(st
.st_mode
)) {
1063 struct object_id oid
;
1064 if (resolve_gitlink_ref(elem
->path
, "HEAD", &oid
) < 0)
1065 result
= grab_blob(opt
->repo
, &elem
->oid
,
1066 elem
->mode
, &result_size
,
1069 result
= grab_blob(opt
->repo
, &oid
, elem
->mode
,
1070 &result_size
, NULL
, NULL
);
1071 } else if (textconv
) {
1072 struct diff_filespec
*df
= alloc_filespec(elem
->path
);
1073 fill_filespec(df
, null_oid(), 0, st
.st_mode
);
1074 result_size
= fill_textconv(opt
->repo
, textconv
, df
, &result
);
1076 } else if (0 <= (fd
= open(elem
->path
, O_RDONLY
))) {
1077 size_t len
= xsize_t(st
.st_size
);
1081 elem
->mode
= canon_mode(st
.st_mode
);
1082 /* if symlinks don't work, assume symlink if all parents
1085 is_file
= has_symlinks
;
1086 for (i
= 0; !is_file
&& i
< num_parent
; i
++)
1087 is_file
= !S_ISLNK(elem
->parent
[i
].mode
);
1089 elem
->mode
= canon_mode(S_IFLNK
);
1092 result
= xmallocz(len
);
1094 done
= read_in_full(fd
, result
, len
);
1096 die_errno("read error '%s'", elem
->path
);
1097 else if (done
< len
)
1098 die("early EOF '%s'", elem
->path
);
1100 /* If not a fake symlink, apply filters, e.g. autocrlf */
1102 struct strbuf buf
= STRBUF_INIT
;
1104 if (convert_to_git(rev
->diffopt
.repo
->index
,
1105 elem
->path
, result
, len
, &buf
, global_conv_flags_eol
)) {
1107 result
= strbuf_detach(&buf
, &len
);
1117 result
= xcalloc(1, 1);
1124 for (i
= 0; i
< num_parent
; i
++) {
1125 if (elem
->parent
[i
].mode
!= elem
->mode
) {
1133 else if (userdiff
->binary
!= -1)
1134 is_binary
= userdiff
->binary
;
1136 is_binary
= buffer_is_binary(result
, result_size
);
1137 for (i
= 0; !is_binary
&& i
< num_parent
; i
++) {
1140 buf
= grab_blob(opt
->repo
,
1141 &elem
->parent
[i
].oid
,
1142 elem
->parent
[i
].mode
,
1144 if (buffer_is_binary(buf
, size
))
1150 show_combined_header(elem
, num_parent
, rev
,
1151 line_prefix
, mode_differs
, 0);
1152 printf("Binary files differ\n");
1157 for (cnt
= 0, cp
= result
; cp
< result
+ result_size
; cp
++) {
1161 if (result_size
&& result
[result_size
-1] != '\n')
1162 cnt
++; /* incomplete line */
1164 CALLOC_ARRAY(sline
, st_add(cnt
, 2));
1165 sline
[0].bol
= result
;
1166 for (lno
= 0, cp
= result
; cp
< result
+ result_size
; cp
++) {
1168 sline
[lno
].len
= cp
- sline
[lno
].bol
;
1171 sline
[lno
].bol
= cp
+ 1;
1174 if (result_size
&& result
[result_size
-1] != '\n')
1175 sline
[cnt
-1].len
= result_size
- (sline
[cnt
-1].bol
- result
);
1177 result_file
.ptr
= result
;
1178 result_file
.size
= result_size
;
1180 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1181 * for deletion hunk at the end.
1183 CALLOC_ARRAY(sline
[0].p_lno
, st_mult(st_add(cnt
, 2), num_parent
));
1184 for (lno
= 0; lno
<= cnt
; lno
++)
1185 sline
[lno
+1].p_lno
= sline
[lno
].p_lno
+ num_parent
;
1187 for (i
= 0; i
< num_parent
; i
++) {
1189 for (j
= 0; j
< i
; j
++) {
1190 if (oideq(&elem
->parent
[i
].oid
,
1191 &elem
->parent
[j
].oid
)) {
1192 reuse_combine_diff(sline
, cnt
, i
, j
);
1197 combine_diff(opt
->repo
,
1198 &elem
->parent
[i
].oid
,
1199 elem
->parent
[i
].mode
,
1200 &result_file
, sline
,
1201 cnt
, i
, num_parent
, result_deleted
,
1202 textconv
, elem
->path
, opt
->xdl_opts
);
1205 show_hunks
= make_hunks(sline
, cnt
, num_parent
, rev
->dense_combined_merges
);
1207 if (show_hunks
|| mode_differs
|| working_tree_file
) {
1208 show_combined_header(elem
, num_parent
, rev
,
1209 line_prefix
, mode_differs
, 1);
1210 dump_sline(sline
, line_prefix
, cnt
, num_parent
,
1211 opt
->use_color
, result_deleted
);
1215 for (lno
= 0; lno
< cnt
; lno
++) {
1216 if (sline
[lno
].lost
) {
1217 struct lline
*ll
= sline
[lno
].lost
;
1219 struct lline
*tmp
= ll
;
1225 free(sline
[0].p_lno
);
1229 static void show_raw_diff(struct combine_diff_path
*p
, int num_parent
, struct rev_info
*rev
)
1231 struct diff_options
*opt
= &rev
->diffopt
;
1232 int line_termination
, inter_name_termination
, i
;
1233 const char *line_prefix
= diff_line_prefix(opt
);
1235 line_termination
= opt
->line_termination
;
1236 inter_name_termination
= '\t';
1237 if (!line_termination
)
1238 inter_name_termination
= 0;
1240 if (rev
->loginfo
&& !rev
->no_commit_id
)
1244 if (opt
->output_format
& DIFF_FORMAT_RAW
) {
1245 printf("%s", line_prefix
);
1247 /* As many colons as there are parents */
1248 for (i
= 0; i
< num_parent
; i
++)
1251 /* Show the modes */
1252 for (i
= 0; i
< num_parent
; i
++)
1253 printf("%06o ", p
->parent
[i
].mode
);
1254 printf("%06o", p
->mode
);
1257 for (i
= 0; i
< num_parent
; i
++)
1258 printf(" %s", diff_aligned_abbrev(&p
->parent
[i
].oid
,
1260 printf(" %s ", diff_aligned_abbrev(&p
->oid
, opt
->abbrev
));
1263 if (opt
->output_format
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
)) {
1264 for (i
= 0; i
< num_parent
; i
++)
1265 putchar(p
->parent
[i
].status
);
1266 putchar(inter_name_termination
);
1269 for (i
= 0; i
< num_parent
; i
++)
1270 if (rev
->combined_all_paths
) {
1271 if (filename_changed(p
->parent
[i
].status
))
1272 write_name_quoted(p
->parent
[i
].path
.buf
, stdout
,
1273 inter_name_termination
);
1275 write_name_quoted(p
->path
, stdout
,
1276 inter_name_termination
);
1278 write_name_quoted(p
->path
, stdout
, line_termination
);
1282 * The result (p->elem) is from the working tree and their
1283 * parents are typically from multiple stages during a merge
1284 * (i.e. diff-files) or the state in HEAD and in the index
1285 * (i.e. diff-index).
1287 void show_combined_diff(struct combine_diff_path
*p
,
1289 struct rev_info
*rev
)
1291 struct diff_options
*opt
= &rev
->diffopt
;
1293 if (opt
->output_format
& (DIFF_FORMAT_RAW
|
1295 DIFF_FORMAT_NAME_STATUS
))
1296 show_raw_diff(p
, num_parent
, rev
);
1297 else if (opt
->output_format
& DIFF_FORMAT_PATCH
)
1298 show_patch_diff(p
, num_parent
, 1, rev
);
1301 static void free_combined_pair(struct diff_filepair
*pair
)
1308 * A combine_diff_path expresses N parents on the LHS against 1 merge
1309 * result. Synthesize a diff_filepair that has N entries on the "one"
1310 * side and 1 entry on the "two" side.
1312 * In the future, we might want to add more data to combine_diff_path
1313 * so that we can fill fields we are ignoring (most notably, size) here,
1314 * but currently nobody uses it, so this should suffice for now.
1316 static struct diff_filepair
*combined_pair(struct combine_diff_path
*p
,
1320 struct diff_filepair
*pair
;
1321 struct diff_filespec
*pool
;
1323 pair
= xmalloc(sizeof(*pair
));
1324 CALLOC_ARRAY(pool
, st_add(num_parent
, 1));
1325 pair
->one
= pool
+ 1;
1328 for (i
= 0; i
< num_parent
; i
++) {
1329 pair
->one
[i
].path
= p
->path
;
1330 pair
->one
[i
].mode
= p
->parent
[i
].mode
;
1331 oidcpy(&pair
->one
[i
].oid
, &p
->parent
[i
].oid
);
1332 pair
->one
[i
].oid_valid
= !is_null_oid(&p
->parent
[i
].oid
);
1333 pair
->one
[i
].has_more_entries
= 1;
1335 pair
->one
[num_parent
- 1].has_more_entries
= 0;
1337 pair
->two
->path
= p
->path
;
1338 pair
->two
->mode
= p
->mode
;
1339 oidcpy(&pair
->two
->oid
, &p
->oid
);
1340 pair
->two
->oid_valid
= !is_null_oid(&p
->oid
);
1344 static void handle_combined_callback(struct diff_options
*opt
,
1345 struct combine_diff_path
*paths
,
1349 struct combine_diff_path
*p
;
1350 struct diff_queue_struct q
;
1353 CALLOC_ARRAY(q
.queue
, num_paths
);
1354 q
.alloc
= num_paths
;
1356 for (i
= 0, p
= paths
; p
; p
= p
->next
)
1357 q
.queue
[i
++] = combined_pair(p
, num_parent
);
1358 opt
->format_callback(&q
, opt
, opt
->format_callback_data
);
1359 for (i
= 0; i
< num_paths
; i
++)
1360 free_combined_pair(q
.queue
[i
]);
1364 static const char *path_path(void *obj
)
1366 struct combine_diff_path
*path
= (struct combine_diff_path
*)obj
;
1372 * Diff stat formats which we always compute solely against the first parent.
1374 #define STAT_FORMAT_MASK (DIFF_FORMAT_NUMSTAT \
1375 | DIFF_FORMAT_SHORTSTAT \
1376 | DIFF_FORMAT_SUMMARY \
1377 | DIFF_FORMAT_DIRSTAT \
1378 | DIFF_FORMAT_DIFFSTAT)
1380 /* find set of paths that every parent touches */
1381 static struct combine_diff_path
*find_paths_generic(const struct object_id
*oid
,
1382 const struct oid_array
*parents
,
1383 struct diff_options
*opt
,
1384 int combined_all_paths
)
1386 struct combine_diff_path
*paths
= NULL
;
1387 int i
, num_parent
= parents
->nr
;
1389 int output_format
= opt
->output_format
;
1390 const char *orderfile
= opt
->orderfile
;
1392 opt
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
1393 /* tell diff_tree to emit paths in sorted (=tree) order */
1394 opt
->orderfile
= NULL
;
1396 /* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn) (wrt paths) */
1397 for (i
= 0; i
< num_parent
; i
++) {
1399 * show stat against the first parent even when doing
1402 int stat_opt
= output_format
& STAT_FORMAT_MASK
;
1403 if (i
== 0 && stat_opt
)
1404 opt
->output_format
= stat_opt
;
1406 opt
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
1407 diff_tree_oid(&parents
->oid
[i
], oid
, "", opt
);
1409 paths
= intersect_paths(paths
, i
, num_parent
,
1410 combined_all_paths
);
1412 /* if showing diff, show it in requested order */
1413 if (opt
->output_format
!= DIFF_FORMAT_NO_OUTPUT
&&
1415 diffcore_order(orderfile
);
1421 opt
->output_format
= output_format
;
1422 opt
->orderfile
= orderfile
;
1428 * find set of paths that everybody touches, assuming diff is run without
1429 * rename/copy detection, etc, comparing all trees simultaneously (= faster).
1431 static struct combine_diff_path
*find_paths_multitree(
1432 const struct object_id
*oid
, const struct oid_array
*parents
,
1433 struct diff_options
*opt
)
1435 int i
, nparent
= parents
->nr
;
1436 const struct object_id
**parents_oid
;
1437 struct combine_diff_path paths_head
;
1440 ALLOC_ARRAY(parents_oid
, nparent
);
1441 for (i
= 0; i
< nparent
; i
++)
1442 parents_oid
[i
] = &parents
->oid
[i
];
1444 /* fake list head, so worker can assume it is non-NULL */
1445 paths_head
.next
= NULL
;
1447 strbuf_init(&base
, PATH_MAX
);
1448 diff_tree_paths(&paths_head
, oid
, parents_oid
, nparent
, &base
, opt
);
1450 strbuf_release(&base
);
1452 return paths_head
.next
;
1455 static int match_objfind(struct combine_diff_path
*path
,
1457 const struct oidset
*set
)
1460 if (oidset_contains(set
, &path
->oid
))
1462 for (i
= 0; i
< num_parent
; i
++) {
1463 if (oidset_contains(set
, &path
->parent
[i
].oid
))
1469 static struct combine_diff_path
*combined_objfind(struct diff_options
*opt
,
1470 struct combine_diff_path
*paths
,
1473 struct combine_diff_path
*ret
= NULL
, **tail
= &ret
;
1474 struct combine_diff_path
*p
= paths
;
1477 struct combine_diff_path
*next
= p
->next
;
1479 if (match_objfind(p
, num_parent
, opt
->objfind
)) {
1492 void diff_tree_combined(const struct object_id
*oid
,
1493 const struct oid_array
*parents
,
1494 struct rev_info
*rev
)
1496 struct diff_options
*opt
= &rev
->diffopt
;
1497 struct diff_options diffopts
;
1498 struct combine_diff_path
*p
, *paths
;
1499 int i
, num_paths
, needsep
, show_log_first
, num_parent
= parents
->nr
;
1500 int need_generic_pathscan
;
1502 if (opt
->ignore_regex_nr
)
1503 die("combined diff and '%s' cannot be used together",
1504 "--ignore-matching-lines");
1505 if (opt
->close_file
)
1506 die("combined diff and '%s' cannot be used together",
1509 /* nothing to do, if no parents */
1513 show_log_first
= !!rev
->loginfo
&& !rev
->no_commit_id
;
1515 if (show_log_first
) {
1518 if (rev
->verbose_header
&& opt
->output_format
&&
1519 opt
->output_format
!= DIFF_FORMAT_NO_OUTPUT
&&
1520 !commit_format_is_empty(rev
->commit_format
))
1521 printf("%s%c", diff_line_prefix(opt
),
1522 opt
->line_termination
);
1526 copy_pathspec(&diffopts
.pathspec
, &opt
->pathspec
);
1527 diffopts
.flags
.recursive
= 1;
1528 diffopts
.flags
.allow_external
= 0;
1530 /* find set of paths that everybody touches
1534 * Diffcore transformations are bound to diff_filespec and logic
1535 * comparing two entries - i.e. they do not apply directly to combine
1538 * If some of such transformations is requested - we launch generic
1539 * path scanning, which works significantly slower compared to
1540 * simultaneous all-trees-in-one-go scan in find_paths_multitree().
1542 * TODO some of the filters could be ported to work on
1543 * combine_diff_paths - i.e. all functionality that skips paths, so in
1544 * theory, we could end up having only multitree path scanning.
1546 * NOTE please keep this semantically in sync with diffcore_std()
1548 need_generic_pathscan
= opt
->skip_stat_unmatch
||
1549 opt
->flags
.follow_renames
||
1550 opt
->break_opt
!= -1 ||
1551 opt
->detect_rename
||
1552 (opt
->pickaxe_opts
&
1553 (DIFF_PICKAXE_KINDS_MASK
& ~DIFF_PICKAXE_KIND_OBJFIND
)) ||
1556 if (need_generic_pathscan
) {
1558 * NOTE generic case also handles --stat, as it computes
1559 * diff(sha1,parent_i) for all i to do the job, specifically
1562 paths
= find_paths_generic(oid
, parents
, &diffopts
,
1563 rev
->combined_all_paths
);
1567 paths
= find_paths_multitree(oid
, parents
, &diffopts
);
1569 if (opt
->pickaxe_opts
& DIFF_PICKAXE_KIND_OBJFIND
)
1570 paths
= combined_objfind(opt
, paths
, num_parent
);
1573 * show stat against the first parent even
1574 * when doing combined diff.
1576 stat_opt
= opt
->output_format
& STAT_FORMAT_MASK
;
1578 diffopts
.output_format
= stat_opt
;
1580 diff_tree_oid(&parents
->oid
[0], oid
, "", &diffopts
);
1581 diffcore_std(&diffopts
);
1583 diffcore_order(opt
->orderfile
);
1584 diff_flush(&diffopts
);
1588 /* find out number of surviving paths */
1589 for (num_paths
= 0, p
= paths
; p
; p
= p
->next
)
1592 /* order paths according to diffcore_order */
1593 if (opt
->orderfile
&& num_paths
) {
1594 struct obj_order
*o
;
1596 ALLOC_ARRAY(o
, num_paths
);
1597 for (i
= 0, p
= paths
; p
; p
= p
->next
, i
++)
1599 order_objects(opt
->orderfile
, path_path
, o
, num_paths
);
1600 for (i
= 0; i
< num_paths
- 1; i
++) {
1602 p
->next
= o
[i
+1].obj
;
1605 p
= o
[num_paths
-1].obj
;
1613 if (opt
->output_format
& (DIFF_FORMAT_RAW
|
1615 DIFF_FORMAT_NAME_STATUS
)) {
1616 for (p
= paths
; p
; p
= p
->next
)
1617 show_raw_diff(p
, num_parent
, rev
);
1620 else if (opt
->output_format
& STAT_FORMAT_MASK
)
1622 else if (opt
->output_format
& DIFF_FORMAT_CALLBACK
)
1623 handle_combined_callback(opt
, paths
, num_parent
, num_paths
);
1625 if (opt
->output_format
& DIFF_FORMAT_PATCH
) {
1627 printf("%s%c", diff_line_prefix(opt
),
1628 opt
->line_termination
);
1629 for (p
= paths
; p
; p
= p
->next
)
1630 show_patch_diff(p
, num_parent
, 0, rev
);
1634 /* Clean things up */
1636 struct combine_diff_path
*tmp
= paths
;
1637 paths
= paths
->next
;
1638 for (i
= 0; i
< num_parent
; i
++)
1639 if (rev
->combined_all_paths
&&
1640 filename_changed(tmp
->parent
[i
].status
))
1641 strbuf_release(&tmp
->parent
[i
].path
);
1645 clear_pathspec(&diffopts
.pathspec
);
1648 void diff_tree_combined_merge(const struct commit
*commit
,
1649 struct rev_info
*rev
)
1651 struct commit_list
*parent
= get_saved_parents(rev
, commit
);
1652 struct oid_array parents
= OID_ARRAY_INIT
;
1655 oid_array_append(&parents
, &parent
->item
->object
.oid
);
1656 parent
= parent
->next
;
1658 diff_tree_combined(&commit
->object
.oid
, &parents
, rev
);
1659 oid_array_clear(&parents
);