Commit | Line | Data |
---|---|---|
9509af68 JS |
1 | #include "cache.h" |
2 | #include "builtin.h" | |
3 | #include "object.h" | |
4 | #include "commit.h" | |
5 | #include "tag.h" | |
9509af68 JS |
6 | #include "run-command.h" |
7 | #include "exec_cmd.h" | |
8 | #include "utf8.h" | |
f8103794 | 9 | #include "parse-options.h" |
45525bd0 | 10 | #include "cache-tree.h" |
0f2d4476 JK |
11 | #include "diff.h" |
12 | #include "revision.h" | |
aa1a0111 | 13 | #include "rerere.h" |
6eb1b437 | 14 | #include "merge-recursive.h" |
c62f6ec3 | 15 | #include "refs.h" |
04d3d3cf | 16 | #include "dir.h" |
26ae337b | 17 | #include "sequencer.h" |
9509af68 JS |
18 | |
19 | /* | |
20 | * This implements the builtins revert and cherry-pick. | |
21 | * | |
22 | * Copyright (c) 2007 Johannes E. Schindelin | |
23 | * | |
24 | * Based on git-revert.sh, which is | |
25 | * | |
26 | * Copyright (c) 2005 Linus Torvalds | |
27 | * Copyright (c) 2005 Junio C Hamano | |
28 | */ | |
29 | ||
f8103794 | 30 | static const char * const revert_usage[] = { |
1b1dd23f | 31 | "git revert [options] <commit-ish>", |
26ae337b | 32 | "git revert <subcommand>", |
f8103794 PH |
33 | NULL |
34 | }; | |
9509af68 | 35 | |
f8103794 | 36 | static const char * const cherry_pick_usage[] = { |
1b1dd23f | 37 | "git cherry-pick [options] <commit-ish>", |
26ae337b | 38 | "git cherry-pick <subcommand>", |
f8103794 PH |
39 | NULL |
40 | }; | |
9509af68 | 41 | |
644a3690 RR |
42 | enum replay_action { |
43 | REPLAY_REVERT, | |
44 | REPLAY_PICK | |
45 | }; | |
46 | ||
539047c1 JN |
47 | enum replay_subcommand { |
48 | REPLAY_NONE, | |
49 | REPLAY_REMOVE_STATE, | |
50 | REPLAY_CONTINUE, | |
51 | REPLAY_ROLLBACK | |
52 | }; | |
80e1f791 RR |
53 | |
54 | struct replay_opts { | |
55 | enum replay_action action; | |
26ae337b | 56 | enum replay_subcommand subcommand; |
80e1f791 RR |
57 | |
58 | /* Boolean options */ | |
59 | int edit; | |
60 | int record_origin; | |
61 | int no_commit; | |
62 | int signoff; | |
63 | int allow_ff; | |
64 | int allow_rerere_auto; | |
65 | ||
66 | int mainline; | |
80e1f791 RR |
67 | |
68 | /* Merge strategy */ | |
69 | const char *strategy; | |
70 | const char **xopts; | |
71 | size_t xopts_nr, xopts_alloc; | |
7f13334e JN |
72 | |
73 | /* Only used by REPLAY_NONE */ | |
74 | struct rev_info *revs; | |
80e1f791 | 75 | }; |
9509af68 JS |
76 | |
77 | #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION" | |
78 | ||
80e1f791 RR |
79 | static const char *action_name(const struct replay_opts *opts) |
80 | { | |
644a3690 | 81 | return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick"; |
80e1f791 RR |
82 | } |
83 | ||
d6856540 JN |
84 | static char *get_encoding(const char *message); |
85 | ||
80e1f791 | 86 | static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts) |
e0ef8495 | 87 | { |
644a3690 | 88 | return opts->action == REPLAY_REVERT ? revert_usage : cherry_pick_usage; |
e0ef8495 JN |
89 | } |
90 | ||
67ac1e1d JN |
91 | static int option_parse_x(const struct option *opt, |
92 | const char *arg, int unset) | |
93 | { | |
80e1f791 RR |
94 | struct replay_opts **opts_ptr = opt->value; |
95 | struct replay_opts *opts = *opts_ptr; | |
96 | ||
67ac1e1d JN |
97 | if (unset) |
98 | return 0; | |
99 | ||
80e1f791 RR |
100 | ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc); |
101 | opts->xopts[opts->xopts_nr++] = xstrdup(arg); | |
67ac1e1d JN |
102 | return 0; |
103 | } | |
104 | ||
9044143f RR |
105 | static void verify_opt_compatible(const char *me, const char *base_opt, ...) |
106 | { | |
107 | const char *this_opt; | |
108 | va_list ap; | |
109 | ||
110 | va_start(ap, base_opt); | |
111 | while ((this_opt = va_arg(ap, const char *))) { | |
112 | if (va_arg(ap, int)) | |
113 | break; | |
114 | } | |
115 | va_end(ap); | |
116 | ||
117 | if (this_opt) | |
118 | die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt); | |
119 | } | |
120 | ||
5a5d80f4 RR |
121 | static void verify_opt_mutually_compatible(const char *me, ...) |
122 | { | |
27c0f768 | 123 | const char *opt1, *opt2 = NULL; |
5a5d80f4 RR |
124 | va_list ap; |
125 | ||
126 | va_start(ap, me); | |
127 | while ((opt1 = va_arg(ap, const char *))) { | |
128 | if (va_arg(ap, int)) | |
129 | break; | |
130 | } | |
131 | if (opt1) { | |
132 | while ((opt2 = va_arg(ap, const char *))) { | |
133 | if (va_arg(ap, int)) | |
134 | break; | |
135 | } | |
136 | } | |
137 | ||
138 | if (opt1 && opt2) | |
139 | die(_("%s: %s cannot be used with %s"), me, opt1, opt2); | |
140 | } | |
141 | ||
80e1f791 | 142 | static void parse_args(int argc, const char **argv, struct replay_opts *opts) |
9509af68 | 143 | { |
80e1f791 | 144 | const char * const * usage_str = revert_or_cherry_pick_usage(opts); |
9044143f | 145 | const char *me = action_name(opts); |
f80a8726 | 146 | int remove_state = 0; |
5a5d80f4 | 147 | int contin = 0; |
539047c1 | 148 | int rollback = 0; |
f8103794 | 149 | struct option options[] = { |
f80a8726 JN |
150 | OPT_BOOLEAN(0, "quit", &remove_state, "end revert or cherry-pick sequence"), |
151 | OPT_BOOLEAN(0, "continue", &contin, "resume revert or cherry-pick sequence"), | |
539047c1 | 152 | OPT_BOOLEAN(0, "abort", &rollback, "cancel revert or cherry-pick sequence"), |
80e1f791 RR |
153 | OPT_BOOLEAN('n', "no-commit", &opts->no_commit, "don't automatically commit"), |
154 | OPT_BOOLEAN('e', "edit", &opts->edit, "edit the commit message"), | |
7cfc6057 | 155 | OPT_NOOP_NOARG('r', NULL), |
80e1f791 RR |
156 | OPT_BOOLEAN('s', "signoff", &opts->signoff, "add Signed-off-by:"), |
157 | OPT_INTEGER('m', "mainline", &opts->mainline, "parent number"), | |
158 | OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto), | |
159 | OPT_STRING(0, "strategy", &opts->strategy, "strategy", "merge strategy"), | |
160 | OPT_CALLBACK('X', "strategy-option", &opts, "option", | |
67ac1e1d | 161 | "option for merge strategy", option_parse_x), |
f8103794 | 162 | OPT_END(), |
c62f6ec3 JH |
163 | OPT_END(), |
164 | OPT_END(), | |
f8103794 PH |
165 | }; |
166 | ||
644a3690 | 167 | if (opts->action == REPLAY_PICK) { |
c62f6ec3 | 168 | struct option cp_extra[] = { |
80e1f791 RR |
169 | OPT_BOOLEAN('x', NULL, &opts->record_origin, "append commit name"), |
170 | OPT_BOOLEAN(0, "ff", &opts->allow_ff, "allow fast-forward"), | |
c62f6ec3 JH |
171 | OPT_END(), |
172 | }; | |
173 | if (parse_options_concat(options, ARRAY_SIZE(options), cp_extra)) | |
f30f71ce | 174 | die(_("program error")); |
c62f6ec3 JH |
175 | } |
176 | ||
7f13334e JN |
177 | argc = parse_options(argc, argv, NULL, options, usage_str, |
178 | PARSE_OPT_KEEP_ARGV0 | | |
179 | PARSE_OPT_KEEP_UNKNOWN); | |
26ae337b | 180 | |
5a5d80f4 RR |
181 | /* Check for incompatible subcommands */ |
182 | verify_opt_mutually_compatible(me, | |
f80a8726 | 183 | "--quit", remove_state, |
5a5d80f4 | 184 | "--continue", contin, |
539047c1 | 185 | "--abort", rollback, |
5a5d80f4 RR |
186 | NULL); |
187 | ||
26ae337b | 188 | /* Set the subcommand */ |
f80a8726 JN |
189 | if (remove_state) |
190 | opts->subcommand = REPLAY_REMOVE_STATE; | |
5a5d80f4 RR |
191 | else if (contin) |
192 | opts->subcommand = REPLAY_CONTINUE; | |
539047c1 JN |
193 | else if (rollback) |
194 | opts->subcommand = REPLAY_ROLLBACK; | |
26ae337b RR |
195 | else |
196 | opts->subcommand = REPLAY_NONE; | |
197 | ||
198 | /* Check for incompatible command line arguments */ | |
5a5d80f4 RR |
199 | if (opts->subcommand != REPLAY_NONE) { |
200 | char *this_operation; | |
f80a8726 JN |
201 | if (opts->subcommand == REPLAY_REMOVE_STATE) |
202 | this_operation = "--quit"; | |
539047c1 | 203 | else if (opts->subcommand == REPLAY_CONTINUE) |
5a5d80f4 | 204 | this_operation = "--continue"; |
539047c1 JN |
205 | else { |
206 | assert(opts->subcommand == REPLAY_ROLLBACK); | |
207 | this_operation = "--abort"; | |
208 | } | |
5a5d80f4 RR |
209 | |
210 | verify_opt_compatible(me, this_operation, | |
26ae337b RR |
211 | "--no-commit", opts->no_commit, |
212 | "--signoff", opts->signoff, | |
213 | "--mainline", opts->mainline, | |
214 | "--strategy", opts->strategy ? 1 : 0, | |
215 | "--strategy-option", opts->xopts ? 1 : 0, | |
216 | "-x", opts->record_origin, | |
217 | "--ff", opts->allow_ff, | |
218 | NULL); | |
219 | } | |
220 | ||
9044143f RR |
221 | if (opts->allow_ff) |
222 | verify_opt_compatible(me, "--ff", | |
223 | "--signoff", opts->signoff, | |
224 | "--no-commit", opts->no_commit, | |
225 | "-x", opts->record_origin, | |
226 | "--edit", opts->edit, | |
227 | NULL); | |
7f13334e JN |
228 | |
229 | if (opts->subcommand != REPLAY_NONE) { | |
230 | opts->revs = NULL; | |
231 | } else { | |
232 | opts->revs = xmalloc(sizeof(*opts->revs)); | |
233 | init_revisions(opts->revs, NULL); | |
234 | opts->revs->no_walk = 1; | |
235 | if (argc < 2) | |
236 | usage_with_options(usage_str, options); | |
237 | argc = setup_revisions(argc, argv, opts->revs, NULL); | |
238 | } | |
239 | ||
240 | if (argc > 1) | |
241 | usage_with_options(usage_str, options); | |
9509af68 JS |
242 | } |
243 | ||
d6856540 JN |
244 | struct commit_message { |
245 | char *parent_label; | |
246 | const char *label; | |
247 | const char *subject; | |
248 | char *reencoded_message; | |
249 | const char *message; | |
250 | }; | |
251 | ||
708f9d96 | 252 | static int get_message(struct commit *commit, struct commit_message *out) |
9509af68 | 253 | { |
d6856540 | 254 | const char *encoding; |
dfe7effe CC |
255 | const char *abbrev, *subject; |
256 | int abbrev_len, subject_len; | |
d6856540 | 257 | char *q; |
9509af68 | 258 | |
708f9d96 | 259 | if (!commit->buffer) |
d6856540 | 260 | return -1; |
708f9d96 | 261 | encoding = get_encoding(commit->buffer); |
d6856540 JN |
262 | if (!encoding) |
263 | encoding = "UTF-8"; | |
264 | if (!git_commit_encoding) | |
265 | git_commit_encoding = "UTF-8"; | |
43acff34 JN |
266 | |
267 | out->reencoded_message = NULL; | |
708f9d96 | 268 | out->message = commit->buffer; |
43acff34 | 269 | if (strcmp(encoding, git_commit_encoding)) |
708f9d96 | 270 | out->reencoded_message = reencode_string(commit->buffer, |
43acff34 JN |
271 | git_commit_encoding, encoding); |
272 | if (out->reencoded_message) | |
d6856540 JN |
273 | out->message = out->reencoded_message; |
274 | ||
275 | abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV); | |
276 | abbrev_len = strlen(abbrev); | |
277 | ||
dfe7effe | 278 | subject_len = find_commit_subject(out->message, &subject); |
d6856540 JN |
279 | |
280 | out->parent_label = xmalloc(strlen("parent of ") + abbrev_len + | |
dfe7effe | 281 | strlen("... ") + subject_len + 1); |
d6856540 JN |
282 | q = out->parent_label; |
283 | q = mempcpy(q, "parent of ", strlen("parent of ")); | |
284 | out->label = q; | |
285 | q = mempcpy(q, abbrev, abbrev_len); | |
286 | q = mempcpy(q, "... ", strlen("... ")); | |
287 | out->subject = q; | |
dfe7effe | 288 | q = mempcpy(q, subject, subject_len); |
d6856540 JN |
289 | *q = '\0'; |
290 | return 0; | |
291 | } | |
292 | ||
293 | static void free_message(struct commit_message *msg) | |
294 | { | |
295 | free(msg->parent_label); | |
296 | free(msg->reencoded_message); | |
9509af68 JS |
297 | } |
298 | ||
52fae7de | 299 | static char *get_encoding(const char *message) |
9509af68 JS |
300 | { |
301 | const char *p = message, *eol; | |
302 | ||
9509af68 JS |
303 | while (*p && *p != '\n') { |
304 | for (eol = p + 1; *eol && *eol != '\n'; eol++) | |
305 | ; /* do nothing */ | |
306 | if (!prefixcmp(p, "encoding ")) { | |
307 | char *result = xmalloc(eol - 8 - p); | |
308 | strlcpy(result, p + 9, eol - 8 - p); | |
309 | return result; | |
310 | } | |
311 | p = eol; | |
312 | if (*p == '\n') | |
313 | p++; | |
314 | } | |
315 | return NULL; | |
316 | } | |
317 | ||
82433cdf | 318 | static void write_cherry_pick_head(struct commit *commit, const char *pseudoref) |
9509af68 | 319 | { |
418c9b17 | 320 | const char *filename; |
d7e5c0cb JS |
321 | int fd; |
322 | struct strbuf buf = STRBUF_INIT; | |
323 | ||
324 | strbuf_addf(&buf, "%s\n", sha1_to_hex(commit->object.sha1)); | |
325 | ||
b4524d34 | 326 | filename = git_path("%s", pseudoref); |
418c9b17 | 327 | fd = open(filename, O_WRONLY | O_CREAT, 0666); |
d7e5c0cb | 328 | if (fd < 0) |
418c9b17 | 329 | die_errno(_("Could not open '%s' for writing"), filename); |
d7e5c0cb | 330 | if (write_in_full(fd, buf.buf, buf.len) != buf.len || close(fd)) |
418c9b17 | 331 | die_errno(_("Could not write to '%s'"), filename); |
d7e5c0cb | 332 | strbuf_release(&buf); |
9509af68 JS |
333 | } |
334 | ||
82352cb6 | 335 | static void print_advice(int show_hint) |
804c7174 | 336 | { |
804c7174 | 337 | char *msg = getenv("GIT_CHERRY_PICK_HELP"); |
804c7174 | 338 | |
314eeb6e JN |
339 | if (msg) { |
340 | fprintf(stderr, "%s\n", msg); | |
d7e5c0cb JS |
341 | /* |
342 | * A conflict has occured but the porcelain | |
343 | * (typically rebase --interactive) wants to take care | |
344 | * of the commit itself so remove CHERRY_PICK_HEAD | |
345 | */ | |
346 | unlink(git_path("CHERRY_PICK_HEAD")); | |
314eeb6e | 347 | return; |
804c7174 | 348 | } |
804c7174 | 349 | |
82352cb6 JS |
350 | if (show_hint) { |
351 | advise("after resolving the conflicts, mark the corrected paths"); | |
352 | advise("with 'git add <paths>' or 'git rm <paths>'"); | |
353 | advise("and commit the result with 'git commit'"); | |
354 | } | |
804c7174 WC |
355 | } |
356 | ||
bc84a7fb CC |
357 | static void write_message(struct strbuf *msgbuf, const char *filename) |
358 | { | |
359 | static struct lock_file msg_file; | |
360 | ||
361 | int msg_fd = hold_lock_file_for_update(&msg_file, filename, | |
362 | LOCK_DIE_ON_ERROR); | |
363 | if (write_in_full(msg_fd, msgbuf->buf, msgbuf->len) < 0) | |
5e1e6b93 | 364 | die_errno(_("Could not write to %s"), filename); |
bc84a7fb CC |
365 | strbuf_release(msgbuf); |
366 | if (commit_lock_file(&msg_file) < 0) | |
f30f71ce | 367 | die(_("Error wrapping up %s"), filename); |
bc84a7fb CC |
368 | } |
369 | ||
6eb1b437 MV |
370 | static struct tree *empty_tree(void) |
371 | { | |
127f0452 | 372 | return lookup_tree((const unsigned char *)EMPTY_TREE_SHA1_BIN); |
6eb1b437 MV |
373 | } |
374 | ||
cf3e2486 | 375 | static int error_dirty_index(struct replay_opts *opts) |
d38a30df | 376 | { |
cf3e2486 RR |
377 | if (read_cache_unmerged()) |
378 | return error_resolve_conflict(action_name(opts)); | |
379 | ||
380 | /* Different translation strings for cherry-pick and revert */ | |
644a3690 | 381 | if (opts->action == REPLAY_PICK) |
cf3e2486 RR |
382 | error(_("Your local changes would be overwritten by cherry-pick.")); |
383 | else | |
384 | error(_("Your local changes would be overwritten by revert.")); | |
385 | ||
386 | if (advice_commit_before_merge) | |
387 | advise(_("Commit your changes or stash them to proceed.")); | |
388 | return -1; | |
d38a30df MM |
389 | } |
390 | ||
c62f6ec3 JH |
391 | static int fast_forward_to(const unsigned char *to, const unsigned char *from) |
392 | { | |
393 | struct ref_lock *ref_lock; | |
394 | ||
395 | read_cache(); | |
396 | if (checkout_fast_forward(from, to)) | |
397 | exit(1); /* the callee should have complained already */ | |
398 | ref_lock = lock_any_ref_for_update("HEAD", from, 0); | |
399 | return write_ref_sha1(ref_lock, to, "cherry-pick"); | |
400 | } | |
401 | ||
7b53b92f CC |
402 | static int do_recursive_merge(struct commit *base, struct commit *next, |
403 | const char *base_label, const char *next_label, | |
80e1f791 RR |
404 | unsigned char *head, struct strbuf *msgbuf, |
405 | struct replay_opts *opts) | |
ae8c79fd CC |
406 | { |
407 | struct merge_options o; | |
408 | struct tree *result, *next_tree, *base_tree, *head_tree; | |
409 | int clean, index_fd; | |
67ac1e1d | 410 | const char **xopt; |
ae8c79fd CC |
411 | static struct lock_file index_lock; |
412 | ||
413 | index_fd = hold_locked_index(&index_lock, 1); | |
414 | ||
415 | read_cache(); | |
7610fa57 | 416 | |
ae8c79fd CC |
417 | init_merge_options(&o); |
418 | o.ancestor = base ? base_label : "(empty tree)"; | |
419 | o.branch1 = "HEAD"; | |
420 | o.branch2 = next ? next_label : "(empty tree)"; | |
421 | ||
422 | head_tree = parse_tree_indirect(head); | |
423 | next_tree = next ? next->tree : empty_tree(); | |
424 | base_tree = base ? base->tree : empty_tree(); | |
425 | ||
80e1f791 | 426 | for (xopt = opts->xopts; xopt != opts->xopts + opts->xopts_nr; xopt++) |
67ac1e1d JN |
427 | parse_merge_opt(&o, *xopt); |
428 | ||
ae8c79fd CC |
429 | clean = merge_trees(&o, |
430 | head_tree, | |
431 | next_tree, base_tree, &result); | |
432 | ||
433 | if (active_cache_changed && | |
434 | (write_cache(index_fd, active_cache, active_nr) || | |
435 | commit_locked_index(&index_lock))) | |
b9c993e0 | 436 | /* TRANSLATORS: %s will be "revert" or "cherry-pick" */ |
80e1f791 | 437 | die(_("%s: Unable to write new index file"), action_name(opts)); |
ae8c79fd CC |
438 | rollback_lock_file(&index_lock); |
439 | ||
440 | if (!clean) { | |
441 | int i; | |
442 | strbuf_addstr(msgbuf, "\nConflicts:\n\n"); | |
443 | for (i = 0; i < active_nr;) { | |
444 | struct cache_entry *ce = active_cache[i++]; | |
445 | if (ce_stage(ce)) { | |
446 | strbuf_addch(msgbuf, '\t'); | |
447 | strbuf_addstr(msgbuf, ce->name); | |
448 | strbuf_addch(msgbuf, '\n'); | |
449 | while (i < active_nr && !strcmp(ce->name, | |
450 | active_cache[i]->name)) | |
451 | i++; | |
452 | } | |
453 | } | |
ae8c79fd | 454 | } |
7b53b92f CC |
455 | |
456 | return !clean; | |
ae8c79fd CC |
457 | } |
458 | ||
5df16453 CC |
459 | /* |
460 | * If we are cherry-pick, and if the merge did not result in | |
461 | * hand-editing, we will hit this commit and inherit the original | |
462 | * author date and name. | |
463 | * If we are revert, or if our cherry-pick results in a hand merge, | |
464 | * we had better say that the current user is responsible for that. | |
465 | */ | |
80e1f791 | 466 | static int run_git_commit(const char *defmsg, struct replay_opts *opts) |
5df16453 CC |
467 | { |
468 | /* 6 is max possible length of our args array including NULL */ | |
469 | const char *args[6]; | |
470 | int i = 0; | |
471 | ||
472 | args[i++] = "commit"; | |
473 | args[i++] = "-n"; | |
80e1f791 | 474 | if (opts->signoff) |
5df16453 | 475 | args[i++] = "-s"; |
80e1f791 | 476 | if (!opts->edit) { |
5df16453 CC |
477 | args[i++] = "-F"; |
478 | args[i++] = defmsg; | |
479 | } | |
480 | args[i] = NULL; | |
481 | ||
482 | return run_command_v_opt(args, RUN_GIT_CMD); | |
ae8c79fd CC |
483 | } |
484 | ||
80e1f791 | 485 | static int do_pick_commit(struct commit *commit, struct replay_opts *opts) |
9509af68 JS |
486 | { |
487 | unsigned char head[20]; | |
7791ecbc | 488 | struct commit *base, *next, *parent; |
bf975d37 | 489 | const char *base_label, *next_label; |
d6856540 | 490 | struct commit_message msg = { NULL, NULL, NULL, NULL, NULL }; |
28db756f | 491 | char *defmsg = NULL; |
bc84a7fb | 492 | struct strbuf msgbuf = STRBUF_INIT; |
7b53b92f | 493 | int res; |
9509af68 | 494 | |
80e1f791 | 495 | if (opts->no_commit) { |
9509af68 JS |
496 | /* |
497 | * We do not intend to commit immediately. We just want to | |
71aa2b8f JH |
498 | * merge the differences in, so let's compute the tree |
499 | * that represents the "current" state for merge-recursive | |
500 | * to work on. | |
9509af68 | 501 | */ |
45525bd0 | 502 | if (write_cache_as_tree(head, 0, NULL)) |
f30f71ce | 503 | die (_("Your index file is unmerged.")); |
9509af68 | 504 | } else { |
9509af68 | 505 | if (get_sha1("HEAD", head)) |
cf3e2486 | 506 | return error(_("You do not have a valid HEAD")); |
75f3ff2e | 507 | if (index_differs_from("HEAD", 0)) |
cf3e2486 | 508 | return error_dirty_index(opts); |
9509af68 | 509 | } |
6eb1b437 MV |
510 | discard_cache(); |
511 | ||
f95ebf74 | 512 | if (!commit->parents) { |
f95ebf74 JS |
513 | parent = NULL; |
514 | } | |
515 | else if (commit->parents->next) { | |
7791ecbc JH |
516 | /* Reverting or cherry-picking a merge commit */ |
517 | int cnt; | |
518 | struct commit_list *p; | |
519 | ||
80e1f791 | 520 | if (!opts->mainline) |
cf3e2486 RR |
521 | return error(_("Commit %s is a merge but no -m option was given."), |
522 | sha1_to_hex(commit->object.sha1)); | |
7791ecbc JH |
523 | |
524 | for (cnt = 1, p = commit->parents; | |
80e1f791 | 525 | cnt != opts->mainline && p; |
7791ecbc JH |
526 | cnt++) |
527 | p = p->next; | |
80e1f791 | 528 | if (cnt != opts->mainline || !p) |
cf3e2486 RR |
529 | return error(_("Commit %s does not have parent %d"), |
530 | sha1_to_hex(commit->object.sha1), opts->mainline); | |
7791ecbc | 531 | parent = p->item; |
80e1f791 | 532 | } else if (0 < opts->mainline) |
cf3e2486 RR |
533 | return error(_("Mainline was specified but commit %s is not a merge."), |
534 | sha1_to_hex(commit->object.sha1)); | |
7791ecbc JH |
535 | else |
536 | parent = commit->parents->item; | |
537 | ||
80e1f791 | 538 | if (opts->allow_ff && parent && !hashcmp(parent->object.sha1, head)) |
c62f6ec3 JH |
539 | return fast_forward_to(commit->object.sha1, head); |
540 | ||
6eb1b437 | 541 | if (parent && parse_commit(parent) < 0) |
b9c993e0 ÆAB |
542 | /* TRANSLATORS: The first %s will be "revert" or |
543 | "cherry-pick", the second %s a SHA1 */ | |
cf3e2486 RR |
544 | return error(_("%s: cannot parse parent commit %s"), |
545 | action_name(opts), sha1_to_hex(parent->object.sha1)); | |
6eb1b437 | 546 | |
708f9d96 | 547 | if (get_message(commit, &msg) != 0) |
cf3e2486 RR |
548 | return error(_("Cannot get commit message for %s"), |
549 | sha1_to_hex(commit->object.sha1)); | |
d6856540 | 550 | |
9509af68 JS |
551 | /* |
552 | * "commit" is an existing commit. We would want to apply | |
553 | * the difference it introduces since its first parent "prev" | |
554 | * on top of the current HEAD if we are cherry-pick. Or the | |
555 | * reverse of it if we are revert. | |
556 | */ | |
557 | ||
28db756f | 558 | defmsg = git_pathdup("MERGE_MSG"); |
c62f6ec3 | 559 | |
644a3690 | 560 | if (opts->action == REPLAY_REVERT) { |
9509af68 | 561 | base = commit; |
bf975d37 | 562 | base_label = msg.label; |
7791ecbc | 563 | next = parent; |
d6856540 | 564 | next_label = msg.parent_label; |
bc84a7fb CC |
565 | strbuf_addstr(&msgbuf, "Revert \""); |
566 | strbuf_addstr(&msgbuf, msg.subject); | |
567 | strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit "); | |
568 | strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1)); | |
d5be89d8 | 569 | |
fad26526 | 570 | if (commit->parents && commit->parents->next) { |
bc84a7fb CC |
571 | strbuf_addstr(&msgbuf, ", reversing\nchanges made to "); |
572 | strbuf_addstr(&msgbuf, sha1_to_hex(parent->object.sha1)); | |
d5be89d8 | 573 | } |
bc84a7fb | 574 | strbuf_addstr(&msgbuf, ".\n"); |
9509af68 | 575 | } else { |
be33c46c RR |
576 | const char *p; |
577 | ||
7791ecbc | 578 | base = parent; |
bf975d37 | 579 | base_label = msg.parent_label; |
9509af68 | 580 | next = commit; |
d6856540 | 581 | next_label = msg.label; |
be33c46c RR |
582 | |
583 | /* | |
584 | * Append the commit log message to msgbuf; it starts | |
585 | * after the tree, parent, author, committer | |
586 | * information followed by "\n\n". | |
587 | */ | |
588 | p = strstr(msg.message, "\n\n"); | |
589 | if (p) { | |
590 | p += 2; | |
591 | strbuf_addstr(&msgbuf, p); | |
592 | } | |
593 | ||
80e1f791 | 594 | if (opts->record_origin) { |
bc84a7fb CC |
595 | strbuf_addstr(&msgbuf, "(cherry picked from commit "); |
596 | strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1)); | |
597 | strbuf_addstr(&msgbuf, ")\n"); | |
9509af68 JS |
598 | } |
599 | } | |
9509af68 | 600 | |
644a3690 | 601 | if (!opts->strategy || !strcmp(opts->strategy, "recursive") || opts->action == REPLAY_REVERT) { |
7b53b92f | 602 | res = do_recursive_merge(base, next, base_label, next_label, |
80e1f791 | 603 | head, &msgbuf, opts); |
7b53b92f CC |
604 | write_message(&msgbuf, defmsg); |
605 | } else { | |
91e52598 CC |
606 | struct commit_list *common = NULL; |
607 | struct commit_list *remotes = NULL; | |
7b53b92f | 608 | |
91e52598 | 609 | write_message(&msgbuf, defmsg); |
7b53b92f | 610 | |
91e52598 CC |
611 | commit_list_insert(base, &common); |
612 | commit_list_insert(next, &remotes); | |
80e1f791 RR |
613 | res = try_merge_command(opts->strategy, opts->xopts_nr, opts->xopts, |
614 | common, sha1_to_hex(head), remotes); | |
91e52598 CC |
615 | free_commit_list(common); |
616 | free_commit_list(remotes); | |
9509af68 | 617 | } |
9509af68 | 618 | |
9fa8aecd JS |
619 | /* |
620 | * If the merge was clean or if it failed due to conflict, we write | |
621 | * CHERRY_PICK_HEAD for the subsequent invocation of commit to use. | |
622 | * However, if the merge did not even start, then we don't want to | |
623 | * write it at all. | |
624 | */ | |
644a3690 | 625 | if (opts->action == REPLAY_PICK && !opts->no_commit && (res == 0 || res == 1)) |
82433cdf | 626 | write_cherry_pick_head(commit, "CHERRY_PICK_HEAD"); |
644a3690 | 627 | if (opts->action == REPLAY_REVERT && ((opts->no_commit && res == 0) || res == 1)) |
82433cdf | 628 | write_cherry_pick_head(commit, "REVERT_HEAD"); |
9fa8aecd | 629 | |
7b53b92f | 630 | if (res) { |
644a3690 | 631 | error(opts->action == REPLAY_REVERT |
cb6aeb22 ÆAB |
632 | ? _("could not revert %s... %s") |
633 | : _("could not apply %s... %s"), | |
981ff5c3 JN |
634 | find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), |
635 | msg.subject); | |
82352cb6 | 636 | print_advice(res == 1); |
80e1f791 | 637 | rerere(opts->allow_rerere_auto); |
7b53b92f | 638 | } else { |
80e1f791 RR |
639 | if (!opts->no_commit) |
640 | res = run_git_commit(defmsg, opts); | |
9509af68 | 641 | } |
2fb0e14f | 642 | |
2fb0e14f | 643 | free_message(&msg); |
d258b258 | 644 | free(defmsg); |
9509af68 | 645 | |
3b2c5b6d | 646 | return res; |
9509af68 JS |
647 | } |
648 | ||
7f13334e | 649 | static void prepare_revs(struct replay_opts *opts) |
7e2bfd3f | 650 | { |
644a3690 | 651 | if (opts->action != REPLAY_REVERT) |
7f13334e | 652 | opts->revs->reverse ^= 1; |
e0ef8495 | 653 | |
7f13334e | 654 | if (prepare_revision_walk(opts->revs)) |
f30f71ce | 655 | die(_("revision walk setup failed")); |
7e2bfd3f | 656 | |
7f13334e | 657 | if (!opts->revs->commits) |
f30f71ce | 658 | die(_("empty commit set passed")); |
7e2bfd3f CC |
659 | } |
660 | ||
80e1f791 | 661 | static void read_and_refresh_cache(struct replay_opts *opts) |
f6ce1f25 JN |
662 | { |
663 | static struct lock_file index_lock; | |
664 | int index_fd = hold_locked_index(&index_lock, 0); | |
665 | if (read_index_preload(&the_index, NULL) < 0) | |
80e1f791 | 666 | die(_("git %s: failed to read the index"), action_name(opts)); |
f6ce1f25 JN |
667 | refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL); |
668 | if (the_index.cache_changed) { | |
669 | if (write_index(&the_index, index_fd) || | |
670 | commit_locked_index(&index_lock)) | |
80e1f791 | 671 | die(_("git %s: failed to refresh the index"), action_name(opts)); |
f6ce1f25 JN |
672 | } |
673 | rollback_lock_file(&index_lock); | |
674 | } | |
675 | ||
04d3d3cf RR |
676 | /* |
677 | * Append a commit to the end of the commit_list. | |
678 | * | |
679 | * next starts by pointing to the variable that holds the head of an | |
680 | * empty commit_list, and is updated to point to the "next" field of | |
681 | * the last item on the list as new commits are appended. | |
682 | * | |
683 | * Usage example: | |
684 | * | |
685 | * struct commit_list *list; | |
686 | * struct commit_list **next = &list; | |
687 | * | |
688 | * next = commit_list_append(c1, next); | |
689 | * next = commit_list_append(c2, next); | |
690 | * assert(commit_list_count(list) == 2); | |
691 | * return list; | |
692 | */ | |
fb3198c5 JH |
693 | static struct commit_list **commit_list_append(struct commit *commit, |
694 | struct commit_list **next) | |
04d3d3cf RR |
695 | { |
696 | struct commit_list *new = xmalloc(sizeof(struct commit_list)); | |
697 | new->item = commit; | |
698 | *next = new; | |
699 | new->next = NULL; | |
700 | return &new->next; | |
701 | } | |
702 | ||
703 | static int format_todo(struct strbuf *buf, struct commit_list *todo_list, | |
704 | struct replay_opts *opts) | |
705 | { | |
706 | struct commit_list *cur = NULL; | |
04d3d3cf | 707 | const char *sha1_abbrev = NULL; |
644a3690 | 708 | const char *action_str = opts->action == REPLAY_REVERT ? "revert" : "pick"; |
9e131364 RR |
709 | const char *subject; |
710 | int subject_len; | |
04d3d3cf RR |
711 | |
712 | for (cur = todo_list; cur; cur = cur->next) { | |
713 | sha1_abbrev = find_unique_abbrev(cur->item->object.sha1, DEFAULT_ABBREV); | |
9e131364 RR |
714 | subject_len = find_commit_subject(cur->item->buffer, &subject); |
715 | strbuf_addf(buf, "%s %s %.*s\n", action_str, sha1_abbrev, | |
716 | subject_len, subject); | |
04d3d3cf RR |
717 | } |
718 | return 0; | |
719 | } | |
720 | ||
6bc1a235 | 721 | static struct commit *parse_insn_line(char *bol, char *eol, struct replay_opts *opts) |
5a5d80f4 RR |
722 | { |
723 | unsigned char commit_sha1[20]; | |
5a5d80f4 | 724 | enum replay_action action; |
6bc1a235 | 725 | char *end_of_object_name; |
0db76962 | 726 | int saved, status, padding; |
5a5d80f4 | 727 | |
0db76962 | 728 | if (!prefixcmp(bol, "pick")) { |
644a3690 | 729 | action = REPLAY_PICK; |
0db76962 RR |
730 | bol += strlen("pick"); |
731 | } else if (!prefixcmp(bol, "revert")) { | |
644a3690 | 732 | action = REPLAY_REVERT; |
0db76962 | 733 | bol += strlen("revert"); |
5a5d80f4 RR |
734 | } else |
735 | return NULL; | |
736 | ||
0db76962 RR |
737 | /* Eat up extra spaces/ tabs before object name */ |
738 | padding = strspn(bol, " \t"); | |
739 | if (!padding) | |
5a5d80f4 | 740 | return NULL; |
0db76962 | 741 | bol += padding; |
5a5d80f4 | 742 | |
0db76962 | 743 | end_of_object_name = bol + strcspn(bol, " \t\n"); |
6bc1a235 RR |
744 | saved = *end_of_object_name; |
745 | *end_of_object_name = '\0'; | |
746 | status = get_sha1(bol, commit_sha1); | |
747 | *end_of_object_name = saved; | |
5a5d80f4 RR |
748 | |
749 | /* | |
750 | * Verify that the action matches up with the one in | |
751 | * opts; we don't support arbitrary instructions | |
752 | */ | |
753 | if (action != opts->action) { | |
754 | const char *action_str; | |
644a3690 | 755 | action_str = action == REPLAY_REVERT ? "revert" : "cherry-pick"; |
5a5d80f4 RR |
756 | error(_("Cannot %s during a %s"), action_str, action_name(opts)); |
757 | return NULL; | |
758 | } | |
759 | ||
6bc1a235 | 760 | if (status < 0) |
5a5d80f4 RR |
761 | return NULL; |
762 | ||
763 | return lookup_commit_reference(commit_sha1); | |
764 | } | |
765 | ||
766 | static int parse_insn_buffer(char *buf, struct commit_list **todo_list, | |
767 | struct replay_opts *opts) | |
768 | { | |
769 | struct commit_list **next = todo_list; | |
770 | struct commit *commit; | |
771 | char *p = buf; | |
772 | int i; | |
773 | ||
774 | for (i = 1; *p; i++) { | |
6bc1a235 RR |
775 | char *eol = strchrnul(p, '\n'); |
776 | commit = parse_insn_line(p, eol, opts); | |
5a5d80f4 RR |
777 | if (!commit) |
778 | return error(_("Could not parse line %d."), i); | |
779 | next = commit_list_append(commit, next); | |
6bc1a235 | 780 | p = *eol ? eol + 1 : eol; |
5a5d80f4 RR |
781 | } |
782 | if (!*todo_list) | |
783 | return error(_("No commits parsed.")); | |
784 | return 0; | |
785 | } | |
786 | ||
787 | static void read_populate_todo(struct commit_list **todo_list, | |
788 | struct replay_opts *opts) | |
789 | { | |
790 | const char *todo_file = git_path(SEQ_TODO_FILE); | |
791 | struct strbuf buf = STRBUF_INIT; | |
792 | int fd, res; | |
793 | ||
794 | fd = open(todo_file, O_RDONLY); | |
795 | if (fd < 0) | |
5e1e6b93 | 796 | die_errno(_("Could not open %s"), todo_file); |
5a5d80f4 RR |
797 | if (strbuf_read(&buf, fd, 0) < 0) { |
798 | close(fd); | |
799 | strbuf_release(&buf); | |
800 | die(_("Could not read %s."), todo_file); | |
801 | } | |
802 | close(fd); | |
803 | ||
804 | res = parse_insn_buffer(buf.buf, todo_list, opts); | |
805 | strbuf_release(&buf); | |
806 | if (res) | |
807 | die(_("Unusable instruction sheet: %s"), todo_file); | |
808 | } | |
809 | ||
810 | static int populate_opts_cb(const char *key, const char *value, void *data) | |
811 | { | |
812 | struct replay_opts *opts = data; | |
813 | int error_flag = 1; | |
814 | ||
815 | if (!value) | |
816 | error_flag = 0; | |
817 | else if (!strcmp(key, "options.no-commit")) | |
818 | opts->no_commit = git_config_bool_or_int(key, value, &error_flag); | |
819 | else if (!strcmp(key, "options.edit")) | |
820 | opts->edit = git_config_bool_or_int(key, value, &error_flag); | |
821 | else if (!strcmp(key, "options.signoff")) | |
822 | opts->signoff = git_config_bool_or_int(key, value, &error_flag); | |
823 | else if (!strcmp(key, "options.record-origin")) | |
824 | opts->record_origin = git_config_bool_or_int(key, value, &error_flag); | |
825 | else if (!strcmp(key, "options.allow-ff")) | |
826 | opts->allow_ff = git_config_bool_or_int(key, value, &error_flag); | |
827 | else if (!strcmp(key, "options.mainline")) | |
828 | opts->mainline = git_config_int(key, value); | |
829 | else if (!strcmp(key, "options.strategy")) | |
830 | git_config_string(&opts->strategy, key, value); | |
831 | else if (!strcmp(key, "options.strategy-option")) { | |
832 | ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc); | |
833 | opts->xopts[opts->xopts_nr++] = xstrdup(value); | |
834 | } else | |
835 | return error(_("Invalid key: %s"), key); | |
836 | ||
837 | if (!error_flag) | |
838 | return error(_("Invalid value for %s: %s"), key, value); | |
839 | ||
840 | return 0; | |
841 | } | |
842 | ||
843 | static void read_populate_opts(struct replay_opts **opts_ptr) | |
844 | { | |
845 | const char *opts_file = git_path(SEQ_OPTS_FILE); | |
846 | ||
847 | if (!file_exists(opts_file)) | |
848 | return; | |
849 | if (git_config_from_file(populate_opts_cb, opts_file, *opts_ptr) < 0) | |
850 | die(_("Malformed options sheet: %s"), opts_file); | |
851 | } | |
852 | ||
04d3d3cf RR |
853 | static void walk_revs_populate_todo(struct commit_list **todo_list, |
854 | struct replay_opts *opts) | |
7af46595 | 855 | { |
708f9d96 | 856 | struct commit *commit; |
04d3d3cf | 857 | struct commit_list **next; |
7e2bfd3f | 858 | |
7f13334e | 859 | prepare_revs(opts); |
04d3d3cf RR |
860 | |
861 | next = todo_list; | |
7f13334e | 862 | while ((commit = get_revision(opts->revs))) |
04d3d3cf RR |
863 | next = commit_list_append(commit, next); |
864 | } | |
865 | ||
21afd080 | 866 | static int create_seq_dir(void) |
04d3d3cf RR |
867 | { |
868 | const char *seq_dir = git_path(SEQ_DIR); | |
869 | ||
b8c74690 JN |
870 | if (file_exists(seq_dir)) { |
871 | error(_("a cherry-pick or revert is already in progress")); | |
539047c1 | 872 | advise(_("try \"git cherry-pick (--continue | --quit | --abort)\"")); |
b8c74690 JN |
873 | return -1; |
874 | } | |
21afd080 | 875 | else if (mkdir(seq_dir, 0777) < 0) |
5e1e6b93 | 876 | die_errno(_("Could not create sequencer directory %s"), seq_dir); |
21afd080 | 877 | return 0; |
04d3d3cf RR |
878 | } |
879 | ||
880 | static void save_head(const char *head) | |
881 | { | |
882 | const char *head_file = git_path(SEQ_HEAD_FILE); | |
883 | static struct lock_file head_lock; | |
884 | struct strbuf buf = STRBUF_INIT; | |
885 | int fd; | |
886 | ||
887 | fd = hold_lock_file_for_update(&head_lock, head_file, LOCK_DIE_ON_ERROR); | |
888 | strbuf_addf(&buf, "%s\n", head); | |
889 | if (write_in_full(fd, buf.buf, buf.len) < 0) | |
5e1e6b93 | 890 | die_errno(_("Could not write to %s"), head_file); |
04d3d3cf RR |
891 | if (commit_lock_file(&head_lock) < 0) |
892 | die(_("Error wrapping up %s."), head_file); | |
893 | } | |
894 | ||
539047c1 JN |
895 | static int reset_for_rollback(const unsigned char *sha1) |
896 | { | |
897 | const char *argv[4]; /* reset --merge <arg> + NULL */ | |
898 | argv[0] = "reset"; | |
899 | argv[1] = "--merge"; | |
900 | argv[2] = sha1_to_hex(sha1); | |
901 | argv[3] = NULL; | |
902 | return run_command_v_opt(argv, RUN_GIT_CMD); | |
903 | } | |
904 | ||
905 | static int rollback_single_pick(void) | |
906 | { | |
907 | unsigned char head_sha1[20]; | |
908 | ||
909 | if (!file_exists(git_path("CHERRY_PICK_HEAD")) && | |
910 | !file_exists(git_path("REVERT_HEAD"))) | |
911 | return error(_("no cherry-pick or revert in progress")); | |
e4776bd9 | 912 | if (read_ref_full("HEAD", head_sha1, 0, NULL)) |
539047c1 JN |
913 | return error(_("cannot resolve HEAD")); |
914 | if (is_null_sha1(head_sha1)) | |
915 | return error(_("cannot abort from a branch yet to be born")); | |
916 | return reset_for_rollback(head_sha1); | |
917 | } | |
918 | ||
919 | static int sequencer_rollback(struct replay_opts *opts) | |
920 | { | |
921 | const char *filename; | |
922 | FILE *f; | |
923 | unsigned char sha1[20]; | |
924 | struct strbuf buf = STRBUF_INIT; | |
925 | ||
926 | filename = git_path(SEQ_HEAD_FILE); | |
927 | f = fopen(filename, "r"); | |
928 | if (!f && errno == ENOENT) { | |
929 | /* | |
930 | * There is no multiple-cherry-pick in progress. | |
931 | * If CHERRY_PICK_HEAD or REVERT_HEAD indicates | |
932 | * a single-cherry-pick in progress, abort that. | |
933 | */ | |
934 | return rollback_single_pick(); | |
935 | } | |
936 | if (!f) | |
937 | return error(_("cannot open %s: %s"), filename, | |
938 | strerror(errno)); | |
939 | if (strbuf_getline(&buf, f, '\n')) { | |
940 | error(_("cannot read %s: %s"), filename, ferror(f) ? | |
941 | strerror(errno) : _("unexpected end of file")); | |
2a4037d0 | 942 | fclose(f); |
539047c1 JN |
943 | goto fail; |
944 | } | |
2a4037d0 | 945 | fclose(f); |
539047c1 JN |
946 | if (get_sha1_hex(buf.buf, sha1) || buf.buf[40] != '\0') { |
947 | error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"), | |
948 | filename); | |
949 | goto fail; | |
950 | } | |
951 | if (reset_for_rollback(sha1)) | |
952 | goto fail; | |
d596118d | 953 | remove_sequencer_state(); |
539047c1 | 954 | strbuf_release(&buf); |
539047c1 JN |
955 | return 0; |
956 | fail: | |
957 | strbuf_release(&buf); | |
539047c1 JN |
958 | return -1; |
959 | } | |
960 | ||
04d3d3cf RR |
961 | static void save_todo(struct commit_list *todo_list, struct replay_opts *opts) |
962 | { | |
963 | const char *todo_file = git_path(SEQ_TODO_FILE); | |
964 | static struct lock_file todo_lock; | |
965 | struct strbuf buf = STRBUF_INIT; | |
966 | int fd; | |
967 | ||
968 | fd = hold_lock_file_for_update(&todo_lock, todo_file, LOCK_DIE_ON_ERROR); | |
969 | if (format_todo(&buf, todo_list, opts) < 0) | |
970 | die(_("Could not format %s."), todo_file); | |
971 | if (write_in_full(fd, buf.buf, buf.len) < 0) { | |
972 | strbuf_release(&buf); | |
5e1e6b93 | 973 | die_errno(_("Could not write to %s"), todo_file); |
04d3d3cf RR |
974 | } |
975 | if (commit_lock_file(&todo_lock) < 0) { | |
976 | strbuf_release(&buf); | |
977 | die(_("Error wrapping up %s."), todo_file); | |
7af46595 | 978 | } |
04d3d3cf RR |
979 | strbuf_release(&buf); |
980 | } | |
7af46595 | 981 | |
6f032263 RR |
982 | static void save_opts(struct replay_opts *opts) |
983 | { | |
984 | const char *opts_file = git_path(SEQ_OPTS_FILE); | |
985 | ||
986 | if (opts->no_commit) | |
987 | git_config_set_in_file(opts_file, "options.no-commit", "true"); | |
988 | if (opts->edit) | |
989 | git_config_set_in_file(opts_file, "options.edit", "true"); | |
990 | if (opts->signoff) | |
991 | git_config_set_in_file(opts_file, "options.signoff", "true"); | |
992 | if (opts->record_origin) | |
993 | git_config_set_in_file(opts_file, "options.record-origin", "true"); | |
994 | if (opts->allow_ff) | |
995 | git_config_set_in_file(opts_file, "options.allow-ff", "true"); | |
996 | if (opts->mainline) { | |
997 | struct strbuf buf = STRBUF_INIT; | |
998 | strbuf_addf(&buf, "%d", opts->mainline); | |
999 | git_config_set_in_file(opts_file, "options.mainline", buf.buf); | |
1000 | strbuf_release(&buf); | |
1001 | } | |
1002 | if (opts->strategy) | |
1003 | git_config_set_in_file(opts_file, "options.strategy", opts->strategy); | |
1004 | if (opts->xopts) { | |
1005 | int i; | |
1006 | for (i = 0; i < opts->xopts_nr; i++) | |
1007 | git_config_set_multivar_in_file(opts_file, | |
1008 | "options.strategy-option", | |
1009 | opts->xopts[i], "^$", 0); | |
1010 | } | |
1011 | } | |
7af46595 | 1012 | |
21b14778 | 1013 | static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts) |
04d3d3cf | 1014 | { |
04d3d3cf RR |
1015 | struct commit_list *cur; |
1016 | int res; | |
7e2bfd3f | 1017 | |
80e1f791 | 1018 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); |
9044143f RR |
1019 | if (opts->allow_ff) |
1020 | assert(!(opts->signoff || opts->no_commit || | |
1021 | opts->record_origin || opts->edit)); | |
80e1f791 | 1022 | read_and_refresh_cache(opts); |
7af46595 | 1023 | |
04d3d3cf RR |
1024 | for (cur = todo_list; cur; cur = cur->next) { |
1025 | save_todo(cur, opts); | |
1026 | res = do_pick_commit(cur->item, opts); | |
218b65fb | 1027 | if (res) |
7e2bfd3f CC |
1028 | return res; |
1029 | } | |
1030 | ||
04d3d3cf RR |
1031 | /* |
1032 | * Sequence of picks finished successfully; cleanup by | |
1033 | * removing the .git/sequencer directory | |
1034 | */ | |
d596118d | 1035 | remove_sequencer_state(); |
7e2bfd3f | 1036 | return 0; |
7af46595 CC |
1037 | } |
1038 | ||
093a3091 JN |
1039 | static int continue_single_pick(void) |
1040 | { | |
1041 | const char *argv[] = { "commit", NULL }; | |
1042 | ||
1043 | if (!file_exists(git_path("CHERRY_PICK_HEAD")) && | |
1044 | !file_exists(git_path("REVERT_HEAD"))) | |
1045 | return error(_("no cherry-pick or revert in progress")); | |
1046 | return run_command_v_opt(argv, RUN_GIT_CMD); | |
1047 | } | |
1048 | ||
1df9bf46 JN |
1049 | static int sequencer_continue(struct replay_opts *opts) |
1050 | { | |
1051 | struct commit_list *todo_list = NULL; | |
1052 | ||
1053 | if (!file_exists(git_path(SEQ_TODO_FILE))) | |
093a3091 | 1054 | return continue_single_pick(); |
1df9bf46 JN |
1055 | read_populate_opts(&opts); |
1056 | read_populate_todo(&todo_list, opts); | |
1057 | ||
1058 | /* Verify that the conflict has been resolved */ | |
093a3091 JN |
1059 | if (file_exists(git_path("CHERRY_PICK_HEAD")) || |
1060 | file_exists(git_path("REVERT_HEAD"))) { | |
1061 | int ret = continue_single_pick(); | |
1062 | if (ret) | |
1063 | return ret; | |
1064 | } | |
1065 | if (index_differs_from("HEAD", 0)) | |
1066 | return error_dirty_index(opts); | |
1067 | todo_list = todo_list->next; | |
1df9bf46 JN |
1068 | return pick_commits(todo_list, opts); |
1069 | } | |
1070 | ||
7acaaac2 JN |
1071 | static int single_pick(struct commit *cmit, struct replay_opts *opts) |
1072 | { | |
1073 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); | |
1074 | return do_pick_commit(cmit, opts); | |
1075 | } | |
1076 | ||
21b14778 RR |
1077 | static int pick_revisions(struct replay_opts *opts) |
1078 | { | |
1079 | struct commit_list *todo_list = NULL; | |
1080 | unsigned char sha1[20]; | |
1081 | ||
7f13334e JN |
1082 | if (opts->subcommand == REPLAY_NONE) |
1083 | assert(opts->revs); | |
1084 | ||
21b14778 RR |
1085 | read_and_refresh_cache(opts); |
1086 | ||
1087 | /* | |
1088 | * Decide what to do depending on the arguments; a fresh | |
1089 | * cherry-pick should be handled differently from an existing | |
1090 | * one that is being continued | |
1091 | */ | |
f80a8726 | 1092 | if (opts->subcommand == REPLAY_REMOVE_STATE) { |
d596118d | 1093 | remove_sequencer_state(); |
26ae337b | 1094 | return 0; |
dffc8600 | 1095 | } |
539047c1 JN |
1096 | if (opts->subcommand == REPLAY_ROLLBACK) |
1097 | return sequencer_rollback(opts); | |
1df9bf46 JN |
1098 | if (opts->subcommand == REPLAY_CONTINUE) |
1099 | return sequencer_continue(opts); | |
21afd080 | 1100 | |
7acaaac2 JN |
1101 | /* |
1102 | * If we were called as "git cherry-pick <commit>", just | |
1103 | * cherry-pick/revert it, set CHERRY_PICK_HEAD / | |
1104 | * REVERT_HEAD, and don't touch the sequencer state. | |
1105 | * This means it is possible to cherry-pick in the middle | |
1106 | * of a cherry-pick sequence. | |
1107 | */ | |
1108 | if (opts->revs->cmdline.nr == 1 && | |
1109 | opts->revs->cmdline.rev->whence == REV_CMD_REV && | |
1110 | opts->revs->no_walk && | |
1111 | !opts->revs->cmdline.rev->flags) { | |
1112 | struct commit *cmit; | |
1113 | if (prepare_revision_walk(opts->revs)) | |
1114 | die(_("revision walk setup failed")); | |
1115 | cmit = get_revision(opts->revs); | |
1116 | if (!cmit || get_revision(opts->revs)) | |
1117 | die("BUG: expected exactly one commit from walk"); | |
1118 | return single_pick(cmit, opts); | |
1119 | } | |
1120 | ||
dffc8600 JN |
1121 | /* |
1122 | * Start a new cherry-pick/ revert sequence; but | |
1123 | * first, make sure that an existing one isn't in | |
1124 | * progress | |
1125 | */ | |
1126 | ||
1127 | walk_revs_populate_todo(&todo_list, opts); | |
b8c74690 | 1128 | if (create_seq_dir() < 0) |
dffc8600 | 1129 | return -1; |
dffc8600 | 1130 | if (get_sha1("HEAD", sha1)) { |
644a3690 | 1131 | if (opts->action == REPLAY_REVERT) |
dffc8600 JN |
1132 | return error(_("Can't revert as initial commit")); |
1133 | return error(_("Can't cherry-pick into empty head")); | |
21b14778 | 1134 | } |
dffc8600 JN |
1135 | save_head(sha1_to_hex(sha1)); |
1136 | save_opts(opts); | |
21b14778 RR |
1137 | return pick_commits(todo_list, opts); |
1138 | } | |
1139 | ||
9509af68 JS |
1140 | int cmd_revert(int argc, const char **argv, const char *prefix) |
1141 | { | |
80e1f791 | 1142 | struct replay_opts opts; |
cf3e2486 | 1143 | int res; |
80e1f791 RR |
1144 | |
1145 | memset(&opts, 0, sizeof(opts)); | |
9509af68 | 1146 | if (isatty(0)) |
80e1f791 | 1147 | opts.edit = 1; |
644a3690 | 1148 | opts.action = REPLAY_REVERT; |
89641472 RR |
1149 | git_config(git_default_config, NULL); |
1150 | parse_args(argc, argv, &opts); | |
cf3e2486 RR |
1151 | res = pick_revisions(&opts); |
1152 | if (res < 0) | |
1153 | die(_("revert failed")); | |
1154 | return res; | |
9509af68 JS |
1155 | } |
1156 | ||
1157 | int cmd_cherry_pick(int argc, const char **argv, const char *prefix) | |
1158 | { | |
80e1f791 | 1159 | struct replay_opts opts; |
cf3e2486 | 1160 | int res; |
80e1f791 RR |
1161 | |
1162 | memset(&opts, 0, sizeof(opts)); | |
644a3690 | 1163 | opts.action = REPLAY_PICK; |
89641472 RR |
1164 | git_config(git_default_config, NULL); |
1165 | parse_args(argc, argv, &opts); | |
cf3e2486 RR |
1166 | res = pick_revisions(&opts); |
1167 | if (res < 0) | |
1168 | die(_("cherry-pick failed")); | |
1169 | return res; | |
9509af68 | 1170 | } |