Merge branch 'pw/wildmatch-fixes'
[git.git] / combine-diff.c
blob91051dc3258925a10d6de5d67101720376b8d630
1 #include "cache.h"
2 #include "object-store.h"
3 #include "commit.h"
4 #include "blob.h"
5 #include "diff.h"
6 #include "diffcore.h"
7 #include "hex.h"
8 #include "quote.h"
9 #include "xdiff-interface.h"
10 #include "xdiff/xmacros.h"
11 #include "log-tree.h"
12 #include "refs.h"
13 #include "userdiff.h"
14 #include "oid-array.h"
15 #include "revision.h"
17 static int compare_paths(const struct combine_diff_path *one,
18 const struct diff_filespec *two)
20 if (!S_ISDIR(one->mode) && !S_ISDIR(two->mode))
21 return strcmp(one->path, two->path);
23 return base_name_compare(one->path, strlen(one->path), one->mode,
24 two->path, strlen(two->path), two->mode);
27 static int filename_changed(char status)
29 return status == 'R' || status == 'C';
32 static struct combine_diff_path *intersect_paths(
33 struct combine_diff_path *curr,
34 int n,
35 int num_parent,
36 int combined_all_paths)
38 struct diff_queue_struct *q = &diff_queued_diff;
39 struct combine_diff_path *p, **tail = &curr;
40 int i, j, cmp;
42 if (!n) {
43 for (i = 0; i < q->nr; i++) {
44 int len;
45 const char *path;
46 if (diff_unmodified_pair(q->queue[i]))
47 continue;
48 path = q->queue[i]->two->path;
49 len = strlen(path);
50 p = xmalloc(combine_diff_path_size(num_parent, len));
51 p->path = (char *) &(p->parent[num_parent]);
52 memcpy(p->path, path, len);
53 p->path[len] = 0;
54 p->next = NULL;
55 memset(p->parent, 0,
56 sizeof(p->parent[0]) * num_parent);
58 oidcpy(&p->oid, &q->queue[i]->two->oid);
59 p->mode = q->queue[i]->two->mode;
60 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
61 p->parent[n].mode = q->queue[i]->one->mode;
62 p->parent[n].status = q->queue[i]->status;
64 if (combined_all_paths &&
65 filename_changed(p->parent[n].status)) {
66 strbuf_init(&p->parent[n].path, 0);
67 strbuf_addstr(&p->parent[n].path,
68 q->queue[i]->one->path);
70 *tail = p;
71 tail = &p->next;
73 return curr;
77 * paths in curr (linked list) and q->queue[] (array) are
78 * both sorted in the tree order.
80 i = 0;
81 while ((p = *tail) != NULL) {
82 cmp = ((i >= q->nr)
83 ? -1 : compare_paths(p, q->queue[i]->two));
85 if (cmp < 0) {
86 /* p->path not in q->queue[]; drop it */
87 *tail = p->next;
88 for (j = 0; j < num_parent; j++)
89 if (combined_all_paths &&
90 filename_changed(p->parent[j].status))
91 strbuf_release(&p->parent[j].path);
92 free(p);
93 continue;
96 if (cmp > 0) {
97 /* q->queue[i] not in p->path; skip it */
98 i++;
99 continue;
102 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
103 p->parent[n].mode = q->queue[i]->one->mode;
104 p->parent[n].status = q->queue[i]->status;
105 if (combined_all_paths &&
106 filename_changed(p->parent[n].status))
107 strbuf_addstr(&p->parent[n].path,
108 q->queue[i]->one->path);
110 tail = &p->next;
111 i++;
113 return curr;
116 /* Lines lost from parent */
117 struct lline {
118 struct lline *next, *prev;
119 int len;
120 unsigned long parent_map;
121 char line[FLEX_ARRAY];
124 /* Lines lost from current parent (before coalescing) */
125 struct plost {
126 struct lline *lost_head, *lost_tail;
127 int len;
130 /* Lines surviving in the merge result */
131 struct sline {
132 /* Accumulated and coalesced lost lines */
133 struct lline *lost;
134 int lenlost;
135 struct plost plost;
136 char *bol;
137 int len;
138 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
139 * we did not change it).
140 * bit N is used for "interesting" lines, including context.
141 * bit (N+1) is used for "do not show deletion before this".
143 unsigned long flag;
144 unsigned long *p_lno;
147 static int match_string_spaces(const char *line1, int len1,
148 const char *line2, int len2,
149 long flags)
151 if (flags & XDF_WHITESPACE_FLAGS) {
152 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
153 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
156 if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
157 return (len1 == len2 && !memcmp(line1, line2, len1));
159 while (len1 > 0 && len2 > 0) {
160 len1--;
161 len2--;
162 if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
163 if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
164 (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
165 return 0;
167 for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
168 for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
170 if (line1[len1] != line2[len2])
171 return 0;
174 if (flags & XDF_IGNORE_WHITESPACE) {
175 /* Consume remaining spaces */
176 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
177 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
180 /* We matched full line1 and line2 */
181 if (!len1 && !len2)
182 return 1;
184 return 0;
187 enum coalesce_direction { MATCH, BASE, NEW };
189 /* Coalesce new lines into base by finding LCS */
190 static struct lline *coalesce_lines(struct lline *base, int *lenbase,
191 struct lline *newline, int lennew,
192 unsigned long parent, long flags)
194 int **lcs;
195 enum coalesce_direction **directions;
196 struct lline *baseend, *newend = NULL;
197 int i, j, origbaselen = *lenbase;
199 if (!newline)
200 return base;
202 if (!base) {
203 *lenbase = lennew;
204 return newline;
208 * Coalesce new lines into base by finding the LCS
209 * - Create the table to run dynamic programming
210 * - Compute the LCS
211 * - Then reverse read the direction structure:
212 * - If we have MATCH, assign parent to base flag, and consume
213 * both baseend and newend
214 * - Else if we have BASE, consume baseend
215 * - Else if we have NEW, insert newend lline into base and
216 * consume newend
218 CALLOC_ARRAY(lcs, st_add(origbaselen, 1));
219 CALLOC_ARRAY(directions, st_add(origbaselen, 1));
220 for (i = 0; i < origbaselen + 1; i++) {
221 CALLOC_ARRAY(lcs[i], st_add(lennew, 1));
222 CALLOC_ARRAY(directions[i], st_add(lennew, 1));
223 directions[i][0] = BASE;
225 for (j = 1; j < lennew + 1; j++)
226 directions[0][j] = NEW;
228 for (i = 1, baseend = base; i < origbaselen + 1; i++) {
229 for (j = 1, newend = newline; j < lennew + 1; j++) {
230 if (match_string_spaces(baseend->line, baseend->len,
231 newend->line, newend->len, flags)) {
232 lcs[i][j] = lcs[i - 1][j - 1] + 1;
233 directions[i][j] = MATCH;
234 } else if (lcs[i][j - 1] >= lcs[i - 1][j]) {
235 lcs[i][j] = lcs[i][j - 1];
236 directions[i][j] = NEW;
237 } else {
238 lcs[i][j] = lcs[i - 1][j];
239 directions[i][j] = BASE;
241 if (newend->next)
242 newend = newend->next;
244 if (baseend->next)
245 baseend = baseend->next;
248 for (i = 0; i < origbaselen + 1; i++)
249 free(lcs[i]);
250 free(lcs);
252 /* At this point, baseend and newend point to the end of each lists */
253 i--;
254 j--;
255 while (i != 0 || j != 0) {
256 if (directions[i][j] == MATCH) {
257 baseend->parent_map |= 1<<parent;
258 baseend = baseend->prev;
259 newend = newend->prev;
260 i--;
261 j--;
262 } else if (directions[i][j] == NEW) {
263 struct lline *lline;
265 lline = newend;
266 /* Remove lline from new list and update newend */
267 if (lline->prev)
268 lline->prev->next = lline->next;
269 else
270 newline = lline->next;
271 if (lline->next)
272 lline->next->prev = lline->prev;
274 newend = lline->prev;
275 j--;
277 /* Add lline to base list */
278 if (baseend) {
279 lline->next = baseend->next;
280 lline->prev = baseend;
281 if (lline->prev)
282 lline->prev->next = lline;
284 else {
285 lline->next = base;
286 base = lline;
288 (*lenbase)++;
290 if (lline->next)
291 lline->next->prev = lline;
293 } else {
294 baseend = baseend->prev;
295 i--;
299 newend = newline;
300 while (newend) {
301 struct lline *lline = newend;
302 newend = newend->next;
303 free(lline);
306 for (i = 0; i < origbaselen + 1; i++)
307 free(directions[i]);
308 free(directions);
310 return base;
313 static char *grab_blob(struct repository *r,
314 const struct object_id *oid, unsigned int mode,
315 unsigned long *size, struct userdiff_driver *textconv,
316 const char *path)
318 char *blob;
319 enum object_type type;
321 if (S_ISGITLINK(mode)) {
322 struct strbuf buf = STRBUF_INIT;
323 strbuf_addf(&buf, "Subproject commit %s\n", oid_to_hex(oid));
324 *size = buf.len;
325 blob = strbuf_detach(&buf, NULL);
326 } else if (is_null_oid(oid)) {
327 /* deleted blob */
328 *size = 0;
329 return xcalloc(1, 1);
330 } else if (textconv) {
331 struct diff_filespec *df = alloc_filespec(path);
332 fill_filespec(df, oid, 1, mode);
333 *size = fill_textconv(r, textconv, df, &blob);
334 free_filespec(df);
335 } else {
336 blob = read_object_file(oid, &type, size);
337 if (type != OBJ_BLOB)
338 die("object '%s' is not a blob!", oid_to_hex(oid));
340 return blob;
343 static void append_lost(struct sline *sline, int n, const char *line, int len)
345 struct lline *lline;
346 unsigned long this_mask = (1UL<<n);
347 if (line[len-1] == '\n')
348 len--;
350 FLEX_ALLOC_MEM(lline, line, line, len);
351 lline->len = len;
352 lline->next = NULL;
353 lline->prev = sline->plost.lost_tail;
354 if (lline->prev)
355 lline->prev->next = lline;
356 else
357 sline->plost.lost_head = lline;
358 sline->plost.lost_tail = lline;
359 sline->plost.len++;
360 lline->parent_map = this_mask;
363 struct combine_diff_state {
364 unsigned int lno;
365 int ob, on, nb, nn;
366 unsigned long nmask;
367 int num_parent;
368 int n;
369 struct sline *sline;
370 struct sline *lost_bucket;
373 static void consume_hunk(void *state_,
374 long ob, long on,
375 long nb, long nn,
376 const char *func UNUSED, long funclen UNUSED)
378 struct combine_diff_state *state = state_;
380 state->ob = ob;
381 state->on = on;
382 state->nb = nb;
383 state->nn = nn;
384 state->lno = state->nb;
385 if (state->nn == 0) {
386 /* @@ -X,Y +N,0 @@ removed Y lines
387 * that would have come *after* line N
388 * in the result. Our lost buckets hang
389 * to the line after the removed lines,
391 * Note that this is correct even when N == 0,
392 * in which case the hunk removes the first
393 * line in the file.
395 state->lost_bucket = &state->sline[state->nb];
396 if (!state->nb)
397 state->nb = 1;
398 } else {
399 state->lost_bucket = &state->sline[state->nb-1];
401 if (!state->sline[state->nb-1].p_lno)
402 CALLOC_ARRAY(state->sline[state->nb - 1].p_lno,
403 state->num_parent);
404 state->sline[state->nb-1].p_lno[state->n] = state->ob;
407 static int consume_line(void *state_, char *line, unsigned long len)
409 struct combine_diff_state *state = state_;
410 if (!state->lost_bucket)
411 return 0; /* not in any hunk yet */
412 switch (line[0]) {
413 case '-':
414 append_lost(state->lost_bucket, state->n, line+1, len-1);
415 break;
416 case '+':
417 state->sline[state->lno-1].flag |= state->nmask;
418 state->lno++;
419 break;
421 return 0;
424 static void combine_diff(struct repository *r,
425 const struct object_id *parent, unsigned int mode,
426 mmfile_t *result_file,
427 struct sline *sline, unsigned int cnt, int n,
428 int num_parent, int result_deleted,
429 struct userdiff_driver *textconv,
430 const char *path, long flags)
432 unsigned int p_lno, lno;
433 unsigned long nmask = (1UL << n);
434 xpparam_t xpp;
435 xdemitconf_t xecfg;
436 mmfile_t parent_file;
437 struct combine_diff_state state;
438 unsigned long sz;
440 if (result_deleted)
441 return; /* result deleted */
443 parent_file.ptr = grab_blob(r, parent, mode, &sz, textconv, path);
444 parent_file.size = sz;
445 memset(&xpp, 0, sizeof(xpp));
446 xpp.flags = flags;
447 memset(&xecfg, 0, sizeof(xecfg));
448 memset(&state, 0, sizeof(state));
449 state.nmask = nmask;
450 state.sline = sline;
451 state.lno = 1;
452 state.num_parent = num_parent;
453 state.n = n;
455 if (xdi_diff_outf(&parent_file, result_file, consume_hunk,
456 consume_line, &state, &xpp, &xecfg))
457 die("unable to generate combined diff for %s",
458 oid_to_hex(parent));
459 free(parent_file.ptr);
461 /* Assign line numbers for this parent.
463 * sline[lno].p_lno[n] records the first line number
464 * (counting from 1) for parent N if the final hunk display
465 * started by showing sline[lno] (possibly showing the lost
466 * lines attached to it first).
468 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
469 struct lline *ll;
470 sline[lno].p_lno[n] = p_lno;
472 /* Coalesce new lines */
473 if (sline[lno].plost.lost_head) {
474 struct sline *sl = &sline[lno];
475 sl->lost = coalesce_lines(sl->lost, &sl->lenlost,
476 sl->plost.lost_head,
477 sl->plost.len, n, flags);
478 sl->plost.lost_head = sl->plost.lost_tail = NULL;
479 sl->plost.len = 0;
482 /* How many lines would this sline advance the p_lno? */
483 ll = sline[lno].lost;
484 while (ll) {
485 if (ll->parent_map & nmask)
486 p_lno++; /* '-' means parent had it */
487 ll = ll->next;
489 if (lno < cnt && !(sline[lno].flag & nmask))
490 p_lno++; /* no '+' means parent had it */
492 sline[lno].p_lno[n] = p_lno; /* trailer */
495 static unsigned long context = 3;
496 static char combine_marker = '@';
498 static int interesting(struct sline *sline, unsigned long all_mask)
500 /* If some parents lost lines here, or if we have added to
501 * some parent, it is interesting.
503 return ((sline->flag & all_mask) || sline->lost);
506 static unsigned long adjust_hunk_tail(struct sline *sline,
507 unsigned long all_mask,
508 unsigned long hunk_begin,
509 unsigned long i)
511 /* i points at the first uninteresting line. If the last line
512 * of the hunk was interesting only because it has some
513 * deletion, then it is not all that interesting for the
514 * purpose of giving trailing context lines. This is because
515 * we output '-' line and then unmodified sline[i-1] itself in
516 * that case which gives us one extra context line.
518 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
519 i--;
520 return i;
523 static unsigned long find_next(struct sline *sline,
524 unsigned long mark,
525 unsigned long i,
526 unsigned long cnt,
527 int look_for_uninteresting)
529 /* We have examined up to i-1 and are about to look at i.
530 * Find next interesting or uninteresting line. Here,
531 * "interesting" does not mean interesting(), but marked by
532 * the give_context() function below (i.e. it includes context
533 * lines that are not interesting to interesting() function
534 * that are surrounded by interesting() ones.
536 while (i <= cnt)
537 if (look_for_uninteresting
538 ? !(sline[i].flag & mark)
539 : (sline[i].flag & mark))
540 return i;
541 else
542 i++;
543 return i;
546 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
548 unsigned long all_mask = (1UL<<num_parent) - 1;
549 unsigned long mark = (1UL<<num_parent);
550 unsigned long no_pre_delete = (2UL<<num_parent);
551 unsigned long i;
553 /* Two groups of interesting lines may have a short gap of
554 * uninteresting lines. Connect such groups to give them a
555 * bit of context.
557 * We first start from what the interesting() function says,
558 * and mark them with "mark", and paint context lines with the
559 * mark. So interesting() would still say false for such context
560 * lines but they are treated as "interesting" in the end.
562 i = find_next(sline, mark, 0, cnt, 0);
563 if (cnt < i)
564 return 0;
566 while (i <= cnt) {
567 unsigned long j = (context < i) ? (i - context) : 0;
568 unsigned long k;
570 /* Paint a few lines before the first interesting line. */
571 while (j < i) {
572 if (!(sline[j].flag & mark))
573 sline[j].flag |= no_pre_delete;
574 sline[j++].flag |= mark;
577 again:
578 /* we know up to i is to be included. where does the
579 * next uninteresting one start?
581 j = find_next(sline, mark, i, cnt, 1);
582 if (cnt < j)
583 break; /* the rest are all interesting */
585 /* lookahead context lines */
586 k = find_next(sline, mark, j, cnt, 0);
587 j = adjust_hunk_tail(sline, all_mask, i, j);
589 if (k < j + context) {
590 /* k is interesting and [j,k) are not, but
591 * paint them interesting because the gap is small.
593 while (j < k)
594 sline[j++].flag |= mark;
595 i = k;
596 goto again;
599 /* j is the first uninteresting line and there is
600 * no overlap beyond it within context lines. Paint
601 * the trailing edge a bit.
603 i = k;
604 k = (j + context < cnt+1) ? j + context : cnt+1;
605 while (j < k)
606 sline[j++].flag |= mark;
608 return 1;
611 static int make_hunks(struct sline *sline, unsigned long cnt,
612 int num_parent, int dense)
614 unsigned long all_mask = (1UL<<num_parent) - 1;
615 unsigned long mark = (1UL<<num_parent);
616 unsigned long i;
617 int has_interesting = 0;
619 for (i = 0; i <= cnt; i++) {
620 if (interesting(&sline[i], all_mask))
621 sline[i].flag |= mark;
622 else
623 sline[i].flag &= ~mark;
625 if (!dense)
626 return give_context(sline, cnt, num_parent);
628 /* Look at each hunk, and if we have changes from only one
629 * parent, or the changes are the same from all but one
630 * parent, mark that uninteresting.
632 i = 0;
633 while (i <= cnt) {
634 unsigned long j, hunk_begin, hunk_end;
635 unsigned long same_diff;
636 while (i <= cnt && !(sline[i].flag & mark))
637 i++;
638 if (cnt < i)
639 break; /* No more interesting hunks */
640 hunk_begin = i;
641 for (j = i + 1; j <= cnt; j++) {
642 if (!(sline[j].flag & mark)) {
643 /* Look beyond the end to see if there
644 * is an interesting line after this
645 * hunk within context span.
647 unsigned long la; /* lookahead */
648 int contin = 0;
649 la = adjust_hunk_tail(sline, all_mask,
650 hunk_begin, j);
651 la = (la + context < cnt + 1) ?
652 (la + context) : cnt + 1;
653 while (la && j <= --la) {
654 if (sline[la].flag & mark) {
655 contin = 1;
656 break;
659 if (!contin)
660 break;
661 j = la;
664 hunk_end = j;
666 /* [i..hunk_end) are interesting. Now is it really
667 * interesting? We check if there are only two versions
668 * and the result matches one of them. That is, we look
669 * at:
670 * (+) line, which records lines added to which parents;
671 * this line appears in the result.
672 * (-) line, which records from what parents the line
673 * was removed; this line does not appear in the result.
674 * then check the set of parents the result has difference
675 * from, from all lines. If there are lines that has
676 * different set of parents that the result has differences
677 * from, that means we have more than two versions.
679 * Even when we have only two versions, if the result does
680 * not match any of the parents, the it should be considered
681 * interesting. In such a case, we would have all '+' line.
682 * After passing the above "two versions" test, that would
683 * appear as "the same set of parents" to be "all parents".
685 same_diff = 0;
686 has_interesting = 0;
687 for (j = i; j < hunk_end && !has_interesting; j++) {
688 unsigned long this_diff = sline[j].flag & all_mask;
689 struct lline *ll = sline[j].lost;
690 if (this_diff) {
691 /* This has some changes. Is it the
692 * same as others?
694 if (!same_diff)
695 same_diff = this_diff;
696 else if (same_diff != this_diff) {
697 has_interesting = 1;
698 break;
701 while (ll && !has_interesting) {
702 /* Lost this line from these parents;
703 * who are they? Are they the same?
705 this_diff = ll->parent_map;
706 if (!same_diff)
707 same_diff = this_diff;
708 else if (same_diff != this_diff) {
709 has_interesting = 1;
711 ll = ll->next;
715 if (!has_interesting && same_diff != all_mask) {
716 /* This hunk is not that interesting after all */
717 for (j = hunk_begin; j < hunk_end; j++)
718 sline[j].flag &= ~mark;
720 i = hunk_end;
723 has_interesting = give_context(sline, cnt, num_parent);
724 return has_interesting;
727 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
729 l0 = sline[l0].p_lno[n];
730 l1 = sline[l1].p_lno[n];
731 printf(" -%lu,%lu", l0, l1-l0-null_context);
734 static int hunk_comment_line(const char *bol)
736 int ch;
738 if (!bol)
739 return 0;
740 ch = *bol & 0xff;
741 return (isalpha(ch) || ch == '_' || ch == '$');
744 static void show_line_to_eol(const char *line, int len, const char *reset)
746 int saw_cr_at_eol = 0;
747 if (len < 0)
748 len = strlen(line);
749 saw_cr_at_eol = (len && line[len-1] == '\r');
751 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
752 reset,
753 saw_cr_at_eol ? "\r" : "");
756 static void dump_sline(struct sline *sline, const char *line_prefix,
757 unsigned long cnt, int num_parent,
758 int use_color, int result_deleted)
760 unsigned long mark = (1UL<<num_parent);
761 unsigned long no_pre_delete = (2UL<<num_parent);
762 int i;
763 unsigned long lno = 0;
764 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
765 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
766 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
767 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
768 const char *c_context = diff_get_color(use_color, DIFF_CONTEXT);
769 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
771 if (result_deleted)
772 return; /* result deleted */
774 while (1) {
775 unsigned long hunk_end;
776 unsigned long rlines;
777 const char *hunk_comment = NULL;
778 unsigned long null_context = 0;
780 while (lno <= cnt && !(sline[lno].flag & mark)) {
781 if (hunk_comment_line(sline[lno].bol))
782 hunk_comment = sline[lno].bol;
783 lno++;
785 if (cnt < lno)
786 break;
787 else {
788 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
789 if (!(sline[hunk_end].flag & mark))
790 break;
792 rlines = hunk_end - lno;
793 if (cnt < hunk_end)
794 rlines--; /* pointing at the last delete hunk */
796 if (!context) {
798 * Even when running with --unified=0, all
799 * lines in the hunk needs to be processed in
800 * the loop below in order to show the
801 * deletion recorded in lost_head. However,
802 * we do not want to show the resulting line
803 * with all blank context markers in such a
804 * case. Compensate.
806 unsigned long j;
807 for (j = lno; j < hunk_end; j++)
808 if (!(sline[j].flag & (mark-1)))
809 null_context++;
810 rlines -= null_context;
813 printf("%s%s", line_prefix, c_frag);
814 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
815 for (i = 0; i < num_parent; i++)
816 show_parent_lno(sline, lno, hunk_end, i, null_context);
817 printf(" +%lu,%lu ", lno+1, rlines);
818 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
820 if (hunk_comment) {
821 int comment_end = 0;
822 for (i = 0; i < 40; i++) {
823 int ch = hunk_comment[i] & 0xff;
824 if (!ch || ch == '\n')
825 break;
826 if (!isspace(ch))
827 comment_end = i;
829 if (comment_end)
830 printf("%s%s %s%s", c_reset,
831 c_context, c_reset,
832 c_func);
833 for (i = 0; i < comment_end; i++)
834 putchar(hunk_comment[i]);
837 printf("%s\n", c_reset);
838 while (lno < hunk_end) {
839 struct lline *ll;
840 int j;
841 unsigned long p_mask;
842 struct sline *sl = &sline[lno++];
843 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost;
844 while (ll) {
845 printf("%s%s", line_prefix, c_old);
846 for (j = 0; j < num_parent; j++) {
847 if (ll->parent_map & (1UL<<j))
848 putchar('-');
849 else
850 putchar(' ');
852 show_line_to_eol(ll->line, -1, c_reset);
853 ll = ll->next;
855 if (cnt < lno)
856 break;
857 p_mask = 1;
858 fputs(line_prefix, stdout);
859 if (!(sl->flag & (mark-1))) {
861 * This sline was here to hang the
862 * lost lines in front of it.
864 if (!context)
865 continue;
866 fputs(c_context, stdout);
868 else
869 fputs(c_new, stdout);
870 for (j = 0; j < num_parent; j++) {
871 if (p_mask & sl->flag)
872 putchar('+');
873 else
874 putchar(' ');
875 p_mask <<= 1;
877 show_line_to_eol(sl->bol, sl->len, c_reset);
882 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
883 int i, int j)
885 /* We have already examined parent j and we know parent i
886 * and parent j are the same, so reuse the combined result
887 * of parent j for parent i.
889 unsigned long lno, imask, jmask;
890 imask = (1UL<<i);
891 jmask = (1UL<<j);
893 for (lno = 0; lno <= cnt; lno++) {
894 struct lline *ll = sline->lost;
895 sline->p_lno[i] = sline->p_lno[j];
896 while (ll) {
897 if (ll->parent_map & jmask)
898 ll->parent_map |= imask;
899 ll = ll->next;
901 if (sline->flag & jmask)
902 sline->flag |= imask;
903 sline++;
905 /* the overall size of the file (sline[cnt]) */
906 sline->p_lno[i] = sline->p_lno[j];
909 static void dump_quoted_path(const char *head,
910 const char *prefix,
911 const char *path,
912 const char *line_prefix,
913 const char *c_meta, const char *c_reset)
915 static struct strbuf buf = STRBUF_INIT;
917 strbuf_reset(&buf);
918 strbuf_addstr(&buf, line_prefix);
919 strbuf_addstr(&buf, c_meta);
920 strbuf_addstr(&buf, head);
921 quote_two_c_style(&buf, prefix, path, 0);
922 strbuf_addstr(&buf, c_reset);
923 puts(buf.buf);
926 static void show_combined_header(struct combine_diff_path *elem,
927 int num_parent,
928 struct rev_info *rev,
929 const char *line_prefix,
930 int mode_differs,
931 int show_file_header)
933 struct diff_options *opt = &rev->diffopt;
934 int abbrev = opt->flags.full_index ? the_hash_algo->hexsz : DEFAULT_ABBREV;
935 const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
936 const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
937 const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
938 const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
939 const char *abb;
940 int added = 0;
941 int deleted = 0;
942 int i;
943 int dense = rev->dense_combined_merges;
945 if (rev->loginfo && !rev->no_commit_id)
946 show_log(rev);
948 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
949 "", elem->path, line_prefix, c_meta, c_reset);
950 printf("%s%sindex ", line_prefix, c_meta);
951 for (i = 0; i < num_parent; i++) {
952 abb = find_unique_abbrev(&elem->parent[i].oid,
953 abbrev);
954 printf("%s%s", i ? "," : "", abb);
956 abb = find_unique_abbrev(&elem->oid, abbrev);
957 printf("..%s%s\n", abb, c_reset);
959 if (mode_differs) {
960 deleted = !elem->mode;
962 /* We say it was added if nobody had it */
963 added = !deleted;
964 for (i = 0; added && i < num_parent; i++)
965 if (elem->parent[i].status !=
966 DIFF_STATUS_ADDED)
967 added = 0;
968 if (added)
969 printf("%s%snew file mode %06o",
970 line_prefix, c_meta, elem->mode);
971 else {
972 if (deleted)
973 printf("%s%sdeleted file ",
974 line_prefix, c_meta);
975 printf("mode ");
976 for (i = 0; i < num_parent; i++) {
977 printf("%s%06o", i ? "," : "",
978 elem->parent[i].mode);
980 if (elem->mode)
981 printf("..%06o", elem->mode);
983 printf("%s\n", c_reset);
986 if (!show_file_header)
987 return;
989 if (rev->combined_all_paths) {
990 for (i = 0; i < num_parent; i++) {
991 char *path = filename_changed(elem->parent[i].status)
992 ? elem->parent[i].path.buf : elem->path;
993 if (elem->parent[i].status == DIFF_STATUS_ADDED)
994 dump_quoted_path("--- ", "", "/dev/null",
995 line_prefix, c_meta, c_reset);
996 else
997 dump_quoted_path("--- ", a_prefix, path,
998 line_prefix, c_meta, c_reset);
1000 } else {
1001 if (added)
1002 dump_quoted_path("--- ", "", "/dev/null",
1003 line_prefix, c_meta, c_reset);
1004 else
1005 dump_quoted_path("--- ", a_prefix, elem->path,
1006 line_prefix, c_meta, c_reset);
1008 if (deleted)
1009 dump_quoted_path("+++ ", "", "/dev/null",
1010 line_prefix, c_meta, c_reset);
1011 else
1012 dump_quoted_path("+++ ", b_prefix, elem->path,
1013 line_prefix, c_meta, c_reset);
1016 static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
1017 int working_tree_file,
1018 struct rev_info *rev)
1020 struct diff_options *opt = &rev->diffopt;
1021 unsigned long result_size, cnt, lno;
1022 int result_deleted = 0;
1023 char *result, *cp;
1024 struct sline *sline; /* survived lines */
1025 int mode_differs = 0;
1026 int i, show_hunks;
1027 mmfile_t result_file;
1028 struct userdiff_driver *userdiff;
1029 struct userdiff_driver *textconv = NULL;
1030 int is_binary;
1031 const char *line_prefix = diff_line_prefix(opt);
1033 context = opt->context;
1034 userdiff = userdiff_find_by_path(opt->repo->index, elem->path);
1035 if (!userdiff)
1036 userdiff = userdiff_find_by_name("default");
1037 if (opt->flags.allow_textconv)
1038 textconv = userdiff_get_textconv(opt->repo, userdiff);
1040 /* Read the result of merge first */
1041 if (!working_tree_file)
1042 result = grab_blob(opt->repo, &elem->oid, elem->mode, &result_size,
1043 textconv, elem->path);
1044 else {
1045 /* Used by diff-tree to read from the working tree */
1046 struct stat st;
1047 int fd = -1;
1049 if (lstat(elem->path, &st) < 0)
1050 goto deleted_file;
1052 if (S_ISLNK(st.st_mode)) {
1053 struct strbuf buf = STRBUF_INIT;
1055 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
1056 error_errno("readlink(%s)", elem->path);
1057 return;
1059 result_size = buf.len;
1060 result = strbuf_detach(&buf, NULL);
1061 elem->mode = canon_mode(st.st_mode);
1062 } else if (S_ISDIR(st.st_mode)) {
1063 struct object_id oid;
1064 if (resolve_gitlink_ref(elem->path, "HEAD", &oid) < 0)
1065 result = grab_blob(opt->repo, &elem->oid,
1066 elem->mode, &result_size,
1067 NULL, NULL);
1068 else
1069 result = grab_blob(opt->repo, &oid, elem->mode,
1070 &result_size, NULL, NULL);
1071 } else if (textconv) {
1072 struct diff_filespec *df = alloc_filespec(elem->path);
1073 fill_filespec(df, null_oid(), 0, st.st_mode);
1074 result_size = fill_textconv(opt->repo, textconv, df, &result);
1075 free_filespec(df);
1076 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
1077 size_t len = xsize_t(st.st_size);
1078 ssize_t done;
1079 int is_file, i;
1081 elem->mode = canon_mode(st.st_mode);
1082 /* if symlinks don't work, assume symlink if all parents
1083 * are symlinks
1085 is_file = has_symlinks;
1086 for (i = 0; !is_file && i < num_parent; i++)
1087 is_file = !S_ISLNK(elem->parent[i].mode);
1088 if (!is_file)
1089 elem->mode = canon_mode(S_IFLNK);
1091 result_size = len;
1092 result = xmallocz(len);
1094 done = read_in_full(fd, result, len);
1095 if (done < 0)
1096 die_errno("read error '%s'", elem->path);
1097 else if (done < len)
1098 die("early EOF '%s'", elem->path);
1100 /* If not a fake symlink, apply filters, e.g. autocrlf */
1101 if (is_file) {
1102 struct strbuf buf = STRBUF_INIT;
1104 if (convert_to_git(rev->diffopt.repo->index,
1105 elem->path, result, len, &buf, global_conv_flags_eol)) {
1106 free(result);
1107 result = strbuf_detach(&buf, &len);
1108 result_size = len;
1112 else {
1113 deleted_file:
1114 result_deleted = 1;
1115 result_size = 0;
1116 elem->mode = 0;
1117 result = xcalloc(1, 1);
1120 if (0 <= fd)
1121 close(fd);
1124 for (i = 0; i < num_parent; i++) {
1125 if (elem->parent[i].mode != elem->mode) {
1126 mode_differs = 1;
1127 break;
1131 if (textconv)
1132 is_binary = 0;
1133 else if (userdiff->binary != -1)
1134 is_binary = userdiff->binary;
1135 else {
1136 is_binary = buffer_is_binary(result, result_size);
1137 for (i = 0; !is_binary && i < num_parent; i++) {
1138 char *buf;
1139 unsigned long size;
1140 buf = grab_blob(opt->repo,
1141 &elem->parent[i].oid,
1142 elem->parent[i].mode,
1143 &size, NULL, NULL);
1144 if (buffer_is_binary(buf, size))
1145 is_binary = 1;
1146 free(buf);
1149 if (is_binary) {
1150 show_combined_header(elem, num_parent, rev,
1151 line_prefix, mode_differs, 0);
1152 printf("Binary files differ\n");
1153 free(result);
1154 return;
1157 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
1158 if (*cp == '\n')
1159 cnt++;
1161 if (result_size && result[result_size-1] != '\n')
1162 cnt++; /* incomplete line */
1164 CALLOC_ARRAY(sline, st_add(cnt, 2));
1165 sline[0].bol = result;
1166 for (lno = 0, cp = result; cp < result + result_size; cp++) {
1167 if (*cp == '\n') {
1168 sline[lno].len = cp - sline[lno].bol;
1169 lno++;
1170 if (lno < cnt)
1171 sline[lno].bol = cp + 1;
1174 if (result_size && result[result_size-1] != '\n')
1175 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
1177 result_file.ptr = result;
1178 result_file.size = result_size;
1180 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1181 * for deletion hunk at the end.
1183 CALLOC_ARRAY(sline[0].p_lno, st_mult(st_add(cnt, 2), num_parent));
1184 for (lno = 0; lno <= cnt; lno++)
1185 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
1187 for (i = 0; i < num_parent; i++) {
1188 int j;
1189 for (j = 0; j < i; j++) {
1190 if (oideq(&elem->parent[i].oid,
1191 &elem->parent[j].oid)) {
1192 reuse_combine_diff(sline, cnt, i, j);
1193 break;
1196 if (i <= j)
1197 combine_diff(opt->repo,
1198 &elem->parent[i].oid,
1199 elem->parent[i].mode,
1200 &result_file, sline,
1201 cnt, i, num_parent, result_deleted,
1202 textconv, elem->path, opt->xdl_opts);
1205 show_hunks = make_hunks(sline, cnt, num_parent, rev->dense_combined_merges);
1207 if (show_hunks || mode_differs || working_tree_file) {
1208 show_combined_header(elem, num_parent, rev,
1209 line_prefix, mode_differs, 1);
1210 dump_sline(sline, line_prefix, cnt, num_parent,
1211 opt->use_color, result_deleted);
1213 free(result);
1215 for (lno = 0; lno < cnt; lno++) {
1216 if (sline[lno].lost) {
1217 struct lline *ll = sline[lno].lost;
1218 while (ll) {
1219 struct lline *tmp = ll;
1220 ll = ll->next;
1221 free(tmp);
1225 free(sline[0].p_lno);
1226 free(sline);
1229 static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
1231 struct diff_options *opt = &rev->diffopt;
1232 int line_termination, inter_name_termination, i;
1233 const char *line_prefix = diff_line_prefix(opt);
1235 line_termination = opt->line_termination;
1236 inter_name_termination = '\t';
1237 if (!line_termination)
1238 inter_name_termination = 0;
1240 if (rev->loginfo && !rev->no_commit_id)
1241 show_log(rev);
1244 if (opt->output_format & DIFF_FORMAT_RAW) {
1245 printf("%s", line_prefix);
1247 /* As many colons as there are parents */
1248 for (i = 0; i < num_parent; i++)
1249 putchar(':');
1251 /* Show the modes */
1252 for (i = 0; i < num_parent; i++)
1253 printf("%06o ", p->parent[i].mode);
1254 printf("%06o", p->mode);
1256 /* Show sha1's */
1257 for (i = 0; i < num_parent; i++)
1258 printf(" %s", diff_aligned_abbrev(&p->parent[i].oid,
1259 opt->abbrev));
1260 printf(" %s ", diff_aligned_abbrev(&p->oid, opt->abbrev));
1263 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
1264 for (i = 0; i < num_parent; i++)
1265 putchar(p->parent[i].status);
1266 putchar(inter_name_termination);
1269 for (i = 0; i < num_parent; i++)
1270 if (rev->combined_all_paths) {
1271 if (filename_changed(p->parent[i].status))
1272 write_name_quoted(p->parent[i].path.buf, stdout,
1273 inter_name_termination);
1274 else
1275 write_name_quoted(p->path, stdout,
1276 inter_name_termination);
1278 write_name_quoted(p->path, stdout, line_termination);
1282 * The result (p->elem) is from the working tree and their
1283 * parents are typically from multiple stages during a merge
1284 * (i.e. diff-files) or the state in HEAD and in the index
1285 * (i.e. diff-index).
1287 void show_combined_diff(struct combine_diff_path *p,
1288 int num_parent,
1289 struct rev_info *rev)
1291 struct diff_options *opt = &rev->diffopt;
1293 if (opt->output_format & (DIFF_FORMAT_RAW |
1294 DIFF_FORMAT_NAME |
1295 DIFF_FORMAT_NAME_STATUS))
1296 show_raw_diff(p, num_parent, rev);
1297 else if (opt->output_format & DIFF_FORMAT_PATCH)
1298 show_patch_diff(p, num_parent, 1, rev);
1301 static void free_combined_pair(struct diff_filepair *pair)
1303 free(pair->two);
1304 free(pair);
1308 * A combine_diff_path expresses N parents on the LHS against 1 merge
1309 * result. Synthesize a diff_filepair that has N entries on the "one"
1310 * side and 1 entry on the "two" side.
1312 * In the future, we might want to add more data to combine_diff_path
1313 * so that we can fill fields we are ignoring (most notably, size) here,
1314 * but currently nobody uses it, so this should suffice for now.
1316 static struct diff_filepair *combined_pair(struct combine_diff_path *p,
1317 int num_parent)
1319 int i;
1320 struct diff_filepair *pair;
1321 struct diff_filespec *pool;
1323 pair = xmalloc(sizeof(*pair));
1324 CALLOC_ARRAY(pool, st_add(num_parent, 1));
1325 pair->one = pool + 1;
1326 pair->two = pool;
1328 for (i = 0; i < num_parent; i++) {
1329 pair->one[i].path = p->path;
1330 pair->one[i].mode = p->parent[i].mode;
1331 oidcpy(&pair->one[i].oid, &p->parent[i].oid);
1332 pair->one[i].oid_valid = !is_null_oid(&p->parent[i].oid);
1333 pair->one[i].has_more_entries = 1;
1335 pair->one[num_parent - 1].has_more_entries = 0;
1337 pair->two->path = p->path;
1338 pair->two->mode = p->mode;
1339 oidcpy(&pair->two->oid, &p->oid);
1340 pair->two->oid_valid = !is_null_oid(&p->oid);
1341 return pair;
1344 static void handle_combined_callback(struct diff_options *opt,
1345 struct combine_diff_path *paths,
1346 int num_parent,
1347 int num_paths)
1349 struct combine_diff_path *p;
1350 struct diff_queue_struct q;
1351 int i;
1353 CALLOC_ARRAY(q.queue, num_paths);
1354 q.alloc = num_paths;
1355 q.nr = num_paths;
1356 for (i = 0, p = paths; p; p = p->next)
1357 q.queue[i++] = combined_pair(p, num_parent);
1358 opt->format_callback(&q, opt, opt->format_callback_data);
1359 for (i = 0; i < num_paths; i++)
1360 free_combined_pair(q.queue[i]);
1361 free(q.queue);
1364 static const char *path_path(void *obj)
1366 struct combine_diff_path *path = (struct combine_diff_path *)obj;
1368 return path->path;
1372 * Diff stat formats which we always compute solely against the first parent.
1374 #define STAT_FORMAT_MASK (DIFF_FORMAT_NUMSTAT \
1375 | DIFF_FORMAT_SHORTSTAT \
1376 | DIFF_FORMAT_SUMMARY \
1377 | DIFF_FORMAT_DIRSTAT \
1378 | DIFF_FORMAT_DIFFSTAT)
1380 /* find set of paths that every parent touches */
1381 static struct combine_diff_path *find_paths_generic(const struct object_id *oid,
1382 const struct oid_array *parents,
1383 struct diff_options *opt,
1384 int combined_all_paths)
1386 struct combine_diff_path *paths = NULL;
1387 int i, num_parent = parents->nr;
1389 int output_format = opt->output_format;
1390 const char *orderfile = opt->orderfile;
1392 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1393 /* tell diff_tree to emit paths in sorted (=tree) order */
1394 opt->orderfile = NULL;
1396 /* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn) (wrt paths) */
1397 for (i = 0; i < num_parent; i++) {
1399 * show stat against the first parent even when doing
1400 * combined diff.
1402 int stat_opt = output_format & STAT_FORMAT_MASK;
1403 if (i == 0 && stat_opt)
1404 opt->output_format = stat_opt;
1405 else
1406 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1407 diff_tree_oid(&parents->oid[i], oid, "", opt);
1408 diffcore_std(opt);
1409 paths = intersect_paths(paths, i, num_parent,
1410 combined_all_paths);
1412 /* if showing diff, show it in requested order */
1413 if (opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1414 orderfile) {
1415 diffcore_order(orderfile);
1418 diff_flush(opt);
1421 opt->output_format = output_format;
1422 opt->orderfile = orderfile;
1423 return paths;
1428 * find set of paths that everybody touches, assuming diff is run without
1429 * rename/copy detection, etc, comparing all trees simultaneously (= faster).
1431 static struct combine_diff_path *find_paths_multitree(
1432 const struct object_id *oid, const struct oid_array *parents,
1433 struct diff_options *opt)
1435 int i, nparent = parents->nr;
1436 const struct object_id **parents_oid;
1437 struct combine_diff_path paths_head;
1438 struct strbuf base;
1440 ALLOC_ARRAY(parents_oid, nparent);
1441 for (i = 0; i < nparent; i++)
1442 parents_oid[i] = &parents->oid[i];
1444 /* fake list head, so worker can assume it is non-NULL */
1445 paths_head.next = NULL;
1447 strbuf_init(&base, PATH_MAX);
1448 diff_tree_paths(&paths_head, oid, parents_oid, nparent, &base, opt);
1450 strbuf_release(&base);
1451 free(parents_oid);
1452 return paths_head.next;
1455 static int match_objfind(struct combine_diff_path *path,
1456 int num_parent,
1457 const struct oidset *set)
1459 int i;
1460 if (oidset_contains(set, &path->oid))
1461 return 1;
1462 for (i = 0; i < num_parent; i++) {
1463 if (oidset_contains(set, &path->parent[i].oid))
1464 return 1;
1466 return 0;
1469 static struct combine_diff_path *combined_objfind(struct diff_options *opt,
1470 struct combine_diff_path *paths,
1471 int num_parent)
1473 struct combine_diff_path *ret = NULL, **tail = &ret;
1474 struct combine_diff_path *p = paths;
1476 while (p) {
1477 struct combine_diff_path *next = p->next;
1479 if (match_objfind(p, num_parent, opt->objfind)) {
1480 p->next = NULL;
1481 *tail = p;
1482 tail = &p->next;
1483 } else {
1484 free(p);
1486 p = next;
1489 return ret;
1492 void diff_tree_combined(const struct object_id *oid,
1493 const struct oid_array *parents,
1494 struct rev_info *rev)
1496 struct diff_options *opt = &rev->diffopt;
1497 struct diff_options diffopts;
1498 struct combine_diff_path *p, *paths;
1499 int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
1500 int need_generic_pathscan;
1502 if (opt->ignore_regex_nr)
1503 die("combined diff and '%s' cannot be used together",
1504 "--ignore-matching-lines");
1505 if (opt->close_file)
1506 die("combined diff and '%s' cannot be used together",
1507 "--output");
1509 /* nothing to do, if no parents */
1510 if (!num_parent)
1511 return;
1513 show_log_first = !!rev->loginfo && !rev->no_commit_id;
1514 needsep = 0;
1515 if (show_log_first) {
1516 show_log(rev);
1518 if (rev->verbose_header && opt->output_format &&
1519 opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1520 !commit_format_is_empty(rev->commit_format))
1521 printf("%s%c", diff_line_prefix(opt),
1522 opt->line_termination);
1525 diffopts = *opt;
1526 copy_pathspec(&diffopts.pathspec, &opt->pathspec);
1527 diffopts.flags.recursive = 1;
1528 diffopts.flags.allow_external = 0;
1530 /* find set of paths that everybody touches
1532 * NOTE
1534 * Diffcore transformations are bound to diff_filespec and logic
1535 * comparing two entries - i.e. they do not apply directly to combine
1536 * diff.
1538 * If some of such transformations is requested - we launch generic
1539 * path scanning, which works significantly slower compared to
1540 * simultaneous all-trees-in-one-go scan in find_paths_multitree().
1542 * TODO some of the filters could be ported to work on
1543 * combine_diff_paths - i.e. all functionality that skips paths, so in
1544 * theory, we could end up having only multitree path scanning.
1546 * NOTE please keep this semantically in sync with diffcore_std()
1548 need_generic_pathscan = opt->skip_stat_unmatch ||
1549 opt->flags.follow_renames ||
1550 opt->break_opt != -1 ||
1551 opt->detect_rename ||
1552 (opt->pickaxe_opts &
1553 (DIFF_PICKAXE_KINDS_MASK & ~DIFF_PICKAXE_KIND_OBJFIND)) ||
1554 opt->filter;
1556 if (need_generic_pathscan) {
1558 * NOTE generic case also handles --stat, as it computes
1559 * diff(sha1,parent_i) for all i to do the job, specifically
1560 * for parent0.
1562 paths = find_paths_generic(oid, parents, &diffopts,
1563 rev->combined_all_paths);
1565 else {
1566 int stat_opt;
1567 paths = find_paths_multitree(oid, parents, &diffopts);
1569 if (opt->pickaxe_opts & DIFF_PICKAXE_KIND_OBJFIND)
1570 paths = combined_objfind(opt, paths, num_parent);
1573 * show stat against the first parent even
1574 * when doing combined diff.
1576 stat_opt = opt->output_format & STAT_FORMAT_MASK;
1577 if (stat_opt) {
1578 diffopts.output_format = stat_opt;
1580 diff_tree_oid(&parents->oid[0], oid, "", &diffopts);
1581 diffcore_std(&diffopts);
1582 if (opt->orderfile)
1583 diffcore_order(opt->orderfile);
1584 diff_flush(&diffopts);
1588 /* find out number of surviving paths */
1589 for (num_paths = 0, p = paths; p; p = p->next)
1590 num_paths++;
1592 /* order paths according to diffcore_order */
1593 if (opt->orderfile && num_paths) {
1594 struct obj_order *o;
1596 ALLOC_ARRAY(o, num_paths);
1597 for (i = 0, p = paths; p; p = p->next, i++)
1598 o[i].obj = p;
1599 order_objects(opt->orderfile, path_path, o, num_paths);
1600 for (i = 0; i < num_paths - 1; i++) {
1601 p = o[i].obj;
1602 p->next = o[i+1].obj;
1605 p = o[num_paths-1].obj;
1606 p->next = NULL;
1607 paths = o[0].obj;
1608 free(o);
1612 if (num_paths) {
1613 if (opt->output_format & (DIFF_FORMAT_RAW |
1614 DIFF_FORMAT_NAME |
1615 DIFF_FORMAT_NAME_STATUS)) {
1616 for (p = paths; p; p = p->next)
1617 show_raw_diff(p, num_parent, rev);
1618 needsep = 1;
1620 else if (opt->output_format & STAT_FORMAT_MASK)
1621 needsep = 1;
1622 else if (opt->output_format & DIFF_FORMAT_CALLBACK)
1623 handle_combined_callback(opt, paths, num_parent, num_paths);
1625 if (opt->output_format & DIFF_FORMAT_PATCH) {
1626 if (needsep)
1627 printf("%s%c", diff_line_prefix(opt),
1628 opt->line_termination);
1629 for (p = paths; p; p = p->next)
1630 show_patch_diff(p, num_parent, 0, rev);
1634 /* Clean things up */
1635 while (paths) {
1636 struct combine_diff_path *tmp = paths;
1637 paths = paths->next;
1638 for (i = 0; i < num_parent; i++)
1639 if (rev->combined_all_paths &&
1640 filename_changed(tmp->parent[i].status))
1641 strbuf_release(&tmp->parent[i].path);
1642 free(tmp);
1645 clear_pathspec(&diffopts.pathspec);
1648 void diff_tree_combined_merge(const struct commit *commit,
1649 struct rev_info *rev)
1651 struct commit_list *parent = get_saved_parents(rev, commit);
1652 struct oid_array parents = OID_ARRAY_INIT;
1654 while (parent) {
1655 oid_array_append(&parents, &parent->item->object.oid);
1656 parent = parent->next;
1658 diff_tree_combined(&commit->object.oid, &parents, rev);
1659 oid_array_clear(&parents);