blob: 51776abea63adf400e02339a716531feb26ad03e [file] [log] [blame]
Johannes Schindelin9509af62007-03-01 05:26:30 +01001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Johannes Schindelin9509af62007-03-01 05:26:30 +01003#include "builtin.h"
Pierre Habouzitf8103792007-10-07 23:02:29 +02004#include "parse-options.h"
Jeff King0f2d4472008-03-03 01:30:56 -05005#include "diff.h"
6#include "revision.h"
Abhijit Menon-Senaa1a0112008-08-10 17:18:55 +05307#include "rerere.h"
Ramkumar Ramachandra04d3d3c2011-08-04 16:09:08 +05308#include "dir.h"
Ramkumar Ramachandra26ae3372011-08-04 16:09:11 +05309#include "sequencer.h"
Nguyễn Thái Ngọc Duy3e7dd992018-08-16 18:06:08 +020010#include "branch.h"
Johannes Schindelin9509af62007-03-01 05:26:30 +010011
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
Pierre Habouzitf8103792007-10-07 23:02:29 +020023static const char * const revert_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -070024 N_("git revert [<options>] <commit-ish>..."),
Nguyễn Thái Ngọc Duya3b26dc2012-08-20 19:32:41 +070025 N_("git revert <subcommand>"),
Pierre Habouzitf8103792007-10-07 23:02:29 +020026 NULL
27};
Johannes Schindelin9509af62007-03-01 05:26:30 +010028
Pierre Habouzitf8103792007-10-07 23:02:29 +020029static const char * const cherry_pick_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -070030 N_("git cherry-pick [<options>] <commit-ish>..."),
Nguyễn Thái Ngọc Duya3b26dc2012-08-20 19:32:41 +070031 N_("git cherry-pick <subcommand>"),
Pierre Habouzitf8103792007-10-07 23:02:29 +020032 NULL
33};
Johannes Schindelin9509af62007-03-01 05:26:30 +010034
Ramkumar Ramachandra80e1f792011-08-04 16:09:05 +053035static const char *action_name(const struct replay_opts *opts)
36{
Ramkumar Ramachandra644a3692012-01-11 23:45:56 +053037 return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
Ramkumar Ramachandra80e1f792011-08-04 16:09:05 +053038}
39
Ramkumar Ramachandra80e1f792011-08-04 16:09:05 +053040static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts)
Jonathan Niedere0ef8492010-06-14 00:32:09 -050041{
Ramkumar Ramachandra644a3692012-01-11 23:45:56 +053042 return opts->action == REPLAY_REVERT ? revert_usage : cherry_pick_usage;
Jonathan Niedere0ef8492010-06-14 00:32:09 -050043}
44
Jonathan Nieder67ac1e12010-12-10 18:51:44 -060045static int option_parse_x(const struct option *opt,
46 const char *arg, int unset)
47{
Ramkumar Ramachandra80e1f792011-08-04 16:09:05 +053048 struct replay_opts **opts_ptr = opt->value;
49 struct replay_opts *opts = *opts_ptr;
50
Jonathan Nieder67ac1e12010-12-10 18:51:44 -060051 if (unset)
52 return 0;
53
Ramkumar Ramachandra80e1f792011-08-04 16:09:05 +053054 ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
55 opts->xopts[opts->xopts_nr++] = xstrdup(arg);
Jonathan Nieder67ac1e12010-12-10 18:51:44 -060056 return 0;
57}
58
Jeff Kingb16a9912017-03-15 12:56:23 -040059static 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)
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +010072 return error(_("option `%s' expects a number greater than zero"),
73 opt->long_name);
Jeff Kingb16a9912017-03-15 12:56:23 -040074
75 return 0;
76}
77
Ramsay Jones9fe3edc2013-07-18 21:02:12 +010078LAST_ARG_MUST_BE_NULL
Ramkumar Ramachandra90441432011-08-04 16:09:07 +053079static void verify_opt_compatible(const char *me, const char *base_opt, ...)
Johannes Schindelin9509af62007-03-01 05:26:30 +010080{
Ramkumar Ramachandra90441432011-08-04 16:09:07 +053081 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
Johannes Schindelin28635842016-10-21 14:24:55 +020095static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
Johannes Schindelin9509af62007-03-01 05:26:30 +010096{
Ramkumar Ramachandra80e1f792011-08-04 16:09:05 +053097 const char * const * usage_str = revert_or_cherry_pick_usage(opts);
Ramkumar Ramachandra90441432011-08-04 16:09:07 +053098 const char *me = action_name(opts);
Denton Liu1a2b9852019-04-17 11:23:30 +010099 const char *cleanup_arg = NULL;
Stefan Beller84d83f62013-08-03 13:51:26 +0200100 int cmd = 0;
Jeff King023ff392016-07-05 16:44:47 -0400101 struct option base_options[] = {
Stefan Beller84d83f62013-08-03 13:51:26 +0200102 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'),
Rohit Ashiwalde81ca32019-07-02 14:41:28 +0530105 OPT_CMDMODE(0, "skip", &cmd, N_("skip current commit and continue"), 's'),
Denton Liu1a2b9852019-04-17 11:23:30 +0100106 OPT_CLEANUP(&cleanup_arg),
Stefan Beller84d83f62013-08-03 13:51:26 +0200107 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")),
René Scharfe7cfc6052011-09-28 19:47:50 +0200109 OPT_NOOP_NOARG('r', NULL),
Bradley M. Kuhn3abd4a62020-10-19 18:03:55 -0700110 OPT_BOOL('s', "signoff", &opts->signoff, N_("add a Signed-off-by trailer")),
Jeff Kingb16a9912017-03-15 12:56:23 -0400111 OPT_CALLBACK('m', "mainline", opts, N_("parent-number"),
112 N_("select mainline parent"), option_parse_m),
Ramkumar Ramachandra80e1f792011-08-04 16:09:05 +0530113 OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
Nguyễn Thái Ngọc Duya3b26dc2012-08-20 19:32:41 +0700114 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),
Junio C Hamanoe703d712014-03-23 15:58:12 -0700117 { OPTION_STRING, 'S', "gpg-sign", &opts->gpg_sign, N_("key-id"),
Nicolas Vigier32535532014-01-24 00:50:58 +0000118 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
Jeff King023ff392016-07-05 16:44:47 -0400119 OPT_END()
Pierre Habouzitf8103792007-10-07 23:02:29 +0200120 };
Jeff King023ff392016-07-05 16:44:47 -0400121 struct option *options = base_options;
Johannes Schindelin9509af62007-03-01 05:26:30 +0100122
Ramkumar Ramachandra644a3692012-01-11 23:45:56 +0530123 if (opts->action == REPLAY_PICK) {
Junio C Hamanoc62f6ec2010-03-06 21:34:42 +0100124 struct option cp_extra[] = {
Stefan Beller84d83f62013-08-03 13:51:26 +0200125 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")),
Junio C Hamanoc62f6ec2010-03-06 21:34:42 +0100130 OPT_END(),
131 };
Jeff King023ff392016-07-05 16:44:47 -0400132 options = parse_options_concat(options, cp_extra);
Junio C Hamanoc62f6ec2010-03-06 21:34:42 +0100133 }
134
Jonathan Nieder7f133342011-08-13 12:06:23 -0500135 argc = parse_options(argc, argv, NULL, options, usage_str,
136 PARSE_OPT_KEEP_ARGV0 |
137 PARSE_OPT_KEEP_UNKNOWN);
Ramkumar Ramachandra26ae3372011-08-04 16:09:11 +0530138
Derrick Stolee516680b2021-09-08 11:24:01 +0000139 prepare_repo_settings(the_repository);
140 the_repository->settings.command_requires_full_index = 0;
141
Neil Hormanb27cfb02012-04-20 10:36:15 -0400142 /* implies allow_empty */
143 if (opts->keep_redundant_commits)
144 opts->allow_empty = 1;
145
Denton Liu1a2b9852019-04-17 11:23:30 +0100146 if (cleanup_arg) {
147 opts->default_msg_cleanup = get_cleanup_mode(cleanup_arg, 1);
148 opts->explicit_cleanup = 1;
149 }
150
Ramkumar Ramachandra26ae3372011-08-04 16:09:11 +0530151 /* Check for incompatible command line arguments */
Johannes Schindelin28635842016-10-21 14:24:55 +0200152 if (cmd) {
Ramkumar Ramachandra5a5d80f2011-08-04 16:09:15 +0530153 char *this_operation;
Johannes Schindelin28635842016-10-21 14:24:55 +0200154 if (cmd == 'q')
Jonathan Niederf80a8722011-11-22 05:14:29 -0600155 this_operation = "--quit";
Johannes Schindelin28635842016-10-21 14:24:55 +0200156 else if (cmd == 'c')
Ramkumar Ramachandra5a5d80f2011-08-04 16:09:15 +0530157 this_operation = "--continue";
Rohit Ashiwalde81ca32019-07-02 14:41:28 +0530158 else if (cmd == 's')
159 this_operation = "--skip";
Jonathan Nieder539047c2011-11-22 19:27:21 -0600160 else {
Johannes Schindelin28635842016-10-21 14:24:55 +0200161 assert(cmd == 'a');
Jonathan Nieder539047c2011-11-22 19:27:21 -0600162 this_operation = "--abort";
163 }
Ramkumar Ramachandra5a5d80f2011-08-04 16:09:15 +0530164
165 verify_opt_compatible(me, this_operation,
Ramkumar Ramachandra26ae3372011-08-04 16:09:11 +0530166 "--no-commit", opts->no_commit,
167 "--signoff", opts->signoff,
168 "--mainline", opts->mainline,
169 "--strategy", opts->strategy ? 1 : 0,
170 "--strategy-option", opts->xopts ? 1 : 0,
171 "-x", opts->record_origin,
172 "--ff", opts->allow_ff,
Phillip Woodf826fb72017-08-02 11:44:20 +0100173 "--rerere-autoupdate", opts->allow_rerere_auto == RERERE_AUTOUPDATE,
174 "--no-rerere-autoupdate", opts->allow_rerere_auto == RERERE_NOAUTOUPDATE,
Ramkumar Ramachandra26ae3372011-08-04 16:09:11 +0530175 NULL);
176 }
177
Elijah Newren14c45862020-11-02 23:45:34 +0000178 if (!opts->strategy && opts->default_strategy) {
179 opts->strategy = opts->default_strategy;
180 opts->default_strategy = NULL;
181 }
182
Ramkumar Ramachandra90441432011-08-04 16:09:07 +0530183 if (opts->allow_ff)
184 verify_opt_compatible(me, "--ff",
185 "--signoff", opts->signoff,
186 "--no-commit", opts->no_commit,
187 "-x", opts->record_origin,
Elijah Newren39edfd52021-03-31 06:52:20 +0000188 "--edit", opts->edit > 0,
Ramkumar Ramachandra90441432011-08-04 16:09:07 +0530189 NULL);
Jonathan Nieder7f133342011-08-13 12:06:23 -0500190
Johannes Schindelin28635842016-10-21 14:24:55 +0200191 if (cmd) {
Jonathan Nieder7f133342011-08-13 12:06:23 -0500192 opts->revs = NULL;
193 } else {
Clemens Buchacher6d5b93f2012-04-14 21:04:48 +0200194 struct setup_revision_opt s_r_opt;
Jonathan Nieder7f133342011-08-13 12:06:23 -0500195 opts->revs = xmalloc(sizeof(*opts->revs));
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +0200196 repo_init_revisions(the_repository, opts->revs, NULL);
Patrick Steinhardt29ef1f22021-08-05 13:25:24 +0200197 opts->revs->no_walk = 1;
198 opts->revs->unsorted_input = 1;
Jonathan Nieder7f133342011-08-13 12:06:23 -0500199 if (argc < 2)
200 usage_with_options(usage_str, options);
Jeff Kingd644c552013-10-10 12:41:17 -0400201 if (!strcmp(argv[1], "-"))
202 argv[1] = "@{-1}";
Clemens Buchacher6d5b93f2012-04-14 21:04:48 +0200203 memset(&s_r_opt, 0, sizeof(s_r_opt));
204 s_r_opt.assume_dashdash = 1;
205 argc = setup_revisions(argc, argv, opts->revs, &s_r_opt);
Jonathan Nieder7f133342011-08-13 12:06:23 -0500206 }
207
208 if (argc > 1)
209 usage_with_options(usage_str, options);
Johannes Schindelin03a4e262016-10-21 14:24:13 +0200210
211 /* These option values will be free()d */
212 opts->gpg_sign = xstrdup_or_null(opts->gpg_sign);
213 opts->strategy = xstrdup_or_null(opts->strategy);
Elijah Newren14c45862020-11-02 23:45:34 +0000214 if (!opts->strategy && getenv("GIT_TEST_MERGE_ALGORITHM"))
215 opts->strategy = xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
Johannes Schindelin28635842016-10-21 14:24:55 +0200216
Nguyễn Thái Ngọc Duy3e7dd992018-08-16 18:06:08 +0200217 if (cmd == 'q') {
218 int ret = sequencer_remove_state(opts);
219 if (!ret)
Nguyễn Thái Ngọc Duyf4a4b9a2019-03-29 17:38:59 +0700220 remove_branch_state(the_repository, 0);
Nguyễn Thái Ngọc Duy3e7dd992018-08-16 18:06:08 +0200221 return ret;
222 }
Johannes Schindelin28635842016-10-21 14:24:55 +0200223 if (cmd == 'c')
Nguyễn Thái Ngọc Duyf11c9582018-11-10 06:48:56 +0100224 return sequencer_continue(the_repository, opts);
Johannes Schindelin28635842016-10-21 14:24:55 +0200225 if (cmd == 'a')
Nguyễn Thái Ngọc Duy005af332018-11-10 06:48:57 +0100226 return sequencer_rollback(the_repository, opts);
Rohit Ashiwalde81ca32019-07-02 14:41:28 +0530227 if (cmd == 's')
228 return sequencer_skip(the_repository, opts);
Nguyễn Thái Ngọc Duyf11c9582018-11-10 06:48:56 +0100229 return sequencer_pick_revisions(the_repository, opts);
Johannes Schindelin9509af62007-03-01 05:26:30 +0100230}
231
Johannes Schindelin9509af62007-03-01 05:26:30 +0100232int cmd_revert(int argc, const char **argv, const char *prefix)
233{
Johannes Schindelinee624c02016-10-14 15:15:56 +0200234 struct replay_opts opts = REPLAY_OPTS_INIT;
Ramkumar Ramachandracf3e2482011-08-04 16:09:16 +0530235 int res;
Ramkumar Ramachandra80e1f792011-08-04 16:09:05 +0530236
Ramkumar Ramachandra644a3692012-01-11 23:45:56 +0530237 opts.action = REPLAY_REVERT;
Phillip Wood28d6dae2017-12-13 11:46:21 +0000238 sequencer_init_config(&opts);
Johannes Schindelin28635842016-10-21 14:24:55 +0200239 res = run_sequencer(argc, argv, &opts);
Ramkumar Ramachandracf3e2482011-08-04 16:09:16 +0530240 if (res < 0)
241 die(_("revert failed"));
242 return res;
Johannes Schindelin9509af62007-03-01 05:26:30 +0100243}
244
245int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
246{
Johannes Schindelinee624c02016-10-14 15:15:56 +0200247 struct replay_opts opts = REPLAY_OPTS_INIT;
Ramkumar Ramachandracf3e2482011-08-04 16:09:16 +0530248 int res;
Ramkumar Ramachandra80e1f792011-08-04 16:09:05 +0530249
Ramkumar Ramachandra644a3692012-01-11 23:45:56 +0530250 opts.action = REPLAY_PICK;
Phillip Wood28d6dae2017-12-13 11:46:21 +0000251 sequencer_init_config(&opts);
Johannes Schindelin28635842016-10-21 14:24:55 +0200252 res = run_sequencer(argc, argv, &opts);
Ramkumar Ramachandracf3e2482011-08-04 16:09:16 +0530253 if (res < 0)
254 die(_("cherry-pick failed"));
255 return res;
Johannes Schindelin9509af62007-03-01 05:26:30 +0100256}