Sync with 2.19.6
[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 opterror(opt, "expects a number greater than zero", 0);
73
74         return 0;
75 }
76
77 LAST_ARG_MUST_BE_NULL
78 static void verify_opt_compatible(const char *me, const char *base_opt, ...)
79 {
80         const char *this_opt;
81         va_list ap;
82
83         va_start(ap, base_opt);
84         while ((this_opt = va_arg(ap, const char *))) {
85                 if (va_arg(ap, int))
86                         break;
87         }
88         va_end(ap);
89
90         if (this_opt)
91                 die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
92 }
93
94 static int run_sequencer(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 cmd = 0;
99         struct option base_options[] = {
100                 OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'),
101                 OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'),
102                 OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'),
103                 OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
104                 OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")),
105                 OPT_NOOP_NOARG('r', NULL),
106                 OPT_BOOL('s', "signoff", &opts->signoff, N_("add Signed-off-by:")),
107                 OPT_CALLBACK('m', "mainline", opts, N_("parent-number"),
108                              N_("select mainline parent"), option_parse_m),
109                 OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
110                 OPT_STRING(0, "strategy", &opts->strategy, N_("strategy"), N_("merge strategy")),
111                 OPT_CALLBACK('X', "strategy-option", &opts, N_("option"),
112                         N_("option for merge strategy"), option_parse_x),
113                 { OPTION_STRING, 'S', "gpg-sign", &opts->gpg_sign, N_("key-id"),
114                   N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
115                 OPT_END()
116         };
117         struct option *options = base_options;
118
119         if (opts->action == REPLAY_PICK) {
120                 struct option cp_extra[] = {
121                         OPT_BOOL('x', NULL, &opts->record_origin, N_("append commit name")),
122                         OPT_BOOL(0, "ff", &opts->allow_ff, N_("allow fast-forward")),
123                         OPT_BOOL(0, "allow-empty", &opts->allow_empty, N_("preserve initially empty commits")),
124                         OPT_BOOL(0, "allow-empty-message", &opts->allow_empty_message, N_("allow commits with empty messages")),
125                         OPT_BOOL(0, "keep-redundant-commits", &opts->keep_redundant_commits, N_("keep redundant, empty commits")),
126                         OPT_END(),
127                 };
128                 options = parse_options_concat(options, cp_extra);
129         }
130
131         argc = parse_options(argc, argv, NULL, options, usage_str,
132                         PARSE_OPT_KEEP_ARGV0 |
133                         PARSE_OPT_KEEP_UNKNOWN);
134
135         /* implies allow_empty */
136         if (opts->keep_redundant_commits)
137                 opts->allow_empty = 1;
138
139         /* Check for incompatible command line arguments */
140         if (cmd) {
141                 char *this_operation;
142                 if (cmd == 'q')
143                         this_operation = "--quit";
144                 else if (cmd == 'c')
145                         this_operation = "--continue";
146                 else {
147                         assert(cmd == 'a');
148                         this_operation = "--abort";
149                 }
150
151                 verify_opt_compatible(me, this_operation,
152                                 "--no-commit", opts->no_commit,
153                                 "--signoff", opts->signoff,
154                                 "--mainline", opts->mainline,
155                                 "--strategy", opts->strategy ? 1 : 0,
156                                 "--strategy-option", opts->xopts ? 1 : 0,
157                                 "-x", opts->record_origin,
158                                 "--ff", opts->allow_ff,
159                                 "--rerere-autoupdate", opts->allow_rerere_auto == RERERE_AUTOUPDATE,
160                                 "--no-rerere-autoupdate", opts->allow_rerere_auto == RERERE_NOAUTOUPDATE,
161                                 NULL);
162         }
163
164         if (opts->allow_ff)
165                 verify_opt_compatible(me, "--ff",
166                                 "--signoff", opts->signoff,
167                                 "--no-commit", opts->no_commit,
168                                 "-x", opts->record_origin,
169                                 "--edit", opts->edit,
170                                 NULL);
171
172         if (cmd) {
173                 opts->revs = NULL;
174         } else {
175                 struct setup_revision_opt s_r_opt;
176                 opts->revs = xmalloc(sizeof(*opts->revs));
177                 repo_init_revisions(the_repository, opts->revs, NULL);
178                 opts->revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
179                 if (argc < 2)
180                         usage_with_options(usage_str, options);
181                 if (!strcmp(argv[1], "-"))
182                         argv[1] = "@{-1}";
183                 memset(&s_r_opt, 0, sizeof(s_r_opt));
184                 s_r_opt.assume_dashdash = 1;
185                 argc = setup_revisions(argc, argv, opts->revs, &s_r_opt);
186         }
187
188         if (argc > 1)
189                 usage_with_options(usage_str, options);
190
191         /* These option values will be free()d */
192         opts->gpg_sign = xstrdup_or_null(opts->gpg_sign);
193         opts->strategy = xstrdup_or_null(opts->strategy);
194
195         if (cmd == 'q') {
196                 int ret = sequencer_remove_state(opts);
197                 if (!ret)
198                         remove_branch_state();
199                 return ret;
200         }
201         if (cmd == 'c')
202                 return sequencer_continue(opts);
203         if (cmd == 'a')
204                 return sequencer_rollback(opts);
205         return sequencer_pick_revisions(opts);
206 }
207
208 int cmd_revert(int argc, const char **argv, const char *prefix)
209 {
210         struct replay_opts opts = REPLAY_OPTS_INIT;
211         int res;
212
213         if (isatty(0))
214                 opts.edit = 1;
215         opts.action = REPLAY_REVERT;
216         sequencer_init_config(&opts);
217         res = run_sequencer(argc, argv, &opts);
218         if (res < 0)
219                 die(_("revert failed"));
220         return res;
221 }
222
223 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
224 {
225         struct replay_opts opts = REPLAY_OPTS_INIT;
226         int res;
227
228         opts.action = REPLAY_PICK;
229         sequencer_init_config(&opts);
230         res = run_sequencer(argc, argv, &opts);
231         if (res < 0)
232                 die(_("cherry-pick failed"));
233         return res;
234 }