7 #include "run-command.h"
10 #include "parse-options.h"
11 #include "cache-tree.h"
15 #include "merge-recursive.h"
18 * This implements the builtins revert and cherry-pick.
20 * Copyright (c) 2007 Johannes E. Schindelin
22 * Based on git-revert.sh, which is
24 * Copyright (c) 2005 Linus Torvalds
25 * Copyright (c) 2005 Junio C Hamano
28 static const char * const revert_usage
[] = {
29 "git revert [options] <commit-ish>",
33 static const char * const cherry_pick_usage
[] = {
34 "git cherry-pick [options] <commit-ish>",
38 static int edit
, no_replay
, no_commit
, mainline
, signoff
;
39 static enum { REVERT
, CHERRY_PICK
} action
;
40 static struct commit
*commit
;
41 static const char *commit_name
;
42 static int allow_rerere_auto
;
44 static const char *me
;
46 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
48 static char *get_encoding(const char *message
);
50 static void parse_args(int argc
, const char **argv
)
52 const char * const * usage_str
=
53 action
== REVERT
? revert_usage
: cherry_pick_usage
;
54 unsigned char sha1
[20];
56 struct option options
[] = {
57 OPT_BOOLEAN('n', "no-commit", &no_commit
, "don't automatically commit"),
58 OPT_BOOLEAN('e', "edit", &edit
, "edit the commit message"),
59 OPT_BOOLEAN('x', NULL
, &no_replay
, "append commit name when cherry-picking"),
60 OPT_BOOLEAN('r', NULL
, &noop
, "no-op (backward compatibility)"),
61 OPT_BOOLEAN('s', "signoff", &signoff
, "add Signed-off-by:"),
62 OPT_INTEGER('m', "mainline", &mainline
, "parent number"),
63 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto
),
67 if (parse_options(argc
, argv
, NULL
, options
, usage_str
, 0) != 1)
68 usage_with_options(usage_str
, options
);
70 commit_name
= argv
[0];
71 if (get_sha1(commit_name
, sha1
))
72 die ("Cannot find '%s'", commit_name
);
73 commit
= lookup_commit_reference(sha1
);
78 struct commit_message
{
82 char *reencoded_message
;
86 static int get_message(const char *raw_message
, struct commit_message
*out
)
89 const char *p
, *abbrev
, *eol
;
91 int abbrev_len
, oneline_len
;
95 encoding
= get_encoding(raw_message
);
98 if (!git_commit_encoding
)
99 git_commit_encoding
= "UTF-8";
100 if ((out
->reencoded_message
= reencode_string(raw_message
,
101 git_commit_encoding
, encoding
)))
102 out
->message
= out
->reencoded_message
;
104 abbrev
= find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
);
105 abbrev_len
= strlen(abbrev
);
107 /* Find beginning and end of commit subject. */
109 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
113 for (eol
= p
+ 1; *eol
&& *eol
!= '\n'; eol
++)
117 oneline_len
= eol
- p
;
119 out
->parent_label
= xmalloc(strlen("parent of ") + abbrev_len
+
120 strlen("... ") + oneline_len
+ 1);
121 q
= out
->parent_label
;
122 q
= mempcpy(q
, "parent of ", strlen("parent of "));
124 q
= mempcpy(q
, abbrev
, abbrev_len
);
125 q
= mempcpy(q
, "... ", strlen("... "));
127 q
= mempcpy(q
, p
, oneline_len
);
132 static void free_message(struct commit_message
*msg
)
134 free(msg
->parent_label
);
135 free(msg
->reencoded_message
);
138 static char *get_encoding(const char *message
)
140 const char *p
= message
, *eol
;
143 die ("Could not read commit message of %s",
144 sha1_to_hex(commit
->object
.sha1
));
145 while (*p
&& *p
!= '\n') {
146 for (eol
= p
+ 1; *eol
&& *eol
!= '\n'; eol
++)
148 if (!prefixcmp(p
, "encoding ")) {
149 char *result
= xmalloc(eol
- 8 - p
);
150 strlcpy(result
, p
+ 9, eol
- 8 - p
);
160 static void add_message_to_msg(struct strbuf
*msgbuf
, const char *message
)
162 const char *p
= message
;
163 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
167 strbuf_addstr(msgbuf
, sha1_to_hex(commit
->object
.sha1
));
170 strbuf_addstr(msgbuf
, p
);
173 static void set_author_ident_env(const char *message
)
175 const char *p
= message
;
177 die ("Could not read commit message of %s",
178 sha1_to_hex(commit
->object
.sha1
));
179 while (*p
&& *p
!= '\n') {
182 for (eol
= p
; *eol
&& *eol
!= '\n'; eol
++)
184 if (!prefixcmp(p
, "author ")) {
185 char *line
, *pend
, *email
, *timestamp
;
188 line
= xmemdupz(p
, eol
- p
);
189 email
= strchr(line
, '<');
191 die ("Could not extract author email from %s",
192 sha1_to_hex(commit
->object
.sha1
));
196 for (pend
= email
; pend
!= line
+ 1 &&
197 isspace(pend
[-1]); pend
--);
201 timestamp
= strchr(email
, '>');
203 die ("Could not extract author time from %s",
204 sha1_to_hex(commit
->object
.sha1
));
206 for (timestamp
++; *timestamp
&& isspace(*timestamp
);
209 setenv("GIT_AUTHOR_NAME", line
, 1);
210 setenv("GIT_AUTHOR_EMAIL", email
, 1);
211 setenv("GIT_AUTHOR_DATE", timestamp
, 1);
219 die ("No author information found in %s",
220 sha1_to_hex(commit
->object
.sha1
));
223 static char *help_msg(const char *name
)
225 struct strbuf helpbuf
= STRBUF_INIT
;
226 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
231 strbuf_addstr(&helpbuf
, " After resolving the conflicts,\n"
232 "mark the corrected paths with 'git add <paths>' or 'git rm <paths>'\n"
233 "and commit the result");
235 if (action
== CHERRY_PICK
) {
236 strbuf_addf(&helpbuf
, " with: \n"
238 " git commit -c %s\n",
242 strbuf_addch(&helpbuf
, '.');
243 return strbuf_detach(&helpbuf
, NULL
);
246 static void write_message(struct strbuf
*msgbuf
, const char *filename
)
248 static struct lock_file msg_file
;
250 int msg_fd
= hold_lock_file_for_update(&msg_file
, filename
,
252 if (write_in_full(msg_fd
, msgbuf
->buf
, msgbuf
->len
) < 0)
253 die_errno("Could not write to %s.", filename
);
254 strbuf_release(msgbuf
);
255 if (commit_lock_file(&msg_file
) < 0)
256 die("Error wrapping up %s", filename
);
259 static struct tree
*empty_tree(void)
261 struct tree
*tree
= xcalloc(1, sizeof(struct tree
));
263 tree
->object
.parsed
= 1;
264 tree
->object
.type
= OBJ_TREE
;
265 pretend_sha1_file(NULL
, 0, OBJ_TREE
, tree
->object
.sha1
);
269 static NORETURN
void die_dirty_index(const char *me
)
271 if (read_cache_unmerged()) {
272 die_resolve_conflict(me
);
274 if (advice_commit_before_merge
)
275 die("Your local changes would be overwritten by %s.\n"
276 "Please, commit your changes or stash them to proceed.", me
);
278 die("Your local changes would be overwritten by %s.\n", me
);
282 static void do_recursive_merge(struct commit
*base
, struct commit
*next
,
283 const char *base_label
, const char *next_label
,
284 unsigned char *head
, struct strbuf
*msgbuf
,
287 struct merge_options o
;
288 struct tree
*result
, *next_tree
, *base_tree
, *head_tree
;
290 static struct lock_file index_lock
;
292 index_fd
= hold_locked_index(&index_lock
, 1);
295 init_merge_options(&o
);
296 o
.ancestor
= base
? base_label
: "(empty tree)";
298 o
.branch2
= next
? next_label
: "(empty tree)";
300 head_tree
= parse_tree_indirect(head
);
301 next_tree
= next
? next
->tree
: empty_tree();
302 base_tree
= base
? base
->tree
: empty_tree();
304 clean
= merge_trees(&o
,
306 next_tree
, base_tree
, &result
);
308 if (active_cache_changed
&&
309 (write_cache(index_fd
, active_cache
, active_nr
) ||
310 commit_locked_index(&index_lock
)))
311 die("%s: Unable to write new index file", me
);
312 rollback_lock_file(&index_lock
);
316 strbuf_addstr(msgbuf
, "\nConflicts:\n\n");
317 for (i
= 0; i
< active_nr
;) {
318 struct cache_entry
*ce
= active_cache
[i
++];
320 strbuf_addch(msgbuf
, '\t');
321 strbuf_addstr(msgbuf
, ce
->name
);
322 strbuf_addch(msgbuf
, '\n');
323 while (i
< active_nr
&& !strcmp(ce
->name
,
324 active_cache
[i
]->name
))
328 write_message(msgbuf
, defmsg
);
329 fprintf(stderr
, "Automatic %s failed.%s\n",
330 me
, help_msg(commit_name
));
331 rerere(allow_rerere_auto
);
334 write_message(msgbuf
, defmsg
);
335 fprintf(stderr
, "Finished one %s.\n", me
);
338 static int revert_or_cherry_pick(int argc
, const char **argv
)
340 unsigned char head
[20];
341 struct commit
*base
, *next
, *parent
;
342 const char *base_label
, *next_label
;
343 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
, NULL
};
344 char *defmsg
= git_pathdup("MERGE_MSG");
345 struct strbuf msgbuf
= STRBUF_INIT
;
347 git_config(git_default_config
, NULL
);
348 me
= action
== REVERT
? "revert" : "cherry-pick";
349 setenv(GIT_REFLOG_ACTION
, me
, 0);
350 parse_args(argc
, argv
);
352 /* this is copied from the shell script, but it's never triggered... */
353 if (action
== REVERT
&& !no_replay
)
354 die("revert is incompatible with replay");
356 if (read_cache() < 0)
357 die("git %s: failed to read the index", me
);
360 * We do not intend to commit immediately. We just want to
361 * merge the differences in, so let's compute the tree
362 * that represents the "current" state for merge-recursive
365 if (write_cache_as_tree(head
, 0, NULL
))
366 die ("Your index file is unmerged.");
368 if (get_sha1("HEAD", head
))
369 die ("You do not have a valid HEAD");
370 if (index_differs_from("HEAD", 0))
375 if (!commit
->parents
) {
376 if (action
== REVERT
)
377 die ("Cannot revert a root commit");
380 else if (commit
->parents
->next
) {
381 /* Reverting or cherry-picking a merge commit */
383 struct commit_list
*p
;
386 die("Commit %s is a merge but no -m option was given.",
387 sha1_to_hex(commit
->object
.sha1
));
389 for (cnt
= 1, p
= commit
->parents
;
390 cnt
!= mainline
&& p
;
393 if (cnt
!= mainline
|| !p
)
394 die("Commit %s does not have parent %d",
395 sha1_to_hex(commit
->object
.sha1
), mainline
);
397 } else if (0 < mainline
)
398 die("Mainline was specified but commit %s is not a merge.",
399 sha1_to_hex(commit
->object
.sha1
));
401 parent
= commit
->parents
->item
;
403 if (parent
&& parse_commit(parent
) < 0)
404 die("%s: cannot parse parent commit %s",
405 me
, sha1_to_hex(parent
->object
.sha1
));
407 if (get_message(commit
->buffer
, &msg
) != 0)
408 die("Cannot get commit message for %s",
409 sha1_to_hex(commit
->object
.sha1
));
412 * "commit" is an existing commit. We would want to apply
413 * the difference it introduces since its first parent "prev"
414 * on top of the current HEAD if we are cherry-pick. Or the
415 * reverse of it if we are revert.
418 if (action
== REVERT
) {
420 base_label
= msg
.label
;
422 next_label
= msg
.parent_label
;
423 strbuf_addstr(&msgbuf
, "Revert \"");
424 strbuf_addstr(&msgbuf
, msg
.subject
);
425 strbuf_addstr(&msgbuf
, "\"\n\nThis reverts commit ");
426 strbuf_addstr(&msgbuf
, sha1_to_hex(commit
->object
.sha1
));
428 if (commit
->parents
->next
) {
429 strbuf_addstr(&msgbuf
, ", reversing\nchanges made to ");
430 strbuf_addstr(&msgbuf
, sha1_to_hex(parent
->object
.sha1
));
432 strbuf_addstr(&msgbuf
, ".\n");
435 base_label
= msg
.parent_label
;
437 next_label
= msg
.label
;
438 set_author_ident_env(msg
.message
);
439 add_message_to_msg(&msgbuf
, msg
.message
);
441 strbuf_addstr(&msgbuf
, "(cherry picked from commit ");
442 strbuf_addstr(&msgbuf
, sha1_to_hex(commit
->object
.sha1
));
443 strbuf_addstr(&msgbuf
, ")\n");
447 do_recursive_merge(base
, next
, base_label
, next_label
,
448 head
, &msgbuf
, defmsg
);
452 * If we are cherry-pick, and if the merge did not result in
453 * hand-editing, we will hit this commit and inherit the original
454 * author date and name.
455 * If we are revert, or if our cherry-pick results in a hand merge,
456 * we had better say that the current user is responsible for that.
460 /* 6 is max possible length of our args array including NULL */
463 args
[i
++] = "commit";
472 return execv_git_cmd(args
);
480 int cmd_revert(int argc
, const char **argv
, const char *prefix
)
486 return revert_or_cherry_pick(argc
, argv
);
489 int cmd_cherry_pick(int argc
, const char **argv
, const char *prefix
)
492 action
= CHERRY_PICK
;
493 return revert_or_cherry_pick(argc
, argv
);