revision traversal and pack: notice and die on missing commit
[git/spearce.git] / builtin-revert.c
blob64f0d0ee028fd143ea065cad93ce7be03d94839d
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 "diff.h"
12 #include "revision.h"
15 * This implements the builtins revert and cherry-pick.
17 * Copyright (c) 2007 Johannes E. Schindelin
19 * Based on git-revert.sh, which is
21 * Copyright (c) 2005 Linus Torvalds
22 * Copyright (c) 2005 Junio C Hamano
25 static const char * const revert_usage[] = {
26 "git-revert [options] <commit-ish>",
27 NULL
30 static const char * const cherry_pick_usage[] = {
31 "git-cherry-pick [options] <commit-ish>",
32 NULL
35 static int edit, no_replay, no_commit, mainline;
36 static enum { REVERT, CHERRY_PICK } action;
37 static struct commit *commit;
39 static const char *me;
41 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
43 static void parse_args(int argc, const char **argv)
45 const char * const * usage_str =
46 action == REVERT ? revert_usage : cherry_pick_usage;
47 unsigned char sha1[20];
48 const char *arg;
49 int noop;
50 struct option options[] = {
51 OPT_BOOLEAN('n', "no-commit", &no_commit, "don't automatically commit"),
52 OPT_BOOLEAN('e', "edit", &edit, "edit the commit message"),
53 OPT_BOOLEAN('x', NULL, &no_replay, "append commit name when cherry-picking"),
54 OPT_BOOLEAN('r', NULL, &noop, "no-op (backward compatibility)"),
55 OPT_INTEGER('m', "mainline", &mainline, "parent number"),
56 OPT_END(),
59 if (parse_options(argc, argv, options, usage_str, 0) != 1)
60 usage_with_options(usage_str, options);
61 arg = argv[0];
63 if (get_sha1(arg, sha1))
64 die ("Cannot find '%s'", arg);
65 commit = (struct commit *)parse_object(sha1);
66 if (!commit)
67 die ("Could not find %s", sha1_to_hex(sha1));
68 if (commit->object.type == OBJ_TAG) {
69 commit = (struct commit *)
70 deref_tag((struct object *)commit, arg, strlen(arg));
72 if (commit->object.type != OBJ_COMMIT)
73 die ("'%s' does not point to a commit", arg);
76 static char *get_oneline(const char *message)
78 char *result;
79 const char *p = message, *abbrev, *eol;
80 int abbrev_len, oneline_len;
82 if (!p)
83 die ("Could not read commit message of %s",
84 sha1_to_hex(commit->object.sha1));
85 while (*p && (*p != '\n' || p[1] != '\n'))
86 p++;
88 if (*p) {
89 p += 2;
90 for (eol = p + 1; *eol && *eol != '\n'; eol++)
91 ; /* do nothing */
92 } else
93 eol = p;
94 abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
95 abbrev_len = strlen(abbrev);
96 oneline_len = eol - p;
97 result = xmalloc(abbrev_len + 5 + oneline_len);
98 memcpy(result, abbrev, abbrev_len);
99 memcpy(result + abbrev_len, "... ", 4);
100 memcpy(result + abbrev_len + 4, p, oneline_len);
101 result[abbrev_len + 4 + oneline_len] = '\0';
102 return result;
105 static char *get_encoding(const char *message)
107 const char *p = message, *eol;
109 if (!p)
110 die ("Could not read commit message of %s",
111 sha1_to_hex(commit->object.sha1));
112 while (*p && *p != '\n') {
113 for (eol = p + 1; *eol && *eol != '\n'; eol++)
114 ; /* do nothing */
115 if (!prefixcmp(p, "encoding ")) {
116 char *result = xmalloc(eol - 8 - p);
117 strlcpy(result, p + 9, eol - 8 - p);
118 return result;
120 p = eol;
121 if (*p == '\n')
122 p++;
124 return NULL;
127 static struct lock_file msg_file;
128 static int msg_fd;
130 static void add_to_msg(const char *string)
132 int len = strlen(string);
133 if (write_in_full(msg_fd, string, len) < 0)
134 die ("Could not write to MERGE_MSG");
137 static void add_message_to_msg(const char *message)
139 const char *p = message;
140 while (*p && (*p != '\n' || p[1] != '\n'))
141 p++;
143 if (!*p)
144 add_to_msg(sha1_to_hex(commit->object.sha1));
146 p += 2;
147 add_to_msg(p);
148 return;
151 static void set_author_ident_env(const char *message)
153 const char *p = message;
154 if (!p)
155 die ("Could not read commit message of %s",
156 sha1_to_hex(commit->object.sha1));
157 while (*p && *p != '\n') {
158 const char *eol;
160 for (eol = p; *eol && *eol != '\n'; eol++)
161 ; /* do nothing */
162 if (!prefixcmp(p, "author ")) {
163 char *line, *pend, *email, *timestamp;
165 p += 7;
166 line = xmemdupz(p, eol - p);
167 email = strchr(line, '<');
168 if (!email)
169 die ("Could not extract author email from %s",
170 sha1_to_hex(commit->object.sha1));
171 if (email == line)
172 pend = line;
173 else
174 for (pend = email; pend != line + 1 &&
175 isspace(pend[-1]); pend--);
176 ; /* do nothing */
177 *pend = '\0';
178 email++;
179 timestamp = strchr(email, '>');
180 if (!timestamp)
181 die ("Could not extract author email from %s",
182 sha1_to_hex(commit->object.sha1));
183 *timestamp = '\0';
184 for (timestamp++; *timestamp && isspace(*timestamp);
185 timestamp++)
186 ; /* do nothing */
187 setenv("GIT_AUTHOR_NAME", line, 1);
188 setenv("GIT_AUTHOR_EMAIL", email, 1);
189 setenv("GIT_AUTHOR_DATE", timestamp, 1);
190 free(line);
191 return;
193 p = eol;
194 if (*p == '\n')
195 p++;
197 die ("No author information found in %s",
198 sha1_to_hex(commit->object.sha1));
201 static int merge_recursive(const char *base_sha1,
202 const char *head_sha1, const char *head_name,
203 const char *next_sha1, const char *next_name)
205 char buffer[256];
206 const char *argv[6];
208 sprintf(buffer, "GITHEAD_%s", head_sha1);
209 setenv(buffer, head_name, 1);
210 sprintf(buffer, "GITHEAD_%s", next_sha1);
211 setenv(buffer, next_name, 1);
214 * This three way merge is an interesting one. We are at
215 * $head, and would want to apply the change between $commit
216 * and $prev on top of us (when reverting), or the change between
217 * $prev and $commit on top of us (when cherry-picking or replaying).
219 argv[0] = "merge-recursive";
220 argv[1] = base_sha1;
221 argv[2] = "--";
222 argv[3] = head_sha1;
223 argv[4] = next_sha1;
224 argv[5] = NULL;
226 return run_command_v_opt(argv, RUN_COMMAND_NO_STDIN | RUN_GIT_CMD);
229 static char *help_msg(const unsigned char *sha1)
231 static char helpbuf[1024];
232 char *msg = getenv("GIT_CHERRY_PICK_HELP");
234 if (msg)
235 return msg;
237 strcpy(helpbuf, " After resolving the conflicts,\n"
238 "mark the corrected paths with 'git add <paths>' "
239 "or 'git rm <paths>' and commit the result.");
241 if (action == CHERRY_PICK) {
242 sprintf(helpbuf + strlen(helpbuf),
243 "\nWhen commiting, use the option "
244 "'-c %s' to retain authorship and message.",
245 find_unique_abbrev(sha1, DEFAULT_ABBREV));
247 return helpbuf;
250 static int index_is_dirty(void)
252 struct rev_info rev;
253 init_revisions(&rev, NULL);
254 setup_revisions(0, NULL, &rev, "HEAD");
255 DIFF_OPT_SET(&rev.diffopt, QUIET);
256 DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
257 run_diff_index(&rev, 1);
258 return !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES);
261 static int revert_or_cherry_pick(int argc, const char **argv)
263 unsigned char head[20];
264 struct commit *base, *next, *parent;
265 int i;
266 char *oneline, *reencoded_message = NULL;
267 const char *message, *encoding;
268 const char *defmsg = xstrdup(git_path("MERGE_MSG"));
270 git_config(git_default_config);
271 me = action == REVERT ? "revert" : "cherry-pick";
272 setenv(GIT_REFLOG_ACTION, me, 0);
273 parse_args(argc, argv);
275 /* this is copied from the shell script, but it's never triggered... */
276 if (action == REVERT && !no_replay)
277 die("revert is incompatible with replay");
279 if (no_commit) {
281 * We do not intend to commit immediately. We just want to
282 * merge the differences in, so let's compute the tree
283 * that represents the "current" state for merge-recursive
284 * to work on.
286 if (write_tree(head, 0, NULL))
287 die ("Your index file is unmerged.");
288 } else {
289 if (get_sha1("HEAD", head))
290 die ("You do not have a valid HEAD");
291 if (read_cache() < 0)
292 die("could not read the index");
293 if (index_is_dirty())
294 die ("Dirty index: cannot %s", me);
295 discard_cache();
298 if (!commit->parents)
299 die ("Cannot %s a root commit", me);
300 if (commit->parents->next) {
301 /* Reverting or cherry-picking a merge commit */
302 int cnt;
303 struct commit_list *p;
305 if (!mainline)
306 die("Commit %s is a merge but no -m option was given.",
307 sha1_to_hex(commit->object.sha1));
309 for (cnt = 1, p = commit->parents;
310 cnt != mainline && p;
311 cnt++)
312 p = p->next;
313 if (cnt != mainline || !p)
314 die("Commit %s does not have parent %d",
315 sha1_to_hex(commit->object.sha1), mainline);
316 parent = p->item;
317 } else if (0 < mainline)
318 die("Mainline was specified but commit %s is not a merge.",
319 sha1_to_hex(commit->object.sha1));
320 else
321 parent = commit->parents->item;
323 if (!(message = commit->buffer))
324 die ("Cannot get commit message for %s",
325 sha1_to_hex(commit->object.sha1));
328 * "commit" is an existing commit. We would want to apply
329 * the difference it introduces since its first parent "prev"
330 * on top of the current HEAD if we are cherry-pick. Or the
331 * reverse of it if we are revert.
334 msg_fd = hold_lock_file_for_update(&msg_file, defmsg, 1);
336 encoding = get_encoding(message);
337 if (!encoding)
338 encoding = "utf-8";
339 if (!git_commit_encoding)
340 git_commit_encoding = "utf-8";
341 if ((reencoded_message = reencode_string(message,
342 git_commit_encoding, encoding)))
343 message = reencoded_message;
345 oneline = get_oneline(message);
347 if (action == REVERT) {
348 char *oneline_body = strchr(oneline, ' ');
350 base = commit;
351 next = parent;
352 add_to_msg("Revert \"");
353 add_to_msg(oneline_body + 1);
354 add_to_msg("\"\n\nThis reverts commit ");
355 add_to_msg(sha1_to_hex(commit->object.sha1));
356 add_to_msg(".\n");
357 } else {
358 base = parent;
359 next = commit;
360 set_author_ident_env(message);
361 add_message_to_msg(message);
362 if (no_replay) {
363 add_to_msg("(cherry picked from commit ");
364 add_to_msg(sha1_to_hex(commit->object.sha1));
365 add_to_msg(")\n");
369 if (merge_recursive(sha1_to_hex(base->object.sha1),
370 sha1_to_hex(head), "HEAD",
371 sha1_to_hex(next->object.sha1), oneline) ||
372 write_tree(head, 0, NULL)) {
373 add_to_msg("\nConflicts:\n\n");
374 read_cache();
375 for (i = 0; i < active_nr;) {
376 struct cache_entry *ce = active_cache[i++];
377 if (ce_stage(ce)) {
378 add_to_msg("\t");
379 add_to_msg(ce->name);
380 add_to_msg("\n");
381 while (i < active_nr && !strcmp(ce->name,
382 active_cache[i]->name))
383 i++;
386 if (commit_lock_file(&msg_file) < 0)
387 die ("Error wrapping up %s", defmsg);
388 fprintf(stderr, "Automatic %s failed.%s\n",
389 me, help_msg(commit->object.sha1));
390 exit(1);
392 if (commit_lock_file(&msg_file) < 0)
393 die ("Error wrapping up %s", defmsg);
394 fprintf(stderr, "Finished one %s.\n", me);
398 * If we are cherry-pick, and if the merge did not result in
399 * hand-editing, we will hit this commit and inherit the original
400 * author date and name.
401 * If we are revert, or if our cherry-pick results in a hand merge,
402 * we had better say that the current user is responsible for that.
405 if (!no_commit) {
406 if (edit)
407 return execl_git_cmd("commit", "-n", NULL);
408 else
409 return execl_git_cmd("commit", "-n", "-F", defmsg, NULL);
411 if (reencoded_message)
412 free(reencoded_message);
414 return 0;
417 int cmd_revert(int argc, const char **argv, const char *prefix)
419 if (isatty(0))
420 edit = 1;
421 no_replay = 1;
422 action = REVERT;
423 return revert_or_cherry_pick(argc, argv);
426 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
428 no_replay = 0;
429 action = CHERRY_PICK;
430 return revert_or_cherry_pick(argc, argv);