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