Re: [PATCH] refs: fix reflog of a symref whose target is in the same transaction
- From
- 井上司 <inoue.tsukasa.4@gmail.com>
- Date
- Nov 13, 2026, 07:21 UTC
- Message-ID
- <CAP8tz2+0+N1HLKc3y9hB-Ncaah7ni8ykrm5mB7N9Ji+Gc7zNaw@mail.gmail.com>
- In-Reply-To
- <CAOLa=ZQ559k_jrSrubdRcZofxLT+Fj=fi_e6BY=e2KJX_OJRag@mail.gmail.com>
Karthik Nayak <karthik.188@gmail.com> writes:
> Can't we use `string_list_lookup(&transaction->refnames, refname)`, > which is a sorted list and uses a binary search.
I tried that first, but ref_transaction_prepare() sorts refnames only at its start. The backends then add updates while preparing, for example split_symref_update() for an update through HEAD, and ref_transaction_add_update() appends those to refnames. A binary search can then miss a target added that way. With HEAD pointing at refs/heads/a:
update HEAD <new>
option no-deref
symref-update refs/heads/s refs/heads/arefnames becomes [HEAD, refs/heads/s, refs/heads/a], and a binary search for refs/heads/a does not find it. I checked this on both backends: with string_list_lookup() the reflog of s records the old value of a, with the linear scan it records <new>. Keeping refnames sorted with string_list_insert() would drop the duplicates that ref_update_reject_duplicates() is meant to catch, so I left the scan in. It only runs for symref updates that write a reflog. I'll add this case as a test in v2.
> So `update->rejection_err` would be set only if the reference update > failed. This would only happen if it was applied before the reflog > update. What about reference updates which fail after the reflog update?
As far as I can see, the backends record all rejections while preparing (in files_transaction_prepare(), reftable_be_transaction_prepare() and the packed backend's write_with_updates()), and write the reflogs when finishing. When the helper runs, the rejected updates are already marked. A failure while finishing fails the whole transaction rather than rejecting a single update.
> We could use a subshell, cd into it and then we don't have to > 1. Do `-C repo` everywhere > 2. Cleanup 'branch-cmd' and 'head-cmd'
Will do, and I'll add the blank lines.
> We should also probably add tests which use > `REF_TRANSACTION_ALLOW_FAILURE` and check if a given reference > update fails and do we apply the new/old value for the reflog then.
Good idea. I'll add a --batch-updates test where the update of the branch is rejected and HEAD is pointed at it, and check that HEAD's reflog records the branch's current value.
Thanks for the review! Tsukasa
2026年11月12日(木) 22:38 Karthik Nayak <karthik.188@gmail.com>:
Show 182 quoted lines
>
> tsukasa inoue <inoue.tsukasa.4@gmail.com> writes:
>
> > When a transaction updates a ref and also points a symref at it, for
> > example
> >
> > update refs/heads/y <new>
> > symref-update HEAD refs/heads/y
> >
> > the reflog entry for HEAD should record <new>. Both backends get the
> > value for that entry by resolving the target with
> > refs_resolve_ref_unsafe(), which reads the refs as they are before the
> > transaction is applied.
> >
> > The reftable backend writes all updates into one table at the end, so
> > it always sees the old value of y. When y is created by the same
> > transaction it sees no y at all, takes the symref as dangling, and
> > writes no reflog entry. The files backend commits the updates one at a
> > time in the order they were queued, so it gets it right only when the
> > update of y comes before the one of HEAD.
> >
> > Add ref_transaction_resolve_new_oid(), which takes the new value from
> > the transaction when it updates the target, following symref updates
> > within it, and reads the ref store otherwise. Use it in both backends.
> >
> > Signed-off-by: tsukasa inoue <inoue.tsukasa.4@gmail.com>
> > ---
> >
> > Notes:
> > This follows up on the report at
> > https://lore.kernel.org/git/CAP8tz2L3Dfhd+ec4+SFkZgXmzQnBg+3Y0REAV+1Hf5X415qnfw@mail.gmail.com/
> >
> > As discussed with Karthik there, this only moves the lookup of the
> > target's new value to refs.c. Moving the rest of the reflog logic out
> > of the backends, and resolving conflicting updates within a single
> > transaction, are left for later.
> >
> > refs.c | 47 +++++++++++++++++++++++++++++++++++++++++
> > refs/files-backend.c | 10 +++++----
> > refs/refs-internal.h | 12 +++++++++++
> > refs/reftable-backend.c | 9 ++++++--
> > t/t1400-update-ref.sh | 36 +++++++++++++++++++++++++++++++
> > 5 files changed, 108 insertions(+), 6 deletions(-)
> >
> > diff --git a/refs.c b/refs.c
> > index 951db56113..3a5c225fc9 100644
> > --- a/refs.c
> > +++ b/refs.c
> > @@ -1388,6 +1388,53 @@ struct ref_update *ref_transaction_add_update(
> > return update;
> > }
> >
> > +static struct ref_update *find_ref_update(struct ref_transaction *transaction,
> > + const char *refname)
> > +{
> > + size_t i;
> > +
> > + for (i = 0; i < transaction->nr; i++) {
> > + struct ref_update *update = transaction->updates[i];
> > +
> > + if (!(update->flags & REF_LOG_ONLY) &&
> > + !strcmp(update->refname, refname))
> > + return update;
> > + }
> > +
> > + return NULL;
> > +}
>
> Can't we use `string_list_lookup(&transaction->refnames, refname)`,
> which is a sorted list and uses a binary search.
>
> > +
> > +int ref_transaction_resolve_new_oid(struct ref_transaction *transaction,
> > + const char *refname,
> > + struct object_id *oid)
> > +{
> > + int depth;
> > +
> > + for (depth = 0; depth < SYMREF_MAXDEPTH; depth++) {
> > + struct ref_update *update;
> > +
> > + update = find_ref_update(transaction, refname);
> > + if (!update)
> > + break;
> > +
> > + if (!(update->flags & REF_HAVE_NEW) || update->rejection_err)
> > + break;
> > +
>
> So `update->rejection_err` would be set only if the reference update
> failed. This would only happen if it was applied before the reflog
> update. What about reference updates which fail after the reflog update?
> Would it make sense to schedule all reflog updates to the end, this
> would mean we know the status of all reference updates by then.
>
> > + if (update->new_target) {
> > + refname = update->new_target;
> > + continue;
> > + }
> > +
> > + if (is_null_oid(&update->new_oid))
> > + return 0;
> > + oidcpy(oid, &update->new_oid);
> > + return 1;
> > + }
> > +
> > + return !!refs_resolve_ref_unsafe(transaction->ref_store, refname,
> > + RESOLVE_REF_READING, oid, NULL);
> > +}
> > +
> > static int transaction_refname_valid(const char *refname,
> > const struct object_id *new_oid,
> > unsigned int flags, struct strbuf *err)
> > * symref updates. It would be nice to capture this as
>
> [snip]
>
> > diff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh
> > index 1a164c96f9..19f0987d2f 100755
> > --- a/t/t1400-update-ref.sh
> > +++ b/t/t1400-update-ref.sh
> > @@ -853,6 +853,42 @@ test_expect_success 'stdin delete symref works option no-deref' '
> > test_cmp expect actual
> > '
> >
> > +for action in update create
> > +do
> > + for order in branch-first head-first
> > + do
> > + test_expect_success "stdin symref-update HEAD logs target's new value ($action, $order)" '
> > + test_when_finished "rm -rf repo branch-cmd head-cmd" &&
>
> We could use a subshell, cd into it and then we don't have to
> 1. Do `-C repo` everywhere
> 2. Cleanup 'branch-cmd' and 'head-cmd'
>
> > + git init repo &&
> > + test_commit -C repo one &&
> > + test_commit -C repo two &&
> > + new=$(git -C repo rev-parse two) &&
> > + if test "$action" = update
> > + then
> > + git -C repo branch topic one
> > + fi &&
> > + echo "$action refs/heads/topic $new" >branch-cmd &&
> > + {
> > + echo "option no-deref" &&
> > + echo "symref-update HEAD refs/heads/topic"
> > + } >head-cmd &&
> > + if test "$order" = branch-first
> > + then
> > + cat branch-cmd head-cmd >stdin
> > + else
> > + cat head-cmd branch-cmd >stdin
> > + fi &&
> > + git -C repo update-ref -m "move HEAD" --stdin <stdin &&
> > + echo "move HEAD" >expect &&
> > + git -C repo reflog -1 --format=%gs HEAD >actual &&
> > + test_cmp expect actual &&
> > + echo $new >expect &&
> > + git -C repo reflog -1 --format=%H HEAD >actual &&
> > + test_cmp expect actual
>
> Nit: A few newlines to separate out blocks like the first few lines are
> setup and so on, could help readability :)
>
> > + '
> > + done
> > +done
> > +
>
> We should also probably add tests which use
> `REF_TRANSACTION_ALLOW_FAILURE` and check if a given reference
> update fails and do we apply the new/old value for the reflog then.
>
> > test_expect_success 'stdin update symref works flag --no-deref' '
> > git symbolic-ref TESTSYMREFONE $b &&
> > git symbolic-ref TESTSYMREFTWO $b &&
> > --
> > 2.56.0
>
> Thanks for the patch. Some small comments from me.