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 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
- 1; i
++) {
50 if (!strcmp(arg
, "-n") || !strcmp(arg
, "--no-commit"))
52 else if (!strcmp(arg
, "-e") || !strcmp(arg
, "--edit"))
54 else if (!strcmp(arg
, "--no-edit"))
56 else if (!strcmp(arg
, "-x") || !strcmp(arg
, "--i-really-want-"
57 "to-expose-my-private-commit-object-name"))
59 else if (strcmp(arg
, "-r"))
64 if (get_sha1(arg
, sha1
))
65 die ("Cannot find '%s'", arg
);
66 commit
= (struct commit
*)parse_object(sha1
);
68 die ("Could not find %s", sha1_to_hex(sha1
));
69 if (commit
->object
.type
== OBJ_TAG
) {
70 commit
= (struct commit
*)
71 deref_tag((struct object
*)commit
, arg
, strlen(arg
));
74 if (commit
->object
.type
!= OBJ_COMMIT
)
75 die ("'%s' does not point to a commit", arg
);
78 static char *get_oneline(const char *message
)
81 const char *p
= message
, *abbrev
, *eol
;
82 int abbrev_len
, oneline_len
;
85 die ("Could not read commit message of %s",
86 sha1_to_hex(commit
->object
.sha1
));
87 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
92 for (eol
= p
+ 1; *eol
&& *eol
!= '\n'; eol
++)
96 abbrev
= find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
);
97 abbrev_len
= strlen(abbrev
);
98 oneline_len
= eol
- p
;
99 result
= xmalloc(abbrev_len
+ 5 + oneline_len
);
100 memcpy(result
, abbrev
, abbrev_len
);
101 memcpy(result
+ abbrev_len
, "... ", 4);
102 memcpy(result
+ abbrev_len
+ 4, p
, oneline_len
);
103 result
[abbrev_len
+ 4 + oneline_len
] = '\0';
107 char *get_encoding(const char *message
)
109 const char *p
= message
, *eol
;
112 die ("Could not read commit message of %s",
113 sha1_to_hex(commit
->object
.sha1
));
114 while (*p
&& *p
!= '\n') {
115 for (eol
= p
+ 1; *eol
&& *eol
!= '\n'; eol
++)
117 if (!prefixcmp(p
, "encoding ")) {
118 char *result
= xmalloc(eol
- 8 - p
);
119 strlcpy(result
, p
+ 9, eol
- 8 - p
);
129 struct lock_file msg_file
;
132 static void add_to_msg(const char *string
)
134 int len
= strlen(string
);
135 if (write_in_full(msg_fd
, string
, len
) < 0)
136 die ("Could not write to .msg");
139 static void add_message_to_msg(const char *message
)
141 const char *p
= message
;
142 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
146 add_to_msg(sha1_to_hex(commit
->object
.sha1
));
153 static void set_author_ident_env(const char *message
)
155 const char *p
= message
;
157 die ("Could not read commit message of %s",
158 sha1_to_hex(commit
->object
.sha1
));
159 while (*p
&& *p
!= '\n') {
162 for (eol
= p
; *eol
&& *eol
!= '\n'; eol
++)
164 if (!prefixcmp(p
, "author ")) {
165 char *line
, *pend
, *email
, *timestamp
;
168 line
= xmalloc(eol
+ 1 - p
);
169 memcpy(line
, p
, eol
- p
);
170 line
[eol
- p
] = '\0';
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 email 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 int merge_recursive(const char *base_sha1
,
206 const char *head_sha1
, const char *head_name
,
207 const char *next_sha1
, const char *next_name
)
211 sprintf(buffer
, "GITHEAD_%s", head_sha1
);
212 setenv(buffer
, head_name
, 1);
213 sprintf(buffer
, "GITHEAD_%s", next_sha1
);
214 setenv(buffer
, next_name
, 1);
217 * This three way merge is an interesting one. We are at
218 * $head, and would want to apply the change between $commit
219 * and $prev on top of us (when reverting), or the change between
220 * $prev and $commit on top of us (when cherry-picking or replaying).
223 return run_command_opt(RUN_COMMAND_NO_STDIN
| RUN_GIT_CMD
,
224 "merge-recursive", base_sha1
, "--",
225 head_sha1
, next_sha1
, NULL
);
228 static int revert_or_cherry_pick(int argc
, const char **argv
)
230 unsigned char head
[20];
231 struct commit
*base
, *next
;
233 char *oneline
, *encoding
, *reencoded_message
= NULL
;
236 git_config(git_default_config
);
237 me
= action
== REVERT
? "revert" : "cherry-pick";
238 setenv(GIT_REFLOG_ACTION
, me
, 0);
239 parse_options(argc
, argv
);
241 /* this is copied from the shell script, but it's never triggered... */
242 if (action
== REVERT
&& replay
)
243 die("revert is incompatible with replay");
247 * We do not intend to commit immediately. We just want to
248 * merge the differences in.
250 if (write_tree(head
, 0, NULL
))
251 die ("Your index file is unmerged.");
255 if (get_sha1("HEAD", head
))
256 die ("You do not have a valid HEAD");
257 wt_status_prepare(&s
);
258 if (s
.commitable
|| s
.workdir_dirty
)
259 die ("Dirty index: cannot %s", me
);
263 if (!commit
->parents
)
264 die ("Cannot %s a root commit", me
);
265 if (commit
->parents
->next
)
266 die ("Cannot %s a multi-parent commit.", me
);
267 if (!(message
= commit
->buffer
))
268 die ("Cannot get commit message for %s",
269 sha1_to_hex(commit
->object
.sha1
));
272 * "commit" is an existing commit. We would want to apply
273 * the difference it introduces since its first parent "prev"
274 * on top of the current HEAD if we are cherry-pick. Or the
275 * reverse of it if we are revert.
278 msg_fd
= hold_lock_file_for_update(&msg_file
, ".msg", 1);
280 encoding
= get_encoding(message
);
283 if (!git_commit_encoding
)
284 git_commit_encoding
= "utf-8";
285 if ((reencoded_message
= reencode_string(message
,
286 git_commit_encoding
, encoding
)))
287 message
= reencoded_message
;
289 oneline
= get_oneline(message
);
291 if (action
== REVERT
) {
293 next
= commit
->parents
->item
;
294 add_to_msg("Revert ");
295 add_to_msg(find_unique_abbrev(commit
->object
.sha1
,
298 add_to_msg("\nThis reverts commit ");
299 add_to_msg(sha1_to_hex(commit
->object
.sha1
));
302 base
= commit
->parents
->item
;
304 set_author_ident_env(message
);
305 add_message_to_msg(message
);
307 add_to_msg("(cherry picked from commit ");
308 add_to_msg(sha1_to_hex(commit
->object
.sha1
));
313 add_to_msg("(original 'git ");
315 add_to_msg("' arguments: ");
316 for (i
= 0; i
< argc
; i
++) {
324 if (merge_recursive(sha1_to_hex(base
->object
.sha1
),
325 sha1_to_hex(head
), "HEAD",
326 sha1_to_hex(next
->object
.sha1
), oneline
) ||
327 write_tree(head
, 0, NULL
)) {
328 const char *target
= git_path("MERGE_MSG");
329 add_to_msg("\nConflicts:\n\n");
331 for (i
= 0; i
< active_nr
;) {
332 struct cache_entry
*ce
= active_cache
[i
++];
335 add_to_msg(ce
->name
);
337 while (i
< active_nr
&& !strcmp(ce
->name
,
338 active_cache
[i
]->name
))
342 if (close(msg_fd
) || commit_lock_file(&msg_file
) < 0)
343 die ("Error wrapping up .msg");
345 if (rename(".msg", target
))
346 die ("Could not move .msg to %s", target
);
347 fprintf(stderr
, "Automatic %s failed. "
348 "After resolving the conflicts,\n"
349 "mark the corrected paths with 'git-add <paths>'\n"
350 "and commit the result.\n", me
);
351 if (action
== CHERRY_PICK
) {
352 fprintf(stderr
, "When commiting, use the option "
353 "'-c %s' to retain authorship and message.\n",
354 find_unique_abbrev(commit
->object
.sha1
,
359 if (close(msg_fd
) || commit_lock_file(&msg_file
) < 0)
360 die ("Error wrapping up .msg");
361 fprintf(stderr
, "Finished one %s.\n", me
);
365 * If we are cherry-pick, and if the merge did not result in
366 * hand-editing, we will hit this commit and inherit the original
367 * author date and name.
368 * If we are revert, or if our cherry-pick results in a hand merge,
369 * we had better say that the current user is responsible for that.
374 return execl_git_cmd("commit", "-n", "-F", ".msg",
377 return execl_git_cmd("commit", "-n", "-F", ".msg",
380 if (reencoded_message
)
381 free(reencoded_message
);
386 int cmd_revert(int argc
, const char **argv
, const char *prefix
)
391 return revert_or_cherry_pick(argc
, argv
);
394 int cmd_cherry_pick(int argc
, const char **argv
, const char *prefix
)
397 action
= CHERRY_PICK
;
398 return revert_or_cherry_pick(argc
, argv
);