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