]>
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 }; |
26ae337b | 43 | enum replay_subcommand { REPLAY_NONE, REPLAY_RESET }; |
80e1f791 RR |
44 | |
45 | struct replay_opts { | |
46 | enum replay_action action; | |
26ae337b | 47 | enum replay_subcommand subcommand; |
80e1f791 RR |
48 | |
49 | /* Boolean options */ | |
50 | int edit; | |
51 | int record_origin; | |
52 | int no_commit; | |
53 | int signoff; | |
54 | int allow_ff; | |
55 | int allow_rerere_auto; | |
56 | ||
57 | int mainline; | |
58 | int commit_argc; | |
59 | const char **commit_argv; | |
60 | ||
61 | /* Merge strategy */ | |
62 | const char *strategy; | |
63 | const char **xopts; | |
64 | size_t xopts_nr, xopts_alloc; | |
65 | }; | |
9509af68 JS |
66 | |
67 | #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION" | |
68 | ||
80e1f791 RR |
69 | static const char *action_name(const struct replay_opts *opts) |
70 | { | |
71 | return opts->action == REVERT ? "revert" : "cherry-pick"; | |
72 | } | |
73 | ||
d6856540 JN |
74 | static char *get_encoding(const char *message); |
75 | ||
80e1f791 | 76 | static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts) |
e0ef8495 | 77 | { |
80e1f791 | 78 | return opts->action == REVERT ? revert_usage : cherry_pick_usage; |
e0ef8495 JN |
79 | } |
80 | ||
67ac1e1d JN |
81 | static int option_parse_x(const struct option *opt, |
82 | const char *arg, int unset) | |
83 | { | |
80e1f791 RR |
84 | struct replay_opts **opts_ptr = opt->value; |
85 | struct replay_opts *opts = *opts_ptr; | |
86 | ||
67ac1e1d JN |
87 | if (unset) |
88 | return 0; | |
89 | ||
80e1f791 RR |
90 | ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc); |
91 | opts->xopts[opts->xopts_nr++] = xstrdup(arg); | |
67ac1e1d JN |
92 | return 0; |
93 | } | |
94 | ||
9044143f RR |
95 | static void verify_opt_compatible(const char *me, const char *base_opt, ...) |
96 | { | |
97 | const char *this_opt; | |
98 | va_list ap; | |
99 | ||
100 | va_start(ap, base_opt); | |
101 | while ((this_opt = va_arg(ap, const char *))) { | |
102 | if (va_arg(ap, int)) | |
103 | break; | |
104 | } | |
105 | va_end(ap); | |
106 | ||
107 | if (this_opt) | |
108 | die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt); | |
109 | } | |
110 | ||
80e1f791 | 111 | static void parse_args(int argc, const char **argv, struct replay_opts *opts) |
9509af68 | 112 | { |
80e1f791 | 113 | const char * const * usage_str = revert_or_cherry_pick_usage(opts); |
9044143f | 114 | const char *me = action_name(opts); |
f8103794 | 115 | int noop; |
26ae337b | 116 | int reset = 0; |
f8103794 | 117 | struct option options[] = { |
26ae337b | 118 | OPT_BOOLEAN(0, "reset", &reset, "forget the current operation"), |
80e1f791 RR |
119 | OPT_BOOLEAN('n', "no-commit", &opts->no_commit, "don't automatically commit"), |
120 | OPT_BOOLEAN('e', "edit", &opts->edit, "edit the commit message"), | |
5e31075a RR |
121 | { OPTION_BOOLEAN, 'r', NULL, &noop, NULL, "no-op (backward compatibility)", |
122 | PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 0 }, | |
80e1f791 RR |
123 | OPT_BOOLEAN('s', "signoff", &opts->signoff, "add Signed-off-by:"), |
124 | OPT_INTEGER('m', "mainline", &opts->mainline, "parent number"), | |
125 | OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto), | |
126 | OPT_STRING(0, "strategy", &opts->strategy, "strategy", "merge strategy"), | |
127 | OPT_CALLBACK('X', "strategy-option", &opts, "option", | |
67ac1e1d | 128 | "option for merge strategy", option_parse_x), |
f8103794 | 129 | OPT_END(), |
c62f6ec3 JH |
130 | OPT_END(), |
131 | OPT_END(), | |
f8103794 PH |
132 | }; |
133 | ||
80e1f791 | 134 | if (opts->action == CHERRY_PICK) { |
c62f6ec3 | 135 | struct option cp_extra[] = { |
80e1f791 RR |
136 | OPT_BOOLEAN('x', NULL, &opts->record_origin, "append commit name"), |
137 | OPT_BOOLEAN(0, "ff", &opts->allow_ff, "allow fast-forward"), | |
c62f6ec3 JH |
138 | OPT_END(), |
139 | }; | |
140 | if (parse_options_concat(options, ARRAY_SIZE(options), cp_extra)) | |
f30f71ce | 141 | die(_("program error")); |
c62f6ec3 JH |
142 | } |
143 | ||
80e1f791 RR |
144 | opts->commit_argc = parse_options(argc, argv, NULL, options, usage_str, |
145 | PARSE_OPT_KEEP_ARGV0 | | |
146 | PARSE_OPT_KEEP_UNKNOWN); | |
26ae337b RR |
147 | |
148 | /* Set the subcommand */ | |
149 | if (reset) | |
150 | opts->subcommand = REPLAY_RESET; | |
151 | else | |
152 | opts->subcommand = REPLAY_NONE; | |
153 | ||
154 | /* Check for incompatible command line arguments */ | |
155 | if (opts->subcommand == REPLAY_RESET) { | |
156 | verify_opt_compatible(me, "--reset", | |
157 | "--no-commit", opts->no_commit, | |
158 | "--signoff", opts->signoff, | |
159 | "--mainline", opts->mainline, | |
160 | "--strategy", opts->strategy ? 1 : 0, | |
161 | "--strategy-option", opts->xopts ? 1 : 0, | |
162 | "-x", opts->record_origin, | |
163 | "--ff", opts->allow_ff, | |
164 | NULL); | |
165 | } | |
166 | ||
167 | else if (opts->commit_argc < 2) | |
f8103794 | 168 | usage_with_options(usage_str, options); |
9509af68 | 169 | |
9044143f RR |
170 | if (opts->allow_ff) |
171 | verify_opt_compatible(me, "--ff", | |
172 | "--signoff", opts->signoff, | |
173 | "--no-commit", opts->no_commit, | |
174 | "-x", opts->record_origin, | |
175 | "--edit", opts->edit, | |
176 | NULL); | |
80e1f791 | 177 | opts->commit_argv = argv; |
9509af68 JS |
178 | } |
179 | ||
d6856540 JN |
180 | struct commit_message { |
181 | char *parent_label; | |
182 | const char *label; | |
183 | const char *subject; | |
184 | char *reencoded_message; | |
185 | const char *message; | |
186 | }; | |
187 | ||
708f9d96 | 188 | static int get_message(struct commit *commit, struct commit_message *out) |
9509af68 | 189 | { |
d6856540 | 190 | const char *encoding; |
dfe7effe CC |
191 | const char *abbrev, *subject; |
192 | int abbrev_len, subject_len; | |
d6856540 | 193 | char *q; |
9509af68 | 194 | |
708f9d96 | 195 | if (!commit->buffer) |
d6856540 | 196 | return -1; |
708f9d96 | 197 | encoding = get_encoding(commit->buffer); |
d6856540 JN |
198 | if (!encoding) |
199 | encoding = "UTF-8"; | |
200 | if (!git_commit_encoding) | |
201 | git_commit_encoding = "UTF-8"; | |
43acff34 JN |
202 | |
203 | out->reencoded_message = NULL; | |
708f9d96 | 204 | out->message = commit->buffer; |
43acff34 | 205 | if (strcmp(encoding, git_commit_encoding)) |
708f9d96 | 206 | out->reencoded_message = reencode_string(commit->buffer, |
43acff34 JN |
207 | git_commit_encoding, encoding); |
208 | if (out->reencoded_message) | |
d6856540 JN |
209 | out->message = out->reencoded_message; |
210 | ||
211 | abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV); | |
212 | abbrev_len = strlen(abbrev); | |
213 | ||
dfe7effe | 214 | subject_len = find_commit_subject(out->message, &subject); |
d6856540 JN |
215 | |
216 | out->parent_label = xmalloc(strlen("parent of ") + abbrev_len + | |
dfe7effe | 217 | strlen("... ") + subject_len + 1); |
d6856540 JN |
218 | q = out->parent_label; |
219 | q = mempcpy(q, "parent of ", strlen("parent of ")); | |
220 | out->label = q; | |
221 | q = mempcpy(q, abbrev, abbrev_len); | |
222 | q = mempcpy(q, "... ", strlen("... ")); | |
223 | out->subject = q; | |
dfe7effe | 224 | q = mempcpy(q, subject, subject_len); |
d6856540 JN |
225 | *q = '\0'; |
226 | return 0; | |
227 | } | |
228 | ||
229 | static void free_message(struct commit_message *msg) | |
230 | { | |
231 | free(msg->parent_label); | |
232 | free(msg->reencoded_message); | |
9509af68 JS |
233 | } |
234 | ||
52fae7de | 235 | static char *get_encoding(const char *message) |
9509af68 JS |
236 | { |
237 | const char *p = message, *eol; | |
238 | ||
9509af68 JS |
239 | while (*p && *p != '\n') { |
240 | for (eol = p + 1; *eol && *eol != '\n'; eol++) | |
241 | ; /* do nothing */ | |
242 | if (!prefixcmp(p, "encoding ")) { | |
243 | char *result = xmalloc(eol - 8 - p); | |
244 | strlcpy(result, p + 9, eol - 8 - p); | |
245 | return result; | |
246 | } | |
247 | p = eol; | |
248 | if (*p == '\n') | |
249 | p++; | |
250 | } | |
251 | return NULL; | |
252 | } | |
253 | ||
708f9d96 | 254 | static void write_cherry_pick_head(struct commit *commit) |
9509af68 | 255 | { |
d7e5c0cb JS |
256 | int fd; |
257 | struct strbuf buf = STRBUF_INIT; | |
258 | ||
259 | strbuf_addf(&buf, "%s\n", sha1_to_hex(commit->object.sha1)); | |
260 | ||
261 | fd = open(git_path("CHERRY_PICK_HEAD"), O_WRONLY | O_CREAT, 0666); | |
262 | if (fd < 0) | |
6c80cd29 | 263 | die_errno(_("Could not open '%s' for writing"), |
d7e5c0cb JS |
264 | git_path("CHERRY_PICK_HEAD")); |
265 | if (write_in_full(fd, buf.buf, buf.len) != buf.len || close(fd)) | |
6c80cd29 | 266 | die_errno(_("Could not write to '%s'"), git_path("CHERRY_PICK_HEAD")); |
d7e5c0cb | 267 | strbuf_release(&buf); |
9509af68 JS |
268 | } |
269 | ||
314eeb6e | 270 | static void print_advice(void) |
804c7174 | 271 | { |
804c7174 | 272 | char *msg = getenv("GIT_CHERRY_PICK_HELP"); |
804c7174 | 273 | |
314eeb6e JN |
274 | if (msg) { |
275 | fprintf(stderr, "%s\n", msg); | |
d7e5c0cb JS |
276 | /* |
277 | * A conflict has occured but the porcelain | |
278 | * (typically rebase --interactive) wants to take care | |
279 | * of the commit itself so remove CHERRY_PICK_HEAD | |
280 | */ | |
281 | unlink(git_path("CHERRY_PICK_HEAD")); | |
314eeb6e | 282 | return; |
804c7174 | 283 | } |
804c7174 | 284 | |
314eeb6e JN |
285 | advise("after resolving the conflicts, mark the corrected paths"); |
286 | advise("with 'git add <paths>' or 'git rm <paths>'"); | |
37f7a857 | 287 | advise("and commit the result with 'git commit'"); |
804c7174 WC |
288 | } |
289 | ||
bc84a7fb CC |
290 | static void write_message(struct strbuf *msgbuf, const char *filename) |
291 | { | |
292 | static struct lock_file msg_file; | |
293 | ||
294 | int msg_fd = hold_lock_file_for_update(&msg_file, filename, | |
295 | LOCK_DIE_ON_ERROR); | |
296 | if (write_in_full(msg_fd, msgbuf->buf, msgbuf->len) < 0) | |
f30f71ce | 297 | die_errno(_("Could not write to %s."), filename); |
bc84a7fb CC |
298 | strbuf_release(msgbuf); |
299 | if (commit_lock_file(&msg_file) < 0) | |
f30f71ce | 300 | die(_("Error wrapping up %s"), filename); |
bc84a7fb CC |
301 | } |
302 | ||
6eb1b437 MV |
303 | static struct tree *empty_tree(void) |
304 | { | |
305 | struct tree *tree = xcalloc(1, sizeof(struct tree)); | |
306 | ||
307 | tree->object.parsed = 1; | |
308 | tree->object.type = OBJ_TREE; | |
309 | pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1); | |
310 | return tree; | |
311 | } | |
312 | ||
80e1f791 | 313 | static NORETURN void die_dirty_index(struct replay_opts *opts) |
d38a30df MM |
314 | { |
315 | if (read_cache_unmerged()) { | |
80e1f791 | 316 | die_resolve_conflict(action_name(opts)); |
d38a30df | 317 | } else { |
d3ee177e | 318 | if (advice_commit_before_merge) { |
80e1f791 | 319 | if (opts->action == REVERT) |
d3ee177e ÆAB |
320 | die(_("Your local changes would be overwritten by revert.\n" |
321 | "Please, commit your changes or stash them to proceed.")); | |
322 | else | |
323 | die(_("Your local changes would be overwritten by cherry-pick.\n" | |
324 | "Please, commit your changes or stash them to proceed.")); | |
325 | } else { | |
80e1f791 | 326 | if (opts->action == REVERT) |
d3ee177e ÆAB |
327 | die(_("Your local changes would be overwritten by revert.\n")); |
328 | else | |
329 | die(_("Your local changes would be overwritten by cherry-pick.\n")); | |
330 | } | |
d38a30df MM |
331 | } |
332 | } | |
333 | ||
c62f6ec3 JH |
334 | static int fast_forward_to(const unsigned char *to, const unsigned char *from) |
335 | { | |
336 | struct ref_lock *ref_lock; | |
337 | ||
338 | read_cache(); | |
339 | if (checkout_fast_forward(from, to)) | |
340 | exit(1); /* the callee should have complained already */ | |
341 | ref_lock = lock_any_ref_for_update("HEAD", from, 0); | |
342 | return write_ref_sha1(ref_lock, to, "cherry-pick"); | |
343 | } | |
344 | ||
7b53b92f CC |
345 | static int do_recursive_merge(struct commit *base, struct commit *next, |
346 | const char *base_label, const char *next_label, | |
80e1f791 RR |
347 | unsigned char *head, struct strbuf *msgbuf, |
348 | struct replay_opts *opts) | |
ae8c79fd CC |
349 | { |
350 | struct merge_options o; | |
351 | struct tree *result, *next_tree, *base_tree, *head_tree; | |
352 | int clean, index_fd; | |
67ac1e1d | 353 | const char **xopt; |
ae8c79fd CC |
354 | static struct lock_file index_lock; |
355 | ||
356 | index_fd = hold_locked_index(&index_lock, 1); | |
357 | ||
358 | read_cache(); | |
7610fa57 | 359 | |
ae8c79fd CC |
360 | init_merge_options(&o); |
361 | o.ancestor = base ? base_label : "(empty tree)"; | |
362 | o.branch1 = "HEAD"; | |
363 | o.branch2 = next ? next_label : "(empty tree)"; | |
364 | ||
365 | head_tree = parse_tree_indirect(head); | |
366 | next_tree = next ? next->tree : empty_tree(); | |
367 | base_tree = base ? base->tree : empty_tree(); | |
368 | ||
80e1f791 | 369 | for (xopt = opts->xopts; xopt != opts->xopts + opts->xopts_nr; xopt++) |
67ac1e1d JN |
370 | parse_merge_opt(&o, *xopt); |
371 | ||
ae8c79fd CC |
372 | clean = merge_trees(&o, |
373 | head_tree, | |
374 | next_tree, base_tree, &result); | |
375 | ||
376 | if (active_cache_changed && | |
377 | (write_cache(index_fd, active_cache, active_nr) || | |
378 | commit_locked_index(&index_lock))) | |
b9c993e0 | 379 | /* TRANSLATORS: %s will be "revert" or "cherry-pick" */ |
80e1f791 | 380 | die(_("%s: Unable to write new index file"), action_name(opts)); |
ae8c79fd CC |
381 | rollback_lock_file(&index_lock); |
382 | ||
383 | if (!clean) { | |
384 | int i; | |
385 | strbuf_addstr(msgbuf, "\nConflicts:\n\n"); | |
386 | for (i = 0; i < active_nr;) { | |
387 | struct cache_entry *ce = active_cache[i++]; | |
388 | if (ce_stage(ce)) { | |
389 | strbuf_addch(msgbuf, '\t'); | |
390 | strbuf_addstr(msgbuf, ce->name); | |
391 | strbuf_addch(msgbuf, '\n'); | |
392 | while (i < active_nr && !strcmp(ce->name, | |
393 | active_cache[i]->name)) | |
394 | i++; | |
395 | } | |
396 | } | |
ae8c79fd | 397 | } |
7b53b92f CC |
398 | |
399 | return !clean; | |
ae8c79fd CC |
400 | } |
401 | ||
5df16453 CC |
402 | /* |
403 | * If we are cherry-pick, and if the merge did not result in | |
404 | * hand-editing, we will hit this commit and inherit the original | |
405 | * author date and name. | |
406 | * If we are revert, or if our cherry-pick results in a hand merge, | |
407 | * we had better say that the current user is responsible for that. | |
408 | */ | |
80e1f791 | 409 | static int run_git_commit(const char *defmsg, struct replay_opts *opts) |
5df16453 CC |
410 | { |
411 | /* 6 is max possible length of our args array including NULL */ | |
412 | const char *args[6]; | |
413 | int i = 0; | |
414 | ||
415 | args[i++] = "commit"; | |
416 | args[i++] = "-n"; | |
80e1f791 | 417 | if (opts->signoff) |
5df16453 | 418 | args[i++] = "-s"; |
80e1f791 | 419 | if (!opts->edit) { |
5df16453 CC |
420 | args[i++] = "-F"; |
421 | args[i++] = defmsg; | |
422 | } | |
423 | args[i] = NULL; | |
424 | ||
425 | return run_command_v_opt(args, RUN_GIT_CMD); | |
ae8c79fd CC |
426 | } |
427 | ||
80e1f791 | 428 | static int do_pick_commit(struct commit *commit, struct replay_opts *opts) |
9509af68 JS |
429 | { |
430 | unsigned char head[20]; | |
7791ecbc | 431 | struct commit *base, *next, *parent; |
bf975d37 | 432 | const char *base_label, *next_label; |
d6856540 | 433 | struct commit_message msg = { NULL, NULL, NULL, NULL, NULL }; |
28db756f | 434 | char *defmsg = NULL; |
bc84a7fb | 435 | struct strbuf msgbuf = STRBUF_INIT; |
7b53b92f | 436 | int res; |
9509af68 | 437 | |
80e1f791 | 438 | if (opts->no_commit) { |
9509af68 JS |
439 | /* |
440 | * We do not intend to commit immediately. We just want to | |
71aa2b8f JH |
441 | * merge the differences in, so let's compute the tree |
442 | * that represents the "current" state for merge-recursive | |
443 | * to work on. | |
9509af68 | 444 | */ |
45525bd0 | 445 | if (write_cache_as_tree(head, 0, NULL)) |
f30f71ce | 446 | die (_("Your index file is unmerged.")); |
9509af68 | 447 | } else { |
9509af68 | 448 | if (get_sha1("HEAD", head)) |
f30f71ce | 449 | die (_("You do not have a valid HEAD")); |
75f3ff2e | 450 | if (index_differs_from("HEAD", 0)) |
80e1f791 | 451 | die_dirty_index(opts); |
9509af68 | 452 | } |
6eb1b437 MV |
453 | discard_cache(); |
454 | ||
f95ebf74 | 455 | if (!commit->parents) { |
f95ebf74 JS |
456 | parent = NULL; |
457 | } | |
458 | else if (commit->parents->next) { | |
7791ecbc JH |
459 | /* Reverting or cherry-picking a merge commit */ |
460 | int cnt; | |
461 | struct commit_list *p; | |
462 | ||
80e1f791 | 463 | if (!opts->mainline) |
f30f71ce | 464 | die(_("Commit %s is a merge but no -m option was given."), |
7791ecbc JH |
465 | sha1_to_hex(commit->object.sha1)); |
466 | ||
467 | for (cnt = 1, p = commit->parents; | |
80e1f791 | 468 | cnt != opts->mainline && p; |
7791ecbc JH |
469 | cnt++) |
470 | p = p->next; | |
80e1f791 | 471 | if (cnt != opts->mainline || !p) |
f30f71ce | 472 | die(_("Commit %s does not have parent %d"), |
80e1f791 | 473 | sha1_to_hex(commit->object.sha1), opts->mainline); |
7791ecbc | 474 | parent = p->item; |
80e1f791 | 475 | } else if (0 < opts->mainline) |
f30f71ce | 476 | die(_("Mainline was specified but commit %s is not a merge."), |
7791ecbc JH |
477 | sha1_to_hex(commit->object.sha1)); |
478 | else | |
479 | parent = commit->parents->item; | |
480 | ||
80e1f791 | 481 | if (opts->allow_ff && parent && !hashcmp(parent->object.sha1, head)) |
c62f6ec3 JH |
482 | return fast_forward_to(commit->object.sha1, head); |
483 | ||
6eb1b437 | 484 | if (parent && parse_commit(parent) < 0) |
b9c993e0 ÆAB |
485 | /* TRANSLATORS: The first %s will be "revert" or |
486 | "cherry-pick", the second %s a SHA1 */ | |
487 | die(_("%s: cannot parse parent commit %s"), | |
80e1f791 | 488 | action_name(opts), sha1_to_hex(parent->object.sha1)); |
6eb1b437 | 489 | |
708f9d96 | 490 | if (get_message(commit, &msg) != 0) |
f30f71ce | 491 | die(_("Cannot get commit message for %s"), |
d6856540 JN |
492 | sha1_to_hex(commit->object.sha1)); |
493 | ||
9509af68 JS |
494 | /* |
495 | * "commit" is an existing commit. We would want to apply | |
496 | * the difference it introduces since its first parent "prev" | |
497 | * on top of the current HEAD if we are cherry-pick. Or the | |
498 | * reverse of it if we are revert. | |
499 | */ | |
500 | ||
28db756f | 501 | defmsg = git_pathdup("MERGE_MSG"); |
c62f6ec3 | 502 | |
80e1f791 | 503 | if (opts->action == REVERT) { |
9509af68 | 504 | base = commit; |
bf975d37 | 505 | base_label = msg.label; |
7791ecbc | 506 | next = parent; |
d6856540 | 507 | next_label = msg.parent_label; |
bc84a7fb CC |
508 | strbuf_addstr(&msgbuf, "Revert \""); |
509 | strbuf_addstr(&msgbuf, msg.subject); | |
510 | strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit "); | |
511 | strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1)); | |
d5be89d8 | 512 | |
fad26526 | 513 | if (commit->parents && commit->parents->next) { |
bc84a7fb CC |
514 | strbuf_addstr(&msgbuf, ", reversing\nchanges made to "); |
515 | strbuf_addstr(&msgbuf, sha1_to_hex(parent->object.sha1)); | |
d5be89d8 | 516 | } |
bc84a7fb | 517 | strbuf_addstr(&msgbuf, ".\n"); |
9509af68 | 518 | } else { |
be33c46c RR |
519 | const char *p; |
520 | ||
7791ecbc | 521 | base = parent; |
bf975d37 | 522 | base_label = msg.parent_label; |
9509af68 | 523 | next = commit; |
d6856540 | 524 | next_label = msg.label; |
be33c46c RR |
525 | |
526 | /* | |
527 | * Append the commit log message to msgbuf; it starts | |
528 | * after the tree, parent, author, committer | |
529 | * information followed by "\n\n". | |
530 | */ | |
531 | p = strstr(msg.message, "\n\n"); | |
532 | if (p) { | |
533 | p += 2; | |
534 | strbuf_addstr(&msgbuf, p); | |
535 | } | |
536 | ||
80e1f791 | 537 | if (opts->record_origin) { |
bc84a7fb CC |
538 | strbuf_addstr(&msgbuf, "(cherry picked from commit "); |
539 | strbuf_addstr(&msgbuf, sha1_to_hex(commit->object.sha1)); | |
540 | strbuf_addstr(&msgbuf, ")\n"); | |
9509af68 | 541 | } |
80e1f791 | 542 | if (!opts->no_commit) |
708f9d96 | 543 | write_cherry_pick_head(commit); |
9509af68 | 544 | } |
9509af68 | 545 | |
80e1f791 | 546 | if (!opts->strategy || !strcmp(opts->strategy, "recursive") || opts->action == REVERT) { |
7b53b92f | 547 | res = do_recursive_merge(base, next, base_label, next_label, |
80e1f791 | 548 | head, &msgbuf, opts); |
7b53b92f CC |
549 | write_message(&msgbuf, defmsg); |
550 | } else { | |
91e52598 CC |
551 | struct commit_list *common = NULL; |
552 | struct commit_list *remotes = NULL; | |
7b53b92f | 553 | |
91e52598 | 554 | write_message(&msgbuf, defmsg); |
7b53b92f | 555 | |
91e52598 CC |
556 | commit_list_insert(base, &common); |
557 | commit_list_insert(next, &remotes); | |
80e1f791 RR |
558 | res = try_merge_command(opts->strategy, opts->xopts_nr, opts->xopts, |
559 | common, sha1_to_hex(head), remotes); | |
91e52598 CC |
560 | free_commit_list(common); |
561 | free_commit_list(remotes); | |
9509af68 | 562 | } |
9509af68 | 563 | |
7b53b92f | 564 | if (res) { |
80e1f791 | 565 | error(opts->action == REVERT |
cb6aeb22 ÆAB |
566 | ? _("could not revert %s... %s") |
567 | : _("could not apply %s... %s"), | |
981ff5c3 JN |
568 | find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), |
569 | msg.subject); | |
314eeb6e | 570 | print_advice(); |
80e1f791 | 571 | rerere(opts->allow_rerere_auto); |
7b53b92f | 572 | } else { |
80e1f791 RR |
573 | if (!opts->no_commit) |
574 | res = run_git_commit(defmsg, opts); | |
9509af68 | 575 | } |
2fb0e14f | 576 | |
2fb0e14f | 577 | free_message(&msg); |
d258b258 | 578 | free(defmsg); |
9509af68 | 579 | |
3b2c5b6d | 580 | return res; |
9509af68 JS |
581 | } |
582 | ||
80e1f791 | 583 | static void prepare_revs(struct rev_info *revs, struct replay_opts *opts) |
7e2bfd3f | 584 | { |
e0ef8495 | 585 | int argc; |
7e2bfd3f | 586 | |
e0ef8495 JN |
587 | init_revisions(revs, NULL); |
588 | revs->no_walk = 1; | |
80e1f791 | 589 | if (opts->action != REVERT) |
e0ef8495 JN |
590 | revs->reverse = 1; |
591 | ||
80e1f791 | 592 | argc = setup_revisions(opts->commit_argc, opts->commit_argv, revs, NULL); |
e0ef8495 | 593 | if (argc > 1) |
80e1f791 | 594 | usage(*revert_or_cherry_pick_usage(opts)); |
7e2bfd3f | 595 | |
7e2bfd3f | 596 | if (prepare_revision_walk(revs)) |
f30f71ce | 597 | die(_("revision walk setup failed")); |
7e2bfd3f CC |
598 | |
599 | if (!revs->commits) | |
f30f71ce | 600 | die(_("empty commit set passed")); |
7e2bfd3f CC |
601 | } |
602 | ||
80e1f791 | 603 | static void read_and_refresh_cache(struct replay_opts *opts) |
f6ce1f25 JN |
604 | { |
605 | static struct lock_file index_lock; | |
606 | int index_fd = hold_locked_index(&index_lock, 0); | |
607 | if (read_index_preload(&the_index, NULL) < 0) | |
80e1f791 | 608 | die(_("git %s: failed to read the index"), action_name(opts)); |
f6ce1f25 JN |
609 | refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL); |
610 | if (the_index.cache_changed) { | |
611 | if (write_index(&the_index, index_fd) || | |
612 | commit_locked_index(&index_lock)) | |
80e1f791 | 613 | die(_("git %s: failed to refresh the index"), action_name(opts)); |
f6ce1f25 JN |
614 | } |
615 | rollback_lock_file(&index_lock); | |
616 | } | |
617 | ||
04d3d3cf RR |
618 | /* |
619 | * Append a commit to the end of the commit_list. | |
620 | * | |
621 | * next starts by pointing to the variable that holds the head of an | |
622 | * empty commit_list, and is updated to point to the "next" field of | |
623 | * the last item on the list as new commits are appended. | |
624 | * | |
625 | * Usage example: | |
626 | * | |
627 | * struct commit_list *list; | |
628 | * struct commit_list **next = &list; | |
629 | * | |
630 | * next = commit_list_append(c1, next); | |
631 | * next = commit_list_append(c2, next); | |
632 | * assert(commit_list_count(list) == 2); | |
633 | * return list; | |
634 | */ | |
635 | struct commit_list **commit_list_append(struct commit *commit, | |
636 | struct commit_list **next) | |
637 | { | |
638 | struct commit_list *new = xmalloc(sizeof(struct commit_list)); | |
639 | new->item = commit; | |
640 | *next = new; | |
641 | new->next = NULL; | |
642 | return &new->next; | |
643 | } | |
644 | ||
645 | static int format_todo(struct strbuf *buf, struct commit_list *todo_list, | |
646 | struct replay_opts *opts) | |
647 | { | |
648 | struct commit_list *cur = NULL; | |
649 | struct commit_message msg = { NULL, NULL, NULL, NULL, NULL }; | |
650 | const char *sha1_abbrev = NULL; | |
651 | const char *action_str = opts->action == REVERT ? "revert" : "pick"; | |
652 | ||
653 | for (cur = todo_list; cur; cur = cur->next) { | |
654 | sha1_abbrev = find_unique_abbrev(cur->item->object.sha1, DEFAULT_ABBREV); | |
655 | if (get_message(cur->item, &msg)) | |
656 | return error(_("Cannot get commit message for %s"), sha1_abbrev); | |
657 | strbuf_addf(buf, "%s %s %s\n", action_str, sha1_abbrev, msg.subject); | |
658 | } | |
659 | return 0; | |
660 | } | |
661 | ||
662 | static void walk_revs_populate_todo(struct commit_list **todo_list, | |
663 | struct replay_opts *opts) | |
7af46595 | 664 | { |
7e2bfd3f | 665 | struct rev_info revs; |
708f9d96 | 666 | struct commit *commit; |
04d3d3cf RR |
667 | struct commit_list **next; |
668 | ||
669 | prepare_revs(&revs, opts); | |
670 | ||
671 | next = todo_list; | |
672 | while ((commit = get_revision(&revs))) | |
673 | next = commit_list_append(commit, next); | |
674 | } | |
675 | ||
676 | static void create_seq_dir(void) | |
677 | { | |
678 | const char *seq_dir = git_path(SEQ_DIR); | |
679 | ||
680 | if (!(file_exists(seq_dir) && is_directory(seq_dir)) | |
681 | && mkdir(seq_dir, 0777) < 0) | |
682 | die_errno(_("Could not create sequencer directory '%s'."), seq_dir); | |
683 | } | |
684 | ||
685 | static void save_head(const char *head) | |
686 | { | |
687 | const char *head_file = git_path(SEQ_HEAD_FILE); | |
688 | static struct lock_file head_lock; | |
689 | struct strbuf buf = STRBUF_INIT; | |
690 | int fd; | |
691 | ||
692 | fd = hold_lock_file_for_update(&head_lock, head_file, LOCK_DIE_ON_ERROR); | |
693 | strbuf_addf(&buf, "%s\n", head); | |
694 | if (write_in_full(fd, buf.buf, buf.len) < 0) | |
695 | die_errno(_("Could not write to %s."), head_file); | |
696 | if (commit_lock_file(&head_lock) < 0) | |
697 | die(_("Error wrapping up %s."), head_file); | |
698 | } | |
699 | ||
700 | static void save_todo(struct commit_list *todo_list, struct replay_opts *opts) | |
701 | { | |
702 | const char *todo_file = git_path(SEQ_TODO_FILE); | |
703 | static struct lock_file todo_lock; | |
704 | struct strbuf buf = STRBUF_INIT; | |
705 | int fd; | |
706 | ||
707 | fd = hold_lock_file_for_update(&todo_lock, todo_file, LOCK_DIE_ON_ERROR); | |
708 | if (format_todo(&buf, todo_list, opts) < 0) | |
709 | die(_("Could not format %s."), todo_file); | |
710 | if (write_in_full(fd, buf.buf, buf.len) < 0) { | |
711 | strbuf_release(&buf); | |
712 | die_errno(_("Could not write to %s."), todo_file); | |
713 | } | |
714 | if (commit_lock_file(&todo_lock) < 0) { | |
715 | strbuf_release(&buf); | |
716 | die(_("Error wrapping up %s."), todo_file); | |
717 | } | |
718 | strbuf_release(&buf); | |
719 | } | |
720 | ||
6f032263 RR |
721 | static void save_opts(struct replay_opts *opts) |
722 | { | |
723 | const char *opts_file = git_path(SEQ_OPTS_FILE); | |
724 | ||
725 | if (opts->no_commit) | |
726 | git_config_set_in_file(opts_file, "options.no-commit", "true"); | |
727 | if (opts->edit) | |
728 | git_config_set_in_file(opts_file, "options.edit", "true"); | |
729 | if (opts->signoff) | |
730 | git_config_set_in_file(opts_file, "options.signoff", "true"); | |
731 | if (opts->record_origin) | |
732 | git_config_set_in_file(opts_file, "options.record-origin", "true"); | |
733 | if (opts->allow_ff) | |
734 | git_config_set_in_file(opts_file, "options.allow-ff", "true"); | |
735 | if (opts->mainline) { | |
736 | struct strbuf buf = STRBUF_INIT; | |
737 | strbuf_addf(&buf, "%d", opts->mainline); | |
738 | git_config_set_in_file(opts_file, "options.mainline", buf.buf); | |
739 | strbuf_release(&buf); | |
740 | } | |
741 | if (opts->strategy) | |
742 | git_config_set_in_file(opts_file, "options.strategy", opts->strategy); | |
743 | if (opts->xopts) { | |
744 | int i; | |
745 | for (i = 0; i < opts->xopts_nr; i++) | |
746 | git_config_set_multivar_in_file(opts_file, | |
747 | "options.strategy-option", | |
748 | opts->xopts[i], "^$", 0); | |
749 | } | |
750 | } | |
751 | ||
21b14778 | 752 | static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts) |
04d3d3cf | 753 | { |
04d3d3cf RR |
754 | struct commit_list *cur; |
755 | int res; | |
7e2bfd3f | 756 | |
80e1f791 | 757 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); |
9044143f RR |
758 | if (opts->allow_ff) |
759 | assert(!(opts->signoff || opts->no_commit || | |
760 | opts->record_origin || opts->edit)); | |
80e1f791 | 761 | read_and_refresh_cache(opts); |
7af46595 | 762 | |
04d3d3cf RR |
763 | for (cur = todo_list; cur; cur = cur->next) { |
764 | save_todo(cur, opts); | |
765 | res = do_pick_commit(cur->item, opts); | |
7e2bfd3f CC |
766 | if (res) |
767 | return res; | |
768 | } | |
769 | ||
04d3d3cf RR |
770 | /* |
771 | * Sequence of picks finished successfully; cleanup by | |
772 | * removing the .git/sequencer directory | |
773 | */ | |
26ae337b | 774 | remove_sequencer_state(1); |
7e2bfd3f | 775 | return 0; |
7af46595 CC |
776 | } |
777 | ||
21b14778 RR |
778 | static int pick_revisions(struct replay_opts *opts) |
779 | { | |
780 | struct commit_list *todo_list = NULL; | |
781 | unsigned char sha1[20]; | |
782 | ||
783 | read_and_refresh_cache(opts); | |
784 | ||
785 | /* | |
786 | * Decide what to do depending on the arguments; a fresh | |
787 | * cherry-pick should be handled differently from an existing | |
788 | * one that is being continued | |
789 | */ | |
26ae337b RR |
790 | if (opts->subcommand == REPLAY_RESET) { |
791 | remove_sequencer_state(1); | |
792 | return 0; | |
793 | } else { | |
794 | /* Start a new cherry-pick/ revert sequence */ | |
795 | walk_revs_populate_todo(&todo_list, opts); | |
796 | create_seq_dir(); | |
797 | if (get_sha1("HEAD", sha1)) { | |
798 | if (opts->action == REVERT) | |
799 | die(_("Can't revert as initial commit")); | |
800 | die(_("Can't cherry-pick into empty head")); | |
801 | } | |
802 | save_head(sha1_to_hex(sha1)); | |
803 | save_opts(opts); | |
21b14778 | 804 | } |
21b14778 RR |
805 | return pick_commits(todo_list, opts); |
806 | } | |
807 | ||
9509af68 JS |
808 | int cmd_revert(int argc, const char **argv, const char *prefix) |
809 | { | |
80e1f791 RR |
810 | struct replay_opts opts; |
811 | ||
812 | memset(&opts, 0, sizeof(opts)); | |
9509af68 | 813 | if (isatty(0)) |
80e1f791 RR |
814 | opts.edit = 1; |
815 | opts.action = REVERT; | |
89641472 RR |
816 | git_config(git_default_config, NULL); |
817 | parse_args(argc, argv, &opts); | |
21b14778 | 818 | return pick_revisions(&opts); |
9509af68 JS |
819 | } |
820 | ||
821 | int cmd_cherry_pick(int argc, const char **argv, const char *prefix) | |
822 | { | |
80e1f791 RR |
823 | struct replay_opts opts; |
824 | ||
825 | memset(&opts, 0, sizeof(opts)); | |
826 | opts.action = CHERRY_PICK; | |
89641472 RR |
827 | git_config(git_default_config, NULL); |
828 | parse_args(argc, argv, &opts); | |
21b14778 | 829 | return pick_revisions(&opts); |
9509af68 | 830 | } |