blob: fabb8c19ce983962b45c415f66c11236fa906bfe [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
Junio C Hamano15b4f7a2010-01-16 21:45:40 -080032static void *three_way_filemerge(const char *path, mmfile_t *base, mmfile_t *our, mmfile_t *their, unsigned long *size)
Linus Torvalds0c799382006-06-28 22:06:36 -070033{
Johannes Schindeline2b70082006-12-13 00:01:41 +010034 int merge_status;
Junio C Hamano15b4f7a2010-01-16 21:45:40 -080035 mmbuffer_t res;
Linus Torvalds0c799382006-06-28 22:06:36 -070036
Jonathan Niedere44b3852010-03-20 19:40:53 -050037 /*
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 */
Jonathan Niederf01de622010-03-20 19:38:58 -050043 merge_status = ll_merge(&res, path, base, NULL,
Jonathan Nieder712516b2010-08-26 00:49:53 -050044 our, ".our", their, ".their", NULL);
Johannes Schindeline2b70082006-12-13 00:01:41 +010045 if (merge_status < 0)
46 return NULL;
47
48 *size = res.size;
49 return res.ptr;
Linus Torvalds0c799382006-06-28 22:06:36 -070050}
51
Junio C Hamanofa2364e2012-12-06 15:08:01 -080052void *merge_blobs(const char *path, struct blob *base, struct blob *our, struct blob *their, unsigned long *size)
Linus Torvalds0c799382006-06-28 22:06:36 -070053{
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) {
Nicolas Pitre21666f12007-02-26 14:55:59 -050065 enum object_type type;
Linus Torvalds0c799382006-06-28 22:06:36 -070066 if (base)
67 return NULL;
68 if (!our)
69 our = their;
brian m. carlsonb4f5aca2018-03-12 02:27:53 +000070 return read_object_file(&our->object.oid, &type, size);
Linus Torvalds0c799382006-06-28 22:06:36 -070071 }
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 {
Jeff Kingb779b3a2016-02-23 01:06:55 -050082 common.ptr = xstrdup("");
83 common.size = 0;
Linus Torvalds0c799382006-06-28 22:06:36 -070084 }
Junio C Hamano15b4f7a2010-01-16 21:45:40 -080085 res = three_way_filemerge(path, &common, &f1, &f2, size);
Linus Torvalds0c799382006-06-28 22:06:36 -070086 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}