add core.usereplacerefs config option
[git/debian.git] / merge-blobs.c
blobfabb8c19ce983962b45c415f66c11236fa906bfe
1 #include "cache.h"
2 #include "run-command.h"
3 #include "xdiff-interface.h"
4 #include "ll-merge.h"
5 #include "blob.h"
6 #include "merge-blobs.h"
7 #include "object-store.h"
9 static int fill_mmfile_blob(mmfile_t *f, struct blob *obj)
11 void *buf;
12 unsigned long size;
13 enum object_type type;
15 buf = read_object_file(&obj->object.oid, &type, &size);
16 if (!buf)
17 return -1;
18 if (type != OBJ_BLOB) {
19 free(buf);
20 return -1;
22 f->ptr = buf;
23 f->size = size;
24 return 0;
27 static void free_mmfile(mmfile_t *f)
29 free(f->ptr);
32 static void *three_way_filemerge(const char *path, mmfile_t *base, mmfile_t *our, mmfile_t *their, unsigned long *size)
34 int merge_status;
35 mmbuffer_t res;
38 * This function is only used by cmd_merge_tree, which
39 * does not respect the merge.conflictstyle option.
40 * There is no need to worry about a label for the
41 * common ancestor.
43 merge_status = ll_merge(&res, path, base, NULL,
44 our, ".our", their, ".their", NULL);
45 if (merge_status < 0)
46 return NULL;
48 *size = res.size;
49 return res.ptr;
52 void *merge_blobs(const char *path, struct blob *base, struct blob *our, struct blob *their, unsigned long *size)
54 void *res = NULL;
55 mmfile_t f1, f2, common;
58 * Removed in either branch?
60 * NOTE! This depends on the caller having done the
61 * proper warning about removing a file that got
62 * modified in the other branch!
64 if (!our || !their) {
65 enum object_type type;
66 if (base)
67 return NULL;
68 if (!our)
69 our = their;
70 return read_object_file(&our->object.oid, &type, size);
73 if (fill_mmfile_blob(&f1, our) < 0)
74 goto out_no_mmfile;
75 if (fill_mmfile_blob(&f2, their) < 0)
76 goto out_free_f1;
78 if (base) {
79 if (fill_mmfile_blob(&common, base) < 0)
80 goto out_free_f2_f1;
81 } else {
82 common.ptr = xstrdup("");
83 common.size = 0;
85 res = three_way_filemerge(path, &common, &f1, &f2, size);
86 free_mmfile(&common);
87 out_free_f2_f1:
88 free_mmfile(&f2);
89 out_free_f1:
90 free_mmfile(&f1);
91 out_no_mmfile:
92 return res;