7 #include "run-command.h"
10 #include "parse-options.h"
13 * This implements the builtins revert and cherry-pick.
15 * Copyright (c) 2007 Johannes E. Schindelin
17 * Based on git-revert.sh, which is
19 * Copyright (c) 2005 Linus Torvalds
20 * Copyright (c) 2005 Junio C Hamano
23 static const char * const revert_usage
[] = {
24 "git-revert [options] <commit-ish>",
28 static const char * const cherry_pick_usage
[] = {
29 "git-cherry-pick [options] <commit-ish>",
33 static int edit
, no_replay
, no_commit
, mainline
;
34 static enum { REVERT
, CHERRY_PICK
} action
;
35 static struct commit
*commit
;
37 static const char *me
;
39 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
41 static void parse_args(int argc
, const char **argv
)
43 const char * const * usage_str
=
44 action
== REVERT
? revert_usage
: cherry_pick_usage
;
45 unsigned char sha1
[20];
48 struct option options
[] = {
49 OPT_BOOLEAN('n', "no-commit", &no_commit
, "don't automatically commit"),
50 OPT_BOOLEAN('e', "edit", &edit
, "edit the commit message"),
51 OPT_BOOLEAN('x', NULL
, &no_replay
, "append commit name when cherry-picking"),
52 OPT_BOOLEAN('r', NULL
, &noop
, "no-op (backward compatibility)"),
53 OPT_INTEGER('m', "mainline", &mainline
, "parent number"),
57 if (parse_options(argc
, argv
, options
, usage_str
, 0) != 1)
58 usage_with_options(usage_str
, options
);
61 if (get_sha1(arg
, sha1
))
62 die ("Cannot find '%s'", arg
);
63 commit
= (struct commit
*)parse_object(sha1
);
65 die ("Could not find %s", sha1_to_hex(sha1
));
66 if (commit
->object
.type
== OBJ_TAG
) {
67 commit
= (struct commit
*)
68 deref_tag((struct object
*)commit
, arg
, strlen(arg
));
70 if (commit
->object
.type
!= OBJ_COMMIT
)
71 die ("'%s' does not point to a commit", arg
);
74 static char *get_oneline(const char *message
)
77 const char *p
= message
, *abbrev
, *eol
;
78 int abbrev_len
, oneline_len
;
81 die ("Could not read commit message of %s",
82 sha1_to_hex(commit
->object
.sha1
));
83 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
88 for (eol
= p
+ 1; *eol
&& *eol
!= '\n'; eol
++)
92 abbrev
= find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
);
93 abbrev_len
= strlen(abbrev
);
94 oneline_len
= eol
- p
;
95 result
= xmalloc(abbrev_len
+ 5 + oneline_len
);
96 memcpy(result
, abbrev
, abbrev_len
);
97 memcpy(result
+ abbrev_len
, "... ", 4);
98 memcpy(result
+ abbrev_len
+ 4, p
, oneline_len
);
99 result
[abbrev_len
+ 4 + oneline_len
] = '\0';
103 static char *get_encoding(const char *message
)
105 const char *p
= message
, *eol
;
108 die ("Could not read commit message of %s",
109 sha1_to_hex(commit
->object
.sha1
));
110 while (*p
&& *p
!= '\n') {
111 for (eol
= p
+ 1; *eol
&& *eol
!= '\n'; eol
++)
113 if (!prefixcmp(p
, "encoding ")) {
114 char *result
= xmalloc(eol
- 8 - p
);
115 strlcpy(result
, p
+ 9, eol
- 8 - p
);
125 static struct lock_file msg_file
;
128 static void add_to_msg(const char *string
)
130 int len
= strlen(string
);
131 if (write_in_full(msg_fd
, string
, len
) < 0)
132 die ("Could not write to MERGE_MSG");
135 static void add_message_to_msg(const char *message
)
137 const char *p
= message
;
138 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
142 add_to_msg(sha1_to_hex(commit
->object
.sha1
));
149 static void set_author_ident_env(const char *message
)
151 const char *p
= message
;
153 die ("Could not read commit message of %s",
154 sha1_to_hex(commit
->object
.sha1
));
155 while (*p
&& *p
!= '\n') {
158 for (eol
= p
; *eol
&& *eol
!= '\n'; eol
++)
160 if (!prefixcmp(p
, "author ")) {
161 char *line
, *pend
, *email
, *timestamp
;
164 line
= xmemdupz(p
, eol
- p
);
165 email
= strchr(line
, '<');
167 die ("Could not extract author email from %s",
168 sha1_to_hex(commit
->object
.sha1
));
172 for (pend
= email
; pend
!= line
+ 1 &&
173 isspace(pend
[-1]); pend
--);
177 timestamp
= strchr(email
, '>');
179 die ("Could not extract author email from %s",
180 sha1_to_hex(commit
->object
.sha1
));
182 for (timestamp
++; *timestamp
&& isspace(*timestamp
);
185 setenv("GIT_AUTHOR_NAME", line
, 1);
186 setenv("GIT_AUTHOR_EMAIL", email
, 1);
187 setenv("GIT_AUTHOR_DATE", timestamp
, 1);
195 die ("No author information found in %s",
196 sha1_to_hex(commit
->object
.sha1
));
199 static int merge_recursive(const char *base_sha1
,
200 const char *head_sha1
, const char *head_name
,
201 const char *next_sha1
, const char *next_name
)
206 sprintf(buffer
, "GITHEAD_%s", head_sha1
);
207 setenv(buffer
, head_name
, 1);
208 sprintf(buffer
, "GITHEAD_%s", next_sha1
);
209 setenv(buffer
, next_name
, 1);
212 * This three way merge is an interesting one. We are at
213 * $head, and would want to apply the change between $commit
214 * and $prev on top of us (when reverting), or the change between
215 * $prev and $commit on top of us (when cherry-picking or replaying).
217 argv
[0] = "merge-recursive";
224 return run_command_v_opt(argv
, RUN_COMMAND_NO_STDIN
| RUN_GIT_CMD
);
227 static char *help_msg(const unsigned char *sha1
)
229 static char helpbuf
[1024];
230 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
235 strcpy(helpbuf
, " After resolving the conflicts,\n"
236 "mark the corrected paths with 'git add <paths>' "
237 "and commit the result.");
239 if (action
== CHERRY_PICK
) {
240 sprintf(helpbuf
+ strlen(helpbuf
),
241 "\nWhen commiting, use the option "
242 "'-c %s' to retain authorship and message.",
243 find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
248 static int revert_or_cherry_pick(int argc
, const char **argv
)
250 unsigned char head
[20];
251 struct commit
*base
, *next
, *parent
;
253 char *oneline
, *reencoded_message
= NULL
;
254 const char *message
, *encoding
;
255 const char *defmsg
= xstrdup(git_path("MERGE_MSG"));
257 git_config(git_default_config
);
258 me
= action
== REVERT
? "revert" : "cherry-pick";
259 setenv(GIT_REFLOG_ACTION
, me
, 0);
260 parse_args(argc
, argv
);
262 /* this is copied from the shell script, but it's never triggered... */
263 if (action
== REVERT
&& !no_replay
)
264 die("revert is incompatible with replay");
268 * We do not intend to commit immediately. We just want to
269 * merge the differences in, so let's compute the tree
270 * that represents the "current" state for merge-recursive
273 if (write_tree(head
, 0, NULL
))
274 die ("Your index file is unmerged.");
278 if (get_sha1("HEAD", head
))
279 die ("You do not have a valid HEAD");
280 wt_status_prepare(&s
);
282 die ("Dirty index: cannot %s", me
);
286 if (!commit
->parents
)
287 die ("Cannot %s a root commit", me
);
288 if (commit
->parents
->next
) {
289 /* Reverting or cherry-picking a merge commit */
291 struct commit_list
*p
;
294 die("Commit %s is a merge but no -m option was given.",
295 sha1_to_hex(commit
->object
.sha1
));
297 for (cnt
= 1, p
= commit
->parents
;
298 cnt
!= mainline
&& p
;
301 if (cnt
!= mainline
|| !p
)
302 die("Commit %s does not have parent %d",
303 sha1_to_hex(commit
->object
.sha1
), mainline
);
305 } else if (0 < mainline
)
306 die("Mainline was specified but commit %s is not a merge.",
307 sha1_to_hex(commit
->object
.sha1
));
309 parent
= commit
->parents
->item
;
311 if (!(message
= commit
->buffer
))
312 die ("Cannot get commit message for %s",
313 sha1_to_hex(commit
->object
.sha1
));
316 * "commit" is an existing commit. We would want to apply
317 * the difference it introduces since its first parent "prev"
318 * on top of the current HEAD if we are cherry-pick. Or the
319 * reverse of it if we are revert.
322 msg_fd
= hold_lock_file_for_update(&msg_file
, defmsg
, 1);
324 encoding
= get_encoding(message
);
327 if (!git_commit_encoding
)
328 git_commit_encoding
= "utf-8";
329 if ((reencoded_message
= reencode_string(message
,
330 git_commit_encoding
, encoding
)))
331 message
= reencoded_message
;
333 oneline
= get_oneline(message
);
335 if (action
== REVERT
) {
336 char *oneline_body
= strchr(oneline
, ' ');
340 add_to_msg("Revert \"");
341 add_to_msg(oneline_body
+ 1);
342 add_to_msg("\"\n\nThis reverts commit ");
343 add_to_msg(sha1_to_hex(commit
->object
.sha1
));
348 set_author_ident_env(message
);
349 add_message_to_msg(message
);
351 add_to_msg("(cherry picked from commit ");
352 add_to_msg(sha1_to_hex(commit
->object
.sha1
));
357 if (merge_recursive(sha1_to_hex(base
->object
.sha1
),
358 sha1_to_hex(head
), "HEAD",
359 sha1_to_hex(next
->object
.sha1
), oneline
) ||
360 write_tree(head
, 0, NULL
)) {
361 add_to_msg("\nConflicts:\n\n");
363 for (i
= 0; i
< active_nr
;) {
364 struct cache_entry
*ce
= active_cache
[i
++];
367 add_to_msg(ce
->name
);
369 while (i
< active_nr
&& !strcmp(ce
->name
,
370 active_cache
[i
]->name
))
374 if (close(msg_fd
) || commit_lock_file(&msg_file
) < 0)
375 die ("Error wrapping up %s", defmsg
);
376 fprintf(stderr
, "Automatic %s failed.%s\n",
377 me
, help_msg(commit
->object
.sha1
));
380 if (close(msg_fd
) || commit_lock_file(&msg_file
) < 0)
381 die ("Error wrapping up %s", defmsg
);
382 fprintf(stderr
, "Finished one %s.\n", me
);
386 * If we are cherry-pick, and if the merge did not result in
387 * hand-editing, we will hit this commit and inherit the original
388 * author date and name.
389 * If we are revert, or if our cherry-pick results in a hand merge,
390 * we had better say that the current user is responsible for that.
395 return execl_git_cmd("commit", "-n", NULL
);
397 return execl_git_cmd("commit", "-n", "-F", defmsg
, NULL
);
399 if (reencoded_message
)
400 free(reencoded_message
);
405 int cmd_revert(int argc
, const char **argv
, const char *prefix
)
411 return revert_or_cherry_pick(argc
, argv
);
414 int cmd_cherry_pick(int argc
, const char **argv
, const char *prefix
)
417 action
= CHERRY_PICK
;
418 return revert_or_cherry_pick(argc
, argv
);