]>
Commit | Line | Data |
---|---|---|
af3feefa | 1 | #include "cache.h" |
cbd53a21 | 2 | #include "object-store.h" |
af3feefa | 3 | #include "commit.h" |
8e440259 | 4 | #include "blob.h" |
af3feefa JH |
5 | #include "diff.h" |
6 | #include "diffcore.h" | |
7 | #include "quote.h" | |
d9ea73e0 | 8 | #include "xdiff-interface.h" |
fa04ae0b | 9 | #include "xdiff/xmacros.h" |
91539833 | 10 | #include "log-tree.h" |
7dae8b21 | 11 | #include "refs.h" |
4d5f3471 | 12 | #include "userdiff.h" |
fe299ec5 | 13 | #include "oid-array.h" |
53d00b39 | 14 | #include "revision.h" |
af3feefa | 15 | |
e09867f0 JK |
16 | static int compare_paths(const struct combine_diff_path *one, |
17 | const struct diff_filespec *two) | |
18 | { | |
19 | if (!S_ISDIR(one->mode) && !S_ISDIR(two->mode)) | |
20 | return strcmp(one->path, two->path); | |
21 | ||
22 | return base_name_compare(one->path, strlen(one->path), one->mode, | |
23 | two->path, strlen(two->path), two->mode); | |
24 | } | |
25 | ||
d76ce4f7 EN |
26 | static int filename_changed(char status) |
27 | { | |
28 | return status == 'R' || status == 'C'; | |
29 | } | |
30 | ||
31 | static struct combine_diff_path *intersect_paths( | |
32 | struct combine_diff_path *curr, | |
33 | int n, | |
34 | int num_parent, | |
35 | int combined_all_paths) | |
af3feefa JH |
36 | { |
37 | struct diff_queue_struct *q = &diff_queued_diff; | |
7b1004b0 | 38 | struct combine_diff_path *p, **tail = &curr; |
d76ce4f7 | 39 | int i, j, cmp; |
af3feefa JH |
40 | |
41 | if (!n) { | |
af3feefa JH |
42 | for (i = 0; i < q->nr; i++) { |
43 | int len; | |
44 | const char *path; | |
a976b0a5 | 45 | if (diff_unmodified_pair(q->queue[i])) |
af3feefa JH |
46 | continue; |
47 | path = q->queue[i]->two->path; | |
48 | len = strlen(path); | |
2454c962 | 49 | p = xmalloc(combine_diff_path_size(num_parent, len)); |
4b25d091 | 50 | p->path = (char *) &(p->parent[num_parent]); |
af3feefa JH |
51 | memcpy(p->path, path, len); |
52 | p->path[len] = 0; | |
af3feefa | 53 | p->next = NULL; |
2454c962 JH |
54 | memset(p->parent, 0, |
55 | sizeof(p->parent[0]) * num_parent); | |
56 | ||
a0d12c44 | 57 | oidcpy(&p->oid, &q->queue[i]->two->oid); |
2454c962 | 58 | p->mode = q->queue[i]->two->mode; |
a0d12c44 | 59 | oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid); |
2454c962 | 60 | p->parent[n].mode = q->queue[i]->one->mode; |
d416df88 | 61 | p->parent[n].status = q->queue[i]->status; |
d76ce4f7 EN |
62 | |
63 | if (combined_all_paths && | |
64 | filename_changed(p->parent[n].status)) { | |
65 | strbuf_init(&p->parent[n].path, 0); | |
66 | strbuf_addstr(&p->parent[n].path, | |
67 | q->queue[i]->one->path); | |
68 | } | |
5290a0f8 JH |
69 | *tail = p; |
70 | tail = &p->next; | |
af3feefa | 71 | } |
7b1004b0 | 72 | return curr; |
af3feefa JH |
73 | } |
74 | ||
8518ff8f | 75 | /* |
7b1004b0 JH |
76 | * paths in curr (linked list) and q->queue[] (array) are |
77 | * both sorted in the tree order. | |
8518ff8f | 78 | */ |
8518ff8f | 79 | i = 0; |
7b1004b0 JH |
80 | while ((p = *tail) != NULL) { |
81 | cmp = ((i >= q->nr) | |
e09867f0 | 82 | ? -1 : compare_paths(p, q->queue[i]->two)); |
8518ff8f | 83 | |
8518ff8f | 84 | if (cmp < 0) { |
7b1004b0 JH |
85 | /* p->path not in q->queue[]; drop it */ |
86 | *tail = p->next; | |
d76ce4f7 EN |
87 | for (j = 0; j < num_parent; j++) |
88 | if (combined_all_paths && | |
89 | filename_changed(p->parent[j].status)) | |
90 | strbuf_release(&p->parent[j].path); | |
7b1004b0 | 91 | free(p); |
af3feefa | 92 | continue; |
8518ff8f | 93 | } |
af3feefa | 94 | |
8518ff8f | 95 | if (cmp > 0) { |
7b1004b0 | 96 | /* q->queue[i] not in p->path; skip it */ |
8518ff8f KS |
97 | i++; |
98 | continue; | |
af3feefa | 99 | } |
8518ff8f | 100 | |
a0d12c44 | 101 | oidcpy(&p->parent[n].oid, &q->queue[i]->one->oid); |
8518ff8f KS |
102 | p->parent[n].mode = q->queue[i]->one->mode; |
103 | p->parent[n].status = q->queue[i]->status; | |
d76ce4f7 EN |
104 | if (combined_all_paths && |
105 | filename_changed(p->parent[n].status)) | |
106 | strbuf_addstr(&p->parent[n].path, | |
107 | q->queue[i]->one->path); | |
8518ff8f | 108 | |
7b1004b0 | 109 | tail = &p->next; |
8518ff8f | 110 | i++; |
af3feefa JH |
111 | } |
112 | return curr; | |
113 | } | |
114 | ||
b469d8b6 | 115 | /* Lines lost from parent */ |
af3feefa | 116 | struct lline { |
99d32060 | 117 | struct lline *next, *prev; |
af3feefa JH |
118 | int len; |
119 | unsigned long parent_map; | |
120 | char line[FLEX_ARRAY]; | |
121 | }; | |
122 | ||
99d32060 AP |
123 | /* Lines lost from current parent (before coalescing) */ |
124 | struct plost { | |
125 | struct lline *lost_head, *lost_tail; | |
126 | int len; | |
127 | }; | |
128 | ||
b469d8b6 | 129 | /* Lines surviving in the merge result */ |
af3feefa | 130 | struct sline { |
99d32060 AP |
131 | /* Accumulated and coalesced lost lines */ |
132 | struct lline *lost; | |
133 | int lenlost; | |
134 | struct plost plost; | |
af3feefa JH |
135 | char *bol; |
136 | int len; | |
46dc9412 JH |
137 | /* bit 0 up to (N-1) are on if the parent has this line (i.e. |
138 | * we did not change it). | |
b469d8b6 | 139 | * bit N is used for "interesting" lines, including context. |
c86fbe53 | 140 | * bit (N+1) is used for "do not show deletion before this". |
b469d8b6 | 141 | */ |
af3feefa | 142 | unsigned long flag; |
f16706cc | 143 | unsigned long *p_lno; |
af3feefa JH |
144 | }; |
145 | ||
fa04ae0b AP |
146 | static int match_string_spaces(const char *line1, int len1, |
147 | const char *line2, int len2, | |
148 | long flags) | |
149 | { | |
150 | if (flags & XDF_WHITESPACE_FLAGS) { | |
151 | for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--); | |
152 | for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--); | |
153 | } | |
154 | ||
155 | if (!(flags & (XDF_IGNORE_WHITESPACE | XDF_IGNORE_WHITESPACE_CHANGE))) | |
156 | return (len1 == len2 && !memcmp(line1, line2, len1)); | |
157 | ||
158 | while (len1 > 0 && len2 > 0) { | |
159 | len1--; | |
160 | len2--; | |
161 | if (XDL_ISSPACE(line1[len1]) || XDL_ISSPACE(line2[len2])) { | |
162 | if ((flags & XDF_IGNORE_WHITESPACE_CHANGE) && | |
163 | (!XDL_ISSPACE(line1[len1]) || !XDL_ISSPACE(line2[len2]))) | |
164 | return 0; | |
165 | ||
166 | for (; len1 > 0 && XDL_ISSPACE(line1[len1]); len1--); | |
167 | for (; len2 > 0 && XDL_ISSPACE(line2[len2]); len2--); | |
168 | } | |
169 | if (line1[len1] != line2[len2]) | |
170 | return 0; | |
171 | } | |
172 | ||
173 | if (flags & XDF_IGNORE_WHITESPACE) { | |
174 | /* Consume remaining spaces */ | |
175 | for (; len1 > 0 && XDL_ISSPACE(line1[len1 - 1]); len1--); | |
176 | for (; len2 > 0 && XDL_ISSPACE(line2[len2 - 1]); len2--); | |
177 | } | |
178 | ||
179 | /* We matched full line1 and line2 */ | |
180 | if (!len1 && !len2) | |
181 | return 1; | |
182 | ||
183 | return 0; | |
184 | } | |
185 | ||
99d32060 AP |
186 | enum coalesce_direction { MATCH, BASE, NEW }; |
187 | ||
188 | /* Coalesce new lines into base by finding LCS */ | |
189 | static struct lline *coalesce_lines(struct lline *base, int *lenbase, | |
06fffa00 | 190 | struct lline *newline, int lennew, |
99d32060 | 191 | unsigned long parent, long flags) |
af3feefa | 192 | { |
99d32060 AP |
193 | int **lcs; |
194 | enum coalesce_direction **directions; | |
195 | struct lline *baseend, *newend = NULL; | |
196 | int i, j, origbaselen = *lenbase; | |
af3feefa | 197 | |
06fffa00 | 198 | if (newline == NULL) |
99d32060 AP |
199 | return base; |
200 | ||
201 | if (base == NULL) { | |
202 | *lenbase = lennew; | |
06fffa00 | 203 | return newline; |
99d32060 AP |
204 | } |
205 | ||
206 | /* | |
207 | * Coalesce new lines into base by finding the LCS | |
98e023de | 208 | * - Create the table to run dynamic programming |
99d32060 AP |
209 | * - Compute the LCS |
210 | * - Then reverse read the direction structure: | |
211 | * - If we have MATCH, assign parent to base flag, and consume | |
212 | * both baseend and newend | |
213 | * - Else if we have BASE, consume baseend | |
214 | * - Else if we have NEW, insert newend lline into base and | |
215 | * consume newend | |
216 | */ | |
ca56dadb RS |
217 | CALLOC_ARRAY(lcs, st_add(origbaselen, 1)); |
218 | CALLOC_ARRAY(directions, st_add(origbaselen, 1)); | |
99d32060 | 219 | for (i = 0; i < origbaselen + 1; i++) { |
ca56dadb RS |
220 | CALLOC_ARRAY(lcs[i], st_add(lennew, 1)); |
221 | CALLOC_ARRAY(directions[i], st_add(lennew, 1)); | |
99d32060 AP |
222 | directions[i][0] = BASE; |
223 | } | |
224 | for (j = 1; j < lennew + 1; j++) | |
225 | directions[0][j] = NEW; | |
226 | ||
227 | for (i = 1, baseend = base; i < origbaselen + 1; i++) { | |
06fffa00 | 228 | for (j = 1, newend = newline; j < lennew + 1; j++) { |
99d32060 AP |
229 | if (match_string_spaces(baseend->line, baseend->len, |
230 | newend->line, newend->len, flags)) { | |
231 | lcs[i][j] = lcs[i - 1][j - 1] + 1; | |
232 | directions[i][j] = MATCH; | |
233 | } else if (lcs[i][j - 1] >= lcs[i - 1][j]) { | |
234 | lcs[i][j] = lcs[i][j - 1]; | |
235 | directions[i][j] = NEW; | |
236 | } else { | |
237 | lcs[i][j] = lcs[i - 1][j]; | |
238 | directions[i][j] = BASE; | |
239 | } | |
240 | if (newend->next) | |
241 | newend = newend->next; | |
242 | } | |
243 | if (baseend->next) | |
244 | baseend = baseend->next; | |
245 | } | |
246 | ||
247 | for (i = 0; i < origbaselen + 1; i++) | |
248 | free(lcs[i]); | |
249 | free(lcs); | |
250 | ||
251 | /* At this point, baseend and newend point to the end of each lists */ | |
252 | i--; | |
253 | j--; | |
254 | while (i != 0 || j != 0) { | |
255 | if (directions[i][j] == MATCH) { | |
256 | baseend->parent_map |= 1<<parent; | |
257 | baseend = baseend->prev; | |
258 | newend = newend->prev; | |
259 | i--; | |
260 | j--; | |
261 | } else if (directions[i][j] == NEW) { | |
262 | struct lline *lline; | |
263 | ||
264 | lline = newend; | |
265 | /* Remove lline from new list and update newend */ | |
266 | if (lline->prev) | |
267 | lline->prev->next = lline->next; | |
268 | else | |
06fffa00 | 269 | newline = lline->next; |
99d32060 AP |
270 | if (lline->next) |
271 | lline->next->prev = lline->prev; | |
272 | ||
273 | newend = lline->prev; | |
274 | j--; | |
275 | ||
276 | /* Add lline to base list */ | |
277 | if (baseend) { | |
278 | lline->next = baseend->next; | |
279 | lline->prev = baseend; | |
280 | if (lline->prev) | |
281 | lline->prev->next = lline; | |
af3feefa | 282 | } |
99d32060 AP |
283 | else { |
284 | lline->next = base; | |
285 | base = lline; | |
286 | } | |
287 | (*lenbase)++; | |
288 | ||
289 | if (lline->next) | |
290 | lline->next->prev = lline; | |
291 | ||
292 | } else { | |
293 | baseend = baseend->prev; | |
294 | i--; | |
af3feefa JH |
295 | } |
296 | } | |
297 | ||
06fffa00 | 298 | newend = newline; |
99d32060 AP |
299 | while (newend) { |
300 | struct lline *lline = newend; | |
301 | newend = newend->next; | |
302 | free(lline); | |
303 | } | |
304 | ||
305 | for (i = 0; i < origbaselen + 1; i++) | |
306 | free(directions[i]); | |
307 | free(directions); | |
308 | ||
309 | return base; | |
310 | } | |
311 | ||
6afaf807 NTND |
312 | static char *grab_blob(struct repository *r, |
313 | const struct object_id *oid, unsigned int mode, | |
99d32060 AP |
314 | unsigned long *size, struct userdiff_driver *textconv, |
315 | const char *path) | |
316 | { | |
317 | char *blob; | |
318 | enum object_type type; | |
319 | ||
320 | if (S_ISGITLINK(mode)) { | |
0dc3b035 JK |
321 | struct strbuf buf = STRBUF_INIT; |
322 | strbuf_addf(&buf, "Subproject commit %s\n", oid_to_hex(oid)); | |
323 | *size = buf.len; | |
324 | blob = strbuf_detach(&buf, NULL); | |
1ff57c13 | 325 | } else if (is_null_oid(oid)) { |
99d32060 AP |
326 | /* deleted blob */ |
327 | *size = 0; | |
328 | return xcalloc(1, 1); | |
329 | } else if (textconv) { | |
330 | struct diff_filespec *df = alloc_filespec(path); | |
f9704c2d | 331 | fill_filespec(df, oid, 1, mode); |
6afaf807 | 332 | *size = fill_textconv(r, textconv, df, &blob); |
99d32060 AP |
333 | free_filespec(df); |
334 | } else { | |
b4f5aca4 | 335 | blob = read_object_file(oid, &type, size); |
99d32060 | 336 | if (type != OBJ_BLOB) |
1ff57c13 | 337 | die("object '%s' is not a blob!", oid_to_hex(oid)); |
99d32060 AP |
338 | } |
339 | return blob; | |
340 | } | |
341 | ||
342 | static void append_lost(struct sline *sline, int n, const char *line, int len) | |
343 | { | |
344 | struct lline *lline; | |
345 | unsigned long this_mask = (1UL<<n); | |
346 | if (line[len-1] == '\n') | |
347 | len--; | |
348 | ||
96ffc06f | 349 | FLEX_ALLOC_MEM(lline, line, line, len); |
af3feefa JH |
350 | lline->len = len; |
351 | lline->next = NULL; | |
99d32060 AP |
352 | lline->prev = sline->plost.lost_tail; |
353 | if (lline->prev) | |
354 | lline->prev->next = lline; | |
355 | else | |
356 | sline->plost.lost_head = lline; | |
357 | sline->plost.lost_tail = lline; | |
358 | sline->plost.len++; | |
af3feefa | 359 | lline->parent_map = this_mask; |
af3feefa JH |
360 | } |
361 | ||
f23fc773 | 362 | struct combine_diff_state { |
a0fd3146 JH |
363 | unsigned int lno; |
364 | int ob, on, nb, nn; | |
f23fc773 JH |
365 | unsigned long nmask; |
366 | int num_parent; | |
367 | int n; | |
368 | struct sline *sline; | |
369 | struct sline *lost_bucket; | |
370 | }; | |
371 | ||
0074c911 JK |
372 | static void consume_hunk(void *state_, |
373 | long ob, long on, | |
374 | long nb, long nn, | |
375 | const char *funcline, long funclen) | |
f23fc773 | 376 | { |
d9ea73e0 | 377 | struct combine_diff_state *state = state_; |
0074c911 JK |
378 | |
379 | state->ob = ob; | |
380 | state->on = on; | |
381 | state->nb = nb; | |
382 | state->nn = nn; | |
383 | state->lno = state->nb; | |
384 | if (state->nn == 0) { | |
385 | /* @@ -X,Y +N,0 @@ removed Y lines | |
386 | * that would have come *after* line N | |
387 | * in the result. Our lost buckets hang | |
388 | * to the line after the removed lines, | |
389 | * | |
390 | * Note that this is correct even when N == 0, | |
391 | * in which case the hunk removes the first | |
392 | * line in the file. | |
393 | */ | |
394 | state->lost_bucket = &state->sline[state->nb]; | |
395 | if (!state->nb) | |
396 | state->nb = 1; | |
397 | } else { | |
398 | state->lost_bucket = &state->sline[state->nb-1]; | |
f23fc773 | 399 | } |
0074c911 | 400 | if (!state->sline[state->nb-1].p_lno) |
ca56dadb RS |
401 | CALLOC_ARRAY(state->sline[state->nb - 1].p_lno, |
402 | state->num_parent); | |
0074c911 JK |
403 | state->sline[state->nb-1].p_lno[state->n] = state->ob; |
404 | } | |
405 | ||
a8d5eb6d | 406 | static int consume_line(void *state_, char *line, unsigned long len) |
0074c911 JK |
407 | { |
408 | struct combine_diff_state *state = state_; | |
f23fc773 | 409 | if (!state->lost_bucket) |
a8d5eb6d | 410 | return 0; /* not in any hunk yet */ |
f23fc773 JH |
411 | switch (line[0]) { |
412 | case '-': | |
99d32060 | 413 | append_lost(state->lost_bucket, state->n, line+1, len-1); |
f23fc773 JH |
414 | break; |
415 | case '+': | |
416 | state->sline[state->lno-1].flag |= state->nmask; | |
417 | state->lno++; | |
418 | break; | |
419 | } | |
a8d5eb6d | 420 | return 0; |
f23fc773 JH |
421 | } |
422 | ||
6afaf807 NTND |
423 | static void combine_diff(struct repository *r, |
424 | const struct object_id *parent, unsigned int mode, | |
7dae8b21 | 425 | mmfile_t *result_file, |
2386c297 | 426 | struct sline *sline, unsigned int cnt, int n, |
0508fe53 JK |
427 | int num_parent, int result_deleted, |
428 | struct userdiff_driver *textconv, | |
fa04ae0b | 429 | const char *path, long flags) |
af3feefa | 430 | { |
f23fc773 | 431 | unsigned int p_lno, lno; |
f16706cc | 432 | unsigned long nmask = (1UL << n); |
f23fc773 JH |
433 | xpparam_t xpp; |
434 | xdemitconf_t xecfg; | |
435 | mmfile_t parent_file; | |
f23fc773 JH |
436 | struct combine_diff_state state; |
437 | unsigned long sz; | |
af3feefa | 438 | |
21798708 | 439 | if (result_deleted) |
4462731e JH |
440 | return; /* result deleted */ |
441 | ||
6afaf807 | 442 | parent_file.ptr = grab_blob(r, parent, mode, &sz, textconv, path); |
f23fc773 | 443 | parent_file.size = sz; |
9ccd0a88 | 444 | memset(&xpp, 0, sizeof(xpp)); |
fa04ae0b | 445 | xpp.flags = flags; |
30b25010 | 446 | memset(&xecfg, 0, sizeof(xecfg)); |
f23fc773 JH |
447 | memset(&state, 0, sizeof(state)); |
448 | state.nmask = nmask; | |
449 | state.sline = sline; | |
450 | state.lno = 1; | |
451 | state.num_parent = num_parent; | |
452 | state.n = n; | |
453 | ||
0074c911 JK |
454 | if (xdi_diff_outf(&parent_file, result_file, consume_hunk, |
455 | consume_line, &state, &xpp, &xecfg)) | |
3efb9880 | 456 | die("unable to generate combined diff for %s", |
11a458be | 457 | oid_to_hex(parent)); |
f23fc773 | 458 | free(parent_file.ptr); |
f16706cc JH |
459 | |
460 | /* Assign line numbers for this parent. | |
461 | * | |
462 | * sline[lno].p_lno[n] records the first line number | |
463 | * (counting from 1) for parent N if the final hunk display | |
464 | * started by showing sline[lno] (possibly showing the lost | |
465 | * lines attached to it first). | |
466 | */ | |
8a470ebf | 467 | for (lno = 0, p_lno = 1; lno <= cnt; lno++) { |
f16706cc JH |
468 | struct lline *ll; |
469 | sline[lno].p_lno[n] = p_lno; | |
470 | ||
99d32060 AP |
471 | /* Coalesce new lines */ |
472 | if (sline[lno].plost.lost_head) { | |
473 | struct sline *sl = &sline[lno]; | |
474 | sl->lost = coalesce_lines(sl->lost, &sl->lenlost, | |
475 | sl->plost.lost_head, | |
476 | sl->plost.len, n, flags); | |
477 | sl->plost.lost_head = sl->plost.lost_tail = NULL; | |
478 | sl->plost.len = 0; | |
479 | } | |
480 | ||
f16706cc | 481 | /* How many lines would this sline advance the p_lno? */ |
99d32060 | 482 | ll = sline[lno].lost; |
f16706cc JH |
483 | while (ll) { |
484 | if (ll->parent_map & nmask) | |
485 | p_lno++; /* '-' means parent had it */ | |
486 | ll = ll->next; | |
487 | } | |
8a470ebf | 488 | if (lno < cnt && !(sline[lno].flag & nmask)) |
f16706cc JH |
489 | p_lno++; /* no '+' means parent had it */ |
490 | } | |
491 | sline[lno].p_lno[n] = p_lno; /* trailer */ | |
af3feefa JH |
492 | } |
493 | ||
494 | static unsigned long context = 3; | |
495 | static char combine_marker = '@'; | |
496 | ||
497 | static int interesting(struct sline *sline, unsigned long all_mask) | |
498 | { | |
46dc9412 JH |
499 | /* If some parents lost lines here, or if we have added to |
500 | * some parent, it is interesting. | |
501 | */ | |
99d32060 | 502 | return ((sline->flag & all_mask) || sline->lost); |
af3feefa JH |
503 | } |
504 | ||
3ec1909f JH |
505 | static unsigned long adjust_hunk_tail(struct sline *sline, |
506 | unsigned long all_mask, | |
507 | unsigned long hunk_begin, | |
508 | unsigned long i) | |
509 | { | |
46dc9412 JH |
510 | /* i points at the first uninteresting line. If the last line |
511 | * of the hunk was interesting only because it has some | |
512 | * deletion, then it is not all that interesting for the | |
513 | * purpose of giving trailing context lines. This is because | |
514 | * we output '-' line and then unmodified sline[i-1] itself in | |
515 | * that case which gives us one extra context line. | |
3ec1909f | 516 | */ |
46dc9412 | 517 | if ((hunk_begin + 1 <= i) && !(sline[i-1].flag & all_mask)) |
3ec1909f JH |
518 | i--; |
519 | return i; | |
520 | } | |
521 | ||
46dc9412 JH |
522 | static unsigned long find_next(struct sline *sline, |
523 | unsigned long mark, | |
524 | unsigned long i, | |
525 | unsigned long cnt, | |
2386c297 | 526 | int look_for_uninteresting) |
3ec1909f | 527 | { |
46dc9412 JH |
528 | /* We have examined up to i-1 and are about to look at i. |
529 | * Find next interesting or uninteresting line. Here, | |
530 | * "interesting" does not mean interesting(), but marked by | |
531 | * the give_context() function below (i.e. it includes context | |
532 | * lines that are not interesting to interesting() function | |
533 | * that are surrounded by interesting() ones. | |
534 | */ | |
74065951 | 535 | while (i <= cnt) |
2386c297 | 536 | if (look_for_uninteresting |
46dc9412 JH |
537 | ? !(sline[i].flag & mark) |
538 | : (sline[i].flag & mark)) | |
3ec1909f JH |
539 | return i; |
540 | else | |
541 | i++; | |
74065951 | 542 | return i; |
3ec1909f JH |
543 | } |
544 | ||
545 | static int give_context(struct sline *sline, unsigned long cnt, int num_parent) | |
af3feefa JH |
546 | { |
547 | unsigned long all_mask = (1UL<<num_parent) - 1; | |
548 | unsigned long mark = (1UL<<num_parent); | |
c86fbe53 | 549 | unsigned long no_pre_delete = (2UL<<num_parent); |
af3feefa JH |
550 | unsigned long i; |
551 | ||
46dc9412 | 552 | /* Two groups of interesting lines may have a short gap of |
82e5a82f | 553 | * uninteresting lines. Connect such groups to give them a |
46dc9412 JH |
554 | * bit of context. |
555 | * | |
556 | * We first start from what the interesting() function says, | |
557 | * and mark them with "mark", and paint context lines with the | |
558 | * mark. So interesting() would still say false for such context | |
559 | * lines but they are treated as "interesting" in the end. | |
560 | */ | |
561 | i = find_next(sline, mark, 0, cnt, 0); | |
74065951 | 562 | if (cnt < i) |
3ec1909f JH |
563 | return 0; |
564 | ||
74065951 | 565 | while (i <= cnt) { |
3ec1909f JH |
566 | unsigned long j = (context < i) ? (i - context) : 0; |
567 | unsigned long k; | |
46dc9412 JH |
568 | |
569 | /* Paint a few lines before the first interesting line. */ | |
aac38571 MK |
570 | while (j < i) { |
571 | if (!(sline[j].flag & mark)) | |
572 | sline[j].flag |= no_pre_delete; | |
573 | sline[j++].flag |= mark; | |
574 | } | |
3ec1909f JH |
575 | |
576 | again: | |
46dc9412 JH |
577 | /* we know up to i is to be included. where does the |
578 | * next uninteresting one start? | |
579 | */ | |
580 | j = find_next(sline, mark, i, cnt, 1); | |
74065951 | 581 | if (cnt < j) |
3ec1909f JH |
582 | break; /* the rest are all interesting */ |
583 | ||
584 | /* lookahead context lines */ | |
46dc9412 | 585 | k = find_next(sline, mark, j, cnt, 0); |
3ec1909f JH |
586 | j = adjust_hunk_tail(sline, all_mask, i, j); |
587 | ||
588 | if (k < j + context) { | |
589 | /* k is interesting and [j,k) are not, but | |
590 | * paint them interesting because the gap is small. | |
591 | */ | |
592 | while (j < k) | |
af3feefa | 593 | sline[j++].flag |= mark; |
3ec1909f JH |
594 | i = k; |
595 | goto again; | |
af3feefa | 596 | } |
3ec1909f JH |
597 | |
598 | /* j is the first uninteresting line and there is | |
46dc9412 JH |
599 | * no overlap beyond it within context lines. Paint |
600 | * the trailing edge a bit. | |
3ec1909f JH |
601 | */ |
602 | i = k; | |
74065951 | 603 | k = (j + context < cnt+1) ? j + context : cnt+1; |
3ec1909f JH |
604 | while (j < k) |
605 | sline[j++].flag |= mark; | |
606 | } | |
607 | return 1; | |
608 | } | |
609 | ||
610 | static int make_hunks(struct sline *sline, unsigned long cnt, | |
611 | int num_parent, int dense) | |
612 | { | |
613 | unsigned long all_mask = (1UL<<num_parent) - 1; | |
614 | unsigned long mark = (1UL<<num_parent); | |
615 | unsigned long i; | |
616 | int has_interesting = 0; | |
617 | ||
74065951 | 618 | for (i = 0; i <= cnt; i++) { |
3ec1909f JH |
619 | if (interesting(&sline[i], all_mask)) |
620 | sline[i].flag |= mark; | |
621 | else | |
622 | sline[i].flag &= ~mark; | |
af3feefa | 623 | } |
d8f4790e | 624 | if (!dense) |
3ec1909f | 625 | return give_context(sline, cnt, num_parent); |
d8f4790e | 626 | |
263eee29 JH |
627 | /* Look at each hunk, and if we have changes from only one |
628 | * parent, or the changes are the same from all but one | |
629 | * parent, mark that uninteresting. | |
d8f4790e JH |
630 | */ |
631 | i = 0; | |
74065951 | 632 | while (i <= cnt) { |
3ec1909f | 633 | unsigned long j, hunk_begin, hunk_end; |
bf1c32bd | 634 | unsigned long same_diff; |
74065951 | 635 | while (i <= cnt && !(sline[i].flag & mark)) |
d8f4790e | 636 | i++; |
74065951 | 637 | if (cnt < i) |
d8f4790e | 638 | break; /* No more interesting hunks */ |
3ec1909f | 639 | hunk_begin = i; |
74065951 | 640 | for (j = i + 1; j <= cnt; j++) { |
3ec1909f JH |
641 | if (!(sline[j].flag & mark)) { |
642 | /* Look beyond the end to see if there | |
643 | * is an interesting line after this | |
644 | * hunk within context span. | |
645 | */ | |
646 | unsigned long la; /* lookahead */ | |
647 | int contin = 0; | |
648 | la = adjust_hunk_tail(sline, all_mask, | |
649 | hunk_begin, j); | |
74065951 JH |
650 | la = (la + context < cnt + 1) ? |
651 | (la + context) : cnt + 1; | |
e5e9b565 | 652 | while (la && j <= --la) { |
3ec1909f JH |
653 | if (sline[la].flag & mark) { |
654 | contin = 1; | |
655 | break; | |
656 | } | |
657 | } | |
658 | if (!contin) | |
659 | break; | |
660 | j = la; | |
661 | } | |
662 | } | |
663 | hunk_end = j; | |
664 | ||
bf1c32bd | 665 | /* [i..hunk_end) are interesting. Now is it really |
fd4b1d21 JH |
666 | * interesting? We check if there are only two versions |
667 | * and the result matches one of them. That is, we look | |
668 | * at: | |
669 | * (+) line, which records lines added to which parents; | |
670 | * this line appears in the result. | |
671 | * (-) line, which records from what parents the line | |
672 | * was removed; this line does not appear in the result. | |
673 | * then check the set of parents the result has difference | |
674 | * from, from all lines. If there are lines that has | |
675 | * different set of parents that the result has differences | |
676 | * from, that means we have more than two versions. | |
677 | * | |
678 | * Even when we have only two versions, if the result does | |
679 | * not match any of the parents, the it should be considered | |
680 | * interesting. In such a case, we would have all '+' line. | |
681 | * After passing the above "two versions" test, that would | |
682 | * appear as "the same set of parents" to be "all parents". | |
d8f4790e | 683 | */ |
bf1c32bd JH |
684 | same_diff = 0; |
685 | has_interesting = 0; | |
686 | for (j = i; j < hunk_end && !has_interesting; j++) { | |
46dc9412 | 687 | unsigned long this_diff = sline[j].flag & all_mask; |
99d32060 | 688 | struct lline *ll = sline[j].lost; |
bf1c32bd JH |
689 | if (this_diff) { |
690 | /* This has some changes. Is it the | |
691 | * same as others? | |
692 | */ | |
693 | if (!same_diff) | |
694 | same_diff = this_diff; | |
695 | else if (same_diff != this_diff) { | |
696 | has_interesting = 1; | |
697 | break; | |
698 | } | |
699 | } | |
700 | while (ll && !has_interesting) { | |
701 | /* Lost this line from these parents; | |
702 | * who are they? Are they the same? | |
703 | */ | |
704 | this_diff = ll->parent_map; | |
705 | if (!same_diff) | |
706 | same_diff = this_diff; | |
707 | else if (same_diff != this_diff) { | |
708 | has_interesting = 1; | |
709 | } | |
710 | ll = ll->next; | |
711 | } | |
d8f4790e | 712 | } |
bf1c32bd | 713 | |
fd4b1d21 | 714 | if (!has_interesting && same_diff != all_mask) { |
d8f4790e | 715 | /* This hunk is not that interesting after all */ |
3ec1909f | 716 | for (j = hunk_begin; j < hunk_end; j++) |
d8f4790e JH |
717 | sline[j].flag &= ~mark; |
718 | } | |
719 | i = hunk_end; | |
720 | } | |
3ec1909f JH |
721 | |
722 | has_interesting = give_context(sline, cnt, num_parent); | |
8828cdcb | 723 | return has_interesting; |
af3feefa JH |
724 | } |
725 | ||
3b0f5e88 | 726 | static void show_parent_lno(struct sline *sline, unsigned long l0, unsigned long l1, int n, unsigned long null_context) |
f16706cc JH |
727 | { |
728 | l0 = sline[l0].p_lno[n]; | |
729 | l1 = sline[l1].p_lno[n]; | |
3b0f5e88 | 730 | printf(" -%lu,%lu", l0, l1-l0-null_context); |
f16706cc JH |
731 | } |
732 | ||
d5f6a01a JH |
733 | static int hunk_comment_line(const char *bol) |
734 | { | |
7a8ac59f JH |
735 | int ch; |
736 | ||
737 | if (!bol) | |
738 | return 0; | |
739 | ch = *bol & 0xff; | |
d5f6a01a JH |
740 | return (isalpha(ch) || ch == '_' || ch == '$'); |
741 | } | |
742 | ||
39280970 JH |
743 | static void show_line_to_eol(const char *line, int len, const char *reset) |
744 | { | |
745 | int saw_cr_at_eol = 0; | |
746 | if (len < 0) | |
747 | len = strlen(line); | |
748 | saw_cr_at_eol = (len && line[len-1] == '\r'); | |
749 | ||
750 | printf("%.*s%s%s\n", len - saw_cr_at_eol, line, | |
751 | reset, | |
752 | saw_cr_at_eol ? "\r" : ""); | |
753 | } | |
754 | ||
41ee2ad6 JK |
755 | static void dump_sline(struct sline *sline, const char *line_prefix, |
756 | unsigned long cnt, int num_parent, | |
21798708 | 757 | int use_color, int result_deleted) |
af3feefa JH |
758 | { |
759 | unsigned long mark = (1UL<<num_parent); | |
c86fbe53 | 760 | unsigned long no_pre_delete = (2UL<<num_parent); |
af3feefa | 761 | int i; |
f16706cc | 762 | unsigned long lno = 0; |
567a03d1 | 763 | const char *c_frag = diff_get_color(use_color, DIFF_FRAGINFO); |
89cb73a1 | 764 | const char *c_func = diff_get_color(use_color, DIFF_FUNCINFO); |
567a03d1 JH |
765 | const char *c_new = diff_get_color(use_color, DIFF_FILE_NEW); |
766 | const char *c_old = diff_get_color(use_color, DIFF_FILE_OLD); | |
8dbf3eb6 | 767 | const char *c_context = diff_get_color(use_color, DIFF_CONTEXT); |
567a03d1 | 768 | const char *c_reset = diff_get_color(use_color, DIFF_RESET); |
af3feefa | 769 | |
21798708 | 770 | if (result_deleted) |
4462731e JH |
771 | return; /* result deleted */ |
772 | ||
af3feefa | 773 | while (1) { |
8bc7574b JH |
774 | unsigned long hunk_end; |
775 | unsigned long rlines; | |
d5f6a01a | 776 | const char *hunk_comment = NULL; |
3b0f5e88 | 777 | unsigned long null_context = 0; |
d5f6a01a JH |
778 | |
779 | while (lno <= cnt && !(sline[lno].flag & mark)) { | |
780 | if (hunk_comment_line(sline[lno].bol)) | |
781 | hunk_comment = sline[lno].bol; | |
af3feefa | 782 | lno++; |
d5f6a01a | 783 | } |
8a470ebf | 784 | if (cnt < lno) |
af3feefa | 785 | break; |
8a470ebf | 786 | else { |
74065951 | 787 | for (hunk_end = lno + 1; hunk_end <= cnt; hunk_end++) |
8a470ebf JH |
788 | if (!(sline[hunk_end].flag & mark)) |
789 | break; | |
790 | } | |
74065951 JH |
791 | rlines = hunk_end - lno; |
792 | if (cnt < hunk_end) | |
793 | rlines--; /* pointing at the last delete hunk */ | |
3b0f5e88 JH |
794 | |
795 | if (!context) { | |
796 | /* | |
797 | * Even when running with --unified=0, all | |
798 | * lines in the hunk needs to be processed in | |
799 | * the loop below in order to show the | |
800 | * deletion recorded in lost_head. However, | |
801 | * we do not want to show the resulting line | |
802 | * with all blank context markers in such a | |
803 | * case. Compensate. | |
804 | */ | |
805 | unsigned long j; | |
806 | for (j = lno; j < hunk_end; j++) | |
807 | if (!(sline[j].flag & (mark-1))) | |
808 | null_context++; | |
809 | rlines -= null_context; | |
810 | } | |
811 | ||
41ee2ad6 | 812 | printf("%s%s", line_prefix, c_frag); |
af3feefa | 813 | for (i = 0; i <= num_parent; i++) putchar(combine_marker); |
f16706cc | 814 | for (i = 0; i < num_parent; i++) |
3b0f5e88 | 815 | show_parent_lno(sline, lno, hunk_end, i, null_context); |
74065951 | 816 | printf(" +%lu,%lu ", lno+1, rlines); |
af3feefa | 817 | for (i = 0; i <= num_parent; i++) putchar(combine_marker); |
d5f6a01a JH |
818 | |
819 | if (hunk_comment) { | |
820 | int comment_end = 0; | |
821 | for (i = 0; i < 40; i++) { | |
822 | int ch = hunk_comment[i] & 0xff; | |
823 | if (!ch || ch == '\n') | |
824 | break; | |
825 | if (!isspace(ch)) | |
826 | comment_end = i; | |
827 | } | |
828 | if (comment_end) | |
89cb73a1 | 829 | printf("%s%s %s%s", c_reset, |
8dbf3eb6 | 830 | c_context, c_reset, |
89cb73a1 | 831 | c_func); |
d5f6a01a JH |
832 | for (i = 0; i < comment_end; i++) |
833 | putchar(hunk_comment[i]); | |
834 | } | |
835 | ||
567a03d1 | 836 | printf("%s\n", c_reset); |
af3feefa JH |
837 | while (lno < hunk_end) { |
838 | struct lline *ll; | |
839 | int j; | |
46dc9412 | 840 | unsigned long p_mask; |
fd13b21f | 841 | struct sline *sl = &sline[lno++]; |
99d32060 | 842 | ll = (sl->flag & no_pre_delete) ? NULL : sl->lost; |
af3feefa | 843 | while (ll) { |
41ee2ad6 | 844 | printf("%s%s", line_prefix, c_old); |
af3feefa JH |
845 | for (j = 0; j < num_parent; j++) { |
846 | if (ll->parent_map & (1UL<<j)) | |
847 | putchar('-'); | |
848 | else | |
849 | putchar(' '); | |
850 | } | |
39280970 | 851 | show_line_to_eol(ll->line, -1, c_reset); |
af3feefa JH |
852 | ll = ll->next; |
853 | } | |
74065951 | 854 | if (cnt < lno) |
8a470ebf | 855 | break; |
46dc9412 | 856 | p_mask = 1; |
41ee2ad6 | 857 | fputs(line_prefix, stdout); |
3b0f5e88 JH |
858 | if (!(sl->flag & (mark-1))) { |
859 | /* | |
860 | * This sline was here to hang the | |
861 | * lost lines in front of it. | |
862 | */ | |
863 | if (!context) | |
864 | continue; | |
8dbf3eb6 | 865 | fputs(c_context, stdout); |
3b0f5e88 | 866 | } |
567a03d1 JH |
867 | else |
868 | fputs(c_new, stdout); | |
af3feefa | 869 | for (j = 0; j < num_parent; j++) { |
46dc9412 | 870 | if (p_mask & sl->flag) |
af3feefa | 871 | putchar('+'); |
46dc9412 JH |
872 | else |
873 | putchar(' '); | |
874 | p_mask <<= 1; | |
af3feefa | 875 | } |
39280970 | 876 | show_line_to_eol(sl->bol, sl->len, c_reset); |
af3feefa JH |
877 | } |
878 | } | |
879 | } | |
880 | ||
3c39e9bd JH |
881 | static void reuse_combine_diff(struct sline *sline, unsigned long cnt, |
882 | int i, int j) | |
883 | { | |
884 | /* We have already examined parent j and we know parent i | |
885 | * and parent j are the same, so reuse the combined result | |
886 | * of parent j for parent i. | |
887 | */ | |
888 | unsigned long lno, imask, jmask; | |
889 | imask = (1UL<<i); | |
890 | jmask = (1UL<<j); | |
891 | ||
8a470ebf | 892 | for (lno = 0; lno <= cnt; lno++) { |
99d32060 | 893 | struct lline *ll = sline->lost; |
f16706cc | 894 | sline->p_lno[i] = sline->p_lno[j]; |
3c39e9bd JH |
895 | while (ll) { |
896 | if (ll->parent_map & jmask) | |
897 | ll->parent_map |= imask; | |
898 | ll = ll->next; | |
899 | } | |
46dc9412 JH |
900 | if (sline->flag & jmask) |
901 | sline->flag |= imask; | |
3c39e9bd JH |
902 | sline++; |
903 | } | |
4462731e JH |
904 | /* the overall size of the file (sline[cnt]) */ |
905 | sline->p_lno[i] = sline->p_lno[j]; | |
3c39e9bd JH |
906 | } |
907 | ||
462a15bc JH |
908 | static void dump_quoted_path(const char *head, |
909 | const char *prefix, | |
910 | const char *path, | |
41ee2ad6 | 911 | const char *line_prefix, |
567a03d1 | 912 | const char *c_meta, const char *c_reset) |
eab144ac | 913 | { |
462a15bc JH |
914 | static struct strbuf buf = STRBUF_INIT; |
915 | ||
916 | strbuf_reset(&buf); | |
41ee2ad6 | 917 | strbuf_addstr(&buf, line_prefix); |
462a15bc JH |
918 | strbuf_addstr(&buf, c_meta); |
919 | strbuf_addstr(&buf, head); | |
d5625091 | 920 | quote_two_c_style(&buf, prefix, path, 0); |
462a15bc JH |
921 | strbuf_addstr(&buf, c_reset); |
922 | puts(buf.buf); | |
eab144ac LT |
923 | } |
924 | ||
7c978a06 JK |
925 | static void show_combined_header(struct combine_diff_path *elem, |
926 | int num_parent, | |
7c978a06 | 927 | struct rev_info *rev, |
41ee2ad6 | 928 | const char *line_prefix, |
4d5f3471 JK |
929 | int mode_differs, |
930 | int show_file_header) | |
7c978a06 JK |
931 | { |
932 | struct diff_options *opt = &rev->diffopt; | |
976ff7e4 | 933 | int abbrev = opt->flags.full_index ? the_hash_algo->hexsz : DEFAULT_ABBREV; |
7c978a06 JK |
934 | const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/"; |
935 | const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/"; | |
f1c96261 JK |
936 | const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO); |
937 | const char *c_reset = diff_get_color_opt(opt, DIFF_RESET); | |
7c978a06 JK |
938 | const char *abb; |
939 | int added = 0; | |
940 | int deleted = 0; | |
941 | int i; | |
d01141de | 942 | int dense = rev->dense_combined_merges; |
7c978a06 JK |
943 | |
944 | if (rev->loginfo && !rev->no_commit_id) | |
945 | show_log(rev); | |
946 | ||
947 | dump_quoted_path(dense ? "diff --cc " : "diff --combined ", | |
41ee2ad6 JK |
948 | "", elem->path, line_prefix, c_meta, c_reset); |
949 | printf("%s%sindex ", line_prefix, c_meta); | |
7c978a06 | 950 | for (i = 0; i < num_parent; i++) { |
aab9583f | 951 | abb = find_unique_abbrev(&elem->parent[i].oid, |
7c978a06 JK |
952 | abbrev); |
953 | printf("%s%s", i ? "," : "", abb); | |
954 | } | |
aab9583f | 955 | abb = find_unique_abbrev(&elem->oid, abbrev); |
7c978a06 JK |
956 | printf("..%s%s\n", abb, c_reset); |
957 | ||
958 | if (mode_differs) { | |
959 | deleted = !elem->mode; | |
960 | ||
961 | /* We say it was added if nobody had it */ | |
962 | added = !deleted; | |
963 | for (i = 0; added && i < num_parent; i++) | |
964 | if (elem->parent[i].status != | |
965 | DIFF_STATUS_ADDED) | |
966 | added = 0; | |
967 | if (added) | |
41ee2ad6 JK |
968 | printf("%s%snew file mode %06o", |
969 | line_prefix, c_meta, elem->mode); | |
7c978a06 JK |
970 | else { |
971 | if (deleted) | |
41ee2ad6 JK |
972 | printf("%s%sdeleted file ", |
973 | line_prefix, c_meta); | |
7c978a06 JK |
974 | printf("mode "); |
975 | for (i = 0; i < num_parent; i++) { | |
976 | printf("%s%06o", i ? "," : "", | |
977 | elem->parent[i].mode); | |
978 | } | |
979 | if (elem->mode) | |
980 | printf("..%06o", elem->mode); | |
981 | } | |
982 | printf("%s\n", c_reset); | |
983 | } | |
984 | ||
4d5f3471 JK |
985 | if (!show_file_header) |
986 | return; | |
987 | ||
d76ce4f7 EN |
988 | if (rev->combined_all_paths) { |
989 | for (i = 0; i < num_parent; i++) { | |
990 | char *path = filename_changed(elem->parent[i].status) | |
991 | ? elem->parent[i].path.buf : elem->path; | |
992 | if (elem->parent[i].status == DIFF_STATUS_ADDED) | |
993 | dump_quoted_path("--- ", "", "/dev/null", | |
994 | line_prefix, c_meta, c_reset); | |
995 | else | |
996 | dump_quoted_path("--- ", a_prefix, path, | |
997 | line_prefix, c_meta, c_reset); | |
998 | } | |
999 | } else { | |
1000 | if (added) | |
1001 | dump_quoted_path("--- ", "", "/dev/null", | |
1002 | line_prefix, c_meta, c_reset); | |
1003 | else | |
1004 | dump_quoted_path("--- ", a_prefix, elem->path, | |
1005 | line_prefix, c_meta, c_reset); | |
1006 | } | |
7c978a06 JK |
1007 | if (deleted) |
1008 | dump_quoted_path("+++ ", "", "/dev/null", | |
41ee2ad6 | 1009 | line_prefix, c_meta, c_reset); |
7c978a06 JK |
1010 | else |
1011 | dump_quoted_path("+++ ", b_prefix, elem->path, | |
41ee2ad6 | 1012 | line_prefix, c_meta, c_reset); |
7c978a06 JK |
1013 | } |
1014 | ||
89b0c4b5 | 1015 | static void show_patch_diff(struct combine_diff_path *elem, int num_parent, |
d01141de | 1016 | int working_tree_file, |
99694544 | 1017 | struct rev_info *rev) |
af3feefa | 1018 | { |
91539833 | 1019 | struct diff_options *opt = &rev->diffopt; |
f23fc773 | 1020 | unsigned long result_size, cnt, lno; |
21798708 | 1021 | int result_deleted = 0; |
310f8b5b | 1022 | char *result, *cp; |
af3feefa | 1023 | struct sline *sline; /* survived lines */ |
2454c962 | 1024 | int mode_differs = 0; |
89b0c4b5 | 1025 | int i, show_hunks; |
f23fc773 | 1026 | mmfile_t result_file; |
4d5f3471 | 1027 | struct userdiff_driver *userdiff; |
0508fe53 | 1028 | struct userdiff_driver *textconv = NULL; |
4d5f3471 | 1029 | int is_binary; |
41ee2ad6 | 1030 | const char *line_prefix = diff_line_prefix(opt); |
af3feefa | 1031 | |
ee1e5412 | 1032 | context = opt->context; |
acd00ea0 | 1033 | userdiff = userdiff_find_by_path(opt->repo->index, elem->path); |
4d5f3471 JK |
1034 | if (!userdiff) |
1035 | userdiff = userdiff_find_by_name("default"); | |
0d1e0e78 | 1036 | if (opt->flags.allow_textconv) |
bd7ad45b | 1037 | textconv = userdiff_get_textconv(opt->repo, userdiff); |
a5a818ee | 1038 | |
af3feefa | 1039 | /* Read the result of merge first */ |
f23fc773 | 1040 | if (!working_tree_file) |
6afaf807 | 1041 | result = grab_blob(opt->repo, &elem->oid, elem->mode, &result_size, |
0508fe53 | 1042 | textconv, elem->path); |
ea726d02 | 1043 | else { |
9843a1f6 | 1044 | /* Used by diff-tree to read from the working tree */ |
ea726d02 | 1045 | struct stat st; |
4fc970c4 JH |
1046 | int fd = -1; |
1047 | ||
1048 | if (lstat(elem->path, &st) < 0) | |
1049 | goto deleted_file; | |
1050 | ||
1051 | if (S_ISLNK(st.st_mode)) { | |
912342d9 JH |
1052 | struct strbuf buf = STRBUF_INIT; |
1053 | ||
1054 | if (strbuf_readlink(&buf, elem->path, st.st_size) < 0) { | |
4b94ec9b | 1055 | error_errno("readlink(%s)", elem->path); |
4fc970c4 JH |
1056 | return; |
1057 | } | |
912342d9 JH |
1058 | result_size = buf.len; |
1059 | result = strbuf_detach(&buf, NULL); | |
4fc970c4 | 1060 | elem->mode = canon_mode(st.st_mode); |
7dae8b21 | 1061 | } else if (S_ISDIR(st.st_mode)) { |
1ff57c13 | 1062 | struct object_id oid; |
a98e6101 | 1063 | if (resolve_gitlink_ref(elem->path, "HEAD", &oid) < 0) |
6afaf807 NTND |
1064 | result = grab_blob(opt->repo, &elem->oid, |
1065 | elem->mode, &result_size, | |
1066 | NULL, NULL); | |
7dae8b21 | 1067 | else |
6afaf807 | 1068 | result = grab_blob(opt->repo, &oid, elem->mode, |
0508fe53 JK |
1069 | &result_size, NULL, NULL); |
1070 | } else if (textconv) { | |
1071 | struct diff_filespec *df = alloc_filespec(elem->path); | |
14228447 | 1072 | fill_filespec(df, null_oid(), 0, st.st_mode); |
6afaf807 | 1073 | result_size = fill_textconv(opt->repo, textconv, df, &result); |
0508fe53 | 1074 | free_filespec(df); |
91fcbcbd | 1075 | } else if (0 <= (fd = open(elem->path, O_RDONLY))) { |
dc49cd76 | 1076 | size_t len = xsize_t(st.st_size); |
c697ad14 | 1077 | ssize_t done; |
a249a9b5 | 1078 | int is_file, i; |
ea726d02 | 1079 | |
1b0c7174 | 1080 | elem->mode = canon_mode(st.st_mode); |
a249a9b5 JS |
1081 | /* if symlinks don't work, assume symlink if all parents |
1082 | * are symlinks | |
1083 | */ | |
1084 | is_file = has_symlinks; | |
1085 | for (i = 0; !is_file && i < num_parent; i++) | |
1086 | is_file = !S_ISLNK(elem->parent[i].mode); | |
1087 | if (!is_file) | |
1088 | elem->mode = canon_mode(S_IFLNK); | |
1089 | ||
f23fc773 | 1090 | result_size = len; |
3733e694 | 1091 | result = xmallocz(len); |
c697ad14 HO |
1092 | |
1093 | done = read_in_full(fd, result, len); | |
1094 | if (done < 0) | |
0721c314 | 1095 | die_errno("read error '%s'", elem->path); |
c697ad14 HO |
1096 | else if (done < len) |
1097 | die("early EOF '%s'", elem->path); | |
1098 | ||
5e568f9e AG |
1099 | /* If not a fake symlink, apply filters, e.g. autocrlf */ |
1100 | if (is_file) { | |
f285a2d7 | 1101 | struct strbuf buf = STRBUF_INIT; |
5e568f9e | 1102 | |
0734f209 NTND |
1103 | if (convert_to_git(rev->diffopt.repo->index, |
1104 | elem->path, result, len, &buf, global_conv_flags_eol)) { | |
5e568f9e AG |
1105 | free(result); |
1106 | result = strbuf_detach(&buf, &len); | |
1107 | result_size = len; | |
1108 | } | |
1109 | } | |
ea726d02 JH |
1110 | } |
1111 | else { | |
4fc970c4 | 1112 | deleted_file: |
21798708 | 1113 | result_deleted = 1; |
f23fc773 | 1114 | result_size = 0; |
713a11fc | 1115 | elem->mode = 0; |
28f75818 | 1116 | result = xcalloc(1, 1); |
ea726d02 | 1117 | } |
4fc970c4 | 1118 | |
ea726d02 JH |
1119 | if (0 <= fd) |
1120 | close(fd); | |
1121 | } | |
af3feefa | 1122 | |
c95b99bb JK |
1123 | for (i = 0; i < num_parent; i++) { |
1124 | if (elem->parent[i].mode != elem->mode) { | |
1125 | mode_differs = 1; | |
1126 | break; | |
1127 | } | |
1128 | } | |
1129 | ||
0508fe53 JK |
1130 | if (textconv) |
1131 | is_binary = 0; | |
1132 | else if (userdiff->binary != -1) | |
4d5f3471 JK |
1133 | is_binary = userdiff->binary; |
1134 | else { | |
1135 | is_binary = buffer_is_binary(result, result_size); | |
1136 | for (i = 0; !is_binary && i < num_parent; i++) { | |
1137 | char *buf; | |
1138 | unsigned long size; | |
6afaf807 NTND |
1139 | buf = grab_blob(opt->repo, |
1140 | &elem->parent[i].oid, | |
4d5f3471 | 1141 | elem->parent[i].mode, |
0508fe53 | 1142 | &size, NULL, NULL); |
4d5f3471 JK |
1143 | if (buffer_is_binary(buf, size)) |
1144 | is_binary = 1; | |
1145 | free(buf); | |
1146 | } | |
1147 | } | |
1148 | if (is_binary) { | |
d01141de | 1149 | show_combined_header(elem, num_parent, rev, |
41ee2ad6 | 1150 | line_prefix, mode_differs, 0); |
4d5f3471 JK |
1151 | printf("Binary files differ\n"); |
1152 | free(result); | |
1153 | return; | |
1154 | } | |
1155 | ||
2386c297 | 1156 | for (cnt = 0, cp = result; cp < result + result_size; cp++) { |
af3feefa JH |
1157 | if (*cp == '\n') |
1158 | cnt++; | |
1159 | } | |
f23fc773 | 1160 | if (result_size && result[result_size-1] != '\n') |
af3feefa JH |
1161 | cnt++; /* incomplete line */ |
1162 | ||
ca56dadb | 1163 | CALLOC_ARRAY(sline, st_add(cnt, 2)); |
af3feefa | 1164 | sline[0].bol = result; |
2386c297 | 1165 | for (lno = 0, cp = result; cp < result + result_size; cp++) { |
af3feefa JH |
1166 | if (*cp == '\n') { |
1167 | sline[lno].len = cp - sline[lno].bol; | |
af3feefa JH |
1168 | lno++; |
1169 | if (lno < cnt) | |
1170 | sline[lno].bol = cp + 1; | |
1171 | } | |
1172 | } | |
f23fc773 JH |
1173 | if (result_size && result[result_size-1] != '\n') |
1174 | sline[cnt-1].len = result_size - (sline[cnt-1].bol - result); | |
1175 | ||
1176 | result_file.ptr = result; | |
1177 | result_file.size = result_size; | |
af3feefa | 1178 | |
8a470ebf JH |
1179 | /* Even p_lno[cnt+1] is valid -- that is for the end line number |
1180 | * for deletion hunk at the end. | |
1181 | */ | |
ca56dadb | 1182 | CALLOC_ARRAY(sline[0].p_lno, st_mult(st_add(cnt, 2), num_parent)); |
8a470ebf | 1183 | for (lno = 0; lno <= cnt; lno++) |
f16706cc JH |
1184 | sline[lno+1].p_lno = sline[lno].p_lno + num_parent; |
1185 | ||
3c39e9bd JH |
1186 | for (i = 0; i < num_parent; i++) { |
1187 | int j; | |
1188 | for (j = 0; j < i; j++) { | |
4a7e27e9 JK |
1189 | if (oideq(&elem->parent[i].oid, |
1190 | &elem->parent[j].oid)) { | |
3c39e9bd JH |
1191 | reuse_combine_diff(sline, cnt, i, j); |
1192 | break; | |
1193 | } | |
1194 | } | |
1195 | if (i <= j) | |
6afaf807 NTND |
1196 | combine_diff(opt->repo, |
1197 | &elem->parent[i].oid, | |
7dae8b21 JH |
1198 | elem->parent[i].mode, |
1199 | &result_file, sline, | |
0508fe53 | 1200 | cnt, i, num_parent, result_deleted, |
fa04ae0b | 1201 | textconv, elem->path, opt->xdl_opts); |
3c39e9bd | 1202 | } |
af3feefa | 1203 | |
d01141de | 1204 | show_hunks = make_hunks(sline, cnt, num_parent, rev->dense_combined_merges); |
af3feefa | 1205 | |
713a11fc | 1206 | if (show_hunks || mode_differs || working_tree_file) { |
d01141de | 1207 | show_combined_header(elem, num_parent, rev, |
41ee2ad6 JK |
1208 | line_prefix, mode_differs, 1); |
1209 | dump_sline(sline, line_prefix, cnt, num_parent, | |
f1c96261 | 1210 | opt->use_color, result_deleted); |
8828cdcb | 1211 | } |
af3feefa JH |
1212 | free(result); |
1213 | ||
2386c297 | 1214 | for (lno = 0; lno < cnt; lno++) { |
99d32060 AP |
1215 | if (sline[lno].lost) { |
1216 | struct lline *ll = sline[lno].lost; | |
af3feefa JH |
1217 | while (ll) { |
1218 | struct lline *tmp = ll; | |
1219 | ll = ll->next; | |
1220 | free(tmp); | |
1221 | } | |
1222 | } | |
1223 | } | |
46dc9412 | 1224 | free(sline[0].p_lno); |
af3feefa JH |
1225 | free(sline); |
1226 | } | |
1227 | ||
91539833 | 1228 | static void show_raw_diff(struct combine_diff_path *p, int num_parent, struct rev_info *rev) |
ee638024 | 1229 | { |
91539833 | 1230 | struct diff_options *opt = &rev->diffopt; |
77667051 | 1231 | int line_termination, inter_name_termination, i; |
41ee2ad6 | 1232 | const char *line_prefix = diff_line_prefix(opt); |
ee638024 LT |
1233 | |
1234 | line_termination = opt->line_termination; | |
1235 | inter_name_termination = '\t'; | |
1236 | if (!line_termination) | |
1237 | inter_name_termination = 0; | |
1238 | ||
44152787 | 1239 | if (rev->loginfo && !rev->no_commit_id) |
02865655 | 1240 | show_log(rev); |
ee638024 | 1241 | |
a1d68bea | 1242 | |
c6744349 | 1243 | if (opt->output_format & DIFF_FORMAT_RAW) { |
41ee2ad6 | 1244 | printf("%s", line_prefix); |
a1d68bea | 1245 | |
77667051 JH |
1246 | /* As many colons as there are parents */ |
1247 | for (i = 0; i < num_parent; i++) | |
1248 | putchar(':'); | |
0a798076 JH |
1249 | |
1250 | /* Show the modes */ | |
77667051 JH |
1251 | for (i = 0; i < num_parent; i++) |
1252 | printf("%06o ", p->parent[i].mode); | |
1253 | printf("%06o", p->mode); | |
0a798076 JH |
1254 | |
1255 | /* Show sha1's */ | |
1256 | for (i = 0; i < num_parent; i++) | |
d6cece51 | 1257 | printf(" %s", diff_aligned_abbrev(&p->parent[i].oid, |
d5e3b01e | 1258 | opt->abbrev)); |
d6cece51 | 1259 | printf(" %s ", diff_aligned_abbrev(&p->oid, opt->abbrev)); |
0a798076 JH |
1260 | } |
1261 | ||
c6744349 | 1262 | if (opt->output_format & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) { |
d416df88 JH |
1263 | for (i = 0; i < num_parent; i++) |
1264 | putchar(p->parent[i].status); | |
1265 | putchar(inter_name_termination); | |
1266 | } | |
0a798076 | 1267 | |
d76ce4f7 EN |
1268 | for (i = 0; i < num_parent; i++) |
1269 | if (rev->combined_all_paths) { | |
1270 | if (filename_changed(p->parent[i].status)) | |
1271 | write_name_quoted(p->parent[i].path.buf, stdout, | |
1272 | inter_name_termination); | |
1273 | else | |
1274 | write_name_quoted(p->path, stdout, | |
1275 | inter_name_termination); | |
1276 | } | |
663af342 | 1277 | write_name_quoted(p->path, stdout, line_termination); |
0a798076 JH |
1278 | } |
1279 | ||
99694544 JH |
1280 | /* |
1281 | * The result (p->elem) is from the working tree and their | |
1282 | * parents are typically from multiple stages during a merge | |
1283 | * (i.e. diff-files) or the state in HEAD and in the index | |
1284 | * (i.e. diff-index). | |
1285 | */ | |
91539833 | 1286 | void show_combined_diff(struct combine_diff_path *p, |
0a798076 | 1287 | int num_parent, |
91539833 | 1288 | struct rev_info *rev) |
0a798076 | 1289 | { |
91539833 | 1290 | struct diff_options *opt = &rev->diffopt; |
41ee2ad6 | 1291 | |
c6744349 TH |
1292 | if (opt->output_format & (DIFF_FORMAT_RAW | |
1293 | DIFF_FORMAT_NAME | | |
89b0c4b5 | 1294 | DIFF_FORMAT_NAME_STATUS)) |
91539833 | 1295 | show_raw_diff(p, num_parent, rev); |
89b0c4b5 | 1296 | else if (opt->output_format & DIFF_FORMAT_PATCH) |
d01141de | 1297 | show_patch_diff(p, num_parent, 1, rev); |
ee638024 LT |
1298 | } |
1299 | ||
25e5e2bf JH |
1300 | static void free_combined_pair(struct diff_filepair *pair) |
1301 | { | |
1302 | free(pair->two); | |
1303 | free(pair); | |
1304 | } | |
1305 | ||
1306 | /* | |
1307 | * A combine_diff_path expresses N parents on the LHS against 1 merge | |
1308 | * result. Synthesize a diff_filepair that has N entries on the "one" | |
1309 | * side and 1 entry on the "two" side. | |
1310 | * | |
1311 | * In the future, we might want to add more data to combine_diff_path | |
1312 | * so that we can fill fields we are ignoring (most notably, size) here, | |
1313 | * but currently nobody uses it, so this should suffice for now. | |
1314 | */ | |
1315 | static struct diff_filepair *combined_pair(struct combine_diff_path *p, | |
1316 | int num_parent) | |
1317 | { | |
1318 | int i; | |
1319 | struct diff_filepair *pair; | |
1320 | struct diff_filespec *pool; | |
1321 | ||
1322 | pair = xmalloc(sizeof(*pair)); | |
ca56dadb | 1323 | CALLOC_ARRAY(pool, st_add(num_parent, 1)); |
25e5e2bf JH |
1324 | pair->one = pool + 1; |
1325 | pair->two = pool; | |
1326 | ||
1327 | for (i = 0; i < num_parent; i++) { | |
1328 | pair->one[i].path = p->path; | |
1329 | pair->one[i].mode = p->parent[i].mode; | |
a0d12c44 | 1330 | oidcpy(&pair->one[i].oid, &p->parent[i].oid); |
41c9560e | 1331 | pair->one[i].oid_valid = !is_null_oid(&p->parent[i].oid); |
25e5e2bf JH |
1332 | pair->one[i].has_more_entries = 1; |
1333 | } | |
1334 | pair->one[num_parent - 1].has_more_entries = 0; | |
1335 | ||
1336 | pair->two->path = p->path; | |
1337 | pair->two->mode = p->mode; | |
a0d12c44 | 1338 | oidcpy(&pair->two->oid, &p->oid); |
41c9560e | 1339 | pair->two->oid_valid = !is_null_oid(&p->oid); |
25e5e2bf JH |
1340 | return pair; |
1341 | } | |
1342 | ||
1343 | static void handle_combined_callback(struct diff_options *opt, | |
1344 | struct combine_diff_path *paths, | |
1345 | int num_parent, | |
1346 | int num_paths) | |
1347 | { | |
1348 | struct combine_diff_path *p; | |
1349 | struct diff_queue_struct q; | |
1350 | int i; | |
1351 | ||
ca56dadb | 1352 | CALLOC_ARRAY(q.queue, num_paths); |
25e5e2bf JH |
1353 | q.alloc = num_paths; |
1354 | q.nr = num_paths; | |
af82c788 | 1355 | for (i = 0, p = paths; p; p = p->next) |
25e5e2bf | 1356 | q.queue[i++] = combined_pair(p, num_parent); |
25e5e2bf JH |
1357 | opt->format_callback(&q, opt, opt->format_callback_data); |
1358 | for (i = 0; i < num_paths; i++) | |
1359 | free_combined_pair(q.queue[i]); | |
1360 | free(q.queue); | |
1361 | } | |
1362 | ||
8518ff8f KS |
1363 | static const char *path_path(void *obj) |
1364 | { | |
1365 | struct combine_diff_path *path = (struct combine_diff_path *)obj; | |
1366 | ||
1367 | return path->path; | |
1368 | } | |
1369 | ||
8817f0cc JK |
1370 | /* |
1371 | * Diff stat formats which we always compute solely against the first parent. | |
1372 | */ | |
1373 | #define STAT_FORMAT_MASK (DIFF_FORMAT_NUMSTAT \ | |
8290faa0 | 1374 | | DIFF_FORMAT_SHORTSTAT \ |
04b19fca | 1375 | | DIFF_FORMAT_SUMMARY \ |
dac03b55 | 1376 | | DIFF_FORMAT_DIRSTAT \ |
8817f0cc | 1377 | | DIFF_FORMAT_DIFFSTAT) |
eeb3f328 KS |
1378 | |
1379 | /* find set of paths that every parent touches */ | |
09fae19a | 1380 | static struct combine_diff_path *find_paths_generic(const struct object_id *oid, |
d76ce4f7 EN |
1381 | const struct oid_array *parents, |
1382 | struct diff_options *opt, | |
1383 | int combined_all_paths) | |
eeb3f328 KS |
1384 | { |
1385 | struct combine_diff_path *paths = NULL; | |
1386 | int i, num_parent = parents->nr; | |
1387 | ||
1388 | int output_format = opt->output_format; | |
1389 | const char *orderfile = opt->orderfile; | |
1390 | ||
1391 | opt->output_format = DIFF_FORMAT_NO_OUTPUT; | |
1392 | /* tell diff_tree to emit paths in sorted (=tree) order */ | |
1393 | opt->orderfile = NULL; | |
1394 | ||
7195fbfa | 1395 | /* D(A,P1...Pn) = D(A,P1) ^ ... ^ D(A,Pn) (wrt paths) */ |
eeb3f328 KS |
1396 | for (i = 0; i < num_parent; i++) { |
1397 | /* | |
1398 | * show stat against the first parent even when doing | |
1399 | * combined diff. | |
1400 | */ | |
8817f0cc | 1401 | int stat_opt = output_format & STAT_FORMAT_MASK; |
eeb3f328 KS |
1402 | if (i == 0 && stat_opt) |
1403 | opt->output_format = stat_opt; | |
1404 | else | |
1405 | opt->output_format = DIFF_FORMAT_NO_OUTPUT; | |
66f414f8 | 1406 | diff_tree_oid(&parents->oid[i], oid, "", opt); |
eeb3f328 | 1407 | diffcore_std(opt); |
d76ce4f7 EN |
1408 | paths = intersect_paths(paths, i, num_parent, |
1409 | combined_all_paths); | |
eeb3f328 KS |
1410 | |
1411 | /* if showing diff, show it in requested order */ | |
1412 | if (opt->output_format != DIFF_FORMAT_NO_OUTPUT && | |
1413 | orderfile) { | |
1414 | diffcore_order(orderfile); | |
1415 | } | |
1416 | ||
1417 | diff_flush(opt); | |
1418 | } | |
1419 | ||
1420 | opt->output_format = output_format; | |
1421 | opt->orderfile = orderfile; | |
1422 | return paths; | |
1423 | } | |
1424 | ||
1425 | ||
7195fbfa KS |
1426 | /* |
1427 | * find set of paths that everybody touches, assuming diff is run without | |
1428 | * rename/copy detection, etc, comparing all trees simultaneously (= faster). | |
1429 | */ | |
1430 | static struct combine_diff_path *find_paths_multitree( | |
09fae19a | 1431 | const struct object_id *oid, const struct oid_array *parents, |
7195fbfa KS |
1432 | struct diff_options *opt) |
1433 | { | |
1434 | int i, nparent = parents->nr; | |
fda94b41 | 1435 | const struct object_id **parents_oid; |
7195fbfa KS |
1436 | struct combine_diff_path paths_head; |
1437 | struct strbuf base; | |
1438 | ||
fda94b41 | 1439 | ALLOC_ARRAY(parents_oid, nparent); |
7195fbfa | 1440 | for (i = 0; i < nparent; i++) |
fda94b41 | 1441 | parents_oid[i] = &parents->oid[i]; |
7195fbfa KS |
1442 | |
1443 | /* fake list head, so worker can assume it is non-NULL */ | |
1444 | paths_head.next = NULL; | |
1445 | ||
1446 | strbuf_init(&base, PATH_MAX); | |
fda94b41 | 1447 | diff_tree_paths(&paths_head, oid, parents_oid, nparent, &base, opt); |
7195fbfa KS |
1448 | |
1449 | strbuf_release(&base); | |
fda94b41 | 1450 | free(parents_oid); |
7195fbfa KS |
1451 | return paths_head.next; |
1452 | } | |
1453 | ||
957876f1 JK |
1454 | static int match_objfind(struct combine_diff_path *path, |
1455 | int num_parent, | |
1456 | const struct oidset *set) | |
1457 | { | |
1458 | int i; | |
1459 | if (oidset_contains(set, &path->oid)) | |
1460 | return 1; | |
1461 | for (i = 0; i < num_parent; i++) { | |
1462 | if (oidset_contains(set, &path->parent[i].oid)) | |
1463 | return 1; | |
1464 | } | |
1465 | return 0; | |
1466 | } | |
1467 | ||
1468 | static struct combine_diff_path *combined_objfind(struct diff_options *opt, | |
1469 | struct combine_diff_path *paths, | |
1470 | int num_parent) | |
1471 | { | |
1472 | struct combine_diff_path *ret = NULL, **tail = &ret; | |
1473 | struct combine_diff_path *p = paths; | |
1474 | ||
1475 | while (p) { | |
1476 | struct combine_diff_path *next = p->next; | |
1477 | ||
1478 | if (match_objfind(p, num_parent, opt->objfind)) { | |
1479 | p->next = NULL; | |
1480 | *tail = p; | |
1481 | tail = &p->next; | |
1482 | } else { | |
1483 | free(p); | |
1484 | } | |
1485 | p = next; | |
1486 | } | |
1487 | ||
1488 | return ret; | |
1489 | } | |
7195fbfa | 1490 | |
b9acf54d | 1491 | void diff_tree_combined(const struct object_id *oid, |
910650d2 | 1492 | const struct oid_array *parents, |
0fe7c1de | 1493 | struct rev_info *rev) |
af3feefa | 1494 | { |
91539833 | 1495 | struct diff_options *opt = &rev->diffopt; |
af3feefa | 1496 | struct diff_options diffopts; |
eeb3f328 | 1497 | struct combine_diff_path *p, *paths; |
0041f09d | 1498 | int i, num_paths, needsep, show_log_first, num_parent = parents->nr; |
7195fbfa | 1499 | int need_generic_pathscan; |
af3feefa | 1500 | |
51af1886 KS |
1501 | /* nothing to do, if no parents */ |
1502 | if (!num_parent) | |
1503 | return; | |
1504 | ||
1505 | show_log_first = !!rev->loginfo && !rev->no_commit_id; | |
1506 | needsep = 0; | |
1507 | if (show_log_first) { | |
1508 | show_log(rev); | |
1509 | ||
e7cc0ede | 1510 | if (rev->verbose_header && opt->output_format && |
b9c7d6e4 JK |
1511 | opt->output_format != DIFF_FORMAT_NO_OUTPUT && |
1512 | !commit_format_is_empty(rev->commit_format)) | |
51af1886 KS |
1513 | printf("%s%c", diff_line_prefix(opt), |
1514 | opt->line_termination); | |
1515 | } | |
1516 | ||
5b236832 | 1517 | diffopts = *opt; |
bd1928df | 1518 | copy_pathspec(&diffopts.pathspec, &opt->pathspec); |
0d1e0e78 BW |
1519 | diffopts.flags.recursive = 1; |
1520 | diffopts.flags.allow_external = 0; | |
af3feefa | 1521 | |
eeb3f328 KS |
1522 | /* find set of paths that everybody touches |
1523 | * | |
7195fbfa KS |
1524 | * NOTE |
1525 | * | |
1526 | * Diffcore transformations are bound to diff_filespec and logic | |
1527 | * comparing two entries - i.e. they do not apply directly to combine | |
1528 | * diff. | |
1529 | * | |
1530 | * If some of such transformations is requested - we launch generic | |
1531 | * path scanning, which works significantly slower compared to | |
1532 | * simultaneous all-trees-in-one-go scan in find_paths_multitree(). | |
1533 | * | |
1534 | * TODO some of the filters could be ported to work on | |
1535 | * combine_diff_paths - i.e. all functionality that skips paths, so in | |
1536 | * theory, we could end up having only multitree path scanning. | |
1537 | * | |
1538 | * NOTE please keep this semantically in sync with diffcore_std() | |
eeb3f328 | 1539 | */ |
7195fbfa | 1540 | need_generic_pathscan = opt->skip_stat_unmatch || |
0d1e0e78 | 1541 | opt->flags.follow_renames || |
7195fbfa KS |
1542 | opt->break_opt != -1 || |
1543 | opt->detect_rename || | |
957876f1 JK |
1544 | (opt->pickaxe_opts & |
1545 | (DIFF_PICKAXE_KINDS_MASK & ~DIFF_PICKAXE_KIND_OBJFIND)) || | |
7195fbfa KS |
1546 | opt->filter; |
1547 | ||
7195fbfa KS |
1548 | if (need_generic_pathscan) { |
1549 | /* | |
1550 | * NOTE generic case also handles --stat, as it computes | |
1551 | * diff(sha1,parent_i) for all i to do the job, specifically | |
1552 | * for parent0. | |
1553 | */ | |
d76ce4f7 EN |
1554 | paths = find_paths_generic(oid, parents, &diffopts, |
1555 | rev->combined_all_paths); | |
7195fbfa KS |
1556 | } |
1557 | else { | |
1558 | int stat_opt; | |
09fae19a | 1559 | paths = find_paths_multitree(oid, parents, &diffopts); |
7195fbfa | 1560 | |
957876f1 JK |
1561 | if (opt->pickaxe_opts & DIFF_PICKAXE_KIND_OBJFIND) |
1562 | paths = combined_objfind(opt, paths, num_parent); | |
1563 | ||
7195fbfa KS |
1564 | /* |
1565 | * show stat against the first parent even | |
1566 | * when doing combined diff. | |
1567 | */ | |
8817f0cc | 1568 | stat_opt = opt->output_format & STAT_FORMAT_MASK; |
7195fbfa KS |
1569 | if (stat_opt) { |
1570 | diffopts.output_format = stat_opt; | |
1571 | ||
66f414f8 | 1572 | diff_tree_oid(&parents->oid[0], oid, "", &diffopts); |
7195fbfa KS |
1573 | diffcore_std(&diffopts); |
1574 | if (opt->orderfile) | |
1575 | diffcore_order(opt->orderfile); | |
1576 | diff_flush(&diffopts); | |
1577 | } | |
1578 | } | |
af3feefa | 1579 | |
af82c788 KS |
1580 | /* find out number of surviving paths */ |
1581 | for (num_paths = 0, p = paths; p; p = p->next) | |
1582 | num_paths++; | |
8518ff8f KS |
1583 | |
1584 | /* order paths according to diffcore_order */ | |
1585 | if (opt->orderfile && num_paths) { | |
1586 | struct obj_order *o; | |
1587 | ||
b32fa95f | 1588 | ALLOC_ARRAY(o, num_paths); |
8518ff8f KS |
1589 | for (i = 0, p = paths; p; p = p->next, i++) |
1590 | o[i].obj = p; | |
1591 | order_objects(opt->orderfile, path_path, o, num_paths); | |
1592 | for (i = 0; i < num_paths - 1; i++) { | |
1593 | p = o[i].obj; | |
1594 | p->next = o[i+1].obj; | |
1595 | } | |
1596 | ||
1597 | p = o[num_paths-1].obj; | |
1598 | p->next = NULL; | |
1599 | paths = o[0].obj; | |
1600 | free(o); | |
1601 | } | |
1602 | ||
1603 | ||
e3c3a550 | 1604 | if (num_paths) { |
c6744349 TH |
1605 | if (opt->output_format & (DIFF_FORMAT_RAW | |
1606 | DIFF_FORMAT_NAME | | |
1607 | DIFF_FORMAT_NAME_STATUS)) { | |
af82c788 KS |
1608 | for (p = paths; p; p = p->next) |
1609 | show_raw_diff(p, num_parent, rev); | |
3969cf7d | 1610 | needsep = 1; |
86ff1d20 | 1611 | } |
8817f0cc | 1612 | else if (opt->output_format & STAT_FORMAT_MASK) |
3969cf7d | 1613 | needsep = 1; |
25e5e2bf JH |
1614 | else if (opt->output_format & DIFF_FORMAT_CALLBACK) |
1615 | handle_combined_callback(opt, paths, num_parent, num_paths); | |
1616 | ||
c6744349 | 1617 | if (opt->output_format & DIFF_FORMAT_PATCH) { |
3969cf7d | 1618 | if (needsep) |
41ee2ad6 JK |
1619 | printf("%s%c", diff_line_prefix(opt), |
1620 | opt->line_termination); | |
af82c788 | 1621 | for (p = paths; p; p = p->next) |
d01141de | 1622 | show_patch_diff(p, num_parent, 0, rev); |
af3feefa JH |
1623 | } |
1624 | } | |
1625 | ||
1626 | /* Clean things up */ | |
1627 | while (paths) { | |
ea726d02 | 1628 | struct combine_diff_path *tmp = paths; |
af3feefa | 1629 | paths = paths->next; |
d76ce4f7 EN |
1630 | for (i = 0; i < num_parent; i++) |
1631 | if (rev->combined_all_paths && | |
1632 | filename_changed(tmp->parent[i].status)) | |
1633 | strbuf_release(&tmp->parent[i].path); | |
af3feefa JH |
1634 | free(tmp); |
1635 | } | |
46ec510a | 1636 | |
ed6e8038 | 1637 | clear_pathspec(&diffopts.pathspec); |
af3feefa | 1638 | } |
0fe7c1de | 1639 | |
d01141de | 1640 | void diff_tree_combined_merge(const struct commit *commit, |
82889295 | 1641 | struct rev_info *rev) |
0fe7c1de | 1642 | { |
53d00b39 | 1643 | struct commit_list *parent = get_saved_parents(rev, commit); |
910650d2 | 1644 | struct oid_array parents = OID_ARRAY_INIT; |
0041f09d RS |
1645 | |
1646 | while (parent) { | |
910650d2 | 1647 | oid_array_append(&parents, &parent->item->object.oid); |
0041f09d RS |
1648 | parent = parent->next; |
1649 | } | |
d01141de | 1650 | diff_tree_combined(&commit->object.oid, &parents, rev); |
910650d2 | 1651 | oid_array_clear(&parents); |
0fe7c1de | 1652 | } |