Merge branch 'lt/config-fsync' into maint
[git/platforms.git] / builtin-revert.c
blobbde28b2c4df9e0d79589a594a8d93103a4f7bd39
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"
10 #include "parse-options.h"
11 #include "cache-tree.h"
12 #include "diff.h"
13 #include "revision.h"
16 * This implements the builtins revert and cherry-pick.
18 * Copyright (c) 2007 Johannes E. Schindelin
20 * Based on git-revert.sh, which is
22 * Copyright (c) 2005 Linus Torvalds
23 * Copyright (c) 2005 Junio C Hamano
26 static const char * const revert_usage[] = {
27 "git-revert [options] <commit-ish>",
28 NULL
31 static const char * const cherry_pick_usage[] = {
32 "git-cherry-pick [options] <commit-ish>",
33 NULL
36 static int edit, no_replay, no_commit, mainline, signoff;
37 static enum { REVERT, CHERRY_PICK } action;
38 static struct commit *commit;
40 static const char *me;
42 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
44 static void parse_args(int argc, const char **argv)
46 const char * const * usage_str =
47 action == REVERT ? revert_usage : cherry_pick_usage;
48 unsigned char sha1[20];
49 const char *arg;
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)"),
56 OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"),
57 OPT_INTEGER('m', "mainline", &mainline, "parent number"),
58 OPT_END(),
61 if (parse_options(argc, argv, options, usage_str, 0) != 1)
62 usage_with_options(usage_str, options);
63 arg = argv[0];
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));
74 if (commit->object.type != OBJ_COMMIT)
75 die ("'%s' does not point to a commit", arg);
78 static char *get_oneline(const char *message)
80 char *result;
81 const char *p = message, *abbrev, *eol;
82 int abbrev_len, oneline_len;
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++;
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;
107 static char *get_encoding(const char *message)
109 const char *p = message, *eol;
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;
122 p = eol;
123 if (*p == '\n')
124 p++;
126 return NULL;
129 static struct lock_file msg_file;
130 static int msg_fd;
132 static void add_to_msg(const char *string)
134 int len = strlen(string);
135 if (write_in_full(msg_fd, string, len) < 0)
136 die ("Could not write to MERGE_MSG");
139 static void add_message_to_msg(const char *message)
141 const char *p = message;
142 while (*p && (*p != '\n' || p[1] != '\n'))
143 p++;
145 if (!*p)
146 add_to_msg(sha1_to_hex(commit->object.sha1));
148 p += 2;
149 add_to_msg(p);
150 return;
153 static void set_author_ident_env(const char *message)
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;
162 for (eol = p; *eol && *eol != '\n'; eol++)
163 ; /* do nothing */
164 if (!prefixcmp(p, "author ")) {
165 char *line, *pend, *email, *timestamp;
167 p += 7;
168 line = xmemdupz(p, eol - p);
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)
183 die ("Could not extract author time from %s",
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;
195 p = eol;
196 if (*p == '\n')
197 p++;
199 die ("No author information found in %s",
200 sha1_to_hex(commit->object.sha1));
203 static 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)
207 char buffer[256];
208 const char *argv[6];
210 sprintf(buffer, "GITHEAD_%s", head_sha1);
211 setenv(buffer, head_name, 1);
212 sprintf(buffer, "GITHEAD_%s", next_sha1);
213 setenv(buffer, next_name, 1);
216 * This three way merge is an interesting one. We are at
217 * $head, and would want to apply the change between $commit
218 * and $prev on top of us (when reverting), or the change between
219 * $prev and $commit on top of us (when cherry-picking or replaying).
221 argv[0] = "merge-recursive";
222 argv[1] = base_sha1;
223 argv[2] = "--";
224 argv[3] = head_sha1;
225 argv[4] = next_sha1;
226 argv[5] = NULL;
228 return run_command_v_opt(argv, RUN_COMMAND_NO_STDIN | RUN_GIT_CMD);
231 static char *help_msg(const unsigned char *sha1)
233 static char helpbuf[1024];
234 char *msg = getenv("GIT_CHERRY_PICK_HELP");
236 if (msg)
237 return msg;
239 strcpy(helpbuf, " After resolving the conflicts,\n"
240 "mark the corrected paths with 'git add <paths>' "
241 "or 'git rm <paths>' and commit the result.");
243 if (action == CHERRY_PICK) {
244 sprintf(helpbuf + strlen(helpbuf),
245 "\nWhen commiting, use the option "
246 "'-c %s' to retain authorship and message.",
247 find_unique_abbrev(sha1, DEFAULT_ABBREV));
249 return helpbuf;
252 static int index_is_dirty(void)
254 struct rev_info rev;
255 init_revisions(&rev, NULL);
256 setup_revisions(0, NULL, &rev, "HEAD");
257 DIFF_OPT_SET(&rev.diffopt, QUIET);
258 DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
259 run_diff_index(&rev, 1);
260 return !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES);
263 static int revert_or_cherry_pick(int argc, const char **argv)
265 unsigned char head[20];
266 struct commit *base, *next, *parent;
267 int i;
268 char *oneline, *reencoded_message = NULL;
269 const char *message, *encoding;
270 const char *defmsg = xstrdup(git_path("MERGE_MSG"));
272 git_config(git_default_config, NULL);
273 me = action == REVERT ? "revert" : "cherry-pick";
274 setenv(GIT_REFLOG_ACTION, me, 0);
275 parse_args(argc, argv);
277 /* this is copied from the shell script, but it's never triggered... */
278 if (action == REVERT && !no_replay)
279 die("revert is incompatible with replay");
281 if (no_commit) {
283 * We do not intend to commit immediately. We just want to
284 * merge the differences in, so let's compute the tree
285 * that represents the "current" state for merge-recursive
286 * to work on.
288 if (write_cache_as_tree(head, 0, NULL))
289 die ("Your index file is unmerged.");
290 } else {
291 if (get_sha1("HEAD", head))
292 die ("You do not have a valid HEAD");
293 if (read_cache() < 0)
294 die("could not read the index");
295 if (index_is_dirty())
296 die ("Dirty index: cannot %s", me);
297 discard_cache();
300 if (!commit->parents)
301 die ("Cannot %s a root commit", me);
302 if (commit->parents->next) {
303 /* Reverting or cherry-picking a merge commit */
304 int cnt;
305 struct commit_list *p;
307 if (!mainline)
308 die("Commit %s is a merge but no -m option was given.",
309 sha1_to_hex(commit->object.sha1));
311 for (cnt = 1, p = commit->parents;
312 cnt != mainline && p;
313 cnt++)
314 p = p->next;
315 if (cnt != mainline || !p)
316 die("Commit %s does not have parent %d",
317 sha1_to_hex(commit->object.sha1), mainline);
318 parent = p->item;
319 } else if (0 < mainline)
320 die("Mainline was specified but commit %s is not a merge.",
321 sha1_to_hex(commit->object.sha1));
322 else
323 parent = commit->parents->item;
325 if (!(message = commit->buffer))
326 die ("Cannot get commit message for %s",
327 sha1_to_hex(commit->object.sha1));
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.
336 msg_fd = hold_lock_file_for_update(&msg_file, defmsg, 1);
338 encoding = get_encoding(message);
339 if (!encoding)
340 encoding = "utf-8";
341 if (!git_commit_encoding)
342 git_commit_encoding = "utf-8";
343 if ((reencoded_message = reencode_string(message,
344 git_commit_encoding, encoding)))
345 message = reencoded_message;
347 oneline = get_oneline(message);
349 if (action == REVERT) {
350 char *oneline_body = strchr(oneline, ' ');
352 base = commit;
353 next = parent;
354 add_to_msg("Revert \"");
355 add_to_msg(oneline_body + 1);
356 add_to_msg("\"\n\nThis reverts commit ");
357 add_to_msg(sha1_to_hex(commit->object.sha1));
358 add_to_msg(".\n");
359 } else {
360 base = parent;
361 next = commit;
362 set_author_ident_env(message);
363 add_message_to_msg(message);
364 if (no_replay) {
365 add_to_msg("(cherry picked from commit ");
366 add_to_msg(sha1_to_hex(commit->object.sha1));
367 add_to_msg(")\n");
371 if (merge_recursive(sha1_to_hex(base->object.sha1),
372 sha1_to_hex(head), "HEAD",
373 sha1_to_hex(next->object.sha1), oneline) ||
374 write_cache_as_tree(head, 0, NULL)) {
375 add_to_msg("\nConflicts:\n\n");
376 read_cache();
377 for (i = 0; i < active_nr;) {
378 struct cache_entry *ce = active_cache[i++];
379 if (ce_stage(ce)) {
380 add_to_msg("\t");
381 add_to_msg(ce->name);
382 add_to_msg("\n");
383 while (i < active_nr && !strcmp(ce->name,
384 active_cache[i]->name))
385 i++;
388 if (commit_lock_file(&msg_file) < 0)
389 die ("Error wrapping up %s", defmsg);
390 fprintf(stderr, "Automatic %s failed.%s\n",
391 me, help_msg(commit->object.sha1));
392 exit(1);
394 if (commit_lock_file(&msg_file) < 0)
395 die ("Error wrapping up %s", defmsg);
396 fprintf(stderr, "Finished one %s.\n", me);
400 * If we are cherry-pick, and if the merge did not result in
401 * hand-editing, we will hit this commit and inherit the original
402 * author date and name.
403 * If we are revert, or if our cherry-pick results in a hand merge,
404 * we had better say that the current user is responsible for that.
407 if (!no_commit) {
408 /* 6 is max possible length of our args array including NULL */
409 const char *args[6];
410 int i = 0;
411 args[i++] = "commit";
412 args[i++] = "-n";
413 if (signoff)
414 args[i++] = "-s";
415 if (!edit) {
416 args[i++] = "-F";
417 args[i++] = defmsg;
419 args[i] = NULL;
420 return execv_git_cmd(args);
422 free(reencoded_message);
424 return 0;
427 int cmd_revert(int argc, const char **argv, const char *prefix)
429 if (isatty(0))
430 edit = 1;
431 no_replay = 1;
432 action = REVERT;
433 return revert_or_cherry_pick(argc, argv);
436 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
438 no_replay = 0;
439 action = CHERRY_PICK;
440 return revert_or_cherry_pick(argc, argv);