7 #include "run-command.h"
12 * This implements the builtins revert and cherry-pick.
14 * Copyright (c) 2007 Johannes E. Schindelin
16 * Based on git-revert.sh, which is
18 * Copyright (c) 2005 Linus Torvalds
19 * Copyright (c) 2005 Junio C Hamano
22 static const char *revert_usage
= "git-revert [--edit | --no-edit] [-n] <commit-ish>";
24 static const char *cherry_pick_usage
= "git-cherry-pick [--edit] [-n] [-r] [-x] <commit-ish>";
28 static enum { REVERT
, CHERRY_PICK
} action
;
30 static struct commit
*commit
;
31 static int needed_deref
;
33 static const char *me
;
35 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
37 static void parse_options(int argc
, const char **argv
)
39 const char *usage_str
= action
== REVERT
?
40 revert_usage
: cherry_pick_usage
;
41 unsigned char sha1
[20];
48 for (i
= 1; i
< argc
; i
++) {
52 if (!strcmp(arg
, "-n") || !strcmp(arg
, "--no-commit"))
54 else if (!strcmp(arg
, "-e") || !strcmp(arg
, "--edit"))
56 else if (!strcmp(arg
, "--no-edit"))
58 else if (!strcmp(arg
, "-x") || !strcmp(arg
, "--i-really-want-"
59 "to-expose-my-private-commit-object-name"))
61 else if (strcmp(arg
, "-r"))
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
));
77 if (commit
->object
.type
!= OBJ_COMMIT
)
78 die ("'%s' does not point to a commit", arg
);
81 static char *get_oneline(const char *message
)
84 const char *p
= message
, *abbrev
, *eol
;
85 int abbrev_len
, oneline_len
;
88 die ("Could not read commit message of %s",
89 sha1_to_hex(commit
->object
.sha1
));
90 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
95 for (eol
= p
+ 1; *eol
&& *eol
!= '\n'; eol
++)
99 abbrev
= find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
);
100 abbrev_len
= strlen(abbrev
);
101 oneline_len
= eol
- p
;
102 result
= xmalloc(abbrev_len
+ 5 + oneline_len
);
103 memcpy(result
, abbrev
, abbrev_len
);
104 memcpy(result
+ abbrev_len
, "... ", 4);
105 memcpy(result
+ abbrev_len
+ 4, p
, oneline_len
);
106 result
[abbrev_len
+ 4 + oneline_len
] = '\0';
110 static char *get_encoding(const char *message
)
112 const char *p
= message
, *eol
;
115 die ("Could not read commit message of %s",
116 sha1_to_hex(commit
->object
.sha1
));
117 while (*p
&& *p
!= '\n') {
118 for (eol
= p
+ 1; *eol
&& *eol
!= '\n'; eol
++)
120 if (!prefixcmp(p
, "encoding ")) {
121 char *result
= xmalloc(eol
- 8 - p
);
122 strlcpy(result
, p
+ 9, eol
- 8 - p
);
132 static struct lock_file msg_file
;
135 static void add_to_msg(const char *string
)
137 int len
= strlen(string
);
138 if (write_in_full(msg_fd
, string
, len
) < 0)
139 die ("Could not write to MERGE_MSG");
142 static void add_message_to_msg(const char *message
)
144 const char *p
= message
;
145 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
149 add_to_msg(sha1_to_hex(commit
->object
.sha1
));
156 static void set_author_ident_env(const char *message
)
158 const char *p
= message
;
160 die ("Could not read commit message of %s",
161 sha1_to_hex(commit
->object
.sha1
));
162 while (*p
&& *p
!= '\n') {
165 for (eol
= p
; *eol
&& *eol
!= '\n'; eol
++)
167 if (!prefixcmp(p
, "author ")) {
168 char *line
, *pend
, *email
, *timestamp
;
171 line
= xmalloc(eol
+ 1 - p
);
172 memcpy(line
, p
, eol
- p
);
173 line
[eol
- p
] = '\0';
174 email
= strchr(line
, '<');
176 die ("Could not extract author email from %s",
177 sha1_to_hex(commit
->object
.sha1
));
181 for (pend
= email
; pend
!= line
+ 1 &&
182 isspace(pend
[-1]); pend
--);
186 timestamp
= strchr(email
, '>');
188 die ("Could not extract author email from %s",
189 sha1_to_hex(commit
->object
.sha1
));
191 for (timestamp
++; *timestamp
&& isspace(*timestamp
);
194 setenv("GIT_AUTHOR_NAME", line
, 1);
195 setenv("GIT_AUTHOR_EMAIL", email
, 1);
196 setenv("GIT_AUTHOR_DATE", timestamp
, 1);
204 die ("No author information found in %s",
205 sha1_to_hex(commit
->object
.sha1
));
208 static int merge_recursive(const char *base_sha1
,
209 const char *head_sha1
, const char *head_name
,
210 const char *next_sha1
, const char *next_name
)
215 sprintf(buffer
, "GITHEAD_%s", head_sha1
);
216 setenv(buffer
, head_name
, 1);
217 sprintf(buffer
, "GITHEAD_%s", next_sha1
);
218 setenv(buffer
, next_name
, 1);
221 * This three way merge is an interesting one. We are at
222 * $head, and would want to apply the change between $commit
223 * and $prev on top of us (when reverting), or the change between
224 * $prev and $commit on top of us (when cherry-picking or replaying).
226 argv
[0] = "merge-recursive";
233 return run_command_v_opt(argv
, RUN_COMMAND_NO_STDIN
| RUN_GIT_CMD
);
236 static int revert_or_cherry_pick(int argc
, const char **argv
)
238 unsigned char head
[20];
239 struct commit
*base
, *next
;
241 char *oneline
, *reencoded_message
= NULL
;
242 const char *message
, *encoding
;
243 const char *defmsg
= xstrdup(git_path("MERGE_MSG"));
245 git_config(git_default_config
);
246 me
= action
== REVERT
? "revert" : "cherry-pick";
247 setenv(GIT_REFLOG_ACTION
, me
, 0);
248 parse_options(argc
, argv
);
250 /* this is copied from the shell script, but it's never triggered... */
251 if (action
== REVERT
&& replay
)
252 die("revert is incompatible with replay");
256 * We do not intend to commit immediately. We just want to
257 * merge the differences in.
259 if (write_tree(head
, 0, NULL
))
260 die ("Your index file is unmerged.");
264 if (get_sha1("HEAD", head
))
265 die ("You do not have a valid HEAD");
266 wt_status_prepare(&s
);
267 if (s
.commitable
|| s
.workdir_dirty
)
268 die ("Dirty index: cannot %s", me
);
272 if (!commit
->parents
)
273 die ("Cannot %s a root commit", me
);
274 if (commit
->parents
->next
)
275 die ("Cannot %s a multi-parent commit.", me
);
276 if (!(message
= commit
->buffer
))
277 die ("Cannot get commit message for %s",
278 sha1_to_hex(commit
->object
.sha1
));
281 * "commit" is an existing commit. We would want to apply
282 * the difference it introduces since its first parent "prev"
283 * on top of the current HEAD if we are cherry-pick. Or the
284 * reverse of it if we are revert.
287 msg_fd
= hold_lock_file_for_update(&msg_file
, defmsg
, 1);
289 encoding
= get_encoding(message
);
292 if (!git_commit_encoding
)
293 git_commit_encoding
= "utf-8";
294 if ((reencoded_message
= reencode_string(message
,
295 git_commit_encoding
, encoding
)))
296 message
= reencoded_message
;
298 oneline
= get_oneline(message
);
300 if (action
== REVERT
) {
301 char *oneline_body
= strchr(oneline
, ' ');
304 next
= commit
->parents
->item
;
305 add_to_msg("Revert \"");
306 add_to_msg(oneline_body
+ 1);
307 add_to_msg("\"\n\nThis reverts commit ");
308 add_to_msg(sha1_to_hex(commit
->object
.sha1
));
311 base
= commit
->parents
->item
;
313 set_author_ident_env(message
);
314 add_message_to_msg(message
);
316 add_to_msg("(cherry picked from commit ");
317 add_to_msg(sha1_to_hex(commit
->object
.sha1
));
322 add_to_msg("(original 'git ");
324 add_to_msg("' arguments: ");
325 for (i
= 0; i
< argc
; i
++) {
333 if (merge_recursive(sha1_to_hex(base
->object
.sha1
),
334 sha1_to_hex(head
), "HEAD",
335 sha1_to_hex(next
->object
.sha1
), oneline
) ||
336 write_tree(head
, 0, NULL
)) {
337 add_to_msg("\nConflicts:\n\n");
339 for (i
= 0; i
< active_nr
;) {
340 struct cache_entry
*ce
= active_cache
[i
++];
343 add_to_msg(ce
->name
);
345 while (i
< active_nr
&& !strcmp(ce
->name
,
346 active_cache
[i
]->name
))
350 if (close(msg_fd
) || commit_lock_file(&msg_file
) < 0)
351 die ("Error wrapping up %s", defmsg
);
352 fprintf(stderr
, "Automatic %s failed. "
353 "After resolving the conflicts,\n"
354 "mark the corrected paths with 'git-add <paths>'\n"
355 "and commit the result.\n", me
);
356 if (action
== CHERRY_PICK
) {
357 fprintf(stderr
, "When commiting, use the option "
358 "'-c %s' to retain authorship and message.\n",
359 find_unique_abbrev(commit
->object
.sha1
,
364 if (close(msg_fd
) || commit_lock_file(&msg_file
) < 0)
365 die ("Error wrapping up %s", defmsg
);
366 fprintf(stderr
, "Finished one %s.\n", me
);
370 * If we are cherry-pick, and if the merge did not result in
371 * hand-editing, we will hit this commit and inherit the original
372 * author date and name.
373 * If we are revert, or if our cherry-pick results in a hand merge,
374 * we had better say that the current user is responsible for that.
379 return execl_git_cmd("commit", "-n", NULL
);
381 return execl_git_cmd("commit", "-n", "-F", defmsg
, NULL
);
383 if (reencoded_message
)
384 free(reencoded_message
);
389 int cmd_revert(int argc
, const char **argv
, const char *prefix
)
394 return revert_or_cherry_pick(argc
, argv
);
397 int cmd_cherry_pick(int argc
, const char **argv
, const char *prefix
)
400 action
= CHERRY_PICK
;
401 return revert_or_cherry_pick(argc
, argv
);