# reftable: wrong HEAD reflog when its target is updated in the same transaction

8 messages from 2026-11-08 to 2026-11-17. Participants: 井上司, Karthik Nayak, tsukasa inoue.
Thread: https://gitlist.dev/t/66806

## 井上司, 2026-11-08 12:50

Subject: reftable: wrong HEAD reflog when its target is updated in the same transaction
Message-ID: <CAP8tz2L3Dfhd+ec4+SFkZgXmzQnBg+3Y0REAV+1Hf5X415qnfw@mail.gmail.com>
URL: https://gitlist.dev/e/CAP8tz2L3Dfhd%2Bec4%2BSFkZgXmzQnBg%2B3Y0REAV%2B1Hf5X415qnfw%40mail.gmail.com

```
Hi,

With the reftable backend, if one transaction updates a branch and points
HEAD at it, the new HEAD reflog entry records the branch's old value. If
the branch is created in the same transaction, no HEAD reflog entry is
written at all. The files backend records the new value in both cases.

To reproduce on current master (fb255012ac):

  git init -q --ref-format=reftable repo && cd repo
  git commit -q --allow-empty -m one
  git commit -q --allow-empty -m two
  git branch y HEAD~
  printf '%s\n' start "update refs/heads/y $(git rev-parse HEAD)" \
          'option no-deref' 'symref-update HEAD refs/heads/y' commit |
  git update-ref -m test --stdin >/dev/null
  git log -1 --format='%h %s' HEAD
  git reflog -1 --format='%h %gs' HEAD

HEAD is at "two", but the "test" reflog entry points at "one". With
--ref-format=files it points at "two".

Using "create refs/heads/z <oid>" instead of updating y leaves HEAD's
reflog without a new entry.

I think the cause is write_transaction_table() in
refs/reftable-backend.c. For a symref update it gets the new oid for the
log record with refs_resolve_ref_unsafe() on new_target, which reads the
refs as they were before the transaction. So it sees the old value of y,
or no y at all, in which case the update is treated as a dangling symref
and the reflog entry is skipped.

I ran into this while looking at updating a new branch and HEAD in one
transaction for "git switch -c". Would taking the new oid from the
target's update in the same transaction, when there is one, be the right
fix? I can try to write a patch.

Thanks,
Tsukasa


```

## Karthik Nayak, 2026-11-08 22:18

Subject: Re: reftable: wrong HEAD reflog when its target is updated in the same transaction
Message-ID: <CAOLa=ZRSQ4_EGC8qTs6f0HBQyxj_k0Z-zt+uXt0JpO2Z3Mpqzg@mail.gmail.com>
URL: https://gitlist.dev/e/CAOLa%3DZRSQ4_EGC8qTs6f0HBQyxj_k0Z-zt%2BuXt0JpO2Z3Mpqzg%40mail.gmail.com
In-Reply-To: <CAP8tz2L3Dfhd+ec4+SFkZgXmzQnBg+3Y0REAV+1Hf5X415qnfw@mail.gmail.com>

