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