Merge branch 'bw/submodule-helper-usage-fix'
[git/git.git] / merge-blobs.c
CommitLineData
0c799383
LT
1#include "cache.h"
2#include "run-command.h"
3#include "xdiff-interface.h"
15b4f7a6 4#include "ll-merge.h"
0c799383 5#include "blob.h"
fa2364ec 6#include "merge-blobs.h"
cbd53a21 7#include "object-store.h"
0c799383 8
0c799383
LT
9static int fill_mmfile_blob(mmfile_t *f, struct blob *obj)
10{
11 void *buf;
12 unsigned long size;
21666f1a 13 enum object_type type;
0c799383 14
b4f5aca4 15 buf = read_object_file(&obj->object.oid, &type, &size);
0c799383
LT
16 if (!buf)
17 return -1;
d687839c
SB
18 if (type != OBJ_BLOB) {
19 free(buf);
0c799383 20 return -1;
d687839c 21 }
0c799383
LT
22 f->ptr = buf;
23 f->size = size;
24 return 0;
25}
26
27static void free_mmfile(mmfile_t *f)
28{
29 free(f->ptr);
30}
31
f4a55b27
NTND
32static void *three_way_filemerge(struct index_state *istate,
33 const char *path,
34 mmfile_t *base,
35 mmfile_t *our,
36 mmfile_t *their,
37 unsigned long *size)
0c799383 38{
e2b70087 39 int merge_status;
15b4f7a6 40 mmbuffer_t res;
0c799383 41
e44b3851
JN
42 /*
43 * This function is only used by cmd_merge_tree, which
44 * does not respect the merge.conflictstyle option.
45 * There is no need to worry about a label for the
46 * common ancestor.
47 */
f01de62e 48 merge_status = ll_merge(&res, path, base, NULL,
32eaa468 49 our, ".our", their, ".their",
f4a55b27 50 istate, NULL);
e2b70087
JS
51 if (merge_status < 0)
52 return NULL;
53
54 *size = res.size;
55 return res.ptr;
0c799383
LT
56}
57
f4a55b27
NTND
58void *merge_blobs(struct index_state *istate, const char *path,
59 struct blob *base, struct blob *our,
60 struct blob *their, unsigned long *size)
0c799383
LT
61{
62 void *res = NULL;
63 mmfile_t f1, f2, common;
64
65 /*
66 * Removed in either branch?
67 *
68 * NOTE! This depends on the caller having done the
69 * proper warning about removing a file that got
70 * modified in the other branch!
71 */
72 if (!our || !their) {
21666f1a 73 enum object_type type;
0c799383
LT
74 if (base)
75 return NULL;
76 if (!our)
77 our = their;
b4f5aca4 78 return read_object_file(&our->object.oid, &type, size);
0c799383
LT
79 }
80
81 if (fill_mmfile_blob(&f1, our) < 0)
82 goto out_no_mmfile;
83 if (fill_mmfile_blob(&f2, their) < 0)
84 goto out_free_f1;
85
86 if (base) {
87 if (fill_mmfile_blob(&common, base) < 0)
88 goto out_free_f2_f1;
89 } else {
b779b3a1
JK
90 common.ptr = xstrdup("");
91 common.size = 0;
0c799383 92 }
f4a55b27 93 res = three_way_filemerge(istate, path, &common, &f1, &f2, size);
0c799383
LT
94 free_mmfile(&common);
95out_free_f2_f1:
96 free_mmfile(&f2);
97out_free_f1:
98 free_mmfile(&f1);
99out_no_mmfile:
100 return res;
101}