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 void append_lost(struct sline
*sline
, int n
, const char *line
, int len
)
117 unsigned long this_mask
= (1UL<<n
);
118 if (line
[len
-1] == '\n')
121 /* Check to see if we can squash things */
122 if (sline
->lost_head
) {
123 struct lline
*last_one
= NULL
;
124 /* We cannot squash it with earlier one */
125 for (lline
= sline
->lost_head
;
128 if (lline
->parent_map
& this_mask
)
130 lline
= last_one
? last_one
->next
: sline
->lost_head
;
132 if (lline
->len
== len
&&
133 !memcmp(lline
->line
, line
, len
)) {
134 lline
->parent_map
|= this_mask
;
141 lline
= xmalloc(sizeof(*lline
) + len
+ 1);
144 lline
->parent_map
= this_mask
;
145 memcpy(lline
->line
, line
, len
);
146 lline
->line
[len
] = 0;
147 *sline
->lost_tail
= lline
;
148 sline
->lost_tail
= &lline
->next
;
151 struct combine_diff_state
{
152 struct xdiff_emit_state xm
;
160 struct sline
*lost_bucket
;
163 static void consume_line(void *state_
, char *line
, unsigned long len
)
165 struct combine_diff_state
*state
= state_
;
166 if (5 < len
&& !memcmp("@@ -", line
, 4)) {
167 if (parse_hunk_header(line
, len
,
168 &state
->ob
, &state
->on
,
169 &state
->nb
, &state
->nn
))
171 state
->lno
= state
->nb
;
173 /* @@ -1,2 +0,0 @@ to remove the
178 /* @@ -X,Y +N,0 @@ removed Y lines
179 * that would have come *after* line N
180 * in the result. Our lost buckets hang
181 * to the line after the removed lines,
183 state
->lost_bucket
= &state
->sline
[state
->nb
];
185 state
->lost_bucket
= &state
->sline
[state
->nb
-1];
186 if (!state
->sline
[state
->nb
-1].p_lno
)
187 state
->sline
[state
->nb
-1].p_lno
=
188 xcalloc(state
->num_parent
,
189 sizeof(unsigned long));
190 state
->sline
[state
->nb
-1].p_lno
[state
->n
] = state
->ob
;
193 if (!state
->lost_bucket
)
194 return; /* not in any hunk yet */
197 append_lost(state
->lost_bucket
, state
->n
, line
+1, len
-1);
200 state
->sline
[state
->lno
-1].flag
|= state
->nmask
;
206 static void combine_diff(const unsigned char *parent
, mmfile_t
*result_file
,
207 struct sline
*sline
, int cnt
, int n
, int num_parent
)
209 unsigned int p_lno
, lno
;
210 unsigned long nmask
= (1UL << n
);
213 mmfile_t parent_file
;
215 struct combine_diff_state state
;
219 return; /* result deleted */
221 parent_file
.ptr
= grab_blob(parent
, &sz
);
222 parent_file
.size
= sz
;
223 xpp
.flags
= XDF_NEED_MINIMAL
;
226 ecb
.outf
= xdiff_outf
;
228 memset(&state
, 0, sizeof(state
));
229 state
.xm
.consume
= consume_line
;
233 state
.num_parent
= num_parent
;
236 xdl_diff(&parent_file
, result_file
, &xpp
, &xecfg
, &ecb
);
237 free(parent_file
.ptr
);
239 /* Assign line numbers for this parent.
241 * sline[lno].p_lno[n] records the first line number
242 * (counting from 1) for parent N if the final hunk display
243 * started by showing sline[lno] (possibly showing the lost
244 * lines attached to it first).
246 for (lno
= 0, p_lno
= 1; lno
<= cnt
; lno
++) {
248 sline
[lno
].p_lno
[n
] = p_lno
;
250 /* How many lines would this sline advance the p_lno? */
251 ll
= sline
[lno
].lost_head
;
253 if (ll
->parent_map
& nmask
)
254 p_lno
++; /* '-' means parent had it */
257 if (lno
< cnt
&& !(sline
[lno
].flag
& nmask
))
258 p_lno
++; /* no '+' means parent had it */
260 sline
[lno
].p_lno
[n
] = p_lno
; /* trailer */
263 static unsigned long context
= 3;
264 static char combine_marker
= '@';
266 static int interesting(struct sline
*sline
, unsigned long all_mask
)
268 /* If some parents lost lines here, or if we have added to
269 * some parent, it is interesting.
271 return ((sline
->flag
& all_mask
) || sline
->lost_head
);
274 static unsigned long adjust_hunk_tail(struct sline
*sline
,
275 unsigned long all_mask
,
276 unsigned long hunk_begin
,
279 /* i points at the first uninteresting line. If the last line
280 * of the hunk was interesting only because it has some
281 * deletion, then it is not all that interesting for the
282 * purpose of giving trailing context lines. This is because
283 * we output '-' line and then unmodified sline[i-1] itself in
284 * that case which gives us one extra context line.
286 if ((hunk_begin
+ 1 <= i
) && !(sline
[i
-1].flag
& all_mask
))
291 static unsigned long find_next(struct sline
*sline
,
297 /* We have examined up to i-1 and are about to look at i.
298 * Find next interesting or uninteresting line. Here,
299 * "interesting" does not mean interesting(), but marked by
300 * the give_context() function below (i.e. it includes context
301 * lines that are not interesting to interesting() function
302 * that are surrounded by interesting() ones.
306 ? !(sline
[i
].flag
& mark
)
307 : (sline
[i
].flag
& mark
))
314 static int give_context(struct sline
*sline
, unsigned long cnt
, int num_parent
)
316 unsigned long all_mask
= (1UL<<num_parent
) - 1;
317 unsigned long mark
= (1UL<<num_parent
);
320 /* Two groups of interesting lines may have a short gap of
321 * unintersting lines. Connect such groups to give them a
324 * We first start from what the interesting() function says,
325 * and mark them with "mark", and paint context lines with the
326 * mark. So interesting() would still say false for such context
327 * lines but they are treated as "interesting" in the end.
329 i
= find_next(sline
, mark
, 0, cnt
, 0);
334 unsigned long j
= (context
< i
) ? (i
- context
) : 0;
337 /* Paint a few lines before the first interesting line. */
339 sline
[j
++].flag
|= mark
;
342 /* we know up to i is to be included. where does the
343 * next uninteresting one start?
345 j
= find_next(sline
, mark
, i
, cnt
, 1);
347 break; /* the rest are all interesting */
349 /* lookahead context lines */
350 k
= find_next(sline
, mark
, j
, cnt
, 0);
351 j
= adjust_hunk_tail(sline
, all_mask
, i
, j
);
353 if (k
< j
+ context
) {
354 /* k is interesting and [j,k) are not, but
355 * paint them interesting because the gap is small.
358 sline
[j
++].flag
|= mark
;
363 /* j is the first uninteresting line and there is
364 * no overlap beyond it within context lines. Paint
365 * the trailing edge a bit.
368 k
= (j
+ context
< cnt
+1) ? j
+ context
: cnt
+1;
370 sline
[j
++].flag
|= mark
;
375 static int make_hunks(struct sline
*sline
, unsigned long cnt
,
376 int num_parent
, int dense
)
378 unsigned long all_mask
= (1UL<<num_parent
) - 1;
379 unsigned long mark
= (1UL<<num_parent
);
381 int has_interesting
= 0;
383 for (i
= 0; i
<= cnt
; i
++) {
384 if (interesting(&sline
[i
], all_mask
))
385 sline
[i
].flag
|= mark
;
387 sline
[i
].flag
&= ~mark
;
390 return give_context(sline
, cnt
, num_parent
);
392 /* Look at each hunk, and if we have changes from only one
393 * parent, or the changes are the same from all but one
394 * parent, mark that uninteresting.
398 unsigned long j
, hunk_begin
, hunk_end
;
399 unsigned long same_diff
;
400 while (i
<= cnt
&& !(sline
[i
].flag
& mark
))
403 break; /* No more interesting hunks */
405 for (j
= i
+ 1; j
<= cnt
; j
++) {
406 if (!(sline
[j
].flag
& mark
)) {
407 /* Look beyond the end to see if there
408 * is an interesting line after this
409 * hunk within context span.
411 unsigned long la
; /* lookahead */
413 la
= adjust_hunk_tail(sline
, all_mask
,
415 la
= (la
+ context
< cnt
+ 1) ?
416 (la
+ context
) : cnt
+ 1;
418 if (sline
[la
].flag
& mark
) {
430 /* [i..hunk_end) are interesting. Now is it really
431 * interesting? We check if there are only two versions
432 * and the result matches one of them. That is, we look
434 * (+) line, which records lines added to which parents;
435 * this line appears in the result.
436 * (-) line, which records from what parents the line
437 * was removed; this line does not appear in the result.
438 * then check the set of parents the result has difference
439 * from, from all lines. If there are lines that has
440 * different set of parents that the result has differences
441 * from, that means we have more than two versions.
443 * Even when we have only two versions, if the result does
444 * not match any of the parents, the it should be considered
445 * interesting. In such a case, we would have all '+' line.
446 * After passing the above "two versions" test, that would
447 * appear as "the same set of parents" to be "all parents".
451 for (j
= i
; j
< hunk_end
&& !has_interesting
; j
++) {
452 unsigned long this_diff
= sline
[j
].flag
& all_mask
;
453 struct lline
*ll
= sline
[j
].lost_head
;
455 /* This has some changes. Is it the
459 same_diff
= this_diff
;
460 else if (same_diff
!= this_diff
) {
465 while (ll
&& !has_interesting
) {
466 /* Lost this line from these parents;
467 * who are they? Are they the same?
469 this_diff
= ll
->parent_map
;
471 same_diff
= this_diff
;
472 else if (same_diff
!= this_diff
) {
479 if (!has_interesting
&& same_diff
!= all_mask
) {
480 /* This hunk is not that interesting after all */
481 for (j
= hunk_begin
; j
< hunk_end
; j
++)
482 sline
[j
].flag
&= ~mark
;
487 has_interesting
= give_context(sline
, cnt
, num_parent
);
488 return has_interesting
;
491 static void show_parent_lno(struct sline
*sline
, unsigned long l0
, unsigned long l1
, unsigned long cnt
, int n
)
493 l0
= sline
[l0
].p_lno
[n
];
494 l1
= sline
[l1
].p_lno
[n
];
495 printf(" -%lu,%lu", l0
, l1
-l0
);
498 static void dump_sline(struct sline
*sline
, unsigned long cnt
, int num_parent
)
500 unsigned long mark
= (1UL<<num_parent
);
502 unsigned long lno
= 0;
505 return; /* result deleted */
508 struct sline
*sl
= &sline
[lno
];
511 while (lno
<= cnt
&& !(sline
[lno
].flag
& mark
))
516 for (hunk_end
= lno
+ 1; hunk_end
<= cnt
; hunk_end
++)
517 if (!(sline
[hunk_end
].flag
& mark
))
520 rlines
= hunk_end
- lno
;
522 rlines
--; /* pointing at the last delete hunk */
523 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
524 for (i
= 0; i
< num_parent
; i
++)
525 show_parent_lno(sline
, lno
, hunk_end
, cnt
, i
);
526 printf(" +%lu,%lu ", lno
+1, rlines
);
527 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
529 while (lno
< hunk_end
) {
532 unsigned long p_mask
;
536 for (j
= 0; j
< num_parent
; j
++) {
537 if (ll
->parent_map
& (1UL<<j
))
548 for (j
= 0; j
< num_parent
; j
++) {
549 if (p_mask
& sl
->flag
)
555 printf("%.*s\n", sl
->len
, sl
->bol
);
560 static void reuse_combine_diff(struct sline
*sline
, unsigned long cnt
,
563 /* We have already examined parent j and we know parent i
564 * and parent j are the same, so reuse the combined result
565 * of parent j for parent i.
567 unsigned long lno
, imask
, jmask
;
571 for (lno
= 0; lno
<= cnt
; lno
++) {
572 struct lline
*ll
= sline
->lost_head
;
573 sline
->p_lno
[i
] = sline
->p_lno
[j
];
575 if (ll
->parent_map
& jmask
)
576 ll
->parent_map
|= imask
;
579 if (sline
->flag
& jmask
)
580 sline
->flag
|= imask
;
583 /* the overall size of the file (sline[cnt]) */
584 sline
->p_lno
[i
] = sline
->p_lno
[j
];
587 static int show_patch_diff(struct combine_diff_path
*elem
, int num_parent
,
588 int dense
, const char *header
,
589 struct diff_options
*opt
)
591 unsigned long result_size
, cnt
, lno
;
592 char *result
, *cp
, *ep
;
593 struct sline
*sline
; /* survived lines */
594 int mode_differs
= 0;
595 int i
, show_hunks
, shown_header
= 0;
596 int working_tree_file
= !memcmp(elem
->sha1
, null_sha1
, 20);
597 int abbrev
= opt
->full_index
? 40 : DEFAULT_ABBREV
;
598 mmfile_t result_file
;
600 /* Read the result of merge first */
601 if (!working_tree_file
)
602 result
= grab_blob(elem
->sha1
, &result_size
);
604 /* Used by diff-tree to read from the working tree */
607 if (0 <= (fd
= open(elem
->path
, O_RDONLY
)) &&
609 int len
= st
.st_size
;
612 elem
->mode
= canon_mode(st
.st_mode
);
614 result
= xmalloc(len
+ 1);
616 int done
= xread(fd
, result
+cnt
, len
-cnt
);
620 die("read error '%s'", elem
->path
);
636 for (cnt
= 0, cp
= result
; cp
- result
< result_size
; cp
++) {
640 if (result_size
&& result
[result_size
-1] != '\n')
641 cnt
++; /* incomplete line */
643 sline
= xcalloc(cnt
+2, sizeof(*sline
));
645 sline
[0].bol
= result
;
646 for (lno
= 0; lno
<= cnt
+ 1; lno
++) {
647 sline
[lno
].lost_tail
= &sline
[lno
].lost_head
;
650 for (lno
= 0, cp
= result
; cp
- result
< result_size
; cp
++) {
652 sline
[lno
].len
= cp
- sline
[lno
].bol
;
655 sline
[lno
].bol
= cp
+ 1;
658 if (result_size
&& result
[result_size
-1] != '\n')
659 sline
[cnt
-1].len
= result_size
- (sline
[cnt
-1].bol
- result
);
661 result_file
.ptr
= result
;
662 result_file
.size
= result_size
;
664 /* Even p_lno[cnt+1] is valid -- that is for the end line number
665 * for deletion hunk at the end.
667 sline
[0].p_lno
= xcalloc((cnt
+2) * num_parent
, sizeof(unsigned long));
668 for (lno
= 0; lno
<= cnt
; lno
++)
669 sline
[lno
+1].p_lno
= sline
[lno
].p_lno
+ num_parent
;
671 for (i
= 0; i
< num_parent
; i
++) {
673 for (j
= 0; j
< i
; j
++) {
674 if (!memcmp(elem
->parent
[i
].sha1
,
675 elem
->parent
[j
].sha1
, 20)) {
676 reuse_combine_diff(sline
, cnt
, i
, j
);
681 combine_diff(elem
->parent
[i
].sha1
, &result_file
, sline
,
683 if (elem
->parent
[i
].mode
!= elem
->mode
)
687 show_hunks
= make_hunks(sline
, cnt
, num_parent
, dense
);
689 if (show_hunks
|| mode_differs
|| working_tree_file
) {
694 printf("%s%c", header
, opt
->line_termination
);
696 printf("diff --%s ", dense
? "cc" : "combined");
697 if (quote_c_style(elem
->path
, NULL
, NULL
, 0))
698 quote_c_style(elem
->path
, NULL
, stdout
, 0);
700 printf("%s", elem
->path
);
703 for (i
= 0; i
< num_parent
; i
++) {
704 abb
= find_unique_abbrev(elem
->parent
[i
].sha1
,
706 printf("%s%s", i
? "," : "", abb
);
708 abb
= find_unique_abbrev(elem
->sha1
, abbrev
);
709 printf("..%s\n", abb
);
712 int added
= !!elem
->mode
;
713 for (i
= 0; added
&& i
< num_parent
; i
++)
714 if (elem
->parent
[i
].status
!=
718 printf("new file mode %06o", elem
->mode
);
721 printf("deleted file ");
723 for (i
= 0; i
< num_parent
; i
++) {
724 printf("%s%06o", i
? "," : "",
725 elem
->parent
[i
].mode
);
728 printf("..%06o", elem
->mode
);
732 dump_sline(sline
, cnt
, num_parent
);
736 for (i
= 0; i
< cnt
; i
++) {
737 if (sline
[i
].lost_head
) {
738 struct lline
*ll
= sline
[i
].lost_head
;
740 struct lline
*tmp
= ll
;
746 free(sline
[0].p_lno
);
751 #define COLONS "::::::::::::::::::::::::::::::::"
753 static void show_raw_diff(struct combine_diff_path
*p
, int num_parent
, const char *header
, struct diff_options
*opt
)
755 int i
, offset
, mod_type
= 'A';
757 int line_termination
, inter_name_termination
;
759 line_termination
= opt
->line_termination
;
760 inter_name_termination
= '\t';
761 if (!line_termination
)
762 inter_name_termination
= 0;
765 printf("%s%c", header
, line_termination
);
767 for (i
= 0; i
< num_parent
; i
++) {
768 if (p
->parent
[i
].mode
)
774 if (opt
->output_format
== DIFF_FORMAT_RAW
) {
775 offset
= strlen(COLONS
) - num_parent
;
778 prefix
= COLONS
+ offset
;
781 for (i
= 0; i
< num_parent
; i
++) {
782 printf("%s%06o", prefix
, p
->parent
[i
].mode
);
785 printf("%s%06o", prefix
, p
->mode
);
788 for (i
= 0; i
< num_parent
; i
++)
789 printf(" %s", diff_unique_abbrev(p
->parent
[i
].sha1
,
791 printf(" %s ", diff_unique_abbrev(p
->sha1
, opt
->abbrev
));
794 if (opt
->output_format
== DIFF_FORMAT_RAW
||
795 opt
->output_format
== DIFF_FORMAT_NAME_STATUS
) {
796 for (i
= 0; i
< num_parent
; i
++)
797 putchar(p
->parent
[i
].status
);
798 putchar(inter_name_termination
);
801 if (line_termination
) {
802 if (quote_c_style(p
->path
, NULL
, NULL
, 0))
803 quote_c_style(p
->path
, NULL
, stdout
, 0);
805 printf("%s", p
->path
);
806 putchar(line_termination
);
809 printf("%s%c", p
->path
, line_termination
);
813 int show_combined_diff(struct combine_diff_path
*p
,
817 struct diff_options
*opt
)
821 switch (opt
->output_format
) {
822 case DIFF_FORMAT_RAW
:
823 case DIFF_FORMAT_NAME_STATUS
:
824 case DIFF_FORMAT_NAME
:
825 show_raw_diff(p
, num_parent
, header
, opt
);
829 case DIFF_FORMAT_PATCH
:
830 return show_patch_diff(p
, num_parent
, dense
, header
, opt
);
834 const char *diff_tree_combined_merge(const unsigned char *sha1
,
835 const char *header
, int dense
,
836 struct diff_options
*opt
)
838 struct commit
*commit
= lookup_commit(sha1
);
839 struct diff_options diffopts
;
840 struct commit_list
*parents
;
841 struct combine_diff_path
*p
, *paths
= NULL
;
842 int num_parent
, i
, num_paths
;
845 diffopts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
846 diffopts
.with_raw
= 0;
847 diffopts
.recursive
= 1;
850 for (parents
= commit
->parents
, num_parent
= 0;
852 parents
= parents
->next
, num_parent
++)
855 /* find set of paths that everybody touches */
856 for (parents
= commit
->parents
, i
= 0;
858 parents
= parents
->next
, i
++) {
859 struct commit
*parent
= parents
->item
;
860 diff_tree_sha1(parent
->object
.sha1
, commit
->object
.sha1
, "",
862 diffcore_std(&diffopts
);
863 paths
= intersect_paths(paths
, i
, num_parent
);
864 diff_flush(&diffopts
);
867 /* find out surviving paths */
868 for (num_paths
= 0, p
= paths
; p
; p
= p
->next
) {
874 int saved_format
= opt
->output_format
;
875 opt
->output_format
= DIFF_FORMAT_RAW
;
876 for (p
= paths
; p
; p
= p
->next
) {
877 if (show_combined_diff(p
, num_parent
, dense
,
881 opt
->output_format
= saved_format
;
882 putchar(opt
->line_termination
);
884 for (p
= paths
; p
; p
= p
->next
) {
885 if (show_combined_diff(p
, num_parent
, dense
,
891 /* Clean things up */
893 struct combine_diff_path
*tmp
= paths
;