]>
Commit | Line | Data |
---|---|---|
9509af68 JS |
1 | #include "cache.h" |
2 | #include "builtin.h" | |
f8103794 | 3 | #include "parse-options.h" |
0f2d4476 JK |
4 | #include "diff.h" |
5 | #include "revision.h" | |
aa1a0111 | 6 | #include "rerere.h" |
04d3d3cf | 7 | #include "dir.h" |
26ae337b | 8 | #include "sequencer.h" |
9509af68 JS |
9 | |
10 | /* | |
11 | * This implements the builtins revert and cherry-pick. | |
12 | * | |
13 | * Copyright (c) 2007 Johannes E. Schindelin | |
14 | * | |
15 | * Based on git-revert.sh, which is | |
16 | * | |
17 | * Copyright (c) 2005 Linus Torvalds | |
18 | * Copyright (c) 2005 Junio C Hamano | |
19 | */ | |
20 | ||
f8103794 | 21 | static const char * const revert_usage[] = { |
1b1dd23f | 22 | "git revert [options] <commit-ish>", |
26ae337b | 23 | "git revert <subcommand>", |
f8103794 PH |
24 | NULL |
25 | }; | |
9509af68 | 26 | |
f8103794 | 27 | static const char * const cherry_pick_usage[] = { |
1b1dd23f | 28 | "git cherry-pick [options] <commit-ish>", |
26ae337b | 29 | "git cherry-pick <subcommand>", |
f8103794 PH |
30 | NULL |
31 | }; | |
9509af68 | 32 | |
80e1f791 RR |
33 | static const char *action_name(const struct replay_opts *opts) |
34 | { | |
644a3690 | 35 | return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick"; |
80e1f791 RR |
36 | } |
37 | ||
80e1f791 | 38 | static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts) |
e0ef8495 | 39 | { |
644a3690 | 40 | return opts->action == REPLAY_REVERT ? revert_usage : cherry_pick_usage; |
e0ef8495 JN |
41 | } |
42 | ||
67ac1e1d JN |
43 | static int option_parse_x(const struct option *opt, |
44 | const char *arg, int unset) | |
45 | { | |
80e1f791 RR |
46 | struct replay_opts **opts_ptr = opt->value; |
47 | struct replay_opts *opts = *opts_ptr; | |
48 | ||
67ac1e1d JN |
49 | if (unset) |
50 | return 0; | |
51 | ||
80e1f791 RR |
52 | ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc); |
53 | opts->xopts[opts->xopts_nr++] = xstrdup(arg); | |
67ac1e1d JN |
54 | return 0; |
55 | } | |
56 | ||
9044143f RR |
57 | static void verify_opt_compatible(const char *me, const char *base_opt, ...) |
58 | { | |
59 | const char *this_opt; | |
60 | va_list ap; | |
61 | ||
62 | va_start(ap, base_opt); | |
63 | while ((this_opt = va_arg(ap, const char *))) { | |
64 | if (va_arg(ap, int)) | |
65 | break; | |
66 | } | |
67 | va_end(ap); | |
68 | ||
69 | if (this_opt) | |
70 | die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt); | |
71 | } | |
72 | ||
5a5d80f4 RR |
73 | static void verify_opt_mutually_compatible(const char *me, ...) |
74 | { | |
27c0f768 | 75 | const char *opt1, *opt2 = NULL; |
5a5d80f4 RR |
76 | va_list ap; |
77 | ||
78 | va_start(ap, me); | |
79 | while ((opt1 = va_arg(ap, const char *))) { | |
80 | if (va_arg(ap, int)) | |
81 | break; | |
82 | } | |
83 | if (opt1) { | |
84 | while ((opt2 = va_arg(ap, const char *))) { | |
85 | if (va_arg(ap, int)) | |
86 | break; | |
87 | } | |
88 | } | |
5ba9b5e7 | 89 | va_end(ap); |
5a5d80f4 RR |
90 | |
91 | if (opt1 && opt2) | |
92 | die(_("%s: %s cannot be used with %s"), me, opt1, opt2); | |
93 | } | |
94 | ||
80e1f791 | 95 | static void parse_args(int argc, const char **argv, struct replay_opts *opts) |
9509af68 | 96 | { |
80e1f791 | 97 | const char * const * usage_str = revert_or_cherry_pick_usage(opts); |
9044143f | 98 | const char *me = action_name(opts); |
f80a8726 | 99 | int remove_state = 0; |
5a5d80f4 | 100 | int contin = 0; |
539047c1 | 101 | int rollback = 0; |
f8103794 | 102 | struct option options[] = { |
f80a8726 JN |
103 | OPT_BOOLEAN(0, "quit", &remove_state, "end revert or cherry-pick sequence"), |
104 | OPT_BOOLEAN(0, "continue", &contin, "resume revert or cherry-pick sequence"), | |
539047c1 | 105 | OPT_BOOLEAN(0, "abort", &rollback, "cancel revert or cherry-pick sequence"), |
80e1f791 RR |
106 | OPT_BOOLEAN('n', "no-commit", &opts->no_commit, "don't automatically commit"), |
107 | OPT_BOOLEAN('e', "edit", &opts->edit, "edit the commit message"), | |
7cfc6057 | 108 | OPT_NOOP_NOARG('r', NULL), |
80e1f791 RR |
109 | OPT_BOOLEAN('s', "signoff", &opts->signoff, "add Signed-off-by:"), |
110 | OPT_INTEGER('m', "mainline", &opts->mainline, "parent number"), | |
111 | OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto), | |
112 | OPT_STRING(0, "strategy", &opts->strategy, "strategy", "merge strategy"), | |
113 | OPT_CALLBACK('X', "strategy-option", &opts, "option", | |
67ac1e1d | 114 | "option for merge strategy", option_parse_x), |
f8103794 | 115 | OPT_END(), |
c62f6ec3 JH |
116 | OPT_END(), |
117 | OPT_END(), | |
df478b74 | 118 | OPT_END(), |
b27cfb0d | 119 | OPT_END(), |
f8103794 PH |
120 | }; |
121 | ||
644a3690 | 122 | if (opts->action == REPLAY_PICK) { |
c62f6ec3 | 123 | struct option cp_extra[] = { |
80e1f791 RR |
124 | OPT_BOOLEAN('x', NULL, &opts->record_origin, "append commit name"), |
125 | OPT_BOOLEAN(0, "ff", &opts->allow_ff, "allow fast-forward"), | |
b27cfb0d NH |
126 | OPT_BOOLEAN(0, "allow-empty", &opts->allow_empty, "preserve initially empty commits"), |
127 | OPT_BOOLEAN(0, "keep-redundant-commits", &opts->keep_redundant_commits, "keep redundant, empty commits"), | |
c62f6ec3 JH |
128 | OPT_END(), |
129 | }; | |
130 | if (parse_options_concat(options, ARRAY_SIZE(options), cp_extra)) | |
f30f71ce | 131 | die(_("program error")); |
c62f6ec3 JH |
132 | } |
133 | ||
7f13334e JN |
134 | argc = parse_options(argc, argv, NULL, options, usage_str, |
135 | PARSE_OPT_KEEP_ARGV0 | | |
136 | PARSE_OPT_KEEP_UNKNOWN); | |
26ae337b | 137 | |
5a5d80f4 RR |
138 | /* Check for incompatible subcommands */ |
139 | verify_opt_mutually_compatible(me, | |
f80a8726 | 140 | "--quit", remove_state, |
5a5d80f4 | 141 | "--continue", contin, |
539047c1 | 142 | "--abort", rollback, |
5a5d80f4 RR |
143 | NULL); |
144 | ||
b27cfb0d NH |
145 | /* implies allow_empty */ |
146 | if (opts->keep_redundant_commits) | |
147 | opts->allow_empty = 1; | |
148 | ||
26ae337b | 149 | /* Set the subcommand */ |
f80a8726 JN |
150 | if (remove_state) |
151 | opts->subcommand = REPLAY_REMOVE_STATE; | |
5a5d80f4 RR |
152 | else if (contin) |
153 | opts->subcommand = REPLAY_CONTINUE; | |
539047c1 JN |
154 | else if (rollback) |
155 | opts->subcommand = REPLAY_ROLLBACK; | |
26ae337b RR |
156 | else |
157 | opts->subcommand = REPLAY_NONE; | |
158 | ||
159 | /* Check for incompatible command line arguments */ | |
5a5d80f4 RR |
160 | if (opts->subcommand != REPLAY_NONE) { |
161 | char *this_operation; | |
f80a8726 JN |
162 | if (opts->subcommand == REPLAY_REMOVE_STATE) |
163 | this_operation = "--quit"; | |
539047c1 | 164 | else if (opts->subcommand == REPLAY_CONTINUE) |
5a5d80f4 | 165 | this_operation = "--continue"; |
539047c1 JN |
166 | else { |
167 | assert(opts->subcommand == REPLAY_ROLLBACK); | |
168 | this_operation = "--abort"; | |
169 | } | |
5a5d80f4 RR |
170 | |
171 | verify_opt_compatible(me, this_operation, | |
26ae337b RR |
172 | "--no-commit", opts->no_commit, |
173 | "--signoff", opts->signoff, | |
174 | "--mainline", opts->mainline, | |
175 | "--strategy", opts->strategy ? 1 : 0, | |
176 | "--strategy-option", opts->xopts ? 1 : 0, | |
177 | "-x", opts->record_origin, | |
178 | "--ff", opts->allow_ff, | |
179 | NULL); | |
180 | } | |
181 | ||
9044143f RR |
182 | if (opts->allow_ff) |
183 | verify_opt_compatible(me, "--ff", | |
184 | "--signoff", opts->signoff, | |
185 | "--no-commit", opts->no_commit, | |
186 | "-x", opts->record_origin, | |
187 | "--edit", opts->edit, | |
188 | NULL); | |
7f13334e JN |
189 | |
190 | if (opts->subcommand != REPLAY_NONE) { | |
191 | opts->revs = NULL; | |
192 | } else { | |
6d5b93f2 | 193 | struct setup_revision_opt s_r_opt; |
7f13334e JN |
194 | opts->revs = xmalloc(sizeof(*opts->revs)); |
195 | init_revisions(opts->revs, NULL); | |
a73e22e9 | 196 | opts->revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED; |
7f13334e JN |
197 | if (argc < 2) |
198 | usage_with_options(usage_str, options); | |
6d5b93f2 CB |
199 | memset(&s_r_opt, 0, sizeof(s_r_opt)); |
200 | s_r_opt.assume_dashdash = 1; | |
201 | argc = setup_revisions(argc, argv, opts->revs, &s_r_opt); | |
7f13334e JN |
202 | } |
203 | ||
204 | if (argc > 1) | |
205 | usage_with_options(usage_str, options); | |
9509af68 JS |
206 | } |
207 | ||
9509af68 JS |
208 | int cmd_revert(int argc, const char **argv, const char *prefix) |
209 | { | |
80e1f791 | 210 | struct replay_opts opts; |
cf3e2486 | 211 | int res; |
80e1f791 RR |
212 | |
213 | memset(&opts, 0, sizeof(opts)); | |
9509af68 | 214 | if (isatty(0)) |
80e1f791 | 215 | opts.edit = 1; |
644a3690 | 216 | opts.action = REPLAY_REVERT; |
89641472 RR |
217 | git_config(git_default_config, NULL); |
218 | parse_args(argc, argv, &opts); | |
043a4492 | 219 | res = sequencer_pick_revisions(&opts); |
cf3e2486 RR |
220 | if (res < 0) |
221 | die(_("revert failed")); | |
222 | return res; | |
9509af68 JS |
223 | } |
224 | ||
225 | int cmd_cherry_pick(int argc, const char **argv, const char *prefix) | |
226 | { | |
80e1f791 | 227 | struct replay_opts opts; |
cf3e2486 | 228 | int res; |
80e1f791 RR |
229 | |
230 | memset(&opts, 0, sizeof(opts)); | |
644a3690 | 231 | opts.action = REPLAY_PICK; |
89641472 RR |
232 | git_config(git_default_config, NULL); |
233 | parse_args(argc, argv, &opts); | |
043a4492 | 234 | res = sequencer_pick_revisions(&opts); |
cf3e2486 RR |
235 | if (res < 0) |
236 | die(_("cherry-pick failed")); | |
237 | return res; | |
9509af68 | 238 | } |