2 #include "run-command.h"
3 #include "xdiff-interface.h"
6 static int fill_mmfile_blob(mmfile_t
*f
, struct blob
*obj
)
10 enum object_type type
;
12 buf
= read_sha1_file(obj
->object
.sha1
, &type
, &size
);
22 static void free_mmfile(mmfile_t
*f
)
27 static void *three_way_filemerge(mmfile_t
*base
, mmfile_t
*our
, mmfile_t
*their
, unsigned long *size
)
33 memset(&xpp
, 0, sizeof(xpp
));
34 merge_status
= xdl_merge(base
, our
, ".our", their
, ".their",
35 &xpp
, XDL_MERGE_ZEALOUS
, &res
);
44 static int common_outf(void *priv_
, mmbuffer_t
*mb
, int nbuf
)
47 mmfile_t
*dst
= priv_
;
49 for (i
= 0; i
< nbuf
; i
++) {
50 memcpy(dst
->ptr
+ dst
->size
, mb
[i
].ptr
, mb
[i
].size
);
51 dst
->size
+= mb
[i
].size
;
56 static int generate_common_file(mmfile_t
*res
, mmfile_t
*f1
, mmfile_t
*f2
)
58 unsigned long size
= f1
->size
< f2
->size
? f1
->size
: f2
->size
;
59 void *ptr
= xmalloc(size
);
64 memset(&xpp
, 0, sizeof(xpp
));
65 xpp
.flags
= XDF_NEED_MINIMAL
;
66 memset(&xecfg
, 0, sizeof(xecfg
));
68 xecfg
.flags
= XDL_EMIT_COMMON
;
69 ecb
.outf
= common_outf
;
75 return xdi_diff(f1
, f2
, &xpp
, &xecfg
, &ecb
);
78 void *merge_file(struct blob
*base
, struct blob
*our
, struct blob
*their
, unsigned long *size
)
81 mmfile_t f1
, f2
, common
;
84 * Removed in either branch?
86 * NOTE! This depends on the caller having done the
87 * proper warning about removing a file that got
88 * modified in the other branch!
91 enum object_type type
;
96 return read_sha1_file(our
->object
.sha1
, &type
, size
);
99 if (fill_mmfile_blob(&f1
, our
) < 0)
101 if (fill_mmfile_blob(&f2
, their
) < 0)
105 if (fill_mmfile_blob(&common
, base
) < 0)
108 if (generate_common_file(&common
, &f1
, &f2
) < 0)
111 res
= three_way_filemerge(&common
, &f1
, &f2
, size
);
112 free_mmfile(&common
);