]> git.ipfire.org Git - thirdparty/git.git/blame - merge-blobs.c
sha1_file: convert read_sha1_file to struct object_id
[thirdparty/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"
0c799383 7
0c799383
LT
8static int fill_mmfile_blob(mmfile_t *f, struct blob *obj)
9{
10 void *buf;
11 unsigned long size;
21666f1a 12 enum object_type type;
0c799383 13
b4f5aca4 14 buf = read_object_file(&obj->object.oid, &type, &size);
0c799383
LT
15 if (!buf)
16 return -1;
d687839c
SB
17 if (type != OBJ_BLOB) {
18 free(buf);
0c799383 19 return -1;
d687839c 20 }
0c799383
LT
21 f->ptr = buf;
22 f->size = size;
23 return 0;
24}
25
26static void free_mmfile(mmfile_t *f)
27{
28 free(f->ptr);
29}
30
15b4f7a6 31static void *three_way_filemerge(const char *path, mmfile_t *base, mmfile_t *our, mmfile_t *their, unsigned long *size)
0c799383 32{
e2b70087 33 int merge_status;
15b4f7a6 34 mmbuffer_t res;
0c799383 35
e44b3851
JN
36 /*
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 */
f01de62e 42 merge_status = ll_merge(&res, path, base, NULL,
712516bc 43 our, ".our", their, ".their", NULL);
e2b70087
JS
44 if (merge_status < 0)
45 return NULL;
46
47 *size = res.size;
48 return res.ptr;
0c799383
LT
49}
50
fa2364ec 51void *merge_blobs(const char *path, struct blob *base, struct blob *our, struct blob *their, unsigned long *size)
0c799383
LT
52{
53 void *res = NULL;
54 mmfile_t f1, f2, common;
55
56 /*
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 */
63 if (!our || !their) {
21666f1a 64 enum object_type type;
0c799383
LT
65 if (base)
66 return NULL;
67 if (!our)
68 our = their;
b4f5aca4 69 return read_object_file(&our->object.oid, &type, size);
0c799383
LT
70 }
71
72 if (fill_mmfile_blob(&f1, our) < 0)
73 goto out_no_mmfile;
74 if (fill_mmfile_blob(&f2, their) < 0)
75 goto out_free_f1;
76
77 if (base) {
78 if (fill_mmfile_blob(&common, base) < 0)
79 goto out_free_f2_f1;
80 } else {
b779b3a1
JK
81 common.ptr = xstrdup("");
82 common.size = 0;
0c799383 83 }
15b4f7a6 84 res = three_way_filemerge(path, &common, &f1, &f2, size);
0c799383
LT
85 free_mmfile(&common);
86out_free_f2_f1:
87 free_mmfile(&f2);
88out_free_f1:
89 free_mmfile(&f1);
90out_no_mmfile:
91 return res;
92}