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