ll-merge.c: remove implicit dependency on the_index
[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
15b4f7a6 32static void *three_way_filemerge(const char *path, mmfile_t *base, mmfile_t *our, mmfile_t *their, unsigned long *size)
0c799383 33{
e2b70087 34 int merge_status;
15b4f7a6 35 mmbuffer_t res;
0c799383 36
e44b3851
JN
37 /*
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.
42 */
f01de62e 43 merge_status = ll_merge(&res, path, base, NULL,
32eaa468
NTND
44 our, ".our", their, ".their",
45 &the_index, NULL);
e2b70087
JS
46 if (merge_status < 0)
47 return NULL;
48
49 *size = res.size;
50 return res.ptr;
0c799383
LT
51}
52
fa2364ec 53void *merge_blobs(const char *path, struct blob *base, struct blob *our, struct blob *their, unsigned long *size)
0c799383
LT
54{
55 void *res = NULL;
56 mmfile_t f1, f2, common;
57
58 /*
59 * Removed in either branch?
60 *
61 * NOTE! This depends on the caller having done the
62 * proper warning about removing a file that got
63 * modified in the other branch!
64 */
65 if (!our || !their) {
21666f1a 66 enum object_type type;
0c799383
LT
67 if (base)
68 return NULL;
69 if (!our)
70 our = their;
b4f5aca4 71 return read_object_file(&our->object.oid, &type, size);
0c799383
LT
72 }
73
74 if (fill_mmfile_blob(&f1, our) < 0)
75 goto out_no_mmfile;
76 if (fill_mmfile_blob(&f2, their) < 0)
77 goto out_free_f1;
78
79 if (base) {
80 if (fill_mmfile_blob(&common, base) < 0)
81 goto out_free_f2_f1;
82 } else {
b779b3a1
JK
83 common.ptr = xstrdup("");
84 common.size = 0;
0c799383 85 }
15b4f7a6 86 res = three_way_filemerge(path, &common, &f1, &f2, size);
0c799383
LT
87 free_mmfile(&common);
88out_free_f2_f1:
89 free_mmfile(&f2);
90out_free_f1:
91 free_mmfile(&f1);
92out_no_mmfile:
93 return res;
94}