Skip to content

Commit 0c79938

Browse files
Linus TorvaldsJunio C Hamano
authored andcommitted
Improved three-way blob merging code
This fleshes out the code that generates a three-way merge of a set of blobs. It still actually does the three-way merge using an external executable (ie just calling "merge"), but the interfaces have been cleaned up a lot and are now fully based on the 'mmfile_t' interface, so if libxdiff were to ever grow a compatible three-way-merge, it could probably be directly plugged in. It also uses the previous XDL_EMIT_COMMON functionality extension to libxdiff to generate a made-up base file for the merge for the case where no base file previously existed. This should be equivalent to what we currently do in git-merge-one-file.sh: diff -u -La/$orig -Lb/$orig $orig $src2 | git-apply --no-add except it should be much simpler and can be done using the direct libxdiff interfaces. Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8378807 commit 0c79938

File tree

3 files changed

+240
-1
lines changed

3 files changed

+240
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ LIB_OBJS = \
217217
server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
218218
tag.o tree.o usage.o config.o environment.o ctype.o copy.o \
219219
fetch-clone.o revision.o pager.o tree-walk.o xdiff-interface.o \
220-
alloc.o $(DIFF_OBJS)
220+
alloc.o merge-file.o $(DIFF_OBJS)
221221

222222
BUILTIN_OBJS = \
223223
builtin-log.o builtin-help.o builtin-count.o builtin-diff.o builtin-push.o \

merge-file.c

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#include "cache.h"
2+
#include "run-command.h"
3+
#include "xdiff-interface.h"
4+
#include "blob.h"
5+
6+
static void rm_temp_file(const char *filename)
7+
{
8+
unlink(filename);
9+
free((void *)filename);
10+
}
11+
12+
static const char *write_temp_file(mmfile_t *f)
13+
{
14+
int fd;
15+
const char *tmp = getenv("TMPDIR");
16+
char *filename;
17+
18+
if (!tmp)
19+
tmp = "/tmp";
20+
filename = mkpath("%s/%s", tmp, "git-tmp-XXXXXX");
21+
fd = mkstemp(filename);
22+
if (fd < 0)
23+
return NULL;
24+
filename = strdup(filename);
25+
if (f->size != xwrite(fd, f->ptr, f->size)) {
26+
rm_temp_file(filename);
27+
return NULL;
28+
}
29+
close(fd);
30+
return filename;
31+
}
32+
33+
static void *read_temp_file(const char *filename, unsigned long *size)
34+
{
35+
struct stat st;
36+
char *buf = NULL;
37+
int fd = open(filename, O_RDONLY);
38+
if (fd < 0)
39+
return NULL;
40+
if (!fstat(fd, &st)) {
41+
*size = st.st_size;
42+
buf = xmalloc(st.st_size);
43+
if (st.st_size != xread(fd, buf, st.st_size)) {
44+
free(buf);
45+
buf = NULL;
46+
}
47+
}
48+
close(fd);
49+
return buf;
50+
}
51+
52+
static int fill_mmfile_blob(mmfile_t *f, struct blob *obj)
53+
{
54+
void *buf;
55+
unsigned long size;
56+
char type[20];
57+
58+
buf = read_sha1_file(obj->object.sha1, type, &size);
59+
if (!buf)
60+
return -1;
61+
if (strcmp(type, blob_type))
62+
return -1;
63+
f->ptr = buf;
64+
f->size = size;
65+
return 0;
66+
}
67+
68+
static void free_mmfile(mmfile_t *f)
69+
{
70+
free(f->ptr);
71+
}
72+
73+
static void *three_way_filemerge(mmfile_t *base, mmfile_t *our, mmfile_t *their, unsigned long *size)
74+
{
75+
void *res;
76+
const char *t1, *t2, *t3;
77+
78+
t1 = write_temp_file(base);
79+
t2 = write_temp_file(our);
80+
t3 = write_temp_file(their);
81+
res = NULL;
82+
if (t1 && t2 && t3) {
83+
int code = run_command("merge", t2, t1, t3, NULL);
84+
if (!code || code == -1)
85+
res = read_temp_file(t2, size);
86+
}
87+
rm_temp_file(t1);
88+
rm_temp_file(t2);
89+
rm_temp_file(t3);
90+
return res;
91+
}
92+
93+
static int common_outf(void *priv_, mmbuffer_t *mb, int nbuf)
94+
{
95+
int i;
96+
mmfile_t *dst = priv_;
97+
98+
for (i = 0; i < nbuf; i++) {
99+
memcpy(dst->ptr + dst->size, mb[i].ptr, mb[i].size);
100+
dst->size += mb[i].size;
101+
}
102+
return 0;
103+
}
104+
105+
static int generate_common_file(mmfile_t *res, mmfile_t *f1, mmfile_t *f2)
106+
{
107+
unsigned long size = f1->size < f2->size ? f1->size : f2->size;
108+
void *ptr = xmalloc(size);
109+
xpparam_t xpp;
110+
xdemitconf_t xecfg;
111+
xdemitcb_t ecb;
112+
113+
xpp.flags = XDF_NEED_MINIMAL;
114+
xecfg.ctxlen = 3;
115+
xecfg.flags = XDL_EMIT_COMMON;
116+
ecb.outf = common_outf;
117+
118+
res->ptr = ptr;
119+
res->size = 0;
120+
121+
ecb.priv = res;
122+
return xdl_diff(f1, f2, &xpp, &xecfg, &ecb);
123+
}
124+
125+
void *merge_file(struct blob *base, struct blob *our, struct blob *their, unsigned long *size)
126+
{
127+
void *res = NULL;
128+
mmfile_t f1, f2, common;
129+
130+
/*
131+
* Removed in either branch?
132+
*
133+
* NOTE! This depends on the caller having done the
134+
* proper warning about removing a file that got
135+
* modified in the other branch!
136+
*/
137+
if (!our || !their) {
138+
char type[20];
139+
if (base)
140+
return NULL;
141+
if (!our)
142+
our = their;
143+
return read_sha1_file(our->object.sha1, type, size);
144+
}
145+
146+
if (fill_mmfile_blob(&f1, our) < 0)
147+
goto out_no_mmfile;
148+
if (fill_mmfile_blob(&f2, their) < 0)
149+
goto out_free_f1;
150+
151+
if (base) {
152+
if (fill_mmfile_blob(&common, base) < 0)
153+
goto out_free_f2_f1;
154+
} else {
155+
if (generate_common_file(&common, &f1, &f2) < 0)
156+
goto out_free_f2_f1;
157+
}
158+
res = three_way_filemerge(&common, &f1, &f2, size);
159+
free_mmfile(&common);
160+
out_free_f2_f1:
161+
free_mmfile(&f2);
162+
out_free_f1:
163+
free_mmfile(&f1);
164+
out_no_mmfile:
165+
return res;
166+
}

