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