```
井上司 <inoue.tsukasa.4@gmail.com> writes:

> Hi,
>
> With the reftable backend, if one transaction updates a branch and points
> HEAD at it, the new HEAD reflog entry records the branch's old value. If
> the branch is created in the same transaction, no HEAD reflog entry is
> written at all. The files backend records the new value in both cases.
>
> To reproduce on current master (fb255012ac):
>
>   git init -q --ref-format=reftable repo && cd repo
>   git commit -q --allow-empty -m one
>   git commit -q --allow-empty -m two
>   git branch y HEAD~
>   printf '%s\n' start "update refs/heads/y $(git rev-parse HEAD)" \
>           'option no-deref' 'symref-update HEAD refs/heads/y' commit |
>   git update-ref -m test --stdin >/dev/null
>   git log -1 --format='%h %s' HEAD
>   git reflog -1 --format='%h %gs' HEAD
>
> HEAD is at "two", but the "test" reflog entry points at "one". With
> --ref-format=files it points at "two".
>
> Using "create refs/heads/z <oid>" instead of updating y leaves HEAD's
> reflog without a new entry.
>

With the files backend, this isn't an issue only because the files
backend doesn't support atomicity. If it did, we'd have the same bug
there too ;)

> I think the cause is write_transaction_table() in
> refs/reftable-backend.c. For a symref update it gets the new oid for the
> log record with refs_resolve_ref_unsafe() on new_target, which reads the
> refs as they were before the transaction. So it sees the old value of y,
> or no y at all, in which case the update is treated as a dangling symref
> and the reflog entry is skipped.
>

I haven't looked at the code, but your inference seems to make sense to
me.

> I ran into this while looking at updating a new branch and HEAD in one
> transaction for "git switch -c". Would taking the new oid from the
> target's update in the same transaction, when there is one, be the right
> fix? I can try to write a patch.
>

That would definitely work, but this same bug would exist with any new
backend that is introduced and supports atomic transactions.

I wonder if we can move move reflog logic to the generic 'refs.c' layer.
So for a given transaction, 'refs.c' can already parse all the updates
and also set the reflog values. Then the individual backends would
simply receive the reflog entry to create and wouldn't have to worry
about solving inter-transaction conflict resoultion.

But that might be a complex undertaking. I prefer the generic approach,
but your approach would also definitely fix the issue and would be a
very welcome fix.

Thanks,
Karthik

```

## 井上司, 2026-11-09 01:39

Subject: Re: reftable: wrong HEAD reflog when its target is updated in the same transaction
Message-ID: <CAP8tz2KFb971L5_XY-hb8M=vfK-AsWwk_7YoMK6nCS8xhUKQ+Q@mail.gmail.com>
URL: https://gitlist.dev/e/CAP8tz2KFb971L5_XY-hb8M%3DvfK-AsWwk_7YoMK6nCS8xhUKQ%2BQ%40mail.gmail.com
In-Reply-To: <CAOLa=ZRSQ4_EGC8qTs6f0HBQyxj_k0Z-zt+uXt0JpO2Z3Mpqzg@mail.gmail.com>

