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