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