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