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