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