fix GIT_TRACE segfault with shell-quoted aliases
[git/ljr.git] / combine-diff.c
blobf617e9ded6b7ff2bb7a8d3629480c2ad22667428
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"
9 #include "refs.h"
11 static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
13 struct diff_queue_struct *q = &diff_queued_diff;
14 struct combine_diff_path *p;
15 int i;
17 if (!n) {
18 struct combine_diff_path *list = NULL, **tail = &list;
19 for (i = 0; i < q->nr; i++) {
20 int len;
21 const char *path;
22 if (diff_unmodified_pair(q->queue[i]))
23 continue;
24 path = q->queue[i]->two->path;
25 len = strlen(path);
26 p = xmalloc(combine_diff_path_size(num_parent, len));
27 p->path = (char*) &(p->parent[num_parent]);
28 memcpy(p->path, path, len);
29 p->path[len] = 0;
30 p->len = len;
31 p->next = NULL;
32 memset(p->parent, 0,
33 sizeof(p->parent[0]) * num_parent);
35 hashcpy(p->sha1, q->queue[i]->two->sha1);
36 p->mode = q->queue[i]->two->mode;
37 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
38 p->parent[n].mode = q->queue[i]->one->mode;
39 p->parent[n].status = q->queue[i]->status;
40 *tail = p;
41 tail = &p->next;
43 return list;
46 for (p = curr; p; p = p->next) {
47 int found = 0;
48 if (!p->len)
49 continue;
50 for (i = 0; i < q->nr; i++) {
51 const char *path;
52 int len;
54 if (diff_unmodified_pair(q->queue[i]))
55 continue;
56 path = q->queue[i]->two->path;
57 len = strlen(path);
58 if (len == p->len && !memcmp(path, p->path, len)) {
59 found = 1;
60 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
61 p->parent[n].mode = q->queue[i]->one->mode;
62 p->parent[n].status = q->queue[i]->status;
63 break;
66 if (!found)
67 p->len = 0;
69 return curr;
72 /* Lines lost from parent */
73 struct lline {
74 struct lline *next;
75 int len;
76 unsigned long parent_map;
77 char line[FLEX_ARRAY];
80 /* Lines surviving in the merge result */
81 struct sline {
82 struct lline *lost_head, **lost_tail;
83 char *bol;
84 int len;
85 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
86 * we did not change it).
87 * bit N is used for "interesting" lines, including context.
88 * bit (N+1) is used for "do not show deletion before this".
90 unsigned long flag;
91 unsigned long *p_lno;
94 static char *grab_blob(const unsigned char *sha1, unsigned int mode, unsigned long *size)
96 char *blob;
97 enum object_type type;
99 if (S_ISGITLINK(mode)) {
100 blob = xmalloc(100);
101 *size = snprintf(blob, 100,
102 "Subproject commit %s\n", sha1_to_hex(sha1));
103 } else if (is_null_sha1(sha1)) {
104 /* deleted blob */
105 *size = 0;
106 return xcalloc(1, 1);
107 } else {
108 blob = read_sha1_file(sha1, &type, size);
109 if (type != OBJ_BLOB)
110 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, unsigned int mode,
208 mmfile_t *result_file,
209 struct sline *sline, unsigned int cnt, int n,
210 int num_parent)
212 unsigned int p_lno, lno;
213 unsigned long nmask = (1UL << n);
214 xpparam_t xpp;
215 xdemitconf_t xecfg;
216 mmfile_t parent_file;
217 xdemitcb_t ecb;
218 struct combine_diff_state state;
219 unsigned long sz;
221 if (!cnt)
222 return; /* result deleted */
224 parent_file.ptr = grab_blob(parent, mode, &sz);
225 parent_file.size = sz;
226 xpp.flags = XDF_NEED_MINIMAL;
227 memset(&xecfg, 0, sizeof(xecfg));
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 xdi_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 no_pre_delete = (2UL<<num_parent);
321 unsigned long i;
323 /* Two groups of interesting lines may have a short gap of
324 * uninteresting lines. Connect such groups to give them a
325 * bit of context.
327 * We first start from what the interesting() function says,
328 * and mark them with "mark", and paint context lines with the
329 * mark. So interesting() would still say false for such context
330 * lines but they are treated as "interesting" in the end.
332 i = find_next(sline, mark, 0, cnt, 0);
333 if (cnt < i)
334 return 0;
336 while (i <= cnt) {
337 unsigned long j = (context < i) ? (i - context) : 0;
338 unsigned long k;
340 /* Paint a few lines before the first interesting line. */
341 while (j < i)
342 sline[j++].flag |= mark | no_pre_delete;
344 again:
345 /* we know up to i is to be included. where does the
346 * next uninteresting one start?
348 j = find_next(sline, mark, i, cnt, 1);
349 if (cnt < j)
350 break; /* the rest are all interesting */
352 /* lookahead context lines */
353 k = find_next(sline, mark, j, cnt, 0);
354 j = adjust_hunk_tail(sline, all_mask, i, j);
356 if (k < j + context) {
357 /* k is interesting and [j,k) are not, but
358 * paint them interesting because the gap is small.
360 while (j < k)
361 sline[j++].flag |= mark;
362 i = k;
363 goto again;
366 /* j is the first uninteresting line and there is
367 * no overlap beyond it within context lines. Paint
368 * the trailing edge a bit.
370 i = k;
371 k = (j + context < cnt+1) ? j + context : cnt+1;
372 while (j < k)
373 sline[j++].flag |= mark;
375 return 1;
378 static int make_hunks(struct sline *sline, unsigned long cnt,
379 int num_parent, int dense)
381 unsigned long all_mask = (1UL<<num_parent) - 1;
382 unsigned long mark = (1UL<<num_parent);
383 unsigned long i;
384 int has_interesting = 0;
386 for (i = 0; i <= cnt; i++) {
387 if (interesting(&sline[i], all_mask))
388 sline[i].flag |= mark;
389 else
390 sline[i].flag &= ~mark;
392 if (!dense)
393 return give_context(sline, cnt, num_parent);
395 /* Look at each hunk, and if we have changes from only one
396 * parent, or the changes are the same from all but one
397 * parent, mark that uninteresting.
399 i = 0;
400 while (i <= cnt) {
401 unsigned long j, hunk_begin, hunk_end;
402 unsigned long same_diff;
403 while (i <= cnt && !(sline[i].flag & mark))
404 i++;
405 if (cnt < i)
406 break; /* No more interesting hunks */
407 hunk_begin = i;
408 for (j = i + 1; j <= cnt; j++) {
409 if (!(sline[j].flag & mark)) {
410 /* Look beyond the end to see if there
411 * is an interesting line after this
412 * hunk within context span.
414 unsigned long la; /* lookahead */
415 int contin = 0;
416 la = adjust_hunk_tail(sline, all_mask,
417 hunk_begin, j);
418 la = (la + context < cnt + 1) ?
419 (la + context) : cnt + 1;
420 while (j <= --la) {
421 if (sline[la].flag & mark) {
422 contin = 1;
423 break;
426 if (!contin)
427 break;
428 j = la;
431 hunk_end = j;
433 /* [i..hunk_end) are interesting. Now is it really
434 * interesting? We check if there are only two versions
435 * and the result matches one of them. That is, we look
436 * at:
437 * (+) line, which records lines added to which parents;
438 * this line appears in the result.
439 * (-) line, which records from what parents the line
440 * was removed; this line does not appear in the result.
441 * then check the set of parents the result has difference
442 * from, from all lines. If there are lines that has
443 * different set of parents that the result has differences
444 * from, that means we have more than two versions.
446 * Even when we have only two versions, if the result does
447 * not match any of the parents, the it should be considered
448 * interesting. In such a case, we would have all '+' line.
449 * After passing the above "two versions" test, that would
450 * appear as "the same set of parents" to be "all parents".
452 same_diff = 0;
453 has_interesting = 0;
454 for (j = i; j < hunk_end && !has_interesting; j++) {
455 unsigned long this_diff = sline[j].flag & all_mask;
456 struct lline *ll = sline[j].lost_head;
457 if (this_diff) {
458 /* This has some changes. Is it the
459 * same as others?
461 if (!same_diff)
462 same_diff = this_diff;
463 else if (same_diff != this_diff) {
464 has_interesting = 1;
465 break;
468 while (ll && !has_interesting) {
469 /* Lost this line from these parents;
470 * who are they? Are they the same?
472 this_diff = ll->parent_map;
473 if (!same_diff)
474 same_diff = this_diff;
475 else if (same_diff != this_diff) {
476 has_interesting = 1;
478 ll = ll->next;
482 if (!has_interesting && same_diff != all_mask) {
483 /* This hunk is not that interesting after all */
484 for (j = hunk_begin; j < hunk_end; j++)
485 sline[j].flag &= ~mark;
487 i = hunk_end;
490 has_interesting = give_context(sline, cnt, num_parent);
491 return has_interesting;
494 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
496 l0 = sline[l0].p_lno[n];
497 l1 = sline[l1].p_lno[n];
498 printf(" -%lu,%lu", l0, l1-l0-null_context);
501 static int hunk_comment_line(const char *bol)
503 int ch;
505 if (!bol)
506 return 0;
507 ch = *bol & 0xff;
508 return (isalpha(ch) || ch == '_' || ch == '$');
511 static void show_line_to_eol(const char *line, int len, const char *reset)
513 int saw_cr_at_eol = 0;
514 if (len < 0)
515 len = strlen(line);
516 saw_cr_at_eol = (len && line[len-1] == '\r');
518 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
519 reset,
520 saw_cr_at_eol ? "\r" : "");
523 static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
524 int use_color)
526 unsigned long mark = (1UL<<num_parent);
527 unsigned long no_pre_delete = (2UL<<num_parent);
528 int i;
529 unsigned long lno = 0;
530 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
531 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
532 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
533 const char *c_plain = diff_get_color(use_color, DIFF_PLAIN);
534 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
536 if (!cnt)
537 return; /* result deleted */
539 while (1) {
540 struct sline *sl = &sline[lno];
541 unsigned long hunk_end;
542 unsigned long rlines;
543 const char *hunk_comment = NULL;
544 unsigned long null_context = 0;
546 while (lno <= cnt && !(sline[lno].flag & mark)) {
547 if (hunk_comment_line(sline[lno].bol))
548 hunk_comment = sline[lno].bol;
549 lno++;
551 if (cnt < lno)
552 break;
553 else {
554 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
555 if (!(sline[hunk_end].flag & mark))
556 break;
558 rlines = hunk_end - lno;
559 if (cnt < hunk_end)
560 rlines--; /* pointing at the last delete hunk */
562 if (!context) {
564 * Even when running with --unified=0, all
565 * lines in the hunk needs to be processed in
566 * the loop below in order to show the
567 * deletion recorded in lost_head. However,
568 * we do not want to show the resulting line
569 * with all blank context markers in such a
570 * case. Compensate.
572 unsigned long j;
573 for (j = lno; j < hunk_end; j++)
574 if (!(sline[j].flag & (mark-1)))
575 null_context++;
576 rlines -= null_context;
579 fputs(c_frag, stdout);
580 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
581 for (i = 0; i < num_parent; i++)
582 show_parent_lno(sline, lno, hunk_end, i, null_context);
583 printf(" +%lu,%lu ", lno+1, rlines);
584 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
586 if (hunk_comment) {
587 int comment_end = 0;
588 for (i = 0; i < 40; i++) {
589 int ch = hunk_comment[i] & 0xff;
590 if (!ch || ch == '\n')
591 break;
592 if (!isspace(ch))
593 comment_end = i;
595 if (comment_end)
596 putchar(' ');
597 for (i = 0; i < comment_end; i++)
598 putchar(hunk_comment[i]);
601 printf("%s\n", c_reset);
602 while (lno < hunk_end) {
603 struct lline *ll;
604 int j;
605 unsigned long p_mask;
606 sl = &sline[lno++];
607 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost_head;
608 while (ll) {
609 fputs(c_old, stdout);
610 for (j = 0; j < num_parent; j++) {
611 if (ll->parent_map & (1UL<<j))
612 putchar('-');
613 else
614 putchar(' ');
616 show_line_to_eol(ll->line, -1, c_reset);
617 ll = ll->next;
619 if (cnt < lno)
620 break;
621 p_mask = 1;
622 if (!(sl->flag & (mark-1))) {
624 * This sline was here to hang the
625 * lost lines in front of it.
627 if (!context)
628 continue;
629 fputs(c_plain, stdout);
631 else
632 fputs(c_new, stdout);
633 for (j = 0; j < num_parent; j++) {
634 if (p_mask & sl->flag)
635 putchar('+');
636 else
637 putchar(' ');
638 p_mask <<= 1;
640 show_line_to_eol(sl->bol, sl->len, c_reset);
645 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
646 int i, int j)
648 /* We have already examined parent j and we know parent i
649 * and parent j are the same, so reuse the combined result
650 * of parent j for parent i.
652 unsigned long lno, imask, jmask;
653 imask = (1UL<<i);
654 jmask = (1UL<<j);
656 for (lno = 0; lno <= cnt; lno++) {
657 struct lline *ll = sline->lost_head;
658 sline->p_lno[i] = sline->p_lno[j];
659 while (ll) {
660 if (ll->parent_map & jmask)
661 ll->parent_map |= imask;
662 ll = ll->next;
664 if (sline->flag & jmask)
665 sline->flag |= imask;
666 sline++;
668 /* the overall size of the file (sline[cnt]) */
669 sline->p_lno[i] = sline->p_lno[j];
672 static void dump_quoted_path(const char *head,
673 const char *prefix,
674 const char *path,
675 const char *c_meta, const char *c_reset)
677 static struct strbuf buf = STRBUF_INIT;
679 strbuf_reset(&buf);
680 strbuf_addstr(&buf, c_meta);
681 strbuf_addstr(&buf, head);
682 quote_two_c_style(&buf, prefix, path, 0);
683 strbuf_addstr(&buf, c_reset);
684 puts(buf.buf);
687 static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
688 int dense, struct rev_info *rev)
690 struct diff_options *opt = &rev->diffopt;
691 unsigned long result_size, cnt, lno;
692 char *result, *cp;
693 struct sline *sline; /* survived lines */
694 int mode_differs = 0;
695 int i, show_hunks;
696 int working_tree_file = is_null_sha1(elem->sha1);
697 int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
698 mmfile_t result_file;
700 context = opt->context;
701 /* Read the result of merge first */
702 if (!working_tree_file)
703 result = grab_blob(elem->sha1, elem->mode, &result_size);
704 else {
705 /* Used by diff-tree to read from the working tree */
706 struct stat st;
707 int fd = -1;
709 if (lstat(elem->path, &st) < 0)
710 goto deleted_file;
712 if (S_ISLNK(st.st_mode)) {
713 size_t len = xsize_t(st.st_size);
714 result_size = len;
715 result = xmalloc(len + 1);
716 if (result_size != readlink(elem->path, result, len)) {
717 error("readlink(%s): %s", elem->path,
718 strerror(errno));
719 return;
721 result[len] = 0;
722 elem->mode = canon_mode(st.st_mode);
723 } else if (S_ISDIR(st.st_mode)) {
724 unsigned char sha1[20];
725 if (resolve_gitlink_ref(elem->path, "HEAD", sha1) < 0)
726 result = grab_blob(elem->sha1, elem->mode, &result_size);
727 else
728 result = grab_blob(sha1, elem->mode, &result_size);
729 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
730 size_t len = xsize_t(st.st_size);
731 ssize_t done;
732 int is_file, i;
734 elem->mode = canon_mode(st.st_mode);
735 /* if symlinks don't work, assume symlink if all parents
736 * are symlinks
738 is_file = has_symlinks;
739 for (i = 0; !is_file && i < num_parent; i++)
740 is_file = !S_ISLNK(elem->parent[i].mode);
741 if (!is_file)
742 elem->mode = canon_mode(S_IFLNK);
744 result_size = len;
745 result = xmalloc(len + 1);
747 done = read_in_full(fd, result, len);
748 if (done < 0)
749 die("read error '%s'", elem->path);
750 else if (done < len)
751 die("early EOF '%s'", elem->path);
753 result[len] = 0;
755 /* If not a fake symlink, apply filters, e.g. autocrlf */
756 if (is_file) {
757 struct strbuf buf;
759 strbuf_init(&buf, 0);
760 if (convert_to_git(elem->path, result, len, &buf, safe_crlf)) {
761 free(result);
762 result = strbuf_detach(&buf, &len);
763 result_size = len;
767 else {
768 deleted_file:
769 result_size = 0;
770 elem->mode = 0;
771 result = xcalloc(1, 1);
774 if (0 <= fd)
775 close(fd);
778 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
779 if (*cp == '\n')
780 cnt++;
782 if (result_size && result[result_size-1] != '\n')
783 cnt++; /* incomplete line */
785 sline = xcalloc(cnt+2, sizeof(*sline));
786 sline[0].bol = result;
787 for (lno = 0; lno <= cnt + 1; lno++) {
788 sline[lno].lost_tail = &sline[lno].lost_head;
789 sline[lno].flag = 0;
791 for (lno = 0, cp = result; cp < result + result_size; cp++) {
792 if (*cp == '\n') {
793 sline[lno].len = cp - sline[lno].bol;
794 lno++;
795 if (lno < cnt)
796 sline[lno].bol = cp + 1;
799 if (result_size && result[result_size-1] != '\n')
800 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
802 result_file.ptr = result;
803 result_file.size = result_size;
805 /* Even p_lno[cnt+1] is valid -- that is for the end line number
806 * for deletion hunk at the end.
808 sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
809 for (lno = 0; lno <= cnt; lno++)
810 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
812 for (i = 0; i < num_parent; i++) {
813 int j;
814 for (j = 0; j < i; j++) {
815 if (!hashcmp(elem->parent[i].sha1,
816 elem->parent[j].sha1)) {
817 reuse_combine_diff(sline, cnt, i, j);
818 break;
821 if (i <= j)
822 combine_diff(elem->parent[i].sha1,
823 elem->parent[i].mode,
824 &result_file, sline,
825 cnt, i, num_parent);
826 if (elem->parent[i].mode != elem->mode)
827 mode_differs = 1;
830 show_hunks = make_hunks(sline, cnt, num_parent, dense);
832 if (show_hunks || mode_differs || working_tree_file) {
833 const char *abb;
834 int use_color = DIFF_OPT_TST(opt, COLOR_DIFF);
835 const char *c_meta = diff_get_color(use_color, DIFF_METAINFO);
836 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
837 int added = 0;
838 int deleted = 0;
840 if (rev->loginfo && !rev->no_commit_id)
841 show_log(rev);
842 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
843 "", elem->path, c_meta, c_reset);
844 printf("%sindex ", c_meta);
845 for (i = 0; i < num_parent; i++) {
846 abb = find_unique_abbrev(elem->parent[i].sha1,
847 abbrev);
848 printf("%s%s", i ? "," : "", abb);
850 abb = find_unique_abbrev(elem->sha1, abbrev);
851 printf("..%s%s\n", abb, c_reset);
853 if (mode_differs) {
854 deleted = !elem->mode;
856 /* We say it was added if nobody had it */
857 added = !deleted;
858 for (i = 0; added && i < num_parent; i++)
859 if (elem->parent[i].status !=
860 DIFF_STATUS_ADDED)
861 added = 0;
862 if (added)
863 printf("%snew file mode %06o",
864 c_meta, elem->mode);
865 else {
866 if (deleted)
867 printf("%sdeleted file ", c_meta);
868 printf("mode ");
869 for (i = 0; i < num_parent; i++) {
870 printf("%s%06o", i ? "," : "",
871 elem->parent[i].mode);
873 if (elem->mode)
874 printf("..%06o", elem->mode);
876 printf("%s\n", c_reset);
878 if (added)
879 dump_quoted_path("--- ", "", "/dev/null",
880 c_meta, c_reset);
881 else
882 dump_quoted_path("--- ", opt->a_prefix, elem->path,
883 c_meta, c_reset);
884 if (deleted)
885 dump_quoted_path("+++ ", "", "/dev/null",
886 c_meta, c_reset);
887 else
888 dump_quoted_path("+++ ", opt->b_prefix, elem->path,
889 c_meta, c_reset);
890 dump_sline(sline, cnt, num_parent,
891 DIFF_OPT_TST(opt, COLOR_DIFF));
893 free(result);
895 for (lno = 0; lno < cnt; lno++) {
896 if (sline[lno].lost_head) {
897 struct lline *ll = sline[lno].lost_head;
898 while (ll) {
899 struct lline *tmp = ll;
900 ll = ll->next;
901 free(tmp);
905 free(sline[0].p_lno);
906 free(sline);
909 #define COLONS "::::::::::::::::::::::::::::::::"
911 static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
913 struct diff_options *opt = &rev->diffopt;
914 int i, offset;
915 const char *prefix;
916 int line_termination, inter_name_termination;
918 line_termination = opt->line_termination;
919 inter_name_termination = '\t';
920 if (!line_termination)
921 inter_name_termination = 0;
923 if (rev->loginfo && !rev->no_commit_id)
924 show_log(rev);
926 if (opt->output_format & DIFF_FORMAT_RAW) {
927 offset = strlen(COLONS) - num_parent;
928 if (offset < 0)
929 offset = 0;
930 prefix = COLONS + offset;
932 /* Show the modes */
933 for (i = 0; i < num_parent; i++) {
934 printf("%s%06o", prefix, p->parent[i].mode);
935 prefix = " ";
937 printf("%s%06o", prefix, p->mode);
939 /* Show sha1's */
940 for (i = 0; i < num_parent; i++)
941 printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
942 opt->abbrev));
943 printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
946 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
947 for (i = 0; i < num_parent; i++)
948 putchar(p->parent[i].status);
949 putchar(inter_name_termination);
952 write_name_quoted(p->path, stdout, line_termination);
955 void show_combined_diff(struct combine_diff_path *p,
956 int num_parent,
957 int dense,
958 struct rev_info *rev)
960 struct diff_options *opt = &rev->diffopt;
961 if (!p->len)
962 return;
963 if (opt->output_format & (DIFF_FORMAT_RAW |
964 DIFF_FORMAT_NAME |
965 DIFF_FORMAT_NAME_STATUS))
966 show_raw_diff(p, num_parent, rev);
967 else if (opt->output_format & DIFF_FORMAT_PATCH)
968 show_patch_diff(p, num_parent, dense, rev);
971 void diff_tree_combined(const unsigned char *sha1,
972 const unsigned char parent[][20],
973 int num_parent,
974 int dense,
975 struct rev_info *rev)
977 struct diff_options *opt = &rev->diffopt;
978 struct diff_options diffopts;
979 struct combine_diff_path *p, *paths = NULL;
980 int i, num_paths, needsep, show_log_first;
982 diffopts = *opt;
983 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
984 DIFF_OPT_SET(&diffopts, RECURSIVE);
985 DIFF_OPT_CLR(&diffopts, ALLOW_EXTERNAL);
987 show_log_first = !!rev->loginfo && !rev->no_commit_id;
988 needsep = 0;
989 /* find set of paths that everybody touches */
990 for (i = 0; i < num_parent; i++) {
991 /* show stat against the first parent even
992 * when doing combined diff.
994 int stat_opt = (opt->output_format &
995 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
996 if (i == 0 && stat_opt)
997 diffopts.output_format = stat_opt;
998 else
999 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
1000 diff_tree_sha1(parent[i], sha1, "", &diffopts);
1001 diffcore_std(&diffopts);
1002 paths = intersect_paths(paths, i, num_parent);
1004 if (show_log_first && i == 0) {
1005 show_log(rev);
1006 if (rev->verbose_header && opt->output_format)
1007 putchar(opt->line_termination);
1009 diff_flush(&diffopts);
1012 /* find out surviving paths */
1013 for (num_paths = 0, p = paths; p; p = p->next) {
1014 if (p->len)
1015 num_paths++;
1017 if (num_paths) {
1018 if (opt->output_format & (DIFF_FORMAT_RAW |
1019 DIFF_FORMAT_NAME |
1020 DIFF_FORMAT_NAME_STATUS)) {
1021 for (p = paths; p; p = p->next) {
1022 if (p->len)
1023 show_raw_diff(p, num_parent, rev);
1025 needsep = 1;
1027 else if (opt->output_format &
1028 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
1029 needsep = 1;
1030 if (opt->output_format & DIFF_FORMAT_PATCH) {
1031 if (needsep)
1032 putchar(opt->line_termination);
1033 for (p = paths; p; p = p->next) {
1034 if (p->len)
1035 show_patch_diff(p, num_parent, dense,
1036 rev);
1041 /* Clean things up */
1042 while (paths) {
1043 struct combine_diff_path *tmp = paths;
1044 paths = paths->next;
1045 free(tmp);
1049 void diff_tree_combined_merge(const unsigned char *sha1,
1050 int dense, struct rev_info *rev)
1052 int num_parent;
1053 const unsigned char (*parent)[20];
1054 struct commit *commit = lookup_commit(sha1);
1055 struct commit_list *parents;
1057 /* count parents */
1058 for (parents = commit->parents, num_parent = 0;
1059 parents;
1060 parents = parents->next, num_parent++)
1061 ; /* nothing */
1063 parent = xmalloc(num_parent * sizeof(*parent));
1064 for (parents = commit->parents, num_parent = 0;
1065 parents;
1066 parents = parents->next, num_parent++)
1067 hashcpy((unsigned char*)(parent + num_parent),
1068 parents->item->object.sha1);
1069 diff_tree_combined(sha1, parent, num_parent, dense, rev);