7 static int uninteresting(struct diff_filepair *p)
9 if (diff_unmodified_pair(p))
11 if (!S_ISREG(p->one->mode) || !S_ISREG(p->two->mode))
16 static struct combine_diff_path *intersect_paths(struct combine_diff_path *curr, int n, int num_parent)
18 struct diff_queue_struct *q = &diff_queued_diff;
19 struct combine_diff_path *p;
23 struct combine_diff_path *list = NULL, **tail = &list;
24 for (i = 0; i < q->nr; i++) {
27 if (uninteresting(q->queue[i]))
29 path = q->queue[i]->two->path;
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);
38 memcpy(p->sha1, q->queue[i]->two->sha1, 20);
39 memcpy(p->parent_sha1[n], q->queue[i]->one->sha1, 20);
46 for (p = curr; p; p = p->next) {
50 for (i = 0; i < q->nr; i++) {
54 if (uninteresting(q->queue[i]))
56 path = q->queue[i]->two->path;
58 if (len == p->len && !memcmp(path, p->path, len)) {
60 memcpy(p->parent_sha1[n],
61 q->queue[i]->one->sha1, 20);
74 unsigned long parent_map;
75 char line[FLEX_ARRAY];
79 struct lline *lost_head, **lost_tail;
85 static char *grab_blob(const unsigned char *sha1, unsigned long *size)
89 if (!memcmp(sha1, null_sha1, 20)) {
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));
100 #define TMPPATHLEN 50
101 #define MAXLINELEN 10240
103 static void write_to_temp_file(char *tmpfile, void *blob, unsigned long size)
105 int fd = git_mkstemp(tmpfile, TMPPATHLEN, ".diff_XXXXXX");
107 die("unable to create temp-file");
108 if (write(fd, blob, size) != size)
109 die("unable to write temp-file");
113 static void write_temp_blob(char *tmpfile, const unsigned char *sha1)
117 blob = grab_blob(sha1, &size);
118 write_to_temp_file(tmpfile, blob, size);
122 static int parse_num(char **cp_p, unsigned int *num_p)
125 unsigned int num = 0;
128 while ('0' <= *cp && *cp <= '9')
129 num = num * 10 + *cp++ - '0';
130 if (!(read_some = cp - *cp_p))
137 static int parse_hunk_header(char *line, int len,
138 unsigned int *ob, unsigned int *on,
139 unsigned int *nb, unsigned int *nn)
143 if (parse_num(&cp, ob)) {
145 return error("malformed diff output: %s", line);
149 if (parse_num(&cp, on))
154 if (*cp++ != ' ' || *cp++ != '+')
156 if (parse_num(&cp, nb))
160 if (parse_num(&cp, nn))
165 return -!!memcmp(cp, " @@", 3);
168 static void append_lost(struct sline *sline, int n, const char *line)
171 int len = strlen(line);
172 unsigned long this_mask = (1UL<<n);
173 if (line[len-1] == '\n')
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;
183 if (lline->parent_map & this_mask)
185 lline = last_one ? last_one->next : sline->lost_head;
187 if (lline->len == len &&
188 !memcmp(lline->line, line, len)) {
189 lline->parent_map |= this_mask;
196 lline = xmalloc(sizeof(*lline) + len + 1);
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;
206 static void combine_diff(const unsigned char *parent, const char *ourtmp,
207 struct sline *sline, int cnt, int n)
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;
217 write_temp_blob(parent_tmp, parent);
218 sprintf(cmd, "diff --unified=0 -La/x -Lb/x '%s' '%s'",
220 in = popen(cmd, "r");
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,
233 /* @@ -1,2 +0,0 @@ to remove the
238 lost_bucket = &sline[nb-1]; /* sline is 0 based */
245 append_lost(lost_bucket, n, line+1);
248 sline[lno-1].flag &= pmask;
257 static unsigned long context = 3;
258 static char combine_marker = '@';
260 static int interesting(struct sline *sline, unsigned long all_mask)
262 return ((sline->flag & all_mask) != all_mask || sline->lost_head);
265 static unsigned long line_common_diff(struct sline *sline, unsigned long all_mask)
268 * Look at the line and see from which parents we have the
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.
276 unsigned long common_adds = (sline->flag ^ all_mask) & all_mask;
277 unsigned long common_removes = all_mask;
279 /* If all the parents have this line, that also counts as
280 * having the same difference.
283 common_adds = all_mask;
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.
291 for (ll = sline->lost_head; ll; ll = ll->next) {
292 common_removes &= ll->parent_map;
295 return common_adds & common_removes;
298 static unsigned long line_all_diff(struct sline *sline, unsigned long all_mask)
301 * Look at the line and see from which parents we have some difference.
303 unsigned long different = (sline->flag ^ all_mask) & all_mask;
304 if (sline->lost_head) {
305 /* Lost head list records the lines removed from
306 * the parents, and parent_map records from which
307 * parent the line was removed.
310 for (ll = sline->lost_head; ll; ll = ll->next) {
311 different |= ll->parent_map;
317 static unsigned long adjust_hunk_tail(struct sline *sline,
318 unsigned long all_mask,
319 unsigned long hunk_begin,
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.
328 if ((hunk_begin + 1 <= i) &&
329 ((sline[i-1].flag & all_mask) == all_mask))
334 static unsigned long next_interesting(struct sline *sline,
342 !(sline[i].flag & mark) :
343 (sline[i].flag & mark))
350 static int give_context(struct sline *sline, unsigned long cnt, int num_parent)
352 unsigned long all_mask = (1UL<<num_parent) - 1;
353 unsigned long mark = (1UL<<num_parent);
356 i = next_interesting(sline, mark, 0, cnt, 0);
361 unsigned long j = (context < i) ? (i - context) : 0;
364 sline[j++].flag |= mark;
367 j = next_interesting(sline, mark, i, cnt, 1);
369 break; /* the rest are all interesting */
371 /* lookahead context lines */
372 k = next_interesting(sline, mark, j, cnt, 0);
373 j = adjust_hunk_tail(sline, all_mask, i, j);
375 if (k < j + context) {
376 /* k is interesting and [j,k) are not, but
377 * paint them interesting because the gap is small.
380 sline[j++].flag |= mark;
385 /* j is the first uninteresting line and there is
386 * no overlap beyond it within context lines.
389 k = (j + context < cnt) ? j + context : cnt;
391 sline[j++].flag |= mark;
396 static int make_hunks(struct sline *sline, unsigned long cnt,
397 int num_parent, int dense)
399 unsigned long all_mask = (1UL<<num_parent) - 1;
400 unsigned long mark = (1UL<<num_parent);
402 int has_interesting = 0;
404 for (i = 0; i < cnt; i++) {
405 if (interesting(&sline[i], all_mask))
406 sline[i].flag |= mark;
408 sline[i].flag &= ~mark;
411 return give_context(sline, cnt, num_parent);
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.
419 unsigned long j, hunk_begin, hunk_end;
421 unsigned long same_diff, all_diff;
422 while (i < cnt && !(sline[i].flag & mark))
425 break; /* No more interesting hunks */
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.
433 unsigned long la; /* lookahead */
435 la = adjust_hunk_tail(sline, all_mask,
437 la = (la + context < cnt) ?
438 (la + context) : cnt;
440 if (sline[la].flag & mark) {
452 /* [i..hunk_end) are interesting. Now does it have
453 * the same change with all but one parent?
455 same_diff = all_mask;
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);
462 for (j = 0; j < num_parent; j++) {
463 if (same_diff & (1UL<<j))
465 if (all_diff & (1UL<<j))
468 if ((num_parent - 1 <= same) || (diff == 1)) {
469 /* This hunk is not that interesting after all */
470 for (j = hunk_begin; j < hunk_end; j++)
471 sline[j].flag &= ~mark;
476 has_interesting = give_context(sline, cnt, num_parent);
477 return has_interesting;
480 static void dump_sline(struct sline *sline, int cnt, int num_parent)
482 unsigned long mark = (1UL<<num_parent);
487 struct sline *sl = &sline[lno];
489 while (lno < cnt && !(sline[lno].flag & mark))
493 for (hunk_end = lno + 1; hunk_end < cnt; hunk_end++)
494 if (!(sline[hunk_end].flag & mark))
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);
500 while (lno < hunk_end) {
506 for (j = 0; j < num_parent; j++) {
507 if (ll->parent_map & (1UL<<j))
515 for (j = 0; j < num_parent; j++) {
516 if ((1UL<<j) & sl->flag)
521 printf("%.*s\n", sl->len, sl->bol);
526 int show_combined_diff(struct combine_diff_path *elem, int num_parent,
527 int dense, const char *header, int show_empty)
529 unsigned long size, cnt, lno;
530 char *result, *cp, *ep;
531 struct sline *sline; /* survived lines */
532 int i, show_hunks, shown_header = 0;
533 char ourtmp_buf[TMPPATHLEN];
534 char *ourtmp = ourtmp_buf;
536 /* Read the result of merge first */
537 if (memcmp(elem->sha1, null_sha1, 20)) {
538 result = grab_blob(elem->sha1, &size);
539 write_to_temp_file(ourtmp, result, size);
545 if (0 <= (fd = open(ourtmp, O_RDONLY)) &&
547 int len = st.st_size;
551 result = xmalloc(len + 1);
553 int done = xread(fd, result+cnt, len-cnt);
557 die("read error '%s'", ourtmp);
567 ourtmp = "/dev/null";
573 for (cnt = 0, cp = result; cp - result < size; cp++) {
577 if (result[size-1] != '\n')
578 cnt++; /* incomplete line */
580 sline = xcalloc(cnt, sizeof(*sline));
582 sline[0].bol = result;
583 for (lno = 0, cp = result; cp - result < size; cp++) {
585 sline[lno].lost_tail = &sline[lno].lost_head;
586 sline[lno].len = cp - sline[lno].bol;
587 sline[lno].flag = (1UL<<num_parent) - 1;
590 sline[lno].bol = cp + 1;
593 if (result[size-1] != '\n') {
594 sline[cnt-1].lost_tail = &sline[cnt-1].lost_head;
595 sline[cnt-1].len = size - (sline[cnt-1].bol - result);
596 sline[cnt-1].flag = (1UL<<num_parent) - 1;
599 for (i = 0; i < num_parent; i++)
600 combine_diff(elem->parent_sha1[i], ourtmp, sline, cnt, i);
602 show_hunks = make_hunks(sline, cnt, num_parent, dense);
604 if (header && (show_hunks || show_empty)) {
609 printf("diff --%s ", dense ? "cc" : "combined");
610 if (quote_c_style(elem->path, NULL, NULL, 0))
611 quote_c_style(elem->path, NULL, stdout, 0);
613 printf("%s", elem->path);
615 dump_sline(sline, cnt, num_parent);
617 if (ourtmp == ourtmp_buf)
621 for (i = 0; i < cnt; i++) {
622 if (sline[i].lost_head) {
623 struct lline *ll = sline[i].lost_head;
625 struct lline *tmp = ll;
635 int diff_tree_combined_merge(const unsigned char *sha1,
637 int show_empty_merge, int dense)
639 struct commit *commit = lookup_commit(sha1);
640 struct diff_options diffopts;
641 struct commit_list *parents;
642 struct combine_diff_path *p, *paths = NULL;
643 int num_parent, i, num_paths;
645 diff_setup(&diffopts);
646 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
647 diffopts.recursive = 1;
650 for (parents = commit->parents, num_parent = 0;
652 parents = parents->next, num_parent++)
655 /* find set of paths that everybody touches */
656 for (parents = commit->parents, i = 0;
658 parents = parents->next, i++) {
659 struct commit *parent = parents->item;
660 diff_tree_sha1(parent->object.sha1, commit->object.sha1, "",
662 paths = intersect_paths(paths, i, num_parent);
663 diff_flush(&diffopts);
666 /* find out surviving paths */
667 for (num_paths = 0, p = paths; p; p = p->next) {
671 if (num_paths || show_empty_merge) {
672 for (p = paths; p; p = p->next) {
675 if (show_combined_diff(p, num_parent, dense, header,
681 /* Clean things up */
683 struct combine_diff_path *tmp = paths;