```
Karthik Nayak <karthik.188@gmail.com> writes:

> With the files backend, this isn't an issue only because the files
> backend doesn't support atomicity. If it did, we'd have the same bug
> there too ;)

Thanks for looking at this so quickly. You're right, and it shows up
with files already. files commits the updates one by one in the order
they were queued, so the reflog is right only when the branch update
comes before HEAD. With "symref-update HEAD" first, files also records
the old oid, or no entry when the branch is created. I was wrong to say
files gets it right in both cases.

> I wonder if we can move move reflog logic to the generic 'refs.c' layer.

I like that direction. As a first step toward it, I'd like to add a
helper in refs.c that returns the new oid of a ref updated in the same
transaction, and have both backends use it before falling back to
refs_resolve_ref_unsafe(). That fixes both backends whatever the order,
and one test can cover both. Moving the rest of the reflog logic to
refs.c could then come on top of that.

Does that sound reasonable?

Thanks,
Tsukasa

2026年11月9日(月) 7:18 Karthik Nayak <karthik.188@gmail.com>:
>
> 井上司 <inoue.tsukasa.4@gmail.com> writes:
>
> > Hi,
> >
> > With the reftable backend, if one transaction updates a branch and points
> > HEAD at it, the new HEAD reflog entry records the branch's old value. If
> > the branch is created in the same transaction, no HEAD reflog entry is
> > written at all. The files backend records the new value in both cases.
> >
> > To reproduce on current master (fb255012ac):
> >
> >   git init -q --ref-format=reftable repo && cd repo
> >   git commit -q --allow-empty -m one
> >   git commit -q --allow-empty -m two
> >   git branch y HEAD~
> >   printf '%s\n' start "update refs/heads/y $(git rev-parse HEAD)" \
> >           'option no-deref' 'symref-update HEAD refs/heads/y' commit |
> >   git update-ref -m test --stdin >/dev/null
> >   git log -1 --format='%h %s' HEAD
> >   git reflog -1 --format='%h %gs' HEAD
> >
> > HEAD is at "two", but the "test" reflog entry points at "one". With
> > --ref-format=files it points at "two".
> >
> > Using "create refs/heads/z <oid>" instead of updating y leaves HEAD's
> > reflog without a new entry.
> >
>
> With the files backend, this isn't an issue only because the files
> backend doesn't support atomicity. If it did, we'd have the same bug
> there too ;)
>
> > I think the cause is write_transaction_table() in
> > refs/reftable-backend.c. For a symref update it gets the new oid for the
> > log record with refs_resolve_ref_unsafe() on new_target, which reads the
> > refs as they were before the transaction. So it sees the old value of y,
> > or no y at all, in which case the update is treated as a dangling symref
> > and the reflog entry is skipped.
> >
>
> I haven't looked at the code, but your inference seems to make sense to
> me.
>
> > I ran into this while looking at updating a new branch and HEAD in one
> > transaction for "git switch -c". Would taking the new oid from the
> > target's update in the same transaction, when there is one, be the right
> > fix? I can try to write a patch.
> >
>
> That would definitely work, but this same bug would exist with any new
> backend that is introduced and supports atomic transactions.
>
> I wonder if we can move move reflog logic to the generic 'refs.c' layer.
> So for a given transaction, 'refs.c' can already parse all the updates
> and also set the reflog values. Then the individual backends would
> simply receive the reflog entry to create and wouldn't have to worry
> about solving inter-transaction conflict resoultion.
>
> But that might be a complex undertaking. I prefer the generic approach,
> but your approach would also definitely fix the issue and would be a
> very welcome fix.
>
> Thanks,
> Karthik


```

## Karthik Nayak, 2026-11-09 15:23

Subject: Re: reftable: wrong HEAD reflog when its target is updated in the same transaction
Message-ID: <CAOLa=ZTYevwM0YF0iQFnEwKG_buV7qg0QdjoaTkw0c8OQSd-mQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAOLa%3DZTYevwM0YF0iQFnEwKG_buV7qg0QdjoaTkw0c8OQSd-mQ%40mail.gmail.com
In-Reply-To: <CAP8tz2KFb971L5_XY-hb8M=vfK-AsWwk_7YoMK6nCS8xhUKQ+Q@mail.gmail.com>

```
井上司 <inoue.tsukasa.4@gmail.com> writes:

> Karthik Nayak <karthik.188@gmail.com> writes:
>
>> With the files backend, this isn't an issue only because the files
>> backend doesn't support atomicity. If it did, we'd have the same bug
>> there too ;)
>
> Thanks for looking at this so quickly. You're right, and it shows up
> with files already. files commits the updates one by one in the order
> they were queued, so the reflog is right only when the branch update
> comes before HEAD. With "symref-update HEAD" first, files also records
> the old oid, or no entry when the branch is created. I was wrong to say
> files gets it right in both cases.
>

Exactly. So it's a bigger bug than just reftables.

>> I wonder if we can move move reflog logic to the generic 'refs.c' layer.
>
> I like that direction. As a first step toward it, I'd like to add a
> helper in refs.c that returns the new oid of a ref updated in the same
> transaction, and have both backends use it before falling back to
> refs_resolve_ref_unsafe(). That fixes both backends whatever the order,
> and one test can cover both. Moving the rest of the reflog logic to
> refs.c could then come on top of that.
>
> Does that sound reasonable?
>

Sounds like a good start to me! Looking forward to the patch. To also
add more context, we've also been thinking about doing conflict
resolution for updates within a given transaction for a while now. This
seems to play into that.

So a transaction like:

   delete refs/heads/foo
   create refs/heads/foo HEAD

would currently fail, which doesn't have to, but I haven't been able to
put some time into this yet.

> Thanks,
> Tsukasa
>
> 2026年11月9日(月) 7:18 Karthik Nayak <karthik.188@gmail.com>:

[snip]

```

