2023-02-24 00:09:24 +00:00
|
|
|
#include "git-compat-util.h"
|
2007-03-01 04:26:30 +00:00
|
|
|
#include "builtin.h"
|
2007-10-07 21:02:29 +00:00
|
|
|
#include "parse-options.h"
|
2008-03-03 06:30:56 +00:00
|
|
|
#include "diff.h"
|
2023-03-21 06:25:54 +00:00
|
|
|
#include "gettext.h"
|
2023-05-16 06:34:00 +00:00
|
|
|
#include "repository.h"
|
2008-03-03 06:30:56 +00:00
|
|
|
#include "revision.h"
|
2008-08-10 11:48:55 +00:00
|
|
|
#include "rerere.h"
|
2011-08-04 10:39:11 +00:00
|
|
|
#include "sequencer.h"
|
2018-08-16 16:06:08 +00:00
|
|
|
#include "branch.h"
|
2007-03-01 04:26:30 +00: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-07 21:02:29 +00:00
|
|
|
static const char * const revert_usage[] = {
|
2022-11-26 17:24:02 +00:00
|
|
|
N_("git revert [--[no-]edit] [-n] [-m <parent-number>] [-s] [-S[<keyid>]] <commit>..."),
|
2022-10-13 15:39:13 +00:00
|
|
|
N_("git revert (--continue | --skip | --abort | --quit)"),
|
2007-10-07 21:02:29 +00:00
|
|
|
NULL
|
|
|
|
};
|
2007-03-01 04:26:30 +00:00
|
|
|
|
2007-10-07 21:02:29 +00:00
|
|
|
static const char * const cherry_pick_usage[] = {
|
2022-10-13 15:39:13 +00:00
|
|
|
N_("git cherry-pick [--edit] [-n] [-m <parent-number>] [-s] [-x] [--ff]\n"
|
|
|
|
" [-S[<keyid>]] <commit>..."),
|
|
|
|
N_("git cherry-pick (--continue | --skip | --abort | --quit)"),
|
2007-10-07 21:02:29 +00:00
|
|
|
NULL
|
|
|
|
};
|
2007-03-01 04:26:30 +00:00
|
|
|
|
2011-08-04 10:39:05 +00:00
|
|
|
static const char *action_name(const struct replay_opts *opts)
|
|
|
|
{
|
2012-01-11 18:15:56 +00:00
|
|
|
return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
|
2011-08-04 10:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts)
|
2010-06-14 05:32:09 +00:00
|
|
|
{
|
2012-01-11 18:15:56 +00:00
|
|
|
return opts->action == REPLAY_REVERT ? revert_usage : cherry_pick_usage;
|
2010-06-14 05:32:09 +00:00
|
|
|
}
|
|
|
|
|
cherry-pick: add `--empty` for more robust redundant commit handling
As with git-rebase(1) and git-am(1), git-cherry-pick(1) can result in a
commit being made redundant if the content from the picked commit is
already present in the target history. However, git-cherry-pick(1) does
not have the same options available that git-rebase(1) and git-am(1) have.
There are three things that can be done with these redundant commits:
drop them, keep them, or have the cherry-pick stop and wait for the user
to take an action. git-rebase(1) has the `--empty` option added in commit
e98c4269c8 (rebase (interactive-backend): fix handling of commits that
become empty, 2020-02-15), which handles all three of these scenarios.
Similarly, git-am(1) got its own `--empty` in 7c096b8d61 (am: support
--empty=<option> to handle empty patches, 2021-12-09).
git-cherry-pick(1), on the other hand, only supports two of the three
possiblities: Keep the redundant commits via `--keep-redundant-commits`,
or have the cherry-pick fail by not specifying that option. There is no
way to automatically drop redundant commits.
In order to bring git-cherry-pick(1) more in-line with git-rebase(1) and
git-am(1), this commit adds an `--empty` option to git-cherry-pick(1). It
has the same three options (keep, drop, and stop), and largely behaves
the same. The notable difference is that for git-cherry-pick(1), the
default will be `stop`, which maintains the current behavior when the
option is not specified.
Like the existing `--keep-redundant-commits`, `--empty=keep` will imply
`--allow-empty`.
The `--keep-redundant-commits` option will be documented as a deprecated
synonym of `--empty=keep`, and will be supported for backwards
compatibility for the time being.
Signed-off-by: Brian Lyles <brianmlyles@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-03-25 23:16:54 +00:00
|
|
|
enum empty_action {
|
|
|
|
EMPTY_COMMIT_UNSPECIFIED = -1,
|
|
|
|
STOP_ON_EMPTY_COMMIT, /* output errors and stop in the middle of a cherry-pick */
|
|
|
|
DROP_EMPTY_COMMIT, /* skip with a notice message */
|
|
|
|
KEEP_EMPTY_COMMIT, /* keep recording as empty commits */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int parse_opt_empty(const struct option *opt, const char *arg, int unset)
|
|
|
|
{
|
|
|
|
int *opt_value = opt->value;
|
|
|
|
|
|
|
|
BUG_ON_OPT_NEG(unset);
|
|
|
|
|
|
|
|
if (!strcmp(arg, "stop"))
|
|
|
|
*opt_value = STOP_ON_EMPTY_COMMIT;
|
|
|
|
else if (!strcmp(arg, "drop"))
|
|
|
|
*opt_value = DROP_EMPTY_COMMIT;
|
|
|
|
else if (!strcmp(arg, "keep"))
|
|
|
|
*opt_value = KEEP_EMPTY_COMMIT;
|
|
|
|
else
|
|
|
|
return error(_("invalid value for '%s': '%s'"), "--empty", arg);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-15 16:56:23 +00: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 05:16:11 +00:00
|
|
|
return error(_("option `%s' expects a number greater than zero"),
|
|
|
|
opt->long_name);
|
2017-03-15 16:56:23 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-18 20:02:12 +00:00
|
|
|
LAST_ARG_MUST_BE_NULL
|
2011-08-04 10:39:07 +00: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);
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:54:32 +00:00
|
|
|
static int run_sequencer(int argc, const char **argv, const char *prefix,
|
|
|
|
struct replay_opts *opts)
|
2007-03-01 04:26:30 +00:00
|
|
|
{
|
2011-08-04 10:39:05 +00:00
|
|
|
const char * const * usage_str = revert_or_cherry_pick_usage(opts);
|
2011-08-04 10:39:07 +00:00
|
|
|
const char *me = action_name(opts);
|
2019-04-17 10:23:30 +00:00
|
|
|
const char *cleanup_arg = NULL;
|
cherry-pick: add `--empty` for more robust redundant commit handling
As with git-rebase(1) and git-am(1), git-cherry-pick(1) can result in a
commit being made redundant if the content from the picked commit is
already present in the target history. However, git-cherry-pick(1) does
not have the same options available that git-rebase(1) and git-am(1) have.
There are three things that can be done with these redundant commits:
drop them, keep them, or have the cherry-pick stop and wait for the user
to take an action. git-rebase(1) has the `--empty` option added in commit
e98c4269c8 (rebase (interactive-backend): fix handling of commits that
become empty, 2020-02-15), which handles all three of these scenarios.
Similarly, git-am(1) got its own `--empty` in 7c096b8d61 (am: support
--empty=<option> to handle empty patches, 2021-12-09).
git-cherry-pick(1), on the other hand, only supports two of the three
possiblities: Keep the redundant commits via `--keep-redundant-commits`,
or have the cherry-pick fail by not specifying that option. There is no
way to automatically drop redundant commits.
In order to bring git-cherry-pick(1) more in-line with git-rebase(1) and
git-am(1), this commit adds an `--empty` option to git-cherry-pick(1). It
has the same three options (keep, drop, and stop), and largely behaves
the same. The notable difference is that for git-cherry-pick(1), the
default will be `stop`, which maintains the current behavior when the
option is not specified.
Like the existing `--keep-redundant-commits`, `--empty=keep` will imply
`--allow-empty`.
The `--keep-redundant-commits` option will be documented as a deprecated
synonym of `--empty=keep`, and will be supported for backwards
compatibility for the time being.
Signed-off-by: Brian Lyles <brianmlyles@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-03-25 23:16:54 +00:00
|
|
|
enum empty_action empty_opt = EMPTY_COMMIT_UNSPECIFIED;
|
2013-08-03 11:51:26 +00:00
|
|
|
int cmd = 0;
|
2016-07-05 20:44:47 +00:00
|
|
|
struct option base_options[] = {
|
2013-08-03 11:51:26 +00: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'),
|
2019-07-02 09:11:28 +00:00
|
|
|
OPT_CMDMODE(0, "skip", &cmd, N_("skip current commit and continue"), 's'),
|
2019-04-17 10:23:30 +00:00
|
|
|
OPT_CLEANUP(&cleanup_arg),
|
2013-08-03 11:51:26 +00:00
|
|
|
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 17:47:50 +00:00
|
|
|
OPT_NOOP_NOARG('r', NULL),
|
Documentation: stylistically normalize references to Signed-off-by:
Ted reported an old typo in the git-commit.txt and merge-options.txt.
Namely, the phrase "Signed-off-by line" was used without either a
definite nor indefinite article.
Upon examination, it seems that the documentation (including items in
Documentation/, but also option help strings) have been quite
inconsistent on usage when referring to `Signed-off-by`.
First, very few places used a definite or indefinite article with the
phrase "Signed-off-by line", but that was the initial typo that led
to this investigation. So, normalize using either an indefinite or
definite article consistently.
The original phrasing, in Commit 3f971fc425b (Documentation updates,
2005-08-14), is "Add Signed-off-by line". Commit 6f855371a53 (Add
--signoff, --check, and long option-names. 2005-12-09) switched to
using "Add `Signed-off-by:` line", but didn't normalize the former
commit to match. Later commits seem to have cut and pasted from one
or the other, which is likely how the usage became so inconsistent.
Junio stated on the git mailing list in
<xmqqy2k1dfoh.fsf@gitster.c.googlers.com> a preference to leave off
the colon. Thus, prefer `Signed-off-by` (with backticks) for the
documentation files and Signed-off-by (without backticks) for option
help strings.
Additionally, Junio argued that "trailer" is now the standard term to
refer to `Signed-off-by`, saying that "becomes plenty clear that we
are not talking about any random line in the log message". As such,
prefer "trailer" over "line" anywhere the former word fits.
However, leave alone those few places in documentation that use
Signed-off-by to refer to the process (rather than the specific
trailer), or in places where mail headers are generally discussed in
comparison with Signed-off-by.
Reported-by: "Theodore Y. Ts'o" <tytso@mit.edu>
Signed-off-by: Bradley M. Kuhn <bkuhn@sfconservancy.org>
Acked-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-10-20 01:03:55 +00:00
|
|
|
OPT_BOOL('s', "signoff", &opts->signoff, N_("add a Signed-off-by trailer")),
|
2017-03-15 16:56:23 +00:00
|
|
|
OPT_CALLBACK('m', "mainline", opts, N_("parent-number"),
|
|
|
|
N_("select mainline parent"), option_parse_m),
|
2011-08-04 10:39:05 +00:00
|
|
|
OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
|
2012-08-20 12:32:41 +00:00
|
|
|
OPT_STRING(0, "strategy", &opts->strategy, N_("strategy"), N_("merge strategy")),
|
2023-04-10 09:08:28 +00:00
|
|
|
OPT_STRVEC('X', "strategy-option", &opts->xopts, N_("option"),
|
|
|
|
N_("option for merge strategy")),
|
2014-03-23 22:58:12 +00:00
|
|
|
{ OPTION_STRING, 'S', "gpg-sign", &opts->gpg_sign, N_("key-id"),
|
2014-01-24 00:50:58 +00:00
|
|
|
N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
|
2016-07-05 20:44:47 +00:00
|
|
|
OPT_END()
|
2007-10-07 21:02:29 +00:00
|
|
|
};
|
2016-07-05 20:44:47 +00:00
|
|
|
struct option *options = base_options;
|
2007-10-07 21:02:29 +00:00
|
|
|
|
2012-01-11 18:15:56 +00:00
|
|
|
if (opts->action == REPLAY_PICK) {
|
2010-03-06 20:34:42 +00:00
|
|
|
struct option cp_extra[] = {
|
2013-08-03 11:51:26 +00: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")),
|
cherry-pick: add `--empty` for more robust redundant commit handling
As with git-rebase(1) and git-am(1), git-cherry-pick(1) can result in a
commit being made redundant if the content from the picked commit is
already present in the target history. However, git-cherry-pick(1) does
not have the same options available that git-rebase(1) and git-am(1) have.
There are three things that can be done with these redundant commits:
drop them, keep them, or have the cherry-pick stop and wait for the user
to take an action. git-rebase(1) has the `--empty` option added in commit
e98c4269c8 (rebase (interactive-backend): fix handling of commits that
become empty, 2020-02-15), which handles all three of these scenarios.
Similarly, git-am(1) got its own `--empty` in 7c096b8d61 (am: support
--empty=<option> to handle empty patches, 2021-12-09).
git-cherry-pick(1), on the other hand, only supports two of the three
possiblities: Keep the redundant commits via `--keep-redundant-commits`,
or have the cherry-pick fail by not specifying that option. There is no
way to automatically drop redundant commits.
In order to bring git-cherry-pick(1) more in-line with git-rebase(1) and
git-am(1), this commit adds an `--empty` option to git-cherry-pick(1). It
has the same three options (keep, drop, and stop), and largely behaves
the same. The notable difference is that for git-cherry-pick(1), the
default will be `stop`, which maintains the current behavior when the
option is not specified.
Like the existing `--keep-redundant-commits`, `--empty=keep` will imply
`--allow-empty`.
The `--keep-redundant-commits` option will be documented as a deprecated
synonym of `--empty=keep`, and will be supported for backwards
compatibility for the time being.
Signed-off-by: Brian Lyles <brianmlyles@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-03-25 23:16:54 +00:00
|
|
|
OPT_BOOL(0, "keep-redundant-commits", &opts->keep_redundant_commits, N_("deprecated: use --empty=keep instead")),
|
|
|
|
OPT_CALLBACK_F(0, "empty", &empty_opt, "(stop|drop|keep)",
|
|
|
|
N_("how to handle commits that become empty"),
|
|
|
|
PARSE_OPT_NONEG, parse_opt_empty),
|
2010-03-06 20:34:42 +00:00
|
|
|
OPT_END(),
|
|
|
|
};
|
2016-07-05 20:44:47 +00:00
|
|
|
options = parse_options_concat(options, cp_extra);
|
2022-05-31 16:22:20 +00:00
|
|
|
} else if (opts->action == REPLAY_REVERT) {
|
|
|
|
struct option cp_extra[] = {
|
|
|
|
OPT_BOOL(0, "reference", &opts->commit_use_reference,
|
|
|
|
N_("use the 'reference' format to refer to commits")),
|
|
|
|
OPT_END(),
|
|
|
|
};
|
|
|
|
options = parse_options_concat(options, cp_extra);
|
2010-03-06 20:34:42 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 20:54:32 +00:00
|
|
|
argc = parse_options(argc, argv, prefix, options, usage_str,
|
2011-08-13 17:06:23 +00:00
|
|
|
PARSE_OPT_KEEP_ARGV0 |
|
2022-08-19 16:03:57 +00:00
|
|
|
PARSE_OPT_KEEP_UNKNOWN_OPT);
|
2011-08-04 10:39:11 +00:00
|
|
|
|
2021-09-08 11:24:01 +00:00
|
|
|
prepare_repo_settings(the_repository);
|
|
|
|
the_repository->settings.command_requires_full_index = 0;
|
|
|
|
|
cherry-pick: add `--empty` for more robust redundant commit handling
As with git-rebase(1) and git-am(1), git-cherry-pick(1) can result in a
commit being made redundant if the content from the picked commit is
already present in the target history. However, git-cherry-pick(1) does
not have the same options available that git-rebase(1) and git-am(1) have.
There are three things that can be done with these redundant commits:
drop them, keep them, or have the cherry-pick stop and wait for the user
to take an action. git-rebase(1) has the `--empty` option added in commit
e98c4269c8 (rebase (interactive-backend): fix handling of commits that
become empty, 2020-02-15), which handles all three of these scenarios.
Similarly, git-am(1) got its own `--empty` in 7c096b8d61 (am: support
--empty=<option> to handle empty patches, 2021-12-09).
git-cherry-pick(1), on the other hand, only supports two of the three
possiblities: Keep the redundant commits via `--keep-redundant-commits`,
or have the cherry-pick fail by not specifying that option. There is no
way to automatically drop redundant commits.
In order to bring git-cherry-pick(1) more in-line with git-rebase(1) and
git-am(1), this commit adds an `--empty` option to git-cherry-pick(1). It
has the same three options (keep, drop, and stop), and largely behaves
the same. The notable difference is that for git-cherry-pick(1), the
default will be `stop`, which maintains the current behavior when the
option is not specified.
Like the existing `--keep-redundant-commits`, `--empty=keep` will imply
`--allow-empty`.
The `--keep-redundant-commits` option will be documented as a deprecated
synonym of `--empty=keep`, and will be supported for backwards
compatibility for the time being.
Signed-off-by: Brian Lyles <brianmlyles@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-03-25 23:16:54 +00:00
|
|
|
if (opts->action == REPLAY_PICK) {
|
|
|
|
opts->drop_redundant_commits = (empty_opt == DROP_EMPTY_COMMIT);
|
|
|
|
opts->keep_redundant_commits = opts->keep_redundant_commits || (empty_opt == KEEP_EMPTY_COMMIT);
|
|
|
|
}
|
|
|
|
|
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 14:36:15 +00:00
|
|
|
/* implies allow_empty */
|
|
|
|
if (opts->keep_redundant_commits)
|
|
|
|
opts->allow_empty = 1;
|
|
|
|
|
2019-04-17 10:23:30 +00:00
|
|
|
if (cleanup_arg) {
|
|
|
|
opts->default_msg_cleanup = get_cleanup_mode(cleanup_arg, 1);
|
|
|
|
opts->explicit_cleanup = 1;
|
|
|
|
}
|
|
|
|
|
2011-08-04 10:39:11 +00:00
|
|
|
/* Check for incompatible command line arguments */
|
2016-10-21 12:24:55 +00:00
|
|
|
if (cmd) {
|
2011-08-04 10:39:15 +00:00
|
|
|
char *this_operation;
|
2016-10-21 12:24:55 +00:00
|
|
|
if (cmd == 'q')
|
2011-11-22 11:14:29 +00:00
|
|
|
this_operation = "--quit";
|
2016-10-21 12:24:55 +00:00
|
|
|
else if (cmd == 'c')
|
2011-08-04 10:39:15 +00:00
|
|
|
this_operation = "--continue";
|
2019-07-02 09:11:28 +00:00
|
|
|
else if (cmd == 's')
|
|
|
|
this_operation = "--skip";
|
2011-11-23 01:27:21 +00:00
|
|
|
else {
|
2016-10-21 12:24:55 +00:00
|
|
|
assert(cmd == 'a');
|
2011-11-23 01:27:21 +00:00
|
|
|
this_operation = "--abort";
|
|
|
|
}
|
2011-08-04 10:39:15 +00:00
|
|
|
|
|
|
|
verify_opt_compatible(me, this_operation,
|
2011-08-04 10:39:11 +00:00
|
|
|
"--no-commit", opts->no_commit,
|
|
|
|
"--signoff", opts->signoff,
|
|
|
|
"--mainline", opts->mainline,
|
|
|
|
"--strategy", opts->strategy ? 1 : 0,
|
2023-04-10 09:08:28 +00:00
|
|
|
"--strategy-option", opts->xopts.nr ? 1 : 0,
|
2011-08-04 10:39:11 +00:00
|
|
|
"-x", opts->record_origin,
|
|
|
|
"--ff", opts->allow_ff,
|
2017-08-02 10:44:20 +00:00
|
|
|
"--rerere-autoupdate", opts->allow_rerere_auto == RERERE_AUTOUPDATE,
|
|
|
|
"--no-rerere-autoupdate", opts->allow_rerere_auto == RERERE_NOAUTOUPDATE,
|
2024-03-25 23:16:53 +00:00
|
|
|
"--keep-redundant-commits", opts->keep_redundant_commits,
|
cherry-pick: add `--empty` for more robust redundant commit handling
As with git-rebase(1) and git-am(1), git-cherry-pick(1) can result in a
commit being made redundant if the content from the picked commit is
already present in the target history. However, git-cherry-pick(1) does
not have the same options available that git-rebase(1) and git-am(1) have.
There are three things that can be done with these redundant commits:
drop them, keep them, or have the cherry-pick stop and wait for the user
to take an action. git-rebase(1) has the `--empty` option added in commit
e98c4269c8 (rebase (interactive-backend): fix handling of commits that
become empty, 2020-02-15), which handles all three of these scenarios.
Similarly, git-am(1) got its own `--empty` in 7c096b8d61 (am: support
--empty=<option> to handle empty patches, 2021-12-09).
git-cherry-pick(1), on the other hand, only supports two of the three
possiblities: Keep the redundant commits via `--keep-redundant-commits`,
or have the cherry-pick fail by not specifying that option. There is no
way to automatically drop redundant commits.
In order to bring git-cherry-pick(1) more in-line with git-rebase(1) and
git-am(1), this commit adds an `--empty` option to git-cherry-pick(1). It
has the same three options (keep, drop, and stop), and largely behaves
the same. The notable difference is that for git-cherry-pick(1), the
default will be `stop`, which maintains the current behavior when the
option is not specified.
Like the existing `--keep-redundant-commits`, `--empty=keep` will imply
`--allow-empty`.
The `--keep-redundant-commits` option will be documented as a deprecated
synonym of `--empty=keep`, and will be supported for backwards
compatibility for the time being.
Signed-off-by: Brian Lyles <brianmlyles@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-03-25 23:16:54 +00:00
|
|
|
"--empty", empty_opt != EMPTY_COMMIT_UNSPECIFIED,
|
2011-08-04 10:39:11 +00:00
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2020-11-02 23:45:34 +00:00
|
|
|
if (!opts->strategy && opts->default_strategy) {
|
|
|
|
opts->strategy = opts->default_strategy;
|
|
|
|
opts->default_strategy = NULL;
|
|
|
|
}
|
|
|
|
|
2011-08-04 10:39:07 +00:00
|
|
|
if (opts->allow_ff)
|
|
|
|
verify_opt_compatible(me, "--ff",
|
|
|
|
"--signoff", opts->signoff,
|
|
|
|
"--no-commit", opts->no_commit,
|
|
|
|
"-x", opts->record_origin,
|
sequencer: fix edit handling for cherry-pick and revert messages
save_opts() should save any non-default values. It was intended to do
this, but since most options in struct replay_opts default to 0, it only
saved non-zero values. Unfortunately, this does not always work for
options.edit. Roughly speaking, options.edit had a default value of 0
for cherry-pick but a default value of 1 for revert. Make save_opts()
record a value whenever it differs from the default.
options.edit was also overly simplistic; we had more than two cases.
The behavior that previously existed was as follows:
Non-conflict commits Right after Conflict
revert Edit iff isatty(0) Edit (ignore isatty(0))
cherry-pick No edit See above
Specify --edit Edit (ignore isatty(0)) See above
Specify --no-edit (*) See above
(*) Before stopping for conflicts, No edit is the behavior. After
stopping for conflicts, the --no-edit flag is not saved so see
the first two rows.
However, the expected behavior is:
Non-conflict commits Right after Conflict
revert Edit iff isatty(0) Edit iff isatty(0)
cherry-pick No edit Edit iff isatty(0)
Specify --edit Edit (ignore isatty(0)) Edit (ignore isatty(0))
Specify --no-edit No edit No edit
In order to get the expected behavior, we need to change options.edit
to a tri-state: unspecified, false, or true. When specified, we follow
what it says. When unspecified, we need to check whether the current
commit being created is resolving a conflict as well as consulting
options.action and isatty(0). While at it, add a should_edit() utility
function that compresses options.edit down to a boolean based on the
additional information for the non-conflict case.
continue_single_pick() is the function responsible for resuming after
conflict cases, regardless of whether there is one commit being picked
or many. Make this function stop assuming edit behavior in all cases,
so that it can correctly handle !isatty(0) and specific requests to not
edit the commit message.
Reported-by: Renato Botelho <garga@freebsd.org>
Signed-off-by: Elijah Newren <newren@gmail.com>
Reviewed-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2021-03-31 06:52:20 +00:00
|
|
|
"--edit", opts->edit > 0,
|
2011-08-04 10:39:07 +00:00
|
|
|
NULL);
|
2011-08-13 17:06:23 +00:00
|
|
|
|
2016-10-21 12:24:55 +00:00
|
|
|
if (cmd) {
|
2011-08-13 17:06:23 +00:00
|
|
|
opts->revs = NULL;
|
|
|
|
} else {
|
2012-04-14 19:04:48 +00:00
|
|
|
struct setup_revision_opt s_r_opt;
|
2011-08-13 17:06:23 +00:00
|
|
|
opts->revs = xmalloc(sizeof(*opts->revs));
|
2018-09-21 15:57:38 +00:00
|
|
|
repo_init_revisions(the_repository, opts->revs, NULL);
|
2021-08-05 11:25:24 +00:00
|
|
|
opts->revs->no_walk = 1;
|
|
|
|
opts->revs->unsorted_input = 1;
|
2011-08-13 17:06:23 +00:00
|
|
|
if (argc < 2)
|
|
|
|
usage_with_options(usage_str, options);
|
2013-10-10 16:41:17 +00:00
|
|
|
if (!strcmp(argv[1], "-"))
|
|
|
|
argv[1] = "@{-1}";
|
2012-04-14 19:04:48 +00: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 17:06:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
usage_with_options(usage_str, options);
|
2016-10-21 12:24:13 +00:00
|
|
|
|
|
|
|
/* These option values will be free()d */
|
|
|
|
opts->gpg_sign = xstrdup_or_null(opts->gpg_sign);
|
|
|
|
opts->strategy = xstrdup_or_null(opts->strategy);
|
2020-11-02 23:45:34 +00:00
|
|
|
if (!opts->strategy && getenv("GIT_TEST_MERGE_ALGORITHM"))
|
|
|
|
opts->strategy = xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
|
2022-11-08 18:17:50 +00:00
|
|
|
free(options);
|
2016-10-21 12:24:55 +00:00
|
|
|
|
2018-08-16 16:06:08 +00:00
|
|
|
if (cmd == 'q') {
|
|
|
|
int ret = sequencer_remove_state(opts);
|
|
|
|
if (!ret)
|
2019-03-29 10:38:59 +00:00
|
|
|
remove_branch_state(the_repository, 0);
|
2018-08-16 16:06:08 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2016-10-21 12:24:55 +00:00
|
|
|
if (cmd == 'c')
|
2018-11-10 05:48:56 +00:00
|
|
|
return sequencer_continue(the_repository, opts);
|
2016-10-21 12:24:55 +00:00
|
|
|
if (cmd == 'a')
|
2018-11-10 05:48:57 +00:00
|
|
|
return sequencer_rollback(the_repository, opts);
|
2019-07-02 09:11:28 +00:00
|
|
|
if (cmd == 's')
|
|
|
|
return sequencer_skip(the_repository, opts);
|
2018-11-10 05:48:56 +00:00
|
|
|
return sequencer_pick_revisions(the_repository, opts);
|
2007-03-01 04:26:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int cmd_revert(int argc, const char **argv, const char *prefix)
|
|
|
|
{
|
2016-10-14 13:15:56 +00:00
|
|
|
struct replay_opts opts = REPLAY_OPTS_INIT;
|
2011-08-04 10:39:16 +00:00
|
|
|
int res;
|
2011-08-04 10:39:05 +00:00
|
|
|
|
2012-01-11 18:15:56 +00:00
|
|
|
opts.action = REPLAY_REVERT;
|
2017-12-13 11:46:21 +00:00
|
|
|
sequencer_init_config(&opts);
|
2023-03-28 20:54:32 +00:00
|
|
|
res = run_sequencer(argc, argv, prefix, &opts);
|
2011-08-04 10:39:16 +00:00
|
|
|
if (res < 0)
|
|
|
|
die(_("revert failed"));
|
2023-02-06 19:08:08 +00:00
|
|
|
replay_opts_release(&opts);
|
2011-08-04 10:39:16 +00:00
|
|
|
return res;
|
2007-03-01 04:26:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
|
|
|
|
{
|
2016-10-14 13:15:56 +00:00
|
|
|
struct replay_opts opts = REPLAY_OPTS_INIT;
|
2011-08-04 10:39:16 +00:00
|
|
|
int res;
|
2011-08-04 10:39:05 +00:00
|
|
|
|
2012-01-11 18:15:56 +00:00
|
|
|
opts.action = REPLAY_PICK;
|
2017-12-13 11:46:21 +00:00
|
|
|
sequencer_init_config(&opts);
|
2023-03-28 20:54:32 +00:00
|
|
|
res = run_sequencer(argc, argv, prefix, &opts);
|
2011-08-04 10:39:16 +00:00
|
|
|
if (res < 0)
|
|
|
|
die(_("cherry-pick failed"));
|
2023-02-06 19:08:08 +00:00
|
|
|
replay_opts_release(&opts);
|
2011-08-04 10:39:16 +00:00
|
|
|
return res;
|
2007-03-01 04:26:30 +00:00
|
|
|
}
|