The fifth batch
[alt-git.git] / combine-diff.c
blob641bc92dbdbc136b61d69527f4ad5386a2d2c6de
1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
5 #include "object-store-ll.h"
6 #include "commit.h"
7 #include "convert.h"
8 #include "diff.h"
9 #include "diffcore.h"
10 #include "environment.h"
11 #include "hex.h"
12 #include "object-name.h"
13 #include "quote.h"
14 #include "xdiff-interface.h"
15 #include "xdiff/xmacros.h"
16 #include "log-tree.h"
17 #include "refs.h"
18 #include "tree.h"
19 #include "userdiff.h"
20 #include "oid-array.h"
21 #include "revision.h"
23 static int compare_paths(const struct combine_diff_path *one,
24 const struct diff_filespec *two)
26 if (!S_ISDIR(one->mode) && !S_ISDIR(two->mode))
27 return strcmp(one->path, two->path);
29 return base_name_compare(one->path, strlen(one->path), one->mode,
30 two->path, strlen(two->path), two->mode);
33 static int filename_changed(char status)
35 return status == 'R' || status == 'C';
38 static struct combine_diff_path *intersect_paths(
39 struct combine_diff_path *curr,
40 int n,
41 int num_parent,
42 int combined_all_paths)
44 struct diff_queue_struct *q = &diff_queued_diff;
45 struct combine_diff_path *p, **tail = &curr;
46 int i, j, cmp;
48 if (!n) {
49 for (i = 0; i < q->nr; i++) {
50 int len;
51 const char *path;
52 if (diff_unmodified_pair(q->queue[i]))
53 continue;
54 path = q->queue[i]->two->path;
55 len = strlen(path);
56 p = xmalloc(combine_diff_path_size(num_parent, len));
57 p->path = (char *) &(p->parent[num_parent]);
58 memcpy(p->path, path, len);
59 p->path[len] = 0;
60 p->next = NULL;
61 memset(p->parent, 0,
62 sizeof(p->parent[0]) * num_parent);
64 oidcpy(&p->oid, &q->queue[i]->two->oid);
65 p->mode = q->queue[i]->two->mode;
66 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
67 p->parent[n].mode = q->queue[i]->one->mode;
68 p->parent[n].status = q->queue[i]->status;
70 if (combined_all_paths &&
71 filename_changed(p->parent[n].status)) {
72 strbuf_init(&p->parent[n].path, 0);
73 strbuf_addstr(&p->parent[n].path,
74 q->queue[i]->one->path);
76 *tail = p;
77 tail = &p->next;
79 return curr;
83 * paths in curr (linked list) and q->queue[] (array) are
84 * both sorted in the tree order.
86 i = 0;
87 while ((p = *tail) != NULL) {
88 cmp = ((i >= q->nr)
89 ? -1 : compare_paths(p, q->queue[i]->two));
91 if (cmp < 0) {
92 /* p->path not in q->queue[]; drop it */
93 *tail = p->next;
94 for (j = 0; j < num_parent; j++)
95 if (combined_all_paths &&
96 filename_changed(p->parent[j].status))
97 strbuf_release(&p->parent[j].path);
98 free(p);
99 continue;
102 if (cmp > 0) {
103 /* q->queue[i] not in p->path; skip it */
104 i++;
105 continue;
108 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
109 p->parent[n].mode = q->queue[i]->one->mode;
110 p->parent[n].status = q->queue[i]->status;
111 if (combined_all_paths &&
112 filename_changed(p->parent[n].status))
113 strbuf_addstr(&p->parent[n].path,
114 q->queue[i]->one->path);
116 tail = &p->next;
117 i++;
119 return curr;
122 /* Lines lost from parent */
123 struct lline {
124 struct lline *next, *prev;
125 int len;
126 unsigned long parent_map;
127 char line[FLEX_ARRAY];
130 /* Lines lost from current parent (before coalescing) */
131 struct plost {
132 struct lline *lost_head, *lost_tail;
133 int len;
136 /* Lines surviving in the merge result */
137 struct sline {
138 /* Accumulated and coalesced lost lines */
139 struct lline *lost;
140 int lenlost;
141 struct plost plost;
142 char *bol;
143 int len;
144 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
145 * we did not change it).
146 * bit N is used for "interesting" lines, including context.
147 * bit (N+1) is used for "do not show deletion before this".
149 unsigned long flag;
150 unsigned long *p_lno;
153 static int match_string_spaces(const char *line1, int len1,
154 const char *line2, int len2,
155 long flags)
157 if (flags & XDF_WHITESPACE_FLAGS) {
158 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
159 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
162 if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
163 return (len1 == len2 && !memcmp(line1, line2, len1));
165 while (len1 > 0 && len2 > 0) {
166 len1--;
167 len2--;
168 if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
169 if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
170 (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
171 return 0;
173 for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
174 for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
176 if (line1[len1] != line2[len2])
177 return 0;
180 if (flags & XDF_IGNORE_WHITESPACE) {
181 /* Consume remaining spaces */
182 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
183 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
186 /* We matched full line1 and line2 */
187 if (!len1 && !len2)
188 return 1;
190 return 0;
193 enum coalesce_direction { MATCH, BASE, NEW };
195 /* Coalesce new lines into base by finding LCS */
196 static struct lline *coalesce_lines(struct lline *base, int *lenbase,
197 struct lline *newline, int lennew,
198 unsigned long parent, long flags)
200 int **lcs;
201 enum coalesce_direction **directions;
202 struct lline *baseend, *newend = NULL;
203 int i, j, origbaselen = *lenbase;
205 if (!newline)
206 return base;
208 if (!base) {
209 *lenbase = lennew;
210 return newline;
214 * Coalesce new lines into base by finding the LCS
215 * - Create the table to run dynamic programming
216 * - Compute the LCS
217 * - Then reverse read the direction structure:
218 * - If we have MATCH, assign parent to base flag, and consume
219 * both baseend and newend
220 * - Else if we have BASE, consume baseend
221 * - Else if we have NEW, insert newend lline into base and
222 * consume newend
224 CALLOC_ARRAY(lcs, st_add(origbaselen, 1));
225 CALLOC_ARRAY(directions, st_add(origbaselen, 1));
226 for (i = 0; i < origbaselen + 1; i++) {
227 CALLOC_ARRAY(lcs[i], st_add(lennew, 1));
228 CALLOC_ARRAY(directions[i], st_add(lennew, 1));
229 directions[i][0] = BASE;
231 for (j = 1; j < lennew + 1; j++)
232 directions[0][j] = NEW;
234 for (i = 1, baseend = base; i < origbaselen + 1; i++) {
235 for (j = 1, newend = newline; j < lennew + 1; j++) {
236 if (match_string_spaces(baseend->line, baseend->len,
237 newend->line, newend->len, flags)) {
238 lcs[i][j] = lcs[i - 1][j - 1] + 1;
239 directions[i][j] = MATCH;
240 } else if (lcs[i][j - 1] >= lcs[i - 1][j]) {
241 lcs[i][j] = lcs[i][j - 1];
242 directions[i][j] = NEW;
243 } else {
244 lcs[i][j] = lcs[i - 1][j];
245 directions[i][j] = BASE;
247 if (newend->next)
248 newend = newend->next;
250 if (baseend->next)
251 baseend = baseend->next;
254 for (i = 0; i < origbaselen + 1; i++)
255 free(lcs[i]);
256 free(lcs);
258 /* At this point, baseend and newend point to the end of each lists */
259 i--;
260 j--;
261 while (i != 0 || j != 0) {
262 if (directions[i][j] == MATCH) {
263 baseend->parent_map |= 1<<parent;
264 baseend = baseend->prev;
265 newend = newend->prev;
266 i--;
267 j--;
268 } else if (directions[i][j] == NEW) {
269 struct lline *lline;
271 lline = newend;
272 /* Remove lline from new list and update newend */
273 if (lline->prev)
274 lline->prev->next = lline->next;
275 else
276 newline = lline->next;
277 if (lline->next)
278 lline->next->prev = lline->prev;
280 newend = lline->prev;
281 j--;
283 /* Add lline to base list */
284 if (baseend) {
285 lline->next = baseend->next;
286 lline->prev = baseend;
287 if (lline->prev)
288 lline->prev->next = lline;
290 else {
291 lline->next = base;
292 base = lline;
294 (*lenbase)++;
296 if (lline->next)
297 lline->next->prev = lline;
299 } else {
300 baseend = baseend->prev;
301 i--;
305 newend = newline;
306 while (newend) {
307 struct lline *lline = newend;
308 newend = newend->next;
309 free(lline);
312 for (i = 0; i < origbaselen + 1; i++)
313 free(directions[i]);
314 free(directions);
316 return base;
319 static char *grab_blob(struct repository *r,
320 const struct object_id *oid, unsigned int mode,
321 unsigned long *size, struct userdiff_driver *textconv,
322 const char *path)
324 char *blob;
325 enum object_type type;
327 if (S_ISGITLINK(mode)) {
328 struct strbuf buf = STRBUF_INIT;
329 strbuf_addf(&buf, "Subproject commit %s\n", oid_to_hex(oid));
330 *size = buf.len;
331 blob = strbuf_detach(&buf, NULL);
332 } else if (is_null_oid(oid)) {
333 /* deleted blob */
334 *size = 0;
335 return xcalloc(1, 1);
336 } else if (textconv) {
337 struct diff_filespec *df = alloc_filespec(path);
338 fill_filespec(df, oid, 1, mode);
339 *size = fill_textconv(r, textconv, df, &blob);
340 free_filespec(df);
341 } else {
342 blob = repo_read_object_file(r, oid, &type, size);
343 if (!blob)
344 die(_("unable to read %s"), oid_to_hex(oid));
345 if (type != OBJ_BLOB)
346 die("object '%s' is not a blob!", oid_to_hex(oid));
348 return blob;
351 static void append_lost(struct sline *sline, int n, const char *line, int len)
353 struct lline *lline;
354 unsigned long this_mask = (1UL<<n);
355 if (line[len-1] == '\n')
356 len--;
358 FLEX_ALLOC_MEM(lline, line, line, len);
359 lline->len = len;
360 lline->next = NULL;
361 lline->prev = sline->plost.lost_tail;
362 if (lline->prev)
363 lline->prev->next = lline;
364 else
365 sline->plost.lost_head = lline;
366 sline->plost.lost_tail = lline;
367 sline->plost.len++;
368 lline->parent_map = this_mask;
371 struct combine_diff_state {
372 unsigned int lno;
373 int ob, on, nb, nn;
374 unsigned long nmask;
375 int num_parent;
376 int n;
377 struct sline *sline;
378 struct sline *lost_bucket;
381 static void consume_hunk(void *state_,
382 long ob, long on,
383 long nb, long nn,
384 const char *func UNUSED, long funclen UNUSED)
386 struct combine_diff_state *state = state_;
388 state->ob = ob;
389 state->on = on;
390 state->nb = nb;
391 state->nn = nn;
392 state->lno = state->nb;
393 if (state->nn == 0) {
394 /* @@ -X,Y +N,0 @@ removed Y lines
395 * that would have come *after* line N
396 * in the result. Our lost buckets hang
397 * to the line after the removed lines,
399 * Note that this is correct even when N == 0,
400 * in which case the hunk removes the first
401 * line in the file.
403 state->lost_bucket = &state->sline[state->nb];
404 if (!state->nb)
405 state->nb = 1;
406 } else {
407 state->lost_bucket = &state->sline[state->nb-1];
409 if (!state->sline[state->nb-1].p_lno)
410 CALLOC_ARRAY(state->sline[state->nb - 1].p_lno,
411 state->num_parent);
412 state->sline[state->nb-1].p_lno[state->n] = state->ob;
415 static int consume_line(void *state_, char *line, unsigned long len)
417 struct combine_diff_state *state = state_;
418 if (!state->lost_bucket)
419 return 0; /* not in any hunk yet */
420 switch (line[0]) {
421 case '-':
422 append_lost(state->lost_bucket, state->n, line+1, len-1);
423 break;
424 case '+':
425 state->sline[state->lno-1].flag |= state->nmask;
426 state->lno++;
427 break;
429 return 0;
432 static void combine_diff(struct repository *r,
433 const struct object_id *parent, unsigned int mode,
434 mmfile_t *result_file,
435 struct sline *sline, unsigned int cnt, int n,
436 int num_parent, int result_deleted,
437 struct userdiff_driver *textconv,
438 const char *path, long flags)
440 unsigned int p_lno, lno;
441 unsigned long nmask = (1UL << n);
442 xpparam_t xpp;
443 xdemitconf_t xecfg;
444 mmfile_t parent_file;
445 struct combine_diff_state state;
446 unsigned long sz;
448 if (result_deleted)
449 return; /* result deleted */
451 parent_file.ptr = grab_blob(r, parent, mode, &sz, textconv, path);
452 parent_file.size = sz;
453 memset(&xpp, 0, sizeof(xpp));
454 xpp.flags = flags;
455 memset(&xecfg, 0, sizeof(xecfg));
456 memset(&state, 0, sizeof(state));
457 state.nmask = nmask;
458 state.sline = sline;
459 state.lno = 1;
460 state.num_parent = num_parent;
461 state.n = n;
463 if (xdi_diff_outf(&parent_file, result_file, consume_hunk,
464 consume_line, &state, &xpp, &xecfg))
465 die("unable to generate combined diff for %s",
466 oid_to_hex(parent));
467 free(parent_file.ptr);
469 /* Assign line numbers for this parent.
471 * sline[lno].p_lno[n] records the first line number
472 * (counting from 1) for parent N if the final hunk display
473 * started by showing sline[lno] (possibly showing the lost
474 * lines attached to it first).
476 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
477 struct lline *ll;
478 sline[lno].p_lno[n] = p_lno;
480 /* Coalesce new lines */
481 if (sline[lno].plost.lost_head) {
482 struct sline *sl = &sline[lno];
483 sl->lost = coalesce_lines(sl->lost, &sl->lenlost,
484 sl->plost.lost_head,
485 sl->plost.len, n, flags);
486 sl->plost.lost_head = sl->plost.lost_tail = NULL;
487 sl->plost.len = 0;
490 /* How many lines would this sline advance the p_lno? */
491 ll = sline[lno].lost;
492 while (ll) {
493 if (ll->parent_map & nmask)
494 p_lno++; /* '-' means parent had it */
495 ll = ll->next;
497 if (lno < cnt && !(sline[lno].flag & nmask))
498 p_lno++; /* no '+' means parent had it */
500 sline[lno].p_lno[n] = p_lno; /* trailer */
503 static unsigned long context = 3;
504 static char combine_marker = '@';
506 static int interesting(struct sline *sline, unsigned long all_mask)
508 /* If some parents lost lines here, or if we have added to
509 * some parent, it is interesting.
511 return ((sline->flag & all_mask) || sline->lost);
514 static unsigned long adjust_hunk_tail(struct sline *sline,
515 unsigned long all_mask,
516 unsigned long hunk_begin,
517 unsigned long i)
519 /* i points at the first uninteresting line. If the last line
520 * of the hunk was interesting only because it has some
521 * deletion, then it is not all that interesting for the
522 * purpose of giving trailing context lines. This is because
523 * we output '-' line and then unmodified sline[i-1] itself in
524 * that case which gives us one extra context line.
526 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
527 i--;
528 return i;
531 static unsigned long find_next(struct sline *sline,
532 unsigned long mark,
533 unsigned long i,
534 unsigned long cnt,
535 int look_for_uninteresting)
537 /* We have examined up to i-1 and are about to look at i.
538 * Find next interesting or uninteresting line. Here,
539 * "interesting" does not mean interesting(), but marked by
540 * the give_context() function below (i.e. it includes context
541 * lines that are not interesting to interesting() function
542 * that are surrounded by interesting() ones.
544 while (i <= cnt)
545 if (look_for_uninteresting
546 ? !(sline[i].flag & mark)
547 : (sline[i].flag & mark))
548 return i;
549 else
550 i++;
551 return i;
554 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
556 unsigned long all_mask = (1UL<<num_parent) - 1;
557 unsigned long mark = (1UL<<num_parent);
558 unsigned long no_pre_delete = (2UL<<num_parent);
559 unsigned long i;
561 /* Two groups of interesting lines may have a short gap of
562 * uninteresting lines. Connect such groups to give them a
563 * bit of context.
565 * We first start from what the interesting() function says,
566 * and mark them with "mark", and paint context lines with the
567 * mark. So interesting() would still say false for such context
568 * lines but they are treated as "interesting" in the end.
570 i = find_next(sline, mark, 0, cnt, 0);
571 if (cnt < i)
572 return 0;
574 while (i <= cnt) {
575 unsigned long j = (context < i) ? (i - context) : 0;
576 unsigned long k;
578 /* Paint a few lines before the first interesting line. */
579 while (j < i) {
580 if (!(sline[j].flag & mark))
581 sline[j].flag |= no_pre_delete;
582 sline[j++].flag |= mark;
585 again:
586 /* we know up to i is to be included. where does the
587 * next uninteresting one start?
589 j = find_next(sline, mark, i, cnt, 1);
590 if (cnt < j)
591 break; /* the rest are all interesting */
593 /* lookahead context lines */
594 k = find_next(sline, mark, j, cnt, 0);
595 j = adjust_hunk_tail(sline, all_mask, i, j);
597 if (k < j + context) {
598 /* k is interesting and [j,k) are not, but
599 * paint them interesting because the gap is small.
601 while (j < k)
602 sline[j++].flag |= mark;
603 i = k;
604 goto again;
607 /* j is the first uninteresting line and there is
608 * no overlap beyond it within context lines. Paint
609 * the trailing edge a bit.
611 i = k;
612 k = (j + context < cnt+1) ? j + context : cnt+1;
613 while (j < k)
614 sline[j++].flag |= mark;
616 return 1;
619 static int make_hunks(struct sline *sline, unsigned long cnt,
620 int num_parent, int dense)
622 unsigned long all_mask = (1UL<<num_parent) - 1;
623 unsigned long mark = (1UL<<num_parent);
624 unsigned long i;
625 int has_interesting = 0;
627 for (i = 0; i <= cnt; i++) {
628 if (interesting(&sline[i], all_mask))
629 sline[i].flag |= mark;
630 else
631 sline[i].flag &= ~mark;
633 if (!dense)
634 return give_context(sline, cnt, num_parent);
636 /* Look at each hunk, and if we have changes from only one
637 * parent, or the changes are the same from all but one
638 * parent, mark that uninteresting.
640 i = 0;
641 while (i <= cnt) {
642 unsigned long j, hunk_begin, hunk_end;
643 unsigned long same_diff;
644 while (i <= cnt && !(sline[i].flag & mark))
645 i++;
646 if (cnt < i)
647 break; /* No more interesting hunks */
648 hunk_begin = i;
649 for (j = i + 1; j <= cnt; j++) {
650 if (!(sline[j].flag & mark)) {
651 /* Look beyond the end to see if there
652 * is an interesting line after this
653 * hunk within context span.
655 unsigned long la; /* lookahead */
656 int contin = 0;
657 la = adjust_hunk_tail(sline, all_mask,
658 hunk_begin, j);
659 la = (la + context < cnt + 1) ?
660 (la + context) : cnt + 1;
661 while (la && j <= --la) {
662 if (sline[la].flag & mark) {
663 contin = 1;
664 break;
667 if (!contin)
668 break;
669 j = la;
672 hunk_end = j;
674 /* [i..hunk_end) are interesting. Now is it really
675 * interesting? We check if there are only two versions
676 * and the result matches one of them. That is, we look
677 * at:
678 * (+) line, which records lines added to which parents;
679 * this line appears in the result.
680 * (-) line, which records from what parents the line
681 * was removed; this line does not appear in the result.
682 * then check the set of parents the result has difference
683 * from, from all lines. If there are lines that has
684 * different set of parents that the result has differences
685 * from, that means we have more than two versions.
687 * Even when we have only two versions, if the result does
688 * not match any of the parents, the it should be considered
689 * interesting. In such a case, we would have all '+' line.
690 * After passing the above "two versions" test, that would
691 * appear as "the same set of parents" to be "all parents".
693 same_diff = 0;
694 has_interesting = 0;
695 for (j = i; j < hunk_end && !has_interesting; j++) {
696 unsigned long this_diff = sline[j].flag & all_mask;
697 struct lline *ll = sline[j].lost;
698 if (this_diff) {
699 /* This has some changes. Is it the
700 * same as others?
702 if (!same_diff)
703 same_diff = this_diff;
704 else if (same_diff != this_diff) {
705 has_interesting = 1;
706 break;
709 while (ll && !has_interesting) {
710 /* Lost this line from these parents;
711 * who are they? Are they the same?
713 this_diff = ll->parent_map;
714 if (!same_diff)
715 same_diff = this_diff;
716 else if (same_diff != this_diff) {
717 has_interesting = 1;
719 ll = ll->next;
723 if (!has_interesting && same_diff != all_mask) {
724 /* This hunk is not that interesting after all */
725 for (j = hunk_begin; j < hunk_end; j++)
726 sline[j].flag &= ~mark;
728 i = hunk_end;
731 has_interesting = give_context(sline, cnt, num_parent);
732 return has_interesting;
735 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
737 l0 = sline[l0].p_lno[n];
738 l1 = sline[l1].p_lno[n];
739 printf(" -%lu,%lu", l0, l1-l0-null_context);
742 static int hunk_comment_line(const char *bol)
744 int ch;
746 if (!bol)
747 return 0;
748 ch = *bol & 0xff;
749 return (isalpha(ch) || ch == '_' || ch == '$');
752 static void show_line_to_eol(const char *line, int len, const char *reset)
754 int saw_cr_at_eol = 0;
755 if (len < 0)
756 len = strlen(line);
757 saw_cr_at_eol = (len && line[len-1] == '\r');
759 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
760 reset,
761 saw_cr_at_eol ? "\r" : "");
764 static void dump_sline(struct sline *sline, const char *line_prefix,
765 unsigned long cnt, int num_parent,
766 int use_color, int result_deleted)
768 unsigned long mark = (1UL<<num_parent);
769 unsigned long no_pre_delete = (2UL<<num_parent);
770 int i;
771 unsigned long lno = 0;
772 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
773 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
774 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
775 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
776 const char *c_context = diff_get_color(use_color, DIFF_CONTEXT);
777 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
779 if (result_deleted)
780 return; /* result deleted */
782 while (1) {
783 unsigned long hunk_end;
784 unsigned long rlines;
785 const char *hunk_comment = NULL;
786 unsigned long null_context = 0;
788 while (lno <= cnt && !(sline[lno].flag & mark)) {
789 if (hunk_comment_line(sline[lno].bol))
790 hunk_comment = sline[lno].bol;
791 lno++;
793 if (cnt < lno)
794 break;
795 else {
796 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
797 if (!(sline[hunk_end].flag & mark))
798 break;
800 rlines = hunk_end - lno;
801 if (cnt < hunk_end)
802 rlines--; /* pointing at the last delete hunk */
804 if (!context) {
806 * Even when running with --unified=0, all
807 * lines in the hunk needs to be processed in
808 * the loop below in order to show the
809 * deletion recorded in lost_head. However,
810 * we do not want to show the resulting line
811 * with all blank context markers in such a
812 * case. Compensate.
814 unsigned long j;
815 for (j = lno; j < hunk_end; j++)
816 if (!(sline[j].flag & (mark-1)))
817 null_context++;
818 rlines -= null_context;
821 printf("%s%s", line_prefix, c_frag);
822 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
823 for (i = 0; i < num_parent; i++)
824 show_parent_lno(sline, lno, hunk_end, i, null_context);
825 printf(" +%lu,%lu ", lno+1, rlines);
826 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
828 if (hunk_comment) {
829 int comment_end = 0;
830 for (i = 0; i < 40; i++) {
831 int ch = hunk_comment[i] & 0xff;
832 if (!ch || ch == '\n')
833 break;
834 if (!isspace(ch))
835 comment_end = i;
837 if (comment_end)
838 printf("%s%s %s%s", c_reset,
839 c_context, c_reset,
840 c_func);
841 for (i = 0; i < comment_end; i++)
842 putchar(hunk_comment[i]);
845 printf("%s\n", c_reset);
846 while (lno < hunk_end) {
847 struct lline *ll;
848 int j;
849 unsigned long p_mask;
850 struct sline *sl = &sline[lno++];
851 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost;
852 while (ll) {
853 printf("%s%s", line_prefix, c_old);
854 for (j = 0; j < num_parent; j++) {
855 if (ll->parent_map & (1UL<<j))
856 putchar('-');
857 else
858 putchar(' ');
860 show_line_to_eol(ll->line, -1, c_reset);
861 ll = ll->next;
863 if (cnt < lno)
864 break;
865 p_mask = 1;
866 fputs(line_prefix, stdout);
867 if (!(sl->flag & (mark-1))) {
869 * This sline was here to hang the
870 * lost lines in front of it.
872 if (!context)
873 continue;
874 fputs(c_context, stdout);
876 else
877 fputs(c_new, stdout);
878 for (j = 0; j < num_parent; j++) {
879 if (p_mask & sl->flag)
880 putchar('+');
881 else
882 putchar(' ');
883 p_mask <<= 1;
885 show_line_to_eol(sl->bol, sl->len, c_reset);
890 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
891 int i, int j)
893 /* We have already examined parent j and we know parent i
894 * and parent j are the same, so reuse the combined result
895 * of parent j for parent i.
897 unsigned long lno, imask, jmask;
898 imask = (1UL<<i);
899 jmask = (1UL<<j);
901 for (lno = 0; lno <= cnt; lno++) {
902 struct lline *ll = sline->lost;
903 sline->p_lno[i] = sline->p_lno[j];
904 while (ll) {
905 if (ll->parent_map & jmask)
906 ll->parent_map |= imask;
907 ll = ll->next;
909 if (sline->flag & jmask)
910 sline->flag |= imask;
911 sline++;
913 /* the overall size of the file (sline[cnt]) */
914 sline->p_lno[i] = sline->p_lno[j];
917 static void dump_quoted_path(const char *head,
918 const char *prefix,
919 const char *path,
920 const char *line_prefix,
921 const char *c_meta, const char *c_reset)
923 static struct strbuf buf = STRBUF_INIT;
925 strbuf_reset(&buf);
926 strbuf_addstr(&buf, line_prefix);
927 strbuf_addstr(&buf, c_meta);
928 strbuf_addstr(&buf, head);
929 quote_two_c_style(&buf, prefix, path, 0);
930 strbuf_addstr(&buf, c_reset);
931 puts(buf.buf);
934 static void show_combined_header(struct combine_diff_path *elem,
935 int num_parent,
936 struct rev_info *rev,
937 const char *line_prefix,
938 int mode_differs,
939 int show_file_header)
941 struct diff_options *opt = &rev->diffopt;
942 int abbrev = opt->flags.full_index ? the_hash_algo->hexsz : DEFAULT_ABBREV;
943 const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
944 const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
945 const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
946 const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
947 const char *abb;
948 int added = 0;
949 int deleted = 0;
950 int i;
951 int dense = rev->dense_combined_merges;
953 if (rev->loginfo && !rev->no_commit_id)
954 show_log(rev);
956 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
957 "", elem->path, line_prefix, c_meta, c_reset);
958 printf("%s%sindex ", line_prefix, c_meta);
959 for (i = 0; i < num_parent; i++) {
960 abb = repo_find_unique_abbrev(the_repository,
961 &elem->parent[i].oid, abbrev);
962 printf("%s%s", i ? "," : "", abb);
964 abb = repo_find_unique_abbrev(the_repository, &elem->oid, abbrev);
965 printf("..%s%s\n", abb, c_reset);
967 if (mode_differs) {
968 deleted = !elem->mode;
970 /* We say it was added if nobody had it */
971 added = !deleted;
972 for (i = 0; added && i < num_parent; i++)
973 if (elem->parent[i].status !=
974 DIFF_STATUS_ADDED)
975 added = 0;
976 if (added)
977 printf("%s%snew file mode %06o",
978 line_prefix, c_meta, elem->mode);
979 else {
980 if (deleted)
981 printf("%s%sdeleted file ",
982 line_prefix, c_meta);
983 printf("mode ");
984 for (i = 0; i < num_parent; i++) {
985 printf("%s%06o", i ? "," : "",
986 elem->parent[i].mode);
988 if (elem->mode)
989 printf("..%06o", elem->mode);
991 printf("%s\n", c_reset);
994 if (!show_file_header)
995 return;
997 if (rev->combined_all_paths) {
998 for (i = 0; i < num_parent; i++) {
999 char *path = filename_changed(elem->parent[i].status)
1000 ? elem->parent[i].path.buf : elem->path;
1001 if (elem->parent[i].status == DIFF_STATUS_ADDED)
1002 dump_quoted_path("--- ", "", "/dev/null",
1003 line_prefix, c_meta, c_reset);
1004 else
1005 dump_quoted_path("--- ", a_prefix, path,
1006 line_prefix, c_meta, c_reset);
1008 } else {
1009 if (added)
1010 dump_quoted_path("--- ", "", "/dev/null",
1011 line_prefix, c_meta, c_reset);
1012 else
1013 dump_quoted_path("--- ", a_prefix, elem->path,
1014 line_prefix, c_meta, c_reset);
1016 if (deleted)
1017 dump_quoted_path("+++ ", "", "/dev/null",
1018 line_prefix, c_meta, c_reset);
1019 else
1020 dump_quoted_path("+++ ", b_prefix, elem->path,
1021 line_prefix, c_meta, c_reset);
1024 static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
1025 int working_tree_file,
1026 struct rev_info *rev)
1028 struct diff_options *opt = &rev->diffopt;
1029 unsigned long result_size, cnt, lno;
1030 int result_deleted = 0;
1031 char *result, *cp;
1032 struct sline *sline; /* survived lines */
1033 int mode_differs = 0;
1034 int i, show_hunks;
1035 mmfile_t result_file;
1036 struct userdiff_driver *userdiff;
1037 struct userdiff_driver *textconv = NULL;
1038 int is_binary;
1039 const char *line_prefix = diff_line_prefix(opt);
1041 context = opt->context;
1042 userdiff = userdiff_find_by_path(opt->repo->index, elem->path);
1043 if (!userdiff)
1044 userdiff = userdiff_find_by_name("default");
1045 if (opt->flags.allow_textconv)
1046 textconv = userdiff_get_textconv(opt->repo, userdiff);
1048 /* Read the result of merge first */
1049 if (!working_tree_file)
1050 result = grab_blob(opt->repo, &elem->oid, elem->mode, &result_size,
1051 textconv, elem->path);
1052 else {
1053 /* Used by diff-tree to read from the working tree */
1054 struct stat st;
1055 int fd = -1;
1057 if (lstat(elem->path, &st) < 0)
1058 goto deleted_file;
1060 if (S_ISLNK(st.st_mode)) {
1061 struct strbuf buf = STRBUF_INIT;
1063 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
1064 error_errno("readlink(%s)", elem->path);
1065 return;
1067 result_size = buf.len;
1068 result = strbuf_detach(&buf, NULL);
1069 elem->mode = canon_mode(st.st_mode);
1070 } else if (S_ISDIR(st.st_mode)) {
1071 struct object_id oid;
1072 if (repo_resolve_gitlink_ref(the_repository, elem->path,
1073 "HEAD", &oid) < 0)
1074 result = grab_blob(opt->repo, &elem->oid,
1075 elem->mode, &result_size,
1076 NULL, NULL);
1077 else
1078 result = grab_blob(opt->repo, &oid, elem->mode,
1079 &result_size, NULL, NULL);
1080 } else if (textconv) {
1081 struct diff_filespec *df = alloc_filespec(elem->path);
1082 fill_filespec(df, null_oid(), 0, st.st_mode);
1083 result_size = fill_textconv(opt->repo, textconv, df, &result);
1084 free_filespec(df);
1085 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
1086 size_t len = xsize_t(st.st_size);
1087 ssize_t done;
1088 int is_file, i;
1090 elem->mode = canon_mode(st.st_mode);
1091 /* if symlinks don't work, assume symlink if all parents
1092 * are symlinks
1094 is_file = has_symlinks;
1095 for (i = 0; !is_file && i < num_parent; i++)
1096 is_file = !S_ISLNK(elem->parent[i].mode);
1097 if (!is_file)
1098 elem->mode = canon_mode(S_IFLNK);
1100 result_size = len;
1101 result = xmallocz(len);
1103 done = read_in_full(fd, result, len);
1104 if (done < 0)
1105 die_errno("read error '%s'", elem->path);
1106 else if (done < len)
1107 die("early EOF '%s'", elem->path);
1109 /* If not a fake symlink, apply filters, e.g. autocrlf */
1110 if (is_file) {
1111 struct strbuf buf = STRBUF_INIT;
1113 if (convert_to_git(rev->diffopt.repo->index,
1114 elem->path, result, len, &buf, global_conv_flags_eol)) {
1115 free(result);
1116 result = strbuf_detach(&buf, &len);
1117 result_size = len;
1121 else {
1122 deleted_file:
1123 result_deleted = 1;
1124 result_size = 0;
1125 elem->mode = 0;
1126 result = xcalloc(1, 1);
1129 if (0 <= fd)
1130 close(fd);
1133 for (i = 0; i < num_parent; i++) {
1134 if (elem->parent[i].mode != elem->mode) {
1135 mode_differs = 1;
1136 break;
1140 if (textconv)
1141 is_binary = 0;
1142 else if (userdiff->binary != -1)
1143 is_binary = userdiff->binary;
1144 else {
1145 is_binary = buffer_is_binary(result, result_size);
1146 for (i = 0; !is_binary && i < num_parent; i++) {
1147 char *buf;
1148 unsigned long size;
1149 buf = grab_blob(opt->repo,
1150 &elem->parent[i].oid,
1151 elem->parent[i].mode,
1152 &size, NULL, NULL);
1153 if (buffer_is_binary(buf, size))
1154 is_binary = 1;
1155 free(buf);
1158 if (is_binary) {
1159 show_combined_header(elem, num_parent, rev,
1160 line_prefix, mode_differs, 0);
1161 printf("Binary files differ\n");
1162 free(result);
1163 return;
1166 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
1167 if (*cp == '\n')
1168 cnt++;
1170 if (result_size && result[result_size-1] != '\n')
1171 cnt++; /* incomplete line */
1173 CALLOC_ARRAY(sline, st_add(cnt, 2));
1174 sline[0].bol = result;
1175 for (lno = 0, cp = result; cp < result + result_size; cp++) {
1176 if (*cp == '\n') {
1177 sline[lno].len = cp - sline[lno].bol;
1178 lno++;
1179 if (lno < cnt)
1180 sline[lno].bol = cp + 1;
1183 if (result_size && result[result_size-1] != '\n')
1184 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
1186 result_file.ptr = result;
1187 result_file.size = result_size;
1190 * Even p_lno[cnt+1] is valid -- that is for the end line number
1191 * for deletion hunk at the end.
1193 CALLOC_ARRAY(sline[0].p_lno, st_mult(st_add(cnt, 2), num_parent));
1194 for (lno = 0; lno <= cnt; lno++)
1195 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
1197 for (i = 0; i < num_parent; i++) {
1198 int j;
1199 for (j = 0; j < i; j++) {
1200 if (oideq(&elem->parent[i].oid,
1201 &elem->parent[j].oid)) {
1202 reuse_combine_diff(sline, cnt, i, j);
1203 break;
1206 if (i <= j)
1207 combine_diff(opt->repo,
1208 &elem->parent[i].oid,
1209 elem->parent[i].mode,
1210 &result_file, sline,
1211 cnt, i, num_parent, result_deleted,
1212 textconv, elem->path, opt->xdl_opts);
1215 show_hunks = make_hunks(sline, cnt, num_parent, rev->dense_combined_merges);
1217 if (show_hunks || mode_differs || working_tree_file) {
1218 show_combined_header(elem, num_parent, rev,
1219 line_prefix, mode_differs, 1);
1220 dump_sline(sline, line_prefix, cnt, num_parent,
1221 opt->use_color, result_deleted);
1223 free(result);
1225 for (lno = 0; lno < cnt + 2; lno++) {
1226 if (sline[lno].lost) {
1227 struct lline *ll = sline[lno].lost;
1228 while (ll) {
1229 struct lline *tmp = ll;
1230 ll = ll->next;
1231 free(tmp);
1235 free(sline[0].p_lno);
1236 free(sline);
1239 static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
1241 struct diff_options *opt = &rev->diffopt;
1242 int line_termination, inter_name_termination, i;
1243 const char *line_prefix = diff_line_prefix(opt);
1245 line_termination = opt->line_termination;
1246 inter_name_termination = '\t';
1247 if (!line_termination)
1248 inter_name_termination = 0;
1250 if (rev->loginfo && !rev->no_commit_id)
1251 show_log(rev);
1254 if (opt->output_format & DIFF_FORMAT_RAW) {
1255 printf("%s", line_prefix);
1257 /* As many colons as there are parents */
1258 for (i = 0; i < num_parent; i++)
1259 putchar(':');
1261 /* Show the modes */
1262 for (i = 0; i < num_parent; i++)
1263 printf("%06o ", p->parent[i].mode);
1264 printf("%06o", p->mode);
1266 /* Show sha1's */
1267 for (i = 0; i < num_parent; i++)
1268 printf(" %s", diff_aligned_abbrev(&p->parent[i].oid,
1269 opt->abbrev));
1270 printf(" %s ", diff_aligned_abbrev(&p->oid, opt->abbrev));
1273 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
1274 for (i = 0; i < num_parent; i++)
1275 putchar(p->parent[i].status);
1276 putchar(inter_name_termination);
1279 for (i = 0; i < num_parent; i++)
1280 if (rev->combined_all_paths) {
1281 if (filename_changed(p->parent[i].status))
1282 write_name_quoted(p->parent[i].path.buf, stdout,
1283 inter_name_termination);
1284 else
1285 write_name_quoted(p->path, stdout,
1286 inter_name_termination);
1288 write_name_quoted(p->path, stdout, line_termination);
1292 * The result (p->elem) is from the working tree and their
1293 * parents are typically from multiple stages during a merge
1294 * (i.e. diff-files) or the state in HEAD and in the index
1295 * (i.e. diff-index).
1297 void show_combined_diff(struct combine_diff_path *p,
1298 int num_parent,
1299 struct rev_info *rev)
1301 struct diff_options *opt = &rev->diffopt;
1303 if (opt->output_format & (DIFF_FORMAT_RAW |
1304 DIFF_FORMAT_NAME |
1305 DIFF_FORMAT_NAME_STATUS))
1306 show_raw_diff(p, num_parent, rev);
1307 else if (opt->output_format & DIFF_FORMAT_PATCH)
1308 show_patch_diff(p, num_parent, 1, rev);
1311 static void free_combined_pair(struct diff_filepair *pair)
1313 free(pair->two);
1314 free(pair);
1318 * A combine_diff_path expresses N parents on the LHS against 1 merge
1319 * result. Synthesize a diff_filepair that has N entries on the "one"
1320 * side and 1 entry on the "two" side.
1322 * In the future, we might want to add more data to combine_diff_path
1323 * so that we can fill fields we are ignoring (most notably, size) here,
1324 * but currently nobody uses it, so this should suffice for now.
1326 static struct diff_filepair *combined_pair(struct combine_diff_path *p,
1327 int num_parent)
1329 int i;
1330 struct diff_filepair *pair;
1331 struct diff_filespec *pool;
1333 pair = xmalloc(sizeof(*pair));
1334 CALLOC_ARRAY(pool, st_add(num_parent, 1));
1335 pair->one = pool + 1;
1336 pair->two = pool;
1338 for (i = 0; i < num_parent; i++) {
1339 pair->one[i].path = p->path;
1340 pair->one[i].mode = p->parent[i].mode;
1341 oidcpy(&pair->one[i].oid, &p->parent[i].oid);
1342 pair->one[i].oid_valid = !is_null_oid(&p->parent[i].oid);
1343 pair->one[i].has_more_entries = 1;
1345 pair->one[num_parent - 1].has_more_entries = 0;
1347 pair->two->path = p->path;
1348 pair->two->mode = p->mode;
1349 oidcpy(&pair->two->oid, &p->oid);
1350 pair->two->oid_valid = !is_null_oid(&p->oid);
1351 return pair;
1354 static void handle_combined_callback(struct diff_options *opt,
1355 struct combine_diff_path *paths,
1356 int num_parent,
1357 int num_paths)
1359 struct combine_diff_path *p;
1360 struct diff_queue_struct q;
1361 int i;
1363 CALLOC_ARRAY(q.queue, num_paths);
1364 q.alloc = num_paths;
1365 q.nr = num_paths;
1366 for (i = 0, p = paths; p; p = p->next)
1367 q.queue[i++] = combined_pair(p, num_parent);
1368 opt->format_callback(&q, opt, opt->format_callback_data);
1369 for (i = 0; i < num_paths; i++)
1370 free_combined_pair(q.queue[i]);
1371 free(q.queue);
1374 static const char *path_path(void *obj)
1376 struct combine_diff_path *path = (struct combine_diff_path *)obj;
1378 return path->path;
1382 * Diff stat formats which we always compute solely against the first parent.
1384 #define STAT_FORMAT_MASK (DIFF_FORMAT_NUMSTAT \
1385 | DIFF_FORMAT_SHORTSTAT \
1386 | DIFF_FORMAT_SUMMARY \
1387 | DIFF_FORMAT_DIRSTAT \
1388 | DIFF_FORMAT_DIFFSTAT)
1390 /* find set of paths that every parent touches */
1391 static struct combine_diff_path *find_paths_generic(const struct object_id *oid,
1392 const struct oid_array *parents,
1393 struct diff_options *opt,
1394 int combined_all_paths)
1396 struct combine_diff_path *paths = NULL;
1397 int i, num_parent = parents->nr;
1398 int output_format = opt->output_format;
1399 char *orderfile = opt->orderfile;
1401 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1402 /* tell diff_tree to emit paths in sorted (=tree) order */
1403 opt->orderfile = NULL;
1405 /* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn) (wrt paths) */
1406 for (i = 0; i < num_parent; i++) {
1408 * show stat against the first parent even when doing
1409 * combined diff.
1411 int stat_opt = output_format & STAT_FORMAT_MASK;
1412 if (i == 0 && stat_opt)
1413 opt->output_format = stat_opt;
1414 else
1415 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1416 diff_tree_oid(&parents->oid[i], oid, "", opt);
1417 diffcore_std(opt);
1418 paths = intersect_paths(paths, i, num_parent,
1419 combined_all_paths);
1421 /* if showing diff, show it in requested order */
1422 if (opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1423 orderfile) {
1424 diffcore_order(orderfile);
1427 diff_flush(opt);
1430 opt->output_format = output_format;
1431 opt->orderfile = orderfile;
1432 return paths;
1437 * find set of paths that everybody touches, assuming diff is run without
1438 * rename/copy detection, etc, comparing all trees simultaneously (= faster).
1440 static struct combine_diff_path *find_paths_multitree(
1441 const struct object_id *oid, const struct oid_array *parents,
1442 struct diff_options *opt)
1444 int i, nparent = parents->nr;
1445 const struct object_id **parents_oid;
1446 struct combine_diff_path paths_head;
1447 struct strbuf base;
1449 ALLOC_ARRAY(parents_oid, nparent);
1450 for (i = 0; i < nparent; i++)
1451 parents_oid[i] = &parents->oid[i];
1453 /* fake list head, so worker can assume it is non-NULL */
1454 paths_head.next = NULL;
1456 strbuf_init(&base, PATH_MAX);
1457 diff_tree_paths(&paths_head, oid, parents_oid, nparent, &base, opt);
1459 strbuf_release(&base);
1460 free(parents_oid);
1461 return paths_head.next;
1464 static int match_objfind(struct combine_diff_path *path,
1465 int num_parent,
1466 const struct oidset *set)
1468 int i;
1469 if (oidset_contains(set, &path->oid))
1470 return 1;
1471 for (i = 0; i < num_parent; i++) {
1472 if (oidset_contains(set, &path->parent[i].oid))
1473 return 1;
1475 return 0;
1478 static struct combine_diff_path *combined_objfind(struct diff_options *opt,
1479 struct combine_diff_path *paths,
1480 int num_parent)
1482 struct combine_diff_path *ret = NULL, **tail = &ret;
1483 struct combine_diff_path *p = paths;
1485 while (p) {
1486 struct combine_diff_path *next = p->next;
1488 if (match_objfind(p, num_parent, opt->objfind)) {
1489 p->next = NULL;
1490 *tail = p;
1491 tail = &p->next;
1492 } else {
1493 free(p);
1495 p = next;
1498 return ret;
1501 void diff_tree_combined(const struct object_id *oid,
1502 const struct oid_array *parents,
1503 struct rev_info *rev)
1505 struct diff_options *opt = &rev->diffopt;
1506 struct diff_options diffopts;
1507 struct combine_diff_path *p, *paths;
1508 int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
1509 int need_generic_pathscan;
1511 if (opt->ignore_regex_nr)
1512 die("combined diff and '%s' cannot be used together",
1513 "--ignore-matching-lines");
1514 if (opt->close_file)
1515 die("combined diff and '%s' cannot be used together",
1516 "--output");
1518 /* nothing to do, if no parents */
1519 if (!num_parent)
1520 return;
1522 show_log_first = !!rev->loginfo && !rev->no_commit_id;
1523 needsep = 0;
1524 if (show_log_first) {
1525 show_log(rev);
1527 if (rev->verbose_header && opt->output_format &&
1528 opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1529 !commit_format_is_empty(rev->commit_format))
1530 printf("%s%c", diff_line_prefix(opt),
1531 opt->line_termination);
1534 diffopts = *opt;
1535 copy_pathspec(&diffopts.pathspec, &opt->pathspec);
1536 diffopts.flags.recursive = 1;
1537 diffopts.flags.allow_external = 0;
1539 /* find set of paths that everybody touches
1541 * NOTE
1543 * Diffcore transformations are bound to diff_filespec and logic
1544 * comparing two entries - i.e. they do not apply directly to combine
1545 * diff.
1547 * If some of such transformations is requested - we launch generic
1548 * path scanning, which works significantly slower compared to
1549 * simultaneous all-trees-in-one-go scan in find_paths_multitree().
1551 * TODO some of the filters could be ported to work on
1552 * combine_diff_paths - i.e. all functionality that skips paths, so in
1553 * theory, we could end up having only multitree path scanning.
1555 * NOTE please keep this semantically in sync with diffcore_std()
1557 need_generic_pathscan = opt->skip_stat_unmatch ||
1558 opt->flags.follow_renames ||
1559 opt->break_opt != -1 ||
1560 opt->detect_rename ||
1561 (opt->pickaxe_opts &
1562 (DIFF_PICKAXE_KINDS_MASK & ~DIFF_PICKAXE_KIND_OBJFIND)) ||
1563 opt->filter;
1565 if (need_generic_pathscan) {
1567 * NOTE generic case also handles --stat, as it computes
1568 * diff(sha1,parent_i) for all i to do the job, specifically
1569 * for parent0.
1571 paths = find_paths_generic(oid, parents, &diffopts,
1572 rev->combined_all_paths);
1574 else {
1575 int stat_opt;
1576 paths = find_paths_multitree(oid, parents, &diffopts);
1578 if (opt->pickaxe_opts & DIFF_PICKAXE_KIND_OBJFIND)
1579 paths = combined_objfind(opt, paths, num_parent);
1582 * show stat against the first parent even
1583 * when doing combined diff.
1585 stat_opt = opt->output_format & STAT_FORMAT_MASK;
1586 if (stat_opt) {
1587 diffopts.output_format = stat_opt;
1589 diff_tree_oid(&parents->oid[0], oid, "", &diffopts);
1590 diffcore_std(&diffopts);
1591 if (opt->orderfile)
1592 diffcore_order(opt->orderfile);
1593 diff_flush(&diffopts);
1597 /* find out number of surviving paths */
1598 for (num_paths = 0, p = paths; p; p = p->next)
1599 num_paths++;
1601 /* order paths according to diffcore_order */
1602 if (opt->orderfile && num_paths) {
1603 struct obj_order *o;
1605 ALLOC_ARRAY(o, num_paths);
1606 for (i = 0, p = paths; p; p = p->next, i++)
1607 o[i].obj = p;
1608 order_objects(opt->orderfile, path_path, o, num_paths);
1609 for (i = 0; i < num_paths - 1; i++) {
1610 p = o[i].obj;
1611 p->next = o[i+1].obj;
1614 p = o[num_paths-1].obj;
1615 p->next = NULL;
1616 paths = o[0].obj;
1617 free(o);
1621 if (num_paths) {
1622 if (opt->output_format & (DIFF_FORMAT_RAW |
1623 DIFF_FORMAT_NAME |
1624 DIFF_FORMAT_NAME_STATUS)) {
1625 for (p = paths; p; p = p->next)
1626 show_raw_diff(p, num_parent, rev);
1627 needsep = 1;
1629 else if (opt->output_format & STAT_FORMAT_MASK)
1630 needsep = 1;
1631 else if (opt->output_format & DIFF_FORMAT_CALLBACK)
1632 handle_combined_callback(opt, paths, num_parent, num_paths);
1634 if (opt->output_format & DIFF_FORMAT_PATCH) {
1635 if (needsep)
1636 printf("%s%c", diff_line_prefix(opt),
1637 opt->line_termination);
1638 for (p = paths; p; p = p->next)
1639 show_patch_diff(p, num_parent, 0, rev);
1643 /* Clean things up */
1644 while (paths) {
1645 struct combine_diff_path *tmp = paths;
1646 paths = paths->next;
1647 for (i = 0; i < num_parent; i++)
1648 if (rev->combined_all_paths &&
1649 filename_changed(tmp->parent[i].status))
1650 strbuf_release(&tmp->parent[i].path);
1651 free(tmp);
1654 clear_pathspec(&diffopts.pathspec);
1657 void diff_tree_combined_merge(const struct commit *commit,
1658 struct rev_info *rev)
1660 struct commit_list *parent = get_saved_parents(rev, commit);
1661 struct oid_array parents = OID_ARRAY_INIT;
1663 while (parent) {
1664 oid_array_append(&parents, &parent->item->object.oid);
1665 parent = parent->next;
1667 diff_tree_combined(&commit->object.oid, &parents, rev);
1668 oid_array_clear(&parents);