dir: revert work-around for retired dangerous behavior
[alt-git.git] / combine-diff.c
blob3b92c44880228a94f71a428b0f11bb1caf693c67
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 "xdiff/xmacros.h"
9 #include "log-tree.h"
10 #include "refs.h"
11 #include "userdiff.h"
12 #include "sha1-array.h"
13 #include "revision.h"
15 static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
17 struct diff_queue_struct *q = &diff_queued_diff;
18 struct combine_diff_path *p;
19 int i;
21 if (!n) {
22 struct combine_diff_path *list = NULL, **tail = &list;
23 for (i = 0; i < q->nr; i++) {
24 int len;
25 const char *path;
26 if (diff_unmodified_pair(q->queue[i]))
27 continue;
28 path = q->queue[i]->two->path;
29 len = strlen(path);
30 p = xmalloc(combine_diff_path_size(num_parent, len));
31 p->path = (char *) &(p->parent[num_parent]);
32 memcpy(p->path, path, len);
33 p->path[len] = 0;
34 p->len = len;
35 p->next = NULL;
36 memset(p->parent, 0,
37 sizeof(p->parent[0]) * num_parent);
39 hashcpy(p->sha1, q->queue[i]->two->sha1);
40 p->mode = q->queue[i]->two->mode;
41 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
42 p->parent[n].mode = q->queue[i]->one->mode;
43 p->parent[n].status = q->queue[i]->status;
44 *tail = p;
45 tail = &p->next;
47 return list;
50 for (p = curr; p; p = p->next) {
51 int found = 0;
52 if (!p->len)
53 continue;
54 for (i = 0; i < q->nr; i++) {
55 const char *path;
56 int len;
58 if (diff_unmodified_pair(q->queue[i]))
59 continue;
60 path = q->queue[i]->two->path;
61 len = strlen(path);
62 if (len == p->len && !memcmp(path, p->path, len)) {
63 found = 1;
64 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
65 p->parent[n].mode = q->queue[i]->one->mode;
66 p->parent[n].status = q->queue[i]->status;
67 break;
70 if (!found)
71 p->len = 0;
73 return curr;
76 /* Lines lost from parent */
77 struct lline {
78 struct lline *next, *prev;
79 int len;
80 unsigned long parent_map;
81 char line[FLEX_ARRAY];
84 /* Lines lost from current parent (before coalescing) */
85 struct plost {
86 struct lline *lost_head, *lost_tail;
87 int len;
90 /* Lines surviving in the merge result */
91 struct sline {
92 /* Accumulated and coalesced lost lines */
93 struct lline *lost;
94 int lenlost;
95 struct plost plost;
96 char *bol;
97 int len;
98 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
99 * we did not change it).
100 * bit N is used for "interesting" lines, including context.
101 * bit (N+1) is used for "do not show deletion before this".
103 unsigned long flag;
104 unsigned long *p_lno;
107 static int match_string_spaces(const char *line1, int len1,
108 const char *line2, int len2,
109 long flags)
111 if (flags & XDF_WHITESPACE_FLAGS) {
112 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
113 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
116 if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
117 return (len1 == len2 && !memcmp(line1, line2, len1));
119 while (len1 > 0 && len2 > 0) {
120 len1--;
121 len2--;
122 if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
123 if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
124 (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
125 return 0;
127 for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
128 for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
130 if (line1[len1] != line2[len2])
131 return 0;
134 if (flags & XDF_IGNORE_WHITESPACE) {
135 /* Consume remaining spaces */
136 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
137 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
140 /* We matched full line1 and line2 */
141 if (!len1 && !len2)
142 return 1;
144 return 0;
147 enum coalesce_direction { MATCH, BASE, NEW };
149 /* Coalesce new lines into base by finding LCS */
150 static struct lline *coalesce_lines(struct lline *base, int *lenbase,
151 struct lline *new, int lennew,
152 unsigned long parent, long flags)
154 int **lcs;
155 enum coalesce_direction **directions;
156 struct lline *baseend, *newend = NULL;
157 int i, j, origbaselen = *lenbase;
159 if (new == NULL)
160 return base;
162 if (base == NULL) {
163 *lenbase = lennew;
164 return new;
168 * Coalesce new lines into base by finding the LCS
169 * - Create the table to run dynamic programming
170 * - Compute the LCS
171 * - Then reverse read the direction structure:
172 * - If we have MATCH, assign parent to base flag, and consume
173 * both baseend and newend
174 * - Else if we have BASE, consume baseend
175 * - Else if we have NEW, insert newend lline into base and
176 * consume newend
178 lcs = xcalloc(origbaselen + 1, sizeof(int*));
179 directions = xcalloc(origbaselen + 1, sizeof(enum coalesce_direction*));
180 for (i = 0; i < origbaselen + 1; i++) {
181 lcs[i] = xcalloc(lennew + 1, sizeof(int));
182 directions[i] = xcalloc(lennew + 1, sizeof(enum coalesce_direction));
183 directions[i][0] = BASE;
185 for (j = 1; j < lennew + 1; j++)
186 directions[0][j] = NEW;
188 for (i = 1, baseend = base; i < origbaselen + 1; i++) {
189 for (j = 1, newend = new; j < lennew + 1; j++) {
190 if (match_string_spaces(baseend->line, baseend->len,
191 newend->line, newend->len, flags)) {
192 lcs[i][j] = lcs[i - 1][j - 1] + 1;
193 directions[i][j] = MATCH;
194 } else if (lcs[i][j - 1] >= lcs[i - 1][j]) {
195 lcs[i][j] = lcs[i][j - 1];
196 directions[i][j] = NEW;
197 } else {
198 lcs[i][j] = lcs[i - 1][j];
199 directions[i][j] = BASE;
201 if (newend->next)
202 newend = newend->next;
204 if (baseend->next)
205 baseend = baseend->next;
208 for (i = 0; i < origbaselen + 1; i++)
209 free(lcs[i]);
210 free(lcs);
212 /* At this point, baseend and newend point to the end of each lists */
213 i--;
214 j--;
215 while (i != 0 || j != 0) {
216 if (directions[i][j] == MATCH) {
217 baseend->parent_map |= 1<<parent;
218 baseend = baseend->prev;
219 newend = newend->prev;
220 i--;
221 j--;
222 } else if (directions[i][j] == NEW) {
223 struct lline *lline;
225 lline = newend;
226 /* Remove lline from new list and update newend */
227 if (lline->prev)
228 lline->prev->next = lline->next;
229 else
230 new = lline->next;
231 if (lline->next)
232 lline->next->prev = lline->prev;
234 newend = lline->prev;
235 j--;
237 /* Add lline to base list */
238 if (baseend) {
239 lline->next = baseend->next;
240 lline->prev = baseend;
241 if (lline->prev)
242 lline->prev->next = lline;
244 else {
245 lline->next = base;
246 base = lline;
248 (*lenbase)++;
250 if (lline->next)
251 lline->next->prev = lline;
253 } else {
254 baseend = baseend->prev;
255 i--;
259 newend = new;
260 while (newend) {
261 struct lline *lline = newend;
262 newend = newend->next;
263 free(lline);
266 for (i = 0; i < origbaselen + 1; i++)
267 free(directions[i]);
268 free(directions);
270 return base;
273 static char *grab_blob(const unsigned char *sha1, unsigned int mode,
274 unsigned long *size, struct userdiff_driver *textconv,
275 const char *path)
277 char *blob;
278 enum object_type type;
280 if (S_ISGITLINK(mode)) {
281 blob = xmalloc(100);
282 *size = snprintf(blob, 100,
283 "Subproject commit %s\n", sha1_to_hex(sha1));
284 } else if (is_null_sha1(sha1)) {
285 /* deleted blob */
286 *size = 0;
287 return xcalloc(1, 1);
288 } else if (textconv) {
289 struct diff_filespec *df = alloc_filespec(path);
290 fill_filespec(df, sha1, 1, mode);
291 *size = fill_textconv(textconv, df, &blob);
292 free_filespec(df);
293 } else {
294 blob = read_sha1_file(sha1, &type, size);
295 if (type != OBJ_BLOB)
296 die("object '%s' is not a blob!", sha1_to_hex(sha1));
298 return blob;
301 static void append_lost(struct sline *sline, int n, const char *line, int len)
303 struct lline *lline;
304 unsigned long this_mask = (1UL<<n);
305 if (line[len-1] == '\n')
306 len--;
308 lline = xmalloc(sizeof(*lline) + len + 1);
309 lline->len = len;
310 lline->next = NULL;
311 lline->prev = sline->plost.lost_tail;
312 if (lline->prev)
313 lline->prev->next = lline;
314 else
315 sline->plost.lost_head = lline;
316 sline->plost.lost_tail = lline;
317 sline->plost.len++;
318 lline->parent_map = this_mask;
319 memcpy(lline->line, line, len);
320 lline->line[len] = 0;
323 struct combine_diff_state {
324 unsigned int lno;
325 int ob, on, nb, nn;
326 unsigned long nmask;
327 int num_parent;
328 int n;
329 struct sline *sline;
330 struct sline *lost_bucket;
333 static void consume_line(void *state_, char *line, unsigned long len)
335 struct combine_diff_state *state = state_;
336 if (5 < len && !memcmp("@@ -", line, 4)) {
337 if (parse_hunk_header(line, len,
338 &state->ob, &state->on,
339 &state->nb, &state->nn))
340 return;
341 state->lno = state->nb;
342 if (state->nn == 0) {
343 /* @@ -X,Y +N,0 @@ removed Y lines
344 * that would have come *after* line N
345 * in the result. Our lost buckets hang
346 * to the line after the removed lines,
348 * Note that this is correct even when N == 0,
349 * in which case the hunk removes the first
350 * line in the file.
352 state->lost_bucket = &state->sline[state->nb];
353 if (!state->nb)
354 state->nb = 1;
355 } else {
356 state->lost_bucket = &state->sline[state->nb-1];
358 if (!state->sline[state->nb-1].p_lno)
359 state->sline[state->nb-1].p_lno =
360 xcalloc(state->num_parent,
361 sizeof(unsigned long));
362 state->sline[state->nb-1].p_lno[state->n] = state->ob;
363 return;
365 if (!state->lost_bucket)
366 return; /* not in any hunk yet */
367 switch (line[0]) {
368 case '-':
369 append_lost(state->lost_bucket, state->n, line+1, len-1);
370 break;
371 case '+':
372 state->sline[state->lno-1].flag |= state->nmask;
373 state->lno++;
374 break;
378 static void combine_diff(const unsigned char *parent, unsigned int mode,
379 mmfile_t *result_file,
380 struct sline *sline, unsigned int cnt, int n,
381 int num_parent, int result_deleted,
382 struct userdiff_driver *textconv,
383 const char *path, long flags)
385 unsigned int p_lno, lno;
386 unsigned long nmask = (1UL << n);
387 xpparam_t xpp;
388 xdemitconf_t xecfg;
389 mmfile_t parent_file;
390 struct combine_diff_state state;
391 unsigned long sz;
393 if (result_deleted)
394 return; /* result deleted */
396 parent_file.ptr = grab_blob(parent, mode, &sz, textconv, path);
397 parent_file.size = sz;
398 memset(&xpp, 0, sizeof(xpp));
399 xpp.flags = flags;
400 memset(&xecfg, 0, sizeof(xecfg));
401 memset(&state, 0, sizeof(state));
402 state.nmask = nmask;
403 state.sline = sline;
404 state.lno = 1;
405 state.num_parent = num_parent;
406 state.n = n;
408 xdi_diff_outf(&parent_file, result_file, consume_line, &state,
409 &xpp, &xecfg);
410 free(parent_file.ptr);
412 /* Assign line numbers for this parent.
414 * sline[lno].p_lno[n] records the first line number
415 * (counting from 1) for parent N if the final hunk display
416 * started by showing sline[lno] (possibly showing the lost
417 * lines attached to it first).
419 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
420 struct lline *ll;
421 sline[lno].p_lno[n] = p_lno;
423 /* Coalesce new lines */
424 if (sline[lno].plost.lost_head) {
425 struct sline *sl = &sline[lno];
426 sl->lost = coalesce_lines(sl->lost, &sl->lenlost,
427 sl->plost.lost_head,
428 sl->plost.len, n, flags);
429 sl->plost.lost_head = sl->plost.lost_tail = NULL;
430 sl->plost.len = 0;
433 /* How many lines would this sline advance the p_lno? */
434 ll = sline[lno].lost;
435 while (ll) {
436 if (ll->parent_map & nmask)
437 p_lno++; /* '-' means parent had it */
438 ll = ll->next;
440 if (lno < cnt && !(sline[lno].flag & nmask))
441 p_lno++; /* no '+' means parent had it */
443 sline[lno].p_lno[n] = p_lno; /* trailer */
446 static unsigned long context = 3;
447 static char combine_marker = '@';
449 static int interesting(struct sline *sline, unsigned long all_mask)
451 /* If some parents lost lines here, or if we have added to
452 * some parent, it is interesting.
454 return ((sline->flag & all_mask) || sline->lost);
457 static unsigned long adjust_hunk_tail(struct sline *sline,
458 unsigned long all_mask,
459 unsigned long hunk_begin,
460 unsigned long i)
462 /* i points at the first uninteresting line. If the last line
463 * of the hunk was interesting only because it has some
464 * deletion, then it is not all that interesting for the
465 * purpose of giving trailing context lines. This is because
466 * we output '-' line and then unmodified sline[i-1] itself in
467 * that case which gives us one extra context line.
469 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
470 i--;
471 return i;
474 static unsigned long find_next(struct sline *sline,
475 unsigned long mark,
476 unsigned long i,
477 unsigned long cnt,
478 int look_for_uninteresting)
480 /* We have examined up to i-1 and are about to look at i.
481 * Find next interesting or uninteresting line. Here,
482 * "interesting" does not mean interesting(), but marked by
483 * the give_context() function below (i.e. it includes context
484 * lines that are not interesting to interesting() function
485 * that are surrounded by interesting() ones.
487 while (i <= cnt)
488 if (look_for_uninteresting
489 ? !(sline[i].flag & mark)
490 : (sline[i].flag & mark))
491 return i;
492 else
493 i++;
494 return i;
497 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
499 unsigned long all_mask = (1UL<<num_parent) - 1;
500 unsigned long mark = (1UL<<num_parent);
501 unsigned long no_pre_delete = (2UL<<num_parent);
502 unsigned long i;
504 /* Two groups of interesting lines may have a short gap of
505 * uninteresting lines. Connect such groups to give them a
506 * bit of context.
508 * We first start from what the interesting() function says,
509 * and mark them with "mark", and paint context lines with the
510 * mark. So interesting() would still say false for such context
511 * lines but they are treated as "interesting" in the end.
513 i = find_next(sline, mark, 0, cnt, 0);
514 if (cnt < i)
515 return 0;
517 while (i <= cnt) {
518 unsigned long j = (context < i) ? (i - context) : 0;
519 unsigned long k;
521 /* Paint a few lines before the first interesting line. */
522 while (j < i) {
523 if (!(sline[j].flag & mark))
524 sline[j].flag |= no_pre_delete;
525 sline[j++].flag |= mark;
528 again:
529 /* we know up to i is to be included. where does the
530 * next uninteresting one start?
532 j = find_next(sline, mark, i, cnt, 1);
533 if (cnt < j)
534 break; /* the rest are all interesting */
536 /* lookahead context lines */
537 k = find_next(sline, mark, j, cnt, 0);
538 j = adjust_hunk_tail(sline, all_mask, i, j);
540 if (k < j + context) {
541 /* k is interesting and [j,k) are not, but
542 * paint them interesting because the gap is small.
544 while (j < k)
545 sline[j++].flag |= mark;
546 i = k;
547 goto again;
550 /* j is the first uninteresting line and there is
551 * no overlap beyond it within context lines. Paint
552 * the trailing edge a bit.
554 i = k;
555 k = (j + context < cnt+1) ? j + context : cnt+1;
556 while (j < k)
557 sline[j++].flag |= mark;
559 return 1;
562 static int make_hunks(struct sline *sline, unsigned long cnt,
563 int num_parent, int dense)
565 unsigned long all_mask = (1UL<<num_parent) - 1;
566 unsigned long mark = (1UL<<num_parent);
567 unsigned long i;
568 int has_interesting = 0;
570 for (i = 0; i <= cnt; i++) {
571 if (interesting(&sline[i], all_mask))
572 sline[i].flag |= mark;
573 else
574 sline[i].flag &= ~mark;
576 if (!dense)
577 return give_context(sline, cnt, num_parent);
579 /* Look at each hunk, and if we have changes from only one
580 * parent, or the changes are the same from all but one
581 * parent, mark that uninteresting.
583 i = 0;
584 while (i <= cnt) {
585 unsigned long j, hunk_begin, hunk_end;
586 unsigned long same_diff;
587 while (i <= cnt && !(sline[i].flag & mark))
588 i++;
589 if (cnt < i)
590 break; /* No more interesting hunks */
591 hunk_begin = i;
592 for (j = i + 1; j <= cnt; j++) {
593 if (!(sline[j].flag & mark)) {
594 /* Look beyond the end to see if there
595 * is an interesting line after this
596 * hunk within context span.
598 unsigned long la; /* lookahead */
599 int contin = 0;
600 la = adjust_hunk_tail(sline, all_mask,
601 hunk_begin, j);
602 la = (la + context < cnt + 1) ?
603 (la + context) : cnt + 1;
604 while (la && j <= --la) {
605 if (sline[la].flag & mark) {
606 contin = 1;
607 break;
610 if (!contin)
611 break;
612 j = la;
615 hunk_end = j;
617 /* [i..hunk_end) are interesting. Now is it really
618 * interesting? We check if there are only two versions
619 * and the result matches one of them. That is, we look
620 * at:
621 * (+) line, which records lines added to which parents;
622 * this line appears in the result.
623 * (-) line, which records from what parents the line
624 * was removed; this line does not appear in the result.
625 * then check the set of parents the result has difference
626 * from, from all lines. If there are lines that has
627 * different set of parents that the result has differences
628 * from, that means we have more than two versions.
630 * Even when we have only two versions, if the result does
631 * not match any of the parents, the it should be considered
632 * interesting. In such a case, we would have all '+' line.
633 * After passing the above "two versions" test, that would
634 * appear as "the same set of parents" to be "all parents".
636 same_diff = 0;
637 has_interesting = 0;
638 for (j = i; j < hunk_end && !has_interesting; j++) {
639 unsigned long this_diff = sline[j].flag & all_mask;
640 struct lline *ll = sline[j].lost;
641 if (this_diff) {
642 /* This has some changes. Is it the
643 * same as others?
645 if (!same_diff)
646 same_diff = this_diff;
647 else if (same_diff != this_diff) {
648 has_interesting = 1;
649 break;
652 while (ll && !has_interesting) {
653 /* Lost this line from these parents;
654 * who are they? Are they the same?
656 this_diff = ll->parent_map;
657 if (!same_diff)
658 same_diff = this_diff;
659 else if (same_diff != this_diff) {
660 has_interesting = 1;
662 ll = ll->next;
666 if (!has_interesting && same_diff != all_mask) {
667 /* This hunk is not that interesting after all */
668 for (j = hunk_begin; j < hunk_end; j++)
669 sline[j].flag &= ~mark;
671 i = hunk_end;
674 has_interesting = give_context(sline, cnt, num_parent);
675 return has_interesting;
678 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
680 l0 = sline[l0].p_lno[n];
681 l1 = sline[l1].p_lno[n];
682 printf(" -%lu,%lu", l0, l1-l0-null_context);
685 static int hunk_comment_line(const char *bol)
687 int ch;
689 if (!bol)
690 return 0;
691 ch = *bol & 0xff;
692 return (isalpha(ch) || ch == '_' || ch == '$');
695 static void show_line_to_eol(const char *line, int len, const char *reset)
697 int saw_cr_at_eol = 0;
698 if (len < 0)
699 len = strlen(line);
700 saw_cr_at_eol = (len && line[len-1] == '\r');
702 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
703 reset,
704 saw_cr_at_eol ? "\r" : "");
707 static void dump_sline(struct sline *sline, const char *line_prefix,
708 unsigned long cnt, int num_parent,
709 int use_color, int result_deleted)
711 unsigned long mark = (1UL<<num_parent);
712 unsigned long no_pre_delete = (2UL<<num_parent);
713 int i;
714 unsigned long lno = 0;
715 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
716 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
717 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
718 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
719 const char *c_plain = diff_get_color(use_color, DIFF_PLAIN);
720 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
722 if (result_deleted)
723 return; /* result deleted */
725 while (1) {
726 unsigned long hunk_end;
727 unsigned long rlines;
728 const char *hunk_comment = NULL;
729 unsigned long null_context = 0;
731 while (lno <= cnt && !(sline[lno].flag & mark)) {
732 if (hunk_comment_line(sline[lno].bol))
733 hunk_comment = sline[lno].bol;
734 lno++;
736 if (cnt < lno)
737 break;
738 else {
739 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
740 if (!(sline[hunk_end].flag & mark))
741 break;
743 rlines = hunk_end - lno;
744 if (cnt < hunk_end)
745 rlines--; /* pointing at the last delete hunk */
747 if (!context) {
749 * Even when running with --unified=0, all
750 * lines in the hunk needs to be processed in
751 * the loop below in order to show the
752 * deletion recorded in lost_head. However,
753 * we do not want to show the resulting line
754 * with all blank context markers in such a
755 * case. Compensate.
757 unsigned long j;
758 for (j = lno; j < hunk_end; j++)
759 if (!(sline[j].flag & (mark-1)))
760 null_context++;
761 rlines -= null_context;
764 printf("%s%s", line_prefix, c_frag);
765 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
766 for (i = 0; i < num_parent; i++)
767 show_parent_lno(sline, lno, hunk_end, i, null_context);
768 printf(" +%lu,%lu ", lno+1, rlines);
769 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
771 if (hunk_comment) {
772 int comment_end = 0;
773 for (i = 0; i < 40; i++) {
774 int ch = hunk_comment[i] & 0xff;
775 if (!ch || ch == '\n')
776 break;
777 if (!isspace(ch))
778 comment_end = i;
780 if (comment_end)
781 printf("%s%s %s%s", c_reset,
782 c_plain, c_reset,
783 c_func);
784 for (i = 0; i < comment_end; i++)
785 putchar(hunk_comment[i]);
788 printf("%s\n", c_reset);
789 while (lno < hunk_end) {
790 struct lline *ll;
791 int j;
792 unsigned long p_mask;
793 struct sline *sl = &sline[lno++];
794 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost;
795 while (ll) {
796 printf("%s%s", line_prefix, c_old);
797 for (j = 0; j < num_parent; j++) {
798 if (ll->parent_map & (1UL<<j))
799 putchar('-');
800 else
801 putchar(' ');
803 show_line_to_eol(ll->line, -1, c_reset);
804 ll = ll->next;
806 if (cnt < lno)
807 break;
808 p_mask = 1;
809 fputs(line_prefix, stdout);
810 if (!(sl->flag & (mark-1))) {
812 * This sline was here to hang the
813 * lost lines in front of it.
815 if (!context)
816 continue;
817 fputs(c_plain, stdout);
819 else
820 fputs(c_new, stdout);
821 for (j = 0; j < num_parent; j++) {
822 if (p_mask & sl->flag)
823 putchar('+');
824 else
825 putchar(' ');
826 p_mask <<= 1;
828 show_line_to_eol(sl->bol, sl->len, c_reset);
833 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
834 int i, int j)
836 /* We have already examined parent j and we know parent i
837 * and parent j are the same, so reuse the combined result
838 * of parent j for parent i.
840 unsigned long lno, imask, jmask;
841 imask = (1UL<<i);
842 jmask = (1UL<<j);
844 for (lno = 0; lno <= cnt; lno++) {
845 struct lline *ll = sline->lost;
846 sline->p_lno[i] = sline->p_lno[j];
847 while (ll) {
848 if (ll->parent_map & jmask)
849 ll->parent_map |= imask;
850 ll = ll->next;
852 if (sline->flag & jmask)
853 sline->flag |= imask;
854 sline++;
856 /* the overall size of the file (sline[cnt]) */
857 sline->p_lno[i] = sline->p_lno[j];
860 static void dump_quoted_path(const char *head,
861 const char *prefix,
862 const char *path,
863 const char *line_prefix,
864 const char *c_meta, const char *c_reset)
866 static struct strbuf buf = STRBUF_INIT;
868 strbuf_reset(&buf);
869 strbuf_addstr(&buf, line_prefix);
870 strbuf_addstr(&buf, c_meta);
871 strbuf_addstr(&buf, head);
872 quote_two_c_style(&buf, prefix, path, 0);
873 strbuf_addstr(&buf, c_reset);
874 puts(buf.buf);
877 static void show_combined_header(struct combine_diff_path *elem,
878 int num_parent,
879 int dense,
880 struct rev_info *rev,
881 const char *line_prefix,
882 int mode_differs,
883 int show_file_header)
885 struct diff_options *opt = &rev->diffopt;
886 int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
887 const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
888 const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
889 const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
890 const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
891 const char *abb;
892 int added = 0;
893 int deleted = 0;
894 int i;
896 if (rev->loginfo && !rev->no_commit_id)
897 show_log(rev);
899 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
900 "", elem->path, line_prefix, c_meta, c_reset);
901 printf("%s%sindex ", line_prefix, c_meta);
902 for (i = 0; i < num_parent; i++) {
903 abb = find_unique_abbrev(elem->parent[i].sha1,
904 abbrev);
905 printf("%s%s", i ? "," : "", abb);
907 abb = find_unique_abbrev(elem->sha1, abbrev);
908 printf("..%s%s\n", abb, c_reset);
910 if (mode_differs) {
911 deleted = !elem->mode;
913 /* We say it was added if nobody had it */
914 added = !deleted;
915 for (i = 0; added && i < num_parent; i++)
916 if (elem->parent[i].status !=
917 DIFF_STATUS_ADDED)
918 added = 0;
919 if (added)
920 printf("%s%snew file mode %06o",
921 line_prefix, c_meta, elem->mode);
922 else {
923 if (deleted)
924 printf("%s%sdeleted file ",
925 line_prefix, c_meta);
926 printf("mode ");
927 for (i = 0; i < num_parent; i++) {
928 printf("%s%06o", i ? "," : "",
929 elem->parent[i].mode);
931 if (elem->mode)
932 printf("..%06o", elem->mode);
934 printf("%s\n", c_reset);
937 if (!show_file_header)
938 return;
940 if (added)
941 dump_quoted_path("--- ", "", "/dev/null",
942 line_prefix, c_meta, c_reset);
943 else
944 dump_quoted_path("--- ", a_prefix, elem->path,
945 line_prefix, c_meta, c_reset);
946 if (deleted)
947 dump_quoted_path("+++ ", "", "/dev/null",
948 line_prefix, c_meta, c_reset);
949 else
950 dump_quoted_path("+++ ", b_prefix, elem->path,
951 line_prefix, c_meta, c_reset);
954 static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
955 int dense, int working_tree_file,
956 struct rev_info *rev)
958 struct diff_options *opt = &rev->diffopt;
959 unsigned long result_size, cnt, lno;
960 int result_deleted = 0;
961 char *result, *cp;
962 struct sline *sline; /* survived lines */
963 int mode_differs = 0;
964 int i, show_hunks;
965 mmfile_t result_file;
966 struct userdiff_driver *userdiff;
967 struct userdiff_driver *textconv = NULL;
968 int is_binary;
969 const char *line_prefix = diff_line_prefix(opt);
971 context = opt->context;
972 userdiff = userdiff_find_by_path(elem->path);
973 if (!userdiff)
974 userdiff = userdiff_find_by_name("default");
975 if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV))
976 textconv = userdiff_get_textconv(userdiff);
978 /* Read the result of merge first */
979 if (!working_tree_file)
980 result = grab_blob(elem->sha1, elem->mode, &result_size,
981 textconv, elem->path);
982 else {
983 /* Used by diff-tree to read from the working tree */
984 struct stat st;
985 int fd = -1;
987 if (lstat(elem->path, &st) < 0)
988 goto deleted_file;
990 if (S_ISLNK(st.st_mode)) {
991 struct strbuf buf = STRBUF_INIT;
993 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
994 error("readlink(%s): %s", elem->path,
995 strerror(errno));
996 return;
998 result_size = buf.len;
999 result = strbuf_detach(&buf, NULL);
1000 elem->mode = canon_mode(st.st_mode);
1001 } else if (S_ISDIR(st.st_mode)) {
1002 unsigned char sha1[20];
1003 if (resolve_gitlink_ref(elem->path, "HEAD", sha1) < 0)
1004 result = grab_blob(elem->sha1, elem->mode,
1005 &result_size, NULL, NULL);
1006 else
1007 result = grab_blob(sha1, elem->mode,
1008 &result_size, NULL, NULL);
1009 } else if (textconv) {
1010 struct diff_filespec *df = alloc_filespec(elem->path);
1011 fill_filespec(df, null_sha1, 0, st.st_mode);
1012 result_size = fill_textconv(textconv, df, &result);
1013 free_filespec(df);
1014 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
1015 size_t len = xsize_t(st.st_size);
1016 ssize_t done;
1017 int is_file, i;
1019 elem->mode = canon_mode(st.st_mode);
1020 /* if symlinks don't work, assume symlink if all parents
1021 * are symlinks
1023 is_file = has_symlinks;
1024 for (i = 0; !is_file && i < num_parent; i++)
1025 is_file = !S_ISLNK(elem->parent[i].mode);
1026 if (!is_file)
1027 elem->mode = canon_mode(S_IFLNK);
1029 result_size = len;
1030 result = xmalloc(len + 1);
1032 done = read_in_full(fd, result, len);
1033 if (done < 0)
1034 die_errno("read error '%s'", elem->path);
1035 else if (done < len)
1036 die("early EOF '%s'", elem->path);
1038 result[len] = 0;
1040 /* If not a fake symlink, apply filters, e.g. autocrlf */
1041 if (is_file) {
1042 struct strbuf buf = STRBUF_INIT;
1044 if (convert_to_git(elem->path, result, len, &buf, safe_crlf)) {
1045 free(result);
1046 result = strbuf_detach(&buf, &len);
1047 result_size = len;
1051 else {
1052 deleted_file:
1053 result_deleted = 1;
1054 result_size = 0;
1055 elem->mode = 0;
1056 result = xcalloc(1, 1);
1059 if (0 <= fd)
1060 close(fd);
1063 for (i = 0; i < num_parent; i++) {
1064 if (elem->parent[i].mode != elem->mode) {
1065 mode_differs = 1;
1066 break;
1070 if (textconv)
1071 is_binary = 0;
1072 else if (userdiff->binary != -1)
1073 is_binary = userdiff->binary;
1074 else {
1075 is_binary = buffer_is_binary(result, result_size);
1076 for (i = 0; !is_binary && i < num_parent; i++) {
1077 char *buf;
1078 unsigned long size;
1079 buf = grab_blob(elem->parent[i].sha1,
1080 elem->parent[i].mode,
1081 &size, NULL, NULL);
1082 if (buffer_is_binary(buf, size))
1083 is_binary = 1;
1084 free(buf);
1087 if (is_binary) {
1088 show_combined_header(elem, num_parent, dense, rev,
1089 line_prefix, mode_differs, 0);
1090 printf("Binary files differ\n");
1091 free(result);
1092 return;
1095 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
1096 if (*cp == '\n')
1097 cnt++;
1099 if (result_size && result[result_size-1] != '\n')
1100 cnt++; /* incomplete line */
1102 sline = xcalloc(cnt+2, sizeof(*sline));
1103 sline[0].bol = result;
1104 for (lno = 0, cp = result; cp < result + result_size; cp++) {
1105 if (*cp == '\n') {
1106 sline[lno].len = cp - sline[lno].bol;
1107 lno++;
1108 if (lno < cnt)
1109 sline[lno].bol = cp + 1;
1112 if (result_size && result[result_size-1] != '\n')
1113 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
1115 result_file.ptr = result;
1116 result_file.size = result_size;
1118 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1119 * for deletion hunk at the end.
1121 sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
1122 for (lno = 0; lno <= cnt; lno++)
1123 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
1125 for (i = 0; i < num_parent; i++) {
1126 int j;
1127 for (j = 0; j < i; j++) {
1128 if (!hashcmp(elem->parent[i].sha1,
1129 elem->parent[j].sha1)) {
1130 reuse_combine_diff(sline, cnt, i, j);
1131 break;
1134 if (i <= j)
1135 combine_diff(elem->parent[i].sha1,
1136 elem->parent[i].mode,
1137 &result_file, sline,
1138 cnt, i, num_parent, result_deleted,
1139 textconv, elem->path, opt->xdl_opts);
1142 show_hunks = make_hunks(sline, cnt, num_parent, dense);
1144 if (show_hunks || mode_differs || working_tree_file) {
1145 show_combined_header(elem, num_parent, dense, rev,
1146 line_prefix, mode_differs, 1);
1147 dump_sline(sline, line_prefix, cnt, num_parent,
1148 opt->use_color, result_deleted);
1150 free(result);
1152 for (lno = 0; lno < cnt; lno++) {
1153 if (sline[lno].lost) {
1154 struct lline *ll = sline[lno].lost;
1155 while (ll) {
1156 struct lline *tmp = ll;
1157 ll = ll->next;
1158 free(tmp);
1162 free(sline[0].p_lno);
1163 free(sline);
1166 static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
1168 struct diff_options *opt = &rev->diffopt;
1169 int line_termination, inter_name_termination, i;
1170 const char *line_prefix = diff_line_prefix(opt);
1172 line_termination = opt->line_termination;
1173 inter_name_termination = '\t';
1174 if (!line_termination)
1175 inter_name_termination = 0;
1177 if (rev->loginfo && !rev->no_commit_id)
1178 show_log(rev);
1181 if (opt->output_format & DIFF_FORMAT_RAW) {
1182 printf("%s", line_prefix);
1184 /* As many colons as there are parents */
1185 for (i = 0; i < num_parent; i++)
1186 putchar(':');
1188 /* Show the modes */
1189 for (i = 0; i < num_parent; i++)
1190 printf("%06o ", p->parent[i].mode);
1191 printf("%06o", p->mode);
1193 /* Show sha1's */
1194 for (i = 0; i < num_parent; i++)
1195 printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
1196 opt->abbrev));
1197 printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
1200 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
1201 for (i = 0; i < num_parent; i++)
1202 putchar(p->parent[i].status);
1203 putchar(inter_name_termination);
1206 write_name_quoted(p->path, stdout, line_termination);
1210 * The result (p->elem) is from the working tree and their
1211 * parents are typically from multiple stages during a merge
1212 * (i.e. diff-files) or the state in HEAD and in the index
1213 * (i.e. diff-index).
1215 void show_combined_diff(struct combine_diff_path *p,
1216 int num_parent,
1217 int dense,
1218 struct rev_info *rev)
1220 struct diff_options *opt = &rev->diffopt;
1222 if (!p->len)
1223 return;
1224 if (opt->output_format & (DIFF_FORMAT_RAW |
1225 DIFF_FORMAT_NAME |
1226 DIFF_FORMAT_NAME_STATUS))
1227 show_raw_diff(p, num_parent, rev);
1228 else if (opt->output_format & DIFF_FORMAT_PATCH)
1229 show_patch_diff(p, num_parent, dense, 1, rev);
1232 static void free_combined_pair(struct diff_filepair *pair)
1234 free(pair->two);
1235 free(pair);
1239 * A combine_diff_path expresses N parents on the LHS against 1 merge
1240 * result. Synthesize a diff_filepair that has N entries on the "one"
1241 * side and 1 entry on the "two" side.
1243 * In the future, we might want to add more data to combine_diff_path
1244 * so that we can fill fields we are ignoring (most notably, size) here,
1245 * but currently nobody uses it, so this should suffice for now.
1247 static struct diff_filepair *combined_pair(struct combine_diff_path *p,
1248 int num_parent)
1250 int i;
1251 struct diff_filepair *pair;
1252 struct diff_filespec *pool;
1254 pair = xmalloc(sizeof(*pair));
1255 pool = xcalloc(num_parent + 1, sizeof(struct diff_filespec));
1256 pair->one = pool + 1;
1257 pair->two = pool;
1259 for (i = 0; i < num_parent; i++) {
1260 pair->one[i].path = p->path;
1261 pair->one[i].mode = p->parent[i].mode;
1262 hashcpy(pair->one[i].sha1, p->parent[i].sha1);
1263 pair->one[i].sha1_valid = !is_null_sha1(p->parent[i].sha1);
1264 pair->one[i].has_more_entries = 1;
1266 pair->one[num_parent - 1].has_more_entries = 0;
1268 pair->two->path = p->path;
1269 pair->two->mode = p->mode;
1270 hashcpy(pair->two->sha1, p->sha1);
1271 pair->two->sha1_valid = !is_null_sha1(p->sha1);
1272 return pair;
1275 static void handle_combined_callback(struct diff_options *opt,
1276 struct combine_diff_path *paths,
1277 int num_parent,
1278 int num_paths)
1280 struct combine_diff_path *p;
1281 struct diff_queue_struct q;
1282 int i;
1284 q.queue = xcalloc(num_paths, sizeof(struct diff_filepair *));
1285 q.alloc = num_paths;
1286 q.nr = num_paths;
1287 for (i = 0, p = paths; p; p = p->next) {
1288 if (!p->len)
1289 continue;
1290 q.queue[i++] = combined_pair(p, num_parent);
1292 opt->format_callback(&q, opt, opt->format_callback_data);
1293 for (i = 0; i < num_paths; i++)
1294 free_combined_pair(q.queue[i]);
1295 free(q.queue);
1298 void diff_tree_combined(const unsigned char *sha1,
1299 const struct sha1_array *parents,
1300 int dense,
1301 struct rev_info *rev)
1303 struct diff_options *opt = &rev->diffopt;
1304 struct diff_options diffopts;
1305 struct combine_diff_path *p, *paths = NULL;
1306 int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
1308 diffopts = *opt;
1309 copy_pathspec(&diffopts.pathspec, &opt->pathspec);
1310 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
1311 DIFF_OPT_SET(&diffopts, RECURSIVE);
1312 DIFF_OPT_CLR(&diffopts, ALLOW_EXTERNAL);
1314 show_log_first = !!rev->loginfo && !rev->no_commit_id;
1315 needsep = 0;
1316 /* find set of paths that everybody touches */
1317 for (i = 0; i < num_parent; i++) {
1318 /* show stat against the first parent even
1319 * when doing combined diff.
1321 int stat_opt = (opt->output_format &
1322 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
1323 if (i == 0 && stat_opt)
1324 diffopts.output_format = stat_opt;
1325 else
1326 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
1327 diff_tree_sha1(parents->sha1[i], sha1, "", &diffopts);
1328 diffcore_std(&diffopts);
1329 paths = intersect_paths(paths, i, num_parent);
1331 if (show_log_first && i == 0) {
1332 show_log(rev);
1334 if (rev->verbose_header && opt->output_format)
1335 printf("%s%c", diff_line_prefix(opt),
1336 opt->line_termination);
1338 diff_flush(&diffopts);
1341 /* find out surviving paths */
1342 for (num_paths = 0, p = paths; p; p = p->next) {
1343 if (p->len)
1344 num_paths++;
1346 if (num_paths) {
1347 if (opt->output_format & (DIFF_FORMAT_RAW |
1348 DIFF_FORMAT_NAME |
1349 DIFF_FORMAT_NAME_STATUS)) {
1350 for (p = paths; p; p = p->next) {
1351 if (p->len)
1352 show_raw_diff(p, num_parent, rev);
1354 needsep = 1;
1356 else if (opt->output_format &
1357 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
1358 needsep = 1;
1359 else if (opt->output_format & DIFF_FORMAT_CALLBACK)
1360 handle_combined_callback(opt, paths, num_parent, num_paths);
1362 if (opt->output_format & DIFF_FORMAT_PATCH) {
1363 if (needsep)
1364 printf("%s%c", diff_line_prefix(opt),
1365 opt->line_termination);
1366 for (p = paths; p; p = p->next) {
1367 if (p->len)
1368 show_patch_diff(p, num_parent, dense,
1369 0, rev);
1374 /* Clean things up */
1375 while (paths) {
1376 struct combine_diff_path *tmp = paths;
1377 paths = paths->next;
1378 free(tmp);
1381 free_pathspec(&diffopts.pathspec);
1384 void diff_tree_combined_merge(const struct commit *commit, int dense,
1385 struct rev_info *rev)
1387 struct commit_list *parent = get_saved_parents(rev, commit);
1388 struct sha1_array parents = SHA1_ARRAY_INIT;
1390 while (parent) {
1391 sha1_array_append(&parents, parent->item->object.sha1);
1392 parent = parent->next;
1394 diff_tree_combined(commit->object.sha1, &parents, dense, rev);
1395 sha1_array_clear(&parents);