combine-diff: split header printing into its own function
[git/dscho.git] / combine-diff.c
blob309dc6c27225b5c758b96f385a04bb6b3397f1c5
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->nn == 0) {
168 /* @@ -X,Y +N,0 @@ removed Y lines
169 * that would have come *after* line N
170 * in the result. Our lost buckets hang
171 * to the line after the removed lines,
173 * Note that this is correct even when N == 0,
174 * in which case the hunk removes the first
175 * line in the file.
177 state->lost_bucket = &state->sline[state->nb];
178 if (!state->nb)
179 state->nb = 1;
180 } else {
181 state->lost_bucket = &state->sline[state->nb-1];
183 if (!state->sline[state->nb-1].p_lno)
184 state->sline[state->nb-1].p_lno =
185 xcalloc(state->num_parent,
186 sizeof(unsigned long));
187 state->sline[state->nb-1].p_lno[state->n] = state->ob;
188 state->lost_bucket->next_lost = state->lost_bucket->lost_head;
189 return;
191 if (!state->lost_bucket)
192 return; /* not in any hunk yet */
193 switch (line[0]) {
194 case '-':
195 append_lost(state->lost_bucket, state->n, line+1, len-1);
196 break;
197 case '+':
198 state->sline[state->lno-1].flag |= state->nmask;
199 state->lno++;
200 break;
204 static void combine_diff(const unsigned char *parent, unsigned int mode,
205 mmfile_t *result_file,
206 struct sline *sline, unsigned int cnt, int n,
207 int num_parent, int result_deleted)
209 unsigned int p_lno, lno;
210 unsigned long nmask = (1UL << n);
211 xpparam_t xpp;
212 xdemitconf_t xecfg;
213 mmfile_t parent_file;
214 struct combine_diff_state state;
215 unsigned long sz;
217 if (result_deleted)
218 return; /* result deleted */
220 parent_file.ptr = grab_blob(parent, mode, &sz);
221 parent_file.size = sz;
222 memset(&xpp, 0, sizeof(xpp));
223 xpp.flags = 0;
224 memset(&xecfg, 0, sizeof(xecfg));
225 memset(&state, 0, sizeof(state));
226 state.nmask = nmask;
227 state.sline = sline;
228 state.lno = 1;
229 state.num_parent = num_parent;
230 state.n = n;
232 xdi_diff_outf(&parent_file, result_file, consume_line, &state,
233 &xpp, &xecfg);
234 free(parent_file.ptr);
236 /* Assign line numbers for this parent.
238 * sline[lno].p_lno[n] records the first line number
239 * (counting from 1) for parent N if the final hunk display
240 * started by showing sline[lno] (possibly showing the lost
241 * lines attached to it first).
243 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
244 struct lline *ll;
245 sline[lno].p_lno[n] = p_lno;
247 /* How many lines would this sline advance the p_lno? */
248 ll = sline[lno].lost_head;
249 while (ll) {
250 if (ll->parent_map & nmask)
251 p_lno++; /* '-' means parent had it */
252 ll = ll->next;
254 if (lno < cnt && !(sline[lno].flag & nmask))
255 p_lno++; /* no '+' means parent had it */
257 sline[lno].p_lno[n] = p_lno; /* trailer */
260 static unsigned long context = 3;
261 static char combine_marker = '@';
263 static int interesting(struct sline *sline, unsigned long all_mask)
265 /* If some parents lost lines here, or if we have added to
266 * some parent, it is interesting.
268 return ((sline->flag & all_mask) || sline->lost_head);
271 static unsigned long adjust_hunk_tail(struct sline *sline,
272 unsigned long all_mask,
273 unsigned long hunk_begin,
274 unsigned long i)
276 /* i points at the first uninteresting line. If the last line
277 * of the hunk was interesting only because it has some
278 * deletion, then it is not all that interesting for the
279 * purpose of giving trailing context lines. This is because
280 * we output '-' line and then unmodified sline[i-1] itself in
281 * that case which gives us one extra context line.
283 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
284 i--;
285 return i;
288 static unsigned long find_next(struct sline *sline,
289 unsigned long mark,
290 unsigned long i,
291 unsigned long cnt,
292 int look_for_uninteresting)
294 /* We have examined up to i-1 and are about to look at i.
295 * Find next interesting or uninteresting line. Here,
296 * "interesting" does not mean interesting(), but marked by
297 * the give_context() function below (i.e. it includes context
298 * lines that are not interesting to interesting() function
299 * that are surrounded by interesting() ones.
301 while (i <= cnt)
302 if (look_for_uninteresting
303 ? !(sline[i].flag & mark)
304 : (sline[i].flag & mark))
305 return i;
306 else
307 i++;
308 return i;
311 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
313 unsigned long all_mask = (1UL<<num_parent) - 1;
314 unsigned long mark = (1UL<<num_parent);
315 unsigned long no_pre_delete = (2UL<<num_parent);
316 unsigned long i;
318 /* Two groups of interesting lines may have a short gap of
319 * uninteresting lines. Connect such groups to give them a
320 * bit of context.
322 * We first start from what the interesting() function says,
323 * and mark them with "mark", and paint context lines with the
324 * mark. So interesting() would still say false for such context
325 * lines but they are treated as "interesting" in the end.
327 i = find_next(sline, mark, 0, cnt, 0);
328 if (cnt < i)
329 return 0;
331 while (i <= cnt) {
332 unsigned long j = (context < i) ? (i - context) : 0;
333 unsigned long k;
335 /* Paint a few lines before the first interesting line. */
336 while (j < i)
337 sline[j++].flag |= mark | no_pre_delete;
339 again:
340 /* we know up to i is to be included. where does the
341 * next uninteresting one start?
343 j = find_next(sline, mark, i, cnt, 1);
344 if (cnt < j)
345 break; /* the rest are all interesting */
347 /* lookahead context lines */
348 k = find_next(sline, mark, j, cnt, 0);
349 j = adjust_hunk_tail(sline, all_mask, i, j);
351 if (k < j + context) {
352 /* k is interesting and [j,k) are not, but
353 * paint them interesting because the gap is small.
355 while (j < k)
356 sline[j++].flag |= mark;
357 i = k;
358 goto again;
361 /* j is the first uninteresting line and there is
362 * no overlap beyond it within context lines. Paint
363 * the trailing edge a bit.
365 i = k;
366 k = (j + context < cnt+1) ? j + context : cnt+1;
367 while (j < k)
368 sline[j++].flag |= mark;
370 return 1;
373 static int make_hunks(struct sline *sline, unsigned long cnt,
374 int num_parent, int dense)
376 unsigned long all_mask = (1UL<<num_parent) - 1;
377 unsigned long mark = (1UL<<num_parent);
378 unsigned long i;
379 int has_interesting = 0;
381 for (i = 0; i <= cnt; i++) {
382 if (interesting(&sline[i], all_mask))
383 sline[i].flag |= mark;
384 else
385 sline[i].flag &= ~mark;
387 if (!dense)
388 return give_context(sline, cnt, num_parent);
390 /* Look at each hunk, and if we have changes from only one
391 * parent, or the changes are the same from all but one
392 * parent, mark that uninteresting.
394 i = 0;
395 while (i <= cnt) {
396 unsigned long j, hunk_begin, hunk_end;
397 unsigned long same_diff;
398 while (i <= cnt && !(sline[i].flag & mark))
399 i++;
400 if (cnt < i)
401 break; /* No more interesting hunks */
402 hunk_begin = i;
403 for (j = i + 1; j <= cnt; j++) {
404 if (!(sline[j].flag & mark)) {
405 /* Look beyond the end to see if there
406 * is an interesting line after this
407 * hunk within context span.
409 unsigned long la; /* lookahead */
410 int contin = 0;
411 la = adjust_hunk_tail(sline, all_mask,
412 hunk_begin, j);
413 la = (la + context < cnt + 1) ?
414 (la + context) : cnt + 1;
415 while (j <= --la) {
416 if (sline[la].flag & mark) {
417 contin = 1;
418 break;
421 if (!contin)
422 break;
423 j = la;
426 hunk_end = j;
428 /* [i..hunk_end) are interesting. Now is it really
429 * interesting? We check if there are only two versions
430 * and the result matches one of them. That is, we look
431 * at:
432 * (+) line, which records lines added to which parents;
433 * this line appears in the result.
434 * (-) line, which records from what parents the line
435 * was removed; this line does not appear in the result.
436 * then check the set of parents the result has difference
437 * from, from all lines. If there are lines that has
438 * different set of parents that the result has differences
439 * from, that means we have more than two versions.
441 * Even when we have only two versions, if the result does
442 * not match any of the parents, the it should be considered
443 * interesting. In such a case, we would have all '+' line.
444 * After passing the above "two versions" test, that would
445 * appear as "the same set of parents" to be "all parents".
447 same_diff = 0;
448 has_interesting = 0;
449 for (j = i; j < hunk_end && !has_interesting; j++) {
450 unsigned long this_diff = sline[j].flag & all_mask;
451 struct lline *ll = sline[j].lost_head;
452 if (this_diff) {
453 /* This has some changes. Is it the
454 * same as others?
456 if (!same_diff)
457 same_diff = this_diff;
458 else if (same_diff != this_diff) {
459 has_interesting = 1;
460 break;
463 while (ll && !has_interesting) {
464 /* Lost this line from these parents;
465 * who are they? Are they the same?
467 this_diff = ll->parent_map;
468 if (!same_diff)
469 same_diff = this_diff;
470 else if (same_diff != this_diff) {
471 has_interesting = 1;
473 ll = ll->next;
477 if (!has_interesting && same_diff != all_mask) {
478 /* This hunk is not that interesting after all */
479 for (j = hunk_begin; j < hunk_end; j++)
480 sline[j].flag &= ~mark;
482 i = hunk_end;
485 has_interesting = give_context(sline, cnt, num_parent);
486 return has_interesting;
489 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
491 l0 = sline[l0].p_lno[n];
492 l1 = sline[l1].p_lno[n];
493 printf(" -%lu,%lu", l0, l1-l0-null_context);
496 static int hunk_comment_line(const char *bol)
498 int ch;
500 if (!bol)
501 return 0;
502 ch = *bol & 0xff;
503 return (isalpha(ch) || ch == '_' || ch == '$');
506 static void show_line_to_eol(const char *line, int len, const char *reset)
508 int saw_cr_at_eol = 0;
509 if (len < 0)
510 len = strlen(line);
511 saw_cr_at_eol = (len && line[len-1] == '\r');
513 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
514 reset,
515 saw_cr_at_eol ? "\r" : "");
518 static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
519 int use_color, int result_deleted)
521 unsigned long mark = (1UL<<num_parent);
522 unsigned long no_pre_delete = (2UL<<num_parent);
523 int i;
524 unsigned long lno = 0;
525 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
526 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
527 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
528 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
529 const char *c_plain = diff_get_color(use_color, DIFF_PLAIN);
530 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
532 if (result_deleted)
533 return; /* result deleted */
535 while (1) {
536 unsigned long hunk_end;
537 unsigned long rlines;
538 const char *hunk_comment = NULL;
539 unsigned long null_context = 0;
541 while (lno <= cnt && !(sline[lno].flag & mark)) {
542 if (hunk_comment_line(sline[lno].bol))
543 hunk_comment = sline[lno].bol;
544 lno++;
546 if (cnt < lno)
547 break;
548 else {
549 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
550 if (!(sline[hunk_end].flag & mark))
551 break;
553 rlines = hunk_end - lno;
554 if (cnt < hunk_end)
555 rlines--; /* pointing at the last delete hunk */
557 if (!context) {
559 * Even when running with --unified=0, all
560 * lines in the hunk needs to be processed in
561 * the loop below in order to show the
562 * deletion recorded in lost_head. However,
563 * we do not want to show the resulting line
564 * with all blank context markers in such a
565 * case. Compensate.
567 unsigned long j;
568 for (j = lno; j < hunk_end; j++)
569 if (!(sline[j].flag & (mark-1)))
570 null_context++;
571 rlines -= null_context;
574 fputs(c_frag, stdout);
575 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
576 for (i = 0; i < num_parent; i++)
577 show_parent_lno(sline, lno, hunk_end, i, null_context);
578 printf(" +%lu,%lu ", lno+1, rlines);
579 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
581 if (hunk_comment) {
582 int comment_end = 0;
583 for (i = 0; i < 40; i++) {
584 int ch = hunk_comment[i] & 0xff;
585 if (!ch || ch == '\n')
586 break;
587 if (!isspace(ch))
588 comment_end = i;
590 if (comment_end)
591 printf("%s%s %s%s", c_reset,
592 c_plain, c_reset,
593 c_func);
594 for (i = 0; i < comment_end; i++)
595 putchar(hunk_comment[i]);
598 printf("%s\n", c_reset);
599 while (lno < hunk_end) {
600 struct lline *ll;
601 int j;
602 unsigned long p_mask;
603 struct sline *sl = &sline[lno++];
604 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost_head;
605 while (ll) {
606 fputs(c_old, stdout);
607 for (j = 0; j < num_parent; j++) {
608 if (ll->parent_map & (1UL<<j))
609 putchar('-');
610 else
611 putchar(' ');
613 show_line_to_eol(ll->line, -1, c_reset);
614 ll = ll->next;
616 if (cnt < lno)
617 break;
618 p_mask = 1;
619 if (!(sl->flag & (mark-1))) {
621 * This sline was here to hang the
622 * lost lines in front of it.
624 if (!context)
625 continue;
626 fputs(c_plain, stdout);
628 else
629 fputs(c_new, stdout);
630 for (j = 0; j < num_parent; j++) {
631 if (p_mask & sl->flag)
632 putchar('+');
633 else
634 putchar(' ');
635 p_mask <<= 1;
637 show_line_to_eol(sl->bol, sl->len, c_reset);
642 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
643 int i, int j)
645 /* We have already examined parent j and we know parent i
646 * and parent j are the same, so reuse the combined result
647 * of parent j for parent i.
649 unsigned long lno, imask, jmask;
650 imask = (1UL<<i);
651 jmask = (1UL<<j);
653 for (lno = 0; lno <= cnt; lno++) {
654 struct lline *ll = sline->lost_head;
655 sline->p_lno[i] = sline->p_lno[j];
656 while (ll) {
657 if (ll->parent_map & jmask)
658 ll->parent_map |= imask;
659 ll = ll->next;
661 if (sline->flag & jmask)
662 sline->flag |= imask;
663 sline++;
665 /* the overall size of the file (sline[cnt]) */
666 sline->p_lno[i] = sline->p_lno[j];
669 static void dump_quoted_path(const char *head,
670 const char *prefix,
671 const char *path,
672 const char *c_meta, const char *c_reset)
674 static struct strbuf buf = STRBUF_INIT;
676 strbuf_reset(&buf);
677 strbuf_addstr(&buf, c_meta);
678 strbuf_addstr(&buf, head);
679 quote_two_c_style(&buf, prefix, path, 0);
680 strbuf_addstr(&buf, c_reset);
681 puts(buf.buf);
684 static void show_combined_header(struct combine_diff_path *elem,
685 int num_parent,
686 int dense,
687 struct rev_info *rev,
688 int mode_differs)
690 struct diff_options *opt = &rev->diffopt;
691 int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
692 const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
693 const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
694 int use_color = DIFF_OPT_TST(opt, COLOR_DIFF);
695 const char *c_meta = diff_get_color(use_color, DIFF_METAINFO);
696 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
697 const char *abb;
698 int added = 0;
699 int deleted = 0;
700 int i;
702 if (rev->loginfo && !rev->no_commit_id)
703 show_log(rev);
705 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
706 "", elem->path, c_meta, c_reset);
707 printf("%sindex ", c_meta);
708 for (i = 0; i < num_parent; i++) {
709 abb = find_unique_abbrev(elem->parent[i].sha1,
710 abbrev);
711 printf("%s%s", i ? "," : "", abb);
713 abb = find_unique_abbrev(elem->sha1, abbrev);
714 printf("..%s%s\n", abb, c_reset);
716 if (mode_differs) {
717 deleted = !elem->mode;
719 /* We say it was added if nobody had it */
720 added = !deleted;
721 for (i = 0; added && i < num_parent; i++)
722 if (elem->parent[i].status !=
723 DIFF_STATUS_ADDED)
724 added = 0;
725 if (added)
726 printf("%snew file mode %06o",
727 c_meta, elem->mode);
728 else {
729 if (deleted)
730 printf("%sdeleted file ", c_meta);
731 printf("mode ");
732 for (i = 0; i < num_parent; i++) {
733 printf("%s%06o", i ? "," : "",
734 elem->parent[i].mode);
736 if (elem->mode)
737 printf("..%06o", elem->mode);
739 printf("%s\n", c_reset);
742 if (added)
743 dump_quoted_path("--- ", "", "/dev/null",
744 c_meta, c_reset);
745 else
746 dump_quoted_path("--- ", a_prefix, elem->path,
747 c_meta, c_reset);
748 if (deleted)
749 dump_quoted_path("+++ ", "", "/dev/null",
750 c_meta, c_reset);
751 else
752 dump_quoted_path("+++ ", b_prefix, elem->path,
753 c_meta, c_reset);
756 static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
757 int dense, struct rev_info *rev)
759 struct diff_options *opt = &rev->diffopt;
760 unsigned long result_size, cnt, lno;
761 int result_deleted = 0;
762 char *result, *cp;
763 struct sline *sline; /* survived lines */
764 int mode_differs = 0;
765 int i, show_hunks;
766 int working_tree_file = is_null_sha1(elem->sha1);
767 mmfile_t result_file;
769 context = opt->context;
771 /* Read the result of merge first */
772 if (!working_tree_file)
773 result = grab_blob(elem->sha1, elem->mode, &result_size);
774 else {
775 /* Used by diff-tree to read from the working tree */
776 struct stat st;
777 int fd = -1;
779 if (lstat(elem->path, &st) < 0)
780 goto deleted_file;
782 if (S_ISLNK(st.st_mode)) {
783 struct strbuf buf = STRBUF_INIT;
785 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
786 error("readlink(%s): %s", elem->path,
787 strerror(errno));
788 return;
790 result_size = buf.len;
791 result = strbuf_detach(&buf, NULL);
792 elem->mode = canon_mode(st.st_mode);
793 } else if (S_ISDIR(st.st_mode)) {
794 unsigned char sha1[20];
795 if (resolve_gitlink_ref(elem->path, "HEAD", sha1) < 0)
796 result = grab_blob(elem->sha1, elem->mode, &result_size);
797 else
798 result = grab_blob(sha1, elem->mode, &result_size);
799 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
800 size_t len = xsize_t(st.st_size);
801 ssize_t done;
802 int is_file, i;
804 elem->mode = canon_mode(st.st_mode);
805 /* if symlinks don't work, assume symlink if all parents
806 * are symlinks
808 is_file = has_symlinks;
809 for (i = 0; !is_file && i < num_parent; i++)
810 is_file = !S_ISLNK(elem->parent[i].mode);
811 if (!is_file)
812 elem->mode = canon_mode(S_IFLNK);
814 result_size = len;
815 result = xmalloc(len + 1);
817 done = read_in_full(fd, result, len);
818 if (done < 0)
819 die_errno("read error '%s'", elem->path);
820 else if (done < len)
821 die("early EOF '%s'", elem->path);
823 result[len] = 0;
825 /* If not a fake symlink, apply filters, e.g. autocrlf */
826 if (is_file) {
827 struct strbuf buf = STRBUF_INIT;
829 if (convert_to_git(elem->path, result, len, &buf, safe_crlf)) {
830 free(result);
831 result = strbuf_detach(&buf, &len);
832 result_size = len;
836 else {
837 deleted_file:
838 result_deleted = 1;
839 result_size = 0;
840 elem->mode = 0;
841 result = xcalloc(1, 1);
844 if (0 <= fd)
845 close(fd);
848 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
849 if (*cp == '\n')
850 cnt++;
852 if (result_size && result[result_size-1] != '\n')
853 cnt++; /* incomplete line */
855 sline = xcalloc(cnt+2, sizeof(*sline));
856 sline[0].bol = result;
857 for (lno = 0; lno <= cnt + 1; lno++) {
858 sline[lno].lost_tail = &sline[lno].lost_head;
859 sline[lno].flag = 0;
861 for (lno = 0, cp = result; cp < result + result_size; cp++) {
862 if (*cp == '\n') {
863 sline[lno].len = cp - sline[lno].bol;
864 lno++;
865 if (lno < cnt)
866 sline[lno].bol = cp + 1;
869 if (result_size && result[result_size-1] != '\n')
870 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
872 result_file.ptr = result;
873 result_file.size = result_size;
875 /* Even p_lno[cnt+1] is valid -- that is for the end line number
876 * for deletion hunk at the end.
878 sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
879 for (lno = 0; lno <= cnt; lno++)
880 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
882 for (i = 0; i < num_parent; i++) {
883 int j;
884 for (j = 0; j < i; j++) {
885 if (!hashcmp(elem->parent[i].sha1,
886 elem->parent[j].sha1)) {
887 reuse_combine_diff(sline, cnt, i, j);
888 break;
891 if (i <= j)
892 combine_diff(elem->parent[i].sha1,
893 elem->parent[i].mode,
894 &result_file, sline,
895 cnt, i, num_parent, result_deleted);
896 if (elem->parent[i].mode != elem->mode)
897 mode_differs = 1;
900 show_hunks = make_hunks(sline, cnt, num_parent, dense);
902 if (show_hunks || mode_differs || working_tree_file) {
903 show_combined_header(elem, num_parent, dense, rev,
904 mode_differs);
905 dump_sline(sline, cnt, num_parent,
906 DIFF_OPT_TST(opt, COLOR_DIFF), result_deleted);
908 free(result);
910 for (lno = 0; lno < cnt; lno++) {
911 if (sline[lno].lost_head) {
912 struct lline *ll = sline[lno].lost_head;
913 while (ll) {
914 struct lline *tmp = ll;
915 ll = ll->next;
916 free(tmp);
920 free(sline[0].p_lno);
921 free(sline);
924 #define COLONS "::::::::::::::::::::::::::::::::"
926 static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
928 struct diff_options *opt = &rev->diffopt;
929 int i, offset;
930 const char *prefix;
931 int line_termination, inter_name_termination;
933 line_termination = opt->line_termination;
934 inter_name_termination = '\t';
935 if (!line_termination)
936 inter_name_termination = 0;
938 if (rev->loginfo && !rev->no_commit_id)
939 show_log(rev);
941 if (opt->output_format & DIFF_FORMAT_RAW) {
942 offset = strlen(COLONS) - num_parent;
943 if (offset < 0)
944 offset = 0;
945 prefix = COLONS + offset;
947 /* Show the modes */
948 for (i = 0; i < num_parent; i++) {
949 printf("%s%06o", prefix, p->parent[i].mode);
950 prefix = " ";
952 printf("%s%06o", prefix, p->mode);
954 /* Show sha1's */
955 for (i = 0; i < num_parent; i++)
956 printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
957 opt->abbrev));
958 printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
961 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
962 for (i = 0; i < num_parent; i++)
963 putchar(p->parent[i].status);
964 putchar(inter_name_termination);
967 write_name_quoted(p->path, stdout, line_termination);
970 void show_combined_diff(struct combine_diff_path *p,
971 int num_parent,
972 int dense,
973 struct rev_info *rev)
975 struct diff_options *opt = &rev->diffopt;
976 if (!p->len)
977 return;
978 if (opt->output_format & (DIFF_FORMAT_RAW |
979 DIFF_FORMAT_NAME |
980 DIFF_FORMAT_NAME_STATUS))
981 show_raw_diff(p, num_parent, rev);
982 else if (opt->output_format & DIFF_FORMAT_PATCH)
983 show_patch_diff(p, num_parent, dense, rev);
986 void diff_tree_combined(const unsigned char *sha1,
987 const unsigned char parent[][20],
988 int num_parent,
989 int dense,
990 struct rev_info *rev)
992 struct diff_options *opt = &rev->diffopt;
993 struct diff_options diffopts;
994 struct combine_diff_path *p, *paths = NULL;
995 int i, num_paths, needsep, show_log_first;
997 diffopts = *opt;
998 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
999 DIFF_OPT_SET(&diffopts, RECURSIVE);
1000 DIFF_OPT_CLR(&diffopts, ALLOW_EXTERNAL);
1002 show_log_first = !!rev->loginfo && !rev->no_commit_id;
1003 needsep = 0;
1004 /* find set of paths that everybody touches */
1005 for (i = 0; i < num_parent; i++) {
1006 /* show stat against the first parent even
1007 * when doing combined diff.
1009 int stat_opt = (opt->output_format &
1010 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
1011 if (i == 0 && stat_opt)
1012 diffopts.output_format = stat_opt;
1013 else
1014 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
1015 diff_tree_sha1(parent[i], sha1, "", &diffopts);
1016 diffcore_std(&diffopts);
1017 paths = intersect_paths(paths, i, num_parent);
1019 if (show_log_first && i == 0) {
1020 show_log(rev);
1021 if (rev->verbose_header && opt->output_format)
1022 putchar(opt->line_termination);
1024 diff_flush(&diffopts);
1027 /* find out surviving paths */
1028 for (num_paths = 0, p = paths; p; p = p->next) {
1029 if (p->len)
1030 num_paths++;
1032 if (num_paths) {
1033 if (opt->output_format & (DIFF_FORMAT_RAW |
1034 DIFF_FORMAT_NAME |
1035 DIFF_FORMAT_NAME_STATUS)) {
1036 for (p = paths; p; p = p->next) {
1037 if (p->len)
1038 show_raw_diff(p, num_parent, rev);
1040 needsep = 1;
1042 else if (opt->output_format &
1043 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
1044 needsep = 1;
1045 if (opt->output_format & DIFF_FORMAT_PATCH) {
1046 if (needsep)
1047 putchar(opt->line_termination);
1048 for (p = paths; p; p = p->next) {
1049 if (p->len)
1050 show_patch_diff(p, num_parent, dense,
1051 rev);
1056 /* Clean things up */
1057 while (paths) {
1058 struct combine_diff_path *tmp = paths;
1059 paths = paths->next;
1060 free(tmp);
1064 void diff_tree_combined_merge(const unsigned char *sha1,
1065 int dense, struct rev_info *rev)
1067 int num_parent;
1068 const unsigned char (*parent)[20];
1069 struct commit *commit = lookup_commit(sha1);
1070 struct commit_list *parents;
1072 /* count parents */
1073 for (parents = commit->parents, num_parent = 0;
1074 parents;
1075 parents = parents->next, num_parent++)
1076 ; /* nothing */
1078 parent = xmalloc(num_parent * sizeof(*parent));
1079 for (parents = commit->parents, num_parent = 0;
1080 parents;
1081 parents = parents->next, num_parent++)
1082 hashcpy((unsigned char *)(parent + num_parent),
1083 parents->item->object.sha1);
1084 diff_tree_combined(sha1, parent, num_parent, dense, rev);