Merge branch 'js/rebase-reschedule-applies-only-to-interactive' into maint
[git] / builtin / revert.c
1 #include "cache.h"
2 #include "config.h"
3 #include "builtin.h"
4 #include "parse-options.h"
5 #include "diff.h"
6 #include "revision.h"
7 #include "rerere.h"
8 #include "dir.h"
9 #include "sequencer.h"
10 #include "branch.h"
11
12 /*
13  * This implements the builtins revert and cherry-pick.
14  *
15  * Copyright (c) 2007 Johannes E. Schindelin
16  *
17  * Based on git-revert.sh, which is
18  *
19  * Copyright (c) 2005 Linus Torvalds
20  * Copyright (c) 2005 Junio C Hamano
21  */
22
23 static const char * const revert_usage[] = {
24         N_("git revert [<options>] <commit-ish>..."),
25         N_("git revert <subcommand>"),
26         NULL
27 };
28
29 static const char * const cherry_pick_usage[] = {
30         N_("git cherry-pick [<options>] <commit-ish>..."),
31         N_("git cherry-pick <subcommand>"),
32         NULL
33 };
34
35 static const char *action_name(const struct replay_opts *opts)
36 {
37         return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
38 }
39
40 static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts)
41 {
42         return opts->action == REPLAY_REVERT ? revert_usage : cherry_pick_usage;
43 }
44
45 static int option_parse_x(const struct option *opt,
46                           const char *arg, int unset)
47 {
48         struct replay_opts **opts_ptr = opt->value;
49         struct replay_opts *opts = *opts_ptr;
50
51         if (unset)
52                 return 0;
53
54         ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
55         opts->xopts[opts->xopts_nr++] = xstrdup(arg);
56         return 0;
57 }
58
59 static int option_parse_m(const struct option *opt,
60                           const char *arg, int unset)
61 {
62         struct replay_opts *replay = opt->value;
63         char *end;
64
65         if (unset) {
66                 replay->mainline = 0;
67                 return 0;
68         }
69
70         replay->mainline = strtol(arg, &end, 10);
71         if (*end || replay->mainline <= 0)
72                 return error(_("option `%s' expects a number greater than zero"),
73                              opt->long_name);
74
75         return 0;
76 }
77
78 LAST_ARG_MUST_BE_NULL
79 static void verify_opt_compatible(const char *me, const char *base_opt, ...)
80 {
81         const char *this_opt;
82         va_list ap;
83
84         va_start(ap, base_opt);
85         while ((this_opt = va_arg(ap, const char *))) {
86                 if (va_arg(ap, int))
87                         break;
88         }
89         va_end(ap);
90
91         if (this_opt)
92                 die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
93 }
94
95 static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
96 {
97         const char * const * usage_str = revert_or_cherry_pick_usage(opts);
98         const char *me = action_name(opts);
99         const char *cleanup_arg = NULL;
100         int cmd = 0;
101         struct option base_options[] = {
102                 OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'),
103                 OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'),
104                 OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'),
105                 OPT_CLEANUP(&cleanup_arg),
106                 OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
107                 OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")),
108                 OPT_NOOP_NOARG('r', NULL),
109                 OPT_BOOL('s', "signoff", &opts->signoff, N_("add Signed-off-by:")),
110                 OPT_CALLBACK('m', "mainline", opts, N_("parent-number"),
111                              N_("select mainline parent"), option_parse_m),
112                 OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
113                 OPT_STRING(0, "strategy", &opts->strategy, N_("strategy"), N_("merge strategy")),
114                 OPT_CALLBACK('X', "strategy-option", &opts, N_("option"),
115                         N_("option for merge strategy"), option_parse_x),
116                 { OPTION_STRING, 'S', "gpg-sign", &opts->gpg_sign, N_("key-id"),
117                   N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
118                 OPT_END()
119         };
120         struct option *options = base_options;
121
122         if (opts->action == REPLAY_PICK) {
123                 struct option cp_extra[] = {
124                         OPT_BOOL('x', NULL, &opts->record_origin, N_("append commit name")),
125                         OPT_BOOL(0, "ff", &opts->allow_ff, N_("allow fast-forward")),
126                         OPT_BOOL(0, "allow-empty", &opts->allow_empty, N_("preserve initially empty commits")),
127                         OPT_BOOL(0, "allow-empty-message", &opts->allow_empty_message, N_("allow commits with empty messages")),
128                         OPT_BOOL(0, "keep-redundant-commits", &opts->keep_redundant_commits, N_("keep redundant, empty commits")),
129                         OPT_END(),
130                 };
131                 options = parse_options_concat(options, cp_extra);
132         }
133
134         argc = parse_options(argc, argv, NULL, options, usage_str,
135                         PARSE_OPT_KEEP_ARGV0 |
136                         PARSE_OPT_KEEP_UNKNOWN);
137
138         /* implies allow_empty */
139         if (opts->keep_redundant_commits)
140                 opts->allow_empty = 1;
141
142         if (cleanup_arg) {
143                 opts->default_msg_cleanup = get_cleanup_mode(cleanup_arg, 1);
144                 opts->explicit_cleanup = 1;
145         }
146
147         /* Check for incompatible command line arguments */
148         if (cmd) {
149                 char *this_operation;
150                 if (cmd == 'q')
151                         this_operation = "--quit";
152                 else if (cmd == 'c')
153                         this_operation = "--continue";
154                 else {
155                         assert(cmd == 'a');
156                         this_operation = "--abort";
157                 }
158
159                 verify_opt_compatible(me, this_operation,
160                                 "--no-commit", opts->no_commit,
161                                 "--signoff", opts->signoff,
162                                 "--mainline", opts->mainline,
163                                 "--strategy", opts->strategy ? 1 : 0,
164                                 "--strategy-option", opts->xopts ? 1 : 0,
165                                 "-x", opts->record_origin,
166                                 "--ff", opts->allow_ff,
167                                 "--rerere-autoupdate", opts->allow_rerere_auto == RERERE_AUTOUPDATE,
168                                 "--no-rerere-autoupdate", opts->allow_rerere_auto == RERERE_NOAUTOUPDATE,
169                                 NULL);
170         }
171
172         if (opts->allow_ff)
173                 verify_opt_compatible(me, "--ff",
174                                 "--signoff", opts->signoff,
175                                 "--no-commit", opts->no_commit,
176                                 "-x", opts->record_origin,
177                                 "--edit", opts->edit,
178                                 NULL);
179
180         if (cmd) {
181                 opts->revs = NULL;
182         } else {
183                 struct setup_revision_opt s_r_opt;
184                 opts->revs = xmalloc(sizeof(*opts->revs));
185                 repo_init_revisions(the_repository, opts->revs, NULL);
186                 opts->revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
187                 if (argc < 2)
188                         usage_with_options(usage_str, options);
189                 if (!strcmp(argv[1], "-"))
190                         argv[1] = "@{-1}";
191                 memset(&s_r_opt, 0, sizeof(s_r_opt));
192                 s_r_opt.assume_dashdash = 1;
193                 argc = setup_revisions(argc, argv, opts->revs, &s_r_opt);
194         }
195
196         if (argc > 1)
197                 usage_with_options(usage_str, options);
198
199         /* These option values will be free()d */
200         opts->gpg_sign = xstrdup_or_null(opts->gpg_sign);
201         opts->strategy = xstrdup_or_null(opts->strategy);
202
203         if (cmd == 'q') {
204                 int ret = sequencer_remove_state(opts);
205                 if (!ret)
206                         remove_branch_state(the_repository);
207                 return ret;
208         }
209         if (cmd == 'c')
210                 return sequencer_continue(the_repository, opts);
211         if (cmd == 'a')
212                 return sequencer_rollback(the_repository, opts);
213         return sequencer_pick_revisions(the_repository, opts);
214 }
215
216 int cmd_revert(int argc, const char **argv, const char *prefix)
217 {
218         struct replay_opts opts = REPLAY_OPTS_INIT;
219         int res;
220
221         if (isatty(0))
222                 opts.edit = 1;
223         opts.action = REPLAY_REVERT;
224         sequencer_init_config(&opts);
225         res = run_sequencer(argc, argv, &opts);
226         if (res < 0)
227                 die(_("revert failed"));
228         return res;
229 }
230
231 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
232 {
233         struct replay_opts opts = REPLAY_OPTS_INIT;
234         int res;
235
236         opts.action = REPLAY_PICK;
237         sequencer_init_config(&opts);
238         res = run_sequencer(argc, argv, &opts);
239         if (res < 0)
240                 die(_("cherry-pick failed"));
241         return res;
242 }