]>
Commit | Line | Data |
---|---|---|
9509af68 | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
9509af68 | 3 | #include "builtin.h" |
f8103794 | 4 | #include "parse-options.h" |
0f2d4476 JK |
5 | #include "diff.h" |
6 | #include "revision.h" | |
aa1a0111 | 7 | #include "rerere.h" |
04d3d3cf | 8 | #include "dir.h" |
26ae337b | 9 | #include "sequencer.h" |
3e7dd992 | 10 | #include "branch.h" |
9509af68 JS |
11 | |
12 | /* | |
13 | * This implements the builtins revert and cherry-pick. | |
14 | * | |
15 | * Copyright (c) 2007 Johannes E. Schindelin | |
16 | * | |
17 | * Based on git-revert.sh, which is | |
18 | * | |
19 | * Copyright (c) 2005 Linus Torvalds | |
20 | * Copyright (c) 2005 Junio C Hamano | |
21 | */ | |
22 | ||
f8103794 | 23 | static const char * const revert_usage[] = { |
9c9b4f2f | 24 | N_("git revert [<options>] <commit-ish>..."), |
a3b26dc7 | 25 | N_("git revert <subcommand>"), |
f8103794 PH |
26 | NULL |
27 | }; | |
9509af68 | 28 | |
f8103794 | 29 | static const char * const cherry_pick_usage[] = { |
9c9b4f2f | 30 | N_("git cherry-pick [<options>] <commit-ish>..."), |
a3b26dc7 | 31 | N_("git cherry-pick <subcommand>"), |
f8103794 PH |
32 | NULL |
33 | }; | |
9509af68 | 34 | |
80e1f791 RR |
35 | static const char *action_name(const struct replay_opts *opts) |
36 | { | |
644a3690 | 37 | return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick"; |
80e1f791 RR |
38 | } |
39 | ||
80e1f791 | 40 | static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts) |
e0ef8495 | 41 | { |
644a3690 | 42 | return opts->action == REPLAY_REVERT ? revert_usage : cherry_pick_usage; |
e0ef8495 JN |
43 | } |
44 | ||
67ac1e1d JN |
45 | static int option_parse_x(const struct option *opt, |
46 | const char *arg, int unset) | |
47 | { | |
80e1f791 RR |
48 | struct replay_opts **opts_ptr = opt->value; |
49 | struct replay_opts *opts = *opts_ptr; | |
50 | ||
67ac1e1d JN |
51 | if (unset) |
52 | return 0; | |
53 | ||
80e1f791 RR |
54 | ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc); |
55 | opts->xopts[opts->xopts_nr++] = xstrdup(arg); | |
67ac1e1d JN |
56 | return 0; |
57 | } | |
58 | ||
b16a991c JK |
59 | static int option_parse_m(const struct option *opt, |
60 | const char *arg, int unset) | |
61 | { | |
62 | struct replay_opts *replay = opt->value; | |
63 | char *end; | |
64 | ||
65 | if (unset) { | |
66 | replay->mainline = 0; | |
67 | return 0; | |
68 | } | |
69 | ||
70 | replay->mainline = strtol(arg, &end, 10); | |
71 | if (*end || replay->mainline <= 0) | |
9440b831 NTND |
72 | return error(_("option `%s' expects a number greater than zero"), |
73 | opt->long_name); | |
b16a991c JK |
74 | |
75 | return 0; | |
76 | } | |
77 | ||
9fe3edc4 | 78 | LAST_ARG_MUST_BE_NULL |
9044143f RR |
79 | static void verify_opt_compatible(const char *me, const char *base_opt, ...) |
80 | { | |
81 | const char *this_opt; | |
82 | va_list ap; | |
83 | ||
84 | va_start(ap, base_opt); | |
85 | while ((this_opt = va_arg(ap, const char *))) { | |
86 | if (va_arg(ap, int)) | |
87 | break; | |
88 | } | |
89 | va_end(ap); | |
90 | ||
91 | if (this_opt) | |
92 | die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt); | |
93 | } | |
94 | ||
2863584f | 95 | static int run_sequencer(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); |
1a2b985f | 99 | const char *cleanup_arg = NULL; |
84d83f64 | 100 | int cmd = 0; |
023ff39b | 101 | struct option base_options[] = { |
84d83f64 SB |
102 | OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'), |
103 | OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'), | |
104 | OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'), | |
de81ca3f | 105 | OPT_CMDMODE(0, "skip", &cmd, N_("skip current commit and continue"), 's'), |
1a2b985f | 106 | OPT_CLEANUP(&cleanup_arg), |
84d83f64 SB |
107 | OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")), |
108 | OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")), | |
7cfc6057 | 109 | OPT_NOOP_NOARG('r', NULL), |
84d83f64 | 110 | OPT_BOOL('s', "signoff", &opts->signoff, N_("add Signed-off-by:")), |
b16a991c JK |
111 | OPT_CALLBACK('m', "mainline", opts, N_("parent-number"), |
112 | N_("select mainline parent"), option_parse_m), | |
80e1f791 | 113 | OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto), |
a3b26dc7 NTND |
114 | OPT_STRING(0, "strategy", &opts->strategy, N_("strategy"), N_("merge strategy")), |
115 | OPT_CALLBACK('X', "strategy-option", &opts, N_("option"), | |
116 | N_("option for merge strategy"), option_parse_x), | |
e703d711 | 117 | { OPTION_STRING, 'S', "gpg-sign", &opts->gpg_sign, N_("key-id"), |
3253553e | 118 | N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, |
023ff39b | 119 | OPT_END() |
f8103794 | 120 | }; |
023ff39b | 121 | struct option *options = base_options; |
f8103794 | 122 | |
644a3690 | 123 | if (opts->action == REPLAY_PICK) { |
c62f6ec3 | 124 | struct option cp_extra[] = { |
84d83f64 SB |
125 | OPT_BOOL('x', NULL, &opts->record_origin, N_("append commit name")), |
126 | OPT_BOOL(0, "ff", &opts->allow_ff, N_("allow fast-forward")), | |
127 | OPT_BOOL(0, "allow-empty", &opts->allow_empty, N_("preserve initially empty commits")), | |
128 | OPT_BOOL(0, "allow-empty-message", &opts->allow_empty_message, N_("allow commits with empty messages")), | |
129 | OPT_BOOL(0, "keep-redundant-commits", &opts->keep_redundant_commits, N_("keep redundant, empty commits")), | |
c62f6ec3 JH |
130 | OPT_END(), |
131 | }; | |
023ff39b | 132 | options = parse_options_concat(options, cp_extra); |
c62f6ec3 JH |
133 | } |
134 | ||
7f13334e JN |
135 | argc = parse_options(argc, argv, NULL, options, usage_str, |
136 | PARSE_OPT_KEEP_ARGV0 | | |
137 | PARSE_OPT_KEEP_UNKNOWN); | |
26ae337b | 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, | |
167 | "--strategy-option", opts->xopts ? 1 : 0, | |
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 | ||
9044143f RR |
175 | if (opts->allow_ff) |
176 | verify_opt_compatible(me, "--ff", | |
177 | "--signoff", opts->signoff, | |
178 | "--no-commit", opts->no_commit, | |
179 | "-x", opts->record_origin, | |
180 | "--edit", opts->edit, | |
181 | NULL); | |
7f13334e | 182 | |
2863584f | 183 | if (cmd) { |
7f13334e JN |
184 | opts->revs = NULL; |
185 | } else { | |
6d5b93f2 | 186 | struct setup_revision_opt s_r_opt; |
7f13334e | 187 | opts->revs = xmalloc(sizeof(*opts->revs)); |
2abf3503 | 188 | repo_init_revisions(the_repository, opts->revs, NULL); |
a73e22e9 | 189 | opts->revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED; |
7f13334e JN |
190 | if (argc < 2) |
191 | usage_with_options(usage_str, options); | |
d644c550 JK |
192 | if (!strcmp(argv[1], "-")) |
193 | argv[1] = "@{-1}"; | |
6d5b93f2 CB |
194 | memset(&s_r_opt, 0, sizeof(s_r_opt)); |
195 | s_r_opt.assume_dashdash = 1; | |
196 | argc = setup_revisions(argc, argv, opts->revs, &s_r_opt); | |
7f13334e JN |
197 | } |
198 | ||
199 | if (argc > 1) | |
200 | usage_with_options(usage_str, options); | |
03a4e260 JS |
201 | |
202 | /* These option values will be free()d */ | |
203 | opts->gpg_sign = xstrdup_or_null(opts->gpg_sign); | |
204 | opts->strategy = xstrdup_or_null(opts->strategy); | |
2863584f | 205 | |
3e7dd992 NTND |
206 | if (cmd == 'q') { |
207 | int ret = sequencer_remove_state(opts); | |
208 | if (!ret) | |
f4a4b9ac | 209 | remove_branch_state(the_repository, 0); |
3e7dd992 NTND |
210 | return ret; |
211 | } | |
2863584f | 212 | if (cmd == 'c') |
f11c9580 | 213 | return sequencer_continue(the_repository, opts); |
2863584f | 214 | if (cmd == 'a') |
005af339 | 215 | return sequencer_rollback(the_repository, opts); |
de81ca3f RA |
216 | if (cmd == 's') |
217 | return sequencer_skip(the_repository, opts); | |
f11c9580 | 218 | return sequencer_pick_revisions(the_repository, opts); |
9509af68 JS |
219 | } |
220 | ||
9509af68 JS |
221 | int cmd_revert(int argc, const char **argv, const char *prefix) |
222 | { | |
ee624c0d | 223 | struct replay_opts opts = REPLAY_OPTS_INIT; |
cf3e2486 | 224 | int res; |
80e1f791 | 225 | |
9509af68 | 226 | if (isatty(0)) |
80e1f791 | 227 | opts.edit = 1; |
644a3690 | 228 | opts.action = REPLAY_REVERT; |
28d6daed | 229 | sequencer_init_config(&opts); |
2863584f | 230 | res = run_sequencer(argc, argv, &opts); |
cf3e2486 RR |
231 | if (res < 0) |
232 | die(_("revert failed")); | |
233 | return res; | |
9509af68 JS |
234 | } |
235 | ||
236 | int cmd_cherry_pick(int argc, const char **argv, const char *prefix) | |
237 | { | |
ee624c0d | 238 | struct replay_opts opts = REPLAY_OPTS_INIT; |
cf3e2486 | 239 | int res; |
80e1f791 | 240 | |
644a3690 | 241 | opts.action = REPLAY_PICK; |
28d6daed | 242 | sequencer_init_config(&opts); |
2863584f | 243 | res = run_sequencer(argc, argv, &opts); |
cf3e2486 RR |
244 | if (res < 0) |
245 | die(_("cherry-pick failed")); | |
246 | return res; | |
9509af68 | 247 | } |