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