Commit | Line | Data |
---|---|---|
c2e86add | 1 | #include "builtin.h" |
1b0c7174 | 2 | #include "tree-walk.h" |
0c799383 | 3 | #include "xdiff-interface.h" |
83788070 | 4 | #include "blob.h" |
2fb3f6db | 5 | #include "exec_cmd.h" |
fa2364ec | 6 | #include "merge-blobs.h" |
492e0759 | 7 | |
34263de0 | 8 | static const char merge_tree_usage[] = "git merge-tree <base-tree> <branch1> <branch2>"; |
492e0759 | 9 | |
83788070 LT |
10 | struct merge_list { |
11 | struct merge_list *next; | |
12 | struct merge_list *link; /* other stages for this object */ | |
13 | ||
b13112fa | 14 | unsigned int stage : 2; |
83788070 LT |
15 | unsigned int mode; |
16 | const char *path; | |
17 | struct blob *blob; | |
18 | }; | |
19 | ||
20 | static struct merge_list *merge_result, **merge_result_end = &merge_result; | |
21 | ||
22 | static void add_merge_entry(struct merge_list *entry) | |
23 | { | |
24 | *merge_result_end = entry; | |
25 | merge_result_end = &entry->next; | |
26 | } | |
27 | ||
492e0759 LT |
28 | static void merge_trees(struct tree_desc t[3], const char *base); |
29 | ||
83788070 LT |
30 | static const char *explanation(struct merge_list *entry) |
31 | { | |
32 | switch (entry->stage) { | |
33 | case 0: | |
34 | return "merged"; | |
35 | case 3: | |
36 | return "added in remote"; | |
37 | case 2: | |
38 | if (entry->link) | |
39 | return "added in both"; | |
40 | return "added in local"; | |
41 | } | |
42 | ||
43 | /* Existed in base */ | |
44 | entry = entry->link; | |
45 | if (!entry) | |
46 | return "removed in both"; | |
47 | ||
48 | if (entry->link) | |
49 | return "changed in both"; | |
50 | ||
51 | if (entry->stage == 3) | |
52 | return "removed in local"; | |
53 | return "removed in remote"; | |
54 | } | |
55 | ||
0c799383 LT |
56 | static void *result(struct merge_list *entry, unsigned long *size) |
57 | { | |
21666f1a | 58 | enum object_type type; |
0c799383 | 59 | struct blob *base, *our, *their; |
21baa6e0 | 60 | const char *path = entry->path; |
0c799383 LT |
61 | |
62 | if (!entry->stage) | |
21666f1a | 63 | return read_sha1_file(entry->blob->object.sha1, &type, size); |
0c799383 LT |
64 | base = NULL; |
65 | if (entry->stage == 1) { | |
66 | base = entry->blob; | |
67 | entry = entry->link; | |
68 | } | |
69 | our = NULL; | |
70 | if (entry && entry->stage == 2) { | |
71 | our = entry->blob; | |
72 | entry = entry->link; | |
73 | } | |
74 | their = NULL; | |
75 | if (entry) | |
76 | their = entry->blob; | |
fa2364ec | 77 | return merge_blobs(path, base, our, their, size); |
0c799383 LT |
78 | } |
79 | ||
80 | static void *origin(struct merge_list *entry, unsigned long *size) | |
81 | { | |
21666f1a | 82 | enum object_type type; |
0c799383 LT |
83 | while (entry) { |
84 | if (entry->stage == 2) | |
21666f1a | 85 | return read_sha1_file(entry->blob->object.sha1, &type, size); |
0c799383 LT |
86 | entry = entry->link; |
87 | } | |
88 | return NULL; | |
89 | } | |
90 | ||
91 | static int show_outf(void *priv_, mmbuffer_t *mb, int nbuf) | |
92 | { | |
93 | int i; | |
94 | for (i = 0; i < nbuf; i++) | |
95 | printf("%.*s", (int) mb[i].size, mb[i].ptr); | |
96 | return 0; | |
97 | } | |
98 | ||
99 | static void show_diff(struct merge_list *entry) | |
100 | { | |
101 | unsigned long size; | |
102 | mmfile_t src, dst; | |
103 | xpparam_t xpp; | |
104 | xdemitconf_t xecfg; | |
105 | xdemitcb_t ecb; | |
106 | ||
582aa00b | 107 | xpp.flags = 0; |
30b25010 | 108 | memset(&xecfg, 0, sizeof(xecfg)); |
0c799383 | 109 | xecfg.ctxlen = 3; |
0c799383 LT |
110 | ecb.outf = show_outf; |
111 | ecb.priv = NULL; | |
112 | ||
113 | src.ptr = origin(entry, &size); | |
114 | if (!src.ptr) | |
115 | size = 0; | |
116 | src.size = size; | |
117 | dst.ptr = result(entry, &size); | |
118 | if (!dst.ptr) | |
119 | size = 0; | |
120 | dst.size = size; | |
c279d7e9 | 121 | xdi_diff(&src, &dst, &xpp, &xecfg, &ecb); |
0c799383 LT |
122 | free(src.ptr); |
123 | free(dst.ptr); | |
124 | } | |
125 | ||
83788070 LT |
126 | static void show_result_list(struct merge_list *entry) |
127 | { | |
128 | printf("%s\n", explanation(entry)); | |
129 | do { | |
130 | struct merge_list *link = entry->link; | |
131 | static const char *desc[4] = { "result", "base", "our", "their" }; | |
132 | printf(" %-6s %o %s %s\n", desc[entry->stage], entry->mode, sha1_to_hex(entry->blob->object.sha1), entry->path); | |
133 | entry = link; | |
134 | } while (entry); | |
135 | } | |
136 | ||
137 | static void show_result(void) | |
138 | { | |
139 | struct merge_list *walk; | |
140 | ||
141 | walk = merge_result; | |
142 | while (walk) { | |
143 | show_result_list(walk); | |
0c799383 | 144 | show_diff(walk); |
83788070 LT |
145 | walk = walk->next; |
146 | } | |
147 | } | |
148 | ||
492e0759 LT |
149 | /* An empty entry never compares same, not even to another empty entry */ |
150 | static int same_entry(struct name_entry *a, struct name_entry *b) | |
151 | { | |
152 | return a->sha1 && | |
153 | b->sha1 && | |
a89fccd2 | 154 | !hashcmp(a->sha1, b->sha1) && |
492e0759 LT |
155 | a->mode == b->mode; |
156 | } | |
157 | ||
83788070 | 158 | static struct merge_list *create_entry(unsigned stage, unsigned mode, const unsigned char *sha1, const char *path) |
492e0759 | 159 | { |
19d4b416 | 160 | struct merge_list *res = xcalloc(1, sizeof(*res)); |
83788070 | 161 | |
83788070 LT |
162 | res->stage = stage; |
163 | res->path = path; | |
164 | res->mode = mode; | |
165 | res->blob = lookup_blob(sha1); | |
166 | return res; | |
01df5297 LT |
167 | } |
168 | ||
40d934df LT |
169 | static char *traverse_path(const struct traverse_info *info, const struct name_entry *n) |
170 | { | |
171 | char *path = xmalloc(traverse_path_len(info, n) + 1); | |
172 | return make_traverse_path(path, info, n); | |
173 | } | |
174 | ||
175 | static void resolve(const struct traverse_info *info, struct name_entry *branch1, struct name_entry *result) | |
01df5297 | 176 | { |
83788070 LT |
177 | struct merge_list *orig, *final; |
178 | const char *path; | |
179 | ||
01df5297 LT |
180 | /* If it's already branch1, don't bother showing it */ |
181 | if (!branch1) | |
182 | return; | |
01df5297 | 183 | |
40d934df | 184 | path = traverse_path(info, result); |
83788070 LT |
185 | orig = create_entry(2, branch1->mode, branch1->sha1, path); |
186 | final = create_entry(0, result->mode, result->sha1, path); | |
187 | ||
188 | final->link = orig; | |
189 | ||
190 | add_merge_entry(final); | |
492e0759 LT |
191 | } |
192 | ||
40d934df | 193 | static int unresolved_directory(const struct traverse_info *info, struct name_entry n[3]) |
492e0759 | 194 | { |
492e0759 LT |
195 | char *newbase; |
196 | struct name_entry *p; | |
197 | struct tree_desc t[3]; | |
198 | void *buf0, *buf1, *buf2; | |
199 | ||
492e0759 LT |
200 | p = n; |
201 | if (!p->mode) { | |
202 | p++; | |
203 | if (!p->mode) | |
204 | p++; | |
205 | } | |
206 | if (!S_ISDIR(p->mode)) | |
207 | return 0; | |
40d934df | 208 | newbase = traverse_path(info, p); |
492e0759 LT |
209 | buf0 = fill_tree_descriptor(t+0, n[0].sha1); |
210 | buf1 = fill_tree_descriptor(t+1, n[1].sha1); | |
211 | buf2 = fill_tree_descriptor(t+2, n[2].sha1); | |
212 | merge_trees(t, newbase); | |
213 | ||
214 | free(buf0); | |
215 | free(buf1); | |
216 | free(buf2); | |
217 | free(newbase); | |
218 | return 1; | |
219 | } | |
220 | ||
83788070 | 221 | |
40d934df | 222 | static struct merge_list *link_entry(unsigned stage, const struct traverse_info *info, struct name_entry *n, struct merge_list *entry) |
83788070 LT |
223 | { |
224 | const char *path; | |
225 | struct merge_list *link; | |
226 | ||
227 | if (!n->mode) | |
228 | return entry; | |
229 | if (entry) | |
230 | path = entry->path; | |
231 | else | |
40d934df | 232 | path = traverse_path(info, n); |
83788070 LT |
233 | link = create_entry(stage, n->mode, n->sha1, path); |
234 | link->link = entry; | |
235 | return link; | |
236 | } | |
237 | ||
40d934df | 238 | static void unresolved(const struct traverse_info *info, struct name_entry n[3]) |
492e0759 | 239 | { |
83788070 LT |
240 | struct merge_list *entry = NULL; |
241 | ||
40d934df | 242 | if (unresolved_directory(info, n)) |
492e0759 | 243 | return; |
83788070 LT |
244 | |
245 | /* | |
246 | * Do them in reverse order so that the resulting link | |
247 | * list has the stages in order - link_entry adds new | |
248 | * links at the front. | |
249 | */ | |
40d934df LT |
250 | entry = link_entry(3, info, n + 2, entry); |
251 | entry = link_entry(2, info, n + 1, entry); | |
252 | entry = link_entry(1, info, n + 0, entry); | |
83788070 LT |
253 | |
254 | add_merge_entry(entry); | |
492e0759 LT |
255 | } |
256 | ||
164dcb97 LT |
257 | /* |
258 | * Merge two trees together (t[1] and t[2]), using a common base (t[0]) | |
259 | * as the origin. | |
260 | * | |
261 | * This walks the (sorted) trees in lock-step, checking every possible | |
262 | * name. Note that directories automatically sort differently from other | |
263 | * files (see "base_name_compare"), so you'll never see file/directory | |
264 | * conflicts, because they won't ever compare the same. | |
265 | * | |
266 | * IOW, if a directory changes to a filename, it will automatically be | |
267 | * seen as the directory going away, and the filename being created. | |
268 | * | |
269 | * Think of this as a three-way diff. | |
270 | * | |
271 | * The output will be either: | |
272 | * - successful merge | |
273 | * "0 mode sha1 filename" | |
274 | * NOTE NOTE NOTE! FIXME! We really really need to walk the index | |
275 | * in parallel with this too! | |
276 | * | |
277 | * - conflict: | |
278 | * "1 mode sha1 filename" | |
279 | * "2 mode sha1 filename" | |
280 | * "3 mode sha1 filename" | |
281 | * where not all of the 1/2/3 lines may exist, of course. | |
282 | * | |
283 | * The successful merge rules are the same as for the three-way merge | |
284 | * in git-read-tree. | |
285 | */ | |
91e4f036 | 286 | static int threeway_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *info) |
164dcb97 LT |
287 | { |
288 | /* Same in both? */ | |
289 | if (same_entry(entry+1, entry+2)) { | |
290 | if (entry[0].sha1) { | |
40d934df | 291 | resolve(info, NULL, entry+1); |
5803c6f8 | 292 | return mask; |
492e0759 | 293 | } |
164dcb97 | 294 | } |
492e0759 | 295 | |
164dcb97 LT |
296 | if (same_entry(entry+0, entry+1)) { |
297 | if (entry[2].sha1 && !S_ISDIR(entry[2].mode)) { | |
40d934df | 298 | resolve(info, entry+1, entry+2); |
5803c6f8 | 299 | return mask; |
492e0759 | 300 | } |
164dcb97 | 301 | } |
492e0759 | 302 | |
164dcb97 LT |
303 | if (same_entry(entry+0, entry+2)) { |
304 | if (entry[1].sha1 && !S_ISDIR(entry[1].mode)) { | |
40d934df | 305 | resolve(info, NULL, entry+1); |
5803c6f8 | 306 | return mask; |
492e0759 | 307 | } |
492e0759 | 308 | } |
164dcb97 | 309 | |
40d934df | 310 | unresolved(info, entry); |
5803c6f8 | 311 | return mask; |
164dcb97 LT |
312 | } |
313 | ||
314 | static void merge_trees(struct tree_desc t[3], const char *base) | |
315 | { | |
40d934df LT |
316 | struct traverse_info info; |
317 | ||
318 | setup_traverse_info(&info, base); | |
319 | info.fn = threeway_callback; | |
320 | traverse_trees(3, t, &info); | |
492e0759 LT |
321 | } |
322 | ||
323 | static void *get_tree_descriptor(struct tree_desc *desc, const char *rev) | |
324 | { | |
325 | unsigned char sha1[20]; | |
326 | void *buf; | |
327 | ||
31fff305 | 328 | if (get_sha1(rev, sha1)) |
492e0759 LT |
329 | die("unknown rev %s", rev); |
330 | buf = fill_tree_descriptor(desc, sha1); | |
331 | if (!buf) | |
332 | die("%s is not a tree", rev); | |
333 | return buf; | |
334 | } | |
335 | ||
907a7cb5 | 336 | int cmd_merge_tree(int argc, const char **argv, const char *prefix) |
492e0759 LT |
337 | { |
338 | struct tree_desc t[3]; | |
339 | void *buf1, *buf2, *buf3; | |
340 | ||
5b6df8e4 | 341 | if (argc != 4) |
492e0759 LT |
342 | usage(merge_tree_usage); |
343 | ||
344 | buf1 = get_tree_descriptor(t+0, argv[1]); | |
345 | buf2 = get_tree_descriptor(t+1, argv[2]); | |
346 | buf3 = get_tree_descriptor(t+2, argv[3]); | |
347 | merge_trees(t, ""); | |
348 | free(buf1); | |
349 | free(buf2); | |
350 | free(buf3); | |
83788070 LT |
351 | |
352 | show_result(); | |
492e0759 LT |
353 | return 0; |
354 | } |