Merge branch 'nh/http' into next
[git/gitweb.git] / combine-diff.c
blob11f5143eeceb62e75ba993460e35a378a65a4831
1 #include "cache.h"
2 #include "commit.h"
3 #include "blob.h"
4 #include "diff.h"
5 #include "diffcore.h"
6 #include "quote.h"
7 #include "xdiff/xdiff.h"
9 static int uninteresting(struct diff_filepair *p)
11 if (diff_unmodified_pair(p))
12 return 1;
13 return 0;
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;
20 int i;
22 if (!n) {
23 struct combine_diff_path *list = NULL, **tail = &list;
24 for (i = 0; i < q->nr; i++) {
25 int len;
26 const char *path;
27 if (uninteresting(q->queue[i]))
28 continue;
29 path = q->queue[i]->two->path;
30 len = strlen(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);
34 p->path[len] = 0;
35 p->len = len;
36 p->next = NULL;
37 memset(p->parent, 0,
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;
44 p->parent[n].status = q->queue[i]->status;
45 *tail = p;
46 tail = &p->next;
48 return list;
51 for (p = curr; p; p = p->next) {
52 int found = 0;
53 if (!p->len)
54 continue;
55 for (i = 0; i < q->nr; i++) {
56 const char *path;
57 int len;
59 if (uninteresting(q->queue[i]))
60 continue;
61 path = q->queue[i]->two->path;
62 len = strlen(path);
63 if (len == p->len && !memcmp(path, p->path, len)) {
64 found = 1;
65 memcpy(p->parent[n].sha1,
66 q->queue[i]->one->sha1, 20);
67 p->parent[n].mode = q->queue[i]->one->mode;
68 p->parent[n].status = q->queue[i]->status;
69 break;
72 if (!found)
73 p->len = 0;
75 return curr;
78 /* Lines lost from parent */
79 struct lline {
80 struct lline *next;
81 int len;
82 unsigned long parent_map;
83 char line[FLEX_ARRAY];
86 /* Lines surviving in the merge result */
87 struct sline {
88 struct lline *lost_head, **lost_tail;
89 char *bol;
90 int len;
91 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
92 * we did not change it).
93 * bit N is used for "interesting" lines, including context.
95 unsigned long flag;
96 unsigned long *p_lno;
99 static char *grab_blob(const unsigned char *sha1, unsigned long *size)
101 char *blob;
102 char type[20];
103 if (!memcmp(sha1, null_sha1, 20)) {
104 /* deleted blob */
105 *size = 0;
106 return xcalloc(1, 1);
108 blob = read_sha1_file(sha1, type, size);
109 if (strcmp(type, blob_type))
110 die("object '%s' is not a blob!", sha1_to_hex(sha1));
111 return blob;
114 static int parse_num(char **cp_p, unsigned int *num_p)
116 char *cp = *cp_p;
117 unsigned int num = 0;
118 int read_some;
120 while ('0' <= *cp && *cp <= '9')
121 num = num * 10 + *cp++ - '0';
122 if (!(read_some = cp - *cp_p))
123 return -1;
124 *cp_p = cp;
125 *num_p = num;
126 return 0;
129 static int parse_hunk_header(char *line, int len,
130 unsigned int *ob, unsigned int *on,
131 unsigned int *nb, unsigned int *nn)
133 char *cp;
134 cp = line + 4;
135 if (parse_num(&cp, ob)) {
136 bad_line:
137 return error("malformed diff output: %s", line);
139 if (*cp == ',') {
140 cp++;
141 if (parse_num(&cp, on))
142 goto bad_line;
144 else
145 *on = 1;
146 if (*cp++ != ' ' || *cp++ != '+')
147 goto bad_line;
148 if (parse_num(&cp, nb))
149 goto bad_line;
150 if (*cp == ',') {
151 cp++;
152 if (parse_num(&cp, nn))
153 goto bad_line;
155 else
156 *nn = 1;
157 return -!!memcmp(cp, " @@", 3);
160 static void append_lost(struct sline *sline, int n, const char *line, int len)
162 struct lline *lline;
163 unsigned long this_mask = (1UL<<n);
164 if (line[len-1] == '\n')
165 len--;
167 /* Check to see if we can squash things */
168 if (sline->lost_head) {
169 struct lline *last_one = NULL;
170 /* We cannot squash it with earlier one */
171 for (lline = sline->lost_head;
172 lline;
173 lline = lline->next)
174 if (lline->parent_map & this_mask)
175 last_one = lline;
176 lline = last_one ? last_one->next : sline->lost_head;
177 while (lline) {
178 if (lline->len == len &&
179 !memcmp(lline->line, line, len)) {
180 lline->parent_map |= this_mask;
181 return;
183 lline = lline->next;
187 lline = xmalloc(sizeof(*lline) + len + 1);
188 lline->len = len;
189 lline->next = NULL;
190 lline->parent_map = this_mask;
191 memcpy(lline->line, line, len);
192 lline->line[len] = 0;
193 *sline->lost_tail = lline;
194 sline->lost_tail = &lline->next;
197 struct combine_diff_state {
198 char *remainder;
199 unsigned long remainder_size;
200 unsigned int lno, ob, on, nb, nn;
201 unsigned long nmask;
202 int num_parent;
203 int n;
204 struct sline *sline;
205 struct sline *lost_bucket;
208 static void consume_line(struct combine_diff_state *state,
209 char *line,
210 unsigned long len)
212 if (5 < len && !memcmp("@@ -", line, 4)) {
213 if (parse_hunk_header(line, len,
214 &state->ob, &state->on,
215 &state->nb, &state->nn))
216 return;
217 state->lno = state->nb;
218 if (!state->nb)
219 /* @@ -1,2 +0,0 @@ to remove the
220 * first two lines...
222 state->nb = 1;
223 if (state->nn == 0)
224 /* @@ -X,Y +N,0 @@ removed Y lines
225 * that would have come *after* line N
226 * in the result. Our lost buckets hang
227 * to the line after the removed lines,
229 state->lost_bucket = &state->sline[state->nb];
230 else
231 state->lost_bucket = &state->sline[state->nb-1];
232 if (!state->sline[state->nb-1].p_lno)
233 state->sline[state->nb-1].p_lno =
234 xcalloc(state->num_parent,
235 sizeof(unsigned long));
236 state->sline[state->nb-1].p_lno[state->n] = state->ob;
237 return;
239 if (!state->lost_bucket)
240 return; /* not in any hunk yet */
241 switch (line[0]) {
242 case '-':
243 append_lost(state->lost_bucket, state->n, line+1, len-1);
244 break;
245 case '+':
246 state->sline[state->lno-1].flag |= state->nmask;
247 state->lno++;
248 break;
252 static int combine_diff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
254 struct combine_diff_state *priv = priv_;
255 int i;
256 for (i = 0; i < nbuf; i++) {
257 if (mb[i].ptr[mb[i].size-1] != '\n') {
258 /* Incomplete line */
259 priv->remainder = realloc(priv->remainder,
260 priv->remainder_size +
261 mb[i].size);
262 memcpy(priv->remainder + priv->remainder_size,
263 mb[i].ptr, mb[i].size);
264 priv->remainder_size += mb[i].size;
265 continue;
268 /* we have a complete line */
269 if (!priv->remainder) {
270 consume_line(priv, mb[i].ptr, mb[i].size);
271 continue;
273 priv->remainder = realloc(priv->remainder,
274 priv->remainder_size +
275 mb[i].size);
276 memcpy(priv->remainder + priv->remainder_size,
277 mb[i].ptr, mb[i].size);
278 consume_line(priv, priv->remainder,
279 priv->remainder_size + mb[i].size);
280 free(priv->remainder);
281 priv->remainder = NULL;
282 priv->remainder_size = 0;
284 return 0;
287 static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
288 struct sline *sline, int cnt, int n, int num_parent)
290 unsigned int p_lno, lno;
291 unsigned long nmask = (1UL << n);
292 xpparam_t xpp;
293 xdemitconf_t xecfg;
294 mmfile_t parent_file;
295 xdemitcb_t ecb;
296 struct combine_diff_state state;
297 unsigned long sz;
299 if (!cnt)
300 return; /* result deleted */
302 parent_file.ptr = grab_blob(parent, &sz);
303 parent_file.size = sz;
304 xpp.flags = XDF_NEED_MINIMAL;
305 xecfg.ctxlen = 0;
306 xecfg.flags = 0;
307 ecb.outf = combine_diff_outf;
308 ecb.priv = &state;
309 memset(&state, 0, sizeof(state));
310 state.nmask = nmask;
311 state.sline = sline;
312 state.lno = 1;
313 state.num_parent = num_parent;
314 state.n = n;
316 xdl_diff(&parent_file, result_file, &xpp, &xecfg, &ecb);
317 if (state.remainder && state.remainder_size)
318 consume_line(&state, state.remainder, state.remainder_size);
319 free(state.remainder);
320 free(parent_file.ptr);
322 /* Assign line numbers for this parent.
324 * sline[lno].p_lno[n] records the first line number
325 * (counting from 1) for parent N if the final hunk display
326 * started by showing sline[lno] (possibly showing the lost
327 * lines attached to it first).
329 for (lno = 0, p_lno = 1; lno < cnt; lno++) {
330 struct lline *ll;
331 sline[lno].p_lno[n] = p_lno;
333 /* How many lines would this sline advance the p_lno? */
334 ll = sline[lno].lost_head;
335 while (ll) {
336 if (ll->parent_map & nmask)
337 p_lno++; /* '-' means parent had it */
338 ll = ll->next;
340 if (!(sline[lno].flag & nmask))
341 p_lno++; /* no '+' means parent had it */
343 sline[lno].p_lno[n] = p_lno; /* trailer */
346 static unsigned long context = 3;
347 static char combine_marker = '@';
349 static int interesting(struct sline *sline, unsigned long all_mask)
351 /* If some parents lost lines here, or if we have added to
352 * some parent, it is interesting.
354 return ((sline->flag & all_mask) || sline->lost_head);
357 static unsigned long adjust_hunk_tail(struct sline *sline,
358 unsigned long all_mask,
359 unsigned long hunk_begin,
360 unsigned long i)
362 /* i points at the first uninteresting line. If the last line
363 * of the hunk was interesting only because it has some
364 * deletion, then it is not all that interesting for the
365 * purpose of giving trailing context lines. This is because
366 * we output '-' line and then unmodified sline[i-1] itself in
367 * that case which gives us one extra context line.
369 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
370 i--;
371 return i;
374 static unsigned long find_next(struct sline *sline,
375 unsigned long mark,
376 unsigned long i,
377 unsigned long cnt,
378 int uninteresting)
380 /* We have examined up to i-1 and are about to look at i.
381 * Find next interesting or uninteresting line. Here,
382 * "interesting" does not mean interesting(), but marked by
383 * the give_context() function below (i.e. it includes context
384 * lines that are not interesting to interesting() function
385 * that are surrounded by interesting() ones.
387 while (i < cnt)
388 if (uninteresting
389 ? !(sline[i].flag & mark)
390 : (sline[i].flag & mark))
391 return i;
392 else
393 i++;
394 return cnt;
397 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
399 unsigned long all_mask = (1UL<<num_parent) - 1;
400 unsigned long mark = (1UL<<num_parent);
401 unsigned long i;
403 /* Two groups of interesting lines may have a short gap of
404 * unintersting lines. Connect such groups to give them a
405 * bit of context.
407 * We first start from what the interesting() function says,
408 * and mark them with "mark", and paint context lines with the
409 * mark. So interesting() would still say false for such context
410 * lines but they are treated as "interesting" in the end.
412 i = find_next(sline, mark, 0, cnt, 0);
413 if (cnt <= i)
414 return 0;
416 while (i < cnt) {
417 unsigned long j = (context < i) ? (i - context) : 0;
418 unsigned long k;
420 /* Paint a few lines before the first interesting line. */
421 while (j < i)
422 sline[j++].flag |= mark;
424 again:
425 /* we know up to i is to be included. where does the
426 * next uninteresting one start?
428 j = find_next(sline, mark, i, cnt, 1);
429 if (cnt <= j)
430 break; /* the rest are all interesting */
432 /* lookahead context lines */
433 k = find_next(sline, mark, j, cnt, 0);
434 j = adjust_hunk_tail(sline, all_mask, i, j);
436 if (k < j + context) {
437 /* k is interesting and [j,k) are not, but
438 * paint them interesting because the gap is small.
440 while (j < k)
441 sline[j++].flag |= mark;
442 i = k;
443 goto again;
446 /* j is the first uninteresting line and there is
447 * no overlap beyond it within context lines. Paint
448 * the trailing edge a bit.
450 i = k;
451 k = (j + context < cnt) ? j + context : cnt;
452 while (j < k)
453 sline[j++].flag |= mark;
455 return 1;
458 static int make_hunks(struct sline *sline, unsigned long cnt,
459 int num_parent, int dense)
461 unsigned long all_mask = (1UL<<num_parent) - 1;
462 unsigned long mark = (1UL<<num_parent);
463 unsigned long i;
464 int has_interesting = 0;
466 for (i = 0; i < cnt; i++) {
467 if (interesting(&sline[i], all_mask))
468 sline[i].flag |= mark;
469 else
470 sline[i].flag &= ~mark;
472 if (!dense)
473 return give_context(sline, cnt, num_parent);
475 /* Look at each hunk, and if we have changes from only one
476 * parent, or the changes are the same from all but one
477 * parent, mark that uninteresting.
479 i = 0;
480 while (i < cnt) {
481 unsigned long j, hunk_begin, hunk_end;
482 unsigned long same_diff;
483 while (i < cnt && !(sline[i].flag & mark))
484 i++;
485 if (cnt <= i)
486 break; /* No more interesting hunks */
487 hunk_begin = i;
488 for (j = i + 1; j < cnt; j++) {
489 if (!(sline[j].flag & mark)) {
490 /* Look beyond the end to see if there
491 * is an interesting line after this
492 * hunk within context span.
494 unsigned long la; /* lookahead */
495 int contin = 0;
496 la = adjust_hunk_tail(sline, all_mask,
497 hunk_begin, j);
498 la = (la + context < cnt) ?
499 (la + context) : cnt;
500 while (j <= --la) {
501 if (sline[la].flag & mark) {
502 contin = 1;
503 break;
506 if (!contin)
507 break;
508 j = la;
511 hunk_end = j;
513 /* [i..hunk_end) are interesting. Now is it really
514 * interesting? We check if there are only two versions
515 * and the result matches one of them. That is, we look
516 * at:
517 * (+) line, which records lines added to which parents;
518 * this line appears in the result.
519 * (-) line, which records from what parents the line
520 * was removed; this line does not appear in the result.
521 * then check the set of parents the result has difference
522 * from, from all lines. If there are lines that has
523 * different set of parents that the result has differences
524 * from, that means we have more than two versions.
526 * Even when we have only two versions, if the result does
527 * not match any of the parents, the it should be considered
528 * interesting. In such a case, we would have all '+' line.
529 * After passing the above "two versions" test, that would
530 * appear as "the same set of parents" to be "all parents".
532 same_diff = 0;
533 has_interesting = 0;
534 for (j = i; j < hunk_end && !has_interesting; j++) {
535 unsigned long this_diff = sline[j].flag & all_mask;
536 struct lline *ll = sline[j].lost_head;
537 if (this_diff) {
538 /* This has some changes. Is it the
539 * same as others?
541 if (!same_diff)
542 same_diff = this_diff;
543 else if (same_diff != this_diff) {
544 has_interesting = 1;
545 break;
548 while (ll && !has_interesting) {
549 /* Lost this line from these parents;
550 * who are they? Are they the same?
552 this_diff = ll->parent_map;
553 if (!same_diff)
554 same_diff = this_diff;
555 else if (same_diff != this_diff) {
556 has_interesting = 1;
558 ll = ll->next;
562 if (!has_interesting && same_diff != all_mask) {
563 /* This hunk is not that interesting after all */
564 for (j = hunk_begin; j < hunk_end; j++)
565 sline[j].flag &= ~mark;
567 i = hunk_end;
570 has_interesting = give_context(sline, cnt, num_parent);
571 return has_interesting;
574 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, unsigned long cnt, int n)
576 l0 = sline[l0].p_lno[n];
577 l1 = sline[l1].p_lno[n];
578 printf(" -%lu,%lu", l0, l1-l0);
581 static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
583 unsigned long mark = (1UL<<num_parent);
584 int i;
585 unsigned long lno = 0;
587 if (!cnt)
588 return; /* result deleted */
590 while (1) {
591 struct sline *sl = &sline[lno];
592 int hunk_end;
593 while (lno < cnt && !(sline[lno].flag & mark))
594 lno++;
595 if (cnt <= lno)
596 break;
597 for (hunk_end = lno + 1; hunk_end < cnt; hunk_end++)
598 if (!(sline[hunk_end].flag & mark))
599 break;
600 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
601 for (i = 0; i < num_parent; i++)
602 show_parent_lno(sline, lno, hunk_end, cnt, i);
603 printf(" +%lu,%lu ", lno+1, hunk_end-lno);
604 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
605 putchar('\n');
606 while (lno < hunk_end) {
607 struct lline *ll;
608 int j;
609 unsigned long p_mask;
610 sl = &sline[lno++];
611 ll = sl->lost_head;
612 while (ll) {
613 for (j = 0; j < num_parent; j++) {
614 if (ll->parent_map & (1UL<<j))
615 putchar('-');
616 else
617 putchar(' ');
619 puts(ll->line);
620 ll = ll->next;
622 p_mask = 1;
623 for (j = 0; j < num_parent; j++) {
624 if (p_mask & sl->flag)
625 putchar('+');
626 else
627 putchar(' ');
628 p_mask <<= 1;
630 printf("%.*s\n", sl->len, sl->bol);
635 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
636 int i, int j)
638 /* We have already examined parent j and we know parent i
639 * and parent j are the same, so reuse the combined result
640 * of parent j for parent i.
642 unsigned long lno, imask, jmask;
643 imask = (1UL<<i);
644 jmask = (1UL<<j);
646 for (lno = 0; lno < cnt; lno++) {
647 struct lline *ll = sline->lost_head;
648 sline->p_lno[i] = sline->p_lno[j];
649 while (ll) {
650 if (ll->parent_map & jmask)
651 ll->parent_map |= imask;
652 ll = ll->next;
654 if (sline->flag & jmask)
655 sline->flag |= imask;
656 sline++;
658 /* the overall size of the file (sline[cnt]) */
659 sline->p_lno[i] = sline->p_lno[j];
662 static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
663 int dense, const char *header,
664 struct diff_options *opt)
666 unsigned long result_size, cnt, lno;
667 char *result, *cp, *ep;
668 struct sline *sline; /* survived lines */
669 int mode_differs = 0;
670 int i, show_hunks, shown_header = 0;
671 int working_tree_file = !memcmp(elem->sha1, null_sha1, 20);
672 int abbrev = opt->full_index ? 40 : DEFAULT_ABBREV;
673 mmfile_t result_file;
675 /* Read the result of merge first */
676 if (!working_tree_file)
677 result = grab_blob(elem->sha1, &result_size);
678 else {
679 /* Used by diff-tree to read from the working tree */
680 struct stat st;
681 int fd;
682 if (0 <= (fd = open(elem->path, O_RDONLY)) &&
683 !fstat(fd, &st)) {
684 int len = st.st_size;
685 int cnt = 0;
687 elem->mode = canon_mode(st.st_mode);
688 result_size = len;
689 result = xmalloc(len + 1);
690 while (cnt < len) {
691 int done = xread(fd, result+cnt, len-cnt);
692 if (done == 0)
693 break;
694 if (done < 0)
695 die("read error '%s'", elem->path);
696 cnt += done;
698 result[len] = 0;
700 else {
701 /* deleted file */
702 result_size = 0;
703 elem->mode = 0;
704 result = xmalloc(1);
705 result[0] = 0;
707 if (0 <= fd)
708 close(fd);
711 for (cnt = 0, cp = result; cp - result < result_size; cp++) {
712 if (*cp == '\n')
713 cnt++;
715 if (result_size && result[result_size-1] != '\n')
716 cnt++; /* incomplete line */
718 sline = xcalloc(cnt+1, sizeof(*sline));
719 ep = result;
720 sline[0].bol = result;
721 for (lno = 0; lno <= cnt; lno++) {
722 sline[lno].lost_tail = &sline[lno].lost_head;
723 sline[lno].flag = 0;
725 for (lno = 0, cp = result; cp - result < result_size; cp++) {
726 if (*cp == '\n') {
727 sline[lno].len = cp - sline[lno].bol;
728 lno++;
729 if (lno < cnt)
730 sline[lno].bol = cp + 1;
733 if (result_size && result[result_size-1] != '\n')
734 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
736 result_file.ptr = result;
737 result_file.size = result_size;
739 sline[0].p_lno = xcalloc((cnt+1) * num_parent, sizeof(unsigned long));
740 for (lno = 0; lno < cnt; lno++)
741 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
743 for (i = 0; i < num_parent; i++) {
744 int j;
745 for (j = 0; j < i; j++) {
746 if (!memcmp(elem->parent[i].sha1,
747 elem->parent[j].sha1, 20)) {
748 reuse_combine_diff(sline, cnt, i, j);
749 break;
752 if (i <= j)
753 combine_diff(elem->parent[i].sha1, &result_file, sline,
754 cnt, i, num_parent);
755 if (elem->parent[i].mode != elem->mode)
756 mode_differs = 1;
759 show_hunks = make_hunks(sline, cnt, num_parent, dense);
761 if (show_hunks || mode_differs || working_tree_file) {
762 const char *abb;
764 if (header) {
765 shown_header++;
766 printf("%s%c", header, opt->line_termination);
768 printf("diff --%s ", dense ? "cc" : "combined");
769 if (quote_c_style(elem->path, NULL, NULL, 0))
770 quote_c_style(elem->path, NULL, stdout, 0);
771 else
772 printf("%s", elem->path);
773 putchar('\n');
774 printf("index ");
775 for (i = 0; i < num_parent; i++) {
776 abb = find_unique_abbrev(elem->parent[i].sha1,
777 abbrev);
778 printf("%s%s", i ? "," : "", abb);
780 abb = find_unique_abbrev(elem->sha1, abbrev);
781 printf("..%s\n", abb);
783 if (mode_differs) {
784 int added = !!elem->mode;
785 for (i = 0; added && i < num_parent; i++)
786 if (elem->parent[i].status !=
787 DIFF_STATUS_ADDED)
788 added = 0;
789 if (added)
790 printf("new file mode %06o", elem->mode);
791 else {
792 if (!elem->mode)
793 printf("deleted file ");
794 printf("mode ");
795 for (i = 0; i < num_parent; i++) {
796 printf("%s%06o", i ? "," : "",
797 elem->parent[i].mode);
799 if (elem->mode)
800 printf("..%06o", elem->mode);
802 putchar('\n');
804 dump_sline(sline, cnt, num_parent);
806 free(result);
808 for (i = 0; i < cnt; i++) {
809 if (sline[i].lost_head) {
810 struct lline *ll = sline[i].lost_head;
811 while (ll) {
812 struct lline *tmp = ll;
813 ll = ll->next;
814 free(tmp);
818 free(sline[0].p_lno);
819 free(sline);
820 return shown_header;
823 #define COLONS "::::::::::::::::::::::::::::::::"
825 static void show_raw_diff(struct combine_diff_path *p, int num_parent, const char *header, struct diff_options *opt)
827 int i, offset, mod_type = 'A';
828 const char *prefix;
829 int line_termination, inter_name_termination;
831 line_termination = opt->line_termination;
832 inter_name_termination = '\t';
833 if (!line_termination)
834 inter_name_termination = 0;
836 if (header)
837 printf("%s%c", header, line_termination);
839 for (i = 0; i < num_parent; i++) {
840 if (p->parent[i].mode)
841 mod_type = 'M';
843 if (!p->mode)
844 mod_type = 'D';
846 if (opt->output_format == DIFF_FORMAT_RAW) {
847 offset = strlen(COLONS) - num_parent;
848 if (offset < 0)
849 offset = 0;
850 prefix = COLONS + offset;
852 /* Show the modes */
853 for (i = 0; i < num_parent; i++) {
854 printf("%s%06o", prefix, p->parent[i].mode);
855 prefix = " ";
857 printf("%s%06o", prefix, p->mode);
859 /* Show sha1's */
860 for (i = 0; i < num_parent; i++)
861 printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
862 opt->abbrev));
863 printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
866 if (opt->output_format == DIFF_FORMAT_RAW ||
867 opt->output_format == DIFF_FORMAT_NAME_STATUS) {
868 for (i = 0; i < num_parent; i++)
869 putchar(p->parent[i].status);
870 putchar(inter_name_termination);
873 if (line_termination) {
874 if (quote_c_style(p->path, NULL, NULL, 0))
875 quote_c_style(p->path, NULL, stdout, 0);
876 else
877 printf("%s", p->path);
878 putchar(line_termination);
880 else {
881 printf("%s%c", p->path, line_termination);
885 int show_combined_diff(struct combine_diff_path *p,
886 int num_parent,
887 int dense,
888 const char *header,
889 struct diff_options *opt)
891 if (!p->len)
892 return 0;
893 switch (opt->output_format) {
894 case DIFF_FORMAT_RAW:
895 case DIFF_FORMAT_NAME_STATUS:
896 case DIFF_FORMAT_NAME:
897 show_raw_diff(p, num_parent, header, opt);
898 return 1;
900 default:
901 case DIFF_FORMAT_PATCH:
902 return show_patch_diff(p, num_parent, dense, header, opt);
906 const char *diff_tree_combined_merge(const unsigned char *sha1,
907 const char *header, int dense,
908 struct diff_options *opt)
910 struct commit *commit = lookup_commit(sha1);
911 struct diff_options diffopts;
912 struct commit_list *parents;
913 struct combine_diff_path *p, *paths = NULL;
914 int num_parent, i, num_paths;
916 diffopts = *opt;
917 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
918 diffopts.recursive = 1;
920 /* count parents */
921 for (parents = commit->parents, num_parent = 0;
922 parents;
923 parents = parents->next, num_parent++)
924 ; /* nothing */
926 /* find set of paths that everybody touches */
927 for (parents = commit->parents, i = 0;
928 parents;
929 parents = parents->next, i++) {
930 struct commit *parent = parents->item;
931 diff_tree_sha1(parent->object.sha1, commit->object.sha1, "",
932 &diffopts);
933 diffcore_std(&diffopts);
934 paths = intersect_paths(paths, i, num_parent);
935 diff_flush(&diffopts);
938 /* find out surviving paths */
939 for (num_paths = 0, p = paths; p; p = p->next) {
940 if (p->len)
941 num_paths++;
943 if (num_paths) {
944 for (p = paths; p; p = p->next) {
945 if (show_combined_diff(p, num_parent, dense,
946 header, opt))
947 header = NULL;
951 /* Clean things up */
952 while (paths) {
953 struct combine_diff_path *tmp = paths;
954 paths = paths->next;
955 free(tmp);
957 return header;