Merge branch 'rs/fix-arghelp'
[git.git] / merge-blobs.c
blob9293cbf75c8ab737a2cf7267d3e31ffd93a48dc8
1 #include "git-compat-util.h"
2 #include "run-command.h"
3 #include "xdiff-interface.h"
4 #include "merge-ll.h"
5 #include "blob.h"
6 #include "merge-blobs.h"
7 #include "object-store-ll.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 = repo_read_object_file(the_repository, &obj->object.oid, &type,
16 &size);
17 if (!buf)
18 return -1;
19 if (type != OBJ_BLOB) {
20 free(buf);
21 return -1;
23 f->ptr = buf;
24 f->size = size;
25 return 0;
28 static void free_mmfile(mmfile_t *f)
30 free(f->ptr);
33 static void *three_way_filemerge(struct index_state *istate,
34 const char *path,
35 mmfile_t *base,
36 mmfile_t *our,
37 mmfile_t *their,
38 unsigned long *size)
40 enum ll_merge_result merge_status;
41 mmbuffer_t res;
44 * This function is only used by cmd_merge_tree, which
45 * does not respect the merge.conflictstyle option.
46 * There is no need to worry about a label for the
47 * common ancestor.
49 merge_status = ll_merge(&res, path, base, NULL,
50 our, ".our", their, ".their",
51 istate, NULL);
52 if (merge_status < 0)
53 return NULL;
54 if (merge_status == LL_MERGE_BINARY_CONFLICT)
55 warning("Cannot merge binary files: %s (%s vs. %s)",
56 path, ".our", ".their");
58 *size = res.size;
59 return res.ptr;
62 void *merge_blobs(struct index_state *istate, const char *path,
63 struct blob *base, struct blob *our,
64 struct blob *their, unsigned long *size)
66 void *res = NULL;
67 mmfile_t f1, f2, common;
70 * Removed in either branch?
72 * NOTE! This depends on the caller having done the
73 * proper warning about removing a file that got
74 * modified in the other branch!
76 if (!our || !their) {
77 enum object_type type;
78 if (base)
79 return NULL;
80 if (!our)
81 our = their;
82 return repo_read_object_file(the_repository, &our->object.oid,
83 &type, size);
86 if (fill_mmfile_blob(&f1, our) < 0)
87 goto out_no_mmfile;
88 if (fill_mmfile_blob(&f2, their) < 0)
89 goto out_free_f1;
91 if (base) {
92 if (fill_mmfile_blob(&common, base) < 0)
93 goto out_free_f2_f1;
94 } else {
95 common.ptr = xstrdup("");
96 common.size = 0;
98 res = three_way_filemerge(istate, path, &common, &f1, &f2, size);
99 free_mmfile(&common);
100 out_free_f2_f1:
101 free_mmfile(&f2);
102 out_free_f1:
103 free_mmfile(&f1);
104 out_no_mmfile:
105 return res;