combine-diff.c: fix performance problem when folding common deleted lines
[git/dkf.git] / combine-diff.c
blobb82f46cc60733aded42ebd95c0163c16d7aaaa1c
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 struct lline *next_lost;
84 char *bol;
85 int len;
86 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
87 * we did not change it).
88 * bit N is used for "interesting" lines, including context.
89 * bit (N+1) is used for "do not show deletion before this".
91 unsigned long flag;
92 unsigned long *p_lno;
95 static char *grab_blob(const unsigned char *sha1, unsigned int mode, unsigned long *size)
97 char *blob;
98 enum object_type type;
100 if (S_ISGITLINK(mode)) {
101 blob = xmalloc(100);
102 *size = snprintf(blob, 100,
103 "Subproject commit %s\n", sha1_to_hex(sha1));
104 } else if (is_null_sha1(sha1)) {
105 /* deleted blob */
106 *size = 0;
107 return xcalloc(1, 1);
108 } else {
109 blob = read_sha1_file(sha1, &type, size);
110 if (type != OBJ_BLOB)
111 die("object '%s' is not a blob!", sha1_to_hex(sha1));
113 return blob;
116 static void append_lost(struct sline *sline, int n, const char *line, int len)
118 struct lline *lline;
119 unsigned long this_mask = (1UL<<n);
120 if (line[len-1] == '\n')
121 len--;
123 /* Check to see if we can squash things */
124 if (sline->lost_head) {
125 lline = sline->next_lost;
126 while (lline) {
127 if (lline->len == len &&
128 !memcmp(lline->line, line, len)) {
129 lline->parent_map |= this_mask;
130 sline->next_lost = lline->next;
131 return;
133 lline = lline->next;
137 lline = xmalloc(sizeof(*lline) + len + 1);
138 lline->len = len;
139 lline->next = NULL;
140 lline->parent_map = this_mask;
141 memcpy(lline->line, line, len);
142 lline->line[len] = 0;
143 *sline->lost_tail = lline;
144 sline->lost_tail = &lline->next;
145 sline->next_lost = NULL;
148 struct combine_diff_state {
149 unsigned int lno;
150 int ob, on, nb, nn;
151 unsigned long nmask;
152 int num_parent;
153 int n;
154 struct sline *sline;
155 struct sline *lost_bucket;
158 static void consume_line(void *state_, char *line, unsigned long len)
160 struct combine_diff_state *state = state_;
161 if (5 < len && !memcmp("@@ -", line, 4)) {
162 if (parse_hunk_header(line, len,
163 &state->ob, &state->on,
164 &state->nb, &state->nn))
165 return;
166 state->lno = state->nb;
167 if (!state->nb)
168 /* @@ -1,2 +0,0 @@ to remove the
169 * first two lines...
171 state->nb = 1;
172 if (state->nn == 0)
173 /* @@ -X,Y +N,0 @@ removed Y lines
174 * that would have come *after* line N
175 * in the result. Our lost buckets hang
176 * to the line after the removed lines,
178 state->lost_bucket = &state->sline[state->nb];
179 else
180 state->lost_bucket = &state->sline[state->nb-1];
181 if (!state->sline[state->nb-1].p_lno)
182 state->sline[state->nb-1].p_lno =
183 xcalloc(state->num_parent,
184 sizeof(unsigned long));
185 state->sline[state->nb-1].p_lno[state->n] = state->ob;
186 state->lost_bucket->next_lost = state->lost_bucket->lost_head;
187 return;
189 if (!state->lost_bucket)
190 return; /* not in any hunk yet */
191 switch (line[0]) {
192 case '-':
193 append_lost(state->lost_bucket, state->n, line+1, len-1);
194 break;
195 case '+':
196 state->sline[state->lno-1].flag |= state->nmask;
197 state->lno++;
198 break;
202 static void combine_diff(const unsigned char *parent, unsigned int mode,
203 mmfile_t *result_file,
204 struct sline *sline, unsigned int cnt, int n,
205 int num_parent)
207 unsigned int p_lno, lno;
208 unsigned long nmask = (1UL << n);
209 xpparam_t xpp;
210 xdemitconf_t xecfg;
211 mmfile_t parent_file;
212 xdemitcb_t ecb;
213 struct combine_diff_state state;
214 unsigned long sz;
216 if (!cnt)
217 return; /* result deleted */
219 parent_file.ptr = grab_blob(parent, mode, &sz);
220 parent_file.size = sz;
221 memset(&xpp, 0, sizeof(xpp));
222 xpp.flags = XDF_NEED_MINIMAL;
223 memset(&xecfg, 0, sizeof(xecfg));
224 memset(&state, 0, sizeof(state));
225 state.nmask = nmask;
226 state.sline = sline;
227 state.lno = 1;
228 state.num_parent = num_parent;
229 state.n = n;
231 xdi_diff_outf(&parent_file, result_file, consume_line, &state,
232 &xpp, &xecfg, &ecb);
233 free(parent_file.ptr);
235 /* Assign line numbers for this parent.
237 * sline[lno].p_lno[n] records the first line number
238 * (counting from 1) for parent N if the final hunk display
239 * started by showing sline[lno] (possibly showing the lost
240 * lines attached to it first).
242 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
243 struct lline *ll;
244 sline[lno].p_lno[n] = p_lno;
246 /* How many lines would this sline advance the p_lno? */
247 ll = sline[lno].lost_head;
248 while (ll) {
249 if (ll->parent_map & nmask)
250 p_lno++; /* '-' means parent had it */
251 ll = ll->next;
253 if (lno < cnt && !(sline[lno].flag & nmask))
254 p_lno++; /* no '+' means parent had it */
256 sline[lno].p_lno[n] = p_lno; /* trailer */
259 static unsigned long context = 3;
260 static char combine_marker = '@';
262 static int interesting(struct sline *sline, unsigned long all_mask)
264 /* If some parents lost lines here, or if we have added to
265 * some parent, it is interesting.
267 return ((sline->flag & all_mask) || sline->lost_head);
270 static unsigned long adjust_hunk_tail(struct sline *sline,
271 unsigned long all_mask,
272 unsigned long hunk_begin,
273 unsigned long i)
275 /* i points at the first uninteresting line. If the last line
276 * of the hunk was interesting only because it has some
277 * deletion, then it is not all that interesting for the
278 * purpose of giving trailing context lines. This is because
279 * we output '-' line and then unmodified sline[i-1] itself in
280 * that case which gives us one extra context line.
282 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
283 i--;
284 return i;
287 static unsigned long find_next(struct sline *sline,
288 unsigned long mark,
289 unsigned long i,
290 unsigned long cnt,
291 int look_for_uninteresting)
293 /* We have examined up to i-1 and are about to look at i.
294 * Find next interesting or uninteresting line. Here,
295 * "interesting" does not mean interesting(), but marked by
296 * the give_context() function below (i.e. it includes context
297 * lines that are not interesting to interesting() function
298 * that are surrounded by interesting() ones.
300 while (i <= cnt)
301 if (look_for_uninteresting
302 ? !(sline[i].flag & mark)
303 : (sline[i].flag & mark))
304 return i;
305 else
306 i++;
307 return i;
310 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
312 unsigned long all_mask = (1UL<<num_parent) - 1;
313 unsigned long mark = (1UL<<num_parent);
314 unsigned long no_pre_delete = (2UL<<num_parent);
315 unsigned long i;
317 /* Two groups of interesting lines may have a short gap of
318 * uninteresting lines. Connect such groups to give them a
319 * bit of context.
321 * We first start from what the interesting() function says,
322 * and mark them with "mark", and paint context lines with the
323 * mark. So interesting() would still say false for such context
324 * lines but they are treated as "interesting" in the end.
326 i = find_next(sline, mark, 0, cnt, 0);
327 if (cnt < i)
328 return 0;
330 while (i <= cnt) {
331 unsigned long j = (context < i) ? (i - context) : 0;
332 unsigned long k;
334 /* Paint a few lines before the first interesting line. */
335 while (j < i)
336 sline[j++].flag |= mark | no_pre_delete;
338 again:
339 /* we know up to i is to be included. where does the
340 * next uninteresting one start?
342 j = find_next(sline, mark, i, cnt, 1);
343 if (cnt < j)
344 break; /* the rest are all interesting */
346 /* lookahead context lines */
347 k = find_next(sline, mark, j, cnt, 0);
348 j = adjust_hunk_tail(sline, all_mask, i, j);
350 if (k < j + context) {
351 /* k is interesting and [j,k) are not, but
352 * paint them interesting because the gap is small.
354 while (j < k)
355 sline[j++].flag |= mark;
356 i = k;
357 goto again;
360 /* j is the first uninteresting line and there is
361 * no overlap beyond it within context lines. Paint
362 * the trailing edge a bit.
364 i = k;
365 k = (j + context < cnt+1) ? j + context : cnt+1;
366 while (j < k)
367 sline[j++].flag |= mark;
369 return 1;
372 static int make_hunks(struct sline *sline, unsigned long cnt,
373 int num_parent, int dense)
375 unsigned long all_mask = (1UL<<num_parent) - 1;
376 unsigned long mark = (1UL<<num_parent);
377 unsigned long i;
378 int has_interesting = 0;
380 for (i = 0; i <= cnt; i++) {
381 if (interesting(&sline[i], all_mask))
382 sline[i].flag |= mark;
383 else
384 sline[i].flag &= ~mark;
386 if (!dense)
387 return give_context(sline, cnt, num_parent);
389 /* Look at each hunk, and if we have changes from only one
390 * parent, or the changes are the same from all but one
391 * parent, mark that uninteresting.
393 i = 0;
394 while (i <= cnt) {
395 unsigned long j, hunk_begin, hunk_end;
396 unsigned long same_diff;
397 while (i <= cnt && !(sline[i].flag & mark))
398 i++;
399 if (cnt < i)
400 break; /* No more interesting hunks */
401 hunk_begin = i;
402 for (j = i + 1; j <= cnt; j++) {
403 if (!(sline[j].flag & mark)) {
404 /* Look beyond the end to see if there
405 * is an interesting line after this
406 * hunk within context span.
408 unsigned long la; /* lookahead */
409 int contin = 0;
410 la = adjust_hunk_tail(sline, all_mask,
411 hunk_begin, j);
412 la = (la + context < cnt + 1) ?
413 (la + context) : cnt + 1;
414 while (j <= --la) {
415 if (sline[la].flag & mark) {
416 contin = 1;
417 break;
420 if (!contin)
421 break;
422 j = la;
425 hunk_end = j;
427 /* [i..hunk_end) are interesting. Now is it really
428 * interesting? We check if there are only two versions
429 * and the result matches one of them. That is, we look
430 * at:
431 * (+) line, which records lines added to which parents;
432 * this line appears in the result.
433 * (-) line, which records from what parents the line
434 * was removed; this line does not appear in the result.
435 * then check the set of parents the result has difference
436 * from, from all lines. If there are lines that has
437 * different set of parents that the result has differences
438 * from, that means we have more than two versions.
440 * Even when we have only two versions, if the result does
441 * not match any of the parents, the it should be considered
442 * interesting. In such a case, we would have all '+' line.
443 * After passing the above "two versions" test, that would
444 * appear as "the same set of parents" to be "all parents".
446 same_diff = 0;
447 has_interesting = 0;
448 for (j = i; j < hunk_end && !has_interesting; j++) {
449 unsigned long this_diff = sline[j].flag & all_mask;
450 struct lline *ll = sline[j].lost_head;
451 if (this_diff) {
452 /* This has some changes. Is it the
453 * same as others?
455 if (!same_diff)
456 same_diff = this_diff;
457 else if (same_diff != this_diff) {
458 has_interesting = 1;
459 break;
462 while (ll && !has_interesting) {
463 /* Lost this line from these parents;
464 * who are they? Are they the same?
466 this_diff = ll->parent_map;
467 if (!same_diff)
468 same_diff = this_diff;
469 else if (same_diff != this_diff) {
470 has_interesting = 1;
472 ll = ll->next;
476 if (!has_interesting && same_diff != all_mask) {
477 /* This hunk is not that interesting after all */
478 for (j = hunk_begin; j < hunk_end; j++)
479 sline[j].flag &= ~mark;
481 i = hunk_end;
484 has_interesting = give_context(sline, cnt, num_parent);
485 return has_interesting;
488 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
490 l0 = sline[l0].p_lno[n];
491 l1 = sline[l1].p_lno[n];
492 printf(" -%lu,%lu", l0, l1-l0-null_context);
495 static int hunk_comment_line(const char *bol)
497 int ch;
499 if (!bol)
500 return 0;
501 ch = *bol & 0xff;
502 return (isalpha(ch) || ch == '_' || ch == '$');
505 static void show_line_to_eol(const char *line, int len, const char *reset)
507 int saw_cr_at_eol = 0;
508 if (len < 0)
509 len = strlen(line);
510 saw_cr_at_eol = (len && line[len-1] == '\r');
512 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
513 reset,
514 saw_cr_at_eol ? "\r" : "");
517 static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
518 int use_color)
520 unsigned long mark = (1UL<<num_parent);
521 unsigned long no_pre_delete = (2UL<<num_parent);
522 int i;
523 unsigned long lno = 0;
524 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
525 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
526 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
527 const char *c_plain = diff_get_color(use_color, DIFF_PLAIN);
528 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
530 if (!cnt)
531 return; /* result deleted */
533 while (1) {
534 unsigned long hunk_end;
535 unsigned long rlines;
536 const char *hunk_comment = NULL;
537 unsigned long null_context = 0;
539 while (lno <= cnt && !(sline[lno].flag & mark)) {
540 if (hunk_comment_line(sline[lno].bol))
541 hunk_comment = sline[lno].bol;
542 lno++;
544 if (cnt < lno)
545 break;
546 else {
547 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
548 if (!(sline[hunk_end].flag & mark))
549 break;
551 rlines = hunk_end - lno;
552 if (cnt < hunk_end)
553 rlines--; /* pointing at the last delete hunk */
555 if (!context) {
557 * Even when running with --unified=0, all
558 * lines in the hunk needs to be processed in
559 * the loop below in order to show the
560 * deletion recorded in lost_head. However,
561 * we do not want to show the resulting line
562 * with all blank context markers in such a
563 * case. Compensate.
565 unsigned long j;
566 for (j = lno; j < hunk_end; j++)
567 if (!(sline[j].flag & (mark-1)))
568 null_context++;
569 rlines -= null_context;
572 fputs(c_frag, stdout);
573 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
574 for (i = 0; i < num_parent; i++)
575 show_parent_lno(sline, lno, hunk_end, i, null_context);
576 printf(" +%lu,%lu ", lno+1, rlines);
577 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
579 if (hunk_comment) {
580 int comment_end = 0;
581 for (i = 0; i < 40; i++) {
582 int ch = hunk_comment[i] & 0xff;
583 if (!ch || ch == '\n')
584 break;
585 if (!isspace(ch))
586 comment_end = i;
588 if (comment_end)
589 putchar(' ');
590 for (i = 0; i < comment_end; i++)
591 putchar(hunk_comment[i]);
594 printf("%s\n", c_reset);
595 while (lno < hunk_end) {
596 struct lline *ll;
597 int j;
598 unsigned long p_mask;
599 struct sline *sl = &sline[lno++];
600 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost_head;
601 while (ll) {
602 fputs(c_old, stdout);
603 for (j = 0; j < num_parent; j++) {
604 if (ll->parent_map & (1UL<<j))
605 putchar('-');
606 else
607 putchar(' ');
609 show_line_to_eol(ll->line, -1, c_reset);
610 ll = ll->next;
612 if (cnt < lno)
613 break;
614 p_mask = 1;
615 if (!(sl->flag & (mark-1))) {
617 * This sline was here to hang the
618 * lost lines in front of it.
620 if (!context)
621 continue;
622 fputs(c_plain, stdout);
624 else
625 fputs(c_new, stdout);
626 for (j = 0; j < num_parent; j++) {
627 if (p_mask & sl->flag)
628 putchar('+');
629 else
630 putchar(' ');
631 p_mask <<= 1;
633 show_line_to_eol(sl->bol, sl->len, c_reset);
638 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
639 int i, int j)
641 /* We have already examined parent j and we know parent i
642 * and parent j are the same, so reuse the combined result
643 * of parent j for parent i.
645 unsigned long lno, imask, jmask;
646 imask = (1UL<<i);
647 jmask = (1UL<<j);
649 for (lno = 0; lno <= cnt; lno++) {
650 struct lline *ll = sline->lost_head;
651 sline->p_lno[i] = sline->p_lno[j];
652 while (ll) {
653 if (ll->parent_map & jmask)
654 ll->parent_map |= imask;
655 ll = ll->next;
657 if (sline->flag & jmask)
658 sline->flag |= imask;
659 sline++;
661 /* the overall size of the file (sline[cnt]) */
662 sline->p_lno[i] = sline->p_lno[j];
665 static void dump_quoted_path(const char *head,
666 const char *prefix,
667 const char *path,
668 const char *c_meta, const char *c_reset)
670 static struct strbuf buf = STRBUF_INIT;
672 strbuf_reset(&buf);
673 strbuf_addstr(&buf, c_meta);
674 strbuf_addstr(&buf, head);
675 quote_two_c_style(&buf, prefix, path, 0);
676 strbuf_addstr(&buf, c_reset);
677 puts(buf.buf);
680 static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
681 int dense, struct rev_info *rev)
683 struct diff_options *opt = &rev->diffopt;
684 unsigned long result_size, cnt, lno;
685 char *result, *cp;
686 struct sline *sline; /* survived lines */
687 int mode_differs = 0;
688 int i, show_hunks;
689 int working_tree_file = is_null_sha1(elem->sha1);
690 int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
691 const char *a_prefix, *b_prefix;
692 mmfile_t result_file;
694 context = opt->context;
695 a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
696 b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
698 /* Read the result of merge first */
699 if (!working_tree_file)
700 result = grab_blob(elem->sha1, elem->mode, &result_size);
701 else {
702 /* Used by diff-tree to read from the working tree */
703 struct stat st;
704 int fd = -1;
706 if (lstat(elem->path, &st) < 0)
707 goto deleted_file;
709 if (S_ISLNK(st.st_mode)) {
710 struct strbuf buf = STRBUF_INIT;
712 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
713 error("readlink(%s): %s", elem->path,
714 strerror(errno));
715 return;
717 result_size = buf.len;
718 result = strbuf_detach(&buf, NULL);
719 elem->mode = canon_mode(st.st_mode);
720 } else if (S_ISDIR(st.st_mode)) {
721 unsigned char sha1[20];
722 if (resolve_gitlink_ref(elem->path, "HEAD", sha1) < 0)
723 result = grab_blob(elem->sha1, elem->mode, &result_size);
724 else
725 result = grab_blob(sha1, elem->mode, &result_size);
726 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
727 size_t len = xsize_t(st.st_size);
728 ssize_t done;
729 int is_file, i;
731 elem->mode = canon_mode(st.st_mode);
732 /* if symlinks don't work, assume symlink if all parents
733 * are symlinks
735 is_file = has_symlinks;
736 for (i = 0; !is_file && i < num_parent; i++)
737 is_file = !S_ISLNK(elem->parent[i].mode);
738 if (!is_file)
739 elem->mode = canon_mode(S_IFLNK);
741 result_size = len;
742 result = xmalloc(len + 1);
744 done = read_in_full(fd, result, len);
745 if (done < 0)
746 die("read error '%s'", elem->path);
747 else if (done < len)
748 die("early EOF '%s'", elem->path);
750 result[len] = 0;
752 /* If not a fake symlink, apply filters, e.g. autocrlf */
753 if (is_file) {
754 struct strbuf buf = STRBUF_INIT;
756 if (convert_to_git(elem->path, result, len, &buf, safe_crlf)) {
757 free(result);
758 result = strbuf_detach(&buf, &len);
759 result_size = len;
763 else {
764 deleted_file:
765 result_size = 0;
766 elem->mode = 0;
767 result = xcalloc(1, 1);
770 if (0 <= fd)
771 close(fd);
774 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
775 if (*cp == '\n')
776 cnt++;
778 if (result_size && result[result_size-1] != '\n')
779 cnt++; /* incomplete line */
781 sline = xcalloc(cnt+2, sizeof(*sline));
782 sline[0].bol = result;
783 for (lno = 0; lno <= cnt + 1; lno++) {
784 sline[lno].lost_tail = &sline[lno].lost_head;
785 sline[lno].flag = 0;
787 for (lno = 0, cp = result; cp < result + result_size; cp++) {
788 if (*cp == '\n') {
789 sline[lno].len = cp - sline[lno].bol;
790 lno++;
791 if (lno < cnt)
792 sline[lno].bol = cp + 1;
795 if (result_size && result[result_size-1] != '\n')
796 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
798 result_file.ptr = result;
799 result_file.size = result_size;
801 /* Even p_lno[cnt+1] is valid -- that is for the end line number
802 * for deletion hunk at the end.
804 sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
805 for (lno = 0; lno <= cnt; lno++)
806 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
808 for (i = 0; i < num_parent; i++) {
809 int j;
810 for (j = 0; j < i; j++) {
811 if (!hashcmp(elem->parent[i].sha1,
812 elem->parent[j].sha1)) {
813 reuse_combine_diff(sline, cnt, i, j);
814 break;
817 if (i <= j)
818 combine_diff(elem->parent[i].sha1,
819 elem->parent[i].mode,
820 &result_file, sline,
821 cnt, i, num_parent);
822 if (elem->parent[i].mode != elem->mode)
823 mode_differs = 1;
826 show_hunks = make_hunks(sline, cnt, num_parent, dense);
828 if (show_hunks || mode_differs || working_tree_file) {
829 const char *abb;
830 int use_color = DIFF_OPT_TST(opt, COLOR_DIFF);
831 const char *c_meta = diff_get_color(use_color, DIFF_METAINFO);
832 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
833 int added = 0;
834 int deleted = 0;
836 if (rev->loginfo && !rev->no_commit_id)
837 show_log(rev);
838 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
839 "", elem->path, c_meta, c_reset);
840 printf("%sindex ", c_meta);
841 for (i = 0; i < num_parent; i++) {
842 abb = find_unique_abbrev(elem->parent[i].sha1,
843 abbrev);
844 printf("%s%s", i ? "," : "", abb);
846 abb = find_unique_abbrev(elem->sha1, abbrev);
847 printf("..%s%s\n", abb, c_reset);
849 if (mode_differs) {
850 deleted = !elem->mode;
852 /* We say it was added if nobody had it */
853 added = !deleted;
854 for (i = 0; added && i < num_parent; i++)
855 if (elem->parent[i].status !=
856 DIFF_STATUS_ADDED)
857 added = 0;
858 if (added)
859 printf("%snew file mode %06o",
860 c_meta, elem->mode);
861 else {
862 if (deleted)
863 printf("%sdeleted file ", c_meta);
864 printf("mode ");
865 for (i = 0; i < num_parent; i++) {
866 printf("%s%06o", i ? "," : "",
867 elem->parent[i].mode);
869 if (elem->mode)
870 printf("..%06o", elem->mode);
872 printf("%s\n", c_reset);
874 if (added)
875 dump_quoted_path("--- ", "", "/dev/null",
876 c_meta, c_reset);
877 else
878 dump_quoted_path("--- ", a_prefix, elem->path,
879 c_meta, c_reset);
880 if (deleted)
881 dump_quoted_path("+++ ", "", "/dev/null",
882 c_meta, c_reset);
883 else
884 dump_quoted_path("+++ ", b_prefix, elem->path,
885 c_meta, c_reset);
886 dump_sline(sline, cnt, num_parent,
887 DIFF_OPT_TST(opt, COLOR_DIFF));
889 free(result);
891 for (lno = 0; lno < cnt; lno++) {
892 if (sline[lno].lost_head) {
893 struct lline *ll = sline[lno].lost_head;
894 while (ll) {
895 struct lline *tmp = ll;
896 ll = ll->next;
897 free(tmp);
901 free(sline[0].p_lno);
902 free(sline);
905 #define COLONS "::::::::::::::::::::::::::::::::"
907 static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
909 struct diff_options *opt = &rev->diffopt;
910 int i, offset;
911 const char *prefix;
912 int line_termination, inter_name_termination;
914 line_termination = opt->line_termination;
915 inter_name_termination = '\t';
916 if (!line_termination)
917 inter_name_termination = 0;
919 if (rev->loginfo && !rev->no_commit_id)
920 show_log(rev);
922 if (opt->output_format & DIFF_FORMAT_RAW) {
923 offset = strlen(COLONS) - num_parent;
924 if (offset < 0)
925 offset = 0;
926 prefix = COLONS + offset;
928 /* Show the modes */
929 for (i = 0; i < num_parent; i++) {
930 printf("%s%06o", prefix, p->parent[i].mode);
931 prefix = " ";
933 printf("%s%06o", prefix, p->mode);
935 /* Show sha1's */
936 for (i = 0; i < num_parent; i++)
937 printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
938 opt->abbrev));
939 printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
942 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
943 for (i = 0; i < num_parent; i++)
944 putchar(p->parent[i].status);
945 putchar(inter_name_termination);
948 write_name_quoted(p->path, stdout, line_termination);
951 void show_combined_diff(struct combine_diff_path *p,
952 int num_parent,
953 int dense,
954 struct rev_info *rev)
956 struct diff_options *opt = &rev->diffopt;
957 if (!p->len)
958 return;
959 if (opt->output_format & (DIFF_FORMAT_RAW |
960 DIFF_FORMAT_NAME |
961 DIFF_FORMAT_NAME_STATUS))
962 show_raw_diff(p, num_parent, rev);
963 else if (opt->output_format & DIFF_FORMAT_PATCH)
964 show_patch_diff(p, num_parent, dense, rev);
967 void diff_tree_combined(const unsigned char *sha1,
968 const unsigned char parent[][20],
969 int num_parent,
970 int dense,
971 struct rev_info *rev)
973 struct diff_options *opt = &rev->diffopt;
974 struct diff_options diffopts;
975 struct combine_diff_path *p, *paths = NULL;
976 int i, num_paths, needsep, show_log_first;
978 diffopts = *opt;
979 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
980 DIFF_OPT_SET(&diffopts, RECURSIVE);
981 DIFF_OPT_CLR(&diffopts, ALLOW_EXTERNAL);
983 show_log_first = !!rev->loginfo && !rev->no_commit_id;
984 needsep = 0;
985 /* find set of paths that everybody touches */
986 for (i = 0; i < num_parent; i++) {
987 /* show stat against the first parent even
988 * when doing combined diff.
990 int stat_opt = (opt->output_format &
991 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
992 if (i == 0 && stat_opt)
993 diffopts.output_format = stat_opt;
994 else
995 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
996 diff_tree_sha1(parent[i], sha1, "", &diffopts);
997 diffcore_std(&diffopts);
998 paths = intersect_paths(paths, i, num_parent);
1000 if (show_log_first && i == 0) {
1001 show_log(rev);
1002 if (rev->verbose_header && opt->output_format)
1003 putchar(opt->line_termination);
1005 diff_flush(&diffopts);
1008 /* find out surviving paths */
1009 for (num_paths = 0, p = paths; p; p = p->next) {
1010 if (p->len)
1011 num_paths++;
1013 if (num_paths) {
1014 if (opt->output_format & (DIFF_FORMAT_RAW |
1015 DIFF_FORMAT_NAME |
1016 DIFF_FORMAT_NAME_STATUS)) {
1017 for (p = paths; p; p = p->next) {
1018 if (p->len)
1019 show_raw_diff(p, num_parent, rev);
1021 needsep = 1;
1023 else if (opt->output_format &
1024 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
1025 needsep = 1;
1026 if (opt->output_format & DIFF_FORMAT_PATCH) {
1027 if (needsep)
1028 putchar(opt->line_termination);
1029 for (p = paths; p; p = p->next) {
1030 if (p->len)
1031 show_patch_diff(p, num_parent, dense,
1032 rev);
1037 /* Clean things up */
1038 while (paths) {
1039 struct combine_diff_path *tmp = paths;
1040 paths = paths->next;
1041 free(tmp);
1045 void diff_tree_combined_merge(const unsigned char *sha1,
1046 int dense, struct rev_info *rev)
1048 int num_parent;
1049 const unsigned char (*parent)[20];
1050 struct commit *commit = lookup_commit(sha1);
1051 struct commit_list *parents;
1053 /* count parents */
1054 for (parents = commit->parents, num_parent = 0;
1055 parents;
1056 parents = parents->next, num_parent++)
1057 ; /* nothing */
1059 parent = xmalloc(num_parent * sizeof(*parent));
1060 for (parents = commit->parents, num_parent = 0;
1061 parents;
1062 parents = parents->next, num_parent++)
1063 hashcpy((unsigned char *)(parent + num_parent),
1064 parents->item->object.sha1);
1065 diff_tree_combined(sha1, parent, num_parent, dense, rev);