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