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 does _not_
85 * have this line (i.e. we changed 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 unsigned long pmask = ~nmask;
223 struct sline *lost_bucket = NULL;
225 write_temp_blob(parent_tmp, parent);
226 sprintf(cmd, "diff --unified=0 -La/x -Lb/x '%s' '%s'",
228 in = popen(cmd, "r");
230 die("cannot spawn %s", cmd);
233 while (fgets(line, sizeof(line), in) != NULL) {
234 int len = strlen(line);
235 if (5 < len && !memcmp("@@ -", line, 4)) {
236 if (parse_hunk_header(line, len,
241 /* @@ -1,2 +0,0 @@ to remove the
246 /* @@ -X,Y +N,0 @@ removed Y lines
247 * that would have come *after* line N
248 * in the result. Our lost buckets hang
249 * to the line after the removed lines,
251 lost_bucket = &sline[nb];
253 lost_bucket = &sline[nb-1];
254 if (!sline[nb-1].p_lno)
257 sizeof(unsigned long));
258 sline[nb-1].p_lno[n] = ob;
262 continue; /* not in any hunk yet */
265 append_lost(lost_bucket, n, line+1);
268 sline[lno-1].flag &= pmask;
276 /* Assign line numbers for this parent.
278 * sline[lno].p_lno[n] records the first line number
279 * (counting from 1) for parent N if the final hunk display
280 * started by showing sline[lno] (possibly showing the lost
281 * lines attached to it first).
283 for (lno = 0, p_lno = 1; lno < cnt; lno++) {
285 sline[lno].p_lno[n] = p_lno;
287 /* How many lines would this sline advance the p_lno? */
288 ll = sline[lno].lost_head;
290 if (ll->parent_map & nmask)
291 p_lno++; /* '-' means parent had it */
294 if (sline[lno].flag & nmask)
295 p_lno++; /* no '+' means parent had it */
297 sline[lno].p_lno[n] = p_lno; /* trailer */
300 static unsigned long context = 3;
301 static char combine_marker = '@';
303 static int interesting(struct sline *sline, unsigned long all_mask)
305 return ((sline->flag & all_mask) != all_mask || sline->lost_head);
308 static unsigned long adjust_hunk_tail(struct sline *sline,
309 unsigned long all_mask,
310 unsigned long hunk_begin,
313 /* i points at the first uninteresting line.
314 * If the last line of the hunk was interesting
315 * only because it has some deletion, then
316 * it is not all that interesting for the
317 * purpose of giving trailing context lines.
319 if ((hunk_begin + 1 <= i) &&
320 ((sline[i-1].flag & all_mask) == all_mask))
325 static unsigned long next_interesting(struct sline *sline,
333 !(sline[i].flag & mark) :
334 (sline[i].flag & mark))
341 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
343 unsigned long all_mask = (1UL<<num_parent) - 1;
344 unsigned long mark = (1UL<<num_parent);
347 i = next_interesting(sline, mark, 0, cnt, 0);
352 unsigned long j = (context < i) ? (i - context) : 0;
355 sline[j++].flag |= mark;
358 j = next_interesting(sline, mark, i, cnt, 1);
360 break; /* the rest are all interesting */
362 /* lookahead context lines */
363 k = next_interesting(sline, mark, j, cnt, 0);
364 j = adjust_hunk_tail(sline, all_mask, i, j);
366 if (k < j + context) {
367 /* k is interesting and [j,k) are not, but
368 * paint them interesting because the gap is small.
371 sline[j++].flag |= mark;
376 /* j is the first uninteresting line and there is
377 * no overlap beyond it within context lines.
380 k = (j + context < cnt) ? j + context : cnt;
382 sline[j++].flag |= mark;
387 static int make_hunks(struct sline *sline, unsigned long cnt,
388 int num_parent, int dense)
390 unsigned long all_mask = (1UL<<num_parent) - 1;
391 unsigned long mark = (1UL<<num_parent);
393 int has_interesting = 0;
395 for (i = 0; i < cnt; i++) {
396 if (interesting(&sline[i], all_mask))
397 sline[i].flag |= mark;
399 sline[i].flag &= ~mark;
402 return give_context(sline, cnt, num_parent);
404 /* Look at each hunk, and if we have changes from only one
405 * parent, or the changes are the same from all but one
406 * parent, mark that uninteresting.
410 unsigned long j, hunk_begin, hunk_end;
411 unsigned long same_diff;
412 while (i < cnt && !(sline[i].flag & mark))
415 break; /* No more interesting hunks */
417 for (j = i + 1; j < cnt; j++) {
418 if (!(sline[j].flag & mark)) {
419 /* Look beyond the end to see if there
420 * is an interesting line after this
421 * hunk within context span.
423 unsigned long la; /* lookahead */
425 la = adjust_hunk_tail(sline, all_mask,
427 la = (la + context < cnt) ?
428 (la + context) : cnt;
430 if (sline[la].flag & mark) {
442 /* [i..hunk_end) are interesting. Now is it really
443 * interesting? We check if there are only two versions
444 * and the result matches one of them. That is, we look
446 * (+) line, which records lines added to which parents;
447 * this line appears in the result.
448 * (-) line, which records from what parents the line
449 * was removed; this line does not appear in the result.
450 * then check the set of parents the result has difference
451 * from, from all lines. If there are lines that has
452 * different set of parents that the result has differences
453 * from, that means we have more than two versions.
455 * Even when we have only two versions, if the result does
456 * not match any of the parents, the it should be considered
457 * interesting. In such a case, we would have all '+' line.
458 * After passing the above "two versions" test, that would
459 * appear as "the same set of parents" to be "all parents".
463 for (j = i; j < hunk_end && !has_interesting; j++) {
464 unsigned long this_diff = ~sline[j].flag & all_mask;
465 struct lline *ll = sline[j].lost_head;
467 /* This has some changes. Is it the
471 same_diff = this_diff;
472 else if (same_diff != this_diff) {
477 while (ll && !has_interesting) {
478 /* Lost this line from these parents;
479 * who are they? Are they the same?
481 this_diff = ll->parent_map;
483 same_diff = this_diff;
484 else if (same_diff != this_diff) {
491 if (!has_interesting && same_diff != all_mask) {
492 /* This hunk is not that interesting after all */
493 for (j = hunk_begin; j < hunk_end; j++)
494 sline[j].flag &= ~mark;
499 has_interesting = give_context(sline, cnt, num_parent);
500 return has_interesting;
503 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, unsigned long cnt, int n)
505 l0 = sline[l0].p_lno[n];
506 l1 = sline[l1].p_lno[n];
507 printf("-%lu,%lu ", l0, l1-l0);
510 static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
512 unsigned long mark = (1UL<<num_parent);
514 unsigned long lno = 0;
517 struct sline *sl = &sline[lno];
519 while (lno < cnt && !(sline[lno].flag & mark))
523 for (hunk_end = lno + 1; hunk_end < cnt; hunk_end++)
524 if (!(sline[hunk_end].flag & mark))
526 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
527 printf(" +%lu,%lu ", lno+1, hunk_end-lno);
528 for (i = 0; i < num_parent; i++)
529 show_parent_lno(sline, lno, hunk_end, cnt, i);
530 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
532 while (lno < hunk_end) {
538 for (j = 0; j < num_parent; j++) {
539 if (ll->parent_map & (1UL<<j))
547 for (j = 0; j < num_parent; j++) {
548 if ((1UL<<j) & sl->flag)
553 printf("%.*s\n", sl->len, sl->bol);
558 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
561 /* We have already examined parent j and we know parent i
562 * and parent j are the same, so reuse the combined result
563 * of parent j for parent i.
565 unsigned long lno, imask, jmask;
569 for (lno = 0; lno < cnt; lno++) {
570 struct lline *ll = sline->lost_head;
571 sline->p_lno[i] = sline->p_lno[j];
573 if (ll->parent_map & jmask)
574 ll->parent_map |= imask;
577 if (!(sline->flag & jmask))
578 sline->flag &= ~imask;
583 int show_combined_diff(struct combine_diff_path *elem, int num_parent,
584 int dense, const char *header, int show_empty)
586 unsigned long size, cnt, lno;
587 char *result, *cp, *ep;
588 struct sline *sline; /* survived lines */
589 int i, show_hunks, shown_header = 0;
590 char ourtmp_buf[TMPPATHLEN];
591 char *ourtmp = ourtmp_buf;
593 /* Read the result of merge first */
594 if (memcmp(elem->sha1, null_sha1, 20)) {
595 result = grab_blob(elem->sha1, &size);
596 write_to_temp_file(ourtmp, result, size);
602 if (0 <= (fd = open(ourtmp, O_RDONLY)) &&
604 int len = st.st_size;
608 result = xmalloc(len + 1);
610 int done = xread(fd, result+cnt, len-cnt);
614 die("read error '%s'", ourtmp);
624 ourtmp = "/dev/null";
630 for (cnt = 0, cp = result; cp - result < size; cp++) {
634 if (result[size-1] != '\n')
635 cnt++; /* incomplete line */
637 sline = xcalloc(cnt+1, sizeof(*sline));
639 sline[0].bol = result;
640 for (lno = 0, cp = result; cp - result < size; cp++) {
642 sline[lno].lost_tail = &sline[lno].lost_head;
643 sline[lno].len = cp - sline[lno].bol;
644 sline[lno].flag = (1UL<<num_parent) - 1;
647 sline[lno].bol = cp + 1;
650 if (result[size-1] != '\n') {
651 sline[cnt-1].lost_tail = &sline[cnt-1].lost_head;
652 sline[cnt-1].len = size - (sline[cnt-1].bol - result);
653 sline[cnt-1].flag = (1UL<<num_parent) - 1;
656 sline[0].p_lno = xcalloc((cnt+1) * num_parent, sizeof(unsigned long));
657 for (lno = 0; lno < cnt; lno++)
658 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
660 for (i = 0; i < num_parent; i++) {
662 for (j = 0; j < i; j++) {
663 if (!memcmp(elem->parent_sha1[i],
664 elem->parent_sha1[j], 20)) {
665 reuse_combine_diff(sline, cnt, i, j);
670 combine_diff(elem->parent_sha1[i], ourtmp, sline,
674 show_hunks = make_hunks(sline, cnt, num_parent, dense);
676 if (header && (show_hunks || show_empty)) {
681 printf("diff --%s ", dense ? "cc" : "combined");
682 if (quote_c_style(elem->path, NULL, NULL, 0))
683 quote_c_style(elem->path, NULL, stdout, 0);
685 printf("%s", elem->path);
688 for (i = 0; i < num_parent; i++) {
691 find_unique_abbrev(elem->parent_sha1[i],
695 find_unique_abbrev(elem->sha1, DEFAULT_ABBREV));
696 dump_sline(sline, cnt, num_parent);
698 if (ourtmp == ourtmp_buf)
702 for (i = 0; i < cnt; i++) {
703 if (sline[i].lost_head) {
704 struct lline *ll = sline[i].lost_head;
706 struct lline *tmp = ll;
716 int diff_tree_combined_merge(const unsigned char *sha1,
718 int show_empty_merge, int dense)
720 struct commit *commit = lookup_commit(sha1);
721 struct diff_options diffopts;
722 struct commit_list *parents;
723 struct combine_diff_path *p, *paths = NULL;
724 int num_parent, i, num_paths;
726 diff_setup(&diffopts);
727 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
728 diffopts.recursive = 1;
731 for (parents = commit->parents, num_parent = 0;
733 parents = parents->next, num_parent++)
736 /* find set of paths that everybody touches */
737 for (parents = commit->parents, i = 0;
739 parents = parents->next, i++) {
740 struct commit *parent = parents->item;
741 diff_tree_sha1(parent->object.sha1, commit->object.sha1, "",
743 paths = intersect_paths(paths, i, num_parent);
744 diff_flush(&diffopts);
747 /* find out surviving paths */
748 for (num_paths = 0, p = paths; p; p = p->next) {
752 if (num_paths || show_empty_merge) {
753 for (p = paths; p; p = p->next) {
756 if (show_combined_diff(p, num_parent, dense, header,
762 /* Clean things up */
764 struct combine_diff_path *tmp = paths;