]>
Commit | Line | Data |
---|---|---|
666f53eb | 1 | #define USE_THE_INDEX_VARIABLE |
c2e86add | 2 | #include "builtin.h" |
1b0c7174 | 3 | #include "tree-walk.h" |
0c799383 | 4 | #include "xdiff-interface.h" |
1f0c3a29 | 5 | #include "help.h" |
f394e093 | 6 | #include "gettext.h" |
41771fa4 | 7 | #include "hex.h" |
66265a69 | 8 | #include "commit.h" |
1f0c3a29 EN |
9 | #include "commit-reach.h" |
10 | #include "merge-ort.h" | |
dabab1d6 | 11 | #include "object-name.h" |
a034e910 | 12 | #include "object-store-ll.h" |
6ec755a0 | 13 | #include "parse-options.h" |
da14a7ff | 14 | #include "repository.h" |
83788070 | 15 | #include "blob.h" |
d807c4a0 | 16 | #include "exec-cmd.h" |
fa2364ec | 17 | #include "merge-blobs.h" |
7fa33388 | 18 | #include "quote.h" |
d4a4f929 | 19 | #include "tree.h" |
b6551fea | 20 | #include "config.h" |
6a4c9e7b | 21 | #include "strvec.h" |
7fa33388 EN |
22 | |
23 | static int line_termination = '\n'; | |
492e0759 | 24 | |
83788070 LT |
25 | struct merge_list { |
26 | struct merge_list *next; | |
27 | struct merge_list *link; /* other stages for this object */ | |
28 | ||
b13112fa | 29 | unsigned int stage : 2; |
83788070 LT |
30 | unsigned int mode; |
31 | const char *path; | |
32 | struct blob *blob; | |
33 | }; | |
34 | ||
35 | static struct merge_list *merge_result, **merge_result_end = &merge_result; | |
36 | ||
37 | static void add_merge_entry(struct merge_list *entry) | |
38 | { | |
39 | *merge_result_end = entry; | |
40 | merge_result_end = &entry->next; | |
41 | } | |
42 | ||
70176b70 | 43 | static void trivial_merge_trees(struct tree_desc t[3], const char *base); |
492e0759 | 44 | |
83788070 LT |
45 | static const char *explanation(struct merge_list *entry) |
46 | { | |
47 | switch (entry->stage) { | |
48 | case 0: | |
49 | return "merged"; | |
50 | case 3: | |
51 | return "added in remote"; | |
52 | case 2: | |
53 | if (entry->link) | |
54 | return "added in both"; | |
55 | return "added in local"; | |
56 | } | |
57 | ||
58 | /* Existed in base */ | |
59 | entry = entry->link; | |
60 | if (!entry) | |
61 | return "removed in both"; | |
62 | ||
63 | if (entry->link) | |
64 | return "changed in both"; | |
65 | ||
66 | if (entry->stage == 3) | |
67 | return "removed in local"; | |
68 | return "removed in remote"; | |
69 | } | |
70 | ||
0c799383 LT |
71 | static void *result(struct merge_list *entry, unsigned long *size) |
72 | { | |
21666f1a | 73 | enum object_type type; |
0c799383 | 74 | struct blob *base, *our, *their; |
21baa6e0 | 75 | const char *path = entry->path; |
0c799383 LT |
76 | |
77 | if (!entry->stage) | |
bc726bd0 ÆAB |
78 | return repo_read_object_file(the_repository, |
79 | &entry->blob->object.oid, &type, | |
80 | size); | |
0c799383 LT |
81 | base = NULL; |
82 | if (entry->stage == 1) { | |
83 | base = entry->blob; | |
84 | entry = entry->link; | |
85 | } | |
86 | our = NULL; | |
87 | if (entry && entry->stage == 2) { | |
88 | our = entry->blob; | |
89 | entry = entry->link; | |
90 | } | |
91 | their = NULL; | |
92 | if (entry) | |
93 | their = entry->blob; | |
f8adbec9 NTND |
94 | return merge_blobs(the_repository->index, path, |
95 | base, our, their, size); | |
0c799383 LT |
96 | } |
97 | ||
98 | static void *origin(struct merge_list *entry, unsigned long *size) | |
99 | { | |
21666f1a | 100 | enum object_type type; |
0c799383 LT |
101 | while (entry) { |
102 | if (entry->stage == 2) | |
bc726bd0 ÆAB |
103 | return repo_read_object_file(the_repository, |
104 | &entry->blob->object.oid, | |
105 | &type, size); | |
0c799383 LT |
106 | entry = entry->link; |
107 | } | |
108 | return NULL; | |
109 | } | |
110 | ||
61bdc7c5 | 111 | static int show_outf(void *priv UNUSED, mmbuffer_t *mb, int nbuf) |
0c799383 LT |
112 | { |
113 | int i; | |
114 | for (i = 0; i < nbuf; i++) | |
115 | printf("%.*s", (int) mb[i].size, mb[i].ptr); | |
116 | return 0; | |
117 | } | |
118 | ||
119 | static void show_diff(struct merge_list *entry) | |
120 | { | |
121 | unsigned long size; | |
122 | mmfile_t src, dst; | |
123 | xpparam_t xpp; | |
124 | xdemitconf_t xecfg; | |
22233d43 | 125 | xdemitcb_t ecb = { .out_line = show_outf }; |
0c799383 | 126 | |
ec7967cf | 127 | memset(&xpp, 0, sizeof(xpp)); |
582aa00b | 128 | xpp.flags = 0; |
30b25010 | 129 | memset(&xecfg, 0, sizeof(xecfg)); |
0c799383 | 130 | xecfg.ctxlen = 3; |
0c799383 LT |
131 | |
132 | src.ptr = origin(entry, &size); | |
133 | if (!src.ptr) | |
134 | size = 0; | |
135 | src.size = size; | |
136 | dst.ptr = result(entry, &size); | |
137 | if (!dst.ptr) | |
138 | size = 0; | |
139 | dst.size = size; | |
3efb9880 JK |
140 | if (xdi_diff(&src, &dst, &xpp, &xecfg, &ecb)) |
141 | die("unable to generate diff"); | |
0c799383 LT |
142 | free(src.ptr); |
143 | free(dst.ptr); | |
144 | } | |
145 | ||
83788070 LT |
146 | static void show_result_list(struct merge_list *entry) |
147 | { | |
148 | printf("%s\n", explanation(entry)); | |
149 | do { | |
150 | struct merge_list *link = entry->link; | |
151 | static const char *desc[4] = { "result", "base", "our", "their" }; | |
f2fd0760 | 152 | printf(" %-6s %o %s %s\n", desc[entry->stage], entry->mode, oid_to_hex(&entry->blob->object.oid), entry->path); |
83788070 LT |
153 | entry = link; |
154 | } while (entry); | |
155 | } | |
156 | ||
157 | static void show_result(void) | |
158 | { | |
159 | struct merge_list *walk; | |
160 | ||
161 | walk = merge_result; | |
162 | while (walk) { | |
163 | show_result_list(walk); | |
0c799383 | 164 | show_diff(walk); |
83788070 LT |
165 | walk = walk->next; |
166 | } | |
167 | } | |
168 | ||
492e0759 LT |
169 | /* An empty entry never compares same, not even to another empty entry */ |
170 | static int same_entry(struct name_entry *a, struct name_entry *b) | |
171 | { | |
ea82b2a0 | 172 | return !is_null_oid(&a->oid) && |
173 | !is_null_oid(&b->oid) && | |
174 | oideq(&a->oid, &b->oid) && | |
492e0759 LT |
175 | a->mode == b->mode; |
176 | } | |
177 | ||
aacecc3b JK |
178 | static int both_empty(struct name_entry *a, struct name_entry *b) |
179 | { | |
ea82b2a0 | 180 | return is_null_oid(&a->oid) && is_null_oid(&b->oid); |
aacecc3b JK |
181 | } |
182 | ||
3e930981 | 183 | static struct merge_list *create_entry(unsigned stage, unsigned mode, const struct object_id *oid, const char *path) |
492e0759 | 184 | { |
19d4b416 | 185 | struct merge_list *res = xcalloc(1, sizeof(*res)); |
83788070 | 186 | |
83788070 LT |
187 | res->stage = stage; |
188 | res->path = path; | |
189 | res->mode = mode; | |
da14a7ff | 190 | res->blob = lookup_blob(the_repository, oid); |
83788070 | 191 | return res; |
01df5297 LT |
192 | } |
193 | ||
40d934df LT |
194 | static char *traverse_path(const struct traverse_info *info, const struct name_entry *n) |
195 | { | |
c43ab062 JK |
196 | struct strbuf buf = STRBUF_INIT; |
197 | strbuf_make_traverse_path(&buf, info, n->path, n->pathlen); | |
198 | return strbuf_detach(&buf, NULL); | |
40d934df LT |
199 | } |
200 | ||
8dd15c6a | 201 | static void resolve(const struct traverse_info *info, struct name_entry *ours, struct name_entry *result) |
01df5297 | 202 | { |
83788070 LT |
203 | struct merge_list *orig, *final; |
204 | const char *path; | |
205 | ||
8dd15c6a JH |
206 | /* If it's already ours, don't bother showing it */ |
207 | if (!ours) | |
01df5297 | 208 | return; |
01df5297 | 209 | |
40d934df | 210 | path = traverse_path(info, result); |
ea82b2a0 | 211 | orig = create_entry(2, ours->mode, &ours->oid, path); |
212 | final = create_entry(0, result->mode, &result->oid, path); | |
83788070 LT |
213 | |
214 | final->link = orig; | |
215 | ||
216 | add_merge_entry(final); | |
492e0759 LT |
217 | } |
218 | ||
57065289 RS |
219 | static void unresolved_directory(const struct traverse_info *info, |
220 | struct name_entry n[3]) | |
492e0759 | 221 | { |
5e575807 | 222 | struct repository *r = the_repository; |
492e0759 LT |
223 | char *newbase; |
224 | struct name_entry *p; | |
225 | struct tree_desc t[3]; | |
226 | void *buf0, *buf1, *buf2; | |
227 | ||
35ffe758 JH |
228 | for (p = n; p < n + 3; p++) { |
229 | if (p->mode && S_ISDIR(p->mode)) | |
230 | break; | |
492e0759 | 231 | } |
35ffe758 JH |
232 | if (n + 3 <= p) |
233 | return; /* there is no tree here */ | |
8dd15c6a | 234 | |
40d934df | 235 | newbase = traverse_path(info, p); |
35ffe758 | 236 | |
ea82b2a0 | 237 | #define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? &(e)->oid : NULL) |
5e575807 NTND |
238 | buf0 = fill_tree_descriptor(r, t + 0, ENTRY_OID(n + 0)); |
239 | buf1 = fill_tree_descriptor(r, t + 1, ENTRY_OID(n + 1)); | |
240 | buf2 = fill_tree_descriptor(r, t + 2, ENTRY_OID(n + 2)); | |
5c377d3d | 241 | #undef ENTRY_OID |
35ffe758 | 242 | |
70176b70 | 243 | trivial_merge_trees(t, newbase); |
492e0759 LT |
244 | |
245 | free(buf0); | |
246 | free(buf1); | |
247 | free(buf2); | |
248 | free(newbase); | |
492e0759 LT |
249 | } |
250 | ||
83788070 | 251 | |
40d934df | 252 | static struct merge_list *link_entry(unsigned stage, const struct traverse_info *info, struct name_entry *n, struct merge_list *entry) |
83788070 LT |
253 | { |
254 | const char *path; | |
255 | struct merge_list *link; | |
256 | ||
257 | if (!n->mode) | |
258 | return entry; | |
259 | if (entry) | |
260 | path = entry->path; | |
261 | else | |
40d934df | 262 | path = traverse_path(info, n); |
ea82b2a0 | 263 | link = create_entry(stage, n->mode, &n->oid, path); |
83788070 LT |
264 | link->link = entry; |
265 | return link; | |
266 | } | |
267 | ||
40d934df | 268 | static void unresolved(const struct traverse_info *info, struct name_entry n[3]) |
492e0759 | 269 | { |
83788070 | 270 | struct merge_list *entry = NULL; |
35ffe758 JH |
271 | int i; |
272 | unsigned dirmask = 0, mask = 0; | |
273 | ||
274 | for (i = 0; i < 3; i++) { | |
187c00c6 | 275 | mask |= (1 << i); |
94883b43 JK |
276 | /* |
277 | * Treat missing entries as directories so that we return | |
278 | * after unresolved_directory has handled this. | |
279 | */ | |
280 | if (!n[i].mode || S_ISDIR(n[i].mode)) | |
35ffe758 JH |
281 | dirmask |= (1 << i); |
282 | } | |
283 | ||
57065289 | 284 | unresolved_directory(info, n); |
83788070 | 285 | |
35ffe758 | 286 | if (dirmask == mask) |
492e0759 | 287 | return; |
83788070 | 288 | |
35ffe758 JH |
289 | if (n[2].mode && !S_ISDIR(n[2].mode)) |
290 | entry = link_entry(3, info, n + 2, entry); | |
291 | if (n[1].mode && !S_ISDIR(n[1].mode)) | |
292 | entry = link_entry(2, info, n + 1, entry); | |
293 | if (n[0].mode && !S_ISDIR(n[0].mode)) | |
294 | entry = link_entry(1, info, n + 0, entry); | |
83788070 LT |
295 | |
296 | add_merge_entry(entry); | |
492e0759 LT |
297 | } |
298 | ||
164dcb97 LT |
299 | /* |
300 | * Merge two trees together (t[1] and t[2]), using a common base (t[0]) | |
301 | * as the origin. | |
302 | * | |
303 | * This walks the (sorted) trees in lock-step, checking every possible | |
304 | * name. Note that directories automatically sort differently from other | |
305 | * files (see "base_name_compare"), so you'll never see file/directory | |
306 | * conflicts, because they won't ever compare the same. | |
307 | * | |
308 | * IOW, if a directory changes to a filename, it will automatically be | |
309 | * seen as the directory going away, and the filename being created. | |
310 | * | |
311 | * Think of this as a three-way diff. | |
312 | * | |
313 | * The output will be either: | |
314 | * - successful merge | |
315 | * "0 mode sha1 filename" | |
316 | * NOTE NOTE NOTE! FIXME! We really really need to walk the index | |
317 | * in parallel with this too! | |
318 | * | |
319 | * - conflict: | |
320 | * "1 mode sha1 filename" | |
321 | * "2 mode sha1 filename" | |
322 | * "3 mode sha1 filename" | |
323 | * where not all of the 1/2/3 lines may exist, of course. | |
324 | * | |
325 | * The successful merge rules are the same as for the three-way merge | |
326 | * in git-read-tree. | |
327 | */ | |
ee550abc JK |
328 | static int threeway_callback(int n UNUSED, unsigned long mask, |
329 | unsigned long dirmask UNUSED, | |
330 | struct name_entry *entry, struct traverse_info *info) | |
164dcb97 LT |
331 | { |
332 | /* Same in both? */ | |
ab5f4242 | 333 | if (same_entry(entry+1, entry+2) || both_empty(entry+1, entry+2)) { |
aacecc3b JK |
334 | /* Modified, added or removed identically */ |
335 | resolve(info, NULL, entry+1); | |
336 | return mask; | |
164dcb97 | 337 | } |
492e0759 | 338 | |
164dcb97 | 339 | if (same_entry(entry+0, entry+1)) { |
ea82b2a0 | 340 | if (!is_null_oid(&entry[2].oid) && !S_ISDIR(entry[2].mode)) { |
8dd15c6a | 341 | /* We did not touch, they modified -- take theirs */ |
40d934df | 342 | resolve(info, entry+1, entry+2); |
5803c6f8 | 343 | return mask; |
492e0759 | 344 | } |
8dd15c6a JH |
345 | /* |
346 | * If we did not touch a directory but they made it | |
347 | * into a file, we fall through and unresolved() | |
348 | * recurses down. Likewise for the opposite case. | |
349 | */ | |
164dcb97 | 350 | } |
492e0759 | 351 | |
aacecc3b JK |
352 | if (same_entry(entry+0, entry+2) || both_empty(entry+0, entry+2)) { |
353 | /* We added, modified or removed, they did not touch -- take ours */ | |
354 | resolve(info, NULL, entry+1); | |
355 | return mask; | |
492e0759 | 356 | } |
164dcb97 | 357 | |
40d934df | 358 | unresolved(info, entry); |
5803c6f8 | 359 | return mask; |
164dcb97 LT |
360 | } |
361 | ||
70176b70 | 362 | static void trivial_merge_trees(struct tree_desc t[3], const char *base) |
164dcb97 | 363 | { |
40d934df LT |
364 | struct traverse_info info; |
365 | ||
366 | setup_traverse_info(&info, base); | |
367 | info.fn = threeway_callback; | |
67022e02 | 368 | traverse_trees(&the_index, 3, t, &info); |
492e0759 LT |
369 | } |
370 | ||
5e575807 NTND |
371 | static void *get_tree_descriptor(struct repository *r, |
372 | struct tree_desc *desc, | |
373 | const char *rev) | |
492e0759 | 374 | { |
d1a35e5c | 375 | struct object_id oid; |
492e0759 LT |
376 | void *buf; |
377 | ||
5e575807 | 378 | if (repo_get_oid(r, rev, &oid)) |
492e0759 | 379 | die("unknown rev %s", rev); |
5e575807 | 380 | buf = fill_tree_descriptor(r, desc, &oid); |
492e0759 LT |
381 | if (!buf) |
382 | die("%s is not a tree", rev); | |
383 | return buf; | |
384 | } | |
385 | ||
6ec755a0 EN |
386 | static int trivial_merge(const char *base, |
387 | const char *branch1, | |
388 | const char *branch2) | |
492e0759 | 389 | { |
5e575807 | 390 | struct repository *r = the_repository; |
492e0759 LT |
391 | struct tree_desc t[3]; |
392 | void *buf1, *buf2, *buf3; | |
393 | ||
6ec755a0 EN |
394 | buf1 = get_tree_descriptor(r, t+0, base); |
395 | buf2 = get_tree_descriptor(r, t+1, branch1); | |
396 | buf3 = get_tree_descriptor(r, t+2, branch2); | |
70176b70 | 397 | trivial_merge_trees(t, ""); |
492e0759 LT |
398 | free(buf1); |
399 | free(buf2); | |
400 | free(buf3); | |
83788070 LT |
401 | |
402 | show_result(); | |
492e0759 LT |
403 | return 0; |
404 | } | |
55e48f6b | 405 | |
6ec755a0 EN |
406 | enum mode { |
407 | MODE_UNKNOWN, | |
408 | MODE_TRIVIAL, | |
409 | MODE_REAL, | |
410 | }; | |
411 | ||
412 | struct merge_tree_options { | |
413 | int mode; | |
7976721d | 414 | int allow_unrelated_histories; |
a1a78119 | 415 | int show_messages; |
b520bc6c | 416 | int name_only; |
ec1edbcb | 417 | int use_stdin; |
6a4c9e7b | 418 | struct merge_options merge_options; |
6ec755a0 EN |
419 | }; |
420 | ||
421 | static int real_merge(struct merge_tree_options *o, | |
66265a69 | 422 | const char *merge_base, |
7fa33388 EN |
423 | const char *branch1, const char *branch2, |
424 | const char *prefix) | |
6ec755a0 | 425 | { |
1f0c3a29 EN |
426 | struct commit *parent1, *parent2; |
427 | struct commit_list *merge_bases = NULL; | |
6a4c9e7b | 428 | struct merge_options opt = o->merge_options; |
1f0c3a29 | 429 | struct merge_result result = { 0 }; |
ec1edbcb | 430 | int show_messages = o->show_messages; |
1f0c3a29 EN |
431 | |
432 | parent1 = get_merge_parent(branch1); | |
433 | if (!parent1) | |
434 | help_unknown_ref(branch1, "merge-tree", | |
435 | _("not something we can merge")); | |
436 | ||
437 | parent2 = get_merge_parent(branch2); | |
438 | if (!parent2) | |
439 | help_unknown_ref(branch2, "merge-tree", | |
440 | _("not something we can merge")); | |
441 | ||
1f0c3a29 EN |
442 | opt.show_rename_progress = 0; |
443 | ||
444 | opt.branch1 = branch1; | |
445 | opt.branch2 = branch2; | |
446 | ||
66265a69 KZ |
447 | if (merge_base) { |
448 | struct commit *base_commit; | |
449 | struct tree *base_tree, *parent1_tree, *parent2_tree; | |
450 | ||
451 | base_commit = lookup_commit_reference_by_name(merge_base); | |
452 | if (!base_commit) | |
e4cf0134 | 453 | die(_("could not lookup commit '%s'"), merge_base); |
66265a69 KZ |
454 | |
455 | opt.ancestor = merge_base; | |
ecb5091f ÆAB |
456 | base_tree = repo_get_commit_tree(the_repository, base_commit); |
457 | parent1_tree = repo_get_commit_tree(the_repository, parent1); | |
458 | parent2_tree = repo_get_commit_tree(the_repository, parent2); | |
66265a69 KZ |
459 | merge_incore_nonrecursive(&opt, base_tree, parent1_tree, parent2_tree, &result); |
460 | } else { | |
461 | /* | |
462 | * Get the merge bases, in reverse order; see comment above | |
463 | * merge_incore_recursive in merge-ort.h | |
464 | */ | |
cb338c23 ÆAB |
465 | merge_bases = repo_get_merge_bases(the_repository, parent1, |
466 | parent2); | |
66265a69 KZ |
467 | if (!merge_bases && !o->allow_unrelated_histories) |
468 | die(_("refusing to merge unrelated histories")); | |
469 | merge_bases = reverse_commit_list(merge_bases); | |
470 | merge_incore_recursive(&opt, merge_bases, parent1, parent2, &result); | |
471 | } | |
1f0c3a29 | 472 | |
1f0c3a29 EN |
473 | if (result.clean < 0) |
474 | die(_("failure to merge")); | |
a1a78119 | 475 | |
ec1edbcb EN |
476 | if (show_messages == -1) |
477 | show_messages = !result.clean; | |
a1a78119 | 478 | |
ec1edbcb EN |
479 | if (o->use_stdin) |
480 | printf("%d%c", result.clean, line_termination); | |
7c48b278 | 481 | printf("%s%c", oid_to_hex(&result.tree->object.oid), line_termination); |
7fa33388 EN |
482 | if (!result.clean) { |
483 | struct string_list conflicted_files = STRING_LIST_INIT_NODUP; | |
484 | const char *last = NULL; | |
485 | int i; | |
486 | ||
487 | merge_get_conflicted_files(&result, &conflicted_files); | |
488 | for (i = 0; i < conflicted_files.nr; i++) { | |
489 | const char *name = conflicted_files.items[i].string; | |
b520bc6c EN |
490 | struct stage_info *c = conflicted_files.items[i].util; |
491 | if (!o->name_only) | |
492 | printf("%06o %s %d\t", | |
493 | c->mode, oid_to_hex(&c->oid), c->stage); | |
494 | else if (last && !strcmp(last, name)) | |
7fa33388 EN |
495 | continue; |
496 | write_name_quoted_relative( | |
497 | name, prefix, stdout, line_termination); | |
498 | last = name; | |
499 | } | |
500 | string_list_clear(&conflicted_files, 1); | |
501 | } | |
ec1edbcb | 502 | if (show_messages) { |
7fa33388 | 503 | putchar(line_termination); |
de905811 EN |
504 | merge_display_update_messages(&opt, line_termination == '\0', |
505 | &result); | |
a1a78119 | 506 | } |
ec1edbcb EN |
507 | if (o->use_stdin) |
508 | putchar(line_termination); | |
1f0c3a29 EN |
509 | merge_finalize(&opt, &result); |
510 | return !result.clean; /* result.clean < 0 handled above */ | |
6ec755a0 EN |
511 | } |
512 | ||
55e48f6b EN |
513 | int cmd_merge_tree(int argc, const char **argv, const char *prefix) |
514 | { | |
a1a78119 | 515 | struct merge_tree_options o = { .show_messages = -1 }; |
6a4c9e7b | 516 | struct strvec xopts = STRVEC_INIT; |
6ec755a0 | 517 | int expected_remaining_argc; |
a1a78119 | 518 | int original_argc; |
66265a69 | 519 | const char *merge_base = NULL; |
6ec755a0 EN |
520 | |
521 | const char * const merge_tree_usage[] = { | |
a1a78119 | 522 | N_("git merge-tree [--write-tree] [<options>] <branch1> <branch2>"), |
6ec755a0 EN |
523 | N_("git merge-tree [--trivial-merge] <base-tree> <branch1> <branch2>"), |
524 | NULL | |
525 | }; | |
526 | struct option mt_options[] = { | |
527 | OPT_CMDMODE(0, "write-tree", &o.mode, | |
528 | N_("do a real merge instead of a trivial merge"), | |
529 | MODE_REAL), | |
530 | OPT_CMDMODE(0, "trivial-merge", &o.mode, | |
531 | N_("do a trivial merge only"), MODE_TRIVIAL), | |
a1a78119 EN |
532 | OPT_BOOL(0, "messages", &o.show_messages, |
533 | N_("also show informational/conflict messages")), | |
7c48b278 EN |
534 | OPT_SET_INT('z', NULL, &line_termination, |
535 | N_("separate paths with the NUL character"), '\0'), | |
b520bc6c EN |
536 | OPT_BOOL_F(0, "name-only", |
537 | &o.name_only, | |
538 | N_("list filenames without modes/oids/stages"), | |
539 | PARSE_OPT_NONEG), | |
7976721d EN |
540 | OPT_BOOL_F(0, "allow-unrelated-histories", |
541 | &o.allow_unrelated_histories, | |
542 | N_("allow merging unrelated histories"), | |
543 | PARSE_OPT_NONEG), | |
ec1edbcb EN |
544 | OPT_BOOL_F(0, "stdin", |
545 | &o.use_stdin, | |
546 | N_("perform multiple merges, one per line of input"), | |
547 | PARSE_OPT_NONEG), | |
66265a69 KZ |
548 | OPT_STRING(0, "merge-base", |
549 | &merge_base, | |
550 | N_("commit"), | |
551 | N_("specify a merge-base for the merge")), | |
6a4c9e7b TY |
552 | OPT_STRVEC('X', "strategy-option", &xopts, N_("option=value"), |
553 | N_("option for selected merge strategy")), | |
6ec755a0 EN |
554 | OPT_END() |
555 | }; | |
556 | ||
6a4c9e7b TY |
557 | /* Init merge options */ |
558 | init_merge_options(&o.merge_options, the_repository); | |
559 | ||
6ec755a0 | 560 | /* Parse arguments */ |
a1a78119 | 561 | original_argc = argc - 1; /* ignoring argv[0] */ |
6ec755a0 EN |
562 | argc = parse_options(argc, argv, prefix, mt_options, |
563 | merge_tree_usage, PARSE_OPT_STOP_AT_NON_OPTION); | |
ec1edbcb | 564 | |
6a4c9e7b TY |
565 | if (xopts.nr && o.mode == MODE_TRIVIAL) |
566 | die(_("--trivial-merge is incompatible with all other options")); | |
567 | for (int x = 0; x < xopts.nr; x++) | |
568 | if (parse_merge_opt(&o.merge_options, xopts.v[x])) | |
569 | die(_("unknown strategy option: -X%s"), xopts.v[x]); | |
570 | ||
ec1edbcb EN |
571 | /* Handle --stdin */ |
572 | if (o.use_stdin) { | |
573 | struct strbuf buf = STRBUF_INIT; | |
574 | ||
575 | if (o.mode == MODE_TRIVIAL) | |
576 | die(_("--trivial-merge is incompatible with all other options")); | |
66265a69 KZ |
577 | if (merge_base) |
578 | die(_("--merge-base is incompatible with --stdin")); | |
ec1edbcb EN |
579 | line_termination = '\0'; |
580 | while (strbuf_getline_lf(&buf, stdin) != EOF) { | |
581 | struct strbuf **split; | |
582 | int result; | |
501e3bab | 583 | const char *input_merge_base = NULL; |
ec1edbcb EN |
584 | |
585 | split = strbuf_split(&buf, ' '); | |
501e3bab | 586 | if (!split[0] || !split[1]) |
ec1edbcb EN |
587 | die(_("malformed input line: '%s'."), buf.buf); |
588 | strbuf_rtrim(split[0]); | |
501e3bab KZ |
589 | strbuf_rtrim(split[1]); |
590 | ||
591 | /* parse the merge-base */ | |
592 | if (!strcmp(split[1]->buf, "--")) { | |
593 | input_merge_base = split[0]->buf; | |
594 | } | |
595 | ||
596 | if (input_merge_base && split[2] && split[3] && !split[4]) { | |
597 | strbuf_rtrim(split[2]); | |
598 | strbuf_rtrim(split[3]); | |
599 | result = real_merge(&o, input_merge_base, split[2]->buf, split[3]->buf, prefix); | |
600 | } else if (!input_merge_base && !split[2]) { | |
601 | result = real_merge(&o, NULL, split[0]->buf, split[1]->buf, prefix); | |
602 | } else { | |
603 | die(_("malformed input line: '%s'."), buf.buf); | |
604 | } | |
605 | ||
ec1edbcb EN |
606 | if (result < 0) |
607 | die(_("merging cannot continue; got unclean result of %d"), result); | |
608 | strbuf_list_free(split); | |
609 | } | |
610 | strbuf_release(&buf); | |
611 | return 0; | |
612 | } | |
613 | ||
614 | /* Figure out which mode to use */ | |
6ec755a0 EN |
615 | switch (o.mode) { |
616 | default: | |
617 | BUG("unexpected command mode %d", o.mode); | |
618 | case MODE_UNKNOWN: | |
619 | switch (argc) { | |
620 | default: | |
621 | usage_with_options(merge_tree_usage, mt_options); | |
622 | case 2: | |
623 | o.mode = MODE_REAL; | |
624 | break; | |
625 | case 3: | |
626 | o.mode = MODE_TRIVIAL; | |
627 | break; | |
628 | } | |
629 | expected_remaining_argc = argc; | |
630 | break; | |
631 | case MODE_REAL: | |
632 | expected_remaining_argc = 2; | |
633 | break; | |
634 | case MODE_TRIVIAL: | |
635 | expected_remaining_argc = 3; | |
a1a78119 EN |
636 | /* Removal of `--trivial-merge` is expected */ |
637 | original_argc--; | |
6ec755a0 EN |
638 | break; |
639 | } | |
a1a78119 EN |
640 | if (o.mode == MODE_TRIVIAL && argc < original_argc) |
641 | die(_("--trivial-merge is incompatible with all other options")); | |
6ec755a0 EN |
642 | |
643 | if (argc != expected_remaining_argc) | |
644 | usage_with_options(merge_tree_usage, mt_options); | |
645 | ||
b6551fea DS |
646 | git_config(git_default_config, NULL); |
647 | ||
6ec755a0 EN |
648 | /* Do the relevant type of merge */ |
649 | if (o.mode == MODE_REAL) | |
66265a69 | 650 | return real_merge(&o, merge_base, argv[0], argv[1], prefix); |
6ec755a0 EN |
651 | else |
652 | return trivial_merge(argv[0], argv[1], argv[2]); | |
55e48f6b | 653 | } |