]> git.ipfire.org Git - thirdparty/git.git/blame - combine-diff.c
Merge branch 'es/add-doc-list-short-form-of-all-in-synopsis' into maint-2.43
[thirdparty/git.git] / combine-diff.c
CommitLineData
5e3f94df 1#include "git-compat-util.h"
a034e910 2#include "object-store-ll.h"
af3feefa 3#include "commit.h"
73359a9b 4#include "convert.h"
8e440259 5#include "blob.h"
af3feefa
JH
6#include "diff.h"
7#include "diffcore.h"
32a8f510 8#include "environment.h"
41771fa4 9#include "hex.h"
dabab1d6 10#include "object-name.h"
af3feefa 11#include "quote.h"
d9ea73e0 12#include "xdiff-interface.h"
fa04ae0b 13#include "xdiff/xmacros.h"
91539833 14#include "log-tree.h"
7dae8b21 15#include "refs.h"
d4a4f929 16#include "tree.h"
4d5f3471 17#include "userdiff.h"
fe299ec5 18#include "oid-array.h"
53d00b39 19#include "revision.h"
af3feefa 20
e09867f0
JK
21static int compare_paths(const struct combine_diff_path *one,
22 const struct diff_filespec *two)
23{
24 if (!S_ISDIR(one->mode) && !S_ISDIR(two->mode))
25 return strcmp(one->path, two->path);
26
27 return base_name_compare(one->path, strlen(one->path), one->mode,
28 two->path, strlen(two->path), two->mode);
29}
30
d76ce4f7
EN
31static int filename_changed(char status)
32{
33 return status == 'R' || status == 'C';
34}
35
36static struct combine_diff_path *intersect_paths(
37 struct combine_diff_path *curr,
38 int n,
39 int num_parent,
40 int combined_all_paths)
af3feefa
JH
41{
42 struct diff_queue_struct *q = &diff_queued_diff;
7b1004b0 43 struct combine_diff_path *p, **tail = &curr;
d76ce4f7 44 int i, j, cmp;
af3feefa
JH
45
46 if (!n) {
af3feefa
JH
47 for (i = 0; i < q->nr; i++) {
48 int len;
49 const char *path;
a976b0a5 50 if (diff_unmodified_pair(q->queue[i]))
af3feefa
JH
51 continue;
52 path = q->queue[i]->two->path;
53 len = strlen(path);
2454c962 54 p = xmalloc(combine_diff_path_size(num_parent, len));
4b25d091 55 p->path = (char *) &(p->parent[num_parent]);
af3feefa
JH
56 memcpy(p->path, path, len);
57 p->path[len] = 0;
af3feefa 58 p->next = NULL;
2454c962
JH
59 memset(p->parent, 0,
60 sizeof(p->parent[0]) * num_parent);
61
a0d12c44 62 oidcpy(&p->oid, &q->queue[i]->two->oid);
2454c962 63 p->mode = q->queue[i]->two->mode;
a0d12c44 64 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
2454c962 65 p->parent[n].mode = q->queue[i]->one->mode;
d416df88 66 p->parent[n].status = q->queue[i]->status;
d76ce4f7
EN
67
68 if (combined_all_paths &&
69 filename_changed(p->parent[n].status)) {
70 strbuf_init(&p->parent[n].path, 0);
71 strbuf_addstr(&p->parent[n].path,
72 q->queue[i]->one->path);
73 }
5290a0f8
JH
74 *tail = p;
75 tail = &p->next;
af3feefa 76 }
7b1004b0 77 return curr;
af3feefa
JH
78 }
79
8518ff8f 80 /*
7b1004b0
JH
81 * paths in curr (linked list) and q->queue[] (array) are
82 * both sorted in the tree order.
8518ff8f 83 */
8518ff8f 84 i = 0;
7b1004b0
JH
85 while ((p = *tail) != NULL) {
86 cmp = ((i >= q->nr)
e09867f0 87 ? -1 : compare_paths(p, q->queue[i]->two));
8518ff8f 88
8518ff8f 89 if (cmp < 0) {
7b1004b0
JH
90 /* p->path not in q->queue[]; drop it */
91 *tail = p->next;
d76ce4f7
EN
92 for (j = 0; j < num_parent; j++)
93 if (combined_all_paths &&
94 filename_changed(p->parent[j].status))
95 strbuf_release(&p->parent[j].path);
7b1004b0 96 free(p);
af3feefa 97 continue;
8518ff8f 98 }
af3feefa 99
8518ff8f 100 if (cmp > 0) {
7b1004b0 101 /* q->queue[i] not in p->path; skip it */
8518ff8f
KS
102 i++;
103 continue;
af3feefa 104 }
8518ff8f 105
a0d12c44 106 oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid);
8518ff8f
KS
107 p->parent[n].mode = q->queue[i]->one->mode;
108 p->parent[n].status = q->queue[i]->status;
d76ce4f7
EN
109 if (combined_all_paths &&
110 filename_changed(p->parent[n].status))
111 strbuf_addstr(&p->parent[n].path,
112 q->queue[i]->one->path);
8518ff8f 113
7b1004b0 114 tail = &p->next;
8518ff8f 115 i++;
af3feefa
JH
116 }
117 return curr;
118}
119
b469d8b6 120/* Lines lost from parent */
af3feefa 121struct lline {
99d32060 122 struct lline *next, *prev;
af3feefa
JH
123 int len;
124 unsigned long parent_map;
125 char line[FLEX_ARRAY];
126};
127
99d32060
AP
128/* Lines lost from current parent (before coalescing) */
129struct plost {
130 struct lline *lost_head, *lost_tail;
131 int len;
132};
133
b469d8b6 134/* Lines surviving in the merge result */
af3feefa 135struct sline {
99d32060
AP
136 /* Accumulated and coalesced lost lines */
137 struct lline *lost;
138 int lenlost;
139 struct plost plost;
af3feefa
JH
140 char *bol;
141 int len;
46dc9412
JH
142 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
143 * we did not change it).
b469d8b6 144 * bit N is used for "interesting" lines, including context.
c86fbe53 145 * bit (N+1) is used for "do not show deletion before this".
b469d8b6 146 */
af3feefa 147 unsigned long flag;
f16706cc 148 unsigned long *p_lno;
af3feefa
JH
149};
150
fa04ae0b
AP
151static int match_string_spaces(const char *line1, int len1,
152 const char *line2, int len2,
153 long flags)
154{
155 if (flags & XDF_WHITESPACE_FLAGS) {
156 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
157 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
158 }
159
160 if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
161 return (len1 == len2 && !memcmp(line1, line2, len1));
162
163 while (len1 > 0 && len2 > 0) {
164 len1--;
165 len2--;
166 if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
167 if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
168 (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
169 return 0;
170
171 for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
172 for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
173 }
174 if (line1[len1] != line2[len2])
175 return 0;
176 }
177
178 if (flags & XDF_IGNORE_WHITESPACE) {
179 /* Consume remaining spaces */
180 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
181 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
182 }
183
184 /* We matched full line1 and line2 */
185 if (!len1 && !len2)
186 return 1;
187
188 return 0;
189}
190
99d32060
AP
191enum coalesce_direction { MATCH, BASE, NEW };
192
193/* Coalesce new lines into base by finding LCS */
194static struct lline *coalesce_lines(struct lline *base, int *lenbase,
06fffa00 195 struct lline *newline, int lennew,
99d32060 196 unsigned long parent, long flags)
af3feefa 197{
99d32060
AP
198 int **lcs;
199 enum coalesce_direction **directions;
200 struct lline *baseend, *newend = NULL;
201 int i, j, origbaselen = *lenbase;
af3feefa 202
afe8a907 203 if (!newline)
99d32060
AP
204 return base;
205
afe8a907 206 if (!base) {
99d32060 207 *lenbase = lennew;
06fffa00 208 return newline;
99d32060
AP
209 }
210
211 /*
212 * Coalesce new lines into base by finding the LCS
98e023de 213 * - Create the table to run dynamic programming
99d32060
AP
214 * - Compute the LCS
215 * - Then reverse read the direction structure:
216 * - If we have MATCH, assign parent to base flag, and consume
217 * both baseend and newend
218 * - Else if we have BASE, consume baseend
219 * - Else if we have NEW, insert newend lline into base and
220 * consume newend
221 */
ca56dadb
RS
222 CALLOC_ARRAY(lcs, st_add(origbaselen, 1));
223 CALLOC_ARRAY(directions, st_add(origbaselen, 1));
99d32060 224 for (i = 0; i < origbaselen + 1; i++) {
ca56dadb
RS
225 CALLOC_ARRAY(lcs[i], st_add(lennew, 1));
226 CALLOC_ARRAY(directions[i], st_add(lennew, 1));
99d32060
AP
227 directions[i][0] = BASE;
228 }
229 for (j = 1; j < lennew + 1; j++)
230 directions[0][j] = NEW;
231
232 for (i = 1, baseend = base; i < origbaselen + 1; i++) {
06fffa00 233 for (j = 1, newend = newline; j < lennew + 1; j++) {
99d32060
AP
234 if (match_string_spaces(baseend->line, baseend->len,
235 newend->line, newend->len, flags)) {
236 lcs[i][j] = lcs[i - 1][j - 1] + 1;
237 directions[i][j] = MATCH;
238 } else if (lcs[i][j - 1] >= lcs[i - 1][j]) {
239 lcs[i][j] = lcs[i][j - 1];
240 directions[i][j] = NEW;
241 } else {
242 lcs[i][j] = lcs[i - 1][j];
243 directions[i][j] = BASE;
244 }
245 if (newend->next)
246 newend = newend->next;
247 }
248 if (baseend->next)
249 baseend = baseend->next;
250 }
251
252 for (i = 0; i < origbaselen + 1; i++)
253 free(lcs[i]);
254 free(lcs);
255
256 /* At this point, baseend and newend point to the end of each lists */
257 i--;
258 j--;
259 while (i != 0 || j != 0) {
260 if (directions[i][j] == MATCH) {
261 baseend->parent_map |= 1<<parent;
262 baseend = baseend->prev;
263 newend = newend->prev;
264 i--;
265 j--;
266 } else if (directions[i][j] == NEW) {
267 struct lline *lline;
268
269 lline = newend;
270 /* Remove lline from new list and update newend */
271 if (lline->prev)
272 lline->prev->next = lline->next;
273 else
06fffa00 274 newline = lline->next;
99d32060
AP
275 if (lline->next)
276 lline->next->prev = lline->prev;
277
278 newend = lline->prev;
279 j--;
280
281 /* Add lline to base list */
282 if (baseend) {
283 lline->next = baseend->next;
284 lline->prev = baseend;
285 if (lline->prev)
286 lline->prev->next = lline;
af3feefa 287 }
99d32060
AP
288 else {
289 lline->next = base;
290 base = lline;
291 }
292 (*lenbase)++;
293
294 if (lline->next)
295 lline->next->prev = lline;
296
297 } else {
298 baseend = baseend->prev;
299 i--;
af3feefa
JH
300 }
301 }
302
06fffa00 303 newend = newline;
99d32060
AP
304 while (newend) {
305 struct lline *lline = newend;
306 newend = newend->next;
307 free(lline);
308 }
309
310 for (i = 0; i < origbaselen + 1; i++)
311 free(directions[i]);
312 free(directions);
313
314 return base;
315}
316
6afaf807
NTND
317static char *grab_blob(struct repository *r,
318 const struct object_id *oid, unsigned int mode,
99d32060
AP
319 unsigned long *size, struct userdiff_driver *textconv,
320 const char *path)
321{
322 char *blob;
323 enum object_type type;
324
325 if (S_ISGITLINK(mode)) {
0dc3b035
JK
326 struct strbuf buf = STRBUF_INIT;
327 strbuf_addf(&buf, "Subproject commit %s\n", oid_to_hex(oid));
328 *size = buf.len;
329 blob = strbuf_detach(&buf, NULL);
1ff57c13 330 } else if (is_null_oid(oid)) {
99d32060
AP
331 /* deleted blob */
332 *size = 0;
333 return xcalloc(1, 1);
334 } else if (textconv) {
335 struct diff_filespec *df = alloc_filespec(path);
f9704c2d 336 fill_filespec(df, oid, 1, mode);
6afaf807 337 *size = fill_textconv(r, textconv, df, &blob);
99d32060
AP
338 free_filespec(df);
339 } else {
4a93b899 340 blob = repo_read_object_file(r, oid, &type, size);
99d32060 341 if (type != OBJ_BLOB)
1ff57c13 342 die("object '%s' is not a blob!", oid_to_hex(oid));
99d32060
AP
343 }
344 return blob;
345}
346
347static void append_lost(struct sline *sline, int n, const char *line, int len)
348{
349 struct lline *lline;
350 unsigned long this_mask = (1UL<<n);
351 if (line[len-1] == '\n')
352 len--;
353
96ffc06f 354 FLEX_ALLOC_MEM(lline, line, line, len);
af3feefa
JH
355 lline->len = len;
356 lline->next = NULL;
99d32060
AP
357 lline->prev = sline->plost.lost_tail;
358 if (lline->prev)
359 lline->prev->next = lline;
360 else
361 sline->plost.lost_head = lline;
362 sline->plost.lost_tail = lline;
363 sline->plost.len++;
af3feefa 364 lline->parent_map = this_mask;
af3feefa
JH
365}
366
f23fc773 367struct combine_diff_state {
a0fd3146
JH
368 unsigned int lno;
369 int ob, on, nb, nn;
f23fc773
JH
370 unsigned long nmask;
371 int num_parent;
372 int n;
373 struct sline *sline;
374 struct sline *lost_bucket;
375};
376
0074c911
JK
377static void consume_hunk(void *state_,
378 long ob, long on,
379 long nb, long nn,
61bdc7c5 380 const char *func UNUSED, long funclen UNUSED)
f23fc773 381{
d9ea73e0 382 struct combine_diff_state *state = state_;
0074c911
JK
383
384 state->ob = ob;
385 state->on = on;
386 state->nb = nb;
387 state->nn = nn;
388 state->lno = state->nb;
389 if (state->nn == 0) {
390 /* @@ -X,Y +N,0 @@ removed Y lines
391 * that would have come *after* line N
392 * in the result. Our lost buckets hang
393 * to the line after the removed lines,
394 *
395 * Note that this is correct even when N == 0,
396 * in which case the hunk removes the first
397 * line in the file.
398 */
399 state->lost_bucket = &state->sline[state->nb];
400 if (!state->nb)
401 state->nb = 1;
402 } else {
403 state->lost_bucket = &state->sline[state->nb-1];
f23fc773 404 }
0074c911 405 if (!state->sline[state->nb-1].p_lno)
ca56dadb
RS
406 CALLOC_ARRAY(state->sline[state->nb - 1].p_lno,
407 state->num_parent);
0074c911
JK
408 state->sline[state->nb-1].p_lno[state->n] = state->ob;
409}
410
a8d5eb6d 411static int consume_line(void *state_, char *line, unsigned long len)
0074c911
JK
412{
413 struct combine_diff_state *state = state_;
f23fc773 414 if (!state->lost_bucket)
a8d5eb6d 415 return 0; /* not in any hunk yet */
f23fc773
JH
416 switch (line[0]) {
417 case '-':
99d32060 418 append_lost(state->lost_bucket, state->n, line+1, len-1);
f23fc773
JH
419 break;
420 case '+':
421 state->sline[state->lno-1].flag |= state->nmask;
422 state->lno++;
423 break;
424 }
a8d5eb6d 425 return 0;
f23fc773
JH
426}
427
6afaf807
NTND
428static void combine_diff(struct repository *r,
429 const struct object_id *parent, unsigned int mode,
7dae8b21 430 mmfile_t *result_file,
2386c297 431 struct sline *sline, unsigned int cnt, int n,
0508fe53
JK
432 int num_parent, int result_deleted,
433 struct userdiff_driver *textconv,
fa04ae0b 434 const char *path, long flags)
af3feefa 435{
f23fc773 436 unsigned int p_lno, lno;
f16706cc 437 unsigned long nmask = (1UL << n);
f23fc773
JH
438 xpparam_t xpp;
439 xdemitconf_t xecfg;
440 mmfile_t parent_file;
f23fc773
JH
441 struct combine_diff_state state;
442 unsigned long sz;
af3feefa 443
21798708 444 if (result_deleted)
4462731e
JH
445 return; /* result deleted */
446
6afaf807 447 parent_file.ptr = grab_blob(r, parent, mode, &sz, textconv, path);
f23fc773 448 parent_file.size = sz;
9ccd0a88 449 memset(&xpp, 0, sizeof(xpp));
fa04ae0b 450 xpp.flags = flags;
30b25010 451 memset(&xecfg, 0, sizeof(xecfg));
f23fc773
JH
452 memset(&state, 0, sizeof(state));
453 state.nmask = nmask;
454 state.sline = sline;
455 state.lno = 1;
456 state.num_parent = num_parent;
457 state.n = n;
458
0074c911
JK
459 if (xdi_diff_outf(&parent_file, result_file, consume_hunk,
460 consume_line, &state, &xpp, &xecfg))
3efb9880 461 die("unable to generate combined diff for %s",
11a458be 462 oid_to_hex(parent));
f23fc773 463 free(parent_file.ptr);
f16706cc
JH
464
465 /* Assign line numbers for this parent.
466 *
467 * sline[lno].p_lno[n] records the first line number
468 * (counting from 1) for parent N if the final hunk display
469 * started by showing sline[lno] (possibly showing the lost
470 * lines attached to it first).
471 */
8a470ebf 472 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
f16706cc
JH
473 struct lline *ll;
474 sline[lno].p_lno[n] = p_lno;
475
99d32060
AP
476 /* Coalesce new lines */
477 if (sline[lno].plost.lost_head) {
478 struct sline *sl = &sline[lno];
479 sl->lost = coalesce_lines(sl->lost, &sl->lenlost,
480 sl->plost.lost_head,
481 sl->plost.len, n, flags);
482 sl->plost.lost_head = sl->plost.lost_tail = NULL;
483 sl->plost.len = 0;
484 }
485
f16706cc 486 /* How many lines would this sline advance the p_lno? */
99d32060 487 ll = sline[lno].lost;
f16706cc
JH
488 while (ll) {
489 if (ll->parent_map & nmask)
490 p_lno++; /* '-' means parent had it */
491 ll = ll->next;
492 }
8a470ebf 493 if (lno < cnt && !(sline[lno].flag & nmask))
f16706cc
JH
494 p_lno++; /* no '+' means parent had it */
495 }
496 sline[lno].p_lno[n] = p_lno; /* trailer */
af3feefa
JH
497}
498
499static unsigned long context = 3;
500static char combine_marker = '@';
501
502static int interesting(struct sline *sline, unsigned long all_mask)
503{
46dc9412
JH
504 /* If some parents lost lines here, or if we have added to
505 * some parent, it is interesting.
506 */
99d32060 507 return ((sline->flag & all_mask) || sline->lost);
af3feefa
JH
508}
509
3ec1909f
JH
510static unsigned long adjust_hunk_tail(struct sline *sline,
511 unsigned long all_mask,
512 unsigned long hunk_begin,
513 unsigned long i)
514{
46dc9412
JH
515 /* i points at the first uninteresting line. If the last line
516 * of the hunk was interesting only because it has some
517 * deletion, then it is not all that interesting for the
518 * purpose of giving trailing context lines. This is because
519 * we output '-' line and then unmodified sline[i-1] itself in
520 * that case which gives us one extra context line.
3ec1909f 521 */
46dc9412 522 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
3ec1909f
JH
523 i--;
524 return i;
525}
526
46dc9412
JH
527static unsigned long find_next(struct sline *sline,
528 unsigned long mark,
529 unsigned long i,
530 unsigned long cnt,
2386c297 531 int look_for_uninteresting)
3ec1909f 532{
46dc9412
JH
533 /* We have examined up to i-1 and are about to look at i.
534 * Find next interesting or uninteresting line. Here,
535 * "interesting" does not mean interesting(), but marked by
536 * the give_context() function below (i.e. it includes context
537 * lines that are not interesting to interesting() function
538 * that are surrounded by interesting() ones.
539 */
74065951 540 while (i <= cnt)
2386c297 541 if (look_for_uninteresting
46dc9412
JH
542 ? !(sline[i].flag & mark)
543 : (sline[i].flag & mark))
3ec1909f
JH
544 return i;
545 else
546 i++;
74065951 547 return i;
3ec1909f
JH
548}
549
550static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
af3feefa
JH
551{
552 unsigned long all_mask = (1UL<<num_parent) - 1;
553 unsigned long mark = (1UL<<num_parent);
c86fbe53 554 unsigned long no_pre_delete = (2UL<<num_parent);
af3feefa
JH
555 unsigned long i;
556
46dc9412 557 /* Two groups of interesting lines may have a short gap of
82e5a82f 558 * uninteresting lines. Connect such groups to give them a
46dc9412
JH
559 * bit of context.
560 *
561 * We first start from what the interesting() function says,
562 * and mark them with "mark", and paint context lines with the
563 * mark. So interesting() would still say false for such context
564 * lines but they are treated as "interesting" in the end.
565 */
566 i = find_next(sline, mark, 0, cnt, 0);
74065951 567 if (cnt < i)
3ec1909f
JH
568 return 0;
569
74065951 570 while (i <= cnt) {
3ec1909f
JH
571 unsigned long j = (context < i) ? (i - context) : 0;
572 unsigned long k;
46dc9412
JH
573
574 /* Paint a few lines before the first interesting line. */
aac38571
MK
575 while (j < i) {
576 if (!(sline[j].flag & mark))
577 sline[j].flag |= no_pre_delete;
578 sline[j++].flag |= mark;
579 }
3ec1909f
JH
580
581 again:
46dc9412
JH
582 /* we know up to i is to be included. where does the
583 * next uninteresting one start?
584 */
585 j = find_next(sline, mark, i, cnt, 1);
74065951 586 if (cnt < j)
3ec1909f
JH
587 break; /* the rest are all interesting */
588
589 /* lookahead context lines */
46dc9412 590 k = find_next(sline, mark, j, cnt, 0);
3ec1909f
JH
591 j = adjust_hunk_tail(sline, all_mask, i, j);
592
593 if (k < j + context) {
594 /* k is interesting and [j,k) are not, but
595 * paint them interesting because the gap is small.
596 */
597 while (j < k)
af3feefa 598 sline[j++].flag |= mark;
3ec1909f
JH
599 i = k;
600 goto again;
af3feefa 601 }
3ec1909f
JH
602
603 /* j is the first uninteresting line and there is
46dc9412
JH
604 * no overlap beyond it within context lines. Paint
605 * the trailing edge a bit.
3ec1909f
JH
606 */
607 i = k;
74065951 608 k = (j + context < cnt+1) ? j + context : cnt+1;
3ec1909f
JH
609 while (j < k)
610 sline[j++].flag |= mark;
611 }
612 return 1;
613}
614
615static int make_hunks(struct sline *sline, unsigned long cnt,
616 int num_parent, int dense)
617{
618 unsigned long all_mask = (1UL<<num_parent) - 1;
619 unsigned long mark = (1UL<<num_parent);
620 unsigned long i;
621 int has_interesting = 0;
622
74065951 623 for (i = 0; i <= cnt; i++) {
3ec1909f
JH
624 if (interesting(&sline[i], all_mask))
625 sline[i].flag |= mark;
626 else
627 sline[i].flag &= ~mark;
af3feefa 628 }
d8f4790e 629 if (!dense)
3ec1909f 630 return give_context(sline, cnt, num_parent);
d8f4790e 631
263eee29
JH
632 /* Look at each hunk, and if we have changes from only one
633 * parent, or the changes are the same from all but one
634 * parent, mark that uninteresting.
d8f4790e
JH
635 */
636 i = 0;
74065951 637 while (i <= cnt) {
3ec1909f 638 unsigned long j, hunk_begin, hunk_end;
bf1c32bd 639 unsigned long same_diff;
74065951 640 while (i <= cnt && !(sline[i].flag & mark))
d8f4790e 641 i++;
74065951 642 if (cnt < i)
d8f4790e 643 break; /* No more interesting hunks */
3ec1909f 644 hunk_begin = i;
74065951 645 for (j = i + 1; j <= cnt; j++) {
3ec1909f
JH
646 if (!(sline[j].flag & mark)) {
647 /* Look beyond the end to see if there
648 * is an interesting line after this
649 * hunk within context span.
650 */
651 unsigned long la; /* lookahead */
652 int contin = 0;
653 la = adjust_hunk_tail(sline, all_mask,
654 hunk_begin, j);
74065951
JH
655 la = (la + context < cnt + 1) ?
656 (la + context) : cnt + 1;
e5e9b565 657 while (la && j <= --la) {
3ec1909f
JH
658 if (sline[la].flag & mark) {
659 contin = 1;
660 break;
661 }
662 }
663 if (!contin)
664 break;
665 j = la;
666 }
667 }
668 hunk_end = j;
669
bf1c32bd 670 /* [i..hunk_end) are interesting. Now is it really
fd4b1d21
JH
671 * interesting? We check if there are only two versions
672 * and the result matches one of them. That is, we look
673 * at:
674 * (+) line, which records lines added to which parents;
675 * this line appears in the result.
676 * (-) line, which records from what parents the line
677 * was removed; this line does not appear in the result.
678 * then check the set of parents the result has difference
679 * from, from all lines. If there are lines that has
680 * different set of parents that the result has differences
681 * from, that means we have more than two versions.
682 *
683 * Even when we have only two versions, if the result does
684 * not match any of the parents, the it should be considered
685 * interesting. In such a case, we would have all '+' line.
686 * After passing the above "two versions" test, that would
687 * appear as "the same set of parents" to be "all parents".
d8f4790e 688 */
bf1c32bd
JH
689 same_diff = 0;
690 has_interesting = 0;
691 for (j = i; j < hunk_end && !has_interesting; j++) {
46dc9412 692 unsigned long this_diff = sline[j].flag & all_mask;
99d32060 693 struct lline *ll = sline[j].lost;
bf1c32bd
JH
694 if (this_diff) {
695 /* This has some changes. Is it the
696 * same as others?
697 */
698 if (!same_diff)
699 same_diff = this_diff;
700 else if (same_diff != this_diff) {
701 has_interesting = 1;
702 break;
703 }
704 }
705 while (ll && !has_interesting) {
706 /* Lost this line from these parents;
707 * who are they? Are they the same?
708 */
709 this_diff = ll->parent_map;
710 if (!same_diff)
711 same_diff = this_diff;
712 else if (same_diff != this_diff) {
713 has_interesting = 1;
714 }
715 ll = ll->next;
716 }
d8f4790e 717 }
bf1c32bd 718
fd4b1d21 719 if (!has_interesting && same_diff != all_mask) {
d8f4790e 720 /* This hunk is not that interesting after all */
3ec1909f 721 for (j = hunk_begin; j < hunk_end; j++)
d8f4790e
JH
722 sline[j].flag &= ~mark;
723 }
724 i = hunk_end;
725 }
3ec1909f
JH
726
727 has_interesting = give_context(sline, cnt, num_parent);
8828cdcb 728 return has_interesting;
af3feefa
JH
729}
730
3b0f5e88 731static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
f16706cc
JH
732{
733 l0 = sline[l0].p_lno[n];
734 l1 = sline[l1].p_lno[n];
3b0f5e88 735 printf(" -%lu,%lu", l0, l1-l0-null_context);
f16706cc
JH
736}
737
d5f6a01a
JH
738static int hunk_comment_line(const char *bol)
739{
7a8ac59f
JH
740 int ch;
741
742 if (!bol)
743 return 0;
744 ch = *bol & 0xff;
d5f6a01a
JH
745 return (isalpha(ch) || ch == '_' || ch == '$');
746}
747
39280970
JH
748static void show_line_to_eol(const char *line, int len, const char *reset)
749{
750 int saw_cr_at_eol = 0;
751 if (len < 0)
752 len = strlen(line);
753 saw_cr_at_eol = (len && line[len-1] == '\r');
754
755 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
756 reset,
757 saw_cr_at_eol ? "\r" : "");
758}
759
41ee2ad6
JK
760static void dump_sline(struct sline *sline, const char *line_prefix,
761 unsigned long cnt, int num_parent,
21798708 762 int use_color, int result_deleted)
af3feefa
JH
763{
764 unsigned long mark = (1UL<<num_parent);
c86fbe53 765 unsigned long no_pre_delete = (2UL<<num_parent);
af3feefa 766 int i;
f16706cc 767 unsigned long lno = 0;
567a03d1 768 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
89cb73a1 769 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
567a03d1
JH
770 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
771 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
8dbf3eb6 772 const char *c_context = diff_get_color(use_color, DIFF_CONTEXT);
567a03d1 773 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
af3feefa 774
21798708 775 if (result_deleted)
4462731e
JH
776 return; /* result deleted */
777
af3feefa 778 while (1) {
8bc7574b
JH
779 unsigned long hunk_end;
780 unsigned long rlines;
d5f6a01a 781 const char *hunk_comment = NULL;
3b0f5e88 782 unsigned long null_context = 0;
d5f6a01a
JH
783
784 while (lno <= cnt && !(sline[lno].flag & mark)) {
785 if (hunk_comment_line(sline[lno].bol))
786 hunk_comment = sline[lno].bol;
af3feefa 787 lno++;
d5f6a01a 788 }
8a470ebf 789 if (cnt < lno)
af3feefa 790 break;
8a470ebf 791 else {
74065951 792 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
8a470ebf
JH
793 if (!(sline[hunk_end].flag & mark))
794 break;
795 }
74065951
JH
796 rlines = hunk_end - lno;
797 if (cnt < hunk_end)
798 rlines--; /* pointing at the last delete hunk */
3b0f5e88
JH
799
800 if (!context) {
801 /*
802 * Even when running with --unified=0, all
803 * lines in the hunk needs to be processed in
804 * the loop below in order to show the
805 * deletion recorded in lost_head. However,
806 * we do not want to show the resulting line
807 * with all blank context markers in such a
808 * case. Compensate.
809 */
810 unsigned long j;
811 for (j = lno; j < hunk_end; j++)
812 if (!(sline[j].flag & (mark-1)))
813 null_context++;
814 rlines -= null_context;
815 }
816
41ee2ad6 817 printf("%s%s", line_prefix, c_frag);
af3feefa 818 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
f16706cc 819 for (i = 0; i < num_parent; i++)
3b0f5e88 820 show_parent_lno(sline, lno, hunk_end, i, null_context);
74065951 821 printf(" +%lu,%lu ", lno+1, rlines);
af3feefa 822 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
d5f6a01a
JH
823
824 if (hunk_comment) {
825 int comment_end = 0;
826 for (i = 0; i < 40; i++) {
827 int ch = hunk_comment[i] & 0xff;
828 if (!ch || ch == '\n')
829 break;
830 if (!isspace(ch))
831 comment_end = i;
832 }
833 if (comment_end)
89cb73a1 834 printf("%s%s %s%s", c_reset,
8dbf3eb6 835 c_context, c_reset,
89cb73a1 836 c_func);
d5f6a01a
JH
837 for (i = 0; i < comment_end; i++)
838 putchar(hunk_comment[i]);
839 }
840
567a03d1 841 printf("%s\n", c_reset);
af3feefa
JH
842 while (lno < hunk_end) {
843 struct lline *ll;
844 int j;
46dc9412 845 unsigned long p_mask;
fd13b21f 846 struct sline *sl = &sline[lno++];
99d32060 847 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost;
af3feefa 848 while (ll) {
41ee2ad6 849 printf("%s%s", line_prefix, c_old);
af3feefa
JH
850 for (j = 0; j < num_parent; j++) {
851 if (ll->parent_map & (1UL<<j))
852 putchar('-');
853 else
854 putchar(' ');
855 }
39280970 856 show_line_to_eol(ll->line, -1, c_reset);
af3feefa
JH
857 ll = ll->next;
858 }
74065951 859 if (cnt < lno)
8a470ebf 860 break;
46dc9412 861 p_mask = 1;
41ee2ad6 862 fputs(line_prefix, stdout);
3b0f5e88
JH
863 if (!(sl->flag & (mark-1))) {
864 /*
865 * This sline was here to hang the
866 * lost lines in front of it.
867 */
868 if (!context)
869 continue;
8dbf3eb6 870 fputs(c_context, stdout);
3b0f5e88 871 }
567a03d1
JH
872 else
873 fputs(c_new, stdout);
af3feefa 874 for (j = 0; j < num_parent; j++) {
46dc9412 875 if (p_mask & sl->flag)
af3feefa 876 putchar('+');
46dc9412
JH
877 else
878 putchar(' ');
879 p_mask <<= 1;
af3feefa 880 }
39280970 881 show_line_to_eol(sl->bol, sl->len, c_reset);
af3feefa
JH
882 }
883 }
884}
885
3c39e9bd
JH
886static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
887 int i, int j)
888{
889 /* We have already examined parent j and we know parent i
890 * and parent j are the same, so reuse the combined result
891 * of parent j for parent i.
892 */
893 unsigned long lno, imask, jmask;
894 imask = (1UL<<i);
895 jmask = (1UL<<j);
896
8a470ebf 897 for (lno = 0; lno <= cnt; lno++) {
99d32060 898 struct lline *ll = sline->lost;
f16706cc 899 sline->p_lno[i] = sline->p_lno[j];
3c39e9bd
JH
900 while (ll) {
901 if (ll->parent_map & jmask)
902 ll->parent_map |= imask;
903 ll = ll->next;
904 }
46dc9412
JH
905 if (sline->flag & jmask)
906 sline->flag |= imask;
3c39e9bd
JH
907 sline++;
908 }
4462731e
JH
909 /* the overall size of the file (sline[cnt]) */
910 sline->p_lno[i] = sline->p_lno[j];
3c39e9bd
JH
911}
912
462a15bc
JH
913static void dump_quoted_path(const char *head,
914 const char *prefix,
915 const char *path,
41ee2ad6 916 const char *line_prefix,
567a03d1 917 const char *c_meta, const char *c_reset)
eab144ac 918{
462a15bc
JH
919 static struct strbuf buf = STRBUF_INIT;
920
921 strbuf_reset(&buf);
41ee2ad6 922 strbuf_addstr(&buf, line_prefix);
462a15bc
JH
923 strbuf_addstr(&buf, c_meta);
924 strbuf_addstr(&buf, head);
d5625091 925 quote_two_c_style(&buf, prefix, path, 0);
462a15bc
JH
926 strbuf_addstr(&buf, c_reset);
927 puts(buf.buf);
eab144ac
LT
928}
929
7c978a06
JK
930static void show_combined_header(struct combine_diff_path *elem,
931 int num_parent,
7c978a06 932 struct rev_info *rev,
41ee2ad6 933 const char *line_prefix,
4d5f3471
JK
934 int mode_differs,
935 int show_file_header)
7c978a06
JK
936{
937 struct diff_options *opt = &rev->diffopt;
976ff7e4 938 int abbrev = opt->flags.full_index ? the_hash_algo->hexsz : DEFAULT_ABBREV;
7c978a06
JK
939 const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
940 const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
f1c96261
JK
941 const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
942 const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
7c978a06
JK
943 const char *abb;
944 int added = 0;
945 int deleted = 0;
946 int i;
d01141de 947 int dense = rev->dense_combined_merges;
7c978a06
JK
948
949 if (rev->loginfo && !rev->no_commit_id)
950 show_log(rev);
951
952 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
41ee2ad6
JK
953 "", elem->path, line_prefix, c_meta, c_reset);
954 printf("%s%sindex ", line_prefix, c_meta);
7c978a06 955 for (i = 0; i < num_parent; i++) {
d850b7a5
ÆAB
956 abb = repo_find_unique_abbrev(the_repository,
957 &elem->parent[i].oid, abbrev);
7c978a06
JK
958 printf("%s%s", i ? "," : "", abb);
959 }
d850b7a5 960 abb = repo_find_unique_abbrev(the_repository, &elem->oid, abbrev);
7c978a06
JK
961 printf("..%s%s\n", abb, c_reset);
962
963 if (mode_differs) {
964 deleted = !elem->mode;
965
966 /* We say it was added if nobody had it */
967 added = !deleted;
968 for (i = 0; added && i < num_parent; i++)
969 if (elem->parent[i].status !=
970 DIFF_STATUS_ADDED)
971 added = 0;
972 if (added)
41ee2ad6
JK
973 printf("%s%snew file mode %06o",
974 line_prefix, c_meta, elem->mode);
7c978a06
JK
975 else {
976 if (deleted)
41ee2ad6
JK
977 printf("%s%sdeleted file ",
978 line_prefix, c_meta);
7c978a06
JK
979 printf("mode ");
980 for (i = 0; i < num_parent; i++) {
981 printf("%s%06o", i ? "," : "",
982 elem->parent[i].mode);
983 }
984 if (elem->mode)
985 printf("..%06o", elem->mode);
986 }
987 printf("%s\n", c_reset);
988 }
989
4d5f3471
JK
990 if (!show_file_header)
991 return;
992
d76ce4f7
EN
993 if (rev->combined_all_paths) {
994 for (i = 0; i < num_parent; i++) {
995 char *path = filename_changed(elem->parent[i].status)
996 ? elem->parent[i].path.buf : elem->path;
997 if (elem->parent[i].status == DIFF_STATUS_ADDED)
998 dump_quoted_path("--- ", "", "/dev/null",
999 line_prefix, c_meta, c_reset);
1000 else
1001 dump_quoted_path("--- ", a_prefix, path,
1002 line_prefix, c_meta, c_reset);
1003 }
1004 } else {
1005 if (added)
1006 dump_quoted_path("--- ", "", "/dev/null",
1007 line_prefix, c_meta, c_reset);
1008 else
1009 dump_quoted_path("--- ", a_prefix, elem->path,
1010 line_prefix, c_meta, c_reset);
1011 }
7c978a06
JK
1012 if (deleted)
1013 dump_quoted_path("+++ ", "", "/dev/null",
41ee2ad6 1014 line_prefix, c_meta, c_reset);
7c978a06
JK
1015 else
1016 dump_quoted_path("+++ ", b_prefix, elem->path,
41ee2ad6 1017 line_prefix, c_meta, c_reset);
7c978a06
JK
1018}
1019
89b0c4b5 1020static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
d01141de 1021 int working_tree_file,
99694544 1022 struct rev_info *rev)
af3feefa 1023{
91539833 1024 struct diff_options *opt = &rev->diffopt;
f23fc773 1025 unsigned long result_size, cnt, lno;
21798708 1026 int result_deleted = 0;
310f8b5b 1027 char *result, *cp;
af3feefa 1028 struct sline *sline; /* survived lines */
2454c962 1029 int mode_differs = 0;
89b0c4b5 1030 int i, show_hunks;
f23fc773 1031 mmfile_t result_file;
4d5f3471 1032 struct userdiff_driver *userdiff;
0508fe53 1033 struct userdiff_driver *textconv = NULL;
4d5f3471 1034 int is_binary;
41ee2ad6 1035 const char *line_prefix = diff_line_prefix(opt);
af3feefa 1036
ee1e5412 1037 context = opt->context;
acd00ea0 1038 userdiff = userdiff_find_by_path(opt->repo->index, elem->path);
4d5f3471
JK
1039 if (!userdiff)
1040 userdiff = userdiff_find_by_name("default");
0d1e0e78 1041 if (opt->flags.allow_textconv)
bd7ad45b 1042 textconv = userdiff_get_textconv(opt->repo, userdiff);
a5a818ee 1043
af3feefa 1044 /* Read the result of merge first */
f23fc773 1045 if (!working_tree_file)
6afaf807 1046 result = grab_blob(opt->repo, &elem->oid, elem->mode, &result_size,
0508fe53 1047 textconv, elem->path);
ea726d02 1048 else {
9843a1f6 1049 /* Used by diff-tree to read from the working tree */
ea726d02 1050 struct stat st;
4fc970c4
JH
1051 int fd = -1;
1052
1053 if (lstat(elem->path, &st) < 0)
1054 goto deleted_file;
1055
1056 if (S_ISLNK(st.st_mode)) {
912342d9
JH
1057 struct strbuf buf = STRBUF_INIT;
1058
1059 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
4b94ec9b 1060 error_errno("readlink(%s)", elem->path);
4fc970c4
JH
1061 return;
1062 }
912342d9
JH
1063 result_size = buf.len;
1064 result = strbuf_detach(&buf, NULL);
4fc970c4 1065 elem->mode = canon_mode(st.st_mode);
7dae8b21 1066 } else if (S_ISDIR(st.st_mode)) {
1ff57c13 1067 struct object_id oid;
a98e6101 1068 if (resolve_gitlink_ref(elem->path, "HEAD", &oid) < 0)
6afaf807
NTND
1069 result = grab_blob(opt->repo, &elem->oid,
1070 elem->mode, &result_size,
1071 NULL, NULL);
7dae8b21 1072 else
6afaf807 1073 result = grab_blob(opt->repo, &oid, elem->mode,
0508fe53
JK
1074 &result_size, NULL, NULL);
1075 } else if (textconv) {
1076 struct diff_filespec *df = alloc_filespec(elem->path);
14228447 1077 fill_filespec(df, null_oid(), 0, st.st_mode);
6afaf807 1078 result_size = fill_textconv(opt->repo, textconv, df, &result);
0508fe53 1079 free_filespec(df);
91fcbcbd 1080 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
dc49cd76 1081 size_t len = xsize_t(st.st_size);
c697ad14 1082 ssize_t done;
a249a9b5 1083 int is_file, i;
ea726d02 1084
1b0c7174 1085 elem->mode = canon_mode(st.st_mode);
a249a9b5
JS
1086 /* if symlinks don't work, assume symlink if all parents
1087 * are symlinks
1088 */
1089 is_file = has_symlinks;
1090 for (i = 0; !is_file && i < num_parent; i++)
1091 is_file = !S_ISLNK(elem->parent[i].mode);
1092 if (!is_file)
1093 elem->mode = canon_mode(S_IFLNK);
1094
f23fc773 1095 result_size = len;
3733e694 1096 result = xmallocz(len);
c697ad14
HO
1097
1098 done = read_in_full(fd, result, len);
1099 if (done < 0)
0721c314 1100 die_errno("read error '%s'", elem->path);
c697ad14
HO
1101 else if (done < len)
1102 die("early EOF '%s'", elem->path);
1103
5e568f9e
AG
1104 /* If not a fake symlink, apply filters, e.g. autocrlf */
1105 if (is_file) {
f285a2d7 1106 struct strbuf buf = STRBUF_INIT;
5e568f9e 1107
0734f209
NTND
1108 if (convert_to_git(rev->diffopt.repo->index,
1109 elem->path, result, len, &buf, global_conv_flags_eol)) {
5e568f9e
AG
1110 free(result);
1111 result = strbuf_detach(&buf, &len);
1112 result_size = len;
1113 }
1114 }
ea726d02
JH
1115 }
1116 else {
4fc970c4 1117 deleted_file:
21798708 1118 result_deleted = 1;
f23fc773 1119 result_size = 0;
713a11fc 1120 elem->mode = 0;
28f75818 1121 result = xcalloc(1, 1);
ea726d02 1122 }
4fc970c4 1123
ea726d02
JH
1124 if (0 <= fd)
1125 close(fd);
1126 }
af3feefa 1127
c95b99bb
JK
1128 for (i = 0; i < num_parent; i++) {
1129 if (elem->parent[i].mode != elem->mode) {
1130 mode_differs = 1;
1131 break;
1132 }
1133 }
1134
0508fe53
JK
1135 if (textconv)
1136 is_binary = 0;
1137 else if (userdiff->binary != -1)
4d5f3471
JK
1138 is_binary = userdiff->binary;
1139 else {
1140 is_binary = buffer_is_binary(result, result_size);
1141 for (i = 0; !is_binary && i < num_parent; i++) {
1142 char *buf;
1143 unsigned long size;
6afaf807
NTND
1144 buf = grab_blob(opt->repo,
1145 &elem->parent[i].oid,
4d5f3471 1146 elem->parent[i].mode,
0508fe53 1147 &size, NULL, NULL);
4d5f3471
JK
1148 if (buffer_is_binary(buf, size))
1149 is_binary = 1;
1150 free(buf);
1151 }
1152 }
1153 if (is_binary) {
d01141de 1154 show_combined_header(elem, num_parent, rev,
41ee2ad6 1155 line_prefix, mode_differs, 0);
4d5f3471
JK
1156 printf("Binary files differ\n");
1157 free(result);
1158 return;
1159 }
1160
2386c297 1161 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
af3feefa
JH
1162 if (*cp == '\n')
1163 cnt++;
1164 }
f23fc773 1165 if (result_size && result[result_size-1] != '\n')
af3feefa
JH
1166 cnt++; /* incomplete line */
1167
ca56dadb 1168 CALLOC_ARRAY(sline, st_add(cnt, 2));
af3feefa 1169 sline[0].bol = result;
2386c297 1170 for (lno = 0, cp = result; cp < result + result_size; cp++) {
af3feefa
JH
1171 if (*cp == '\n') {
1172 sline[lno].len = cp - sline[lno].bol;
af3feefa
JH
1173 lno++;
1174 if (lno < cnt)
1175 sline[lno].bol = cp + 1;
1176 }
1177 }
f23fc773
JH
1178 if (result_size && result[result_size-1] != '\n')
1179 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
1180
1181 result_file.ptr = result;
1182 result_file.size = result_size;
af3feefa 1183
8a470ebf
JH
1184 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1185 * for deletion hunk at the end.
1186 */
ca56dadb 1187 CALLOC_ARRAY(sline[0].p_lno, st_mult(st_add(cnt, 2), num_parent));
8a470ebf 1188 for (lno = 0; lno <= cnt; lno++)
f16706cc
JH
1189 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
1190
3c39e9bd
JH
1191 for (i = 0; i < num_parent; i++) {
1192 int j;
1193 for (j = 0; j < i; j++) {
4a7e27e9
JK
1194 if (oideq(&elem->parent[i].oid,
1195 &elem->parent[j].oid)) {
3c39e9bd
JH
1196 reuse_combine_diff(sline, cnt, i, j);
1197 break;
1198 }
1199 }
1200 if (i <= j)
6afaf807
NTND
1201 combine_diff(opt->repo,
1202 &elem->parent[i].oid,
7dae8b21
JH
1203 elem->parent[i].mode,
1204 &result_file, sline,
0508fe53 1205 cnt, i, num_parent, result_deleted,
fa04ae0b 1206 textconv, elem->path, opt->xdl_opts);
3c39e9bd 1207 }
af3feefa 1208
d01141de 1209 show_hunks = make_hunks(sline, cnt, num_parent, rev->dense_combined_merges);
af3feefa 1210
713a11fc 1211 if (show_hunks || mode_differs || working_tree_file) {
d01141de 1212 show_combined_header(elem, num_parent, rev,
41ee2ad6
JK
1213 line_prefix, mode_differs, 1);
1214 dump_sline(sline, line_prefix, cnt, num_parent,
f1c96261 1215 opt->use_color, result_deleted);
8828cdcb 1216 }
af3feefa
JH
1217 free(result);
1218
2386c297 1219 for (lno = 0; lno < cnt; lno++) {
99d32060
AP
1220 if (sline[lno].lost) {
1221 struct lline *ll = sline[lno].lost;
af3feefa
JH
1222 while (ll) {
1223 struct lline *tmp = ll;
1224 ll = ll->next;
1225 free(tmp);
1226 }
1227 }
1228 }
46dc9412 1229 free(sline[0].p_lno);
af3feefa
JH
1230 free(sline);
1231}
1232
91539833 1233static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
ee638024 1234{
91539833 1235 struct diff_options *opt = &rev->diffopt;
77667051 1236 int line_termination, inter_name_termination, i;
41ee2ad6 1237 const char *line_prefix = diff_line_prefix(opt);
ee638024
LT
1238
1239 line_termination = opt->line_termination;
1240 inter_name_termination = '\t';
1241 if (!line_termination)
1242 inter_name_termination = 0;
1243
44152787 1244 if (rev->loginfo && !rev->no_commit_id)
02865655 1245 show_log(rev);
ee638024 1246
a1d68bea 1247
c6744349 1248 if (opt->output_format & DIFF_FORMAT_RAW) {
41ee2ad6 1249 printf("%s", line_prefix);
a1d68bea 1250
77667051
JH
1251 /* As many colons as there are parents */
1252 for (i = 0; i < num_parent; i++)
1253 putchar(':');
0a798076
JH
1254
1255 /* Show the modes */
77667051
JH
1256 for (i = 0; i < num_parent; i++)
1257 printf("%06o ", p->parent[i].mode);
1258 printf("%06o", p->mode);
0a798076
JH
1259
1260 /* Show sha1's */
1261 for (i = 0; i < num_parent; i++)
d6cece51 1262 printf(" %s", diff_aligned_abbrev(&p->parent[i].oid,
d5e3b01e 1263 opt->abbrev));
d6cece51 1264 printf(" %s ", diff_aligned_abbrev(&p->oid, opt->abbrev));
0a798076
JH
1265 }
1266
c6744349 1267 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
d416df88
JH
1268 for (i = 0; i < num_parent; i++)
1269 putchar(p->parent[i].status);
1270 putchar(inter_name_termination);
1271 }
0a798076 1272
d76ce4f7
EN
1273 for (i = 0; i < num_parent; i++)
1274 if (rev->combined_all_paths) {
1275 if (filename_changed(p->parent[i].status))
1276 write_name_quoted(p->parent[i].path.buf, stdout,
1277 inter_name_termination);
1278 else
1279 write_name_quoted(p->path, stdout,
1280 inter_name_termination);
1281 }
663af342 1282 write_name_quoted(p->path, stdout, line_termination);
0a798076
JH
1283}
1284
99694544
JH
1285/*
1286 * The result (p->elem) is from the working tree and their
1287 * parents are typically from multiple stages during a merge
1288 * (i.e. diff-files) or the state in HEAD and in the index
1289 * (i.e. diff-index).
1290 */
91539833 1291void show_combined_diff(struct combine_diff_path *p,
0a798076 1292 int num_parent,
91539833 1293 struct rev_info *rev)
0a798076 1294{
91539833 1295 struct diff_options *opt = &rev->diffopt;
41ee2ad6 1296
c6744349
TH
1297 if (opt->output_format & (DIFF_FORMAT_RAW |
1298 DIFF_FORMAT_NAME |
89b0c4b5 1299 DIFF_FORMAT_NAME_STATUS))
91539833 1300 show_raw_diff(p, num_parent, rev);
89b0c4b5 1301 else if (opt->output_format & DIFF_FORMAT_PATCH)
d01141de 1302 show_patch_diff(p, num_parent, 1, rev);
ee638024
LT
1303}
1304
25e5e2bf
JH
1305static void free_combined_pair(struct diff_filepair *pair)
1306{
1307 free(pair->two);
1308 free(pair);
1309}
1310
1311/*
1312 * A combine_diff_path expresses N parents on the LHS against 1 merge
1313 * result. Synthesize a diff_filepair that has N entries on the "one"
1314 * side and 1 entry on the "two" side.
1315 *
1316 * In the future, we might want to add more data to combine_diff_path
1317 * so that we can fill fields we are ignoring (most notably, size) here,
1318 * but currently nobody uses it, so this should suffice for now.
1319 */
1320static struct diff_filepair *combined_pair(struct combine_diff_path *p,
1321 int num_parent)
1322{
1323 int i;
1324 struct diff_filepair *pair;
1325 struct diff_filespec *pool;
1326
1327 pair = xmalloc(sizeof(*pair));
ca56dadb 1328 CALLOC_ARRAY(pool, st_add(num_parent, 1));
25e5e2bf
JH
1329 pair->one = pool + 1;
1330 pair->two = pool;
1331
1332 for (i = 0; i < num_parent; i++) {
1333 pair->one[i].path = p->path;
1334 pair->one[i].mode = p->parent[i].mode;
a0d12c44 1335 oidcpy(&pair->one[i].oid, &p->parent[i].oid);
41c9560e 1336 pair->one[i].oid_valid = !is_null_oid(&p->parent[i].oid);
25e5e2bf
JH
1337 pair->one[i].has_more_entries = 1;
1338 }
1339 pair->one[num_parent - 1].has_more_entries = 0;
1340
1341 pair->two->path = p->path;
1342 pair->two->mode = p->mode;
a0d12c44 1343 oidcpy(&pair->two->oid, &p->oid);
41c9560e 1344 pair->two->oid_valid = !is_null_oid(&p->oid);
25e5e2bf
JH
1345 return pair;
1346}
1347
1348static void handle_combined_callback(struct diff_options *opt,
1349 struct combine_diff_path *paths,
1350 int num_parent,
1351 int num_paths)
1352{
1353 struct combine_diff_path *p;
1354 struct diff_queue_struct q;
1355 int i;
1356
ca56dadb 1357 CALLOC_ARRAY(q.queue, num_paths);
25e5e2bf
JH
1358 q.alloc = num_paths;
1359 q.nr = num_paths;
af82c788 1360 for (i = 0, p = paths; p; p = p->next)
25e5e2bf 1361 q.queue[i++] = combined_pair(p, num_parent);
25e5e2bf
JH
1362 opt->format_callback(&q, opt, opt->format_callback_data);
1363 for (i = 0; i < num_paths; i++)
1364 free_combined_pair(q.queue[i]);
1365 free(q.queue);
1366}
1367
8518ff8f
KS
1368static const char *path_path(void *obj)
1369{
1370 struct combine_diff_path *path = (struct combine_diff_path *)obj;
1371
1372 return path->path;
1373}
1374
8817f0cc
JK
1375/*
1376 * Diff stat formats which we always compute solely against the first parent.
1377 */
1378#define STAT_FORMAT_MASK (DIFF_FORMAT_NUMSTAT \
8290faa0 1379 | DIFF_FORMAT_SHORTSTAT \
04b19fca 1380 | DIFF_FORMAT_SUMMARY \
dac03b55 1381 | DIFF_FORMAT_DIRSTAT \
8817f0cc 1382 | DIFF_FORMAT_DIFFSTAT)
eeb3f328
KS
1383
1384/* find set of paths that every parent touches */
09fae19a 1385static struct combine_diff_path *find_paths_generic(const struct object_id *oid,
d76ce4f7
EN
1386 const struct oid_array *parents,
1387 struct diff_options *opt,
1388 int combined_all_paths)
eeb3f328
KS
1389{
1390 struct combine_diff_path *paths = NULL;
1391 int i, num_parent = parents->nr;
1392
1393 int output_format = opt->output_format;
1394 const char *orderfile = opt->orderfile;
1395
1396 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
1397 /* tell diff_tree to emit paths in sorted (=tree) order */
1398 opt->orderfile = NULL;
1399
7195fbfa 1400 /* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn) (wrt paths) */
eeb3f328
KS
1401 for (i = 0; i < num_parent; i++) {
1402 /*
1403 * show stat against the first parent even when doing
1404 * combined diff.
1405 */
8817f0cc 1406 int stat_opt = output_format & STAT_FORMAT_MASK;
eeb3f328
KS
1407 if (i == 0 && stat_opt)
1408 opt->output_format = stat_opt;
1409 else
1410 opt->output_format = DIFF_FORMAT_NO_OUTPUT;
66f414f8 1411 diff_tree_oid(&parents->oid[i], oid, "", opt);
eeb3f328 1412 diffcore_std(opt);
d76ce4f7
EN
1413 paths = intersect_paths(paths, i, num_parent,
1414 combined_all_paths);
eeb3f328
KS
1415
1416 /* if showing diff, show it in requested order */
1417 if (opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1418 orderfile) {
1419 diffcore_order(orderfile);
1420 }
1421
1422 diff_flush(opt);
1423 }
1424
1425 opt->output_format = output_format;
1426 opt->orderfile = orderfile;
1427 return paths;
1428}
1429
1430
7195fbfa
KS
1431/*
1432 * find set of paths that everybody touches, assuming diff is run without
1433 * rename/copy detection, etc, comparing all trees simultaneously (= faster).
1434 */
1435static struct combine_diff_path *find_paths_multitree(
09fae19a 1436 const struct object_id *oid, const struct oid_array *parents,
7195fbfa
KS
1437 struct diff_options *opt)
1438{
1439 int i, nparent = parents->nr;
fda94b41 1440 const struct object_id **parents_oid;
7195fbfa
KS
1441 struct combine_diff_path paths_head;
1442 struct strbuf base;
1443
fda94b41 1444 ALLOC_ARRAY(parents_oid, nparent);
7195fbfa 1445 for (i = 0; i < nparent; i++)
fda94b41 1446 parents_oid[i] = &parents->oid[i];
7195fbfa
KS
1447
1448 /* fake list head, so worker can assume it is non-NULL */
1449 paths_head.next = NULL;
1450
1451 strbuf_init(&base, PATH_MAX);
fda94b41 1452 diff_tree_paths(&paths_head, oid, parents_oid, nparent, &base, opt);
7195fbfa
KS
1453
1454 strbuf_release(&base);
fda94b41 1455 free(parents_oid);
7195fbfa
KS
1456 return paths_head.next;
1457}
1458
957876f1
JK
1459static int match_objfind(struct combine_diff_path *path,
1460 int num_parent,
1461 const struct oidset *set)
1462{
1463 int i;
1464 if (oidset_contains(set, &path->oid))
1465 return 1;
1466 for (i = 0; i < num_parent; i++) {
1467 if (oidset_contains(set, &path->parent[i].oid))
1468 return 1;
1469 }
1470 return 0;
1471}
1472
1473static struct combine_diff_path *combined_objfind(struct diff_options *opt,
1474 struct combine_diff_path *paths,
1475 int num_parent)
1476{
1477 struct combine_diff_path *ret = NULL, **tail = &ret;
1478 struct combine_diff_path *p = paths;
1479
1480 while (p) {
1481 struct combine_diff_path *next = p->next;
1482
1483 if (match_objfind(p, num_parent, opt->objfind)) {
1484 p->next = NULL;
1485 *tail = p;
1486 tail = &p->next;
1487 } else {
1488 free(p);
1489 }
1490 p = next;
1491 }
1492
1493 return ret;
1494}
7195fbfa 1495
b9acf54d 1496void diff_tree_combined(const struct object_id *oid,
910650d2 1497 const struct oid_array *parents,
0fe7c1de 1498 struct rev_info *rev)
af3feefa 1499{
91539833 1500 struct diff_options *opt = &rev->diffopt;
af3feefa 1501 struct diff_options diffopts;
eeb3f328 1502 struct combine_diff_path *p, *paths;
0041f09d 1503 int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
7195fbfa 1504 int need_generic_pathscan;
af3feefa 1505
e3d1be42
RS
1506 if (opt->ignore_regex_nr)
1507 die("combined diff and '%s' cannot be used together",
1508 "--ignore-matching-lines");
cfb19ae0
RS
1509 if (opt->close_file)
1510 die("combined diff and '%s' cannot be used together",
1511 "--output");
e3d1be42 1512
51af1886
KS
1513 /* nothing to do, if no parents */
1514 if (!num_parent)
1515 return;
1516
1517 show_log_first = !!rev->loginfo && !rev->no_commit_id;
1518 needsep = 0;
1519 if (show_log_first) {
1520 show_log(rev);
1521
e7cc0ede 1522 if (rev->verbose_header && opt->output_format &&
b9c7d6e4
JK
1523 opt->output_format != DIFF_FORMAT_NO_OUTPUT &&
1524 !commit_format_is_empty(rev->commit_format))
51af1886
KS
1525 printf("%s%c", diff_line_prefix(opt),
1526 opt->line_termination);
1527 }
1528
5b236832 1529 diffopts = *opt;
bd1928df 1530 copy_pathspec(&diffopts.pathspec, &opt->pathspec);
0d1e0e78
BW
1531 diffopts.flags.recursive = 1;
1532 diffopts.flags.allow_external = 0;
af3feefa 1533
eeb3f328
KS
1534 /* find set of paths that everybody touches
1535 *
7195fbfa
KS
1536 * NOTE
1537 *
1538 * Diffcore transformations are bound to diff_filespec and logic
1539 * comparing two entries - i.e. they do not apply directly to combine
1540 * diff.
1541 *
1542 * If some of such transformations is requested - we launch generic
1543 * path scanning, which works significantly slower compared to
1544 * simultaneous all-trees-in-one-go scan in find_paths_multitree().
1545 *
1546 * TODO some of the filters could be ported to work on
1547 * combine_diff_paths - i.e. all functionality that skips paths, so in
1548 * theory, we could end up having only multitree path scanning.
1549 *
1550 * NOTE please keep this semantically in sync with diffcore_std()
eeb3f328 1551 */
7195fbfa 1552 need_generic_pathscan = opt->skip_stat_unmatch ||
0d1e0e78 1553 opt->flags.follow_renames ||
7195fbfa
KS
1554 opt->break_opt != -1 ||
1555 opt->detect_rename ||
957876f1
JK
1556 (opt->pickaxe_opts &
1557 (DIFF_PICKAXE_KINDS_MASK & ~DIFF_PICKAXE_KIND_OBJFIND)) ||
7195fbfa
KS
1558 opt->filter;
1559
7195fbfa
KS
1560 if (need_generic_pathscan) {
1561 /*
1562 * NOTE generic case also handles --stat, as it computes
1563 * diff(sha1,parent_i) for all i to do the job, specifically
1564 * for parent0.
1565 */
d76ce4f7
EN
1566 paths = find_paths_generic(oid, parents, &diffopts,
1567 rev->combined_all_paths);
7195fbfa
KS
1568 }
1569 else {
1570 int stat_opt;
09fae19a 1571 paths = find_paths_multitree(oid, parents, &diffopts);
7195fbfa 1572
957876f1
JK
1573 if (opt->pickaxe_opts & DIFF_PICKAXE_KIND_OBJFIND)
1574 paths = combined_objfind(opt, paths, num_parent);
1575
7195fbfa
KS
1576 /*
1577 * show stat against the first parent even
1578 * when doing combined diff.
1579 */
8817f0cc 1580 stat_opt = opt->output_format & STAT_FORMAT_MASK;
7195fbfa
KS
1581 if (stat_opt) {
1582 diffopts.output_format = stat_opt;
1583
66f414f8 1584 diff_tree_oid(&parents->oid[0], oid, "", &diffopts);
7195fbfa
KS
1585 diffcore_std(&diffopts);
1586 if (opt->orderfile)
1587 diffcore_order(opt->orderfile);
1588 diff_flush(&diffopts);
1589 }
1590 }
af3feefa 1591
af82c788
KS
1592 /* find out number of surviving paths */
1593 for (num_paths = 0, p = paths; p; p = p->next)
1594 num_paths++;
8518ff8f
KS
1595
1596 /* order paths according to diffcore_order */
1597 if (opt->orderfile && num_paths) {
1598 struct obj_order *o;
1599
b32fa95f 1600 ALLOC_ARRAY(o, num_paths);
8518ff8f
KS
1601 for (i = 0, p = paths; p; p = p->next, i++)
1602 o[i].obj = p;
1603 order_objects(opt->orderfile, path_path, o, num_paths);
1604 for (i = 0; i < num_paths - 1; i++) {
1605 p = o[i].obj;
1606 p->next = o[i+1].obj;
1607 }
1608
1609 p = o[num_paths-1].obj;
1610 p->next = NULL;
1611 paths = o[0].obj;
1612 free(o);
1613 }
1614
1615
e3c3a550 1616 if (num_paths) {
c6744349
TH
1617 if (opt->output_format & (DIFF_FORMAT_RAW |
1618 DIFF_FORMAT_NAME |
1619 DIFF_FORMAT_NAME_STATUS)) {
af82c788
KS
1620 for (p = paths; p; p = p->next)
1621 show_raw_diff(p, num_parent, rev);
3969cf7d 1622 needsep = 1;
86ff1d20 1623 }
8817f0cc 1624 else if (opt->output_format & STAT_FORMAT_MASK)
3969cf7d 1625 needsep = 1;
25e5e2bf
JH
1626 else if (opt->output_format & DIFF_FORMAT_CALLBACK)
1627 handle_combined_callback(opt, paths, num_parent, num_paths);
1628
c6744349 1629 if (opt->output_format & DIFF_FORMAT_PATCH) {
3969cf7d 1630 if (needsep)
41ee2ad6
JK
1631 printf("%s%c", diff_line_prefix(opt),
1632 opt->line_termination);
af82c788 1633 for (p = paths; p; p = p->next)
d01141de 1634 show_patch_diff(p, num_parent, 0, rev);
af3feefa
JH
1635 }
1636 }
1637
1638 /* Clean things up */
1639 while (paths) {
ea726d02 1640 struct combine_diff_path *tmp = paths;
af3feefa 1641 paths = paths->next;
d76ce4f7
EN
1642 for (i = 0; i < num_parent; i++)
1643 if (rev->combined_all_paths &&
1644 filename_changed(tmp->parent[i].status))
1645 strbuf_release(&tmp->parent[i].path);
af3feefa
JH
1646 free(tmp);
1647 }
46ec510a 1648
ed6e8038 1649 clear_pathspec(&diffopts.pathspec);
af3feefa 1650}
0fe7c1de 1651
d01141de 1652void diff_tree_combined_merge(const struct commit *commit,
82889295 1653 struct rev_info *rev)
0fe7c1de 1654{
53d00b39 1655 struct commit_list *parent = get_saved_parents(rev, commit);
910650d2 1656 struct oid_array parents = OID_ARRAY_INIT;
0041f09d
RS
1657
1658 while (parent) {
910650d2 1659 oid_array_append(&parents, &parent->item->object.oid);
0041f09d
RS
1660 parent = parent->next;
1661 }
d01141de 1662 diff_tree_combined(&commit->object.oid, &parents, rev);
910650d2 1663 oid_array_clear(&parents);
0fe7c1de 1664}