Skip to content

Commit 15cd4ef

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 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>
1 parent e031fa1 commit 15cd4ef

File tree

3 files changed

+199
-40
lines changed

3 files changed

+199
-40
lines changed

Documentation/git-replay.adoc

Lines changed: 39 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,27 @@ 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, similar to the way `git rebase --update-refs`
35+
updates multiple branches in the affected range.
3636

3737
--advance <branch>::
3838
Starting point at which to create the new commits; must be a
3939
branch name.
4040
+
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).
41+
The history is replayed on top of the <branch> and <branch> is updated to
42+
point at the tip of the resulting history. This is different from `--onto`,
43+
which uses the target only as a starting point without updating it.
44+
45+
--ref-action[=<mode>]::
46+
Control how references are updated. The mode can be:
47+
+
48+
--
49+
* `update` (default): Update refs directly using an atomic transaction.
50+
All refs are updated or none are (all-or-nothing behavior).
51+
* `print`: Output update-ref commands for pipeline use. This is the
52+
traditional behavior where output can be piped to `git update-ref --stdin`.
53+
--
4454

4555
<revision-range>::
4656
Range of commits to replay. More than one <revision-range> can
@@ -54,8 +64,11 @@ include::rev-list-options.adoc[]
5464
OUTPUT
5565
------
5666

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:
67+
By default, or with `--ref-action=update`, this command produces no output on
68+
success, as refs are updated directly using an atomic transaction.
69+
70+
When using `--ref-action=print`, the output is usable as input to
71+
`git update-ref --stdin`. It is of the form:
5972

6073
update refs/heads/branch1 ${NEW_branch1_HASH} ${OLD_branch1_HASH}
6174
update refs/heads/branch2 ${NEW_branch2_HASH} ${OLD_branch2_HASH}
@@ -81,40 +94,44 @@ To simply rebase `mybranch` onto `target`:
8194

8295
------------
8396
$ git replay --onto target origin/main..mybranch
97+
------------
98+
99+
The refs are updated atomically and no output is produced on success.
100+
101+
To see what would be updated without actually updating:
102+
103+
------------
104+
$ git replay --ref-action=print --onto target origin/main..mybranch
84105
update refs/heads/mybranch ${NEW_mybranch_HASH} ${OLD_mybranch_HASH}
85106
------------
86107

87108
To cherry-pick the commits from mybranch onto target:
88109

89110
------------
90111
$ git replay --advance target origin/main..mybranch
91-
update refs/heads/target ${NEW_target_HASH} ${OLD_target_HASH}
92112
------------
93113

94114
Note that the first two examples replay the exact same commits and on
95115
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.
116+
updates mybranch to point at the new commits and the second updates
117+
target to point at them.
98118

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

102122
------------
103123
$ 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}
107124
------------
108125

126+
All three branches (`branch1`, `branch2`, and `tipbranch`) are updated
127+
atomically.
128+
109129
When calling `git replay`, one does not need to specify a range of
110130
commits to replay using the syntax `A..B`; any range expression will
111131
do:
112132

113133
------------
114134
$ 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}
118135
------------
119136

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

builtin/replay.c

Lines changed: 101 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,38 @@ static struct commit *pick_regular_commit(struct repository *repo,
284289
return create_commit(repo, result->tree, pickme, replayed_base);
285290
}
286291

