]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/revert.c
ci: deprecate ci/config/allow-ref script
[thirdparty/git.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 [--[no-]edit] [-n] [-m <parent-number>] [-s] [-S[<keyid>]] <commit>..."),
25 N_("git revert (--continue | --skip | --abort | --quit)"),
26 NULL
27 };
28
29 static const char * const cherry_pick_usage[] = {
30 N_("git cherry-pick [--edit] [-n] [-m <parent-number>] [-s] [-x] [--ff]\n"
31 " [-S[<keyid>]] <commit>..."),
32 N_("git cherry-pick (--continue | --skip | --abort | --quit)"),
33 NULL
34 };
35
36 static const char *action_name(const struct replay_opts *opts)
37 {
38 return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
39 }
40
41 static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts)
42 {
43 return opts->action == REPLAY_REVERT ? revert_usage : cherry_pick_usage;
44 }
45
46 static int option_parse_x(const struct option *opt,
47 const char *arg, int unset)
48 {
49 struct replay_opts **opts_ptr = opt->value;
50 struct replay_opts *opts = *opts_ptr;
51
52 if (unset)
53 return 0;
54
55 ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
56 opts->xopts[opts->xopts_nr++] = xstrdup(arg);
57 return 0;
58 }
59
60 static int option_parse_m(const struct option *opt,
61 const char *arg, int unset)
62 {
63 struct replay_opts *replay = opt->value;
64 char *end;
65
66 if (unset) {
67 replay->mainline = 0;
68 return 0;
69 }
70
71 replay->mainline = strtol(arg, &end, 10);
72 if (*end || replay->mainline <= 0)
73 return error(_("option `%s' expects a number greater than zero"),
74 opt->long_name);
75
76 return 0;
77 }
78
79 LAST_ARG_MUST_BE_NULL
80 static void verify_opt_compatible(const char *me, const char *base_opt, ...)
81 {
82 const char *this_opt;
83 va_list ap;
84
85 va_start(ap, base_opt);
86 while ((this_opt = va_arg(ap, const char *))) {
87 if (va_arg(ap, int))
88 break;
89 }
90 va_end(ap);
91
92 if (this_opt)
93 die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
94 }
95
96 static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
97 {
98 const char * const * usage_str = revert_or_cherry_pick_usage(opts);
99 const char *me = action_name(opts);
100 const char *cleanup_arg = NULL;
101 int cmd = 0;
102 struct option base_options[] = {
103 OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'),
104 OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'),
105 OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'),
106 OPT_CMDMODE(0, "skip", &cmd, N_("skip current commit and continue"), 's'),
107 OPT_CLEANUP(&cleanup_arg),
108 OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
109 OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")),
110 OPT_NOOP_NOARG('r', NULL),
111 OPT_BOOL('s', "signoff", &opts->signoff, N_("add a Signed-off-by trailer")),
112 OPT_CALLBACK('m', "mainline", opts, N_("parent-number"),
113 N_("select mainline parent"), option_parse_m),
114 OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
115 OPT_STRING(0, "strategy", &opts->strategy, N_("strategy"), N_("merge strategy")),
116 OPT_CALLBACK('X', "strategy-option", &opts, N_("option"),
117 N_("option for merge strategy"), option_parse_x),
118 { OPTION_STRING, 'S', "gpg-sign", &opts->gpg_sign, N_("key-id"),
119 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
120 OPT_END()
121 };
122 struct option *options = base_options;
123
124 if (opts->action == REPLAY_PICK) {
125 struct option cp_extra[] = {
126 OPT_BOOL('x', NULL, &opts->record_origin, N_("append commit name")),
127 OPT_BOOL(0, "ff", &opts->allow_ff, N_("allow fast-forward")),
128 OPT_BOOL(0, "allow-empty", &opts->allow_empty, N_("preserve initially empty commits")),
129 OPT_BOOL(0, "allow-empty-message", &opts->allow_empty_message, N_("allow commits with empty messages")),
130 OPT_BOOL(0, "keep-redundant-commits", &opts->keep_redundant_commits, N_("keep redundant, empty commits")),
131 OPT_END(),
132 };
133 options = parse_options_concat(options, cp_extra);
134 } else if (opts->action == REPLAY_REVERT) {
135 struct option cp_extra[] = {
136 OPT_BOOL(0, "reference", &opts->commit_use_reference,
137 N_("use the 'reference' format to refer to commits")),
138 OPT_END(),
139 };
140 options = parse_options_concat(options, cp_extra);
141 }
142
143 argc = parse_options(argc, argv, NULL, options, usage_str,
144 PARSE_OPT_KEEP_ARGV0 |
145 PARSE_OPT_KEEP_UNKNOWN_OPT);
146
147 prepare_repo_settings(the_repository);
148 the_repository->settings.command_requires_full_index = 0;
149
150 /* implies allow_empty */
151 if (opts->keep_redundant_commits)
152 opts->allow_empty = 1;
153
154 if (cleanup_arg) {
155 opts->default_msg_cleanup = get_cleanup_mode(cleanup_arg, 1);
156 opts->explicit_cleanup = 1;
157 }
158
159 /* Check for incompatible command line arguments */
160 if (cmd) {
161 char *this_operation;
162 if (cmd == 'q')
163 this_operation = "--quit";
164 else if (cmd == 'c')
165 this_operation = "--continue";
166 else if (cmd == 's')
167 this_operation = "--skip";
168 else {
169 assert(cmd == 'a');
170 this_operation = "--abort";
171 }
172
173 verify_opt_compatible(me, this_operation,
174 "--no-commit", opts->no_commit,
175 "--signoff", opts->signoff,
176 "--mainline", opts->mainline,
177 "--strategy", opts->strategy ? 1 : 0,
178 "--strategy-option", opts->xopts ? 1 : 0,
179 "-x", opts->record_origin,
180 "--ff", opts->allow_ff,
181 "--rerere-autoupdate", opts->allow_rerere_auto == RERERE_AUTOUPDATE,
182 "--no-rerere-autoupdate", opts->allow_rerere_auto == RERERE_NOAUTOUPDATE,
183 NULL);
184 }
185
186 if (!opts->strategy && opts->default_strategy) {
187 opts->strategy = opts->default_strategy;
188 opts->default_strategy = NULL;
189 }
190
191 if (opts->allow_ff)
192 verify_opt_compatible(me, "--ff",
193 "--signoff", opts->signoff,
194 "--no-commit", opts->no_commit,
195 "-x", opts->record_origin,
196 "--edit", opts->edit > 0,
197 NULL);
198
199 if (cmd) {
200 opts->revs = NULL;
201 } else {
202 struct setup_revision_opt s_r_opt;
203 opts->revs = xmalloc(sizeof(*opts->revs));
204 repo_init_revisions(the_repository, opts->revs, NULL);
205 opts->revs->no_walk = 1;
206 opts->revs->unsorted_input = 1;
207 if (argc < 2)
208 usage_with_options(usage_str, options);
209 if (!strcmp(argv[1], "-"))
210 argv[1] = "@{-1}";
211 memset(&s_r_opt, 0, sizeof(s_r_opt));
212 s_r_opt.assume_dashdash = 1;
213 argc = setup_revisions(argc, argv, opts->revs, &s_r_opt);
214 }
215
216 if (argc > 1)
217 usage_with_options(usage_str, options);
218
219 /* These option values will be free()d */
220 opts->gpg_sign = xstrdup_or_null(opts->gpg_sign);
221 opts->strategy = xstrdup_or_null(opts->strategy);
222 if (!opts->strategy && getenv("GIT_TEST_MERGE_ALGORITHM"))
223 opts->strategy = xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
224 free(options);
225
226 if (cmd == 'q') {
227 int ret = sequencer_remove_state(opts);
228 if (!ret)
229 remove_branch_state(the_repository, 0);
230 return ret;
231 }
232 if (cmd == 'c')
233 return sequencer_continue(the_repository, opts);
234 if (cmd == 'a')
235 return sequencer_rollback(the_repository, opts);
236 if (cmd == 's')
237 return sequencer_skip(the_repository, opts);
238 return sequencer_pick_revisions(the_repository, opts);
239 }
240
241 int cmd_revert(int argc, const char **argv, const char *prefix)
242 {
243 struct replay_opts opts = REPLAY_OPTS_INIT;
244 int res;
245
246 opts.action = REPLAY_REVERT;
247 sequencer_init_config(&opts);
248 res = run_sequencer(argc, argv, &opts);
249 if (res < 0)
250 die(_("revert failed"));
251 replay_opts_release(&opts);
252 return res;
253 }
254
255 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
256 {
257 struct replay_opts opts = REPLAY_OPTS_INIT;
258 int res;
259
260 opts.action = REPLAY_PICK;
261 sequencer_init_config(&opts);
262 res = run_sequencer(argc, argv, &opts);
263 if (res < 0)
264 die(_("cherry-pick failed"));
265 replay_opts_release(&opts);
266 return res;
267 }