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