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;
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;
50 for (p = curr; p; p = p->next) {
54 for (i = 0; i < q->nr; i++) {
58 if (uninteresting(q->queue[i]))
60 path = q->queue[i]->two->path;
62 if (len == p->len && !memcmp(path, p->path, len)) {
64 memcpy(p->parent[n].sha1,
65 q->queue[i]->one->sha1, 20);
66 p->parent[n].mode = q->queue[i]->one->mode;
76 /* Lines lost from parent */
80 unsigned long parent_map;
81 char line[FLEX_ARRAY];
84 /* Lines surviving in the merge result */
86 struct lline *lost_head, **lost_tail;
89 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
90 * we did not change it).
91 * bit N is used for "interesting" lines, including context.
97 static char *grab_blob(const unsigned char *sha1, unsigned long *size)
101 if (!memcmp(sha1, null_sha1, 20)) {
104 return xcalloc(1, 1);
106 blob = read_sha1_file(sha1, type, size);
107 if (strcmp(type, "blob"))
108 die("object '%s' is not a blob!", sha1_to_hex(sha1));
112 #define TMPPATHLEN 50
113 #define MAXLINELEN 10240
115 static void write_to_temp_file(char *tmpfile, void *blob, unsigned long size)
117 int fd = git_mkstemp(tmpfile, TMPPATHLEN, ".diff_XXXXXX");
119 die("unable to create temp-file");
120 if (write(fd, blob, size) != size)
121 die("unable to write temp-file");
125 static void write_temp_blob(char *tmpfile, const unsigned char *sha1)
129 blob = grab_blob(sha1, &size);
130 write_to_temp_file(tmpfile, blob, size);
134 static int parse_num(char **cp_p, unsigned int *num_p)
137 unsigned int num = 0;
140 while ('0' <= *cp && *cp <= '9')
141 num = num * 10 + *cp++ - '0';
142 if (!(read_some = cp - *cp_p))
149 static int parse_hunk_header(char *line, int len,
150 unsigned int *ob, unsigned int *on,
151 unsigned int *nb, unsigned int *nn)
155 if (parse_num(&cp, ob)) {
157 return error("malformed diff output: %s", line);
161 if (parse_num(&cp, on))
166 if (*cp++ != ' ' || *cp++ != '+')
168 if (parse_num(&cp, nb))
172 if (parse_num(&cp, nn))
177 return -!!memcmp(cp, " @@", 3);
180 static void append_lost(struct sline *sline, int n, const char *line)
183 int len = strlen(line);
184 unsigned long this_mask = (1UL<<n);
185 if (line[len-1] == '\n')
188 /* Check to see if we can squash things */
189 if (sline->lost_head) {
190 struct lline *last_one = NULL;
191 /* We cannot squash it with earlier one */
192 for (lline = sline->lost_head;
195 if (lline->parent_map & this_mask)
197 lline = last_one ? last_one->next : sline->lost_head;
199 if (lline->len == len &&
200 !memcmp(lline->line, line, len)) {
201 lline->parent_map |= this_mask;
208 lline = xmalloc(sizeof(*lline) + len + 1);
211 lline->parent_map = this_mask;
212 memcpy(lline->line, line, len);
213 lline->line[len] = 0;
214 *sline->lost_tail = lline;
215 sline->lost_tail = &lline->next;
218 static void combine_diff(const unsigned char *parent, const char *ourtmp,
219 struct sline *sline, int cnt, int n, int num_parent)
222 char parent_tmp[TMPPATHLEN];
223 char cmd[TMPPATHLEN * 2 + 1024];
224 char line[MAXLINELEN];
225 unsigned int lno, ob, on, nb, nn, p_lno;
226 unsigned long nmask = (1UL << n);
227 struct sline *lost_bucket = NULL;
229 write_temp_blob(parent_tmp, parent);
230 sprintf(cmd, "diff --unified=0 -La/x -Lb/x '%s' '%s'",
232 in = popen(cmd, "r");
234 die("cannot spawn %s", cmd);
237 while (fgets(line, sizeof(line), in) != NULL) {
238 int len = strlen(line);
239 if (5 < len && !memcmp("@@ -", line, 4)) {
240 if (parse_hunk_header(line, len,
245 /* @@ -1,2 +0,0 @@ to remove the
250 /* @@ -X,Y +N,0 @@ removed Y lines
251 * that would have come *after* line N
252 * in the result. Our lost buckets hang
253 * to the line after the removed lines,
255 lost_bucket = &sline[nb];
257 lost_bucket = &sline[nb-1];
258 if (!sline[nb-1].p_lno)
261 sizeof(unsigned long));
262 sline[nb-1].p_lno[n] = ob;
266 continue; /* not in any hunk yet */
269 append_lost(lost_bucket, n, line+1);
272 sline[lno-1].flag |= nmask;
280 /* Assign line numbers for this parent.
282 * sline[lno].p_lno[n] records the first line number
283 * (counting from 1) for parent N if the final hunk display
284 * started by showing sline[lno] (possibly showing the lost
285 * lines attached to it first).
287 for (lno = 0, p_lno = 1; lno < cnt; lno++) {
289 sline[lno].p_lno[n] = p_lno;
291 /* How many lines would this sline advance the p_lno? */
292 ll = sline[lno].lost_head;
294 if (ll->parent_map & nmask)
295 p_lno++; /* '-' means parent had it */
298 if (!(sline[lno].flag & nmask))
299 p_lno++; /* no '+' means parent had it */
301 sline[lno].p_lno[n] = p_lno; /* trailer */
304 static unsigned long context = 3;
305 static char combine_marker = '@';
307 static int interesting(struct sline *sline, unsigned long all_mask)
309 /* If some parents lost lines here, or if we have added to
310 * some parent, it is interesting.
312 return ((sline->flag & all_mask) || sline->lost_head);
315 static unsigned long adjust_hunk_tail(struct sline *sline,
316 unsigned long all_mask,
317 unsigned long hunk_begin,
320 /* i points at the first uninteresting line. If the last line
321 * of the hunk was interesting only because it has some
322 * deletion, then it is not all that interesting for the
323 * purpose of giving trailing context lines. This is because
324 * we output '-' line and then unmodified sline[i-1] itself in
325 * that case which gives us one extra context line.
327 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
332 static unsigned long find_next(struct sline *sline,
338 /* We have examined up to i-1 and are about to look at i.
339 * Find next interesting or uninteresting line. Here,
340 * "interesting" does not mean interesting(), but marked by
341 * the give_context() function below (i.e. it includes context
342 * lines that are not interesting to interesting() function
343 * that are surrounded by interesting() ones.
347 ? !(sline[i].flag & mark)
348 : (sline[i].flag & mark))
355 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
357 unsigned long all_mask = (1UL<<num_parent) - 1;
358 unsigned long mark = (1UL<<num_parent);
361 /* Two groups of interesting lines may have a short gap of
362 * unintersting lines. Connect such groups to give them a
365 * We first start from what the interesting() function says,
366 * and mark them with "mark", and paint context lines with the
367 * mark. So interesting() would still say false for such context
368 * lines but they are treated as "interesting" in the end.
370 i = find_next(sline, mark, 0, cnt, 0);
375 unsigned long j = (context < i) ? (i - context) : 0;
378 /* Paint a few lines before the first interesting line. */
380 sline[j++].flag |= mark;
383 /* we know up to i is to be included. where does the
384 * next uninteresting one start?
386 j = find_next(sline, mark, i, cnt, 1);
388 break; /* the rest are all interesting */
390 /* lookahead context lines */
391 k = find_next(sline, mark, j, cnt, 0);
392 j = adjust_hunk_tail(sline, all_mask, i, j);
394 if (k < j + context) {
395 /* k is interesting and [j,k) are not, but
396 * paint them interesting because the gap is small.
399 sline[j++].flag |= mark;
404 /* j is the first uninteresting line and there is
405 * no overlap beyond it within context lines. Paint
406 * the trailing edge a bit.
409 k = (j + context < cnt) ? j + context : cnt;
411 sline[j++].flag |= mark;
416 static int make_hunks(struct sline *sline, unsigned long cnt,
417 int num_parent, int dense)
419 unsigned long all_mask = (1UL<<num_parent) - 1;
420 unsigned long mark = (1UL<<num_parent);
422 int has_interesting = 0;
424 for (i = 0; i < cnt; i++) {
425 if (interesting(&sline[i], all_mask))
426 sline[i].flag |= mark;
428 sline[i].flag &= ~mark;
431 return give_context(sline, cnt, num_parent);
433 /* Look at each hunk, and if we have changes from only one
434 * parent, or the changes are the same from all but one
435 * parent, mark that uninteresting.
439 unsigned long j, hunk_begin, hunk_end;
440 unsigned long same_diff;
441 while (i < cnt && !(sline[i].flag & mark))
444 break; /* No more interesting hunks */
446 for (j = i + 1; j < cnt; j++) {
447 if (!(sline[j].flag & mark)) {
448 /* Look beyond the end to see if there
449 * is an interesting line after this
450 * hunk within context span.
452 unsigned long la; /* lookahead */
454 la = adjust_hunk_tail(sline, all_mask,
456 la = (la + context < cnt) ?
457 (la + context) : cnt;
459 if (sline[la].flag & mark) {
471 /* [i..hunk_end) are interesting. Now is it really
472 * interesting? We check if there are only two versions
473 * and the result matches one of them. That is, we look
475 * (+) line, which records lines added to which parents;
476 * this line appears in the result.
477 * (-) line, which records from what parents the line
478 * was removed; this line does not appear in the result.
479 * then check the set of parents the result has difference
480 * from, from all lines. If there are lines that has
481 * different set of parents that the result has differences
482 * from, that means we have more than two versions.
484 * Even when we have only two versions, if the result does
485 * not match any of the parents, the it should be considered
486 * interesting. In such a case, we would have all '+' line.
487 * After passing the above "two versions" test, that would
488 * appear as "the same set of parents" to be "all parents".
492 for (j = i; j < hunk_end && !has_interesting; j++) {
493 unsigned long this_diff = sline[j].flag & all_mask;
494 struct lline *ll = sline[j].lost_head;
496 /* This has some changes. Is it the
500 same_diff = this_diff;
501 else if (same_diff != this_diff) {
506 while (ll && !has_interesting) {
507 /* Lost this line from these parents;
508 * who are they? Are they the same?
510 this_diff = ll->parent_map;
512 same_diff = this_diff;
513 else if (same_diff != this_diff) {
520 if (!has_interesting && same_diff != all_mask) {
521 /* This hunk is not that interesting after all */
522 for (j = hunk_begin; j < hunk_end; j++)
523 sline[j].flag &= ~mark;
528 has_interesting = give_context(sline, cnt, num_parent);
529 return has_interesting;
532 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, unsigned long cnt, int n)
534 l0 = sline[l0].p_lno[n];
535 l1 = sline[l1].p_lno[n];
536 printf(" -%lu,%lu", l0, l1-l0);
539 static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
541 unsigned long mark = (1UL<<num_parent);
543 unsigned long lno = 0;
546 struct sline *sl = &sline[lno];
548 while (lno < cnt && !(sline[lno].flag & mark))
552 for (hunk_end = lno + 1; hunk_end < cnt; hunk_end++)
553 if (!(sline[hunk_end].flag & mark))
555 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
556 for (i = 0; i < num_parent; i++)
557 show_parent_lno(sline, lno, hunk_end, cnt, i);
558 printf(" +%lu,%lu ", lno+1, hunk_end-lno);
559 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
561 while (lno < hunk_end) {
564 unsigned long p_mask;
568 for (j = 0; j < num_parent; j++) {
569 if (ll->parent_map & (1UL<<j))
578 for (j = 0; j < num_parent; j++) {
579 if (p_mask & sl->flag)
585 printf("%.*s\n", sl->len, sl->bol);
590 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
593 /* We have already examined parent j and we know parent i
594 * and parent j are the same, so reuse the combined result
595 * of parent j for parent i.
597 unsigned long lno, imask, jmask;
601 for (lno = 0; lno < cnt; lno++) {
602 struct lline *ll = sline->lost_head;
603 sline->p_lno[i] = sline->p_lno[j];
605 if (ll->parent_map & jmask)
606 ll->parent_map |= imask;
609 if (sline->flag & jmask)
610 sline->flag |= imask;
615 int show_combined_diff(struct combine_diff_path *elem, int num_parent,
616 int dense, const char *header)
618 unsigned long size, cnt, lno;
619 char *result, *cp, *ep;
620 struct sline *sline; /* survived lines */
621 int mode_differs = 0;
622 int i, show_hunks, shown_header = 0;
623 char ourtmp_buf[TMPPATHLEN];
624 char *ourtmp = ourtmp_buf;
626 /* Read the result of merge first */
627 if (memcmp(elem->sha1, null_sha1, 20)) {
628 result = grab_blob(elem->sha1, &size);
629 write_to_temp_file(ourtmp, result, size);
632 /* Used by diff-tree to read from the working tree */
636 if (0 <= (fd = open(ourtmp, O_RDONLY)) &&
638 int len = st.st_size;
642 result = xmalloc(len + 1);
644 int done = xread(fd, result+cnt, len-cnt);
648 die("read error '%s'", ourtmp);
658 ourtmp = "/dev/null";
664 for (cnt = 0, cp = result; cp - result < size; cp++) {
668 if (result[size-1] != '\n')
669 cnt++; /* incomplete line */
671 sline = xcalloc(cnt+1, sizeof(*sline));
673 sline[0].bol = result;
674 for (lno = 0, cp = result; cp - result < size; cp++) {
676 sline[lno].lost_tail = &sline[lno].lost_head;
677 sline[lno].len = cp - sline[lno].bol;
681 sline[lno].bol = cp + 1;
684 if (result[size-1] != '\n') {
685 sline[cnt-1].lost_tail = &sline[cnt-1].lost_head;
686 sline[cnt-1].len = size - (sline[cnt-1].bol - result);
687 sline[cnt-1].flag = 0;
690 sline[0].p_lno = xcalloc((cnt+1) * num_parent, sizeof(unsigned long));
691 for (lno = 0; lno < cnt; lno++)
692 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
694 for (i = 0; i < num_parent; i++) {
696 for (j = 0; j < i; j++) {
697 if (!memcmp(elem->parent[i].sha1,
698 elem->parent[j].sha1, 20)) {
699 reuse_combine_diff(sline, cnt, i, j);
704 combine_diff(elem->parent[i].sha1, ourtmp, sline,
706 if (elem->parent[i].mode != elem->mode)
710 show_hunks = make_hunks(sline, cnt, num_parent, dense);
712 if (show_hunks || mode_differs) {
714 char null_abb[DEFAULT_ABBREV + 1];
716 memset(null_abb, '0', DEFAULT_ABBREV);
717 null_abb[DEFAULT_ABBREV] = 0;
722 printf("diff --%s ", dense ? "cc" : "combined");
723 if (quote_c_style(elem->path, NULL, NULL, 0))
724 quote_c_style(elem->path, NULL, stdout, 0);
726 printf("%s", elem->path);
729 for (i = 0; i < num_parent; i++) {
730 if (elem->parent[i].mode != elem->mode)
732 if (memcmp(elem->parent[i].sha1, null_sha1, 20))
733 abb = find_unique_abbrev(elem->parent[i].sha1,
737 printf("%s%s", i ? "," : "", abb);
739 if (memcmp(elem->sha1, null_sha1, 20))
740 abb = find_unique_abbrev(elem->sha1, DEFAULT_ABBREV);
743 printf("..%s\n", abb);
747 for (i = 0; i < num_parent; i++) {
748 printf("%s%06o", i ? "," : "",
749 elem->parent[i].mode);
751 printf("..%06o\n", elem->mode);
753 /* if (show_hunks) perhaps */
754 dump_sline(sline, cnt, num_parent);
756 if (ourtmp == ourtmp_buf)
760 for (i = 0; i < cnt; i++) {
761 if (sline[i].lost_head) {
762 struct lline *ll = sline[i].lost_head;
764 struct lline *tmp = ll;
770 free(sline[0].p_lno);
775 int diff_tree_combined_merge(const unsigned char *sha1,
776 const char *header, int dense)
778 struct commit *commit = lookup_commit(sha1);
779 struct diff_options diffopts;
780 struct commit_list *parents;
781 struct combine_diff_path *p, *paths = NULL;
782 int num_parent, i, num_paths;
784 diff_setup(&diffopts);
785 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
786 diffopts.recursive = 1;
789 for (parents = commit->parents, num_parent = 0;
791 parents = parents->next, num_parent++)
794 /* find set of paths that everybody touches */
795 for (parents = commit->parents, i = 0;
797 parents = parents->next, i++) {
798 struct commit *parent = parents->item;
799 diff_tree_sha1(parent->object.sha1, commit->object.sha1, "",
801 paths = intersect_paths(paths, i, num_parent);
802 diff_flush(&diffopts);
805 /* find out surviving paths */
806 for (num_paths = 0, p = paths; p; p = p->next) {
811 for (p = paths; p; p = p->next) {
814 if (show_combined_diff(p, num_parent, dense, header))
819 /* Clean things up */
821 struct combine_diff_path *tmp = paths;