]>
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[] = { |
2130bf3c | 22 | N_("git revert [options] <commit-ish>..."), |
a3b26dc7 | 23 | N_("git revert <subcommand>"), |
f8103794 PH |
24 | NULL |
25 | }; | |
9509af68 | 26 | |
f8103794 | 27 | static const char * const cherry_pick_usage[] = { |
2130bf3c | 28 | N_("git cherry-pick [options] <commit-ish>..."), |
a3b26dc7 | 29 | N_("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 | ||
9fe3edc4 | 57 | LAST_ARG_MUST_BE_NULL |
9044143f RR |
58 | static void verify_opt_compatible(const char *me, const char *base_opt, ...) |
59 | { | |
60 | const char *this_opt; | |
61 | va_list ap; | |
62 | ||
63 | va_start(ap, base_opt); | |
64 | while ((this_opt = va_arg(ap, const char *))) { | |
65 | if (va_arg(ap, int)) | |
66 | break; | |
67 | } | |
68 | va_end(ap); | |
69 | ||
70 | if (this_opt) | |
71 | die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt); | |
72 | } | |
73 | ||
80e1f791 | 74 | static void parse_args(int argc, const char **argv, struct replay_opts *opts) |
9509af68 | 75 | { |
80e1f791 | 76 | const char * const * usage_str = revert_or_cherry_pick_usage(opts); |
9044143f | 77 | const char *me = action_name(opts); |
84d83f64 | 78 | int cmd = 0; |
f8103794 | 79 | struct option options[] = { |
84d83f64 SB |
80 | OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'), |
81 | OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'), | |
82 | OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'), | |
83 | OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")), | |
84 | OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")), | |
7cfc6057 | 85 | OPT_NOOP_NOARG('r', NULL), |
84d83f64 | 86 | OPT_BOOL('s', "signoff", &opts->signoff, N_("add Signed-off-by:")), |
a3b26dc7 | 87 | OPT_INTEGER('m', "mainline", &opts->mainline, N_("parent number")), |
80e1f791 | 88 | OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto), |
a3b26dc7 NTND |
89 | OPT_STRING(0, "strategy", &opts->strategy, N_("strategy"), N_("merge strategy")), |
90 | OPT_CALLBACK('X', "strategy-option", &opts, N_("option"), | |
91 | N_("option for merge strategy"), option_parse_x), | |
e703d711 | 92 | { OPTION_STRING, 'S', "gpg-sign", &opts->gpg_sign, N_("key-id"), |
3253553e | 93 | N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, |
f8103794 | 94 | OPT_END(), |
c62f6ec3 JH |
95 | OPT_END(), |
96 | OPT_END(), | |
df478b74 | 97 | OPT_END(), |
b27cfb0d | 98 | OPT_END(), |
4bee9584 | 99 | OPT_END(), |
f8103794 PH |
100 | }; |
101 | ||
644a3690 | 102 | if (opts->action == REPLAY_PICK) { |
c62f6ec3 | 103 | struct option cp_extra[] = { |
84d83f64 SB |
104 | OPT_BOOL('x', NULL, &opts->record_origin, N_("append commit name")), |
105 | OPT_BOOL(0, "ff", &opts->allow_ff, N_("allow fast-forward")), | |
106 | OPT_BOOL(0, "allow-empty", &opts->allow_empty, N_("preserve initially empty commits")), | |
107 | OPT_BOOL(0, "allow-empty-message", &opts->allow_empty_message, N_("allow commits with empty messages")), | |
108 | OPT_BOOL(0, "keep-redundant-commits", &opts->keep_redundant_commits, N_("keep redundant, empty commits")), | |
c62f6ec3 JH |
109 | OPT_END(), |
110 | }; | |
111 | if (parse_options_concat(options, ARRAY_SIZE(options), cp_extra)) | |
f30f71ce | 112 | die(_("program error")); |
c62f6ec3 JH |
113 | } |
114 | ||
7f13334e JN |
115 | argc = parse_options(argc, argv, NULL, options, usage_str, |
116 | PARSE_OPT_KEEP_ARGV0 | | |
117 | PARSE_OPT_KEEP_UNKNOWN); | |
26ae337b | 118 | |
b27cfb0d NH |
119 | /* implies allow_empty */ |
120 | if (opts->keep_redundant_commits) | |
121 | opts->allow_empty = 1; | |
122 | ||
26ae337b | 123 | /* Set the subcommand */ |
84d83f64 | 124 | if (cmd == 'q') |
f80a8726 | 125 | opts->subcommand = REPLAY_REMOVE_STATE; |
84d83f64 | 126 | else if (cmd == 'c') |
5a5d80f4 | 127 | opts->subcommand = REPLAY_CONTINUE; |
84d83f64 | 128 | else if (cmd == 'a') |
539047c1 | 129 | opts->subcommand = REPLAY_ROLLBACK; |
26ae337b RR |
130 | else |
131 | opts->subcommand = REPLAY_NONE; | |
132 | ||
133 | /* Check for incompatible command line arguments */ | |
5a5d80f4 RR |
134 | if (opts->subcommand != REPLAY_NONE) { |
135 | char *this_operation; | |
f80a8726 JN |
136 | if (opts->subcommand == REPLAY_REMOVE_STATE) |
137 | this_operation = "--quit"; | |
539047c1 | 138 | else if (opts->subcommand == REPLAY_CONTINUE) |
5a5d80f4 | 139 | this_operation = "--continue"; |
539047c1 JN |
140 | else { |
141 | assert(opts->subcommand == REPLAY_ROLLBACK); | |
142 | this_operation = "--abort"; | |
143 | } | |
5a5d80f4 RR |
144 | |
145 | verify_opt_compatible(me, this_operation, | |
26ae337b RR |
146 | "--no-commit", opts->no_commit, |
147 | "--signoff", opts->signoff, | |
148 | "--mainline", opts->mainline, | |
149 | "--strategy", opts->strategy ? 1 : 0, | |
150 | "--strategy-option", opts->xopts ? 1 : 0, | |
151 | "-x", opts->record_origin, | |
152 | "--ff", opts->allow_ff, | |
153 | NULL); | |
154 | } | |
155 | ||
9044143f RR |
156 | if (opts->allow_ff) |
157 | verify_opt_compatible(me, "--ff", | |
158 | "--signoff", opts->signoff, | |
159 | "--no-commit", opts->no_commit, | |
160 | "-x", opts->record_origin, | |
161 | "--edit", opts->edit, | |
162 | NULL); | |
7f13334e JN |
163 | |
164 | if (opts->subcommand != REPLAY_NONE) { | |
165 | opts->revs = NULL; | |
166 | } else { | |
6d5b93f2 | 167 | struct setup_revision_opt s_r_opt; |
7f13334e JN |
168 | opts->revs = xmalloc(sizeof(*opts->revs)); |
169 | init_revisions(opts->revs, NULL); | |
a73e22e9 | 170 | opts->revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED; |
7f13334e JN |
171 | if (argc < 2) |
172 | usage_with_options(usage_str, options); | |
d644c550 JK |
173 | if (!strcmp(argv[1], "-")) |
174 | argv[1] = "@{-1}"; | |
6d5b93f2 CB |
175 | memset(&s_r_opt, 0, sizeof(s_r_opt)); |
176 | s_r_opt.assume_dashdash = 1; | |
177 | argc = setup_revisions(argc, argv, opts->revs, &s_r_opt); | |
7f13334e JN |
178 | } |
179 | ||
180 | if (argc > 1) | |
181 | usage_with_options(usage_str, options); | |
9509af68 JS |
182 | } |
183 | ||
9509af68 JS |
184 | int cmd_revert(int argc, const char **argv, const char *prefix) |
185 | { | |
80e1f791 | 186 | struct replay_opts opts; |
cf3e2486 | 187 | int res; |
80e1f791 RR |
188 | |
189 | memset(&opts, 0, sizeof(opts)); | |
9509af68 | 190 | if (isatty(0)) |
80e1f791 | 191 | opts.edit = 1; |
644a3690 | 192 | opts.action = REPLAY_REVERT; |
89641472 RR |
193 | git_config(git_default_config, NULL); |
194 | parse_args(argc, argv, &opts); | |
043a4492 | 195 | res = sequencer_pick_revisions(&opts); |
cf3e2486 RR |
196 | if (res < 0) |
197 | die(_("revert failed")); | |
198 | return res; | |
9509af68 JS |
199 | } |
200 | ||
201 | int cmd_cherry_pick(int argc, const char **argv, const char *prefix) | |
202 | { | |
80e1f791 | 203 | struct replay_opts opts; |
cf3e2486 | 204 | int res; |
80e1f791 RR |
205 | |
206 | memset(&opts, 0, sizeof(opts)); | |
644a3690 | 207 | opts.action = REPLAY_PICK; |
89641472 RR |
208 | git_config(git_default_config, NULL); |
209 | parse_args(argc, argv, &opts); | |
043a4492 | 210 | res = sequencer_pick_revisions(&opts); |
cf3e2486 RR |
211 | if (res < 0) |
212 | die(_("cherry-pick failed")); | |
213 | return res; | |
9509af68 | 214 | } |