7 static int uninteresting(struct diff_filepair
*p
)
9 if (diff_unmodified_pair(p
))
14 static struct combine_diff_path
*intersect_paths(struct combine_diff_path
*curr
, int n
, int num_parent
)
16 struct diff_queue_struct
*q
= &diff_queued_diff
;
17 struct combine_diff_path
*p
;
21 struct combine_diff_path
*list
= NULL
, **tail
= &list
;
22 for (i
= 0; i
< q
->nr
; i
++) {
25 if (uninteresting(q
->queue
[i
]))
27 path
= q
->queue
[i
]->two
->path
;
29 p
= xmalloc(combine_diff_path_size(num_parent
, len
));
30 p
->path
= (char*) &(p
->parent
[num_parent
]);
31 memcpy(p
->path
, path
, len
);
36 sizeof(p
->parent
[0]) * num_parent
);
38 memcpy(p
->sha1
, q
->queue
[i
]->two
->sha1
, 20);
39 p
->mode
= q
->queue
[i
]->two
->mode
;
40 memcpy(p
->parent
[n
].sha1
, q
->queue
[i
]->one
->sha1
, 20);
41 p
->parent
[n
].mode
= q
->queue
[i
]->one
->mode
;
48 for (p
= curr
; p
; p
= p
->next
) {
52 for (i
= 0; i
< q
->nr
; i
++) {
56 if (uninteresting(q
->queue
[i
]))
58 path
= q
->queue
[i
]->two
->path
;
60 if (len
== p
->len
&& !memcmp(path
, p
->path
, len
)) {
62 memcpy(p
->parent
[n
].sha1
,
63 q
->queue
[i
]->one
->sha1
, 20);
64 p
->parent
[n
].mode
= q
->queue
[i
]->one
->mode
;
74 /* Lines lost from parent */
78 unsigned long parent_map
;
79 char line
[FLEX_ARRAY
];
82 /* Lines surviving in the merge result */
84 struct lline
*lost_head
, **lost_tail
;
87 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
88 * we did not change it).
89 * bit N is used for "interesting" lines, including context.
95 static char *grab_blob(const unsigned char *sha1
, unsigned long *size
)
99 if (!memcmp(sha1
, null_sha1
, 20)) {
102 return xcalloc(1, 1);
104 blob
= read_sha1_file(sha1
, type
, size
);
105 if (strcmp(type
, "blob"))
106 die("object '%s' is not a blob!", sha1_to_hex(sha1
));
110 #define TMPPATHLEN 50
111 #define MAXLINELEN 10240
113 static void write_to_temp_file(char *tmpfile
, void *blob
, unsigned long size
)
115 int fd
= git_mkstemp(tmpfile
, TMPPATHLEN
, ".diff_XXXXXX");
117 die("unable to create temp-file");
118 if (write(fd
, blob
, size
) != size
)
119 die("unable to write temp-file");
123 static void write_temp_blob(char *tmpfile
, const unsigned char *sha1
)
127 blob
= grab_blob(sha1
, &size
);
128 write_to_temp_file(tmpfile
, blob
, size
);
132 static int parse_num(char **cp_p
, unsigned int *num_p
)
135 unsigned int num
= 0;
138 while ('0' <= *cp
&& *cp
<= '9')
139 num
= num
* 10 + *cp
++ - '0';
140 if (!(read_some
= cp
- *cp_p
))
147 static int parse_hunk_header(char *line
, int len
,
148 unsigned int *ob
, unsigned int *on
,
149 unsigned int *nb
, unsigned int *nn
)
153 if (parse_num(&cp
, ob
)) {
155 return error("malformed diff output: %s", line
);
159 if (parse_num(&cp
, on
))
164 if (*cp
++ != ' ' || *cp
++ != '+')
166 if (parse_num(&cp
, nb
))
170 if (parse_num(&cp
, nn
))
175 return -!!memcmp(cp
, " @@", 3);
178 static void append_lost(struct sline
*sline
, int n
, const char *line
)
181 int len
= strlen(line
);
182 unsigned long this_mask
= (1UL<<n
);
183 if (line
[len
-1] == '\n')
186 /* Check to see if we can squash things */
187 if (sline
->lost_head
) {
188 struct lline
*last_one
= NULL
;
189 /* We cannot squash it with earlier one */
190 for (lline
= sline
->lost_head
;
193 if (lline
->parent_map
& this_mask
)
195 lline
= last_one
? last_one
->next
: sline
->lost_head
;
197 if (lline
->len
== len
&&
198 !memcmp(lline
->line
, line
, len
)) {
199 lline
->parent_map
|= this_mask
;
206 lline
= xmalloc(sizeof(*lline
) + len
+ 1);
209 lline
->parent_map
= this_mask
;
210 memcpy(lline
->line
, line
, len
);
211 lline
->line
[len
] = 0;
212 *sline
->lost_tail
= lline
;
213 sline
->lost_tail
= &lline
->next
;
216 static void combine_diff(const unsigned char *parent
, const char *ourtmp
,
217 struct sline
*sline
, int cnt
, int n
, int num_parent
)
220 char parent_tmp
[TMPPATHLEN
];
221 char cmd
[TMPPATHLEN
* 2 + 1024];
222 char line
[MAXLINELEN
];
223 unsigned int lno
, ob
, on
, nb
, nn
, p_lno
;
224 unsigned long nmask
= (1UL << n
);
225 struct sline
*lost_bucket
= NULL
;
228 return; /* result deleted */
230 write_temp_blob(parent_tmp
, parent
);
231 sprintf(cmd
, "diff --unified=0 -La/x -Lb/x '%s' '%s'",
233 in
= popen(cmd
, "r");
235 die("cannot spawn %s", cmd
);
238 while (fgets(line
, sizeof(line
), in
) != NULL
) {
239 int len
= strlen(line
);
240 if (5 < len
&& !memcmp("@@ -", line
, 4)) {
241 if (parse_hunk_header(line
, len
,
246 /* @@ -1,2 +0,0 @@ to remove the
251 /* @@ -X,Y +N,0 @@ removed Y lines
252 * that would have come *after* line N
253 * in the result. Our lost buckets hang
254 * to the line after the removed lines,
256 lost_bucket
= &sline
[nb
];
258 lost_bucket
= &sline
[nb
-1];
259 if (!sline
[nb
-1].p_lno
)
262 sizeof(unsigned long));
263 sline
[nb
-1].p_lno
[n
] = ob
;
267 continue; /* not in any hunk yet */
270 append_lost(lost_bucket
, n
, line
+1);
273 sline
[lno
-1].flag
|= nmask
;
281 /* Assign line numbers for this parent.
283 * sline[lno].p_lno[n] records the first line number
284 * (counting from 1) for parent N if the final hunk display
285 * started by showing sline[lno] (possibly showing the lost
286 * lines attached to it first).
288 for (lno
= 0, p_lno
= 1; lno
< cnt
; lno
++) {
290 sline
[lno
].p_lno
[n
] = p_lno
;
292 /* How many lines would this sline advance the p_lno? */
293 ll
= sline
[lno
].lost_head
;
295 if (ll
->parent_map
& nmask
)
296 p_lno
++; /* '-' means parent had it */
299 if (!(sline
[lno
].flag
& nmask
))
300 p_lno
++; /* no '+' means parent had it */
302 sline
[lno
].p_lno
[n
] = p_lno
; /* trailer */
305 static unsigned long context
= 3;
306 static char combine_marker
= '@';
308 static int interesting(struct sline
*sline
, unsigned long all_mask
)
310 /* If some parents lost lines here, or if we have added to
311 * some parent, it is interesting.
313 return ((sline
->flag
& all_mask
) || sline
->lost_head
);
316 static unsigned long adjust_hunk_tail(struct sline
*sline
,
317 unsigned long all_mask
,
318 unsigned long hunk_begin
,
321 /* i points at the first uninteresting line. If the last line
322 * of the hunk was interesting only because it has some
323 * deletion, then it is not all that interesting for the
324 * purpose of giving trailing context lines. This is because
325 * we output '-' line and then unmodified sline[i-1] itself in
326 * that case which gives us one extra context line.
328 if ((hunk_begin
+ 1 <= i
) && !(sline
[i
-1].flag
& all_mask
))
333 static unsigned long find_next(struct sline
*sline
,
339 /* We have examined up to i-1 and are about to look at i.
340 * Find next interesting or uninteresting line. Here,
341 * "interesting" does not mean interesting(), but marked by
342 * the give_context() function below (i.e. it includes context
343 * lines that are not interesting to interesting() function
344 * that are surrounded by interesting() ones.
348 ? !(sline
[i
].flag
& mark
)
349 : (sline
[i
].flag
& mark
))
356 static int give_context(struct sline
*sline
, unsigned long cnt
, int num_parent
)
358 unsigned long all_mask
= (1UL<<num_parent
) - 1;
359 unsigned long mark
= (1UL<<num_parent
);
362 /* Two groups of interesting lines may have a short gap of
363 * unintersting lines. Connect such groups to give them a
366 * We first start from what the interesting() function says,
367 * and mark them with "mark", and paint context lines with the
368 * mark. So interesting() would still say false for such context
369 * lines but they are treated as "interesting" in the end.
371 i
= find_next(sline
, mark
, 0, cnt
, 0);
376 unsigned long j
= (context
< i
) ? (i
- context
) : 0;
379 /* Paint a few lines before the first interesting line. */
381 sline
[j
++].flag
|= mark
;
384 /* we know up to i is to be included. where does the
385 * next uninteresting one start?
387 j
= find_next(sline
, mark
, i
, cnt
, 1);
389 break; /* the rest are all interesting */
391 /* lookahead context lines */
392 k
= find_next(sline
, mark
, j
, cnt
, 0);
393 j
= adjust_hunk_tail(sline
, all_mask
, i
, j
);
395 if (k
< j
+ context
) {
396 /* k is interesting and [j,k) are not, but
397 * paint them interesting because the gap is small.
400 sline
[j
++].flag
|= mark
;
405 /* j is the first uninteresting line and there is
406 * no overlap beyond it within context lines. Paint
407 * the trailing edge a bit.
410 k
= (j
+ context
< cnt
) ? j
+ context
: cnt
;
412 sline
[j
++].flag
|= mark
;
417 static int make_hunks(struct sline
*sline
, unsigned long cnt
,
418 int num_parent
, int dense
)
420 unsigned long all_mask
= (1UL<<num_parent
) - 1;
421 unsigned long mark
= (1UL<<num_parent
);
423 int has_interesting
= 0;
425 for (i
= 0; i
< cnt
; i
++) {
426 if (interesting(&sline
[i
], all_mask
))
427 sline
[i
].flag
|= mark
;
429 sline
[i
].flag
&= ~mark
;
432 return give_context(sline
, cnt
, num_parent
);
434 /* Look at each hunk, and if we have changes from only one
435 * parent, or the changes are the same from all but one
436 * parent, mark that uninteresting.
440 unsigned long j
, hunk_begin
, hunk_end
;
441 unsigned long same_diff
;
442 while (i
< cnt
&& !(sline
[i
].flag
& mark
))
445 break; /* No more interesting hunks */
447 for (j
= i
+ 1; j
< cnt
; j
++) {
448 if (!(sline
[j
].flag
& mark
)) {
449 /* Look beyond the end to see if there
450 * is an interesting line after this
451 * hunk within context span.
453 unsigned long la
; /* lookahead */
455 la
= adjust_hunk_tail(sline
, all_mask
,
457 la
= (la
+ context
< cnt
) ?
458 (la
+ context
) : cnt
;
460 if (sline
[la
].flag
& mark
) {
472 /* [i..hunk_end) are interesting. Now is it really
473 * interesting? We check if there are only two versions
474 * and the result matches one of them. That is, we look
476 * (+) line, which records lines added to which parents;
477 * this line appears in the result.
478 * (-) line, which records from what parents the line
479 * was removed; this line does not appear in the result.
480 * then check the set of parents the result has difference
481 * from, from all lines. If there are lines that has
482 * different set of parents that the result has differences
483 * from, that means we have more than two versions.
485 * Even when we have only two versions, if the result does
486 * not match any of the parents, the it should be considered
487 * interesting. In such a case, we would have all '+' line.
488 * After passing the above "two versions" test, that would
489 * appear as "the same set of parents" to be "all parents".
493 for (j
= i
; j
< hunk_end
&& !has_interesting
; j
++) {
494 unsigned long this_diff
= sline
[j
].flag
& all_mask
;
495 struct lline
*ll
= sline
[j
].lost_head
;
497 /* This has some changes. Is it the
501 same_diff
= this_diff
;
502 else if (same_diff
!= this_diff
) {
507 while (ll
&& !has_interesting
) {
508 /* Lost this line from these parents;
509 * who are they? Are they the same?
511 this_diff
= ll
->parent_map
;
513 same_diff
= this_diff
;
514 else if (same_diff
!= this_diff
) {
521 if (!has_interesting
&& same_diff
!= all_mask
) {
522 /* This hunk is not that interesting after all */
523 for (j
= hunk_begin
; j
< hunk_end
; j
++)
524 sline
[j
].flag
&= ~mark
;
529 has_interesting
= give_context(sline
, cnt
, num_parent
);
530 return has_interesting
;
533 static void show_parent_lno(struct sline
*sline
, unsigned long l0
, unsigned long l1
, unsigned long cnt
, int n
)
535 l0
= sline
[l0
].p_lno
[n
];
536 l1
= sline
[l1
].p_lno
[n
];
537 printf(" -%lu,%lu", l0
, l1
-l0
);
540 static void dump_sline(struct sline
*sline
, unsigned long cnt
, int num_parent
)
542 unsigned long mark
= (1UL<<num_parent
);
544 unsigned long lno
= 0;
547 return; /* result deleted */
550 struct sline
*sl
= &sline
[lno
];
552 while (lno
< cnt
&& !(sline
[lno
].flag
& mark
))
556 for (hunk_end
= lno
+ 1; hunk_end
< cnt
; hunk_end
++)
557 if (!(sline
[hunk_end
].flag
& mark
))
559 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
560 for (i
= 0; i
< num_parent
; i
++)
561 show_parent_lno(sline
, lno
, hunk_end
, cnt
, i
);
562 printf(" +%lu,%lu ", lno
+1, hunk_end
-lno
);
563 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
565 while (lno
< hunk_end
) {
568 unsigned long p_mask
;
572 for (j
= 0; j
< num_parent
; j
++) {
573 if (ll
->parent_map
& (1UL<<j
))
582 for (j
= 0; j
< num_parent
; j
++) {
583 if (p_mask
& sl
->flag
)
589 printf("%.*s\n", sl
->len
, sl
->bol
);
594 static void reuse_combine_diff(struct sline
*sline
, unsigned long cnt
,
597 /* We have already examined parent j and we know parent i
598 * and parent j are the same, so reuse the combined result
599 * of parent j for parent i.
601 unsigned long lno
, imask
, jmask
;
605 for (lno
= 0; lno
< cnt
; lno
++) {
606 struct lline
*ll
= sline
->lost_head
;
607 sline
->p_lno
[i
] = sline
->p_lno
[j
];
609 if (ll
->parent_map
& jmask
)
610 ll
->parent_map
|= imask
;
613 if (sline
->flag
& jmask
)
614 sline
->flag
|= imask
;
617 /* the overall size of the file (sline[cnt]) */
618 sline
->p_lno
[i
] = sline
->p_lno
[j
];
621 int show_combined_diff(struct combine_diff_path
*elem
, int num_parent
,
622 int dense
, const char *header
)
624 unsigned long size
, cnt
, lno
;
625 char *result
, *cp
, *ep
;
626 struct sline
*sline
; /* survived lines */
627 int mode_differs
= 0;
628 int i
, show_hunks
, shown_header
= 0;
629 char ourtmp_buf
[TMPPATHLEN
];
630 char *ourtmp
= ourtmp_buf
;
632 /* Read the result of merge first */
633 if (memcmp(elem
->sha1
, null_sha1
, 20)) {
634 result
= grab_blob(elem
->sha1
, &size
);
635 write_to_temp_file(ourtmp
, result
, size
);
638 /* Used by diff-tree to read from the working tree */
642 if (0 <= (fd
= open(ourtmp
, O_RDONLY
)) &&
644 int len
= st
.st_size
;
648 result
= xmalloc(len
+ 1);
650 int done
= xread(fd
, result
+cnt
, len
-cnt
);
654 die("read error '%s'", ourtmp
);
664 ourtmp
= "/dev/null";
670 for (cnt
= 0, cp
= result
; cp
- result
< size
; cp
++) {
674 if (size
&& result
[size
-1] != '\n')
675 cnt
++; /* incomplete line */
677 sline
= xcalloc(cnt
+1, sizeof(*sline
));
679 sline
[0].bol
= result
;
680 for (lno
= 0; lno
<= cnt
; lno
++) {
681 sline
[lno
].lost_tail
= &sline
[lno
].lost_head
;
684 for (lno
= 0, cp
= result
; cp
- result
< size
; cp
++) {
686 sline
[lno
].len
= cp
- sline
[lno
].bol
;
689 sline
[lno
].bol
= cp
+ 1;
692 if (size
&& result
[size
-1] != '\n')
693 sline
[cnt
-1].len
= size
- (sline
[cnt
-1].bol
- result
);
695 sline
[0].p_lno
= xcalloc((cnt
+1) * num_parent
, sizeof(unsigned long));
696 for (lno
= 0; lno
< cnt
; lno
++)
697 sline
[lno
+1].p_lno
= sline
[lno
].p_lno
+ num_parent
;
699 for (i
= 0; i
< num_parent
; i
++) {
701 for (j
= 0; j
< i
; j
++) {
702 if (!memcmp(elem
->parent
[i
].sha1
,
703 elem
->parent
[j
].sha1
, 20)) {
704 reuse_combine_diff(sline
, cnt
, i
, j
);
709 combine_diff(elem
->parent
[i
].sha1
, ourtmp
, sline
,
711 if (elem
->parent
[i
].mode
!= elem
->mode
)
715 show_hunks
= make_hunks(sline
, cnt
, num_parent
, dense
);
717 if (show_hunks
|| mode_differs
) {
719 char null_abb
[DEFAULT_ABBREV
+ 1];
721 memset(null_abb
, '0', DEFAULT_ABBREV
);
722 null_abb
[DEFAULT_ABBREV
] = 0;
727 printf("diff --%s ", dense
? "cc" : "combined");
728 if (quote_c_style(elem
->path
, NULL
, NULL
, 0))
729 quote_c_style(elem
->path
, NULL
, stdout
, 0);
731 printf("%s", elem
->path
);
734 for (i
= 0; i
< num_parent
; i
++) {
735 if (elem
->parent
[i
].mode
!= elem
->mode
)
737 if (memcmp(elem
->parent
[i
].sha1
, null_sha1
, 20))
738 abb
= find_unique_abbrev(elem
->parent
[i
].sha1
,
742 printf("%s%s", i
? "," : "", abb
);
744 if (memcmp(elem
->sha1
, null_sha1
, 20))
745 abb
= find_unique_abbrev(elem
->sha1
, DEFAULT_ABBREV
);
748 printf("..%s\n", abb
);
752 for (i
= 0; i
< num_parent
; i
++) {
753 printf("%s%06o", i
? "," : "",
754 elem
->parent
[i
].mode
);
756 printf("..%06o\n", elem
->mode
);
758 dump_sline(sline
, cnt
, num_parent
);
760 if (ourtmp
== ourtmp_buf
)
764 for (i
= 0; i
< cnt
; i
++) {
765 if (sline
[i
].lost_head
) {
766 struct lline
*ll
= sline
[i
].lost_head
;
768 struct lline
*tmp
= ll
;
774 free(sline
[0].p_lno
);
779 int diff_tree_combined_merge(const unsigned char *sha1
,
780 const char *header
, int dense
)
782 struct commit
*commit
= lookup_commit(sha1
);
783 struct diff_options diffopts
;
784 struct commit_list
*parents
;
785 struct combine_diff_path
*p
, *paths
= NULL
;
786 int num_parent
, i
, num_paths
;
788 diff_setup(&diffopts
);
789 diffopts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
790 diffopts
.recursive
= 1;
793 for (parents
= commit
->parents
, num_parent
= 0;
795 parents
= parents
->next
, num_parent
++)
798 /* find set of paths that everybody touches */
799 for (parents
= commit
->parents
, i
= 0;
801 parents
= parents
->next
, i
++) {
802 struct commit
*parent
= parents
->item
;
803 diff_tree_sha1(parent
->object
.sha1
, commit
->object
.sha1
, "",
805 paths
= intersect_paths(paths
, i
, num_parent
);
806 diff_flush(&diffopts
);
809 /* find out surviving paths */
810 for (num_paths
= 0, p
= paths
; p
; p
= p
->next
) {
815 for (p
= paths
; p
; p
= p
->next
) {
818 if (show_combined_diff(p
, num_parent
, dense
, header
))
823 /* Clean things up */
825 struct combine_diff_path
*tmp
= paths
;