git-gc --auto: simplify "repack" command line building
[git/dscho.git] / builtin-revert.c
bloba655c8ee2ab25ef778b182fbd5ff298a732c1cfd
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"
12 * This implements the builtins revert and cherry-pick.
14 * Copyright (c) 2007 Johannes E. Schindelin
16 * Based on git-revert.sh, which is
18 * Copyright (c) 2005 Linus Torvalds
19 * Copyright (c) 2005 Junio C Hamano
22 static const char *revert_usage = "git-revert [--edit | --no-edit] [-n] <commit-ish>";
24 static const char *cherry_pick_usage = "git-cherry-pick [--edit] [-n] [-r] [-x] <commit-ish>";
26 static int edit;
27 static int replay;
28 static enum { REVERT, CHERRY_PICK } action;
29 static int no_commit;
30 static struct commit *commit;
31 static int needed_deref;
33 static const char *me;
35 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
37 static void parse_options(int argc, const char **argv)
39 const char *usage_str = action == REVERT ?
40 revert_usage : cherry_pick_usage;
41 unsigned char sha1[20];
42 const char *arg;
43 int i;
45 if (argc < 2)
46 usage(usage_str);
48 for (i = 1; i < argc; i++) {
49 arg = argv[i];
50 if (arg[0] != '-')
51 break;
52 if (!strcmp(arg, "-n") || !strcmp(arg, "--no-commit"))
53 no_commit = 1;
54 else if (!strcmp(arg, "-e") || !strcmp(arg, "--edit"))
55 edit = 1;
56 else if (!strcmp(arg, "--no-edit"))
57 edit = 0;
58 else if (!strcmp(arg, "-x") || !strcmp(arg, "--i-really-want-"
59 "to-expose-my-private-commit-object-name"))
60 replay = 0;
61 else if (strcmp(arg, "-r"))
62 usage(usage_str);
64 if (i != argc - 1)
65 usage(usage_str);
66 arg = argv[argc - 1];
67 if (get_sha1(arg, sha1))
68 die ("Cannot find '%s'", arg);
69 commit = (struct commit *)parse_object(sha1);
70 if (!commit)
71 die ("Could not find %s", sha1_to_hex(sha1));
72 if (commit->object.type == OBJ_TAG) {
73 commit = (struct commit *)
74 deref_tag((struct object *)commit, arg, strlen(arg));
75 needed_deref = 1;
77 if (commit->object.type != OBJ_COMMIT)
78 die ("'%s' does not point to a commit", arg);
81 static char *get_oneline(const char *message)
83 char *result;
84 const char *p = message, *abbrev, *eol;
85 int abbrev_len, oneline_len;
87 if (!p)
88 die ("Could not read commit message of %s",
89 sha1_to_hex(commit->object.sha1));
90 while (*p && (*p != '\n' || p[1] != '\n'))
91 p++;
93 if (*p) {
94 p += 2;
95 for (eol = p + 1; *eol && *eol != '\n'; eol++)
96 ; /* do nothing */
97 } else
98 eol = p;
99 abbrev = find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV);
100 abbrev_len = strlen(abbrev);
101 oneline_len = eol - p;
102 result = xmalloc(abbrev_len + 5 + oneline_len);
103 memcpy(result, abbrev, abbrev_len);
104 memcpy(result + abbrev_len, "... ", 4);
105 memcpy(result + abbrev_len + 4, p, oneline_len);
106 result[abbrev_len + 4 + oneline_len] = '\0';
107 return result;
110 static char *get_encoding(const char *message)
112 const char *p = message, *eol;
114 if (!p)
115 die ("Could not read commit message of %s",
116 sha1_to_hex(commit->object.sha1));
117 while (*p && *p != '\n') {
118 for (eol = p + 1; *eol && *eol != '\n'; eol++)
119 ; /* do nothing */
120 if (!prefixcmp(p, "encoding ")) {
121 char *result = xmalloc(eol - 8 - p);
122 strlcpy(result, p + 9, eol - 8 - p);
123 return result;
125 p = eol;
126 if (*p == '\n')
127 p++;
129 return NULL;
132 static struct lock_file msg_file;
133 static int msg_fd;
135 static void add_to_msg(const char *string)
137 int len = strlen(string);
138 if (write_in_full(msg_fd, string, len) < 0)
139 die ("Could not write to MERGE_MSG");
142 static void add_message_to_msg(const char *message)
144 const char *p = message;
145 while (*p && (*p != '\n' || p[1] != '\n'))
146 p++;
148 if (!*p)
149 add_to_msg(sha1_to_hex(commit->object.sha1));
151 p += 2;
152 add_to_msg(p);
153 return;
156 static void set_author_ident_env(const char *message)
158 const char *p = message;
159 if (!p)
160 die ("Could not read commit message of %s",
161 sha1_to_hex(commit->object.sha1));
162 while (*p && *p != '\n') {
163 const char *eol;
165 for (eol = p; *eol && *eol != '\n'; eol++)
166 ; /* do nothing */
167 if (!prefixcmp(p, "author ")) {
168 char *line, *pend, *email, *timestamp;
170 p += 7;
171 line = xmemdupz(p, eol - p);
172 email = strchr(line, '<');
173 if (!email)
174 die ("Could not extract author email from %s",
175 sha1_to_hex(commit->object.sha1));
176 if (email == line)
177 pend = line;
178 else
179 for (pend = email; pend != line + 1 &&
180 isspace(pend[-1]); pend--);
181 ; /* do nothing */
182 *pend = '\0';
183 email++;
184 timestamp = strchr(email, '>');
185 if (!timestamp)
186 die ("Could not extract author email from %s",
187 sha1_to_hex(commit->object.sha1));
188 *timestamp = '\0';
189 for (timestamp++; *timestamp && isspace(*timestamp);
190 timestamp++)
191 ; /* do nothing */
192 setenv("GIT_AUTHOR_NAME", line, 1);
193 setenv("GIT_AUTHOR_EMAIL", email, 1);
194 setenv("GIT_AUTHOR_DATE", timestamp, 1);
195 free(line);
196 return;
198 p = eol;
199 if (*p == '\n')
200 p++;
202 die ("No author information found in %s",
203 sha1_to_hex(commit->object.sha1));
206 static int merge_recursive(const char *base_sha1,
207 const char *head_sha1, const char *head_name,
208 const char *next_sha1, const char *next_name)
210 char buffer[256];
211 const char *argv[6];
213 sprintf(buffer, "GITHEAD_%s", head_sha1);
214 setenv(buffer, head_name, 1);
215 sprintf(buffer, "GITHEAD_%s", next_sha1);
216 setenv(buffer, next_name, 1);
219 * This three way merge is an interesting one. We are at
220 * $head, and would want to apply the change between $commit
221 * and $prev on top of us (when reverting), or the change between
222 * $prev and $commit on top of us (when cherry-picking or replaying).
224 argv[0] = "merge-recursive";
225 argv[1] = base_sha1;
226 argv[2] = "--";
227 argv[3] = head_sha1;
228 argv[4] = next_sha1;
229 argv[5] = NULL;
231 return run_command_v_opt(argv, RUN_COMMAND_NO_STDIN | RUN_GIT_CMD);
234 static int revert_or_cherry_pick(int argc, const char **argv)
236 unsigned char head[20];
237 struct commit *base, *next;
238 int i;
239 char *oneline, *reencoded_message = NULL;
240 const char *message, *encoding;
241 const char *defmsg = xstrdup(git_path("MERGE_MSG"));
243 git_config(git_default_config);
244 me = action == REVERT ? "revert" : "cherry-pick";
245 setenv(GIT_REFLOG_ACTION, me, 0);
246 parse_options(argc, argv);
248 /* this is copied from the shell script, but it's never triggered... */
249 if (action == REVERT && replay)
250 die("revert is incompatible with replay");
252 if (no_commit) {
254 * We do not intend to commit immediately. We just want to
255 * merge the differences in.
257 if (write_tree(head, 0, NULL))
258 die ("Your index file is unmerged.");
259 } else {
260 struct wt_status s;
262 if (get_sha1("HEAD", head))
263 die ("You do not have a valid HEAD");
264 wt_status_prepare(&s);
265 if (s.commitable || s.workdir_dirty)
266 die ("Dirty index: cannot %s", me);
267 discard_cache();
270 if (!commit->parents)
271 die ("Cannot %s a root commit", me);
272 if (commit->parents->next)
273 die ("Cannot %s a multi-parent commit.", me);
274 if (!(message = commit->buffer))
275 die ("Cannot get commit message for %s",
276 sha1_to_hex(commit->object.sha1));
279 * "commit" is an existing commit. We would want to apply
280 * the difference it introduces since its first parent "prev"
281 * on top of the current HEAD if we are cherry-pick. Or the
282 * reverse of it if we are revert.
285 msg_fd = hold_lock_file_for_update(&msg_file, defmsg, 1);
287 encoding = get_encoding(message);
288 if (!encoding)
289 encoding = "utf-8";
290 if (!git_commit_encoding)
291 git_commit_encoding = "utf-8";
292 if ((reencoded_message = reencode_string(message,
293 git_commit_encoding, encoding)))
294 message = reencoded_message;
296 oneline = get_oneline(message);
298 if (action == REVERT) {
299 char *oneline_body = strchr(oneline, ' ');
301 base = commit;
302 next = commit->parents->item;
303 add_to_msg("Revert \"");
304 add_to_msg(oneline_body + 1);
305 add_to_msg("\"\n\nThis reverts commit ");
306 add_to_msg(sha1_to_hex(commit->object.sha1));
307 add_to_msg(".\n");
308 } else {
309 base = commit->parents->item;
310 next = commit;
311 set_author_ident_env(message);
312 add_message_to_msg(message);
313 if (!replay) {
314 add_to_msg("(cherry picked from commit ");
315 add_to_msg(sha1_to_hex(commit->object.sha1));
316 add_to_msg(")\n");
319 if (needed_deref) {
320 add_to_msg("(original 'git ");
321 add_to_msg(me);
322 add_to_msg("' arguments: ");
323 for (i = 0; i < argc; i++) {
324 if (i)
325 add_to_msg(" ");
326 add_to_msg(argv[i]);
328 add_to_msg(")\n");
331 if (merge_recursive(sha1_to_hex(base->object.sha1),
332 sha1_to_hex(head), "HEAD",
333 sha1_to_hex(next->object.sha1), oneline) ||
334 write_tree(head, 0, NULL)) {
335 add_to_msg("\nConflicts:\n\n");
336 read_cache();
337 for (i = 0; i < active_nr;) {
338 struct cache_entry *ce = active_cache[i++];
339 if (ce_stage(ce)) {
340 add_to_msg("\t");
341 add_to_msg(ce->name);
342 add_to_msg("\n");
343 while (i < active_nr && !strcmp(ce->name,
344 active_cache[i]->name))
345 i++;
348 if (close(msg_fd) || commit_lock_file(&msg_file) < 0)
349 die ("Error wrapping up %s", defmsg);
350 fprintf(stderr, "Automatic %s failed. "
351 "After resolving the conflicts,\n"
352 "mark the corrected paths with 'git-add <paths>'\n"
353 "and commit the result.\n", me);
354 if (action == CHERRY_PICK) {
355 fprintf(stderr, "When commiting, use the option "
356 "'-c %s' to retain authorship and message.\n",
357 find_unique_abbrev(commit->object.sha1,
358 DEFAULT_ABBREV));
360 exit(1);
362 if (close(msg_fd) || commit_lock_file(&msg_file) < 0)
363 die ("Error wrapping up %s", defmsg);
364 fprintf(stderr, "Finished one %s.\n", me);
368 * If we are cherry-pick, and if the merge did not result in
369 * hand-editing, we will hit this commit and inherit the original
370 * author date and name.
371 * If we are revert, or if our cherry-pick results in a hand merge,
372 * we had better say that the current user is responsible for that.
375 if (!no_commit) {
376 if (edit)
377 return execl_git_cmd("commit", "-n", NULL);
378 else
379 return execl_git_cmd("commit", "-n", "-F", defmsg, NULL);
381 if (reencoded_message)
382 free(reencoded_message);
384 return 0;
387 int cmd_revert(int argc, const char **argv, const char *prefix)
389 if (isatty(0))
390 edit = 1;
391 action = REVERT;
392 return revert_or_cherry_pick(argc, argv);
395 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
397 replay = 1;
398 action = CHERRY_PICK;
399 return revert_or_cherry_pick(argc, argv);