## tsukasa inoue, 2026-11-10 13:34

Subject: [PATCH] refs: fix reflog of a symref whose target is in the same transaction
Message-ID: <20261110133457.43700-1-inoue.tsukasa.4@gmail.com>
URL: https://gitlist.dev/e/20261110133457.43700-1-inoue.tsukasa.4%40gmail.com
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



```

## Karthik Nayak, 2026-11-12 13:37

Subject: Re: [PATCH] refs: fix reflog of a symref whose target is in the same transaction
Message-ID: <CAOLa=ZQ559k_jrSrubdRcZofxLT+Fj=fi_e6BY=e2KJX_OJRag@mail.gmail.com>
URL: https://gitlist.dev/e/CAOLa%3DZQ559k_jrSrubdRcZofxLT%2BFj%3Dfi_e6BY%3De2KJX_OJRag%40mail.gmail.com
In-Reply-To: <20261110133457.43700-1-inoue.tsukasa.4@gmail.com>

```
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.

```

## 井上司, 2026-11-13 07:21

Subject: Re: [PATCH] refs: fix reflog of a symref whose target is in the same transaction
Message-ID: <CAP8tz2+0+N1HLKc3y9hB-Ncaah7ni8ykrm5mB7N9Ji+Gc7zNaw@mail.gmail.com>
URL: https://gitlist.dev/e/CAP8tz2%2B0%2BN1HLKc3y9hB-Ncaah7ni8ykrm5mB7N9Ji%2BGc7zNaw%40mail.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/a

refnames 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>:
>
> 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.


```

## tsukasa inoue, 2026-11-17 09:56

Subject: [PATCH v2] refs: fix reflog of a symref whose target is in the same transaction
Message-ID: <20261117095615.70423-1-inoue.tsukasa.4@gmail.com>
URL: https://gitlist.dev/e/20261117095615.70423-1-inoue.tsukasa.4%40gmail.com
In-Reply-To: <20261110133457.43700-1-inoue.tsukasa.4@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, treats 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 update 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:
    Changes since v1:
    
     - Run the tests in a subshell inside the test repository, with blank
       lines between the steps (Karthik).
     - Add a test where the symref target is updated through HEAD. That
       update is added to refnames after it is sorted, which is why the
       helper scans transaction->updates instead of using
       string_list_lookup().
     - Add a --batch-updates test where the update of the target is
       rejected.

 refs.c                  |  46 ++++++++++++++++++
 refs/files-backend.c    |  10 ++--
 refs/refs-internal.h    |  11 +++++
 refs/reftable-backend.c |   9 +++-
 t/t1400-update-ref.sh   | 101 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 171 insertions(+), 6 deletions(-)

