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