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