cocci: remove 'unused.cocci'
[git.git] / combine-diff.c
blob44ef6a1a812b02153f0d35137489930440357573
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 "environment.h"
8 #include "hex.h"
9 #include "quote.h"
10 #include "xdiff-interface.h"
11 #include "xdiff/xmacros.h"
12 #include "log-tree.h"
13 #include "refs.h"
14 #include "userdiff.h"
15 #include "oid-array.h"
16 #include "revision.h"
18 static int compare_paths(const struct combine_diff_path *one,
19 const struct diff_filespec *two)
21 if (!S_ISDIR(one->mode) && !S_ISDIR(two->mode))
22 return strcmp(one->path, two->path);
24 return base_name_compare(one->path, strlen(one->path), one->mode,
25 two->path, strlen(two->path), two->mode);
28 static int filename_changed(char status)
30 return status == 'R' || status == 'C';
33 static struct combine_diff_path *intersect_paths(
34 struct combine_diff_path *curr,
35 int n,
36 int num_parent,
37 int combined_all_paths)
39 struct diff_queue_struct *q = &diff_queued_diff;
40 struct combine_diff_path *p, **tail = &curr;
41 int i, j, cmp;
43 if (!n) {
44 for (i = 0; i < q->nr; i++) {
45 int len;
46 const char *path;
47 if (diff_unmodified_pair(q->queue[i]))
48 continue;
49 path = q->queue[i]->two->path;
50 len = strlen(path);
51 p = xmalloc(combine_diff_path_size(num_parent, len));
52 p->path = (char *) &(p->parent[num_parent]);
53 memcpy(p->path, path, len);
54 p->path[len] = 0;
55 p->next = NULL;
56 memset(p->parent, 0,
57 sizeof(p->parent[0]) * num_parent);
59 oidcpy(&p->oid, &q->queue[i]->two->oid);
60 p->mode = q->queue[i]->two->mode;
61 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
62 p->parent[n].mode = q->queue[i]->one->mode;
63 p->parent[n].status = q->queue[i]->status;
65 if (combined_all_paths &&
66 filename_changed(p->parent[n].status)) {
67 strbuf_init(&p->parent[n].path, 0);
68 strbuf_addstr(&p->parent[n].path,
69 q->queue[i]->one->path);
71 *tail = p;
72 tail = &p->next;
74 return curr;
78 * paths in curr (linked list) and q->queue[] (array) are
79 * both sorted in the tree order.
81 i = 0;
82 while ((p = *tail) != NULL) {
83 cmp = ((i >= q->nr)
84 ? -1 : compare_paths(p, q->queue[i]->two));
86 if (cmp < 0) {
87 /* p->path not in q->queue[]; drop it */
88 *tail = p->next;
89 for (j = 0; j < num_parent; j++)
90 if (combined_all_paths &&
91 filename_changed(p->parent[j].status))
92 strbuf_release(&p->parent[j].path);
93 free(p);
94 continue;
97 if (cmp > 0) {
98 /* q->queue[i] not in p->path; skip it */
99 i++;
100 continue;
103 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
104 p->parent[n].mode = q->queue[i]->one->mode;
105 p->parent[n].status = q->queue[i]->status;
106 if (combined_all_paths &&
107 filename_changed(p->parent[n].status))
108 strbuf_addstr(&p->parent[n].path,
109 q->queue[i]->one->path);
111 tail = &p->next;
112 i++;
114 return curr;
117 /* Lines lost from parent */
118 struct lline {
119 struct lline *next, *prev;
120 int len;
121 unsigned long parent_map;
122 char line[FLEX_ARRAY];
125 /* Lines lost from current parent (before coalescing) */
126 struct plost {
127 struct lline *lost_head, *lost_tail;
128 int len;
131 /* Lines surviving in the merge result */
132 struct sline {
133 /* Accumulated and coalesced lost lines */
134 struct lline *lost;
135 int lenlost;
136 struct plost plost;
137 char *bol;
138 int len;
139 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
140 * we did not change it).
141 * bit N is used for "interesting" lines, including context.
142 * bit (N+1) is used for "do not show deletion before this".
144 unsigned long flag;
145 unsigned long *p_lno;
148 static int match_string_spaces(const char *line1, int len1,
149 const char *line2, int len2,
150 long flags)
152 if (flags & XDF_WHITESPACE_FLAGS) {
153 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
154 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
157 if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
158 return (len1 == len2 && !memcmp(line1, line2, len1));
160 while (len1 > 0 && len2 > 0) {
161 len1--;
162 len2--;
163 if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
164 if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
165 (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
166 return 0;
168 for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
169 for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
171 if (line1[len1] != line2[len2])
172 return 0;
175 if (flags & XDF_IGNORE_WHITESPACE) {
176 /* Consume remaining spaces */
177 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
178 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
181 /* We matched full line1 and line2 */
182 if (!len1 && !len2)
183 return 1;
185 return 0;
188 enum coalesce_direction { MATCH, BASE, NEW };
190 /* Coalesce new lines into base by finding LCS */
191 static struct lline *coalesce_lines(struct lline *base, int *lenbase,
192 struct lline *newline, int lennew,
193 unsigned long parent, long flags)
195 int **lcs;
196 enum coalesce_direction **directions;
197 struct lline *baseend, *newend = NULL;
198 int i, j, origbaselen = *lenbase;
200 if (!newline)
201 return base;
203 if (!base) {
204 *lenbase = lennew;
205 return newline;
209 * Coalesce new lines into base by finding the LCS
210 * - Create the table to run dynamic programming
211 * - Compute the LCS
212 * - Then reverse read the direction structure:
213 * - If we have MATCH, assign parent to base flag, and consume
214 * both baseend and newend
215 * - Else if we have BASE, consume baseend
216 * - Else if we have NEW, insert newend lline into base and
217 * consume newend
219 CALLOC_ARRAY(lcs, st_add(origbaselen, 1));
220 CALLOC_ARRAY(directions, st_add(origbaselen, 1));
221 for (i = 0; i < origbaselen + 1; i++) {
222 CALLOC_ARRAY(lcs[i], st_add(lennew, 1));
223 CALLOC_ARRAY(directions[i], st_add(lennew, 1));
224 directions[i][0] = BASE;
226 for (j = 1; j < lennew + 1; j++)
227 directions[0][j] = NEW;
229 for (i = 1, baseend = base; i < origbaselen + 1; i++) {
230 for (j = 1, newend = newline; j < lennew + 1; j++) {
231 if (match_string_spaces(baseend->line, baseend->len,
232 newend->line, newend->len, flags)) {
233 lcs[i][j] = lcs[i - 1][j - 1] + 1;
234 directions[i][j] = MATCH;
235 } else if (lcs[i][j - 1] >= lcs[i - 1][j]) {
236 lcs[i][j] = lcs[i][j - 1];
237 directions[i][j] = NEW;
238 } else {
239 lcs[i][j] = lcs[i - 1][j];
240 directions[i][j] = BASE;
242 if (newend->next)
243 newend = newend->next;
245 if (baseend->next)
246 baseend = baseend->next;
249 for (i = 0; i < origbaselen + 1; i++)
250 free(lcs[i]);
251 free(lcs);
253 /* At this point, baseend and newend point to the end of each lists */
254 i--;
255 j--;
256 while (i != 0 || j != 0) {
257 if (directions[i][j] == MATCH) {
258 baseend->parent_map |= 1<<parent;
259 baseend = baseend->prev;
260 newend = newend->prev;
261 i--;
262 j--;
263 } else if (directions[i][j] == NEW) {
264 struct lline *lline;
266 lline = newend;
267 /* Remove lline from new list and update newend */
268 if (lline->prev)
269 lline->prev->next = lline->next;
270 else
271 newline = lline->next;
272 if (lline->next)
273 lline->next->prev = lline->prev;
275 newend = lline->prev;
276 j--;
278 /* Add lline to base list */
279 if (baseend) {
280 lline->next = baseend->next;
281 lline->prev = baseend;
282 if (lline->prev)
283 lline->prev->next = lline;
285 else {
286 lline->next = base;
287 base = lline;
289 (*lenbase)++;
291 if (lline->next)
292 lline->next->prev = lline;
294 } else {
295 baseend = baseend->prev;
296 i--;
300 newend = newline;
301 while (newend) {
302 struct lline *lline = newend;
303 newend = newend->next;
304 free(lline);
307 for (i = 0; i < origbaselen + 1; i++)
308 free(directions[i]);
309 free(directions);
311 return base;
314 static char *grab_blob(struct repository *r,
315 const struct object_id *oid, unsigned int mode,
316 unsigned long *size, struct userdiff_driver *textconv,
317 const char *path)
319 char *blob;
320 enum object_type type;
322 if (S_ISGITLINK(mode)) {
323 struct strbuf buf = STRBUF_INIT;
324 strbuf_addf(&buf, "Subproject commit %s\n", oid_to_hex(oid));
325 *size = buf.len;
326 blob = strbuf_detach(&buf, NULL);
327 } else if (is_null_oid(oid)) {
328 /* deleted blob */
329 *size = 0;
330 return xcalloc(1, 1);
331 } else if (textconv) {
332 struct diff_filespec *df = alloc_filespec(path);
333 fill_filespec(df, oid, 1, mode);
334 *size = fill_textconv(r, textconv, df, &blob);
335 free_filespec(df);
336 } else {
337 blob = repo_read_object_file(r, oid, &type, size);
338 if (type != OBJ_BLOB)
339 die("object '%s' is not a blob!", oid_to_hex(oid));
341 return blob;
344 static void append_lost(struct sline *sline, int n, const char *line, int len)
346 struct lline *lline;
347 unsigned long this_mask = (1UL<<n);
348 if (line[len-1] == '\n')
349 len--;
351 FLEX_ALLOC_MEM(lline, line, line, len);
352 lline->len = len;
353 lline->next = NULL;
354 lline->prev = sline->plost.lost_tail;
355 if (lline->prev)
356 lline->prev->next = lline;
357 else
358 sline->plost.lost_head = lline;
359 sline->plost.lost_tail = lline;
360 sline->plost.len++;
361 lline->parent_map = this_mask;
364 struct combine_diff_state {
365 unsigned int lno;
366 int ob, on, nb, nn;
367 unsigned long nmask;
368 int num_parent;
369 int n;
370 struct sline *sline;
371 struct sline *lost_bucket;
374 static void consume_hunk(void *state_,
375 long ob, long on,
376 long nb, long nn,
377 const char *func UNUSED, long funclen UNUSED)
379 struct combine_diff_state *state = state_;
381 state->ob = ob;
382 state->on = on;
383 state->nb = nb;
384 state->nn = nn;
385 state->lno = state->nb;
386 if (state->nn == 0) {
387 /* @@ -X,Y +N,0 @@ removed Y lines
388 * that would have come *after* line N
389 * in the result. Our lost buckets hang
390 * to the line after the removed lines,
392 * Note that this is correct even when N == 0,
393 * in which case the hunk removes the first
394 * line in the file.
396 state->lost_bucket = &state->sline[state->nb];
397 if (!state->nb)
398 state->nb = 1;
399 } else {
400 state->lost_bucket = &state->sline[state->nb-1];
402 if (!state->sline[state->nb-1].p_lno)
403 CALLOC_ARRAY(state->sline[state->nb - 1].p_lno,
404 state->num_parent);
405 state->sline[state->nb-1].p_lno[state->n] = state->ob;
408 static int consume_line(void *state_, char *line, unsigned long len)
410 struct combine_diff_state *state = state_;
411 if (!state->lost_bucket)
412 return 0; /* not in any hunk yet */
413 switch (line[0]) {
414 case '-':
415 append_lost(state->lost_bucket, state->n, line+1, len-1);
416 break;
417 case '+':
418 state->sline[state->lno-1].flag |= state->nmask;
419 state->lno++;
420 break;
422 return 0;
425 static void combine_diff(struct repository *r,
426 const struct object_id *parent, unsigned int mode,
427 mmfile_t *result_file,
428 struct sline *sline, unsigned int cnt, int n,
429 int num_parent, int result_deleted,
430 struct userdiff_driver *textconv,
431 const char *path, long flags)
433 unsigned int p_lno, lno;
434 unsigned long nmask = (1UL << n);
435 xpparam_t xpp;
436 xdemitconf_t xecfg;
437 mmfile_t parent_file;
438 struct combine_diff_state state;
439 unsigned long sz;
441 if (result_deleted)
442 return; /* result deleted */
444 parent_file.ptr = grab_blob(r, parent, mode, &sz, textconv, path);
445 parent_file.size = sz;
446 memset(&xpp, 0, sizeof(xpp));
447 xpp.flags = flags;
448 memset(&xecfg, 0, sizeof(xecfg));
449 memset(&state, 0, sizeof(state));
450 state.nmask = nmask;
451 state.sline = sline;
452 state.lno = 1;
453 state.num_parent = num_parent;
454 state.n = n;
456 if (xdi_diff_outf(&parent_file, result_file, consume_hunk,
457 consume_line, &state, &xpp, &xecfg))
458 die("unable to generate combined diff for %s",
459 oid_to_hex(parent));
460 free(parent_file.ptr);
462 /* Assign line numbers for this parent.
464 * sline[lno].p_lno[n] records the first line number
465 * (counting from 1) for parent N if the final hunk display
466 * started by showing sline[lno] (possibly showing the lost
467 * lines attached to it first).
469 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
470 struct lline *ll;
471 sline[lno].p_lno[n] = p_lno;
473 /* Coalesce new lines */
474 if (sline[lno].plost.lost_head) {
475 struct sline *sl = &sline[lno];
476 sl->lost = coalesce_lines(sl->lost, &sl->lenlost,
477 sl->plost.lost_head,
478 sl->plost.len, n, flags);
479 sl->plost.lost_head = sl->plost.lost_tail = NULL;
480 sl->plost.len = 0;
483 /* How many lines would this sline advance the p_lno? */
484 ll = sline[lno].lost;
485 while (ll) {
486 if (ll->parent_map & nmask)
487 p_lno++; /* '-' means parent had it */
488 ll = ll->next;
490 if (lno < cnt && !(sline[lno].flag & nmask))
491 p_lno++; /* no '+' means parent had it */
493 sline[lno].p_lno[n] = p_lno; /* trailer */
496 static unsigned long context = 3;
497 static char combine_marker = '@';
499 static int interesting(struct sline *sline, unsigned long all_mask)
501 /* If some parents lost lines here, or if we have added to
502 * some parent, it is interesting.
504 return ((sline->flag & all_mask) || sline->lost);
507 static unsigned long adjust_hunk_tail(struct sline *sline,
508 unsigned long all_mask,
509 unsigned long hunk_begin,
510 unsigned long i)
512 /* i points at the first uninteresting line. If the last line
513 * of the hunk was interesting only because it has some
514 * deletion, then it is not all that interesting for the
515 * purpose of giving trailing context lines. This is because
516 * we output '-' line and then unmodified sline[i-1] itself in
517 * that case which gives us one extra context line.
519 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
520 i--;
521 return i;
524 static unsigned long find_next(struct sline *sline,
525 unsigned long mark,
526 unsigned long i,
527 unsigned long cnt,
528 int look_for_uninteresting)
530 /* We have examined up to i-1 and are about to look at i.
531 * Find next interesting or uninteresting line. Here,
532 * "interesting" does not mean interesting(), but marked by
533 * the give_context() function below (i.e. it includes context
534 * lines that are not interesting to interesting() function
535 * that are surrounded by interesting() ones.
537 while (i <= cnt)
538 if (look_for_uninteresting
539 ? !(sline[i].flag & mark)
540 : (sline[i].flag & mark))
541 return i;
542 else
543 i++;
544 return i;
547 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
549 unsigned long all_mask = (1UL<<num_parent) - 1;
550 unsigned long mark = (1UL<<num_parent);
551 unsigned long no_pre_delete = (2UL<<num_parent);
552 unsigned long i;
554 /* Two groups of interesting lines may have a short gap of
555 * uninteresting lines. Connect such groups to give them a
556 * bit of context.
558 * We first start from what the interesting() function says,
559 * and mark them with "mark", and paint context lines with the
560 * mark. So interesting() would still say false for such context
561 * lines but they are treated as "interesting" in the end.
563 i = find_next(sline, mark, 0, cnt, 0);
564 if (cnt < i)
565 return 0;
567 while (i <= cnt) {
568 unsigned long j = (context < i) ? (i - context) : 0;
569 unsigned long k;
571 /* Paint a few lines before the first interesting line. */
572 while (j < i) {
573 if (!(sline[j].flag & mark))
574 sline[j].flag |= no_pre_delete;
575 sline[j++].flag |= mark;
578 again:
579 /* we know up to i is to be included. where does the
580 * next uninteresting one start?
582 j = find_next(sline, mark, i, cnt, 1);
583 if (cnt < j)
584 break; /* the rest are all interesting */
586 /* lookahead context lines */
587 k = find_next(sline, mark, j, cnt, 0);
588 j = adjust_hunk_tail(sline, all_mask, i, j);
590 if (k < j + context) {
591 /* k is interesting and [j,k) are not, but
592 * paint them interesting because the gap is small.
594 while (j < k)
595 sline[j++].flag |= mark;
596 i = k;
597 goto again;
600 /* j is the first uninteresting line and there is
601 * no overlap beyond it within context lines. Paint
602 * the trailing edge a bit.
604 i = k;
605 k = (j + context < cnt+1) ? j + context : cnt+1;
606 while (j < k)
607 sline[j++].flag |= mark;
609 return 1;
612 static int make_hunks(struct sline *sline, unsigned long cnt,
613 int num_parent, int dense)
615 unsigned long all_mask = (1UL<<num_parent) - 1;
616 unsigned long mark = (1UL<<num_parent);
617 unsigned long i;
618 int has_interesting = 0;
620 for (i = 0; i <= cnt; i++) {
621 if (interesting(&sline[i], all_mask))
622 sline[i].flag |= mark;
623 else
624 sline[i].flag &= ~mark;
626 if (!dense)
627 return give_context(sline, cnt, num_parent);
629 /* Look at each hunk, and if we have changes from only one
630 * parent, or the changes are the same from all but one
631 * parent, mark that uninteresting.
633 i = 0;
634 while (i <= cnt) {
635 unsigned long j, hunk_begin, hunk_end;
636 unsigned long same_diff;
637 while (i <= cnt && !(sline[i].flag & mark))
638 i++;
639 if (cnt < i)
640 break; /* No more interesting hunks */
641 hunk_begin = i;
642 for (j = i + 1; j <= cnt; j++) {
643 if (!(sline[j].flag & mark)) {
644 /* Look beyond the end to see if there
645 * is an interesting line after this
646 * hunk within context span.
648 unsigned long la; /* lookahead */
649 int contin = 0;
650 la = adjust_hunk_tail(sline, all_mask,
651 hunk_begin, j);
652 la = (la + context < cnt + 1) ?
653 (la + context) : cnt + 1;
654 while (la && j <= --la) {
655 if (sline[la].flag & mark) {
656 contin = 1;
657 break;
660 if (!contin)
661 break;
662 j = la;
665 hunk_end = j;
667 /* [i..hunk_end) are interesting. Now is it really
668 * interesting? We check if there are only two versions
669 * and the result matches one of them. That is, we look
670 * at:
671 * (+) line, which records lines added to which parents;
672 * this line appears in the result.
673 * (-) line, which records from what parents the line
674 * was removed; this line does not appear in the result.
675 * then check the set of parents the result has difference
676 * from, from all lines. If there are lines that has
677 * different set of parents that the result has differences
678 * from, that means we have more than two versions.
680 * Even when we have only two versions, if the result does
681 * not match any of the parents, the it should be considered
682 * interesting. In such a case, we would have all '+' line.
683 * After passing the above "two versions" test, that would
684 * appear as "the same set of parents" to be "all parents".
686 same_diff = 0;
687 has_interesting = 0;
688 for (j = i; j < hunk_end && !has_interesting; j++) {
689 unsigned long this_diff = sline[j].flag & all_mask;
690 struct lline *ll = sline[j].lost;
691 if (this_diff) {
692 /* This has some changes. Is it the
693 * same as others?
695 if (!same_diff)
696 same_diff = this_diff;
697 else if (same_diff != this_diff) {
698 has_interesting = 1;
699 break;
702 while (ll && !has_interesting) {
703 /* Lost this line from these parents;
704 * who are they? Are they the same?
706 this_diff = ll->parent_map;
707 if (!same_diff)
708 same_diff = this_diff;
709 else if (same_diff != this_diff) {
710 has_interesting = 1;
712 ll = ll->next;
716 if (!has_interesting && same_diff != all_mask) {
717 /* This hunk is not that interesting after all */
718 for (j = hunk_begin; j < hunk_end; j++)
719 sline[j].flag &= ~mark;
721 i = hunk_end;
724 has_interesting = give_context(sline, cnt, num_parent);
725 return has_interesting;
728 static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
730 l0 = sline[l0].p_lno[n];
731 l1 = sline[l1].p_lno[n];
732 printf(" -%lu,%lu", l0, l1-l0-null_context);
735 static int hunk_comment_line(const char *bol)
737 int ch;
739 if (!bol)
740 return 0;
741 ch = *bol & 0xff;
742 return (isalpha(ch) || ch == '_' || ch == '$');
745 static void show_line_to_eol(const char *line, int len, const char *reset)
747 int saw_cr_at_eol = 0;
748 if (len < 0)
749 len = strlen(line);
750 saw_cr_at_eol = (len && line[len-1] == '\r');
752 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
753 reset,
754 saw_cr_at_eol ? "\r" : "");
757 static void dump_sline(struct sline *sline, const char *line_prefix,
758 unsigned long cnt, int num_parent,
759 int use_color, int result_deleted)
761 unsigned long mark = (1UL<<num_parent);
762 unsigned long no_pre_delete = (2UL<<num_parent);
763 int i;
764 unsigned long lno = 0;
765 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
766 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
767 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
768 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
769 const char *c_context = diff_get_color(use_color, DIFF_CONTEXT);
770 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
772 if (result_deleted)
773 return; /* result deleted */
775 while (1) {
776 unsigned long hunk_end;
777 unsigned long rlines;
778 const char *hunk_comment = NULL;
779 unsigned long null_context = 0;
781 while (lno <= cnt && !(sline[lno].flag & mark)) {
782 if (hunk_comment_line(sline[lno].bol))
783 hunk_comment = sline[lno].bol;
784 lno++;
786 if (cnt < lno)
787 break;
788 else {
789 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
790 if (!(sline[hunk_end].flag & mark))
791 break;
793 rlines = hunk_end - lno;
794 if (cnt < hunk_end)
795 rlines--; /* pointing at the last delete hunk */
797 if (!context) {
799 * Even when running with --unified=0, all
800 * lines in the hunk needs to be processed in
801 * the loop below in order to show the
802 * deletion recorded in lost_head. However,
803 * we do not want to show the resulting line
804 * with all blank context markers in such a
805 * case. Compensate.
807 unsigned long j;
808 for (j = lno; j < hunk_end; j++)
809 if (!(sline[j].flag & (mark-1)))
810 null_context++;
811 rlines -= null_context;
814 printf("%s%s", line_prefix, c_frag);
815 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
816 for (i = 0; i < num_parent; i++)
817 show_parent_lno(sline, lno, hunk_end, i, null_context);
818 printf(" +%lu,%lu ", lno+1, rlines);
819 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
821 if (hunk_comment) {
822 int comment_end = 0;
823 for (i = 0; i < 40; i++) {
824 int ch = hunk_comment[i] & 0xff;
825 if (!ch || ch == '\n')
826 break;
827 if (!isspace(ch))
828 comment_end = i;
830 if (comment_end)
831 printf("%s%s %s%s", c_reset,
832 c_context, c_reset,
833 c_func);
834 for (i = 0; i < comment_end; i++)
835 putchar(hunk_comment[i]);
838 printf("%s\n", c_reset);
839 while (lno < hunk_end) {
840 struct lline *ll;
841 int j;
842 unsigned long p_mask;
843 struct sline *sl = &sline[lno++];
844 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost;
845 while (ll) {
846 printf("%s%s", line_prefix, c_old);
847 for (j = 0; j < num_parent; j++) {
848 if (ll->parent_map & (1UL<<j))
849 putchar('-');
850 else
851 putchar(' ');
853 show_line_to_eol(ll->line, -1, c_reset);
854 ll = ll->next;
856 if (cnt < lno)
857 break;
858 p_mask = 1;
859 fputs(line_prefix, stdout);
860 if (!(sl->flag & (mark-1))) {
862 * This sline was here to hang the
863 * lost lines in front of it.
865 if (!context)
866 continue;
867 fputs(c_context, stdout);
869 else
870 fputs(c_new, stdout);
871 for (j = 0; j < num_parent; j++) {
872 if (p_mask & sl->flag)
873 putchar('+');
874 else
875 putchar(' ');
876 p_mask <<= 1;
878 show_line_to_eol(sl->bol, sl->len, c_reset);
883 static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
884 int i, int j)
886 /* We have already examined parent j and we know parent i
887 * and parent j are the same, so reuse the combined result
888 * of parent j for parent i.
890 unsigned long lno, imask, jmask;
891 imask = (1UL<<i);
892 jmask = (1UL<<j);
894 for (lno = 0; lno <= cnt; lno++) {
895 struct lline *ll = sline->lost;
896 sline->p_lno[i] = sline->p_lno[j];
897 while (ll) {
898 if (ll->parent_map & jmask)
899 ll->parent_map |= imask;
900 ll = ll->next;
902 if (sline->flag & jmask)
903 sline->flag |= imask;
904 sline++;
906 /* the overall size of the file (sline[cnt]) */
907 sline->p_lno[i] = sline->p_lno[j];
910 static void dump_quoted_path(const char *head,
911 const char *prefix,
912 const char *path,
913 const char *line_prefix,
914 const char *c_meta, const char *c_reset)
916 static struct strbuf buf = STRBUF_INIT;
918 strbuf_reset(&buf);
919 strbuf_addstr(&buf, line_prefix);
920 strbuf_addstr(&buf, c_meta);
921 strbuf_addstr(&buf, head);
922 quote_two_c_style(&buf, prefix, path, 0);
923 strbuf_addstr(&buf, c_reset);
924 puts(buf.buf);
927 static void show_combined_header(struct combine_diff_path *elem,
928 int num_parent,
929 struct rev_info *rev,
930 const char *line_prefix,
931 int mode_differs,
932 int show_file_header)
934 struct diff_options *opt = &rev->diffopt;
935 int abbrev = opt->flags.full_index ? the_hash_algo->hexsz : DEFAULT_ABBREV;
936 const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
937 const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
938 const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
939 const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
940 const char *abb;
941 int added = 0;
942 int deleted = 0;
943 int i;
944 int dense = rev->dense_combined_merges;
946 if (rev->loginfo && !rev->no_commit_id)
947 show_log(rev);
949 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
950 "", elem->path, line_prefix, c_meta, c_reset);
951 printf("%s%sindex ", line_prefix, c_meta);
952 for (i = 0; i < num_parent; i++) {
953 abb = repo_find_unique_abbrev(the_repository,
954 &elem->parent[i].oid, abbrev);
955 printf("%s%s", i ? "," : "", abb);
957 abb = repo_find_unique_abbrev(the_repository, &elem->oid, abbrev);
958 printf("..%s%s\n", abb, c_reset);
960 if (mode_differs) {
961 deleted = !elem->mode;
963 /* We say it was added if nobody had it */
964 added = !deleted;
965 for (i = 0; added && i < num_parent; i++)
966 if (elem->parent[i].status !=
967 DIFF_STATUS_ADDED)
968 added = 0;
969 if (added)
970 printf("%s%snew file mode %06o",
971 line_prefix, c_meta, elem->mode);
972 else {
973 if (deleted)
974 printf("%s%sdeleted file ",
975 line_prefix, c_meta);
976 printf("mode ");
977 for (i = 0; i < num_parent; i++) {
978 printf("%s%06o", i ? "," : "",
979 elem->parent[i].mode);
981 if (elem->mode)
982 printf("..%06o", elem->mode);
984 printf("%s\n", c_reset);
987 if (!show_file_header)
988 return;
990 if (rev->combined_all_paths) {
991 for (i = 0; i < num_parent; i++) {
992 char *path = filename_changed(elem->parent[i].status)
993 ? elem->parent[i].path.buf : elem->path;
994 if (elem->parent[i].status == DIFF_STATUS_ADDED)
995 dump_quoted_path("--- ", "", "/dev/null",
996 line_prefix, c_meta, c_reset);
997 else
998 dump_quoted_path("--- ", a_prefix, path,
999 line_prefix, c_meta, c_reset);
1001 } else {
1002 if (added)
1003 dump_quoted_path("--- ", "", "/dev/null",
1004 line_prefix, c_meta, c_reset);
1005 else
1006 dump_quoted_path("--- ", a_prefix, elem->path,
1007 line_prefix, c_meta, c_reset);
1009 if (deleted)
1010 dump_quoted_path("+++ ", "", "/dev/null",
1011 line_prefix, c_meta, c_reset);
1012 else
1013 dump_quoted_path("+++ ", b_prefix, elem->path,
1014 line_prefix, c_meta, c_reset);
1017 static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
1018 int working_tree_file,
1019 struct rev_info *rev)
1021 struct diff_options *opt = &rev->diffopt;
1022 unsigned long result_size, cnt, lno;
1023 int result_deleted = 0;
1024 char *result, *cp;
1025 struct sline *sline; /* survived lines */
1026 int mode_differs = 0;
1027 int i, show_hunks;
1028 mmfile_t result_file;
1029 struct userdiff_driver *userdiff;
1030 struct userdiff_driver *textconv = NULL;
1031 int is_binary;
1032 const char *line_prefix = diff_line_prefix(opt);
1034 context = opt->context;
1035 userdiff = userdiff_find_by_path(opt->repo->index, elem->path);
1036 if (!userdiff)
1037 userdiff = userdiff_find_by_name("default");
1038 if (opt->flags.allow_textconv)
1039 textconv = userdiff_get_textconv(opt->repo, userdiff);
1041 /* Read the result of merge first */
1042 if (!working_tree_file)
1043 result = grab_blob(opt->repo, &elem->oid, elem->mode, &result_size,
1044 textconv, elem->path);
1045 else {
1046 /* Used by diff-tree to read from the working tree */
1047 struct stat st;
1048 int fd = -1;
1050 if (lstat(elem->path, &st) < 0)
1051 goto deleted_file;
1053 if (S_ISLNK(st.st_mode)) {
1054 struct strbuf buf = STRBUF_INIT;
1056 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
1057 error_errno("readlink(%s)", elem->path);
1058 return;
1060 result_size = buf.len;
1061 result = strbuf_detach(&buf, NULL);
1062 elem->mode = canon_mode(st.st_mode);
1063 } else if (S_ISDIR(st.st_mode)) {
1064 struct object_id oid;
1065 if (resolve_gitlink_ref(elem->path, "HEAD", &oid) < 0)
1066 result = grab_blob(opt->repo, &elem->oid,
1067 elem->mode, &result_size,
1068 NULL, NULL);
1069 else
1070 result = grab_blob(opt->repo, &oid, elem->mode,
1071 &result_size, NULL, NULL);
1072 } else if (textconv) {
1073 struct diff_filespec *df = alloc_filespec(elem->path);
1074 fill_filespec(df, null_oid(), 0, st.st_mode);
1075 result_size = fill_textconv(opt->repo, textconv, df, &result);
1076 free_filespec(df);
1077 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
1078 size_t len = xsize_t(st.st_size);
1079 ssize_t done;
1080 int is_file, i;
1082 elem->mode = canon_mode(st.st_mode);
1083 /* if symlinks don't work, assume symlink if all parents
1084 * are symlinks
1086 is_file = has_symlinks;
1087 for (i = 0; !is_file && i < num_parent; i++)
1088 is_file = !S_ISLNK(elem->parent[i].mode);
1089 if (!is_file)
1090 elem->mode = canon_mode(S_IFLNK);
1092 result_size = len;
1093 result = xmallocz(len);
1095 done = read_in_full(fd, result, len);
1096 if (done < 0)
1097 die_errno("read error '%s'", elem->path);
1098 else if (done < len)
1099 die("early EOF '%s'", elem->path);
1101 /* If not a fake symlink, apply filters, e.g. autocrlf */
1102 if (is_file) {
1103 struct strbuf buf = STRBUF_INIT;
1105 if (convert_to_git(rev->diffopt.repo->index,
1106 elem->path, result, len, &buf, global_conv_flags_eol)) {
1107 free(result);
1108 result = strbuf_detach(&buf, &len);
1109 result_size = len;
1113 else {
1114 deleted_file:
1115 result_deleted = 1;
1116 result_size = 0;
1117 elem->mode = 0;
1118 result = xcalloc(1, 1);
1121 if (0 <= fd)
1122 close(fd);
1125 for (i = 0; i < num_parent; i++) {
1126 if (elem->parent[i].mode != elem->mode) {
1127 mode_differs = 1;
1128 break;
1132 if (textconv)
1133 is_binary = 0;
1134 else if (userdiff->binary != -1)
1135 is_binary = userdiff->binary;
1136 else {
1137 is_binary = buffer_is_binary(result, result_size);
1138 for (i = 0; !is_binary && i < num_parent; i++) {
1139 char *buf;
1140 unsigned long size;
1141 buf = grab_blob(opt->repo,
1142 &elem->parent[i].oid,
1143 elem->parent[i].mode,
1144 &size, NULL, NULL);
1145 if (buffer_is_binary(buf, size))
1146 is_binary = 1;
1147 free(buf);
1150 if (is_binary) {
1151 show_combined_header(elem, num_parent, rev,
1152 line_prefix, mode_differs, 0);
1153 printf("Binary files differ\n");
1154 free(result);
1155 return;
1158 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
1159 if (*cp == '\n')
1160 cnt++;
1162 if (result_size && result[result_size-1] != '\n')
1163 cnt++; /* incomplete line */
1165 CALLOC_ARRAY(sline, st_add(cnt, 2));
1166 sline[0].bol = result;
1167 for (lno = 0, cp = result; cp < result + result_size; cp++) {
1168 if (*cp == '\n') {
1169 sline[lno].len = cp - sline[lno].bol;
1170 lno++;
1171 if (lno < cnt)
1172 sline[lno].bol = cp + 1;
1175 if (result_size && result[result_size-1] != '\n')
1176 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
1178 result_file.ptr = result;
1179 result_file.size = result_size;
1181 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1182 * for deletion hunk at the end.
1184 CALLOC_ARRAY(sline[0].p_lno, st_mult(st_add(cnt, 2), num_parent));
1185 for (lno = 0; lno <= cnt; lno++)
1186 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
1188 for (i = 0; i < num_parent; i++) {
1189 int j;
1190 for (j = 0; j < i; j++) {
1191 if (oideq(&elem->parent[i].oid,
1192 &elem->parent[j].oid)) {
1193 reuse_combine_diff(sline, cnt, i, j);
1194 break;
1197 if (i <= j)
1198 combine_diff(opt->repo,
1199 &elem->parent[i].oid,
1200 elem->parent[i].mode,
1201 &result_file, sline,
1202 cnt, i, num_parent, result_deleted,
1203 textconv, elem->path, opt->xdl_opts);
1206 show_hunks = make_hunks(sline, cnt, num_parent, rev->dense_combined_merges);
1208 if (show_hunks || mode_differs || working_tree_file) {
1209 show_combined_header(elem, num_parent, rev,
1210 line_prefix, mode_differs, 1);
1211 dump_sline(sline, line_prefix, cnt, num_parent,
1212 opt->use_color, result_deleted);
1214 free(result);
1216 for (lno = 0; lno < cnt; lno++) {
1217 if (sline[lno].lost) {
1218 struct lline *ll = sline[lno].lost;
1219 while (ll) {
1220 struct lline *tmp = ll;
1221 ll = ll->next;
1222 free(tmp);
1226 free(sline[0].p_lno);
1227 free(sline);
1230 static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
1232 struct diff_options *opt = &rev->diffopt;
1233 int line_termination, inter_name_termination, i;
1234 const char *line_prefix = diff_line_prefix(opt);
1236 line_termination = opt->line_termination;
1237 inter_name_termination = '\t';
1238 if (!line_termination)
1239 inter_name_termination = 0;
1241 if (rev->loginfo && !rev->no_commit_id)
1242 show_log(rev);
1245 if (opt->output_format & DIFF_FORMAT_RAW) {
1246 printf("%s", line_prefix);
1248 /* As many colons as there are parents */
1249 for (i = 0; i < num_parent; i++)
1250 putchar(':');
1252 /* Show the modes */
1253 for (i = 0; i < num_parent; i++)
1254 printf("%06o ", p->parent[i].mode);
1255 printf("%06o", p->mode);
1257 /* Show sha1's */
1258 for (i = 0; i < num_parent; i++)
1259 printf(" %s", diff_aligned_abbrev(&p->parent[i].oid,
1260 opt->abbrev));
1261 printf(" %s ", diff_aligned_abbrev(&p->oid, opt->abbrev));
1264 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
1265 for (i = 0; i < num_parent; i++)
1266 putchar(p->parent[i].status);
1267 putchar(inter_name_termination);
1270 for (i = 0; i < num_parent; i++)
1271 if (rev->combined_all_paths) {
1272 if (filename_changed(p->parent[i].status))
1273 write_name_quoted(p->parent[i].path.buf, stdout,
1274 inter_name_termination);
1275 else
1276 write_name_quoted(p->path, stdout,
1277 inter_name_termination);
1279 write_name_quoted(p->path, stdout, line_termination);
1283 * The result (p->elem) is from the working tree and their
1284 * parents are typically from multiple stages during a merge
1285 * (i.e. diff-files) or the state in HEAD and in the index
1286 * (i.e. diff-index).
1288 void show_combined_diff(struct combine_diff_path *p,
1289 int num_parent,
1290 struct rev_info *rev)
1292 struct diff_options *opt = &rev->diffopt;
1294 if (opt->output_format & (DIFF_FORMAT_RAW |
1295 DIFF_FORMAT_NAME |
1296 DIFF_FORMAT_NAME_STATUS))
1297 show_raw_diff(p, num_parent, rev);
1298 else if (opt->output_format & DIFF_FORMAT_PATCH)
1299 show_patch_diff(p, num_parent, 1, rev);
1302 static void free_combined_pair(struct diff_filepair *pair)
1304 free(pair->two);
1305 free(pair);
1309 * A combine_diff_path expresses N parents on the LHS against 1 merge
1310 * result. Synthesize a diff_filepair that has N entries on the "one"
1311 * side and 1 entry on the "two" side.
1313 * In the future, we might want to add more data to combine_diff_path
1314 * so that we can fill fields we are ignoring (most notably, size) here,
1315 * but currently nobody uses it, so this should suffice for now.
1317 static struct diff_filepair *combined_pair(struct combine_diff_path *p,
1318 int num_parent)
1320 int i;
1321 struct diff_filepair *pair;
1322 struct diff_filespec *pool;
1324 pair = xmalloc(sizeof(*pair));
1325 CALLOC_ARRAY(pool, st_add(num_parent, 1));
1326 pair->one = pool + 1;
1327 pair->two = pool;
1329 for (i = 0; i < num_parent; i++) {
1330 pair->one[i].path = p->path;
1331 pair->one[i].mode = p->parent[i].mode;
1332 oidcpy(&pair->one[i].oid, &p->parent[i].oid);
1333 pair->one[i].oid_valid = !is_null_oid(&p->parent[i].oid);
1334 pair->one[i].has_more_entries = 1;
1336 pair->one[num_parent - 1].has_more_entries = 0;
1338 pair->two->path = p->path;
1339 pair->two->mode = p->mode;
1340 oidcpy(&pair->two->oid, &p->oid);
1341 pair->two->oid_valid = !is_null_oid(&p->oid);
1342 return pair;
1345 static void handle_combined_callback(struct diff_options *opt,
1346 struct combine_diff_path *paths,
1347 int num_parent,
1348 int num_paths)
1350 struct combine_diff_path *p;
1351 struct diff_queue_struct q;
1352 int i;
1354 CALLOC_ARRAY(q.queue, num_paths);
1355 q.alloc = num_paths;
1356 q.nr = num_paths;
1357 for (i = 0, p = paths; p; p = p->next)
1358 q.queue[i++] = combined_pair(p, num_parent);
1359 opt->format_callback(&q, opt, opt->format_callback_data);
1360 for (i = 0; i < num_paths; i++)
1361 free_combined_pair(q.queue[i]);
1362 free(q.queue);
1365 static const char *path_path(void *obj)
1367 struct combine_diff_path *path = (struct combine_diff_path *)obj;
1369 return path->path;
1373 * Diff stat formats which we always compute solely against the first parent.
1375 #define STAT_FORMAT_MASK (DIFF_FORMAT_NUMSTAT \
1376 | DIFF_FORMAT_SHORTSTAT \
1377 | DIFF_FORMAT_SUMMARY \
1378 | DIFF_FORMAT_DIRSTAT \
1379 | DIFF_FORMAT_DIFFSTAT)
1381 /* find set of paths that every parent touches */
1382 static struct combine_diff_path *find_paths_generic(const struct object_id *oid,
1383 const struct oid_array *parents,
1384 struct diff_options *opt,
1385 int combined_all_paths)
1387 struct combine_diff_path *paths = NULL;
1388 int i, num_parent = parents->nr;
1390 int output_format = opt->output_format;
1391 const char *orderfile = opt->orderfile;
1393 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1394 /* tell diff_tree to emit paths in sorted (=tree) order */
1395 opt->orderfile = NULL;
1397 /* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn) (wrt paths) */
1398 for (i = 0; i < num_parent; i++) {
1400 * show stat against the first parent even when doing
1401 * combined diff.
1403 int stat_opt = output_format & STAT_FORMAT_MASK;
1404 if (i == 0 && stat_opt)
1405 opt->output_format = stat_opt;
1406 else
1407 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1408 diff_tree_oid(&parents->oid[i], oid, "", opt);
1409 diffcore_std(opt);
1410 paths = intersect_paths(paths, i, num_parent,
1411 combined_all_paths);
1413 /* if showing diff, show it in requested order */
1414 if (opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1415 orderfile) {
1416 diffcore_order(orderfile);
1419 diff_flush(opt);
1422 opt->output_format = output_format;
1423 opt->orderfile = orderfile;
1424 return paths;
1429 * find set of paths that everybody touches, assuming diff is run without
1430 * rename/copy detection, etc, comparing all trees simultaneously (= faster).
1432 static struct combine_diff_path *find_paths_multitree(
1433 const struct object_id *oid, const struct oid_array *parents,
1434 struct diff_options *opt)
1436 int i, nparent = parents->nr;
1437 const struct object_id **parents_oid;
1438 struct combine_diff_path paths_head;
1439 struct strbuf base;
1441 ALLOC_ARRAY(parents_oid, nparent);
1442 for (i = 0; i < nparent; i++)
1443 parents_oid[i] = &parents->oid[i];
1445 /* fake list head, so worker can assume it is non-NULL */
1446 paths_head.next = NULL;
1448 strbuf_init(&base, PATH_MAX);
1449 diff_tree_paths(&paths_head, oid, parents_oid, nparent, &base, opt);
1451 strbuf_release(&base);
1452 free(parents_oid);
1453 return paths_head.next;
1456 static int match_objfind(struct combine_diff_path *path,
1457 int num_parent,
1458 const struct oidset *set)
1460 int i;
1461 if (oidset_contains(set, &path->oid))
1462 return 1;
1463 for (i = 0; i < num_parent; i++) {
1464 if (oidset_contains(set, &path->parent[i].oid))
1465 return 1;
1467 return 0;
1470 static struct combine_diff_path *combined_objfind(struct diff_options *opt,
1471 struct combine_diff_path *paths,
1472 int num_parent)
1474 struct combine_diff_path *ret = NULL, **tail = &ret;
1475 struct combine_diff_path *p = paths;
1477 while (p) {
1478 struct combine_diff_path *next = p->next;
1480 if (match_objfind(p, num_parent, opt->objfind)) {
1481 p->next = NULL;
1482 *tail = p;
1483 tail = &p->next;
1484 } else {
1485 free(p);
1487 p = next;
1490 return ret;
1493 void diff_tree_combined(const struct object_id *oid,
1494 const struct oid_array *parents,
1495 struct rev_info *rev)
1497 struct diff_options *opt = &rev->diffopt;
1498 struct diff_options diffopts;
1499 struct combine_diff_path *p, *paths;
1500 int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
1501 int need_generic_pathscan;
1503 if (opt->ignore_regex_nr)
1504 die("combined diff and '%s' cannot be used together",
1505 "--ignore-matching-lines");
1506 if (opt->close_file)
1507 die("combined diff and '%s' cannot be used together",
1508 "--output");
1510 /* nothing to do, if no parents */
1511 if (!num_parent)
1512 return;
1514 show_log_first = !!rev->loginfo && !rev->no_commit_id;
1515 needsep = 0;
1516 if (show_log_first) {
1517 show_log(rev);
1519 if (rev->verbose_header && opt->output_format &&
1520 opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1521 !commit_format_is_empty(rev->commit_format))
1522 printf("%s%c", diff_line_prefix(opt),
1523 opt->line_termination);
1526 diffopts = *opt;
1527 copy_pathspec(&diffopts.pathspec, &opt->pathspec);
1528 diffopts.flags.recursive = 1;
1529 diffopts.flags.allow_external = 0;
1531 /* find set of paths that everybody touches
1533 * NOTE
1535 * Diffcore transformations are bound to diff_filespec and logic
1536 * comparing two entries - i.e. they do not apply directly to combine
1537 * diff.
1539 * If some of such transformations is requested - we launch generic
1540 * path scanning, which works significantly slower compared to
1541 * simultaneous all-trees-in-one-go scan in find_paths_multitree().
1543 * TODO some of the filters could be ported to work on
1544 * combine_diff_paths - i.e. all functionality that skips paths, so in
1545 * theory, we could end up having only multitree path scanning.
1547 * NOTE please keep this semantically in sync with diffcore_std()
1549 need_generic_pathscan = opt->skip_stat_unmatch ||
1550 opt->flags.follow_renames ||
1551 opt->break_opt != -1 ||
1552 opt->detect_rename ||
1553 (opt->pickaxe_opts &
1554 (DIFF_PICKAXE_KINDS_MASK & ~DIFF_PICKAXE_KIND_OBJFIND)) ||
1555 opt->filter;
1557 if (need_generic_pathscan) {
1559 * NOTE generic case also handles --stat, as it computes
1560 * diff(sha1,parent_i) for all i to do the job, specifically
1561 * for parent0.
1563 paths = find_paths_generic(oid, parents, &diffopts,
1564 rev->combined_all_paths);
1566 else {
1567 int stat_opt;
1568 paths = find_paths_multitree(oid, parents, &diffopts);
1570 if (opt->pickaxe_opts & DIFF_PICKAXE_KIND_OBJFIND)
1571 paths = combined_objfind(opt, paths, num_parent);
1574 * show stat against the first parent even
1575 * when doing combined diff.
1577 stat_opt = opt->output_format & STAT_FORMAT_MASK;
1578 if (stat_opt) {
1579 diffopts.output_format = stat_opt;
1581 diff_tree_oid(&parents->oid[0], oid, "", &diffopts);
1582 diffcore_std(&diffopts);
1583 if (opt->orderfile)
1584 diffcore_order(opt->orderfile);
1585 diff_flush(&diffopts);
1589 /* find out number of surviving paths */
1590 for (num_paths = 0, p = paths; p; p = p->next)
1591 num_paths++;
1593 /* order paths according to diffcore_order */
1594 if (opt->orderfile && num_paths) {
1595 struct obj_order *o;
1597 ALLOC_ARRAY(o, num_paths);
1598 for (i = 0, p = paths; p; p = p->next, i++)
1599 o[i].obj = p;
1600 order_objects(opt->orderfile, path_path, o, num_paths);
1601 for (i = 0; i < num_paths - 1; i++) {
1602 p = o[i].obj;
1603 p->next = o[i+1].obj;
1606 p = o[num_paths-1].obj;
1607 p->next = NULL;
1608 paths = o[0].obj;
1609 free(o);
1613 if (num_paths) {
1614 if (opt->output_format & (DIFF_FORMAT_RAW |
1615 DIFF_FORMAT_NAME |
1616 DIFF_FORMAT_NAME_STATUS)) {
1617 for (p = paths; p; p = p->next)
1618 show_raw_diff(p, num_parent, rev);
1619 needsep = 1;
1621 else if (opt->output_format & STAT_FORMAT_MASK)
1622 needsep = 1;
1623 else if (opt->output_format & DIFF_FORMAT_CALLBACK)
1624 handle_combined_callback(opt, paths, num_parent, num_paths);
1626 if (opt->output_format & DIFF_FORMAT_PATCH) {
1627 if (needsep)
1628 printf("%s%c", diff_line_prefix(opt),
1629 opt->line_termination);
1630 for (p = paths; p; p = p->next)
1631 show_patch_diff(p, num_parent, 0, rev);
1635 /* Clean things up */
1636 while (paths) {
1637 struct combine_diff_path *tmp = paths;
1638 paths = paths->next;
1639 for (i = 0; i < num_parent; i++)
1640 if (rev->combined_all_paths &&
1641 filename_changed(tmp->parent[i].status))
1642 strbuf_release(&tmp->parent[i].path);
1643 free(tmp);
1646 clear_pathspec(&diffopts.pathspec);
1649 void diff_tree_combined_merge(const struct commit *commit,
1650 struct rev_info *rev)
1652 struct commit_list *parent = get_saved_parents(rev, commit);
1653 struct oid_array parents = OID_ARRAY_INIT;
1655 while (parent) {
1656 oid_array_append(&parents, &parent->item->object.oid);
1657 parent = parent->next;
1659 diff_tree_combined(&commit->object.oid, &parents, rev);
1660 oid_array_clear(&parents);