blob: a0586f9753189d13d5af1c790ea875a5f383f34f [file] [log] [blame]
Johannes Schindelin9509af62007-03-01 05:26:30 +01001#include "cache.h"
2#include "builtin.h"
3#include "object.h"
4#include "commit.h"
5#include "tag.h"
6#include "wt-status.h"
7#include "run-command.h"
8#include "exec_cmd.h"
9#include "utf8.h"
Pierre Habouzitf8103792007-10-07 23:02:29 +020010#include "parse-options.h"
Johannes Schindelin9509af62007-03-01 05:26:30 +010011
12/*
13 * This implements the builtins revert and cherry-pick.
14 *
15 * Copyright (c) 2007 Johannes E. Schindelin
16 *
17 * Based on git-revert.sh, which is
18 *
19 * Copyright (c) 2005 Linus Torvalds
20 * Copyright (c) 2005 Junio C Hamano
21 */
22
Pierre Habouzitf8103792007-10-07 23:02:29 +020023static const char * const revert_usage[] = {
24 "git-revert [options] <commit-ish>",
25 NULL
26};
Johannes Schindelin9509af62007-03-01 05:26:30 +010027
Pierre Habouzitf8103792007-10-07 23:02:29 +020028static const char * const cherry_pick_usage[] = {
29 "git-cherry-pick [options] <commit-ish>",
30 NULL
31};
Johannes Schindelin9509af62007-03-01 05:26:30 +010032
Junio C Hamano757f58e2007-11-25 15:15:48 -080033static int edit, no_replay, no_commit, mainline;
Junio C Hamano4175e9e2007-06-13 01:42:05 -070034static enum { REVERT, CHERRY_PICK } action;
Johannes Schindelin9509af62007-03-01 05:26:30 +010035static struct commit *commit;
Johannes Schindelin9509af62007-03-01 05:26:30 +010036
37static const char *me;
38
39#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
40
Pierre Habouzitf8103792007-10-07 23:02:29 +020041static void parse_args(int argc, const char **argv)
Johannes Schindelin9509af62007-03-01 05:26:30 +010042{
Pierre Habouzitf8103792007-10-07 23:02:29 +020043 const char * const * usage_str =
44 action == REVERT ? revert_usage : cherry_pick_usage;
Johannes Schindelin9509af62007-03-01 05:26:30 +010045 unsigned char sha1[20];
46 const char *arg;
Pierre Habouzitf8103792007-10-07 23:02:29 +020047 int noop;
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)"),
Junio C Hamano02273fd2007-11-04 01:26:02 -070053 OPT_INTEGER('m', "mainline", &mainline, "parent number"),
Pierre Habouzitf8103792007-10-07 23:02:29 +020054 OPT_END(),
55 };
Johannes Schindelin9509af62007-03-01 05:26:30 +010056
Pierre Habouzitf8103792007-10-07 23:02:29 +020057 if (parse_options(argc, argv, options, usage_str, 0) != 1)
58 usage_with_options(usage_str, options);
59 arg = argv[0];
Johannes Schindelin9509af62007-03-01 05:26:30 +010060
Johannes Schindelin9509af62007-03-01 05:26:30 +010061 if (get_sha1(arg, sha1))
62 die ("Cannot find '%s'", arg);
63 commit = (struct commit *)parse_object(sha1);
64 if (!commit)
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));
Johannes Schindelin9509af62007-03-01 05:26:30 +010069 }
70 if (commit->object.type != OBJ_COMMIT)
71 die ("'%s' does not point to a commit", arg);
72}
73
74static char *get_oneline(const char *message)
75{
76 char *result;
77 const char *p = message, *abbrev, *eol;
78 int abbrev_len, oneline_len;
79
80 if (!p)
81 die ("Could not read commit message of %s",
82 sha1_to_hex(commit->object.sha1));
83 while (*p && (*p != '\n' || p[1] != '\n'))
84 p++;
85
86 if (*p) {
87 p += 2;
88 for (eol = p + 1; *eol && *eol != '\n'; eol++)
89 ; /* do nothing */
90 } else
91 eol = p;
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';
100 return result;
101}
102
Pierre Habouzit52fae7d2007-06-07 22:45:00 +0200103static char *get_encoding(const char *message)
Johannes Schindelin9509af62007-03-01 05:26:30 +0100104{
105 const char *p = message, *eol;
106
107 if (!p)
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++)
112 ; /* do nothing */
113 if (!prefixcmp(p, "encoding ")) {
114 char *result = xmalloc(eol - 8 - p);
115 strlcpy(result, p + 9, eol - 8 - p);
116 return result;
117 }
118 p = eol;
119 if (*p == '\n')
120 p++;
121 }
122 return NULL;
123}
124
Junio C Hamano4175e9e2007-06-13 01:42:05 -0700125static struct lock_file msg_file;
Johannes Schindelin9509af62007-03-01 05:26:30 +0100126static int msg_fd;
127
128static void add_to_msg(const char *string)
129{
130 int len = strlen(string);
131 if (write_in_full(msg_fd, string, len) < 0)
Shawn O. Pearce1dcb3b62007-05-10 18:10:36 -0400132 die ("Could not write to MERGE_MSG");
Johannes Schindelin9509af62007-03-01 05:26:30 +0100133}
134
135static void add_message_to_msg(const char *message)
136{
137 const char *p = message;
138 while (*p && (*p != '\n' || p[1] != '\n'))
139 p++;
140
141 if (!*p)
142 add_to_msg(sha1_to_hex(commit->object.sha1));
143
144 p += 2;
145 add_to_msg(p);
146 return;
147}
148
149static void set_author_ident_env(const char *message)
150{
151 const char *p = message;
152 if (!p)
153 die ("Could not read commit message of %s",
154 sha1_to_hex(commit->object.sha1));
155 while (*p && *p != '\n') {
156 const char *eol;
157
158 for (eol = p; *eol && *eol != '\n'; eol++)
159 ; /* do nothing */
160 if (!prefixcmp(p, "author ")) {
161 char *line, *pend, *email, *timestamp;
162
163 p += 7;
Pierre Habouzit182af832007-09-16 00:32:36 +0200164 line = xmemdupz(p, eol - p);
Johannes Schindelin9509af62007-03-01 05:26:30 +0100165 email = strchr(line, '<');
166 if (!email)
167 die ("Could not extract author email from %s",
168 sha1_to_hex(commit->object.sha1));
169 if (email == line)
170 pend = line;
171 else
172 for (pend = email; pend != line + 1 &&
173 isspace(pend[-1]); pend--);
174 ; /* do nothing */
175 *pend = '\0';
176 email++;
177 timestamp = strchr(email, '>');
178 if (!timestamp)
179 die ("Could not extract author email from %s",
180 sha1_to_hex(commit->object.sha1));
181 *timestamp = '\0';
182 for (timestamp++; *timestamp && isspace(*timestamp);
183 timestamp++)
184 ; /* do nothing */
185 setenv("GIT_AUTHOR_NAME", line, 1);
186 setenv("GIT_AUTHOR_EMAIL", email, 1);
187 setenv("GIT_AUTHOR_DATE", timestamp, 1);
188 free(line);
189 return;
190 }
191 p = eol;
192 if (*p == '\n')
193 p++;
194 }
195 die ("No author information found in %s",
196 sha1_to_hex(commit->object.sha1));
197}
198
199static 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)
202{
203 char buffer[256];
Shawn O. Pearce497bdc82007-03-10 03:27:28 -0500204 const char *argv[6];
Johannes Schindelin9509af62007-03-01 05:26:30 +0100205
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);
210
211 /*
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).
216 */
Shawn O. Pearce497bdc82007-03-10 03:27:28 -0500217 argv[0] = "merge-recursive";
218 argv[1] = base_sha1;
219 argv[2] = "--";
220 argv[3] = head_sha1;
221 argv[4] = next_sha1;
222 argv[5] = NULL;
Johannes Schindelin9509af62007-03-01 05:26:30 +0100223
Shawn O. Pearce497bdc82007-03-10 03:27:28 -0500224 return run_command_v_opt(argv, RUN_COMMAND_NO_STDIN | RUN_GIT_CMD);
Johannes Schindelin9509af62007-03-01 05:26:30 +0100225}
226
227static int revert_or_cherry_pick(int argc, const char **argv)
228{
229 unsigned char head[20];
Junio C Hamano7791ecb2007-10-23 13:33:26 -0700230 struct commit *base, *next, *parent;
Johannes Schindelin9509af62007-03-01 05:26:30 +0100231 int i;
Shawn O. Pearce1a8f2742007-03-12 15:33:18 -0400232 char *oneline, *reencoded_message = NULL;
233 const char *message, *encoding;
Shawn O. Pearceabda5222007-05-07 22:57:15 -0400234 const char *defmsg = xstrdup(git_path("MERGE_MSG"));
Johannes Schindelin9509af62007-03-01 05:26:30 +0100235
236 git_config(git_default_config);
237 me = action == REVERT ? "revert" : "cherry-pick";
238 setenv(GIT_REFLOG_ACTION, me, 0);
Pierre Habouzitf8103792007-10-07 23:02:29 +0200239 parse_args(argc, argv);
Johannes Schindelin9509af62007-03-01 05:26:30 +0100240
241 /* this is copied from the shell script, but it's never triggered... */
Pierre Habouzitf8103792007-10-07 23:02:29 +0200242 if (action == REVERT && !no_replay)
Johannes Schindelin9509af62007-03-01 05:26:30 +0100243 die("revert is incompatible with replay");
244
245 if (no_commit) {
246 /*
247 * We do not intend to commit immediately. We just want to
Junio C Hamano71aa2b82007-11-13 13:45:11 -0800248 * merge the differences in, so let's compute the tree
249 * that represents the "current" state for merge-recursive
250 * to work on.
Johannes Schindelin9509af62007-03-01 05:26:30 +0100251 */
252 if (write_tree(head, 0, NULL))
253 die ("Your index file is unmerged.");
254 } else {
255 struct wt_status s;
256
257 if (get_sha1("HEAD", head))
258 die ("You do not have a valid HEAD");
259 wt_status_prepare(&s);
Junio C Hamano245de362007-11-13 12:28:53 -0800260 if (s.commitable)
Johannes Schindelin9509af62007-03-01 05:26:30 +0100261 die ("Dirty index: cannot %s", me);
262 discard_cache();
263 }
264
265 if (!commit->parents)
266 die ("Cannot %s a root commit", me);
Junio C Hamano7791ecb2007-10-23 13:33:26 -0700267 if (commit->parents->next) {
268 /* Reverting or cherry-picking a merge commit */
269 int cnt;
270 struct commit_list *p;
271
272 if (!mainline)
273 die("Commit %s is a merge but no -m option was given.",
274 sha1_to_hex(commit->object.sha1));
275
276 for (cnt = 1, p = commit->parents;
277 cnt != mainline && p;
278 cnt++)
279 p = p->next;
280 if (cnt != mainline || !p)
281 die("Commit %s does not have parent %d",
282 sha1_to_hex(commit->object.sha1), mainline);
283 parent = p->item;
284 } else if (0 < mainline)
285 die("Mainline was specified but commit %s is not a merge.",
286 sha1_to_hex(commit->object.sha1));
287 else
288 parent = commit->parents->item;
289
Johannes Schindelin9509af62007-03-01 05:26:30 +0100290 if (!(message = commit->buffer))
291 die ("Cannot get commit message for %s",
292 sha1_to_hex(commit->object.sha1));
293
294 /*
295 * "commit" is an existing commit. We would want to apply
296 * the difference it introduces since its first parent "prev"
297 * on top of the current HEAD if we are cherry-pick. Or the
298 * reverse of it if we are revert.
299 */
300
Shawn O. Pearceabda5222007-05-07 22:57:15 -0400301 msg_fd = hold_lock_file_for_update(&msg_file, defmsg, 1);
Johannes Schindelin9509af62007-03-01 05:26:30 +0100302
303 encoding = get_encoding(message);
304 if (!encoding)
305 encoding = "utf-8";
306 if (!git_commit_encoding)
307 git_commit_encoding = "utf-8";
308 if ((reencoded_message = reencode_string(message,
309 git_commit_encoding, encoding)))
310 message = reencoded_message;
311
312 oneline = get_oneline(message);
313
314 if (action == REVERT) {
Johannes Schindeline43b0102007-03-23 17:06:11 +0100315 char *oneline_body = strchr(oneline, ' ');
316
Johannes Schindelin9509af62007-03-01 05:26:30 +0100317 base = commit;
Junio C Hamano7791ecb2007-10-23 13:33:26 -0700318 next = parent;
Johannes Schindeline43b0102007-03-23 17:06:11 +0100319 add_to_msg("Revert \"");
320 add_to_msg(oneline_body + 1);
321 add_to_msg("\"\n\nThis reverts commit ");
Johannes Schindelin9509af62007-03-01 05:26:30 +0100322 add_to_msg(sha1_to_hex(commit->object.sha1));
323 add_to_msg(".\n");
324 } else {
Junio C Hamano7791ecb2007-10-23 13:33:26 -0700325 base = parent;
Johannes Schindelin9509af62007-03-01 05:26:30 +0100326 next = commit;
327 set_author_ident_env(message);
328 add_message_to_msg(message);
Pierre Habouzitf8103792007-10-07 23:02:29 +0200329 if (no_replay) {
Shawn O. Pearce0e624042007-03-06 00:46:00 -0500330 add_to_msg("(cherry picked from commit ");
Johannes Schindelin9509af62007-03-01 05:26:30 +0100331 add_to_msg(sha1_to_hex(commit->object.sha1));
332 add_to_msg(")\n");
333 }
334 }
Johannes Schindelin9509af62007-03-01 05:26:30 +0100335
336 if (merge_recursive(sha1_to_hex(base->object.sha1),
337 sha1_to_hex(head), "HEAD",
Johannes Schindelinf52463a2007-03-04 14:26:05 +0100338 sha1_to_hex(next->object.sha1), oneline) ||
339 write_tree(head, 0, NULL)) {
Johannes Schindelin9509af62007-03-01 05:26:30 +0100340 add_to_msg("\nConflicts:\n\n");
341 read_cache();
342 for (i = 0; i < active_nr;) {
343 struct cache_entry *ce = active_cache[i++];
344 if (ce_stage(ce)) {
345 add_to_msg("\t");
346 add_to_msg(ce->name);
347 add_to_msg("\n");
348 while (i < active_nr && !strcmp(ce->name,
349 active_cache[i]->name))
350 i++;
351 }
352 }
353 if (close(msg_fd) || commit_lock_file(&msg_file) < 0)
Shawn O. Pearceabda5222007-05-07 22:57:15 -0400354 die ("Error wrapping up %s", defmsg);
Johannes Schindelin9509af62007-03-01 05:26:30 +0100355 fprintf(stderr, "Automatic %s failed. "
356 "After resolving the conflicts,\n"
Nicolas Pitre04bd8e52007-10-30 15:59:24 -0400357 "mark the corrected paths with 'git add <paths>' "
Johannes Schindelin9509af62007-03-01 05:26:30 +0100358 "and commit the result.\n", me);
359 if (action == CHERRY_PICK) {
Johannes Schindelinf52463a2007-03-04 14:26:05 +0100360 fprintf(stderr, "When commiting, use the option "
361 "'-c %s' to retain authorship and message.\n",
362 find_unique_abbrev(commit->object.sha1,
363 DEFAULT_ABBREV));
Johannes Schindelin9509af62007-03-01 05:26:30 +0100364 }
365 exit(1);
366 }
367 if (close(msg_fd) || commit_lock_file(&msg_file) < 0)
Shawn O. Pearceabda5222007-05-07 22:57:15 -0400368 die ("Error wrapping up %s", defmsg);
Johannes Schindelin9509af62007-03-01 05:26:30 +0100369 fprintf(stderr, "Finished one %s.\n", me);
370
371 /*
372 *
373 * If we are cherry-pick, and if the merge did not result in
374 * hand-editing, we will hit this commit and inherit the original
375 * author date and name.
376 * If we are revert, or if our cherry-pick results in a hand merge,
377 * we had better say that the current user is responsible for that.
378 */
379
380 if (!no_commit) {
381 if (edit)
Shawn O. Pearceabda5222007-05-07 22:57:15 -0400382 return execl_git_cmd("commit", "-n", NULL);
Johannes Schindelin9509af62007-03-01 05:26:30 +0100383 else
Shawn O. Pearceabda5222007-05-07 22:57:15 -0400384 return execl_git_cmd("commit", "-n", "-F", defmsg, NULL);
Johannes Schindelin9509af62007-03-01 05:26:30 +0100385 }
386 if (reencoded_message)
387 free(reencoded_message);
388
389 return 0;
390}
391
392int cmd_revert(int argc, const char **argv, const char *prefix)
393{
394 if (isatty(0))
395 edit = 1;
Pierre Habouzitf8103792007-10-07 23:02:29 +0200396 no_replay = 1;
Johannes Schindelin9509af62007-03-01 05:26:30 +0100397 action = REVERT;
398 return revert_or_cherry_pick(argc, argv);
399}
400
401int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
402{
Pierre Habouzitf8103792007-10-07 23:02:29 +0200403 no_replay = 0;
Johannes Schindelin9509af62007-03-01 05:26:30 +0100404 action = CHERRY_PICK;
405 return revert_or_cherry_pick(argc, argv);
406}