interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / builtin / revert.c

324 lines · 9544 bytes Raw Blame History
1#define USE_THE_REPOSITORY_VARIABLE
2
3#include "git-compat-util.h"
4#include "builtin.h"
5#include "parse-options.h"
6#include "diff.h"
7#include "environment.h"
8#include "gettext.h"
9#include "revision.h"
10#include "rerere.h"
11#include "run-command.h"
12#include "sequencer.h"
13#include "branch.h"
14
15/*
16 * This implements the builtins revert and cherry-pick.
17 *
18 * Copyright (c) 2007 Johannes E. Schindelin
19 *
20 * Based on git-revert.sh, which is
21 *
22 * Copyright (c) 2005 Linus Torvalds
23 * Copyright (c) 2005 Junio C Hamano
24 */
25
26static const char * const revert_usage[] = {
27 N_("git revert [--[no-]edit] [-n] [-m <parent-number>] [-s] [-S[<keyid>]] <commit>..."),
28 N_("git revert (--continue | --skip | --abort | --quit)"),
29 NULL
30};
31
32static const char * const cherry_pick_usage[] = {
33 N_("git cherry-pick [--edit] [-n] [-m <parent-number>] [-s] [-x] [--ff]\n"
34 " [-S[<keyid>]] <commit>..."),
35 N_("git cherry-pick (--continue | --skip | --abort | --quit)"),
36 NULL
37};
38
39static const char *action_name(const struct replay_opts *opts)
40{
41 return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
42}
43
44static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts)
45{
46 return opts->action == REPLAY_REVERT ? revert_usage : cherry_pick_usage;
47}
48
49enum empty_action {
50 EMPTY_COMMIT_UNSPECIFIED = -1,
51 STOP_ON_EMPTY_COMMIT, /* output errors and stop in the middle of a cherry-pick */
52 DROP_EMPTY_COMMIT, /* skip with a notice message */
53 KEEP_EMPTY_COMMIT, /* keep recording as empty commits */
54};
55
56static int parse_opt_empty(const struct option *opt, const char *arg, int unset)
57{
58 int *opt_value = opt->value;
59
60 BUG_ON_OPT_NEG(unset);
61
62 if (!strcmp(arg, "stop"))
63 *opt_value = STOP_ON_EMPTY_COMMIT;
64 else if (!strcmp(arg, "drop"))
65 *opt_value = DROP_EMPTY_COMMIT;
66 else if (!strcmp(arg, "keep"))
67 *opt_value = KEEP_EMPTY_COMMIT;
68 else
69 return error(_("invalid value for '%s': '%s'"), "--empty", arg);
70
71 return 0;
72}
73
74static int option_parse_m(const struct option *opt,
75 const char *arg, int unset)
76{
77 struct replay_opts *replay = opt->value;
78 char *end;
79
80 if (unset) {
81 replay->mainline = 0;
82 return 0;
83 }
84
85 replay->mainline = strtol(arg, &end, 10);
86 if (*end || replay->mainline <= 0)
87 return error(_("option `%s' expects a number greater than zero"),
88 opt->long_name);
89
90 return 0;
91}
92
93LAST_ARG_MUST_BE_NULL
94static void verify_opt_compatible(const char *me, const char *base_opt, ...)
95{
96 const char *this_opt;
97 va_list ap;
98
99 va_start(ap, base_opt);
100 while ((this_opt = va_arg(ap, const char *))) {
101 if (va_arg(ap, int))
102 break;
103 }
104 va_end(ap);
105
106 if (this_opt)
107 die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
108}
109
110static int run_sequencer(int argc, const char **argv, const char *prefix,
111 struct replay_opts *opts)
112{
113 const char * const * usage_str = revert_or_cherry_pick_usage(opts);
114 const char *me = action_name(opts);
115 const char *cleanup_arg = NULL;
116 const char sentinel_value = 0; /* value not important */
117 const char *strategy = &sentinel_value;
118 const char *gpg_sign = &sentinel_value;
119 enum empty_action empty_opt = EMPTY_COMMIT_UNSPECIFIED;
120 int cmd = 0, ret;
121 struct option base_options[] = {
122 OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'),
123 OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'),
124 OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'),
125 OPT_CMDMODE(0, "skip", &cmd, N_("skip current commit and continue"), 's'),
126 OPT_CLEANUP(&cleanup_arg),
127 OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
128 OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")),
129 OPT_NOOP_NOARG('r', NULL),
130 OPT_BOOL('s', "signoff", &opts->signoff, N_("add a Signed-off-by trailer")),
131 OPT_CALLBACK('m', "mainline", opts, N_("parent-number"),
132 N_("select mainline parent"), option_parse_m),
133 OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
134 OPT_STRING(0, "strategy", &strategy, N_("strategy"), N_("merge strategy")),
135 OPT_STRVEC('X', "strategy-option", &opts->xopts, N_("option"),
136 N_("option for merge strategy")),
137 {
138 .type = OPTION_STRING,
139 .short_name = 'S',
140 .long_name = "gpg-sign",
141 .value = &gpg_sign,
142 .argh = N_("key-id"),
143 .help = N_("GPG sign commit"),
144 .flags = PARSE_OPT_OPTARG,
145 .defval = (intptr_t) "",
146 },
147 OPT_END()
148 };
149 struct option *options = base_options;
150
151 if (opts->action == REPLAY_PICK) {
152 struct option cp_extra[] = {
153 OPT_BOOL('x', NULL, &opts->record_origin, N_("append commit name")),
154 OPT_BOOL(0, "ff", &opts->allow_ff, N_("allow fast-forward")),
155 OPT_BOOL(0, "allow-empty", &opts->allow_empty, N_("preserve initially empty commits")),
156 OPT_BOOL(0, "allow-empty-message", &opts->allow_empty_message, N_("allow commits with empty messages")),
157 OPT_BOOL(0, "keep-redundant-commits", &opts->keep_redundant_commits, N_("deprecated: use --empty=keep instead")),
158 OPT_CALLBACK_F(0, "empty", &empty_opt, "(stop|drop|keep)",
159 N_("how to handle commits that become empty"),
160 PARSE_OPT_NONEG, parse_opt_empty),
161 OPT_END(),
162 };
163 options = parse_options_concat(options, cp_extra);
164 } else if (opts->action == REPLAY_REVERT) {
165 struct option cp_extra[] = {
166 OPT_BOOL(0, "reference", &opts->commit_use_reference,
167 N_("use the 'reference' format to refer to commits")),
168 OPT_END(),
169 };
170 options = parse_options_concat(options, cp_extra);
171 }
172
173 argc = parse_options(argc, argv, prefix, options, usage_str,
174 PARSE_OPT_KEEP_ARGV0 |
175 PARSE_OPT_KEEP_UNKNOWN_OPT);
176
177 prepare_repo_settings(the_repository);
178 the_repository->settings.command_requires_full_index = 0;
179
180 if (opts->action == REPLAY_PICK) {
181 opts->drop_redundant_commits = (empty_opt == DROP_EMPTY_COMMIT);
182 opts->keep_redundant_commits = opts->keep_redundant_commits || (empty_opt == KEEP_EMPTY_COMMIT);
183 }
184
185 /* implies allow_empty */
186 if (opts->keep_redundant_commits)
187 opts->allow_empty = 1;
188
189 if (cleanup_arg) {
190 opts->default_msg_cleanup = get_cleanup_mode(cleanup_arg, 1);
191 opts->explicit_cleanup = 1;
192 }
193
194 /* Check for incompatible command line arguments */
195 if (cmd) {
196 const char *this_operation;
197 if (cmd == 'q')
198 this_operation = "--quit";
199 else if (cmd == 'c')
200 this_operation = "--continue";
201 else if (cmd == 's')
202 this_operation = "--skip";
203 else {
204 assert(cmd == 'a');
205 this_operation = "--abort";
206 }
207
208 verify_opt_compatible(me, this_operation,
209 "--no-commit", opts->no_commit,
210 "--signoff", opts->signoff,
211 "--mainline", opts->mainline,
212 "--strategy", opts->strategy ? 1 : 0,
213 "--strategy-option", opts->xopts.nr ? 1 : 0,
214 "-x", opts->record_origin,
215 "--ff", opts->allow_ff,
216 "--rerere-autoupdate", opts->allow_rerere_auto == RERERE_AUTOUPDATE,
217 "--no-rerere-autoupdate", opts->allow_rerere_auto == RERERE_NOAUTOUPDATE,
218 "--keep-redundant-commits", opts->keep_redundant_commits,
219 "--empty", empty_opt != EMPTY_COMMIT_UNSPECIFIED,
220 NULL);
221 }
222
223 if (!opts->strategy && opts->default_strategy) {
224 opts->strategy = opts->default_strategy;
225 opts->default_strategy = NULL;
226 }
227
228 if (opts->allow_ff)
229 verify_opt_compatible(me, "--ff",
230 "--signoff", opts->signoff,
231 "--no-commit", opts->no_commit,
232 "-x", opts->record_origin,
233 "--edit", opts->edit > 0,
234 NULL);
235
236 if (cmd) {
237 opts->revs = NULL;
238 } else {
239 struct setup_revision_opt s_r_opt;
240 opts->revs = xmalloc(sizeof(*opts->revs));
241 repo_init_revisions(the_repository, opts->revs, NULL);
242 opts->revs->no_walk = 1;
243 opts->revs->unsorted_input = 1;
244 if (argc < 2)
245 usage_with_options(usage_str, options);
246 if (!strcmp(argv[1], "-"))
247 argv[1] = "@{-1}";
248 memset(&s_r_opt, 0, sizeof(s_r_opt));
249 s_r_opt.assume_dashdash = 1;
250 argc = setup_revisions(argc, argv, opts->revs, &s_r_opt);
251 }
252
253 if (argc > 1)
254 usage_with_options(usage_str, options);
255
256 /* These option values will be free()d */
257 if (gpg_sign != &sentinel_value) {
258 free(opts->gpg_sign);
259 opts->gpg_sign = xstrdup_or_null(gpg_sign);
260 }
261 if (strategy != &sentinel_value) {
262 free(opts->strategy);
263 opts->strategy = xstrdup_or_null(strategy);
264 }
265 free(options);
266
267 if (cmd == 'q') {
268 ret = sequencer_remove_state(opts);
269 if (!ret)
270 remove_branch_state(the_repository, 0);
271 return ret;
272 }
273 if (cmd == 'a')
274 return sequencer_rollback(the_repository, opts);
275 if (cmd == 'c')
276 ret = sequencer_continue(the_repository, opts);
277 else if (cmd == 's')
278 ret = sequencer_skip(the_repository, opts);
279 else
280 ret = sequencer_pick_revisions(the_repository, opts);
281 if (!ret)
282 run_auto_maintenance(the_repository, opts->quiet);
283 return ret;
284}
285
286int cmd_revert(int argc,
287 const char **argv,
288 const char *prefix,
289 struct repository *repo UNUSED)
290{
291 struct replay_opts opts = REPLAY_OPTS_INIT;
292 int res;
293
294#ifndef WITH_BREAKING_CHANGES
295 warn_on_auto_comment_char = true;
296#endif /* !WITH_BREAKING_CHANGES */
297 opts.action = REPLAY_REVERT;
298 sequencer_init_config(&opts);
299 res = run_sequencer(argc, argv, prefix, &opts);
300 if (res < 0)
301 die(_("revert failed"));
302 replay_opts_release(&opts);
303 return res;
304}
305
306int cmd_cherry_pick(int argc,
307const char **argv,
308const char *prefix,
309struct repository *repo UNUSED)
310{
311 struct replay_opts opts = REPLAY_OPTS_INIT;
312 int res;
313
314#ifndef WITH_BREAKING_CHANGES
315 warn_on_auto_comment_char = true;
316#endif /* !WITH_BREAKING_CHANGES */
317 opts.action = REPLAY_PICK;
318 sequencer_init_config(&opts);
319 res = run_sequencer(argc, argv, prefix, &opts);
320 if (res < 0)
321 die(_("cherry-pick failed"));
322 replay_opts_release(&opts);
323 return res;
324}