8f295c89d0e75647f6bac0b370b8f25f5ebe5ec3
1#include "cache.h"
2#include "commit.h"
3#include "diff.h"
4#include "diffcore.h"
5#include "quote.h"
6
7static int uninteresting(struct diff_filepair *p)
8{
9 if (diff_unmodified_pair(p))
10 return 1;
11 if (!S_ISREG(p->one->mode) || !S_ISREG(p->two->mode))
12 return 1;
13 return 0;
14}
15
16static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
17{
18 struct diff_queue_struct *q = &diff_queued_diff;
19 struct combine_diff_path *p;
20 int i;
21
22 if (!n) {
23 struct combine_diff_path *list = NULL, **tail = &list;
24 for (i = 0; i < q->nr; i++) {
25 int len;
26 const char *path;
27 if (uninteresting(q->queue[i]))
28 continue;
29 path = q->queue[i]->two->path;
30 len = strlen(path);
31
32 p = xmalloc(sizeof(*p) + len + 1 + num_parent * 20);
33 p->path = (char*) &(p->parent_sha1[num_parent][0]);
34 memcpy(p->path, path, len);
35 p->path[len] = 0;
36 p->len = len;
37 p->next = NULL;
38 memcpy(p->sha1, q->queue[i]->two->sha1, 20);
39 memcpy(p->parent_sha1[n], q->queue[i]->one->sha1, 20);
40 *tail = p;
41 tail = &p->next;
42 }
43 return list;
44 }
45
46 for (p = curr; p; p = p->next) {
47 int found = 0;
48 if (!p->len)
49 continue;
50 for (i = 0; i < q->nr; i++) {
51 const char *path;
52 int len;
53
54 if (uninteresting(q->queue[i]))
55 continue;
56 path = q->queue[i]->two->path;
57 len = strlen(path);
58 if (len == p->len && !memcmp(path, p->path, len)) {
59 found = 1;
60 memcpy(p->parent_sha1[n],
61 q->queue[i]->one->sha1, 20);
62 break;
63 }
64 }
65 if (!found)
66 p->len = 0;
67 }
68 return curr;
69}
70
71struct lline {
72 struct lline *next;
73 int len;
74 unsigned long parent_map;
75 char line[FLEX_ARRAY];
76};
77
78struct sline {
79 struct lline *lost_head, **lost_tail;
80 char *bol;
81 int len;
82 unsigned long flag;
83};
84
85static char *grab_blob(const unsigned char *sha1, unsigned long *size)
86{
87 char *blob;
88 char type[20];
89 if (!memcmp(sha1, null_sha1, 20)) {
90 /* deleted blob */
91 *size = 0;
92 return xcalloc(1, 1);
93 }
94 blob = read_sha1_file(sha1, type, size);
95 if (strcmp(type, "blob"))
96 die("object '%s' is not a blob!", sha1_to_hex(sha1));
97 return blob;
98}
99
100#define TMPPATHLEN 50
101#define MAXLINELEN 10240
102
103static void write_to_temp_file(char *tmpfile, void *blob, unsigned long size)
104{
105 int fd = git_mkstemp(tmpfile, TMPPATHLEN, ".diff_XXXXXX");
106 if (fd < 0)
107 die("unable to create temp-file");
108 if (write(fd, blob, size) != size)
109 die("unable to write temp-file");
110 close(fd);
111}
112
113static void write_temp_blob(char *tmpfile, const unsigned char *sha1)
114{
115 unsigned long size;
116 void *blob;
117 blob = grab_blob(sha1, &size);
118 write_to_temp_file(tmpfile, blob, size);
119 free(blob);
120}
121
122static int parse_num(char **cp_p, unsigned int *num_p)
123{
124 char *cp = *cp_p;
125 unsigned int num = 0;
126 int read_some;
127
128 while ('0' <= *cp && *cp <= '9')
129 num = num * 10 + *cp++ - '0';
130 if (!(read_some = cp - *cp_p))
131 return -1;
132 *cp_p = cp;
133 *num_p = num;
134 return 0;
135}
136
137static int parse_hunk_header(char *line, int len,
138 unsigned int *ob, unsigned int *on,
139 unsigned int *nb, unsigned int *nn)
140{
141 char *cp;
142 cp = line + 4;
143 if (parse_num(&cp, ob)) {
144 bad_line:
145 return error("malformed diff output: %s", line);
146 }
147 if (*cp == ',') {
148 cp++;
149 if (parse_num(&cp, on))
150 goto bad_line;
151 }
152 else
153 *on = 1;
154 if (*cp++ != ' ' || *cp++ != '+')
155 goto bad_line;
156 if (parse_num(&cp, nb))
157 goto bad_line;
158 if (*cp == ',') {
159 cp++;
160 if (parse_num(&cp, nn))
161 goto bad_line;
162 }
163 else
164 *nn = 1;
165 return -!!memcmp(cp, " @@", 3);
166}
167
168static void append_lost(struct sline *sline, int n, const char *line)
169{
170 struct lline *lline;
171 int len = strlen(line);
172 unsigned long this_mask = (1UL<<n);
173 if (line[len-1] == '\n')
174 len--;
175
176 /* Check to see if we can squash things */
177 if (sline->lost_head) {
178 struct lline *last_one = NULL;
179 /* We cannot squash it with earlier one */
180 for (lline = sline->lost_head;
181 lline;
182 lline = lline->next)
183 if (lline->parent_map & this_mask)
184 last_one = lline;
185 lline = last_one ? last_one->next : sline->lost_head;
186 while (lline) {
187 if (lline->len == len &&
188 !memcmp(lline->line, line, len)) {
189 lline->parent_map |= this_mask;
190 return;
191 }
192 lline = lline->next;
193 }
194 }
195
196 lline = xmalloc(sizeof(*lline) + len + 1);
197 lline->len = len;
198 lline->next = NULL;
199 lline->parent_map = this_mask;
200 memcpy(lline->line, line, len);
201 lline->line[len] = 0;
202 *sline->lost_tail = lline;
203 sline->lost_tail = &lline->next;
204}
205
206static void combine_diff(const unsigned char *parent, const char *ourtmp,
207 struct sline *sline, int cnt, int n)
208{
209 FILE *in;
210 char parent_tmp[TMPPATHLEN];
211 char cmd[TMPPATHLEN * 2 + 1024];
212 char line[MAXLINELEN];
213 unsigned int lno, ob, on, nb, nn;
214 unsigned long pmask = ~(1UL << n);
215 struct sline *lost_bucket = NULL;
216
217 write_temp_blob(parent_tmp, parent);
218 sprintf(cmd, "diff --unified=0 -La/x -Lb/x '%s' '%s'",
219 parent_tmp, ourtmp);
220 in = popen(cmd, "r");
221 if (!in)
222 return;
223
224 lno = 1;
225 while (fgets(line, sizeof(line), in) != NULL) {
226 int len = strlen(line);
227 if (5 < len && !memcmp("@@ -", line, 4)) {
228 if (parse_hunk_header(line, len,
229 &ob, &on, &nb, &nn))
230 break;
231 lno = nb;
232 if (!nb) {
233 /* @@ -1,2 +0,0 @@ to remove the
234 * first two lines...
235 */
236 nb = 1;
237 }
238 lost_bucket = &sline[nb-1]; /* sline is 0 based */
239 continue;
240 }
241 if (!lost_bucket)
242 continue;
243 switch (line[0]) {
244 case '-':
245 append_lost(lost_bucket, n, line+1);
246 break;
247 case '+':
248 sline[lno-1].flag &= pmask;
249 lno++;
250 break;
251 }
252 }
253 fclose(in);
254 unlink(parent_tmp);
255}
256
257static unsigned long context = 3;
258static char combine_marker = '@';
259
260static int interesting(struct sline *sline, unsigned long all_mask)
261{
262 return ((sline->flag & all_mask) != all_mask || sline->lost_head);
263}
264
265static unsigned long adjust_hunk_tail(struct sline *sline,
266 unsigned long all_mask,
267 unsigned long hunk_begin,
268 unsigned long i)
269{
270 /* i points at the first uninteresting line.
271 * If the last line of the hunk was interesting
272 * only because it has some deletion, then
273 * it is not all that interesting for the
274 * purpose of giving trailing context lines.
275 */
276 if ((hunk_begin + 1 <= i) &&
277 ((sline[i-1].flag & all_mask) == all_mask))
278 i--;
279 return i;
280}
281
282static unsigned long next_interesting(struct sline *sline,
283 unsigned long mark,
284 unsigned long i,
285 unsigned long cnt,
286 int uninteresting)
287{
288 while (i < cnt)
289 if (uninteresting ?
290 !(sline[i].flag & mark) :
291 (sline[i].flag & mark))
292 return i;
293 else
294 i++;
295 return cnt;
296}
297
298static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
299{
300 unsigned long all_mask = (1UL<<num_parent) - 1;
301 unsigned long mark = (1UL<<num_parent);
302 unsigned long i;
303
304 i = next_interesting(sline, mark, 0, cnt, 0);
305 if (cnt <= i)
306 return 0;
307
308 while (i < cnt) {
309 unsigned long j = (context < i) ? (i - context) : 0;
310 unsigned long k;
311 while (j < i)
312 sline[j++].flag |= mark;
313
314 again:
315 j = next_interesting(sline, mark, i, cnt, 1);
316 if (cnt <= j)
317 break; /* the rest are all interesting */
318
319 /* lookahead context lines */
320 k = next_interesting(sline, mark, j, cnt, 0);
321 j = adjust_hunk_tail(sline, all_mask, i, j);
322
323 if (k < j + context) {
324 /* k is interesting and [j,k) are not, but
325 * paint them interesting because the gap is small.
326 */
327 while (j < k)
328 sline[j++].flag |= mark;
329 i = k;
330 goto again;
331 }
332
333 /* j is the first uninteresting line and there is
334 * no overlap beyond it within context lines.
335 */
336 i = k;
337 k = (j + context < cnt) ? j + context : cnt;
338 while (j < k)
339 sline[j++].flag |= mark;
340 }
341 return 1;
342}
343
344static int make_hunks(struct sline *sline, unsigned long cnt,
345 int num_parent, int dense)
346{
347 unsigned long all_mask = (1UL<<num_parent) - 1;
348 unsigned long mark = (1UL<<num_parent);
349 unsigned long i;
350 int has_interesting = 0;
351
352 for (i = 0; i < cnt; i++) {
353 if (interesting(&sline[i], all_mask))
354 sline[i].flag |= mark;
355 else
356 sline[i].flag &= ~mark;
357 }
358 if (!dense)
359 return give_context(sline, cnt, num_parent);
360
361 /* Look at each hunk, and if we have changes from only one
362 * parent, or the changes are the same from all but one
363 * parent, mark that uninteresting.
364 */
365 i = 0;
366 while (i < cnt) {
367 unsigned long j, hunk_begin, hunk_end;
368 unsigned long same_diff;
369 while (i < cnt && !(sline[i].flag & mark))
370 i++;
371 if (cnt <= i)
372 break; /* No more interesting hunks */
373 hunk_begin = i;
374 for (j = i + 1; j < cnt; j++) {
375 if (!(sline[j].flag & mark)) {
376 /* Look beyond the end to see if there
377 * is an interesting line after this
378 * hunk within context span.
379 */
380 unsigned long la; /* lookahead */
381 int contin = 0;
382 la = adjust_hunk_tail(sline, all_mask,
383 hunk_begin, j);
384 la = (la + context < cnt) ?
385 (la + context) : cnt;
386 while (j <= --la) {
387 if (sline[la].flag & mark) {
388 contin = 1;
389 break;
390 }
391 }
392 if (!contin)
393 break;
394 j = la;
395 }
396 }
397 hunk_end = j;
398
399 /* [i..hunk_end) are interesting. Now is it really
400 * interesting? We check if there are only two versions
401 * and the result matches one of them. That is, we look
402 * at:
403 * (+) line, which records lines added to which parents;
404 * this line appears in the result.
405 * (-) line, which records from what parents the line
406 * was removed; this line does not appear in the result.
407 * then check the set of parents the result has difference
408 * from, from all lines. If there are lines that has
409 * different set of parents that the result has differences
410 * from, that means we have more than two versions.
411 *
412 * Even when we have only two versions, if the result does
413 * not match any of the parents, the it should be considered
414 * interesting. In such a case, we would have all '+' line.
415 * After passing the above "two versions" test, that would
416 * appear as "the same set of parents" to be "all parents".
417 */
418 same_diff = 0;
419 has_interesting = 0;
420 for (j = i; j < hunk_end && !has_interesting; j++) {
421 unsigned long this_diff = ~sline[j].flag & all_mask;
422 struct lline *ll = sline[j].lost_head;
423 if (this_diff) {
424 /* This has some changes. Is it the
425 * same as others?
426 */
427 if (!same_diff)
428 same_diff = this_diff;
429 else if (same_diff != this_diff) {
430 has_interesting = 1;
431 break;
432 }
433 }
434 while (ll && !has_interesting) {
435 /* Lost this line from these parents;
436 * who are they? Are they the same?
437 */
438 this_diff = ll->parent_map;
439 if (!same_diff)
440 same_diff = this_diff;
441 else if (same_diff != this_diff) {
442 has_interesting = 1;
443 }
444 ll = ll->next;
445 }
446 }
447
448 if (!has_interesting && same_diff != all_mask) {
449 /* This hunk is not that interesting after all */
450 for (j = hunk_begin; j < hunk_end; j++)
451 sline[j].flag &= ~mark;
452 }
453 i = hunk_end;
454 }
455
456 has_interesting = give_context(sline, cnt, num_parent);
457 return has_interesting;
458}
459
460static void dump_sline(struct sline *sline, int cnt, int num_parent)
461{
462 unsigned long mark = (1UL<<num_parent);
463 int i;
464 int lno = 0;
465
466 while (1) {
467 struct sline *sl = &sline[lno];
468 int hunk_end;
469 while (lno < cnt && !(sline[lno].flag & mark))
470 lno++;
471 if (cnt <= lno)
472 break;
473 for (hunk_end = lno + 1; hunk_end < cnt; hunk_end++)
474 if (!(sline[hunk_end].flag & mark))
475 break;
476 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
477 printf(" +%d,%d ", lno+1, hunk_end-lno);
478 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
479 putchar('\n');
480 while (lno < hunk_end) {
481 struct lline *ll;
482 int j;
483 sl = &sline[lno++];
484 ll = sl->lost_head;
485 while (ll) {
486 for (j = 0; j < num_parent; j++) {
487 if (ll->parent_map & (1UL<<j))
488 putchar('-');
489 else
490 putchar(' ');
491 }
492 puts(ll->line);
493 ll = ll->next;
494 }
495 for (j = 0; j < num_parent; j++) {
496 if ((1UL<<j) & sl->flag)
497 putchar(' ');
498 else
499 putchar('+');
500 }
501 printf("%.*s\n", sl->len, sl->bol);
502 }
503 }
504}
505
506static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
507 int i, int j)
508{
509 /* We have already examined parent j and we know parent i
510 * and parent j are the same, so reuse the combined result
511 * of parent j for parent i.
512 */
513 unsigned long lno, imask, jmask;
514 imask = (1UL<<i);
515 jmask = (1UL<<j);
516
517 for (lno = 0; lno < cnt; lno++) {
518 struct lline *ll = sline->lost_head;
519 while (ll) {
520 if (ll->parent_map & jmask)
521 ll->parent_map |= imask;
522 ll = ll->next;
523 }
524 if (!(sline->flag & jmask))
525 sline->flag &= ~imask;
526 sline++;
527 }
528}
529
530int show_combined_diff(struct combine_diff_path *elem, int num_parent,
531 int dense, const char *header, int show_empty)
532{
533 unsigned long size, cnt, lno;
534 char *result, *cp, *ep;
535 struct sline *sline; /* survived lines */
536 int i, show_hunks, shown_header = 0;
537 char ourtmp_buf[TMPPATHLEN];
538 char *ourtmp = ourtmp_buf;
539
540 /* Read the result of merge first */
541 if (memcmp(elem->sha1, null_sha1, 20)) {
542 result = grab_blob(elem->sha1, &size);
543 write_to_temp_file(ourtmp, result, size);
544 }
545 else {
546 struct stat st;
547 int fd;
548 ourtmp = elem->path;
549 if (0 <= (fd = open(ourtmp, O_RDONLY)) &&
550 !fstat(fd, &st)) {
551 int len = st.st_size;
552 int cnt = 0;
553
554 size = len;
555 result = xmalloc(len + 1);
556 while (cnt < len) {
557 int done = xread(fd, result+cnt, len-cnt);
558 if (done == 0)
559 break;
560 if (done < 0)
561 die("read error '%s'", ourtmp);
562 cnt += done;
563 }
564 result[len] = 0;
565 }
566 else {
567 /* deleted file */
568 size = 0;
569 result = xmalloc(1);
570 result[0] = 0;
571 ourtmp = "/dev/null";
572 }
573 if (0 <= fd)
574 close(fd);
575 }
576
577 for (cnt = 0, cp = result; cp - result < size; cp++) {
578 if (*cp == '\n')
579 cnt++;
580 }
581 if (result[size-1] != '\n')
582 cnt++; /* incomplete line */
583
584 sline = xcalloc(cnt, sizeof(*sline));
585 ep = result;
586 sline[0].bol = result;
587 for (lno = 0, cp = result; cp - result < size; cp++) {
588 if (*cp == '\n') {
589 sline[lno].lost_tail = &sline[lno].lost_head;
590 sline[lno].len = cp - sline[lno].bol;
591 sline[lno].flag = (1UL<<num_parent) - 1;
592 lno++;
593 if (lno < cnt)
594 sline[lno].bol = cp + 1;
595 }
596 }
597 if (result[size-1] != '\n') {
598 sline[cnt-1].lost_tail = &sline[cnt-1].lost_head;
599 sline[cnt-1].len = size - (sline[cnt-1].bol - result);
600 sline[cnt-1].flag = (1UL<<num_parent) - 1;
601 }
602
603 for (i = 0; i < num_parent; i++) {
604 int j;
605 for (j = 0; j < i; j++) {
606 if (!memcmp(elem->parent_sha1[i],
607 elem->parent_sha1[j], 20)) {
608 reuse_combine_diff(sline, cnt, i, j);
609 break;
610 }
611 }
612 if (i <= j)
613 combine_diff(elem->parent_sha1[i], ourtmp, sline,
614 cnt, i);
615 }
616
617 show_hunks = make_hunks(sline, cnt, num_parent, dense);
618
619 if (header && (show_hunks || show_empty)) {
620 shown_header++;
621 puts(header);
622 }
623 if (show_hunks) {
624 printf("diff --%s ", dense ? "cc" : "combined");
625 if (quote_c_style(elem->path, NULL, NULL, 0))
626 quote_c_style(elem->path, NULL, stdout, 0);
627 else
628 printf("%s", elem->path);
629 putchar('\n');
630 dump_sline(sline, cnt, num_parent);
631 }
632 if (ourtmp == ourtmp_buf)
633 unlink(ourtmp);
634 free(result);
635
636 for (i = 0; i < cnt; i++) {
637 if (sline[i].lost_head) {
638 struct lline *ll = sline[i].lost_head;
639 while (ll) {
640 struct lline *tmp = ll;
641 ll = ll->next;
642 free(tmp);
643 }
644 }
645 }
646 free(sline);
647 return shown_header;
648}
649
650int diff_tree_combined_merge(const unsigned char *sha1,
651 const char *header,
652 int show_empty_merge, int dense)
653{
654 struct commit *commit = lookup_commit(sha1);
655 struct diff_options diffopts;
656 struct commit_list *parents;
657 struct combine_diff_path *p, *paths = NULL;
658 int num_parent, i, num_paths;
659
660 diff_setup(&diffopts);
661 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
662 diffopts.recursive = 1;
663
664 /* count parents */
665 for (parents = commit->parents, num_parent = 0;
666 parents;
667 parents = parents->next, num_parent++)
668 ; /* nothing */
669
670 /* find set of paths that everybody touches */
671 for (parents = commit->parents, i = 0;
672 parents;
673 parents = parents->next, i++) {
674 struct commit *parent = parents->item;
675 diff_tree_sha1(parent->object.sha1, commit->object.sha1, "",
676 &diffopts);
677 paths = intersect_paths(paths, i, num_parent);
678 diff_flush(&diffopts);
679 }
680
681 /* find out surviving paths */
682 for (num_paths = 0, p = paths; p; p = p->next) {
683 if (p->len)
684 num_paths++;
685 }
686 if (num_paths || show_empty_merge) {
687 for (p = paths; p; p = p->next) {
688 if (!p->len)
689 continue;
690 if (show_combined_diff(p, num_parent, dense, header,
691 show_empty_merge))
692 header = NULL;
693 }
694 }
695
696 /* Clean things up */
697 while (paths) {
698 struct combine_diff_path *tmp = paths;
699 paths = paths->next;
700 free(tmp);
701 }
702 return 0;
703}