]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/revert.c
Merge branch 'js/t5563-portability-fix'
[thirdparty/git.git] / builtin / revert.c
CommitLineData
36bf1958
EN
1#include "git-compat-util.h"
2#include "alloc.h"
b2141fc1 3#include "config.h"
9509af68 4#include "builtin.h"
f8103794 5#include "parse-options.h"
0f2d4476
JK
6#include "diff.h"
7#include "revision.h"
aa1a0111 8#include "rerere.h"
04d3d3cf 9#include "dir.h"
26ae337b 10#include "sequencer.h"
3e7dd992 11#include "branch.h"
9509af68
JS
12
13/*
14 * This implements the builtins revert and cherry-pick.
15 *
16 * Copyright (c) 2007 Johannes E. Schindelin
17 *
18 * Based on git-revert.sh, which is
19 *
20 * Copyright (c) 2005 Linus Torvalds
21 * Copyright (c) 2005 Junio C Hamano
22 */
23
f8103794 24static const char * const revert_usage[] = {
d1ddc4e3 25 N_("git revert [--[no-]edit] [-n] [-m <parent-number>] [-s] [-S[<keyid>]] <commit>..."),
8c9e292d 26 N_("git revert (--continue | --skip | --abort | --quit)"),
f8103794
PH
27 NULL
28};
9509af68 29
f8103794 30static const char * const cherry_pick_usage[] = {
8c9e292d
ÆAB
31 N_("git cherry-pick [--edit] [-n] [-m <parent-number>] [-s] [-x] [--ff]\n"
32 " [-S[<keyid>]] <commit>..."),
33 N_("git cherry-pick (--continue | --skip | --abort | --quit)"),
f8103794
PH
34 NULL
35};
9509af68 36
80e1f791
RR
37static const char *action_name(const struct replay_opts *opts)
38{
644a3690 39 return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
80e1f791
RR
40}
41
80e1f791 42static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts)
e0ef8495 43{
644a3690 44 return opts->action == REPLAY_REVERT ? revert_usage : cherry_pick_usage;
e0ef8495
JN
45}
46
67ac1e1d
JN
47static int option_parse_x(const struct option *opt,
48 const char *arg, int unset)
49{
80e1f791
RR
50 struct replay_opts **opts_ptr = opt->value;
51 struct replay_opts *opts = *opts_ptr;
52
67ac1e1d
JN
53 if (unset)
54 return 0;
55
80e1f791
RR
56 ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
57 opts->xopts[opts->xopts_nr++] = xstrdup(arg);
67ac1e1d
JN
58 return 0;
59}
60
b16a991c
JK
61static int option_parse_m(const struct option *opt,
62 const char *arg, int unset)
63{
64 struct replay_opts *replay = opt->value;
65 char *end;
66
67 if (unset) {
68 replay->mainline = 0;
69 return 0;
70 }
71
72 replay->mainline = strtol(arg, &end, 10);
73 if (*end || replay->mainline <= 0)
9440b831
NTND
74 return error(_("option `%s' expects a number greater than zero"),
75 opt->long_name);
b16a991c
JK
76
77 return 0;
78}
79
9fe3edc4 80LAST_ARG_MUST_BE_NULL
9044143f
RR
81static void verify_opt_compatible(const char *me, const char *base_opt, ...)
82{
83 const char *this_opt;
84 va_list ap;
85
86 va_start(ap, base_opt);
87 while ((this_opt = va_arg(ap, const char *))) {
88 if (va_arg(ap, int))
89 break;
90 }
91 va_end(ap);
92
93 if (this_opt)
94 die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
95}
96
2863584f 97static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
9509af68 98{
80e1f791 99 const char * const * usage_str = revert_or_cherry_pick_usage(opts);
9044143f 100 const char *me = action_name(opts);
1a2b985f 101 const char *cleanup_arg = NULL;
84d83f64 102 int cmd = 0;
023ff39b 103 struct option base_options[] = {
84d83f64
SB
104 OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'),
105 OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'),
106 OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'),
de81ca3f 107 OPT_CMDMODE(0, "skip", &cmd, N_("skip current commit and continue"), 's'),
1a2b985f 108 OPT_CLEANUP(&cleanup_arg),
84d83f64
SB
109 OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
110 OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")),
7cfc6057 111 OPT_NOOP_NOARG('r', NULL),
3abd4a67 112 OPT_BOOL('s', "signoff", &opts->signoff, N_("add a Signed-off-by trailer")),
b16a991c
JK
113 OPT_CALLBACK('m', "mainline", opts, N_("parent-number"),
114 N_("select mainline parent"), option_parse_m),
80e1f791 115 OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
a3b26dc7
NTND
116 OPT_STRING(0, "strategy", &opts->strategy, N_("strategy"), N_("merge strategy")),
117 OPT_CALLBACK('X', "strategy-option", &opts, N_("option"),
118 N_("option for merge strategy"), option_parse_x),
e703d711 119 { OPTION_STRING, 'S', "gpg-sign", &opts->gpg_sign, N_("key-id"),
3253553e 120 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
023ff39b 121 OPT_END()
f8103794 122 };
023ff39b 123 struct option *options = base_options;
f8103794 124
644a3690 125 if (opts->action == REPLAY_PICK) {
c62f6ec3 126 struct option cp_extra[] = {
84d83f64
SB
127 OPT_BOOL('x', NULL, &opts->record_origin, N_("append commit name")),
128 OPT_BOOL(0, "ff", &opts->allow_ff, N_("allow fast-forward")),
129 OPT_BOOL(0, "allow-empty", &opts->allow_empty, N_("preserve initially empty commits")),
130 OPT_BOOL(0, "allow-empty-message", &opts->allow_empty_message, N_("allow commits with empty messages")),
131 OPT_BOOL(0, "keep-redundant-commits", &opts->keep_redundant_commits, N_("keep redundant, empty commits")),
c62f6ec3
JH
132 OPT_END(),
133 };
023ff39b 134 options = parse_options_concat(options, cp_extra);
191faaf7
JH
135 } else if (opts->action == REPLAY_REVERT) {
136 struct option cp_extra[] = {
137 OPT_BOOL(0, "reference", &opts->commit_use_reference,
138 N_("use the 'reference' format to refer to commits")),
139 OPT_END(),
140 };
141 options = parse_options_concat(options, cp_extra);
c62f6ec3
JH
142 }
143
7f13334e
JN
144 argc = parse_options(argc, argv, NULL, options, usage_str,
145 PARSE_OPT_KEEP_ARGV0 |
99d86d60 146 PARSE_OPT_KEEP_UNKNOWN_OPT);
26ae337b 147
516680ba
DS
148 prepare_repo_settings(the_repository);
149 the_repository->settings.command_requires_full_index = 0;
150
b27cfb0d
NH
151 /* implies allow_empty */
152 if (opts->keep_redundant_commits)
153 opts->allow_empty = 1;
154
1a2b985f
DL
155 if (cleanup_arg) {
156 opts->default_msg_cleanup = get_cleanup_mode(cleanup_arg, 1);
157 opts->explicit_cleanup = 1;
158 }
159
26ae337b 160 /* Check for incompatible command line arguments */
2863584f 161 if (cmd) {
5a5d80f4 162 char *this_operation;
2863584f 163 if (cmd == 'q')
f80a8726 164 this_operation = "--quit";
2863584f 165 else if (cmd == 'c')
5a5d80f4 166 this_operation = "--continue";
de81ca3f
RA
167 else if (cmd == 's')
168 this_operation = "--skip";
539047c1 169 else {
2863584f 170 assert(cmd == 'a');
539047c1
JN
171 this_operation = "--abort";
172 }
5a5d80f4
RR
173
174 verify_opt_compatible(me, this_operation,
26ae337b
RR
175 "--no-commit", opts->no_commit,
176 "--signoff", opts->signoff,
177 "--mainline", opts->mainline,
178 "--strategy", opts->strategy ? 1 : 0,
179 "--strategy-option", opts->xopts ? 1 : 0,
180 "-x", opts->record_origin,
181 "--ff", opts->allow_ff,
f826fb79
PW
182 "--rerere-autoupdate", opts->allow_rerere_auto == RERERE_AUTOUPDATE,
183 "--no-rerere-autoupdate", opts->allow_rerere_auto == RERERE_NOAUTOUPDATE,
26ae337b
RR
184 NULL);
185 }
186
14c4586c
EN
187 if (!opts->strategy && opts->default_strategy) {
188 opts->strategy = opts->default_strategy;
189 opts->default_strategy = NULL;
190 }
191
9044143f
RR
192 if (opts->allow_ff)
193 verify_opt_compatible(me, "--ff",
194 "--signoff", opts->signoff,
195 "--no-commit", opts->no_commit,
196 "-x", opts->record_origin,
39edfd5c 197 "--edit", opts->edit > 0,
9044143f 198 NULL);
7f13334e 199
2863584f 200 if (cmd) {
7f13334e
JN
201 opts->revs = NULL;
202 } else {
6d5b93f2 203 struct setup_revision_opt s_r_opt;
7f13334e 204 opts->revs = xmalloc(sizeof(*opts->revs));
2abf3503 205 repo_init_revisions(the_repository, opts->revs, NULL);
29ef1f27
PS
206 opts->revs->no_walk = 1;
207 opts->revs->unsorted_input = 1;
7f13334e
JN
208 if (argc < 2)
209 usage_with_options(usage_str, options);
d644c550
JK
210 if (!strcmp(argv[1], "-"))
211 argv[1] = "@{-1}";
6d5b93f2
CB
212 memset(&s_r_opt, 0, sizeof(s_r_opt));
213 s_r_opt.assume_dashdash = 1;
214 argc = setup_revisions(argc, argv, opts->revs, &s_r_opt);
7f13334e
JN
215 }
216
217 if (argc > 1)
218 usage_with_options(usage_str, options);
03a4e260
JS
219
220 /* These option values will be free()d */
221 opts->gpg_sign = xstrdup_or_null(opts->gpg_sign);
222 opts->strategy = xstrdup_or_null(opts->strategy);
14c4586c
EN
223 if (!opts->strategy && getenv("GIT_TEST_MERGE_ALGORITHM"))
224 opts->strategy = xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
603f2f57 225 free(options);
2863584f 226
3e7dd992
NTND
227 if (cmd == 'q') {
228 int ret = sequencer_remove_state(opts);
229 if (!ret)
f4a4b9ac 230 remove_branch_state(the_repository, 0);
3e7dd992
NTND
231 return ret;
232 }
2863584f 233 if (cmd == 'c')
f11c9580 234 return sequencer_continue(the_repository, opts);
2863584f 235 if (cmd == 'a')
005af339 236 return sequencer_rollback(the_repository, opts);
de81ca3f
RA
237 if (cmd == 's')
238 return sequencer_skip(the_repository, opts);
f11c9580 239 return sequencer_pick_revisions(the_repository, opts);
9509af68
JS
240}
241
9509af68
JS
242int cmd_revert(int argc, const char **argv, const char *prefix)
243{
ee624c0d 244 struct replay_opts opts = REPLAY_OPTS_INIT;
cf3e2486 245 int res;
80e1f791 246
644a3690 247 opts.action = REPLAY_REVERT;
28d6daed 248 sequencer_init_config(&opts);
2863584f 249 res = run_sequencer(argc, argv, &opts);
cf3e2486
RR
250 if (res < 0)
251 die(_("revert failed"));
9ff2f060 252 replay_opts_release(&opts);
cf3e2486 253 return res;
9509af68
JS
254}
255
256int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
257{
ee624c0d 258 struct replay_opts opts = REPLAY_OPTS_INIT;
cf3e2486 259 int res;
80e1f791 260
644a3690 261 opts.action = REPLAY_PICK;
28d6daed 262 sequencer_init_config(&opts);
2863584f 263 res = run_sequencer(argc, argv, &opts);
cf3e2486
RR
264 if (res < 0)
265 die(_("cherry-pick failed"));
9ff2f060 266 replay_opts_release(&opts);
cf3e2486 267 return res;
9509af68 268}