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