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