From: Karthik Nayak Date: Thu, 12 Nov 2026 13:37:58 GMT Subject: Re: [PATCH] refs: fix reflog of a symref whose target is in the same transaction Message-ID: In-Reply-To: <20261110133457.43700-1-inoue.tsukasa.4@gmail.com> tsukasa inoue writes: > When a transaction updates a ref and also points a symref at it, for > example > > update refs/heads/y > symref-update HEAD refs/heads/y > > the reflog entry for HEAD should record . 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 > --- > > 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 + 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.