7 #include "run-command.h"
10 #include "parse-options.h"
11 #include "cache-tree.h"
15 #include "merge-recursive.h"
19 * This implements the builtins revert and cherry-pick.
21 * Copyright (c) 2007 Johannes E. Schindelin
23 * Based on git-revert.sh, which is
25 * Copyright (c) 2005 Linus Torvalds
26 * Copyright (c) 2005 Junio C Hamano
29 static const char * const revert_usage[] = {
30 "git revert [options] <commit-ish>",
34 static const char * const cherry_pick_usage[] = {
35 "git cherry-pick [options] <commit-ish>",
39 static int edit, no_replay, no_commit, mainline, signoff, allow_ff;
40 static enum { REVERT, CHERRY_PICK } action;
41 static struct commit *commit;
42 static const char *commit_name;
43 static int allow_rerere_auto;
45 static const char *me;
47 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
49 static char *get_encoding(const char *message);
51 static void parse_args(int argc, const char **argv)
53 const char * const * usage_str =
54 action == REVERT ? revert_usage : cherry_pick_usage;
55 unsigned char sha1[20];
57 struct option options[] = {
58 OPT_BOOLEAN('n', "no-commit", &no_commit, "don't automatically commit"),
59 OPT_BOOLEAN('e', "edit", &edit, "edit the commit message"),
60 OPT_BOOLEAN('x', NULL, &no_replay, "append commit name when cherry-picking"),
61 OPT_BOOLEAN('r', NULL, &noop, "no-op (backward compatibility)"),
62 OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"),
63 OPT_INTEGER('m', "mainline", &mainline, "parent number"),
64 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto),
70 if (action == CHERRY_PICK) {
71 struct option cp_extra[] = {
72 OPT_BOOLEAN(0, "ff", &allow_ff, "allow fast-forward"),
75 if (parse_options_concat(options, ARRAY_SIZE(options), cp_extra))
79 if (parse_options(argc, argv, NULL, options, usage_str, 0) != 1)
80 usage_with_options(usage_str, options);
82 commit_name = argv[0];
83 if (get_sha1(commit_name, sha1))
84 die ("Cannot find '%s'", commit_name);
85 commit = lookup_commit_reference(sha1);
90 struct commit_message {
94 char *reencoded_message;
98 static int get_message(const char *raw_message, struct commit_message *out)
100 const char *encoding;
101 const char *p, *abbrev, *eol;
103 int abbrev_len, oneline_len;
107 encoding = get_encoding(raw_message);
110 if (!git_commit_encoding)
111 git_commit_encoding = "UTF-8";
113 out->reencoded_message = NULL;
114 out->message = raw_message;
115 if (strcmp(encoding, git_commit_encoding))
116 out->reencoded_message = reencode_string(raw_message,
117 git_commit_encoding, encoding);
118 if (out->reencoded_message)
119 out->message = out->reencoded_message;
121 abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
122 abbrev_len = strlen(abbrev);
124 /* Find beginning and end of commit subject. */
126 while (*p && (*p != '\n' || p[1] != '\n'))
130 for (eol = p + 1; *eol && *eol != '\n'; eol++)
134 oneline_len = eol - p;
136 out->parent_label = xmalloc(strlen("parent of ") + abbrev_len +
137 strlen("... ") + oneline_len + 1);
138 q = out->parent_label;
139 q = mempcpy(q, "parent of ", strlen("parent of "));
141 q = mempcpy(q, abbrev, abbrev_len);
142 q = mempcpy(q, "... ", strlen("... "));
144 q = mempcpy(q, p, oneline_len);
149 static void free_message(struct commit_message *msg)
151 free(msg->parent_label);
152 free(msg->reencoded_message);
155 static char *get_encoding(const char *message)
157 const char *p = message, *eol;
160 die ("Could not read commit message of %s",
161 sha1_to_hex(commit->object.sha1));
162 while (*p && *p != '\n') {
163 for (eol = p + 1; *eol && *eol != '\n'; eol++)
165 if (!prefixcmp(p, "encoding ")) {
166 char *result = xmalloc(eol - 8 - p);
167 strlcpy(result, p + 9, eol - 8 - p);
177 static struct lock_file msg_file;
180 static void add_to_msg(const char *string)
182 int len = strlen(string);
183 if (write_in_full(msg_fd, string, len) < 0)
184 die_errno ("Could not write to MERGE_MSG");
187 static void add_message_to_msg(const char *message)
189 const char *p = message;
190 while (*p && (*p != '\n' || p[1] != '\n'))
194 add_to_msg(sha1_to_hex(commit->object.sha1));
201 static void set_author_ident_env(const char *message)
203 const char *p = message;
205 die ("Could not read commit message of %s",
206 sha1_to_hex(commit->object.sha1));
207 while (*p && *p != '\n') {
210 for (eol = p; *eol && *eol != '\n'; eol++)
212 if (!prefixcmp(p, "author ")) {
213 char *line, *pend, *email, *timestamp;
216 line = xmemdupz(p, eol - p);
217 email = strchr(line, '<');
219 die ("Could not extract author email from %s",
220 sha1_to_hex(commit->object.sha1));
224 for (pend = email; pend != line + 1 &&
225 isspace(pend[-1]); pend--);
229 timestamp = strchr(email, '>');
231 die ("Could not extract author time from %s",
232 sha1_to_hex(commit->object.sha1));
234 for (timestamp++; *timestamp && isspace(*timestamp);
237 setenv("GIT_AUTHOR_NAME", line, 1);
238 setenv("GIT_AUTHOR_EMAIL", email, 1);
239 setenv("GIT_AUTHOR_DATE", timestamp, 1);
247 die ("No author information found in %s",
248 sha1_to_hex(commit->object.sha1));
251 static char *help_msg(const char *name)
253 struct strbuf helpbuf = STRBUF_INIT;
254 char *msg = getenv("GIT_CHERRY_PICK_HELP");
259 strbuf_addstr(&helpbuf, " After resolving the conflicts,\n"
260 "mark the corrected paths with 'git add <paths>' or 'git rm <paths>'\n"
261 "and commit the result");
263 if (action == CHERRY_PICK) {
264 strbuf_addf(&helpbuf, " with: \n"
266 " git commit -c %s\n",
270 strbuf_addch(&helpbuf, '.');
271 return strbuf_detach(&helpbuf, NULL);
274 static struct tree *empty_tree(void)
276 struct tree *tree = xcalloc(1, sizeof(struct tree));
278 tree->object.parsed = 1;
279 tree->object.type = OBJ_TREE;
280 pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
284 static NORETURN void die_dirty_index(const char *me)
286 if (read_cache_unmerged()) {
287 die_resolve_conflict(me);
289 if (advice_commit_before_merge)
290 die("Your local changes would be overwritten by %s.\n"
291 "Please, commit your changes or stash them to proceed.", me);
293 die("Your local changes would be overwritten by %s.\n", me);
297 static int fast_forward_to(const unsigned char *to, const unsigned char *from)
299 struct ref_lock *ref_lock;
302 if (checkout_fast_forward(from, to))
303 exit(1); /* the callee should have complained already */
304 ref_lock = lock_any_ref_for_update("HEAD", from, 0);
305 return write_ref_sha1(ref_lock, to, "cherry-pick");
308 static int revert_or_cherry_pick(int argc, const char **argv)
310 unsigned char head[20];
311 struct commit *base, *next, *parent;
312 const char *base_label, *next_label;
313 int i, index_fd, clean;
314 struct commit_message msg = { NULL, NULL, NULL, NULL, NULL };
316 struct merge_options o;
317 struct tree *result, *next_tree, *base_tree, *head_tree;
318 static struct lock_file index_lock;
320 git_config(git_default_config, NULL);
321 me = action == REVERT ? "revert" : "cherry-pick";
322 setenv(GIT_REFLOG_ACTION, me, 0);
323 parse_args(argc, argv);
325 /* this is copied from the shell script, but it's never triggered... */
326 if (action == REVERT && !no_replay)
327 die("revert is incompatible with replay");
331 die("cherry-pick --ff cannot be used with --signoff");
333 die("cherry-pick --ff cannot be used with --no-commit");
335 die("cherry-pick --ff cannot be used with -x");
337 die("cherry-pick --ff cannot be used with --edit");
340 if (read_cache() < 0)
341 die("git %s: failed to read the index", me);
344 * We do not intend to commit immediately. We just want to
345 * merge the differences in, so let's compute the tree
346 * that represents the "current" state for merge-recursive
349 if (write_cache_as_tree(head, 0, NULL))
350 die ("Your index file is unmerged.");
352 if (get_sha1("HEAD", head))
353 die ("You do not have a valid HEAD");
354 if (index_differs_from("HEAD", 0))
359 if (!commit->parents) {
360 if (action == REVERT)
361 die ("Cannot revert a root commit");
364 else if (commit->parents->next) {
365 /* Reverting or cherry-picking a merge commit */
367 struct commit_list *p;
370 die("Commit %s is a merge but no -m option was given.",
371 sha1_to_hex(commit->object.sha1));
373 for (cnt = 1, p = commit->parents;
374 cnt != mainline && p;
377 if (cnt != mainline || !p)
378 die("Commit %s does not have parent %d",
379 sha1_to_hex(commit->object.sha1), mainline);
381 } else if (0 < mainline)
382 die("Mainline was specified but commit %s is not a merge.",
383 sha1_to_hex(commit->object.sha1));
385 parent = commit->parents->item;
387 if (allow_ff && !hashcmp(parent->object.sha1, head))
388 return fast_forward_to(commit->object.sha1, head);
390 if (parent && parse_commit(parent) < 0)
391 die("%s: cannot parse parent commit %s",
392 me, sha1_to_hex(parent->object.sha1));
394 if (get_message(commit->buffer, &msg) != 0)
395 die("Cannot get commit message for %s",
396 sha1_to_hex(commit->object.sha1));
399 * "commit" is an existing commit. We would want to apply
400 * the difference it introduces since its first parent "prev"
401 * on top of the current HEAD if we are cherry-pick. Or the
402 * reverse of it if we are revert.
405 defmsg = git_pathdup("MERGE_MSG");
406 msg_fd = hold_lock_file_for_update(&msg_file, defmsg,
409 index_fd = hold_locked_index(&index_lock, 1);
411 if (action == REVERT) {
413 base_label = msg.label;
415 next_label = msg.parent_label;
416 add_to_msg("Revert \"");
417 add_to_msg(msg.subject);
418 add_to_msg("\"\n\nThis reverts commit ");
419 add_to_msg(sha1_to_hex(commit->object.sha1));
421 if (commit->parents->next) {
422 add_to_msg(", reversing\nchanges made to ");
423 add_to_msg(sha1_to_hex(parent->object.sha1));
428 base_label = msg.parent_label;
430 next_label = msg.label;
431 set_author_ident_env(msg.message);
432 add_message_to_msg(msg.message);
434 add_to_msg("(cherry picked from commit ");
435 add_to_msg(sha1_to_hex(commit->object.sha1));
441 init_merge_options(&o);
442 o.ancestor = base ? base_label : "(empty tree)";
444 o.branch2 = next ? next_label : "(empty tree)";
446 head_tree = parse_tree_indirect(head);
447 next_tree = next ? next->tree : empty_tree();
448 base_tree = base ? base->tree : empty_tree();
450 clean = merge_trees(&o,
452 next_tree, base_tree, &result);
454 if (active_cache_changed &&
455 (write_cache(index_fd, active_cache, active_nr) ||
456 commit_locked_index(&index_lock)))
457 die("%s: Unable to write new index file", me);
458 rollback_lock_file(&index_lock);
461 add_to_msg("\nConflicts:\n\n");
462 for (i = 0; i < active_nr;) {
463 struct cache_entry *ce = active_cache[i++];
466 add_to_msg(ce->name);
468 while (i < active_nr && !strcmp(ce->name,
469 active_cache[i]->name))
473 if (commit_lock_file(&msg_file) < 0)
474 die ("Error wrapping up %s", defmsg);
475 fprintf(stderr, "Automatic %s failed.%s\n",
476 me, help_msg(commit_name));
477 rerere(allow_rerere_auto);
480 if (commit_lock_file(&msg_file) < 0)
481 die ("Error wrapping up %s", defmsg);
482 fprintf(stderr, "Finished one %s.\n", me);
486 * If we are cherry-pick, and if the merge did not result in
487 * hand-editing, we will hit this commit and inherit the original
488 * author date and name.
489 * If we are revert, or if our cherry-pick results in a hand merge,
490 * we had better say that the current user is responsible for that.
494 /* 6 is max possible length of our args array including NULL */
497 args[i++] = "commit";
506 return execv_git_cmd(args);
514 int cmd_revert(int argc, const char **argv, const char *prefix)
520 return revert_or_cherry_pick(argc, argv);
523 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
526 action = CHERRY_PICK;
527 return revert_or_cherry_pick(argc, argv);