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;
42 p->parent[n].status = q->queue[i]->status;
49 for (p = curr; p; p = p->next) {
53 for (i = 0; i < q->nr; i++) {
57 if (uninteresting(q->queue[i]))
59 path = q->queue[i]->two->path;
61 if (len == p->len && !memcmp(path, p->path, len)) {
63 memcpy(p->parent[n].sha1,
64 q->queue[i]->one->sha1, 20);
65 p->parent[n].mode = q->queue[i]->one->mode;
66 p->parent[n].status = q->queue[i]->status;
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;
230 return; /* result deleted */
232 write_temp_blob(parent_tmp, parent);
233 sprintf(cmd, "diff --unified=0 -La/x -Lb/x '%s' '%s'",
235 in = popen(cmd, "r");
237 die("cannot spawn %s", cmd);
240 while (fgets(line, sizeof(line), in) != NULL) {
241 int len = strlen(line);
242 if (5 < len && !memcmp("@@ -", line, 4)) {
243 if (parse_hunk_header(line, len,
248 /* @@ -1,2 +0,0 @@ to remove the
253 /* @@ -X,Y +N,0 @@ removed Y lines
254 * that would have come *after* line N
255 * in the result. Our lost buckets hang
256 * to the line after the removed lines,
258 lost_bucket = &sline[nb];
260 lost_bucket = &sline[nb-1];
261 if (!sline[nb-1].p_lno)
264 sizeof(unsigned long));
265 sline[nb-1].p_lno[n] = ob;
269 continue; /* not in any hunk yet */
272 append_lost(lost_bucket, n, line+1);
275 sline[lno-1].flag |= nmask;
283 /* Assign line numbers for this parent.
285 * sline[lno].p_lno[n] records the first line number
286 * (counting from 1) for parent N if the final hunk display
287 * started by showing sline[lno] (possibly showing the lost
288 * lines attached to it first).
290 for (lno = 0, p_lno = 1; lno < cnt; lno++) {
292 sline[lno].p_lno[n] = p_lno;
294 /* How many lines would this sline advance the p_lno? */
295 ll = sline[lno].lost_head;
297 if (ll->parent_map & nmask)
298 p_lno++; /* '-' means parent had it */
301 if (!(sline[lno].flag & nmask))
302 p_lno++; /* no '+' means parent had it */
304 sline[lno].p_lno[n] = p_lno; /* trailer */
307 static unsigned long context = 3;
308 static char combine_marker = '@';
310 static int interesting(struct sline *sline, unsigned long all_mask)
312 /* If some parents lost lines here, or if we have added to
313 * some parent, it is interesting.
315 return ((sline->flag & all_mask) || sline->lost_head);
318 static unsigned long adjust_hunk_tail(struct sline *sline,
319 unsigned long all_mask,
320 unsigned long hunk_begin,
323 /* i points at the first uninteresting line. If the last line
324 * of the hunk was interesting only because it has some
325 * deletion, then it is not all that interesting for the
326 * purpose of giving trailing context lines. This is because
327 * we output '-' line and then unmodified sline[i-1] itself in
328 * that case which gives us one extra context line.
330 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
335 static unsigned long find_next(struct sline *sline,
341 /* We have examined up to i-1 and are about to look at i.
342 * Find next interesting or uninteresting line. Here,
343 * "interesting" does not mean interesting(), but marked by
344 * the give_context() function below (i.e. it includes context
345 * lines that are not interesting to interesting() function
346 * that are surrounded by interesting() ones.
350 ? !(sline[i].flag & mark)
351 : (sline[i].flag & mark))
358 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
360 unsigned long all_mask = (1UL<<num_parent) - 1;
361 unsigned long mark = (1UL<<num_parent);
364 /* Two groups of interesting lines may have a short gap of
365 * unintersting lines. Connect such groups to give them a
368 * We first start from what the interesting() function says,
369 * and mark them with "mark", and paint context lines with the
370 * mark. So interesting() would still say false for such context
371 * lines but they are treated as "interesting" in the end.
373 i = find_next(sline, mark, 0, cnt, 0);
378 unsigned long j = (context < i) ? (i - context) : 0;
381 /* Paint a few lines before the first interesting line. */
383 sline[j++].flag |= mark;
386 /* we know up to i is to be included. where does the
387 * next uninteresting one start?
389 j = find_next(sline, mark, i, cnt, 1);
391 break; /* the rest are all interesting */
393 /* lookahead context lines */
394 k = find_next(sline, mark, j, cnt, 0);
395 j = adjust_hunk_tail(sline, all_mask, i, j);
397 if (k < j + context) {
398 /* k is interesting and [j,k) are not, but
399 * paint them interesting because the gap is small.
402 sline[j++].flag |= mark;
407 /* j is the first uninteresting line and there is
408 * no overlap beyond it within context lines. Paint
409 * the trailing edge a bit.
412 k = (j + context < cnt) ? j + context : cnt;
414 sline[j++].flag |= mark;
419 static int make_hunks(struct sline *sline, unsigned long cnt,
420 int num_parent, int dense)
422 unsigned long all_mask = (1UL<<num_parent) - 1;
423 unsigned long mark = (1UL<<num_parent);
425 int has_interesting = 0;
427 for (i = 0; i < cnt; i++) {
428 if (interesting(&sline[i], all_mask))
429 sline[i].flag |= mark;
431 sline[i].flag &= ~mark;
434 return give_context(sline, cnt, num_parent);
436 /* Look at each hunk, and if we have changes from only one
437 * parent, or the changes are the same from all but one
438 * parent, mark that uninteresting.
442 unsigned long j, hunk_begin, hunk_end;
443 unsigned long same_diff;
444 while (i < cnt && !(sline[i].flag & mark))
447 break; /* No more interesting hunks */
449 for (j = i + 1; j < cnt; j++) {
450 if (!(sline[j].flag & mark)) {
451 /* Look beyond the end to see if there
452 * is an interesting line after this
453 * hunk within context span.
455 unsigned long la; /* lookahead */
457 la = adjust_hunk_tail(sline, all_mask,
459 la = (la + context < cnt) ?
460 (la + context) : cnt;
462 if (sline[la].flag & mark) {
474 /* [i..hunk_end) are interesting. Now is it really
475 * interesting? We check if there are only two versions
476 * and the result matches one of them. That is, we look
478 * (+) line, which records lines added to which parents;
479 * this line appears in the result.
480 * (-) line, which records from what parents the line
481 * was removed; this line does not appear in the result.
482 * then check the set of parents the result has difference
483 * from, from all lines. If there are lines that has
484 * different set of parents that the result has differences
485 * from, that means we have more than two versions.
487 * Even when we have only two versions, if the result does
488 * not match any of the parents, the it should be considered
489 * interesting. In such a case, we would have all '+' line.
490 * After passing the above "two versions" test, that would
491 * appear as "the same set of parents" to be "all parents".
495 for (j = i; j < hunk_end && !has_interesting; j++) {
496 unsigned long this_diff = sline[j].flag & all_mask;
497 struct lline *ll = sline[j].lost_head;
499 /* This has some changes. Is it the
503 same_diff = this_diff;
504 else if (same_diff != this_diff) {
509 while (ll && !has_interesting) {
510 /* Lost this line from these parents;
511 * who are they? Are they the same?
513 this_diff = ll->parent_map;
515 same_diff = this_diff;
516 else if (same_diff != this_diff) {
523 if (!has_interesting && same_diff != all_mask) {
524 /* This hunk is not that interesting after all */
525 for (j = hunk_begin; j < hunk_end; j++)
526 sline[j].flag &= ~mark;
531 has_interesting = give_context(sline, cnt, num_parent);
532 return has_interesting;
535 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, unsigned long cnt, int n)
537 l0 = sline[l0].p_lno[n];
538 l1 = sline[l1].p_lno[n];
539 printf(" -%lu,%lu", l0, l1-l0);
542 static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
544 unsigned long mark = (1UL<<num_parent);
546 unsigned long lno = 0;
549 return; /* result deleted */
552 struct sline *sl = &sline[lno];
554 while (lno < cnt && !(sline[lno].flag & mark))
558 for (hunk_end = lno + 1; hunk_end < cnt; hunk_end++)
559 if (!(sline[hunk_end].flag & mark))
561 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
562 for (i = 0; i < num_parent; i++)
563 show_parent_lno(sline, lno, hunk_end, cnt, i);
564 printf(" +%lu,%lu ", lno+1, hunk_end-lno);
565 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
567 while (lno < hunk_end) {
570 unsigned long p_mask;
574 for (j = 0; j < num_parent; j++) {
575 if (ll->parent_map & (1UL<<j))
584 for (j = 0; j < num_parent; j++) {
585 if (p_mask & sl->flag)
591 printf("%.*s\n", sl->len, sl->bol);
596 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
599 /* We have already examined parent j and we know parent i
600 * and parent j are the same, so reuse the combined result
601 * of parent j for parent i.
603 unsigned long lno, imask, jmask;
607 for (lno = 0; lno < cnt; lno++) {
608 struct lline *ll = sline->lost_head;
609 sline->p_lno[i] = sline->p_lno[j];
611 if (ll->parent_map & jmask)
612 ll->parent_map |= imask;
615 if (sline->flag & jmask)
616 sline->flag |= imask;
619 /* the overall size of the file (sline[cnt]) */
620 sline->p_lno[i] = sline->p_lno[j];
623 static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
624 int dense, const char *header)
626 unsigned long size, cnt, lno;
627 char *result, *cp, *ep;
628 struct sline *sline; /* survived lines */
629 int mode_differs = 0;
630 int i, show_hunks, shown_header = 0;
631 char ourtmp_buf[TMPPATHLEN];
632 char *ourtmp = ourtmp_buf;
633 int working_tree_file = !memcmp(elem->sha1, null_sha1, 20);
635 /* Read the result of merge first */
636 if (!working_tree_file) {
637 result = grab_blob(elem->sha1, &size);
638 write_to_temp_file(ourtmp, result, size);
641 /* Used by diff-tree to read from the working tree */
645 if (0 <= (fd = open(ourtmp, O_RDONLY)) &&
647 int len = st.st_size;
650 elem->mode = DIFF_FILE_CANON_MODE(st.st_mode);
652 result = xmalloc(len + 1);
654 int done = xread(fd, result+cnt, len-cnt);
658 die("read error '%s'", ourtmp);
669 ourtmp = "/dev/null";
675 for (cnt = 0, cp = result; cp - result < size; cp++) {
679 if (size && result[size-1] != '\n')
680 cnt++; /* incomplete line */
682 sline = xcalloc(cnt+1, sizeof(*sline));
684 sline[0].bol = result;
685 for (lno = 0; lno <= cnt; lno++) {
686 sline[lno].lost_tail = &sline[lno].lost_head;
689 for (lno = 0, cp = result; cp - result < size; cp++) {
691 sline[lno].len = cp - sline[lno].bol;
694 sline[lno].bol = cp + 1;
697 if (size && result[size-1] != '\n')
698 sline[cnt-1].len = size - (sline[cnt-1].bol - result);
700 sline[0].p_lno = xcalloc((cnt+1) * num_parent, sizeof(unsigned long));
701 for (lno = 0; lno < cnt; lno++)
702 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
704 for (i = 0; i < num_parent; i++) {
706 for (j = 0; j < i; j++) {
707 if (!memcmp(elem->parent[i].sha1,
708 elem->parent[j].sha1, 20)) {
709 reuse_combine_diff(sline, cnt, i, j);
714 combine_diff(elem->parent[i].sha1, ourtmp, sline,
716 if (elem->parent[i].mode != elem->mode)
720 show_hunks = make_hunks(sline, cnt, num_parent, dense);
722 if (show_hunks || mode_differs || working_tree_file) {
729 printf("diff --%s ", dense ? "cc" : "combined");
730 if (quote_c_style(elem->path, NULL, NULL, 0))
731 quote_c_style(elem->path, NULL, stdout, 0);
733 printf("%s", elem->path);
736 for (i = 0; i < num_parent; i++) {
737 abb = find_unique_abbrev(elem->parent[i].sha1,
739 printf("%s%s", i ? "," : "", abb);
741 abb = find_unique_abbrev(elem->sha1, DEFAULT_ABBREV);
742 printf("..%s\n", abb);
745 int added = !!elem->mode;
746 for (i = 0; added && i < num_parent; i++)
747 if (elem->parent[i].status !=
751 printf("new file mode %06o", elem->mode);
754 printf("deleted file ");
756 for (i = 0; i < num_parent; i++) {
757 printf("%s%06o", i ? "," : "",
758 elem->parent[i].mode);
761 printf("..%06o", elem->mode);
765 dump_sline(sline, cnt, num_parent);
767 if (ourtmp == ourtmp_buf)
771 for (i = 0; i < cnt; i++) {
772 if (sline[i].lost_head) {
773 struct lline *ll = sline[i].lost_head;
775 struct lline *tmp = ll;
781 free(sline[0].p_lno);
786 #define COLONS "::::::::::::::::::::::::::::::::"
788 static void show_raw_diff(struct combine_diff_path *p, int num_parent, const char *header, struct diff_options *opt)
790 int i, offset, mod_type = 'A';
792 int line_termination, inter_name_termination;
794 line_termination = opt->line_termination;
795 inter_name_termination = '\t';
796 if (!line_termination)
797 inter_name_termination = 0;
802 for (i = 0; i < num_parent; i++) {
803 if (p->parent[i].mode)
809 if (opt->output_format == DIFF_FORMAT_RAW) {
810 offset = strlen(COLONS) - num_parent;
813 prefix = COLONS + offset;
816 for (i = 0; i < num_parent; i++) {
817 printf("%s%06o", prefix, p->parent[i].mode);
820 printf("%s%06o", prefix, p->mode);
823 for (i = 0; i < num_parent; i++)
824 printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
826 printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
829 if (opt->output_format == DIFF_FORMAT_RAW ||
830 opt->output_format == DIFF_FORMAT_NAME_STATUS) {
831 for (i = 0; i < num_parent; i++)
832 putchar(p->parent[i].status);
833 putchar(inter_name_termination);
836 if (line_termination) {
837 if (quote_c_style(p->path, NULL, NULL, 0))
838 quote_c_style(p->path, NULL, stdout, 0);
840 printf("%s", p->path);
841 putchar(line_termination);
844 printf("%s%c", p->path, line_termination);
848 int show_combined_diff(struct combine_diff_path *p,
852 struct diff_options *opt)
856 switch (opt->output_format) {
857 case DIFF_FORMAT_RAW:
858 case DIFF_FORMAT_NAME_STATUS:
859 case DIFF_FORMAT_NAME:
860 show_raw_diff(p, num_parent, header, opt);
864 case DIFF_FORMAT_PATCH:
865 return show_patch_diff(p, num_parent, dense, header);
869 const char *diff_tree_combined_merge(const unsigned char *sha1,
870 const char *header, int dense,
871 struct diff_options *opt)
873 struct commit *commit = lookup_commit(sha1);
874 struct diff_options diffopts;
875 struct commit_list *parents;
876 struct combine_diff_path *p, *paths = NULL;
877 int num_parent, i, num_paths;
880 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
881 diffopts.recursive = 1;
884 for (parents = commit->parents, num_parent = 0;
886 parents = parents->next, num_parent++)
889 /* find set of paths that everybody touches */
890 for (parents = commit->parents, i = 0;
892 parents = parents->next, i++) {
893 struct commit *parent = parents->item;
894 diff_tree_sha1(parent->object.sha1, commit->object.sha1, "",
896 diffcore_std(&diffopts);
897 paths = intersect_paths(paths, i, num_parent);
898 diff_flush(&diffopts);
901 /* find out surviving paths */
902 for (num_paths = 0, p = paths; p; p = p->next) {
907 for (p = paths; p; p = p->next) {
908 if (show_combined_diff(p, num_parent, dense,
914 /* Clean things up */
916 struct combine_diff_path *tmp = paths;