]>
Commit | Line | Data |
---|---|---|
1c028400 | 1 | #include "git-compat-util.h" |
67238999 | 2 | #include "merge-ll.h" |
0c799383 | 3 | #include "blob.h" |
fa2364ec | 4 | #include "merge-blobs.h" |
a034e910 | 5 | #include "object-store-ll.h" |
0c799383 | 6 | |
0c799383 LT |
7 | static int fill_mmfile_blob(mmfile_t *f, struct blob *obj) |
8 | { | |
9 | void *buf; | |
10 | unsigned long size; | |
21666f1a | 11 | enum object_type type; |
0c799383 | 12 | |
bc726bd0 ÆAB |
13 | buf = repo_read_object_file(the_repository, &obj->object.oid, &type, |
14 | &size); | |
0c799383 LT |
15 | if (!buf) |
16 | return -1; | |
d687839c SB |
17 | if (type != OBJ_BLOB) { |
18 | free(buf); | |
0c799383 | 19 | return -1; |
d687839c | 20 | } |
0c799383 LT |
21 | f->ptr = buf; |
22 | f->size = size; | |
23 | return 0; | |
24 | } | |
25 | ||
26 | static void free_mmfile(mmfile_t *f) | |
27 | { | |
28 | free(f->ptr); | |
29 | } | |
30 | ||
f4a55b27 NTND |
31 | static void *three_way_filemerge(struct index_state *istate, |
32 | const char *path, | |
33 | mmfile_t *base, | |
34 | mmfile_t *our, | |
35 | mmfile_t *their, | |
36 | unsigned long *size) | |
0c799383 | 37 | { |
35f69671 | 38 | enum ll_merge_result merge_status; |
15b4f7a6 | 39 | mmbuffer_t res; |
0c799383 | 40 | |
e44b3851 JN |
41 | /* |
42 | * This function is only used by cmd_merge_tree, which | |
43 | * does not respect the merge.conflictstyle option. | |
44 | * There is no need to worry about a label for the | |
45 | * common ancestor. | |
46 | */ | |
f01de62e | 47 | merge_status = ll_merge(&res, path, base, NULL, |
32eaa468 | 48 | our, ".our", their, ".their", |
f4a55b27 | 49 | istate, NULL); |
e2b70087 JS |
50 | if (merge_status < 0) |
51 | return NULL; | |
35f69671 EN |
52 | if (merge_status == LL_MERGE_BINARY_CONFLICT) |
53 | warning("Cannot merge binary files: %s (%s vs. %s)", | |
54 | path, ".our", ".their"); | |
e2b70087 JS |
55 | |
56 | *size = res.size; | |
57 | return res.ptr; | |
0c799383 LT |
58 | } |
59 | ||
f4a55b27 NTND |
60 | void *merge_blobs(struct index_state *istate, const char *path, |
61 | struct blob *base, struct blob *our, | |
62 | struct blob *their, unsigned long *size) | |
0c799383 LT |
63 | { |
64 | void *res = NULL; | |
65 | mmfile_t f1, f2, common; | |
66 | ||
67 | /* | |
68 | * Removed in either branch? | |
69 | * | |
70 | * NOTE! This depends on the caller having done the | |
71 | * proper warning about removing a file that got | |
72 | * modified in the other branch! | |
73 | */ | |
74 | if (!our || !their) { | |
21666f1a | 75 | enum object_type type; |
0c799383 LT |
76 | if (base) |
77 | return NULL; | |
78 | if (!our) | |
79 | our = their; | |
bc726bd0 ÆAB |
80 | return repo_read_object_file(the_repository, &our->object.oid, |
81 | &type, size); | |
0c799383 LT |
82 | } |
83 | ||
84 | if (fill_mmfile_blob(&f1, our) < 0) | |
85 | goto out_no_mmfile; | |
86 | if (fill_mmfile_blob(&f2, their) < 0) | |
87 | goto out_free_f1; | |
88 | ||
89 | if (base) { | |
90 | if (fill_mmfile_blob(&common, base) < 0) | |
91 | goto out_free_f2_f1; | |
92 | } else { | |
b779b3a1 JK |
93 | common.ptr = xstrdup(""); |
94 | common.size = 0; | |
0c799383 | 95 | } |
f4a55b27 | 96 | res = three_way_filemerge(istate, path, &common, &f1, &f2, size); |
0c799383 LT |
97 | free_mmfile(&common); |
98 | out_free_f2_f1: | |
99 | free_mmfile(&f2); | |
100 | out_free_f1: | |
101 | free_mmfile(&f1); | |
102 | out_no_mmfile: | |
103 | return res; | |
104 | } |