Merge branch 'sn/typo-grammo-phraso-fixes'
[git.git] / combine-diff.c
blobf90f442482932e618cc7399924c00ddde8c50eaf
1 #include "git-compat-util.h"
2 #include "object-store-ll.h"
3 #include "commit.h"
4 #include "convert.h"
5 #include "blob.h"
6 #include "diff.h"
7 #include "diffcore.h"
8 #include "environment.h"
9 #include "hex.h"
10 #include "object-name.h"
11 #include "quote.h"
12 #include "xdiff-interface.h"
13 #include "xdiff/xmacros.h"
14 #include "log-tree.h"
15 #include "refs.h"
16 #include "tree.h"
17 #include "userdiff.h"
18 #include "oid-array.h"
19 #include "revision.h"
21 static int compare_paths(const struct combine_diff_path *one,
22 const struct diff_filespec *two)
24 if (!S_ISDIR(one->mode) && !S_ISDIR(two->mode))
25 return strcmp(one->path, two->path);
27 return base_name_compare(one->path, strlen(one->path), one->mode,
28 two->path, strlen(two->path), two->mode);
31 static int filename_changed(char status)
33 return status == 'R' || status == 'C';
36 static struct combine_diff_path *intersect_paths(
37 struct combine_diff_path *curr,
38 int n,
39 int num_parent,
40 int combined_all_paths)
42 struct diff_queue_struct *q = &diff_queued_diff;
43 struct combine_diff_path *p, **tail = &curr;
44 int i, j, cmp;
46 if (!n) {
47 for (i = 0; i < q->nr; i++) {
48 int len;
49 const char *path;
50 if (diff_unmodified_pair(q->queue[i]))
51 continue;
52 path = q->queue[i]->two->path;
53 len = strlen(path);
54 p = xmalloc(combine_diff_path_size(num_parent, len));
55 p->path = (char *) &(p->parent[num_parent]);
56 memcpy(p->path, path, len);
57 p->path[len] = 0;
58 p->next = NULL;
59 memset(p->parent, 0,
60 sizeof(p->parent[0]) * num_parent);
62 oidcpy(&p->oid, &q->queue[i]->two->oid);
63 p->mode = q->queue[i]->two->mode;
64 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
65 p->parent[n].mode = q->queue[i]->one->mode;
66 p->parent[n].status = q->queue[i]->status;
68 if (combined_all_paths &&
69 filename_changed(p->parent[n].status)) {
70 strbuf_init(&p->parent[n].path, 0);
71 strbuf_addstr(&p->parent[n].path,
72 q->queue[i]->one->path);
74 *tail = p;
75 tail = &p->next;
77 return curr;
81 * paths in curr (linked list) and q->queue[] (array) are
82 * both sorted in the tree order.
84 i = 0;
85 while ((p = *tail) != NULL) {
86 cmp = ((i >= q->nr)
87 ? -1 : compare_paths(p, q->queue[i]->two));
89 if (cmp < 0) {
90 /* p->path not in q->queue[]; drop it */
91 *tail = p->next;
92 for (j = 0; j < num_parent; j++)
93 if (combined_all_paths &&
94 filename_changed(p->parent[j].status))
95 strbuf_release(&p->parent[j].path);
96 free(p);
97 continue;
100 if (cmp > 0) {
101 /* q->queue[i] not in p->path; skip it */
102 i++;
103 continue;
106 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
107 p->parent[n].mode = q->queue[i]->one->mode;
108 p->parent[n].status = q->queue[i]->status;
109 if (combined_all_paths &&
110 filename_changed(p->parent[n].status))
111 strbuf_addstr(&p->parent[n].path,
112 q->queue[i]->one->path);
114 tail = &p->next;
115 i++;
117 return curr;
120 /* Lines lost from parent */
121 struct lline {
122 struct lline *next, *prev;
123 int len;
124 unsigned long parent_map;
125 char line[FLEX_ARRAY];
128 /* Lines lost from current parent (before coalescing) */
129 struct plost {
130 struct lline *lost_head, *lost_tail;
131 int len;
134 /* Lines surviving in the merge result */
135 struct sline {
136 /* Accumulated and coalesced lost lines */
137 struct lline *lost;
138 int lenlost;
139 struct plost plost;
140 char *bol;
141 int len;
142 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
143 * we did not change it).
144 * bit N is used for "interesting" lines, including context.
145 * bit (N+1) is used for "do not show deletion before this".
147 unsigned long flag;
148 unsigned long *p_lno;
151 static int match_string_spaces(const char *line1, int len1,
152 const char *line2, int len2,
153 long flags)
155 if (flags & XDF_WHITESPACE_FLAGS) {
156 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
157 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
160 if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
161 return (len1 == len2 && !memcmp(line1, line2, len1));
163 while (len1 > 0 && len2 > 0) {
164 len1--;
165 len2--;
166 if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
167 if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
168 (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
169 return 0;
171 for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
172 for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
174 if (line1[len1] != line2[len2])
175 return 0;
178 if (flags & XDF_IGNORE_WHITESPACE) {
179 /* Consume remaining spaces */
180 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
181 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
184 /* We matched full line1 and line2 */
185 if (!len1 && !len2)
186 return 1;
188 return 0;
191 enum coalesce_direction { MATCH, BASE, NEW };
193 /* Coalesce new lines into base by finding LCS */
194 static struct lline *coalesce_lines(struct lline *base, int *lenbase,
195 struct lline *newline, int lennew,
196 unsigned long parent, long flags)
198 int **lcs;
199 enum coalesce_direction **directions;
200 struct lline *baseend, *newend = NULL;
201 int i, j, origbaselen = *lenbase;
203 if (!newline)
204 return base;
206 if (!base) {
207 *lenbase = lennew;
208 return newline;
212 * Coalesce new lines into base by finding the LCS
213 * - Create the table to run dynamic programming
214 * - Compute the LCS
215 * - Then reverse read the direction structure:
216 * - If we have MATCH, assign parent to base flag, and consume
217 * both baseend and newend
218 * - Else if we have BASE, consume baseend
219 * - Else if we have NEW, insert newend lline into base and
220 * consume newend
222 CALLOC_ARRAY(lcs, st_add(origbaselen, 1));
223 CALLOC_ARRAY(directions, st_add(origbaselen, 1));
224 for (i = 0; i < origbaselen + 1; i++) {
225 CALLOC_ARRAY(lcs[i], st_add(lennew, 1));
226 CALLOC_ARRAY(directions[i], st_add(lennew, 1));
227 directions[i][0] = BASE;
229 for (j = 1; j < lennew + 1; j++)
230 directions[0][j] = NEW;
232 for (i = 1, baseend = base; i < origbaselen + 1; i++) {
233 for (j = 1, newend = newline; j < lennew + 1; j++) {
234 if (match_string_spaces(baseend->line, baseend->len,
235 newend->line, newend->len, flags)) {
236 lcs[i][j] = lcs[i - 1][j - 1] + 1;
237 directions[i][j] = MATCH;
238 } else if (lcs[i][j - 1] >= lcs[i - 1][j]) {
239 lcs[i][j] = lcs[i][j - 1];
240 directions[i][j] = NEW;
241 } else {
242 lcs[i][j] = lcs[i - 1][j];
243 directions[i][j] = BASE;
245 if (newend->next)
246 newend = newend->next;
248 if (baseend->next)
249 baseend = baseend->next;
252 for (i = 0; i < origbaselen + 1; i++)
253 free(lcs[i]);
254 free(lcs);
256 /* At this point, baseend and newend point to the end of each lists */
257 i--;
258 j--;
259 while (i != 0 || j != 0) {
260 if (directions[i][j] == MATCH) {
261 baseend->parent_map |= 1<<parent;
262 baseend = baseend->prev;
263 newend = newend->prev;
264 i--;
265 j--;
266 } else if (directions[i][j] == NEW) {
267 struct lline *lline;
269 lline = newend;
270 /* Remove lline from new list and update newend */
271 if (lline->prev)
272 lline->prev->next = lline->next;
273 else
274 newline = lline->next;
275 if (lline->next)
276 lline->next->prev = lline->prev;
278 newend = lline->prev;
279 j--;
281 /* Add lline to base list */
282 if (baseend) {
283 lline->next = baseend->next;
284 lline->prev = baseend;
285 if (lline->prev)
286 lline->prev->next = lline;
288 else {
289 lline->next = base;
290 base = lline;
292 (*lenbase)++;
294 if (lline->next)
295 lline->next->prev = lline;
297 } else {
298 baseend = baseend->prev;
299 i--;
303 newend = newline;
304 while (newend) {
305 struct lline *lline = newend;
306 newend = newend->next;
307 free(lline);
310 for (i = 0; i < origbaselen + 1; i++)
311 free(directions[i]);
312 free(directions);
314 return base;
317 static char *grab_blob(struct repository *r,
318 const struct object_id *oid, unsigned int mode,
319 unsigned long *size, struct userdiff_driver *textconv,
320 const char *path)
322 char *blob;
323 enum object_type type;
325 if (S_ISGITLINK(mode)) {
326 struct strbuf buf = STRBUF_INIT;
327 strbuf_addf(&buf, "Subproject commit %s\n", oid_to_hex(oid));
328 *size = buf.len;
329 blob = strbuf_detach(&buf, NULL);
330 } else if (is_null_oid(oid)) {
331 /* deleted blob */
332 *size = 0;
333 return xcalloc(1, 1);
334 } else if (textconv) {
335 struct diff_filespec *df = alloc_filespec(path);
336 fill_filespec(df, oid, 1, mode);
337 *size = fill_textconv(r, textconv, df, &blob);
338 free_filespec(df);
339 } else {
340 blob = repo_read_object_file(r, oid, &type, size);
341 if (type != OBJ_BLOB)
342 die("object '%s' is not a blob!", oid_to_hex(oid));
344 return blob;
347 static void append_lost(struct sline *sline, int n, const char *line, int len)
349 struct lline *lline;
350 unsigned long this_mask = (1UL<<n);
351 if (line[len-1] == '\n')
352 len--;
354 FLEX_ALLOC_MEM(lline, line, line, len);
355 lline->len = len;
356 lline->next = NULL;
357 lline->prev = sline->plost.lost_tail;
358 if (lline->prev)
359 lline->prev->next = lline;
360 else
361 sline->plost.lost_head = lline;
362 sline->plost.lost_tail = lline;
363 sline->plost.len++;
364 lline->parent_map = this_mask;
367 struct combine_diff_state {
368 unsigned int lno;
369 int ob, on, nb, nn;
370 unsigned long nmask;
371 int num_parent;
372 int n;
373 struct sline *sline;
374 struct sline *lost_bucket;
377 static void consume_hunk(void *state_,
378 long ob, long on,
379 long nb, long nn,
380 const char *func UNUSED, long funclen UNUSED)
382 struct combine_diff_state *state = state_;
384 state->ob = ob;
385 state->on = on;
386 state->nb = nb;
387 state->nn = nn;
388 state->lno = state->nb;
389 if (state->nn == 0) {
390 /* @@ -X,Y +N,0 @@ removed Y lines
391 * that would have come *after* line N
392 * in the result. Our lost buckets hang
393 * to the line after the removed lines,
395 * Note that this is correct even when N == 0,
396 * in which case the hunk removes the first
397 * line in the file.
399 state->lost_bucket = &state->sline[state->nb];
400 if (!state->nb)
401 state->nb = 1;
402 } else {
403 state->lost_bucket = &state->sline[state->nb-1];
405 if (!state->sline[state->nb-1].p_lno)
406 CALLOC_ARRAY(state->sline[state->nb - 1].p_lno,
407 state->num_parent);
408 state->sline[state->nb-1].p_lno[state->n] = state->ob;
411 static int consume_line(void *state_, char *line, unsigned long len)
413 struct combine_diff_state *state = state_;
414 if (!state->lost_bucket)
415 return 0; /* not in any hunk yet */
416 switch (line[0]) {
417 case '-':
418 append_lost(state->lost_bucket, state->n, line+1, len-1);
419 break;
420 case '+':
421 state->sline[state->lno-1].flag |= state->nmask;
422 state->lno++;
423 break;
425 return 0;
428 static void combine_diff(struct repository *r,
429 const struct object_id *parent, unsigned int mode,
430 mmfile_t *result_file,
431 struct sline *sline, unsigned int cnt, int n,
432 int num_parent, int result_deleted,
433 struct userdiff_driver *textconv,
434 const char *path, long flags)
436 unsigned int p_lno, lno;
437 unsigned long nmask = (1UL << n);
438 xpparam_t xpp;
439 xdemitconf_t xecfg;
440 mmfile_t parent_file;
441 struct combine_diff_state state;
442 unsigned long sz;
444 if (result_deleted)
445 return; /* result deleted */
447 parent_file.ptr = grab_blob(r, parent, mode, &sz, textconv, path);
448 parent_file.size = sz;
449 memset(&xpp, 0, sizeof(xpp));
450 xpp.flags = flags;
451 memset(&xecfg, 0, sizeof(xecfg));
452 memset(&state, 0, sizeof(state));
453 state.nmask = nmask;
454 state.sline = sline;
455 state.lno = 1;
456 state.num_parent = num_parent;
457 state.n = n;
459 if (xdi_diff_outf(&parent_file, result_file, consume_hunk,
460 consume_line, &state, &xpp, &xecfg))
461 die("unable to generate combined diff for %s",
462 oid_to_hex(parent));
463 free(parent_file.ptr);
465 /* Assign line numbers for this parent.
467 * sline[lno].p_lno[n] records the first line number
468 * (counting from 1) for parent N if the final hunk display
469 * started by showing sline[lno] (possibly showing the lost
470 * lines attached to it first).
472 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
473 struct lline *ll;
474 sline[lno].p_lno[n] = p_lno;
476 /* Coalesce new lines */
477 if (sline[lno].plost.lost_head) {
478 struct sline *sl = &sline[lno];
479 sl->lost = coalesce_lines(sl->lost, &sl->lenlost,
480 sl->plost.lost_head,
481 sl->plost.len, n, flags);
482 sl->plost.lost_head = sl->plost.lost_tail = NULL;
483 sl->plost.len = 0;
486 /* How many lines would this sline advance the p_lno? */
487 ll = sline[lno].lost;
488 while (ll) {
489 if (ll->parent_map & nmask)
490 p_lno++; /* '-' means parent had it */
491 ll = ll->next;
493 if (lno < cnt && !(sline[lno].flag & nmask))
494 p_lno++; /* no '+' means parent had it */
496 sline[lno].p_lno[n] = p_lno; /* trailer */
499 static unsigned long context = 3;
500 static char combine_marker = '@';
502 static int interesting(struct sline *sline, unsigned long all_mask)
504 /* If some parents lost lines here, or if we have added to
505 * some parent, it is interesting.
507 return ((sline->flag & all_mask) || sline->lost);
510 static unsigned long adjust_hunk_tail(struct sline *sline,
511 unsigned long all_mask,
512 unsigned long hunk_begin,
513 unsigned long i)
515 /* i points at the first uninteresting line. If the last line
516 * of the hunk was interesting only because it has some
517 * deletion, then it is not all that interesting for the
518 * purpose of giving trailing context lines. This is because
519 * we output '-' line and then unmodified sline[i-1] itself in
520 * that case which gives us one extra context line.
522 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
523 i--;
524 return i;
527 static unsigned long find_next(struct sline *sline,
528 unsigned long mark,
529 unsigned long i,
530 unsigned long cnt,
531 int look_for_uninteresting)
533 /* We have examined up to i-1 and are about to look at i.
534 * Find next interesting or uninteresting line. Here,
535 * "interesting" does not mean interesting(), but marked by
536 * the give_context() function below (i.e. it includes context
537 * lines that are not interesting to interesting() function
538 * that are surrounded by interesting() ones.
540 while (i <= cnt)
541 if (look_for_uninteresting
542 ? !(sline[i].flag & mark)
543 : (sline[i].flag & mark))
544 return i;
545 else
546 i++;
547 return i;
550 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
552 unsigned long all_mask = (1UL<<num_parent) - 1;
553 unsigned long mark = (1UL<<num_parent);
554 unsigned long no_pre_delete = (2UL<<num_parent);
555 unsigned long i;
557 /* Two groups of interesting lines may have a short gap of
558 * uninteresting lines. Connect such groups to give them a
559 * bit of context.
561 * We first start from what the interesting() function says,
562 * and mark them with "mark", and paint context lines with the
563 * mark. So interesting() would still say false for such context
564 * lines but they are treated as "interesting" in the end.
566 i = find_next(sline, mark, 0, cnt, 0);
567 if (cnt < i)
568 return 0;
570 while (i <= cnt) {
571 unsigned long j = (context < i) ? (i - context) : 0;
572 unsigned long k;
574 /* Paint a few lines before the first interesting line. */
575 while (j < i) {
576 if (!(sline[j].flag & mark))
577 sline[j].flag |= no_pre_delete;
578 sline[j++].flag |= mark;
581 again:
582 /* we know up to i is to be included. where does the
583 * next uninteresting one start?
585 j = find_next(sline, mark, i, cnt, 1);
586 if (cnt < j)
587 break; /* the rest are all interesting */
589 /* lookahead context lines */
590 k = find_next(sline, mark, j, cnt, 0);
591 j = adjust_hunk_tail(sline, all_mask, i, j);
593 if (k < j + context) {
594 /* k is interesting and [j,k) are not, but
595 * paint them interesting because the gap is small.
597 while (j < k)
598 sline[j++].flag |= mark;
599 i = k;
600 goto again;
603 /* j is the first uninteresting line and there is
604 * no overlap beyond it within context lines. Paint
605 * the trailing edge a bit.
607 i = k;
608 k = (j + context < cnt+1) ? j + context : cnt+1;
609 while (j < k)
610 sline[j++].flag |= mark;
612 return 1;
615 static int make_hunks(struct sline *sline, unsigned long cnt,
616 int num_parent, int dense)
618 unsigned long all_mask = (1UL<<num_parent) - 1;
619 unsigned long mark = (1UL<<num_parent);
620 unsigned long i;
621 int has_interesting = 0;
623 for (i = 0; i <= cnt; i++) {
624 if (interesting(&sline[i], all_mask))
625 sline[i].flag |= mark;
626 else
627 sline[i].flag &= ~mark;
629 if (!dense)
630 return give_context(sline, cnt, num_parent);
632 /* Look at each hunk, and if we have changes from only one
633 * parent, or the changes are the same from all but one
634 * parent, mark that uninteresting.
636 i = 0;
637 while (i <= cnt) {
638 unsigned long j, hunk_begin, hunk_end;
639 unsigned long same_diff;
640 while (i <= cnt && !(sline[i].flag & mark))
641 i++;
642 if (cnt < i)
643 break; /* No more interesting hunks */
644 hunk_begin = i;
645 for (j = i + 1; j <= cnt; j++) {
646 if (!(sline[j].flag & mark)) {
647 /* Look beyond the end to see if there
648 * is an interesting line after this
649 * hunk within context span.
651 unsigned long la; /* lookahead */
652 int contin = 0;
653 la = adjust_hunk_tail(sline, all_mask,
654 hunk_begin, j);
655 la = (la + context < cnt + 1) ?
656 (la + context) : cnt + 1;
657 while (la && j <= --la) {
658 if (sline[la].flag & mark) {
659 contin = 1;
660 break;
663 if (!contin)
664 break;
665 j = la;
668 hunk_end = j;
670 /* [i..hunk_end) are interesting. Now is it really
671 * interesting? We check if there are only two versions
672 * and the result matches one of them. That is, we look
673 * at:
674 * (+) line, which records lines added to which parents;
675 * this line appears in the result.
676 * (-) line, which records from what parents the line
677 * was removed; this line does not appear in the result.
678 * then check the set of parents the result has difference
679 * from, from all lines. If there are lines that has
680 * different set of parents that the result has differences
681 * from, that means we have more than two versions.
683 * Even when we have only two versions, if the result does
684 * not match any of the parents, the it should be considered
685 * interesting. In such a case, we would have all '+' line.
686 * After passing the above "two versions" test, that would
687 * appear as "the same set of parents" to be "all parents".
689 same_diff = 0;
690 has_interesting = 0;
691 for (j = i; j < hunk_end && !has_interesting; j++) {
692 unsigned long this_diff = sline[j].flag & all_mask;
693 struct lline *ll = sline[j].lost;
694 if (this_diff) {
695 /* This has some changes. Is it the
696 * same as others?
698 if (!same_diff)
699 same_diff = this_diff;
700 else if (same_diff != this_diff) {
701 has_interesting = 1;
702 break;
705 while (ll && !has_interesting) {
706 /* Lost this line from these parents;
707 * who are they? Are they the same?
709 this_diff = ll->parent_map;
710 if (!same_diff)
711 same_diff = this_diff;
712 else if (same_diff != this_diff) {
713 has_interesting = 1;
715 ll = ll->next;
719 if (!has_interesting && same_diff != all_mask) {
720 /* This hunk is not that interesting after all */
721 for (j = hunk_begin; j < hunk_end; j++)
722 sline[j].flag &= ~mark;
724 i = hunk_end;
727 has_interesting = give_context(sline, cnt, num_parent);
728 return has_interesting;
731 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
733 l0 = sline[l0].p_lno[n];
734 l1 = sline[l1].p_lno[n];
735 printf(" -%lu,%lu", l0, l1-l0-null_context);
738 static int hunk_comment_line(const char *bol)
740 int ch;
742 if (!bol)
743 return 0;
744 ch = *bol & 0xff;
745 return (isalpha(ch) || ch == '_' || ch == '$');
748 static void show_line_to_eol(const char *line, int len, const char *reset)
750 int saw_cr_at_eol = 0;
751 if (len < 0)
752 len = strlen(line);
753 saw_cr_at_eol = (len && line[len-1] == '\r');
755 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
756 reset,
757 saw_cr_at_eol ? "\r" : "");
760 static void dump_sline(struct sline *sline, const char *line_prefix,
761 unsigned long cnt, int num_parent,
762 int use_color, int result_deleted)
764 unsigned long mark = (1UL<<num_parent);
765 unsigned long no_pre_delete = (2UL<<num_parent);
766 int i;
767 unsigned long lno = 0;
768 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
769 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
770 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
771 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
772 const char *c_context = diff_get_color(use_color, DIFF_CONTEXT);
773 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
775 if (result_deleted)
776 return; /* result deleted */
778 while (1) {
779 unsigned long hunk_end;
780 unsigned long rlines;
781 const char *hunk_comment = NULL;
782 unsigned long null_context = 0;
784 while (lno <= cnt && !(sline[lno].flag & mark)) {
785 if (hunk_comment_line(sline[lno].bol))
786 hunk_comment = sline[lno].bol;
787 lno++;
789 if (cnt < lno)
790 break;
791 else {
792 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
793 if (!(sline[hunk_end].flag & mark))
794 break;
796 rlines = hunk_end - lno;
797 if (cnt < hunk_end)
798 rlines--; /* pointing at the last delete hunk */
800 if (!context) {
802 * Even when running with --unified=0, all
803 * lines in the hunk needs to be processed in
804 * the loop below in order to show the
805 * deletion recorded in lost_head. However,
806 * we do not want to show the resulting line
807 * with all blank context markers in such a
808 * case. Compensate.
810 unsigned long j;
811 for (j = lno; j < hunk_end; j++)
812 if (!(sline[j].flag & (mark-1)))
813 null_context++;
814 rlines -= null_context;
817 printf("%s%s", line_prefix, c_frag);
818 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
819 for (i = 0; i < num_parent; i++)
820 show_parent_lno(sline, lno, hunk_end, i, null_context);
821 printf(" +%lu,%lu ", lno+1, rlines);
822 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
824 if (hunk_comment) {
825 int comment_end = 0;
826 for (i = 0; i < 40; i++) {
827 int ch = hunk_comment[i] & 0xff;
828 if (!ch || ch == '\n')
829 break;
830 if (!isspace(ch))
831 comment_end = i;
833 if (comment_end)
834 printf("%s%s %s%s", c_reset,
835 c_context, c_reset,
836 c_func);
837 for (i = 0; i < comment_end; i++)
838 putchar(hunk_comment[i]);
841 printf("%s\n", c_reset);
842 while (lno < hunk_end) {
843 struct lline *ll;
844 int j;
845 unsigned long p_mask;
846 struct sline *sl = &sline[lno++];
847 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost;
848 while (ll) {
849 printf("%s%s", line_prefix, c_old);
850 for (j = 0; j < num_parent; j++) {
851 if (ll->parent_map & (1UL<<j))
852 putchar('-');
853 else
854 putchar(' ');
856 show_line_to_eol(ll->line, -1, c_reset);
857 ll = ll->next;
859 if (cnt < lno)
860 break;
861 p_mask = 1;
862 fputs(line_prefix, stdout);
863 if (!(sl->flag & (mark-1))) {
865 * This sline was here to hang the
866 * lost lines in front of it.
868 if (!context)
869 continue;
870 fputs(c_context, stdout);
872 else
873 fputs(c_new, stdout);
874 for (j = 0; j < num_parent; j++) {
875 if (p_mask & sl->flag)
876 putchar('+');
877 else
878 putchar(' ');
879 p_mask <<= 1;
881 show_line_to_eol(sl->bol, sl->len, c_reset);
886 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
887 int i, int j)
889 /* We have already examined parent j and we know parent i
890 * and parent j are the same, so reuse the combined result
891 * of parent j for parent i.
893 unsigned long lno, imask, jmask;
894 imask = (1UL<<i);
895 jmask = (1UL<<j);
897 for (lno = 0; lno <= cnt; lno++) {
898 struct lline *ll = sline->lost;
899 sline->p_lno[i] = sline->p_lno[j];
900 while (ll) {
901 if (ll->parent_map & jmask)
902 ll->parent_map |= imask;
903 ll = ll->next;
905 if (sline->flag & jmask)
906 sline->flag |= imask;
907 sline++;
909 /* the overall size of the file (sline[cnt]) */
910 sline->p_lno[i] = sline->p_lno[j];
913 static void dump_quoted_path(const char *head,
914 const char *prefix,
915 const char *path,
916 const char *line_prefix,
917 const char *c_meta, const char *c_reset)
919 static struct strbuf buf = STRBUF_INIT;
921 strbuf_reset(&buf);
922 strbuf_addstr(&buf, line_prefix);
923 strbuf_addstr(&buf, c_meta);
924 strbuf_addstr(&buf, head);
925 quote_two_c_style(&buf, prefix, path, 0);
926 strbuf_addstr(&buf, c_reset);
927 puts(buf.buf);
930 static void show_combined_header(struct combine_diff_path *elem,
931 int num_parent,
932 struct rev_info *rev,
933 const char *line_prefix,
934 int mode_differs,
935 int show_file_header)
937 struct diff_options *opt = &rev->diffopt;
938 int abbrev = opt->flags.full_index ? the_hash_algo->hexsz : DEFAULT_ABBREV;
939 const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
940 const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
941 const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
942 const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
943 const char *abb;
944 int added = 0;
945 int deleted = 0;
946 int i;
947 int dense = rev->dense_combined_merges;
949 if (rev->loginfo && !rev->no_commit_id)
950 show_log(rev);
952 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
953 "", elem->path, line_prefix, c_meta, c_reset);
954 printf("%s%sindex ", line_prefix, c_meta);
955 for (i = 0; i < num_parent; i++) {
956 abb = repo_find_unique_abbrev(the_repository,
957 &elem->parent[i].oid, abbrev);
958 printf("%s%s", i ? "," : "", abb);
960 abb = repo_find_unique_abbrev(the_repository, &elem->oid, abbrev);
961 printf("..%s%s\n", abb, c_reset);
963 if (mode_differs) {
964 deleted = !elem->mode;
966 /* We say it was added if nobody had it */
967 added = !deleted;
968 for (i = 0; added && i < num_parent; i++)
969 if (elem->parent[i].status !=
970 DIFF_STATUS_ADDED)
971 added = 0;
972 if (added)
973 printf("%s%snew file mode %06o",
974 line_prefix, c_meta, elem->mode);
975 else {
976 if (deleted)
977 printf("%s%sdeleted file ",
978 line_prefix, c_meta);
979 printf("mode ");
980 for (i = 0; i < num_parent; i++) {
981 printf("%s%06o", i ? "," : "",
982 elem->parent[i].mode);
984 if (elem->mode)
985 printf("..%06o", elem->mode);
987 printf("%s\n", c_reset);
990 if (!show_file_header)
991 return;
993 if (rev->combined_all_paths) {
994 for (i = 0; i < num_parent; i++) {
995 char *path = filename_changed(elem->parent[i].status)
996 ? elem->parent[i].path.buf : elem->path;
997 if (elem->parent[i].status == DIFF_STATUS_ADDED)
998 dump_quoted_path("--- ", "", "/dev/null",
999 line_prefix, c_meta, c_reset);
1000 else
1001 dump_quoted_path("--- ", a_prefix, path,
1002 line_prefix, c_meta, c_reset);
1004 } else {
1005 if (added)
1006 dump_quoted_path("--- ", "", "/dev/null",
1007 line_prefix, c_meta, c_reset);
1008 else
1009 dump_quoted_path("--- ", a_prefix, elem->path,
1010 line_prefix, c_meta, c_reset);
1012 if (deleted)
1013 dump_quoted_path("+++ ", "", "/dev/null",
1014 line_prefix, c_meta, c_reset);
1015 else
1016 dump_quoted_path("+++ ", b_prefix, elem->path,
1017 line_prefix, c_meta, c_reset);
1020 static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
1021 int working_tree_file,
1022 struct rev_info *rev)
1024 struct diff_options *opt = &rev->diffopt;
1025 unsigned long result_size, cnt, lno;
1026 int result_deleted = 0;
1027 char *result, *cp;
1028 struct sline *sline; /* survived lines */
1029 int mode_differs = 0;
1030 int i, show_hunks;
1031 mmfile_t result_file;
1032 struct userdiff_driver *userdiff;
1033 struct userdiff_driver *textconv = NULL;
1034 int is_binary;
1035 const char *line_prefix = diff_line_prefix(opt);
1037 context = opt->context;
1038 userdiff = userdiff_find_by_path(opt->repo->index, elem->path);
1039 if (!userdiff)
1040 userdiff = userdiff_find_by_name("default");
1041 if (opt->flags.allow_textconv)
1042 textconv = userdiff_get_textconv(opt->repo, userdiff);
1044 /* Read the result of merge first */
1045 if (!working_tree_file)
1046 result = grab_blob(opt->repo, &elem->oid, elem->mode, &result_size,
1047 textconv, elem->path);
1048 else {
1049 /* Used by diff-tree to read from the working tree */
1050 struct stat st;
1051 int fd = -1;
1053 if (lstat(elem->path, &st) < 0)
1054 goto deleted_file;
1056 if (S_ISLNK(st.st_mode)) {
1057 struct strbuf buf = STRBUF_INIT;
1059 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
1060 error_errno("readlink(%s)", elem->path);
1061 return;
1063 result_size = buf.len;
1064 result = strbuf_detach(&buf, NULL);
1065 elem->mode = canon_mode(st.st_mode);
1066 } else if (S_ISDIR(st.st_mode)) {
1067 struct object_id oid;
1068 if (resolve_gitlink_ref(elem->path, "HEAD", &oid) < 0)
1069 result = grab_blob(opt->repo, &elem->oid,
1070 elem->mode, &result_size,
1071 NULL, NULL);
1072 else
1073 result = grab_blob(opt->repo, &oid, elem->mode,
1074 &result_size, NULL, NULL);
1075 } else if (textconv) {
1076 struct diff_filespec *df = alloc_filespec(elem->path);
1077 fill_filespec(df, null_oid(), 0, st.st_mode);
1078 result_size = fill_textconv(opt->repo, textconv, df, &result);
1079 free_filespec(df);
1080 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
1081 size_t len = xsize_t(st.st_size);
1082 ssize_t done;
1083 int is_file, i;
1085 elem->mode = canon_mode(st.st_mode);
1086 /* if symlinks don't work, assume symlink if all parents
1087 * are symlinks
1089 is_file = has_symlinks;
1090 for (i = 0; !is_file && i < num_parent; i++)
1091 is_file = !S_ISLNK(elem->parent[i].mode);
1092 if (!is_file)
1093 elem->mode = canon_mode(S_IFLNK);
1095 result_size = len;
1096 result = xmallocz(len);
1098 done = read_in_full(fd, result, len);
1099 if (done < 0)
1100 die_errno("read error '%s'", elem->path);
1101 else if (done < len)
1102 die("early EOF '%s'", elem->path);
1104 /* If not a fake symlink, apply filters, e.g. autocrlf */
1105 if (is_file) {
1106 struct strbuf buf = STRBUF_INIT;
1108 if (convert_to_git(rev->diffopt.repo->index,
1109 elem->path, result, len, &buf, global_conv_flags_eol)) {
1110 free(result);
1111 result = strbuf_detach(&buf, &len);
1112 result_size = len;
1116 else {
1117 deleted_file:
1118 result_deleted = 1;
1119 result_size = 0;
1120 elem->mode = 0;
1121 result = xcalloc(1, 1);
1124 if (0 <= fd)
1125 close(fd);
1128 for (i = 0; i < num_parent; i++) {
1129 if (elem->parent[i].mode != elem->mode) {
1130 mode_differs = 1;
1131 break;
1135 if (textconv)
1136 is_binary = 0;
1137 else if (userdiff->binary != -1)
1138 is_binary = userdiff->binary;
1139 else {
1140 is_binary = buffer_is_binary(result, result_size);
1141 for (i = 0; !is_binary && i < num_parent; i++) {
1142 char *buf;
1143 unsigned long size;
1144 buf = grab_blob(opt->repo,
1145 &elem->parent[i].oid,
1146 elem->parent[i].mode,
1147 &size, NULL, NULL);
1148 if (buffer_is_binary(buf, size))
1149 is_binary = 1;
1150 free(buf);
1153 if (is_binary) {
1154 show_combined_header(elem, num_parent, rev,
1155 line_prefix, mode_differs, 0);
1156 printf("Binary files differ\n");
1157 free(result);
1158 return;
1161 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
1162 if (*cp == '\n')
1163 cnt++;
1165 if (result_size && result[result_size-1] != '\n')
1166 cnt++; /* incomplete line */
1168 CALLOC_ARRAY(sline, st_add(cnt, 2));
1169 sline[0].bol = result;
1170 for (lno = 0, cp = result; cp < result + result_size; cp++) {
1171 if (*cp == '\n') {
1172 sline[lno].len = cp - sline[lno].bol;
1173 lno++;
1174 if (lno < cnt)
1175 sline[lno].bol = cp + 1;
1178 if (result_size && result[result_size-1] != '\n')
1179 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
1181 result_file.ptr = result;
1182 result_file.size = result_size;
1184 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1185 * for deletion hunk at the end.
1187 CALLOC_ARRAY(sline[0].p_lno, st_mult(st_add(cnt, 2), num_parent));
1188 for (lno = 0; lno <= cnt; lno++)
1189 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
1191 for (i = 0; i < num_parent; i++) {
1192 int j;
1193 for (j = 0; j < i; j++) {
1194 if (oideq(&elem->parent[i].oid,
1195 &elem->parent[j].oid)) {
1196 reuse_combine_diff(sline, cnt, i, j);
1197 break;
1200 if (i <= j)
1201 combine_diff(opt->repo,
1202 &elem->parent[i].oid,
1203 elem->parent[i].mode,
1204 &result_file, sline,
1205 cnt, i, num_parent, result_deleted,
1206 textconv, elem->path, opt->xdl_opts);
1209 show_hunks = make_hunks(sline, cnt, num_parent, rev->dense_combined_merges);
1211 if (show_hunks || mode_differs || working_tree_file) {
1212 show_combined_header(elem, num_parent, rev,
1213 line_prefix, mode_differs, 1);
1214 dump_sline(sline, line_prefix, cnt, num_parent,
1215 opt->use_color, result_deleted);
1217 free(result);
1219 for (lno = 0; lno < cnt; lno++) {
1220 if (sline[lno].lost) {
1221 struct lline *ll = sline[lno].lost;
1222 while (ll) {
1223 struct lline *tmp = ll;
1224 ll = ll->next;
1225 free(tmp);
1229 free(sline[0].p_lno);
1230 free(sline);
1233 static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
1235 struct diff_options *opt = &rev->diffopt;
1236 int line_termination, inter_name_termination, i;
1237 const char *line_prefix = diff_line_prefix(opt);
1239 line_termination = opt->line_termination;
1240 inter_name_termination = '\t';
1241 if (!line_termination)
1242 inter_name_termination = 0;
1244 if (rev->loginfo && !rev->no_commit_id)
1245 show_log(rev);
1248 if (opt->output_format & DIFF_FORMAT_RAW) {
1249 printf("%s", line_prefix);
1251 /* As many colons as there are parents */
1252 for (i = 0; i < num_parent; i++)
1253 putchar(':');
1255 /* Show the modes */
1256 for (i = 0; i < num_parent; i++)
1257 printf("%06o ", p->parent[i].mode);
1258 printf("%06o", p->mode);
1260 /* Show sha1's */
1261 for (i = 0; i < num_parent; i++)
1262 printf(" %s", diff_aligned_abbrev(&p->parent[i].oid,
1263 opt->abbrev));
1264 printf(" %s ", diff_aligned_abbrev(&p->oid, opt->abbrev));
1267 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
1268 for (i = 0; i < num_parent; i++)
1269 putchar(p->parent[i].status);
1270 putchar(inter_name_termination);
1273 for (i = 0; i < num_parent; i++)
1274 if (rev->combined_all_paths) {
1275 if (filename_changed(p->parent[i].status))
1276 write_name_quoted(p->parent[i].path.buf, stdout,
1277 inter_name_termination);
1278 else
1279 write_name_quoted(p->path, stdout,
1280 inter_name_termination);
1282 write_name_quoted(p->path, stdout, line_termination);
1286 * The result (p->elem) is from the working tree and their
1287 * parents are typically from multiple stages during a merge
1288 * (i.e. diff-files) or the state in HEAD and in the index
1289 * (i.e. diff-index).
1291 void show_combined_diff(struct combine_diff_path *p,
1292 int num_parent,
1293 struct rev_info *rev)
1295 struct diff_options *opt = &rev->diffopt;
1297 if (opt->output_format & (DIFF_FORMAT_RAW |
1298 DIFF_FORMAT_NAME |
1299 DIFF_FORMAT_NAME_STATUS))
1300 show_raw_diff(p, num_parent, rev);
1301 else if (opt->output_format & DIFF_FORMAT_PATCH)
1302 show_patch_diff(p, num_parent, 1, rev);
1305 static void free_combined_pair(struct diff_filepair *pair)
1307 free(pair->two);
1308 free(pair);
1312 * A combine_diff_path expresses N parents on the LHS against 1 merge
1313 * result. Synthesize a diff_filepair that has N entries on the "one"
1314 * side and 1 entry on the "two" side.
1316 * In the future, we might want to add more data to combine_diff_path
1317 * so that we can fill fields we are ignoring (most notably, size) here,
1318 * but currently nobody uses it, so this should suffice for now.
1320 static struct diff_filepair *combined_pair(struct combine_diff_path *p,
1321 int num_parent)
1323 int i;
1324 struct diff_filepair *pair;
1325 struct diff_filespec *pool;
1327 pair = xmalloc(sizeof(*pair));
1328 CALLOC_ARRAY(pool, st_add(num_parent, 1));
1329 pair->one = pool + 1;
1330 pair->two = pool;
1332 for (i = 0; i < num_parent; i++) {
1333 pair->one[i].path = p->path;
1334 pair->one[i].mode = p->parent[i].mode;
1335 oidcpy(&pair->one[i].oid, &p->parent[i].oid);
1336 pair->one[i].oid_valid = !is_null_oid(&p->parent[i].oid);
1337 pair->one[i].has_more_entries = 1;
1339 pair->one[num_parent - 1].has_more_entries = 0;
1341 pair->two->path = p->path;
1342 pair->two->mode = p->mode;
1343 oidcpy(&pair->two->oid, &p->oid);
1344 pair->two->oid_valid = !is_null_oid(&p->oid);
1345 return pair;
1348 static void handle_combined_callback(struct diff_options *opt,
1349 struct combine_diff_path *paths,
1350 int num_parent,
1351 int num_paths)
1353 struct combine_diff_path *p;
1354 struct diff_queue_struct q;
1355 int i;
1357 CALLOC_ARRAY(q.queue, num_paths);
1358 q.alloc = num_paths;
1359 q.nr = num_paths;
1360 for (i = 0, p = paths; p; p = p->next)
1361 q.queue[i++] = combined_pair(p, num_parent);
1362 opt->format_callback(&q, opt, opt->format_callback_data);
1363 for (i = 0; i < num_paths; i++)
1364 free_combined_pair(q.queue[i]);
1365 free(q.queue);
1368 static const char *path_path(void *obj)
1370 struct combine_diff_path *path = (struct combine_diff_path *)obj;
1372 return path->path;
1376 * Diff stat formats which we always compute solely against the first parent.
1378 #define STAT_FORMAT_MASK (DIFF_FORMAT_NUMSTAT \
1379 | DIFF_FORMAT_SHORTSTAT \
1380 | DIFF_FORMAT_SUMMARY \
1381 | DIFF_FORMAT_DIRSTAT \
1382 | DIFF_FORMAT_DIFFSTAT)
1384 /* find set of paths that every parent touches */
1385 static struct combine_diff_path *find_paths_generic(const struct object_id *oid,
1386 const struct oid_array *parents,
1387 struct diff_options *opt,
1388 int combined_all_paths)
1390 struct combine_diff_path *paths = NULL;
1391 int i, num_parent = parents->nr;
1393 int output_format = opt->output_format;
1394 const char *orderfile = opt->orderfile;
1396 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1397 /* tell diff_tree to emit paths in sorted (=tree) order */
1398 opt->orderfile = NULL;
1400 /* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn) (wrt paths) */
1401 for (i = 0; i < num_parent; i++) {
1403 * show stat against the first parent even when doing
1404 * combined diff.
1406 int stat_opt = output_format & STAT_FORMAT_MASK;
1407 if (i == 0 && stat_opt)
1408 opt->output_format = stat_opt;
1409 else
1410 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1411 diff_tree_oid(&parents->oid[i], oid, "", opt);
1412 diffcore_std(opt);
1413 paths = intersect_paths(paths, i, num_parent,
1414 combined_all_paths);
1416 /* if showing diff, show it in requested order */
1417 if (opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1418 orderfile) {
1419 diffcore_order(orderfile);
1422 diff_flush(opt);
1425 opt->output_format = output_format;
1426 opt->orderfile = orderfile;
1427 return paths;
1432 * find set of paths that everybody touches, assuming diff is run without
1433 * rename/copy detection, etc, comparing all trees simultaneously (= faster).
1435 static struct combine_diff_path *find_paths_multitree(
1436 const struct object_id *oid, const struct oid_array *parents,
1437 struct diff_options *opt)
1439 int i, nparent = parents->nr;
1440 const struct object_id **parents_oid;
1441 struct combine_diff_path paths_head;
1442 struct strbuf base;
1444 ALLOC_ARRAY(parents_oid, nparent);
1445 for (i = 0; i < nparent; i++)
1446 parents_oid[i] = &parents->oid[i];
1448 /* fake list head, so worker can assume it is non-NULL */
1449 paths_head.next = NULL;
1451 strbuf_init(&base, PATH_MAX);
1452 diff_tree_paths(&paths_head, oid, parents_oid, nparent, &base, opt);
1454 strbuf_release(&base);
1455 free(parents_oid);
1456 return paths_head.next;
1459 static int match_objfind(struct combine_diff_path *path,
1460 int num_parent,
1461 const struct oidset *set)
1463 int i;
1464 if (oidset_contains(set, &path->oid))
1465 return 1;
1466 for (i = 0; i < num_parent; i++) {
1467 if (oidset_contains(set, &path->parent[i].oid))
1468 return 1;
1470 return 0;
1473 static struct combine_diff_path *combined_objfind(struct diff_options *opt,
1474 struct combine_diff_path *paths,
1475 int num_parent)
1477 struct combine_diff_path *ret = NULL, **tail = &ret;
1478 struct combine_diff_path *p = paths;
1480 while (p) {
1481 struct combine_diff_path *next = p->next;
1483 if (match_objfind(p, num_parent, opt->objfind)) {
1484 p->next = NULL;
1485 *tail = p;
1486 tail = &p->next;
1487 } else {
1488 free(p);
1490 p = next;
1493 return ret;
1496 void diff_tree_combined(const struct object_id *oid,
1497 const struct oid_array *parents,
1498 struct rev_info *rev)
1500 struct diff_options *opt = &rev->diffopt;
1501 struct diff_options diffopts;
1502 struct combine_diff_path *p, *paths;
1503 int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
1504 int need_generic_pathscan;
1506 if (opt->ignore_regex_nr)
1507 die("combined diff and '%s' cannot be used together",
1508 "--ignore-matching-lines");
1509 if (opt->close_file)
1510 die("combined diff and '%s' cannot be used together",
1511 "--output");
1513 /* nothing to do, if no parents */
1514 if (!num_parent)
1515 return;
1517 show_log_first = !!rev->loginfo && !rev->no_commit_id;
1518 needsep = 0;
1519 if (show_log_first) {
1520 show_log(rev);
1522 if (rev->verbose_header && opt->output_format &&
1523 opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1524 !commit_format_is_empty(rev->commit_format))
1525 printf("%s%c", diff_line_prefix(opt),
1526 opt->line_termination);
1529 diffopts = *opt;
1530 copy_pathspec(&diffopts.pathspec, &opt->pathspec);
1531 diffopts.flags.recursive = 1;
1532 diffopts.flags.allow_external = 0;
1534 /* find set of paths that everybody touches
1536 * NOTE
1538 * Diffcore transformations are bound to diff_filespec and logic
1539 * comparing two entries - i.e. they do not apply directly to combine
1540 * diff.
1542 * If some of such transformations is requested - we launch generic
1543 * path scanning, which works significantly slower compared to
1544 * simultaneous all-trees-in-one-go scan in find_paths_multitree().
1546 * TODO some of the filters could be ported to work on
1547 * combine_diff_paths - i.e. all functionality that skips paths, so in
1548 * theory, we could end up having only multitree path scanning.
1550 * NOTE please keep this semantically in sync with diffcore_std()
1552 need_generic_pathscan = opt->skip_stat_unmatch ||
1553 opt->flags.follow_renames ||
1554 opt->break_opt != -1 ||
1555 opt->detect_rename ||
1556 (opt->pickaxe_opts &
1557 (DIFF_PICKAXE_KINDS_MASK & ~DIFF_PICKAXE_KIND_OBJFIND)) ||
1558 opt->filter;
1560 if (need_generic_pathscan) {
1562 * NOTE generic case also handles --stat, as it computes
1563 * diff(sha1,parent_i) for all i to do the job, specifically
1564 * for parent0.
1566 paths = find_paths_generic(oid, parents, &diffopts,
1567 rev->combined_all_paths);
1569 else {
1570 int stat_opt;
1571 paths = find_paths_multitree(oid, parents, &diffopts);
1573 if (opt->pickaxe_opts & DIFF_PICKAXE_KIND_OBJFIND)
1574 paths = combined_objfind(opt, paths, num_parent);
1577 * show stat against the first parent even
1578 * when doing combined diff.
1580 stat_opt = opt->output_format & STAT_FORMAT_MASK;
1581 if (stat_opt) {
1582 diffopts.output_format = stat_opt;
1584 diff_tree_oid(&parents->oid[0], oid, "", &diffopts);
1585 diffcore_std(&diffopts);
1586 if (opt->orderfile)
1587 diffcore_order(opt->orderfile);
1588 diff_flush(&diffopts);
1592 /* find out number of surviving paths */
1593 for (num_paths = 0, p = paths; p; p = p->next)
1594 num_paths++;
1596 /* order paths according to diffcore_order */
1597 if (opt->orderfile && num_paths) {
1598 struct obj_order *o;
1600 ALLOC_ARRAY(o, num_paths);
1601 for (i = 0, p = paths; p; p = p->next, i++)
1602 o[i].obj = p;
1603 order_objects(opt->orderfile, path_path, o, num_paths);
1604 for (i = 0; i < num_paths - 1; i++) {
1605 p = o[i].obj;
1606 p->next = o[i+1].obj;
1609 p = o[num_paths-1].obj;
1610 p->next = NULL;
1611 paths = o[0].obj;
1612 free(o);
1616 if (num_paths) {
1617 if (opt->output_format & (DIFF_FORMAT_RAW |
1618 DIFF_FORMAT_NAME |
1619 DIFF_FORMAT_NAME_STATUS)) {
1620 for (p = paths; p; p = p->next)
1621 show_raw_diff(p, num_parent, rev);
1622 needsep = 1;
1624 else if (opt->output_format & STAT_FORMAT_MASK)
1625 needsep = 1;
1626 else if (opt->output_format & DIFF_FORMAT_CALLBACK)
1627 handle_combined_callback(opt, paths, num_parent, num_paths);
1629 if (opt->output_format & DIFF_FORMAT_PATCH) {
1630 if (needsep)
1631 printf("%s%c", diff_line_prefix(opt),
1632 opt->line_termination);
1633 for (p = paths; p; p = p->next)
1634 show_patch_diff(p, num_parent, 0, rev);
1638 /* Clean things up */
1639 while (paths) {
1640 struct combine_diff_path *tmp = paths;
1641 paths = paths->next;
1642 for (i = 0; i < num_parent; i++)
1643 if (rev->combined_all_paths &&
1644 filename_changed(tmp->parent[i].status))
1645 strbuf_release(&tmp->parent[i].path);
1646 free(tmp);
1649 clear_pathspec(&diffopts.pathspec);
1652 void diff_tree_combined_merge(const struct commit *commit,
1653 struct rev_info *rev)
1655 struct commit_list *parent = get_saved_parents(rev, commit);
1656 struct oid_array parents = OID_ARRAY_INIT;
1658 while (parent) {
1659 oid_array_append(&parents, &parent->item->object.oid);
1660 parent = parent->next;
1662 diff_tree_combined(&commit->object.oid, &parents, rev);
1663 oid_array_clear(&parents);