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";
112 if ((out->reencoded_message = reencode_string(raw_message,
113 git_commit_encoding, encoding)))
114 out->message = out->reencoded_message;
116 abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
117 abbrev_len = strlen(abbrev);
119 /* Find beginning and end of commit subject. */
121 while (*p && (*p != '\n' || p[1] != '\n'))
125 for (eol = p + 1; *eol && *eol != '\n'; eol++)
129 oneline_len = eol - p;
131 out->parent_label = xmalloc(strlen("parent of ") + abbrev_len +
132 strlen("... ") + oneline_len + 1);
133 q = out->parent_label;
134 q = mempcpy(q, "parent of ", strlen("parent of "));
136 q = mempcpy(q, abbrev, abbrev_len);
137 q = mempcpy(q, "... ", strlen("... "));
139 q = mempcpy(q, p, oneline_len);
144 static void free_message(struct commit_message *msg)
146 free(msg->parent_label);
147 free(msg->reencoded_message);
150 static char *get_encoding(const char *message)
152 const char *p = message, *eol;
155 die ("Could not read commit message of %s",
156 sha1_to_hex(commit->object.sha1));
157 while (*p && *p != '\n') {
158 for (eol = p + 1; *eol && *eol != '\n'; eol++)
160 if (!prefixcmp(p, "encoding ")) {
161 char *result = xmalloc(eol - 8 - p);
162 strlcpy(result, p + 9, eol - 8 - p);
172 static struct lock_file msg_file;
175 static void add_to_msg(const char *string)
177 int len = strlen(string);
178 if (write_in_full(msg_fd, string, len) < 0)
179 die_errno ("Could not write to MERGE_MSG");
182 static void add_message_to_msg(const char *message)
184 const char *p = message;
185 while (*p && (*p != '\n' || p[1] != '\n'))
189 add_to_msg(sha1_to_hex(commit->object.sha1));
196 static void set_author_ident_env(const char *message)
198 const char *p = message;
200 die ("Could not read commit message of %s",
201 sha1_to_hex(commit->object.sha1));
202 while (*p && *p != '\n') {
205 for (eol = p; *eol && *eol != '\n'; eol++)
207 if (!prefixcmp(p, "author ")) {
208 char *line, *pend, *email, *timestamp;
211 line = xmemdupz(p, eol - p);
212 email = strchr(line, '<');
214 die ("Could not extract author email from %s",
215 sha1_to_hex(commit->object.sha1));
219 for (pend = email; pend != line + 1 &&
220 isspace(pend[-1]); pend--);
224 timestamp = strchr(email, '>');
226 die ("Could not extract author time from %s",
227 sha1_to_hex(commit->object.sha1));
229 for (timestamp++; *timestamp && isspace(*timestamp);
232 setenv("GIT_AUTHOR_NAME", line, 1);
233 setenv("GIT_AUTHOR_EMAIL", email, 1);
234 setenv("GIT_AUTHOR_DATE", timestamp, 1);
242 die ("No author information found in %s",
243 sha1_to_hex(commit->object.sha1));
246 static char *help_msg(const char *name)
248 struct strbuf helpbuf = STRBUF_INIT;
249 char *msg = getenv("GIT_CHERRY_PICK_HELP");
254 strbuf_addstr(&helpbuf, " After resolving the conflicts,\n"
255 "mark the corrected paths with 'git add <paths>' or 'git rm <paths>'\n"
256 "and commit the result");
258 if (action == CHERRY_PICK) {
259 strbuf_addf(&helpbuf, " with: \n"
261 " git commit -c %s\n",
265 strbuf_addch(&helpbuf, '.');
266 return strbuf_detach(&helpbuf, NULL);
269 static struct tree *empty_tree(void)
271 struct tree *tree = xcalloc(1, sizeof(struct tree));
273 tree->object.parsed = 1;
274 tree->object.type = OBJ_TREE;
275 pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
279 static NORETURN void die_dirty_index(const char *me)
281 if (read_cache_unmerged()) {
282 die_resolve_conflict(me);
284 if (advice_commit_before_merge)
285 die("Your local changes would be overwritten by %s.\n"
286 "Please, commit your changes or stash them to proceed.", me);
288 die("Your local changes would be overwritten by %s.\n", me);
292 static int fast_forward_to(const unsigned char *to, const unsigned char *from)
294 struct ref_lock *ref_lock;
297 if (checkout_fast_forward(from, to))
298 exit(1); /* the callee should have complained already */
299 ref_lock = lock_any_ref_for_update("HEAD", from, 0);
300 return write_ref_sha1(ref_lock, to, "cherry-pick");
303 static int revert_or_cherry_pick(int argc, const char **argv)
305 unsigned char head[20];
306 struct commit *base, *next, *parent;
307 const char *base_label, *next_label;
308 int i, index_fd, clean;
309 struct commit_message msg = { NULL, NULL, NULL, NULL, NULL };
311 struct merge_options o;
312 struct tree *result, *next_tree, *base_tree, *head_tree;
313 static struct lock_file index_lock;
315 git_config(git_default_config, NULL);
316 me = action == REVERT ? "revert" : "cherry-pick";
317 setenv(GIT_REFLOG_ACTION, me, 0);
318 parse_args(argc, argv);
320 /* this is copied from the shell script, but it's never triggered... */
321 if (action == REVERT && !no_replay)
322 die("revert is incompatible with replay");
326 die("cherry-pick --ff cannot be used with --signoff");
328 die("cherry-pick --ff cannot be used with --no-commit");
330 die("cherry-pick --ff cannot be used with -x");
332 die("cherry-pick --ff cannot be used with --edit");
335 if (read_cache() < 0)
336 die("git %s: failed to read the index", me);
339 * We do not intend to commit immediately. We just want to
340 * merge the differences in, so let's compute the tree
341 * that represents the "current" state for merge-recursive
344 if (write_cache_as_tree(head, 0, NULL))
345 die ("Your index file is unmerged.");
347 if (get_sha1("HEAD", head))
348 die ("You do not have a valid HEAD");
349 if (index_differs_from("HEAD", 0))
354 if (!commit->parents) {
355 if (action == REVERT)
356 die ("Cannot revert a root commit");
359 else if (commit->parents->next) {
360 /* Reverting or cherry-picking a merge commit */
362 struct commit_list *p;
365 die("Commit %s is a merge but no -m option was given.",
366 sha1_to_hex(commit->object.sha1));
368 for (cnt = 1, p = commit->parents;
369 cnt != mainline && p;
372 if (cnt != mainline || !p)
373 die("Commit %s does not have parent %d",
374 sha1_to_hex(commit->object.sha1), mainline);
376 } else if (0 < mainline)
377 die("Mainline was specified but commit %s is not a merge.",
378 sha1_to_hex(commit->object.sha1));
380 parent = commit->parents->item;
382 if (allow_ff && !hashcmp(parent->object.sha1, head))
383 return fast_forward_to(commit->object.sha1, head);
385 if (parent && parse_commit(parent) < 0)
386 die("%s: cannot parse parent commit %s",
387 me, sha1_to_hex(parent->object.sha1));
389 if (get_message(commit->buffer, &msg) != 0)
390 die("Cannot get commit message for %s",
391 sha1_to_hex(commit->object.sha1));
394 * "commit" is an existing commit. We would want to apply
395 * the difference it introduces since its first parent "prev"
396 * on top of the current HEAD if we are cherry-pick. Or the
397 * reverse of it if we are revert.
400 defmsg = git_pathdup("MERGE_MSG");
401 msg_fd = hold_lock_file_for_update(&msg_file, defmsg,
404 index_fd = hold_locked_index(&index_lock, 1);
406 if (action == REVERT) {
408 base_label = msg.label;
410 next_label = msg.parent_label;
411 add_to_msg("Revert \"");
412 add_to_msg(msg.subject);
413 add_to_msg("\"\n\nThis reverts commit ");
414 add_to_msg(sha1_to_hex(commit->object.sha1));
416 if (commit->parents->next) {
417 add_to_msg(", reversing\nchanges made to ");
418 add_to_msg(sha1_to_hex(parent->object.sha1));
423 base_label = msg.parent_label;
425 next_label = msg.label;
426 set_author_ident_env(msg.message);
427 add_message_to_msg(msg.message);
429 add_to_msg("(cherry picked from commit ");
430 add_to_msg(sha1_to_hex(commit->object.sha1));
436 init_merge_options(&o);
437 o.ancestor = base ? base_label : "(empty tree)";
439 o.branch2 = next ? next_label : "(empty tree)";
441 head_tree = parse_tree_indirect(head);
442 next_tree = next ? next->tree : empty_tree();
443 base_tree = base ? base->tree : empty_tree();
445 clean = merge_trees(&o,
447 next_tree, base_tree, &result);
449 if (active_cache_changed &&
450 (write_cache(index_fd, active_cache, active_nr) ||
451 commit_locked_index(&index_lock)))
452 die("%s: Unable to write new index file", me);
453 rollback_lock_file(&index_lock);
456 add_to_msg("\nConflicts:\n\n");
457 for (i = 0; i < active_nr;) {
458 struct cache_entry *ce = active_cache[i++];
461 add_to_msg(ce->name);
463 while (i < active_nr && !strcmp(ce->name,
464 active_cache[i]->name))
468 if (commit_lock_file(&msg_file) < 0)
469 die ("Error wrapping up %s", defmsg);
470 fprintf(stderr, "Automatic %s failed.%s\n",
471 me, help_msg(commit_name));
472 rerere(allow_rerere_auto);
475 if (commit_lock_file(&msg_file) < 0)
476 die ("Error wrapping up %s", defmsg);
477 fprintf(stderr, "Finished one %s.\n", me);
481 * If we are cherry-pick, and if the merge did not result in
482 * hand-editing, we will hit this commit and inherit the original
483 * author date and name.
484 * If we are revert, or if our cherry-pick results in a hand merge,
485 * we had better say that the current user is responsible for that.
489 /* 6 is max possible length of our args array including NULL */
492 args[i++] = "commit";
501 return execv_git_cmd(args);
509 int cmd_revert(int argc, const char **argv, const char *prefix)
515 return revert_or_cherry_pick(argc, argv);
518 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
521 action = CHERRY_PICK;
522 return revert_or_cherry_pick(argc, argv);