7 static int uninteresting(struct diff_filepair
*p
)
9 if (diff_unmodified_pair(p
))
11 if (!S_ISREG(p
->one
->mode
) || !S_ISREG(p
->two
->mode
))
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
;
32 p
= xmalloc(sizeof(*p
) + len
+ 1 + num_parent
* 20);
33 p
->path
= (char*) &(p
->parent_sha1
[num_parent
][0]);
34 memcpy(p
->path
, path
, len
);
38 memcpy(p
->sha1
, q
->queue
[i
]->two
->sha1
, 20);
39 memcpy(p
->parent_sha1
[n
], q
->queue
[i
]->one
->sha1
, 20);
46 for (p
= curr
; p
; p
= p
->next
) {
50 for (i
= 0; i
< q
->nr
; i
++) {
54 if (uninteresting(q
->queue
[i
]))
56 path
= q
->queue
[i
]->two
->path
;
58 if (len
== p
->len
&& !memcmp(path
, p
->path
, len
)) {
60 memcpy(p
->parent_sha1
[n
],
61 q
->queue
[i
]->one
->sha1
, 20);
71 /* Lines lost from parent */
75 unsigned long parent_map
;
76 char line
[FLEX_ARRAY
];
79 /* Lines surviving in the merge result */
81 struct lline
*lost_head
, **lost_tail
;
84 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
85 * we did not change it).
86 * bit N is used for "interesting" lines, including context.
92 static char *grab_blob(const unsigned char *sha1
, unsigned long *size
)
96 if (!memcmp(sha1
, null_sha1
, 20)) {
101 blob
= read_sha1_file(sha1
, type
, size
);
102 if (strcmp(type
, "blob"))
103 die("object '%s' is not a blob!", sha1_to_hex(sha1
));
107 #define TMPPATHLEN 50
108 #define MAXLINELEN 10240
110 static void write_to_temp_file(char *tmpfile
, void *blob
, unsigned long size
)
112 int fd
= git_mkstemp(tmpfile
, TMPPATHLEN
, ".diff_XXXXXX");
114 die("unable to create temp-file");
115 if (write(fd
, blob
, size
) != size
)
116 die("unable to write temp-file");
120 static void write_temp_blob(char *tmpfile
, const unsigned char *sha1
)
124 blob
= grab_blob(sha1
, &size
);
125 write_to_temp_file(tmpfile
, blob
, size
);
129 static int parse_num(char **cp_p
, unsigned int *num_p
)
132 unsigned int num
= 0;
135 while ('0' <= *cp
&& *cp
<= '9')
136 num
= num
* 10 + *cp
++ - '0';
137 if (!(read_some
= cp
- *cp_p
))
144 static int parse_hunk_header(char *line
, int len
,
145 unsigned int *ob
, unsigned int *on
,
146 unsigned int *nb
, unsigned int *nn
)
150 if (parse_num(&cp
, ob
)) {
152 return error("malformed diff output: %s", line
);
156 if (parse_num(&cp
, on
))
161 if (*cp
++ != ' ' || *cp
++ != '+')
163 if (parse_num(&cp
, nb
))
167 if (parse_num(&cp
, nn
))
172 return -!!memcmp(cp
, " @@", 3);
175 static void append_lost(struct sline
*sline
, int n
, const char *line
)
178 int len
= strlen(line
);
179 unsigned long this_mask
= (1UL<<n
);
180 if (line
[len
-1] == '\n')
183 /* Check to see if we can squash things */
184 if (sline
->lost_head
) {
185 struct lline
*last_one
= NULL
;
186 /* We cannot squash it with earlier one */
187 for (lline
= sline
->lost_head
;
190 if (lline
->parent_map
& this_mask
)
192 lline
= last_one
? last_one
->next
: sline
->lost_head
;
194 if (lline
->len
== len
&&
195 !memcmp(lline
->line
, line
, len
)) {
196 lline
->parent_map
|= this_mask
;
203 lline
= xmalloc(sizeof(*lline
) + len
+ 1);
206 lline
->parent_map
= this_mask
;
207 memcpy(lline
->line
, line
, len
);
208 lline
->line
[len
] = 0;
209 *sline
->lost_tail
= lline
;
210 sline
->lost_tail
= &lline
->next
;
213 static void combine_diff(const unsigned char *parent
, const char *ourtmp
,
214 struct sline
*sline
, int cnt
, int n
, int num_parent
)
217 char parent_tmp
[TMPPATHLEN
];
218 char cmd
[TMPPATHLEN
* 2 + 1024];
219 char line
[MAXLINELEN
];
220 unsigned int lno
, ob
, on
, nb
, nn
, p_lno
;
221 unsigned long nmask
= (1UL << n
);
222 struct sline
*lost_bucket
= NULL
;
224 write_temp_blob(parent_tmp
, parent
);
225 sprintf(cmd
, "diff --unified=0 -La/x -Lb/x '%s' '%s'",
227 in
= popen(cmd
, "r");
229 die("cannot spawn %s", cmd
);
232 while (fgets(line
, sizeof(line
), in
) != NULL
) {
233 int len
= strlen(line
);
234 if (5 < len
&& !memcmp("@@ -", line
, 4)) {
235 if (parse_hunk_header(line
, len
,
240 /* @@ -1,2 +0,0 @@ to remove the
245 /* @@ -X,Y +N,0 @@ removed Y lines
246 * that would have come *after* line N
247 * in the result. Our lost buckets hang
248 * to the line after the removed lines,
250 lost_bucket
= &sline
[nb
];
252 lost_bucket
= &sline
[nb
-1];
253 if (!sline
[nb
-1].p_lno
)
256 sizeof(unsigned long));
257 sline
[nb
-1].p_lno
[n
] = ob
;
261 continue; /* not in any hunk yet */
264 append_lost(lost_bucket
, n
, line
+1);
267 sline
[lno
-1].flag
|= nmask
;
275 /* Assign line numbers for this parent.
277 * sline[lno].p_lno[n] records the first line number
278 * (counting from 1) for parent N if the final hunk display
279 * started by showing sline[lno] (possibly showing the lost
280 * lines attached to it first).
282 for (lno
= 0, p_lno
= 1; lno
< cnt
; lno
++) {
284 sline
[lno
].p_lno
[n
] = p_lno
;
286 /* How many lines would this sline advance the p_lno? */
287 ll
= sline
[lno
].lost_head
;
289 if (ll
->parent_map
& nmask
)
290 p_lno
++; /* '-' means parent had it */
293 if (!(sline
[lno
].flag
& nmask
))
294 p_lno
++; /* no '+' means parent had it */
296 sline
[lno
].p_lno
[n
] = p_lno
; /* trailer */
299 static unsigned long context
= 3;
300 static char combine_marker
= '@';
302 static int interesting(struct sline
*sline
, unsigned long all_mask
)
304 /* If some parents lost lines here, or if we have added to
305 * some parent, it is interesting.
307 return ((sline
->flag
& all_mask
) || sline
->lost_head
);
310 static unsigned long adjust_hunk_tail(struct sline
*sline
,
311 unsigned long all_mask
,
312 unsigned long hunk_begin
,
315 /* i points at the first uninteresting line. If the last line
316 * of the hunk was interesting only because it has some
317 * deletion, then it is not all that interesting for the
318 * purpose of giving trailing context lines. This is because
319 * we output '-' line and then unmodified sline[i-1] itself in
320 * that case which gives us one extra context line.
322 if ((hunk_begin
+ 1 <= i
) && !(sline
[i
-1].flag
& all_mask
))
327 static unsigned long find_next(struct sline
*sline
,
333 /* We have examined up to i-1 and are about to look at i.
334 * Find next interesting or uninteresting line. Here,
335 * "interesting" does not mean interesting(), but marked by
336 * the give_context() function below (i.e. it includes context
337 * lines that are not interesting to interesting() function
338 * that are surrounded by interesting() ones.
342 ? !(sline
[i
].flag
& mark
)
343 : (sline
[i
].flag
& mark
))
350 static int give_context(struct sline
*sline
, unsigned long cnt
, int num_parent
)
352 unsigned long all_mask
= (1UL<<num_parent
) - 1;
353 unsigned long mark
= (1UL<<num_parent
);
356 /* Two groups of interesting lines may have a short gap of
357 * unintersting lines. Connect such groups to give them a
360 * We first start from what the interesting() function says,
361 * and mark them with "mark", and paint context lines with the
362 * mark. So interesting() would still say false for such context
363 * lines but they are treated as "interesting" in the end.
365 i
= find_next(sline
, mark
, 0, cnt
, 0);
370 unsigned long j
= (context
< i
) ? (i
- context
) : 0;
373 /* Paint a few lines before the first interesting line. */
375 sline
[j
++].flag
|= mark
;
378 /* we know up to i is to be included. where does the
379 * next uninteresting one start?
381 j
= find_next(sline
, mark
, i
, cnt
, 1);
383 break; /* the rest are all interesting */
385 /* lookahead context lines */
386 k
= find_next(sline
, mark
, j
, cnt
, 0);
387 j
= adjust_hunk_tail(sline
, all_mask
, i
, j
);
389 if (k
< j
+ context
) {
390 /* k is interesting and [j,k) are not, but
391 * paint them interesting because the gap is small.
394 sline
[j
++].flag
|= mark
;
399 /* j is the first uninteresting line and there is
400 * no overlap beyond it within context lines. Paint
401 * the trailing edge a bit.
404 k
= (j
+ context
< cnt
) ? j
+ context
: cnt
;
406 sline
[j
++].flag
|= mark
;
411 static int make_hunks(struct sline
*sline
, unsigned long cnt
,
412 int num_parent
, int dense
)
414 unsigned long all_mask
= (1UL<<num_parent
) - 1;
415 unsigned long mark
= (1UL<<num_parent
);
417 int has_interesting
= 0;
419 for (i
= 0; i
< cnt
; i
++) {
420 if (interesting(&sline
[i
], all_mask
))
421 sline
[i
].flag
|= mark
;
423 sline
[i
].flag
&= ~mark
;
426 return give_context(sline
, cnt
, num_parent
);
428 /* Look at each hunk, and if we have changes from only one
429 * parent, or the changes are the same from all but one
430 * parent, mark that uninteresting.
434 unsigned long j
, hunk_begin
, hunk_end
;
435 unsigned long same_diff
;
436 while (i
< cnt
&& !(sline
[i
].flag
& mark
))
439 break; /* No more interesting hunks */
441 for (j
= i
+ 1; j
< cnt
; j
++) {
442 if (!(sline
[j
].flag
& mark
)) {
443 /* Look beyond the end to see if there
444 * is an interesting line after this
445 * hunk within context span.
447 unsigned long la
; /* lookahead */
449 la
= adjust_hunk_tail(sline
, all_mask
,
451 la
= (la
+ context
< cnt
) ?
452 (la
+ context
) : cnt
;
454 if (sline
[la
].flag
& mark
) {
466 /* [i..hunk_end) are interesting. Now is it really
467 * interesting? We check if there are only two versions
468 * and the result matches one of them. That is, we look
470 * (+) line, which records lines added to which parents;
471 * this line appears in the result.
472 * (-) line, which records from what parents the line
473 * was removed; this line does not appear in the result.
474 * then check the set of parents the result has difference
475 * from, from all lines. If there are lines that has
476 * different set of parents that the result has differences
477 * from, that means we have more than two versions.
479 * Even when we have only two versions, if the result does
480 * not match any of the parents, the it should be considered
481 * interesting. In such a case, we would have all '+' line.
482 * After passing the above "two versions" test, that would
483 * appear as "the same set of parents" to be "all parents".
487 for (j
= i
; j
< hunk_end
&& !has_interesting
; j
++) {
488 unsigned long this_diff
= sline
[j
].flag
& all_mask
;
489 struct lline
*ll
= sline
[j
].lost_head
;
491 /* This has some changes. Is it the
495 same_diff
= this_diff
;
496 else if (same_diff
!= this_diff
) {
501 while (ll
&& !has_interesting
) {
502 /* Lost this line from these parents;
503 * who are they? Are they the same?
505 this_diff
= ll
->parent_map
;
507 same_diff
= this_diff
;
508 else if (same_diff
!= this_diff
) {
515 if (!has_interesting
&& same_diff
!= all_mask
) {
516 /* This hunk is not that interesting after all */
517 for (j
= hunk_begin
; j
< hunk_end
; j
++)
518 sline
[j
].flag
&= ~mark
;
523 has_interesting
= give_context(sline
, cnt
, num_parent
);
524 return has_interesting
;
527 static void show_parent_lno(struct sline
*sline
, unsigned long l0
, unsigned long l1
, unsigned long cnt
, int n
)
529 l0
= sline
[l0
].p_lno
[n
];
530 l1
= sline
[l1
].p_lno
[n
];
531 printf(" -%lu,%lu", l0
, l1
-l0
);
534 static void dump_sline(struct sline
*sline
, unsigned long cnt
, int num_parent
)
536 unsigned long mark
= (1UL<<num_parent
);
538 unsigned long lno
= 0;
541 struct sline
*sl
= &sline
[lno
];
543 while (lno
< cnt
&& !(sline
[lno
].flag
& mark
))
547 for (hunk_end
= lno
+ 1; hunk_end
< cnt
; hunk_end
++)
548 if (!(sline
[hunk_end
].flag
& mark
))
550 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
551 for (i
= 0; i
< num_parent
; i
++)
552 show_parent_lno(sline
, lno
, hunk_end
, cnt
, i
);
553 printf(" +%lu,%lu ", lno
+1, hunk_end
-lno
);
554 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
556 while (lno
< hunk_end
) {
559 unsigned long p_mask
;
563 for (j
= 0; j
< num_parent
; j
++) {
564 if (ll
->parent_map
& (1UL<<j
))
573 for (j
= 0; j
< num_parent
; j
++) {
574 if (p_mask
& sl
->flag
)
580 printf("%.*s\n", sl
->len
, sl
->bol
);
585 static void reuse_combine_diff(struct sline
*sline
, unsigned long cnt
,
588 /* We have already examined parent j and we know parent i
589 * and parent j are the same, so reuse the combined result
590 * of parent j for parent i.
592 unsigned long lno
, imask
, jmask
;
596 for (lno
= 0; lno
< cnt
; lno
++) {
597 struct lline
*ll
= sline
->lost_head
;
598 sline
->p_lno
[i
] = sline
->p_lno
[j
];
600 if (ll
->parent_map
& jmask
)
601 ll
->parent_map
|= imask
;
604 if (sline
->flag
& jmask
)
605 sline
->flag
|= imask
;
610 int show_combined_diff(struct combine_diff_path
*elem
, int num_parent
,
611 int dense
, const char *header
)
613 unsigned long size
, cnt
, lno
;
614 char *result
, *cp
, *ep
;
615 struct sline
*sline
; /* survived lines */
616 int i
, show_hunks
, shown_header
= 0;
617 char ourtmp_buf
[TMPPATHLEN
];
618 char *ourtmp
= ourtmp_buf
;
620 /* Read the result of merge first */
621 if (memcmp(elem
->sha1
, null_sha1
, 20)) {
622 result
= grab_blob(elem
->sha1
, &size
);
623 write_to_temp_file(ourtmp
, result
, size
);
629 if (0 <= (fd
= open(ourtmp
, O_RDONLY
)) &&
631 int len
= st
.st_size
;
635 result
= xmalloc(len
+ 1);
637 int done
= xread(fd
, result
+cnt
, len
-cnt
);
641 die("read error '%s'", ourtmp
);
651 ourtmp
= "/dev/null";
657 for (cnt
= 0, cp
= result
; cp
- result
< size
; cp
++) {
661 if (result
[size
-1] != '\n')
662 cnt
++; /* incomplete line */
664 sline
= xcalloc(cnt
+1, sizeof(*sline
));
666 sline
[0].bol
= result
;
667 for (lno
= 0, cp
= result
; cp
- result
< size
; cp
++) {
669 sline
[lno
].lost_tail
= &sline
[lno
].lost_head
;
670 sline
[lno
].len
= cp
- sline
[lno
].bol
;
674 sline
[lno
].bol
= cp
+ 1;
677 if (result
[size
-1] != '\n') {
678 sline
[cnt
-1].lost_tail
= &sline
[cnt
-1].lost_head
;
679 sline
[cnt
-1].len
= size
- (sline
[cnt
-1].bol
- result
);
680 sline
[cnt
-1].flag
= 0;
683 sline
[0].p_lno
= xcalloc((cnt
+1) * num_parent
, sizeof(unsigned long));
684 for (lno
= 0; lno
< cnt
; lno
++)
685 sline
[lno
+1].p_lno
= sline
[lno
].p_lno
+ num_parent
;
687 for (i
= 0; i
< num_parent
; i
++) {
689 for (j
= 0; j
< i
; j
++) {
690 if (!memcmp(elem
->parent_sha1
[i
],
691 elem
->parent_sha1
[j
], 20)) {
692 reuse_combine_diff(sline
, cnt
, i
, j
);
697 combine_diff(elem
->parent_sha1
[i
], ourtmp
, sline
,
701 show_hunks
= make_hunks(sline
, cnt
, num_parent
, dense
);
708 printf("diff --%s ", dense
? "cc" : "combined");
709 if (quote_c_style(elem
->path
, NULL
, NULL
, 0))
710 quote_c_style(elem
->path
, NULL
, stdout
, 0);
712 printf("%s", elem
->path
);
715 for (i
= 0; i
< num_parent
; i
++) {
718 find_unique_abbrev(elem
->parent_sha1
[i
],
722 find_unique_abbrev(elem
->sha1
, DEFAULT_ABBREV
));
723 dump_sline(sline
, cnt
, num_parent
);
725 if (ourtmp
== ourtmp_buf
)
729 for (i
= 0; i
< cnt
; i
++) {
730 if (sline
[i
].lost_head
) {
731 struct lline
*ll
= sline
[i
].lost_head
;
733 struct lline
*tmp
= ll
;
739 free(sline
[0].p_lno
);
744 int diff_tree_combined_merge(const unsigned char *sha1
,
745 const char *header
, int dense
)
747 struct commit
*commit
= lookup_commit(sha1
);
748 struct diff_options diffopts
;
749 struct commit_list
*parents
;
750 struct combine_diff_path
*p
, *paths
= NULL
;
751 int num_parent
, i
, num_paths
;
753 diff_setup(&diffopts
);
754 diffopts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
755 diffopts
.recursive
= 1;
758 for (parents
= commit
->parents
, num_parent
= 0;
760 parents
= parents
->next
, num_parent
++)
763 /* find set of paths that everybody touches */
764 for (parents
= commit
->parents
, i
= 0;
766 parents
= parents
->next
, i
++) {
767 struct commit
*parent
= parents
->item
;
768 diff_tree_sha1(parent
->object
.sha1
, commit
->object
.sha1
, "",
770 paths
= intersect_paths(paths
, i
, num_parent
);
771 diff_flush(&diffopts
);
774 /* find out surviving paths */
775 for (num_paths
= 0, p
= paths
; p
; p
= p
->next
) {
780 for (p
= paths
; p
; p
= p
->next
) {
783 if (show_combined_diff(p
, num_parent
, dense
, header
))
788 /* Clean things up */
790 struct combine_diff_path
*tmp
= paths
;