Git 2.44
[git.git] / merge-blobs.c
blob2f659fd01432d4249d1d2be1876f0edfd8c9cd09
1 #include "git-compat-util.h"
2 #include "merge-ll.h"
3 #include "blob.h"
4 #include "merge-blobs.h"
5 #include "object-store-ll.h"
7 static int fill_mmfile_blob(mmfile_t *f, struct blob *obj)
9 void *buf;
10 unsigned long size;
11 enum object_type type;
13 buf = repo_read_object_file(the_repository, &obj->object.oid, &type,
14 &size);
15 if (!buf)
16 return -1;
17 if (type != OBJ_BLOB) {
18 free(buf);
19 return -1;
21 f->ptr = buf;
22 f->size = size;
23 return 0;
26 static void free_mmfile(mmfile_t *f)
28 free(f->ptr);
31 static void *three_way_filemerge(struct index_state *istate,
32 const char *path,
33 mmfile_t *base,
34 mmfile_t *our,
35 mmfile_t *their,
36 unsigned long *size)
38 enum ll_merge_result merge_status;
39 mmbuffer_t res;
42 * This function is only used by cmd_merge_tree, which
43 * does not respect the merge.conflictstyle option.
44 * There is no need to worry about a label for the
45 * common ancestor.
47 merge_status = ll_merge(&res, path, base, NULL,
48 our, ".our", their, ".their",
49 istate, NULL);
50 if (merge_status < 0)
51 return NULL;
52 if (merge_status == LL_MERGE_BINARY_CONFLICT)
53 warning("Cannot merge binary files: %s (%s vs. %s)",
54 path, ".our", ".their");
56 *size = res.size;
57 return res.ptr;
60 void *merge_blobs(struct index_state *istate, const char *path,
61 struct blob *base, struct blob *our,
62 struct blob *their, unsigned long *size)
64 void *res = NULL;
65 mmfile_t f1, f2, common;
68 * Removed in either branch?
70 * NOTE! This depends on the caller having done the
71 * proper warning about removing a file that got
72 * modified in the other branch!
74 if (!our || !their) {
75 enum object_type type;
76 if (base)
77 return NULL;
78 if (!our)
79 our = their;
80 return repo_read_object_file(the_repository, &our->object.oid,
81 &type, size);
84 if (fill_mmfile_blob(&f1, our) < 0)
85 goto out_no_mmfile;
86 if (fill_mmfile_blob(&f2, their) < 0)
87 goto out_free_f1;
89 if (base) {
90 if (fill_mmfile_blob(&common, base) < 0)
91 goto out_free_f2_f1;
92 } else {
93 common.ptr = xstrdup("");
94 common.size = 0;
96 res = three_way_filemerge(istate, path, &common, &f1, &f2, size);
97 free_mmfile(&common);
98 out_free_f2_f1:
99 free_mmfile(&f2);
100 out_free_f1:
101 free_mmfile(&f1);
102 out_no_mmfile:
103 return res;