cherry-pick: remember rerere-autoupdate
[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 void parse_args(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
80         struct option options[] = {
81                 OPT__QUIET(&opts->quiet, N_("suppress progress reporting")),
82                 OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'),
83                 OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'),
84                 OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'),
85                 OPT_CMDMODE(0, "skip", &cmd, N_("skip current commit in the sequence"), 's'),
86                 OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
87                 OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")),
88                 OPT_NOOP_NOARG('r', NULL),
89                 OPT_BOOL('s', "signoff", &opts->signoff, N_("add Signed-off-by:")),
90                 OPT_INTEGER('m', "mainline", &opts->mainline, N_("parent number")),
91                 OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
92                 OPT_STRING(0, "strategy", &opts->strategy, N_("strategy"), N_("merge strategy")),
93                 OPT_CALLBACK('X', "strategy-option", &opts, N_("option"),
94                         N_("option for merge strategy"), option_parse_x),
95                 { OPTION_STRING, 'S', "gpg-sign", &opts->gpg_sign, N_("key-id"),
96                   N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
97                 OPT_END(),
98                 OPT_END(),
99                 OPT_END(),
100                 OPT_END(),
101                 OPT_END(),
102                 OPT_END(),
103                 OPT_END(),
104         };
105
106         if (opts->action == REPLAY_PICK) {
107                 struct option cp_extra[] = {
108                         OPT_BOOL('x', NULL, &opts->record_origin, N_("append commit name")),
109                         OPT_BOOL(0, "ff", &opts->allow_ff, N_("allow fast-forward")),
110                         OPT_BOOL(0, "allow-empty", &opts->allow_empty, N_("preserve initially empty commits")),
111                         OPT_BOOL(0, "allow-empty-message", &opts->allow_empty_message, N_("allow commits with empty messages")),
112                         OPT_BOOL(0, "keep-redundant-commits", &opts->keep_redundant_commits, N_("keep redundant, empty commits")),
113                         OPT_BOOL(0, "skip-empty", &opts->skip_empty, N_("skip empty commits")),
114                         OPT_END(),
115                 };
116                 if (parse_options_concat(options, ARRAY_SIZE(options), cp_extra))
117                         die(_("program error"));
118         }
119
120         argc = parse_options(argc, argv, NULL, options, usage_str,
121                         PARSE_OPT_KEEP_ARGV0 |
122                         PARSE_OPT_KEEP_UNKNOWN);
123
124         /* implies allow_empty */
125         if (opts->keep_redundant_commits)
126                 opts->allow_empty = 1;
127
128         /* Set the subcommand */
129         if (cmd == 'q')
130                 opts->subcommand = REPLAY_REMOVE_STATE;
131         else if (cmd == 'c')
132                 opts->subcommand = REPLAY_CONTINUE;
133         else if (cmd == 'a')
134                 opts->subcommand = REPLAY_ROLLBACK;
135         else if (cmd == 's')
136                 opts->subcommand = REPLAY_SKIP;
137         else
138                 opts->subcommand = REPLAY_NONE;
139
140         /* Check for incompatible command line arguments */
141         if (opts->subcommand != REPLAY_NONE) {
142                 char *this_operation;
143                 if (opts->subcommand == REPLAY_REMOVE_STATE)
144                         this_operation = "--quit";
145                 else if (opts->subcommand == REPLAY_CONTINUE)
146                         this_operation = "--continue";
147                 else if (opts->subcommand == REPLAY_SKIP)
148                         this_operation = "--skip";
149                 else {
150                         assert(opts->subcommand == REPLAY_ROLLBACK);
151                         this_operation = "--abort";
152                 }
153
154                 verify_opt_compatible(me, this_operation,
155                                 "--no-commit", opts->no_commit,
156                                 "--signoff", opts->signoff,
157                                 "--mainline", opts->mainline,
158                                 "--strategy", opts->strategy ? 1 : 0,
159                                 "--strategy-option", opts->xopts ? 1 : 0,
160                                 "-x", opts->record_origin,
161                                 "--ff", opts->allow_ff,
162                                 NULL);
163         }
164
165         if (opts->allow_ff)
166                 verify_opt_compatible(me, "--ff",
167                                 "--signoff", opts->signoff,
168                                 "--no-commit", opts->no_commit,
169                                 "-x", opts->record_origin,
170                                 "--edit", opts->edit,
171                                 NULL);
172
173         if (opts->subcommand != REPLAY_NONE) {
174                 opts->revs = NULL;
175         } else {
176                 struct setup_revision_opt s_r_opt;
177                 opts->revs = xmalloc(sizeof(*opts->revs));
178                 init_revisions(opts->revs, NULL);
179                 opts->revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
180                 if (argc < 2)
181                         usage_with_options(usage_str, options);
182                 if (!strcmp(argv[1], "-"))
183                         argv[1] = "@{-1}";
184                 memset(&s_r_opt, 0, sizeof(s_r_opt));
185                 s_r_opt.assume_dashdash = 1;
186                 argc = setup_revisions(argc, argv, opts->revs, &s_r_opt);
187         }
188
189         if (argc > 1)
190                 usage_with_options(usage_str, options);
191 }
192
193 int cmd_revert(int argc, const char **argv, const char *prefix)
194 {
195         struct replay_opts opts;
196         int res;
197
198         memset(&opts, 0, sizeof(opts));
199         if (isatty(0))
200                 opts.edit = 1;
201         opts.action = REPLAY_REVERT;
202         git_config(git_default_config, NULL);
203         parse_args(argc, argv, &opts);
204         res = sequencer_pick_revisions(&opts);
205         if (res < 0)
206                 die(_("revert failed"));
207         return res;
208 }
209
210 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
211 {
212         struct replay_opts opts;
213         int res;
214
215         memset(&opts, 0, sizeof(opts));
216         opts.action = REPLAY_PICK;
217         git_config(git_default_config, NULL);
218         parse_args(argc, argv, &opts);
219         res = sequencer_pick_revisions(&opts);
220         if (res < 0)
221                 die(_("cherry-pick failed"));
222         return res;
223 }