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