]>
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 | ||
57065289 | 28 | static void merge_trees(struct tree_desc t[3], const char *base); |
492e0759 | 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) | |
ed1c9977 | 63 | return read_sha1_file(entry->blob->object.oid.hash, &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) | |
ed1c9977 | 85 | return read_sha1_file(entry->blob->object.oid.hash, &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; | |
3efb9880 JK |
121 | if (xdi_diff(&src, &dst, &xpp, &xecfg, &ecb)) |
122 | die("unable to generate diff"); | |
0c799383 LT |
123 | free(src.ptr); |
124 | free(dst.ptr); | |
125 | } | |
126 | ||
83788070 LT |
127 | static void show_result_list(struct merge_list *entry) |
128 | { | |
129 | printf("%s\n", explanation(entry)); | |
130 | do { | |
131 | struct merge_list *link = entry->link; | |
132 | static const char *desc[4] = { "result", "base", "our", "their" }; | |
f2fd0760 | 133 | printf(" %-6s %o %s %s\n", desc[entry->stage], entry->mode, oid_to_hex(&entry->blob->object.oid), entry->path); |
83788070 LT |
134 | entry = link; |
135 | } while (entry); | |
136 | } | |
137 | ||
138 | static void show_result(void) | |
139 | { | |
140 | struct merge_list *walk; | |
141 | ||
142 | walk = merge_result; | |
143 | while (walk) { | |
144 | show_result_list(walk); | |
0c799383 | 145 | show_diff(walk); |
83788070 LT |
146 | walk = walk->next; |
147 | } | |
148 | } | |
149 | ||
492e0759 LT |
150 | /* An empty entry never compares same, not even to another empty entry */ |
151 | static int same_entry(struct name_entry *a, struct name_entry *b) | |
152 | { | |
7d924c91 | 153 | return a->oid && |
154 | b->oid && | |
155 | !oidcmp(a->oid, b->oid) && | |
492e0759 LT |
156 | a->mode == b->mode; |
157 | } | |
158 | ||
aacecc3b JK |
159 | static int both_empty(struct name_entry *a, struct name_entry *b) |
160 | { | |
7d924c91 | 161 | return !(a->oid || b->oid); |
aacecc3b JK |
162 | } |
163 | ||
3e930981 | 164 | static struct merge_list *create_entry(unsigned stage, unsigned mode, const struct object_id *oid, const char *path) |
492e0759 | 165 | { |
19d4b416 | 166 | struct merge_list *res = xcalloc(1, sizeof(*res)); |
83788070 | 167 | |
83788070 LT |
168 | res->stage = stage; |
169 | res->path = path; | |
170 | res->mode = mode; | |
3aca1fc6 | 171 | res->blob = lookup_blob(oid); |
83788070 | 172 | return res; |
01df5297 LT |
173 | } |
174 | ||
40d934df LT |
175 | static char *traverse_path(const struct traverse_info *info, const struct name_entry *n) |
176 | { | |
3733e694 | 177 | char *path = xmallocz(traverse_path_len(info, n)); |
40d934df LT |
178 | return make_traverse_path(path, info, n); |
179 | } | |
180 | ||
8dd15c6a | 181 | static void resolve(const struct traverse_info *info, struct name_entry *ours, struct name_entry *result) |
01df5297 | 182 | { |
83788070 LT |
183 | struct merge_list *orig, *final; |
184 | const char *path; | |
185 | ||
8dd15c6a JH |
186 | /* If it's already ours, don't bother showing it */ |
187 | if (!ours) | |
01df5297 | 188 | return; |
01df5297 | 189 | |
40d934df | 190 | path = traverse_path(info, result); |
3e930981 | 191 | orig = create_entry(2, ours->mode, ours->oid, path); |
192 | final = create_entry(0, result->mode, result->oid, path); | |
83788070 LT |
193 | |
194 | final->link = orig; | |
195 | ||
196 | add_merge_entry(final); | |
492e0759 LT |
197 | } |
198 | ||
57065289 RS |
199 | static void unresolved_directory(const struct traverse_info *info, |
200 | struct name_entry n[3]) | |
492e0759 | 201 | { |
492e0759 LT |
202 | char *newbase; |
203 | struct name_entry *p; | |
204 | struct tree_desc t[3]; | |
205 | void *buf0, *buf1, *buf2; | |
206 | ||
35ffe758 JH |
207 | for (p = n; p < n + 3; p++) { |
208 | if (p->mode && S_ISDIR(p->mode)) | |
209 | break; | |
492e0759 | 210 | } |
35ffe758 JH |
211 | if (n + 3 <= p) |
212 | return; /* there is no tree here */ | |
8dd15c6a | 213 | |
40d934df | 214 | newbase = traverse_path(info, p); |
35ffe758 | 215 | |
5c377d3d RS |
216 | #define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? (e)->oid : NULL) |
217 | buf0 = fill_tree_descriptor(t + 0, ENTRY_OID(n + 0)); | |
218 | buf1 = fill_tree_descriptor(t + 1, ENTRY_OID(n + 1)); | |
219 | buf2 = fill_tree_descriptor(t + 2, ENTRY_OID(n + 2)); | |
220 | #undef ENTRY_OID | |
35ffe758 | 221 | |
57065289 | 222 | merge_trees(t, newbase); |
492e0759 LT |
223 | |
224 | free(buf0); | |
225 | free(buf1); | |
226 | free(buf2); | |
227 | free(newbase); | |
492e0759 LT |
228 | } |
229 | ||
83788070 | 230 | |
40d934df | 231 | static struct merge_list *link_entry(unsigned stage, const struct traverse_info *info, struct name_entry *n, struct merge_list *entry) |
83788070 LT |
232 | { |
233 | const char *path; | |
234 | struct merge_list *link; | |
235 | ||
236 | if (!n->mode) | |
237 | return entry; | |
238 | if (entry) | |
239 | path = entry->path; | |
240 | else | |
40d934df | 241 | path = traverse_path(info, n); |
3e930981 | 242 | link = create_entry(stage, n->mode, n->oid, path); |
83788070 LT |
243 | link->link = entry; |
244 | return link; | |
245 | } | |
246 | ||
40d934df | 247 | static void unresolved(const struct traverse_info *info, struct name_entry n[3]) |
492e0759 | 248 | { |
83788070 | 249 | struct merge_list *entry = NULL; |
35ffe758 JH |
250 | int i; |
251 | unsigned dirmask = 0, mask = 0; | |
252 | ||
253 | for (i = 0; i < 3; i++) { | |
187c00c6 | 254 | mask |= (1 << i); |
94883b43 JK |
255 | /* |
256 | * Treat missing entries as directories so that we return | |
257 | * after unresolved_directory has handled this. | |
258 | */ | |
259 | if (!n[i].mode || S_ISDIR(n[i].mode)) | |
35ffe758 JH |
260 | dirmask |= (1 << i); |
261 | } | |
262 | ||
57065289 | 263 | unresolved_directory(info, n); |
83788070 | 264 | |
35ffe758 | 265 | if (dirmask == mask) |
492e0759 | 266 | return; |
83788070 | 267 | |
35ffe758 JH |
268 | if (n[2].mode && !S_ISDIR(n[2].mode)) |
269 | entry = link_entry(3, info, n + 2, entry); | |
270 | if (n[1].mode && !S_ISDIR(n[1].mode)) | |
271 | entry = link_entry(2, info, n + 1, entry); | |
272 | if (n[0].mode && !S_ISDIR(n[0].mode)) | |
273 | entry = link_entry(1, info, n + 0, entry); | |
83788070 LT |
274 | |
275 | add_merge_entry(entry); | |
492e0759 LT |
276 | } |
277 | ||
164dcb97 LT |
278 | /* |
279 | * Merge two trees together (t[1] and t[2]), using a common base (t[0]) | |
280 | * as the origin. | |
281 | * | |
282 | * This walks the (sorted) trees in lock-step, checking every possible | |
283 | * name. Note that directories automatically sort differently from other | |
284 | * files (see "base_name_compare"), so you'll never see file/directory | |
285 | * conflicts, because they won't ever compare the same. | |
286 | * | |
287 | * IOW, if a directory changes to a filename, it will automatically be | |
288 | * seen as the directory going away, and the filename being created. | |
289 | * | |
290 | * Think of this as a three-way diff. | |
291 | * | |
292 | * The output will be either: | |
293 | * - successful merge | |
294 | * "0 mode sha1 filename" | |
295 | * NOTE NOTE NOTE! FIXME! We really really need to walk the index | |
296 | * in parallel with this too! | |
297 | * | |
298 | * - conflict: | |
299 | * "1 mode sha1 filename" | |
300 | * "2 mode sha1 filename" | |
301 | * "3 mode sha1 filename" | |
302 | * where not all of the 1/2/3 lines may exist, of course. | |
303 | * | |
304 | * The successful merge rules are the same as for the three-way merge | |
305 | * in git-read-tree. | |
306 | */ | |
91e4f036 | 307 | static int threeway_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *info) |
164dcb97 LT |
308 | { |
309 | /* Same in both? */ | |
ab5f4242 | 310 | if (same_entry(entry+1, entry+2) || both_empty(entry+1, entry+2)) { |
aacecc3b JK |
311 | /* Modified, added or removed identically */ |
312 | resolve(info, NULL, entry+1); | |
313 | return mask; | |
164dcb97 | 314 | } |
492e0759 | 315 | |
164dcb97 | 316 | if (same_entry(entry+0, entry+1)) { |
7d924c91 | 317 | if (entry[2].oid && !S_ISDIR(entry[2].mode)) { |
8dd15c6a | 318 | /* We did not touch, they modified -- take theirs */ |
40d934df | 319 | resolve(info, entry+1, entry+2); |
5803c6f8 | 320 | return mask; |
492e0759 | 321 | } |
8dd15c6a JH |
322 | /* |
323 | * If we did not touch a directory but they made it | |
324 | * into a file, we fall through and unresolved() | |
325 | * recurses down. Likewise for the opposite case. | |
326 | */ | |
164dcb97 | 327 | } |
492e0759 | 328 | |
aacecc3b JK |
329 | if (same_entry(entry+0, entry+2) || both_empty(entry+0, entry+2)) { |
330 | /* We added, modified or removed, they did not touch -- take ours */ | |
331 | resolve(info, NULL, entry+1); | |
332 | return mask; | |
492e0759 | 333 | } |
164dcb97 | 334 | |
40d934df | 335 | unresolved(info, entry); |
5803c6f8 | 336 | return mask; |
164dcb97 LT |
337 | } |
338 | ||
57065289 | 339 | static void merge_trees(struct tree_desc t[3], const char *base) |
164dcb97 | 340 | { |
40d934df LT |
341 | struct traverse_info info; |
342 | ||
343 | setup_traverse_info(&info, base); | |
344 | info.fn = threeway_callback; | |
345 | traverse_trees(3, t, &info); | |
492e0759 LT |
346 | } |
347 | ||
348 | static void *get_tree_descriptor(struct tree_desc *desc, const char *rev) | |
349 | { | |
d1a35e5c | 350 | struct object_id oid; |
492e0759 LT |
351 | void *buf; |
352 | ||
d1a35e5c | 353 | if (get_oid(rev, &oid)) |
492e0759 | 354 | die("unknown rev %s", rev); |
5c377d3d | 355 | buf = fill_tree_descriptor(desc, &oid); |
492e0759 LT |
356 | if (!buf) |
357 | die("%s is not a tree", rev); | |
358 | return buf; | |
359 | } | |
360 | ||
907a7cb5 | 361 | int cmd_merge_tree(int argc, const char **argv, const char *prefix) |
492e0759 LT |
362 | { |
363 | struct tree_desc t[3]; | |
364 | void *buf1, *buf2, *buf3; | |
365 | ||
5b6df8e4 | 366 | if (argc != 4) |
492e0759 LT |
367 | usage(merge_tree_usage); |
368 | ||
369 | buf1 = get_tree_descriptor(t+0, argv[1]); | |
370 | buf2 = get_tree_descriptor(t+1, argv[2]); | |
371 | buf3 = get_tree_descriptor(t+2, argv[3]); | |
372 | merge_trees(t, ""); | |
373 | free(buf1); | |
374 | free(buf2); | |
375 | free(buf3); | |
83788070 LT |
376 | |
377 | show_result(); | |
492e0759 LT |
378 | return 0; |
379 | } |