git/builtin/replay.c

453 lines
13 KiB
C

/*
* "git replay" builtin command
*/
#define DISABLE_SIGN_COMPARE_WARNINGS
#include "git-compat-util.h"
#include "builtin.h"
#include "environment.h"
#include "hex.h"
#include "lockfile.h"
#include "merge-ort.h"
#include "object-name.h"
#include "parse-options.h"
#include "refs.h"
#include "replay.h"
#include "revision.h"
#include "strmap.h"
#include <oidset.h>
#include <tree.h>
static struct commit *peel_committish(struct repository *repo, const char *name)
{
struct object *obj;
struct object_id oid;
if (repo_get_oid(repo, name, &oid))
return NULL;
obj = parse_object(repo, &oid);
return (struct commit *)repo_peel_to_type(repo, name, 0, obj,
OBJ_COMMIT);
}
struct ref_info {
struct commit *onto;
struct strset positive_refs;
struct strset negative_refs;
int positive_refexprs;
int negative_refexprs;
};
static void get_ref_information(struct repository *repo,
struct rev_cmdline_info *cmd_info,
struct ref_info *ref_info)
{
int i;
ref_info->onto = NULL;
strset_init(&ref_info->positive_refs);
strset_init(&ref_info->negative_refs);
ref_info->positive_refexprs = 0;
ref_info->negative_refexprs = 0;
/*
* When the user specifies e.g.
* git replay origin/main..mybranch
* git replay ^origin/next mybranch1 mybranch2
* we want to be able to determine where to replay the commits. In
* these examples, the branches are probably based on an old version
* of either origin/main or origin/next, so we want to replay on the
* newest version of that branch. In contrast we would want to error
* out if they ran
* git replay ^origin/master ^origin/next mybranch
* git replay mybranch~2..mybranch
* the first of those because there's no unique base to choose, and
* the second because they'd likely just be replaying commits on top
* of the same commit and not making any difference.
*/
for (i = 0; i < cmd_info->nr; i++) {
struct rev_cmdline_entry *e = cmd_info->rev + i;
struct object_id oid;
const char *refexpr = e->name;
char *fullname = NULL;
int can_uniquely_dwim = 1;
if (*refexpr == '^')
refexpr++;
if (repo_dwim_ref(repo, refexpr, strlen(refexpr), &oid, &fullname, 0) != 1)
can_uniquely_dwim = 0;
if (e->flags & BOTTOM) {
if (can_uniquely_dwim)
strset_add(&ref_info->negative_refs, fullname);
if (!ref_info->negative_refexprs)
ref_info->onto = lookup_commit_reference_gently(repo,
&e->item->oid, 1);
ref_info->negative_refexprs++;
} else {
if (can_uniquely_dwim)
strset_add(&ref_info->positive_refs, fullname);
ref_info->positive_refexprs++;
}
free(fullname);
}
}
static void determine_replay_mode(struct repository *repo,
struct rev_cmdline_info *cmd_info,
const char *onto_name,
char **advance_name,
struct commit **onto,
struct strset **update_refs)
{
struct ref_info rinfo;
get_ref_information(repo, cmd_info, &rinfo);
if (!rinfo.positive_refexprs)
die(_("need some commits to replay"));
die_for_incompatible_opt2(!!onto_name, "--onto",
!!*advance_name, "--advance");
if (onto_name) {
*onto = peel_committish(repo, onto_name);
if (rinfo.positive_refexprs <
strset_get_size(&rinfo.positive_refs))
die(_("all positive revisions given must be references"));
} else if (*advance_name) {
struct object_id oid;
char *fullname = NULL;
*onto = peel_committish(repo, *advance_name);
if (repo_dwim_ref(repo, *advance_name, strlen(*advance_name),
&oid, &fullname, 0) == 1) {
free(*advance_name);
*advance_name = fullname;
} else {
die(_("argument to --advance must be a reference"));
}
if (rinfo.positive_refexprs > 1)
die(_("cannot advance target with multiple sources because ordering would be ill-defined"));
} else {
int positive_refs_complete = (
rinfo.positive_refexprs ==
strset_get_size(&rinfo.positive_refs));
int negative_refs_complete = (
rinfo.negative_refexprs ==
strset_get_size(&rinfo.negative_refs));
/*
* We need either positive_refs_complete or
* negative_refs_complete, but not both.
*/
if (rinfo.negative_refexprs > 0 &&
positive_refs_complete == negative_refs_complete)
die(_("cannot implicitly determine whether this is an --advance or --onto operation"));
if (negative_refs_complete) {
struct hashmap_iter iter;
struct strmap_entry *entry;
const char *last_key = NULL;
if (rinfo.negative_refexprs == 0)
die(_("all positive revisions given must be references"));
else if (rinfo.negative_refexprs > 1)
die(_("cannot implicitly determine whether this is an --advance or --onto operation"));
else if (rinfo.positive_refexprs > 1)
die(_("cannot advance target with multiple source branches because ordering would be ill-defined"));
/* Only one entry, but we have to loop to get it */
strset_for_each_entry(&rinfo.negative_refs,
&iter, entry) {
last_key = entry->key;
}
free(*advance_name);
*advance_name = xstrdup_or_null(last_key);
} else { /* positive_refs_complete */
if (rinfo.negative_refexprs > 1)
die(_("cannot implicitly determine correct base for --onto"));
if (rinfo.negative_refexprs == 1)
*onto = rinfo.onto;
}
}
if (!*advance_name) {
*update_refs = xcalloc(1, sizeof(**update_refs));
**update_refs = rinfo.positive_refs;
memset(&rinfo.positive_refs, 0, sizeof(**update_refs));
}
strset_clear(&rinfo.negative_refs);
strset_clear(&rinfo.positive_refs);
}
static int add_ref_to_transaction(struct ref_transaction *transaction,
const char *refname,
const struct object_id *new_oid,
const struct object_id *old_oid,
struct strbuf *err)
{
return ref_transaction_update(transaction, refname, new_oid, old_oid,
NULL, NULL, 0, "git replay", err);
}
static void print_rejected_update(const char *refname,
const struct object_id *old_oid UNUSED,
const struct object_id *new_oid UNUSED,
const char *old_target UNUSED,
const char *new_target UNUSED,
enum ref_transaction_error err,
void *cb_data UNUSED)
{
const char *reason = ref_transaction_error_msg(err);
warning(_("failed to update %s: %s"), refname, reason);
}
int cmd_replay(int argc,
const char **argv,
const char *prefix,
struct repository *repo)
{
const char *advance_name_opt = NULL;
char *advance_name = NULL;
struct commit *onto = NULL;
const char *onto_name = NULL;
int contained = 0;
int output_commands = 0;
int allow_partial = 0;
struct rev_info revs;
struct commit *last_commit = NULL;
struct commit *commit;
struct merge_options merge_opt;
struct merge_result result;
struct strset *update_refs = NULL;
kh_oid_map_t *replayed_commits;
struct ref_transaction *transaction = NULL;
struct strbuf transaction_err = STRBUF_INIT;
int commits_processed = 0;
int ret = 0;
const char *const replay_usage[] = {
N_("(EXPERIMENTAL!) git replay "
"([--contained] --onto <newbase> | --advance <branch>) "
"[--output-commands | --allow-partial] <revision-range>..."),
NULL
};
struct option replay_options[] = {
OPT_STRING(0, "advance", &advance_name_opt,
N_("branch"),
N_("make replay advance given branch")),
OPT_STRING(0, "onto", &onto_name,
N_("revision"),
N_("replay onto given commit")),
OPT_BOOL(0, "contained", &contained,
N_("advance all branches contained in revision-range")),
OPT_BOOL(0, "output-commands", &output_commands,
N_("output update commands instead of updating refs")),
OPT_BOOL(0, "allow-partial", &allow_partial,
N_("allow some ref updates to succeed even if others fail")),
OPT_END()
};
argc = parse_options(argc, argv, prefix, replay_options, replay_usage,
PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT);
if (!onto_name && !advance_name_opt) {
error(_("option --onto or --advance is mandatory"));
usage_with_options(replay_usage, replay_options);
}
die_for_incompatible_opt2(!!advance_name_opt, "--advance",
contained, "--contained");
die_for_incompatible_opt2(allow_partial, "--allow-partial",
output_commands, "--output-commands");
advance_name = xstrdup_or_null(advance_name_opt);
repo_init_revisions(repo, &revs, prefix);
/*
* Set desired values for rev walking options here. If they
* are changed by some user specified option in setup_revisions()
* below, we will detect that below and then warn.
*
* TODO: In the future we might want to either die(), or allow
* some options changing these values if we think they could
* be useful.
*/
revs.reverse = 1;
revs.sort_order = REV_SORT_IN_GRAPH_ORDER;
revs.topo_order = 1;
revs.simplify_history = 0;
argc = setup_revisions(argc, argv, &revs, NULL);
if (argc > 1) {
ret = error(_("unrecognized argument: %s"), argv[1]);
goto cleanup;
}
/*
* Detect and warn if we override some user specified rev
* walking options.
*/
if (revs.reverse != 1) {
warning(_("some rev walking options will be overridden as "
"'%s' bit in 'struct rev_info' will be forced"),
"reverse");
revs.reverse = 1;
}
if (revs.sort_order != REV_SORT_IN_GRAPH_ORDER) {
warning(_("some rev walking options will be overridden as "
"'%s' bit in 'struct rev_info' will be forced"),
"sort_order");
revs.sort_order = REV_SORT_IN_GRAPH_ORDER;
}
if (revs.topo_order != 1) {
warning(_("some rev walking options will be overridden as "
"'%s' bit in 'struct rev_info' will be forced"),
"topo_order");
revs.topo_order = 1;
}
if (revs.simplify_history != 0) {
warning(_("some rev walking options will be overridden as "
"'%s' bit in 'struct rev_info' will be forced"),
"simplify_history");
revs.simplify_history = 0;
}
determine_replay_mode(repo, &revs.cmdline, onto_name, &advance_name,
&onto, &update_refs);
if (!output_commands) {
unsigned int transaction_flags = allow_partial ? REF_TRANSACTION_ALLOW_FAILURE : 0;
transaction = ref_store_transaction_begin(get_main_ref_store(repo),
transaction_flags,
&transaction_err);
if (!transaction) {
ret = error(_("failed to begin ref transaction: %s"), transaction_err.buf);
goto cleanup;
}
}
if (!onto) /* FIXME: Should handle replaying down to root commit */
die("Replaying down to root commit is not supported yet!");
if (prepare_revision_walk(&revs) < 0) {
ret = error(_("error preparing revisions"));
goto cleanup;
}
init_basic_merge_options(&merge_opt, repo);
memset(&result, 0, sizeof(result));
merge_opt.show_rename_progress = 0;
last_commit = onto;
replayed_commits = kh_init_oid_map();
while ((commit = get_revision(&revs))) {
const struct name_decoration *decoration;
khint_t pos;
int hr;
commits_processed = 1;
if (!commit->parents)
die(_("replaying down to root commit is not supported yet!"));
if (commit->parents->next)
die(_("replaying merge commits is not supported yet!"));
last_commit = replay_pick_regular_commit(repo, commit, replayed_commits,
onto, &merge_opt, &result);
if (!last_commit)
break;
/* Record commit -> last_commit mapping */
pos = kh_put_oid_map(replayed_commits, commit->object.oid, &hr);
if (hr == 0)
BUG("Duplicate rewritten commit: %s\n",
oid_to_hex(&commit->object.oid));
kh_value(replayed_commits, pos) = last_commit;
/* Update any necessary branches */
if (advance_name)
continue;
decoration = get_name_decoration(&commit->object);
if (!decoration)
continue;
while (decoration) {
if (decoration->type == DECORATION_REF_LOCAL &&
(contained || strset_contains(update_refs,
decoration->name))) {
if (output_commands) {
printf("update %s %s %s\n",
decoration->name,
oid_to_hex(&last_commit->object.oid),
oid_to_hex(&commit->object.oid));
} else if (add_ref_to_transaction(transaction, decoration->name,
&last_commit->object.oid,
&commit->object.oid,
&transaction_err) < 0) {
ret = error(_("failed to add ref update to transaction: %s"), transaction_err.buf);
goto cleanup;
}
}
decoration = decoration->next;
}
}
/* In --advance mode, advance the target ref */
if (result.clean == 1 && advance_name) {
if (output_commands) {
printf("update %s %s %s\n",
advance_name,
oid_to_hex(&last_commit->object.oid),
oid_to_hex(&onto->object.oid));
} else if (add_ref_to_transaction(transaction, advance_name,
&last_commit->object.oid,
&onto->object.oid,
&transaction_err) < 0) {
ret = error(_("failed to add ref update to transaction: %s"), transaction_err.buf);
goto cleanup;
}
}
/* Commit the ref transaction if we have one */
if (transaction && result.clean == 1) {
if (ref_transaction_commit(transaction, &transaction_err)) {
if (allow_partial) {
warning(_("some ref updates failed: %s"), transaction_err.buf);
ref_transaction_for_each_rejected_update(transaction,
print_rejected_update, NULL);
ret = 0; /* Set failure even with allow_partial */
} else {
ret = error(_("failed to update refs: %s"), transaction_err.buf);
goto cleanup;
}
}
}
merge_finalize(&merge_opt, &result);
kh_destroy_oid_map(replayed_commits);
if (update_refs) {
strset_clear(update_refs);
free(update_refs);
}
/* Handle empty ranges: if no commits were processed, treat as success */
if (!commits_processed)
ret = 1; /* Success - no commits to replay is not an error */
else
ret = result.clean;
cleanup:
if (transaction)
ref_transaction_free(transaction);
strbuf_release(&transaction_err);
release_revisions(&revs);
free(advance_name);
/* Return */
if (ret < 0)
exit(128);
return ret ? 0 : 1;
}