blob: 57211bccb7d0a5aad8538906bcb2f4ad2900f008 [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
Nicolas Pitre21666f12007-02-26 14:55:59 -050014 buf = read_sha1_file(obj->object.sha1, &type, &size);
Linus Torvalds0c799382006-06-28 22:06:36 -070015 if (!buf)
16 return -1;
Nicolas Pitre21666f12007-02-26 14:55:59 -050017 if (type != OBJ_BLOB)
Linus Torvalds0c799382006-06-28 22:06:36 -070018 return -1;
19 f->ptr = buf;
20 f->size = size;
21 return 0;
22}
23
24static void free_mmfile(mmfile_t *f)
25{
26 free(f->ptr);
27}
28
Junio C Hamano15b4f7a2010-01-16 21:45:40 -080029static 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 -070030{
Johannes Schindeline2b70082006-12-13 00:01:41 +010031 int merge_status;
Junio C Hamano15b4f7a2010-01-16 21:45:40 -080032 mmbuffer_t res;
Linus Torvalds0c799382006-06-28 22:06:36 -070033
Jonathan Niedere44b3852010-03-20 19:40:53 -050034 /*
35 * This function is only used by cmd_merge_tree, which
36 * does not respect the merge.conflictstyle option.
37 * There is no need to worry about a label for the
38 * common ancestor.
39 */
Jonathan Niederf01de622010-03-20 19:38:58 -050040 merge_status = ll_merge(&res, path, base, NULL,
Jonathan Nieder712516b2010-08-26 00:49:53 -050041 our, ".our", their, ".their", NULL);
Johannes Schindeline2b70082006-12-13 00:01:41 +010042 if (merge_status < 0)
43 return NULL;
44
45 *size = res.size;
46 return res.ptr;
Linus Torvalds0c799382006-06-28 22:06:36 -070047}
48
49static int common_outf(void *priv_, mmbuffer_t *mb, int nbuf)
50{
51 int i;
52 mmfile_t *dst = priv_;
53
54 for (i = 0; i < nbuf; i++) {
55 memcpy(dst->ptr + dst->size, mb[i].ptr, mb[i].size);
56 dst->size += mb[i].size;
57 }
58 return 0;
59}
60
61static int generate_common_file(mmfile_t *res, mmfile_t *f1, mmfile_t *f2)
62{
63 unsigned long size = f1->size < f2->size ? f1->size : f2->size;
64 void *ptr = xmalloc(size);
65 xpparam_t xpp;
66 xdemitconf_t xecfg;
67 xdemitcb_t ecb;
68
Brian Downing9ccd0a82008-10-25 15:30:37 +020069 memset(&xpp, 0, sizeof(xpp));
René Scharfe582aa002010-05-02 15:04:41 +020070 xpp.flags = 0;
Johannes Schindelin30b25012007-07-04 19:05:46 +010071 memset(&xecfg, 0, sizeof(xecfg));
Linus Torvalds0c799382006-06-28 22:06:36 -070072 xecfg.ctxlen = 3;
73 xecfg.flags = XDL_EMIT_COMMON;
74 ecb.outf = common_outf;
75
76 res->ptr = ptr;
77 res->size = 0;
78
79 ecb.priv = res;
Junio C Hamanoc279d7e2007-12-13 13:25:07 -080080 return xdi_diff(f1, f2, &xpp, &xecfg, &ecb);
Linus Torvalds0c799382006-06-28 22:06:36 -070081}
82
Junio C Hamanofa2364e2012-12-06 15:08:01 -080083void *merge_blobs(const char *path, struct blob *base, struct blob *our, struct blob *their, unsigned long *size)
Linus Torvalds0c799382006-06-28 22:06:36 -070084{
85 void *res = NULL;
86 mmfile_t f1, f2, common;
87
88 /*
89 * Removed in either branch?
90 *
91 * NOTE! This depends on the caller having done the
92 * proper warning about removing a file that got
93 * modified in the other branch!
94 */
95 if (!our || !their) {
Nicolas Pitre21666f12007-02-26 14:55:59 -050096 enum object_type type;
Linus Torvalds0c799382006-06-28 22:06:36 -070097 if (base)
98 return NULL;
99 if (!our)
100 our = their;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500101 return read_sha1_file(our->object.sha1, &type, size);
Linus Torvalds0c799382006-06-28 22:06:36 -0700102 }
103
104 if (fill_mmfile_blob(&f1, our) < 0)
105 goto out_no_mmfile;
106 if (fill_mmfile_blob(&f2, their) < 0)
107 goto out_free_f1;
108
109 if (base) {
110 if (fill_mmfile_blob(&common, base) < 0)
111 goto out_free_f2_f1;
112 } else {
113 if (generate_common_file(&common, &f1, &f2) < 0)
114 goto out_free_f2_f1;
115 }
Junio C Hamano15b4f7a2010-01-16 21:45:40 -0800116 res = three_way_filemerge(path, &common, &f1, &f2, size);
Linus Torvalds0c799382006-06-28 22:06:36 -0700117 free_mmfile(&common);
118out_free_f2_f1:
119 free_mmfile(&f2);
120out_free_f1:
121 free_mmfile(&f1);
122out_no_mmfile:
123 return res;
124}