blob: 2f659fd01432d4249d1d2be1876f0edfd8c9cd09 [file] [log] [blame]
Elijah Newren1c028402023-02-24 00:09:32 +00001#include "git-compat-util.h"
Elijah Newren67238992023-05-16 06:34:04 +00002#include "merge-ll.h"
Linus Torvalds0c799382006-06-28 22:06:36 -07003#include "blob.h"
Junio C Hamanofa2364e2012-12-06 15:08:01 -08004#include "merge-blobs.h"
Elijah Newrena034e912023-05-16 06:34:06 +00005#include "object-store-ll.h"
Linus Torvalds0c799382006-06-28 22:06:36 -07006
Linus Torvalds0c799382006-06-28 22:06:36 -07007static int fill_mmfile_blob(mmfile_t *f, struct blob *obj)
8{
9 void *buf;
10 unsigned long size;
Nicolas Pitre21666f12007-02-26 14:55:59 -050011 enum object_type type;
Linus Torvalds0c799382006-06-28 22:06:36 -070012
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +020013 buf = repo_read_object_file(the_repository, &obj->object.oid, &type,
14 &size);
Linus Torvalds0c799382006-06-28 22:06:36 -070015 if (!buf)
16 return -1;
Stefan Bellerd6878392015-03-20 17:28:03 -070017 if (type != OBJ_BLOB) {
18 free(buf);
Linus Torvalds0c799382006-06-28 22:06:36 -070019 return -1;
Stefan Bellerd6878392015-03-20 17:28:03 -070020 }
Linus Torvalds0c799382006-06-28 22:06:36 -070021 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
Nguyễn Thái Ngọc Duyf4a55b22018-09-21 17:57:28 +020031static void *three_way_filemerge(struct index_state *istate,
32 const char *path,
33 mmfile_t *base,
34 mmfile_t *our,
35 mmfile_t *their,
36 unsigned long *size)
Linus Torvalds0c799382006-06-28 22:06:36 -070037{
Elijah Newren35f69672022-02-02 02:37:30 +000038 enum ll_merge_result merge_status;
Junio C Hamano15b4f7a2010-01-16 21:45:40 -080039 mmbuffer_t res;
Linus Torvalds0c799382006-06-28 22:06:36 -070040
Jonathan Niedere44b3852010-03-20 19:40:53 -050041 /*
42 * This function is only used by cmd_merge_tree, which
43 * does not respect the merge.conflictstyle option.
44 * There is no need to worry about a label for the
45 * common ancestor.
46 */
Jonathan Niederf01de622010-03-20 19:38:58 -050047 merge_status = ll_merge(&res, path, base, NULL,
Nguyễn Thái Ngọc Duy32eaa462018-09-21 17:57:27 +020048 our, ".our", their, ".their",
Nguyễn Thái Ngọc Duyf4a55b22018-09-21 17:57:28 +020049 istate, NULL);
Johannes Schindeline2b70082006-12-13 00:01:41 +010050 if (merge_status < 0)
51 return NULL;
Elijah Newren35f69672022-02-02 02:37:30 +000052 if (merge_status == LL_MERGE_BINARY_CONFLICT)
53 warning("Cannot merge binary files: %s (%s vs. %s)",
54 path, ".our", ".their");
Johannes Schindeline2b70082006-12-13 00:01:41 +010055
56 *size = res.size;
57 return res.ptr;
Linus Torvalds0c799382006-06-28 22:06:36 -070058}
59
Nguyễn Thái Ngọc Duyf4a55b22018-09-21 17:57:28 +020060void *merge_blobs(struct index_state *istate, const char *path,
61 struct blob *base, struct blob *our,
62 struct blob *their, unsigned long *size)
Linus Torvalds0c799382006-06-28 22:06:36 -070063{
64 void *res = NULL;
65 mmfile_t f1, f2, common;
66
67 /*
68 * Removed in either branch?
69 *
70 * NOTE! This depends on the caller having done the
71 * proper warning about removing a file that got
72 * modified in the other branch!
73 */
74 if (!our || !their) {
Nicolas Pitre21666f12007-02-26 14:55:59 -050075 enum object_type type;
Linus Torvalds0c799382006-06-28 22:06:36 -070076 if (base)
77 return NULL;
78 if (!our)
79 our = their;
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +020080 return repo_read_object_file(the_repository, &our->object.oid,
81 &type, size);
Linus Torvalds0c799382006-06-28 22:06:36 -070082 }
83
84 if (fill_mmfile_blob(&f1, our) < 0)
85 goto out_no_mmfile;
86 if (fill_mmfile_blob(&f2, their) < 0)
87 goto out_free_f1;
88
89 if (base) {
90 if (fill_mmfile_blob(&common, base) < 0)
91 goto out_free_f2_f1;
92 } else {
Jeff Kingb779b3a2016-02-23 01:06:55 -050093 common.ptr = xstrdup("");
94 common.size = 0;
Linus Torvalds0c799382006-06-28 22:06:36 -070095 }
Nguyễn Thái Ngọc Duyf4a55b22018-09-21 17:57:28 +020096 res = three_way_filemerge(istate, path, &common, &f1, &f2, size);
Linus Torvalds0c799382006-06-28 22:06:36 -070097 free_mmfile(&common);
98out_free_f2_f1:
99 free_mmfile(&f2);
100out_free_f1:
101 free_mmfile(&f1);
102out_no_mmfile:
103 return res;
104}