]>
Commit | Line | Data |
---|---|---|
1c7b76be MV |
1 | /* |
2 | * Builtin "git merge" | |
3 | * | |
4 | * Copyright (c) 2008 Miklos Vajna <vmiklos@frugalware.org> | |
5 | * | |
6 | * Based on git-merge.sh by Junio C Hamano. | |
7 | */ | |
8 | ||
9 | #include "cache.h" | |
10 | #include "parse-options.h" | |
11 | #include "builtin.h" | |
12 | #include "run-command.h" | |
13 | #include "diff.h" | |
14 | #include "refs.h" | |
15 | #include "commit.h" | |
16 | #include "diffcore.h" | |
17 | #include "revision.h" | |
18 | #include "unpack-trees.h" | |
19 | #include "cache-tree.h" | |
20 | #include "dir.h" | |
21 | #include "utf8.h" | |
22 | #include "log-tree.h" | |
23 | #include "color.h" | |
fcab40a3 | 24 | #include "rerere.h" |
87091b49 | 25 | #include "help.h" |
18668f53 | 26 | #include "merge-recursive.h" |
cfc5789a | 27 | #include "resolve-undo.h" |
93e535a5 | 28 | #include "remote.h" |
1c7b76be MV |
29 | |
30 | #define DEFAULT_TWOHEAD (1<<0) | |
31 | #define DEFAULT_OCTOPUS (1<<1) | |
32 | #define NO_FAST_FORWARD (1<<2) | |
33 | #define NO_TRIVIAL (1<<3) | |
34 | ||
35 | struct strategy { | |
36 | const char *name; | |
37 | unsigned attr; | |
38 | }; | |
39 | ||
40 | static const char * const builtin_merge_usage[] = { | |
93e535a5 | 41 | "git merge [options] [<commit>...]", |
c395c25b JH |
42 | "git merge [options] <msg> HEAD <commit>", |
43 | "git merge --abort", | |
1c7b76be MV |
44 | NULL |
45 | }; | |
46 | ||
96e9420c | 47 | static int show_diffstat = 1, shortlog_len, squash; |
1c7b76be | 48 | static int option_commit = 1, allow_fast_forward = 1; |
13474835 | 49 | static int fast_forward_only; |
1c7b76be MV |
50 | static int allow_trivial = 1, have_message; |
51 | static struct strbuf merge_msg; | |
52 | static struct commit_list *remoteheads; | |
53 | static unsigned char head[20], stash[20]; | |
54 | static struct strategy **use_strategies; | |
55 | static size_t use_strategies_nr, use_strategies_alloc; | |
8cc5b290 AP |
56 | static const char **xopts; |
57 | static size_t xopts_nr, xopts_alloc; | |
1c7b76be | 58 | static const char *branch; |
0d8fc3ef | 59 | static char *branch_mergeoptions; |
7610fa57 | 60 | static int option_renormalize; |
7f87aff2 | 61 | static int verbosity; |
cb6020bb | 62 | static int allow_rerere_auto; |
35d2fffd | 63 | static int abort_current_merge; |
99bfc669 | 64 | static int show_progress = -1; |
93e535a5 | 65 | static int default_to_upstream; |
1c7b76be MV |
66 | |
67 | static struct strategy all_strategy[] = { | |
1c7b76be MV |
68 | { "recursive", DEFAULT_TWOHEAD | NO_TRIVIAL }, |
69 | { "octopus", DEFAULT_OCTOPUS }, | |
70 | { "resolve", 0 }, | |
1c7b76be MV |
71 | { "ours", NO_FAST_FORWARD | NO_TRIVIAL }, |
72 | { "subtree", NO_FAST_FORWARD | NO_TRIVIAL }, | |
73 | }; | |
74 | ||
75 | static const char *pull_twohead, *pull_octopus; | |
76 | ||
77 | static int option_parse_message(const struct option *opt, | |
78 | const char *arg, int unset) | |
79 | { | |
80 | struct strbuf *buf = opt->value; | |
81 | ||
82 | if (unset) | |
83 | strbuf_setlen(buf, 0); | |
74f5b7fb | 84 | else if (arg) { |
ce9d823b | 85 | strbuf_addf(buf, "%s%s", buf->len ? "\n\n" : "", arg); |
1c7b76be | 86 | have_message = 1; |
74f5b7fb | 87 | } else |
bacec478 | 88 | return error(_("switch `m' requires a value")); |
1c7b76be MV |
89 | return 0; |
90 | } | |
91 | ||
92 | static struct strategy *get_strategy(const char *name) | |
93 | { | |
94 | int i; | |
87091b49 MV |
95 | struct strategy *ret; |
96 | static struct cmdnames main_cmds, other_cmds; | |
e321180e | 97 | static int loaded; |
1c7b76be MV |
98 | |
99 | if (!name) | |
100 | return NULL; | |
101 | ||
102 | for (i = 0; i < ARRAY_SIZE(all_strategy); i++) | |
103 | if (!strcmp(name, all_strategy[i].name)) | |
104 | return &all_strategy[i]; | |
1719b5e4 | 105 | |
e321180e | 106 | if (!loaded) { |
87091b49 | 107 | struct cmdnames not_strategies; |
e321180e | 108 | loaded = 1; |
87091b49 | 109 | |
87091b49 | 110 | memset(¬_strategies, 0, sizeof(struct cmdnames)); |
e321180e | 111 | load_command_list("git-merge-", &main_cmds, &other_cmds); |
87091b49 MV |
112 | for (i = 0; i < main_cmds.cnt; i++) { |
113 | int j, found = 0; | |
114 | struct cmdname *ent = main_cmds.names[i]; | |
115 | for (j = 0; j < ARRAY_SIZE(all_strategy); j++) | |
116 | if (!strncmp(ent->name, all_strategy[j].name, ent->len) | |
117 | && !all_strategy[j].name[ent->len]) | |
118 | found = 1; | |
119 | if (!found) | |
120 | add_cmdname(¬_strategies, ent->name, ent->len); | |
87091b49 | 121 | } |
ed874656 | 122 | exclude_cmds(&main_cmds, ¬_strategies); |
87091b49 MV |
123 | } |
124 | if (!is_in_cmdlist(&main_cmds, name) && !is_in_cmdlist(&other_cmds, name)) { | |
bacec478 ÆAB |
125 | fprintf(stderr, _("Could not find merge strategy '%s'.\n"), name); |
126 | fprintf(stderr, _("Available strategies are:")); | |
131f9a10 JH |
127 | for (i = 0; i < main_cmds.cnt; i++) |
128 | fprintf(stderr, " %s", main_cmds.names[i]->name); | |
129 | fprintf(stderr, ".\n"); | |
130 | if (other_cmds.cnt) { | |
bacec478 | 131 | fprintf(stderr, _("Available custom strategies are:")); |
131f9a10 JH |
132 | for (i = 0; i < other_cmds.cnt; i++) |
133 | fprintf(stderr, " %s", other_cmds.names[i]->name); | |
134 | fprintf(stderr, ".\n"); | |
135 | } | |
87091b49 MV |
136 | exit(1); |
137 | } | |
138 | ||
19d4b416 | 139 | ret = xcalloc(1, sizeof(struct strategy)); |
87091b49 | 140 | ret->name = xstrdup(name); |
52b48ef1 | 141 | ret->attr = NO_TRIVIAL; |
87091b49 | 142 | return ret; |
1c7b76be MV |
143 | } |
144 | ||
145 | static void append_strategy(struct strategy *s) | |
146 | { | |
147 | ALLOC_GROW(use_strategies, use_strategies_nr + 1, use_strategies_alloc); | |
148 | use_strategies[use_strategies_nr++] = s; | |
149 | } | |
150 | ||
151 | static int option_parse_strategy(const struct option *opt, | |
152 | const char *name, int unset) | |
153 | { | |
1c7b76be MV |
154 | if (unset) |
155 | return 0; | |
156 | ||
1719b5e4 | 157 | append_strategy(get_strategy(name)); |
1c7b76be MV |
158 | return 0; |
159 | } | |
160 | ||
8cc5b290 AP |
161 | static int option_parse_x(const struct option *opt, |
162 | const char *arg, int unset) | |
163 | { | |
164 | if (unset) | |
165 | return 0; | |
166 | ||
167 | ALLOC_GROW(xopts, xopts_nr + 1, xopts_alloc); | |
168 | xopts[xopts_nr++] = xstrdup(arg); | |
169 | return 0; | |
170 | } | |
171 | ||
1c7b76be MV |
172 | static int option_parse_n(const struct option *opt, |
173 | const char *arg, int unset) | |
174 | { | |
175 | show_diffstat = unset; | |
176 | return 0; | |
177 | } | |
178 | ||
179 | static struct option builtin_merge_options[] = { | |
180 | { OPTION_CALLBACK, 'n', NULL, NULL, NULL, | |
181 | "do not show a diffstat at the end of the merge", | |
182 | PARSE_OPT_NOARG, option_parse_n }, | |
183 | OPT_BOOLEAN(0, "stat", &show_diffstat, | |
184 | "show a diffstat at the end of the merge"), | |
185 | OPT_BOOLEAN(0, "summary", &show_diffstat, "(synonym to --stat)"), | |
96e9420c RR |
186 | { OPTION_INTEGER, 0, "log", &shortlog_len, "n", |
187 | "add (at most <n>) entries from shortlog to merge commit message", | |
188 | PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN }, | |
1c7b76be MV |
189 | OPT_BOOLEAN(0, "squash", &squash, |
190 | "create a single commit instead of doing a merge"), | |
191 | OPT_BOOLEAN(0, "commit", &option_commit, | |
192 | "perform a commit if the merge succeeds (default)"), | |
193 | OPT_BOOLEAN(0, "ff", &allow_fast_forward, | |
a75d7b54 | 194 | "allow fast-forward (default)"), |
13474835 | 195 | OPT_BOOLEAN(0, "ff-only", &fast_forward_only, |
4d8c3258 | 196 | "abort if fast-forward is not possible"), |
cb6020bb | 197 | OPT_RERERE_AUTOUPDATE(&allow_rerere_auto), |
1c7b76be MV |
198 | OPT_CALLBACK('s', "strategy", &use_strategies, "strategy", |
199 | "merge strategy to use", option_parse_strategy), | |
8cc5b290 AP |
200 | OPT_CALLBACK('X', "strategy-option", &xopts, "option=value", |
201 | "option for selected merge strategy", option_parse_x), | |
23c6a803 | 202 | OPT_CALLBACK('m', "message", &merge_msg, "message", |
3f406175 | 203 | "merge commit message (for a non-fast-forward merge)", |
1c7b76be | 204 | option_parse_message), |
7f87aff2 | 205 | OPT__VERBOSITY(&verbosity), |
35d2fffd JH |
206 | OPT_BOOLEAN(0, "abort", &abort_current_merge, |
207 | "abort the current in-progress merge"), | |
99bfc669 | 208 | OPT_SET_INT(0, "progress", &show_progress, "force progress reporting", 1), |
1c7b76be MV |
209 | OPT_END() |
210 | }; | |
211 | ||
212 | /* Cleans up metadata that is uninteresting after a succeeded merge. */ | |
213 | static void drop_save(void) | |
214 | { | |
215 | unlink(git_path("MERGE_HEAD")); | |
216 | unlink(git_path("MERGE_MSG")); | |
cf10f9fd | 217 | unlink(git_path("MERGE_MODE")); |
1c7b76be MV |
218 | } |
219 | ||
220 | static void save_state(void) | |
221 | { | |
222 | int len; | |
223 | struct child_process cp; | |
224 | struct strbuf buffer = STRBUF_INIT; | |
225 | const char *argv[] = {"stash", "create", NULL}; | |
226 | ||
227 | memset(&cp, 0, sizeof(cp)); | |
228 | cp.argv = argv; | |
229 | cp.out = -1; | |
230 | cp.git_cmd = 1; | |
231 | ||
232 | if (start_command(&cp)) | |
bacec478 | 233 | die(_("could not run stash.")); |
1c7b76be MV |
234 | len = strbuf_read(&buffer, cp.out, 1024); |
235 | close(cp.out); | |
236 | ||
237 | if (finish_command(&cp) || len < 0) | |
bacec478 | 238 | die(_("stash failed")); |
1c7b76be MV |
239 | else if (!len) |
240 | return; | |
241 | strbuf_setlen(&buffer, buffer.len-1); | |
242 | if (get_sha1(buffer.buf, stash)) | |
bacec478 | 243 | die(_("not a valid object: %s"), buffer.buf); |
1c7b76be MV |
244 | } |
245 | ||
172b6428 CB |
246 | static void read_empty(unsigned const char *sha1, int verbose) |
247 | { | |
248 | int i = 0; | |
249 | const char *args[7]; | |
250 | ||
251 | args[i++] = "read-tree"; | |
252 | if (verbose) | |
253 | args[i++] = "-v"; | |
254 | args[i++] = "-m"; | |
255 | args[i++] = "-u"; | |
256 | args[i++] = EMPTY_TREE_SHA1_HEX; | |
257 | args[i++] = sha1_to_hex(sha1); | |
258 | args[i] = NULL; | |
259 | ||
260 | if (run_command_v_opt(args, RUN_GIT_CMD)) | |
bacec478 | 261 | die(_("read-tree failed")); |
172b6428 CB |
262 | } |
263 | ||
1c7b76be MV |
264 | static void reset_hard(unsigned const char *sha1, int verbose) |
265 | { | |
266 | int i = 0; | |
267 | const char *args[6]; | |
268 | ||
269 | args[i++] = "read-tree"; | |
270 | if (verbose) | |
271 | args[i++] = "-v"; | |
272 | args[i++] = "--reset"; | |
273 | args[i++] = "-u"; | |
274 | args[i++] = sha1_to_hex(sha1); | |
275 | args[i] = NULL; | |
276 | ||
277 | if (run_command_v_opt(args, RUN_GIT_CMD)) | |
bacec478 | 278 | die(_("read-tree failed")); |
1c7b76be MV |
279 | } |
280 | ||
281 | static void restore_state(void) | |
282 | { | |
f285a2d7 | 283 | struct strbuf sb = STRBUF_INIT; |
1c7b76be MV |
284 | const char *args[] = { "stash", "apply", NULL, NULL }; |
285 | ||
286 | if (is_null_sha1(stash)) | |
287 | return; | |
288 | ||
289 | reset_hard(head, 1); | |
290 | ||
1c7b76be MV |
291 | args[2] = sha1_to_hex(stash); |
292 | ||
293 | /* | |
294 | * It is OK to ignore error here, for example when there was | |
295 | * nothing to restore. | |
296 | */ | |
297 | run_command_v_opt(args, RUN_GIT_CMD); | |
298 | ||
299 | strbuf_release(&sb); | |
300 | refresh_cache(REFRESH_QUIET); | |
301 | } | |
302 | ||
303 | /* This is called when no merge was necessary. */ | |
304 | static void finish_up_to_date(const char *msg) | |
305 | { | |
7f87aff2 | 306 | if (verbosity >= 0) |
bacec478 | 307 | printf("%s%s\n", squash ? _(" (nothing to squash)") : "", msg); |
1c7b76be MV |
308 | drop_save(); |
309 | } | |
310 | ||
311 | static void squash_message(void) | |
312 | { | |
313 | struct rev_info rev; | |
314 | struct commit *commit; | |
f285a2d7 | 315 | struct strbuf out = STRBUF_INIT; |
1c7b76be MV |
316 | struct commit_list *j; |
317 | int fd; | |
dd2e794a | 318 | struct pretty_print_context ctx = {0}; |
1c7b76be | 319 | |
bacec478 | 320 | printf(_("Squash commit -- not updating HEAD\n")); |
1c7b76be MV |
321 | fd = open(git_path("SQUASH_MSG"), O_WRONLY | O_CREAT, 0666); |
322 | if (fd < 0) | |
bacec478 | 323 | die_errno(_("Could not write to '%s'"), git_path("SQUASH_MSG")); |
1c7b76be MV |
324 | |
325 | init_revisions(&rev, NULL); | |
326 | rev.ignore_merges = 1; | |
327 | rev.commit_format = CMIT_FMT_MEDIUM; | |
328 | ||
329 | commit = lookup_commit(head); | |
330 | commit->object.flags |= UNINTERESTING; | |
331 | add_pending_object(&rev, &commit->object, NULL); | |
332 | ||
333 | for (j = remoteheads; j; j = j->next) | |
334 | add_pending_object(&rev, &j->item->object, NULL); | |
335 | ||
336 | setup_revisions(0, NULL, &rev, NULL); | |
337 | if (prepare_revision_walk(&rev)) | |
bacec478 | 338 | die(_("revision walk setup failed")); |
1c7b76be | 339 | |
dd2e794a TR |
340 | ctx.abbrev = rev.abbrev; |
341 | ctx.date_mode = rev.date_mode; | |
342 | ||
1c7b76be MV |
343 | strbuf_addstr(&out, "Squashed commit of the following:\n"); |
344 | while ((commit = get_revision(&rev)) != NULL) { | |
345 | strbuf_addch(&out, '\n'); | |
346 | strbuf_addf(&out, "commit %s\n", | |
347 | sha1_to_hex(commit->object.sha1)); | |
dd2e794a | 348 | pretty_print_commit(rev.commit_format, commit, &out, &ctx); |
1c7b76be | 349 | } |
47d32af2 | 350 | if (write(fd, out.buf, out.len) < 0) |
bacec478 | 351 | die_errno(_("Writing SQUASH_MSG")); |
47d32af2 | 352 | if (close(fd)) |
bacec478 | 353 | die_errno(_("Finishing SQUASH_MSG")); |
1c7b76be MV |
354 | strbuf_release(&out); |
355 | } | |
356 | ||
1c7b76be MV |
357 | static void finish(const unsigned char *new_head, const char *msg) |
358 | { | |
f285a2d7 | 359 | struct strbuf reflog_message = STRBUF_INIT; |
1c7b76be | 360 | |
1c7b76be MV |
361 | if (!msg) |
362 | strbuf_addstr(&reflog_message, getenv("GIT_REFLOG_ACTION")); | |
363 | else { | |
7f87aff2 TA |
364 | if (verbosity >= 0) |
365 | printf("%s\n", msg); | |
1c7b76be MV |
366 | strbuf_addf(&reflog_message, "%s: %s", |
367 | getenv("GIT_REFLOG_ACTION"), msg); | |
368 | } | |
369 | if (squash) { | |
370 | squash_message(); | |
371 | } else { | |
7f87aff2 | 372 | if (verbosity >= 0 && !merge_msg.len) |
bacec478 | 373 | printf(_("No merge message -- not updating HEAD\n")); |
1c7b76be MV |
374 | else { |
375 | const char *argv_gc_auto[] = { "gc", "--auto", NULL }; | |
376 | update_ref(reflog_message.buf, "HEAD", | |
377 | new_head, head, 0, | |
378 | DIE_ON_ERR); | |
379 | /* | |
380 | * We ignore errors in 'gc --auto', since the | |
381 | * user should see them. | |
382 | */ | |
383 | run_command_v_opt(argv_gc_auto, RUN_GIT_CMD); | |
384 | } | |
385 | } | |
386 | if (new_head && show_diffstat) { | |
387 | struct diff_options opts; | |
388 | diff_setup(&opts); | |
389 | opts.output_format |= | |
390 | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT; | |
391 | opts.detect_rename = DIFF_DETECT_RENAME; | |
392 | if (diff_use_color_default > 0) | |
393 | DIFF_OPT_SET(&opts, COLOR_DIFF); | |
394 | if (diff_setup_done(&opts) < 0) | |
bacec478 | 395 | die(_("diff_setup_done failed")); |
1c7b76be MV |
396 | diff_tree_sha1(head, new_head, "", &opts); |
397 | diffcore_std(&opts); | |
398 | diff_flush(&opts); | |
399 | } | |
400 | ||
401 | /* Run a post-merge hook */ | |
ae98a008 | 402 | run_hook(NULL, "post-merge", squash ? "1" : "0", NULL); |
1c7b76be MV |
403 | |
404 | strbuf_release(&reflog_message); | |
405 | } | |
406 | ||
407 | /* Get the name for the merge commit's message. */ | |
408 | static void merge_name(const char *remote, struct strbuf *msg) | |
409 | { | |
410 | struct object *remote_head; | |
411 | unsigned char branch_head[20], buf_sha[20]; | |
f285a2d7 | 412 | struct strbuf buf = STRBUF_INIT; |
c9717ee9 | 413 | struct strbuf bname = STRBUF_INIT; |
1c7b76be | 414 | const char *ptr; |
751c5974 | 415 | char *found_ref; |
1c7b76be MV |
416 | int len, early; |
417 | ||
a552de75 JH |
418 | strbuf_branchname(&bname, remote); |
419 | remote = bname.buf; | |
c9717ee9 | 420 | |
1c7b76be MV |
421 | memset(branch_head, 0, sizeof(branch_head)); |
422 | remote_head = peel_to_type(remote, 0, NULL, OBJ_COMMIT); | |
423 | if (!remote_head) | |
bacec478 | 424 | die(_("'%s' does not point to a commit"), remote); |
1c7b76be | 425 | |
751c5974 JK |
426 | if (dwim_ref(remote, strlen(remote), branch_head, &found_ref) > 0) { |
427 | if (!prefixcmp(found_ref, "refs/heads/")) { | |
428 | strbuf_addf(msg, "%s\t\tbranch '%s' of .\n", | |
429 | sha1_to_hex(branch_head), remote); | |
430 | goto cleanup; | |
431 | } | |
69a8b7c7 | 432 | if (!prefixcmp(found_ref, "refs/remotes/")) { |
13931236 | 433 | strbuf_addf(msg, "%s\t\tremote-tracking branch '%s' of .\n", |
69a8b7c7 JK |
434 | sha1_to_hex(branch_head), remote); |
435 | goto cleanup; | |
436 | } | |
1c7b76be MV |
437 | } |
438 | ||
439 | /* See if remote matches <name>^^^.. or <name>~<number> */ | |
440 | for (len = 0, ptr = remote + strlen(remote); | |
441 | remote < ptr && ptr[-1] == '^'; | |
442 | ptr--) | |
443 | len++; | |
444 | if (len) | |
445 | early = 1; | |
446 | else { | |
447 | early = 0; | |
448 | ptr = strrchr(remote, '~'); | |
449 | if (ptr) { | |
450 | int seen_nonzero = 0; | |
451 | ||
452 | len++; /* count ~ */ | |
453 | while (*++ptr && isdigit(*ptr)) { | |
454 | seen_nonzero |= (*ptr != '0'); | |
455 | len++; | |
456 | } | |
457 | if (*ptr) | |
458 | len = 0; /* not ...~<number> */ | |
459 | else if (seen_nonzero) | |
460 | early = 1; | |
461 | else if (len == 1) | |
462 | early = 1; /* "name~" is "name~1"! */ | |
463 | } | |
464 | } | |
465 | if (len) { | |
466 | struct strbuf truname = STRBUF_INIT; | |
467 | strbuf_addstr(&truname, "refs/heads/"); | |
468 | strbuf_addstr(&truname, remote); | |
9b6bf4d5 | 469 | strbuf_setlen(&truname, truname.len - len); |
b81f925f | 470 | if (resolve_ref(truname.buf, buf_sha, 1, NULL)) { |
1c7b76be MV |
471 | strbuf_addf(msg, |
472 | "%s\t\tbranch '%s'%s of .\n", | |
473 | sha1_to_hex(remote_head->sha1), | |
9b6bf4d5 | 474 | truname.buf + 11, |
1c7b76be | 475 | (early ? " (early part)" : "")); |
c9717ee9 JH |
476 | strbuf_release(&truname); |
477 | goto cleanup; | |
1c7b76be MV |
478 | } |
479 | } | |
480 | ||
481 | if (!strcmp(remote, "FETCH_HEAD") && | |
482 | !access(git_path("FETCH_HEAD"), R_OK)) { | |
483 | FILE *fp; | |
f285a2d7 | 484 | struct strbuf line = STRBUF_INIT; |
1c7b76be MV |
485 | char *ptr; |
486 | ||
1c7b76be MV |
487 | fp = fopen(git_path("FETCH_HEAD"), "r"); |
488 | if (!fp) | |
bacec478 | 489 | die_errno(_("could not open '%s' for reading"), |
d824cbba | 490 | git_path("FETCH_HEAD")); |
1c7b76be MV |
491 | strbuf_getline(&line, fp, '\n'); |
492 | fclose(fp); | |
493 | ptr = strstr(line.buf, "\tnot-for-merge\t"); | |
494 | if (ptr) | |
495 | strbuf_remove(&line, ptr-line.buf+1, 13); | |
496 | strbuf_addbuf(msg, &line); | |
497 | strbuf_release(&line); | |
c9717ee9 | 498 | goto cleanup; |
1c7b76be MV |
499 | } |
500 | strbuf_addf(msg, "%s\t\tcommit '%s'\n", | |
501 | sha1_to_hex(remote_head->sha1), remote); | |
c9717ee9 JH |
502 | cleanup: |
503 | strbuf_release(&buf); | |
504 | strbuf_release(&bname); | |
1c7b76be MV |
505 | } |
506 | ||
0d8fc3ef JH |
507 | static void parse_branch_merge_options(char *bmo) |
508 | { | |
509 | const char **argv; | |
510 | int argc; | |
511 | ||
512 | if (!bmo) | |
513 | return; | |
514 | argc = split_cmdline(bmo, &argv); | |
515 | if (argc < 0) | |
c7fe5b61 JH |
516 | die(_("Bad branch.%s.mergeoptions string: %s"), branch, |
517 | split_cmdline_strerror(argc)); | |
0d8fc3ef JH |
518 | argv = xrealloc(argv, sizeof(*argv) * (argc + 2)); |
519 | memmove(argv + 1, argv, sizeof(*argv) * (argc + 1)); | |
520 | argc++; | |
521 | argv[0] = "branch.*.mergeoptions"; | |
522 | parse_options(argc, argv, NULL, builtin_merge_options, | |
523 | builtin_merge_usage, 0); | |
524 | free(argv); | |
525 | } | |
526 | ||
186458b1 | 527 | static int git_merge_config(const char *k, const char *v, void *cb) |
1c7b76be MV |
528 | { |
529 | if (branch && !prefixcmp(k, "branch.") && | |
530 | !prefixcmp(k + 7, branch) && | |
531 | !strcmp(k + 7 + strlen(branch), ".mergeoptions")) { | |
0d8fc3ef JH |
532 | free(branch_mergeoptions); |
533 | branch_mergeoptions = xstrdup(v); | |
534 | return 0; | |
1c7b76be MV |
535 | } |
536 | ||
537 | if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat")) | |
538 | show_diffstat = git_config_bool(k, v); | |
539 | else if (!strcmp(k, "pull.twohead")) | |
540 | return git_config_string(&pull_twohead, k, v); | |
541 | else if (!strcmp(k, "pull.octopus")) | |
542 | return git_config_string(&pull_octopus, k, v); | |
7610fa57 JN |
543 | else if (!strcmp(k, "merge.renormalize")) |
544 | option_renormalize = git_config_bool(k, v); | |
96e9420c | 545 | else if (!strcmp(k, "merge.log") || !strcmp(k, "merge.summary")) { |
bda3b8ff RR |
546 | int is_bool; |
547 | shortlog_len = git_config_bool_or_int(k, v, &is_bool); | |
548 | if (!is_bool && shortlog_len < 0) | |
bacec478 | 549 | return error(_("%s: negative length %s"), k, v); |
bda3b8ff RR |
550 | if (is_bool && shortlog_len) |
551 | shortlog_len = DEFAULT_MERGE_LOG_LEN; | |
96e9420c | 552 | return 0; |
f23e8dec JH |
553 | } else if (!strcmp(k, "merge.ff")) { |
554 | int boolval = git_config_maybe_bool(k, v); | |
555 | if (0 <= boolval) { | |
556 | allow_fast_forward = boolval; | |
557 | } else if (v && !strcmp(v, "only")) { | |
558 | allow_fast_forward = 1; | |
559 | fast_forward_only = 1; | |
560 | } /* do not barf on values from future versions of git */ | |
561 | return 0; | |
93e535a5 JH |
562 | } else if (!strcmp(k, "merge.defaulttoupstream")) { |
563 | default_to_upstream = git_config_bool(k, v); | |
564 | return 0; | |
96e9420c | 565 | } |
1c7b76be MV |
566 | return git_diff_ui_config(k, v, cb); |
567 | } | |
568 | ||
569 | static int read_tree_trivial(unsigned char *common, unsigned char *head, | |
570 | unsigned char *one) | |
571 | { | |
572 | int i, nr_trees = 0; | |
573 | struct tree *trees[MAX_UNPACK_TREES]; | |
574 | struct tree_desc t[MAX_UNPACK_TREES]; | |
575 | struct unpack_trees_options opts; | |
576 | ||
577 | memset(&opts, 0, sizeof(opts)); | |
578 | opts.head_idx = 2; | |
579 | opts.src_index = &the_index; | |
580 | opts.dst_index = &the_index; | |
581 | opts.update = 1; | |
582 | opts.verbose_update = 1; | |
583 | opts.trivial_merges_only = 1; | |
584 | opts.merge = 1; | |
585 | trees[nr_trees] = parse_tree_indirect(common); | |
586 | if (!trees[nr_trees++]) | |
587 | return -1; | |
588 | trees[nr_trees] = parse_tree_indirect(head); | |
589 | if (!trees[nr_trees++]) | |
590 | return -1; | |
591 | trees[nr_trees] = parse_tree_indirect(one); | |
592 | if (!trees[nr_trees++]) | |
593 | return -1; | |
594 | opts.fn = threeway_merge; | |
595 | cache_tree_free(&active_cache_tree); | |
596 | for (i = 0; i < nr_trees; i++) { | |
597 | parse_tree(trees[i]); | |
598 | init_tree_desc(t+i, trees[i]->buffer, trees[i]->size); | |
599 | } | |
600 | if (unpack_trees(nr_trees, t, &opts)) | |
601 | return -1; | |
602 | return 0; | |
603 | } | |
604 | ||
605 | static void write_tree_trivial(unsigned char *sha1) | |
606 | { | |
607 | if (write_cache_as_tree(sha1, 0, NULL)) | |
bacec478 | 608 | die(_("git write-tree failed to write a tree")); |
1c7b76be MV |
609 | } |
610 | ||
16180734 JK |
611 | static const char *merge_argument(struct commit *commit) |
612 | { | |
613 | if (commit) | |
614 | return sha1_to_hex(commit->object.sha1); | |
615 | else | |
616 | return EMPTY_TREE_SHA1_HEX; | |
617 | } | |
618 | ||
67ac1e1d JN |
619 | int try_merge_command(const char *strategy, size_t xopts_nr, |
620 | const char **xopts, struct commit_list *common, | |
c674d052 | 621 | const char *head_arg, struct commit_list *remotes) |
1c7b76be MV |
622 | { |
623 | const char **args; | |
8cc5b290 | 624 | int i = 0, x = 0, ret; |
1c7b76be | 625 | struct commit_list *j; |
f285a2d7 | 626 | struct strbuf buf = STRBUF_INIT; |
3f9083cd CC |
627 | |
628 | args = xmalloc((4 + xopts_nr + commit_list_count(common) + | |
629 | commit_list_count(remotes)) * sizeof(char *)); | |
630 | strbuf_addf(&buf, "merge-%s", strategy); | |
631 | args[i++] = buf.buf; | |
632 | for (x = 0; x < xopts_nr; x++) { | |
633 | char *s = xmalloc(strlen(xopts[x])+2+1); | |
634 | strcpy(s, "--"); | |
635 | strcpy(s+2, xopts[x]); | |
636 | args[i++] = s; | |
637 | } | |
638 | for (j = common; j; j = j->next) | |
16180734 | 639 | args[i++] = xstrdup(merge_argument(j->item)); |
3f9083cd CC |
640 | args[i++] = "--"; |
641 | args[i++] = head_arg; | |
642 | for (j = remotes; j; j = j->next) | |
16180734 | 643 | args[i++] = xstrdup(merge_argument(j->item)); |
3f9083cd CC |
644 | args[i] = NULL; |
645 | ret = run_command_v_opt(args, RUN_GIT_CMD); | |
646 | strbuf_release(&buf); | |
647 | i = 1; | |
648 | for (x = 0; x < xopts_nr; x++) | |
649 | free((void *)args[i++]); | |
650 | for (j = common; j; j = j->next) | |
651 | free((void *)args[i++]); | |
652 | i += 2; | |
653 | for (j = remotes; j; j = j->next) | |
654 | free((void *)args[i++]); | |
655 | free(args); | |
656 | discard_cache(); | |
657 | if (read_cache() < 0) | |
bacec478 | 658 | die(_("failed to read the cache")); |
3f9083cd CC |
659 | resolve_undo_clear(); |
660 | ||
661 | return ret; | |
662 | } | |
663 | ||
664 | static int try_merge_strategy(const char *strategy, struct commit_list *common, | |
665 | const char *head_arg) | |
666 | { | |
668f26ff MV |
667 | int index_fd; |
668 | struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); | |
669 | ||
670 | index_fd = hold_locked_index(lock, 1); | |
671 | refresh_cache(REFRESH_QUIET); | |
672 | if (active_cache_changed && | |
673 | (write_cache(index_fd, active_cache, active_nr) || | |
674 | commit_locked_index(lock))) | |
bacec478 | 675 | return error(_("Unable to write index.")); |
668f26ff | 676 | rollback_lock_file(lock); |
1c7b76be | 677 | |
18668f53 | 678 | if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) { |
3f9083cd | 679 | int clean, x; |
18668f53 MV |
680 | struct commit *result; |
681 | struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); | |
682 | int index_fd; | |
683 | struct commit_list *reversed = NULL; | |
684 | struct merge_options o; | |
3f9083cd | 685 | struct commit_list *j; |
18668f53 MV |
686 | |
687 | if (remoteheads->next) { | |
bacec478 | 688 | error(_("Not handling anything other than two heads merge.")); |
18668f53 MV |
689 | return 2; |
690 | } | |
691 | ||
692 | init_merge_options(&o); | |
693 | if (!strcmp(strategy, "subtree")) | |
85e51b78 | 694 | o.subtree_shift = ""; |
8cc5b290 | 695 | |
7610fa57 | 696 | o.renormalize = option_renormalize; |
99bfc669 JK |
697 | o.show_rename_progress = |
698 | show_progress == -1 ? isatty(2) : show_progress; | |
7610fa57 | 699 | |
635a7bb1 JN |
700 | for (x = 0; x < xopts_nr; x++) |
701 | if (parse_merge_opt(&o, xopts[x])) | |
bacec478 | 702 | die(_("Unknown option for merge-recursive: -X%s"), xopts[x]); |
18668f53 MV |
703 | |
704 | o.branch1 = head_arg; | |
705 | o.branch2 = remoteheads->item->util; | |
706 | ||
707 | for (j = common; j; j = j->next) | |
708 | commit_list_insert(j->item, &reversed); | |
709 | ||
710 | index_fd = hold_locked_index(lock, 1); | |
711 | clean = merge_recursive(&o, lookup_commit(head), | |
712 | remoteheads->item, reversed, &result); | |
713 | if (active_cache_changed && | |
714 | (write_cache(index_fd, active_cache, active_nr) || | |
715 | commit_locked_index(lock))) | |
bacec478 | 716 | die (_("unable to write %s"), get_index_file()); |
42716660 | 717 | rollback_lock_file(lock); |
18668f53 MV |
718 | return clean ? 0 : 1; |
719 | } else { | |
67ac1e1d JN |
720 | return try_merge_command(strategy, xopts_nr, xopts, |
721 | common, head_arg, remoteheads); | |
18668f53 | 722 | } |
1c7b76be MV |
723 | } |
724 | ||
725 | static void count_diff_files(struct diff_queue_struct *q, | |
726 | struct diff_options *opt, void *data) | |
727 | { | |
728 | int *count = data; | |
729 | ||
730 | (*count) += q->nr; | |
731 | } | |
732 | ||
733 | static int count_unmerged_entries(void) | |
734 | { | |
1c7b76be MV |
735 | int i, ret = 0; |
736 | ||
be6ff819 JH |
737 | for (i = 0; i < active_nr; i++) |
738 | if (ce_stage(active_cache[i])) | |
1c7b76be MV |
739 | ret++; |
740 | ||
741 | return ret; | |
742 | } | |
743 | ||
cac42b26 | 744 | int checkout_fast_forward(const unsigned char *head, const unsigned char *remote) |
1c7b76be MV |
745 | { |
746 | struct tree *trees[MAX_UNPACK_TREES]; | |
747 | struct unpack_trees_options opts; | |
748 | struct tree_desc t[MAX_UNPACK_TREES]; | |
749 | int i, fd, nr_trees = 0; | |
750 | struct dir_struct dir; | |
751 | struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file)); | |
752 | ||
9ca8f607 | 753 | refresh_cache(REFRESH_QUIET); |
1c7b76be MV |
754 | |
755 | fd = hold_locked_index(lock_file, 1); | |
756 | ||
757 | memset(&trees, 0, sizeof(trees)); | |
758 | memset(&opts, 0, sizeof(opts)); | |
759 | memset(&t, 0, sizeof(t)); | |
5d2299de | 760 | memset(&dir, 0, sizeof(dir)); |
7c4c97c0 | 761 | dir.flags |= DIR_SHOW_IGNORED; |
1c7b76be MV |
762 | dir.exclude_per_dir = ".gitignore"; |
763 | opts.dir = &dir; | |
764 | ||
765 | opts.head_idx = 1; | |
766 | opts.src_index = &the_index; | |
767 | opts.dst_index = &the_index; | |
768 | opts.update = 1; | |
769 | opts.verbose_update = 1; | |
770 | opts.merge = 1; | |
771 | opts.fn = twoway_merge; | |
e294030f | 772 | setup_unpack_trees_porcelain(&opts, "merge"); |
1c7b76be MV |
773 | |
774 | trees[nr_trees] = parse_tree_indirect(head); | |
775 | if (!trees[nr_trees++]) | |
776 | return -1; | |
777 | trees[nr_trees] = parse_tree_indirect(remote); | |
778 | if (!trees[nr_trees++]) | |
779 | return -1; | |
780 | for (i = 0; i < nr_trees; i++) { | |
781 | parse_tree(trees[i]); | |
782 | init_tree_desc(t+i, trees[i]->buffer, trees[i]->size); | |
783 | } | |
784 | if (unpack_trees(nr_trees, t, &opts)) | |
785 | return -1; | |
786 | if (write_cache(fd, active_cache, active_nr) || | |
787 | commit_locked_index(lock_file)) | |
bacec478 | 788 | die(_("unable to write new index file")); |
1c7b76be MV |
789 | return 0; |
790 | } | |
791 | ||
792 | static void split_merge_strategies(const char *string, struct strategy **list, | |
793 | int *nr, int *alloc) | |
794 | { | |
795 | char *p, *q, *buf; | |
796 | ||
797 | if (!string) | |
798 | return; | |
799 | ||
800 | buf = xstrdup(string); | |
801 | q = buf; | |
802 | for (;;) { | |
803 | p = strchr(q, ' '); | |
804 | if (!p) { | |
805 | ALLOC_GROW(*list, *nr + 1, *alloc); | |
806 | (*list)[(*nr)++].name = xstrdup(q); | |
807 | free(buf); | |
808 | return; | |
809 | } else { | |
810 | *p = '\0'; | |
811 | ALLOC_GROW(*list, *nr + 1, *alloc); | |
812 | (*list)[(*nr)++].name = xstrdup(q); | |
813 | q = ++p; | |
814 | } | |
815 | } | |
816 | } | |
817 | ||
818 | static void add_strategies(const char *string, unsigned attr) | |
819 | { | |
820 | struct strategy *list = NULL; | |
821 | int list_alloc = 0, list_nr = 0, i; | |
822 | ||
823 | memset(&list, 0, sizeof(list)); | |
824 | split_merge_strategies(string, &list, &list_nr, &list_alloc); | |
1719b5e4 MV |
825 | if (list) { |
826 | for (i = 0; i < list_nr; i++) | |
827 | append_strategy(get_strategy(list[i].name)); | |
1c7b76be MV |
828 | return; |
829 | } | |
830 | for (i = 0; i < ARRAY_SIZE(all_strategy); i++) | |
831 | if (all_strategy[i].attr & attr) | |
832 | append_strategy(&all_strategy[i]); | |
833 | ||
834 | } | |
835 | ||
65969d43 JS |
836 | static void write_merge_msg(void) |
837 | { | |
838 | int fd = open(git_path("MERGE_MSG"), O_WRONLY | O_CREAT, 0666); | |
839 | if (fd < 0) | |
6c80cd29 | 840 | die_errno(_("Could not open '%s' for writing"), |
65969d43 JS |
841 | git_path("MERGE_MSG")); |
842 | if (write_in_full(fd, merge_msg.buf, merge_msg.len) != merge_msg.len) | |
6c80cd29 | 843 | die_errno(_("Could not write to '%s'"), git_path("MERGE_MSG")); |
65969d43 JS |
844 | close(fd); |
845 | } | |
846 | ||
847 | static void read_merge_msg(void) | |
848 | { | |
849 | strbuf_reset(&merge_msg); | |
850 | if (strbuf_read_file(&merge_msg, git_path("MERGE_MSG"), 0) < 0) | |
650ed9db | 851 | die_errno(_("Could not read from '%s'"), git_path("MERGE_MSG")); |
65969d43 JS |
852 | } |
853 | ||
854 | static void run_prepare_commit_msg(void) | |
855 | { | |
856 | write_merge_msg(); | |
857 | run_hook(get_index_file(), "prepare-commit-msg", | |
858 | git_path("MERGE_MSG"), "merge", NULL, NULL); | |
859 | read_merge_msg(); | |
860 | } | |
861 | ||
1c7b76be MV |
862 | static int merge_trivial(void) |
863 | { | |
864 | unsigned char result_tree[20], result_commit[20]; | |
36e40535 | 865 | struct commit_list *parent = xmalloc(sizeof(*parent)); |
1c7b76be MV |
866 | |
867 | write_tree_trivial(result_tree); | |
157efde1 | 868 | printf(_("Wonderful.\n")); |
446247db | 869 | parent->item = lookup_commit(head); |
36e40535 | 870 | parent->next = xmalloc(sizeof(*parent->next)); |
446247db JH |
871 | parent->next->item = remoteheads->item; |
872 | parent->next->next = NULL; | |
65969d43 | 873 | run_prepare_commit_msg(); |
ee20a129 | 874 | commit_tree(merge_msg.buf, result_tree, parent, result_commit, NULL); |
1c7b76be MV |
875 | finish(result_commit, "In-index merge"); |
876 | drop_save(); | |
877 | return 0; | |
878 | } | |
879 | ||
880 | static int finish_automerge(struct commit_list *common, | |
881 | unsigned char *result_tree, | |
882 | const char *wt_strategy) | |
883 | { | |
884 | struct commit_list *parents = NULL, *j; | |
885 | struct strbuf buf = STRBUF_INIT; | |
886 | unsigned char result_commit[20]; | |
887 | ||
888 | free_commit_list(common); | |
889 | if (allow_fast_forward) { | |
890 | parents = remoteheads; | |
891 | commit_list_insert(lookup_commit(head), &parents); | |
892 | parents = reduce_heads(parents); | |
893 | } else { | |
894 | struct commit_list **pptr = &parents; | |
895 | ||
896 | pptr = &commit_list_insert(lookup_commit(head), | |
897 | pptr)->next; | |
898 | for (j = remoteheads; j; j = j->next) | |
899 | pptr = &commit_list_insert(j->item, pptr)->next; | |
900 | } | |
901 | free_commit_list(remoteheads); | |
902 | strbuf_addch(&merge_msg, '\n'); | |
65969d43 | 903 | run_prepare_commit_msg(); |
ee20a129 | 904 | commit_tree(merge_msg.buf, result_tree, parents, result_commit, NULL); |
1c7b76be MV |
905 | strbuf_addf(&buf, "Merge made by %s.", wt_strategy); |
906 | finish(result_commit, buf.buf); | |
907 | strbuf_release(&buf); | |
908 | drop_save(); | |
909 | return 0; | |
910 | } | |
911 | ||
7610fa57 | 912 | static int suggest_conflicts(int renormalizing) |
1c7b76be MV |
913 | { |
914 | FILE *fp; | |
915 | int pos; | |
916 | ||
917 | fp = fopen(git_path("MERGE_MSG"), "a"); | |
918 | if (!fp) | |
bacec478 | 919 | die_errno(_("Could not open '%s' for writing"), |
0721c314 | 920 | git_path("MERGE_MSG")); |
1c7b76be MV |
921 | fprintf(fp, "\nConflicts:\n"); |
922 | for (pos = 0; pos < active_nr; pos++) { | |
923 | struct cache_entry *ce = active_cache[pos]; | |
924 | ||
925 | if (ce_stage(ce)) { | |
926 | fprintf(fp, "\t%s\n", ce->name); | |
927 | while (pos + 1 < active_nr && | |
928 | !strcmp(ce->name, | |
929 | active_cache[pos + 1]->name)) | |
930 | pos++; | |
931 | } | |
932 | } | |
933 | fclose(fp); | |
cb6020bb | 934 | rerere(allow_rerere_auto); |
bacec478 ÆAB |
935 | printf(_("Automatic merge failed; " |
936 | "fix conflicts and then commit the result.\n")); | |
1c7b76be MV |
937 | return 1; |
938 | } | |
939 | ||
940 | static struct commit *is_old_style_invocation(int argc, const char **argv) | |
941 | { | |
942 | struct commit *second_token = NULL; | |
76bf488e | 943 | if (argc > 2) { |
1c7b76be MV |
944 | unsigned char second_sha1[20]; |
945 | ||
946 | if (get_sha1(argv[1], second_sha1)) | |
947 | return NULL; | |
948 | second_token = lookup_commit_reference_gently(second_sha1, 0); | |
949 | if (!second_token) | |
bacec478 | 950 | die(_("'%s' is not a commit"), argv[1]); |
1c7b76be MV |
951 | if (hashcmp(second_token->object.sha1, head)) |
952 | return NULL; | |
953 | } | |
954 | return second_token; | |
955 | } | |
956 | ||
957 | static int evaluate_result(void) | |
958 | { | |
959 | int cnt = 0; | |
960 | struct rev_info rev; | |
961 | ||
1c7b76be MV |
962 | /* Check how many files differ. */ |
963 | init_revisions(&rev, ""); | |
964 | setup_revisions(0, NULL, &rev, NULL); | |
965 | rev.diffopt.output_format |= | |
966 | DIFF_FORMAT_CALLBACK; | |
967 | rev.diffopt.format_callback = count_diff_files; | |
968 | rev.diffopt.format_callback_data = &cnt; | |
969 | run_diff_files(&rev, 0); | |
970 | ||
971 | /* | |
972 | * Check how many unmerged entries are | |
973 | * there. | |
974 | */ | |
975 | cnt += count_unmerged_entries(); | |
976 | ||
977 | return cnt; | |
978 | } | |
979 | ||
93e535a5 JH |
980 | /* |
981 | * Pretend as if the user told us to merge with the tracking | |
982 | * branch we have for the upstream of the current branch | |
983 | */ | |
984 | static int setup_with_upstream(const char ***argv) | |
985 | { | |
986 | struct branch *branch = branch_get(NULL); | |
987 | int i; | |
988 | const char **args; | |
989 | ||
990 | if (!branch) | |
c7f426d4 | 991 | die(_("No current branch.")); |
93e535a5 | 992 | if (!branch->remote) |
c7f426d4 | 993 | die(_("No remote for the current branch.")); |
93e535a5 | 994 | if (!branch->merge_nr) |
c7f426d4 | 995 | die(_("No default upstream defined for the current branch.")); |
93e535a5 JH |
996 | |
997 | args = xcalloc(branch->merge_nr + 1, sizeof(char *)); | |
998 | for (i = 0; i < branch->merge_nr; i++) { | |
999 | if (!branch->merge[i]->dst) | |
c7f426d4 | 1000 | die(_("No remote tracking branch for %s from %s"), |
93e535a5 JH |
1001 | branch->merge[i]->src, branch->remote_name); |
1002 | args[i] = branch->merge[i]->dst; | |
1003 | } | |
1004 | args[i] = NULL; | |
1005 | *argv = args; | |
1006 | return i; | |
1007 | } | |
1008 | ||
1c7b76be MV |
1009 | int cmd_merge(int argc, const char **argv, const char *prefix) |
1010 | { | |
1011 | unsigned char result_tree[20]; | |
f285a2d7 | 1012 | struct strbuf buf = STRBUF_INIT; |
1c7b76be MV |
1013 | const char *head_arg; |
1014 | int flag, head_invalid = 0, i; | |
1015 | int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0; | |
1016 | struct commit_list *common = NULL; | |
1017 | const char *best_strategy = NULL, *wt_strategy = NULL; | |
1018 | struct commit_list **remotes = &remoteheads; | |
1019 | ||
da53eec6 NTND |
1020 | if (argc == 2 && !strcmp(argv[1], "-h")) |
1021 | usage_with_options(builtin_merge_usage, builtin_merge_options); | |
1c7b76be MV |
1022 | |
1023 | /* | |
1024 | * Check if we are _not_ on a detached HEAD, i.e. if there is a | |
1025 | * current branch. | |
1026 | */ | |
1027 | branch = resolve_ref("HEAD", head, 0, &flag); | |
1028 | if (branch && !prefixcmp(branch, "refs/heads/")) | |
1029 | branch += 11; | |
1030 | if (is_null_sha1(head)) | |
1031 | head_invalid = 1; | |
1032 | ||
1033 | git_config(git_merge_config, NULL); | |
1034 | ||
1035 | /* for color.ui */ | |
1036 | if (diff_use_color_default == -1) | |
1037 | diff_use_color_default = git_use_color_default; | |
1038 | ||
0d8fc3ef JH |
1039 | if (branch_mergeoptions) |
1040 | parse_branch_merge_options(branch_mergeoptions); | |
37782920 | 1041 | argc = parse_options(argc, argv, prefix, builtin_merge_options, |
1c7b76be | 1042 | builtin_merge_usage, 0); |
2a22c1b3 | 1043 | |
99bfc669 JK |
1044 | if (verbosity < 0 && show_progress == -1) |
1045 | show_progress = 0; | |
1046 | ||
35d2fffd JH |
1047 | if (abort_current_merge) { |
1048 | int nargc = 2; | |
1049 | const char *nargv[] = {"reset", "--merge", NULL}; | |
1050 | ||
1051 | if (!file_exists(git_path("MERGE_HEAD"))) | |
bacec478 | 1052 | die(_("There is no merge to abort (MERGE_HEAD missing).")); |
35d2fffd JH |
1053 | |
1054 | /* Invoke 'git reset --merge' */ | |
1055 | return cmd_reset(nargc, nargv, prefix); | |
1056 | } | |
1057 | ||
2a22c1b3 JH |
1058 | if (read_cache_unmerged()) |
1059 | die_resolve_conflict("merge"); | |
1060 | ||
1061 | if (file_exists(git_path("MERGE_HEAD"))) { | |
1062 | /* | |
1063 | * There is no unmerged entry, don't advise 'git | |
1064 | * add/rm <file>', just 'git commit'. | |
1065 | */ | |
1066 | if (advice_resolve_conflict) | |
bacec478 ÆAB |
1067 | die(_("You have not concluded your merge (MERGE_HEAD exists).\n" |
1068 | "Please, commit your changes before you can merge.")); | |
2a22c1b3 | 1069 | else |
bacec478 | 1070 | die(_("You have not concluded your merge (MERGE_HEAD exists).")); |
2a22c1b3 | 1071 | } |
d7e5c0cb JS |
1072 | if (file_exists(git_path("CHERRY_PICK_HEAD"))) { |
1073 | if (advice_resolve_conflict) | |
f68f1801 ÆAB |
1074 | die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n" |
1075 | "Please, commit your changes before you can merge.")); | |
d7e5c0cb | 1076 | else |
f68f1801 | 1077 | die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).")); |
2a22c1b3 JH |
1078 | } |
1079 | resolve_undo_clear(); | |
1080 | ||
7f87aff2 TA |
1081 | if (verbosity < 0) |
1082 | show_diffstat = 0; | |
1c7b76be MV |
1083 | |
1084 | if (squash) { | |
1085 | if (!allow_fast_forward) | |
bacec478 | 1086 | die(_("You cannot combine --squash with --no-ff.")); |
1c7b76be MV |
1087 | option_commit = 0; |
1088 | } | |
1089 | ||
13474835 | 1090 | if (!allow_fast_forward && fast_forward_only) |
bacec478 | 1091 | die(_("You cannot combine --no-ff with --ff-only.")); |
13474835 | 1092 | |
4e8115ff JH |
1093 | if (!abort_current_merge) { |
1094 | if (!argc && default_to_upstream) | |
1095 | argc = setup_with_upstream(&argv); | |
1096 | else if (argc == 1 && !strcmp(argv[0], "-")) | |
1097 | argv[0] = "@{-1}"; | |
1098 | } | |
1c7b76be MV |
1099 | if (!argc) |
1100 | usage_with_options(builtin_merge_usage, | |
1101 | builtin_merge_options); | |
1102 | ||
1103 | /* | |
1104 | * This could be traditional "merge <msg> HEAD <commit>..." and | |
1105 | * the way we can tell it is to see if the second token is HEAD, | |
1106 | * but some people might have misused the interface and used a | |
1107 | * committish that is the same as HEAD there instead. | |
1108 | * Traditional format never would have "-m" so it is an | |
1109 | * additional safety measure to check for it. | |
1110 | */ | |
1c7b76be MV |
1111 | |
1112 | if (!have_message && is_old_style_invocation(argc, argv)) { | |
1113 | strbuf_addstr(&merge_msg, argv[0]); | |
1114 | head_arg = argv[1]; | |
1115 | argv += 2; | |
1116 | argc -= 2; | |
1117 | } else if (head_invalid) { | |
1118 | struct object *remote_head; | |
1119 | /* | |
1120 | * If the merged head is a valid one there is no reason | |
1121 | * to forbid "git merge" into a branch yet to be born. | |
1122 | * We do the same for "git pull". | |
1123 | */ | |
1124 | if (argc != 1) | |
bacec478 ÆAB |
1125 | die(_("Can merge only exactly one commit into " |
1126 | "empty head")); | |
4be636f4 | 1127 | if (squash) |
bacec478 | 1128 | die(_("Squash commit into empty head not supported yet")); |
4be636f4 | 1129 | if (!allow_fast_forward) |
bacec478 ÆAB |
1130 | die(_("Non-fast-forward commit does not make sense into " |
1131 | "an empty head")); | |
1c7b76be MV |
1132 | remote_head = peel_to_type(argv[0], 0, NULL, OBJ_COMMIT); |
1133 | if (!remote_head) | |
bacec478 | 1134 | die(_("%s - not something we can merge"), argv[0]); |
97b1b4f3 | 1135 | read_empty(remote_head->sha1, 0); |
1c7b76be MV |
1136 | update_ref("initial pull", "HEAD", remote_head->sha1, NULL, 0, |
1137 | DIE_ON_ERR); | |
1c7b76be MV |
1138 | return 0; |
1139 | } else { | |
97d45bcb | 1140 | struct strbuf merge_names = STRBUF_INIT; |
1c7b76be MV |
1141 | |
1142 | /* We are invoked directly as the first-class UI. */ | |
1143 | head_arg = "HEAD"; | |
1144 | ||
1145 | /* | |
1146 | * All the rest are the commits being merged; | |
1147 | * prepare the standard merge summary message to | |
1148 | * be appended to the given message. If remote | |
1149 | * is invalid we will die later in the common | |
1150 | * codepath so we discard the error in this | |
1151 | * loop. | |
1152 | */ | |
f0ecac2b TRC |
1153 | for (i = 0; i < argc; i++) |
1154 | merge_name(argv[i], &merge_names); | |
1155 | ||
96e9420c | 1156 | if (!have_message || shortlog_len) { |
1876166a | 1157 | fmt_merge_msg(&merge_names, &merge_msg, !have_message, |
96e9420c | 1158 | shortlog_len); |
1876166a RR |
1159 | if (merge_msg.len) |
1160 | strbuf_setlen(&merge_msg, merge_msg.len - 1); | |
1161 | } | |
1c7b76be MV |
1162 | } |
1163 | ||
1164 | if (head_invalid || !argc) | |
1165 | usage_with_options(builtin_merge_usage, | |
1166 | builtin_merge_options); | |
1167 | ||
1168 | strbuf_addstr(&buf, "merge"); | |
1169 | for (i = 0; i < argc; i++) | |
1170 | strbuf_addf(&buf, " %s", argv[i]); | |
1171 | setenv("GIT_REFLOG_ACTION", buf.buf, 0); | |
1172 | strbuf_reset(&buf); | |
1173 | ||
1174 | for (i = 0; i < argc; i++) { | |
1175 | struct object *o; | |
18668f53 | 1176 | struct commit *commit; |
1c7b76be MV |
1177 | |
1178 | o = peel_to_type(argv[i], 0, NULL, OBJ_COMMIT); | |
1179 | if (!o) | |
bacec478 | 1180 | die(_("%s - not something we can merge"), argv[i]); |
18668f53 MV |
1181 | commit = lookup_commit(o->sha1); |
1182 | commit->util = (void *)argv[i]; | |
1183 | remotes = &commit_list_insert(commit, remotes)->next; | |
1c7b76be MV |
1184 | |
1185 | strbuf_addf(&buf, "GITHEAD_%s", sha1_to_hex(o->sha1)); | |
1186 | setenv(buf.buf, argv[i], 1); | |
1187 | strbuf_reset(&buf); | |
1188 | } | |
1189 | ||
1190 | if (!use_strategies) { | |
1191 | if (!remoteheads->next) | |
1192 | add_strategies(pull_twohead, DEFAULT_TWOHEAD); | |
1193 | else | |
1194 | add_strategies(pull_octopus, DEFAULT_OCTOPUS); | |
1195 | } | |
1196 | ||
1197 | for (i = 0; i < use_strategies_nr; i++) { | |
1198 | if (use_strategies[i]->attr & NO_FAST_FORWARD) | |
1199 | allow_fast_forward = 0; | |
1200 | if (use_strategies[i]->attr & NO_TRIVIAL) | |
1201 | allow_trivial = 0; | |
1202 | } | |
1203 | ||
1204 | if (!remoteheads->next) | |
1205 | common = get_merge_bases(lookup_commit(head), | |
1206 | remoteheads->item, 1); | |
1207 | else { | |
1208 | struct commit_list *list = remoteheads; | |
1209 | commit_list_insert(lookup_commit(head), &list); | |
1210 | common = get_octopus_merge_bases(list); | |
1211 | free(list); | |
1212 | } | |
1213 | ||
1214 | update_ref("updating ORIG_HEAD", "ORIG_HEAD", head, NULL, 0, | |
1215 | DIE_ON_ERR); | |
1216 | ||
1217 | if (!common) | |
1218 | ; /* No common ancestors found. We need a real merge. */ | |
1219 | else if (!remoteheads->next && !common->next && | |
1220 | common->item == remoteheads->item) { | |
1221 | /* | |
1222 | * If head can reach all the merge then we are up to date. | |
1223 | * but first the most common case of merging one remote. | |
1224 | */ | |
1225 | finish_up_to_date("Already up-to-date."); | |
1226 | return 0; | |
1227 | } else if (allow_fast_forward && !remoteheads->next && | |
1228 | !common->next && | |
1229 | !hashcmp(common->item->object.sha1, head)) { | |
1230 | /* Again the most common case of merging one remote. */ | |
f285a2d7 | 1231 | struct strbuf msg = STRBUF_INIT; |
1c7b76be MV |
1232 | struct object *o; |
1233 | char hex[41]; | |
1234 | ||
1235 | strcpy(hex, find_unique_abbrev(head, DEFAULT_ABBREV)); | |
1236 | ||
7f87aff2 | 1237 | if (verbosity >= 0) |
2ceb61e0 | 1238 | printf(_("Updating %s..%s\n"), |
7f87aff2 TA |
1239 | hex, |
1240 | find_unique_abbrev(remoteheads->item->object.sha1, | |
1241 | DEFAULT_ABBREV)); | |
a75d7b54 | 1242 | strbuf_addstr(&msg, "Fast-forward"); |
1c7b76be MV |
1243 | if (have_message) |
1244 | strbuf_addstr(&msg, | |
1245 | " (no commit created; -m option ignored)"); | |
1246 | o = peel_to_type(sha1_to_hex(remoteheads->item->object.sha1), | |
1247 | 0, NULL, OBJ_COMMIT); | |
1248 | if (!o) | |
1249 | return 1; | |
1250 | ||
1251 | if (checkout_fast_forward(head, remoteheads->item->object.sha1)) | |
1252 | return 1; | |
1253 | ||
1254 | finish(o->sha1, msg.buf); | |
1255 | drop_save(); | |
1256 | return 0; | |
1257 | } else if (!remoteheads->next && common->next) | |
1258 | ; | |
1259 | /* | |
a75d7b54 | 1260 | * We are not doing octopus and not fast-forward. Need |
1c7b76be MV |
1261 | * a real merge. |
1262 | */ | |
1263 | else if (!remoteheads->next && !common->next && option_commit) { | |
1264 | /* | |
a75d7b54 | 1265 | * We are not doing octopus, not fast-forward, and have |
1c7b76be MV |
1266 | * only one common. |
1267 | */ | |
1268 | refresh_cache(REFRESH_QUIET); | |
13474835 | 1269 | if (allow_trivial && !fast_forward_only) { |
1c7b76be MV |
1270 | /* See if it is really trivial. */ |
1271 | git_committer_info(IDENT_ERROR_ON_NO_NAME); | |
bacec478 | 1272 | printf(_("Trying really trivial in-index merge...\n")); |
1c7b76be MV |
1273 | if (!read_tree_trivial(common->item->object.sha1, |
1274 | head, remoteheads->item->object.sha1)) | |
1275 | return merge_trivial(); | |
bacec478 | 1276 | printf(_("Nope.\n")); |
1c7b76be MV |
1277 | } |
1278 | } else { | |
1279 | /* | |
1280 | * An octopus. If we can reach all the remote we are up | |
1281 | * to date. | |
1282 | */ | |
1283 | int up_to_date = 1; | |
1284 | struct commit_list *j; | |
1285 | ||
1286 | for (j = remoteheads; j; j = j->next) { | |
1287 | struct commit_list *common_one; | |
1288 | ||
1289 | /* | |
1290 | * Here we *have* to calculate the individual | |
1291 | * merge_bases again, otherwise "git merge HEAD^ | |
1292 | * HEAD^^" would be missed. | |
1293 | */ | |
1294 | common_one = get_merge_bases(lookup_commit(head), | |
1295 | j->item, 1); | |
1296 | if (hashcmp(common_one->item->object.sha1, | |
1297 | j->item->object.sha1)) { | |
1298 | up_to_date = 0; | |
1299 | break; | |
1300 | } | |
1301 | } | |
1302 | if (up_to_date) { | |
1303 | finish_up_to_date("Already up-to-date. Yeeah!"); | |
1304 | return 0; | |
1305 | } | |
1306 | } | |
1307 | ||
13474835 | 1308 | if (fast_forward_only) |
bacec478 | 1309 | die(_("Not possible to fast-forward, aborting.")); |
13474835 | 1310 | |
1c7b76be MV |
1311 | /* We are going to make a new commit. */ |
1312 | git_committer_info(IDENT_ERROR_ON_NO_NAME); | |
1313 | ||
1314 | /* | |
1315 | * At this point, we need a real merge. No matter what strategy | |
1316 | * we use, it would operate on the index, possibly affecting the | |
1317 | * working tree, and when resolved cleanly, have the desired | |
1318 | * tree in the index -- this means that the index must be in | |
1319 | * sync with the head commit. The strategies are responsible | |
1320 | * to ensure this. | |
1321 | */ | |
1322 | if (use_strategies_nr != 1) { | |
1323 | /* | |
1324 | * Stash away the local changes so that we can try more | |
1325 | * than one. | |
1326 | */ | |
1327 | save_state(); | |
1328 | } else { | |
1329 | memcpy(stash, null_sha1, 20); | |
1330 | } | |
1331 | ||
1332 | for (i = 0; i < use_strategies_nr; i++) { | |
1333 | int ret; | |
1334 | if (i) { | |
bacec478 | 1335 | printf(_("Rewinding the tree to pristine...\n")); |
1c7b76be MV |
1336 | restore_state(); |
1337 | } | |
1338 | if (use_strategies_nr != 1) | |
bacec478 | 1339 | printf(_("Trying merge strategy %s...\n"), |
1c7b76be MV |
1340 | use_strategies[i]->name); |
1341 | /* | |
1342 | * Remember which strategy left the state in the working | |
1343 | * tree. | |
1344 | */ | |
1345 | wt_strategy = use_strategies[i]->name; | |
1346 | ||
1347 | ret = try_merge_strategy(use_strategies[i]->name, | |
1348 | common, head_arg); | |
1349 | if (!option_commit && !ret) { | |
1350 | merge_was_ok = 1; | |
1351 | /* | |
1352 | * This is necessary here just to avoid writing | |
1353 | * the tree, but later we will *not* exit with | |
1354 | * status code 1 because merge_was_ok is set. | |
1355 | */ | |
1356 | ret = 1; | |
1357 | } | |
1358 | ||
1359 | if (ret) { | |
1360 | /* | |
1361 | * The backend exits with 1 when conflicts are | |
1362 | * left to be resolved, with 2 when it does not | |
1363 | * handle the given merge at all. | |
1364 | */ | |
1365 | if (ret == 1) { | |
1366 | int cnt = evaluate_result(); | |
1367 | ||
1368 | if (best_cnt <= 0 || cnt <= best_cnt) { | |
1369 | best_strategy = use_strategies[i]->name; | |
1370 | best_cnt = cnt; | |
1371 | } | |
1372 | } | |
1373 | if (merge_was_ok) | |
1374 | break; | |
1375 | else | |
1376 | continue; | |
1377 | } | |
1378 | ||
1379 | /* Automerge succeeded. */ | |
1380 | write_tree_trivial(result_tree); | |
1381 | automerge_was_ok = 1; | |
1382 | break; | |
1383 | } | |
1384 | ||
1385 | /* | |
1386 | * If we have a resulting tree, that means the strategy module | |
1387 | * auto resolved the merge cleanly. | |
1388 | */ | |
1389 | if (automerge_was_ok) | |
1390 | return finish_automerge(common, result_tree, wt_strategy); | |
1391 | ||
1392 | /* | |
1393 | * Pick the result from the best strategy and have the user fix | |
1394 | * it up. | |
1395 | */ | |
1396 | if (!best_strategy) { | |
1397 | restore_state(); | |
1398 | if (use_strategies_nr > 1) | |
1399 | fprintf(stderr, | |
bacec478 | 1400 | _("No merge strategy handled the merge.\n")); |
1c7b76be | 1401 | else |
bacec478 | 1402 | fprintf(stderr, _("Merge with strategy %s failed.\n"), |
1c7b76be MV |
1403 | use_strategies[0]->name); |
1404 | return 2; | |
1405 | } else if (best_strategy == wt_strategy) | |
1406 | ; /* We already have its result in the working tree. */ | |
1407 | else { | |
bacec478 | 1408 | printf(_("Rewinding the tree to pristine...\n")); |
1c7b76be | 1409 | restore_state(); |
bacec478 | 1410 | printf(_("Using the %s to prepare resolving by hand.\n"), |
1c7b76be MV |
1411 | best_strategy); |
1412 | try_merge_strategy(best_strategy, common, head_arg); | |
1413 | } | |
1414 | ||
1415 | if (squash) | |
1416 | finish(NULL, NULL); | |
1417 | else { | |
1418 | int fd; | |
1419 | struct commit_list *j; | |
1420 | ||
1421 | for (j = remoteheads; j; j = j->next) | |
1422 | strbuf_addf(&buf, "%s\n", | |
1423 | sha1_to_hex(j->item->object.sha1)); | |
1424 | fd = open(git_path("MERGE_HEAD"), O_WRONLY | O_CREAT, 0666); | |
1425 | if (fd < 0) | |
bacec478 | 1426 | die_errno(_("Could not open '%s' for writing"), |
0721c314 | 1427 | git_path("MERGE_HEAD")); |
1c7b76be | 1428 | if (write_in_full(fd, buf.buf, buf.len) != buf.len) |
bacec478 | 1429 | die_errno(_("Could not write to '%s'"), git_path("MERGE_HEAD")); |
1c7b76be MV |
1430 | close(fd); |
1431 | strbuf_addch(&merge_msg, '\n'); | |
65969d43 | 1432 | write_merge_msg(); |
cf10f9fd MV |
1433 | fd = open(git_path("MERGE_MODE"), O_WRONLY | O_CREAT | O_TRUNC, 0666); |
1434 | if (fd < 0) | |
bacec478 | 1435 | die_errno(_("Could not open '%s' for writing"), |
0721c314 | 1436 | git_path("MERGE_MODE")); |
cf10f9fd MV |
1437 | strbuf_reset(&buf); |
1438 | if (!allow_fast_forward) | |
1439 | strbuf_addf(&buf, "no-ff"); | |
1440 | if (write_in_full(fd, buf.buf, buf.len) != buf.len) | |
bacec478 | 1441 | die_errno(_("Could not write to '%s'"), git_path("MERGE_MODE")); |
cf10f9fd | 1442 | close(fd); |
1c7b76be MV |
1443 | } |
1444 | ||
1445 | if (merge_was_ok) { | |
bacec478 ÆAB |
1446 | fprintf(stderr, _("Automatic merge went well; " |
1447 | "stopped before committing as requested\n")); | |
1c7b76be MV |
1448 | return 0; |
1449 | } else | |
7610fa57 | 1450 | return suggest_conflicts(option_renormalize); |
1c7b76be | 1451 | } |