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