2007-03-01 12:26:30 +08:00
|
|
|
#include "cache.h"
|
|
|
|
#include "builtin.h"
|
2007-10-08 05:02:29 +08:00
|
|
|
#include "parse-options.h"
|
2008-03-03 14:30:56 +08:00
|
|
|
#include "diff.h"
|
|
|
|
#include "revision.h"
|
2008-08-10 19:48:55 +08:00
|
|
|
#include "rerere.h"
|
2011-08-04 18:39:08 +08:00
|
|
|
#include "dir.h"
|
2011-08-04 18:39:11 +08:00
|
|
|
#include "sequencer.h"
|
2007-03-01 12:26:30 +08: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 05:02:29 +08:00
|
|
|
static const char * const revert_usage[] = {
|
2008-07-13 21:36:15 +08:00
|
|
|
"git revert [options] <commit-ish>",
|
2011-08-04 18:39:11 +08:00
|
|
|
"git revert <subcommand>",
|
2007-10-08 05:02:29 +08:00
|
|
|
NULL
|
|
|
|
};
|
2007-03-01 12:26:30 +08:00
|
|
|
|
2007-10-08 05:02:29 +08:00
|
|
|
static const char * const cherry_pick_usage[] = {
|
2008-07-13 21:36:15 +08:00
|
|
|
"git cherry-pick [options] <commit-ish>",
|
2011-08-04 18:39:11 +08:00
|
|
|
"git cherry-pick <subcommand>",
|
2007-10-08 05:02:29 +08:00
|
|
|
NULL
|
|
|
|
};
|
2007-03-01 12:26:30 +08:00
|
|
|
|
2011-08-04 18:39:05 +08:00
|
|
|
static const char *action_name(const struct replay_opts *opts)
|
|
|
|
{
|
2012-01-12 02:15:56 +08:00
|
|
|
return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
|
2011-08-04 18:39:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char * const *revert_or_cherry_pick_usage(struct replay_opts *opts)
|
2010-06-14 13:32:09 +08:00
|
|
|
{
|
2012-01-12 02:15:56 +08:00
|
|
|
return opts->action == REPLAY_REVERT ? revert_usage : cherry_pick_usage;
|
2010-06-14 13:32:09 +08:00
|
|
|
}
|
|
|
|
|
2010-12-11 08:51:44 +08:00
|
|
|
static int option_parse_x(const struct option *opt,
|
|
|
|
const char *arg, int unset)
|
|
|
|
{
|
2011-08-04 18:39:05 +08:00
|
|
|
struct replay_opts **opts_ptr = opt->value;
|
|
|
|
struct replay_opts *opts = *opts_ptr;
|
|
|
|
|
2010-12-11 08:51:44 +08:00
|
|
|
if (unset)
|
|
|
|
return 0;
|
|
|
|
|
2011-08-04 18:39:05 +08:00
|
|
|
ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
|
|
|
|
opts->xopts[opts->xopts_nr++] = xstrdup(arg);
|
2010-12-11 08:51:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-04 18:39:07 +08: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);
|
|
|
|
}
|
|
|
|
|
2011-08-04 18:39:15 +08:00
|
|
|
static void verify_opt_mutually_compatible(const char *me, ...)
|
|
|
|
{
|
2011-09-12 03:39:32 +08:00
|
|
|
const char *opt1, *opt2 = NULL;
|
2011-08-04 18:39:15 +08:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, me);
|
|
|
|
while ((opt1 = va_arg(ap, const char *))) {
|
|
|
|
if (va_arg(ap, int))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (opt1) {
|
|
|
|
while ((opt2 = va_arg(ap, const char *))) {
|
|
|
|
if (va_arg(ap, int))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-04-22 05:15:19 +08:00
|
|
|
va_end(ap);
|
2011-08-04 18:39:15 +08:00
|
|
|
|
|
|
|
if (opt1 && opt2)
|
|
|
|
die(_("%s: %s cannot be used with %s"), me, opt1, opt2);
|
|
|
|
}
|
|
|
|
|
2011-08-04 18:39:05 +08:00
|
|
|
static void parse_args(int argc, const char **argv, struct replay_opts *opts)
|
2007-03-01 12:26:30 +08:00
|
|
|
{
|
2011-08-04 18:39:05 +08:00
|
|
|
const char * const * usage_str = revert_or_cherry_pick_usage(opts);
|
2011-08-04 18:39:07 +08:00
|
|
|
const char *me = action_name(opts);
|
2011-11-22 19:14:29 +08:00
|
|
|
int remove_state = 0;
|
2011-08-04 18:39:15 +08:00
|
|
|
int contin = 0;
|
2011-11-23 09:27:21 +08:00
|
|
|
int rollback = 0;
|
2007-10-08 05:02:29 +08:00
|
|
|
struct option options[] = {
|
2011-11-22 19:14:29 +08:00
|
|
|
OPT_BOOLEAN(0, "quit", &remove_state, "end revert or cherry-pick sequence"),
|
|
|
|
OPT_BOOLEAN(0, "continue", &contin, "resume revert or cherry-pick sequence"),
|
2011-11-23 09:27:21 +08:00
|
|
|
OPT_BOOLEAN(0, "abort", &rollback, "cancel revert or cherry-pick sequence"),
|
2011-08-04 18:39:05 +08:00
|
|
|
OPT_BOOLEAN('n', "no-commit", &opts->no_commit, "don't automatically commit"),
|
|
|
|
OPT_BOOLEAN('e', "edit", &opts->edit, "edit the commit message"),
|
2011-09-29 01:47:50 +08:00
|
|
|
OPT_NOOP_NOARG('r', NULL),
|
2011-08-04 18:39:05 +08:00
|
|
|
OPT_BOOLEAN('s', "signoff", &opts->signoff, "add Signed-off-by:"),
|
|
|
|
OPT_INTEGER('m', "mainline", &opts->mainline, "parent number"),
|
|
|
|
OPT_RERERE_AUTOUPDATE(&opts->allow_rerere_auto),
|
|
|
|
OPT_STRING(0, "strategy", &opts->strategy, "strategy", "merge strategy"),
|
|
|
|
OPT_CALLBACK('X', "strategy-option", &opts, "option",
|
2010-12-11 08:51:44 +08:00
|
|
|
"option for merge strategy", option_parse_x),
|
2007-10-08 05:02:29 +08:00
|
|
|
OPT_END(),
|
2010-03-07 04:34:42 +08:00
|
|
|
OPT_END(),
|
|
|
|
OPT_END(),
|
2012-04-12 04:21:53 +08:00
|
|
|
OPT_END(),
|
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 22:36:15 +08:00
|
|
|
OPT_END(),
|
2007-10-08 05:02:29 +08:00
|
|
|
};
|
|
|
|
|
2012-01-12 02:15:56 +08:00
|
|
|
if (opts->action == REPLAY_PICK) {
|
2010-03-07 04:34:42 +08:00
|
|
|
struct option cp_extra[] = {
|
2011-08-04 18:39:05 +08:00
|
|
|
OPT_BOOLEAN('x', NULL, &opts->record_origin, "append commit name"),
|
|
|
|
OPT_BOOLEAN(0, "ff", &opts->allow_ff, "allow fast-forward"),
|
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 22:36:15 +08:00
|
|
|
OPT_BOOLEAN(0, "allow-empty", &opts->allow_empty, "preserve initially empty commits"),
|
|
|
|
OPT_BOOLEAN(0, "keep-redundant-commits", &opts->keep_redundant_commits, "keep redundant, empty commits"),
|
2010-03-07 04:34:42 +08:00
|
|
|
OPT_END(),
|
|
|
|
};
|
|
|
|
if (parse_options_concat(options, ARRAY_SIZE(options), cp_extra))
|
2011-02-23 07:42:28 +08:00
|
|
|
die(_("program error"));
|
2010-03-07 04:34:42 +08:00
|
|
|
}
|
|
|
|
|
2011-08-14 01:06:23 +08:00
|
|
|
argc = parse_options(argc, argv, NULL, options, usage_str,
|
|
|
|
PARSE_OPT_KEEP_ARGV0 |
|
|
|
|
PARSE_OPT_KEEP_UNKNOWN);
|
2011-08-04 18:39:11 +08:00
|
|
|
|
2011-08-04 18:39:15 +08:00
|
|
|
/* Check for incompatible subcommands */
|
|
|
|
verify_opt_mutually_compatible(me,
|
2011-11-22 19:14:29 +08:00
|
|
|
"--quit", remove_state,
|
2011-08-04 18:39:15 +08:00
|
|
|
"--continue", contin,
|
2011-11-23 09:27:21 +08:00
|
|
|
"--abort", rollback,
|
2011-08-04 18:39:15 +08:00
|
|
|
NULL);
|
|
|
|
|
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 22:36:15 +08:00
|
|
|
/* implies allow_empty */
|
|
|
|
if (opts->keep_redundant_commits)
|
|
|
|
opts->allow_empty = 1;
|
|
|
|
|
2011-08-04 18:39:11 +08:00
|
|
|
/* Set the subcommand */
|
2011-11-22 19:14:29 +08:00
|
|
|
if (remove_state)
|
|
|
|
opts->subcommand = REPLAY_REMOVE_STATE;
|
2011-08-04 18:39:15 +08:00
|
|
|
else if (contin)
|
|
|
|
opts->subcommand = REPLAY_CONTINUE;
|
2011-11-23 09:27:21 +08:00
|
|
|
else if (rollback)
|
|
|
|
opts->subcommand = REPLAY_ROLLBACK;
|
2011-08-04 18:39:11 +08:00
|
|
|
else
|
|
|
|
opts->subcommand = REPLAY_NONE;
|
|
|
|
|
|
|
|
/* Check for incompatible command line arguments */
|
2011-08-04 18:39:15 +08:00
|
|
|
if (opts->subcommand != REPLAY_NONE) {
|
|
|
|
char *this_operation;
|
2011-11-22 19:14:29 +08:00
|
|
|
if (opts->subcommand == REPLAY_REMOVE_STATE)
|
|
|
|
this_operation = "--quit";
|
2011-11-23 09:27:21 +08:00
|
|
|
else if (opts->subcommand == REPLAY_CONTINUE)
|
2011-08-04 18:39:15 +08:00
|
|
|
this_operation = "--continue";
|
2011-11-23 09:27:21 +08:00
|
|
|
else {
|
|
|
|
assert(opts->subcommand == REPLAY_ROLLBACK);
|
|
|
|
this_operation = "--abort";
|
|
|
|
}
|
2011-08-04 18:39:15 +08:00
|
|
|
|
|
|
|
verify_opt_compatible(me, this_operation,
|
2011-08-04 18:39:11 +08: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,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2011-08-04 18:39:07 +08: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-14 01:06:23 +08:00
|
|
|
|
|
|
|
if (opts->subcommand != REPLAY_NONE) {
|
|
|
|
opts->revs = NULL;
|
|
|
|
} else {
|
2012-04-15 03:04:48 +08:00
|
|
|
struct setup_revision_opt s_r_opt;
|
2011-08-14 01:06:23 +08:00
|
|
|
opts->revs = xmalloc(sizeof(*opts->revs));
|
|
|
|
init_revisions(opts->revs, NULL);
|
teach log --no-walk=unsorted, which avoids sorting
When 'git log' is passed the --no-walk option, no revision walk takes
place, naturally. Perhaps somewhat surprisingly, however, the provided
revisions still get sorted by commit date. So e.g 'git log --no-walk
HEAD HEAD~1' and 'git log --no-walk HEAD~1 HEAD' give the same result
(unless the two revisions share the commit date, in which case they
will retain the order given on the command line). As the commit that
introduced --no-walk (8e64006 (Teach revision machinery about
--no-walk, 2007-07-24)) points out, the sorting is intentional, to
allow things like
git log --abbrev-commit --pretty=oneline --decorate --all --no-walk
to show all refs in order by commit date.
But there are also other cases where the sorting is not wanted, such
as
<command producing revisions in order> |
git log --oneline --no-walk --stdin
To accomodate both cases, leave the decision of whether or not to sort
up to the caller, by allowing --no-walk={sorted,unsorted}, defaulting
to 'sorted' for backward-compatibility reasons.
Signed-off-by: Martin von Zweigbergk <martinvonz@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2012-08-29 14:15:54 +08:00
|
|
|
opts->revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
|
2011-08-14 01:06:23 +08:00
|
|
|
if (argc < 2)
|
|
|
|
usage_with_options(usage_str, options);
|
2012-04-15 03:04:48 +08: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-14 01:06:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
usage_with_options(usage_str, options);
|
2007-03-01 12:26:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int cmd_revert(int argc, const char **argv, const char *prefix)
|
|
|
|
{
|
2011-08-04 18:39:05 +08:00
|
|
|
struct replay_opts opts;
|
2011-08-04 18:39:16 +08:00
|
|
|
int res;
|
2011-08-04 18:39:05 +08:00
|
|
|
|
|
|
|
memset(&opts, 0, sizeof(opts));
|
2007-03-01 12:26:30 +08:00
|
|
|
if (isatty(0))
|
2011-08-04 18:39:05 +08:00
|
|
|
opts.edit = 1;
|
2012-01-12 02:15:56 +08:00
|
|
|
opts.action = REPLAY_REVERT;
|
revert: Separate cmdline parsing from functional code
Currently, revert_or_cherry_pick sets up a default git config, parses
command-line arguments, before preparing to pick commits. This makes
for a bad API as the central worry of callers is to assert whether or
not a conflict occured while cherry picking. The current API is like:
if (revert_or_cherry_pick(argc, argv, opts) < 0)
print "Something failed, we're not sure what"
Simplify and rename revert_or_cherry_pick to pick_commits so that it
only has the responsibility of setting up the revision walker and
picking commits in a loop. Transfer the remaining work to its
callers. Now, the API is simplified as:
if (parse_args(argc, argv, opts) < 0)
print "Can't parse arguments"
if (pick_commits(opts) < 0)
print "Error encountered in picking machinery"
Later in the series, pick_commits will also serve as the starting
point for continuing a cherry-pick or revert.
Inspired-by: Christian Couder <chriscool@tuxfamily.org>
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-08-04 18:39:06 +08:00
|
|
|
git_config(git_default_config, NULL);
|
|
|
|
parse_args(argc, argv, &opts);
|
2012-01-12 02:15:57 +08:00
|
|
|
res = sequencer_pick_revisions(&opts);
|
2011-08-04 18:39:16 +08:00
|
|
|
if (res < 0)
|
|
|
|
die(_("revert failed"));
|
|
|
|
return res;
|
2007-03-01 12:26:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
|
|
|
|
{
|
2011-08-04 18:39:05 +08:00
|
|
|
struct replay_opts opts;
|
2011-08-04 18:39:16 +08:00
|
|
|
int res;
|
2011-08-04 18:39:05 +08:00
|
|
|
|
|
|
|
memset(&opts, 0, sizeof(opts));
|
2012-01-12 02:15:56 +08:00
|
|
|
opts.action = REPLAY_PICK;
|
revert: Separate cmdline parsing from functional code
Currently, revert_or_cherry_pick sets up a default git config, parses
command-line arguments, before preparing to pick commits. This makes
for a bad API as the central worry of callers is to assert whether or
not a conflict occured while cherry picking. The current API is like:
if (revert_or_cherry_pick(argc, argv, opts) < 0)
print "Something failed, we're not sure what"
Simplify and rename revert_or_cherry_pick to pick_commits so that it
only has the responsibility of setting up the revision walker and
picking commits in a loop. Transfer the remaining work to its
callers. Now, the API is simplified as:
if (parse_args(argc, argv, opts) < 0)
print "Can't parse arguments"
if (pick_commits(opts) < 0)
print "Error encountered in picking machinery"
Later in the series, pick_commits will also serve as the starting
point for continuing a cherry-pick or revert.
Inspired-by: Christian Couder <chriscool@tuxfamily.org>
Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-08-04 18:39:06 +08:00
|
|
|
git_config(git_default_config, NULL);
|
|
|
|
parse_args(argc, argv, &opts);
|
2012-01-12 02:15:57 +08:00
|
|
|
res = sequencer_pick_revisions(&opts);
|
2011-08-04 18:39:16 +08:00
|
|
|
if (res < 0)
|
|
|
|
die(_("cherry-pick failed"));
|
|
|
|
return res;
|
2007-03-01 12:26:30 +08:00
|
|
|
}
|