blob: 9293cbf75c8ab737a2cf7267d3e31ffd93a48dc8 [file] [log] [blame]
Elijah Newren1c028402023-02-24 00:09:32 +00001#include "git-compat-util.h"
Linus Torvalds0c799382006-06-28 22:06:36 -07002#include "run-command.h"
3#include "xdiff-interface.h"
Elijah Newren67238992023-05-16 06:34:04 +00004#include "merge-ll.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"
Elijah Newrena034e912023-05-16 06:34:06 +00007#include "object-store-ll.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
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +020015 buf = repo_read_object_file(the_repository, &obj->object.oid, &type,
16 &size);
Linus Torvalds0c799382006-06-28 22:06:36 -070017 if (!buf)
18 return -1;
Stefan Bellerd6878392015-03-20 17:28:03 -070019 if (type != OBJ_BLOB) {
20 free(buf);
Linus Torvalds0c799382006-06-28 22:06:36 -070021 return -1;
Stefan Bellerd6878392015-03-20 17:28:03 -070022 }
Linus Torvalds0c799382006-06-28 22:06:36 -070023 f->ptr = buf;
24 f->size = size;
25 return 0;
26}
27
28static void free_mmfile(mmfile_t *f)
29{
30 free(f->ptr);
31}
32
Nguyễn Thái Ngọc Duyf4a55b22018-09-21 17:57:28 +020033static void *three_way_filemerge(struct index_state *istate,
34 const char *path,
35 mmfile_t *base,
36 mmfile_t *our,
37 mmfile_t *their,
38 unsigned long *size)
Linus Torvalds0c799382006-06-28 22:06:36 -070039{
Elijah Newren35f69672022-02-02 02:37:30 +000040 enum ll_merge_result merge_status;
Junio C Hamano15b4f7a2010-01-16 21:45:40 -080041 mmbuffer_t res;
Linus Torvalds0c799382006-06-28 22:06:36 -070042
Jonathan Niedere44b3852010-03-20 19:40:53 -050043 /*
44 * This function is only used by cmd_merge_tree, which
45 * does not respect the merge.conflictstyle option.
46 * There is no need to worry about a label for the
47 * common ancestor.
48 */
Jonathan Niederf01de622010-03-20 19:38:58 -050049 merge_status = ll_merge(&res, path, base, NULL,
Nguyễn Thái Ngọc Duy32eaa462018-09-21 17:57:27 +020050 our, ".our", their, ".their",
Nguyễn Thái Ngọc Duyf4a55b22018-09-21 17:57:28 +020051 istate, NULL);
Johannes Schindeline2b70082006-12-13 00:01:41 +010052 if (merge_status < 0)
53 return NULL;
Elijah Newren35f69672022-02-02 02:37:30 +000054 if (merge_status == LL_MERGE_BINARY_CONFLICT)
55 warning("Cannot merge binary files: %s (%s vs. %s)",
56 path, ".our", ".their");
Johannes Schindeline2b70082006-12-13 00:01:41 +010057
58 *size = res.size;
59 return res.ptr;
Linus Torvalds0c799382006-06-28 22:06:36 -070060}
61
Nguyễn Thái Ngọc Duyf4a55b22018-09-21 17:57:28 +020062void *merge_blobs(struct index_state *istate, const char *path,
63 struct blob *base, struct blob *our,
64 struct blob *their, unsigned long *size)
Linus Torvalds0c799382006-06-28 22:06:36 -070065{
66 void *res = NULL;
67 mmfile_t f1, f2, common;
68
69 /*
70 * Removed in either branch?
71 *
72 * NOTE! This depends on the caller having done the
73 * proper warning about removing a file that got
74 * modified in the other branch!
75 */
76 if (!our || !their) {
Nicolas Pitre21666f12007-02-26 14:55:59 -050077 enum object_type type;
Linus Torvalds0c799382006-06-28 22:06:36 -070078 if (base)
79 return NULL;
80 if (!our)
81 our = their;
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +020082 return repo_read_object_file(the_repository, &our->object.oid,
83 &type, size);
Linus Torvalds0c799382006-06-28 22:06:36 -070084 }
85
86 if (fill_mmfile_blob(&f1, our) < 0)
87 goto out_no_mmfile;
88 if (fill_mmfile_blob(&f2, their) < 0)
89 goto out_free_f1;
90
91 if (base) {
92 if (fill_mmfile_blob(&common, base) < 0)
93 goto out_free_f2_f1;
94 } else {
Jeff Kingb779b3a2016-02-23 01:06:55 -050095 common.ptr = xstrdup("");
96 common.size = 0;
Linus Torvalds0c799382006-06-28 22:06:36 -070097 }
Nguyễn Thái Ngọc Duyf4a55b22018-09-21 17:57:28 +020098 res = three_way_filemerge(istate, path, &common, &f1, &f2, size);
Linus Torvalds0c799382006-06-28 22:06:36 -070099 free_mmfile(&common);
100out_free_f2_f1:
101 free_mmfile(&f2);
102out_free_f1:
103 free_mmfile(&f1);
104out_no_mmfile:
105 return res;
106}