Export some email and pretty-printing functions
[git/mjg.git] / builtin-revert.c
blobe219859f9b04dc94a2444346541e86503085fdea
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"
14 * This implements the builtins revert and cherry-pick.
16 * Copyright (c) 2007 Johannes E. Schindelin
18 * Based on git-revert.sh, which is
20 * Copyright (c) 2005 Linus Torvalds
21 * Copyright (c) 2005 Junio C Hamano
24 static const char * const revert_usage[] = {
25 "git-revert [options] <commit-ish>",
26 NULL
29 static const char * const cherry_pick_usage[] = {
30 "git-cherry-pick [options] <commit-ish>",
31 NULL
34 static int edit, no_replay, no_commit, mainline;
35 static enum { REVERT, CHERRY_PICK } action;
36 static struct commit *commit;
38 static const char *me;
40 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
42 static void parse_args(int argc, const char **argv)
44 const char * const * usage_str =
45 action == REVERT ? revert_usage : cherry_pick_usage;
46 unsigned char sha1[20];
47 const char *arg;
48 int noop;
49 struct option options[] = {
50 OPT_BOOLEAN('n', "no-commit", &no_commit, "don't automatically commit"),
51 OPT_BOOLEAN('e', "edit", &edit, "edit the commit message"),
52 OPT_BOOLEAN('x', NULL, &no_replay, "append commit name when cherry-picking"),
53 OPT_BOOLEAN('r', NULL, &noop, "no-op (backward compatibility)"),
54 OPT_INTEGER('m', "mainline", &mainline, "parent number"),
55 OPT_END(),
58 if (parse_options(argc, argv, options, usage_str, 0) != 1)
59 usage_with_options(usage_str, options);
60 arg = argv[0];
62 if (get_sha1(arg, sha1))
63 die ("Cannot find '%s'", arg);
64 commit = (struct commit *)parse_object(sha1);
65 if (!commit)
66 die ("Could not find %s", sha1_to_hex(sha1));
67 if (commit->object.type == OBJ_TAG) {
68 commit = (struct commit *)
69 deref_tag((struct object *)commit, arg, strlen(arg));
71 if (commit->object.type != OBJ_COMMIT)
72 die ("'%s' does not point to a commit", arg);
75 static char *get_oneline(const char *message)
77 char *result;
78 const char *p = message, *abbrev, *eol;
79 int abbrev_len, oneline_len;
81 if (!p)
82 die ("Could not read commit message of %s",
83 sha1_to_hex(commit->object.sha1));
84 while (*p && (*p != '\n' || p[1] != '\n'))
85 p++;
87 if (*p) {
88 p += 2;
89 for (eol = p + 1; *eol && *eol != '\n'; eol++)
90 ; /* do nothing */
91 } else
92 eol = p;
93 abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
94 abbrev_len = strlen(abbrev);
95 oneline_len = eol - p;
96 result = xmalloc(abbrev_len + 5 + oneline_len);
97 memcpy(result, abbrev, abbrev_len);
98 memcpy(result + abbrev_len, "... ", 4);
99 memcpy(result + abbrev_len + 4, p, oneline_len);
100 result[abbrev_len + 4 + oneline_len] = '\0';
101 return result;
104 static char *get_encoding(const char *message)
106 const char *p = message, *eol;
108 if (!p)
109 die ("Could not read commit message of %s",
110 sha1_to_hex(commit->object.sha1));
111 while (*p && *p != '\n') {
112 for (eol = p + 1; *eol && *eol != '\n'; eol++)
113 ; /* do nothing */
114 if (!prefixcmp(p, "encoding ")) {
115 char *result = xmalloc(eol - 8 - p);
116 strlcpy(result, p + 9, eol - 8 - p);
117 return result;
119 p = eol;
120 if (*p == '\n')
121 p++;
123 return NULL;
126 static struct lock_file msg_file;
127 static int msg_fd;
129 static void add_to_msg(const char *string)
131 int len = strlen(string);
132 if (write_in_full(msg_fd, string, len) < 0)
133 die ("Could not write to MERGE_MSG");
136 static void add_message_to_msg(const char *message)
138 const char *p = message;
139 while (*p && (*p != '\n' || p[1] != '\n'))
140 p++;
142 if (!*p)
143 add_to_msg(sha1_to_hex(commit->object.sha1));
145 p += 2;
146 add_to_msg(p);
147 return;
150 static void set_author_ident_env(const char *message)
152 const char *p = message;
153 if (!p)
154 die ("Could not read commit message of %s",
155 sha1_to_hex(commit->object.sha1));
156 while (*p && *p != '\n') {
157 const char *eol;
159 for (eol = p; *eol && *eol != '\n'; eol++)
160 ; /* do nothing */
161 if (!prefixcmp(p, "author ")) {
162 char *line, *pend, *email, *timestamp;
164 p += 7;
165 line = xmemdupz(p, eol - p);
166 email = strchr(line, '<');
167 if (!email)
168 die ("Could not extract author email from %s",
169 sha1_to_hex(commit->object.sha1));
170 if (email == line)
171 pend = line;
172 else
173 for (pend = email; pend != line + 1 &&
174 isspace(pend[-1]); pend--);
175 ; /* do nothing */
176 *pend = '\0';
177 email++;
178 timestamp = strchr(email, '>');
179 if (!timestamp)
180 die ("Could not extract author email from %s",
181 sha1_to_hex(commit->object.sha1));
182 *timestamp = '\0';
183 for (timestamp++; *timestamp && isspace(*timestamp);
184 timestamp++)
185 ; /* do nothing */
186 setenv("GIT_AUTHOR_NAME", line, 1);
187 setenv("GIT_AUTHOR_EMAIL", email, 1);
188 setenv("GIT_AUTHOR_DATE", timestamp, 1);
189 free(line);
190 return;
192 p = eol;
193 if (*p == '\n')
194 p++;
196 die ("No author information found in %s",
197 sha1_to_hex(commit->object.sha1));
200 static int merge_recursive(const char *base_sha1,
201 const char *head_sha1, const char *head_name,
202 const char *next_sha1, const char *next_name)
204 char buffer[256];
205 const char *argv[6];
207 sprintf(buffer, "GITHEAD_%s", head_sha1);
208 setenv(buffer, head_name, 1);
209 sprintf(buffer, "GITHEAD_%s", next_sha1);
210 setenv(buffer, next_name, 1);
213 * This three way merge is an interesting one. We are at
214 * $head, and would want to apply the change between $commit
215 * and $prev on top of us (when reverting), or the change between
216 * $prev and $commit on top of us (when cherry-picking or replaying).
218 argv[0] = "merge-recursive";
219 argv[1] = base_sha1;
220 argv[2] = "--";
221 argv[3] = head_sha1;
222 argv[4] = next_sha1;
223 argv[5] = NULL;
225 return run_command_v_opt(argv, RUN_COMMAND_NO_STDIN | RUN_GIT_CMD);
228 static char *help_msg(const unsigned char *sha1)
230 static char helpbuf[1024];
231 char *msg = getenv("GIT_CHERRY_PICK_HELP");
233 if (msg)
234 return msg;
236 strcpy(helpbuf, " After resolving the conflicts,\n"
237 "mark the corrected paths with 'git add <paths>' "
238 "or 'git rm <paths>' and commit the result.");
240 if (action == CHERRY_PICK) {
241 sprintf(helpbuf + strlen(helpbuf),
242 "\nWhen commiting, use the option "
243 "'-c %s' to retain authorship and message.",
244 find_unique_abbrev(sha1, DEFAULT_ABBREV));
246 return helpbuf;
249 static int revert_or_cherry_pick(int argc, const char **argv)
251 unsigned char head[20];
252 struct commit *base, *next, *parent;
253 int i;
254 char *oneline, *reencoded_message = NULL;
255 const char *message, *encoding;
256 const char *defmsg = xstrdup(git_path("MERGE_MSG"));
258 git_config(git_default_config);
259 me = action == REVERT ? "revert" : "cherry-pick";
260 setenv(GIT_REFLOG_ACTION, me, 0);
261 parse_args(argc, argv);
263 /* this is copied from the shell script, but it's never triggered... */
264 if (action == REVERT && !no_replay)
265 die("revert is incompatible with replay");
267 if (no_commit) {
269 * We do not intend to commit immediately. We just want to
270 * merge the differences in, so let's compute the tree
271 * that represents the "current" state for merge-recursive
272 * to work on.
274 if (write_cache_as_tree(head, 0, NULL))
275 die ("Your index file is unmerged.");
276 } else {
277 struct wt_status s;
279 if (get_sha1("HEAD", head))
280 die ("You do not have a valid HEAD");
281 wt_status_prepare(&s);
282 if (s.commitable)
283 die ("Dirty index: cannot %s", me);
284 discard_cache();
287 if (!commit->parents)
288 die ("Cannot %s a root commit", me);
289 if (commit->parents->next) {
290 /* Reverting or cherry-picking a merge commit */
291 int cnt;
292 struct commit_list *p;
294 if (!mainline)
295 die("Commit %s is a merge but no -m option was given.",
296 sha1_to_hex(commit->object.sha1));
298 for (cnt = 1, p = commit->parents;
299 cnt != mainline && p;
300 cnt++)
301 p = p->next;
302 if (cnt != mainline || !p)
303 die("Commit %s does not have parent %d",
304 sha1_to_hex(commit->object.sha1), mainline);
305 parent = p->item;
306 } else if (0 < mainline)
307 die("Mainline was specified but commit %s is not a merge.",
308 sha1_to_hex(commit->object.sha1));
309 else
310 parent = commit->parents->item;
312 if (!(message = commit->buffer))
313 die ("Cannot get commit message for %s",
314 sha1_to_hex(commit->object.sha1));
317 * "commit" is an existing commit. We would want to apply
318 * the difference it introduces since its first parent "prev"
319 * on top of the current HEAD if we are cherry-pick. Or the
320 * reverse of it if we are revert.
323 msg_fd = hold_lock_file_for_update(&msg_file, defmsg, 1);
325 encoding = get_encoding(message);
326 if (!encoding)
327 encoding = "utf-8";
328 if (!git_commit_encoding)
329 git_commit_encoding = "utf-8";
330 if ((reencoded_message = reencode_string(message,
331 git_commit_encoding, encoding)))
332 message = reencoded_message;
334 oneline = get_oneline(message);
336 if (action == REVERT) {
337 char *oneline_body = strchr(oneline, ' ');
339 base = commit;
340 next = parent;
341 add_to_msg("Revert \"");
342 add_to_msg(oneline_body + 1);
343 add_to_msg("\"\n\nThis reverts commit ");
344 add_to_msg(sha1_to_hex(commit->object.sha1));
345 add_to_msg(".\n");
346 } else {
347 base = parent;
348 next = commit;
349 set_author_ident_env(message);
350 add_message_to_msg(message);
351 if (no_replay) {
352 add_to_msg("(cherry picked from commit ");
353 add_to_msg(sha1_to_hex(commit->object.sha1));
354 add_to_msg(")\n");
358 if (merge_recursive(sha1_to_hex(base->object.sha1),
359 sha1_to_hex(head), "HEAD",
360 sha1_to_hex(next->object.sha1), oneline) ||
361 write_cache_as_tree(head, 0, NULL)) {
362 add_to_msg("\nConflicts:\n\n");
363 read_cache();
364 for (i = 0; i < active_nr;) {
365 struct cache_entry *ce = active_cache[i++];
366 if (ce_stage(ce)) {
367 add_to_msg("\t");
368 add_to_msg(ce->name);
369 add_to_msg("\n");
370 while (i < active_nr && !strcmp(ce->name,
371 active_cache[i]->name))
372 i++;
375 if (commit_lock_file(&msg_file) < 0)
376 die ("Error wrapping up %s", defmsg);
377 fprintf(stderr, "Automatic %s failed.%s\n",
378 me, help_msg(commit->object.sha1));
379 exit(1);
381 if (commit_lock_file(&msg_file) < 0)
382 die ("Error wrapping up %s", defmsg);
383 fprintf(stderr, "Finished one %s.\n", me);
387 * If we are cherry-pick, and if the merge did not result in
388 * hand-editing, we will hit this commit and inherit the original
389 * author date and name.
390 * If we are revert, or if our cherry-pick results in a hand merge,
391 * we had better say that the current user is responsible for that.
394 if (!no_commit) {
395 if (edit)
396 return execl_git_cmd("commit", "-n", NULL);
397 else
398 return execl_git_cmd("commit", "-n", "-F", defmsg, NULL);
400 if (reencoded_message)
401 free(reencoded_message);
403 return 0;
406 int cmd_revert(int argc, const char **argv, const char *prefix)
408 if (isatty(0))
409 edit = 1;
410 no_replay = 1;
411 action = REVERT;
412 return revert_or_cherry_pick(argc, argv);
415 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
417 no_replay = 0;
418 action = CHERRY_PICK;
419 return revert_or_cherry_pick(argc, argv);