Skip to content

Commit fc73f8d

Browse files
edith007gitster
authored andcommitted
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 Implementation details: The atomic ref updates are implemented using Git's ref transaction API. In cmd_replay(), when not in `print` mode, we initialize a transaction using ref_store_transaction_begin() with the default atomic behavior. As commits are replayed, ref updates are staged into the transaction using ref_transaction_update(). Finally, ref_transaction_commit() applies all updates atomically—either all updates succeed or none do. To avoid code duplication between the 'print' and 'update' modes, this commit extracts a handle_ref_update() helper function. This function takes the mode (as an enum) and either prints the update command or stages it into the transaction. Using an enum rather than passing the string around provides type safety and allows the compiler to catch typos. The switch statement makes it easy to add future modes. The helper function signature: 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, struct strbuf *err) The enum is defined as: enum ref_action_mode { REF_ACTION_UPDATE, REF_ACTION_PRINT }; The mode string is converted to enum immediately after parse_options() to avoid string comparisons throughout the codebase and provide compiler protection against typos. 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 The bare repository tests were fixed to rebuild their expectations independently rather than comparing to previous test output, improving test reliability and isolation. 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 <[email protected]> Helped-by: Patrick Steinhardt <[email protected]> Helped-by: Christian Couder <[email protected]> Helped-by: Phillip Wood <[email protected]> Signed-off-by: Siddharth Asthana <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7c9f838 commit fc73f8d

File tree

3 files changed

+167
-40
lines changed

3 files changed

+167
-40
lines changed

Documentation/git-replay.adoc

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ git-replay - EXPERIMENTAL: Replay commits on a new base, works with bare repos t
99
SYNOPSIS
1010
--------
1111
[verse]
12-
(EXPERIMENTAL!) 'git replay' ([--contained] --onto <newbase> | --advance <branch>) <revision-range>...
12+
(EXPERIMENTAL!) 'git replay' ([--contained] --onto <newbase> | --advance <branch>) [--ref-action[=<mode>]] <revision-range>...
1313

1414
DESCRIPTION
1515
-----------
1616

1717
Takes ranges of commits and replays them onto a new location. Leaves
18-
the working tree and the index untouched, and updates no references.
19-
The output of this command is meant to be used as input to
20-
`git update-ref --stdin`, which would update the relevant branches
18+
the working tree and the index untouched. By default, updates the
19+
relevant references using an atomic transaction (all refs update or
20+
none). Use `--ref-action=print` to avoid automatic ref updates and
21+
instead get update commands that can be piped to `git update-ref --stdin`
2122
(see the OUTPUT section below).
2223

2324
THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.
@@ -29,18 +30,31 @@ OPTIONS
2930
Starting point at which to create the new commits. May be any
3031
valid commit, and not just an existing branch name.
3132
+
32-
When `--onto` is specified, the update-ref command(s) in the output will
33-
update the branch(es) in the revision range to point at the new
34-
commits, similar to the way how `git rebase --update-refs` updates
35-
multiple branches in the affected range.
33+
When `--onto` is specified, the branch(es) in the revision range will be
34+
updated to point at the new commits (or update commands will be printed
35+
if `--ref-action=print` is used), similar to the way `git rebase --update-refs`
36+
updates multiple branches in the affected range.
3637

3738
--advance <branch>::
3839
Starting point at which to create the new commits; must be a
3940
branch name.
4041
+
41-
When `--advance` is specified, the update-ref command(s) in the output
42-
will update the branch passed as an argument to `--advance` to point at
43-
the new commits (in other words, this mimics a cherry-pick operation).
42+
The history is replayed on top of the <branch> and <branch> is updated to
43+
point at the tip of the resulting history (or an update command will be
44+
printed if `--ref-action=print` is used). This is different from `--onto`,
45+
which uses the target only as a starting point without updating it.
46+
47+
--ref-action[=<mode>]::
48+
Control how references are updated. The mode can be:
49+
+
50+
--
51+
* `update` (default): Update refs directly using an atomic transaction.
52+
All refs are updated or none are (all-or-nothing behavior).
53+
* `print`: Output update-ref commands for pipeline use. This is the
54+
traditional behavior where output can be piped to `git update-ref --stdin`.
55+
--
56+
+
57+
The default mode can be configured via the `replay.refAction` configuration variable.
4458

4559
<revision-range>::
4660
Range of commits to replay. More than one <revision-range> can
@@ -54,8 +68,11 @@ include::rev-list-options.adoc[]
5468
OUTPUT
5569
------
5670

57-
When there are no conflicts, the output of this command is usable as
58-
input to `git update-ref --stdin`. It is of the form:
71+
By default, or with `--ref-action=update`, this command produces no output on
72+
success, as refs are updated directly using an atomic transaction.
73+
74+
When using `--ref-action=print`, the output is usable as input to
75+
`git update-ref --stdin`. It is of the form:
5976

