7 #include "parse-options.h"
14 * This implements the builtins revert and cherry-pick.
16 * Copyright (c) 2007 Johannes E. Schindelin
18 * Based on git-revert.sh, which is
20 * Copyright (c) 2005 Linus Torvalds
21 * Copyright (c) 2005 Junio C Hamano
24 static const char * const revert_usage
[] = {
25 "git revert [options] <commit-ish>",
29 static const char * const cherry_pick_usage
[] = {
30 "git cherry-pick [options] <commit-ish>",
34 static int edit
, no_commit
, mainline
, signoff
;
36 static struct commit
*commit
;
38 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
40 static void parse_args(int argc
, const char **argv
)
42 const char * const * usage_str
=
43 flags
& PICK_REVERSE
? revert_usage
: cherry_pick_usage
;
44 unsigned char sha1
[20];
47 struct option options
[] = {
48 OPT_BOOLEAN('n', "no-commit", &no_commit
, "don't automatically commit"),
49 OPT_BOOLEAN('e', "edit", &edit
, "edit the commit message"),
50 OPT_BIT('x', NULL
, &flags
, "append commit name when cherry-picking", PICK_ADD_NOTE
),
51 OPT_BOOLEAN('r', NULL
, &noop
, "no-op (backward compatibility)"),
52 OPT_BOOLEAN('s', "signoff", &signoff
, "add Signed-off-by:"),
53 OPT_INTEGER('m', "mainline", &mainline
, "parent number"),
57 if (parse_options(argc
, argv
, NULL
, 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_encoding(const char *message
)
76 const char *p
= message
, *eol
;
80 while (*p
&& *p
!= '\n') {
81 for (eol
= p
+ 1; *eol
&& *eol
!= '\n'; eol
++)
83 if (!prefixcmp(p
, "encoding ")) {
84 char *result
= xmalloc(eol
- 8 - p
);
85 strlcpy(result
, p
+ 9, eol
- 8 - p
);
95 static void set_author_ident_env(const char *message
)
97 const char *p
= message
;
99 die ("Could not read commit message of %s",
100 sha1_to_hex(commit
->object
.sha1
));
101 while (*p
&& *p
!= '\n') {
104 for (eol
= p
; *eol
&& *eol
!= '\n'; eol
++)
106 if (!prefixcmp(p
, "author ")) {
107 char *line
, *pend
, *email
, *timestamp
;
110 line
= xmemdupz(p
, eol
- p
);
111 email
= strchr(line
, '<');
113 die ("Could not extract author email from %s",
114 sha1_to_hex(commit
->object
.sha1
));
118 for (pend
= email
; pend
!= line
+ 1 &&
119 isspace(pend
[-1]); pend
--);
123 timestamp
= strchr(email
, '>');
125 die ("Could not extract author time from %s",
126 sha1_to_hex(commit
->object
.sha1
));
128 for (timestamp
++; *timestamp
&& isspace(*timestamp
);
131 setenv("GIT_AUTHOR_NAME", line
, 1);
132 setenv("GIT_AUTHOR_EMAIL", email
, 1);
133 setenv("GIT_AUTHOR_DATE", timestamp
, 1);
141 die ("No author information found in %s",
142 sha1_to_hex(commit
->object
.sha1
));
145 static char *help_msg(const unsigned char *sha1
)
147 static char helpbuf
[1024];
148 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
153 strcpy(helpbuf
, " After resolving the conflicts,\n"
154 "mark the corrected paths with 'git add <paths>' "
155 "or 'git rm <paths>' and commit the result.");
157 if (!(flags
& PICK_REVERSE
)) {
158 sprintf(helpbuf
+ strlen(helpbuf
),
159 "\nWhen commiting, use the option "
160 "'-c %s' to retain authorship and message.",
161 find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
166 static void write_message(struct strbuf
*msgbuf
, const char *filename
)
168 struct lock_file msg_file
;
170 msg_fd
= hold_lock_file_for_update(&msg_file
, filename
,
172 if (write_in_full(msg_fd
, msgbuf
->buf
, msgbuf
->len
) < 0)
173 die_errno("Could not write to %s.", filename
);
174 strbuf_release(msgbuf
);
175 if (commit_lock_file(&msg_file
) < 0)
176 die("Error wrapping up %s", filename
);
179 static int revert_or_cherry_pick(int argc
, const char **argv
)
182 struct strbuf msgbuf
;
183 char *reencoded_message
= NULL
;
184 const char *encoding
;
187 git_config(git_default_config
, NULL
);
188 me
= flags
& PICK_REVERSE
? "revert" : "cherry-pick";
189 setenv(GIT_REFLOG_ACTION
, me
, 0);
190 parse_args(argc
, argv
);
192 if (read_cache() < 0)
193 die("git %s: failed to read the index", me
);
194 if (!no_commit
&& index_differs_from("HEAD", 0))
195 die ("Dirty index: cannot %s", me
);
198 return error("Cannot get commit message for %s",
199 sha1_to_hex(commit
->object
.sha1
));
200 encoding
= get_encoding(commit
->buffer
);
203 if (!git_commit_encoding
)
204 git_commit_encoding
= "UTF-8";
205 if ((reencoded_message
= reencode_string(commit
->buffer
,
206 git_commit_encoding
, encoding
)))
207 commit
->buffer
= reencoded_message
;
209 failed
= pick_commit(commit
, mainline
, flags
, &msgbuf
);
212 } else if (failed
> 0) {
213 fprintf(stderr
, "Automatic %s failed.%s\n",
214 me
, help_msg(commit
->object
.sha1
));
215 write_message(&msgbuf
, git_path("MERGE_MSG"));
219 if (!(flags
& PICK_REVERSE
))
220 set_author_ident_env(commit
->buffer
);
221 free(reencoded_message
);
223 fprintf(stderr
, "Finished one %s.\n", me
);
225 write_message(&msgbuf
, git_path("MERGE_MSG"));
228 * If we are cherry-pick, and if the merge did not result in
229 * hand-editing, we will hit this commit and inherit the original
230 * author date and name.
231 * If we are revert, or if our cherry-pick results in a hand merge,
232 * we had better say that the current user is responsible for that.
236 /* 6 is max possible length of our args array including NULL */
239 args
[i
++] = "commit";
245 args
[i
++] = git_path("MERGE_MSG");
248 return execv_git_cmd(args
);
253 int cmd_revert(int argc
, const char **argv
, const char *prefix
)
257 flags
= PICK_REVERSE
| PICK_ADD_NOTE
;
258 return revert_or_cherry_pick(argc
, argv
);
261 int cmd_cherry_pick(int argc
, const char **argv
, const char *prefix
)
264 return revert_or_cherry_pick(argc
, argv
);