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