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 int allow_rerere_auto
;
43 static const char *me
;
45 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
47 static void parse_args(int argc
, const char **argv
)
49 const char * const * usage_str
=
50 action
== REVERT
? revert_usage
: cherry_pick_usage
;
51 unsigned char sha1
[20];
54 struct option options
[] = {
55 OPT_BOOLEAN('n', "no-commit", &no_commit
, "don't automatically commit"),
56 OPT_BOOLEAN('e', "edit", &edit
, "edit the commit message"),
57 OPT_BOOLEAN('x', NULL
, &no_replay
, "append commit name when cherry-picking"),
58 OPT_BOOLEAN('r', NULL
, &noop
, "no-op (backward compatibility)"),
59 OPT_BOOLEAN('s', "signoff", &signoff
, "add Signed-off-by:"),
60 OPT_INTEGER('m', "mainline", &mainline
, "parent number"),
61 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto
),
65 if (parse_options(argc
, argv
, NULL
, options
, usage_str
, 0) != 1)
66 usage_with_options(usage_str
, options
);
69 if (get_sha1(arg
, sha1
))
70 die ("Cannot find '%s'", arg
);
71 commit
= (struct commit
*)parse_object(sha1
);
73 die ("Could not find %s", sha1_to_hex(sha1
));
74 if (commit
->object
.type
== OBJ_TAG
) {
75 commit
= (struct commit
*)
76 deref_tag((struct object
*)commit
, arg
, strlen(arg
));
78 if (commit
->object
.type
!= OBJ_COMMIT
)
79 die ("'%s' does not point to a commit", arg
);
82 static char *get_oneline(const char *message
)
85 const char *p
= message
, *abbrev
, *eol
;
86 int abbrev_len
, oneline_len
;
89 die ("Could not read commit message of %s",
90 sha1_to_hex(commit
->object
.sha1
));
91 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
96 for (eol
= p
+ 1; *eol
&& *eol
!= '\n'; eol
++)
100 abbrev
= find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
);
101 abbrev_len
= strlen(abbrev
);
102 oneline_len
= eol
- p
;
103 result
= xmalloc(abbrev_len
+ 5 + oneline_len
);
104 memcpy(result
, abbrev
, abbrev_len
);
105 memcpy(result
+ abbrev_len
, "... ", 4);
106 memcpy(result
+ abbrev_len
+ 4, p
, oneline_len
);
107 result
[abbrev_len
+ 4 + oneline_len
] = '\0';
111 static char *get_encoding(const char *message
)
113 const char *p
= message
, *eol
;
116 die ("Could not read commit message of %s",
117 sha1_to_hex(commit
->object
.sha1
));
118 while (*p
&& *p
!= '\n') {
119 for (eol
= p
+ 1; *eol
&& *eol
!= '\n'; eol
++)
121 if (!prefixcmp(p
, "encoding ")) {
122 char *result
= xmalloc(eol
- 8 - p
);
123 strlcpy(result
, p
+ 9, eol
- 8 - p
);
133 static struct lock_file msg_file
;
136 static void add_to_msg(const char *string
)
138 int len
= strlen(string
);
139 if (write_in_full(msg_fd
, string
, len
) < 0)
140 die_errno ("Could not write to MERGE_MSG");
143 static void add_message_to_msg(const char *message
)
145 const char *p
= message
;
146 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
150 add_to_msg(sha1_to_hex(commit
->object
.sha1
));
157 static void set_author_ident_env(const char *message
)
159 const char *p
= message
;
161 die ("Could not read commit message of %s",
162 sha1_to_hex(commit
->object
.sha1
));
163 while (*p
&& *p
!= '\n') {
166 for (eol
= p
; *eol
&& *eol
!= '\n'; eol
++)
168 if (!prefixcmp(p
, "author ")) {
169 char *line
, *pend
, *email
, *timestamp
;
172 line
= xmemdupz(p
, eol
- p
);
173 email
= strchr(line
, '<');
175 die ("Could not extract author email from %s",
176 sha1_to_hex(commit
->object
.sha1
));
180 for (pend
= email
; pend
!= line
+ 1 &&
181 isspace(pend
[-1]); pend
--);
185 timestamp
= strchr(email
, '>');
187 die ("Could not extract author time from %s",
188 sha1_to_hex(commit
->object
.sha1
));
190 for (timestamp
++; *timestamp
&& isspace(*timestamp
);
193 setenv("GIT_AUTHOR_NAME", line
, 1);
194 setenv("GIT_AUTHOR_EMAIL", email
, 1);
195 setenv("GIT_AUTHOR_DATE", timestamp
, 1);
203 die ("No author information found in %s",
204 sha1_to_hex(commit
->object
.sha1
));
207 static char *help_msg(const unsigned char *sha1
)
209 static char helpbuf
[1024];
210 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
215 strcpy(helpbuf
, " After resolving the conflicts,\n"
216 "mark the corrected paths with 'git add <paths>' "
217 "or 'git rm <paths>' and commit the result.");
219 if (action
== CHERRY_PICK
) {
220 sprintf(helpbuf
+ strlen(helpbuf
),
221 "\nWhen commiting, use the option "
222 "'-c %s' to retain authorship and message.",
223 find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
228 static struct tree
*empty_tree(void)
230 struct tree
*tree
= xcalloc(1, sizeof(struct tree
));
232 tree
->object
.parsed
= 1;
233 tree
->object
.type
= OBJ_TREE
;
234 pretend_sha1_file(NULL
, 0, OBJ_TREE
, tree
->object
.sha1
);
238 static int revert_or_cherry_pick(int argc
, const char **argv
)
240 unsigned char head
[20];
241 struct commit
*base
, *next
, *parent
;
242 int i
, index_fd
, clean
;
243 char *oneline
, *reencoded_message
= NULL
;
244 const char *message
, *encoding
;
245 char *defmsg
= git_pathdup("MERGE_MSG");
246 struct merge_options o
;
247 struct tree
*result
, *next_tree
, *base_tree
, *head_tree
;
248 static struct lock_file index_lock
;
250 git_config(git_default_config
, NULL
);
251 me
= action
== REVERT
? "revert" : "cherry-pick";
252 setenv(GIT_REFLOG_ACTION
, me
, 0);
253 parse_args(argc
, argv
);
255 /* this is copied from the shell script, but it's never triggered... */
256 if (action
== REVERT
&& !no_replay
)
257 die("revert is incompatible with replay");
259 if (read_cache() < 0)
260 die("git %s: failed to read the index", me
);
263 * We do not intend to commit immediately. We just want to
264 * merge the differences in, so let's compute the tree
265 * that represents the "current" state for merge-recursive
268 if (write_cache_as_tree(head
, 0, NULL
))
269 die ("Your index file is unmerged.");
271 if (get_sha1("HEAD", head
))
272 die ("You do not have a valid HEAD");
273 if (index_differs_from("HEAD", 0))
274 die ("Dirty index: cannot %s", me
);
278 index_fd
= hold_locked_index(&index_lock
, 1);
280 if (!commit
->parents
) {
281 if (action
== REVERT
)
282 die ("Cannot revert a root commit");
285 else if (commit
->parents
->next
) {
286 /* Reverting or cherry-picking a merge commit */
288 struct commit_list
*p
;
291 die("Commit %s is a merge but no -m option was given.",
292 sha1_to_hex(commit
->object
.sha1
));
294 for (cnt
= 1, p
= commit
->parents
;
295 cnt
!= mainline
&& p
;
298 if (cnt
!= mainline
|| !p
)
299 die("Commit %s does not have parent %d",
300 sha1_to_hex(commit
->object
.sha1
), mainline
);
302 } else if (0 < mainline
)
303 die("Mainline was specified but commit %s is not a merge.",
304 sha1_to_hex(commit
->object
.sha1
));
306 parent
= commit
->parents
->item
;
308 if (!(message
= commit
->buffer
))
309 die ("Cannot get commit message for %s",
310 sha1_to_hex(commit
->object
.sha1
));
312 if (parent
&& parse_commit(parent
) < 0)
313 die("%s: cannot parse parent commit %s",
314 me
, sha1_to_hex(parent
->object
.sha1
));
317 * "commit" is an existing commit. We would want to apply
318 * the difference it introduces since its first parent "prev"
319 * on top of the current HEAD if we are cherry-pick. Or the
320 * reverse of it if we are revert.
323 msg_fd
= hold_lock_file_for_update(&msg_file
, defmsg
,
326 encoding
= get_encoding(message
);
329 if (!git_commit_encoding
)
330 git_commit_encoding
= "UTF-8";
331 if ((reencoded_message
= reencode_string(message
,
332 git_commit_encoding
, encoding
)))
333 message
= reencoded_message
;
335 oneline
= get_oneline(message
);
337 if (action
== REVERT
) {
338 char *oneline_body
= strchr(oneline
, ' ');
342 add_to_msg("Revert \"");
343 add_to_msg(oneline_body
+ 1);
344 add_to_msg("\"\n\nThis reverts commit ");
345 add_to_msg(sha1_to_hex(commit
->object
.sha1
));
347 if (commit
->parents
->next
) {
348 add_to_msg(", reversing\nchanges made to ");
349 add_to_msg(sha1_to_hex(parent
->object
.sha1
));
355 set_author_ident_env(message
);
356 add_message_to_msg(message
);
358 add_to_msg("(cherry picked from commit ");
359 add_to_msg(sha1_to_hex(commit
->object
.sha1
));
365 init_merge_options(&o
);
369 head_tree
= parse_tree_indirect(head
);
370 next_tree
= next
? next
->tree
: empty_tree();
371 base_tree
= base
? base
->tree
: empty_tree();
373 clean
= merge_trees(&o
,
375 next_tree
, base_tree
, &result
);
377 if (active_cache_changed
&&
378 (write_cache(index_fd
, active_cache
, active_nr
) ||
379 commit_locked_index(&index_lock
)))
380 die("%s: Unable to write new index file", me
);
381 rollback_lock_file(&index_lock
);
384 add_to_msg("\nConflicts:\n\n");
385 for (i
= 0; i
< active_nr
;) {
386 struct cache_entry
*ce
= active_cache
[i
++];
389 add_to_msg(ce
->name
);
391 while (i
< active_nr
&& !strcmp(ce
->name
,
392 active_cache
[i
]->name
))
396 if (commit_lock_file(&msg_file
) < 0)
397 die ("Error wrapping up %s", defmsg
);
398 fprintf(stderr
, "Automatic %s failed.%s\n",
399 me
, help_msg(commit
->object
.sha1
));
400 rerere(allow_rerere_auto
);
403 if (commit_lock_file(&msg_file
) < 0)
404 die ("Error wrapping up %s", defmsg
);
405 fprintf(stderr
, "Finished one %s.\n", me
);
409 * If we are cherry-pick, and if the merge did not result in
410 * hand-editing, we will hit this commit and inherit the original
411 * author date and name.
412 * If we are revert, or if our cherry-pick results in a hand merge,
413 * we had better say that the current user is responsible for that.
417 /* 6 is max possible length of our args array including NULL */
420 args
[i
++] = "commit";
429 return execv_git_cmd(args
);
431 free(reencoded_message
);
437 int cmd_revert(int argc
, const char **argv
, const char *prefix
)
443 return revert_or_cherry_pick(argc
, argv
);
446 int cmd_cherry_pick(int argc
, const char **argv
, const char *prefix
)
449 action
= CHERRY_PICK
;
450 return revert_or_cherry_pick(argc
, argv
);