blob: 8138090f81cf726ee9834daa7edbfc5b542aec44 [file] [log] [blame]
Linus Torvalds0c799382006-06-28 22:06:36 -07001#include "cache.h"
2#include "run-command.h"
3#include "xdiff-interface.h"
Junio C Hamano15b4f7a2010-01-16 21:45:40 -08004#include "ll-merge.h"
Linus Torvalds0c799382006-06-28 22:06:36 -07005#include "blob.h"
Junio C Hamanofa2364e2012-12-06 15:08:01 -08006#include "merge-blobs.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07007#include "object-store.h"
Linus Torvalds0c799382006-06-28 22:06:36 -07008
Linus Torvalds0c799382006-06-28 22:06:36 -07009static int fill_mmfile_blob(mmfile_t *f, struct blob *obj)
10{
11 void *buf;
12 unsigned long size;
Nicolas Pitre21666f12007-02-26 14:55:59 -050013 enum object_type type;
Linus Torvalds0c799382006-06-28 22:06:36 -070014
brian m. carlsonb4f5aca2018-03-12 02:27:53 +000015 buf = read_object_file(&obj->object.oid, &type, &size);
Linus Torvalds0c799382006-06-28 22:06:36 -070016 if (!buf)
17 return -1;
Stefan Bellerd6878392015-03-20 17:28:03 -070018 if (type != OBJ_BLOB) {
19 free(buf);
Linus Torvalds0c799382006-06-28 22:06:36 -070020 return -1;
Stefan Bellerd6878392015-03-20 17:28:03 -070021 }
Linus Torvalds0c799382006-06-28 22:06:36 -070022 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
Nguyễn Thái Ngọc Duyf4a55b22018-09-21 17:57:28 +020032static void *three_way_filemerge(struct index_state *istate,
33 const char *path,
34 mmfile_t *base,
35 mmfile_t *our,
36 mmfile_t *their,
37 unsigned long *size)
Linus Torvalds0c799382006-06-28 22:06:36 -070038{
Elijah Newren35f69672022-02-02 02:37:30 +000039 enum ll_merge_result merge_status;
Junio C Hamano15b4f7a2010-01-16 21:45:40 -080040 mmbuffer_t res;
Linus Torvalds0c799382006-06-28 22:06:36 -070041
Jonathan Niedere44b3852010-03-20 19:40:53 -050042 /*
43 * This function is only used by cmd_merge_tree, which
44 * does not respect the merge.conflictstyle option.
45 * There is no need to worry about a label for the
46 * common ancestor.
47 */
Jonathan Niederf01de622010-03-20 19:38:58 -050048 merge_status = ll_merge(&res, path, base, NULL,
Nguyễn Thái Ngọc Duy32eaa462018-09-21 17:57:27 +020049 our, ".our", their, ".their",
Nguyễn Thái Ngọc Duyf4a55b22018-09-21 17:57:28 +020050 istate, NULL);
Johannes Schindeline2b70082006-12-13 00:01:41 +010051 if (merge_status < 0)
52 return NULL;
Elijah Newren35f69672022-02-02 02:37:30 +000053 if (merge_status == LL_MERGE_BINARY_CONFLICT)
54 warning("Cannot merge binary files: %s (%s vs. %s)",
55 path, ".our", ".their");
Johannes Schindeline2b70082006-12-13 00:01:41 +010056
57 *size = res.size;
58 return res.ptr;
Linus Torvalds0c799382006-06-28 22:06:36 -070059}
60
Nguyễn Thái Ngọc Duyf4a55b22018-09-21 17:57:28 +020061void *merge_blobs(struct index_state *istate, const char *path,
62 struct blob *base, struct blob *our,
63 struct blob *their, unsigned long *size)
Linus Torvalds0c799382006-06-28 22:06:36 -070064{
65 void *res = NULL;
66 mmfile_t f1, f2, common;
67
68 /*
69 * Removed in either branch?
70 *
71 * NOTE! This depends on the caller having done the
72 * proper warning about removing a file that got
73 * modified in the other branch!
74 */
75 if (!our || !their) {
Nicolas Pitre21666f12007-02-26 14:55:59 -050076 enum object_type type;
Linus Torvalds0c799382006-06-28 22:06:36 -070077 if (base)
78 return NULL;
79 if (!our)
80 our = their;
brian m. carlsonb4f5aca2018-03-12 02:27:53 +000081 return read_object_file(&our->object.oid, &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}