From: tsukasa inoue <inoue.tsukasa.4@gmail.com>
To: git@vger.kernel.org
Cc: Karthik Nayak <karthik.188@gmail.com>,
Patrick Steinhardt <ps@pks.im>,
Junio C Hamano <gitster@pobox.com>,
tsukasa inoue <inoue.tsukasa.4@gmail.com>
Subject: [PATCH] refs: fix reflog of a symref whose target is in the same transaction
Date: Tue, 10 Nov 2026 22:34:57 +0900 [thread overview]
Message-ID: <20261110133457.43700-1-inoue.tsukasa.4@gmail.com> (raw)
In-Reply-To: <CAOLa=ZTYevwM0YF0iQFnEwKG_buV7qg0QdjoaTkw0c8OQSd-mQ@mail.gmail.com>
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;
+}
+
+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;
+
+ 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)
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 87d04557a5..d8878f4df3 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -3156,6 +3156,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,
}
static int parse_and_write_reflog(struct files_ref_store *refs,
+ struct ref_transaction *transaction,
struct ref_update *update,
struct ref_lock *lock,
struct strbuf *err)
@@ -3179,9 +3180,9 @@ static int parse_and_write_reflog(struct files_ref_store *refs,
* We want to get the resolved OID for the target, to ensure
* that the correct value is added to the reflog.
*/
- if (!refs_resolve_ref_unsafe(&refs->base, update->new_target,
- RESOLVE_REF_READING,
- &update->new_oid, NULL)) {
+ if (!ref_transaction_resolve_new_oid(transaction,
+ update->new_target,
+ &update->new_oid)) {
/*
* TODO: currently we skip creating reflogs for dangling
* symref updates. It would be nice to capture this as
@@ -3377,7 +3378,8 @@ static int files_transaction_finish(struct ref_store *ref_store,
if (update->flags & REF_NEEDS_COMMIT ||
update->flags & REF_LOG_ONLY) {
- if (parse_and_write_reflog(refs, update, lock, err)) {
+ if (parse_and_write_reflog(refs, transaction, update,
+ lock, err)) {
ret = REF_TRANSACTION_ERROR_GENERIC;
goto cleanup;
}
diff --git a/refs/refs-internal.h b/refs/refs-internal.h
index c3ac7b556f..33ba285dc4 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
@@ -187,6 +187,18 @@ struct ref_update *ref_transaction_add_update(
const char *committer_info,
const char *msg);
+/*
+ * Resolve `refname` to the object ID it will have once `transaction` is
+ * committed: if the transaction updates `refname`, use the new value of
+ * that update (following `new_target` of symref updates in the
+ * transaction), otherwise read it from the ref store. Return 1 and fill
+ * `oid` if it resolves to an object, 0 if it would be dangling (missing,
+ * or deleted by the transaction). Meant to be used while committing.
+ */
+int ref_transaction_resolve_new_oid(struct ref_transaction *transaction,
+ const char *refname,
+ struct object_id *oid);
+
/*
* Transaction states.
*
diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c
index d0de066355..46142a4f73 100644
--- a/refs/reftable-backend.c
+++ b/refs/reftable-backend.c
@@ -940,6 +940,7 @@ struct reftable_transaction_update {
struct write_transaction_table_arg {
struct reftable_ref_store *refs;
+ struct ref_transaction *transaction;
struct reftable_backend *be;
struct reftable_addition *addition;
struct reftable_transaction_update *updates;
@@ -950,6 +951,7 @@ struct write_transaction_table_arg {
};
struct reftable_transaction_data {
+ struct ref_transaction *transaction;
struct write_transaction_table_arg *args;
size_t args_nr, args_alloc;
};
@@ -1014,6 +1016,7 @@ static int prepare_transaction_update(struct write_transaction_table_arg **out,
tx_data->args_alloc);
arg = &tx_data->args[tx_data->args_nr++];
arg->refs = refs;
+ arg->transaction = tx_data->transaction;
arg->be = be;
arg->addition = addition;
arg->updates = NULL;
@@ -1332,6 +1335,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
goto done;
tx_data = xcalloc(1, sizeof(*tx_data));
+ tx_data->transaction = transaction;
/*
* Preprocess all updates. For one we check that there are no duplicate
@@ -1557,8 +1561,9 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data
int create_reflog = 1;
if (u->new_target) {
- if (!refs_resolve_ref_unsafe(&arg->refs->base, u->new_target,
- RESOLVE_REF_READING, &u->new_oid, NULL)) {
+ if (!ref_transaction_resolve_new_oid(arg->transaction,
+ u->new_target,
+ &u->new_oid)) {
/*
* TODO: currently we skip creating reflogs for dangling
* symref updates. It would be nice to capture this as
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" &&
+ 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
+ '
+ done
+done
+
test_expect_success 'stdin update symref works flag --no-deref' '
git symbolic-ref TESTSYMREFONE $b &&
git symbolic-ref TESTSYMREFTWO $b &&
--
2.56.0
next prev parent reply other threads:[~2026-11-10 13:35 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-08 12:50 reftable: wrong HEAD reflog when its target is updated in the same transaction 井上司
2026-11-08 22:18 ` Karthik Nayak
2026-11-09 1:39 ` 井上司
2026-11-09 15:23 ` Karthik Nayak
2026-11-10 13:34 ` tsukasa inoue [this message]
2026-11-12 13:37 ` [PATCH] refs: fix reflog of a symref whose target is " Karthik Nayak
2026-11-13 7:21 ` 井上司
2026-11-17 9:56 ` [PATCH v2] " tsukasa inoue
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261110133457.43700-1-inoue.tsukasa.4@gmail.com \
--to=inoue.tsukasa.4@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=karthik.188@gmail.com \
--cc=ps@pks.im \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox