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
;
42 static const char *me
;
44 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
46 static void parse_args(int argc
, const char **argv
)
48 const char * const * usage_str
=
49 action
== REVERT
? revert_usage
: cherry_pick_usage
;
50 unsigned char sha1
[20];
53 struct option options
[] = {
54 OPT_BOOLEAN('n', "no-commit", &no_commit
, "don't automatically commit"),
55 OPT_BOOLEAN('e', "edit", &edit
, "edit the commit message"),
56 OPT_BOOLEAN('x', NULL
, &no_replay
, "append commit name when cherry-picking"),
57 OPT_BOOLEAN('r', NULL
, &noop
, "no-op (backward compatibility)"),
58 OPT_BOOLEAN('s', "signoff", &signoff
, "add Signed-off-by:"),
59 OPT_INTEGER('m', "mainline", &mainline
, "parent number"),
63 if (parse_options(argc
, argv
, options
, usage_str
, 0) != 1)
64 usage_with_options(usage_str
, options
);
67 if (get_sha1(arg
, sha1
))
68 die ("Cannot find '%s'", arg
);
69 commit
= (struct commit
*)parse_object(sha1
);
71 die ("Could not find %s", sha1_to_hex(sha1
));
72 if (commit
->object
.type
== OBJ_TAG
) {
73 commit
= (struct commit
*)
74 deref_tag((struct object
*)commit
, arg
, strlen(arg
));
76 if (commit
->object
.type
!= OBJ_COMMIT
)
77 die ("'%s' does not point to a commit", arg
);
80 static char *get_oneline(const char *message
)
83 const char *p
= message
, *abbrev
, *eol
;
84 int abbrev_len
, oneline_len
;
87 die ("Could not read commit message of %s",
88 sha1_to_hex(commit
->object
.sha1
));
89 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
94 for (eol
= p
+ 1; *eol
&& *eol
!= '\n'; eol
++)
98 abbrev
= find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
);
99 abbrev_len
= strlen(abbrev
);
100 oneline_len
= eol
- p
;
101 result
= xmalloc(abbrev_len
+ 5 + oneline_len
);
102 memcpy(result
, abbrev
, abbrev_len
);
103 memcpy(result
+ abbrev_len
, "... ", 4);
104 memcpy(result
+ abbrev_len
+ 4, p
, oneline_len
);
105 result
[abbrev_len
+ 4 + oneline_len
] = '\0';
109 static char *get_encoding(const char *message
)
111 const char *p
= message
, *eol
;
114 die ("Could not read commit message of %s",
115 sha1_to_hex(commit
->object
.sha1
));
116 while (*p
&& *p
!= '\n') {
117 for (eol
= p
+ 1; *eol
&& *eol
!= '\n'; eol
++)
119 if (!prefixcmp(p
, "encoding ")) {
120 char *result
= xmalloc(eol
- 8 - p
);
121 strlcpy(result
, p
+ 9, eol
- 8 - p
);
131 static struct lock_file msg_file
;
134 static void add_to_msg(const char *string
)
136 int len
= strlen(string
);
137 if (write_in_full(msg_fd
, string
, len
) < 0)
138 die ("Could not write to MERGE_MSG");
141 static void add_message_to_msg(const char *message
)
143 const char *p
= message
;
144 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
148 add_to_msg(sha1_to_hex(commit
->object
.sha1
));
155 static void set_author_ident_env(const char *message
)
157 const char *p
= message
;
159 die ("Could not read commit message of %s",
160 sha1_to_hex(commit
->object
.sha1
));
161 while (*p
&& *p
!= '\n') {
164 for (eol
= p
; *eol
&& *eol
!= '\n'; eol
++)
166 if (!prefixcmp(p
, "author ")) {
167 char *line
, *pend
, *email
, *timestamp
;
170 line
= xmemdupz(p
, eol
- p
);
171 email
= strchr(line
, '<');
173 die ("Could not extract author email from %s",
174 sha1_to_hex(commit
->object
.sha1
));
178 for (pend
= email
; pend
!= line
+ 1 &&
179 isspace(pend
[-1]); pend
--);
183 timestamp
= strchr(email
, '>');
185 die ("Could not extract author time from %s",
186 sha1_to_hex(commit
->object
.sha1
));
188 for (timestamp
++; *timestamp
&& isspace(*timestamp
);
191 setenv("GIT_AUTHOR_NAME", line
, 1);
192 setenv("GIT_AUTHOR_EMAIL", email
, 1);
193 setenv("GIT_AUTHOR_DATE", timestamp
, 1);
201 die ("No author information found in %s",
202 sha1_to_hex(commit
->object
.sha1
));
205 static char *help_msg(const unsigned char *sha1
)
207 static char helpbuf
[1024];
208 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
213 strcpy(helpbuf
, " After resolving the conflicts,\n"
214 "mark the corrected paths with 'git add <paths>' "
215 "or 'git rm <paths>' and commit the result.");
217 if (action
== CHERRY_PICK
) {
218 sprintf(helpbuf
+ strlen(helpbuf
),
219 "\nWhen commiting, use the option "
220 "'-c %s' to retain authorship and message.",
221 find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
226 static int index_is_dirty(void)
229 init_revisions(&rev
, NULL
);
230 setup_revisions(0, NULL
, &rev
, "HEAD");
231 DIFF_OPT_SET(&rev
.diffopt
, QUIET
);
232 DIFF_OPT_SET(&rev
.diffopt
, EXIT_WITH_STATUS
);
233 run_diff_index(&rev
, 1);
234 return !!DIFF_OPT_TST(&rev
.diffopt
, HAS_CHANGES
);
237 static struct tree
*empty_tree(void)
239 struct tree
*tree
= xcalloc(1, sizeof(struct tree
));
241 tree
->object
.parsed
= 1;
242 tree
->object
.type
= OBJ_TREE
;
243 pretend_sha1_file(NULL
, 0, OBJ_TREE
, tree
->object
.sha1
);
247 static int revert_or_cherry_pick(int argc
, const char **argv
)
249 unsigned char head
[20];
250 struct commit
*base
, *next
, *parent
;
251 int i
, index_fd
, clean
;
252 char *oneline
, *reencoded_message
= NULL
;
253 const char *message
, *encoding
;
254 char *defmsg
= git_pathdup("MERGE_MSG");
255 struct merge_options o
;
256 struct tree
*result
, *next_tree
, *base_tree
, *head_tree
;
257 static struct lock_file index_lock
;
259 git_config(git_default_config
, NULL
);
260 me
= action
== REVERT
? "revert" : "cherry-pick";
261 setenv(GIT_REFLOG_ACTION
, me
, 0);
262 parse_args(argc
, argv
);
264 /* this is copied from the shell script, but it's never triggered... */
265 if (action
== REVERT
&& !no_replay
)
266 die("revert is incompatible with replay");
268 if (read_cache() < 0)
269 die("git %s: failed to read the index", me
);
272 * We do not intend to commit immediately. We just want to
273 * merge the differences in, so let's compute the tree
274 * that represents the "current" state for merge-recursive
277 if (write_cache_as_tree(head
, 0, NULL
))
278 die ("Your index file is unmerged.");
280 if (get_sha1("HEAD", head
))
281 die ("You do not have a valid HEAD");
282 if (index_is_dirty())
283 die ("Dirty index: cannot %s", me
);
287 index_fd
= hold_locked_index(&index_lock
, 1);
289 if (!commit
->parents
) {
290 if (action
== REVERT
)
291 die ("Cannot revert a root commit");
294 else if (commit
->parents
->next
) {
295 /* Reverting or cherry-picking a merge commit */
297 struct commit_list
*p
;
300 die("Commit %s is a merge but no -m option was given.",
301 sha1_to_hex(commit
->object
.sha1
));
303 for (cnt
= 1, p
= commit
->parents
;
304 cnt
!= mainline
&& p
;
307 if (cnt
!= mainline
|| !p
)
308 die("Commit %s does not have parent %d",
309 sha1_to_hex(commit
->object
.sha1
), mainline
);
311 } else if (0 < mainline
)
312 die("Mainline was specified but commit %s is not a merge.",
313 sha1_to_hex(commit
->object
.sha1
));
315 parent
= commit
->parents
->item
;
317 if (!(message
= commit
->buffer
))
318 die ("Cannot get commit message for %s",
319 sha1_to_hex(commit
->object
.sha1
));
321 if (parent
&& parse_commit(parent
) < 0)
322 die("%s: cannot parse parent commit %s",
323 me
, sha1_to_hex(parent
->object
.sha1
));
326 * "commit" is an existing commit. We would want to apply
327 * the difference it introduces since its first parent "prev"
328 * on top of the current HEAD if we are cherry-pick. Or the
329 * reverse of it if we are revert.
332 msg_fd
= hold_lock_file_for_update(&msg_file
, defmsg
,
335 encoding
= get_encoding(message
);
338 if (!git_commit_encoding
)
339 git_commit_encoding
= "utf-8";
340 if ((reencoded_message
= reencode_string(message
,
341 git_commit_encoding
, encoding
)))
342 message
= reencoded_message
;
344 oneline
= get_oneline(message
);
346 if (action
== REVERT
) {
347 char *oneline_body
= strchr(oneline
, ' ');
351 add_to_msg("Revert \"");
352 add_to_msg(oneline_body
+ 1);
353 add_to_msg("\"\n\nThis reverts commit ");
354 add_to_msg(sha1_to_hex(commit
->object
.sha1
));
359 set_author_ident_env(message
);
360 add_message_to_msg(message
);
362 add_to_msg("(cherry picked from commit ");
363 add_to_msg(sha1_to_hex(commit
->object
.sha1
));
369 init_merge_options(&o
);
373 head_tree
= parse_tree_indirect(head
);
374 next_tree
= next
? next
->tree
: empty_tree();
375 base_tree
= base
? base
->tree
: empty_tree();
377 clean
= merge_trees(&o
,
379 next_tree
, base_tree
, &result
);
381 if (active_cache_changed
&&
382 (write_cache(index_fd
, active_cache
, active_nr
) ||
383 commit_locked_index(&index_lock
)))
384 die("%s: Unable to write new index file", me
);
387 add_to_msg("\nConflicts:\n\n");
388 for (i
= 0; i
< active_nr
;) {
389 struct cache_entry
*ce
= active_cache
[i
++];
392 add_to_msg(ce
->name
);
394 while (i
< active_nr
&& !strcmp(ce
->name
,
395 active_cache
[i
]->name
))
399 if (commit_lock_file(&msg_file
) < 0)
400 die ("Error wrapping up %s", defmsg
);
401 fprintf(stderr
, "Automatic %s failed.%s\n",
402 me
, help_msg(commit
->object
.sha1
));
406 if (commit_lock_file(&msg_file
) < 0)
407 die ("Error wrapping up %s", defmsg
);
408 fprintf(stderr
, "Finished one %s.\n", me
);
412 * If we are cherry-pick, and if the merge did not result in
413 * hand-editing, we will hit this commit and inherit the original
414 * author date and name.
415 * If we are revert, or if our cherry-pick results in a hand merge,
416 * we had better say that the current user is responsible for that.
420 /* 6 is max possible length of our args array including NULL */
423 args
[i
++] = "commit";
432 return execv_git_cmd(args
);
434 free(reencoded_message
);
440 int cmd_revert(int argc
, const char **argv
, const char *prefix
)
446 return revert_or_cherry_pick(argc
, argv
);
449 int cmd_cherry_pick(int argc
, const char **argv
, const char *prefix
)
452 action
= CHERRY_PICK
;
453 return revert_or_cherry_pick(argc
, argv
);