292+
static enum ref_action_mode parse_ref_action_mode(const char *ref_action, const char *source)
293+
{
294+
if (!ref_action || !strcmp(ref_action, "update"))
295+
return REF_ACTION_UPDATE;
296+
if (!strcmp(ref_action, "print"))
297+
return REF_ACTION_PRINT;
298+
die(_("invalid %s value: '%s'"), source, ref_action);
299+
}
300+
301+
static int handle_ref_update(enum ref_action_mode mode,
302+
struct ref_transaction *transaction,
303+
const char *refname,
304+
const struct object_id *new_oid,
305+
const struct object_id *old_oid,
306+
const char *reflog_msg,
307+
struct strbuf *err)
308+
{
309+
switch (mode) {
310+
case REF_ACTION_PRINT:
311+
printf("update %s %s %s\n",
312+
refname,
313+
oid_to_hex(new_oid),
314+
oid_to_hex(old_oid));
315+
return 0;
316+
case REF_ACTION_UPDATE:
317+
return ref_transaction_update(transaction, refname, new_oid, old_oid,
318+
NULL, NULL, 0, reflog_msg, err);
319+
default:
320+
BUG("unknown ref_action_mode %d", mode);
321+
}
322+
}
323+
287324
int cmd_replay(int argc,
288325
const char **argv,
289326
const char *prefix,
@@ -294,6 +331,8 @@ int cmd_replay(int argc,
294331
struct commit *onto = NULL;
295332
const char *onto_name = NULL;
296333
int contained = 0;
334+
const char *ref_action = NULL;
335+
enum ref_action_mode ref_mode = REF_ACTION_UPDATE;
297336

298337
struct rev_info revs;
299338
struct commit *last_commit = NULL;
@@ -302,12 +341,15 @@ int cmd_replay(int argc,
302341
struct merge_result result;
303342
struct strset *update_refs = NULL;
304343
kh_oid_map_t *replayed_commits;
344+
struct ref_transaction *transaction = NULL;
345+
struct strbuf transaction_err = STRBUF_INIT;
346+
struct strbuf reflog_msg = STRBUF_INIT;
305347
int ret = 0;
306348

307-
const char * const replay_usage[] = {
349+
const char *const replay_usage[] = {
308350
N_("(EXPERIMENTAL!) git replay "
309351
"([--contained] --onto <newbase> | --advance <branch>) "
310-
"<revision-range>..."),
352+
"[--ref-action[=<mode>]] <revision-range>..."),
311353
NULL
312354
};
313355
struct option replay_options[] = {
@@ -319,6 +361,9 @@ int cmd_replay(int argc,
319361
N_("replay onto given commit")),
320362
OPT_BOOL(0, "contained", &contained,
321363
N_("advance all branches contained in revision-range")),
364+
OPT_STRING(0, "ref-action", &ref_action,
365+
N_("mode"),
366+
N_("control ref update behavior (update|print)")),
322367
OPT_END()
323368
};
324369

@@ -333,6 +378,10 @@ int cmd_replay(int argc,
333378
die_for_incompatible_opt2(!!advance_name_opt, "--advance",
334379
contained, "--contained");
335380

381+
/* Parse ref action mode */
382+
if (ref_action)
383+
ref_mode = parse_ref_action_mode(ref_action, "--ref-action");
384+
336385
advance_name = xstrdup_or_null(advance_name_opt);
337386

338387
repo_init_revisions(repo, &revs, prefix);
@@ -389,6 +438,24 @@ int cmd_replay(int argc,
389438
determine_replay_mode(repo, &revs.cmdline, onto_name, &advance_name,
390439
&onto, &update_refs);
391440

441+
/* Build reflog message */
442+
if (advance_name_opt)
443+
strbuf_addf(&reflog_msg, "replay --advance %s", advance_name_opt);
444+
else
445+
strbuf_addf(&reflog_msg, "replay --onto %s",
446+
oid_to_hex(&onto->object.oid));
447+
448+
/* Initialize ref transaction if using update mode */
449+
if (ref_mode == REF_ACTION_UPDATE) {
450+
transaction = ref_store_transaction_begin(get_main_ref_store(repo),
451+
0, &transaction_err);
452+
if (!transaction) {
453+
ret = error(_("failed to begin ref transaction: %s"),
454+
transaction_err.buf);
455+
goto cleanup;
456+
}
457+
}
458+
392459
if (!onto) /* FIXME: Should handle replaying down to root commit */
393460
die("Replaying down to root commit is not supported yet!");
394461

@@ -434,21 +501,41 @@ int cmd_replay(int argc,
434501
if (decoration->type == DECORATION_REF_LOCAL &&
435502
(contained || strset_contains(update_refs,
436503
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));
504+
if (handle_ref_update(ref_mode, transaction,
505+
decoration->name,
506+
&last_commit->object.oid,
507+
&commit->object.oid,
508+
reflog_msg.buf,
509+
&transaction_err) < 0) {
510+
ret = error(_("failed to update ref '%s': %s"),
511+
decoration->name, transaction_err.buf);
512+
goto cleanup;
513+
}
441514
}
442515
decoration = decoration->next;
443516
}
444517
}
445518

446519
/* In --advance mode, advance the target ref */
447520
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));
521+
if (handle_ref_update(ref_mode, transaction, advance_name,
522+
&last_commit->object.oid,
523+
&onto->object.oid,
524+
reflog_msg.buf,
525+
&transaction_err) < 0) {
526+
ret = error(_("failed to update ref '%s': %s"),
527+
advance_name, transaction_err.buf);
528+
goto cleanup;
529+
}
530+
}
531+
532+
/* Commit the ref transaction if we have one */
533+
if (transaction && result.clean == 1) {
534+
if (ref_transaction_commit(transaction, &transaction_err)) {
535+
ret = error(_("failed to commit ref transaction: %s"),
536+
transaction_err.buf);
537+
goto cleanup;
538+
}
452539
}
453540

454541
merge_finalize(&merge_opt, &result);
@@ -460,6 +547,10 @@ int cmd_replay(int argc,
460547
ret = result.clean;
461548

462549
cleanup:
550+
if (transaction)
551+
ref_transaction_free(transaction);
552+
strbuf_release(&transaction_err);
553+
strbuf_release(&reflog_msg);
463554
release_revisions(&revs);
464555
free(advance_name);
465556

0 commit comments

Comments
 (0)