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