git-rebase: add keep_empty flag
[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         "git revert [options] <commit-ish>",
23         "git revert <subcommand>",
24         NULL
25 };
26
27 static const char * const cherry_pick_usage[] = {
28         "git cherry-pick [options] <commit-ish>",
29         "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 static void verify_opt_compatible(const char *me, const char *base_opt, ...)
58 {
59         const char *this_opt;
60         va_list ap;
61
62         va_start(ap, base_opt);
63         while ((this_opt = va_arg(ap, const char *))) {
64                 if (va_arg(ap, int))
65                         break;
66         }
67         va_end(ap);
68
69         if (this_opt)
70                 die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
71 }
72
73 static void verify_opt_mutually_compatible(const char *me, ...)
74 {
75         const char *opt1, *opt2 = NULL;
76         va_list ap;
77
78         va_start(ap, me);
79         while ((opt1 = va_arg(ap, const char *))) {
80                 if (va_arg(ap, int))
81                         break;
82         }
83         if (opt1) {
84                 while ((opt2 = va_arg(ap, const char *))) {
85                         if (va_arg(ap, int))
86                                 break;
87                 }
88         }
89
90         if (opt1 && opt2)
91                 die(_("%s: %s cannot be used with %s"), me, opt1, opt2);
92 }
93
94 static void parse_args(int argc, const char **argv, struct replay_opts *opts)
95 {
96         const char * const * usage_str = revert_or_cherry_pick_usage(opts);
97         const char *me = action_name(opts);
98         int remove_state = 0;
99         int contin = 0;
100         int rollback = 0;
101         struct option options[] = {
102                 OPT_BOOLEAN(0, "quit", &remove_state, "end revert or cherry-pick sequence"),
103                 OPT_BOOLEAN(0, "continue", &contin, "resume revert or cherry-pick sequence"),
104                 OPT_BOOLEAN(0, "abort", &rollback, "cancel revert or cherry-pick sequence"),
105                 OPT_BOOLEAN('n', "no-commit", &opts->no_commit, "don't automatically commit"),
106                 OPT_BOOLEAN('e', "edit", &opts->edit, "edit the commit message"),
107                 OPT_NOOP_NOARG('r', NULL),
108                 OPT_BOOLEAN('s', "signoff", &opts->signoff, "add Signed-off-by:"),
109                 OPT_INTEGER('m', "mainline", &opts->mainline, "parent number"),
110                 OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
111                 OPT_STRING(0, "strategy", &opts->strategy, "strategy", "merge strategy"),
112                 OPT_CALLBACK('X', "strategy-option", &opts, "option",
113                         "option for merge strategy", option_parse_x),
114                 OPT_END(),
115                 OPT_END(),
116                 OPT_END(),
117                 OPT_END(),
118                 OPT_END(),
119         };
120
121         if (opts->action == REPLAY_PICK) {
122                 struct option cp_extra[] = {
123                         OPT_BOOLEAN('x', NULL, &opts->record_origin, "append commit name"),
124                         OPT_BOOLEAN(0, "ff", &opts->allow_ff, "allow fast-forward"),
125                         OPT_BOOLEAN(0, "allow-empty", &opts->allow_empty, "preserve initially empty commits"),
126                         OPT_BOOLEAN(0, "keep-redundant-commits", &opts->keep_redundant_commits, "keep redundant, empty commits"),
127                         OPT_END(),
128                 };
129                 if (parse_options_concat(options, ARRAY_SIZE(options), cp_extra))
130                         die(_("program error"));
131         }
132
133         argc = parse_options(argc, argv, NULL, options, usage_str,
134                         PARSE_OPT_KEEP_ARGV0 |
135                         PARSE_OPT_KEEP_UNKNOWN);
136
137         /* Check for incompatible subcommands */
138         verify_opt_mutually_compatible(me,
139                                 "--quit", remove_state,
140                                 "--continue", contin,
141                                 "--abort", rollback,
142                                 NULL);
143
144         /* implies allow_empty */
145         if (opts->keep_redundant_commits)
146                 opts->allow_empty = 1;
147
148         /* Set the subcommand */
149         if (remove_state)
150                 opts->subcommand = REPLAY_REMOVE_STATE;
151         else if (contin)
152                 opts->subcommand = REPLAY_CONTINUE;
153         else if (rollback)
154                 opts->subcommand = REPLAY_ROLLBACK;
155         else
156                 opts->subcommand = REPLAY_NONE;
157
158         /* Check for incompatible command line arguments */
159         if (opts->subcommand != REPLAY_NONE) {
160                 char *this_operation;
161                 if (opts->subcommand == REPLAY_REMOVE_STATE)
162                         this_operation = "--quit";
163                 else if (opts->subcommand == REPLAY_CONTINUE)
164                         this_operation = "--continue";
165                 else {
166                         assert(opts->subcommand == REPLAY_ROLLBACK);
167                         this_operation = "--abort";
168                 }
169
170                 verify_opt_compatible(me, this_operation,
171                                 "--no-commit", opts->no_commit,
172                                 "--signoff", opts->signoff,
173                                 "--mainline", opts->mainline,
174                                 "--strategy", opts->strategy ? 1 : 0,
175                                 "--strategy-option", opts->xopts ? 1 : 0,
176                                 "-x", opts->record_origin,
177                                 "--ff", opts->allow_ff,
178                                 NULL);
179         }
180
181         if (opts->allow_ff)
182                 verify_opt_compatible(me, "--ff",
183                                 "--signoff", opts->signoff,
184                                 "--no-commit", opts->no_commit,
185                                 "-x", opts->record_origin,
186                                 "--edit", opts->edit,
187                                 NULL);
188
189         if (opts->subcommand != REPLAY_NONE) {
190                 opts->revs = NULL;
191         } else {
192                 opts->revs = xmalloc(sizeof(*opts->revs));
193                 init_revisions(opts->revs, NULL);
194                 opts->revs->no_walk = 1;
195                 if (argc < 2)
196                         usage_with_options(usage_str, options);
197                 argc = setup_revisions(argc, argv, opts->revs, NULL);
198         }
199
200         if (argc > 1)
201                 usage_with_options(usage_str, options);
202 }
203
204 int cmd_revert(int argc, const char **argv, const char *prefix)
205 {
206         struct replay_opts opts;
207         int res;
208
209         memset(&opts, 0, sizeof(opts));
210         if (isatty(0))
211                 opts.edit = 1;
212         opts.action = REPLAY_REVERT;
213         git_config(git_default_config, NULL);
214         parse_args(argc, argv, &opts);
215         res = sequencer_pick_revisions(&opts);
216         if (res < 0)
217                 die(_("revert failed"));
218         return res;
219 }
220
221 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
222 {
223         struct replay_opts opts;
224         int res;
225
226         memset(&opts, 0, sizeof(opts));
227         opts.action = REPLAY_PICK;
228         git_config(git_default_config, NULL);
229         parse_args(argc, argv, &opts);
230         res = sequencer_pick_revisions(&opts);
231         if (res < 0)
232                 die(_("cherry-pick failed"));
233         return res;
234 }