]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/revert.c
cache.h: remove dependence on hex.h; make other files include it explicitly
[thirdparty/git.git] / builtin / revert.c
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "config.h"
4 #include "builtin.h"
5 #include "parse-options.h"
6 #include "diff.h"
7 #include "revision.h"
8 #include "rerere.h"
9 #include "dir.h"
10 #include "sequencer.h"
11 #include "branch.h"
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
24 static const char * const revert_usage[] = {
25 N_("git revert [--[no-]edit] [-n] [-m <parent-number>] [-s] [-S[<keyid>]] <commit>..."),
26 N_("git revert (--continue | --skip | --abort | --quit)"),
27 NULL
28 };
29
30 static const char * const cherry_pick_usage[] = {
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)"),
34 NULL
35 };
36
37 static const char *action_name(const struct replay_opts *opts)
38 {
39 return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
40 }
41
42 static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts)
43 {
44 return opts->action == REPLAY_REVERT ? revert_usage : cherry_pick_usage;
45 }
46
47 static int option_parse_x(const struct option *opt,
48 const char *arg, int unset)
49 {
50 struct replay_opts **opts_ptr = opt->value;
51 struct replay_opts *opts = *opts_ptr;
52
53 if (unset)
54 return 0;
55
56 ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
57 opts->xopts[opts->xopts_nr++] = xstrdup(arg);
58 return 0;
59 }
60
61 static 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)
74 return error(_("option `%s' expects a number greater than zero"),
75 opt->long_name);
76
77 return 0;
78 }
79
80 LAST_ARG_MUST_BE_NULL
81 static 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
97 static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
98 {
99 const char * const * usage_str = revert_or_cherry_pick_usage(opts);
100 const char *me = action_name(opts);
101 const char *cleanup_arg = NULL;
102 int cmd = 0;
103 struct option base_options[] = {
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'),
107 OPT_CMDMODE(0, "skip", &cmd, N_("skip current commit and continue"), 's'),
108 OPT_CLEANUP(&cleanup_arg),
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")),
111 OPT_NOOP_NOARG('r', NULL),
112 OPT_BOOL('s', "signoff", &opts->signoff, N_("add a Signed-off-by trailer")),
113 OPT_CALLBACK('m', "mainline", opts, N_("parent-number"),
114 N_("select mainline parent"), option_parse_m),
115 OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
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),
119 { OPTION_STRING, 'S', "gpg-sign", &opts->gpg_sign, N_("key-id"),
120 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
121 OPT_END()
122 };
123 struct option *options = base_options;
124
125 if (opts->action == REPLAY_PICK) {
126 struct option cp_extra[] = {
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")),
132 OPT_END(),
133 };
134 options = parse_options_concat(options, cp_extra);
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);
142 }
143
144 argc = parse_options(argc, argv, NULL, options, usage_str,
145 PARSE_OPT_KEEP_ARGV0 |
146 PARSE_OPT_KEEP_UNKNOWN_OPT);
147
148 prepare_repo_settings(the_repository);
149 the_repository->settings.command_requires_full_index = 0;
150
151 /* implies allow_empty */
152 if (opts->keep_redundant_commits)
153 opts->allow_empty = 1;
154
155 if (cleanup_arg) {
156 opts->default_msg_cleanup = get_cleanup_mode(cleanup_arg, 1);
157 opts->explicit_cleanup = 1;
158 }
159
160 /* Check for incompatible command line arguments */
161 if (cmd) {
162 char *this_operation;
163 if (cmd == 'q')
164 this_operation = "--quit";
165 else if (cmd == 'c')
166 this_operation = "--continue";
167 else if (cmd == 's')
168 this_operation = "--skip";
169 else {
170 assert(cmd == 'a');
171 this_operation = "--abort";
172 }
173
174 verify_opt_compatible(me, this_operation,
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,
182 "--rerere-autoupdate", opts->allow_rerere_auto == RERERE_AUTOUPDATE,
183 "--no-rerere-autoupdate", opts->allow_rerere_auto == RERERE_NOAUTOUPDATE,
184 NULL);
185 }
186
187 if (!opts->strategy && opts->default_strategy) {
188 opts->strategy = opts->default_strategy;
189 opts->default_strategy = NULL;
190 }
191
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,
197 "--edit", opts->edit > 0,
198 NULL);
199
200 if (cmd) {
201 opts->revs = NULL;
202 } else {
203 struct setup_revision_opt s_r_opt;
204 opts->revs = xmalloc(sizeof(*opts->revs));
205 repo_init_revisions(the_repository, opts->revs, NULL);
206 opts->revs->no_walk = 1;
207 opts->revs->unsorted_input = 1;
208 if (argc < 2)
209 usage_with_options(usage_str, options);
210 if (!strcmp(argv[1], "-"))
211 argv[1] = "@{-1}";
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);
215 }
216
217 if (argc > 1)
218 usage_with_options(usage_str, options);
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);
223 if (!opts->strategy && getenv("GIT_TEST_MERGE_ALGORITHM"))
224 opts->strategy = xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
225 free(options);
226
227 if (cmd == 'q') {
228 int ret = sequencer_remove_state(opts);
229 if (!ret)
230 remove_branch_state(the_repository, 0);
231 return ret;
232 }
233 if (cmd == 'c')
234 return sequencer_continue(the_repository, opts);
235 if (cmd == 'a')
236 return sequencer_rollback(the_repository, opts);
237 if (cmd == 's')
238 return sequencer_skip(the_repository, opts);
239 return sequencer_pick_revisions(the_repository, opts);
240 }
241
242 int cmd_revert(int argc, const char **argv, const char *prefix)
243 {
244 struct replay_opts opts = REPLAY_OPTS_INIT;
245 int res;
246
247 opts.action = REPLAY_REVERT;
248 sequencer_init_config(&opts);
249 res = run_sequencer(argc, argv, &opts);
250 if (res < 0)
251 die(_("revert failed"));
252 replay_opts_release(&opts);
253 return res;
254 }
255
256 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
257 {
258 struct replay_opts opts = REPLAY_OPTS_INIT;
259 int res;
260
261 opts.action = REPLAY_PICK;
262 sequencer_init_config(&opts);
263 res = run_sequencer(argc, argv, &opts);
264 if (res < 0)
265 die(_("cherry-pick failed"));
266 replay_opts_release(&opts);
267 return res;
268 }