builtin-fast-export.c: fix crash on tagged trees
[git/jnareb-git/bp-gitweb.git] / combine-diff.c
blob066ce841ed76e03dd87e789f6688cc3f5411ba64
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 unsigned int lno;
147 int ob, on, nb, nn;
148 unsigned long nmask;
149 int num_parent;
150 int n;
151 struct sline *sline;
152 struct sline *lost_bucket;
155 static void consume_line(void *state_, char *line, unsigned long len)
157 struct combine_diff_state *state = state_;
158 if (5 < len && !memcmp("@@ -", line, 4)) {
159 if (parse_hunk_header(line, len,
160 &state->ob, &state->on,
161 &state->nb, &state->nn))
162 return;
163 state->lno = state->nb;
164 if (!state->nb)
165 /* @@ -1,2 +0,0 @@ to remove the
166 * first two lines...
168 state->nb = 1;
169 if (state->nn == 0)
170 /* @@ -X,Y +N,0 @@ removed Y lines
171 * that would have come *after* line N
172 * in the result. Our lost buckets hang
173 * to the line after the removed lines,
175 state->lost_bucket = &state->sline[state->nb];
176 else
177 state->lost_bucket = &state->sline[state->nb-1];
178 if (!state->sline[state->nb-1].p_lno)
179 state->sline[state->nb-1].p_lno =
180 xcalloc(state->num_parent,
181 sizeof(unsigned long));
182 state->sline[state->nb-1].p_lno[state->n] = state->ob;
183 return;
185 if (!state->lost_bucket)
186 return; /* not in any hunk yet */
187 switch (line[0]) {
188 case '-':
189 append_lost(state->lost_bucket, state->n, line+1, len-1);
190 break;
191 case '+':
192 state->sline[state->lno-1].flag |= state->nmask;
193 state->lno++;
194 break;
198 static void combine_diff(const unsigned char *parent, mmfile_t *result_file,
199 struct sline *sline, unsigned int cnt, int n,
200 int num_parent)
202 unsigned int p_lno, lno;
203 unsigned long nmask = (1UL << n);
204 xpparam_t xpp;
205 xdemitconf_t xecfg;
206 mmfile_t parent_file;
207 xdemitcb_t ecb;
208 struct combine_diff_state state;
209 unsigned long sz;
211 if (!cnt)
212 return; /* result deleted */
214 parent_file.ptr = grab_blob(parent, &sz);
215 parent_file.size = sz;
216 memset(&xpp, 0, sizeof(xpp));
217 xpp.flags = XDF_NEED_MINIMAL;
218 memset(&xecfg, 0, sizeof(xecfg));
219 memset(&state, 0, sizeof(state));
220 state.nmask = nmask;
221 state.sline = sline;
222 state.lno = 1;
223 state.num_parent = num_parent;
224 state.n = n;
226 xdi_diff_outf(&parent_file, result_file, consume_line, &state,
227 &xpp, &xecfg, &ecb);
228 free(parent_file.ptr);
230 /* Assign line numbers for this parent.
232 * sline[lno].p_lno[n] records the first line number
233 * (counting from 1) for parent N if the final hunk display
234 * started by showing sline[lno] (possibly showing the lost
235 * lines attached to it first).
237 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
238 struct lline *ll;
239 sline[lno].p_lno[n] = p_lno;
241 /* How many lines would this sline advance the p_lno? */
242 ll = sline[lno].lost_head;
243 while (ll) {
244 if (ll->parent_map & nmask)
245 p_lno++; /* '-' means parent had it */
246 ll = ll->next;
248 if (lno < cnt && !(sline[lno].flag & nmask))
249 p_lno++; /* no '+' means parent had it */
251 sline[lno].p_lno[n] = p_lno; /* trailer */
254 static unsigned long context = 3;
255 static char combine_marker = '@';
257 static int interesting(struct sline *sline, unsigned long all_mask)
259 /* If some parents lost lines here, or if we have added to
260 * some parent, it is interesting.
262 return ((sline->flag & all_mask) || sline->lost_head);
265 static unsigned long adjust_hunk_tail(struct sline *sline,
266 unsigned long all_mask,
267 unsigned long hunk_begin,
268 unsigned long i)
270 /* i points at the first uninteresting line. If the last line
271 * of the hunk was interesting only because it has some
272 * deletion, then it is not all that interesting for the
273 * purpose of giving trailing context lines. This is because
274 * we output '-' line and then unmodified sline[i-1] itself in
275 * that case which gives us one extra context line.
277 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
278 i--;
279 return i;
282 static unsigned long find_next(struct sline *sline,
283 unsigned long mark,
284 unsigned long i,
285 unsigned long cnt,
286 int look_for_uninteresting)
288 /* We have examined up to i-1 and are about to look at i.
289 * Find next interesting or uninteresting line. Here,
290 * "interesting" does not mean interesting(), but marked by
291 * the give_context() function below (i.e. it includes context
292 * lines that are not interesting to interesting() function
293 * that are surrounded by interesting() ones.
295 while (i <= cnt)
296 if (look_for_uninteresting
297 ? !(sline[i].flag & mark)
298 : (sline[i].flag & mark))
299 return i;
300 else
301 i++;
302 return i;
305 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
307 unsigned long all_mask = (1UL<<num_parent) - 1;
308 unsigned long mark = (1UL<<num_parent);
309 unsigned long no_pre_delete = (2UL<<num_parent);
310 unsigned long i;
312 /* Two groups of interesting lines may have a short gap of
313 * uninteresting lines. Connect such groups to give them a
314 * bit of context.
316 * We first start from what the interesting() function says,
317 * and mark them with "mark", and paint context lines with the
318 * mark. So interesting() would still say false for such context
319 * lines but they are treated as "interesting" in the end.
321 i = find_next(sline, mark, 0, cnt, 0);
322 if (cnt < i)
323 return 0;
325 while (i <= cnt) {
326 unsigned long j = (context < i) ? (i - context) : 0;
327 unsigned long k;
329 /* Paint a few lines before the first interesting line. */
330 while (j < i)
331 sline[j++].flag |= mark | no_pre_delete;
333 again:
334 /* we know up to i is to be included. where does the
335 * next uninteresting one start?
337 j = find_next(sline, mark, i, cnt, 1);
338 if (cnt < j)
339 break; /* the rest are all interesting */
341 /* lookahead context lines */
342 k = find_next(sline, mark, j, cnt, 0);
343 j = adjust_hunk_tail(sline, all_mask, i, j);
345 if (k < j + context) {
346 /* k is interesting and [j,k) are not, but
347 * paint them interesting because the gap is small.
349 while (j < k)
350 sline[j++].flag |= mark;
351 i = k;
352 goto again;
355 /* j is the first uninteresting line and there is
356 * no overlap beyond it within context lines. Paint
357 * the trailing edge a bit.
359 i = k;
360 k = (j + context < cnt+1) ? j + context : cnt+1;
361 while (j < k)
362 sline[j++].flag |= mark;
364 return 1;
367 static int make_hunks(struct sline *sline, unsigned long cnt,
368 int num_parent, int dense)
370 unsigned long all_mask = (1UL<<num_parent) - 1;
371 unsigned long mark = (1UL<<num_parent);
372 unsigned long i;
373 int has_interesting = 0;
375 for (i = 0; i <= cnt; i++) {
376 if (interesting(&sline[i], all_mask))
377 sline[i].flag |= mark;
378 else
379 sline[i].flag &= ~mark;
381 if (!dense)
382 return give_context(sline, cnt, num_parent);
384 /* Look at each hunk, and if we have changes from only one
385 * parent, or the changes are the same from all but one
386 * parent, mark that uninteresting.
388 i = 0;
389 while (i <= cnt) {
390 unsigned long j, hunk_begin, hunk_end;
391 unsigned long same_diff;
392 while (i <= cnt && !(sline[i].flag & mark))
393 i++;
394 if (cnt < i)
395 break; /* No more interesting hunks */
396 hunk_begin = i;
397 for (j = i + 1; j <= cnt; j++) {
398 if (!(sline[j].flag & mark)) {
399 /* Look beyond the end to see if there
400 * is an interesting line after this
401 * hunk within context span.
403 unsigned long la; /* lookahead */
404 int contin = 0;
405 la = adjust_hunk_tail(sline, all_mask,
406 hunk_begin, j);
407 la = (la + context < cnt + 1) ?
408 (la + context) : cnt + 1;
409 while (j <= --la) {
410 if (sline[la].flag & mark) {
411 contin = 1;
412 break;
415 if (!contin)
416 break;
417 j = la;
420 hunk_end = j;
422 /* [i..hunk_end) are interesting. Now is it really
423 * interesting? We check if there are only two versions
424 * and the result matches one of them. That is, we look
425 * at:
426 * (+) line, which records lines added to which parents;
427 * this line appears in the result.
428 * (-) line, which records from what parents the line
429 * was removed; this line does not appear in the result.
430 * then check the set of parents the result has difference
431 * from, from all lines. If there are lines that has
432 * different set of parents that the result has differences
433 * from, that means we have more than two versions.
435 * Even when we have only two versions, if the result does
436 * not match any of the parents, the it should be considered
437 * interesting. In such a case, we would have all '+' line.
438 * After passing the above "two versions" test, that would
439 * appear as "the same set of parents" to be "all parents".
441 same_diff = 0;
442 has_interesting = 0;
443 for (j = i; j < hunk_end && !has_interesting; j++) {
444 unsigned long this_diff = sline[j].flag & all_mask;
445 struct lline *ll = sline[j].lost_head;
446 if (this_diff) {
447 /* This has some changes. Is it the
448 * same as others?
450 if (!same_diff)
451 same_diff = this_diff;
452 else if (same_diff != this_diff) {
453 has_interesting = 1;
454 break;
457 while (ll && !has_interesting) {
458 /* Lost this line from these parents;
459 * who are they? Are they the same?
461 this_diff = ll->parent_map;
462 if (!same_diff)
463 same_diff = this_diff;
464 else if (same_diff != this_diff) {
465 has_interesting = 1;
467 ll = ll->next;
471 if (!has_interesting && same_diff != all_mask) {
472 /* This hunk is not that interesting after all */
473 for (j = hunk_begin; j < hunk_end; j++)
474 sline[j].flag &= ~mark;
476 i = hunk_end;
479 has_interesting = give_context(sline, cnt, num_parent);
480 return has_interesting;
483 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
485 l0 = sline[l0].p_lno[n];
486 l1 = sline[l1].p_lno[n];
487 printf(" -%lu,%lu", l0, l1-l0-null_context);
490 static int hunk_comment_line(const char *bol)
492 int ch;
494 if (!bol)
495 return 0;
496 ch = *bol & 0xff;
497 return (isalpha(ch) || ch == '_' || ch == '$');
500 static void show_line_to_eol(const char *line, int len, const char *reset)
502 int saw_cr_at_eol = 0;
503 if (len < 0)
504 len = strlen(line);
505 saw_cr_at_eol = (len && line[len-1] == '\r');
507 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
508 reset,
509 saw_cr_at_eol ? "\r" : "");
512 static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
513 int use_color)
515 unsigned long mark = (1UL<<num_parent);
516 unsigned long no_pre_delete = (2UL<<num_parent);
517 int i;
518 unsigned long lno = 0;
519 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
520 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
521 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
522 const char *c_plain = diff_get_color(use_color, DIFF_PLAIN);
523 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
525 if (!cnt)
526 return; /* result deleted */
528 while (1) {
529 unsigned long hunk_end;
530 unsigned long rlines;
531 const char *hunk_comment = NULL;
532 unsigned long null_context = 0;
534 while (lno <= cnt && !(sline[lno].flag & mark)) {
535 if (hunk_comment_line(sline[lno].bol))
536 hunk_comment = sline[lno].bol;
537 lno++;
539 if (cnt < lno)
540 break;
541 else {
542 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
543 if (!(sline[hunk_end].flag & mark))
544 break;
546 rlines = hunk_end - lno;
547 if (cnt < hunk_end)
548 rlines--; /* pointing at the last delete hunk */
550 if (!context) {
552 * Even when running with --unified=0, all
553 * lines in the hunk needs to be processed in
554 * the loop below in order to show the
555 * deletion recorded in lost_head. However,
556 * we do not want to show the resulting line
557 * with all blank context markers in such a
558 * case. Compensate.
560 unsigned long j;
561 for (j = lno; j < hunk_end; j++)
562 if (!(sline[j].flag & (mark-1)))
563 null_context++;
564 rlines -= null_context;
567 fputs(c_frag, stdout);
568 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
569 for (i = 0; i < num_parent; i++)
570 show_parent_lno(sline, lno, hunk_end, i, null_context);
571 printf(" +%lu,%lu ", lno+1, rlines);
572 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
574 if (hunk_comment) {
575 int comment_end = 0;
576 for (i = 0; i < 40; i++) {
577 int ch = hunk_comment[i] & 0xff;
578 if (!ch || ch == '\n')
579 break;
580 if (!isspace(ch))
581 comment_end = i;
583 if (comment_end)
584 putchar(' ');
585 for (i = 0; i < comment_end; i++)
586 putchar(hunk_comment[i]);
589 printf("%s\n", c_reset);
590 while (lno < hunk_end) {
591 struct lline *ll;
592 int j;
593 unsigned long p_mask;
594 struct sline *sl = &sline[lno++];
595 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost_head;
596 while (ll) {
597 fputs(c_old, stdout);
598 for (j = 0; j < num_parent; j++) {
599 if (ll->parent_map & (1UL<<j))
600 putchar('-');
601 else
602 putchar(' ');
604 show_line_to_eol(ll->line, -1, c_reset);
605 ll = ll->next;
607 if (cnt < lno)
608 break;
609 p_mask = 1;
610 if (!(sl->flag & (mark-1))) {
612 * This sline was here to hang the
613 * lost lines in front of it.
615 if (!context)
616 continue;
617 fputs(c_plain, stdout);
619 else
620 fputs(c_new, stdout);
621 for (j = 0; j < num_parent; j++) {
622 if (p_mask & sl->flag)
623 putchar('+');
624 else
625 putchar(' ');
626 p_mask <<= 1;
628 show_line_to_eol(sl->bol, sl->len, c_reset);
633 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
634 int i, int j)
636 /* We have already examined parent j and we know parent i
637 * and parent j are the same, so reuse the combined result
638 * of parent j for parent i.
640 unsigned long lno, imask, jmask;
641 imask = (1UL<<i);
642 jmask = (1UL<<j);
644 for (lno = 0; lno <= cnt; lno++) {
645 struct lline *ll = sline->lost_head;
646 sline->p_lno[i] = sline->p_lno[j];
647 while (ll) {
648 if (ll->parent_map & jmask)
649 ll->parent_map |= imask;
650 ll = ll->next;
652 if (sline->flag & jmask)
653 sline->flag |= imask;
654 sline++;
656 /* the overall size of the file (sline[cnt]) */
657 sline->p_lno[i] = sline->p_lno[j];
660 static void dump_quoted_path(const char *head,
661 const char *prefix,
662 const char *path,
663 const char *c_meta, const char *c_reset)
665 static struct strbuf buf = STRBUF_INIT;
667 strbuf_reset(&buf);
668 strbuf_addstr(&buf, c_meta);
669 strbuf_addstr(&buf, head);
670 quote_two_c_style(&buf, prefix, path, 0);
671 strbuf_addstr(&buf, c_reset);
672 puts(buf.buf);
675 static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
676 int dense, struct rev_info *rev)
678 struct diff_options *opt = &rev->diffopt;
679 unsigned long result_size, cnt, lno;
680 char *result, *cp;
681 struct sline *sline; /* survived lines */
682 int mode_differs = 0;
683 int i, show_hunks;
684 int working_tree_file = is_null_sha1(elem->sha1);
685 int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
686 const char *a_prefix, *b_prefix;
687 mmfile_t result_file;
689 context = opt->context;
690 a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
691 b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
693 /* Read the result of merge first */
694 if (!working_tree_file)
695 result = grab_blob(elem->sha1, &result_size);
696 else {
697 /* Used by diff-tree to read from the working tree */
698 struct stat st;
699 int fd = -1;
701 if (lstat(elem->path, &st) < 0)
702 goto deleted_file;
704 if (S_ISLNK(st.st_mode)) {
705 struct strbuf buf = STRBUF_INIT;
707 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
708 error("readlink(%s): %s", elem->path,
709 strerror(errno));
710 return;
712 result_size = buf.len;
713 result = strbuf_detach(&buf, NULL);
714 elem->mode = canon_mode(st.st_mode);
715 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
716 size_t len = xsize_t(st.st_size);
717 ssize_t done;
718 int is_file, i;
720 elem->mode = canon_mode(st.st_mode);
721 /* if symlinks don't work, assume symlink if all parents
722 * are symlinks
724 is_file = has_symlinks;
725 for (i = 0; !is_file && i < num_parent; i++)
726 is_file = !S_ISLNK(elem->parent[i].mode);
727 if (!is_file)
728 elem->mode = canon_mode(S_IFLNK);
730 result_size = len;
731 result = xmalloc(len + 1);
733 done = read_in_full(fd, result, len);
734 if (done < 0)
735 die("read error '%s'", elem->path);
736 else if (done < len)
737 die("early EOF '%s'", elem->path);
739 result[len] = 0;
741 /* If not a fake symlink, apply filters, e.g. autocrlf */
742 if (is_file) {
743 struct strbuf buf = STRBUF_INIT;
745 if (convert_to_git(elem->path, result, len, &buf, safe_crlf)) {
746 free(result);
747 result = strbuf_detach(&buf, &len);
748 result_size = len;
752 else {
753 deleted_file:
754 result_size = 0;
755 elem->mode = 0;
756 result = xcalloc(1, 1);
759 if (0 <= fd)
760 close(fd);
763 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
764 if (*cp == '\n')
765 cnt++;
767 if (result_size && result[result_size-1] != '\n')
768 cnt++; /* incomplete line */
770 sline = xcalloc(cnt+2, sizeof(*sline));
771 sline[0].bol = result;
772 for (lno = 0; lno <= cnt + 1; lno++) {
773 sline[lno].lost_tail = &sline[lno].lost_head;
774 sline[lno].flag = 0;
776 for (lno = 0, cp = result; cp < result + result_size; cp++) {
777 if (*cp == '\n') {
778 sline[lno].len = cp - sline[lno].bol;
779 lno++;
780 if (lno < cnt)
781 sline[lno].bol = cp + 1;
784 if (result_size && result[result_size-1] != '\n')
785 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
787 result_file.ptr = result;
788 result_file.size = result_size;
790 /* Even p_lno[cnt+1] is valid -- that is for the end line number
791 * for deletion hunk at the end.
793 sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
794 for (lno = 0; lno <= cnt; lno++)
795 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
797 for (i = 0; i < num_parent; i++) {
798 int j;
799 for (j = 0; j < i; j++) {
800 if (!hashcmp(elem->parent[i].sha1,
801 elem->parent[j].sha1)) {
802 reuse_combine_diff(sline, cnt, i, j);
803 break;
806 if (i <= j)
807 combine_diff(elem->parent[i].sha1, &result_file, sline,
808 cnt, i, num_parent);
809 if (elem->parent[i].mode != elem->mode)
810 mode_differs = 1;
813 show_hunks = make_hunks(sline, cnt, num_parent, dense);
815 if (show_hunks || mode_differs || working_tree_file) {
816 const char *abb;
817 int use_color = DIFF_OPT_TST(opt, COLOR_DIFF);
818 const char *c_meta = diff_get_color(use_color, DIFF_METAINFO);
819 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
820 int added = 0;
821 int deleted = 0;
823 if (rev->loginfo && !rev->no_commit_id)
824 show_log(rev);
825 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
826 "", elem->path, c_meta, c_reset);
827 printf("%sindex ", c_meta);
828 for (i = 0; i < num_parent; i++) {
829 abb = find_unique_abbrev(elem->parent[i].sha1,
830 abbrev);
831 printf("%s%s", i ? "," : "", abb);
833 abb = find_unique_abbrev(elem->sha1, abbrev);
834 printf("..%s%s\n", abb, c_reset);
836 if (mode_differs) {
837 deleted = !elem->mode;
839 /* We say it was added if nobody had it */
840 added = !deleted;
841 for (i = 0; added && i < num_parent; i++)
842 if (elem->parent[i].status !=
843 DIFF_STATUS_ADDED)
844 added = 0;
845 if (added)
846 printf("%snew file mode %06o",
847 c_meta, elem->mode);
848 else {
849 if (deleted)
850 printf("%sdeleted file ", c_meta);
851 printf("mode ");
852 for (i = 0; i < num_parent; i++) {
853 printf("%s%06o", i ? "," : "",
854 elem->parent[i].mode);
856 if (elem->mode)
857 printf("..%06o", elem->mode);
859 printf("%s\n", c_reset);
861 if (added)
862 dump_quoted_path("--- ", "", "/dev/null",
863 c_meta, c_reset);
864 else
865 dump_quoted_path("--- ", a_prefix, elem->path,
866 c_meta, c_reset);
867 if (deleted)
868 dump_quoted_path("+++ ", "", "/dev/null",
869 c_meta, c_reset);
870 else
871 dump_quoted_path("+++ ", b_prefix, elem->path,
872 c_meta, c_reset);
873 dump_sline(sline, cnt, num_parent,
874 DIFF_OPT_TST(opt, COLOR_DIFF));
876 free(result);
878 for (lno = 0; lno < cnt; lno++) {
879 if (sline[lno].lost_head) {
880 struct lline *ll = sline[lno].lost_head;
881 while (ll) {
882 struct lline *tmp = ll;
883 ll = ll->next;
884 free(tmp);
888 free(sline[0].p_lno);
889 free(sline);
892 #define COLONS "::::::::::::::::::::::::::::::::"
894 static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
896 struct diff_options *opt = &rev->diffopt;
897 int i, offset;
898 const char *prefix;
899 int line_termination, inter_name_termination;
901 line_termination = opt->line_termination;
902 inter_name_termination = '\t';
903 if (!line_termination)
904 inter_name_termination = 0;
906 if (rev->loginfo && !rev->no_commit_id)
907 show_log(rev);
909 if (opt->output_format & DIFF_FORMAT_RAW) {
910 offset = strlen(COLONS) - num_parent;
911 if (offset < 0)
912 offset = 0;
913 prefix = COLONS + offset;
915 /* Show the modes */
916 for (i = 0; i < num_parent; i++) {
917 printf("%s%06o", prefix, p->parent[i].mode);
918 prefix = " ";
920 printf("%s%06o", prefix, p->mode);
922 /* Show sha1's */
923 for (i = 0; i < num_parent; i++)
924 printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
925 opt->abbrev));
926 printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
929 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
930 for (i = 0; i < num_parent; i++)
931 putchar(p->parent[i].status);
932 putchar(inter_name_termination);
935 write_name_quoted(p->path, stdout, line_termination);
938 void show_combined_diff(struct combine_diff_path *p,
939 int num_parent,
940 int dense,
941 struct rev_info *rev)
943 struct diff_options *opt = &rev->diffopt;
944 if (!p->len)
945 return;
946 if (opt->output_format & (DIFF_FORMAT_RAW |
947 DIFF_FORMAT_NAME |
948 DIFF_FORMAT_NAME_STATUS))
949 show_raw_diff(p, num_parent, rev);
950 else if (opt->output_format & DIFF_FORMAT_PATCH)
951 show_patch_diff(p, num_parent, dense, rev);
954 void diff_tree_combined(const unsigned char *sha1,
955 const unsigned char parent[][20],
956 int num_parent,
957 int dense,
958 struct rev_info *rev)
960 struct diff_options *opt = &rev->diffopt;
961 struct diff_options diffopts;
962 struct combine_diff_path *p, *paths = NULL;
963 int i, num_paths, needsep, show_log_first;
965 diffopts = *opt;
966 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
967 DIFF_OPT_SET(&diffopts, RECURSIVE);
968 DIFF_OPT_CLR(&diffopts, ALLOW_EXTERNAL);
970 show_log_first = !!rev->loginfo && !rev->no_commit_id;
971 needsep = 0;
972 /* find set of paths that everybody touches */
973 for (i = 0; i < num_parent; i++) {
974 /* show stat against the first parent even
975 * when doing combined diff.
977 int stat_opt = (opt->output_format &
978 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
979 if (i == 0 && stat_opt)
980 diffopts.output_format = stat_opt;
981 else
982 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
983 diff_tree_sha1(parent[i], sha1, "", &diffopts);
984 diffcore_std(&diffopts);
985 paths = intersect_paths(paths, i, num_parent);
987 if (show_log_first && i == 0) {
988 show_log(rev);
989 if (rev->verbose_header && opt->output_format)
990 putchar(opt->line_termination);
992 diff_flush(&diffopts);
995 /* find out surviving paths */
996 for (num_paths = 0, p = paths; p; p = p->next) {
997 if (p->len)
998 num_paths++;
1000 if (num_paths) {
1001 if (opt->output_format & (DIFF_FORMAT_RAW |
1002 DIFF_FORMAT_NAME |
1003 DIFF_FORMAT_NAME_STATUS)) {
1004 for (p = paths; p; p = p->next) {
1005 if (p->len)
1006 show_raw_diff(p, num_parent, rev);
1008 needsep = 1;
1010 else if (opt->output_format &
1011 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
1012 needsep = 1;
1013 if (opt->output_format & DIFF_FORMAT_PATCH) {
1014 if (needsep)
1015 putchar(opt->line_termination);
1016 for (p = paths; p; p = p->next) {
1017 if (p->len)
1018 show_patch_diff(p, num_parent, dense,
1019 rev);
1024 /* Clean things up */
1025 while (paths) {
1026 struct combine_diff_path *tmp = paths;
1027 paths = paths->next;
1028 free(tmp);
1032 void diff_tree_combined_merge(const unsigned char *sha1,
1033 int dense, struct rev_info *rev)
1035 int num_parent;
1036 const unsigned char (*parent)[20];
1037 struct commit *commit = lookup_commit(sha1);
1038 struct commit_list *parents;
1040 /* count parents */
1041 for (parents = commit->parents, num_parent = 0;
1042 parents;
1043 parents = parents->next, num_parent++)
1044 ; /* nothing */
1046 parent = xmalloc(num_parent * sizeof(*parent));
1047 for (parents = commit->parents, num_parent = 0;
1048 parents;
1049 parents = parents->next, num_parent++)
1050 hashcpy((unsigned char*)(parent + num_parent),
1051 parents->item->object.sha1);
1052 diff_tree_combined(sha1, parent, num_parent, dense, rev);