Merge branch 'nd/packobjectshook-doc-fix' into maint
[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,
712516bc 44 our, ".our", their, ".their", NULL);
e2b70087
JS
45 if (merge_status < 0)
46 return NULL;
47
48 *size = res.size;
49 return res.ptr;
0c799383
LT
50}
51
fa2364ec 52void *merge_blobs(const char *path, struct blob *base, struct blob *our, struct blob *their, unsigned long *size)
0c799383
LT
53{
54 void *res = NULL;
55 mmfile_t f1, f2, common;
56
57 /*
58 * Removed in either branch?
59 *
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!
63 */
64 if (!our || !their) {
21666f1a 65 enum object_type type;
0c799383
LT
66 if (base)
67 return NULL;
68 if (!our)
69 our = their;
b4f5aca4 70 return read_object_file(&our->object.oid, &type, size);
0c799383
LT
71 }
72
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;
77
78 if (base) {
79 if (fill_mmfile_blob(&common, base) < 0)
80 goto out_free_f2_f1;
81 } else {
b779b3a1
JK
82 common.ptr = xstrdup("");
83 common.size = 0;
0c799383 84 }
15b4f7a6 85 res = three_way_filemerge(path, &common, &f1, &f2, size);
0c799383
LT
86 free_mmfile(&common);
87out_free_f2_f1:
88 free_mmfile(&f2);
89out_free_f1:
90 free_mmfile(&f1);
91out_no_mmfile:
92 return res;
93}