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