3 #include "parse-options.h"
11 * This implements the builtins revert and cherry-pick.
13 * Copyright (c) 2007 Johannes E. Schindelin
15 * Based on git-revert.sh, which is
17 * Copyright (c) 2005 Linus Torvalds
18 * Copyright (c) 2005 Junio C Hamano
21 static const char * const revert_usage[] = {
22 N_("git revert [<options>] <commit-ish>..."),
23 N_("git revert <subcommand>"),
27 static const char * const cherry_pick_usage[] = {
28 N_("git cherry-pick [<options>] <commit-ish>..."),
29 N_("git cherry-pick <subcommand>"),
33 static const char *action_name(const struct replay_opts *opts)
35 return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
38 static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts)
40 return opts->action == REPLAY_REVERT ? revert_usage : cherry_pick_usage;
43 static int option_parse_x(const struct option *opt,
44 const char *arg, int unset)
46 struct replay_opts **opts_ptr = opt->value;
47 struct replay_opts *opts = *opts_ptr;
52 ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
53 opts->xopts[opts->xopts_nr++] = xstrdup(arg);
58 static void verify_opt_compatible(const char *me, const char *base_opt, ...)
63 va_start(ap, base_opt);
64 while ((this_opt = va_arg(ap, const char *))) {
71 die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
74 static void parse_args(int argc, const char **argv, struct replay_opts *opts)
76 const char * const * usage_str = revert_or_cherry_pick_usage(opts);
77 const char *me = action_name(opts);
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) "" },
107 if (opts->action == REPLAY_PICK) {
108 struct option cp_extra[] = {
109 OPT_BOOL('x', NULL, &opts->record_origin, N_("append commit name")),
110 OPT_BOOL(0, "ff", &opts->allow_ff, N_("allow fast-forward")),
111 OPT_BOOL(0, "allow-empty", &opts->allow_empty, N_("preserve initially empty commits")),
112 OPT_BOOL(0, "allow-empty-message", &opts->allow_empty_message, N_("allow commits with empty messages")),
113 OPT_BOOL(0, "keep-redundant-commits", &opts->keep_redundant_commits, N_("keep redundant, empty commits")),
114 OPT_BOOL(0, "skip-empty", &opts->skip_empty, N_("skip empty commits")),
115 OPT_STRING(0, "action-name", &opts->action_name, N_("name"), N_("action name")),
118 if (parse_options_concat(options, ARRAY_SIZE(options), cp_extra))
119 die(_("program error"));
122 argc = parse_options(argc, argv, NULL, options, usage_str,
123 PARSE_OPT_KEEP_ARGV0 |
124 PARSE_OPT_KEEP_UNKNOWN);
126 /* implies allow_empty */
127 if (opts->keep_redundant_commits)
128 opts->allow_empty = 1;
130 /* Set the subcommand */
132 opts->subcommand = REPLAY_REMOVE_STATE;
134 opts->subcommand = REPLAY_CONTINUE;
136 opts->subcommand = REPLAY_ROLLBACK;
138 opts->subcommand = REPLAY_SKIP;
140 opts->subcommand = REPLAY_NONE;
142 /* Check for incompatible command line arguments */
143 if (opts->subcommand != REPLAY_NONE) {
144 char *this_operation;
145 if (opts->subcommand == REPLAY_REMOVE_STATE)
146 this_operation = "--quit";
147 else if (opts->subcommand == REPLAY_CONTINUE)
148 this_operation = "--continue";
149 else if (opts->subcommand == REPLAY_SKIP)
150 this_operation = "--skip";
152 assert(opts->subcommand == REPLAY_ROLLBACK);
153 this_operation = "--abort";
156 verify_opt_compatible(me, this_operation,
157 "--no-commit", opts->no_commit,
158 "--signoff", opts->signoff,
159 "--mainline", opts->mainline,
160 "--strategy", opts->strategy ? 1 : 0,
161 "--strategy-option", opts->xopts ? 1 : 0,
162 "-x", opts->record_origin,
163 "--ff", opts->allow_ff,
168 verify_opt_compatible(me, "--ff",
169 "--signoff", opts->signoff,
170 "--no-commit", opts->no_commit,
171 "-x", opts->record_origin,
172 "--edit", opts->edit,
175 if (opts->subcommand != REPLAY_NONE) {
178 struct setup_revision_opt s_r_opt;
179 opts->revs = xmalloc(sizeof(*opts->revs));
180 init_revisions(opts->revs, NULL);
181 opts->revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
183 usage_with_options(usage_str, options);
184 if (!strcmp(argv[1], "-"))
186 memset(&s_r_opt, 0, sizeof(s_r_opt));
187 s_r_opt.assume_dashdash = 1;
188 argc = setup_revisions(argc, argv, opts->revs, &s_r_opt);
192 usage_with_options(usage_str, options);
195 int cmd_revert(int argc, const char **argv, const char *prefix)
197 struct replay_opts opts;
200 memset(&opts, 0, sizeof(opts));
203 opts.action = REPLAY_REVERT;
204 git_config(git_default_config, NULL);
205 parse_args(argc, argv, &opts);
206 res = sequencer_pick_revisions(&opts);
208 die(_("revert failed"));
212 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
214 struct replay_opts opts;
217 memset(&opts, 0, sizeof(opts));
218 opts.action = REPLAY_PICK;
219 git_config(git_default_config, NULL);
220 parse_args(argc, argv, &opts);
221 res = sequencer_pick_revisions(&opts);
223 die(_("cherry-pick failed"));