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