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 void make_hunks(struct sline
*sline
, unsigned long cnt
, int num_parent
)
283 unsigned long all_mask
= (1UL<<num_parent
) - 1;
284 unsigned long mark
= (1UL<<num_parent
);
289 if (interesting(&sline
[i
], all_mask
)) {
290 unsigned long j
= (context
< i
) ? i
- context
: 0;
292 sline
[j
++].flag
|= mark
;
294 if (!interesting(&sline
[i
], all_mask
))
296 sline
[i
].flag
|= mark
;
298 j
= (i
+ context
< cnt
) ? i
+ context
: cnt
;
300 sline
[i
++].flag
|= mark
;
307 static void dump_sline(struct sline
*sline
, int cnt
, int num_parent
)
309 unsigned long mark
= (1UL<<num_parent
);
314 struct sline
*sl
= &sline
[lno
];
316 while (lno
< cnt
&& !(sline
[lno
].flag
& mark
))
320 for (hunk_end
= lno
+ 1; hunk_end
< cnt
; hunk_end
++)
321 if (!(sline
[hunk_end
].flag
& mark
))
323 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
324 printf(" +%d,%d ", lno
+1, hunk_end
-lno
);
325 for (i
= 0; i
<= num_parent
; i
++) putchar(combine_marker
);
327 while (lno
< hunk_end
) {
333 for (j
= 0; j
< num_parent
; j
++) {
334 if (ll
->parent_map
& (1UL<<j
))
343 for (j
= 0; j
< num_parent
; j
++) {
344 if ((1UL<<j
) & sl
->flag
)
349 printf(" %.*s\n", sl
->len
, sl
->bol
);
354 static void show_combined_diff(struct path_list
*elem
, int num_parent
)
356 unsigned long size
, cnt
, lno
;
357 char *result
, *cp
, *ep
;
358 struct sline
*sline
; /* survived lines */
360 char ourtmp
[TMPPATHLEN
];
362 /* Read the result of merge first */
363 result
= grab_blob(elem
->sha1
, &size
);
364 write_to_temp_file(ourtmp
, result
, size
);
366 for (cnt
= 0, cp
= result
; cp
- result
< size
; cp
++) {
370 if (result
[size
-1] != '\n')
371 cnt
++; /* incomplete line */
373 sline
= xcalloc(cnt
, sizeof(*sline
));
375 sline
[0].bol
= result
;
376 for (lno
= 0, cp
= result
; cp
- result
< size
; cp
++) {
378 sline
[lno
].len
= cp
- sline
[lno
].bol
;
379 sline
[lno
].flag
= (1UL<<num_parent
) - 1;
382 sline
[lno
].bol
= cp
+ 1;
385 if (result
[size
-1] != '\n') {
386 sline
[cnt
-1].len
= size
- (sline
[cnt
-1].bol
- result
);
387 sline
[cnt
-1].flag
= (1UL<<num_parent
) - 1;
390 for (i
= 0; i
< num_parent
; i
++)
391 combine_diff(elem
->parent_sha1
[i
], ourtmp
, sline
, cnt
, i
);
393 make_hunks(sline
, cnt
, num_parent
);
395 dump_sline(sline
, cnt
, num_parent
);
399 for (i
= 0; i
< cnt
; i
++) {
400 if (sline
[i
].lost_head
) {
401 struct lline
*ll
= sline
[i
].lost_head
;
403 struct lline
*tmp
= ll
;
412 int diff_tree_combined_merge(const unsigned char *sha1
,
413 const char *header
, int show_empty_merge
)
415 struct commit
*commit
= lookup_commit(sha1
);
416 struct diff_options diffopts
;
417 struct commit_list
*parents
;
418 struct path_list
*p
, *paths
= NULL
;
419 int num_parent
, i
, num_paths
;
421 diff_setup(&diffopts
);
422 diffopts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
423 diffopts
.recursive
= 1;
426 for (parents
= commit
->parents
, num_parent
= 0;
428 parents
= parents
->next
, num_parent
++)
431 /* find set of paths that everybody touches */
432 for (parents
= commit
->parents
, i
= 0;
434 parents
= parents
->next
, i
++) {
435 struct commit
*parent
= parents
->item
;
436 diff_tree_sha1(parent
->object
.sha1
, commit
->object
.sha1
, "",
438 paths
= intersect_paths(paths
, i
, num_parent
);
439 diff_flush(&diffopts
);
442 /* find out surviving paths */
443 for (num_paths
= 0, p
= paths
; p
; p
= p
->next
) {
447 if (num_paths
|| show_empty_merge
) {
449 for (p
= paths
; p
; p
= p
->next
) {
452 printf("diff --combined ");
453 if (quote_c_style(p
->path
, NULL
, NULL
, 0))
454 quote_c_style(p
->path
, NULL
, stdout
, 0);
456 printf("%s", p
->path
);
458 show_combined_diff(p
, num_parent
);
462 /* Clean things up */
464 struct path_list
*tmp
= paths
;