2 #include "run-command.h"
3 #include "xdiff-interface.h"
7 static int fill_mmfile_blob(mmfile_t
*f
, struct blob
*obj
)
11 enum object_type type
;
13 buf
= read_sha1_file(obj
->object
.sha1
, &type
, &size
);
23 static void free_mmfile(mmfile_t
*f
)
28 static void *three_way_filemerge(const char *path
, mmfile_t
*base
, mmfile_t
*our
, mmfile_t
*their
, unsigned long *size
)
33 merge_status
= ll_merge(&res
, path
, base
,
34 our
, ".our", their
, ".their", 0);
42 static int common_outf(void *priv_
, mmbuffer_t
*mb
, int nbuf
)
45 mmfile_t
*dst
= priv_
;
47 for (i
= 0; i
< nbuf
; i
++) {
48 memcpy(dst
->ptr
+ dst
->size
, mb
[i
].ptr
, mb
[i
].size
);
49 dst
->size
+= mb
[i
].size
;
54 static int generate_common_file(mmfile_t
*res
, mmfile_t
*f1
, mmfile_t
*f2
)
56 unsigned long size
= f1
->size
< f2
->size
? f1
->size
: f2
->size
;
57 void *ptr
= xmalloc(size
);
62 memset(&xpp
, 0, sizeof(xpp
));
63 xpp
.flags
= XDF_NEED_MINIMAL
;
64 memset(&xecfg
, 0, sizeof(xecfg
));
66 xecfg
.flags
= XDL_EMIT_COMMON
;
67 ecb
.outf
= common_outf
;
73 return xdi_diff(f1
, f2
, &xpp
, &xecfg
, &ecb
);
76 void *merge_file(const char *path
, struct blob
*base
, struct blob
*our
, struct blob
*their
, unsigned long *size
)
79 mmfile_t f1
, f2
, common
;
82 * Removed in either branch?
84 * NOTE! This depends on the caller having done the
85 * proper warning about removing a file that got
86 * modified in the other branch!
89 enum object_type type
;
94 return read_sha1_file(our
->object
.sha1
, &type
, size
);
97 if (fill_mmfile_blob(&f1
, our
) < 0)
99 if (fill_mmfile_blob(&f2
, their
) < 0)
103 if (fill_mmfile_blob(&common
, base
) < 0)
106 if (generate_common_file(&common
, &f1
, &f2
) < 0)
109 res
= three_way_filemerge(path
, &common
, &f1
, &f2
, size
);
110 free_mmfile(&common
);