2007-03-01 07:26:30 +03:00
|
|
|
#include "cache.h"
|
2017-06-14 21:07:36 +03:00
|
|
|
#include "config.h"
|
2007-03-01 07:26:30 +03:00
|
|
|
#include "builtin.h"
|
2007-10-08 01:02:29 +04:00
|
|
|
#include "parse-options.h"
|
2008-03-03 09:30:56 +03:00
|
|
|
#include "diff.h"
|
|
|
|
#include "revision.h"
|
2008-08-10 15:48:55 +04:00
|
|
|
#include "rerere.h"
|
2011-08-04 14:39:08 +04:00
|
|
|
#include "dir.h"
|
2011-08-04 14:39:11 +04:00
|
|
|
#include "sequencer.h"
|
2018-08-16 19:06:08 +03:00
|
|
|
#include "branch.h"
|
2007-03-01 07:26:30 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This implements the builtins revert and cherry-pick.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Johannes E. Schindelin
|
|
|
|
*
|
|
|
|
* Based on git-revert.sh, which is
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005 Linus Torvalds
|
|
|
|
* Copyright (c) 2005 Junio C Hamano
|
|
|
|
*/
|
|
|
|
|
2007-10-08 01:02:29 +04:00
|
|
|
static const char * const revert_usage[] = {
|
2015-01-13 10:44:47 +03:00
|
|
|
N_("git revert [<options>] <commit-ish>..."),
|
2012-08-20 16:32:41 +04:00
|
|
|
N_("git revert <subcommand>"),
|
2007-10-08 01:02:29 +04:00
|
|
|
NULL
|
|
|
|
};
|
2007-03-01 07:26:30 +03:00
|
|
|
|
2007-10-08 01:02:29 +04:00
|
|
|
static const char * const cherry_pick_usage[] = {
|
2015-01-13 10:44:47 +03:00
|
|
|
N_("git cherry-pick [<options>] <commit-ish>..."),
|
2012-08-20 16:32:41 +04:00
|
|
|
N_("git cherry-pick <subcommand>"),
|
2007-10-08 01:02:29 +04:00
|
|
|
NULL
|
|
|
|
};
|
2007-03-01 07:26:30 +03:00
|
|
|
|
2011-08-04 14:39:05 +04:00
|
|
|
static const char *action_name(const struct replay_opts *opts)
|
|
|
|
{
|
2012-01-11 22:15:56 +04:00
|
|
|
return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
|
2011-08-04 14:39:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts)
|
2010-06-14 09:32:09 +04:00
|
|
|
{
|
2012-01-11 22:15:56 +04:00
|
|
|
return opts->action == REPLAY_REVERT ? revert_usage : cherry_pick_usage;
|
2010-06-14 09:32:09 +04:00
|
|
|
}
|
|
|
|
|
2010-12-11 03:51:44 +03:00
|
|
|
static int option_parse_x(const struct option *opt,
|
|
|
|
const char *arg, int unset)
|
|
|
|
{
|
2011-08-04 14:39:05 +04:00
|
|
|
struct replay_opts **opts_ptr = opt->value;
|
|
|
|
struct replay_opts *opts = *opts_ptr;
|
|
|
|
|
2010-12-11 03:51:44 +03:00
|
|
|
if (unset)
|
|
|
|
return 0;
|
|
|
|
|
2011-08-04 14:39:05 +04:00
|
|
|
ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
|
|
|
|
opts->xopts[opts->xopts_nr++] = xstrdup(arg);
|
2010-12-11 03:51:44 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-15 19:56:23 +03:00
|
|
|
static int option_parse_m(const struct option *opt,
|
|
|
|
const char *arg, int unset)
|
|
|
|
{
|
|
|
|
struct replay_opts *replay = opt->value;
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
if (unset) {
|
|
|
|
replay->mainline = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
replay->mainline = strtol(arg, &end, 10);
|
|
|
|
if (*end || replay->mainline <= 0)
|
2018-11-10 08:16:11 +03:00
|
|
|
return error(_("option `%s' expects a number greater than zero"),
|
|
|
|
opt->long_name);
|
2017-03-15 19:56:23 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-19 00:02:12 +04:00
|
|
|
LAST_ARG_MUST_BE_NULL
|
2011-08-04 14:39:07 +04:00
|
|
|
static void verify_opt_compatible(const char *me, const char *base_opt, ...)
|
|
|
|
{
|
|
|
|
const char *this_opt;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, base_opt);
|
|
|
|
while ((this_opt = va_arg(ap, const char *))) {
|
|
|
|
if (va_arg(ap, int))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (this_opt)
|
|
|
|
die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
|
|
|
|
}
|
|
|
|
|
2016-10-21 15:24:55 +03:00
|
|
|
static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
|
2007-03-01 07:26:30 +03:00
|
|
|
{
|
2011-08-04 14:39:05 +04:00
|
|
|
const char * const * usage_str = revert_or_cherry_pick_usage(opts);
|
2011-08-04 14:39:07 +04:00
|
|
|
const char *me = action_name(opts);
|
2013-08-03 15:51:26 +04:00
|
|
|
int cmd = 0;
|
2016-07-05 23:44:47 +03:00
|
|
|
struct option base_options[] = {
|
2013-08-03 15:51:26 +04:00
|
|
|
OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'),
|
|
|
|
OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'),
|
|
|
|
OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'),
|
|
|
|
OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
|
|
|
|
OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")),
|
2011-09-28 21:47:50 +04:00
|
|
|
OPT_NOOP_NOARG('r', NULL),
|
2013-08-03 15:51:26 +04:00
|
|
|
OPT_BOOL('s', "signoff", &opts->signoff, N_("add Signed-off-by:")),
|
2017-03-15 19:56:23 +03:00
|
|
|
OPT_CALLBACK('m', "mainline", opts, N_("parent-number"),
|
|
|
|
N_("select mainline parent"), option_parse_m),
|
2011-08-04 14:39:05 +04:00
|
|
|
OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
|
2012-08-20 16:32:41 +04:00
|
|
|
OPT_STRING(0, "strategy", &opts->strategy, N_("strategy"), N_("merge strategy")),
|
|
|
|
OPT_CALLBACK('X', "strategy-option", &opts, N_("option"),
|
|
|
|
N_("option for merge strategy"), option_parse_x),
|
2014-03-24 02:58:12 +04:00
|
|
|
{ OPTION_STRING, 'S', "gpg-sign", &opts->gpg_sign, N_("key-id"),
|
2014-01-24 04:50:58 +04:00
|
|
|
N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
|
2016-07-05 23:44:47 +03:00
|
|
|
OPT_END()
|
2007-10-08 01:02:29 +04:00
|
|
|
};
|
2016-07-05 23:44:47 +03:00
|
|
|
struct option *options = base_options;
|
2007-10-08 01:02:29 +04:00
|
|
|
|
2012-01-11 22:15:56 +04:00
|
|
|
if (opts->action == REPLAY_PICK) {
|
2010-03-06 23:34:42 +03:00
|
|
|
struct option cp_extra[] = {
|
2013-08-03 15:51:26 +04:00
|
|
|
OPT_BOOL('x', NULL, &opts->record_origin, N_("append commit name")),
|
|
|
|
OPT_BOOL(0, "ff", &opts->allow_ff, N_("allow fast-forward")),
|
|
|
|
OPT_BOOL(0, "allow-empty", &opts->allow_empty, N_("preserve initially empty commits")),
|
|
|
|
OPT_BOOL(0, "allow-empty-message", &opts->allow_empty_message, N_("allow commits with empty messages")),
|
|
|
|
OPT_BOOL(0, "keep-redundant-commits", &opts->keep_redundant_commits, N_("keep redundant, empty commits")),
|
2010-03-06 23:34:42 +03:00
|
|
|
OPT_END(),
|
|
|
|
};
|
2016-07-05 23:44:47 +03:00
|
|
|
options = parse_options_concat(options, cp_extra);
|
2010-03-06 23:34:42 +03:00
|
|
|
}
|
|
|
|
|
2011-08-13 21:06:23 +04:00
|
|
|
argc = parse_options(argc, argv, NULL, options, usage_str,
|
|
|
|
PARSE_OPT_KEEP_ARGV0 |
|
|
|
|
PARSE_OPT_KEEP_UNKNOWN);
|
2011-08-04 14:39:11 +04:00
|
|
|
|
git-cherry-pick: Add keep-redundant-commits option
The git-cherry-pick --allow-empty command by default only preserves empty
commits that were originally empty, i.e only those commits for which
<commit>^{tree} and <commit>^^{tree} are equal. By default commits which are
non-empty, but were made empty by the inclusion of a prior commit on the current
history are filtered out. This option allows us to override that behavior and
include redundant commits as empty commits in the change history.
Note that this patch changes the default behavior of git cherry-pick slightly.
Prior to this patch all commits in a cherry-pick sequence were applied and git
commit was run. The implication here was that, if a commit was redundant, and
the commit did not trigger the fast forward logic, the git commit operation, and
therefore the git cherry-pick operation would fail, displaying the cherry pick
advice (i.e. run git commit --allow-empty). With this patch however, such
redundant commits are automatically skipped without stopping, unless
--keep-redundant-commits is specified, in which case, they are automatically
applied as empty commits.
Signed-off-by: Neil Horman <nhorman@tuxdriver.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2012-04-20 18:36:15 +04:00
|
|
|
/* implies allow_empty */
|
|
|
|
if (opts->keep_redundant_commits)
|
|
|
|
opts->allow_empty = 1;
|
|
|
|
|
2011-08-04 14:39:11 +04:00
|
|
|
/* Check for incompatible command line arguments */
|
2016-10-21 15:24:55 +03:00
|
|
|
if (cmd) {
|
2011-08-04 14:39:15 +04:00
|
|
|
char *this_operation;
|
2016-10-21 15:24:55 +03:00
|
|
|
if (cmd == 'q')
|
2011-11-22 15:14:29 +04:00
|
|
|
this_operation = "--quit";
|
2016-10-21 15:24:55 +03:00
|
|
|
else if (cmd == 'c')
|
2011-08-04 14:39:15 +04:00
|
|
|
this_operation = "--continue";
|
2011-11-23 05:27:21 +04:00
|
|
|
else {
|
2016-10-21 15:24:55 +03:00
|
|
|
assert(cmd == 'a');
|
2011-11-23 05:27:21 +04:00
|
|
|
this_operation = "--abort";
|
|
|
|
}
|
2011-08-04 14:39:15 +04:00
|
|
|
|
|
|
|
verify_opt_compatible(me, this_operation,
|
2011-08-04 14:39:11 +04:00
|
|
|
"--no-commit", opts->no_commit,
|
|
|
|
"--signoff", opts->signoff,
|
|
|
|
"--mainline", opts->mainline,
|
|
|
|
"--strategy", opts->strategy ? 1 : 0,
|
|
|
|
"--strategy-option", opts->xopts ? 1 : 0,
|
|
|
|
"-x", opts->record_origin,
|
|
|
|
"--ff", opts->allow_ff,
|
2017-08-02 13:44:20 +03:00
|
|
|
"--rerere-autoupdate", opts->allow_rerere_auto == RERERE_AUTOUPDATE,
|
|
|
|
"--no-rerere-autoupdate", opts->allow_rerere_auto == RERERE_NOAUTOUPDATE,
|
2011-08-04 14:39:11 +04:00
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2011-08-04 14:39:07 +04:00
|
|
|
if (opts->allow_ff)
|
|
|
|
verify_opt_compatible(me, "--ff",
|
|
|
|
"--signoff", opts->signoff,
|
|
|
|
"--no-commit", opts->no_commit,
|
|
|
|
"-x", opts->record_origin,
|
|
|
|
"--edit", opts->edit,
|
|
|
|
NULL);
|
2011-08-13 21:06:23 +04:00
|
|
|
|
2016-10-21 15:24:55 +03:00
|
|
|
if (cmd) {
|
2011-08-13 21:06:23 +04:00
|
|
|
opts->revs = NULL;
|
|
|
|
} else {
|
2012-04-14 23:04:48 +04:00
|
|
|
struct setup_revision_opt s_r_opt;
|
2011-08-13 21:06:23 +04:00
|
|
|
opts->revs = xmalloc(sizeof(*opts->revs));
|
2018-09-21 18:57:38 +03:00
|
|
|
repo_init_revisions(the_repository, opts->revs, NULL);
|
2012-08-29 10:15:56 +04:00
|
|
|
opts->revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
|
2011-08-13 21:06:23 +04:00
|
|
|
if (argc < 2)
|
|
|
|
usage_with_options(usage_str, options);
|
2013-10-10 20:41:17 +04:00
|
|
|
if (!strcmp(argv[1], "-"))
|
|
|
|
argv[1] = "@{-1}";
|
2012-04-14 23:04:48 +04:00
|
|
|
memset(&s_r_opt, 0, sizeof(s_r_opt));
|
|
|
|
s_r_opt.assume_dashdash = 1;
|
|
|
|
argc = setup_revisions(argc, argv, opts->revs, &s_r_opt);
|
2011-08-13 21:06:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
usage_with_options(usage_str, options);
|
2016-10-21 15:24:13 +03:00
|
|
|
|
|
|
|
/* These option values will be free()d */
|
|
|
|
opts->gpg_sign = xstrdup_or_null(opts->gpg_sign);
|
|
|
|
opts->strategy = xstrdup_or_null(opts->strategy);
|
2016-10-21 15:24:55 +03:00
|
|
|
|
2018-08-16 19:06:08 +03:00
|
|
|
if (cmd == 'q') {
|
|
|
|
int ret = sequencer_remove_state(opts);
|
|
|
|
if (!ret)
|
2018-11-10 08:49:00 +03:00
|
|
|
remove_branch_state(the_repository);
|
2018-08-16 19:06:08 +03:00
|
|
|
return ret;
|
|
|
|
}
|
2016-10-21 15:24:55 +03:00
|
|
|
if (cmd == 'c')
|
2018-11-10 08:48:56 +03:00
|
|
|
return sequencer_continue(the_repository, opts);
|
2016-10-21 15:24:55 +03:00
|
|
|
if (cmd == 'a')
|
2018-11-10 08:48:57 +03:00
|
|
|
return sequencer_rollback(the_repository, opts);
|
2018-11-10 08:48:56 +03:00
|
|
|
return sequencer_pick_revisions(the_repository, opts);
|
2007-03-01 07:26:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int cmd_revert(int argc, const char **argv, const char *prefix)
|
|
|
|
{
|
2016-10-14 16:15:56 +03:00
|
|
|
struct replay_opts opts = REPLAY_OPTS_INIT;
|
2011-08-04 14:39:16 +04:00
|
|
|
int res;
|
2011-08-04 14:39:05 +04:00
|
|
|
|
2007-03-01 07:26:30 +03:00
|
|
|
if (isatty(0))
|
2011-08-04 14:39:05 +04:00
|
|
|
opts.edit = 1;
|
2012-01-11 22:15:56 +04:00
|
|
|
opts.action = REPLAY_REVERT;
|
2017-12-13 14:46:21 +03:00
|
|
|
sequencer_init_config(&opts);
|
2016-10-21 15:24:55 +03:00
|
|
|
res = run_sequencer(argc, argv, &opts);
|
2011-08-04 14:39:16 +04:00
|
|
|
if (res < 0)
|
|
|
|
die(_("revert failed"));
|
|
|
|
return res;
|
2007-03-01 07:26:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
|
|
|
|
{
|
2016-10-14 16:15:56 +03:00
|
|
|
struct replay_opts opts = REPLAY_OPTS_INIT;
|
2011-08-04 14:39:16 +04:00
|
|
|
int res;
|
2011-08-04 14:39:05 +04:00
|
|
|
|
2012-01-11 22:15:56 +04:00
|
|
|
opts.action = REPLAY_PICK;
|
2017-12-13 14:46:21 +03:00
|
|
|
sequencer_init_config(&opts);
|
2016-10-21 15:24:55 +03:00
|
|
|
res = run_sequencer(argc, argv, &opts);
|
2011-08-04 14:39:16 +04:00
|
|
|
if (res < 0)
|
|
|
|
die(_("cherry-pick failed"));
|
|
|
|
return res;
|
2007-03-01 07:26:30 +03:00
|
|
|
}
|