7 #include "xdiff-interface.h"
9 static int uninteresting(struct diff_filepair
*p
)
11 if (diff_unmodified_pair(p
))
16 static struct combine_diff_path
*intersect_paths(struct combine_diff_path
*curr
, int n
, int num_parent
)
18 struct diff_queue_struct
*q
= &diff_queued_diff
;
19 struct combine_diff_path
*p
;
23 struct combine_diff_path
*list
= NULL
, **tail
= &list
;
24 for (i
= 0; i
< q
->nr
; i
++) {
27 if (uninteresting(q
->queue
[i
]))
29 path
= q
->queue
[i
]->two
->path
;
31 p
= xmalloc(combine_diff_path_size(num_parent
, len
));
32 p
->path
= (char*) &(p
->parent
[num_parent
]);
33 memcpy(p
->path
, path
, len
);
38 sizeof(p
->parent
[0]) * num_parent
);
40 memcpy(p
->sha1
, q
->queue
[i
]->two
->sha1
, 20);
41 p
->mode
= q
->queue
[i
]->two
->mode
;
42 memcpy(p
->parent
[n
].sha1
, q
->queue
[i
]->one
->sha1
, 20);
43 p
->parent
[n
].mode
= q
->queue
[i
]->one
->mode
;
44 p
->parent
[n
].status
= q
->queue
[i
]->status
;
51 for (p
= curr
; p
; p
= p
->next
) {
55 for (i
= 0; i
< q
->nr
; i
++) {
59 if (uninteresting(q
->queue
[i
]))
61 path
= q
->queue
[i
]->two
->path
;
63 if (len
== p
->len
&& !memcmp(path
, p
->path
, len
)) {
65 memcpy(p
->parent
[n
].sha1
,
66 q
->queue
[i
]->one
->sha1
, 20);
67 p
->parent
[n
].mode
= q
->queue
[i
]->one
->mode
;
68 p
->parent
[n
].status
= q
->queue
[i
]->status
;
78 /* Lines lost from parent */
82 unsigned long parent_map
;
83 char line
[FLEX_ARRAY
];
86 /* Lines surviving in the merge result */
88 struct lline
*lost_head
, **lost_tail
;
91 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
92 * we did not change it).
93 * bit N is used for "interesting" lines, including context.
99 static char *grab_blob(const unsigned char *sha1
, unsigned long *size
)
103 if (!memcmp(sha1
, null_sha1
, 20)) {
106 return xcalloc(1, 1);
108 blob
= read_sha1_file(sha1
, type
, size
);
109 if (strcmp(type
, blob_type
))
110 die("object '%s' is not a blob!", sha1_to_hex(sha1
));
114 static int parse_num(char **cp_p
, unsigned int *num_p
)
117 unsigned int num
= 0;
120 while ('0' <= *cp
&& *cp
<= '9')
121 num
= num
* 10 + *cp
++ - '0';
122 if (!(read_some
= cp
- *cp_p
))
129 static int parse_hunk_header(char *line
, int len
,
130 unsigned int *ob
, unsigned int *on
,
131 unsigned int *nb
, unsigned int *nn
)
135 if (parse_num(&cp
, ob
)) {
137 return error("malformed diff output: %s", line
);
141 if (parse_num(&cp
, on
))
146 if (*cp
++ != ' ' || *cp
++ != '+')
148 if (parse_num(&cp
, nb
))
152 if (parse_num(&cp
, nn
))
157 return -!!memcmp(cp
, " @@", 3);
160 static void append_lost(struct sline
*sline
, int n
, const char *line
, int len
)
163 unsigned long this_mask
= (1UL<<n
);
164 if (line
[len
-1] == '\n')
167 /* Check to see if we can squash things */
168 if (sline
->lost_head
) {
169 struct lline
*last_one
= NULL
;
170 /* We cannot squash it with earlier one */
171 for (lline
= sline
->lost_head
;
174 if (lline
->parent_map
& this_mask
)
176 lline
= last_one
? last_one
->next
: sline
->lost_head
;
178 if (lline
->len
== len
&&
179 !memcmp(lline
->line
, line
, len
)) {
180 lline
->parent_map
|= this_mask
;
187 lline
= xmalloc(sizeof(*lline
) + len
+ 1);
190 lline
->parent_map
= this_mask
;
191 memcpy(lline
->line
, line
, len
);
192 lline
->line
[len
] = 0;
193 *sline
->lost_tail
= lline
;
194 sline
->lost_tail
= &lline
->next
;
197 struct combine_diff_state
{
198 struct xdiff_emit_state xm
;
200 unsigned int lno
, ob
, on
, nb
, nn
;
205 struct sline
*lost_bucket
;
208 static void consume_line(void *state_
, char *line
, unsigned long len
)
210 struct combine_diff_state
*state
= state_
;
211 if (5 < len
&& !memcmp("@@ -", line
, 4)) {
212 if (parse_hunk_header(line
, len
,
213 &state
->ob
, &state
->on
,
214 &state
->nb
, &state
->nn
))
216 state
->lno
= state
->nb
;
218 /* @@ -1,2 +0,0 @@ to remove the
223 /* @@ -X,Y +N,0 @@ removed Y lines
224 * that would have come *after* line N
225 * in the result. Our lost buckets hang
226 * to the line after the removed lines,
228 state
->lost_bucket
= &state
->sline
[state
->nb
];
230 state
->lost_bucket
= &state
->sline
[state
->nb
-1];
231 if (!state
->sline
[state
->nb
-1].p_lno
)
232 state
->sline
[state
->nb
-1].p_lno
=
233 xcalloc(state
->num_parent
,
234 sizeof(unsigned long));
235 state
->sline
[state
->nb
-1].p_lno
[state
->n
] = state
->ob
;
238 if (!state
->lost_bucket
)
239 return; /* not in any hunk yet */
242 append_lost(state
->lost_bucket
, state
->n
, line
+1, len
-1);
245 state
->sline
[state
->lno
-1].flag
|= state
->nmask
;
251 static void combine_diff(const unsigned char *parent
, mmfile_t
*result_file
,
252 struct sline
*sline
, int cnt
, int n
, int num_parent
)
254 unsigned int p_lno
, lno
;
255 unsigned long nmask
= (1UL << n
);
258 mmfile_t parent_file
;
260 struct combine_diff_state state
;
264 return; /* result deleted */
266 parent_file
.ptr
= grab_blob(parent
, &sz
);
267 parent_file
.size
= sz
;
268 xpp
.flags
= XDF_NEED_MINIMAL
;
271 ecb
.outf
= xdiff_outf
;
273 memset(&state
, 0, sizeof(state
));
274 state
.xm
.consume
= consume_line
;
278 state
.num_parent
= num_parent
;
281 xdl_diff(&parent_file
, result_file
, &xpp
, &xecfg
, &ecb
);
282 free(parent_file
.ptr
);
284 /* Assign line numbers for this parent.
286 * sline[lno].p_lno[n] records the first line number
287 * (counting from 1) for parent N if the final hunk display
288 * started by showing sline[lno] (possibly showing the lost
289 * lines attached to it first).
291 for (lno
= 0, p_lno
= 1; lno
< cnt
; lno
++) {
293 sline
[lno
].p_lno
[n
] = p_lno
;
295 /* How many lines would this sline advance the p_lno? */
296 ll
= sline
[lno
].lost_head
;
298 if (ll
->parent_map
& nmask
)
299 p_lno
++; /* '-' means parent had it */
302 if (!(sline
[lno
].flag
& nmask
))
303 p_lno
++; /* no '+' means parent had it */
305 sline
[lno
].p_lno
[n
] = p_lno
; /* trailer */
308 static unsigned long context
= 3;
309 static char combine_marker
= '@';
311 static int interesting(struct sline
*sline
, unsigned long all_mask
)
313 /* If some parents lost lines here, or if we have added to
314 * some parent, it is interesting.
316 return ((sline
->flag
& all_mask
) || sline
->lost_head
);
319 static unsigned long adjust_hunk_tail(struct sline
*sline
,
320 unsigned long all_mask
,
321 unsigned long hunk_begin
,
324 /* i points at the first uninteresting line. If the last line
325 * of the hunk was interesting only because it has some
326 * deletion, then it is not all that interesting for the
327 * purpose of giving trailing context lines. This is because
328 * we output '-' line and then unmodified sline[i-1] itself in
329 * that case which gives us one extra context line.
331 if ((hunk_begin
+ 1 <= i
) && !(sline
[i
-1].flag
& all_mask
))
336 static unsigned long find_next(struct sline
*sline
,
342 /* We have examined up to i-1 and are about to look at i.
343 * Find next interesting or uninteresting line. Here,
344 * "interesting" does not mean interesting(), but marked by
345 * the give_context() function below (i.e. it includes context
346 * lines that are not interesting to interesting() function
347 * that are surrounded by interesting() ones.
351 ? !(sline
[i
].flag
& mark
)
352 : (sline
[i
].flag
& mark
))
359 static int give_context(struct sline
*sline
, unsigned long cnt
, int num_parent
)
361 unsigned long all_mask
= (1UL<<num_parent
) - 1;
362 unsigned long mark
= (1UL<<num_parent
);
365 /* Two groups of interesting lines may have a short gap of
366 * unintersting lines. Connect such groups to give them a
369 * We first start from what the interesting() function says,
370 * and mark them with "mark", and paint context lines with the
371 * mark. So interesting() would still say false for such context
372 * lines but they are treated as "interesting" in the end.
374 i
= find_next(sline
, mark
, 0, cnt
, 0);
379 unsigned long j
= (context
< i
) ? (i
- context
) : 0;
382 /* Paint a few lines before the first interesting line. */
384 sline
[j
++].flag
|= mark
;
387 /* we know up to i is to be included. where does the
388 * next uninteresting one start?
390 j
= find_next(sline
, mark
, i
, cnt
, 1);
392 break; /* the rest are all interesting */
394 /* lookahead context lines */
395 k
= find_next(sline
, mark
, j
, cnt
, 0);
396 j
= adjust_hunk_tail(sline
, all_mask
, i
, j
);
398 if (k
< j
+ context
) {
399 /* k is interesting and [j,k) are not, but
400 * paint them interesting because the gap is small.
403 sline
[j
++].flag
|= mark
;
408 /* j is the first uninteresting line and there is
409 * no overlap beyond it within context lines. Paint
410 * the trailing edge a bit.
413 k
= (j
+ context
< cnt
) ? j
+ context
: cnt
;
415 sline
[j
++].flag
|= mark
;
420 static int make_hunks(struct sline
*sline
, unsigned long cnt
,
421 int num_parent
, int dense
)
423 unsigned long all_mask
= (1UL<<num_parent
) - 1;
424 unsigned long mark
= (1UL<<num_parent
);
426 int has_interesting
= 0;
428 for (i
= 0; i
< cnt
; i
++) {
429 if (interesting(&sline
[i
], all_mask
))
430 sline
[i
].flag
|= mark
;
432 sline
[i
].flag
&= ~mark
;
435 return give_context(sline
, cnt
, num_parent
);
437 /* Look at each hunk, and if we have changes from only one
438 * parent, or the changes are the same from all but one
439 * parent, mark that uninteresting.
443 unsigned long j
, hunk_begin
, hunk_end
;
444 unsigned long same_diff
;
445 while (i
< cnt
&& !(sline
[i
].flag
& mark
))
448 break; /* No more interesting hunks */
450 for (j
= i
+ 1; j
< cnt
; j
++) {
451 if (!(sline
[j
].flag
& mark
)) {
452 /* Look beyond the end to see if there
453 * is an interesting line after this
454 * hunk within context span.
456 unsigned long la
; /* lookahead */
458 la
= adjust_hunk_tail(sline
, all_mask
,
460 la
= (la
+ context
< cnt
) ?
461 (la
+ context
) : cnt
;
463 if (sline
[la
].flag
& mark
) {
475 /* [i..hunk_end) are interesting. Now is it really
476 * interesting? We check if there are only two versions
477 * and the result matches one of them. That is, we look
479 * (+) line, which records lines added to which parents;
480 * this line appears in the result.
481 * (-) line, which records from what parents the line
482 * was removed; this line does not appear in the result.
483 * then check the set of parents the result has difference
484 * from, from all lines. If there are lines that has
485 * different set of parents that the result has differences
486 * from, that means we have more than two versions.
488 * Even when we have only two versions, if the result does
489 * not match any of the parents, the it should be considered
490 * interesting. In such a case, we would have all '+' line.
491 * After passing the above "two versions" test, that would
492 * appear as "the same set of parents" to be "all parents".
496 for (j
= i
; j
< hunk_end
&& !has_interesting
; j
++) {
497 unsigned long this_diff
= sline
[j
].flag
& all_mask
;
498 struct lline
*ll
= sline
[j
].lost_head
;
500 /* This has some changes. Is it the
504 same_diff
= this_diff
;
505 else if (same_diff
!= this_diff
) {
510 while (ll
&& !has_interesting
) {
511 /* Lost this line from these parents;
512 * who are they? Are they the same?
514 this_diff
= ll
->parent_map
;
516 same_diff
= this_diff
;
517 else if (same_diff
!= this_diff
) {
524 if (!has_interesting
&& same_diff
!= all_mask
) {
525 /* This hunk is not that interesting after all */
526 for (j
= hunk_begin
; j
< hunk_end
; j
++)
527 sline
[j
].flag
&= ~mark
;
532 has_interesting
= give_context(sline
, cnt
, num_parent
);
533 return has_interesting
;
536 static void show_parent_lno(struct sline
*sline
, unsigned long l0
, unsigned long l1
, unsigned long cnt
, int n
)
538 l0
= sline
[l0
].p_lno
[n
];
539 l1
= sline
[l1
].p_lno
[n
];
540 printf(" -%lu,%lu", l0
, l1
-l0
);
543 static void dump_sline(struct sline
*sline
, unsigned long cnt
, int num_parent
)
545 unsigned long mark
= (1UL<<num_parent
);
547 unsigned long lno
= 0;
550 return; /* result deleted */
553 struct sline
*sl
= &sline
[lno
];
555 while (lno
< cnt
&& !(sline
[lno
].flag
& mark
))
559 for (hunk_end
= lno
+ 1; hunk_end
< cnt
; hunk_end
++)
560 if (!(sline
[hunk_end
].flag
& mark
))
562 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
563 for (i
= 0; i
< num_parent
; i
++)
564 show_parent_lno(sline
, lno
, hunk_end
, cnt
, i
);
565 printf(" +%lu,%lu ", lno
+1, hunk_end
-lno
);
566 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
568 while (lno
< hunk_end
) {
571 unsigned long p_mask
;
575 for (j
= 0; j
< num_parent
; j
++) {
576 if (ll
->parent_map
& (1UL<<j
))
585 for (j
= 0; j
< num_parent
; j
++) {
586 if (p_mask
& sl
->flag
)
592 printf("%.*s\n", sl
->len
, sl
->bol
);
597 static void reuse_combine_diff(struct sline
*sline
, unsigned long cnt
,
600 /* We have already examined parent j and we know parent i
601 * and parent j are the same, so reuse the combined result
602 * of parent j for parent i.
604 unsigned long lno
, imask
, jmask
;
608 for (lno
= 0; lno
< cnt
; lno
++) {
609 struct lline
*ll
= sline
->lost_head
;
610 sline
->p_lno
[i
] = sline
->p_lno
[j
];
612 if (ll
->parent_map
& jmask
)
613 ll
->parent_map
|= imask
;
616 if (sline
->flag
& jmask
)
617 sline
->flag
|= imask
;
620 /* the overall size of the file (sline[cnt]) */
621 sline
->p_lno
[i
] = sline
->p_lno
[j
];
624 static int show_patch_diff(struct combine_diff_path
*elem
, int num_parent
,
625 int dense
, const char *header
,
626 struct diff_options
*opt
)
628 unsigned long result_size
, cnt
, lno
;
629 char *result
, *cp
, *ep
;
630 struct sline
*sline
; /* survived lines */
631 int mode_differs
= 0;
632 int i
, show_hunks
, shown_header
= 0;
633 int working_tree_file
= !memcmp(elem
->sha1
, null_sha1
, 20);
634 int abbrev
= opt
->full_index
? 40 : DEFAULT_ABBREV
;
635 mmfile_t result_file
;
637 /* Read the result of merge first */
638 if (!working_tree_file
)
639 result
= grab_blob(elem
->sha1
, &result_size
);
641 /* Used by diff-tree to read from the working tree */
644 if (0 <= (fd
= open(elem
->path
, O_RDONLY
)) &&
646 int len
= st
.st_size
;
649 elem
->mode
= canon_mode(st
.st_mode
);
651 result
= xmalloc(len
+ 1);
653 int done
= xread(fd
, result
+cnt
, len
-cnt
);
657 die("read error '%s'", elem
->path
);
673 for (cnt
= 0, cp
= result
; cp
- result
< result_size
; cp
++) {
677 if (result_size
&& result
[result_size
-1] != '\n')
678 cnt
++; /* incomplete line */
680 sline
= xcalloc(cnt
+1, sizeof(*sline
));
682 sline
[0].bol
= result
;
683 for (lno
= 0; lno
<= cnt
; lno
++) {
684 sline
[lno
].lost_tail
= &sline
[lno
].lost_head
;
687 for (lno
= 0, cp
= result
; cp
- result
< result_size
; cp
++) {
689 sline
[lno
].len
= cp
- sline
[lno
].bol
;
692 sline
[lno
].bol
= cp
+ 1;
695 if (result_size
&& result
[result_size
-1] != '\n')
696 sline
[cnt
-1].len
= result_size
- (sline
[cnt
-1].bol
- result
);
698 result_file
.ptr
= result
;
699 result_file
.size
= result_size
;
701 sline
[0].p_lno
= xcalloc((cnt
+1) * num_parent
, sizeof(unsigned long));
702 for (lno
= 0; lno
< cnt
; lno
++)
703 sline
[lno
+1].p_lno
= sline
[lno
].p_lno
+ num_parent
;
705 for (i
= 0; i
< num_parent
; i
++) {
707 for (j
= 0; j
< i
; j
++) {
708 if (!memcmp(elem
->parent
[i
].sha1
,
709 elem
->parent
[j
].sha1
, 20)) {
710 reuse_combine_diff(sline
, cnt
, i
, j
);
715 combine_diff(elem
->parent
[i
].sha1
, &result_file
, sline
,
717 if (elem
->parent
[i
].mode
!= elem
->mode
)
721 show_hunks
= make_hunks(sline
, cnt
, num_parent
, dense
);
723 if (show_hunks
|| mode_differs
|| working_tree_file
) {
728 printf("%s%c", header
, opt
->line_termination
);
730 printf("diff --%s ", dense
? "cc" : "combined");
731 if (quote_c_style(elem
->path
, NULL
, NULL
, 0))
732 quote_c_style(elem
->path
, NULL
, stdout
, 0);
734 printf("%s", elem
->path
);
737 for (i
= 0; i
< num_parent
; i
++) {
738 abb
= find_unique_abbrev(elem
->parent
[i
].sha1
,
740 printf("%s%s", i
? "," : "", abb
);
742 abb
= find_unique_abbrev(elem
->sha1
, abbrev
);
743 printf("..%s\n", abb
);
746 int added
= !!elem
->mode
;
747 for (i
= 0; added
&& i
< num_parent
; i
++)
748 if (elem
->parent
[i
].status
!=
752 printf("new file mode %06o", elem
->mode
);
755 printf("deleted file ");
757 for (i
= 0; i
< num_parent
; i
++) {
758 printf("%s%06o", i
? "," : "",
759 elem
->parent
[i
].mode
);
762 printf("..%06o", elem
->mode
);
766 dump_sline(sline
, cnt
, num_parent
);
770 for (i
= 0; i
< cnt
; i
++) {
771 if (sline
[i
].lost_head
) {
772 struct lline
*ll
= sline
[i
].lost_head
;
774 struct lline
*tmp
= ll
;
780 free(sline
[0].p_lno
);
785 #define COLONS "::::::::::::::::::::::::::::::::"
787 static void show_raw_diff(struct combine_diff_path
*p
, int num_parent
, const char *header
, struct diff_options
*opt
)
789 int i
, offset
, mod_type
= 'A';
791 int line_termination
, inter_name_termination
;
793 line_termination
= opt
->line_termination
;
794 inter_name_termination
= '\t';
795 if (!line_termination
)
796 inter_name_termination
= 0;
799 printf("%s%c", header
, line_termination
);
801 for (i
= 0; i
< num_parent
; i
++) {
802 if (p
->parent
[i
].mode
)
808 if (opt
->output_format
== DIFF_FORMAT_RAW
) {
809 offset
= strlen(COLONS
) - num_parent
;
812 prefix
= COLONS
+ offset
;
815 for (i
= 0; i
< num_parent
; i
++) {
816 printf("%s%06o", prefix
, p
->parent
[i
].mode
);
819 printf("%s%06o", prefix
, p
->mode
);
822 for (i
= 0; i
< num_parent
; i
++)
823 printf(" %s", diff_unique_abbrev(p
->parent
[i
].sha1
,
825 printf(" %s ", diff_unique_abbrev(p
->sha1
, opt
->abbrev
));
828 if (opt
->output_format
== DIFF_FORMAT_RAW
||
829 opt
->output_format
== DIFF_FORMAT_NAME_STATUS
) {
830 for (i
= 0; i
< num_parent
; i
++)
831 putchar(p
->parent
[i
].status
);
832 putchar(inter_name_termination
);
835 if (line_termination
) {
836 if (quote_c_style(p
->path
, NULL
, NULL
, 0))
837 quote_c_style(p
->path
, NULL
, stdout
, 0);
839 printf("%s", p
->path
);
840 putchar(line_termination
);
843 printf("%s%c", p
->path
, line_termination
);
847 int show_combined_diff(struct combine_diff_path
*p
,
851 struct diff_options
*opt
)
855 switch (opt
->output_format
) {
856 case DIFF_FORMAT_RAW
:
857 case DIFF_FORMAT_NAME_STATUS
:
858 case DIFF_FORMAT_NAME
:
859 show_raw_diff(p
, num_parent
, header
, opt
);
863 case DIFF_FORMAT_PATCH
:
864 return show_patch_diff(p
, num_parent
, dense
, header
, opt
);
868 const char *diff_tree_combined_merge(const unsigned char *sha1
,
869 const char *header
, int dense
,
870 struct diff_options
*opt
)
872 struct commit
*commit
= lookup_commit(sha1
);
873 struct diff_options diffopts
;
874 struct commit_list
*parents
;
875 struct combine_diff_path
*p
, *paths
= NULL
;
876 int num_parent
, i
, num_paths
;
879 diffopts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
880 diffopts
.recursive
= 1;
883 for (parents
= commit
->parents
, num_parent
= 0;
885 parents
= parents
->next
, num_parent
++)
888 /* find set of paths that everybody touches */
889 for (parents
= commit
->parents
, i
= 0;
891 parents
= parents
->next
, i
++) {
892 struct commit
*parent
= parents
->item
;
893 diff_tree_sha1(parent
->object
.sha1
, commit
->object
.sha1
, "",
895 diffcore_std(&diffopts
);
896 paths
= intersect_paths(paths
, i
, num_parent
);
897 diff_flush(&diffopts
);
900 /* find out surviving paths */
901 for (num_paths
= 0, p
= paths
; p
; p
= p
->next
) {
906 for (p
= paths
; p
; p
= p
->next
) {
907 if (show_combined_diff(p
, num_parent
, dense
,
913 /* Clean things up */
915 struct combine_diff_path
*tmp
= paths
;