]> git.ipfire.org Git - thirdparty/git.git/blame - combine-diff.c
diff test: add tests for combine-diff with orderfile
[thirdparty/git.git] / combine-diff.c
CommitLineData
af3feefa
JH
1#include "cache.h"
2#include "commit.h"
8e440259 3#include "blob.h"
af3feefa
JH
4#include "diff.h"
5#include "diffcore.h"
6#include "quote.h"
d9ea73e0 7#include "xdiff-interface.h"
fa04ae0b 8#include "xdiff/xmacros.h"
91539833 9#include "log-tree.h"
7dae8b21 10#include "refs.h"
4d5f3471 11#include "userdiff.h"
0041f09d 12#include "sha1-array.h"
53d00b39 13#include "revision.h"
af3feefa 14
ea726d02 15static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
af3feefa
JH
16{
17 struct diff_queue_struct *q = &diff_queued_diff;
ea726d02 18 struct combine_diff_path *p;
af3feefa
JH
19 int i;
20
21 if (!n) {
ea726d02 22 struct combine_diff_path *list = NULL, **tail = &list;
af3feefa
JH
23 for (i = 0; i < q->nr; i++) {
24 int len;
25 const char *path;
a976b0a5 26 if (diff_unmodified_pair(q->queue[i]))
af3feefa
JH
27 continue;
28 path = q->queue[i]->two->path;
29 len = strlen(path);
2454c962 30 p = xmalloc(combine_diff_path_size(num_parent, len));
4b25d091 31 p->path = (char *) &(p->parent[num_parent]);
af3feefa
JH
32 memcpy(p->path, path, len);
33 p->path[len] = 0;
34 p->len = len;
35 p->next = NULL;
2454c962
JH
36 memset(p->parent, 0,
37 sizeof(p->parent[0]) * num_parent);
38
e702496e 39 hashcpy(p->sha1, q->queue[i]->two->sha1);
2454c962 40 p->mode = q->queue[i]->two->mode;
e702496e 41 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
2454c962 42 p->parent[n].mode = q->queue[i]->one->mode;
d416df88 43 p->parent[n].status = q->queue[i]->status;
5290a0f8
JH
44 *tail = p;
45 tail = &p->next;
af3feefa
JH
46 }
47 return list;
48 }
49
50 for (p = curr; p; p = p->next) {
51 int found = 0;
52 if (!p->len)
53 continue;
54 for (i = 0; i < q->nr; i++) {
55 const char *path;
56 int len;
57
a976b0a5 58 if (diff_unmodified_pair(q->queue[i]))
af3feefa
JH
59 continue;
60 path = q->queue[i]->two->path;
61 len = strlen(path);
62 if (len == p->len && !memcmp(path, p->path, len)) {
63 found = 1;
e702496e 64 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
2454c962 65 p->parent[n].mode = q->queue[i]->one->mode;
d416df88 66 p->parent[n].status = q->queue[i]->status;
af3feefa
JH
67 break;
68 }
69 }
70 if (!found)
71 p->len = 0;
72 }
73 return curr;
74}
75
b469d8b6 76/* Lines lost from parent */
af3feefa 77struct lline {
99d32060 78 struct lline *next, *prev;
af3feefa
JH
79 int len;
80 unsigned long parent_map;
81 char line[FLEX_ARRAY];
82};
83
99d32060
AP
84/* Lines lost from current parent (before coalescing) */
85struct plost {
86 struct lline *lost_head, *lost_tail;
87 int len;
88};
89
b469d8b6 90/* Lines surviving in the merge result */
af3feefa 91struct sline {
99d32060
AP
92 /* Accumulated and coalesced lost lines */
93 struct lline *lost;
94 int lenlost;
95 struct plost plost;
af3feefa
JH
96 char *bol;
97 int len;
46dc9412
JH
98 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
99 * we did not change it).
b469d8b6 100 * bit N is used for "interesting" lines, including context.
c86fbe53 101 * bit (N+1) is used for "do not show deletion before this".
b469d8b6 102 */
af3feefa 103 unsigned long flag;
f16706cc 104 unsigned long *p_lno;
af3feefa
JH
105};
106
fa04ae0b
AP
107static int match_string_spaces(const char *line1, int len1,
108 const char *line2, int len2,
109 long flags)
110{
111 if (flags & XDF_WHITESPACE_FLAGS) {
112 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
113 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
114 }
115
116 if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE)))
117 return (len1 == len2 && !memcmp(line1, line2, len1));
118
119 while (len1 > 0 && len2 > 0) {
120 len1--;
121 len2--;
122 if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) {
123 if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) &&
124 (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2])))
125 return 0;
126
127 for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--);
128 for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--);
129 }
130 if (line1[len1] != line2[len2])
131 return 0;
132 }
133
134 if (flags & XDF_IGNORE_WHITESPACE) {
135 /* Consume remaining spaces */
136 for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--);
137 for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--);
138 }
139
140 /* We matched full line1 and line2 */
141 if (!len1 && !len2)
142 return 1;
143
144 return 0;
145}
146
99d32060
AP
147enum coalesce_direction { MATCH, BASE, NEW };
148
149/* Coalesce new lines into base by finding LCS */
150static struct lline *coalesce_lines(struct lline *base, int *lenbase,
151 struct lline *new, int lennew,
152 unsigned long parent, long flags)
af3feefa 153{
99d32060
AP
154 int **lcs;
155 enum coalesce_direction **directions;
156 struct lline *baseend, *newend = NULL;
157 int i, j, origbaselen = *lenbase;
af3feefa 158
99d32060
AP
159 if (new == NULL)
160 return base;
161
162 if (base == NULL) {
163 *lenbase = lennew;
164 return new;
165 }
166
167 /*
168 * Coalesce new lines into base by finding the LCS
98e023de 169 * - Create the table to run dynamic programming
99d32060
AP
170 * - Compute the LCS
171 * - Then reverse read the direction structure:
172 * - If we have MATCH, assign parent to base flag, and consume
173 * both baseend and newend
174 * - Else if we have BASE, consume baseend
175 * - Else if we have NEW, insert newend lline into base and
176 * consume newend
177 */
178 lcs = xcalloc(origbaselen + 1, sizeof(int*));
179 directions = xcalloc(origbaselen + 1, sizeof(enum coalesce_direction*));
180 for (i = 0; i < origbaselen + 1; i++) {
181 lcs[i] = xcalloc(lennew + 1, sizeof(int));
182 directions[i] = xcalloc(lennew + 1, sizeof(enum coalesce_direction));
183 directions[i][0] = BASE;
184 }
185 for (j = 1; j < lennew + 1; j++)
186 directions[0][j] = NEW;
187
188 for (i = 1, baseend = base; i < origbaselen + 1; i++) {
189 for (j = 1, newend = new; j < lennew + 1; j++) {
190 if (match_string_spaces(baseend->line, baseend->len,
191 newend->line, newend->len, flags)) {
192 lcs[i][j] = lcs[i - 1][j - 1] + 1;
193 directions[i][j] = MATCH;
194 } else if (lcs[i][j - 1] >= lcs[i - 1][j]) {
195 lcs[i][j] = lcs[i][j - 1];
196 directions[i][j] = NEW;
197 } else {
198 lcs[i][j] = lcs[i - 1][j];
199 directions[i][j] = BASE;
200 }
201 if (newend->next)
202 newend = newend->next;
203 }
204 if (baseend->next)
205 baseend = baseend->next;
206 }
207
208 for (i = 0; i < origbaselen + 1; i++)
209 free(lcs[i]);
210 free(lcs);
211
212 /* At this point, baseend and newend point to the end of each lists */
213 i--;
214 j--;
215 while (i != 0 || j != 0) {
216 if (directions[i][j] == MATCH) {
217 baseend->parent_map |= 1<<parent;
218 baseend = baseend->prev;
219 newend = newend->prev;
220 i--;
221 j--;
222 } else if (directions[i][j] == NEW) {
223 struct lline *lline;
224
225 lline = newend;
226 /* Remove lline from new list and update newend */
227 if (lline->prev)
228 lline->prev->next = lline->next;
229 else
230 new = lline->next;
231 if (lline->next)
232 lline->next->prev = lline->prev;
233
234 newend = lline->prev;
235 j--;
236
237 /* Add lline to base list */
238 if (baseend) {
239 lline->next = baseend->next;
240 lline->prev = baseend;
241 if (lline->prev)
242 lline->prev->next = lline;
af3feefa 243 }
99d32060
AP
244 else {
245 lline->next = base;
246 base = lline;
247 }
248 (*lenbase)++;
249
250 if (lline->next)
251 lline->next->prev = lline;
252
253 } else {
254 baseend = baseend->prev;
255 i--;
af3feefa
JH
256 }
257 }
258
99d32060
AP
259 newend = new;
260 while (newend) {
261 struct lline *lline = newend;
262 newend = newend->next;
263 free(lline);
264 }
265
266 for (i = 0; i < origbaselen + 1; i++)
267 free(directions[i]);
268 free(directions);
269
270 return base;
271}
272
273static char *grab_blob(const unsigned char *sha1, unsigned int mode,
274 unsigned long *size, struct userdiff_driver *textconv,
275 const char *path)
276{
277 char *blob;
278 enum object_type type;
279
280 if (S_ISGITLINK(mode)) {
281 blob = xmalloc(100);
282 *size = snprintf(blob, 100,
283 "Subproject commit %s\n", sha1_to_hex(sha1));
284 } else if (is_null_sha1(sha1)) {
285 /* deleted blob */
286 *size = 0;
287 return xcalloc(1, 1);
288 } else if (textconv) {
289 struct diff_filespec *df = alloc_filespec(path);
290 fill_filespec(df, sha1, 1, mode);
291 *size = fill_textconv(textconv, df, &blob);
292 free_filespec(df);
293 } else {
294 blob = read_sha1_file(sha1, &type, size);
295 if (type != OBJ_BLOB)
296 die("object '%s' is not a blob!", sha1_to_hex(sha1));
297 }
298 return blob;
299}
300
301static void append_lost(struct sline *sline, int n, const char *line, int len)
302{
303 struct lline *lline;
304 unsigned long this_mask = (1UL<<n);
305 if (line[len-1] == '\n')
306 len--;
307
af3feefa
JH
308 lline = xmalloc(sizeof(*lline) + len + 1);
309 lline->len = len;
310 lline->next = NULL;
99d32060
AP
311 lline->prev = sline->plost.lost_tail;
312 if (lline->prev)
313 lline->prev->next = lline;
314 else
315 sline->plost.lost_head = lline;
316 sline->plost.lost_tail = lline;
317 sline->plost.len++;
af3feefa
JH
318 lline->parent_map = this_mask;
319 memcpy(lline->line, line, len);
320 lline->line[len] = 0;
af3feefa
JH
321}
322
f23fc773 323struct combine_diff_state {
a0fd3146
JH
324 unsigned int lno;
325 int ob, on, nb, nn;
f23fc773
JH
326 unsigned long nmask;
327 int num_parent;
328 int n;
329 struct sline *sline;
330 struct sline *lost_bucket;
331};
332
d9ea73e0 333static void consume_line(void *state_, char *line, unsigned long len)
f23fc773 334{
d9ea73e0 335 struct combine_diff_state *state = state_;
f23fc773
JH
336 if (5 < len && !memcmp("@@ -", line, 4)) {
337 if (parse_hunk_header(line, len,
338 &state->ob, &state->on,
339 &state->nb, &state->nn))
340 return;
341 state->lno = state->nb;
b810cbbd 342 if (state->nn == 0) {
f23fc773
JH
343 /* @@ -X,Y +N,0 @@ removed Y lines
344 * that would have come *after* line N
345 * in the result. Our lost buckets hang
346 * to the line after the removed lines,
b810cbbd
JH
347 *
348 * Note that this is correct even when N == 0,
349 * in which case the hunk removes the first
350 * line in the file.
f23fc773
JH
351 */
352 state->lost_bucket = &state->sline[state->nb];
b810cbbd
JH
353 if (!state->nb)
354 state->nb = 1;
355 } else {
f23fc773 356 state->lost_bucket = &state->sline[state->nb-1];
b810cbbd 357 }
f23fc773
JH
358 if (!state->sline[state->nb-1].p_lno)
359 state->sline[state->nb-1].p_lno =
360 xcalloc(state->num_parent,
361 sizeof(unsigned long));
362 state->sline[state->nb-1].p_lno[state->n] = state->ob;
363 return;
364 }
365 if (!state->lost_bucket)
366 return; /* not in any hunk yet */
367 switch (line[0]) {
368 case '-':
99d32060 369 append_lost(state->lost_bucket, state->n, line+1, len-1);
f23fc773
JH
370 break;
371 case '+':
372 state->sline[state->lno-1].flag |= state->nmask;
373 state->lno++;
374 break;
375 }
376}
377
7dae8b21
JH
378static void combine_diff(const unsigned char *parent, unsigned int mode,
379 mmfile_t *result_file,
2386c297 380 struct sline *sline, unsigned int cnt, int n,
0508fe53
JK
381 int num_parent, int result_deleted,
382 struct userdiff_driver *textconv,
fa04ae0b 383 const char *path, long flags)
af3feefa 384{
f23fc773 385 unsigned int p_lno, lno;
f16706cc 386 unsigned long nmask = (1UL << n);
f23fc773
JH
387 xpparam_t xpp;
388 xdemitconf_t xecfg;
389 mmfile_t parent_file;
f23fc773
JH
390 struct combine_diff_state state;
391 unsigned long sz;
af3feefa 392
21798708 393 if (result_deleted)
4462731e
JH
394 return; /* result deleted */
395
0508fe53 396 parent_file.ptr = grab_blob(parent, mode, &sz, textconv, path);
f23fc773 397 parent_file.size = sz;
9ccd0a88 398 memset(&xpp, 0, sizeof(xpp));
fa04ae0b 399 xpp.flags = flags;
30b25010 400 memset(&xecfg, 0, sizeof(xecfg));
f23fc773
JH
401 memset(&state, 0, sizeof(state));
402 state.nmask = nmask;
403 state.sline = sline;
404 state.lno = 1;
405 state.num_parent = num_parent;
406 state.n = n;
407
8a3f524b 408 xdi_diff_outf(&parent_file, result_file, consume_line, &state,
dfea7900 409 &xpp, &xecfg);
f23fc773 410 free(parent_file.ptr);
f16706cc
JH
411
412 /* Assign line numbers for this parent.
413 *
414 * sline[lno].p_lno[n] records the first line number
415 * (counting from 1) for parent N if the final hunk display
416 * started by showing sline[lno] (possibly showing the lost
417 * lines attached to it first).
418 */
8a470ebf 419 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
f16706cc
JH
420 struct lline *ll;
421 sline[lno].p_lno[n] = p_lno;
422
99d32060
AP
423 /* Coalesce new lines */
424 if (sline[lno].plost.lost_head) {
425 struct sline *sl = &sline[lno];
426 sl->lost = coalesce_lines(sl->lost, &sl->lenlost,
427 sl->plost.lost_head,
428 sl->plost.len, n, flags);
429 sl->plost.lost_head = sl->plost.lost_tail = NULL;
430 sl->plost.len = 0;
431 }
432
f16706cc 433 /* How many lines would this sline advance the p_lno? */
99d32060 434 ll = sline[lno].lost;
f16706cc
JH
435 while (ll) {
436 if (ll->parent_map & nmask)
437 p_lno++; /* '-' means parent had it */
438 ll = ll->next;
439 }
8a470ebf 440 if (lno < cnt && !(sline[lno].flag & nmask))
f16706cc
JH
441 p_lno++; /* no '+' means parent had it */
442 }
443 sline[lno].p_lno[n] = p_lno; /* trailer */
af3feefa
JH
444}
445
446static unsigned long context = 3;
447static char combine_marker = '@';
448
449static int interesting(struct sline *sline, unsigned long all_mask)
450{
46dc9412
JH
451 /* If some parents lost lines here, or if we have added to
452 * some parent, it is interesting.
453 */
99d32060 454 return ((sline->flag & all_mask) || sline->lost);
af3feefa
JH
455}
456
3ec1909f
JH
457static unsigned long adjust_hunk_tail(struct sline *sline,
458 unsigned long all_mask,
459 unsigned long hunk_begin,
460 unsigned long i)
461{
46dc9412
JH
462 /* i points at the first uninteresting line. If the last line
463 * of the hunk was interesting only because it has some
464 * deletion, then it is not all that interesting for the
465 * purpose of giving trailing context lines. This is because
466 * we output '-' line and then unmodified sline[i-1] itself in
467 * that case which gives us one extra context line.
3ec1909f 468 */
46dc9412 469 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
3ec1909f
JH
470 i--;
471 return i;
472}
473
46dc9412
JH
474static unsigned long find_next(struct sline *sline,
475 unsigned long mark,
476 unsigned long i,
477 unsigned long cnt,
2386c297 478 int look_for_uninteresting)
3ec1909f 479{
46dc9412
JH
480 /* We have examined up to i-1 and are about to look at i.
481 * Find next interesting or uninteresting line. Here,
482 * "interesting" does not mean interesting(), but marked by
483 * the give_context() function below (i.e. it includes context
484 * lines that are not interesting to interesting() function
485 * that are surrounded by interesting() ones.
486 */
74065951 487 while (i <= cnt)
2386c297 488 if (look_for_uninteresting
46dc9412
JH
489 ? !(sline[i].flag & mark)
490 : (sline[i].flag & mark))
3ec1909f
JH
491 return i;
492 else
493 i++;
74065951 494 return i;
3ec1909f
JH
495}
496
497static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
af3feefa
JH
498{
499 unsigned long all_mask = (1UL<<num_parent) - 1;
500 unsigned long mark = (1UL<<num_parent);
c86fbe53 501 unsigned long no_pre_delete = (2UL<<num_parent);
af3feefa
JH
502 unsigned long i;
503
46dc9412 504 /* Two groups of interesting lines may have a short gap of
82e5a82f 505 * uninteresting lines. Connect such groups to give them a
46dc9412
JH
506 * bit of context.
507 *
508 * We first start from what the interesting() function says,
509 * and mark them with "mark", and paint context lines with the
510 * mark. So interesting() would still say false for such context
511 * lines but they are treated as "interesting" in the end.
512 */
513 i = find_next(sline, mark, 0, cnt, 0);
74065951 514 if (cnt < i)
3ec1909f
JH
515 return 0;
516
74065951 517 while (i <= cnt) {
3ec1909f
JH
518 unsigned long j = (context < i) ? (i - context) : 0;
519 unsigned long k;
46dc9412
JH
520
521 /* Paint a few lines before the first interesting line. */
aac38571
MK
522 while (j < i) {
523 if (!(sline[j].flag & mark))
524 sline[j].flag |= no_pre_delete;
525 sline[j++].flag |= mark;
526 }
3ec1909f
JH
527
528 again:
46dc9412
JH
529 /* we know up to i is to be included. where does the
530 * next uninteresting one start?
531 */
532 j = find_next(sline, mark, i, cnt, 1);
74065951 533 if (cnt < j)
3ec1909f
JH
534 break; /* the rest are all interesting */
535
536 /* lookahead context lines */
46dc9412 537 k = find_next(sline, mark, j, cnt, 0);
3ec1909f
JH
538 j = adjust_hunk_tail(sline, all_mask, i, j);
539
540 if (k < j + context) {
541 /* k is interesting and [j,k) are not, but
542 * paint them interesting because the gap is small.
543 */
544 while (j < k)
af3feefa 545 sline[j++].flag |= mark;
3ec1909f
JH
546 i = k;
547 goto again;
af3feefa 548 }
3ec1909f
JH
549
550 /* j is the first uninteresting line and there is
46dc9412
JH
551 * no overlap beyond it within context lines. Paint
552 * the trailing edge a bit.
3ec1909f
JH
553 */
554 i = k;
74065951 555 k = (j + context < cnt+1) ? j + context : cnt+1;
3ec1909f
JH
556 while (j < k)
557 sline[j++].flag |= mark;
558 }
559 return 1;
560}
561
562static int make_hunks(struct sline *sline, unsigned long cnt,
563 int num_parent, int dense)
564{
565 unsigned long all_mask = (1UL<<num_parent) - 1;
566 unsigned long mark = (1UL<<num_parent);
567 unsigned long i;
568 int has_interesting = 0;
569
74065951 570 for (i = 0; i <= cnt; i++) {
3ec1909f
JH
571 if (interesting(&sline[i], all_mask))
572 sline[i].flag |= mark;
573 else
574 sline[i].flag &= ~mark;
af3feefa 575 }
d8f4790e 576 if (!dense)
3ec1909f 577 return give_context(sline, cnt, num_parent);
d8f4790e 578
263eee29
JH
579 /* Look at each hunk, and if we have changes from only one
580 * parent, or the changes are the same from all but one
581 * parent, mark that uninteresting.
d8f4790e
JH
582 */
583 i = 0;
74065951 584 while (i <= cnt) {
3ec1909f 585 unsigned long j, hunk_begin, hunk_end;
bf1c32bd 586 unsigned long same_diff;
74065951 587 while (i <= cnt && !(sline[i].flag & mark))
d8f4790e 588 i++;
74065951 589 if (cnt < i)
d8f4790e 590 break; /* No more interesting hunks */
3ec1909f 591 hunk_begin = i;
74065951 592 for (j = i + 1; j <= cnt; j++) {
3ec1909f
JH
593 if (!(sline[j].flag & mark)) {
594 /* Look beyond the end to see if there
595 * is an interesting line after this
596 * hunk within context span.
597 */
598 unsigned long la; /* lookahead */
599 int contin = 0;
600 la = adjust_hunk_tail(sline, all_mask,
601 hunk_begin, j);
74065951
JH
602 la = (la + context < cnt + 1) ?
603 (la + context) : cnt + 1;
e5e9b565 604 while (la && j <= --la) {
3ec1909f
JH
605 if (sline[la].flag & mark) {
606 contin = 1;
607 break;
608 }
609 }
610 if (!contin)
611 break;
612 j = la;
613 }
614 }
615 hunk_end = j;
616
bf1c32bd 617 /* [i..hunk_end) are interesting. Now is it really
fd4b1d21
JH
618 * interesting? We check if there are only two versions
619 * and the result matches one of them. That is, we look
620 * at:
621 * (+) line, which records lines added to which parents;
622 * this line appears in the result.
623 * (-) line, which records from what parents the line
624 * was removed; this line does not appear in the result.
625 * then check the set of parents the result has difference
626 * from, from all lines. If there are lines that has
627 * different set of parents that the result has differences
628 * from, that means we have more than two versions.
629 *
630 * Even when we have only two versions, if the result does
631 * not match any of the parents, the it should be considered
632 * interesting. In such a case, we would have all '+' line.
633 * After passing the above "two versions" test, that would
634 * appear as "the same set of parents" to be "all parents".
d8f4790e 635 */
bf1c32bd
JH
636 same_diff = 0;
637 has_interesting = 0;
638 for (j = i; j < hunk_end && !has_interesting; j++) {
46dc9412 639 unsigned long this_diff = sline[j].flag & all_mask;
99d32060 640 struct lline *ll = sline[j].lost;
bf1c32bd
JH
641 if (this_diff) {
642 /* This has some changes. Is it the
643 * same as others?
644 */
645 if (!same_diff)
646 same_diff = this_diff;
647 else if (same_diff != this_diff) {
648 has_interesting = 1;
649 break;
650 }
651 }
652 while (ll && !has_interesting) {
653 /* Lost this line from these parents;
654 * who are they? Are they the same?
655 */
656 this_diff = ll->parent_map;
657 if (!same_diff)
658 same_diff = this_diff;
659 else if (same_diff != this_diff) {
660 has_interesting = 1;
661 }
662 ll = ll->next;
663 }
d8f4790e 664 }
bf1c32bd 665
fd4b1d21 666 if (!has_interesting && same_diff != all_mask) {
d8f4790e 667 /* This hunk is not that interesting after all */
3ec1909f 668 for (j = hunk_begin; j < hunk_end; j++)
d8f4790e
JH
669 sline[j].flag &= ~mark;
670 }
671 i = hunk_end;
672 }
3ec1909f
JH
673
674 has_interesting = give_context(sline, cnt, num_parent);
8828cdcb 675 return has_interesting;
af3feefa
JH
676}
677
3b0f5e88 678static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
f16706cc
JH
679{
680 l0 = sline[l0].p_lno[n];
681 l1 = sline[l1].p_lno[n];
3b0f5e88 682 printf(" -%lu,%lu", l0, l1-l0-null_context);
f16706cc
JH
683}
684
d5f6a01a
JH
685static int hunk_comment_line(const char *bol)
686{
7a8ac59f
JH
687 int ch;
688
689 if (!bol)
690 return 0;
691 ch = *bol & 0xff;
d5f6a01a
JH
692 return (isalpha(ch) || ch == '_' || ch == '$');
693}
694
39280970
JH
695static void show_line_to_eol(const char *line, int len, const char *reset)
696{
697 int saw_cr_at_eol = 0;
698 if (len < 0)
699 len = strlen(line);
700 saw_cr_at_eol = (len && line[len-1] == '\r');
701
702 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
703 reset,
704 saw_cr_at_eol ? "\r" : "");
705}
706
41ee2ad6
JK
707static void dump_sline(struct sline *sline, const char *line_prefix,
708 unsigned long cnt, int num_parent,
21798708 709 int use_color, int result_deleted)
af3feefa
JH
710{
711 unsigned long mark = (1UL<<num_parent);
c86fbe53 712 unsigned long no_pre_delete = (2UL<<num_parent);
af3feefa 713 int i;
f16706cc 714 unsigned long lno = 0;
567a03d1 715 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
89cb73a1 716 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
567a03d1
JH
717 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
718 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
719 const char *c_plain = diff_get_color(use_color, DIFF_PLAIN);
720 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
af3feefa 721
21798708 722 if (result_deleted)
4462731e
JH
723 return; /* result deleted */
724
af3feefa 725 while (1) {
8bc7574b
JH
726 unsigned long hunk_end;
727 unsigned long rlines;
d5f6a01a 728 const char *hunk_comment = NULL;
3b0f5e88 729 unsigned long null_context = 0;
d5f6a01a
JH
730
731 while (lno <= cnt && !(sline[lno].flag & mark)) {
732 if (hunk_comment_line(sline[lno].bol))
733 hunk_comment = sline[lno].bol;
af3feefa 734 lno++;
d5f6a01a 735 }
8a470ebf 736 if (cnt < lno)
af3feefa 737 break;
8a470ebf 738 else {
74065951 739 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
8a470ebf
JH
740 if (!(sline[hunk_end].flag & mark))
741 break;
742 }
74065951
JH
743 rlines = hunk_end - lno;
744 if (cnt < hunk_end)
745 rlines--; /* pointing at the last delete hunk */
3b0f5e88
JH
746
747 if (!context) {
748 /*
749 * Even when running with --unified=0, all
750 * lines in the hunk needs to be processed in
751 * the loop below in order to show the
752 * deletion recorded in lost_head. However,
753 * we do not want to show the resulting line
754 * with all blank context markers in such a
755 * case. Compensate.
756 */
757 unsigned long j;
758 for (j = lno; j < hunk_end; j++)
759 if (!(sline[j].flag & (mark-1)))
760 null_context++;
761 rlines -= null_context;
762 }
763
41ee2ad6 764 printf("%s%s", line_prefix, c_frag);
af3feefa 765 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
f16706cc 766 for (i = 0; i < num_parent; i++)
3b0f5e88 767 show_parent_lno(sline, lno, hunk_end, i, null_context);
74065951 768 printf(" +%lu,%lu ", lno+1, rlines);
af3feefa 769 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
d5f6a01a
JH
770
771 if (hunk_comment) {
772 int comment_end = 0;
773 for (i = 0; i < 40; i++) {
774 int ch = hunk_comment[i] & 0xff;
775 if (!ch || ch == '\n')
776 break;
777 if (!isspace(ch))
778 comment_end = i;
779 }
780 if (comment_end)
89cb73a1
BW
781 printf("%s%s %s%s", c_reset,
782 c_plain, c_reset,
783 c_func);
d5f6a01a
JH
784 for (i = 0; i < comment_end; i++)
785 putchar(hunk_comment[i]);
786 }
787
567a03d1 788 printf("%s\n", c_reset);
af3feefa
JH
789 while (lno < hunk_end) {
790 struct lline *ll;
791 int j;
46dc9412 792 unsigned long p_mask;
fd13b21f 793 struct sline *sl = &sline[lno++];
99d32060 794 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost;
af3feefa 795 while (ll) {
41ee2ad6 796 printf("%s%s", line_prefix, c_old);
af3feefa
JH
797 for (j = 0; j < num_parent; j++) {
798 if (ll->parent_map & (1UL<<j))
799 putchar('-');
800 else
801 putchar(' ');
802 }
39280970 803 show_line_to_eol(ll->line, -1, c_reset);
af3feefa
JH
804 ll = ll->next;
805 }
74065951 806 if (cnt < lno)
8a470ebf 807 break;
46dc9412 808 p_mask = 1;
41ee2ad6 809 fputs(line_prefix, stdout);
3b0f5e88
JH
810 if (!(sl->flag & (mark-1))) {
811 /*
812 * This sline was here to hang the
813 * lost lines in front of it.
814 */
815 if (!context)
816 continue;
567a03d1 817 fputs(c_plain, stdout);
3b0f5e88 818 }
567a03d1
JH
819 else
820 fputs(c_new, stdout);
af3feefa 821 for (j = 0; j < num_parent; j++) {
46dc9412 822 if (p_mask & sl->flag)
af3feefa 823 putchar('+');
46dc9412
JH
824 else
825 putchar(' ');
826 p_mask <<= 1;
af3feefa 827 }
39280970 828 show_line_to_eol(sl->bol, sl->len, c_reset);
af3feefa
JH
829 }
830 }
831}
832
3c39e9bd
JH
833static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
834 int i, int j)
835{
836 /* We have already examined parent j and we know parent i
837 * and parent j are the same, so reuse the combined result
838 * of parent j for parent i.
839 */
840 unsigned long lno, imask, jmask;
841 imask = (1UL<<i);
842 jmask = (1UL<<j);
843
8a470ebf 844 for (lno = 0; lno <= cnt; lno++) {
99d32060 845 struct lline *ll = sline->lost;
f16706cc 846 sline->p_lno[i] = sline->p_lno[j];
3c39e9bd
JH
847 while (ll) {
848 if (ll->parent_map & jmask)
849 ll->parent_map |= imask;
850 ll = ll->next;
851 }
46dc9412
JH
852 if (sline->flag & jmask)
853 sline->flag |= imask;
3c39e9bd
JH
854 sline++;
855 }
4462731e
JH
856 /* the overall size of the file (sline[cnt]) */
857 sline->p_lno[i] = sline->p_lno[j];
3c39e9bd
JH
858}
859
462a15bc
JH
860static void dump_quoted_path(const char *head,
861 const char *prefix,
862 const char *path,
41ee2ad6 863 const char *line_prefix,
567a03d1 864 const char *c_meta, const char *c_reset)
eab144ac 865{
462a15bc
JH
866 static struct strbuf buf = STRBUF_INIT;
867
868 strbuf_reset(&buf);
41ee2ad6 869 strbuf_addstr(&buf, line_prefix);
462a15bc
JH
870 strbuf_addstr(&buf, c_meta);
871 strbuf_addstr(&buf, head);
d5625091 872 quote_two_c_style(&buf, prefix, path, 0);
462a15bc
JH
873 strbuf_addstr(&buf, c_reset);
874 puts(buf.buf);
eab144ac
LT
875}
876
7c978a06
JK
877static void show_combined_header(struct combine_diff_path *elem,
878 int num_parent,
879 int dense,
880 struct rev_info *rev,
41ee2ad6 881 const char *line_prefix,
4d5f3471
JK
882 int mode_differs,
883 int show_file_header)
7c978a06
JK
884{
885 struct diff_options *opt = &rev->diffopt;
886 int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
887 const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
888 const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
f1c96261
JK
889 const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
890 const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
7c978a06
JK
891 const char *abb;
892 int added = 0;
893 int deleted = 0;
894 int i;
895
896 if (rev->loginfo && !rev->no_commit_id)
897 show_log(rev);
898
899 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
41ee2ad6
JK
900 "", elem->path, line_prefix, c_meta, c_reset);
901 printf("%s%sindex ", line_prefix, c_meta);
7c978a06
JK
902 for (i = 0; i < num_parent; i++) {
903 abb = find_unique_abbrev(elem->parent[i].sha1,
904 abbrev);
905 printf("%s%s", i ? "," : "", abb);
906 }
907 abb = find_unique_abbrev(elem->sha1, abbrev);
908 printf("..%s%s\n", abb, c_reset);
909
910 if (mode_differs) {
911 deleted = !elem->mode;
912
913 /* We say it was added if nobody had it */
914 added = !deleted;
915 for (i = 0; added && i < num_parent; i++)
916 if (elem->parent[i].status !=
917 DIFF_STATUS_ADDED)
918 added = 0;
919 if (added)
41ee2ad6
JK
920 printf("%s%snew file mode %06o",
921 line_prefix, c_meta, elem->mode);
7c978a06
JK
922 else {
923 if (deleted)
41ee2ad6
JK
924 printf("%s%sdeleted file ",
925 line_prefix, c_meta);
7c978a06
JK
926 printf("mode ");
927 for (i = 0; i < num_parent; i++) {
928 printf("%s%06o", i ? "," : "",
929 elem->parent[i].mode);
930 }
931 if (elem->mode)
932 printf("..%06o", elem->mode);
933 }
934 printf("%s\n", c_reset);
935 }
936
4d5f3471
JK
937 if (!show_file_header)
938 return;
939
7c978a06
JK
940 if (added)
941 dump_quoted_path("--- ", "", "/dev/null",
41ee2ad6 942 line_prefix, c_meta, c_reset);
7c978a06
JK
943 else
944 dump_quoted_path("--- ", a_prefix, elem->path,
41ee2ad6 945 line_prefix, c_meta, c_reset);
7c978a06
JK
946 if (deleted)
947 dump_quoted_path("+++ ", "", "/dev/null",
41ee2ad6 948 line_prefix, c_meta, c_reset);
7c978a06
JK
949 else
950 dump_quoted_path("+++ ", b_prefix, elem->path,
41ee2ad6 951 line_prefix, c_meta, c_reset);
7c978a06
JK
952}
953
89b0c4b5 954static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
99694544
JH
955 int dense, int working_tree_file,
956 struct rev_info *rev)
af3feefa 957{
91539833 958 struct diff_options *opt = &rev->diffopt;
f23fc773 959 unsigned long result_size, cnt, lno;
21798708 960 int result_deleted = 0;
310f8b5b 961 char *result, *cp;
af3feefa 962 struct sline *sline; /* survived lines */
2454c962 963 int mode_differs = 0;
89b0c4b5 964 int i, show_hunks;
f23fc773 965 mmfile_t result_file;
4d5f3471 966 struct userdiff_driver *userdiff;
0508fe53 967 struct userdiff_driver *textconv = NULL;
4d5f3471 968 int is_binary;
41ee2ad6 969 const char *line_prefix = diff_line_prefix(opt);
af3feefa 970
ee1e5412 971 context = opt->context;
4d5f3471
JK
972 userdiff = userdiff_find_by_path(elem->path);
973 if (!userdiff)
974 userdiff = userdiff_find_by_name("default");
0508fe53
JK
975 if (DIFF_OPT_TST(opt, ALLOW_TEXTCONV))
976 textconv = userdiff_get_textconv(userdiff);
a5a818ee 977
af3feefa 978 /* Read the result of merge first */
f23fc773 979 if (!working_tree_file)
0508fe53
JK
980 result = grab_blob(elem->sha1, elem->mode, &result_size,
981 textconv, elem->path);
ea726d02 982 else {
9843a1f6 983 /* Used by diff-tree to read from the working tree */
ea726d02 984 struct stat st;
4fc970c4
JH
985 int fd = -1;
986
987 if (lstat(elem->path, &st) < 0)
988 goto deleted_file;
989
990 if (S_ISLNK(st.st_mode)) {
912342d9
JH
991 struct strbuf buf = STRBUF_INIT;
992
993 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
4fc970c4
JH
994 error("readlink(%s): %s", elem->path,
995 strerror(errno));
996 return;
997 }
912342d9
JH
998 result_size = buf.len;
999 result = strbuf_detach(&buf, NULL);
4fc970c4 1000 elem->mode = canon_mode(st.st_mode);
7dae8b21
JH
1001 } else if (S_ISDIR(st.st_mode)) {
1002 unsigned char sha1[20];
1003 if (resolve_gitlink_ref(elem->path, "HEAD", sha1) < 0)
0508fe53
JK
1004 result = grab_blob(elem->sha1, elem->mode,
1005 &result_size, NULL, NULL);
7dae8b21 1006 else
0508fe53
JK
1007 result = grab_blob(sha1, elem->mode,
1008 &result_size, NULL, NULL);
1009 } else if (textconv) {
1010 struct diff_filespec *df = alloc_filespec(elem->path);
e5450100 1011 fill_filespec(df, null_sha1, 0, st.st_mode);
0508fe53
JK
1012 result_size = fill_textconv(textconv, df, &result);
1013 free_filespec(df);
91fcbcbd 1014 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
dc49cd76 1015 size_t len = xsize_t(st.st_size);
c697ad14 1016 ssize_t done;
a249a9b5 1017 int is_file, i;
ea726d02 1018
1b0c7174 1019 elem->mode = canon_mode(st.st_mode);
a249a9b5
JS
1020 /* if symlinks don't work, assume symlink if all parents
1021 * are symlinks
1022 */
1023 is_file = has_symlinks;
1024 for (i = 0; !is_file && i < num_parent; i++)
1025 is_file = !S_ISLNK(elem->parent[i].mode);
1026 if (!is_file)
1027 elem->mode = canon_mode(S_IFLNK);
1028
f23fc773 1029 result_size = len;
ea726d02 1030 result = xmalloc(len + 1);
c697ad14
HO
1031
1032 done = read_in_full(fd, result, len);
1033 if (done < 0)
0721c314 1034 die_errno("read error '%s'", elem->path);
c697ad14
HO
1035 else if (done < len)
1036 die("early EOF '%s'", elem->path);
1037
ea726d02 1038 result[len] = 0;
5e568f9e
AG
1039
1040 /* If not a fake symlink, apply filters, e.g. autocrlf */
1041 if (is_file) {
f285a2d7 1042 struct strbuf buf = STRBUF_INIT;
5e568f9e 1043
5e568f9e
AG
1044 if (convert_to_git(elem->path, result, len, &buf, safe_crlf)) {
1045 free(result);
1046 result = strbuf_detach(&buf, &len);
1047 result_size = len;
1048 }
1049 }
ea726d02
JH
1050 }
1051 else {
4fc970c4 1052 deleted_file:
21798708 1053 result_deleted = 1;
f23fc773 1054 result_size = 0;
713a11fc 1055 elem->mode = 0;
28f75818 1056 result = xcalloc(1, 1);
ea726d02 1057 }
4fc970c4 1058
ea726d02
JH
1059 if (0 <= fd)
1060 close(fd);
1061 }
af3feefa 1062
c95b99bb
JK
1063 for (i = 0; i < num_parent; i++) {
1064 if (elem->parent[i].mode != elem->mode) {
1065 mode_differs = 1;
1066 break;
1067 }
1068 }
1069
0508fe53
JK
1070 if (textconv)
1071 is_binary = 0;
1072 else if (userdiff->binary != -1)
4d5f3471
JK
1073 is_binary = userdiff->binary;
1074 else {
1075 is_binary = buffer_is_binary(result, result_size);
1076 for (i = 0; !is_binary && i < num_parent; i++) {
1077 char *buf;
1078 unsigned long size;
1079 buf = grab_blob(elem->parent[i].sha1,
1080 elem->parent[i].mode,
0508fe53 1081 &size, NULL, NULL);
4d5f3471
JK
1082 if (buffer_is_binary(buf, size))
1083 is_binary = 1;
1084 free(buf);
1085 }
1086 }
1087 if (is_binary) {
1088 show_combined_header(elem, num_parent, dense, rev,
41ee2ad6 1089 line_prefix, mode_differs, 0);
4d5f3471
JK
1090 printf("Binary files differ\n");
1091 free(result);
1092 return;
1093 }
1094
2386c297 1095 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
af3feefa
JH
1096 if (*cp == '\n')
1097 cnt++;
1098 }
f23fc773 1099 if (result_size && result[result_size-1] != '\n')
af3feefa
JH
1100 cnt++; /* incomplete line */
1101
8a470ebf 1102 sline = xcalloc(cnt+2, sizeof(*sline));
af3feefa 1103 sline[0].bol = result;
2386c297 1104 for (lno = 0, cp = result; cp < result + result_size; cp++) {
af3feefa
JH
1105 if (*cp == '\n') {
1106 sline[lno].len = cp - sline[lno].bol;
af3feefa
JH
1107 lno++;
1108 if (lno < cnt)
1109 sline[lno].bol = cp + 1;
1110 }
1111 }
f23fc773
JH
1112 if (result_size && result[result_size-1] != '\n')
1113 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
1114
1115 result_file.ptr = result;
1116 result_file.size = result_size;
af3feefa 1117
8a470ebf
JH
1118 /* Even p_lno[cnt+1] is valid -- that is for the end line number
1119 * for deletion hunk at the end.
1120 */
1121 sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
1122 for (lno = 0; lno <= cnt; lno++)
f16706cc
JH
1123 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
1124
3c39e9bd
JH
1125 for (i = 0; i < num_parent; i++) {
1126 int j;
1127 for (j = 0; j < i; j++) {
a89fccd2
DR
1128 if (!hashcmp(elem->parent[i].sha1,
1129 elem->parent[j].sha1)) {
3c39e9bd
JH
1130 reuse_combine_diff(sline, cnt, i, j);
1131 break;
1132 }
1133 }
1134 if (i <= j)
7dae8b21
JH
1135 combine_diff(elem->parent[i].sha1,
1136 elem->parent[i].mode,
1137 &result_file, sline,
0508fe53 1138 cnt, i, num_parent, result_deleted,
fa04ae0b 1139 textconv, elem->path, opt->xdl_opts);
3c39e9bd 1140 }
af3feefa 1141
8828cdcb 1142 show_hunks = make_hunks(sline, cnt, num_parent, dense);
af3feefa 1143
713a11fc 1144 if (show_hunks || mode_differs || working_tree_file) {
7c978a06 1145 show_combined_header(elem, num_parent, dense, rev,
41ee2ad6
JK
1146 line_prefix, mode_differs, 1);
1147 dump_sline(sline, line_prefix, cnt, num_parent,
f1c96261 1148 opt->use_color, result_deleted);
8828cdcb 1149 }
af3feefa
JH
1150 free(result);
1151
2386c297 1152 for (lno = 0; lno < cnt; lno++) {
99d32060
AP
1153 if (sline[lno].lost) {
1154 struct lline *ll = sline[lno].lost;
af3feefa
JH
1155 while (ll) {
1156 struct lline *tmp = ll;
1157 ll = ll->next;
1158 free(tmp);
1159 }
1160 }
1161 }
46dc9412 1162 free(sline[0].p_lno);
af3feefa
JH
1163 free(sline);
1164}
1165
91539833 1166static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
ee638024 1167{
91539833 1168 struct diff_options *opt = &rev->diffopt;
77667051 1169 int line_termination, inter_name_termination, i;
41ee2ad6 1170 const char *line_prefix = diff_line_prefix(opt);
ee638024
LT
1171
1172 line_termination = opt->line_termination;
1173 inter_name_termination = '\t';
1174 if (!line_termination)
1175 inter_name_termination = 0;
1176
44152787 1177 if (rev->loginfo && !rev->no_commit_id)
02865655 1178 show_log(rev);
ee638024 1179
a1d68bea 1180
c6744349 1181 if (opt->output_format & DIFF_FORMAT_RAW) {
41ee2ad6 1182 printf("%s", line_prefix);
a1d68bea 1183
77667051
JH
1184 /* As many colons as there are parents */
1185 for (i = 0; i < num_parent; i++)
1186 putchar(':');
0a798076
JH
1187
1188 /* Show the modes */
77667051
JH
1189 for (i = 0; i < num_parent; i++)
1190 printf("%06o ", p->parent[i].mode);
1191 printf("%06o", p->mode);
0a798076
JH
1192
1193 /* Show sha1's */
1194 for (i = 0; i < num_parent; i++)
1195 printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
1196 opt->abbrev));
1197 printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
1198 }
1199
c6744349 1200 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
d416df88
JH
1201 for (i = 0; i < num_parent; i++)
1202 putchar(p->parent[i].status);
1203 putchar(inter_name_termination);
1204 }
0a798076 1205
663af342 1206 write_name_quoted(p->path, stdout, line_termination);
0a798076
JH
1207}
1208
99694544
JH
1209/*
1210 * The result (p->elem) is from the working tree and their
1211 * parents are typically from multiple stages during a merge
1212 * (i.e. diff-files) or the state in HEAD and in the index
1213 * (i.e. diff-index).
1214 */
91539833 1215void show_combined_diff(struct combine_diff_path *p,
0a798076
JH
1216 int num_parent,
1217 int dense,
91539833 1218 struct rev_info *rev)
0a798076 1219{
91539833 1220 struct diff_options *opt = &rev->diffopt;
41ee2ad6 1221
0a798076 1222 if (!p->len)
91539833 1223 return;
c6744349
TH
1224 if (opt->output_format & (DIFF_FORMAT_RAW |
1225 DIFF_FORMAT_NAME |
89b0c4b5 1226 DIFF_FORMAT_NAME_STATUS))
91539833 1227 show_raw_diff(p, num_parent, rev);
89b0c4b5 1228 else if (opt->output_format & DIFF_FORMAT_PATCH)
99694544 1229 show_patch_diff(p, num_parent, dense, 1, rev);
ee638024
LT
1230}
1231
25e5e2bf
JH
1232static void free_combined_pair(struct diff_filepair *pair)
1233{
1234 free(pair->two);
1235 free(pair);
1236}
1237
1238/*
1239 * A combine_diff_path expresses N parents on the LHS against 1 merge
1240 * result. Synthesize a diff_filepair that has N entries on the "one"
1241 * side and 1 entry on the "two" side.
1242 *
1243 * In the future, we might want to add more data to combine_diff_path
1244 * so that we can fill fields we are ignoring (most notably, size) here,
1245 * but currently nobody uses it, so this should suffice for now.
1246 */
1247static struct diff_filepair *combined_pair(struct combine_diff_path *p,
1248 int num_parent)
1249{
1250 int i;
1251 struct diff_filepair *pair;
1252 struct diff_filespec *pool;
1253
1254 pair = xmalloc(sizeof(*pair));
1255 pool = xcalloc(num_parent + 1, sizeof(struct diff_filespec));
1256 pair->one = pool + 1;
1257 pair->two = pool;
1258
1259 for (i = 0; i < num_parent; i++) {
1260 pair->one[i].path = p->path;
1261 pair->one[i].mode = p->parent[i].mode;
1262 hashcpy(pair->one[i].sha1, p->parent[i].sha1);
1263 pair->one[i].sha1_valid = !is_null_sha1(p->parent[i].sha1);
1264 pair->one[i].has_more_entries = 1;
1265 }
1266 pair->one[num_parent - 1].has_more_entries = 0;
1267
1268 pair->two->path = p->path;
1269 pair->two->mode = p->mode;
1270 hashcpy(pair->two->sha1, p->sha1);
1271 pair->two->sha1_valid = !is_null_sha1(p->sha1);
1272 return pair;
1273}
1274
1275static void handle_combined_callback(struct diff_options *opt,
1276 struct combine_diff_path *paths,
1277 int num_parent,
1278 int num_paths)
1279{
1280 struct combine_diff_path *p;
1281 struct diff_queue_struct q;
1282 int i;
1283
1284 q.queue = xcalloc(num_paths, sizeof(struct diff_filepair *));
1285 q.alloc = num_paths;
1286 q.nr = num_paths;
1287 for (i = 0, p = paths; p; p = p->next) {
1288 if (!p->len)
1289 continue;
1290 q.queue[i++] = combined_pair(p, num_parent);
1291 }
1292 opt->format_callback(&q, opt, opt->format_callback_data);
1293 for (i = 0; i < num_paths; i++)
1294 free_combined_pair(q.queue[i]);
1295 free(q.queue);
1296}
1297
0fe7c1de 1298void diff_tree_combined(const unsigned char *sha1,
0041f09d 1299 const struct sha1_array *parents,
0fe7c1de
JH
1300 int dense,
1301 struct rev_info *rev)
af3feefa 1302{
91539833 1303 struct diff_options *opt = &rev->diffopt;
af3feefa 1304 struct diff_options diffopts;
ea726d02 1305 struct combine_diff_path *p, *paths = NULL;
0041f09d 1306 int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
af3feefa 1307
5b236832 1308 diffopts = *opt;
bd1928df 1309 copy_pathspec(&diffopts.pathspec, &opt->pathspec);
3969cf7d 1310 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
8f67f8ae
PH
1311 DIFF_OPT_SET(&diffopts, RECURSIVE);
1312 DIFF_OPT_CLR(&diffopts, ALLOW_EXTERNAL);
af3feefa 1313
44152787 1314 show_log_first = !!rev->loginfo && !rev->no_commit_id;
3969cf7d 1315 needsep = 0;
af3feefa 1316 /* find set of paths that everybody touches */
0fe7c1de 1317 for (i = 0; i < num_parent; i++) {
965f803c
JH
1318 /* show stat against the first parent even
1319 * when doing combined diff.
1320 */
74e2abe5
JH
1321 int stat_opt = (opt->output_format &
1322 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
1323 if (i == 0 && stat_opt)
1324 diffopts.output_format = stat_opt;
965f803c
JH
1325 else
1326 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
0041f09d 1327 diff_tree_sha1(parents->sha1[i], sha1, "", &diffopts);
5b236832 1328 diffcore_std(&diffopts);
af3feefa 1329 paths = intersect_paths(paths, i, num_parent);
eab144ac 1330
3969cf7d 1331 if (show_log_first && i == 0) {
02865655 1332 show_log(rev);
41ee2ad6 1333
3969cf7d 1334 if (rev->verbose_header && opt->output_format)
41ee2ad6
JK
1335 printf("%s%c", diff_line_prefix(opt),
1336 opt->line_termination);
3969cf7d 1337 }
af3feefa
JH
1338 diff_flush(&diffopts);
1339 }
1340
1341 /* find out surviving paths */
1342 for (num_paths = 0, p = paths; p; p = p->next) {
1343 if (p->len)
1344 num_paths++;
1345 }
e3c3a550 1346 if (num_paths) {
c6744349
TH
1347 if (opt->output_format & (DIFF_FORMAT_RAW |
1348 DIFF_FORMAT_NAME |
1349 DIFF_FORMAT_NAME_STATUS)) {
86ff1d20 1350 for (p = paths; p; p = p->next) {
c6744349
TH
1351 if (p->len)
1352 show_raw_diff(p, num_parent, rev);
86ff1d20 1353 }
3969cf7d 1354 needsep = 1;
86ff1d20 1355 }
74e2abe5
JH
1356 else if (opt->output_format &
1357 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
3969cf7d 1358 needsep = 1;
25e5e2bf
JH
1359 else if (opt->output_format & DIFF_FORMAT_CALLBACK)
1360 handle_combined_callback(opt, paths, num_parent, num_paths);
1361
c6744349 1362 if (opt->output_format & DIFF_FORMAT_PATCH) {
3969cf7d 1363 if (needsep)
41ee2ad6
JK
1364 printf("%s%c", diff_line_prefix(opt),
1365 opt->line_termination);
c6744349
TH
1366 for (p = paths; p; p = p->next) {
1367 if (p->len)
1368 show_patch_diff(p, num_parent, dense,
99694544 1369 0, rev);
c6744349 1370 }
af3feefa
JH
1371 }
1372 }
1373
1374 /* Clean things up */
1375 while (paths) {
ea726d02 1376 struct combine_diff_path *tmp = paths;
af3feefa
JH
1377 paths = paths->next;
1378 free(tmp);
1379 }
46ec510a 1380
bd1928df 1381 free_pathspec(&diffopts.pathspec);
af3feefa 1382}
0fe7c1de 1383
82889295
RS
1384void diff_tree_combined_merge(const struct commit *commit, int dense,
1385 struct rev_info *rev)
0fe7c1de 1386{
53d00b39 1387 struct commit_list *parent = get_saved_parents(rev, commit);
0041f09d
RS
1388 struct sha1_array parents = SHA1_ARRAY_INIT;
1389
1390 while (parent) {
1391 sha1_array_append(&parents, parent->item->object.sha1);
1392 parent = parent->next;
1393 }
82889295 1394 diff_tree_combined(commit->object.sha1, &parents, dense, rev);
0041f09d 1395 sha1_array_clear(&parents);
0fe7c1de 1396}