Use strbuf for struct xdiff_emit_state's remainder
[git/jrn.git] / combine-diff.c
blob72dd6d22340ddd2b5f3d9c7887911a16334848bd
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 struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
12 struct diff_queue_struct *q = &diff_queued_diff;
13 struct combine_diff_path *p;
14 int i;
16 if (!n) {
17 struct combine_diff_path *list = NULL, **tail = &list;
18 for (i = 0; i < q->nr; i++) {
19 int len;
20 const char *path;
21 if (diff_unmodified_pair(q->queue[i]))
22 continue;
23 path = q->queue[i]->two->path;
24 len = strlen(path);
25 p = xmalloc(combine_diff_path_size(num_parent, len));
26 p->path = (char*) &(p->parent[num_parent]);
27 memcpy(p->path, path, len);
28 p->path[len] = 0;
29 p->len = len;
30 p->next = NULL;
31 memset(p->parent, 0,
32 sizeof(p->parent[0]) * num_parent);
34 hashcpy(p->sha1, q->queue[i]->two->sha1);
35 p->mode = q->queue[i]->two->mode;
36 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
37 p->parent[n].mode = q->queue[i]->one->mode;
38 p->parent[n].status = q->queue[i]->status;
39 *tail = p;
40 tail = &p->next;
42 return list;
45 for (p = curr; p; p = p->next) {
46 int found = 0;
47 if (!p->len)
48 continue;
49 for (i = 0; i < q->nr; i++) {
50 const char *path;
51 int len;
53 if (diff_unmodified_pair(q->queue[i]))
54 continue;
55 path = q->queue[i]->two->path;
56 len = strlen(path);
57 if (len == p->len && !memcmp(path, p->path, len)) {
58 found = 1;
59 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
60 p->parent[n].mode = q->queue[i]->one->mode;
61 p->parent[n].status = q->queue[i]->status;
62 break;
65 if (!found)
66 p->len = 0;
68 return curr;
71 /* Lines lost from parent */
72 struct lline {
73 struct lline *next;
74 int len;
75 unsigned long parent_map;
76 char line[FLEX_ARRAY];
79 /* Lines surviving in the merge result */
80 struct sline {
81 struct lline *lost_head, **lost_tail;
82 char *bol;
83 int len;
84 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
85 * we did not change it).
86 * bit N is used for "interesting" lines, including context.
87 * bit (N+1) is used for "do not show deletion before this".
89 unsigned long flag;
90 unsigned long *p_lno;
93 static char *grab_blob(const unsigned char *sha1, unsigned long *size)
95 char *blob;
96 enum object_type type;
97 if (is_null_sha1(sha1)) {
98 /* deleted blob */
99 *size = 0;
100 return xcalloc(1, 1);
102 blob = read_sha1_file(sha1, &type, size);
103 if (type != OBJ_BLOB)
104 die("object '%s' is not a blob!", sha1_to_hex(sha1));
105 return blob;
108 static void append_lost(struct sline *sline, int n, const char *line, int len)
110 struct lline *lline;
111 unsigned long this_mask = (1UL<<n);
112 if (line[len-1] == '\n')
113 len--;
115 /* Check to see if we can squash things */
116 if (sline->lost_head) {
117 struct lline *last_one = NULL;
118 /* We cannot squash it with earlier one */
119 for (lline = sline->lost_head;
120 lline;
121 lline = lline->next)
122 if (lline->parent_map & this_mask)
123 last_one = lline;
124 lline = last_one ? last_one->next : sline->lost_head;
125 while (lline) {
126 if (lline->len == len &&
127 !memcmp(lline->line, line, len)) {
128 lline->parent_map |= this_mask;
129 return;
131 lline = lline->next;
135 lline = xmalloc(sizeof(*lline) + len + 1);
136 lline->len = len;
137 lline->next = NULL;
138 lline->parent_map = this_mask;
139 memcpy(lline->line, line, len);
140 lline->line[len] = 0;
141 *sline->lost_tail = lline;
142 sline->lost_tail = &lline->next;
145 struct combine_diff_state {
146 struct xdiff_emit_state xm;
148 unsigned int lno;
149 int ob, on, nb, nn;
150 unsigned long nmask;
151 int num_parent;
152 int n;
153 struct sline *sline;
154 struct sline *lost_bucket;
157 static void consume_line(void *state_, char *line, unsigned long len)
159 struct combine_diff_state *state = state_;
160 if (5 < len && !memcmp("@@ -", line, 4)) {
161 if (parse_hunk_header(line, len,
162 &state->ob, &state->on,
163 &state->nb, &state->nn))
164 return;
165 state->lno = state->nb;
166 if (!state->nb)
167 /* @@ -1,2 +0,0 @@ to remove the
168 * first two lines...
170 state->nb = 1;
171 if (state->nn == 0)
172 /* @@ -X,Y +N,0 @@ removed Y lines
173 * that would have come *after* line N
174 * in the result. Our lost buckets hang
175 * to the line after the removed lines,
177 state->lost_bucket = &state->sline[state->nb];
178 else
179 state->lost_bucket = &state->sline[state->nb-1];
180 if (!state->sline[state->nb-1].p_lno)
181 state->sline[state->nb-1].p_lno =
182 xcalloc(state->num_parent,
183 sizeof(unsigned long));
184 state->sline[state->nb-1].p_lno[state->n] = state->ob;
185 return;
187 if (!state->lost_bucket)
188 return; /* not in any hunk yet */
189 switch (line[0]) {
190 case '-':
191 append_lost(state->lost_bucket, state->n, line+1, len-1);
192 break;
193 case '+':
194 state->sline[state->lno-1].flag |= state->nmask;
195 state->lno++;
196 break;
200 static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
201 struct sline *sline, unsigned int cnt, int n,
202 int num_parent)
204 unsigned int p_lno, lno;
205 unsigned long nmask = (1UL << n);
206 xpparam_t xpp;
207 xdemitconf_t xecfg;
208 mmfile_t parent_file;
209 xdemitcb_t ecb;
210 struct combine_diff_state state;
211 unsigned long sz;
213 if (!cnt)
214 return; /* result deleted */
216 parent_file.ptr = grab_blob(parent, &sz);
217 parent_file.size = sz;
218 xpp.flags = XDF_NEED_MINIMAL;
219 memset(&xecfg, 0, sizeof(xecfg));
220 memset(&state, 0, sizeof(state));
221 state.xm.consume = consume_line;
222 state.nmask = nmask;
223 state.sline = sline;
224 state.lno = 1;
225 state.num_parent = num_parent;
226 state.n = n;
228 xdi_diff_outf(&parent_file, result_file,
229 &state.xm, &xpp, &xecfg, &ecb);
230 free(parent_file.ptr);
232 /* Assign line numbers for this parent.
234 * sline[lno].p_lno[n] records the first line number
235 * (counting from 1) for parent N if the final hunk display
236 * started by showing sline[lno] (possibly showing the lost
237 * lines attached to it first).
239 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
240 struct lline *ll;
241 sline[lno].p_lno[n] = p_lno;
243 /* How many lines would this sline advance the p_lno? */
244 ll = sline[lno].lost_head;
245 while (ll) {
246 if (ll->parent_map & nmask)
247 p_lno++; /* '-' means parent had it */
248 ll = ll->next;
250 if (lno < cnt && !(sline[lno].flag & nmask))
251 p_lno++; /* no '+' means parent had it */
253 sline[lno].p_lno[n] = p_lno; /* trailer */
256 static unsigned long context = 3;
257 static char combine_marker = '@';
259 static int interesting(struct sline *sline, unsigned long all_mask)
261 /* If some parents lost lines here, or if we have added to
262 * some parent, it is interesting.
264 return ((sline->flag & all_mask) || sline->lost_head);
267 static unsigned long adjust_hunk_tail(struct sline *sline,
268 unsigned long all_mask,
269 unsigned long hunk_begin,
270 unsigned long i)
272 /* i points at the first uninteresting line. If the last line
273 * of the hunk was interesting only because it has some
274 * deletion, then it is not all that interesting for the
275 * purpose of giving trailing context lines. This is because
276 * we output '-' line and then unmodified sline[i-1] itself in
277 * that case which gives us one extra context line.
279 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
280 i--;
281 return i;
284 static unsigned long find_next(struct sline *sline,
285 unsigned long mark,
286 unsigned long i,
287 unsigned long cnt,
288 int look_for_uninteresting)
290 /* We have examined up to i-1 and are about to look at i.
291 * Find next interesting or uninteresting line. Here,
292 * "interesting" does not mean interesting(), but marked by
293 * the give_context() function below (i.e. it includes context
294 * lines that are not interesting to interesting() function
295 * that are surrounded by interesting() ones.
297 while (i <= cnt)
298 if (look_for_uninteresting
299 ? !(sline[i].flag & mark)
300 : (sline[i].flag & mark))
301 return i;
302 else
303 i++;
304 return i;
307 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
309 unsigned long all_mask = (1UL<<num_parent) - 1;
310 unsigned long mark = (1UL<<num_parent);
311 unsigned long no_pre_delete = (2UL<<num_parent);
312 unsigned long i;
314 /* Two groups of interesting lines may have a short gap of
315 * uninteresting lines. Connect such groups to give them a
316 * bit of context.
318 * We first start from what the interesting() function says,
319 * and mark them with "mark", and paint context lines with the
320 * mark. So interesting() would still say false for such context
321 * lines but they are treated as "interesting" in the end.
323 i = find_next(sline, mark, 0, cnt, 0);
324 if (cnt < i)
325 return 0;
327 while (i <= cnt) {
328 unsigned long j = (context < i) ? (i - context) : 0;
329 unsigned long k;
331 /* Paint a few lines before the first interesting line. */
332 while (j < i)
333 sline[j++].flag |= mark | no_pre_delete;
335 again:
336 /* we know up to i is to be included. where does the
337 * next uninteresting one start?
339 j = find_next(sline, mark, i, cnt, 1);
340 if (cnt < j)
341 break; /* the rest are all interesting */
343 /* lookahead context lines */
344 k = find_next(sline, mark, j, cnt, 0);
345 j = adjust_hunk_tail(sline, all_mask, i, j);
347 if (k < j + context) {
348 /* k is interesting and [j,k) are not, but
349 * paint them interesting because the gap is small.
351 while (j < k)
352 sline[j++].flag |= mark;
353 i = k;
354 goto again;
357 /* j is the first uninteresting line and there is
358 * no overlap beyond it within context lines. Paint
359 * the trailing edge a bit.
361 i = k;
362 k = (j + context < cnt+1) ? j + context : cnt+1;
363 while (j < k)
364 sline[j++].flag |= mark;
366 return 1;
369 static int make_hunks(struct sline *sline, unsigned long cnt,
370 int num_parent, int dense)
372 unsigned long all_mask = (1UL<<num_parent) - 1;
373 unsigned long mark = (1UL<<num_parent);
374 unsigned long i;
375 int has_interesting = 0;
377 for (i = 0; i <= cnt; i++) {
378 if (interesting(&sline[i], all_mask))
379 sline[i].flag |= mark;
380 else
381 sline[i].flag &= ~mark;
383 if (!dense)
384 return give_context(sline, cnt, num_parent);
386 /* Look at each hunk, and if we have changes from only one
387 * parent, or the changes are the same from all but one
388 * parent, mark that uninteresting.
390 i = 0;
391 while (i <= cnt) {
392 unsigned long j, hunk_begin, hunk_end;
393 unsigned long same_diff;
394 while (i <= cnt && !(sline[i].flag & mark))
395 i++;
396 if (cnt < i)
397 break; /* No more interesting hunks */
398 hunk_begin = i;
399 for (j = i + 1; j <= cnt; j++) {
400 if (!(sline[j].flag & mark)) {
401 /* Look beyond the end to see if there
402 * is an interesting line after this
403 * hunk within context span.
405 unsigned long la; /* lookahead */
406 int contin = 0;
407 la = adjust_hunk_tail(sline, all_mask,
408 hunk_begin, j);
409 la = (la + context < cnt + 1) ?
410 (la + context) : cnt + 1;
411 while (j <= --la) {
412 if (sline[la].flag & mark) {
413 contin = 1;
414 break;
417 if (!contin)
418 break;
419 j = la;
422 hunk_end = j;
424 /* [i..hunk_end) are interesting. Now is it really
425 * interesting? We check if there are only two versions
426 * and the result matches one of them. That is, we look
427 * at:
428 * (+) line, which records lines added to which parents;
429 * this line appears in the result.
430 * (-) line, which records from what parents the line
431 * was removed; this line does not appear in the result.
432 * then check the set of parents the result has difference
433 * from, from all lines. If there are lines that has
434 * different set of parents that the result has differences
435 * from, that means we have more than two versions.
437 * Even when we have only two versions, if the result does
438 * not match any of the parents, the it should be considered
439 * interesting. In such a case, we would have all '+' line.
440 * After passing the above "two versions" test, that would
441 * appear as "the same set of parents" to be "all parents".
443 same_diff = 0;
444 has_interesting = 0;
445 for (j = i; j < hunk_end && !has_interesting; j++) {
446 unsigned long this_diff = sline[j].flag & all_mask;
447 struct lline *ll = sline[j].lost_head;
448 if (this_diff) {
449 /* This has some changes. Is it the
450 * same as others?
452 if (!same_diff)
453 same_diff = this_diff;
454 else if (same_diff != this_diff) {
455 has_interesting = 1;
456 break;
459 while (ll && !has_interesting) {
460 /* Lost this line from these parents;
461 * who are they? Are they the same?
463 this_diff = ll->parent_map;
464 if (!same_diff)
465 same_diff = this_diff;
466 else if (same_diff != this_diff) {
467 has_interesting = 1;
469 ll = ll->next;
473 if (!has_interesting && same_diff != all_mask) {
474 /* This hunk is not that interesting after all */
475 for (j = hunk_begin; j < hunk_end; j++)
476 sline[j].flag &= ~mark;
478 i = hunk_end;
481 has_interesting = give_context(sline, cnt, num_parent);
482 return has_interesting;
485 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
487 l0 = sline[l0].p_lno[n];
488 l1 = sline[l1].p_lno[n];
489 printf(" -%lu,%lu", l0, l1-l0-null_context);
492 static int hunk_comment_line(const char *bol)
494 int ch;
496 if (!bol)
497 return 0;
498 ch = *bol & 0xff;
499 return (isalpha(ch) || ch == '_' || ch == '$');
502 static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
503 int use_color)
505 unsigned long mark = (1UL<<num_parent);
506 unsigned long no_pre_delete = (2UL<<num_parent);
507 int i;
508 unsigned long lno = 0;
509 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
510 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
511 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
512 const char *c_plain = diff_get_color(use_color, DIFF_PLAIN);
513 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
515 if (!cnt)
516 return; /* result deleted */
518 while (1) {
519 struct sline *sl = &sline[lno];
520 unsigned long hunk_end;
521 unsigned long rlines;
522 const char *hunk_comment = NULL;
523 unsigned long null_context = 0;
525 while (lno <= cnt && !(sline[lno].flag & mark)) {
526 if (hunk_comment_line(sline[lno].bol))
527 hunk_comment = sline[lno].bol;
528 lno++;
530 if (cnt < lno)
531 break;
532 else {
533 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
534 if (!(sline[hunk_end].flag & mark))
535 break;
537 rlines = hunk_end - lno;
538 if (cnt < hunk_end)
539 rlines--; /* pointing at the last delete hunk */
541 if (!context) {
543 * Even when running with --unified=0, all
544 * lines in the hunk needs to be processed in
545 * the loop below in order to show the
546 * deletion recorded in lost_head. However,
547 * we do not want to show the resulting line
548 * with all blank context markers in such a
549 * case. Compensate.
551 unsigned long j;
552 for (j = lno; j < hunk_end; j++)
553 if (!(sline[j].flag & (mark-1)))
554 null_context++;
555 rlines -= null_context;
558 fputs(c_frag, stdout);
559 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
560 for (i = 0; i < num_parent; i++)
561 show_parent_lno(sline, lno, hunk_end, i, null_context);
562 printf(" +%lu,%lu ", lno+1, rlines);
563 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
565 if (hunk_comment) {
566 int comment_end = 0;
567 for (i = 0; i < 40; i++) {
568 int ch = hunk_comment[i] & 0xff;
569 if (!ch || ch == '\n')
570 break;
571 if (!isspace(ch))
572 comment_end = i;
574 if (comment_end)
575 putchar(' ');
576 for (i = 0; i < comment_end; i++)
577 putchar(hunk_comment[i]);
580 printf("%s\n", c_reset);
581 while (lno < hunk_end) {
582 struct lline *ll;
583 int j;
584 unsigned long p_mask;
585 sl = &sline[lno++];
586 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost_head;
587 while (ll) {
588 fputs(c_old, stdout);
589 for (j = 0; j < num_parent; j++) {
590 if (ll->parent_map & (1UL<<j))
591 putchar('-');
592 else
593 putchar(' ');
595 printf("%s%s\n", ll->line, c_reset);
596 ll = ll->next;
598 if (cnt < lno)
599 break;
600 p_mask = 1;
601 if (!(sl->flag & (mark-1))) {
603 * This sline was here to hang the
604 * lost lines in front of it.
606 if (!context)
607 continue;
608 fputs(c_plain, stdout);
610 else
611 fputs(c_new, stdout);
612 for (j = 0; j < num_parent; j++) {
613 if (p_mask & sl->flag)
614 putchar('+');
615 else
616 putchar(' ');
617 p_mask <<= 1;
619 printf("%.*s%s\n", sl->len, sl->bol, c_reset);
624 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
625 int i, int j)
627 /* We have already examined parent j and we know parent i
628 * and parent j are the same, so reuse the combined result
629 * of parent j for parent i.
631 unsigned long lno, imask, jmask;
632 imask = (1UL<<i);
633 jmask = (1UL<<j);
635 for (lno = 0; lno <= cnt; lno++) {
636 struct lline *ll = sline->lost_head;
637 sline->p_lno[i] = sline->p_lno[j];
638 while (ll) {
639 if (ll->parent_map & jmask)
640 ll->parent_map |= imask;
641 ll = ll->next;
643 if (sline->flag & jmask)
644 sline->flag |= imask;
645 sline++;
647 /* the overall size of the file (sline[cnt]) */
648 sline->p_lno[i] = sline->p_lno[j];
651 static void dump_quoted_path(const char *head,
652 const char *prefix,
653 const char *path,
654 const char *c_meta, const char *c_reset)
656 static struct strbuf buf = STRBUF_INIT;
658 strbuf_reset(&buf);
659 strbuf_addstr(&buf, c_meta);
660 strbuf_addstr(&buf, head);
661 quote_two_c_style(&buf, prefix, path, 0);
662 strbuf_addstr(&buf, c_reset);
663 puts(buf.buf);
666 static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
667 int dense, struct rev_info *rev)
669 struct diff_options *opt = &rev->diffopt;
670 unsigned long result_size, cnt, lno;
671 char *result, *cp;
672 struct sline *sline; /* survived lines */
673 int mode_differs = 0;
674 int i, show_hunks;
675 int working_tree_file = is_null_sha1(elem->sha1);
676 int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
677 mmfile_t result_file;
679 context = opt->context;
680 /* Read the result of merge first */
681 if (!working_tree_file)
682 result = grab_blob(elem->sha1, &result_size);
683 else {
684 /* Used by diff-tree to read from the working tree */
685 struct stat st;
686 int fd = -1;
688 if (lstat(elem->path, &st) < 0)
689 goto deleted_file;
691 if (S_ISLNK(st.st_mode)) {
692 size_t len = xsize_t(st.st_size);
693 result_size = len;
694 result = xmalloc(len + 1);
695 if (result_size != readlink(elem->path, result, len)) {
696 error("readlink(%s): %s", elem->path,
697 strerror(errno));
698 return;
700 result[len] = 0;
701 elem->mode = canon_mode(st.st_mode);
703 else if (0 <= (fd = open(elem->path, O_RDONLY)) &&
704 !fstat(fd, &st)) {
705 size_t len = xsize_t(st.st_size);
706 ssize_t done;
707 int is_file, i;
709 elem->mode = canon_mode(st.st_mode);
710 /* if symlinks don't work, assume symlink if all parents
711 * are symlinks
713 is_file = has_symlinks;
714 for (i = 0; !is_file && i < num_parent; i++)
715 is_file = !S_ISLNK(elem->parent[i].mode);
716 if (!is_file)
717 elem->mode = canon_mode(S_IFLNK);
719 result_size = len;
720 result = xmalloc(len + 1);
722 done = read_in_full(fd, result, len);
723 if (done < 0)
724 die("read error '%s'", elem->path);
725 else if (done < len)
726 die("early EOF '%s'", elem->path);
728 result[len] = 0;
730 else {
731 deleted_file:
732 result_size = 0;
733 elem->mode = 0;
734 result = xcalloc(1, 1);
737 if (0 <= fd)
738 close(fd);
741 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
742 if (*cp == '\n')
743 cnt++;
745 if (result_size && result[result_size-1] != '\n')
746 cnt++; /* incomplete line */
748 sline = xcalloc(cnt+2, sizeof(*sline));
749 sline[0].bol = result;
750 for (lno = 0; lno <= cnt + 1; lno++) {
751 sline[lno].lost_tail = &sline[lno].lost_head;
752 sline[lno].flag = 0;
754 for (lno = 0, cp = result; cp < result + result_size; cp++) {
755 if (*cp == '\n') {
756 sline[lno].len = cp - sline[lno].bol;
757 lno++;
758 if (lno < cnt)
759 sline[lno].bol = cp + 1;
762 if (result_size && result[result_size-1] != '\n')
763 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
765 result_file.ptr = result;
766 result_file.size = result_size;
768 /* Even p_lno[cnt+1] is valid -- that is for the end line number
769 * for deletion hunk at the end.
771 sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
772 for (lno = 0; lno <= cnt; lno++)
773 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
775 for (i = 0; i < num_parent; i++) {
776 int j;
777 for (j = 0; j < i; j++) {
778 if (!hashcmp(elem->parent[i].sha1,
779 elem->parent[j].sha1)) {
780 reuse_combine_diff(sline, cnt, i, j);
781 break;
784 if (i <= j)
785 combine_diff(elem->parent[i].sha1, &result_file, sline,
786 cnt, i, num_parent);
787 if (elem->parent[i].mode != elem->mode)
788 mode_differs = 1;
791 show_hunks = make_hunks(sline, cnt, num_parent, dense);
793 if (show_hunks || mode_differs || working_tree_file) {
794 const char *abb;
795 int use_color = DIFF_OPT_TST(opt, COLOR_DIFF);
796 const char *c_meta = diff_get_color(use_color, DIFF_METAINFO);
797 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
798 int added = 0;
799 int deleted = 0;
801 if (rev->loginfo && !rev->no_commit_id)
802 show_log(rev);
803 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
804 "", elem->path, c_meta, c_reset);
805 printf("%sindex ", c_meta);
806 for (i = 0; i < num_parent; i++) {
807 abb = find_unique_abbrev(elem->parent[i].sha1,
808 abbrev);
809 printf("%s%s", i ? "," : "", abb);
811 abb = find_unique_abbrev(elem->sha1, abbrev);
812 printf("..%s%s\n", abb, c_reset);
814 if (mode_differs) {
815 deleted = !elem->mode;
817 /* We say it was added if nobody had it */
818 added = !deleted;
819 for (i = 0; added && i < num_parent; i++)
820 if (elem->parent[i].status !=
821 DIFF_STATUS_ADDED)
822 added = 0;
823 if (added)
824 printf("%snew file mode %06o",
825 c_meta, elem->mode);
826 else {
827 if (deleted)
828 printf("%sdeleted file ", c_meta);
829 printf("mode ");
830 for (i = 0; i < num_parent; i++) {
831 printf("%s%06o", i ? "," : "",
832 elem->parent[i].mode);
834 if (elem->mode)
835 printf("..%06o", elem->mode);
837 printf("%s\n", c_reset);
839 if (added)
840 dump_quoted_path("--- ", "", "/dev/null",
841 c_meta, c_reset);
842 else
843 dump_quoted_path("--- ", opt->a_prefix, elem->path,
844 c_meta, c_reset);
845 if (deleted)
846 dump_quoted_path("+++ ", "", "/dev/null",
847 c_meta, c_reset);
848 else
849 dump_quoted_path("+++ ", opt->b_prefix, elem->path,
850 c_meta, c_reset);
851 dump_sline(sline, cnt, num_parent,
852 DIFF_OPT_TST(opt, COLOR_DIFF));
854 free(result);
856 for (lno = 0; lno < cnt; lno++) {
857 if (sline[lno].lost_head) {
858 struct lline *ll = sline[lno].lost_head;
859 while (ll) {
860 struct lline *tmp = ll;
861 ll = ll->next;
862 free(tmp);
866 free(sline[0].p_lno);
867 free(sline);
870 #define COLONS "::::::::::::::::::::::::::::::::"
872 static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
874 struct diff_options *opt = &rev->diffopt;
875 int i, offset;
876 const char *prefix;
877 int line_termination, inter_name_termination;
879 line_termination = opt->line_termination;
880 inter_name_termination = '\t';
881 if (!line_termination)
882 inter_name_termination = 0;
884 if (rev->loginfo && !rev->no_commit_id)
885 show_log(rev);
887 if (opt->output_format & DIFF_FORMAT_RAW) {
888 offset = strlen(COLONS) - num_parent;
889 if (offset < 0)
890 offset = 0;
891 prefix = COLONS + offset;
893 /* Show the modes */
894 for (i = 0; i < num_parent; i++) {
895 printf("%s%06o", prefix, p->parent[i].mode);
896 prefix = " ";
898 printf("%s%06o", prefix, p->mode);
900 /* Show sha1's */
901 for (i = 0; i < num_parent; i++)
902 printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
903 opt->abbrev));
904 printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
907 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
908 for (i = 0; i < num_parent; i++)
909 putchar(p->parent[i].status);
910 putchar(inter_name_termination);
913 write_name_quoted(p->path, stdout, line_termination);
916 void show_combined_diff(struct combine_diff_path *p,
917 int num_parent,
918 int dense,
919 struct rev_info *rev)
921 struct diff_options *opt = &rev->diffopt;
922 if (!p->len)
923 return;
924 if (opt->output_format & (DIFF_FORMAT_RAW |
925 DIFF_FORMAT_NAME |
926 DIFF_FORMAT_NAME_STATUS))
927 show_raw_diff(p, num_parent, rev);
928 else if (opt->output_format & DIFF_FORMAT_PATCH)
929 show_patch_diff(p, num_parent, dense, rev);
932 void diff_tree_combined(const unsigned char *sha1,
933 const unsigned char parent[][20],
934 int num_parent,
935 int dense,
936 struct rev_info *rev)
938 struct diff_options *opt = &rev->diffopt;
939 struct diff_options diffopts;
940 struct combine_diff_path *p, *paths = NULL;
941 int i, num_paths, needsep, show_log_first;
943 diffopts = *opt;
944 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
945 DIFF_OPT_SET(&diffopts, RECURSIVE);
946 DIFF_OPT_CLR(&diffopts, ALLOW_EXTERNAL);
948 show_log_first = !!rev->loginfo && !rev->no_commit_id;
949 needsep = 0;
950 /* find set of paths that everybody touches */
951 for (i = 0; i < num_parent; i++) {
952 /* show stat against the first parent even
953 * when doing combined diff.
955 int stat_opt = (opt->output_format &
956 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
957 if (i == 0 && stat_opt)
958 diffopts.output_format = stat_opt;
959 else
960 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
961 diff_tree_sha1(parent[i], sha1, "", &diffopts);
962 diffcore_std(&diffopts);
963 paths = intersect_paths(paths, i, num_parent);
965 if (show_log_first && i == 0) {
966 show_log(rev);
967 if (rev->verbose_header && opt->output_format)
968 putchar(opt->line_termination);
970 diff_flush(&diffopts);
973 /* find out surviving paths */
974 for (num_paths = 0, p = paths; p; p = p->next) {
975 if (p->len)
976 num_paths++;
978 if (num_paths) {
979 if (opt->output_format & (DIFF_FORMAT_RAW |
980 DIFF_FORMAT_NAME |
981 DIFF_FORMAT_NAME_STATUS)) {
982 for (p = paths; p; p = p->next) {
983 if (p->len)
984 show_raw_diff(p, num_parent, rev);
986 needsep = 1;
988 else if (opt->output_format &
989 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
990 needsep = 1;
991 if (opt->output_format & DIFF_FORMAT_PATCH) {
992 if (needsep)
993 putchar(opt->line_termination);
994 for (p = paths; p; p = p->next) {
995 if (p->len)
996 show_patch_diff(p, num_parent, dense,
997 rev);
1002 /* Clean things up */
1003 while (paths) {
1004 struct combine_diff_path *tmp = paths;
1005 paths = paths->next;
1006 free(tmp);
1010 void diff_tree_combined_merge(const unsigned char *sha1,
1011 int dense, struct rev_info *rev)
1013 int num_parent;
1014 const unsigned char (*parent)[20];
1015 struct commit *commit = lookup_commit(sha1);
1016 struct commit_list *parents;
1018 /* count parents */
1019 for (parents = commit->parents, num_parent = 0;
1020 parents;
1021 parents = parents->next, num_parent++)
1022 ; /* nothing */
1024 parent = xmalloc(num_parent * sizeof(*parent));
1025 for (parents = commit->parents, num_parent = 0;
1026 parents;
1027 parents = parents->next, num_parent++)
1028 hashcpy((unsigned char*)(parent + num_parent),
1029 parents->item->object.sha1);
1030 diff_tree_combined(sha1, parent, num_parent, dense, rev);