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