6077
update refs/heads/branch1 ${NEW_branch1_HASH} ${OLD_branch1_HASH}
6178
update refs/heads/branch2 ${NEW_branch2_HASH} ${OLD_branch2_HASH}
@@ -81,40 +98,44 @@ To simply rebase `mybranch` onto `target`:
8198

8299
------------
83100
$ git replay --onto target origin/main..mybranch
101+
------------
102+
103+
The refs are updated atomically and no output is produced on success.
104+
105+
To see what would be updated without actually updating:
106+
107+
------------
108+
$ git replay --ref-action=print --onto target origin/main..mybranch
84109
update refs/heads/mybranch ${NEW_mybranch_HASH} ${OLD_mybranch_HASH}
85110
------------
86111

87112
To cherry-pick the commits from mybranch onto target:
88113

89114
------------
90115
$ git replay --advance target origin/main..mybranch
91-
update refs/heads/target ${NEW_target_HASH} ${OLD_target_HASH}
92116
------------
93117

94118
Note that the first two examples replay the exact same commits and on
95119
top of the exact same new base, they only differ in that the first
96-
provides instructions to make mybranch point at the new commits and
97-
the second provides instructions to make target point at them.
120+
updates mybranch to point at the new commits and the second updates
121+
target to point at them.
98122

99123
What if you have a stack of branches, one depending upon another, and
100124
you'd really like to rebase the whole set?
101125

102126
------------
103127
$ git replay --contained --onto origin/main origin/main..tipbranch
104-
update refs/heads/branch1 ${NEW_branch1_HASH} ${OLD_branch1_HASH}
105-
update refs/heads/branch2 ${NEW_branch2_HASH} ${OLD_branch2_HASH}
106-
update refs/heads/tipbranch ${NEW_tipbranch_HASH} ${OLD_tipbranch_HASH}
107128
------------
108129

130+
All three branches (`branch1`, `branch2`, and `tipbranch`) are updated
131+
atomically.
132+
109133
When calling `git replay`, one does not need to specify a range of
110134
commits to replay using the syntax `A..B`; any range expression will
111135
do:
112136

113137
------------
114138
$ git replay --onto origin/main ^base branch1 branch2 branch3
115-
update refs/heads/branch1 ${NEW_branch1_HASH} ${OLD_branch1_HASH}
116-
update refs/heads/branch2 ${NEW_branch2_HASH} ${OLD_branch2_HASH}
117-
update refs/heads/branch3 ${NEW_branch3_HASH} ${OLD_branch3_HASH}
118139
------------
119140

120141
This will simultaneously rebase `branch1`, `branch2`, and `branch3`,

builtin/replay.c

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
#include <oidset.h>
2121
#include <tree.h>
2222

