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
= &list
;
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);
55 for (p
= curr
; p
; p
= p
->next
) {
59 for (i
= 0; i
< q
->nr
; i
++) {
63 if (uninteresting(q
->queue
[i
]))
65 path
= q
->queue
[i
]->two
->path
;
67 if (len
== p
->len
&& !memcmp(path
, p
->path
, len
)) {
69 memcpy(p
->parent_sha1
[n
],
70 q
->queue
[i
]->one
->sha1
, 20);
83 unsigned long parent_map
;
84 char line
[FLEX_ARRAY
];
88 struct lline
*lost_head
, **lost_tail
;
94 static char *grab_blob(const unsigned char *sha1
, unsigned long *size
)
98 if (!memcmp(sha1
, null_sha1
, 20)) {
101 return xcalloc(1, 1);
103 blob
= read_sha1_file(sha1
, type
, size
);
104 if (strcmp(type
, "blob"))
105 die("object '%s' is not a blob!", sha1_to_hex(sha1
));
109 #define TMPPATHLEN 50
110 #define MAXLINELEN 10240
112 static void write_to_temp_file(char *tmpfile
, void *blob
, unsigned long size
)
114 int fd
= git_mkstemp(tmpfile
, TMPPATHLEN
, ".diff_XXXXXX");
116 die("unable to create temp-file");
117 if (write(fd
, blob
, size
) != size
)
118 die("unable to write temp-file");
122 static void write_temp_blob(char *tmpfile
, const unsigned char *sha1
)
126 blob
= grab_blob(sha1
, &size
);
127 write_to_temp_file(tmpfile
, blob
, size
);
131 static int parse_num(char **cp_p
, unsigned int *num_p
)
134 unsigned int num
= 0;
137 while ('0' <= *cp
&& *cp
<= '9')
138 num
= num
* 10 + *cp
++ - '0';
139 if (!(read_some
= cp
- *cp_p
))
146 static int parse_hunk_header(char *line
, int len
,
147 unsigned int *ob
, unsigned int *on
,
148 unsigned int *nb
, unsigned int *nn
)
152 if (parse_num(&cp
, ob
)) {
154 return error("malformed diff output: %s", line
);
158 if (parse_num(&cp
, on
))
163 if (*cp
++ != ' ' || *cp
++ != '+')
165 if (parse_num(&cp
, nb
))
169 if (parse_num(&cp
, nn
))
174 return -!!memcmp(cp
, " @@", 3);
177 static void append_lost(struct sline
*sline
, int n
, const char *line
)
180 int len
= strlen(line
);
181 unsigned long this_mask
= (1UL<<n
);
182 if (line
[len
-1] == '\n')
185 /* Check to see if we can squash things */
186 if (sline
->lost_head
) {
187 struct lline
*last_one
= NULL
;
188 /* We cannot squash it with earlier one */
189 for (lline
= sline
->lost_head
;
192 if (lline
->parent_map
& this_mask
)
194 lline
= last_one
? last_one
->next
: sline
->lost_head
;
196 if (lline
->len
== len
&&
197 !memcmp(lline
->line
, line
, len
)) {
198 lline
->parent_map
|= this_mask
;
205 lline
= xmalloc(sizeof(*lline
) + len
+ 1);
208 lline
->parent_map
= this_mask
;
209 memcpy(lline
->line
, line
, len
);
210 lline
->line
[len
] = 0;
211 *sline
->lost_tail
= lline
;
212 sline
->lost_tail
= &lline
->next
;
215 static void combine_diff(const unsigned char *parent
, const char *ourtmp
,
216 struct sline
*sline
, int cnt
, int n
)
219 char parent_tmp
[TMPPATHLEN
];
220 char cmd
[TMPPATHLEN
* 2 + 1024];
221 char line
[MAXLINELEN
];
222 unsigned int lno
, ob
, on
, nb
, nn
;
223 unsigned long pmask
= ~(1UL << n
);
224 struct sline
*lost_bucket
= NULL
;
226 write_temp_blob(parent_tmp
, parent
);
227 sprintf(cmd
, "diff --unified=0 -La/x -Lb/x '%s' '%s'",
229 in
= popen(cmd
, "r");
234 while (fgets(line
, sizeof(line
), in
) != NULL
) {
235 int len
= strlen(line
);
236 if (5 < len
&& !memcmp("@@ -", line
, 4)) {
237 if (parse_hunk_header(line
, len
,
242 /* @@ -1,2 +0,0 @@ to remove the
247 lost_bucket
= &sline
[nb
-1]; /* sline is 0 based */
254 append_lost(lost_bucket
, n
, line
+1);
257 sline
[lno
-1].flag
&= pmask
;
266 static unsigned long context
= 3;
267 static char combine_marker
= '@';
269 static int interesting(struct sline
*sline
, unsigned long all_mask
)
271 return ((sline
->flag
& all_mask
) != all_mask
|| sline
->lost_head
);
274 static unsigned long line_common_diff(struct sline
*sline
, unsigned long all_mask
)
277 * Look at the line and see from which parents we have the
281 /* Lower bits of sline->flag records if the parent had this
282 * line, so XOR with all_mask gives us on-bits for parents we
283 * have differences with.
285 unsigned long common_adds
= (sline
->flag
^ all_mask
) & all_mask
;
286 unsigned long common_removes
= all_mask
;
288 /* If all the parents have this line, that also counts as
289 * having the same difference.
292 common_adds
= all_mask
;
294 if (sline
->lost_head
) {
295 /* Lost head list records the lines removed from
296 * the parents, and parent_map records from which
297 * parent the line was removed.
300 for (ll
= sline
->lost_head
; ll
; ll
= ll
->next
) {
301 common_removes
&= ll
->parent_map
;
304 return common_adds
& common_removes
;
307 static unsigned long line_all_diff(struct sline
*sline
, unsigned long all_mask
)
310 * Look at the line and see from which parents we have some difference.
312 unsigned long different
= (sline
->flag
^ all_mask
) & all_mask
;
313 if (sline
->lost_head
) {
314 /* Lost head list records the lines removed from
315 * the parents, and parent_map records from which
316 * parent the line was removed.
319 for (ll
= sline
->lost_head
; ll
; ll
= ll
->next
) {
320 different
|= ll
->parent_map
;
326 static int make_hunks(struct sline
*sline
, unsigned long cnt
,
327 int num_parent
, int dense
)
329 unsigned long all_mask
= (1UL<<num_parent
) - 1;
330 unsigned long mark
= (1UL<<num_parent
);
332 int has_interesting
= 0;
336 if (interesting(&sline
[i
], all_mask
)) {
337 unsigned long j
= (context
< i
) ? i
- context
: 0;
339 sline
[j
++].flag
|= mark
;
341 if (!interesting(&sline
[i
], all_mask
))
343 sline
[i
].flag
|= mark
;
345 j
= (i
+ context
< cnt
) ? i
+ context
: cnt
;
347 sline
[i
++].flag
|= mark
;
354 return has_interesting
;
356 /* Look at each hunk, and if we have changes from only one
357 * parent, or the changes are the same from all but one
358 * parent, mark that uninteresting.
363 int j
, hunk_end
, same
, diff
;
364 unsigned long same_diff
, all_diff
;
365 while (i
< cnt
&& !(sline
[i
].flag
& mark
))
368 break; /* No more interesting hunks */
369 for (hunk_end
= i
+ 1; hunk_end
< cnt
; hunk_end
++)
370 if (!(sline
[hunk_end
].flag
& mark
))
372 /* [i..hunk_end) are interesting. Now does it have
373 * the same change with all but one parent?
375 same_diff
= all_mask
;
377 for (j
= i
; j
< hunk_end
; j
++) {
378 same_diff
&= line_common_diff(sline
+ j
, all_mask
);
379 all_diff
|= line_all_diff(sline
+ j
, all_mask
);
382 for (j
= 0; j
< num_parent
; j
++) {
383 if (same_diff
& (1UL<<j
))
385 if (all_diff
& (1UL<<j
))
388 if ((num_parent
- 1 <= same
) || (diff
== 1)) {
389 /* This hunk is not that interesting after all */
390 for (j
= i
; j
< hunk_end
; j
++)
391 sline
[j
].flag
&= ~mark
;
397 return has_interesting
;
400 static void dump_sline(struct sline
*sline
, int cnt
, int num_parent
)
402 unsigned long mark
= (1UL<<num_parent
);
407 struct sline
*sl
= &sline
[lno
];
409 while (lno
< cnt
&& !(sline
[lno
].flag
& mark
))
413 for (hunk_end
= lno
+ 1; hunk_end
< cnt
; hunk_end
++)
414 if (!(sline
[hunk_end
].flag
& mark
))
416 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
417 printf(" +%d,%d ", lno
+1, hunk_end
-lno
);
418 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
420 while (lno
< hunk_end
) {
426 for (j
= 0; j
< num_parent
; j
++) {
427 if (ll
->parent_map
& (1UL<<j
))
435 for (j
= 0; j
< num_parent
; j
++) {
436 if ((1UL<<j
) & sl
->flag
)
441 printf("%.*s\n", sl
->len
, sl
->bol
);
446 static int show_combined_diff(struct path_list
*elem
, int num_parent
,
447 int dense
, const char *header
, int show_empty
)
449 unsigned long size
, cnt
, lno
;
450 char *result
, *cp
, *ep
;
451 struct sline
*sline
; /* survived lines */
452 int i
, show_hunks
, shown_header
= 0;
453 char ourtmp
[TMPPATHLEN
];
455 /* Read the result of merge first */
456 result
= grab_blob(elem
->sha1
, &size
);
457 write_to_temp_file(ourtmp
, result
, size
);
459 for (cnt
= 0, cp
= result
; cp
- result
< size
; cp
++) {
463 if (result
[size
-1] != '\n')
464 cnt
++; /* incomplete line */
466 sline
= xcalloc(cnt
, sizeof(*sline
));
468 sline
[0].bol
= result
;
469 for (lno
= 0, cp
= result
; cp
- result
< size
; cp
++) {
471 sline
[lno
].lost_tail
= &sline
[lno
].lost_head
;
472 sline
[lno
].len
= cp
- sline
[lno
].bol
;
473 sline
[lno
].flag
= (1UL<<num_parent
) - 1;
476 sline
[lno
].bol
= cp
+ 1;
479 if (result
[size
-1] != '\n') {
480 sline
[cnt
-1].lost_tail
= &sline
[cnt
-1].lost_head
;
481 sline
[cnt
-1].len
= size
- (sline
[cnt
-1].bol
- result
);
482 sline
[cnt
-1].flag
= (1UL<<num_parent
) - 1;
485 for (i
= 0; i
< num_parent
; i
++)
486 combine_diff(elem
->parent_sha1
[i
], ourtmp
, sline
, cnt
, i
);
488 show_hunks
= make_hunks(sline
, cnt
, num_parent
, dense
);
490 if (header
&& (show_hunks
|| show_empty
)) {
495 printf("diff --%s ", dense
? "cc" : "combined");
496 if (quote_c_style(elem
->path
, NULL
, NULL
, 0))
497 quote_c_style(elem
->path
, NULL
, stdout
, 0);
499 printf("%s", elem
->path
);
501 dump_sline(sline
, cnt
, num_parent
);
506 for (i
= 0; i
< cnt
; i
++) {
507 if (sline
[i
].lost_head
) {
508 struct lline
*ll
= sline
[i
].lost_head
;
510 struct lline
*tmp
= ll
;
520 int diff_tree_combined_merge(const unsigned char *sha1
,
522 int show_empty_merge
, int dense
)
524 struct commit
*commit
= lookup_commit(sha1
);
525 struct diff_options diffopts
;
526 struct commit_list
*parents
;
527 struct path_list
*p
, *paths
= NULL
;
528 int num_parent
, i
, num_paths
;
530 diff_setup(&diffopts
);
531 diffopts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
532 diffopts
.recursive
= 1;
535 for (parents
= commit
->parents
, num_parent
= 0;
537 parents
= parents
->next
, num_parent
++)
540 /* find set of paths that everybody touches */
541 for (parents
= commit
->parents
, i
= 0;
543 parents
= parents
->next
, i
++) {
544 struct commit
*parent
= parents
->item
;
545 diff_tree_sha1(parent
->object
.sha1
, commit
->object
.sha1
, "",
547 paths
= intersect_paths(paths
, i
, num_parent
);
548 diff_flush(&diffopts
);
551 /* find out surviving paths */
552 for (num_paths
= 0, p
= paths
; p
; p
= p
->next
) {
556 if (num_paths
|| show_empty_merge
) {
557 for (p
= paths
; p
; p
= p
->next
) {
560 if (show_combined_diff(p
, num_parent
, dense
, header
,
566 /* Clean things up */
568 struct path_list
*tmp
= paths
;