{"thread":{"id":"66806","subject":"reftable: wrong HEAD reflog when its target is updated in the same transaction","startedAt":"2026-11-08T12:50:55Z","lastAt":"2026-11-17T09:56:15Z","messageCount":8,"participants":["井上司","Karthik Nayak","tsukasa inoue"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"557841","messageId":"CAP8tz2L3Dfhd+ec4+SFkZgXmzQnBg+3Y0REAV+1Hf5X415qnfw@mail.gmail.com","threadId":"66806","inReplyTo":null,"subject":"reftable: wrong HEAD reflog when its target is updated in the same transaction","fromName":"井上司","fromEmail":"inoue.tsukasa.4@gmail.com","sentAt":"2026-11-08T12:50:55Z","receivedAt":"2026-11-08T12:50:55Z","isPatch":false,"sender":{"key":"inoue.tsukasa.4@gmail.com","avatar":null},"body":"Hi,\n\nWith the reftable backend, if one transaction updates a branch and points\nHEAD at it, the new HEAD reflog entry records the branch's old value. If\nthe branch is created in the same transaction, no HEAD reflog entry is\nwritten at all. The files backend records the new value in both cases.\n\nTo reproduce on current master (fb255012ac):\n\n  git init -q --ref-format=reftable repo && cd repo\n  git commit -q --allow-empty -m one\n  git commit -q --allow-empty -m two\n  git branch y HEAD~\n  printf '%s\\n' start \"update refs/heads/y $(git rev-parse HEAD)\" \\\n          'option no-deref' 'symref-update HEAD refs/heads/y' commit |\n  git update-ref -m test --stdin >/dev/null\n  git log -1 --format='%h %s' HEAD\n  git reflog -1 --format='%h %gs' HEAD\n\nHEAD is at \"two\", but the \"test\" reflog entry points at \"one\". With\n--ref-format=files it points at \"two\".\n\nUsing \"create refs/heads/z <oid>\" instead of updating y leaves HEAD's\nreflog without a new entry.\n\nI think the cause is write_transaction_table() in\nrefs/reftable-backend.c. For a symref update it gets the new oid for the\nlog record with refs_resolve_ref_unsafe() on new_target, which reads the\nrefs as they were before the transaction. So it sees the old value of y,\nor no y at all, in which case the update is treated as a dangling symref\nand the reflog entry is skipped.\n\nI ran into this while looking at updating a new branch and HEAD in one\ntransaction for \"git switch -c\". Would taking the new oid from the\ntarget's update in the same transaction, when there is one, be the right\nfix? I can try to write a patch.\n\nThanks,\nTsukasa\n\n"},{"id":"557864","messageId":"CAOLa=ZRSQ4_EGC8qTs6f0HBQyxj_k0Z-zt+uXt0JpO2Z3Mpqzg@mail.gmail.com","threadId":"66806","inReplyTo":"CAP8tz2L3Dfhd+ec4+SFkZgXmzQnBg+3Y0REAV+1Hf5X415qnfw@mail.gmail.com","subject":"Re: reftable: wrong HEAD reflog when its target is updated in the same transaction","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-11-08T22:18:04Z","receivedAt":"2026-11-08T22:18:04Z","isPatch":false,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"井上司 <inoue.tsukasa.4@gmail.com> writes:\n\n> Hi,\n>\n> With the reftable backend, if one transaction updates a branch and points\n> HEAD at it, the new HEAD reflog entry records the branch's old value. If\n> the branch is created in the same transaction, no HEAD reflog entry is\n> written at all. The files backend records the new value in both cases.\n>\n> To reproduce on current master (fb255012ac):\n>\n>   git init -q --ref-format=reftable repo && cd repo\n>   git commit -q --allow-empty -m one\n>   git commit -q --allow-empty -m two\n>   git branch y HEAD~\n>   printf '%s\\n' start \"update refs/heads/y $(git rev-parse HEAD)\" \\\n>           'option no-deref' 'symref-update HEAD refs/heads/y' commit |\n>   git update-ref -m test --stdin >/dev/null\n>   git log -1 --format='%h %s' HEAD\n>   git reflog -1 --format='%h %gs' HEAD\n>\n> HEAD is at \"two\", but the \"test\" reflog entry points at \"one\". With\n> --ref-format=files it points at \"two\".\n>\n> Using \"create refs/heads/z <oid>\" instead of updating y leaves HEAD's\n> reflog without a new entry.\n>\n\nWith the files backend, this isn't an issue only because the files\nbackend doesn't support atomicity. If it did, we'd have the same bug\nthere too ;)\n\n> I think the cause is write_transaction_table() in\n> refs/reftable-backend.c. For a symref update it gets the new oid for the\n> log record with refs_resolve_ref_unsafe() on new_target, which reads the\n> refs as they were before the transaction. So it sees the old value of y,\n> or no y at all, in which case the update is treated as a dangling symref\n> and the reflog entry is skipped.\n>\n\nI haven't looked at the code, but your inference seems to make sense to\nme.\n\n> I ran into this while looking at updating a new branch and HEAD in one\n> transaction for \"git switch -c\". Would taking the new oid from the\n> target's update in the same transaction, when there is one, be the right\n> fix? I can try to write a patch.\n>\n\nThat would definitely work, but this same bug would exist with any new\nbackend that is introduced and supports atomic transactions.\n\nI wonder if we can move move reflog logic to the generic 'refs.c' layer.\nSo for a given transaction, 'refs.c' can already parse all the updates\nand also set the reflog values. Then the individual backends would\nsimply receive the reflog entry to create and wouldn't have to worry\nabout solving inter-transaction conflict resoultion.\n\nBut that might be a complex undertaking. I prefer the generic approach,\nbut your approach would also definitely fix the issue and would be a\nvery welcome fix.\n\nThanks,\nKarthik\n"},{"id":"557867","messageId":"CAP8tz2KFb971L5_XY-hb8M=vfK-AsWwk_7YoMK6nCS8xhUKQ+Q@mail.gmail.com","threadId":"66806","inReplyTo":"CAOLa=ZRSQ4_EGC8qTs6f0HBQyxj_k0Z-zt+uXt0JpO2Z3Mpqzg@mail.gmail.com","subject":"Re: reftable: wrong HEAD reflog when its target is updated in the same transaction","fromName":"井上司","fromEmail":"inoue.tsukasa.4@gmail.com","sentAt":"2026-11-09T01:39:16Z","receivedAt":"2026-11-09T01:39:16Z","isPatch":false,"sender":{"key":"inoue.tsukasa.4@gmail.com","avatar":null},"body":"Karthik Nayak <karthik.188@gmail.com> writes:\n\n> With the files backend, this isn't an issue only because the files\n> backend doesn't support atomicity. If it did, we'd have the same bug\n> there too ;)\n\nThanks for looking at this so quickly. You're right, and it shows up\nwith files already. files commits the updates one by one in the order\nthey were queued, so the reflog is right only when the branch update\ncomes before HEAD. With \"symref-update HEAD\" first, files also records\nthe old oid, or no entry when the branch is created. I was wrong to say\nfiles gets it right in both cases.\n\n> I wonder if we can move move reflog logic to the generic 'refs.c' layer.\n\nI like that direction. As a first step toward it, I'd like to add a\nhelper in refs.c that returns the new oid of a ref updated in the same\ntransaction, and have both backends use it before falling back to\nrefs_resolve_ref_unsafe(). That fixes both backends whatever the order,\nand one test can cover both. Moving the rest of the reflog logic to\nrefs.c could then come on top of that.\n\nDoes that sound reasonable?\n\nThanks,\nTsukasa\n\n2026年11月9日(月) 7:18 Karthik Nayak <karthik.188@gmail.com>:\n>\n> 井上司 <inoue.tsukasa.4@gmail.com> writes:\n>\n> > Hi,\n> >\n> > With the reftable backend, if one transaction updates a branch and points\n> > HEAD at it, the new HEAD reflog entry records the branch's old value. If\n> > the branch is created in the same transaction, no HEAD reflog entry is\n> > written at all. The files backend records the new value in both cases.\n> >\n> > To reproduce on current master (fb255012ac):\n> >\n> >   git init -q --ref-format=reftable repo && cd repo\n> >   git commit -q --allow-empty -m one\n> >   git commit -q --allow-empty -m two\n> >   git branch y HEAD~\n> >   printf '%s\\n' start \"update refs/heads/y $(git rev-parse HEAD)\" \\\n> >           'option no-deref' 'symref-update HEAD refs/heads/y' commit |\n> >   git update-ref -m test --stdin >/dev/null\n> >   git log -1 --format='%h %s' HEAD\n> >   git reflog -1 --format='%h %gs' HEAD\n> >\n> > HEAD is at \"two\", but the \"test\" reflog entry points at \"one\". With\n> > --ref-format=files it points at \"two\".\n> >\n> > Using \"create refs/heads/z <oid>\" instead of updating y leaves HEAD's\n> > reflog without a new entry.\n> >\n>\n> With the files backend, this isn't an issue only because the files\n> backend doesn't support atomicity. If it did, we'd have the same bug\n> there too ;)\n>\n> > I think the cause is write_transaction_table() in\n> > refs/reftable-backend.c. For a symref update it gets the new oid for the\n> > log record with refs_resolve_ref_unsafe() on new_target, which reads the\n> > refs as they were before the transaction. So it sees the old value of y,\n> > or no y at all, in which case the update is treated as a dangling symref\n> > and the reflog entry is skipped.\n> >\n>\n> I haven't looked at the code, but your inference seems to make sense to\n> me.\n>\n> > I ran into this while looking at updating a new branch and HEAD in one\n> > transaction for \"git switch -c\". Would taking the new oid from the\n> > target's update in the same transaction, when there is one, be the right\n> > fix? I can try to write a patch.\n> >\n>\n> That would definitely work, but this same bug would exist with any new\n> backend that is introduced and supports atomic transactions.\n>\n> I wonder if we can move move reflog logic to the generic 'refs.c' layer.\n> So for a given transaction, 'refs.c' can already parse all the updates\n> and also set the reflog values. Then the individual backends would\n> simply receive the reflog entry to create and wouldn't have to worry\n> about solving inter-transaction conflict resoultion.\n>\n> But that might be a complex undertaking. I prefer the generic approach,\n> but your approach would also definitely fix the issue and would be a\n> very welcome fix.\n>\n> Thanks,\n> Karthik\n\n"},{"id":"557952","messageId":"CAOLa=ZTYevwM0YF0iQFnEwKG_buV7qg0QdjoaTkw0c8OQSd-mQ@mail.gmail.com","threadId":"66806","inReplyTo":"CAP8tz2KFb971L5_XY-hb8M=vfK-AsWwk_7YoMK6nCS8xhUKQ+Q@mail.gmail.com","subject":"Re: reftable: wrong HEAD reflog when its target is updated in the same transaction","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-11-09T15:23:52Z","receivedAt":"2026-11-09T15:23:52Z","isPatch":false,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"井上司 <inoue.tsukasa.4@gmail.com> writes:\n\n> Karthik Nayak <karthik.188@gmail.com> writes:\n>\n>> With the files backend, this isn't an issue only because the files\n>> backend doesn't support atomicity. If it did, we'd have the same bug\n>> there too ;)\n>\n> Thanks for looking at this so quickly. You're right, and it shows up\n> with files already. files commits the updates one by one in the order\n> they were queued, so the reflog is right only when the branch update\n> comes before HEAD. With \"symref-update HEAD\" first, files also records\n> the old oid, or no entry when the branch is created. I was wrong to say\n> files gets it right in both cases.\n>\n\nExactly. So it's a bigger bug than just reftables.\n\n>> I wonder if we can move move reflog logic to the generic 'refs.c' layer.\n>\n> I like that direction. As a first step toward it, I'd like to add a\n> helper in refs.c that returns the new oid of a ref updated in the same\n> transaction, and have both backends use it before falling back to\n> refs_resolve_ref_unsafe(). That fixes both backends whatever the order,\n> and one test can cover both. Moving the rest of the reflog logic to\n> refs.c could then come on top of that.\n>\n> Does that sound reasonable?\n>\n\nSounds like a good start to me! Looking forward to the patch. To also\nadd more context, we've also been thinking about doing conflict\nresolution for updates within a given transaction for a while now. This\nseems to play into that.\n\nSo a transaction like:\n\n   delete refs/heads/foo\n   create refs/heads/foo HEAD\n\nwould currently fail, which doesn't have to, but I haven't been able to\nput some time into this yet.\n\n> Thanks,\n> Tsukasa\n>\n> 2026年11月9日(月) 7:18 Karthik Nayak <karthik.188@gmail.com>:\n\n[snip]\n"},{"id":"558082","messageId":"20261110133457.43700-1-inoue.tsukasa.4@gmail.com","threadId":"66806","inReplyTo":"CAOLa=ZTYevwM0YF0iQFnEwKG_buV7qg0QdjoaTkw0c8OQSd-mQ@mail.gmail.com","subject":"[PATCH] refs: fix reflog of a symref whose target is in the same transaction","fromName":"tsukasa inoue","fromEmail":"inoue.tsukasa.4@gmail.com","sentAt":"2026-11-10T13:34:57Z","receivedAt":"2026-11-10T13:34:57Z","isPatch":true,"sender":{"key":"inoue.tsukasa.4@gmail.com","avatar":null},"body":"When a transaction updates a ref and also points a symref at it, for\nexample\n\n    update refs/heads/y <new>\n    symref-update HEAD refs/heads/y\n\nthe reflog entry for HEAD should record <new>. Both backends get the\nvalue for that entry by resolving the target with\nrefs_resolve_ref_unsafe(), which reads the refs as they are before the\ntransaction is applied.\n\nThe reftable backend writes all updates into one table at the end, so\nit always sees the old value of y. When y is created by the same\ntransaction it sees no y at all, takes the symref as dangling, and\nwrites no reflog entry. The files backend commits the updates one at a\ntime in the order they were queued, so it gets it right only when the\nupdate of y comes before the one of HEAD.\n\nAdd ref_transaction_resolve_new_oid(), which takes the new value from\nthe transaction when it updates the target, following symref updates\nwithin it, and reads the ref store otherwise. Use it in both backends.\n\nSigned-off-by: tsukasa inoue <inoue.tsukasa.4@gmail.com>\n---\n\nNotes:\n    This follows up on the report at\n    https://lore.kernel.org/git/CAP8tz2L3Dfhd+ec4+SFkZgXmzQnBg+3Y0REAV+1Hf5X415qnfw@mail.gmail.com/\n    \n    As discussed with Karthik there, this only moves the lookup of the\n    target's new value to refs.c. Moving the rest of the reflog logic out\n    of the backends, and resolving conflicting updates within a single\n    transaction, are left for later.\n\n refs.c                  | 47 +++++++++++++++++++++++++++++++++++++++++\n refs/files-backend.c    | 10 +++++----\n refs/refs-internal.h    | 12 +++++++++++\n refs/reftable-backend.c |  9 ++++++--\n t/t1400-update-ref.sh   | 36 +++++++++++++++++++++++++++++++\n 5 files changed, 108 insertions(+), 6 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 951db56113..3a5c225fc9 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1388,6 +1388,53 @@ struct ref_update *ref_transaction_add_update(\n \treturn update;\n }\n \n+static struct ref_update *find_ref_update(struct ref_transaction *transaction,\n+\t\t\t\t\t  const char *refname)\n+{\n+\tsize_t i;\n+\n+\tfor (i = 0; i < transaction->nr; i++) {\n+\t\tstruct ref_update *update = transaction->updates[i];\n+\n+\t\tif (!(update->flags & REF_LOG_ONLY) &&\n+\t\t    !strcmp(update->refname, refname))\n+\t\t\treturn update;\n+\t}\n+\n+\treturn NULL;\n+}\n+\n+int ref_transaction_resolve_new_oid(struct ref_transaction *transaction,\n+\t\t\t\t    const char *refname,\n+\t\t\t\t    struct object_id *oid)\n+{\n+\tint depth;\n+\n+\tfor (depth = 0; depth < SYMREF_MAXDEPTH; depth++) {\n+\t\tstruct ref_update *update;\n+\n+\t\tupdate = find_ref_update(transaction, refname);\n+\t\tif (!update)\n+\t\t\tbreak;\n+\n+\t\tif (!(update->flags & REF_HAVE_NEW) || update->rejection_err)\n+\t\t\tbreak;\n+\n+\t\tif (update->new_target) {\n+\t\t\trefname = update->new_target;\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tif (is_null_oid(&update->new_oid))\n+\t\t\treturn 0;\n+\t\toidcpy(oid, &update->new_oid);\n+\t\treturn 1;\n+\t}\n+\n+\treturn !!refs_resolve_ref_unsafe(transaction->ref_store, refname,\n+\t\t\t\t\t RESOLVE_REF_READING, oid, NULL);\n+}\n+\n static int transaction_refname_valid(const char *refname,\n \t\t\t\t     const struct object_id *new_oid,\n \t\t\t\t     unsigned int flags, struct strbuf *err)\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 87d04557a5..d8878f4df3 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -3156,6 +3156,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n }\n \n static int parse_and_write_reflog(struct files_ref_store *refs,\n+\t\t\t\t  struct ref_transaction *transaction,\n \t\t\t\t  struct ref_update *update,\n \t\t\t\t  struct ref_lock *lock,\n \t\t\t\t  struct strbuf *err)\n@@ -3179,9 +3180,9 @@ static int parse_and_write_reflog(struct files_ref_store *refs,\n \t\t * We want to get the resolved OID for the target, to ensure\n \t\t * that the correct value is added to the reflog.\n \t\t */\n-\t\tif (!refs_resolve_ref_unsafe(&refs->base, update->new_target,\n-\t\t\t\t\t     RESOLVE_REF_READING,\n-\t\t\t\t\t     &update->new_oid, NULL)) {\n+\t\tif (!ref_transaction_resolve_new_oid(transaction,\n+\t\t\t\t\t\t     update->new_target,\n+\t\t\t\t\t\t     &update->new_oid)) {\n \t\t\t/*\n \t\t\t * TODO: currently we skip creating reflogs for dangling\n \t\t\t * symref updates. It would be nice to capture this as\n@@ -3377,7 +3378,8 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \n \t\tif (update->flags & REF_NEEDS_COMMIT ||\n \t\t    update->flags & REF_LOG_ONLY) {\n-\t\t\tif (parse_and_write_reflog(refs, update, lock, err)) {\n+\t\t\tif (parse_and_write_reflog(refs, transaction, update,\n+\t\t\t\t\t\t   lock, err)) {\n \t\t\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n \t\t\t\tgoto cleanup;\n \t\t\t}\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex c3ac7b556f..33ba285dc4 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -187,6 +187,18 @@ struct ref_update *ref_transaction_add_update(\n \t\tconst char *committer_info,\n \t\tconst char *msg);\n \n+/*\n+ * Resolve `refname` to the object ID it will have once `transaction` is\n+ * committed: if the transaction updates `refname`, use the new value of\n+ * that update (following `new_target` of symref updates in the\n+ * transaction), otherwise read it from the ref store. Return 1 and fill\n+ * `oid` if it resolves to an object, 0 if it would be dangling (missing,\n+ * or deleted by the transaction). Meant to be used while committing.\n+ */\n+int ref_transaction_resolve_new_oid(struct ref_transaction *transaction,\n+\t\t\t\t    const char *refname,\n+\t\t\t\t    struct object_id *oid);\n+\n /*\n  * Transaction states.\n  *\ndiff --git a/refs/reftable-backend.c b/refs/reftable-backend.c\nindex d0de066355..46142a4f73 100644\n--- a/refs/reftable-backend.c\n+++ b/refs/reftable-backend.c\n@@ -940,6 +940,7 @@ struct reftable_transaction_update {\n \n struct write_transaction_table_arg {\n \tstruct reftable_ref_store *refs;\n+\tstruct ref_transaction *transaction;\n \tstruct reftable_backend *be;\n \tstruct reftable_addition *addition;\n \tstruct reftable_transaction_update *updates;\n@@ -950,6 +951,7 @@ struct write_transaction_table_arg {\n };\n \n struct reftable_transaction_data {\n+\tstruct ref_transaction *transaction;\n \tstruct write_transaction_table_arg *args;\n \tsize_t args_nr, args_alloc;\n };\n@@ -1014,6 +1016,7 @@ static int prepare_transaction_update(struct write_transaction_table_arg **out,\n \t\t\t   tx_data->args_alloc);\n \t\targ = &tx_data->args[tx_data->args_nr++];\n \t\targ->refs = refs;\n+\t\targ->transaction = tx_data->transaction;\n \t\targ->be = be;\n \t\targ->addition = addition;\n \t\targ->updates = NULL;\n@@ -1332,6 +1335,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \t\tgoto done;\n \n \ttx_data = xcalloc(1, sizeof(*tx_data));\n+\ttx_data->transaction = transaction;\n \n \t/*\n \t * Preprocess all updates. For one we check that there are no duplicate\n@@ -1557,8 +1561,9 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data\n \t\t\tint create_reflog = 1;\n \n \t\t\tif (u->new_target) {\n-\t\t\t\tif (!refs_resolve_ref_unsafe(&arg->refs->base, u->new_target,\n-\t\t\t\t\t\t\t     RESOLVE_REF_READING, &u->new_oid, NULL)) {\n+\t\t\t\tif (!ref_transaction_resolve_new_oid(arg->transaction,\n+\t\t\t\t\t\t\t\t     u->new_target,\n+\t\t\t\t\t\t\t\t     &u->new_oid)) {\n \t\t\t\t\t/*\n \t\t\t\t\t * TODO: currently we skip creating reflogs for dangling\n \t\t\t\t\t * symref updates. It would be nice to capture this as\ndiff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh\nindex 1a164c96f9..19f0987d2f 100755\n--- a/t/t1400-update-ref.sh\n+++ b/t/t1400-update-ref.sh\n@@ -853,6 +853,42 @@ test_expect_success 'stdin delete symref works option no-deref' '\n \ttest_cmp expect actual\n '\n \n+for action in update create\n+do\n+\tfor order in branch-first head-first\n+\tdo\n+\t\ttest_expect_success \"stdin symref-update HEAD logs target's new value ($action, $order)\" '\n+\t\t\ttest_when_finished \"rm -rf repo branch-cmd head-cmd\" &&\n+\t\t\tgit init repo &&\n+\t\t\ttest_commit -C repo one &&\n+\t\t\ttest_commit -C repo two &&\n+\t\t\tnew=$(git -C repo rev-parse two) &&\n+\t\t\tif test \"$action\" = update\n+\t\t\tthen\n+\t\t\t\tgit -C repo branch topic one\n+\t\t\tfi &&\n+\t\t\techo \"$action refs/heads/topic $new\" >branch-cmd &&\n+\t\t\t{\n+\t\t\t\techo \"option no-deref\" &&\n+\t\t\t\techo \"symref-update HEAD refs/heads/topic\"\n+\t\t\t} >head-cmd &&\n+\t\t\tif test \"$order\" = branch-first\n+\t\t\tthen\n+\t\t\t\tcat branch-cmd head-cmd >stdin\n+\t\t\telse\n+\t\t\t\tcat head-cmd branch-cmd >stdin\n+\t\t\tfi &&\n+\t\t\tgit -C repo update-ref -m \"move HEAD\" --stdin <stdin &&\n+\t\t\techo \"move HEAD\" >expect &&\n+\t\t\tgit -C repo reflog -1 --format=%gs HEAD >actual &&\n+\t\t\ttest_cmp expect actual &&\n+\t\t\techo $new >expect &&\n+\t\t\tgit -C repo reflog -1 --format=%H HEAD >actual &&\n+\t\t\ttest_cmp expect actual\n+\t\t'\n+\tdone\n+done\n+\n test_expect_success 'stdin update symref works flag --no-deref' '\n \tgit symbolic-ref TESTSYMREFONE $b &&\n \tgit symbolic-ref TESTSYMREFTWO $b &&\n-- \n2.56.0\n\n\n"},{"id":"558291","messageId":"CAOLa=ZQ559k_jrSrubdRcZofxLT+Fj=fi_e6BY=e2KJX_OJRag@mail.gmail.com","threadId":"66806","inReplyTo":"20261110133457.43700-1-inoue.tsukasa.4@gmail.com","subject":"Re: [PATCH] refs: fix reflog of a symref whose target is in the same transaction","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2026-11-12T13:37:58Z","receivedAt":"2026-11-12T13:37:58Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"tsukasa inoue <inoue.tsukasa.4@gmail.com> writes:\n\n> When a transaction updates a ref and also points a symref at it, for\n> example\n>\n>     update refs/heads/y <new>\n>     symref-update HEAD refs/heads/y\n>\n> the reflog entry for HEAD should record <new>. Both backends get the\n> value for that entry by resolving the target with\n> refs_resolve_ref_unsafe(), which reads the refs as they are before the\n> transaction is applied.\n>\n> The reftable backend writes all updates into one table at the end, so\n> it always sees the old value of y. When y is created by the same\n> transaction it sees no y at all, takes the symref as dangling, and\n> writes no reflog entry. The files backend commits the updates one at a\n> time in the order they were queued, so it gets it right only when the\n> update of y comes before the one of HEAD.\n>\n> Add ref_transaction_resolve_new_oid(), which takes the new value from\n> the transaction when it updates the target, following symref updates\n> within it, and reads the ref store otherwise. Use it in both backends.\n>\n> Signed-off-by: tsukasa inoue <inoue.tsukasa.4@gmail.com>\n> ---\n>\n> Notes:\n>     This follows up on the report at\n>     https://lore.kernel.org/git/CAP8tz2L3Dfhd+ec4+SFkZgXmzQnBg+3Y0REAV+1Hf5X415qnfw@mail.gmail.com/\n>\n>     As discussed with Karthik there, this only moves the lookup of the\n>     target's new value to refs.c. Moving the rest of the reflog logic out\n>     of the backends, and resolving conflicting updates within a single\n>     transaction, are left for later.\n>\n>  refs.c                  | 47 +++++++++++++++++++++++++++++++++++++++++\n>  refs/files-backend.c    | 10 +++++----\n>  refs/refs-internal.h    | 12 +++++++++++\n>  refs/reftable-backend.c |  9 ++++++--\n>  t/t1400-update-ref.sh   | 36 +++++++++++++++++++++++++++++++\n>  5 files changed, 108 insertions(+), 6 deletions(-)\n>\n> diff --git a/refs.c b/refs.c\n> index 951db56113..3a5c225fc9 100644\n> --- a/refs.c\n> +++ b/refs.c\n> @@ -1388,6 +1388,53 @@ struct ref_update *ref_transaction_add_update(\n>  \treturn update;\n>  }\n>\n> +static struct ref_update *find_ref_update(struct ref_transaction *transaction,\n> +\t\t\t\t\t  const char *refname)\n> +{\n> +\tsize_t i;\n> +\n> +\tfor (i = 0; i < transaction->nr; i++) {\n> +\t\tstruct ref_update *update = transaction->updates[i];\n> +\n> +\t\tif (!(update->flags & REF_LOG_ONLY) &&\n> +\t\t    !strcmp(update->refname, refname))\n> +\t\t\treturn update;\n> +\t}\n> +\n> +\treturn NULL;\n> +}\n\nCan't we use `string_list_lookup(&transaction->refnames, refname)`,\nwhich is a sorted list and uses a binary search.\n\n> +\n> +int ref_transaction_resolve_new_oid(struct ref_transaction *transaction,\n> +\t\t\t\t    const char *refname,\n> +\t\t\t\t    struct object_id *oid)\n> +{\n> +\tint depth;\n> +\n> +\tfor (depth = 0; depth < SYMREF_MAXDEPTH; depth++) {\n> +\t\tstruct ref_update *update;\n> +\n> +\t\tupdate = find_ref_update(transaction, refname);\n> +\t\tif (!update)\n> +\t\t\tbreak;\n> +\n> +\t\tif (!(update->flags & REF_HAVE_NEW) || update->rejection_err)\n> +\t\t\tbreak;\n> +\n\nSo `update->rejection_err` would be set only if the reference update\nfailed. This would only happen if it was applied before the reflog\nupdate. What about reference updates which fail after the reflog update?\nWould it make sense to schedule all reflog updates to the end, this\nwould mean we know the status of all reference updates by then.\n\n> +\t\tif (update->new_target) {\n> +\t\t\trefname = update->new_target;\n> +\t\t\tcontinue;\n> +\t\t}\n> +\n> +\t\tif (is_null_oid(&update->new_oid))\n> +\t\t\treturn 0;\n> +\t\toidcpy(oid, &update->new_oid);\n> +\t\treturn 1;\n> +\t}\n> +\n> +\treturn !!refs_resolve_ref_unsafe(transaction->ref_store, refname,\n> +\t\t\t\t\t RESOLVE_REF_READING, oid, NULL);\n> +}\n> +\n>  static int transaction_refname_valid(const char *refname,\n>  \t\t\t\t     const struct object_id *new_oid,\n>  \t\t\t\t     unsigned int flags, struct strbuf *err)\n>  \t\t\t\t\t * symref updates. It would be nice to capture this as\n\n[snip]\n\n> diff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh\n> index 1a164c96f9..19f0987d2f 100755\n> --- a/t/t1400-update-ref.sh\n> +++ b/t/t1400-update-ref.sh\n> @@ -853,6 +853,42 @@ test_expect_success 'stdin delete symref works option no-deref' '\n>  \ttest_cmp expect actual\n>  '\n>\n> +for action in update create\n> +do\n> +\tfor order in branch-first head-first\n> +\tdo\n> +\t\ttest_expect_success \"stdin symref-update HEAD logs target's new value ($action, $order)\" '\n> +\t\t\ttest_when_finished \"rm -rf repo branch-cmd head-cmd\" &&\n\nWe could use a subshell, cd into it and then we don't have to\n1. Do `-C repo` everywhere\n2. Cleanup 'branch-cmd' and 'head-cmd'\n\n> +\t\t\tgit init repo &&\n> +\t\t\ttest_commit -C repo one &&\n> +\t\t\ttest_commit -C repo two &&\n> +\t\t\tnew=$(git -C repo rev-parse two) &&\n> +\t\t\tif test \"$action\" = update\n> +\t\t\tthen\n> +\t\t\t\tgit -C repo branch topic one\n> +\t\t\tfi &&\n> +\t\t\techo \"$action refs/heads/topic $new\" >branch-cmd &&\n> +\t\t\t{\n> +\t\t\t\techo \"option no-deref\" &&\n> +\t\t\t\techo \"symref-update HEAD refs/heads/topic\"\n> +\t\t\t} >head-cmd &&\n> +\t\t\tif test \"$order\" = branch-first\n> +\t\t\tthen\n> +\t\t\t\tcat branch-cmd head-cmd >stdin\n> +\t\t\telse\n> +\t\t\t\tcat head-cmd branch-cmd >stdin\n> +\t\t\tfi &&\n> +\t\t\tgit -C repo update-ref -m \"move HEAD\" --stdin <stdin &&\n> +\t\t\techo \"move HEAD\" >expect &&\n> +\t\t\tgit -C repo reflog -1 --format=%gs HEAD >actual &&\n> +\t\t\ttest_cmp expect actual &&\n> +\t\t\techo $new >expect &&\n> +\t\t\tgit -C repo reflog -1 --format=%H HEAD >actual &&\n> +\t\t\ttest_cmp expect actual\n\nNit: A few newlines to separate out blocks like the first few lines are\nsetup and so on, could help readability :)\n\n> +\t\t'\n> +\tdone\n> +done\n> +\n\nWe should also probably add tests which use\n`REF_TRANSACTION_ALLOW_FAILURE` and check if a given reference\nupdate fails and do we apply the new/old value for the reflog then.\n\n>  test_expect_success 'stdin update symref works flag --no-deref' '\n>  \tgit symbolic-ref TESTSYMREFONE $b &&\n>  \tgit symbolic-ref TESTSYMREFTWO $b &&\n> --\n> 2.56.0\n\nThanks for the patch. Some small comments from me.\n"},{"id":"558372","messageId":"CAP8tz2+0+N1HLKc3y9hB-Ncaah7ni8ykrm5mB7N9Ji+Gc7zNaw@mail.gmail.com","threadId":"66806","inReplyTo":"CAOLa=ZQ559k_jrSrubdRcZofxLT+Fj=fi_e6BY=e2KJX_OJRag@mail.gmail.com","subject":"Re: [PATCH] refs: fix reflog of a symref whose target is in the same transaction","fromName":"井上司","fromEmail":"inoue.tsukasa.4@gmail.com","sentAt":"2026-11-13T07:21:41Z","receivedAt":"2026-11-13T07:21:41Z","isPatch":true,"sender":{"key":"inoue.tsukasa.4@gmail.com","avatar":null},"body":"Karthik Nayak <karthik.188@gmail.com> writes:\n\n> Can't we use `string_list_lookup(&transaction->refnames, refname)`,\n> which is a sorted list and uses a binary search.\n\nI tried that first, but ref_transaction_prepare() sorts refnames only at\nits start. The backends then add updates while preparing, for example\nsplit_symref_update() for an update through HEAD, and\nref_transaction_add_update() appends those to refnames. A binary search\ncan then miss a target added that way. With HEAD pointing at\nrefs/heads/a:\n\n    update HEAD <new>\n    option no-deref\n    symref-update refs/heads/s refs/heads/a\n\nrefnames becomes [HEAD, refs/heads/s, refs/heads/a], and a binary search\nfor refs/heads/a does not find it. I checked this on both backends: with\nstring_list_lookup() the reflog of s records the old value of a, with\nthe linear scan it records <new>. Keeping refnames sorted with\nstring_list_insert() would drop the duplicates that\nref_update_reject_duplicates() is meant to catch, so I left the scan in.\nIt only runs for symref updates that write a reflog. I'll add this case\nas a test in v2.\n\n> So `update->rejection_err` would be set only if the reference update\n> failed. This would only happen if it was applied before the reflog\n> update. What about reference updates which fail after the reflog update?\n\nAs far as I can see, the backends record all rejections while preparing\n(in files_transaction_prepare(), reftable_be_transaction_prepare() and\nthe packed backend's write_with_updates()), and write the reflogs when\nfinishing. When the helper runs, the rejected updates are already\nmarked. A failure while finishing fails the whole transaction rather\nthan rejecting a single update.\n\n> We could use a subshell, cd into it and then we don't have to\n> 1. Do `-C repo` everywhere\n> 2. Cleanup 'branch-cmd' and 'head-cmd'\n\nWill do, and I'll add the blank lines.\n\n> We should also probably add tests which use\n> `REF_TRANSACTION_ALLOW_FAILURE` and check if a given reference\n> update fails and do we apply the new/old value for the reflog then.\n\nGood idea. I'll add a --batch-updates test where the update of the\nbranch is rejected and HEAD is pointed at it, and check that HEAD's\nreflog records the branch's current value.\n\nThanks for the review!\nTsukasa\n\n2026年11月12日(木) 22:38 Karthik Nayak <karthik.188@gmail.com>:\n>\n> tsukasa inoue <inoue.tsukasa.4@gmail.com> writes:\n>\n> > When a transaction updates a ref and also points a symref at it, for\n> > example\n> >\n> >     update refs/heads/y <new>\n> >     symref-update HEAD refs/heads/y\n> >\n> > the reflog entry for HEAD should record <new>. Both backends get the\n> > value for that entry by resolving the target with\n> > refs_resolve_ref_unsafe(), which reads the refs as they are before the\n> > transaction is applied.\n> >\n> > The reftable backend writes all updates into one table at the end, so\n> > it always sees the old value of y. When y is created by the same\n> > transaction it sees no y at all, takes the symref as dangling, and\n> > writes no reflog entry. The files backend commits the updates one at a\n> > time in the order they were queued, so it gets it right only when the\n> > update of y comes before the one of HEAD.\n> >\n> > Add ref_transaction_resolve_new_oid(), which takes the new value from\n> > the transaction when it updates the target, following symref updates\n> > within it, and reads the ref store otherwise. Use it in both backends.\n> >\n> > Signed-off-by: tsukasa inoue <inoue.tsukasa.4@gmail.com>\n> > ---\n> >\n> > Notes:\n> >     This follows up on the report at\n> >     https://lore.kernel.org/git/CAP8tz2L3Dfhd+ec4+SFkZgXmzQnBg+3Y0REAV+1Hf5X415qnfw@mail.gmail.com/\n> >\n> >     As discussed with Karthik there, this only moves the lookup of the\n> >     target's new value to refs.c. Moving the rest of the reflog logic out\n> >     of the backends, and resolving conflicting updates within a single\n> >     transaction, are left for later.\n> >\n> >  refs.c                  | 47 +++++++++++++++++++++++++++++++++++++++++\n> >  refs/files-backend.c    | 10 +++++----\n> >  refs/refs-internal.h    | 12 +++++++++++\n> >  refs/reftable-backend.c |  9 ++++++--\n> >  t/t1400-update-ref.sh   | 36 +++++++++++++++++++++++++++++++\n> >  5 files changed, 108 insertions(+), 6 deletions(-)\n> >\n> > diff --git a/refs.c b/refs.c\n> > index 951db56113..3a5c225fc9 100644\n> > --- a/refs.c\n> > +++ b/refs.c\n> > @@ -1388,6 +1388,53 @@ struct ref_update *ref_transaction_add_update(\n> >       return update;\n> >  }\n> >\n> > +static struct ref_update *find_ref_update(struct ref_transaction *transaction,\n> > +                                       const char *refname)\n> > +{\n> > +     size_t i;\n> > +\n> > +     for (i = 0; i < transaction->nr; i++) {\n> > +             struct ref_update *update = transaction->updates[i];\n> > +\n> > +             if (!(update->flags & REF_LOG_ONLY) &&\n> > +                 !strcmp(update->refname, refname))\n> > +                     return update;\n> > +     }\n> > +\n> > +     return NULL;\n> > +}\n>\n> Can't we use `string_list_lookup(&transaction->refnames, refname)`,\n> which is a sorted list and uses a binary search.\n>\n> > +\n> > +int ref_transaction_resolve_new_oid(struct ref_transaction *transaction,\n> > +                                 const char *refname,\n> > +                                 struct object_id *oid)\n> > +{\n> > +     int depth;\n> > +\n> > +     for (depth = 0; depth < SYMREF_MAXDEPTH; depth++) {\n> > +             struct ref_update *update;\n> > +\n> > +             update = find_ref_update(transaction, refname);\n> > +             if (!update)\n> > +                     break;\n> > +\n> > +             if (!(update->flags & REF_HAVE_NEW) || update->rejection_err)\n> > +                     break;\n> > +\n>\n> So `update->rejection_err` would be set only if the reference update\n> failed. This would only happen if it was applied before the reflog\n> update. What about reference updates which fail after the reflog update?\n> Would it make sense to schedule all reflog updates to the end, this\n> would mean we know the status of all reference updates by then.\n>\n> > +             if (update->new_target) {\n> > +                     refname = update->new_target;\n> > +                     continue;\n> > +             }\n> > +\n> > +             if (is_null_oid(&update->new_oid))\n> > +                     return 0;\n> > +             oidcpy(oid, &update->new_oid);\n> > +             return 1;\n> > +     }\n> > +\n> > +     return !!refs_resolve_ref_unsafe(transaction->ref_store, refname,\n> > +                                      RESOLVE_REF_READING, oid, NULL);\n> > +}\n> > +\n> >  static int transaction_refname_valid(const char *refname,\n> >                                    const struct object_id *new_oid,\n> >                                    unsigned int flags, struct strbuf *err)\n> >                                        * symref updates. It would be nice to capture this as\n>\n> [snip]\n>\n> > diff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh\n> > index 1a164c96f9..19f0987d2f 100755\n> > --- a/t/t1400-update-ref.sh\n> > +++ b/t/t1400-update-ref.sh\n> > @@ -853,6 +853,42 @@ test_expect_success 'stdin delete symref works option no-deref' '\n> >       test_cmp expect actual\n> >  '\n> >\n> > +for action in update create\n> > +do\n> > +     for order in branch-first head-first\n> > +     do\n> > +             test_expect_success \"stdin symref-update HEAD logs target's new value ($action, $order)\" '\n> > +                     test_when_finished \"rm -rf repo branch-cmd head-cmd\" &&\n>\n> We could use a subshell, cd into it and then we don't have to\n> 1. Do `-C repo` everywhere\n> 2. Cleanup 'branch-cmd' and 'head-cmd'\n>\n> > +                     git init repo &&\n> > +                     test_commit -C repo one &&\n> > +                     test_commit -C repo two &&\n> > +                     new=$(git -C repo rev-parse two) &&\n> > +                     if test \"$action\" = update\n> > +                     then\n> > +                             git -C repo branch topic one\n> > +                     fi &&\n> > +                     echo \"$action refs/heads/topic $new\" >branch-cmd &&\n> > +                     {\n> > +                             echo \"option no-deref\" &&\n> > +                             echo \"symref-update HEAD refs/heads/topic\"\n> > +                     } >head-cmd &&\n> > +                     if test \"$order\" = branch-first\n> > +                     then\n> > +                             cat branch-cmd head-cmd >stdin\n> > +                     else\n> > +                             cat head-cmd branch-cmd >stdin\n> > +                     fi &&\n> > +                     git -C repo update-ref -m \"move HEAD\" --stdin <stdin &&\n> > +                     echo \"move HEAD\" >expect &&\n> > +                     git -C repo reflog -1 --format=%gs HEAD >actual &&\n> > +                     test_cmp expect actual &&\n> > +                     echo $new >expect &&\n> > +                     git -C repo reflog -1 --format=%H HEAD >actual &&\n> > +                     test_cmp expect actual\n>\n> Nit: A few newlines to separate out blocks like the first few lines are\n> setup and so on, could help readability :)\n>\n> > +             '\n> > +     done\n> > +done\n> > +\n>\n> We should also probably add tests which use\n> `REF_TRANSACTION_ALLOW_FAILURE` and check if a given reference\n> update fails and do we apply the new/old value for the reflog then.\n>\n> >  test_expect_success 'stdin update symref works flag --no-deref' '\n> >       git symbolic-ref TESTSYMREFONE $b &&\n> >       git symbolic-ref TESTSYMREFTWO $b &&\n> > --\n> > 2.56.0\n>\n> Thanks for the patch. Some small comments from me.\n\n"},{"id":"558600","messageId":"20261117095615.70423-1-inoue.tsukasa.4@gmail.com","threadId":"66806","inReplyTo":"20261110133457.43700-1-inoue.tsukasa.4@gmail.com","subject":"[PATCH v2] refs: fix reflog of a symref whose target is in the same transaction","fromName":"tsukasa inoue","fromEmail":"inoue.tsukasa.4@gmail.com","sentAt":"2026-11-17T09:56:15Z","receivedAt":"2026-11-17T09:56:15Z","isPatch":true,"sender":{"key":"inoue.tsukasa.4@gmail.com","avatar":null},"body":"When a transaction updates a ref and also points a symref at it, for\nexample\n\n    update refs/heads/y <new>\n    symref-update HEAD refs/heads/y\n\nthe reflog entry for HEAD should record <new>. Both backends get the\nvalue for that entry by resolving the target with\nrefs_resolve_ref_unsafe(), which reads the refs as they are before the\ntransaction is applied.\n\nThe reftable backend writes all updates into one table at the end, so\nit always sees the old value of y. When y is created by the same\ntransaction it sees no y at all, treats the symref as dangling, and\nwrites no reflog entry. The files backend commits the updates one at a\ntime in the order they were queued, so it gets it right only when the\nupdate of y comes before the update of HEAD.\n\nAdd ref_transaction_resolve_new_oid(), which takes the new value from\nthe transaction when it updates the target, following symref updates\nwithin it, and reads the ref store otherwise. Use it in both backends.\n\nSigned-off-by: tsukasa inoue <inoue.tsukasa.4@gmail.com>\n---\n\nNotes:\n    Changes since v1:\n    \n     - Run the tests in a subshell inside the test repository, with blank\n       lines between the steps (Karthik).\n     - Add a test where the symref target is updated through HEAD. That\n       update is added to refnames after it is sorted, which is why the\n       helper scans transaction->updates instead of using\n       string_list_lookup().\n     - Add a --batch-updates test where the update of the target is\n       rejected.\n\n refs.c                  |  46 ++++++++++++++++++\n refs/files-backend.c    |  10 ++--\n refs/refs-internal.h    |  11 +++++\n refs/reftable-backend.c |   9 +++-\n t/t1400-update-ref.sh   | 101 ++++++++++++++++++++++++++++++++++++++++\n 5 files changed, 171 insertions(+), 6 deletions(-)\n\ndiff --git a/refs.c b/refs.c\nindex 951db56113..d7bed9d7bb 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -1388,6 +1388,52 @@ struct ref_update *ref_transaction_add_update(\n \treturn update;\n }\n \n+static struct ref_update *find_ref_update(struct ref_transaction *transaction,\n+\t\t\t\t\t  const char *refname)\n+{\n+\tsize_t i;\n+\n+\tfor (i = 0; i < transaction->nr; i++) {\n+\t\tstruct ref_update *update = transaction->updates[i];\n+\n+\t\tif (!(update->flags & REF_LOG_ONLY) &&\n+\t\t    !strcmp(update->refname, refname))\n+\t\t\treturn update;\n+\t}\n+\n+\treturn NULL;\n+}\n+\n+int ref_transaction_resolve_new_oid(struct ref_transaction *transaction,\n+\t\t\t\t    const char *refname,\n+\t\t\t\t    struct object_id *oid)\n+{\n+\tint depth;\n+\n+\tfor (depth = 0; depth < SYMREF_MAXDEPTH; depth++) {\n+\t\tstruct ref_update *update = find_ref_update(transaction, refname);\n+\n+\t\tif (!update)\n+\t\t\tbreak;\n+\n+\t\tif (!(update->flags & REF_HAVE_NEW) || update->rejection_err)\n+\t\t\tbreak;\n+\n+\t\tif (update->new_target) {\n+\t\t\trefname = update->new_target;\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tif (is_null_oid(&update->new_oid))\n+\t\t\treturn 0;\n+\t\toidcpy(oid, &update->new_oid);\n+\t\treturn 1;\n+\t}\n+\n+\treturn !!refs_resolve_ref_unsafe(transaction->ref_store, refname,\n+\t\t\t\t\t RESOLVE_REF_READING, oid, NULL);\n+}\n+\n static int transaction_refname_valid(const char *refname,\n \t\t\t\t     const struct object_id *new_oid,\n \t\t\t\t     unsigned int flags, struct strbuf *err)\ndiff --git a/refs/files-backend.c b/refs/files-backend.c\nindex 87d04557a5..d8878f4df3 100644\n--- a/refs/files-backend.c\n+++ b/refs/files-backend.c\n@@ -3156,6 +3156,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,\n }\n \n static int parse_and_write_reflog(struct files_ref_store *refs,\n+\t\t\t\t  struct ref_transaction *transaction,\n \t\t\t\t  struct ref_update *update,\n \t\t\t\t  struct ref_lock *lock,\n \t\t\t\t  struct strbuf *err)\n@@ -3179,9 +3180,9 @@ static int parse_and_write_reflog(struct files_ref_store *refs,\n \t\t * We want to get the resolved OID for the target, to ensure\n \t\t * that the correct value is added to the reflog.\n \t\t */\n-\t\tif (!refs_resolve_ref_unsafe(&refs->base, update->new_target,\n-\t\t\t\t\t     RESOLVE_REF_READING,\n-\t\t\t\t\t     &update->new_oid, NULL)) {\n+\t\tif (!ref_transaction_resolve_new_oid(transaction,\n+\t\t\t\t\t\t     update->new_target,\n+\t\t\t\t\t\t     &update->new_oid)) {\n \t\t\t/*\n \t\t\t * TODO: currently we skip creating reflogs for dangling\n \t\t\t * symref updates. It would be nice to capture this as\n@@ -3377,7 +3378,8 @@ static int files_transaction_finish(struct ref_store *ref_store,\n \n \t\tif (update->flags & REF_NEEDS_COMMIT ||\n \t\t    update->flags & REF_LOG_ONLY) {\n-\t\t\tif (parse_and_write_reflog(refs, update, lock, err)) {\n+\t\t\tif (parse_and_write_reflog(refs, transaction, update,\n+\t\t\t\t\t\t   lock, err)) {\n \t\t\t\tret = REF_TRANSACTION_ERROR_GENERIC;\n \t\t\t\tgoto cleanup;\n \t\t\t}\ndiff --git a/refs/refs-internal.h b/refs/refs-internal.h\nindex c3ac7b556f..d4e8834627 100644\n--- a/refs/refs-internal.h\n+++ b/refs/refs-internal.h\n@@ -187,6 +187,17 @@ struct ref_update *ref_transaction_add_update(\n \t\tconst char *committer_info,\n \t\tconst char *msg);\n \n+/*\n+ * Resolve `refname` to the object ID it will have once `transaction` is\n+ * committed. If the transaction updates `refname`, its new value is used,\n+ * following symref updates in the transaction; otherwise the ref store is\n+ * read. Return 1 and fill `oid` if it resolves to an object, 0 if it is\n+ * missing or deleted by the transaction.\n+ */\n+int ref_transaction_resolve_new_oid(struct ref_transaction *transaction,\n+\t\t\t\t    const char *refname,\n+\t\t\t\t    struct object_id *oid);\n+\n /*\n  * Transaction states.\n  *\ndiff --git a/refs/reftable-backend.c b/refs/reftable-backend.c\nindex d0de066355..46142a4f73 100644\n--- a/refs/reftable-backend.c\n+++ b/refs/reftable-backend.c\n@@ -940,6 +940,7 @@ struct reftable_transaction_update {\n \n struct write_transaction_table_arg {\n \tstruct reftable_ref_store *refs;\n+\tstruct ref_transaction *transaction;\n \tstruct reftable_backend *be;\n \tstruct reftable_addition *addition;\n \tstruct reftable_transaction_update *updates;\n@@ -950,6 +951,7 @@ struct write_transaction_table_arg {\n };\n \n struct reftable_transaction_data {\n+\tstruct ref_transaction *transaction;\n \tstruct write_transaction_table_arg *args;\n \tsize_t args_nr, args_alloc;\n };\n@@ -1014,6 +1016,7 @@ static int prepare_transaction_update(struct write_transaction_table_arg **out,\n \t\t\t   tx_data->args_alloc);\n \t\targ = &tx_data->args[tx_data->args_nr++];\n \t\targ->refs = refs;\n+\t\targ->transaction = tx_data->transaction;\n \t\targ->be = be;\n \t\targ->addition = addition;\n \t\targ->updates = NULL;\n@@ -1332,6 +1335,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,\n \t\tgoto done;\n \n \ttx_data = xcalloc(1, sizeof(*tx_data));\n+\ttx_data->transaction = transaction;\n \n \t/*\n \t * Preprocess all updates. For one we check that there are no duplicate\n@@ -1557,8 +1561,9 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data\n \t\t\tint create_reflog = 1;\n \n \t\t\tif (u->new_target) {\n-\t\t\t\tif (!refs_resolve_ref_unsafe(&arg->refs->base, u->new_target,\n-\t\t\t\t\t\t\t     RESOLVE_REF_READING, &u->new_oid, NULL)) {\n+\t\t\t\tif (!ref_transaction_resolve_new_oid(arg->transaction,\n+\t\t\t\t\t\t\t\t     u->new_target,\n+\t\t\t\t\t\t\t\t     &u->new_oid)) {\n \t\t\t\t\t/*\n \t\t\t\t\t * TODO: currently we skip creating reflogs for dangling\n \t\t\t\t\t * symref updates. It would be nice to capture this as\ndiff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh\nindex 1a164c96f9..7bc4aaaae9 100755\n--- a/t/t1400-update-ref.sh\n+++ b/t/t1400-update-ref.sh\n@@ -853,6 +853,107 @@ test_expect_success 'stdin delete symref works option no-deref' '\n \ttest_cmp expect actual\n '\n \n+for action in update create\n+do\n+\tfor order in branch-first head-first\n+\tdo\n+\t\ttest_expect_success \"stdin symref-update HEAD logs target's new value ($action, $order)\" '\n+\t\t\ttest_when_finished \"rm -rf repo\" &&\n+\t\t\tgit init repo &&\n+\t\t\t(\n+\t\t\t\tcd repo &&\n+\t\t\t\ttest_commit one &&\n+\t\t\t\ttest_commit two &&\n+\t\t\t\tnew=$(git rev-parse two) &&\n+\t\t\t\tif test \"$action\" = update\n+\t\t\t\tthen\n+\t\t\t\t\tgit branch topic one\n+\t\t\t\tfi &&\n+\n+\t\t\t\techo \"$action refs/heads/topic $new\" >branch-cmd &&\n+\t\t\t\t{\n+\t\t\t\t\techo \"option no-deref\" &&\n+\t\t\t\t\techo \"symref-update HEAD refs/heads/topic\"\n+\t\t\t\t} >head-cmd &&\n+\t\t\t\tif test \"$order\" = branch-first\n+\t\t\t\tthen\n+\t\t\t\t\tcat branch-cmd head-cmd >stdin\n+\t\t\t\telse\n+\t\t\t\t\tcat head-cmd branch-cmd >stdin\n+\t\t\t\tfi &&\n+\t\t\t\tgit update-ref -m \"move HEAD\" --stdin <stdin &&\n+\n+\t\t\t\techo \"move HEAD\" >expect &&\n+\t\t\t\tgit reflog -1 --format=%gs HEAD >actual &&\n+\t\t\t\ttest_cmp expect actual &&\n+\t\t\t\techo $new >expect &&\n+\t\t\t\tgit reflog -1 --format=%H HEAD >actual &&\n+\t\t\t\ttest_cmp expect actual\n+\t\t\t)\n+\t\t'\n+\tdone\n+done\n+\n+test_expect_success 'stdin symref-update logs new value of target updated via HEAD' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init -b a repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit one &&\n+\t\ttest_commit two &&\n+\t\tnew=$(git rev-parse two) &&\n+\t\tgit update-ref refs/heads/a one &&\n+\n+\t\tcat >stdin <<-EOF &&\n+\t\tupdate HEAD $new\n+\t\toption no-deref\n+\t\tsymref-update refs/heads/s refs/heads/a\n+\t\tEOF\n+\t\tgit update-ref -m \"move s\" --stdin <stdin &&\n+\n+\t\techo \"move s\" >expect &&\n+\t\tgit reflog -1 --format=%gs refs/heads/s >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\techo $new >expect &&\n+\t\tgit reflog -1 --format=%H refs/heads/s >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'stdin batch-updates symref-update HEAD does not log rejected target value' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit one &&\n+\t\ttest_commit two &&\n+\t\told=$(git rev-parse one) &&\n+\t\tnew=$(git rev-parse two) &&\n+\t\tgit branch y one &&\n+\n+\t\tcat >stdin <<-EOF &&\n+\t\tupdate refs/heads/y $new $new\n+\t\toption no-deref\n+\t\tsymref-update HEAD refs/heads/y\n+\t\tEOF\n+\t\tgit update-ref -m \"move HEAD\" --batch-updates --stdin <stdin >stdout &&\n+\n+\t\ttest_grep \"rejected refs/heads/y\" stdout &&\n+\t\techo $old >expect &&\n+\t\tgit rev-parse refs/heads/y >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\techo refs/heads/y >expect &&\n+\t\tgit symbolic-ref HEAD >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\techo \"move HEAD\" >expect &&\n+\t\tgit reflog -1 --format=%gs HEAD >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\techo $old >expect &&\n+\t\tgit reflog -1 --format=%H HEAD >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n test_expect_success 'stdin update symref works flag --no-deref' '\n \tgit symbolic-ref TESTSYMREFONE $b &&\n \tgit symbolic-ref TESTSYMREFTWO $b &&\n\nRange-diff against v1:\n1:  337abe33a3 ! 1:  acd2808b3a refs: fix reflog of a symref whose target is in the same transaction\n    @@ Commit message\n     \n         The reftable backend writes all updates into one table at the end, so\n         it always sees the old value of y. When y is created by the same\n    -    transaction it sees no y at all, takes the symref as dangling, and\n    +    transaction it sees no y at all, treats the symref as dangling, and\n         writes no reflog entry. The files backend commits the updates one at a\n         time in the order they were queued, so it gets it right only when the\n    -    update of y comes before the one of HEAD.\n    +    update of y comes before the update of HEAD.\n     \n         Add ref_transaction_resolve_new_oid(), which takes the new value from\n         the transaction when it updates the target, following symref updates\n    @@ Commit message\n     \n     \n      ## Notes ##\n    -    This follows up on the report at\n    -    https://lore.kernel.org/git/CAP8tz2L3Dfhd+ec4+SFkZgXmzQnBg+3Y0REAV+1Hf5X415qnfw@mail.gmail.com/\n    +    Changes since v1:\n     \n    -    As discussed with Karthik there, this only moves the lookup of the\n    -    target's new value to refs.c. Moving the rest of the reflog logic out\n    -    of the backends, and resolving conflicting updates within a single\n    -    transaction, are left for later.\n    +     - Run the tests in a subshell inside the test repository, with blank\n    +       lines between the steps (Karthik).\n    +     - Add a test where the symref target is updated through HEAD. That\n    +       update is added to refnames after it is sorted, which is why the\n    +       helper scans transaction->updates instead of using\n    +       string_list_lookup().\n    +     - Add a --batch-updates test where the update of the target is\n    +       rejected.\n     \n      ## refs.c ##\n     @@ refs.c: struct ref_update *ref_transaction_add_update(\n    @@ refs.c: struct ref_update *ref_transaction_add_update(\n     +\tint depth;\n     +\n     +\tfor (depth = 0; depth < SYMREF_MAXDEPTH; depth++) {\n    -+\t\tstruct ref_update *update;\n    ++\t\tstruct ref_update *update = find_ref_update(transaction, refname);\n     +\n    -+\t\tupdate = find_ref_update(transaction, refname);\n     +\t\tif (!update)\n     +\t\t\tbreak;\n     +\n    @@ refs/refs-internal.h: struct ref_update *ref_transaction_add_update(\n      \n     +/*\n     + * Resolve `refname` to the object ID it will have once `transaction` is\n    -+ * committed: if the transaction updates `refname`, use the new value of\n    -+ * that update (following `new_target` of symref updates in the\n    -+ * transaction), otherwise read it from the ref store. Return 1 and fill\n    -+ * `oid` if it resolves to an object, 0 if it would be dangling (missing,\n    -+ * or deleted by the transaction). Meant to be used while committing.\n    ++ * committed. If the transaction updates `refname`, its new value is used,\n    ++ * following symref updates in the transaction; otherwise the ref store is\n    ++ * read. Return 1 and fill `oid` if it resolves to an object, 0 if it is\n    ++ * missing or deleted by the transaction.\n     + */\n     +int ref_transaction_resolve_new_oid(struct ref_transaction *transaction,\n     +\t\t\t\t    const char *refname,\n    @@ t/t1400-update-ref.sh: test_expect_success 'stdin delete symref works option no-\n     +\tfor order in branch-first head-first\n     +\tdo\n     +\t\ttest_expect_success \"stdin symref-update HEAD logs target's new value ($action, $order)\" '\n    -+\t\t\ttest_when_finished \"rm -rf repo branch-cmd head-cmd\" &&\n    ++\t\t\ttest_when_finished \"rm -rf repo\" &&\n     +\t\t\tgit init repo &&\n    -+\t\t\ttest_commit -C repo one &&\n    -+\t\t\ttest_commit -C repo two &&\n    -+\t\t\tnew=$(git -C repo rev-parse two) &&\n    -+\t\t\tif test \"$action\" = update\n    -+\t\t\tthen\n    -+\t\t\t\tgit -C repo branch topic one\n    -+\t\t\tfi &&\n    -+\t\t\techo \"$action refs/heads/topic $new\" >branch-cmd &&\n    -+\t\t\t{\n    -+\t\t\t\techo \"option no-deref\" &&\n    -+\t\t\t\techo \"symref-update HEAD refs/heads/topic\"\n    -+\t\t\t} >head-cmd &&\n    -+\t\t\tif test \"$order\" = branch-first\n    -+\t\t\tthen\n    -+\t\t\t\tcat branch-cmd head-cmd >stdin\n    -+\t\t\telse\n    -+\t\t\t\tcat head-cmd branch-cmd >stdin\n    -+\t\t\tfi &&\n    -+\t\t\tgit -C repo update-ref -m \"move HEAD\" --stdin <stdin &&\n    -+\t\t\techo \"move HEAD\" >expect &&\n    -+\t\t\tgit -C repo reflog -1 --format=%gs HEAD >actual &&\n    -+\t\t\ttest_cmp expect actual &&\n    -+\t\t\techo $new >expect &&\n    -+\t\t\tgit -C repo reflog -1 --format=%H HEAD >actual &&\n    -+\t\t\ttest_cmp expect actual\n    ++\t\t\t(\n    ++\t\t\t\tcd repo &&\n    ++\t\t\t\ttest_commit one &&\n    ++\t\t\t\ttest_commit two &&\n    ++\t\t\t\tnew=$(git rev-parse two) &&\n    ++\t\t\t\tif test \"$action\" = update\n    ++\t\t\t\tthen\n    ++\t\t\t\t\tgit branch topic one\n    ++\t\t\t\tfi &&\n    ++\n    ++\t\t\t\techo \"$action refs/heads/topic $new\" >branch-cmd &&\n    ++\t\t\t\t{\n    ++\t\t\t\t\techo \"option no-deref\" &&\n    ++\t\t\t\t\techo \"symref-update HEAD refs/heads/topic\"\n    ++\t\t\t\t} >head-cmd &&\n    ++\t\t\t\tif test \"$order\" = branch-first\n    ++\t\t\t\tthen\n    ++\t\t\t\t\tcat branch-cmd head-cmd >stdin\n    ++\t\t\t\telse\n    ++\t\t\t\t\tcat head-cmd branch-cmd >stdin\n    ++\t\t\t\tfi &&\n    ++\t\t\t\tgit update-ref -m \"move HEAD\" --stdin <stdin &&\n    ++\n    ++\t\t\t\techo \"move HEAD\" >expect &&\n    ++\t\t\t\tgit reflog -1 --format=%gs HEAD >actual &&\n    ++\t\t\t\ttest_cmp expect actual &&\n    ++\t\t\t\techo $new >expect &&\n    ++\t\t\t\tgit reflog -1 --format=%H HEAD >actual &&\n    ++\t\t\t\ttest_cmp expect actual\n    ++\t\t\t)\n     +\t\t'\n     +\tdone\n     +done\n    ++\n    ++test_expect_success 'stdin symref-update logs new value of target updated via HEAD' '\n    ++\ttest_when_finished \"rm -rf repo\" &&\n    ++\tgit init -b a repo &&\n    ++\t(\n    ++\t\tcd repo &&\n    ++\t\ttest_commit one &&\n    ++\t\ttest_commit two &&\n    ++\t\tnew=$(git rev-parse two) &&\n    ++\t\tgit update-ref refs/heads/a one &&\n    ++\n    ++\t\tcat >stdin <<-EOF &&\n    ++\t\tupdate HEAD $new\n    ++\t\toption no-deref\n    ++\t\tsymref-update refs/heads/s refs/heads/a\n    ++\t\tEOF\n    ++\t\tgit update-ref -m \"move s\" --stdin <stdin &&\n    ++\n    ++\t\techo \"move s\" >expect &&\n    ++\t\tgit reflog -1 --format=%gs refs/heads/s >actual &&\n    ++\t\ttest_cmp expect actual &&\n    ++\t\techo $new >expect &&\n    ++\t\tgit reflog -1 --format=%H refs/heads/s >actual &&\n    ++\t\ttest_cmp expect actual\n    ++\t)\n    ++'\n    ++\n    ++test_expect_success 'stdin batch-updates symref-update HEAD does not log rejected target value' '\n    ++\ttest_when_finished \"rm -rf repo\" &&\n    ++\tgit init repo &&\n    ++\t(\n    ++\t\tcd repo &&\n    ++\t\ttest_commit one &&\n    ++\t\ttest_commit two &&\n    ++\t\told=$(git rev-parse one) &&\n    ++\t\tnew=$(git rev-parse two) &&\n    ++\t\tgit branch y one &&\n    ++\n    ++\t\tcat >stdin <<-EOF &&\n    ++\t\tupdate refs/heads/y $new $new\n    ++\t\toption no-deref\n    ++\t\tsymref-update HEAD refs/heads/y\n    ++\t\tEOF\n    ++\t\tgit update-ref -m \"move HEAD\" --batch-updates --stdin <stdin >stdout &&\n    ++\n    ++\t\ttest_grep \"rejected refs/heads/y\" stdout &&\n    ++\t\techo $old >expect &&\n    ++\t\tgit rev-parse refs/heads/y >actual &&\n    ++\t\ttest_cmp expect actual &&\n    ++\t\techo refs/heads/y >expect &&\n    ++\t\tgit symbolic-ref HEAD >actual &&\n    ++\t\ttest_cmp expect actual &&\n    ++\t\techo \"move HEAD\" >expect &&\n    ++\t\tgit reflog -1 --format=%gs HEAD >actual &&\n    ++\t\ttest_cmp expect actual &&\n    ++\t\techo $old >expect &&\n    ++\t\tgit reflog -1 --format=%H HEAD >actual &&\n    ++\t\ttest_cmp expect actual\n    ++\t)\n    ++'\n     +\n      test_expect_success 'stdin update symref works flag --no-deref' '\n      \tgit symbolic-ref TESTSYMREFONE $b &&\n-- \n2.54.0 (Apple Git-157)\n\n\n"}]}