blob: 9b6eac22e4256d8f2bf82961b6e4f320d89fdeba [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"
Linus Torvalds0c799382006-06-28 22:06:36 -07007
Linus Torvalds0c799382006-06-28 22:06:36 -07008static int fill_mmfile_blob(mmfile_t *f, struct blob *obj)
9{
10 void *buf;
11 unsigned long size;
Nicolas Pitre21666f12007-02-26 14:55:59 -050012 enum object_type type;
Linus Torvalds0c799382006-06-28 22:06:36 -070013
brian m. carlsoned1c9972015-11-10 02:22:29 +000014 buf = read_sha1_file(obj->object.oid.hash, &type, &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
Junio C Hamano15b4f7a2010-01-16 21:45:40 -080031static 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 -070032{
Johannes Schindeline2b70082006-12-13 00:01:41 +010033 int merge_status;
Junio C Hamano15b4f7a2010-01-16 21:45:40 -080034 mmbuffer_t res;
Linus Torvalds0c799382006-06-28 22:06:36 -070035
Jonathan Niedere44b3852010-03-20 19:40:53 -050036 /*
37 * This function is only used by cmd_merge_tree, which
38 * does not respect the merge.conflictstyle option.
39 * There is no need to worry about a label for the
40 * common ancestor.
41 */
Jonathan Niederf01de622010-03-20 19:38:58 -050042 merge_status = ll_merge(&res, path, base, NULL,
Jonathan Nieder712516b2010-08-26 00:49:53 -050043 our, ".our", their, ".their", NULL);
Johannes Schindeline2b70082006-12-13 00:01:41 +010044 if (merge_status < 0)
45 return NULL;
46
47 *size = res.size;
48 return res.ptr;
Linus Torvalds0c799382006-06-28 22:06:36 -070049}
50
Junio C Hamanofa2364e2012-12-06 15:08:01 -080051void *merge_blobs(const char *path, struct blob *base, struct blob *our, struct blob *their, unsigned long *size)
Linus Torvalds0c799382006-06-28 22:06:36 -070052{
53 void *res = NULL;
54 mmfile_t f1, f2, common;
55
56 /*
57 * Removed in either branch?
58 *
59 * NOTE! This depends on the caller having done the
60 * proper warning about removing a file that got
61 * modified in the other branch!
62 */
63 if (!our || !their) {
Nicolas Pitre21666f12007-02-26 14:55:59 -050064 enum object_type type;
Linus Torvalds0c799382006-06-28 22:06:36 -070065 if (base)
66 return NULL;
67 if (!our)
68 our = their;
brian m. carlsoned1c9972015-11-10 02:22:29 +000069 return read_sha1_file(our->object.oid.hash, &type, size);
Linus Torvalds0c799382006-06-28 22:06:36 -070070 }
71
72 if (fill_mmfile_blob(&f1, our) < 0)
73 goto out_no_mmfile;
74 if (fill_mmfile_blob(&f2, their) < 0)
75 goto out_free_f1;
76
77 if (base) {
78 if (fill_mmfile_blob(&common, base) < 0)
79 goto out_free_f2_f1;
80 } else {
Jeff Kingb779b3a2016-02-23 01:06:55 -050081 common.ptr = xstrdup("");
82 common.size = 0;
Linus Torvalds0c799382006-06-28 22:06:36 -070083 }
Junio C Hamano15b4f7a2010-01-16 21:45:40 -080084 res = three_way_filemerge(path, &common, &f1, &f2, size);
Linus Torvalds0c799382006-06-28 22:06:36 -070085 free_mmfile(&common);
86out_free_f2_f1:
87 free_mmfile(&f2);
88out_free_f1:
89 free_mmfile(&f1);
90out_no_mmfile:
91 return res;
92}