Git 2.44
[alt-git.git] / combine-diff.c
blobd6d6fa168942053e756317d8b490435e4d1280b6
1 #include "git-compat-util.h"
2 #include "object-store-ll.h"
3 #include "commit.h"
4 #include "convert.h"
5 #include "diff.h"
6 #include "diffcore.h"
7 #include "environment.h"
8 #include "hex.h"
9 #include "object-name.h"
10 #include "quote.h"
11 #include "xdiff-interface.h"
12 #include "xdiff/xmacros.h"
13 #include "log-tree.h"
14 #include "refs.h"
15 #include "tree.h"
16 #include "userdiff.h"
17 #include "oid-array.h"
18 #include "revision.h"
20 static int compare_paths(const struct combine_diff_path *one,
21 const struct diff_filespec *two)
23 if (!S_ISDIR(one->mode) && !S_ISDIR(two->mode))
24 return strcmp(one->path, two->path);
26 return base_name_compare(one->path, strlen(one->path), one->mode,
27 two->path, strlen(two->path), two->mode);
30 static int filename_changed(char status)
32 return status == 'R' || status == 'C';
35 static struct combine_diff_path *intersect_paths(
36 struct combine_diff_path *curr,
37 int n,
38 int num_parent,
39 int combined_all_paths)
41 struct diff_queue_struct *q = &diff_queued_diff;
42 struct combine_diff_path *p, **tail = &curr;
43 int i, j, cmp;
45 if (!n) {
46 for (i = 0; i < q->nr; i++) {
47 int len;
48 const char *path;
49 if (diff_unmodified_pair(q->queue[i]))
50 continue;
51 path = q->queue[i]->two->path;
52 len = strlen(path);
53 p = xmalloc(combine_diff_path_size(num_parent, len));
54 p->path = (char *) &(p->parent[num_parent]);
55 memcpy(p->path, path, len);
56 p->path[len] = 0;
57 p->next = NULL;
58 memset(p->parent, 0,
59 sizeof(p->parent[0]) * num_parent);
61 oidcpy(&p->oid, &q->queue[i]->two->oid);
62 p->mode = q->queue[i]->two->mode;
63 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
64 p->parent[n].mode = q->queue[i]->one->mode;
65 p->parent[n].status = q->queue[i]->status;
67 if (combined_all_paths &&
68 filename_changed(p->parent[n].status)) {
69 strbuf_init(&p->parent[n].path, 0);
70 strbuf_addstr(&p->parent[n].path,
71 q->queue[i]->one->path);
73 *tail = p;
74 tail = &p->next;
76 return curr;
80 * paths in curr (linked list) and q->queue[] (array) are
81 * both sorted in the tree order.
83 i = 0;
84 while ((p = *tail) != NULL) {
85 cmp = ((i >= q->nr)
86 ? -1 : compare_paths(p, q->queue[i]->two));
88 if (cmp < 0) {
89 /* p->path not in q->queue[]; drop it */
90 *tail = p->next;
91 for (j = 0; j < num_parent; j++)
92 if (combined_all_paths &&
93 filename_changed(p->parent[j].status))
94 strbuf_release(&p->parent[j].path);
95 free(p);
96 continue;
99 if (cmp > 0) {
100 /* q->queue[i] not in p->path; skip it */
101 i++;
102 continue;
105 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
106 p->parent[n].mode = q->queue[i]->one->mode;
107 p->parent[n].status = q->queue[i]->status;
108 if (combined_all_paths &&
109 filename_changed(p->parent[n].status))
110 strbuf_addstr(&p->parent[n].path,
111 q->queue[i]->one->path);
113 tail = &p->next;
114 i++;
116 return curr;
119 /* Lines lost from parent */
120 struct lline {
121 struct lline *next, *prev;
122 int len;
123 unsigned long parent_map;
124 char line[FLEX_ARRAY];
127 /* Lines lost from current parent (before coalescing) */
128 struct plost {
129 struct lline *lost_head, *lost_tail;
130 int len;
133 /* Lines surviving in the merge result */
134 struct sline {
135 /* Accumulated and coalesced lost lines */
136 struct lline *lost;
137 int lenlost;
138 struct plost plost;
139 char *bol;
140 int len;
141 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
142 * we did not change it).
143 * bit N is used for "interesting" lines, including context.
144 * bit (N+1) is used for "do not show deletion before this".
146 unsigned long flag;
147 unsigned long *p_lno;
150 static int match_string_spaces(const char *line1, int len1,
151 const char *line2, int len2,
152 long flags)
154 if (flags & XDF_WHITESPACE_FLAGS) {
155 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
156 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
159 if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
160 return (len1 == len2 && !memcmp(line1, line2, len1));
162 while (len1 > 0 && len2 > 0) {
163 len1--;
164 len2--;
165 if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
166 if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
167 (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
168 return 0;
170 for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
171 for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
173 if (line1[len1] != line2[len2])
174 return 0;
177 if (flags & XDF_IGNORE_WHITESPACE) {
178 /* Consume remaining spaces */
179 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
180 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
183 /* We matched full line1 and line2 */
184 if (!len1 && !len2)
185 return 1;
187 return 0;
190 enum coalesce_direction { MATCH, BASE, NEW };
192 /* Coalesce new lines into base by finding LCS */
193 static struct lline *coalesce_lines(struct lline *base, int *lenbase,
194 struct lline *newline, int lennew,
195 unsigned long parent, long flags)
197 int **lcs;
198 enum coalesce_direction **directions;
199 struct lline *baseend, *newend = NULL;
200 int i, j, origbaselen = *lenbase;
202 if (!newline)
203 return base;
205 if (!base) {
206 *lenbase = lennew;
207 return newline;
211 * Coalesce new lines into base by finding the LCS
212 * - Create the table to run dynamic programming
213 * - Compute the LCS
214 * - Then reverse read the direction structure:
215 * - If we have MATCH, assign parent to base flag, and consume
216 * both baseend and newend
217 * - Else if we have BASE, consume baseend
218 * - Else if we have NEW, insert newend lline into base and
219 * consume newend
221 CALLOC_ARRAY(lcs, st_add(origbaselen, 1));
222 CALLOC_ARRAY(directions, st_add(origbaselen, 1));
223 for (i = 0; i < origbaselen + 1; i++) {
224 CALLOC_ARRAY(lcs[i], st_add(lennew, 1));
225 CALLOC_ARRAY(directions[i], st_add(lennew, 1));
226 directions[i][0] = BASE;
228 for (j = 1; j < lennew + 1; j++)
229 directions[0][j] = NEW;
231 for (i = 1, baseend = base; i < origbaselen + 1; i++) {
232 for (j = 1, newend = newline; j < lennew + 1; j++) {
233 if (match_string_spaces(baseend->line, baseend->len,
234 newend->line, newend->len, flags)) {
235 lcs[i][j] = lcs[i - 1][j - 1] + 1;
236 directions[i][j] = MATCH;
237 } else if (lcs[i][j - 1] >= lcs[i - 1][j]) {
238 lcs[i][j] = lcs[i][j - 1];
239 directions[i][j] = NEW;
240 } else {
241 lcs[i][j] = lcs[i - 1][j];
242 directions[i][j] = BASE;
244 if (newend->next)
245 newend = newend->next;
247 if (baseend->next)
248 baseend = baseend->next;
251 for (i = 0; i < origbaselen + 1; i++)
252 free(lcs[i]);
253 free(lcs);
255 /* At this point, baseend and newend point to the end of each lists */
256 i--;
257 j--;
258 while (i != 0 || j != 0) {
259 if (directions[i][j] == MATCH) {
260 baseend->parent_map |= 1<<parent;
261 baseend = baseend->prev;
262 newend = newend->prev;
263 i--;
264 j--;
265 } else if (directions[i][j] == NEW) {
266 struct lline *lline;
268 lline = newend;
269 /* Remove lline from new list and update newend */
270 if (lline->prev)
271 lline->prev->next = lline->next;
272 else
273 newline = lline->next;
274 if (lline->next)
275 lline->next->prev = lline->prev;
277 newend = lline->prev;
278 j--;
280 /* Add lline to base list */
281 if (baseend) {
282 lline->next = baseend->next;
283 lline->prev = baseend;
284 if (lline->prev)
285 lline->prev->next = lline;
287 else {
288 lline->next = base;
289 base = lline;
291 (*lenbase)++;
293 if (lline->next)
294 lline->next->prev = lline;
296 } else {
297 baseend = baseend->prev;
298 i--;
302 newend = newline;
303 while (newend) {
304 struct lline *lline = newend;
305 newend = newend->next;
306 free(lline);
309 for (i = 0; i < origbaselen + 1; i++)
310 free(directions[i]);
311 free(directions);
313 return base;
316 static char *grab_blob(struct repository *r,
317 const struct object_id *oid, unsigned int mode,
318 unsigned long *size, struct userdiff_driver *textconv,
319 const char *path)
321 char *blob;
322 enum object_type type;
324 if (S_ISGITLINK(mode)) {
325 struct strbuf buf = STRBUF_INIT;
326 strbuf_addf(&buf, "Subproject commit %s\n", oid_to_hex(oid));
327 *size = buf.len;
328 blob = strbuf_detach(&buf, NULL);
329 } else if (is_null_oid(oid)) {
330 /* deleted blob */
331 *size = 0;
332 return xcalloc(1, 1);
333 } else if (textconv) {
334 struct diff_filespec *df = alloc_filespec(path);
335 fill_filespec(df, oid, 1, mode);
336 *size = fill_textconv(r, textconv, df, &blob);
337 free_filespec(df);
338 } else {
339 blob = repo_read_object_file(r, oid, &type, size);
340 if (!blob)
341 die(_("unable to read %s"), oid_to_hex(oid));
342 if (type != OBJ_BLOB)
343 die("object '%s' is not a blob!", oid_to_hex(oid));
345 return blob;
348 static void append_lost(struct sline *sline, int n, const char *line, int len)
350 struct lline *lline;
351 unsigned long this_mask = (1UL<<n);
352 if (line[len-1] == '\n')
353 len--;
355 FLEX_ALLOC_MEM(lline, line, line, len);
356 lline->len = len;
357 lline->next = NULL;
358 lline->prev = sline->plost.lost_tail;
359 if (lline->prev)
360 lline->prev->next = lline;
361 else
362 sline->plost.lost_head = lline;
363 sline->plost.lost_tail = lline;
364 sline->plost.len++;
365 lline->parent_map = this_mask;
368 struct combine_diff_state {
369 unsigned int lno;
370 int ob, on, nb, nn;
371 unsigned long nmask;
372 int num_parent;
373 int n;
374 struct sline *sline;
375 struct sline *lost_bucket;
378 static void consume_hunk(void *state_,
379 long ob, long on,
380 long nb, long nn,
381 const char *func UNUSED, long funclen UNUSED)
383 struct combine_diff_state *state = state_;
385 state->ob = ob;
386 state->on = on;
387 state->nb = nb;
388 state->nn = nn;
389 state->lno = state->nb;
390 if (state->nn == 0) {
391 /* @@ -X,Y +N,0 @@ removed Y lines
392 * that would have come *after* line N
393 * in the result. Our lost buckets hang
394 * to the line after the removed lines,
396 * Note that this is correct even when N == 0,
397 * in which case the hunk removes the first
398 * line in the file.
400 state->lost_bucket = &state->sline[state->nb];
401 if (!state->nb)
402 state->nb = 1;
403 } else {
404 state->lost_bucket = &state->sline[state->nb-1];
406 if (!state->sline[state->nb-1].p_lno)
407 CALLOC_ARRAY(state->sline[state->nb - 1].p_lno,
408 state->num_parent);
409 state->sline[state->nb-1].p_lno[state->n] = state->ob;
412 static int consume_line(void *state_, char *line, unsigned long len)
414 struct combine_diff_state *state = state_;
415 if (!state->lost_bucket)
416 return 0; /* not in any hunk yet */
417 switch (line[0]) {
418 case '-':
419 append_lost(state->lost_bucket, state->n, line+1, len-1);
420 break;
421 case '+':
422 state->sline[state->lno-1].flag |= state->nmask;
423 state->lno++;
424 break;
426 return 0;
429 static void combine_diff(struct repository *r,
430 const struct object_id *parent, unsigned int mode,
431 mmfile_t *result_file,
432 struct sline *sline, unsigned int cnt, int n,
433 int num_parent, int result_deleted,
434 struct userdiff_driver *textconv,
435 const char *path, long flags)
437 unsigned int p_lno, lno;
438 unsigned long nmask = (1UL << n);
439 xpparam_t xpp;
440 xdemitconf_t xecfg;
441 mmfile_t parent_file;
442 struct combine_diff_state state;
443 unsigned long sz;
445 if (result_deleted)
446 return; /* result deleted */
448 parent_file.ptr = grab_blob(r, parent, mode, &sz, textconv, path);
449 parent_file.size = sz;
450 memset(&xpp, 0, sizeof(xpp));
451 xpp.flags = flags;
452 memset(&xecfg, 0, sizeof(xecfg));
453 memset(&state, 0, sizeof(state));
454 state.nmask = nmask;
455 state.sline = sline;
456 state.lno = 1;
457 state.num_parent = num_parent;
458 state.n = n;
460 if (xdi_diff_outf(&parent_file, result_file, consume_hunk,
461 consume_line, &state, &xpp, &xecfg))
462 die("unable to generate combined diff for %s",
463 oid_to_hex(parent));
464 free(parent_file.ptr);
466 /* Assign line numbers for this parent.
468 * sline[lno].p_lno[n] records the first line number
469 * (counting from 1) for parent N if the final hunk display
470 * started by showing sline[lno] (possibly showing the lost
471 * lines attached to it first).
473 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
474 struct lline *ll;
475 sline[lno].p_lno[n] = p_lno;
477 /* Coalesce new lines */
478 if (sline[lno].plost.lost_head) {
479 struct sline *sl = &sline[lno];
480 sl->lost = coalesce_lines(sl->lost, &sl->lenlost,
481 sl->plost.lost_head,
482 sl->plost.len, n, flags);
483 sl->plost.lost_head = sl->plost.lost_tail = NULL;
484 sl->plost.len = 0;
487 /* How many lines would this sline advance the p_lno? */
488 ll = sline[lno].lost;
489 while (ll) {
490 if (ll->parent_map & nmask)
491 p_lno++; /* '-' means parent had it */
492 ll = ll->next;
494 if (lno < cnt && !(sline[lno].flag & nmask))
495 p_lno++; /* no '+' means parent had it */
497 sline[lno].p_lno[n] = p_lno; /* trailer */
500 static unsigned long context = 3;
501 static char combine_marker = '@';
503 static int interesting(struct sline *sline, unsigned long all_mask)
505 /* If some parents lost lines here, or if we have added to
506 * some parent, it is interesting.
508 return ((sline->flag & all_mask) || sline->lost);
511 static unsigned long adjust_hunk_tail(struct sline *sline,
512 unsigned long all_mask,
513 unsigned long hunk_begin,
514 unsigned long i)
516 /* i points at the first uninteresting line. If the last line
517 * of the hunk was interesting only because it has some
518 * deletion, then it is not all that interesting for the
519 * purpose of giving trailing context lines. This is because
520 * we output '-' line and then unmodified sline[i-1] itself in
521 * that case which gives us one extra context line.
523 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
524 i--;
525 return i;
528 static unsigned long find_next(struct sline *sline,
529 unsigned long mark,
530 unsigned long i,
531 unsigned long cnt,
532 int look_for_uninteresting)
534 /* We have examined up to i-1 and are about to look at i.
535 * Find next interesting or uninteresting line. Here,
536 * "interesting" does not mean interesting(), but marked by
537 * the give_context() function below (i.e. it includes context
538 * lines that are not interesting to interesting() function
539 * that are surrounded by interesting() ones.
541 while (i <= cnt)
542 if (look_for_uninteresting
543 ? !(sline[i].flag & mark)
544 : (sline[i].flag & mark))
545 return i;
546 else
547 i++;
548 return i;
551 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
553 unsigned long all_mask = (1UL<<num_parent) - 1;
554 unsigned long mark = (1UL<<num_parent);
555 unsigned long no_pre_delete = (2UL<<num_parent);
556 unsigned long i;
558 /* Two groups of interesting lines may have a short gap of
559 * uninteresting lines. Connect such groups to give them a
560 * bit of context.
562 * We first start from what the interesting() function says,
563 * and mark them with "mark", and paint context lines with the
564 * mark. So interesting() would still say false for such context
565 * lines but they are treated as "interesting" in the end.
567 i = find_next(sline, mark, 0, cnt, 0);
568 if (cnt < i)
569 return 0;
571 while (i <= cnt) {
572 unsigned long j = (context < i) ? (i - context) : 0;
573 unsigned long k;
575 /* Paint a few lines before the first interesting line. */
576 while (j < i) {
577 if (!(sline[j].flag & mark))
578 sline[j].flag |= no_pre_delete;
579 sline[j++].flag |= mark;
582 again:
583 /* we know up to i is to be included. where does the
584 * next uninteresting one start?
586 j = find_next(sline, mark, i, cnt, 1);
587 if (cnt < j)
588 break; /* the rest are all interesting */
590 /* lookahead context lines */
591 k = find_next(sline, mark, j, cnt, 0);
592 j = adjust_hunk_tail(sline, all_mask, i, j);
594 if (k < j + context) {
595 /* k is interesting and [j,k) are not, but
596 * paint them interesting because the gap is small.
598 while (j < k)
599 sline[j++].flag |= mark;
600 i = k;
601 goto again;
604 /* j is the first uninteresting line and there is
605 * no overlap beyond it within context lines. Paint
606 * the trailing edge a bit.
608 i = k;
609 k = (j + context < cnt+1) ? j + context : cnt+1;
610 while (j < k)
611 sline[j++].flag |= mark;
613 return 1;
616 static int make_hunks(struct sline *sline, unsigned long cnt,
617 int num_parent, int dense)
619 unsigned long all_mask = (1UL<<num_parent) - 1;
620 unsigned long mark = (1UL<<num_parent);
621 unsigned long i;
622 int has_interesting = 0;
624 for (i = 0; i <= cnt; i++) {
625 if (interesting(&sline[i], all_mask))
626 sline[i].flag |= mark;
627 else
628 sline[i].flag &= ~mark;
630 if (!dense)
631 return give_context(sline, cnt, num_parent);
633 /* Look at each hunk, and if we have changes from only one
634 * parent, or the changes are the same from all but one
635 * parent, mark that uninteresting.
637 i = 0;
638 while (i <= cnt) {
639 unsigned long j, hunk_begin, hunk_end;
640 unsigned long same_diff;
641 while (i <= cnt && !(sline[i].flag & mark))
642 i++;
643 if (cnt < i)
644 break; /* No more interesting hunks */
645 hunk_begin = i;
646 for (j = i + 1; j <= cnt; j++) {
647 if (!(sline[j].flag & mark)) {
648 /* Look beyond the end to see if there
649 * is an interesting line after this
650 * hunk within context span.
652 unsigned long la; /* lookahead */
653 int contin = 0;
654 la = adjust_hunk_tail(sline, all_mask,
655 hunk_begin, j);
656 la = (la + context < cnt + 1) ?
657 (la + context) : cnt + 1;
658 while (la && j <= --la) {
659 if (sline[la].flag & mark) {
660 contin = 1;
661 break;
664 if (!contin)
665 break;
666 j = la;
669 hunk_end = j;
671 /* [i..hunk_end) are interesting. Now is it really
672 * interesting? We check if there are only two versions
673 * and the result matches one of them. That is, we look
674 * at:
675 * (+) line, which records lines added to which parents;
676 * this line appears in the result.
677 * (-) line, which records from what parents the line
678 * was removed; this line does not appear in the result.
679 * then check the set of parents the result has difference
680 * from, from all lines. If there are lines that has
681 * different set of parents that the result has differences
682 * from, that means we have more than two versions.
684 * Even when we have only two versions, if the result does
685 * not match any of the parents, the it should be considered
686 * interesting. In such a case, we would have all '+' line.
687 * After passing the above "two versions" test, that would
688 * appear as "the same set of parents" to be "all parents".
690 same_diff = 0;
691 has_interesting = 0;
692 for (j = i; j < hunk_end && !has_interesting; j++) {
693 unsigned long this_diff = sline[j].flag & all_mask;
694 struct lline *ll = sline[j].lost;
695 if (this_diff) {
696 /* This has some changes. Is it the
697 * same as others?
699 if (!same_diff)
700 same_diff = this_diff;
701 else if (same_diff != this_diff) {
702 has_interesting = 1;
703 break;
706 while (ll && !has_interesting) {
707 /* Lost this line from these parents;
708 * who are they? Are they the same?
710 this_diff = ll->parent_map;
711 if (!same_diff)
712 same_diff = this_diff;
713 else if (same_diff != this_diff) {
714 has_interesting = 1;
716 ll = ll->next;
720 if (!has_interesting && same_diff != all_mask) {
721 /* This hunk is not that interesting after all */
722 for (j = hunk_begin; j < hunk_end; j++)
723 sline[j].flag &= ~mark;
725 i = hunk_end;
728 has_interesting = give_context(sline, cnt, num_parent);
729 return has_interesting;
732 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
734 l0 = sline[l0].p_lno[n];
735 l1 = sline[l1].p_lno[n];
736 printf(" -%lu,%lu", l0, l1-l0-null_context);
739 static int hunk_comment_line(const char *bol)
741 int ch;
743 if (!bol)
744 return 0;
745 ch = *bol & 0xff;
746 return (isalpha(ch) || ch == '_' || ch == '$');
749 static void show_line_to_eol(const char *line, int len, const char *reset)
751 int saw_cr_at_eol = 0;
752 if (len < 0)
753 len = strlen(line);
754 saw_cr_at_eol = (len && line[len-1] == '\r');
756 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
757 reset,
758 saw_cr_at_eol ? "\r" : "");
761 static void dump_sline(struct sline *sline, const char *line_prefix,
762 unsigned long cnt, int num_parent,
763 int use_color, int result_deleted)
765 unsigned long mark = (1UL<<num_parent);
766 unsigned long no_pre_delete = (2UL<<num_parent);
767 int i;
768 unsigned long lno = 0;
769 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
770 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
771 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
772 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
773 const char *c_context = diff_get_color(use_color, DIFF_CONTEXT);
774 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
776 if (result_deleted)
777 return; /* result deleted */
779 while (1) {
780 unsigned long hunk_end;
781 unsigned long rlines;
782 const char *hunk_comment = NULL;
783 unsigned long null_context = 0;
785 while (lno <= cnt && !(sline[lno].flag & mark)) {
786 if (hunk_comment_line(sline[lno].bol))
787 hunk_comment = sline[lno].bol;
788 lno++;
790 if (cnt < lno)
791 break;
792 else {
793 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
794 if (!(sline[hunk_end].flag & mark))
795 break;
797 rlines = hunk_end - lno;
798 if (cnt < hunk_end)
799 rlines--; /* pointing at the last delete hunk */
801 if (!context) {
803 * Even when running with --unified=0, all
804 * lines in the hunk needs to be processed in
805 * the loop below in order to show the
806 * deletion recorded in lost_head. However,
807 * we do not want to show the resulting line
808 * with all blank context markers in such a
809 * case. Compensate.
811 unsigned long j;
812 for (j = lno; j < hunk_end; j++)
813 if (!(sline[j].flag & (mark-1)))
814 null_context++;
815 rlines -= null_context;
818 printf("%s%s", line_prefix, c_frag);
819 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
820 for (i = 0; i < num_parent; i++)
821 show_parent_lno(sline, lno, hunk_end, i, null_context);
822 printf(" +%lu,%lu ", lno+1, rlines);
823 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
825 if (hunk_comment) {
826 int comment_end = 0;
827 for (i = 0; i < 40; i++) {
828 int ch = hunk_comment[i] & 0xff;
829 if (!ch || ch == '\n')
830 break;
831 if (!isspace(ch))
832 comment_end = i;
834 if (comment_end)
835 printf("%s%s %s%s", c_reset,
836 c_context, c_reset,
837 c_func);
838 for (i = 0; i < comment_end; i++)
839 putchar(hunk_comment[i]);
842 printf("%s\n", c_reset);
843 while (lno < hunk_end) {
844 struct lline *ll;
845 int j;
846 unsigned long p_mask;
847 struct sline *sl = &sline[lno++];
848 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost;
849 while (ll) {
850 printf("%s%s", line_prefix, c_old);
851 for (j = 0; j < num_parent; j++) {
852 if (ll->parent_map & (1UL<<j))
853 putchar('-');
854 else
855 putchar(' ');
857 show_line_to_eol(ll->line, -1, c_reset);
858 ll = ll->next;
860 if (cnt < lno)
861 break;
862 p_mask = 1;
863 fputs(line_prefix, stdout);
864 if (!(sl->flag & (mark-1))) {
866 * This sline was here to hang the
867 * lost lines in front of it.
869 if (!context)
870 continue;
871 fputs(c_context, stdout);
873 else
874 fputs(c_new, stdout);
875 for (j = 0; j < num_parent; j++) {
876 if (p_mask & sl->flag)
877 putchar('+');
878 else
879 putchar(' ');
880 p_mask <<= 1;
882 show_line_to_eol(sl->bol, sl->len, c_reset);
887 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
888 int i, int j)
890 /* We have already examined parent j and we know parent i
891 * and parent j are the same, so reuse the combined result
892 * of parent j for parent i.
894 unsigned long lno, imask, jmask;
895 imask = (1UL<<i);
896 jmask = (1UL<<j);
898 for (lno = 0; lno <= cnt; lno++) {
899 struct lline *ll = sline->lost;
900 sline->p_lno[i] = sline->p_lno[j];
901 while (ll) {
902 if (ll->parent_map & jmask)
903 ll->parent_map |= imask;
904 ll = ll->next;
906 if (sline->flag & jmask)
907 sline->flag |= imask;
908 sline++;
910 /* the overall size of the file (sline[cnt]) */
911 sline->p_lno[i] = sline->p_lno[j];
914 static void dump_quoted_path(const char *head,
915 const char *prefix,
916 const char *path,
917 const char *line_prefix,
918 const char *c_meta, const char *c_reset)
920 static struct strbuf buf = STRBUF_INIT;
922 strbuf_reset(&buf);
923 strbuf_addstr(&buf, line_prefix);
924 strbuf_addstr(&buf, c_meta);
925 strbuf_addstr(&buf, head);
926 quote_two_c_style(&buf, prefix, path, 0);
927 strbuf_addstr(&buf, c_reset);
928 puts(buf.buf);
931 static void show_combined_header(struct combine_diff_path *elem,
932 int num_parent,
933 struct rev_info *rev,
934 const char *line_prefix,
935 int mode_differs,
936 int show_file_header)
938 struct diff_options *opt = &rev->diffopt;
939 int abbrev = opt->flags.full_index ? the_hash_algo->hexsz : DEFAULT_ABBREV;
940 const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
941 const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
942 const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
943 const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
944 const char *abb;
945 int added = 0;
946 int deleted = 0;
947 int i;
948 int dense = rev->dense_combined_merges;
950 if (rev->loginfo && !rev->no_commit_id)
951 show_log(rev);
953 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
954 "", elem->path, line_prefix, c_meta, c_reset);
955 printf("%s%sindex ", line_prefix, c_meta);
956 for (i = 0; i < num_parent; i++) {
957 abb = repo_find_unique_abbrev(the_repository,
958 &elem->parent[i].oid, abbrev);
959 printf("%s%s", i ? "," : "", abb);
961 abb = repo_find_unique_abbrev(the_repository, &elem->oid, abbrev);
962 printf("..%s%s\n", abb, c_reset);
964 if (mode_differs) {
965 deleted = !elem->mode;
967 /* We say it was added if nobody had it */
968 added = !deleted;
969 for (i = 0; added && i < num_parent; i++)
970 if (elem->parent[i].status !=
971 DIFF_STATUS_ADDED)
972 added = 0;
973 if (added)
974 printf("%s%snew file mode %06o",
975 line_prefix, c_meta, elem->mode);
976 else {
977 if (deleted)
978 printf("%s%sdeleted file ",
979 line_prefix, c_meta);
980 printf("mode ");
981 for (i = 0; i < num_parent; i++) {
982 printf("%s%06o", i ? "," : "",
983 elem->parent[i].mode);
985 if (elem->mode)
986 printf("..%06o", elem->mode);
988 printf("%s\n", c_reset);
991 if (!show_file_header)
992 return;
994 if (rev->combined_all_paths) {
995 for (i = 0; i < num_parent; i++) {
996 char *path = filename_changed(elem->parent[i].status)
997 ? elem->parent[i].path.buf : elem->path;
998 if (elem->parent[i].status == DIFF_STATUS_ADDED)
999 dump_quoted_path("--- ", "", "/dev/null",
1000 line_prefix, c_meta, c_reset);
1001 else
1002 dump_quoted_path("--- ", a_prefix, path,
1003 line_prefix, c_meta, c_reset);
1005 } else {
1006 if (added)
1007 dump_quoted_path("--- ", "", "/dev/null",
1008 line_prefix, c_meta, c_reset);
1009 else
1010 dump_quoted_path("--- ", a_prefix, elem->path,
1011 line_prefix, c_meta, c_reset);
1013 if (deleted)
1014 dump_quoted_path("+++ ", "", "/dev/null",
1015 line_prefix, c_meta, c_reset);
1016 else
1017 dump_quoted_path("+++ ", b_prefix, elem->path,
1018 line_prefix, c_meta, c_reset);
1021 static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
1022 int working_tree_file,
1023 struct rev_info *rev)
1025 struct diff_options *opt = &rev->diffopt;
1026 unsigned long result_size, cnt, lno;
1027 int result_deleted = 0;
1028 char *result, *cp;
1029 struct sline *sline; /* survived lines */
1030 int mode_differs = 0;
1031 int i, show_hunks;
1032 mmfile_t result_file;
1033 struct userdiff_driver *userdiff;
1034 struct userdiff_driver *textconv = NULL;
1035 int is_binary;
1036 const char *line_prefix = diff_line_prefix(opt);
1038 context = opt->context;
1039 userdiff = userdiff_find_by_path(opt->repo->index, elem->path);
1040 if (!userdiff)
1041 userdiff = userdiff_find_by_name("default");
1042 if (opt->flags.allow_textconv)
1043 textconv = userdiff_get_textconv(opt->repo, userdiff);
1045 /* Read the result of merge first */
1046 if (!working_tree_file)
1047 result = grab_blob(opt->repo, &elem->oid, elem->mode, &result_size,
1048 textconv, elem->path);
1049 else {
1050 /* Used by diff-tree to read from the working tree */
1051 struct stat st;
1052 int fd = -1;
1054 if (lstat(elem->path, &st) < 0)
1055 goto deleted_file;
1057 if (S_ISLNK(st.st_mode)) {
1058 struct strbuf buf = STRBUF_INIT;
1060 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
1061 error_errno("readlink(%s)", elem->path);
1062 return;
1064 result_size = buf.len;
1065 result = strbuf_detach(&buf, NULL);
1066 elem->mode = canon_mode(st.st_mode);
1067 } else if (S_ISDIR(st.st_mode)) {
1068 struct object_id oid;
1069 if (resolve_gitlink_ref(elem->path, "HEAD", &oid) < 0)
1070 result = grab_blob(opt->repo, &elem->oid,
1071 elem->mode, &result_size,
1072 NULL, NULL);
1073 else
1074 result = grab_blob(opt->repo, &oid, elem->mode,
1075 &result_size, NULL, NULL);
1076 } else if (textconv) {
1077 struct diff_filespec *df = alloc_filespec(elem->path);
1078 fill_filespec(df, null_oid(), 0, st.st_mode);
1079 result_size = fill_textconv(opt->repo, textconv, df, &result);
1080 free_filespec(df);
1081 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
1082 size_t len = xsize_t(st.st_size);
1083 ssize_t done;
1084 int is_file, i;
1086 elem->mode = canon_mode(st.st_mode);
1087 /* if symlinks don't work, assume symlink if all parents
1088 * are symlinks
1090 is_file = has_symlinks;
1091 for (i = 0; !is_file && i < num_parent; i++)
1092 is_file = !S_ISLNK(elem->parent[i].mode);
1093 if (!is_file)
1094 elem->mode = canon_mode(S_IFLNK);
1096 result_size = len;
1097 result = xmallocz(len);
1099 done = read_in_full(fd, result, len);
1100 if (done < 0)
1101 die_errno("read error '%s'", elem->path);
1102 else if (done < len)
1103 die("early EOF '%s'", elem->path);
1105 /* If not a fake symlink, apply filters, e.g. autocrlf */
1106 if (is_file) {
1107 struct strbuf buf = STRBUF_INIT;
1109 if (convert_to_git(rev->diffopt.repo->index,
1110 elem->path, result, len, &buf, global_conv_flags_eol)) {
1111 free(result);
1112 result = strbuf_detach(&buf, &len);
1113 result_size = len;
1117 else {
1118 deleted_file:
1119 result_deleted = 1;
1120 result_size = 0;
1121 elem->mode = 0;
1122 result = xcalloc(1, 1);
1125 if (0 <= fd)
1126 close(fd);
1129 for (i = 0; i < num_parent; i++) {
1130 if (elem->parent[i].mode != elem->mode) {
1131 mode_differs = 1;
1132 break;
1136 if (textconv)
1137 is_binary = 0;
1138 else if (userdiff->binary != -1)
1139 is_binary = userdiff->binary;
1140 else {
1141 is_binary = buffer_is_binary(result, result_size);
1142 for (i = 0; !is_binary && i < num_parent; i++) {
1143 char *buf;
1144 unsigned long size;
1145 buf = grab_blob(opt->repo,
1146 &elem->parent[i].oid,
1147 elem->parent[i].mode,
1148 &size, NULL, NULL);
1149 if (buffer_is_binary(buf, size))
1150 is_binary = 1;
1151 free(buf);
1154 if (is_binary) {
1155 show_combined_header(elem, num_parent, rev,
1156 line_prefix, mode_differs, 0);
1157 printf("Binary files differ\n");
1158 free(result);
1159 return;
1162 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
1163 if (*cp == '\n')
1164 cnt++;
1166 if (result_size && result[result_size-1] != '\n')
1167 cnt++; /* incomplete line */
1169 CALLOC_ARRAY(sline, st_add(cnt, 2));
1170 sline[0].bol = result;
1171 for (lno = 0, cp = result; cp < result + result_size; cp++) {
1172 if (*cp == '\n') {
1173 sline[lno].len = cp - sline[lno].bol;
1174 lno++;
1175 if (lno < cnt)
1176 sline[lno].bol = cp + 1;
1179 if (result_size && result[result_size-1] != '\n')
1180 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
1182 result_file.ptr = result;
1183 result_file.size = result_size;
1185 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1186 * for deletion hunk at the end.
1188 CALLOC_ARRAY(sline[0].p_lno, st_mult(st_add(cnt, 2), num_parent));
1189 for (lno = 0; lno <= cnt; lno++)
1190 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
1192 for (i = 0; i < num_parent; i++) {
1193 int j;
1194 for (j = 0; j < i; j++) {
1195 if (oideq(&elem->parent[i].oid,
1196 &elem->parent[j].oid)) {
1197 reuse_combine_diff(sline, cnt, i, j);
1198 break;
1201 if (i <= j)
1202 combine_diff(opt->repo,
1203 &elem->parent[i].oid,
1204 elem->parent[i].mode,
1205 &result_file, sline,
1206 cnt, i, num_parent, result_deleted,
1207 textconv, elem->path, opt->xdl_opts);
1210 show_hunks = make_hunks(sline, cnt, num_parent, rev->dense_combined_merges);
1212 if (show_hunks || mode_differs || working_tree_file) {
1213 show_combined_header(elem, num_parent, rev,
1214 line_prefix, mode_differs, 1);
1215 dump_sline(sline, line_prefix, cnt, num_parent,
1216 opt->use_color, result_deleted);
1218 free(result);
1220 for (lno = 0; lno < cnt; lno++) {
1221 if (sline[lno].lost) {
1222 struct lline *ll = sline[lno].lost;
1223 while (ll) {
1224 struct lline *tmp = ll;
1225 ll = ll->next;
1226 free(tmp);
1230 free(sline[0].p_lno);
1231 free(sline);
1234 static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
1236 struct diff_options *opt = &rev->diffopt;
1237 int line_termination, inter_name_termination, i;
1238 const char *line_prefix = diff_line_prefix(opt);
1240 line_termination = opt->line_termination;
1241 inter_name_termination = '\t';
1242 if (!line_termination)
1243 inter_name_termination = 0;
1245 if (rev->loginfo && !rev->no_commit_id)
1246 show_log(rev);
1249 if (opt->output_format & DIFF_FORMAT_RAW) {
1250 printf("%s", line_prefix);
1252 /* As many colons as there are parents */
1253 for (i = 0; i < num_parent; i++)
1254 putchar(':');
1256 /* Show the modes */
1257 for (i = 0; i < num_parent; i++)
1258 printf("%06o ", p->parent[i].mode);
1259 printf("%06o", p->mode);
1261 /* Show sha1's */
1262 for (i = 0; i < num_parent; i++)
1263 printf(" %s", diff_aligned_abbrev(&p->parent[i].oid,
1264 opt->abbrev));
1265 printf(" %s ", diff_aligned_abbrev(&p->oid, opt->abbrev));
1268 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
1269 for (i = 0; i < num_parent; i++)
1270 putchar(p->parent[i].status);
1271 putchar(inter_name_termination);
1274 for (i = 0; i < num_parent; i++)
1275 if (rev->combined_all_paths) {
1276 if (filename_changed(p->parent[i].status))
1277 write_name_quoted(p->parent[i].path.buf, stdout,
1278 inter_name_termination);
1279 else
1280 write_name_quoted(p->path, stdout,
1281 inter_name_termination);
1283 write_name_quoted(p->path, stdout, line_termination);
1287 * The result (p->elem) is from the working tree and their
1288 * parents are typically from multiple stages during a merge
1289 * (i.e. diff-files) or the state in HEAD and in the index
1290 * (i.e. diff-index).
1292 void show_combined_diff(struct combine_diff_path *p,
1293 int num_parent,
1294 struct rev_info *rev)
1296 struct diff_options *opt = &rev->diffopt;
1298 if (opt->output_format & (DIFF_FORMAT_RAW |
1299 DIFF_FORMAT_NAME |
1300 DIFF_FORMAT_NAME_STATUS))
1301 show_raw_diff(p, num_parent, rev);
1302 else if (opt->output_format & DIFF_FORMAT_PATCH)
1303 show_patch_diff(p, num_parent, 1, rev);
1306 static void free_combined_pair(struct diff_filepair *pair)
1308 free(pair->two);
1309 free(pair);
1313 * A combine_diff_path expresses N parents on the LHS against 1 merge
1314 * result. Synthesize a diff_filepair that has N entries on the "one"
1315 * side and 1 entry on the "two" side.
1317 * In the future, we might want to add more data to combine_diff_path
1318 * so that we can fill fields we are ignoring (most notably, size) here,
1319 * but currently nobody uses it, so this should suffice for now.
1321 static struct diff_filepair *combined_pair(struct combine_diff_path *p,
1322 int num_parent)
1324 int i;
1325 struct diff_filepair *pair;
1326 struct diff_filespec *pool;
1328 pair = xmalloc(sizeof(*pair));
1329 CALLOC_ARRAY(pool, st_add(num_parent, 1));
1330 pair->one = pool + 1;
1331 pair->two = pool;
1333 for (i = 0; i < num_parent; i++) {
1334 pair->one[i].path = p->path;
1335 pair->one[i].mode = p->parent[i].mode;
1336 oidcpy(&pair->one[i].oid, &p->parent[i].oid);
1337 pair->one[i].oid_valid = !is_null_oid(&p->parent[i].oid);
1338 pair->one[i].has_more_entries = 1;
1340 pair->one[num_parent - 1].has_more_entries = 0;
1342 pair->two->path = p->path;
1343 pair->two->mode = p->mode;
1344 oidcpy(&pair->two->oid, &p->oid);
1345 pair->two->oid_valid = !is_null_oid(&p->oid);
1346 return pair;
1349 static void handle_combined_callback(struct diff_options *opt,
1350 struct combine_diff_path *paths,
1351 int num_parent,
1352 int num_paths)
1354 struct combine_diff_path *p;
1355 struct diff_queue_struct q;
1356 int i;
1358 CALLOC_ARRAY(q.queue, num_paths);
1359 q.alloc = num_paths;
1360 q.nr = num_paths;
1361 for (i = 0, p = paths; p; p = p->next)
1362 q.queue[i++] = combined_pair(p, num_parent);
1363 opt->format_callback(&q, opt, opt->format_callback_data);
1364 for (i = 0; i < num_paths; i++)
1365 free_combined_pair(q.queue[i]);
1366 free(q.queue);
1369 static const char *path_path(void *obj)
1371 struct combine_diff_path *path = (struct combine_diff_path *)obj;
1373 return path->path;
1377 * Diff stat formats which we always compute solely against the first parent.
1379 #define STAT_FORMAT_MASK (DIFF_FORMAT_NUMSTAT \
1380 | DIFF_FORMAT_SHORTSTAT \
1381 | DIFF_FORMAT_SUMMARY \
1382 | DIFF_FORMAT_DIRSTAT \
1383 | DIFF_FORMAT_DIFFSTAT)
1385 /* find set of paths that every parent touches */
1386 static struct combine_diff_path *find_paths_generic(const struct object_id *oid,
1387 const struct oid_array *parents,
1388 struct diff_options *opt,
1389 int combined_all_paths)
1391 struct combine_diff_path *paths = NULL;
1392 int i, num_parent = parents->nr;
1394 int output_format = opt->output_format;
1395 const char *orderfile = opt->orderfile;
1397 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1398 /* tell diff_tree to emit paths in sorted (=tree) order */
1399 opt->orderfile = NULL;
1401 /* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn) (wrt paths) */
1402 for (i = 0; i < num_parent; i++) {
1404 * show stat against the first parent even when doing
1405 * combined diff.
1407 int stat_opt = output_format & STAT_FORMAT_MASK;
1408 if (i == 0 && stat_opt)
1409 opt->output_format = stat_opt;
1410 else
1411 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1412 diff_tree_oid(&parents->oid[i], oid, "", opt);
1413 diffcore_std(opt);
1414 paths = intersect_paths(paths, i, num_parent,
1415 combined_all_paths);
1417 /* if showing diff, show it in requested order */
1418 if (opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1419 orderfile) {
1420 diffcore_order(orderfile);
1423 diff_flush(opt);
1426 opt->output_format = output_format;
1427 opt->orderfile = orderfile;
1428 return paths;
1433 * find set of paths that everybody touches, assuming diff is run without
1434 * rename/copy detection, etc, comparing all trees simultaneously (= faster).
1436 static struct combine_diff_path *find_paths_multitree(
1437 const struct object_id *oid, const struct oid_array *parents,
1438 struct diff_options *opt)
1440 int i, nparent = parents->nr;
1441 const struct object_id **parents_oid;
1442 struct combine_diff_path paths_head;
1443 struct strbuf base;
1445 ALLOC_ARRAY(parents_oid, nparent);
1446 for (i = 0; i < nparent; i++)
1447 parents_oid[i] = &parents->oid[i];
1449 /* fake list head, so worker can assume it is non-NULL */
1450 paths_head.next = NULL;
1452 strbuf_init(&base, PATH_MAX);
1453 diff_tree_paths(&paths_head, oid, parents_oid, nparent, &base, opt);
1455 strbuf_release(&base);
1456 free(parents_oid);
1457 return paths_head.next;
1460 static int match_objfind(struct combine_diff_path *path,
1461 int num_parent,
1462 const struct oidset *set)
1464 int i;
1465 if (oidset_contains(set, &path->oid))
1466 return 1;
1467 for (i = 0; i < num_parent; i++) {
1468 if (oidset_contains(set, &path->parent[i].oid))
1469 return 1;
1471 return 0;
1474 static struct combine_diff_path *combined_objfind(struct diff_options *opt,
1475 struct combine_diff_path *paths,
1476 int num_parent)
1478 struct combine_diff_path *ret = NULL, **tail = &ret;
1479 struct combine_diff_path *p = paths;
1481 while (p) {
1482 struct combine_diff_path *next = p->next;
1484 if (match_objfind(p, num_parent, opt->objfind)) {
1485 p->next = NULL;
1486 *tail = p;
1487 tail = &p->next;
1488 } else {
1489 free(p);
1491 p = next;
1494 return ret;
1497 void diff_tree_combined(const struct object_id *oid,
1498 const struct oid_array *parents,
1499 struct rev_info *rev)
1501 struct diff_options *opt = &rev->diffopt;
1502 struct diff_options diffopts;
1503 struct combine_diff_path *p, *paths;
1504 int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
1505 int need_generic_pathscan;
1507 if (opt->ignore_regex_nr)
1508 die("combined diff and '%s' cannot be used together",
1509 "--ignore-matching-lines");
1510 if (opt->close_file)
1511 die("combined diff and '%s' cannot be used together",
1512 "--output");
1514 /* nothing to do, if no parents */
1515 if (!num_parent)
1516 return;
1518 show_log_first = !!rev->loginfo && !rev->no_commit_id;
1519 needsep = 0;
1520 if (show_log_first) {
1521 show_log(rev);
1523 if (rev->verbose_header && opt->output_format &&
1524 opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1525 !commit_format_is_empty(rev->commit_format))
1526 printf("%s%c", diff_line_prefix(opt),
1527 opt->line_termination);
1530 diffopts = *opt;
1531 copy_pathspec(&diffopts.pathspec, &opt->pathspec);
1532 diffopts.flags.recursive = 1;
1533 diffopts.flags.allow_external = 0;
1535 /* find set of paths that everybody touches
1537 * NOTE
1539 * Diffcore transformations are bound to diff_filespec and logic
1540 * comparing two entries - i.e. they do not apply directly to combine
1541 * diff.
1543 * If some of such transformations is requested - we launch generic
1544 * path scanning, which works significantly slower compared to
1545 * simultaneous all-trees-in-one-go scan in find_paths_multitree().
1547 * TODO some of the filters could be ported to work on
1548 * combine_diff_paths - i.e. all functionality that skips paths, so in
1549 * theory, we could end up having only multitree path scanning.
1551 * NOTE please keep this semantically in sync with diffcore_std()
1553 need_generic_pathscan = opt->skip_stat_unmatch ||
1554 opt->flags.follow_renames ||
1555 opt->break_opt != -1 ||
1556 opt->detect_rename ||
1557 (opt->pickaxe_opts &
1558 (DIFF_PICKAXE_KINDS_MASK & ~DIFF_PICKAXE_KIND_OBJFIND)) ||
1559 opt->filter;
1561 if (need_generic_pathscan) {
1563 * NOTE generic case also handles --stat, as it computes
1564 * diff(sha1,parent_i) for all i to do the job, specifically
1565 * for parent0.
1567 paths = find_paths_generic(oid, parents, &diffopts,
1568 rev->combined_all_paths);
1570 else {
1571 int stat_opt;
1572 paths = find_paths_multitree(oid, parents, &diffopts);
1574 if (opt->pickaxe_opts & DIFF_PICKAXE_KIND_OBJFIND)
1575 paths = combined_objfind(opt, paths, num_parent);
1578 * show stat against the first parent even
1579 * when doing combined diff.
1581 stat_opt = opt->output_format & STAT_FORMAT_MASK;
1582 if (stat_opt) {
1583 diffopts.output_format = stat_opt;
1585 diff_tree_oid(&parents->oid[0], oid, "", &diffopts);
1586 diffcore_std(&diffopts);
1587 if (opt->orderfile)
1588 diffcore_order(opt->orderfile);
1589 diff_flush(&diffopts);
1593 /* find out number of surviving paths */
1594 for (num_paths = 0, p = paths; p; p = p->next)
1595 num_paths++;
1597 /* order paths according to diffcore_order */
1598 if (opt->orderfile && num_paths) {
1599 struct obj_order *o;
1601 ALLOC_ARRAY(o, num_paths);
1602 for (i = 0, p = paths; p; p = p->next, i++)
1603 o[i].obj = p;
1604 order_objects(opt->orderfile, path_path, o, num_paths);
1605 for (i = 0; i < num_paths - 1; i++) {
1606 p = o[i].obj;
1607 p->next = o[i+1].obj;
1610 p = o[num_paths-1].obj;
1611 p->next = NULL;
1612 paths = o[0].obj;
1613 free(o);
1617 if (num_paths) {
1618 if (opt->output_format & (DIFF_FORMAT_RAW |
1619 DIFF_FORMAT_NAME |
1620 DIFF_FORMAT_NAME_STATUS)) {
1621 for (p = paths; p; p = p->next)
1622 show_raw_diff(p, num_parent, rev);
1623 needsep = 1;
1625 else if (opt->output_format & STAT_FORMAT_MASK)
1626 needsep = 1;
1627 else if (opt->output_format & DIFF_FORMAT_CALLBACK)
1628 handle_combined_callback(opt, paths, num_parent, num_paths);
1630 if (opt->output_format & DIFF_FORMAT_PATCH) {
1631 if (needsep)
1632 printf("%s%c", diff_line_prefix(opt),
1633 opt->line_termination);
1634 for (p = paths; p; p = p->next)
1635 show_patch_diff(p, num_parent, 0, rev);
1639 /* Clean things up */
1640 while (paths) {
1641 struct combine_diff_path *tmp = paths;
1642 paths = paths->next;
1643 for (i = 0; i < num_parent; i++)
1644 if (rev->combined_all_paths &&
1645 filename_changed(tmp->parent[i].status))
1646 strbuf_release(&tmp->parent[i].path);
1647 free(tmp);
1650 clear_pathspec(&diffopts.pathspec);
1653 void diff_tree_combined_merge(const struct commit *commit,
1654 struct rev_info *rev)
1656 struct commit_list *parent = get_saved_parents(rev, commit);
1657 struct oid_array parents = OID_ARRAY_INIT;
1659 while (parent) {
1660 oid_array_append(&parents, &parent->item->object.oid);
1661 parent = parent->next;
1663 diff_tree_combined(&commit->object.oid, &parents, rev);
1664 oid_array_clear(&parents);