diff --git a/refs.c b/refs.c
index 951db56113..d7bed9d7bb 100644
--- a/refs.c
+++ b/refs.c
@@ -1388,6 +1388,52 @@ 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 = 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..d4e8834627 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
@@ -187,6 +187,17 @@ 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`, its new value is used,
+ * following symref updates in the transaction; otherwise the ref store is
+ * read. Return 1 and fill `oid` if it resolves to an object, 0 if it is
+ * missing or deleted by the transaction.
+ */
+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..7bc4aaaae9 100755
--- a/t/t1400-update-ref.sh
+++ b/t/t1400-update-ref.sh
@@ -853,6 +853,107 @@ 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" &&
+			git init repo &&
+			(
+				cd repo &&
+				test_commit one &&
+				test_commit two &&
+				new=$(git rev-parse two) &&
+				if test "$action" = update
+				then
+					git 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 update-ref -m "move HEAD" --stdin <stdin &&
+
+				echo "move HEAD" >expect &&
+				git reflog -1 --format=%gs HEAD >actual &&
+				test_cmp expect actual &&
+				echo $new >expect &&
+				git reflog -1 --format=%H HEAD >actual &&
+				test_cmp expect actual
+			)
+		'
+	done
+done
+
+test_expect_success 'stdin symref-update logs new value of target updated via HEAD' '
+	test_when_finished "rm -rf repo" &&
+	git init -b a repo &&
+	(
+		cd repo &&
+		test_commit one &&
+		test_commit two &&
+		new=$(git rev-parse two) &&
+		git update-ref refs/heads/a one &&
+
+		cat >stdin <<-EOF &&
+		update HEAD $new
+		option no-deref
+		symref-update refs/heads/s refs/heads/a
+		EOF
+		git update-ref -m "move s" --stdin <stdin &&
+
+		echo "move s" >expect &&
+		git reflog -1 --format=%gs refs/heads/s >actual &&
+		test_cmp expect actual &&
+		echo $new >expect &&
+		git reflog -1 --format=%H refs/heads/s >actual &&
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'stdin batch-updates symref-update HEAD does not log rejected target value' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		test_commit one &&
+		test_commit two &&
+		old=$(git rev-parse one) &&
+		new=$(git rev-parse two) &&
+		git branch y one &&
+
+		cat >stdin <<-EOF &&
+		update refs/heads/y $new $new
+		option no-deref
+		symref-update HEAD refs/heads/y
+		EOF
+		git update-ref -m "move HEAD" --batch-updates --stdin <stdin >stdout &&
+
+		test_grep "rejected refs/heads/y" stdout &&
+		echo $old >expect &&
+		git rev-parse refs/heads/y >actual &&
+		test_cmp expect actual &&
+		echo refs/heads/y >expect &&
+		git symbolic-ref HEAD >actual &&
+		test_cmp expect actual &&
+		echo "move HEAD" >expect &&
+		git reflog -1 --format=%gs HEAD >actual &&
+		test_cmp expect actual &&
+		echo $old >expect &&
+		git reflog -1 --format=%H HEAD >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_expect_success 'stdin update symref works flag --no-deref' '
 	git symbolic-ref TESTSYMREFONE $b &&
 	git symbolic-ref TESTSYMREFTWO $b &&

Range-diff against v1:
1:  337abe33a3 ! 1:  acd2808b3a refs: fix reflog of a symref whose target is in the same transaction
    @@ Commit message
     
         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
    +    transaction it sees no y at all, treats 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.
    +    update of y comes before the update of HEAD.
     
         Add ref_transaction_resolve_new_oid(), which takes the new value from
         the transaction when it updates the target, following symref updates
    @@ Commit message
     
     
      ## Notes ##
    -    This follows up on the report at
    -    https://lore.kernel.org/git/CAP8tz2L3Dfhd+ec4+SFkZgXmzQnBg+3Y0REAV+1Hf5X415qnfw@mail.gmail.com/
    +    Changes since v1:
     
    -    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.
    +     - Run the tests in a subshell inside the test repository, with blank
    +       lines between the steps (Karthik).
    +     - Add a test where the symref target is updated through HEAD. That
    +       update is added to refnames after it is sorted, which is why the
    +       helper scans transaction->updates instead of using
    +       string_list_lookup().
    +     - Add a --batch-updates test where the update of the target is
    +       rejected.
     
      ## refs.c ##
     @@ refs.c: struct ref_update *ref_transaction_add_update(
    @@ refs.c: struct ref_update *ref_transaction_add_update(
     +	int depth;
     +
     +	for (depth = 0; depth < SYMREF_MAXDEPTH; depth++) {
    -+		struct ref_update *update;
    ++		struct ref_update *update = find_ref_update(transaction, refname);
     +
    -+		update = find_ref_update(transaction, refname);
     +		if (!update)
     +			break;
     +
    @@ refs/refs-internal.h: struct ref_update *ref_transaction_add_update(
      
     +/*
     + * 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.
    ++ * committed. If the transaction updates `refname`, its new value is used,
    ++ * following symref updates in the transaction; otherwise the ref store is
    ++ * read. Return 1 and fill `oid` if it resolves to an object, 0 if it is
    ++ * missing or deleted by the transaction.
     + */
     +int ref_transaction_resolve_new_oid(struct ref_transaction *transaction,
     +				    const char *refname,
    @@ t/t1400-update-ref.sh: test_expect_success 'stdin delete symref works option no-
     +	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" &&
    ++			test_when_finished "rm -rf repo" &&
     +			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
    ++			(
    ++				cd repo &&
    ++				test_commit one &&
    ++				test_commit two &&
    ++				new=$(git rev-parse two) &&
    ++				if test "$action" = update
    ++				then
    ++					git 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 update-ref -m "move HEAD" --stdin <stdin &&
    ++
    ++				echo "move HEAD" >expect &&
    ++				git reflog -1 --format=%gs HEAD >actual &&
    ++				test_cmp expect actual &&
    ++				echo $new >expect &&
    ++				git reflog -1 --format=%H HEAD >actual &&
    ++				test_cmp expect actual
    ++			)
     +		'
     +	done
     +done
    ++
    ++test_expect_success 'stdin symref-update logs new value of target updated via HEAD' '
    ++	test_when_finished "rm -rf repo" &&
    ++	git init -b a repo &&
    ++	(
    ++		cd repo &&
    ++		test_commit one &&
    ++		test_commit two &&
    ++		new=$(git rev-parse two) &&
    ++		git update-ref refs/heads/a one &&
    ++
    ++		cat >stdin <<-EOF &&
    ++		update HEAD $new
    ++		option no-deref
    ++		symref-update refs/heads/s refs/heads/a
    ++		EOF
    ++		git update-ref -m "move s" --stdin <stdin &&
    ++
    ++		echo "move s" >expect &&
    ++		git reflog -1 --format=%gs refs/heads/s >actual &&
    ++		test_cmp expect actual &&
    ++		echo $new >expect &&
    ++		git reflog -1 --format=%H refs/heads/s >actual &&
    ++		test_cmp expect actual
    ++	)
    ++'
    ++
    ++test_expect_success 'stdin batch-updates symref-update HEAD does not log rejected target value' '
    ++	test_when_finished "rm -rf repo" &&
    ++	git init repo &&
    ++	(
    ++		cd repo &&
    ++		test_commit one &&
    ++		test_commit two &&
    ++		old=$(git rev-parse one) &&
    ++		new=$(git rev-parse two) &&
    ++		git branch y one &&
    ++
    ++		cat >stdin <<-EOF &&
    ++		update refs/heads/y $new $new
    ++		option no-deref
    ++		symref-update HEAD refs/heads/y
    ++		EOF
    ++		git update-ref -m "move HEAD" --batch-updates --stdin <stdin >stdout &&
    ++
    ++		test_grep "rejected refs/heads/y" stdout &&
    ++		echo $old >expect &&
    ++		git rev-parse refs/heads/y >actual &&
    ++		test_cmp expect actual &&
    ++		echo refs/heads/y >expect &&
    ++		git symbolic-ref HEAD >actual &&
    ++		test_cmp expect actual &&
    ++		echo "move HEAD" >expect &&
    ++		git reflog -1 --format=%gs HEAD >actual &&
    ++		test_cmp expect actual &&
    ++		echo $old >expect &&
    ++		git reflog -1 --format=%H HEAD >actual &&
    ++		test_cmp expect actual
    ++	)
    ++'
     +
      test_expect_success 'stdin update symref works flag --no-deref' '
      	git symbolic-ref TESTSYMREFONE $b &&
-- 
2.54.0 (Apple Git-157)



```
