]> git.ipfire.org Git - thirdparty/git.git/blame - combine-diff.c
combine-diff: reuse diff from the same blob.
[thirdparty/git.git] / combine-diff.c
CommitLineData
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
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
ea726d02 16static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
af3feefa
JH
17{
18 struct diff_queue_struct *q = &diff_queued_diff;
ea726d02 19 struct combine_diff_path *p;
af3feefa
JH
20 int i;
21
22 if (!n) {
ea726d02 23 struct combine_diff_path *list = NULL, **tail = &list;
af3feefa
JH
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);
5290a0f8
JH
40 *tail = p;
41 tail = &p->next;
af3feefa
JH
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;
5290a0f8 202 *sline->lost_tail = lline;
af3feefa
JH
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
263eee29 265static unsigned long line_common_diff(struct sline *sline, unsigned long all_mask)
d8f4790e
JH
266{
267 /*
263eee29
JH
268 * Look at the line and see from which parents we have the
269 * same difference.
d8f4790e 270 */
263eee29
JH
271
272 /* Lower bits of sline->flag records if the parent had this
273 * line, so XOR with all_mask gives us on-bits for parents we
274 * have differences with.
275 */
276 unsigned long common_adds = (sline->flag ^ all_mask) & all_mask;
277 unsigned long common_removes = all_mask;
278
279 /* If all the parents have this line, that also counts as
280 * having the same difference.
281 */
282 if (!common_adds)
283 common_adds = all_mask;
284
285 if (sline->lost_head) {
286 /* Lost head list records the lines removed from
287 * the parents, and parent_map records from which
288 * parent the line was removed.
289 */
290 struct lline *ll;
291 for (ll = sline->lost_head; ll; ll = ll->next) {
292 common_removes &= ll->parent_map;
293 }
294 }
295 return common_adds & common_removes;
296}
297
298static unsigned long line_all_diff(struct sline *sline, unsigned long all_mask)
299{
300 /*
301 * Look at the line and see from which parents we have some difference.
302 */
303 unsigned long different = (sline->flag ^ all_mask) & all_mask;
d8f4790e 304 if (sline->lost_head) {
263eee29
JH
305 /* Lost head list records the lines removed from
306 * the parents, and parent_map records from which
307 * parent the line was removed.
308 */
d8f4790e 309 struct lline *ll;
263eee29
JH
310 for (ll = sline->lost_head; ll; ll = ll->next) {
311 different |= ll->parent_map;
312 }
d8f4790e 313 }
263eee29 314 return different;
d8f4790e
JH
315}
316
3ec1909f
JH
317static unsigned long adjust_hunk_tail(struct sline *sline,
318 unsigned long all_mask,
319 unsigned long hunk_begin,
320 unsigned long i)
321{
322 /* i points at the first uninteresting line.
323 * If the last line of the hunk was interesting
324 * only because it has some deletion, then
325 * it is not all that interesting for the
326 * purpose of giving trailing context lines.
327 */
328 if ((hunk_begin + 1 <= i) &&
329 ((sline[i-1].flag & all_mask) == all_mask))
330 i--;
331 return i;
332}
333
334static unsigned long next_interesting(struct sline *sline,
335 unsigned long mark,
336 unsigned long i,
337 unsigned long cnt,
338 int uninteresting)
339{
340 while (i < cnt)
341 if (uninteresting ?
342 !(sline[i].flag & mark) :
343 (sline[i].flag & mark))
344 return i;
345 else
346 i++;
347 return cnt;
348}
349
350static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
af3feefa
JH
351{
352 unsigned long all_mask = (1UL<<num_parent) - 1;
353 unsigned long mark = (1UL<<num_parent);
354 unsigned long i;
355
3ec1909f
JH
356 i = next_interesting(sline, mark, 0, cnt, 0);
357 if (cnt <= i)
358 return 0;
359
af3feefa 360 while (i < cnt) {
3ec1909f
JH
361 unsigned long j = (context < i) ? (i - context) : 0;
362 unsigned long k;
363 while (j < i)
364 sline[j++].flag |= mark;
365
366 again:
367 j = next_interesting(sline, mark, i, cnt, 1);
368 if (cnt <= j)
369 break; /* the rest are all interesting */
370
371 /* lookahead context lines */
372 k = next_interesting(sline, mark, j, cnt, 0);
373 j = adjust_hunk_tail(sline, all_mask, i, j);
374
375 if (k < j + context) {
376 /* k is interesting and [j,k) are not, but
377 * paint them interesting because the gap is small.
378 */
379 while (j < k)
af3feefa 380 sline[j++].flag |= mark;
3ec1909f
JH
381 i = k;
382 goto again;
af3feefa 383 }
3ec1909f
JH
384
385 /* j is the first uninteresting line and there is
386 * no overlap beyond it within context lines.
387 */
388 i = k;
389 k = (j + context < cnt) ? j + context : cnt;
390 while (j < k)
391 sline[j++].flag |= mark;
392 }
393 return 1;
394}
395
396static int make_hunks(struct sline *sline, unsigned long cnt,
397 int num_parent, int dense)
398{
399 unsigned long all_mask = (1UL<<num_parent) - 1;
400 unsigned long mark = (1UL<<num_parent);
401 unsigned long i;
402 int has_interesting = 0;
403
404 for (i = 0; i < cnt; i++) {
405 if (interesting(&sline[i], all_mask))
406 sline[i].flag |= mark;
407 else
408 sline[i].flag &= ~mark;
af3feefa 409 }
d8f4790e 410 if (!dense)
3ec1909f 411 return give_context(sline, cnt, num_parent);
d8f4790e 412
263eee29
JH
413 /* Look at each hunk, and if we have changes from only one
414 * parent, or the changes are the same from all but one
415 * parent, mark that uninteresting.
d8f4790e
JH
416 */
417 i = 0;
418 while (i < cnt) {
3ec1909f
JH
419 unsigned long j, hunk_begin, hunk_end;
420 int same, diff;
8828cdcb 421 unsigned long same_diff, all_diff;
d8f4790e
JH
422 while (i < cnt && !(sline[i].flag & mark))
423 i++;
424 if (cnt <= i)
425 break; /* No more interesting hunks */
3ec1909f
JH
426 hunk_begin = i;
427 for (j = i + 1; j < cnt; j++) {
428 if (!(sline[j].flag & mark)) {
429 /* Look beyond the end to see if there
430 * is an interesting line after this
431 * hunk within context span.
432 */
433 unsigned long la; /* lookahead */
434 int contin = 0;
435 la = adjust_hunk_tail(sline, all_mask,
436 hunk_begin, j);
437 la = (la + context < cnt) ?
438 (la + context) : cnt;
439 while (j <= --la) {
440 if (sline[la].flag & mark) {
441 contin = 1;
442 break;
443 }
444 }
445 if (!contin)
446 break;
447 j = la;
448 }
449 }
450 hunk_end = j;
451
263eee29
JH
452 /* [i..hunk_end) are interesting. Now does it have
453 * the same change with all but one parent?
d8f4790e 454 */
263eee29
JH
455 same_diff = all_mask;
456 all_diff = 0;
457 for (j = i; j < hunk_end; j++) {
458 same_diff &= line_common_diff(sline + j, all_mask);
459 all_diff |= line_all_diff(sline + j, all_mask);
460 }
461 diff = same = 0;
d8f4790e 462 for (j = 0; j < num_parent; j++) {
263eee29
JH
463 if (same_diff & (1UL<<j))
464 same++;
465 if (all_diff & (1UL<<j))
466 diff++;
d8f4790e 467 }
263eee29 468 if ((num_parent - 1 <= same) || (diff == 1)) {
d8f4790e 469 /* This hunk is not that interesting after all */
3ec1909f 470 for (j = hunk_begin; j < hunk_end; j++)
d8f4790e
JH
471 sline[j].flag &= ~mark;
472 }
473 i = hunk_end;
474 }
3ec1909f
JH
475
476 has_interesting = give_context(sline, cnt, num_parent);
8828cdcb 477 return has_interesting;
af3feefa
JH
478}
479
480static void dump_sline(struct sline *sline, int cnt, int num_parent)
481{
482 unsigned long mark = (1UL<<num_parent);
483 int i;
484 int lno = 0;
485
486 while (1) {
487 struct sline *sl = &sline[lno];
488 int hunk_end;
489 while (lno < cnt && !(sline[lno].flag & mark))
490 lno++;
491 if (cnt <= lno)
492 break;
493 for (hunk_end = lno + 1; hunk_end < cnt; hunk_end++)
494 if (!(sline[hunk_end].flag & mark))
495 break;
496 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
497 printf(" +%d,%d ", lno+1, hunk_end-lno);
498 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
499 putchar('\n');
500 while (lno < hunk_end) {
501 struct lline *ll;
502 int j;
503 sl = &sline[lno++];
504 ll = sl->lost_head;
505 while (ll) {
506 for (j = 0; j < num_parent; j++) {
507 if (ll->parent_map & (1UL<<j))
508 putchar('-');
509 else
510 putchar(' ');
511 }
af3feefa
JH
512 puts(ll->line);
513 ll = ll->next;
514 }
515 for (j = 0; j < num_parent; j++) {
516 if ((1UL<<j) & sl->flag)
517 putchar(' ');
518 else
519 putchar('+');
520 }
e2283409 521 printf("%.*s\n", sl->len, sl->bol);
af3feefa
JH
522 }
523 }
524}
525
3c39e9bd
JH
526static void reuse_combine_diff(struct sline *sline, unsigned long cnt,
527 int i, int j)
528{
529 /* We have already examined parent j and we know parent i
530 * and parent j are the same, so reuse the combined result
531 * of parent j for parent i.
532 */
533 unsigned long lno, imask, jmask;
534 imask = (1UL<<i);
535 jmask = (1UL<<j);
536
537 for (lno = 0; lno < cnt; lno++) {
538 struct lline *ll = sline->lost_head;
539 while (ll) {
540 if (ll->parent_map & jmask)
541 ll->parent_map |= imask;
542 ll = ll->next;
543 }
544 if (!(sline->flag & jmask))
545 sline->flag &= ~imask;
546 sline++;
547 }
548}
549
ea726d02
JH
550int show_combined_diff(struct combine_diff_path *elem, int num_parent,
551 int dense, const char *header, int show_empty)
af3feefa
JH
552{
553 unsigned long size, cnt, lno;
554 char *result, *cp, *ep;
555 struct sline *sline; /* survived lines */
8828cdcb 556 int i, show_hunks, shown_header = 0;
ea726d02
JH
557 char ourtmp_buf[TMPPATHLEN];
558 char *ourtmp = ourtmp_buf;
af3feefa
JH
559
560 /* Read the result of merge first */
ea726d02
JH
561 if (memcmp(elem->sha1, null_sha1, 20)) {
562 result = grab_blob(elem->sha1, &size);
563 write_to_temp_file(ourtmp, result, size);
564 }
565 else {
566 struct stat st;
567 int fd;
568 ourtmp = elem->path;
569 if (0 <= (fd = open(ourtmp, O_RDONLY)) &&
570 !fstat(fd, &st)) {
571 int len = st.st_size;
572 int cnt = 0;
573
574 size = len;
575 result = xmalloc(len + 1);
576 while (cnt < len) {
577 int done = xread(fd, result+cnt, len-cnt);
578 if (done == 0)
579 break;
580 if (done < 0)
581 die("read error '%s'", ourtmp);
582 cnt += done;
583 }
584 result[len] = 0;
585 }
586 else {
587 /* deleted file */
588 size = 0;
589 result = xmalloc(1);
590 result[0] = 0;
591 ourtmp = "/dev/null";
592 }
593 if (0 <= fd)
594 close(fd);
595 }
af3feefa
JH
596
597 for (cnt = 0, cp = result; cp - result < size; cp++) {
598 if (*cp == '\n')
599 cnt++;
600 }
601 if (result[size-1] != '\n')
602 cnt++; /* incomplete line */
603
604 sline = xcalloc(cnt, sizeof(*sline));
605 ep = result;
606 sline[0].bol = result;
607 for (lno = 0, cp = result; cp - result < size; cp++) {
608 if (*cp == '\n') {
5290a0f8 609 sline[lno].lost_tail = &sline[lno].lost_head;
af3feefa
JH
610 sline[lno].len = cp - sline[lno].bol;
611 sline[lno].flag = (1UL<<num_parent) - 1;
612 lno++;
613 if (lno < cnt)
614 sline[lno].bol = cp + 1;
615 }
616 }
617 if (result[size-1] != '\n') {
5290a0f8 618 sline[cnt-1].lost_tail = &sline[cnt-1].lost_head;
af3feefa
JH
619 sline[cnt-1].len = size - (sline[cnt-1].bol - result);
620 sline[cnt-1].flag = (1UL<<num_parent) - 1;
621 }
622
3c39e9bd
JH
623 for (i = 0; i < num_parent; i++) {
624 int j;
625 for (j = 0; j < i; j++) {
626 if (!memcmp(elem->parent_sha1[i],
627 elem->parent_sha1[j], 20)) {
628 reuse_combine_diff(sline, cnt, i, j);
629 break;
630 }
631 }
632 if (i <= j)
633 combine_diff(elem->parent_sha1[i], ourtmp, sline,
634 cnt, i);
635 }
af3feefa 636
8828cdcb 637 show_hunks = make_hunks(sline, cnt, num_parent, dense);
af3feefa 638
8828cdcb
JH
639 if (header && (show_hunks || show_empty)) {
640 shown_header++;
641 puts(header);
642 }
643 if (show_hunks) {
644 printf("diff --%s ", dense ? "cc" : "combined");
645 if (quote_c_style(elem->path, NULL, NULL, 0))
646 quote_c_style(elem->path, NULL, stdout, 0);
647 else
648 printf("%s", elem->path);
649 putchar('\n');
650 dump_sline(sline, cnt, num_parent);
651 }
ea726d02
JH
652 if (ourtmp == ourtmp_buf)
653 unlink(ourtmp);
af3feefa
JH
654 free(result);
655
656 for (i = 0; i < cnt; i++) {
657 if (sline[i].lost_head) {
658 struct lline *ll = sline[i].lost_head;
659 while (ll) {
660 struct lline *tmp = ll;
661 ll = ll->next;
662 free(tmp);
663 }
664 }
665 }
666 free(sline);
8828cdcb 667 return shown_header;
af3feefa
JH
668}
669
670int diff_tree_combined_merge(const unsigned char *sha1,
d8f4790e
JH
671 const char *header,
672 int show_empty_merge, int dense)
af3feefa
JH
673{
674 struct commit *commit = lookup_commit(sha1);
675 struct diff_options diffopts;
676 struct commit_list *parents;
ea726d02 677 struct combine_diff_path *p, *paths = NULL;
af3feefa
JH
678 int num_parent, i, num_paths;
679
680 diff_setup(&diffopts);
681 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
682 diffopts.recursive = 1;
683
684 /* count parents */
685 for (parents = commit->parents, num_parent = 0;
686 parents;
687 parents = parents->next, num_parent++)
688 ; /* nothing */
689
690 /* find set of paths that everybody touches */
691 for (parents = commit->parents, i = 0;
692 parents;
693 parents = parents->next, i++) {
694 struct commit *parent = parents->item;
695 diff_tree_sha1(parent->object.sha1, commit->object.sha1, "",
696 &diffopts);
697 paths = intersect_paths(paths, i, num_parent);
698 diff_flush(&diffopts);
699 }
700
701 /* find out surviving paths */
702 for (num_paths = 0, p = paths; p; p = p->next) {
703 if (p->len)
704 num_paths++;
705 }
706 if (num_paths || show_empty_merge) {
af3feefa
JH
707 for (p = paths; p; p = p->next) {
708 if (!p->len)
709 continue;
8828cdcb
JH
710 if (show_combined_diff(p, num_parent, dense, header,
711 show_empty_merge))
712 header = NULL;
af3feefa
JH
713 }
714 }
715
716 /* Clean things up */
717 while (paths) {
ea726d02 718 struct combine_diff_path *tmp = paths;
af3feefa
JH
719 paths = paths->next;
720 free(tmp);
721 }
722 return 0;
723}