8 struct path_list *next;
11 unsigned char sha1[20];
12 unsigned char parent_sha1[FLEX_ARRAY][20];
15 static int uninteresting(struct diff_filepair *p)
17 if (diff_unmodified_pair(p))
19 if (!S_ISREG(p->one->mode) || !S_ISREG(p->two->mode))
24 static struct path_list *intersect_paths(struct path_list *curr,
25 int n, int num_parent)
27 struct diff_queue_struct *q = &diff_queued_diff;
32 struct path_list *list = NULL, *tail = NULL;
33 for (i = 0; i < q->nr; i++) {
36 if (uninteresting(q->queue[i]))
38 path = q->queue[i]->two->path;
41 p = xmalloc(sizeof(*p) + len + 1 + num_parent * 20);
42 p->path = (char*) &(p->parent_sha1[num_parent][0]);
43 memcpy(p->path, path, len);
47 memcpy(p->sha1, q->queue[i]->two->sha1, 20);
48 memcpy(p->parent_sha1[n], q->queue[i]->one->sha1, 20);
59 for (p = curr; p; p = p->next) {
63 for (i = 0; i < q->nr; i++) {
67 if (uninteresting(q->queue[i]))
69 path = q->queue[i]->two->path;
71 if (len == p->len && !memcmp(path, p->path, len)) {
73 memcpy(p->parent_sha1[n],
74 q->queue[i]->one->sha1, 20);
87 unsigned long parent_map;
88 char line[FLEX_ARRAY];
92 struct lline *lost_head, **lost_tail;
98 static char *grab_blob(const unsigned char *sha1, unsigned long *size)
102 if (!memcmp(sha1, null_sha1, 20)) {
105 return xcalloc(1, 1);
107 blob = read_sha1_file(sha1, type, size);
108 if (strcmp(type, "blob"))
109 die("object '%s' is not a blob!", sha1_to_hex(sha1));
113 #define TMPPATHLEN 50
114 #define MAXLINELEN 10240
116 static void write_to_temp_file(char *tmpfile, void *blob, unsigned long size)
118 int fd = git_mkstemp(tmpfile, TMPPATHLEN, ".diff_XXXXXX");
120 die("unable to create temp-file");
121 if (write(fd, blob, size) != size)
122 die("unable to write temp-file");
126 static void write_temp_blob(char *tmpfile, const unsigned char *sha1)
130 blob = grab_blob(sha1, &size);
131 write_to_temp_file(tmpfile, blob, size);
135 static int parse_num(char **cp_p, unsigned int *num_p)
138 unsigned int num = 0;
141 while ('0' <= *cp && *cp <= '9')
142 num = num * 10 + *cp++ - '0';
143 if (!(read_some = cp - *cp_p))
150 static int parse_hunk_header(char *line, int len,
151 unsigned int *ob, unsigned int *on,
152 unsigned int *nb, unsigned int *nn)
156 if (parse_num(&cp, ob)) {
158 return error("malformed diff output: %s", line);
162 if (parse_num(&cp, on))
167 if (*cp++ != ' ' || *cp++ != '+')
169 if (parse_num(&cp, nb))
173 if (parse_num(&cp, nn))
178 return -!!memcmp(cp, " @@", 3);
181 static void append_lost(struct sline *sline, int n, const char *line)
184 int len = strlen(line);
185 unsigned long this_mask = (1UL<<n);
186 if (line[len-1] == '\n')
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;
196 if (lline->parent_map & this_mask)
198 lline = last_one ? last_one->next : sline->lost_head;
200 if (lline->len == len &&
201 !memcmp(lline->line, line, len)) {
202 lline->parent_map |= this_mask;
209 lline = xmalloc(sizeof(*lline) + len + 1);
212 lline->parent_map = this_mask;
213 memcpy(lline->line, line, len);
214 lline->line[len] = 0;
215 if (sline->lost_head)
216 *(sline->lost_tail) = lline;
218 sline->lost_head = lline;
219 sline->lost_tail = &lline->next;
222 static void combine_diff(const unsigned char *parent, const char *ourtmp,
223 struct sline *sline, int cnt, int n)
226 char parent_tmp[TMPPATHLEN];
227 char cmd[TMPPATHLEN * 2 + 1024];
228 char line[MAXLINELEN];
229 unsigned int lno, ob, on, nb, nn;
230 unsigned long pmask = ~(1UL << n);
231 struct sline *lost_bucket = NULL;
233 write_temp_blob(parent_tmp, parent);
234 sprintf(cmd, "diff --unified=0 -La/x -Lb/x '%s' '%s'",
236 in = popen(cmd, "r");
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,
249 /* @@ -1,2 +0,0 @@ to remove the
254 lost_bucket = &sline[nb-1]; /* sline is 0 based */
261 append_lost(lost_bucket, n, line+1);
264 sline[lno-1].flag &= pmask;
273 static unsigned long context = 3;
274 static char combine_marker = '@';
276 static int interesting(struct sline *sline, unsigned long all_mask)
278 return ((sline->flag & all_mask) != all_mask || sline->lost_head);
281 static unsigned long line_diff_parents(struct sline *sline, unsigned long all_mask)
284 * Look at the line and see from which parents we have difference.
285 * Lower bits of sline->flag records if the parent had this line,
286 * so XOR with all_mask gives us on-bits for parents we have
289 unsigned long parents = (sline->flag ^ all_mask);
290 if (sline->lost_head) {
292 for (ll = sline->lost_head; ll; ll = ll->next)
293 parents |= ll->parent_map;
295 return parents & all_mask;
298 static void make_hunks(struct sline *sline, unsigned long cnt,
299 int num_parent, int dense)
301 unsigned long all_mask = (1UL<<num_parent) - 1;
302 unsigned long mark = (1UL<<num_parent);
307 if (interesting(&sline[i], all_mask)) {
308 unsigned long j = (context < i) ? i - context : 0;
310 sline[j++].flag |= mark;
312 if (!interesting(&sline[i], all_mask))
314 sline[i].flag |= mark;
316 j = (i + context < cnt) ? i + context : cnt;
318 sline[i++].flag |= mark;
326 /* Look at each hunk, and if it contains changes from only
327 * one parent, mark that uninteresting.
331 int j, hunk_end, diffs;
332 unsigned long parents;
333 while (i < cnt && !(sline[i].flag & mark))
336 break; /* No more interesting hunks */
337 for (hunk_end = i + 1; hunk_end < cnt; hunk_end++)
338 if (!(sline[hunk_end].flag & mark))
340 /* [i..hunk_end) are interesting. Now is it from
342 * If lost lines are only from one parent and
343 * remaining lines existed in parents other than
344 * that parent, then the hunk is not that interesting.
348 for (j = i; j < hunk_end; j++)
349 parents |= line_diff_parents(sline + j, all_mask);
350 /* Now, how many bits from [0..num_parent) are on? */
351 for (j = 0; j < num_parent; j++) {
352 if (parents & (1UL<<j))
356 /* This hunk is not that interesting after all */
357 for (j = i; j < hunk_end; j++)
358 sline[j].flag &= ~mark;
364 static void dump_sline(struct sline *sline, int cnt, int num_parent)
366 unsigned long mark = (1UL<<num_parent);
371 struct sline *sl = &sline[lno];
373 while (lno < cnt && !(sline[lno].flag & mark))
377 for (hunk_end = lno + 1; hunk_end < cnt; hunk_end++)
378 if (!(sline[hunk_end].flag & mark))
380 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
381 printf(" +%d,%d ", lno+1, hunk_end-lno);
382 for (i = 0; i <= num_parent; i++) putchar(combine_marker);
384 while (lno < hunk_end) {
390 for (j = 0; j < num_parent; j++) {
391 if (ll->parent_map & (1UL<<j))
400 for (j = 0; j < num_parent; j++) {
401 if ((1UL<<j) & sl->flag)
406 printf(" %.*s\n", sl->len, sl->bol);
411 static void show_combined_diff(struct path_list *elem, int num_parent,
414 unsigned long size, cnt, lno;
415 char *result, *cp, *ep;
416 struct sline *sline; /* survived lines */
418 char ourtmp[TMPPATHLEN];
420 /* Read the result of merge first */
421 result = grab_blob(elem->sha1, &size);
422 write_to_temp_file(ourtmp, result, size);
424 for (cnt = 0, cp = result; cp - result < size; cp++) {
428 if (result[size-1] != '\n')
429 cnt++; /* incomplete line */
431 sline = xcalloc(cnt, sizeof(*sline));
433 sline[0].bol = result;
434 for (lno = 0, cp = result; cp - result < size; cp++) {
436 sline[lno].len = cp - sline[lno].bol;
437 sline[lno].flag = (1UL<<num_parent) - 1;
440 sline[lno].bol = cp + 1;
443 if (result[size-1] != '\n') {
444 sline[cnt-1].len = size - (sline[cnt-1].bol - result);
445 sline[cnt-1].flag = (1UL<<num_parent) - 1;
448 for (i = 0; i < num_parent; i++)
449 combine_diff(elem->parent_sha1[i], ourtmp, sline, cnt, i);
451 make_hunks(sline, cnt, num_parent, dense);
453 dump_sline(sline, cnt, num_parent);
457 for (i = 0; i < cnt; i++) {
458 if (sline[i].lost_head) {
459 struct lline *ll = sline[i].lost_head;
461 struct lline *tmp = ll;
470 int diff_tree_combined_merge(const unsigned char *sha1,
472 int show_empty_merge, int dense)
474 struct commit *commit = lookup_commit(sha1);
475 struct diff_options diffopts;
476 struct commit_list *parents;
477 struct path_list *p, *paths = NULL;
478 int num_parent, i, num_paths;
480 diff_setup(&diffopts);
481 diffopts.output_format = DIFF_FORMAT_NO_OUTPUT;
482 diffopts.recursive = 1;
485 for (parents = commit->parents, num_parent = 0;
487 parents = parents->next, num_parent++)
490 /* find set of paths that everybody touches */
491 for (parents = commit->parents, i = 0;
493 parents = parents->next, i++) {
494 struct commit *parent = parents->item;
495 diff_tree_sha1(parent->object.sha1, commit->object.sha1, "",
497 paths = intersect_paths(paths, i, num_parent);
498 diff_flush(&diffopts);
501 /* find out surviving paths */
502 for (num_paths = 0, p = paths; p; p = p->next) {
506 if (num_paths || show_empty_merge) {
508 for (p = paths; p; p = p->next) {
511 printf("diff --combined ");
512 if (quote_c_style(p->path, NULL, NULL, 0))
513 quote_c_style(p->path, NULL, stdout, 0);
515 printf("%s", p->path);
517 show_combined_diff(p, num_parent, dense);
521 /* Clean things up */
523 struct path_list *tmp = paths;