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