]>
Commit | Line | Data |
---|---|---|
1 | #include "cache.h" | |
2 | #include "run-command.h" | |
3 | #include "xdiff-interface.h" | |
4 | #include "ll-merge.h" | |
5 | #include "blob.h" | |
6 | #include "merge-blobs.h" | |
7 | #include "object-store.h" | |
8 | ||
9 | static int fill_mmfile_blob(mmfile_t *f, struct blob *obj) | |
10 | { | |
11 | void *buf; | |
12 | unsigned long size; | |
13 | enum object_type type; | |
14 | ||
15 | buf = read_object_file(&obj->object.oid, &type, &size); | |
16 | if (!buf) | |
17 | return -1; | |
18 | if (type != OBJ_BLOB) { | |
19 | free(buf); | |
20 | return -1; | |
21 | } | |
22 | f->ptr = buf; | |
23 | f->size = size; | |
24 | return 0; | |
25 | } | |
26 | ||
27 | static void free_mmfile(mmfile_t *f) | |
28 | { | |
29 | free(f->ptr); | |
30 | } | |
31 | ||
32 | static void *three_way_filemerge(struct index_state *istate, | |
33 | const char *path, | |
34 | mmfile_t *base, | |
35 | mmfile_t *our, | |
36 | mmfile_t *their, | |
37 | unsigned long *size) | |
38 | { | |
39 | enum ll_merge_result merge_status; | |
40 | mmbuffer_t res; | |
41 | ||
42 | /* | |
43 | * This function is only used by cmd_merge_tree, which | |
44 | * does not respect the merge.conflictstyle option. | |
45 | * There is no need to worry about a label for the | |
46 | * common ancestor. | |
47 | */ | |
48 | merge_status = ll_merge(&res, path, base, NULL, | |
49 | our, ".our", their, ".their", | |
50 | istate, NULL); | |
51 | if (merge_status < 0) | |
52 | return NULL; | |
53 | if (merge_status == LL_MERGE_BINARY_CONFLICT) | |
54 | warning("Cannot merge binary files: %s (%s vs. %s)", | |
55 | path, ".our", ".their"); | |
56 | ||
57 | *size = res.size; | |
58 | return res.ptr; | |
59 | } | |
60 | ||
61 | void *merge_blobs(struct index_state *istate, const char *path, | |
62 | struct blob *base, struct blob *our, | |
63 | struct blob *their, unsigned long *size) | |
64 | { | |
65 | void *res = NULL; | |
66 | mmfile_t f1, f2, common; | |
67 | ||
68 | /* | |
69 | * Removed in either branch? | |
70 | * | |
71 | * NOTE! This depends on the caller having done the | |
72 | * proper warning about removing a file that got | |
73 | * modified in the other branch! | |
74 | */ | |
75 | if (!our || !their) { | |
76 | enum object_type type; | |
77 | if (base) | |
78 | return NULL; | |
79 | if (!our) | |
80 | our = their; | |
81 | return read_object_file(&our->object.oid, &type, size); | |
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 { | |
93 | common.ptr = xstrdup(""); | |
94 | common.size = 0; | |
95 | } | |
96 | res = three_way_filemerge(istate, path, &common, &f1, &f2, size); | |
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 | } |