merge-tree.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cache.h"
22
#include "tree-walk.h"
3+
#include "xdiff-interface.h"
34
#include "blob.h"
45

56
static const char merge_tree_usage[] = "git-merge-tree <base-tree> <branch1> <branch2>";
@@ -52,6 +53,77 @@ static const char *explanation(struct merge_list *entry)
5253
return "removed in remote";
5354
}
5455

56+
extern void *merge_file(struct blob *, struct blob *, struct blob *, unsigned long *);
57+
58+
static void *result(struct merge_list *entry, unsigned long *size)
59+
{
60+
char type[20];
61+
struct blob *base, *our, *their;
62+
63+
if (!entry->stage)
64+
return read_sha1_file(entry->blob->object.sha1, type, size);
65+
base = NULL;
66+
if (entry->stage == 1) {
67+
base = entry->blob;
68+
entry = entry->link;
69+
}
70+
our = NULL;
71+
if (entry && entry->stage == 2) {
72+
our = entry->blob;
73+
entry = entry->link;
74+
}
75+
their = NULL;
76+
if (entry)
77+
their = entry->blob;
78+
return merge_file(base, our, their, size);
79+
}
80+
81+
static void *origin(struct merge_list *entry, unsigned long *size)
82+
{
83+
char type[20];
84+
while (entry) {
85+
if (entry->stage == 2)
86+
return read_sha1_file(entry->blob->object.sha1, type, size);
87+
entry = entry->link;
88+
}
89+
return NULL;
90+
}
91+
92+
static int show_outf(void *priv_, mmbuffer_t *mb, int nbuf)
93+
{
94+
int i;
95+
for (i = 0; i < nbuf; i++)
96+
printf("%.*s", (int) mb[i].size, mb[i].ptr);
97+
return 0;
98+
}
99+
100+
static void show_diff(struct merge_list *entry)
101+
{
102+
unsigned long size;
103+
mmfile_t src, dst;
104+
xpparam_t xpp;
105+
xdemitconf_t xecfg;
106+
xdemitcb_t ecb;
107+
108+
xpp.flags = XDF_NEED_MINIMAL;
109+
xecfg.ctxlen = 3;
110+
xecfg.flags = 0;
111+
ecb.outf = show_outf;
112+
ecb.priv = NULL;
113+
114+
src.ptr = origin(entry, &size);
115+
if (!src.ptr)
116+
size = 0;
117+
src.size = size;
118+
dst.ptr = result(entry, &size);
119+
if (!dst.ptr)
120+
size = 0;
121+
dst.size = size;
122+
xdl_diff(&src, &dst, &xpp, &xecfg, &ecb);
123+
free(src.ptr);
124+
free(dst.ptr);
125+
}
126+
55127
static void show_result_list(struct merge_list *entry)
56128
{
57129
printf("%s\n", explanation(entry));
@@ -70,6 +142,7 @@ static void show_result(void)
70142
walk = merge_result;
71143
while (walk) {
72144
show_result_list(walk);
145+
show_diff(walk);
73146
walk = walk->next;
74147
}
75148
}

0 commit comments

Comments
 (0)