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