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