aboutsummaryrefslogtreecommitdiff
path: root/builtin
diff options
context:
space:
mode:
authorSiddharth Asthana <siddharthasthana31@gmail.com>2025-11-06 00:46:00 +0530
committerJunio C Hamano <gitster@pobox.com>2025-11-05 13:34:55 -0800
commit15cd4ef1f495e51f7db39583b7f562e7170da3d2 (patch)
tree69b252a113cb75a143bbb4409a947c0307d2612e /builtin
parente031fa100603af74def6bf2a646c731e4fcd12fc (diff)
downloadgit-15cd4ef1f495e51f7db39583b7f562e7170da3d2.tar.xz
replay: make atomic ref updates the default behavior
The git replay command currently outputs update commands that can be piped to update-ref to achieve a rebase, e.g. git replay --onto main topic1..topic2 | git update-ref --stdin This separation had advantages for three special cases: * it made testing easy (when state isn't modified from one step to the next, you don't need to make temporary branches or have undo commands, or try to track the changes) * it provided a natural can-it-rebase-cleanly (and what would it rebase to) capability without automatically updating refs, similar to a --dry-run * it provided a natural low-level tool for the suite of hash-object, mktree, commit-tree, mktag, merge-tree, and update-ref, allowing users to have another building block for experimentation and making new tools However, it should be noted that all three of these are somewhat special cases; users, whether on the client or server side, would almost certainly find it more ergonomic to simply have the updating of refs be the default. For server-side operations in particular, the pipeline architecture creates process coordination overhead. Server implementations that need to perform rebases atomically must maintain additional code to: 1. Spawn and manage a pipeline between git-replay and git-update-ref 2. Coordinate stdout/stderr streams across the pipe boundary 3. Handle partial failure states if the pipeline breaks mid-execution 4. Parse and validate the update-ref command output Change the default behavior to update refs directly, and atomically (at least to the extent supported by the refs backend in use). This eliminates the process coordination overhead for the common case. For users needing the traditional pipeline workflow, add a new --ref-action=<mode> option that preserves the original behavior: git replay --ref-action=print --onto main topic1..topic2 | git update-ref --stdin The mode can be: * update (default): Update refs directly using an atomic transaction * print: Output update-ref commands for pipeline use Test suite changes: All existing tests that expected command output now use --ref-action=print to preserve their original behavior. This keeps the tests valid while allowing them to verify that the pipeline workflow still works correctly. New tests were added to verify: - Default atomic behavior (no output, refs updated directly) - Bare repository support (server-side use case) - Equivalence between traditional pipeline and atomic updates - Real atomicity using a lock file to verify all-or-nothing guarantee - Test isolation using test_when_finished to clean up state - Reflog messages include replay mode and target A following commit will add a replay.refAction configuration option for users who prefer the traditional pipeline output as their default behavior. Helped-by: Elijah Newren <newren@gmail.com> Helped-by: Patrick Steinhardt <ps@pks.im> Helped-by: Christian Couder <christian.couder@gmail.com> Helped-by: Phillip Wood <phillip.wood123@gmail.com> Signed-off-by: Siddharth Asthana <siddharthasthana31@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin')
-rw-r--r--builtin/replay.c111
1 files changed, 101 insertions, 10 deletions
diff --git a/builtin/replay.c b/builtin/replay.c
index b64fc72063..94e60b5b10 100644
--- a/builtin/replay.c
+++ b/builtin/replay.c
@@ -20,6 +20,11 @@
#include <oidset.h>
#include <tree.h>
+enum ref_action_mode {
+ REF_ACTION_UPDATE,
+ REF_ACTION_PRINT,
+};
+
static const char *short_commit_name(struct repository *repo,
struct commit *commit)
{
@@ -284,6 +289,38 @@ static struct commit *pick_regular_commit(struct repository *repo,
return create_commit(repo, result->tree, pickme, replayed_base);
}
+static enum ref_action_mode parse_ref_action_mode(const char *ref_action, const char *source)
+{
+ if (!ref_action || !strcmp(ref_action, "update"))
+ return REF_ACTION_UPDATE;
+ if (!strcmp(ref_action, "print"))
+ return REF_ACTION_PRINT;
+ die(_("invalid %s value: '%s'"), source, ref_action);
+}
+
+static int handle_ref_update(enum ref_action_mode mode,
+ struct ref_transaction *transaction,
+ const char *refname,
+ const struct object_id *new_oid,
+ const struct object_id *old_oid,
+ const char *reflog_msg,
+ struct strbuf *err)
+{
+ switch (mode) {
+ case REF_ACTION_PRINT:
+ printf("update %s %s %s\n",
+ refname,
+ oid_to_hex(new_oid),
+ oid_to_hex(old_oid));
+ return 0;
+ case REF_ACTION_UPDATE:
+ return ref_transaction_update(transaction, refname, new_oid, old_oid,
+ NULL, NULL, 0, reflog_msg, err);
+ default:
+ BUG("unknown ref_action_mode %d", mode);
+ }
+}
+
int cmd_replay(int argc,
const char **argv,
const char *prefix,
@@ -294,6 +331,8 @@ int cmd_replay(int argc,
struct commit *onto = NULL;
const char *onto_name = NULL;
int contained = 0;
+ const char *ref_action = NULL;
+ enum ref_action_mode ref_mode = REF_ACTION_UPDATE;
struct rev_info revs;
struct commit *last_commit = NULL;
@@ -302,12 +341,15 @@ int cmd_replay(int argc,
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;
+ struct strbuf reflog_msg = STRBUF_INIT;
int ret = 0;
- const char * const replay_usage[] = {
+ const char *const replay_usage[] = {
N_("(EXPERIMENTAL!) git replay "
"([--contained] --onto <newbase> | --advance <branch>) "
- "<revision-range>..."),
+ "[--ref-action[=<mode>]] <revision-range>..."),
NULL
};
struct option replay_options[] = {
@@ -319,6 +361,9 @@ int cmd_replay(int argc,
N_("replay onto given commit")),
OPT_BOOL(0, "contained", &contained,
N_("advance all branches contained in revision-range")),
+ OPT_STRING(0, "ref-action", &ref_action,
+ N_("mode"),
+ N_("control ref update behavior (update|print)")),
OPT_END()
};
@@ -333,6 +378,10 @@ int cmd_replay(int argc,
die_for_incompatible_opt2(!!advance_name_opt, "--advance",
contained, "--contained");
+ /* Parse ref action mode */
+ if (ref_action)
+ ref_mode = parse_ref_action_mode(ref_action, "--ref-action");
+
advance_name = xstrdup_or_null(advance_name_opt);
repo_init_revisions(repo, &revs, prefix);
@@ -389,6 +438,24 @@ int cmd_replay(int argc,
determine_replay_mode(repo, &revs.cmdline, onto_name, &advance_name,
&onto, &update_refs);
+ /* Build reflog message */
+ if (advance_name_opt)
+ strbuf_addf(&reflog_msg, "replay --advance %s", advance_name_opt);
+ else
+ strbuf_addf(&reflog_msg, "replay --onto %s",
+ oid_to_hex(&onto->object.oid));
+
+ /* Initialize ref transaction if using update mode */
+ if (ref_mode == REF_ACTION_UPDATE) {
+ transaction = ref_store_transaction_begin(get_main_ref_store(repo),
+ 0, &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!");
@@ -434,10 +501,16 @@ int cmd_replay(int argc,
if (decoration->type == DECORATION_REF_LOCAL &&
(contained || strset_contains(update_refs,
decoration->name))) {
- printf("update %s %s %s\n",
- decoration->name,
- oid_to_hex(&last_commit->object.oid),
- oid_to_hex(&commit->object.oid));
+ if (handle_ref_update(ref_mode, transaction,
+ decoration->name,
+ &last_commit->object.oid,
+ &commit->object.oid,
+ reflog_msg.buf,
+ &transaction_err) < 0) {
+ ret = error(_("failed to update ref '%s': %s"),
+ decoration->name, transaction_err.buf);
+ goto cleanup;
+ }
}
decoration = decoration->next;
}
@@ -445,10 +518,24 @@ int cmd_replay(int argc,
/* In --advance mode, advance the target ref */
if (result.clean == 1 && advance_name) {
- printf("update %s %s %s\n",
- advance_name,
- oid_to_hex(&last_commit->object.oid),
- oid_to_hex(&onto->object.oid));
+ if (handle_ref_update(ref_mode, transaction, advance_name,
+ &last_commit->object.oid,
+ &onto->object.oid,
+ reflog_msg.buf,
+ &transaction_err) < 0) {
+ ret = error(_("failed to update ref '%s': %s"),
+ advance_name, 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)) {
+ ret = error(_("failed to commit ref transaction: %s"),
+ transaction_err.buf);
+ goto cleanup;
+ }
}
merge_finalize(&merge_opt, &result);
@@ -460,6 +547,10 @@ int cmd_replay(int argc,
ret = result.clean;
cleanup:
+ if (transaction)
+ ref_transaction_free(transaction);
+ strbuf_release(&transaction_err);
+ strbuf_release(&reflog_msg);
release_revisions(&revs);
free(advance_name);