Perl interface: make testsuite work again.
[git/repo.git] / combine-diff.c
blob22542217ee6aeba5058d02d99b27e20ca894af7f
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-interface.h"
8 #include "log-tree.h"
10 static int uninteresting(struct diff_filepair *p)
12 if (diff_unmodified_pair(p))
13 return 1;
14 return 0;
17 static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
19 struct diff_queue_struct *q = &diff_queued_diff;
20 struct combine_diff_path *p;
21 int i;
23 if (!n) {
24 struct combine_diff_path *list = NULL, **tail = &list;
25 for (i = 0; i < q->nr; i++) {
26 int len;
27 const char *path;
28 if (uninteresting(q->queue[i]))
29 continue;
30 path = q->queue[i]->two->path;
31 len = strlen(path);
32 p = xmalloc(combine_diff_path_size(num_parent, len));
33 p->path = (char*) &(p->parent[num_parent]);
34 memcpy(p->path, path, len);
35 p->path[len] = 0;
36 p->len = len;
37 p->next = NULL;
38 memset(p->parent, 0,
39 sizeof(p->parent[0]) * num_parent);
41 memcpy(p->sha1, q->queue[i]->two->sha1, 20);
42 p->mode = q->queue[i]->two->mode;
43 memcpy(p->parent[n].sha1, q->queue[i]->one->sha1, 20);
44 p->parent[n].mode = q->queue[i]->one->mode;
45 p->parent[n].status = q->queue[i]->status;
46 *tail = p;
47 tail = &p->next;
49 return list;
52 for (p = curr; p; p = p->next) {
53 int found = 0;
54 if (!p->len)
55 continue;
56 for (i = 0; i < q->nr; i++) {
57 const char *path;
58 int len;
60 if (uninteresting(q->queue[i]))
61 continue;
62 path = q->queue[i]->two->path;
63 len = strlen(path);
64 if (len == p->len && !memcmp(path, p->path, len)) {
65 found = 1;
66 memcpy(p->parent[n].sha1,
67 q->queue[i]->one->sha1, 20);
68 p->parent[n].mode = q->queue[i]->one->mode;
69 p->parent[n].status = q->queue[i]->status;
70 break;
73 if (!found)
74 p->len = 0;
76 return curr;
79 /* Lines lost from parent */
80 struct lline {
81 struct lline *next;
82 int len;
83 unsigned long parent_map;
84 char line[FLEX_ARRAY];
87 /* Lines surviving in the merge result */
88 struct sline {
89 struct lline *lost_head, **lost_tail;
90 char *bol;
91 int len;
92 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
93 * we did not change it).
94 * bit N is used for "interesting" lines, including context.
96 unsigned long flag;
97 unsigned long *p_lno;
100 static char *grab_blob(const unsigned char *sha1, unsigned long *size)
102 char *blob;
103 char type[20];
104 if (!memcmp(sha1, null_sha1, 20)) {
105 /* deleted blob */
106 *size = 0;
107 return xcalloc(1, 1);
109 blob = read_sha1_file(sha1, type, size);
110 if (strcmp(type, blob_type))
111 die("object '%s' is not a blob!", sha1_to_hex(sha1));
112 return blob;
115 static void append_lost(struct sline *sline, int n, const char *line, int len)
117 struct lline *lline;
118 unsigned long this_mask = (1UL<<n);
119 if (line[len-1] == '\n')
120 len--;
122 /* Check to see if we can squash things */
123 if (sline->lost_head) {
124 struct lline *last_one = NULL;
125 /* We cannot squash it with earlier one */
126 for (lline = sline->lost_head;
127 lline;
128 lline = lline->next)
129 if (lline->parent_map & this_mask)
130 last_one = lline;
131 lline = last_one ? last_one->next : sline->lost_head;
132 while (lline) {
133 if (lline->len == len &&
134 !memcmp(lline->line, line, len)) {
135 lline->parent_map |= this_mask;
136 return;
138 lline = lline->next;
142 lline = xmalloc(sizeof(*lline) + len + 1);
143 lline->len = len;
144 lline->next = NULL;
145 lline->parent_map = this_mask;
146 memcpy(lline->line, line, len);
147 lline->line[len] = 0;
148 *sline->lost_tail = lline;
149 sline->lost_tail = &lline->next;
152 struct combine_diff_state {
153 struct xdiff_emit_state xm;
155 unsigned int lno;
156 int ob, on, nb, nn;
157 unsigned long nmask;
158 int num_parent;
159 int n;
160 struct sline *sline;
161 struct sline *lost_bucket;
164 static void consume_line(void *state_, char *line, unsigned long len)
166 struct combine_diff_state *state = state_;
167 if (5 < len && !memcmp("@@ -", line, 4)) {
168 if (parse_hunk_header(line, len,
169 &state->ob, &state->on,
170 &state->nb, &state->nn))
171 return;
172 state->lno = state->nb;
173 if (!state->nb)
174 /* @@ -1,2 +0,0 @@ to remove the
175 * first two lines...
177 state->nb = 1;
178 if (state->nn == 0)
179 /* @@ -X,Y +N,0 @@ removed Y lines
180 * that would have come *after* line N
181 * in the result. Our lost buckets hang
182 * to the line after the removed lines,
184 state->lost_bucket = &state->sline[state->nb];
185 else
186 state->lost_bucket = &state->sline[state->nb-1];
187 if (!state->sline[state->nb-1].p_lno)
188 state->sline[state->nb-1].p_lno =
189 xcalloc(state->num_parent,
190 sizeof(unsigned long));
191 state->sline[state->nb-1].p_lno[state->n] = state->ob;
192 return;
194 if (!state->lost_bucket)
195 return; /* not in any hunk yet */
196 switch (line[0]) {
197 case '-':
198 append_lost(state->lost_bucket, state->n, line+1, len-1);
199 break;
200 case '+':
201 state->sline[state->lno-1].flag |= state->nmask;
202 state->lno++;
203 break;
207 static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
208 struct sline *sline, unsigned int cnt, int n,
209 int num_parent)
211 unsigned int p_lno, lno;
212 unsigned long nmask = (1UL << n);
213 xpparam_t xpp;
214 xdemitconf_t xecfg;
215 mmfile_t parent_file;
216 xdemitcb_t ecb;
217 struct combine_diff_state state;
218 unsigned long sz;
220 if (!cnt)
221 return; /* result deleted */
223 parent_file.ptr = grab_blob(parent, &sz);
224 parent_file.size = sz;
225 xpp.flags = XDF_NEED_MINIMAL;
226 xecfg.ctxlen = 0;
227 xecfg.flags = 0;
228 ecb.outf = xdiff_outf;
229 ecb.priv = &state;
230 memset(&state, 0, sizeof(state));
231 state.xm.consume = consume_line;
232 state.nmask = nmask;
233 state.sline = sline;
234 state.lno = 1;
235 state.num_parent = num_parent;
236 state.n = n;
238 xdl_diff(&parent_file, result_file, &xpp, &xecfg, &ecb);
239 free(parent_file.ptr);
241 /* Assign line numbers for this parent.
243 * sline[lno].p_lno[n] records the first line number
244 * (counting from 1) for parent N if the final hunk display
245 * started by showing sline[lno] (possibly showing the lost
246 * lines attached to it first).
248 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
249 struct lline *ll;
250 sline[lno].p_lno[n] = p_lno;
252 /* How many lines would this sline advance the p_lno? */
253 ll = sline[lno].lost_head;
254 while (ll) {
255 if (ll->parent_map & nmask)
256 p_lno++; /* '-' means parent had it */
257 ll = ll->next;
259 if (lno < cnt && !(sline[lno].flag & nmask))
260 p_lno++; /* no '+' means parent had it */
262 sline[lno].p_lno[n] = p_lno; /* trailer */
265 static unsigned long context = 3;
266 static char combine_marker = '@';
268 static int interesting(struct sline *sline, unsigned long all_mask)
270 /* If some parents lost lines here, or if we have added to
271 * some parent, it is interesting.
273 return ((sline->flag & all_mask) || sline->lost_head);
276 static unsigned long adjust_hunk_tail(struct sline *sline,
277 unsigned long all_mask,
278 unsigned long hunk_begin,
279 unsigned long i)
281 /* i points at the first uninteresting line. If the last line
282 * of the hunk was interesting only because it has some
283 * deletion, then it is not all that interesting for the
284 * purpose of giving trailing context lines. This is because
285 * we output '-' line and then unmodified sline[i-1] itself in
286 * that case which gives us one extra context line.
288 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
289 i--;
290 return i;
293 static unsigned long find_next(struct sline *sline,
294 unsigned long mark,
295 unsigned long i,
296 unsigned long cnt,
297 int look_for_uninteresting)
299 /* We have examined up to i-1 and are about to look at i.
300 * Find next interesting or uninteresting line. Here,
301 * "interesting" does not mean interesting(), but marked by
302 * the give_context() function below (i.e. it includes context
303 * lines that are not interesting to interesting() function
304 * that are surrounded by interesting() ones.
306 while (i <= cnt)
307 if (look_for_uninteresting
308 ? !(sline[i].flag & mark)
309 : (sline[i].flag & mark))
310 return i;
311 else
312 i++;
313 return i;
316 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
318 unsigned long all_mask = (1UL<<num_parent) - 1;
319 unsigned long mark = (1UL<<num_parent);
320 unsigned long i;
322 /* Two groups of interesting lines may have a short gap of
323 * unintersting lines. Connect such groups to give them a
324 * bit of context.
326 * We first start from what the interesting() function says,
327 * and mark them with "mark", and paint context lines with the
328 * mark. So interesting() would still say false for such context
329 * lines but they are treated as "interesting" in the end.
331 i = find_next(sline, mark, 0, cnt, 0);
332 if (cnt < i)
333 return 0;
335 while (i <= cnt) {
336 unsigned long j = (context < i) ? (i - context) : 0;
337 unsigned long k;
339 /* Paint a few lines before the first interesting line. */
340 while (j < i)
341 sline[j++].flag |= mark;
343 again:
344 /* we know up to i is to be included. where does the
345 * next uninteresting one start?
347 j = find_next(sline, mark, i, cnt, 1);
348 if (cnt < j)
349 break; /* the rest are all interesting */
351 /* lookahead context lines */
352 k = find_next(sline, mark, j, cnt, 0);
353 j = adjust_hunk_tail(sline, all_mask, i, j);
355 if (k < j + context) {
356 /* k is interesting and [j,k) are not, but
357 * paint them interesting because the gap is small.
359 while (j < k)
360 sline[j++].flag |= mark;
361 i = k;
362 goto again;
365 /* j is the first uninteresting line and there is
366 * no overlap beyond it within context lines. Paint
367 * the trailing edge a bit.
369 i = k;
370 k = (j + context < cnt+1) ? j + context : cnt+1;
371 while (j < k)
372 sline[j++].flag |= mark;
374 return 1;
377 static int make_hunks(struct sline *sline, unsigned long cnt,
378 int num_parent, int dense)
380 unsigned long all_mask = (1UL<<num_parent) - 1;
381 unsigned long mark = (1UL<<num_parent);
382 unsigned long i;
383 int has_interesting = 0;
385 for (i = 0; i <= cnt; i++) {
386 if (interesting(&sline[i], all_mask))
387 sline[i].flag |= mark;
388 else
389 sline[i].flag &= ~mark;
391 if (!dense)
392 return give_context(sline, cnt, num_parent);
394 /* Look at each hunk, and if we have changes from only one
395 * parent, or the changes are the same from all but one
396 * parent, mark that uninteresting.
398 i = 0;
399 while (i <= cnt) {
400 unsigned long j, hunk_begin, hunk_end;
401 unsigned long same_diff;
402 while (i <= cnt && !(sline[i].flag & mark))
403 i++;
404 if (cnt < i)
405 break; /* No more interesting hunks */
406 hunk_begin = i;
407 for (j = i + 1; j <= cnt; j++) {
408 if (!(sline[j].flag & mark)) {
409 /* Look beyond the end to see if there
410 * is an interesting line after this
411 * hunk within context span.
413 unsigned long la; /* lookahead */
414 int contin = 0;
415 la = adjust_hunk_tail(sline, all_mask,
416 hunk_begin, j);
417 la = (la + context < cnt + 1) ?
418 (la + context) : cnt + 1;
419 while (j <= --la) {
420 if (sline[la].flag & mark) {
421 contin = 1;
422 break;
425 if (!contin)
426 break;
427 j = la;
430 hunk_end = j;
432 /* [i..hunk_end) are interesting. Now is it really
433 * interesting? We check if there are only two versions
434 * and the result matches one of them. That is, we look
435 * at:
436 * (+) line, which records lines added to which parents;
437 * this line appears in the result.
438 * (-) line, which records from what parents the line
439 * was removed; this line does not appear in the result.
440 * then check the set of parents the result has difference
441 * from, from all lines. If there are lines that has
442 * different set of parents that the result has differences
443 * from, that means we have more than two versions.
445 * Even when we have only two versions, if the result does
446 * not match any of the parents, the it should be considered
447 * interesting. In such a case, we would have all '+' line.
448 * After passing the above "two versions" test, that would
449 * appear as "the same set of parents" to be "all parents".
451 same_diff = 0;
452 has_interesting = 0;
453 for (j = i; j < hunk_end && !has_interesting; j++) {
454 unsigned long this_diff = sline[j].flag & all_mask;
455 struct lline *ll = sline[j].lost_head;
456 if (this_diff) {
457 /* This has some changes. Is it the
458 * same as others?
460 if (!same_diff)
461 same_diff = this_diff;
462 else if (same_diff != this_diff) {
463 has_interesting = 1;
464 break;
467 while (ll && !has_interesting) {
468 /* Lost this line from these parents;
469 * who are they? Are they the same?
471 this_diff = ll->parent_map;
472 if (!same_diff)
473 same_diff = this_diff;
474 else if (same_diff != this_diff) {
475 has_interesting = 1;
477 ll = ll->next;
481 if (!has_interesting && same_diff != all_mask) {
482 /* This hunk is not that interesting after all */
483 for (j = hunk_begin; j < hunk_end; j++)
484 sline[j].flag &= ~mark;
486 i = hunk_end;
489 has_interesting = give_context(sline, cnt, num_parent);
490 return has_interesting;
493 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n)
495 l0 = sline[l0].p_lno[n];
496 l1 = sline[l1].p_lno[n];
497 printf(" -%lu,%lu", l0, l1-l0);
500 static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent)
502 unsigned long mark = (1UL<<num_parent);
503 int i;
504 unsigned long lno = 0;
506 if (!cnt)
507 return; /* result deleted */
509 while (1) {
510 struct sline *sl = &sline[lno];
511 unsigned long hunk_end;
512 unsigned long rlines;
513 while (lno <= cnt && !(sline[lno].flag & mark))
514 lno++;
515 if (cnt < lno)
516 break;
517 else {
518 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
519 if (!(sline[hunk_end].flag & mark))
520 break;
522 rlines = hunk_end - lno;
523 if (cnt < hunk_end)
524 rlines--; /* pointing at the last delete hunk */
525 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
526 for (i = 0; i < num_parent; i++)
527 show_parent_lno(sline, lno, hunk_end, i);
528 printf(" +%lu,%lu ", lno+1, rlines);
529 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
530 putchar('\n');
531 while (lno < hunk_end) {
532 struct lline *ll;
533 int j;
534 unsigned long p_mask;
535 sl = &sline[lno++];
536 ll = sl->lost_head;
537 while (ll) {
538 for (j = 0; j < num_parent; j++) {
539 if (ll->parent_map & (1UL<<j))
540 putchar('-');
541 else
542 putchar(' ');
544 puts(ll->line);
545 ll = ll->next;
547 if (cnt < lno)
548 break;
549 p_mask = 1;
550 for (j = 0; j < num_parent; j++) {
551 if (p_mask & sl->flag)
552 putchar('+');
553 else
554 putchar(' ');
555 p_mask <<= 1;
557 printf("%.*s\n", sl->len, sl->bol);
562 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
563 int i, int j)
565 /* We have already examined parent j and we know parent i
566 * and parent j are the same, so reuse the combined result
567 * of parent j for parent i.
569 unsigned long lno, imask, jmask;
570 imask = (1UL<<i);
571 jmask = (1UL<<j);
573 for (lno = 0; lno <= cnt; lno++) {
574 struct lline *ll = sline->lost_head;
575 sline->p_lno[i] = sline->p_lno[j];
576 while (ll) {
577 if (ll->parent_map & jmask)
578 ll->parent_map |= imask;
579 ll = ll->next;
581 if (sline->flag & jmask)
582 sline->flag |= imask;
583 sline++;
585 /* the overall size of the file (sline[cnt]) */
586 sline->p_lno[i] = sline->p_lno[j];
589 static void dump_quoted_path(const char *prefix, const char *path)
591 fputs(prefix, stdout);
592 if (quote_c_style(path, NULL, NULL, 0))
593 quote_c_style(path, NULL, stdout, 0);
594 else
595 printf("%s", path);
596 putchar('\n');
599 static int show_patch_diff(struct combine_diff_path *elem, int num_parent,
600 int dense, struct rev_info *rev)
602 struct diff_options *opt = &rev->diffopt;
603 unsigned long result_size, cnt, lno;
604 char *result, *cp;
605 struct sline *sline; /* survived lines */
606 int mode_differs = 0;
607 int i, show_hunks, shown_header = 0;
608 int working_tree_file = !memcmp(elem->sha1, null_sha1, 20);
609 int abbrev = opt->full_index ? 40 : DEFAULT_ABBREV;
610 mmfile_t result_file;
612 context = opt->context;
613 /* Read the result of merge first */
614 if (!working_tree_file)
615 result = grab_blob(elem->sha1, &result_size);
616 else {
617 /* Used by diff-tree to read from the working tree */
618 struct stat st;
619 int fd;
620 if (0 <= (fd = open(elem->path, O_RDONLY)) &&
621 !fstat(fd, &st)) {
622 int len = st.st_size;
623 int sz = 0;
625 elem->mode = canon_mode(st.st_mode);
626 result_size = len;
627 result = xmalloc(len + 1);
628 while (sz < len) {
629 int done = xread(fd, result+sz, len-sz);
630 if (done == 0)
631 break;
632 if (done < 0)
633 die("read error '%s'", elem->path);
634 sz += done;
636 result[len] = 0;
638 else {
639 /* deleted file */
640 result_size = 0;
641 elem->mode = 0;
642 result = xmalloc(1);
643 result[0] = 0;
645 if (0 <= fd)
646 close(fd);
649 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
650 if (*cp == '\n')
651 cnt++;
653 if (result_size && result[result_size-1] != '\n')
654 cnt++; /* incomplete line */
656 sline = xcalloc(cnt+2, sizeof(*sline));
657 sline[0].bol = result;
658 for (lno = 0; lno <= cnt + 1; lno++) {
659 sline[lno].lost_tail = &sline[lno].lost_head;
660 sline[lno].flag = 0;
662 for (lno = 0, cp = result; cp < result + result_size; cp++) {
663 if (*cp == '\n') {
664 sline[lno].len = cp - sline[lno].bol;
665 lno++;
666 if (lno < cnt)
667 sline[lno].bol = cp + 1;
670 if (result_size && result[result_size-1] != '\n')
671 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
673 result_file.ptr = result;
674 result_file.size = result_size;
676 /* Even p_lno[cnt+1] is valid -- that is for the end line number
677 * for deletion hunk at the end.
679 sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
680 for (lno = 0; lno <= cnt; lno++)
681 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
683 for (i = 0; i < num_parent; i++) {
684 int j;
685 for (j = 0; j < i; j++) {
686 if (!memcmp(elem->parent[i].sha1,
687 elem->parent[j].sha1, 20)) {
688 reuse_combine_diff(sline, cnt, i, j);
689 break;
692 if (i <= j)
693 combine_diff(elem->parent[i].sha1, &result_file, sline,
694 cnt, i, num_parent);
695 if (elem->parent[i].mode != elem->mode)
696 mode_differs = 1;
699 show_hunks = make_hunks(sline, cnt, num_parent, dense);
701 if (show_hunks || mode_differs || working_tree_file) {
702 const char *abb;
704 if (rev->loginfo)
705 show_log(rev, rev->loginfo, "\n");
706 dump_quoted_path(dense ? "diff --cc " : "diff --combined ", elem->path);
707 printf("index ");
708 for (i = 0; i < num_parent; i++) {
709 abb = find_unique_abbrev(elem->parent[i].sha1,
710 abbrev);
711 printf("%s%s", i ? "," : "", abb);
713 abb = find_unique_abbrev(elem->sha1, abbrev);
714 printf("..%s\n", abb);
716 if (mode_differs) {
717 int added = !!elem->mode;
718 for (i = 0; added && i < num_parent; i++)
719 if (elem->parent[i].status !=
720 DIFF_STATUS_ADDED)
721 added = 0;
722 if (added)
723 printf("new file mode %06o", elem->mode);
724 else {
725 if (!elem->mode)
726 printf("deleted file ");
727 printf("mode ");
728 for (i = 0; i < num_parent; i++) {
729 printf("%s%06o", i ? "," : "",
730 elem->parent[i].mode);
732 if (elem->mode)
733 printf("..%06o", elem->mode);
735 putchar('\n');
737 dump_quoted_path("--- a/", elem->path);
738 dump_quoted_path("+++ b/", elem->path);
739 dump_sline(sline, cnt, num_parent);
741 free(result);
743 for (lno = 0; lno < cnt; lno++) {
744 if (sline[lno].lost_head) {
745 struct lline *ll = sline[lno].lost_head;
746 while (ll) {
747 struct lline *tmp = ll;
748 ll = ll->next;
749 free(tmp);
753 free(sline[0].p_lno);
754 free(sline);
755 return shown_header;
758 #define COLONS "::::::::::::::::::::::::::::::::"
760 static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
762 struct diff_options *opt = &rev->diffopt;
763 int i, offset;
764 const char *prefix;
765 int line_termination, inter_name_termination;
767 line_termination = opt->line_termination;
768 inter_name_termination = '\t';
769 if (!line_termination)
770 inter_name_termination = 0;
772 if (rev->loginfo)
773 show_log(rev, rev->loginfo, "\n");
775 if (opt->output_format == DIFF_FORMAT_RAW) {
776 offset = strlen(COLONS) - num_parent;
777 if (offset < 0)
778 offset = 0;
779 prefix = COLONS + offset;
781 /* Show the modes */
782 for (i = 0; i < num_parent; i++) {
783 printf("%s%06o", prefix, p->parent[i].mode);
784 prefix = " ";
786 printf("%s%06o", prefix, p->mode);
788 /* Show sha1's */
789 for (i = 0; i < num_parent; i++)
790 printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
791 opt->abbrev));
792 printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
795 if (opt->output_format == DIFF_FORMAT_RAW ||
796 opt->output_format == DIFF_FORMAT_NAME_STATUS) {
797 for (i = 0; i < num_parent; i++)
798 putchar(p->parent[i].status);
799 putchar(inter_name_termination);
802 if (line_termination) {
803 if (quote_c_style(p->path, NULL, NULL, 0))
804 quote_c_style(p->path, NULL, stdout, 0);
805 else
806 printf("%s", p->path);
807 putchar(line_termination);
809 else {
810 printf("%s%c", p->path, line_termination);
814 void show_combined_diff(struct combine_diff_path *p,
815 int num_parent,
816 int dense,
817 struct rev_info *rev)
819 struct diff_options *opt = &rev->diffopt;
820 if (!p->len)
821 return;
822 switch (opt->output_format) {
823 case DIFF_FORMAT_RAW:
824 case DIFF_FORMAT_NAME_STATUS:
825 case DIFF_FORMAT_NAME:
826 show_raw_diff(p, num_parent, rev);
827 return;
828 case DIFF_FORMAT_PATCH:
829 show_patch_diff(p, num_parent, dense, rev);
830 return;
831 default:
832 return;
836 void diff_tree_combined(const unsigned char *sha1,
837 const unsigned char parent[][20],
838 int num_parent,
839 int dense,
840 struct rev_info *rev)
842 struct diff_options *opt = &rev->diffopt;
843 struct diff_options diffopts;
844 struct combine_diff_path *p, *paths = NULL;
845 int i, num_paths;
846 int do_diffstat;
848 do_diffstat = (opt->output_format == DIFF_FORMAT_DIFFSTAT ||
849 opt->with_stat);
850 diffopts = *opt;
851 diffopts.with_raw = 0;
852 diffopts.with_stat = 0;
853 diffopts.recursive = 1;
855 /* find set of paths that everybody touches */
856 for (i = 0; i < num_parent; i++) {
857 /* show stat against the first parent even
858 * when doing combined diff.
860 if (i == 0 && do_diffstat)
861 diffopts.output_format = DIFF_FORMAT_DIFFSTAT;
862 else
863 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
864 diff_tree_sha1(parent[i], sha1, "", &diffopts);
865 diffcore_std(&diffopts);
866 paths = intersect_paths(paths, i, num_parent);
868 if (do_diffstat && rev->loginfo)
869 show_log(rev, rev->loginfo,
870 opt->with_stat ? "---\n" : "\n");
871 diff_flush(&diffopts);
872 if (opt->with_stat)
873 putchar('\n');
876 /* find out surviving paths */
877 for (num_paths = 0, p = paths; p; p = p->next) {
878 if (p->len)
879 num_paths++;
881 if (num_paths) {
882 if (opt->with_raw) {
883 int saved_format = opt->output_format;
884 opt->output_format = DIFF_FORMAT_RAW;
885 for (p = paths; p; p = p->next) {
886 show_combined_diff(p, num_parent, dense, rev);
888 opt->output_format = saved_format;
889 putchar(opt->line_termination);
891 for (p = paths; p; p = p->next) {
892 show_combined_diff(p, num_parent, dense, rev);
896 /* Clean things up */
897 while (paths) {
898 struct combine_diff_path *tmp = paths;
899 paths = paths->next;
900 free(tmp);
904 void diff_tree_combined_merge(const unsigned char *sha1,
905 int dense, struct rev_info *rev)
907 int num_parent;
908 const unsigned char (*parent)[20];
909 struct commit *commit = lookup_commit(sha1);
910 struct commit_list *parents;
912 /* count parents */
913 for (parents = commit->parents, num_parent = 0;
914 parents;
915 parents = parents->next, num_parent++)
916 ; /* nothing */
918 parent = xmalloc(num_parent * sizeof(*parent));
919 for (parents = commit->parents, num_parent = 0;
920 parents;
921 parents = parents->next, num_parent++)
922 memcpy(parent + num_parent, parents->item->object.sha1, 20);
923 diff_tree_combined(sha1, parent, num_parent, dense, rev);