Merge branch 'ma/t7005-bash-workaround' into maint
[git] / merge-blobs.c
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"
7 #include "object-store.h"
8
9 static int fill_mmfile_blob(mmfile_t *f, struct blob *obj)
10 {
11         void *buf;
12         unsigned long size;
13         enum object_type type;
14
15         buf = read_object_file(&obj->object.oid, &type, &size);
16         if (!buf)
17                 return -1;
18         if (type != OBJ_BLOB) {
19                 free(buf);
20                 return -1;
21         }
22         f->ptr = buf;
23         f->size = size;
24         return 0;
25 }
26
27 static void free_mmfile(mmfile_t *f)
28 {
29         free(f->ptr);
30 }
31
32 static void *three_way_filemerge(const char *path, mmfile_t *base, mmfile_t *our, mmfile_t *their, unsigned long *size)
33 {
34         int merge_status;
35         mmbuffer_t res;
36
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          */
43         merge_status = ll_merge(&res, path, base, NULL,
44                                 our, ".our", their, ".their", NULL);
45         if (merge_status < 0)
46                 return NULL;
47
48         *size = res.size;
49         return res.ptr;
50 }
51
52 void *merge_blobs(const char *path, struct blob *base, struct blob *our, struct blob *their, unsigned long *size)
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) {
65                 enum object_type type;
66                 if (base)
67                         return NULL;
68                 if (!our)
69                         our = their;
70                 return read_object_file(&our->object.oid, &type, size);
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 {
82                 common.ptr = xstrdup("");
83                 common.size = 0;
84         }
85         res = three_way_filemerge(path, &common, &f1, &f2, size);
86         free_mmfile(&common);
87 out_free_f2_f1:
88         free_mmfile(&f2);
89 out_free_f1:
90         free_mmfile(&f1);
91 out_no_mmfile:
92         return res;
93 }