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"
78
static int fill_mmfile_blob(mmfile_t *f, struct blob *obj)
9{
10void *buf;
11unsigned long size;
12enum object_type type;
1314
buf = read_object_file(&obj->object.oid, &type, &size);
15if (!buf)
16return -1;
17if (type != OBJ_BLOB) {
18free(buf);
19return -1;
20}
21f->ptr = buf;
22f->size = size;
23return 0;
24}
2526
static void free_mmfile(mmfile_t *f)
27{
28free(f->ptr);
29}
3031
static void *three_way_filemerge(const char *path, mmfile_t *base, mmfile_t *our, mmfile_t *their, unsigned long *size)
32{
33int merge_status;
34mmbuffer_t res;
3536
/*
37* This function is only used by cmd_merge_tree, which
38* does not respect the merge.conflictstyle option.
39* There is no need to worry about a label for the
40* common ancestor.
41*/
42merge_status = ll_merge(&res, path, base, NULL,
43our, ".our", their, ".their", NULL);
44if (merge_status < 0)
45return NULL;
4647
*size = res.size;
48return res.ptr;
49}
5051
void *merge_blobs(const char *path, struct blob *base, struct blob *our, struct blob *their, unsigned long *size)
52{
53void *res = NULL;
54mmfile_t f1, f2, common;
5556
/*
57* Removed in either branch?
58*
59* NOTE! This depends on the caller having done the
60* proper warning about removing a file that got
61* modified in the other branch!
62*/
63if (!our || !their) {
64enum object_type type;
65if (base)
66return NULL;
67if (!our)
68our = their;
69return read_object_file(&our->object.oid, &type, size);
70}
7172
if (fill_mmfile_blob(&f1, our) < 0)
73goto out_no_mmfile;
74if (fill_mmfile_blob(&f2, their) < 0)
75goto out_free_f1;
7677
if (base) {
78if (fill_mmfile_blob(&common, base) < 0)
79goto out_free_f2_f1;
80} else {
81common.ptr = xstrdup("");
82common.size = 0;
83}
84res = three_way_filemerge(path, &common, &f1, &f2, size);
85free_mmfile(&common);
86out_free_f2_f1:
87free_mmfile(&f2);
88out_free_f1:
89free_mmfile(&f1);
90out_no_mmfile:
91return res;
92}