Makefile: Fix 'clean' target to remove all gitweb build files
[git/git.git] / builtin / revert.c
CommitLineData
9509af68
JS
1#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"
f8103794 10#include "parse-options.h"
45525bd0 11#include "cache-tree.h"
0f2d4476
JK
12#include "diff.h"
13#include "revision.h"
aa1a0111 14#include "rerere.h"
6eb1b437 15#include "merge-recursive.h"
c62f6ec3 16#include "refs.h"
9509af68
JS
17
18/*
19 * This implements the builtins revert and cherry-pick.
20 *
21 * Copyright (c) 2007 Johannes E. Schindelin
22 *
23 * Based on git-revert.sh, which is
24 *
25 * Copyright (c) 2005 Linus Torvalds
26 * Copyright (c) 2005 Junio C Hamano
27 */
28
f8103794 29static const char * const revert_usage[] = {
1b1dd23f 30 "git revert [options] <commit-ish>",
f8103794
PH
31 NULL
32};
9509af68 33
f8103794 34static const char * const cherry_pick_usage[] = {
1b1dd23f 35 "git cherry-pick [options] <commit-ish>",
f8103794
PH
36 NULL
37};
9509af68 38
c62f6ec3 39static int edit, no_replay, no_commit, mainline, signoff, allow_ff;
4175e9e3 40static enum { REVERT, CHERRY_PICK } action;
9509af68 41static struct commit *commit;
97915544 42static const char *commit_name;
cb6020bb 43static int allow_rerere_auto;
9509af68
JS
44
45static const char *me;
46
47#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
48
d6856540
JN
49static char *get_encoding(const char *message);
50
f8103794 51static void parse_args(int argc, const char **argv)
9509af68 52{
f8103794
PH
53 const char * const * usage_str =
54 action == REVERT ? revert_usage : cherry_pick_usage;
9509af68 55 unsigned char sha1[20];
f8103794
PH
56 int noop;
57 struct option options[] = {
58 OPT_BOOLEAN('n', "no-commit", &no_commit, "don't automatically commit"),
59 OPT_BOOLEAN('e', "edit", &edit, "edit the commit message"),
60 OPT_BOOLEAN('x', NULL, &no_replay, "append commit name when cherry-picking"),
61 OPT_BOOLEAN('r', NULL, &noop, "no-op (backward compatibility)"),
cfd9c277 62 OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"),
02273fdb 63 OPT_INTEGER('m', "mainline", &mainline, "parent number"),
cb6020bb 64 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto),
f8103794 65 OPT_END(),
c62f6ec3
JH
66 OPT_END(),
67 OPT_END(),
f8103794
PH
68 };
69
c62f6ec3
JH
70 if (action == CHERRY_PICK) {
71 struct option cp_extra[] = {
72 OPT_BOOLEAN(0, "ff", &allow_ff, "allow fast-forward"),
73 OPT_END(),
74 };
75 if (parse_options_concat(options, ARRAY_SIZE(options), cp_extra))
76 die("program error");
77 }
78
37782920 79 if (parse_options(argc, argv, NULL, options, usage_str, 0) != 1)
f8103794 80 usage_with_options(usage_str, options);
9509af68 81
97915544
JK
82 commit_name = argv[0];
83 if (get_sha1(commit_name, sha1))
84 die ("Cannot find '%s'", commit_name);
dd9314cc 85 commit = lookup_commit_reference(sha1);
9509af68 86 if (!commit)
dd9314cc 87 exit(1);
9509af68
JS
88}
89
d6856540
JN
90struct commit_message {
91 char *parent_label;
92 const char *label;
93 const char *subject;
94 char *reencoded_message;
95 const char *message;
96};
97
98static int get_message(const char *raw_message, struct commit_message *out)
9509af68 99{
d6856540
JN
100 const char *encoding;
101 const char *p, *abbrev, *eol;
102 char *q;
9509af68
JS
103 int abbrev_len, oneline_len;
104
d6856540
JN
105 if (!raw_message)
106 return -1;
107 encoding = get_encoding(raw_message);
108 if (!encoding)
109 encoding = "UTF-8";
110 if (!git_commit_encoding)
111 git_commit_encoding = "UTF-8";
112 if ((out->reencoded_message = reencode_string(raw_message,
113 git_commit_encoding, encoding)))
114 out->message = out->reencoded_message;
115
116 abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
117 abbrev_len = strlen(abbrev);
118
119 /* Find beginning and end of commit subject. */
120 p = out->message;
9509af68
JS
121 while (*p && (*p != '\n' || p[1] != '\n'))
122 p++;
9509af68
JS
123 if (*p) {
124 p += 2;
125 for (eol = p + 1; *eol && *eol != '\n'; eol++)
126 ; /* do nothing */
127 } else
128 eol = p;
9509af68 129 oneline_len = eol - p;
d6856540
JN
130
131 out->parent_label = xmalloc(strlen("parent of ") + abbrev_len +
132 strlen("... ") + oneline_len + 1);
133 q = out->parent_label;
134 q = mempcpy(q, "parent of ", strlen("parent of "));
135 out->label = q;
136 q = mempcpy(q, abbrev, abbrev_len);
137 q = mempcpy(q, "... ", strlen("... "));
138 out->subject = q;
139 q = mempcpy(q, p, oneline_len);
140 *q = '\0';
141 return 0;
142}
143
144static void free_message(struct commit_message *msg)
145{
146 free(msg->parent_label);
147 free(msg->reencoded_message);
9509af68
JS
148}
149
52fae7de 150static char *get_encoding(const char *message)
9509af68
JS
151{
152 const char *p = message, *eol;
153
154 if (!p)
155 die ("Could not read commit message of %s",
156 sha1_to_hex(commit->object.sha1));
157 while (*p && *p != '\n') {
158 for (eol = p + 1; *eol && *eol != '\n'; eol++)
159 ; /* do nothing */
160 if (!prefixcmp(p, "encoding ")) {
161 char *result = xmalloc(eol - 8 - p);
162 strlcpy(result, p + 9, eol - 8 - p);
163 return result;
164 }
165 p = eol;
166 if (*p == '\n')
167 p++;
168 }
169 return NULL;
170}
171
4175e9e3 172static struct lock_file msg_file;
9509af68
JS
173static int msg_fd;
174
175static void add_to_msg(const char *string)
176{
177 int len = strlen(string);
178 if (write_in_full(msg_fd, string, len) < 0)
0721c314 179 die_errno ("Could not write to MERGE_MSG");
9509af68
JS
180}
181
182static void add_message_to_msg(const char *message)
183{
184 const char *p = message;
185 while (*p && (*p != '\n' || p[1] != '\n'))
186 p++;
187
188 if (!*p)
189 add_to_msg(sha1_to_hex(commit->object.sha1));
190
191 p += 2;
192 add_to_msg(p);
193 return;
194}
195
196static void set_author_ident_env(const char *message)
197{
198 const char *p = message;
199 if (!p)
200 die ("Could not read commit message of %s",
201 sha1_to_hex(commit->object.sha1));
202 while (*p && *p != '\n') {
203 const char *eol;
204
205 for (eol = p; *eol && *eol != '\n'; eol++)
206 ; /* do nothing */
207 if (!prefixcmp(p, "author ")) {
208 char *line, *pend, *email, *timestamp;
209
210 p += 7;
182af834 211 line = xmemdupz(p, eol - p);
9509af68
JS
212 email = strchr(line, '<');
213 if (!email)
214 die ("Could not extract author email from %s",
215 sha1_to_hex(commit->object.sha1));
216 if (email == line)
217 pend = line;
218 else
219 for (pend = email; pend != line + 1 &&
220 isspace(pend[-1]); pend--);
221 ; /* do nothing */
222 *pend = '\0';
223 email++;
224 timestamp = strchr(email, '>');
225 if (!timestamp)
1e5f7add 226 die ("Could not extract author time from %s",
9509af68
JS
227 sha1_to_hex(commit->object.sha1));
228 *timestamp = '\0';
229 for (timestamp++; *timestamp && isspace(*timestamp);
230 timestamp++)
231 ; /* do nothing */
232 setenv("GIT_AUTHOR_NAME", line, 1);
233 setenv("GIT_AUTHOR_EMAIL", email, 1);
234 setenv("GIT_AUTHOR_DATE", timestamp, 1);
235 free(line);
236 return;
237 }
238 p = eol;
239 if (*p == '\n')
240 p++;
241 }
242 die ("No author information found in %s",
243 sha1_to_hex(commit->object.sha1));
244}
245
97915544 246static char *help_msg(const char *name)
804c7174 247{
08565bdb 248 struct strbuf helpbuf = STRBUF_INIT;
804c7174
WC
249 char *msg = getenv("GIT_CHERRY_PICK_HELP");
250
251 if (msg)
252 return msg;
253
08565bdb 254 strbuf_addstr(&helpbuf, " After resolving the conflicts,\n"
6e359978 255 "mark the corrected paths with 'git add <paths>' or 'git rm <paths>'\n"
4d128884 256 "and commit the result");
804c7174
WC
257
258 if (action == CHERRY_PICK) {
4d128884
JK
259 strbuf_addf(&helpbuf, " with: \n"
260 "\n"
261 " git commit -c %s\n",
97915544 262 name);
804c7174 263 }
4d128884
JK
264 else
265 strbuf_addch(&helpbuf, '.');
08565bdb 266 return strbuf_detach(&helpbuf, NULL);
804c7174
WC
267}
268
6eb1b437
MV
269static struct tree *empty_tree(void)
270{
271 struct tree *tree = xcalloc(1, sizeof(struct tree));
272
273 tree->object.parsed = 1;
274 tree->object.type = OBJ_TREE;
275 pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
276 return tree;
277}
278
d38a30df
MM
279static NORETURN void die_dirty_index(const char *me)
280{
281 if (read_cache_unmerged()) {
282 die_resolve_conflict(me);
283 } else {
284 if (advice_commit_before_merge)
285 die("Your local changes would be overwritten by %s.\n"
286 "Please, commit your changes or stash them to proceed.", me);
287 else
288 die("Your local changes would be overwritten by %s.\n", me);
289 }
290}
291
c62f6ec3
JH
292static int fast_forward_to(const unsigned char *to, const unsigned char *from)
293{
294 struct ref_lock *ref_lock;
295
296 read_cache();
297 if (checkout_fast_forward(from, to))
298 exit(1); /* the callee should have complained already */
299 ref_lock = lock_any_ref_for_update("HEAD", from, 0);
300 return write_ref_sha1(ref_lock, to, "cherry-pick");
301}
302
9509af68
JS
303static int revert_or_cherry_pick(int argc, const char **argv)
304{
305 unsigned char head[20];
7791ecbc 306 struct commit *base, *next, *parent;
bf975d37 307 const char *base_label, *next_label;
6eb1b437 308 int i, index_fd, clean;
d6856540 309 struct commit_message msg = { NULL, NULL, NULL, NULL, NULL };
28db756f 310 char *defmsg = NULL;
6eb1b437
MV
311 struct merge_options o;
312 struct tree *result, *next_tree, *base_tree, *head_tree;
313 static struct lock_file index_lock;
9509af68 314
ef90d6d4 315 git_config(git_default_config, NULL);
9509af68
JS
316 me = action == REVERT ? "revert" : "cherry-pick";
317 setenv(GIT_REFLOG_ACTION, me, 0);
f8103794 318 parse_args(argc, argv);
9509af68
JS
319
320 /* this is copied from the shell script, but it's never triggered... */
f8103794 321 if (action == REVERT && !no_replay)
9509af68
JS
322 die("revert is incompatible with replay");
323
c62f6ec3
JH
324 if (allow_ff) {
325 if (signoff)
326 die("cherry-pick --ff cannot be used with --signoff");
327 if (no_commit)
328 die("cherry-pick --ff cannot be used with --no-commit");
329 if (no_replay)
330 die("cherry-pick --ff cannot be used with -x");
331 if (edit)
332 die("cherry-pick --ff cannot be used with --edit");
333 }
334
6eb1b437
MV
335 if (read_cache() < 0)
336 die("git %s: failed to read the index", me);
9509af68
JS
337 if (no_commit) {
338 /*
339 * We do not intend to commit immediately. We just want to
71aa2b8f
JH
340 * merge the differences in, so let's compute the tree
341 * that represents the "current" state for merge-recursive
342 * to work on.
9509af68 343 */
45525bd0 344 if (write_cache_as_tree(head, 0, NULL))
9509af68
JS
345 die ("Your index file is unmerged.");
346 } else {
9509af68
JS
347 if (get_sha1("HEAD", head))
348 die ("You do not have a valid HEAD");
75f3ff2e 349 if (index_differs_from("HEAD", 0))
d38a30df 350 die_dirty_index(me);
9509af68 351 }
6eb1b437
MV
352 discard_cache();
353
f95ebf74
JS
354 if (!commit->parents) {
355 if (action == REVERT)
356 die ("Cannot revert a root commit");
357 parent = NULL;
358 }
359 else if (commit->parents->next) {
7791ecbc
JH
360 /* Reverting or cherry-picking a merge commit */
361 int cnt;
362 struct commit_list *p;
363
364 if (!mainline)
365 die("Commit %s is a merge but no -m option was given.",
366 sha1_to_hex(commit->object.sha1));
367
368 for (cnt = 1, p = commit->parents;
369 cnt != mainline && p;
370 cnt++)
371 p = p->next;
372 if (cnt != mainline || !p)
373 die("Commit %s does not have parent %d",
374 sha1_to_hex(commit->object.sha1), mainline);
375 parent = p->item;
376 } else if (0 < mainline)
377 die("Mainline was specified but commit %s is not a merge.",
378 sha1_to_hex(commit->object.sha1));
379 else
380 parent = commit->parents->item;
381
c62f6ec3
JH
382 if (allow_ff && !hashcmp(parent->object.sha1, head))
383 return fast_forward_to(commit->object.sha1, head);
384
6eb1b437
MV
385 if (parent && parse_commit(parent) < 0)
386 die("%s: cannot parse parent commit %s",
387 me, sha1_to_hex(parent->object.sha1));
388
d6856540
JN
389 if (get_message(commit->buffer, &msg) != 0)
390 die("Cannot get commit message for %s",
391 sha1_to_hex(commit->object.sha1));
392
9509af68
JS
393 /*
394 * "commit" is an existing commit. We would want to apply
395 * the difference it introduces since its first parent "prev"
396 * on top of the current HEAD if we are cherry-pick. Or the
397 * reverse of it if we are revert.
398 */
399
28db756f 400 defmsg = git_pathdup("MERGE_MSG");
acd3b9ec
JH
401 msg_fd = hold_lock_file_for_update(&msg_file, defmsg,
402 LOCK_DIE_ON_ERROR);
9509af68 403
c62f6ec3
JH
404 index_fd = hold_locked_index(&index_lock, 1);
405
9509af68
JS
406 if (action == REVERT) {
407 base = commit;
bf975d37 408 base_label = msg.label;
7791ecbc 409 next = parent;
d6856540 410 next_label = msg.parent_label;
e43b0105 411 add_to_msg("Revert \"");
d6856540 412 add_to_msg(msg.subject);
e43b0105 413 add_to_msg("\"\n\nThis reverts commit ");
9509af68 414 add_to_msg(sha1_to_hex(commit->object.sha1));
d5be89d8
RR
415
416 if (commit->parents->next) {
417 add_to_msg(", reversing\nchanges made to ");
418 add_to_msg(sha1_to_hex(parent->object.sha1));
419 }
9509af68
JS
420 add_to_msg(".\n");
421 } else {
7791ecbc 422 base = parent;
bf975d37 423 base_label = msg.parent_label;
9509af68 424 next = commit;
d6856540
JN
425 next_label = msg.label;
426 set_author_ident_env(msg.message);
427 add_message_to_msg(msg.message);
f8103794 428 if (no_replay) {
0e624044 429 add_to_msg("(cherry picked from commit ");
9509af68
JS
430 add_to_msg(sha1_to_hex(commit->object.sha1));
431 add_to_msg(")\n");
432 }
433 }
9509af68 434
6eb1b437
MV
435 read_cache();
436 init_merge_options(&o);
bf975d37 437 o.ancestor = base ? base_label : "(empty tree)";
6eb1b437 438 o.branch1 = "HEAD";
d6856540 439 o.branch2 = next ? next_label : "(empty tree)";
6eb1b437
MV
440
441 head_tree = parse_tree_indirect(head);
442 next_tree = next ? next->tree : empty_tree();
443 base_tree = base ? base->tree : empty_tree();
444
445 clean = merge_trees(&o,
446 head_tree,
447 next_tree, base_tree, &result);
448
449 if (active_cache_changed &&
450 (write_cache(index_fd, active_cache, active_nr) ||
451 commit_locked_index(&index_lock)))
452 die("%s: Unable to write new index file", me);
0d66e959 453 rollback_lock_file(&index_lock);
6eb1b437
MV
454
455 if (!clean) {
9509af68 456 add_to_msg("\nConflicts:\n\n");
9509af68
JS
457 for (i = 0; i < active_nr;) {
458 struct cache_entry *ce = active_cache[i++];
459 if (ce_stage(ce)) {
460 add_to_msg("\t");
461 add_to_msg(ce->name);
462 add_to_msg("\n");
463 while (i < active_nr && !strcmp(ce->name,
464 active_cache[i]->name))
465 i++;
466 }
467 }
4ed7cd3a 468 if (commit_lock_file(&msg_file) < 0)
abda5227 469 die ("Error wrapping up %s", defmsg);
804c7174 470 fprintf(stderr, "Automatic %s failed.%s\n",
97915544 471 me, help_msg(commit_name));
cb6020bb 472 rerere(allow_rerere_auto);
9509af68
JS
473 exit(1);
474 }
4ed7cd3a 475 if (commit_lock_file(&msg_file) < 0)
abda5227 476 die ("Error wrapping up %s", defmsg);
9509af68
JS
477 fprintf(stderr, "Finished one %s.\n", me);
478
479 /*
480 *
481 * If we are cherry-pick, and if the merge did not result in
482 * hand-editing, we will hit this commit and inherit the original
483 * author date and name.
484 * If we are revert, or if our cherry-pick results in a hand merge,
485 * we had better say that the current user is responsible for that.
486 */
487
488 if (!no_commit) {
cfd9c277
DM
489 /* 6 is max possible length of our args array including NULL */
490 const char *args[6];
491 int i = 0;
492 args[i++] = "commit";
493 args[i++] = "-n";
494 if (signoff)
495 args[i++] = "-s";
496 if (!edit) {
497 args[i++] = "-F";
498 args[i++] = defmsg;
499 }
500 args[i] = NULL;
501 return execv_git_cmd(args);
9509af68 502 }
d6856540 503 free_message(&msg);
d258b258 504 free(defmsg);
9509af68
JS
505
506 return 0;
507}
508
509int cmd_revert(int argc, const char **argv, const char *prefix)
510{
511 if (isatty(0))
512 edit = 1;
f8103794 513 no_replay = 1;
9509af68
JS
514 action = REVERT;
515 return revert_or_cherry_pick(argc, argv);
516}
517
518int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
519{
f8103794 520 no_replay = 0;
9509af68
JS
521 action = CHERRY_PICK;
522 return revert_or_cherry_pick(argc, argv);
523}