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