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