blob: db4d0d50d32d8852d1cb5173125e4173c3badb49 [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"
6
Linus Torvalds0c799382006-06-28 22:06:36 -07007static int fill_mmfile_blob(mmfile_t *f, struct blob *obj)
8{
9 void *buf;
10 unsigned long size;
Nicolas Pitre21666f12007-02-26 14:55:59 -050011 enum object_type type;
Linus Torvalds0c799382006-06-28 22:06:36 -070012
Nicolas Pitre21666f12007-02-26 14:55:59 -050013 buf = read_sha1_file(obj->object.sha1, &type, &size);
Linus Torvalds0c799382006-06-28 22:06:36 -070014 if (!buf)
15 return -1;
Nicolas Pitre21666f12007-02-26 14:55:59 -050016 if (type != OBJ_BLOB)
Linus Torvalds0c799382006-06-28 22:06:36 -070017 return -1;
18 f->ptr = buf;
19 f->size = size;
20 return 0;
21}
22
23static void free_mmfile(mmfile_t *f)
24{
25 free(f->ptr);
26}
27
Junio C Hamano15b4f7a2010-01-16 21:45:40 -080028static 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 -070029{
Johannes Schindeline2b70082006-12-13 00:01:41 +010030 int merge_status;
Junio C Hamano15b4f7a2010-01-16 21:45:40 -080031 mmbuffer_t res;
Linus Torvalds0c799382006-06-28 22:06:36 -070032
Jonathan Niedere44b3852010-03-20 19:40:53 -050033 /*
34 * This function is only used by cmd_merge_tree, which
35 * does not respect the merge.conflictstyle option.
36 * There is no need to worry about a label for the
37 * common ancestor.
38 */
Jonathan Niederf01de622010-03-20 19:38:58 -050039 merge_status = ll_merge(&res, path, base, NULL,
Junio C Hamano15b4f7a2010-01-16 21:45:40 -080040 our, ".our", their, ".their", 0);
Johannes Schindeline2b70082006-12-13 00:01:41 +010041 if (merge_status < 0)
42 return NULL;
43
44 *size = res.size;
45 return res.ptr;
Linus Torvalds0c799382006-06-28 22:06:36 -070046}
47
48static int common_outf(void *priv_, mmbuffer_t *mb, int nbuf)
49{
50 int i;
51 mmfile_t *dst = priv_;
52
53 for (i = 0; i < nbuf; i++) {
54 memcpy(dst->ptr + dst->size, mb[i].ptr, mb[i].size);
55 dst->size += mb[i].size;
56 }
57 return 0;
58}
59
60static int generate_common_file(mmfile_t *res, mmfile_t *f1, mmfile_t *f2)
61{
62 unsigned long size = f1->size < f2->size ? f1->size : f2->size;
63 void *ptr = xmalloc(size);
64 xpparam_t xpp;
65 xdemitconf_t xecfg;
66 xdemitcb_t ecb;
67
Brian Downing9ccd0a82008-10-25 15:30:37 +020068 memset(&xpp, 0, sizeof(xpp));
René Scharfe582aa002010-05-02 15:04:41 +020069 xpp.flags = 0;
Johannes Schindelin30b25012007-07-04 19:05:46 +010070 memset(&xecfg, 0, sizeof(xecfg));
Linus Torvalds0c799382006-06-28 22:06:36 -070071 xecfg.ctxlen = 3;
72 xecfg.flags = XDL_EMIT_COMMON;
73 ecb.outf = common_outf;
74
75 res->ptr = ptr;
76 res->size = 0;
77
78 ecb.priv = res;
Junio C Hamanoc279d7e2007-12-13 13:25:07 -080079 return xdi_diff(f1, f2, &xpp, &xecfg, &ecb);
Linus Torvalds0c799382006-06-28 22:06:36 -070080}
81
Junio C Hamano15b4f7a2010-01-16 21:45:40 -080082void *merge_file(const char *path, struct blob *base, struct blob *our, struct blob *their, unsigned long *size)
Linus Torvalds0c799382006-06-28 22:06:36 -070083{
84 void *res = NULL;
85 mmfile_t f1, f2, common;
86
87 /*
88 * Removed in either branch?
89 *
90 * NOTE! This depends on the caller having done the
91 * proper warning about removing a file that got
92 * modified in the other branch!
93 */
94 if (!our || !their) {
Nicolas Pitre21666f12007-02-26 14:55:59 -050095 enum object_type type;
Linus Torvalds0c799382006-06-28 22:06:36 -070096 if (base)
97 return NULL;
98 if (!our)
99 our = their;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500100 return read_sha1_file(our->object.sha1, &type, size);
Linus Torvalds0c799382006-06-28 22:06:36 -0700101 }
102
103 if (fill_mmfile_blob(&f1, our) < 0)
104 goto out_no_mmfile;
105 if (fill_mmfile_blob(&f2, their) < 0)
106 goto out_free_f1;
107
108 if (base) {
109 if (fill_mmfile_blob(&common, base) < 0)
110 goto out_free_f2_f1;
111 } else {
112 if (generate_common_file(&common, &f1, &f2) < 0)
113 goto out_free_f2_f1;
114 }
Junio C Hamano15b4f7a2010-01-16 21:45:40 -0800115 res = three_way_filemerge(path, &common, &f1, &f2, size);
Linus Torvalds0c799382006-06-28 22:06:36 -0700116 free_mmfile(&common);
117out_free_f2_f1:
118 free_mmfile(&f2);
119out_free_f1:
120 free_mmfile(&f1);
121out_no_mmfile:
122 return res;
123}