4 #include "parse-options.h"
13 * This implements the builtins revert and cherry-pick.
15 * Copyright (c) 2007 Johannes E. Schindelin
17 * Based on git-revert.sh, which is
19 * Copyright (c) 2005 Linus Torvalds
20 * Copyright (c) 2005 Junio C Hamano
23 static const char * const revert_usage[] = {
24 N_("git revert [<options>] <commit-ish>..."),
25 N_("git revert <subcommand>"),
29 static const char * const cherry_pick_usage[] = {
30 N_("git cherry-pick [<options>] <commit-ish>..."),
31 N_("git cherry-pick <subcommand>"),
35 static const char *action_name(const struct replay_opts *opts)
37 return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
40 static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts)
42 return opts->action == REPLAY_REVERT ? revert_usage : cherry_pick_usage;
45 static int option_parse_x(const struct option *opt,
46 const char *arg, int unset)
48 struct replay_opts **opts_ptr = opt->value;
49 struct replay_opts *opts = *opts_ptr;
54 ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
55 opts->xopts[opts->xopts_nr++] = xstrdup(arg);
59 static int option_parse_m(const struct option *opt,
60 const char *arg, int unset)
62 struct replay_opts *replay = opt->value;
70 replay->mainline = strtol(arg, &end, 10);
71 if (*end || replay->mainline <= 0)
72 return error(_("option `%s' expects a number greater than zero"),
79 static void verify_opt_compatible(const char *me, const char *base_opt, ...)
84 va_start(ap, base_opt);
85 while ((this_opt = va_arg(ap, const char *))) {
92 die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
95 static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
97 const char * const * usage_str = revert_or_cherry_pick_usage(opts);
98 const char *me = action_name(opts);
100 struct option base_options[] = {
101 OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'),
102 OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'),
103 OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'),
104 OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
105 OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")),
106 OPT_NOOP_NOARG('r', NULL),
107 OPT_BOOL('s', "signoff", &opts->signoff, N_("add Signed-off-by:")),
108 OPT_CALLBACK('m', "mainline", opts, N_("parent-number"),
109 N_("select mainline parent"), option_parse_m),
110 OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
111 OPT_STRING(0, "strategy", &opts->strategy, N_("strategy"), N_("merge strategy")),
112 OPT_CALLBACK('X', "strategy-option", &opts, N_("option"),
113 N_("option for merge strategy"), option_parse_x),
114 { OPTION_STRING, 'S', "gpg-sign", &opts->gpg_sign, N_("key-id"),
115 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
118 struct option *options = base_options;
120 if (opts->action == REPLAY_PICK) {
121 struct option cp_extra[] = {
122 OPT_BOOL('x', NULL, &opts->record_origin, N_("append commit name")),
123 OPT_BOOL(0, "ff", &opts->allow_ff, N_("allow fast-forward")),
124 OPT_BOOL(0, "allow-empty", &opts->allow_empty, N_("preserve initially empty commits")),
125 OPT_BOOL(0, "allow-empty-message", &opts->allow_empty_message, N_("allow commits with empty messages")),
126 OPT_BOOL(0, "keep-redundant-commits", &opts->keep_redundant_commits, N_("keep redundant, empty commits")),
129 options = parse_options_concat(options, cp_extra);
132 argc = parse_options(argc, argv, NULL, options, usage_str,
133 PARSE_OPT_KEEP_ARGV0 |
134 PARSE_OPT_KEEP_UNKNOWN);
136 /* implies allow_empty */
137 if (opts->keep_redundant_commits)
138 opts->allow_empty = 1;
140 /* Check for incompatible command line arguments */
142 char *this_operation;
144 this_operation = "--quit";
146 this_operation = "--continue";
149 this_operation = "--abort";
152 verify_opt_compatible(me, this_operation,
153 "--no-commit", opts->no_commit,
154 "--signoff", opts->signoff,
155 "--mainline", opts->mainline,
156 "--strategy", opts->strategy ? 1 : 0,
157 "--strategy-option", opts->xopts ? 1 : 0,
158 "-x", opts->record_origin,
159 "--ff", opts->allow_ff,
160 "--rerere-autoupdate", opts->allow_rerere_auto == RERERE_AUTOUPDATE,
161 "--no-rerere-autoupdate", opts->allow_rerere_auto == RERERE_NOAUTOUPDATE,
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,
176 struct setup_revision_opt s_r_opt;
177 opts->revs = xmalloc(sizeof(*opts->revs));
178 repo_init_revisions(the_repository, opts->revs, NULL);
179 opts->revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
181 usage_with_options(usage_str, options);
182 if (!strcmp(argv[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);
190 usage_with_options(usage_str, options);
192 /* These option values will be free()d */
193 opts->gpg_sign = xstrdup_or_null(opts->gpg_sign);
194 opts->strategy = xstrdup_or_null(opts->strategy);
197 int ret = sequencer_remove_state(opts);
199 remove_branch_state(the_repository);
203 return sequencer_continue(the_repository, opts);
205 return sequencer_rollback(the_repository, opts);
206 return sequencer_pick_revisions(the_repository, opts);
209 int cmd_revert(int argc, const char **argv, const char *prefix)
211 struct replay_opts opts = REPLAY_OPTS_INIT;
216 opts.action = REPLAY_REVERT;
217 sequencer_init_config(&opts);
218 res = run_sequencer(argc, argv, &opts);
220 die(_("revert failed"));
224 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
226 struct replay_opts opts = REPLAY_OPTS_INIT;
229 opts.action = REPLAY_PICK;
230 sequencer_init_config(&opts);
231 res = run_sequencer(argc, argv, &opts);
233 die(_("cherry-pick failed"));