Merge branch 'ah/patch-id-doc'
[git/debian.git] / builtin / revert.c
blob16028b9ea82edee9cf41044c69a47e8994d78fc6
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"
12 * This implements the builtins revert and cherry-pick.
14 * Copyright (c) 2007 Johannes E. Schindelin
16 * Based on git-revert.sh, which is
18 * Copyright (c) 2005 Linus Torvalds
19 * Copyright (c) 2005 Junio C Hamano
22 static const char * const revert_usage[] = {
23 N_("git revert [<options>] <commit-ish>..."),
24 N_("git revert <subcommand>"),
25 NULL
28 static const char * const cherry_pick_usage[] = {
29 N_("git cherry-pick [<options>] <commit-ish>..."),
30 N_("git cherry-pick <subcommand>"),
31 NULL
34 static const char *action_name(const struct replay_opts *opts)
36 return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
39 static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts)
41 return opts->action == REPLAY_REVERT ? revert_usage : cherry_pick_usage;
44 static int option_parse_x(const struct option *opt,
45 const char *arg, int unset)
47 struct replay_opts **opts_ptr = opt->value;
48 struct replay_opts *opts = *opts_ptr;
50 if (unset)
51 return 0;
53 ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
54 opts->xopts[opts->xopts_nr++] = xstrdup(arg);
55 return 0;
58 static int option_parse_m(const struct option *opt,
59 const char *arg, int unset)
61 struct replay_opts *replay = opt->value;
62 char *end;
64 if (unset) {
65 replay->mainline = 0;
66 return 0;
69 replay->mainline = strtol(arg, &end, 10);
70 if (*end || replay->mainline <= 0)
71 return opterror(opt, "expects a number greater than zero", 0);
73 return 0;
76 LAST_ARG_MUST_BE_NULL
77 static void verify_opt_compatible(const char *me, const char *base_opt, ...)
79 const char *this_opt;
80 va_list ap;
82 va_start(ap, base_opt);
83 while ((this_opt = va_arg(ap, const char *))) {
84 if (va_arg(ap, int))
85 break;
87 va_end(ap);
89 if (this_opt)
90 die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
93 static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
95 const char * const * usage_str = revert_or_cherry_pick_usage(opts);
96 const char *me = action_name(opts);
97 int cmd = 0;
98 struct option base_options[] = {
99 OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'),
100 OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'),
101 OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'),
102 OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
103 OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")),
104 OPT_NOOP_NOARG('r', NULL),
105 OPT_BOOL('s', "signoff", &opts->signoff, N_("add Signed-off-by:")),
106 OPT_CALLBACK('m', "mainline", opts, N_("parent-number"),
107 N_("select mainline parent"), option_parse_m),
108 OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
109 OPT_STRING(0, "strategy", &opts->strategy, N_("strategy"), N_("merge strategy")),
110 OPT_CALLBACK('X', "strategy-option", &opts, N_("option"),
111 N_("option for merge strategy"), option_parse_x),
112 { OPTION_STRING, 'S', "gpg-sign", &opts->gpg_sign, N_("key-id"),
113 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
114 OPT_END()
116 struct option *options = base_options;
118 if (opts->action == REPLAY_PICK) {
119 struct option cp_extra[] = {
120 OPT_BOOL('x', NULL, &opts->record_origin, N_("append commit name")),
121 OPT_BOOL(0, "ff", &opts->allow_ff, N_("allow fast-forward")),
122 OPT_BOOL(0, "allow-empty", &opts->allow_empty, N_("preserve initially empty commits")),
123 OPT_BOOL(0, "allow-empty-message", &opts->allow_empty_message, N_("allow commits with empty messages")),
124 OPT_BOOL(0, "keep-redundant-commits", &opts->keep_redundant_commits, N_("keep redundant, empty commits")),
125 OPT_END(),
127 options = parse_options_concat(options, cp_extra);
130 argc = parse_options(argc, argv, NULL, options, usage_str,
131 PARSE_OPT_KEEP_ARGV0 |
132 PARSE_OPT_KEEP_UNKNOWN);
134 /* implies allow_empty */
135 if (opts->keep_redundant_commits)
136 opts->allow_empty = 1;
138 /* Check for incompatible command line arguments */
139 if (cmd) {
140 char *this_operation;
141 if (cmd == 'q')
142 this_operation = "--quit";
143 else if (cmd == 'c')
144 this_operation = "--continue";
145 else {
146 assert(cmd == 'a');
147 this_operation = "--abort";
150 verify_opt_compatible(me, this_operation,
151 "--no-commit", opts->no_commit,
152 "--signoff", opts->signoff,
153 "--mainline", opts->mainline,
154 "--strategy", opts->strategy ? 1 : 0,
155 "--strategy-option", opts->xopts ? 1 : 0,
156 "-x", opts->record_origin,
157 "--ff", opts->allow_ff,
158 NULL);
161 if (opts->allow_ff)
162 verify_opt_compatible(me, "--ff",
163 "--signoff", opts->signoff,
164 "--no-commit", opts->no_commit,
165 "-x", opts->record_origin,
166 "--edit", opts->edit,
167 NULL);
169 if (cmd) {
170 opts->revs = NULL;
171 } else {
172 struct setup_revision_opt s_r_opt;
173 opts->revs = xmalloc(sizeof(*opts->revs));
174 init_revisions(opts->revs, NULL);
175 opts->revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
176 if (argc < 2)
177 usage_with_options(usage_str, options);
178 if (!strcmp(argv[1], "-"))
179 argv[1] = "@{-1}";
180 memset(&s_r_opt, 0, sizeof(s_r_opt));
181 s_r_opt.assume_dashdash = 1;
182 argc = setup_revisions(argc, argv, opts->revs, &s_r_opt);
185 if (argc > 1)
186 usage_with_options(usage_str, options);
188 /* These option values will be free()d */
189 opts->gpg_sign = xstrdup_or_null(opts->gpg_sign);
190 opts->strategy = xstrdup_or_null(opts->strategy);
192 if (cmd == 'q')
193 return sequencer_remove_state(opts);
194 if (cmd == 'c')
195 return sequencer_continue(opts);
196 if (cmd == 'a')
197 return sequencer_rollback(opts);
198 return sequencer_pick_revisions(opts);
201 int cmd_revert(int argc, const char **argv, const char *prefix)
203 struct replay_opts opts = REPLAY_OPTS_INIT;
204 int res;
206 if (isatty(0))
207 opts.edit = 1;
208 opts.action = REPLAY_REVERT;
209 git_config(git_default_config, NULL);
210 res = run_sequencer(argc, argv, &opts);
211 if (res < 0)
212 die(_("revert failed"));
213 return res;
216 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
218 struct replay_opts opts = REPLAY_OPTS_INIT;
219 int res;
221 opts.action = REPLAY_PICK;
222 git_config(git_default_config, NULL);
223 res = run_sequencer(argc, argv, &opts);
224 if (res < 0)
225 die(_("cherry-pick failed"));
226 return res;