filter-branch: Export variable `workdir' for --commit-filter
[git/git.git] / combine-diff.c
CommitLineData
af3feefa
JH
1#include "cache.h"
2#include "commit.h"
8e440259 3#include "blob.h"
af3feefa
JH
4#include "diff.h"
5#include "diffcore.h"
6#include "quote.h"
d9ea73e0 7#include "xdiff-interface.h"
91539833 8#include "log-tree.h"
7dae8b21 9#include "refs.h"
af3feefa 10
ea726d02 11static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
af3feefa
JH
12{
13 struct diff_queue_struct *q = &diff_queued_diff;
ea726d02 14 struct combine_diff_path *p;
af3feefa
JH
15 int i;
16
17 if (!n) {
ea726d02 18 struct combine_diff_path *list = NULL, **tail = &list;
af3feefa
JH
19 for (i = 0; i < q->nr; i++) {
20 int len;
21 const char *path;
a976b0a5 22 if (diff_unmodified_pair(q->queue[i]))
af3feefa
JH
23 continue;
24 path = q->queue[i]->two->path;
25 len = strlen(path);
2454c962 26 p = xmalloc(combine_diff_path_size(num_parent, len));
4b25d091 27 p->path = (char *) &(p->parent[num_parent]);
af3feefa
JH
28 memcpy(p->path, path, len);
29 p->path[len] = 0;
30 p->len = len;
31 p->next = NULL;
2454c962
JH
32 memset(p->parent, 0,
33 sizeof(p->parent[0]) * num_parent);
34
e702496e 35 hashcpy(p->sha1, q->queue[i]->two->sha1);
2454c962 36 p->mode = q->queue[i]->two->mode;
e702496e 37 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
2454c962 38 p->parent[n].mode = q->queue[i]->one->mode;
d416df88 39 p->parent[n].status = q->queue[i]->status;
5290a0f8
JH
40 *tail = p;
41 tail = &p->next;
af3feefa
JH
42 }
43 return list;
44 }
45
46 for (p = curr; p; p = p->next) {
47 int found = 0;
48 if (!p->len)
49 continue;
50 for (i = 0; i < q->nr; i++) {
51 const char *path;
52 int len;
53
a976b0a5 54 if (diff_unmodified_pair(q->queue[i]))
af3feefa
JH
55 continue;
56 path = q->queue[i]->two->path;
57 len = strlen(path);
58 if (len == p->len && !memcmp(path, p->path, len)) {
59 found = 1;
e702496e 60 hashcpy(p->parent[n].sha1, q->queue[i]->one->sha1);
2454c962 61 p->parent[n].mode = q->queue[i]->one->mode;
d416df88 62 p->parent[n].status = q->queue[i]->status;
af3feefa
JH
63 break;
64 }
65 }
66 if (!found)
67 p->len = 0;
68 }
69 return curr;
70}
71
b469d8b6 72/* Lines lost from parent */
af3feefa
JH
73struct lline {
74 struct lline *next;
75 int len;
76 unsigned long parent_map;
77 char line[FLEX_ARRAY];
78};
79
b469d8b6 80/* Lines surviving in the merge result */
af3feefa
JH
81struct sline {
82 struct lline *lost_head, **lost_tail;
55d5d5ba 83 struct lline *next_lost;
af3feefa
JH
84 char *bol;
85 int len;
46dc9412
JH
86 /* bit 0 up to (N-1) are on if the parent has this line (i.e.
87 * we did not change it).
b469d8b6 88 * bit N is used for "interesting" lines, including context.
c86fbe53 89 * bit (N+1) is used for "do not show deletion before this".
b469d8b6 90 */
af3feefa 91 unsigned long flag;
f16706cc 92 unsigned long *p_lno;
af3feefa
JH
93};
94
7dae8b21 95static char *grab_blob(const unsigned char *sha1, unsigned int mode, unsigned long *size)
af3feefa
JH
96{
97 char *blob;
21666f1a 98 enum object_type type;
7dae8b21
JH
99
100 if (S_ISGITLINK(mode)) {
101 blob = xmalloc(100);
102 *size = snprintf(blob, 100,
103 "Subproject commit %s\n", sha1_to_hex(sha1));
104 } else if (is_null_sha1(sha1)) {
af3feefa
JH
105 /* deleted blob */
106 *size = 0;
107 return xcalloc(1, 1);
7dae8b21
JH
108 } else {
109 blob = read_sha1_file(sha1, &type, size);
110 if (type != OBJ_BLOB)
111 die("object '%s' is not a blob!", sha1_to_hex(sha1));
af3feefa 112 }
af3feefa
JH
113 return blob;
114}
115
f23fc773 116static void append_lost(struct sline *sline, int n, const char *line, int len)
af3feefa
JH
117{
118 struct lline *lline;
af3feefa
JH
119 unsigned long this_mask = (1UL<<n);
120 if (line[len-1] == '\n')
121 len--;
122
123 /* Check to see if we can squash things */
124 if (sline->lost_head) {
55d5d5ba 125 lline = sline->next_lost;
af3feefa
JH
126 while (lline) {
127 if (lline->len == len &&
128 !memcmp(lline->line, line, len)) {
129 lline->parent_map |= this_mask;
55d5d5ba 130 sline->next_lost = lline->next;
af3feefa
JH
131 return;
132 }
133 lline = lline->next;
134 }
135 }
136
137 lline = xmalloc(sizeof(*lline) + len + 1);
138 lline->len = len;
139 lline->next = NULL;
140 lline->parent_map = this_mask;
141 memcpy(lline->line, line, len);
142 lline->line[len] = 0;
5290a0f8 143 *sline->lost_tail = lline;
af3feefa 144 sline->lost_tail = &lline->next;
55d5d5ba 145 sline->next_lost = NULL;
af3feefa
JH
146}
147
f23fc773 148struct combine_diff_state {
a0fd3146
JH
149 unsigned int lno;
150 int ob, on, nb, nn;
f23fc773
JH
151 unsigned long nmask;
152 int num_parent;
153 int n;
154 struct sline *sline;
155 struct sline *lost_bucket;
156};
157
d9ea73e0 158static void consume_line(void *state_, char *line, unsigned long len)
f23fc773 159{
d9ea73e0 160 struct combine_diff_state *state = state_;
f23fc773
JH
161 if (5 < len && !memcmp("@@ -", line, 4)) {
162 if (parse_hunk_header(line, len,
163 &state->ob, &state->on,
164 &state->nb, &state->nn))
165 return;
166 state->lno = state->nb;
b810cbbd 167 if (state->nn == 0) {
f23fc773
JH
168 /* @@ -X,Y +N,0 @@ removed Y lines
169 * that would have come *after* line N
170 * in the result. Our lost buckets hang
171 * to the line after the removed lines,
b810cbbd
JH
172 *
173 * Note that this is correct even when N == 0,
174 * in which case the hunk removes the first
175 * line in the file.
f23fc773
JH
176 */
177 state->lost_bucket = &state->sline[state->nb];
b810cbbd
JH
178 if (!state->nb)
179 state->nb = 1;
180 } else {
f23fc773 181 state->lost_bucket = &state->sline[state->nb-1];
b810cbbd 182 }
f23fc773
JH
183 if (!state->sline[state->nb-1].p_lno)
184 state->sline[state->nb-1].p_lno =
185 xcalloc(state->num_parent,
186 sizeof(unsigned long));
187 state->sline[state->nb-1].p_lno[state->n] = state->ob;
55d5d5ba 188 state->lost_bucket->next_lost = state->lost_bucket->lost_head;
f23fc773
JH
189 return;
190 }
191 if (!state->lost_bucket)
192 return; /* not in any hunk yet */
193 switch (line[0]) {
194 case '-':
195 append_lost(state->lost_bucket, state->n, line+1, len-1);
196 break;
197 case '+':
198 state->sline[state->lno-1].flag |= state->nmask;
199 state->lno++;
200 break;
201 }
202}
203
7dae8b21
JH
204static void combine_diff(const unsigned char *parent, unsigned int mode,
205 mmfile_t *result_file,
2386c297 206 struct sline *sline, unsigned int cnt, int n,
21798708 207 int num_parent, int result_deleted)
af3feefa 208{
f23fc773 209 unsigned int p_lno, lno;
f16706cc 210 unsigned long nmask = (1UL << n);
f23fc773
JH
211 xpparam_t xpp;
212 xdemitconf_t xecfg;
213 mmfile_t parent_file;
f23fc773
JH
214 struct combine_diff_state state;
215 unsigned long sz;
af3feefa 216
21798708 217 if (result_deleted)
4462731e
JH
218 return; /* result deleted */
219
7dae8b21 220 parent_file.ptr = grab_blob(parent, mode, &sz);
f23fc773 221 parent_file.size = sz;
9ccd0a88 222 memset(&xpp, 0, sizeof(xpp));
582aa00b 223 xpp.flags = 0;
30b25010 224 memset(&xecfg, 0, sizeof(xecfg));
f23fc773
JH
225 memset(&state, 0, sizeof(state));
226 state.nmask = nmask;
227 state.sline = sline;
228 state.lno = 1;
229 state.num_parent = num_parent;
230 state.n = n;
231
8a3f524b 232 xdi_diff_outf(&parent_file, result_file, consume_line, &state,
dfea7900 233 &xpp, &xecfg);
f23fc773 234 free(parent_file.ptr);
f16706cc
JH
235
236 /* Assign line numbers for this parent.
237 *
238 * sline[lno].p_lno[n] records the first line number
239 * (counting from 1) for parent N if the final hunk display
240 * started by showing sline[lno] (possibly showing the lost
241 * lines attached to it first).
242 */
8a470ebf 243 for (lno = 0, p_lno = 1; lno <= cnt; lno++) {
f16706cc
JH
244 struct lline *ll;
245 sline[lno].p_lno[n] = p_lno;
246
247 /* How many lines would this sline advance the p_lno? */
248 ll = sline[lno].lost_head;
249 while (ll) {
250 if (ll->parent_map & nmask)
251 p_lno++; /* '-' means parent had it */
252 ll = ll->next;
253 }
8a470ebf 254 if (lno < cnt && !(sline[lno].flag & nmask))
f16706cc
JH
255 p_lno++; /* no '+' means parent had it */
256 }
257 sline[lno].p_lno[n] = p_lno; /* trailer */
af3feefa
JH
258}
259
260static unsigned long context = 3;
261static char combine_marker = '@';
262
263static int interesting(struct sline *sline, unsigned long all_mask)
264{
46dc9412
JH
265 /* If some parents lost lines here, or if we have added to
266 * some parent, it is interesting.
267 */
268 return ((sline->flag & all_mask) || sline->lost_head);
af3feefa
JH
269}
270
3ec1909f
JH
271static unsigned long adjust_hunk_tail(struct sline *sline,
272 unsigned long all_mask,
273 unsigned long hunk_begin,
274 unsigned long i)
275{
46dc9412
JH
276 /* i points at the first uninteresting line. If the last line
277 * of the hunk was interesting only because it has some
278 * deletion, then it is not all that interesting for the
279 * purpose of giving trailing context lines. This is because
280 * we output '-' line and then unmodified sline[i-1] itself in
281 * that case which gives us one extra context line.
3ec1909f 282 */
46dc9412 283 if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask))
3ec1909f
JH
284 i--;
285 return i;
286}
287
46dc9412
JH
288static unsigned long find_next(struct sline *sline,
289 unsigned long mark,
290 unsigned long i,
291 unsigned long cnt,
2386c297 292 int look_for_uninteresting)
3ec1909f 293{
46dc9412
JH
294 /* We have examined up to i-1 and are about to look at i.
295 * Find next interesting or uninteresting line. Here,
296 * "interesting" does not mean interesting(), but marked by
297 * the give_context() function below (i.e. it includes context
298 * lines that are not interesting to interesting() function
299 * that are surrounded by interesting() ones.
300 */
74065951 301 while (i <= cnt)
2386c297 302 if (look_for_uninteresting
46dc9412
JH
303 ? !(sline[i].flag & mark)
304 : (sline[i].flag & mark))
3ec1909f
JH
305 return i;
306 else
307 i++;
74065951 308 return i;
3ec1909f
JH
309}
310
311static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
af3feefa
JH
312{
313 unsigned long all_mask = (1UL<<num_parent) - 1;
314 unsigned long mark = (1UL<<num_parent);
c86fbe53 315 unsigned long no_pre_delete = (2UL<<num_parent);
af3feefa
JH
316 unsigned long i;
317
46dc9412 318 /* Two groups of interesting lines may have a short gap of
82e5a82f 319 * uninteresting lines. Connect such groups to give them a
46dc9412
JH
320 * bit of context.
321 *
322 * We first start from what the interesting() function says,
323 * and mark them with "mark", and paint context lines with the
324 * mark. So interesting() would still say false for such context
325 * lines but they are treated as "interesting" in the end.
326 */
327 i = find_next(sline, mark, 0, cnt, 0);
74065951 328 if (cnt < i)
3ec1909f
JH
329 return 0;
330
74065951 331 while (i <= cnt) {
3ec1909f
JH
332 unsigned long j = (context < i) ? (i - context) : 0;
333 unsigned long k;
46dc9412
JH
334
335 /* Paint a few lines before the first interesting line. */
3ec1909f 336 while (j < i)
c86fbe53 337 sline[j++].flag |= mark | no_pre_delete;
3ec1909f
JH
338
339 again:
46dc9412
JH
340 /* we know up to i is to be included. where does the
341 * next uninteresting one start?
342 */
343 j = find_next(sline, mark, i, cnt, 1);
74065951 344 if (cnt < j)
3ec1909f
JH
345 break; /* the rest are all interesting */
346
347 /* lookahead context lines */
46dc9412 348 k = find_next(sline, mark, j, cnt, 0);
3ec1909f
JH
349 j = adjust_hunk_tail(sline, all_mask, i, j);
350
351 if (k < j + context) {
352 /* k is interesting and [j,k) are not, but
353 * paint them interesting because the gap is small.
354 */
355 while (j < k)
af3feefa 356 sline[j++].flag |= mark;
3ec1909f
JH
357 i = k;
358 goto again;
af3feefa 359 }
3ec1909f
JH
360
361 /* j is the first uninteresting line and there is
46dc9412
JH
362 * no overlap beyond it within context lines. Paint
363 * the trailing edge a bit.
3ec1909f
JH
364 */
365 i = k;
74065951 366 k = (j + context < cnt+1) ? j + context : cnt+1;
3ec1909f
JH
367 while (j < k)
368 sline[j++].flag |= mark;
369 }
370 return 1;
371}
372
373static int make_hunks(struct sline *sline, unsigned long cnt,
374 int num_parent, int dense)
375{
376 unsigned long all_mask = (1UL<<num_parent) - 1;
377 unsigned long mark = (1UL<<num_parent);
378 unsigned long i;
379 int has_interesting = 0;
380
74065951 381 for (i = 0; i <= cnt; i++) {
3ec1909f
JH
382 if (interesting(&sline[i], all_mask))
383 sline[i].flag |= mark;
384 else
385 sline[i].flag &= ~mark;
af3feefa 386 }
d8f4790e 387 if (!dense)
3ec1909f 388 return give_context(sline, cnt, num_parent);
d8f4790e 389
263eee29
JH
390 /* Look at each hunk, and if we have changes from only one
391 * parent, or the changes are the same from all but one
392 * parent, mark that uninteresting.
d8f4790e
JH
393 */
394 i = 0;
74065951 395 while (i <= cnt) {
3ec1909f 396 unsigned long j, hunk_begin, hunk_end;
bf1c32bd 397 unsigned long same_diff;
74065951 398 while (i <= cnt && !(sline[i].flag & mark))
d8f4790e 399 i++;
74065951 400 if (cnt < i)
d8f4790e 401 break; /* No more interesting hunks */
3ec1909f 402 hunk_begin = i;
74065951 403 for (j = i + 1; j <= cnt; j++) {
3ec1909f
JH
404 if (!(sline[j].flag & mark)) {
405 /* Look beyond the end to see if there
406 * is an interesting line after this
407 * hunk within context span.
408 */
409 unsigned long la; /* lookahead */
410 int contin = 0;
411 la = adjust_hunk_tail(sline, all_mask,
412 hunk_begin, j);
74065951
JH
413 la = (la + context < cnt + 1) ?
414 (la + context) : cnt + 1;
3ec1909f
JH
415 while (j <= --la) {
416 if (sline[la].flag & mark) {
417 contin = 1;
418 break;
419 }
420 }
421 if (!contin)
422 break;
423 j = la;
424 }
425 }
426 hunk_end = j;
427
bf1c32bd 428 /* [i..hunk_end) are interesting. Now is it really
fd4b1d21
JH
429 * interesting? We check if there are only two versions
430 * and the result matches one of them. That is, we look
431 * at:
432 * (+) line, which records lines added to which parents;
433 * this line appears in the result.
434 * (-) line, which records from what parents the line
435 * was removed; this line does not appear in the result.
436 * then check the set of parents the result has difference
437 * from, from all lines. If there are lines that has
438 * different set of parents that the result has differences
439 * from, that means we have more than two versions.
440 *
441 * Even when we have only two versions, if the result does
442 * not match any of the parents, the it should be considered
443 * interesting. In such a case, we would have all '+' line.
444 * After passing the above "two versions" test, that would
445 * appear as "the same set of parents" to be "all parents".
d8f4790e 446 */
bf1c32bd
JH
447 same_diff = 0;
448 has_interesting = 0;
449 for (j = i; j < hunk_end && !has_interesting; j++) {
46dc9412 450 unsigned long this_diff = sline[j].flag & all_mask;
bf1c32bd
JH
451 struct lline *ll = sline[j].lost_head;
452 if (this_diff) {
453 /* This has some changes. Is it the
454 * same as others?
455 */
456 if (!same_diff)
457 same_diff = this_diff;
458 else if (same_diff != this_diff) {
459 has_interesting = 1;
460 break;
461 }
462 }
463 while (ll && !has_interesting) {
464 /* Lost this line from these parents;
465 * who are they? Are they the same?
466 */
467 this_diff = ll->parent_map;
468 if (!same_diff)
469 same_diff = this_diff;
470 else if (same_diff != this_diff) {
471 has_interesting = 1;
472 }
473 ll = ll->next;
474 }
d8f4790e 475 }
bf1c32bd 476
fd4b1d21 477 if (!has_interesting && same_diff != all_mask) {
d8f4790e 478 /* This hunk is not that interesting after all */
3ec1909f 479 for (j = hunk_begin; j < hunk_end; j++)
d8f4790e
JH
480 sline[j].flag &= ~mark;
481 }
482 i = hunk_end;
483 }
3ec1909f
JH
484
485 has_interesting = give_context(sline, cnt, num_parent);
8828cdcb 486 return has_interesting;
af3feefa
JH
487}
488
3b0f5e88 489static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context)
f16706cc
JH
490{
491 l0 = sline[l0].p_lno[n];
492 l1 = sline[l1].p_lno[n];
3b0f5e88 493 printf(" -%lu,%lu", l0, l1-l0-null_context);
f16706cc
JH
494}
495
d5f6a01a
JH
496static int hunk_comment_line(const char *bol)
497{
7a8ac59f
JH
498 int ch;
499
500 if (!bol)
501 return 0;
502 ch = *bol & 0xff;
d5f6a01a
JH
503 return (isalpha(ch) || ch == '_' || ch == '$');
504}
505
39280970
JH
506static void show_line_to_eol(const char *line, int len, const char *reset)
507{
508 int saw_cr_at_eol = 0;
509 if (len < 0)
510 len = strlen(line);
511 saw_cr_at_eol = (len && line[len-1] == '\r');
512
513 printf("%.*s%s%s\n", len - saw_cr_at_eol, line,
514 reset,
515 saw_cr_at_eol ? "\r" : "");
516}
517
567a03d1 518static void dump_sline(struct sline *sline, unsigned long cnt, int num_parent,
21798708 519 int use_color, int result_deleted)
af3feefa
JH
520{
521 unsigned long mark = (1UL<<num_parent);
c86fbe53 522 unsigned long no_pre_delete = (2UL<<num_parent);
af3feefa 523 int i;
f16706cc 524 unsigned long lno = 0;
567a03d1 525 const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO);
89cb73a1 526 const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO);
567a03d1
JH
527 const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW);
528 const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD);
529 const char *c_plain = diff_get_color(use_color, DIFF_PLAIN);
530 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
af3feefa 531
21798708 532 if (result_deleted)
4462731e
JH
533 return; /* result deleted */
534
af3feefa 535 while (1) {
8bc7574b
JH
536 unsigned long hunk_end;
537 unsigned long rlines;
d5f6a01a 538 const char *hunk_comment = NULL;
3b0f5e88 539 unsigned long null_context = 0;
d5f6a01a
JH
540
541 while (lno <= cnt && !(sline[lno].flag & mark)) {
542 if (hunk_comment_line(sline[lno].bol))
543 hunk_comment = sline[lno].bol;
af3feefa 544 lno++;
d5f6a01a 545 }
8a470ebf 546 if (cnt < lno)
af3feefa 547 break;
8a470ebf 548 else {
74065951 549 for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++)
8a470ebf
JH
550 if (!(sline[hunk_end].flag & mark))
551 break;
552 }
74065951
JH
553 rlines = hunk_end - lno;
554 if (cnt < hunk_end)
555 rlines--; /* pointing at the last delete hunk */
3b0f5e88
JH
556
557 if (!context) {
558 /*
559 * Even when running with --unified=0, all
560 * lines in the hunk needs to be processed in
561 * the loop below in order to show the
562 * deletion recorded in lost_head. However,
563 * we do not want to show the resulting line
564 * with all blank context markers in such a
565 * case. Compensate.
566 */
567 unsigned long j;
568 for (j = lno; j < hunk_end; j++)
569 if (!(sline[j].flag & (mark-1)))
570 null_context++;
571 rlines -= null_context;
572 }
573
567a03d1 574 fputs(c_frag, stdout);
af3feefa 575 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
f16706cc 576 for (i = 0; i < num_parent; i++)
3b0f5e88 577 show_parent_lno(sline, lno, hunk_end, i, null_context);
74065951 578 printf(" +%lu,%lu ", lno+1, rlines);
af3feefa 579 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
d5f6a01a
JH
580
581 if (hunk_comment) {
582 int comment_end = 0;
583 for (i = 0; i < 40; i++) {
584 int ch = hunk_comment[i] & 0xff;
585 if (!ch || ch == '\n')
586 break;
587 if (!isspace(ch))
588 comment_end = i;
589 }
590 if (comment_end)
89cb73a1
BW
591 printf("%s%s %s%s", c_reset,
592 c_plain, c_reset,
593 c_func);
d5f6a01a
JH
594 for (i = 0; i < comment_end; i++)
595 putchar(hunk_comment[i]);
596 }
597
567a03d1 598 printf("%s\n", c_reset);
af3feefa
JH
599 while (lno < hunk_end) {
600 struct lline *ll;
601 int j;
46dc9412 602 unsigned long p_mask;
fd13b21f 603 struct sline *sl = &sline[lno++];
c86fbe53 604 ll = (sl->flag & no_pre_delete) ? NULL : sl->lost_head;
af3feefa 605 while (ll) {
567a03d1 606 fputs(c_old, stdout);
af3feefa
JH
607 for (j = 0; j < num_parent; j++) {
608 if (ll->parent_map & (1UL<<j))
609 putchar('-');
610 else
611 putchar(' ');
612 }
39280970 613 show_line_to_eol(ll->line, -1, c_reset);
af3feefa
JH
614 ll = ll->next;
615 }
74065951 616 if (cnt < lno)
8a470ebf 617 break;
46dc9412 618 p_mask = 1;
3b0f5e88
JH
619 if (!(sl->flag & (mark-1))) {
620 /*
621 * This sline was here to hang the
622 * lost lines in front of it.
623 */
624 if (!context)
625 continue;
567a03d1 626 fputs(c_plain, stdout);
3b0f5e88 627 }
567a03d1
JH
628 else
629 fputs(c_new, stdout);
af3feefa 630 for (j = 0; j < num_parent; j++) {
46dc9412 631 if (p_mask & sl->flag)
af3feefa 632 putchar('+');
46dc9412
JH
633 else
634 putchar(' ');
635 p_mask <<= 1;
af3feefa 636 }
39280970 637 show_line_to_eol(sl->bol, sl->len, c_reset);
af3feefa
JH
638 }
639 }
640}
641
3c39e9bd
JH
642static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
643 int i, int j)
644{
645 /* We have already examined parent j and we know parent i
646 * and parent j are the same, so reuse the combined result
647 * of parent j for parent i.
648 */
649 unsigned long lno, imask, jmask;
650 imask = (1UL<<i);
651 jmask = (1UL<<j);
652
8a470ebf 653 for (lno = 0; lno <= cnt; lno++) {
3c39e9bd 654 struct lline *ll = sline->lost_head;
f16706cc 655 sline->p_lno[i] = sline->p_lno[j];
3c39e9bd
JH
656 while (ll) {
657 if (ll->parent_map & jmask)
658 ll->parent_map |= imask;
659 ll = ll->next;
660 }
46dc9412
JH
661 if (sline->flag & jmask)
662 sline->flag |= imask;
3c39e9bd
JH
663 sline++;
664 }
4462731e
JH
665 /* the overall size of the file (sline[cnt]) */
666 sline->p_lno[i] = sline->p_lno[j];
3c39e9bd
JH
667}
668
462a15bc
JH
669static void dump_quoted_path(const char *head,
670 const char *prefix,
671 const char *path,
567a03d1 672 const char *c_meta, const char *c_reset)
eab144ac 673{
462a15bc
JH
674 static struct strbuf buf = STRBUF_INIT;
675
676 strbuf_reset(&buf);
677 strbuf_addstr(&buf, c_meta);
678 strbuf_addstr(&buf, head);
d5625091 679 quote_two_c_style(&buf, prefix, path, 0);
462a15bc
JH
680 strbuf_addstr(&buf, c_reset);
681 puts(buf.buf);
eab144ac
LT
682}
683
89b0c4b5
JH
684static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
685 int dense, struct rev_info *rev)
af3feefa 686{
91539833 687 struct diff_options *opt = &rev->diffopt;
f23fc773 688 unsigned long result_size, cnt, lno;
21798708 689 int result_deleted = 0;
310f8b5b 690 char *result, *cp;
af3feefa 691 struct sline *sline; /* survived lines */
2454c962 692 int mode_differs = 0;
89b0c4b5 693 int i, show_hunks;
0bef57ee 694 int working_tree_file = is_null_sha1(elem->sha1);
8f67f8ae 695 int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
a5a818ee 696 const char *a_prefix, *b_prefix;
f23fc773 697 mmfile_t result_file;
af3feefa 698
ee1e5412 699 context = opt->context;
a5a818ee
JH
700 a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
701 b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
702
af3feefa 703 /* Read the result of merge first */
f23fc773 704 if (!working_tree_file)
7dae8b21 705 result = grab_blob(elem->sha1, elem->mode, &result_size);
ea726d02 706 else {
9843a1f6 707 /* Used by diff-tree to read from the working tree */
ea726d02 708 struct stat st;
4fc970c4
JH
709 int fd = -1;
710
711 if (lstat(elem->path, &st) < 0)
712 goto deleted_file;
713
714 if (S_ISLNK(st.st_mode)) {
912342d9
JH
715 struct strbuf buf = STRBUF_INIT;
716
717 if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) {
4fc970c4
JH
718 error("readlink(%s): %s", elem->path,
719 strerror(errno));
720 return;
721 }
912342d9
JH
722 result_size = buf.len;
723 result = strbuf_detach(&buf, NULL);
4fc970c4 724 elem->mode = canon_mode(st.st_mode);
7dae8b21
JH
725 } else if (S_ISDIR(st.st_mode)) {
726 unsigned char sha1[20];
727 if (resolve_gitlink_ref(elem->path, "HEAD", sha1) < 0)
728 result = grab_blob(elem->sha1, elem->mode, &result_size);
729 else
730 result = grab_blob(sha1, elem->mode, &result_size);
91fcbcbd 731 } else if (0 <= (fd = open(elem->path, O_RDONLY))) {
dc49cd76 732 size_t len = xsize_t(st.st_size);
c697ad14 733 ssize_t done;
a249a9b5 734 int is_file, i;
ea726d02 735
1b0c7174 736 elem->mode = canon_mode(st.st_mode);
a249a9b5
JS
737 /* if symlinks don't work, assume symlink if all parents
738 * are symlinks
739 */
740 is_file = has_symlinks;
741 for (i = 0; !is_file && i < num_parent; i++)
742 is_file = !S_ISLNK(elem->parent[i].mode);
743 if (!is_file)
744 elem->mode = canon_mode(S_IFLNK);
745
f23fc773 746 result_size = len;
ea726d02 747 result = xmalloc(len + 1);
c697ad14
HO
748
749 done = read_in_full(fd, result, len);
750 if (done < 0)
0721c314 751 die_errno("read error '%s'", elem->path);
c697ad14
HO
752 else if (done < len)
753 die("early EOF '%s'", elem->path);
754
ea726d02 755 result[len] = 0;
5e568f9e
AG
756
757 /* If not a fake symlink, apply filters, e.g. autocrlf */
758 if (is_file) {
f285a2d7 759 struct strbuf buf = STRBUF_INIT;
5e568f9e 760
5e568f9e
AG
761 if (convert_to_git(elem->path, result, len, &buf, safe_crlf)) {
762 free(result);
763 result = strbuf_detach(&buf, &len);
764 result_size = len;
765 }
766 }
ea726d02
JH
767 }
768 else {
4fc970c4 769 deleted_file:
21798708 770 result_deleted = 1;
f23fc773 771 result_size = 0;
713a11fc 772 elem->mode = 0;
28f75818 773 result = xcalloc(1, 1);
ea726d02 774 }
4fc970c4 775
ea726d02
JH
776 if (0 <= fd)
777 close(fd);
778 }
af3feefa 779
2386c297 780 for (cnt = 0, cp = result; cp < result + result_size; cp++) {
af3feefa
JH
781 if (*cp == '\n')
782 cnt++;
783 }
f23fc773 784 if (result_size && result[result_size-1] != '\n')
af3feefa
JH
785 cnt++; /* incomplete line */
786
8a470ebf 787 sline = xcalloc(cnt+2, sizeof(*sline));
af3feefa 788 sline[0].bol = result;
8a470ebf 789 for (lno = 0; lno <= cnt + 1; lno++) {
4462731e
JH
790 sline[lno].lost_tail = &sline[lno].lost_head;
791 sline[lno].flag = 0;
792 }
2386c297 793 for (lno = 0, cp = result; cp < result + result_size; cp++) {
af3feefa
JH
794 if (*cp == '\n') {
795 sline[lno].len = cp - sline[lno].bol;
af3feefa
JH
796 lno++;
797 if (lno < cnt)
798 sline[lno].bol = cp + 1;
799 }
800 }
f23fc773
JH
801 if (result_size && result[result_size-1] != '\n')
802 sline[cnt-1].len = result_size - (sline[cnt-1].bol - result);
803
804 result_file.ptr = result;
805 result_file.size = result_size;
af3feefa 806
8a470ebf
JH
807 /* Even p_lno[cnt+1] is valid -- that is for the end line number
808 * for deletion hunk at the end.
809 */
810 sline[0].p_lno = xcalloc((cnt+2) * num_parent, sizeof(unsigned long));
811 for (lno = 0; lno <= cnt; lno++)
f16706cc
JH
812 sline[lno+1].p_lno = sline[lno].p_lno + num_parent;
813
3c39e9bd
JH
814 for (i = 0; i < num_parent; i++) {
815 int j;
816 for (j = 0; j < i; j++) {
a89fccd2
DR
817 if (!hashcmp(elem->parent[i].sha1,
818 elem->parent[j].sha1)) {
3c39e9bd
JH
819 reuse_combine_diff(sline, cnt, i, j);
820 break;
821 }
822 }
823 if (i <= j)
7dae8b21
JH
824 combine_diff(elem->parent[i].sha1,
825 elem->parent[i].mode,
826 &result_file, sline,
21798708 827 cnt, i, num_parent, result_deleted);
2454c962
JH
828 if (elem->parent[i].mode != elem->mode)
829 mode_differs = 1;
3c39e9bd 830 }
af3feefa 831
8828cdcb 832 show_hunks = make_hunks(sline, cnt, num_parent, dense);
af3feefa 833
713a11fc 834 if (show_hunks || mode_differs || working_tree_file) {
9843a1f6 835 const char *abb;
8f67f8ae 836 int use_color = DIFF_OPT_TST(opt, COLOR_DIFF);
567a03d1
JH
837 const char *c_meta = diff_get_color(use_color, DIFF_METAINFO);
838 const char *c_reset = diff_get_color(use_color, DIFF_RESET);
d5f6a01a
JH
839 int added = 0;
840 int deleted = 0;
9843a1f6 841
44152787 842 if (rev->loginfo && !rev->no_commit_id)
02865655 843 show_log(rev);
567a03d1 844 dump_quoted_path(dense ? "diff --cc " : "diff --combined ",
462a15bc 845 "", elem->path, c_meta, c_reset);
567a03d1 846 printf("%sindex ", c_meta);
823bcd6e 847 for (i = 0; i < num_parent; i++) {
297a1aad 848 abb = find_unique_abbrev(elem->parent[i].sha1,
e70c6b35 849 abbrev);
9843a1f6 850 printf("%s%s", i ? "," : "", abb);
823bcd6e 851 }
e70c6b35 852 abb = find_unique_abbrev(elem->sha1, abbrev);
567a03d1 853 printf("..%s%s\n", abb, c_reset);
2454c962
JH
854
855 if (mode_differs) {
d5f6a01a
JH
856 deleted = !elem->mode;
857
858 /* We say it was added if nobody had it */
859 added = !deleted;
d416df88
JH
860 for (i = 0; added && i < num_parent; i++)
861 if (elem->parent[i].status !=
862 DIFF_STATUS_ADDED)
863 added = 0;
864 if (added)
567a03d1
JH
865 printf("%snew file mode %06o",
866 c_meta, elem->mode);
d416df88 867 else {
d5f6a01a 868 if (deleted)
567a03d1 869 printf("%sdeleted file ", c_meta);
d416df88
JH
870 printf("mode ");
871 for (i = 0; i < num_parent; i++) {
872 printf("%s%06o", i ? "," : "",
873 elem->parent[i].mode);
874 }
875 if (elem->mode)
876 printf("..%06o", elem->mode);
2454c962 877 }
567a03d1 878 printf("%s\n", c_reset);
2454c962 879 }
d5f6a01a 880 if (added)
462a15bc
JH
881 dump_quoted_path("--- ", "", "/dev/null",
882 c_meta, c_reset);
d5f6a01a 883 else
a5a818ee 884 dump_quoted_path("--- ", a_prefix, elem->path,
462a15bc 885 c_meta, c_reset);
d5f6a01a 886 if (deleted)
462a15bc
JH
887 dump_quoted_path("+++ ", "", "/dev/null",
888 c_meta, c_reset);
d5f6a01a 889 else
a5a818ee 890 dump_quoted_path("+++ ", b_prefix, elem->path,
462a15bc
JH
891 c_meta, c_reset);
892 dump_sline(sline, cnt, num_parent,
21798708 893 DIFF_OPT_TST(opt, COLOR_DIFF), result_deleted);
8828cdcb 894 }
af3feefa
JH
895 free(result);
896
2386c297
JH
897 for (lno = 0; lno < cnt; lno++) {
898 if (sline[lno].lost_head) {
899 struct lline *ll = sline[lno].lost_head;
af3feefa
JH
900 while (ll) {
901 struct lline *tmp = ll;
902 ll = ll->next;
903 free(tmp);
904 }
905 }
906 }
46dc9412 907 free(sline[0].p_lno);
af3feefa
JH
908 free(sline);
909}
910
ee638024
LT
911#define COLONS "::::::::::::::::::::::::::::::::"
912
91539833 913static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev)
ee638024 914{
91539833 915 struct diff_options *opt = &rev->diffopt;
310f8b5b 916 int i, offset;
ee638024
LT
917 const char *prefix;
918 int line_termination, inter_name_termination;
919
920 line_termination = opt->line_termination;
921 inter_name_termination = '\t';
922 if (!line_termination)
923 inter_name_termination = 0;
924
44152787 925 if (rev->loginfo && !rev->no_commit_id)
02865655 926 show_log(rev);
ee638024 927
c6744349 928 if (opt->output_format & DIFF_FORMAT_RAW) {
0a798076
JH
929 offset = strlen(COLONS) - num_parent;
930 if (offset < 0)
931 offset = 0;
932 prefix = COLONS + offset;
933
934 /* Show the modes */
935 for (i = 0; i < num_parent; i++) {
936 printf("%s%06o", prefix, p->parent[i].mode);
937 prefix = " ";
938 }
939 printf("%s%06o", prefix, p->mode);
940
941 /* Show sha1's */
942 for (i = 0; i < num_parent; i++)
943 printf(" %s", diff_unique_abbrev(p->parent[i].sha1,
944 opt->abbrev));
945 printf(" %s ", diff_unique_abbrev(p->sha1, opt->abbrev));
946 }
947
c6744349 948 if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) {
d416df88
JH
949 for (i = 0; i < num_parent; i++)
950 putchar(p->parent[i].status);
951 putchar(inter_name_termination);
952 }
0a798076 953
663af342 954 write_name_quoted(p->path, stdout, line_termination);
0a798076
JH
955}
956
91539833 957void show_combined_diff(struct combine_diff_path *p,
0a798076
JH
958 int num_parent,
959 int dense,
91539833 960 struct rev_info *rev)
0a798076 961{
91539833 962 struct diff_options *opt = &rev->diffopt;
0a798076 963 if (!p->len)
91539833 964 return;
c6744349
TH
965 if (opt->output_format & (DIFF_FORMAT_RAW |
966 DIFF_FORMAT_NAME |
89b0c4b5 967 DIFF_FORMAT_NAME_STATUS))
91539833 968 show_raw_diff(p, num_parent, rev);
89b0c4b5 969 else if (opt->output_format & DIFF_FORMAT_PATCH)
91539833 970 show_patch_diff(p, num_parent, dense, rev);
ee638024
LT
971}
972
0fe7c1de
JH
973void diff_tree_combined(const unsigned char *sha1,
974 const unsigned char parent[][20],
975 int num_parent,
976 int dense,
977 struct rev_info *rev)
af3feefa 978{
91539833 979 struct diff_options *opt = &rev->diffopt;
af3feefa 980 struct diff_options diffopts;
ea726d02 981 struct combine_diff_path *p, *paths = NULL;
3969cf7d 982 int i, num_paths, needsep, show_log_first;
af3feefa 983
5b236832 984 diffopts = *opt;
3969cf7d 985 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
8f67f8ae
PH
986 DIFF_OPT_SET(&diffopts, RECURSIVE);
987 DIFF_OPT_CLR(&diffopts, ALLOW_EXTERNAL);
af3feefa 988
44152787 989 show_log_first = !!rev->loginfo && !rev->no_commit_id;
3969cf7d 990 needsep = 0;
af3feefa 991 /* find set of paths that everybody touches */
0fe7c1de 992 for (i = 0; i < num_parent; i++) {
965f803c
JH
993 /* show stat against the first parent even
994 * when doing combined diff.
995 */
74e2abe5
JH
996 int stat_opt = (opt->output_format &
997 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT));
998 if (i == 0 && stat_opt)
999 diffopts.output_format = stat_opt;
965f803c
JH
1000 else
1001 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
0fe7c1de 1002 diff_tree_sha1(parent[i], sha1, "", &diffopts);
5b236832 1003 diffcore_std(&diffopts);
af3feefa 1004 paths = intersect_paths(paths, i, num_parent);
eab144ac 1005
3969cf7d 1006 if (show_log_first && i == 0) {
02865655 1007 show_log(rev);
3969cf7d
JH
1008 if (rev->verbose_header && opt->output_format)
1009 putchar(opt->line_termination);
1010 }
af3feefa
JH
1011 diff_flush(&diffopts);
1012 }
1013
1014 /* find out surviving paths */
1015 for (num_paths = 0, p = paths; p; p = p->next) {
1016 if (p->len)
1017 num_paths++;
1018 }
e3c3a550 1019 if (num_paths) {
c6744349
TH
1020 if (opt->output_format & (DIFF_FORMAT_RAW |
1021 DIFF_FORMAT_NAME |
1022 DIFF_FORMAT_NAME_STATUS)) {
86ff1d20 1023 for (p = paths; p; p = p->next) {
c6744349
TH
1024 if (p->len)
1025 show_raw_diff(p, num_parent, rev);
86ff1d20 1026 }
3969cf7d 1027 needsep = 1;
86ff1d20 1028 }
74e2abe5
JH
1029 else if (opt->output_format &
1030 (DIFF_FORMAT_NUMSTAT|DIFF_FORMAT_DIFFSTAT))
3969cf7d 1031 needsep = 1;
c6744349 1032 if (opt->output_format & DIFF_FORMAT_PATCH) {
3969cf7d
JH
1033 if (needsep)
1034 putchar(opt->line_termination);
c6744349
TH
1035 for (p = paths; p; p = p->next) {
1036 if (p->len)
1037 show_patch_diff(p, num_parent, dense,
1038 rev);
1039 }
af3feefa
JH
1040 }
1041 }
1042
1043 /* Clean things up */
1044 while (paths) {
ea726d02 1045 struct combine_diff_path *tmp = paths;
af3feefa
JH
1046 paths = paths->next;
1047 free(tmp);
1048 }
af3feefa 1049}
0fe7c1de
JH
1050
1051void diff_tree_combined_merge(const unsigned char *sha1,
1052 int dense, struct rev_info *rev)
1053{
1054 int num_parent;
1055 const unsigned char (*parent)[20];
1056 struct commit *commit = lookup_commit(sha1);
1057 struct commit_list *parents;
1058
1059 /* count parents */
1060 for (parents = commit->parents, num_parent = 0;
1061 parents;
1062 parents = parents->next, num_parent++)
1063 ; /* nothing */
1064
1065 parent = xmalloc(num_parent * sizeof(*parent));
1066 for (parents = commit->parents, num_parent = 0;
1067 parents;
1068 parents = parents->next, num_parent++)
4b25d091 1069 hashcpy((unsigned char *)(parent + num_parent),
e702496e 1070 parents->item->object.sha1);
0fe7c1de
JH
1071 diff_tree_combined(sha1, parent, num_parent, dense, rev);
1072}