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