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