23+
enum ref_action_mode {
24+
REF_ACTION_UPDATE,
25+
REF_ACTION_PRINT,
26+
};
27+
2328
static const char *short_commit_name(struct repository *repo,
2429
struct commit *commit)
2530
{
@@ -284,6 +289,28 @@ static struct commit *pick_regular_commit(struct repository *repo,
284289
return create_commit(repo, result->tree, pickme, replayed_base);
285290
}
286291

292+
static int handle_ref_update(enum ref_action_mode mode,
293+
struct ref_transaction *transaction,
294+
const char *refname,
295+
const struct object_id *new_oid,
296+
const struct object_id *old_oid,
297+
struct strbuf *err)
298+
{
299+
switch (mode) {
300+
case REF_ACTION_PRINT:
301+
printf("update %s %s %s\n",
302+
refname,
303+
oid_to_hex(new_oid),
304+
oid_to_hex(old_oid));
305+
return 0;
306+
case REF_ACTION_UPDATE:
307+
return ref_transaction_update(transaction, refname, new_oid, old_oid,
308+
NULL, NULL, 0, "git replay", err);
309+
default:
310+
BUG("unknown ref_action_mode %d", mode);
311+
}
312+
}
313+
287314
int cmd_replay(int argc,
288315
const char **argv,
289316
const char *prefix,
@@ -294,6 +321,8 @@ int cmd_replay(int argc,
294321
struct commit *onto = NULL;
295322
const char *onto_name = NULL;
296323
int contained = 0;
324+
const char *ref_action_str = NULL;
325+
enum ref_action_mode ref_action = REF_ACTION_UPDATE;
297326

298327
struct rev_info revs;
299328
struct commit *last_commit = NULL;
@@ -302,12 +331,14 @@ int cmd_replay(int argc,
302331
struct merge_result result;
303332
struct strset *update_refs = NULL;
304333
kh_oid_map_t *replayed_commits;
334+
struct ref_transaction *transaction = NULL;
335+
struct strbuf transaction_err = STRBUF_INIT;
305336
int ret = 0;
306337

307-
const char * const replay_usage[] = {
338+
const char *const replay_usage[] = {
308339
N_("(EXPERIMENTAL!) git replay "
309340
"([--contained] --onto <newbase> | --advance <branch>) "
310-
"<revision-range>..."),
341+
"[--ref-action[=<mode>]] <revision-range>..."),
311342
NULL
312343
};
313344
struct option replay_options[] = {
@@ -319,6 +350,9 @@ int cmd_replay(int argc,
319350
N_("replay onto given commit")),
320351
OPT_BOOL(0, "contained", &contained,
321352
N_("advance all branches contained in revision-range")),
353+
OPT_STRING(0, "ref-action", &ref_action_str,
354+
N_("mode"),
355+
N_("control ref update behavior (update|print)")),
322356
OPT_END()
323357
};
324358

@@ -333,6 +367,18 @@ int cmd_replay(int argc,
333367
die_for_incompatible_opt2(!!advance_name_opt, "--advance",
334368
contained, "--contained");
335369

370+
/* Default to update mode if not specified */
371+
if (!ref_action_str)
372+
ref_action_str = "update";
373+
374+
/* Parse ref action mode */
375+
if (!strcmp(ref_action_str, "update"))
376+
ref_action = REF_ACTION_UPDATE;
377+
else if (!strcmp(ref_action_str, "print"))
378+
ref_action = REF_ACTION_PRINT;
379+
else
380+
die(_("unknown --ref-action mode '%s'"), ref_action_str);
381+
336382
advance_name = xstrdup_or_null(advance_name_opt);
337383

338384
repo_init_revisions(repo, &revs, prefix);
@@ -389,6 +435,17 @@ int cmd_replay(int argc,
389435
determine_replay_mode(repo, &revs.cmdline, onto_name, &advance_name,
390436
&onto, &update_refs);
391437

438+
/* Initialize ref transaction if using update mode */
439+
if (ref_action == REF_ACTION_UPDATE) {
440+
transaction = ref_store_transaction_begin(get_main_ref_store(repo),
441+
0, &transaction_err);
442+
if (!transaction) {
443+
ret = error(_("failed to begin ref transaction: %s"),
444+
transaction_err.buf);
445+
goto cleanup;
446+
}
447+
}
448+
392449
if (!onto) /* FIXME: Should handle replaying down to root commit */
393450
die("Replaying down to root commit is not supported yet!");
394451

@@ -434,21 +491,39 @@ int cmd_replay(int argc,
434491
if (decoration->type == DECORATION_REF_LOCAL &&
435492
(contained || strset_contains(update_refs,
436493
decoration->name))) {
437-
printf("update %s %s %s\n",
438-
decoration->name,
439-
oid_to_hex(&last_commit->object.oid),
440-
oid_to_hex(&commit->object.oid));
494+
if (handle_ref_update(ref_action, transaction,
495+
decoration->name,
496+
&last_commit->object.oid,
497+
&commit->object.oid,
498+
&transaction_err) < 0) {
499+
ret = error(_("failed to update ref '%s': %s"),
500+
decoration->name, transaction_err.buf);
501+
goto cleanup;
502+
}
441503
}
442504
decoration = decoration->next;
443505
}
444506
}
445507

446508
/* In --advance mode, advance the target ref */
447509
if (result.clean == 1 && advance_name) {
448-
printf("update %s %s %s\n",
449-
advance_name,
450-
oid_to_hex(&last_commit->object.oid),
451-
oid_to_hex(&onto->object.oid));
510+
if (handle_ref_update(ref_action, transaction, advance_name,
511+
&last_commit->object.oid,
512+
&onto->object.oid,
513+
&transaction_err) < 0) {
514+
ret = error(_("failed to update ref '%s': %s"),
515+
advance_name, transaction_err.buf);
516+
goto cleanup;
517+
}
518+
}
519+
520+
/* Commit the ref transaction if we have one */
521+
if (transaction && result.clean == 1) {
522+
if (ref_transaction_commit(transaction, &transaction_err)) {
523+
ret = error(_("failed to commit ref transaction: %s"),
524+
transaction_err.buf);
525+
goto cleanup;
526+
}
452527
}
453528

454529
merge_finalize(&merge_opt, &result);
@@ -460,6 +535,9 @@ int cmd_replay(int argc,
460535
ret = result.clean;
461536

462537
cleanup:
538+
if (transaction)
539+
ref_transaction_free(transaction);
540+
strbuf_release(&transaction_err);
463541
release_revisions(&revs);
464542
free(advance_name);
465543

0 commit comments

Comments
 (0)